Re: newbie questions about while (1) in kernel mode and spinlocks

Paolo Ornati
2006-12-21T10:07:51Z

> The Linux Device Drivers book says that a spin_lock should not be
> shared between a process and an interrupt handler. The explanation is
> that the process may hold the lock, an interrupt occurs, the interrupt
> handler spins on the lock held by the process and the system freezes.
> Why should it freeze? Isn't it possible for the interrupt handler to
> re-enable interrupts as its first thing, then to spin at the lock, the
> timer interrupt to preempt the interrupt handler and to relinquish
> control to the process which in turn will finish its critical section
> and release the lock, making way for the interrupt handler to
> continue.

Iterrupt handlers are executend in the process context (on top of the
process that they interrupted).

So, if you have a proccess A that does:

Usual Kernel Code Interrupt Handler

...
spin_lock(my_lock);
...
-------interrupt-----> ...
spin_lock(my_lock); // deadlock!
...
<------ back ---------
----
spin_unlock(my_lock);

See?

If the interrupt comes in when process A is running and holding the
lock PREEMPTION can't do anything.