views:

46

answers:

1

I recently downloaded linux source from http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.34.1.tar.bz2 . I came across the below paragraph in the file called spinlocks.txt in linux-2.6.34.1\Documentation folder.

" it does mean that if you have some code that does

cli();
.. critical section ..
sti();

and another sequence that does

spin_lock_irqsave(flags);
.. critical section ..
spin_unlock_irqrestore(flags);

then they are NOT mutually exclusive, and the critical regions can happen at the same time on two different CPU's. That's fine per se, but the critical regions had better be critical for different things (ie they can't stomp on each other). "

How can they impact if some code is using cli()/sti() and other part of the same code uses spin_lock_irqsave(flags)/spin_unlock_irqrestore(flags) ?

+4  A: 

The key part here is "on two different CPUs". Some background:

  • Historically on uni-processor (UP) systems the only source of concurrency was hardware interrupts. It was enough to cli/sti around the critical section to prevent an IRQ handler from messing things up.
  • Then there was the giant lock design where the kernel would effectively run on a single CPU and only one process could be in the kernel at a time (that what the giant lock was for). Again, disabling interrupts was enough to protect kernel from itself.
  • On full SMP systems, where multiple threads could be active in the kernel at the same time and interrupts could be delivered to pretty much any CPU, it's no longer enough to only disable interrupts on single processor, or only grab a single lock. Both are required: disabling interrupts protects from IRQ handler on the same CPU, holding a lock protects from other threads entering the same critical sections on different CPU. This is exactly why spin_lock_irqsave() and spin_unlock_irqrestore() were invented.
Nikolai N Fetissov
Interesting to know the reason behind the spinlock ! Thanks . +1 for you !
S.Man
@S.Man, this is about combination of spinlocks and interrupt masks. I hope you understand that spinlocks are useful in their on right as a building block for higher-level synchronization primitives like mutexes, semaphores, monitors, etc.
Nikolai N Fetissov