Question

In the Linux Device Drivers. When it introduces spinlocks, it gives the following example:

Your driver is executing and has just taken out a lock that controls access to its device. While the lock is held, the device issues an interrupt, which causes your interrupt handler to run. The interrupt handler, before accessing the device, must also obtain the lock. Taking out a spinlock in an interrupt handler is a legitimate thing to do; that is one of the reasons that spinlock operations do not sleep. But what happens if the interrupt routine executes in the same processor as the code that took out the lock originally? While the interrupt handler is spinning, the noninterrupt code will not be able to run to release the lock. That processor will spin forever.

How to solve this issue? Using semaphores? Please help.

Was it helpful?

Solution

You use spin_lock_irqsave from non-interrupt code to disable hardware interrupts on the current core, or use any of the tools that are described in the "Alternatives to Locking" section in the same chapter.

Edit 0:

Don't remember the exact details there, but the theory is that:

  • On UP systems you don't need spin locks at all - disabling interrupts from the BH code that accesses data shared with interrupt handlers is enough.
  • On SMP systems you need a spinlock to protect from same BH code executing on other cores, and, if you deal with hardware, you also want to disable interrupts on the same core to avoid deadlocking with the IRQ routine.
  • The interrupt handler always needs to disable interrupts while holding any locks to avoid deadlocking with itself.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top