How does Kernel handle the lock in process context when an interrupt comes?

后端 未结 2 597
[愿得一人]
[愿得一人] 2021-01-25 12:05

First of all sorry for a little bit ambiguity in Question... What I want to understand is the below scenario

Suppose porcess is running, it holds one lock, Now after acq

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-25 12:43

    The Linux kernel has a few functions for acquiring spinlocks, to deal with issues like the one you're raising here. In particular, there is spin_lock_irq(), which disables interrupts (on the CPU the process is running on) and acquires the spinlock. This can be used when the code knows interrupts are enabled before the spinlock is acquired; in case the function might be called in different contexts, there is also spin_lock_irqsave(), which stashes away the current state of interrupts before disabling them, so that they can be reenabled by spin_unlock_irqrestore().

    In any case, if a lock is used in both process and interrupt context (which is a good and very common design if there is data that needs to be shared between the contexts), then process context must disable interrupts (locally on the CPU it's running on) when acquiring the spinlock to avoid deadlocks. In fact, lockdep ("CONFIG_PROVE_LOCKING") will verify this and warn if a spinlock is used in a way that is susceptible to the "interrupt while process context holds a lock" deadlock.

提交回复
热议问题