Skip to main content

Spin Lock Understanding in Basic Language

 

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:

  1. spinlock_t
  2. 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

Popular posts from this blog

Interview Prep for Embedded Engineer

Locking and Synchronization 1. On a uniprocessor machine with preemption disabled, what will happen internally when we say spin_lock()? Answer: Hog the CPU 2. Can I lock a spinlock in one CPU and unlock it another CPU? Answer: No it should be on same CPU as that preemption is disabled for that core and spinlock which locked the core need to disable the preemption and go out of that core only 3. What are the pros and cons of using per CPU variable as synchronization method? https://www.makelinux.net/ldd3/chp-8-sect-5.shtml https://0xax.gitbook.io/linux-insides/summary/concepts/linux-cpu-1 https://distkeys.com/operating%20systems/linux/2013/10/07/process-synchronization-in-linux-kernel.html 4. What is the maximum amount of time CPU can be in critical section after acquiring spinlock? https://coffeebeforearch.github.io/2020/11/07/spinlocks-6.html it could remain in critical section till we don't do spinlock_unlock or another core hit watchdog bite (300ms) . Ideally critical section ti...

Per CPU Variable

https://elixir.bootlin.com/linux/latest/source/arch/arm/kernel/process.c#L40 example:   #if  defined ( CONFIG_CURRENT_POINTER_IN_TPIDRURO ) ||  defined ( CONFIG_SMP ) DEFINE_PER_CPU ( struct task_struct * , __entry_task ); #endif