Spin lock
It's a spinning (polling) lock which is running and try to take a lock in a tight loop and checking again and again if lock is got freed up or not and once it free it will immediately take it up . Basically spin lock divided in to 4 Parts :
1. Preempt Disable : 1st operation spinlock will disable other Hardware interrupts so that what ever context is got scheduled will not preempt
2. Spin_lock_acquire: It will generate a lock context for further tracking in lock dep
3. do_spin_try_lock: Spinning in a loop (do -while) to take the lock and spin till not get the lock free.
4. do_spin_lock: Take the lock
After taking the lock it use to put a memory barrier to make sure memory will not be overwritten mistakenly
Spin unlock
use to unlock the spin lock . Divided in to 3 parts.
1. Spin_release: Mark in lock context for lock dep that we are going to release the lock for this particular lock context
2. do_spin_lock_unlock: Actual release of spin lock
3. preempt_enable: Enablement of Context switching
Types of spin lock:
- raw_spinlock_t
- bit spinlocks
On non-PREEMPT_RT kernels, these lock types are also spinning locks:
- spinlock_t
- rwlock_t
Spinning locks implicitly disable preemption and the lock / unlock functions can have suffixes which apply further protections:
- _bh() Disable / enable bottom halves (soft interrupts)
- _irq() Disable / enable interrupts
- _irqsave/restore() Save and disable / restore interrupt disabled state
Uniprocessor :
On a uniprocessor, it will either immediately acquire the lock or it will spin forever - if the lock is contended, then there will never be an opportunity for the process which currently holds the resource to give it up. Spinlocks are only useful when another process can execute while one is spinning on the lock - which means multiprocessor systems.
On uniprocessor we should always avoid spin lock as it could cause a permanent dead lock and in case we still want to take a spin lock better first try to use (spin_is_locked).
or other way to do spin_lock_bh where we only want to disable soft interrupts
Comments
Post a Comment