In this Article, we discuss the memory locking mechanism that is commonly seen in the linux kernel – Spinlock
Spinlock:
Spinlocks are kernel locking mechanism wherein if the lock is not obtained, the code attempting to get the lock just keeps spinning at that instance trying to acquire the lock. Hence the name “spin”lock. The fact that the kernel code is spinning till it acquires the lock, means that the code cannot sleep.
Some of the characteristics of a spinlock are provided below
1 |
Spinlock is a locking mechanism wherein the process spins on the lock till the lock is acquired and does not relinquish the scheduler (for its scheduler time slice) if it is waiting for the lock |
2 |
Spinlock in multi-processor systems provide locking between multiple cores in addition to multiple processes contending for the lock |
3 |
For a uniprocessor system which does not have pre-emption set, spinlock does not exist and is a “No OP”. |
4 |
For a Uniprocessor system which has pre-emption set, spinlock disables the pre-emption before the lock is acquired. |
5 |
Spinlocks due to its spinning nature can be used to lock critical sections invoked in interrupt contexts. |
6 |
Functions that can sleep should not be invoked from within a lock gained using spinlock. E.g. copy_from_user |
In the following articles, different types of spinlocks and their behaviour are explained.
Pingback: Linux Kernel – atomic operations – bit operations – sample code | Hitch Hiker's Guide to Learning