SlideShare a Scribd company logo
1 of 7
Why Synchronization:
   Critical region is a section of code that must be completely executed by the
      control path that enters it before any other control flow path can enter it.

Thus kernel locking mechanism is required to provide:
   • Synchronization
   • Data integrity

If kernel supports interleaving of control path flow, protecting the critical region becomes
mandatory as one single control flow might not finish before other flow becomes active.
Linux provides several synchronization tools in kernel space.
Synchronization tools in kernel space?
     Kernel space also has different execution paths. In case of preemptive kernel,
        interleaving becomes little more complicated and prominent.
     Asynchronous events like Interrupt will always remain in system.

Kernel preemption:

In non-preemptive kernel, when process is executing in kernel space (kernel thread or
user thread entering the kernel via system call), process was allowed to complete the
execution unless it does self-relinquish of CPU.
In preemptive kernel, process can be switched by scheduler before it finishes also, based
on scheduling criteria. This was introduced to decrease the process latency and increase
responsiveness.

Kernel space execution paths:
    Process / Thread
    Interrupt
    Bottom-half handlers – tasklets, softirqs

Kernel locking for different paths:
Process/Thread:
- Between process/thread disabling kernel preemption will provide synchronicity.
Interrupts:
- Interrupts are asynchronous and requires synchronicity between interrupts, bottom-
    half and processes to protect the critical region shared between them.
        o Disabling interrupts/interrupt line can provide synchronicity

Thus practically, disabling preemption and interrupt can provide synchronicity.

Why other tools?
> On SMP, above methods will not work.

In case of SMP need of such tools becomes mandatory, as simultaneous execution of
code flow can happen.
Spinlocks:

Although spinlock were designed for SMP systems but it is encouraged to be used in
kernel code for uni-processor also to have code portability on SMP systems.

What is spinlock?
If kernel control path finds the lock available, it will acquire the lock and proceed.
In case lock in un-available, it will spin on a tight loop forever until lock is made
available. This means, the process flow will be executing continuously and blocking the
CPU from any other code path execution.

Linux Files related to spinlock implementation:

Include/asm-arm/spinlock.h: architecture dependant code for spinlock implementation
Include/linux/spinlock.h
Kerel/spinlock.c

Spinlock Usage:

- Define spinlock variable
Spinlock_t var= SPIN_LOCK (0) / SPIN_UNLOCKED (1)
Spin_lock_init() - set the spinlock to 1 (SPIN_UNLOCKED)

- To acquire the lock:
    • Spin_lock_irq()
    • Spin_lock_irqsave
    • Spin_lock_bh
    • Spin_lock
    • Spin_lock_trylock

All spin lock calls:
    • Disable interrupts
    • Disable kernel preemption
    • Decrements the spinlock count (spinlock count can go to any large negative
        number indicating its demand) –> ( _raw_spin_lock present in architecture
        dependent)
    • Checks if count is equal to zero (if not equal to zero, keep looping).
    • In case of uni-processor, spinlock call just disables kernel preemption. – On uni-
        processor,
Which spinlock call to use:
If critical section is not shared by the interrupts and is only present in the process context
(system call) then spin_lock can be used.
Spin_lock_irqsave should be used in all other cases when critical section is shared
between interrupts/process path.
Spin_lock_trylock checks spinlock can be acquired or not and returns immediately.
Caller has to decide on the action based on the return value.
Spin_lock_bh performs actions as spin_lock_irq in addition to disabling softirq’s.

Spinlock are not recursive. Therefore spinlock should not be re-acquired.

Spinlock should be acquired and released without any wait/sleep. All calls that can lead
to system sleep should not be used while spinlock is acquired as interrupts/preemption is
disabled.


   1. Spin_lock,
   2. spin_lock_irq
   3. spin_lock_irqsave

spin_lock is to be used when there is no protection required from interrupts/BH/other
processes. There is only one user for the critical section and will provide protection on
SMP.

In case protection required from interrupts, and sure that interrupts will be enabled at the
time of accessing critical sections use 2nd option. This is because at the unlock stage,it
will enable the interrupt despite of the state beforehand.

In case the state of interrupts is unknown at the time of accessing critical section, use 3rd
option.

New Spinlock implementation in Linux world (ticket-spinlock):

This is new spinlock implementation that is new to the latest linux version.
The purpose of change:
Spinlock on SMP systems can be acquired from multiple paths, thus the value of spinlock
can be any large negative number.
On release of spinlock, any one of the waiting path can acquire spinlock instead of the
one that requested first for spinlock.
New spinlock is named as ticket spinlock, that makes sure that first requester of spinlock
gets the spinlock once it is released back to the system.

Memory Optimization
System in production can save memory from spinlock optimization by changing spinlock
functions from inline functions to out-of-line function calls.
In development system, this change might make debugging difficult as every time system
profiling is done it will show spinlock function instead of the function in which spinlock
API is used.

Semaphore:

How Semaphore works?
Semaphore is lock based on count which defines the number of locks available in the
system.
Semaphore is based on the counter value (count ), which indicates number of process that
can concurrently acquire the resource ( semaphore lock).

Down – To acquire the lock before entering critical section.

-   Test weather the lock is available
-   If available proceed,
-   Else suspend the execution path (push process out of run queue and put in a wait
    queue)

As semaphore wait pushes the control path in sleep, they can be used only in process
context. Thus should be avoided in interrupt context like ISR, BH, exceptions.

UP – To release the lock at the end of critical section
 - Release the lock
 - If other threads waiting for the lock, wake up processes
 - Else increment the count and exit
 -
Files:
Include/asm-mips/semaphore.h
Arch/mips/kernel/semaphore.c

API:

Sema_init - initialize the semaphore count with given value
Down – try to lock the critical section by decreasing the semaphore count , mark the
thread as UNINTERRUPTIBLE and sleep (putting the thread in wait queue).
Down_interruptible – same as down , with marking thread as INTERRUPTIBLE
Down_trylock – Return immediately with success or failure
Up – release the semaphore lock.



Special (Binary) Sempahores:
Binary semaphore are called as Mutex. Operations (API) on mutex remain same as
semaphore with initial count value of 1.
API:
DECLARE_MUTEX
DECLARE_MUTEX_LOCKED
INIT_MUTEX
INIT_MUTEX_LOCKED

New Mutexes:
It is different from the mutex name being used to define the binary semaphore previously.
These mutexes are new implementation in linux from 2.6.16 kernel onwards.
Features:
      only one task can hold the lock
      only the owner can unlock
      multiple unlocks and recursive locks are not allowed
      process may not exit with mutex held
      memory areas where held locks reside must not be freed

   File:
   Kernel/mutex.c
   Include/linux/mutex.h
   Include/linux/lockdep.h
   Include/asm-generic/mutex-xxx.h

   API:
   DEFINE_MUTEX
   Mutex_init
   Mutex_lock
   Mutex_lock_interruptible
   Mutex_trylock
   Mutex_unlock

Some other linux synchronization tools:

Atomic Operations:

To make read-modify-write operations as atomic, we need to provide some mechanism.
Atomic operations are defined to meet this requirement.
Atomic operations are dependant on the processor instructions provided for atomicity.
Atomicity is based on the LL/SC (Load-link / Store-conditional).
Load-link returns the current value of a memory location. A subsequent store-conditional
to the same memory location will store a new value only if no updates have occurred to
that location since the load-link. If any updates have occurred, the store-conditional is
guaranteed to fail, even if the value read by the load-link has since been restored.

File:
Include/asm-mips/atomic.h
API:
atomic_t my_atomic_counter = ATOMIC_INIT(0);

atomic read(v)
Atomically read the value of v.

atomic set(i, v)
Atomically set the value of v to i.

atomic add(i, v)
Atomically add the i to the value of v.

atomic sub(i, v)
Atomically subtract the i from the value of v.

atomic sub and test(i, v)
Atomically subtract i from v and return true if and only if the result is zero.

atomic inc(v)
Atomically increase value of v by 1.

atomic dec(v)
Atomically decrease value of v by 1.

atomic dec and test(v)
Atomically decrease value of v by 1 and return true if and only if the result is
zero.

atomic inc and test(v)
Atomically increase value of v by 1 and return true if and only if the result is
zero.

atomic add negative(i, v)
Atomically add i to v and return true if and only if the result is negative.

atomic add return(i, v)
Atomically add the i to the value of v and return the result.

atomic sub return(i, v)
Atomically subtract the i from the value of v and return the result.

atomic inc return(v)
Atomically increase value of v by 1 and return the result.

atomic dec return(v)
Atomically decrease value of v by 1 and return the result.
Memory barriers
Memory barrier primitive ensures that the instruction before the primitive is completed
before executing the instruction after the primitive.
In MIPS, sync instruction provides the basic memory barrier instruction.

API:

Memory barriers are in Linux implemented as architecture-dependent macros
(<asm/system.h>). The most common one are:

barrier();
Prevent compile-time reordering by inserting optimization barrier (empty
code, thus no performance loss).

mb();
Prevent read, write and optimization reordering (SMP and UP).

rmb();
Prevent read and optimization reordering (SMP and UP).

wmb();
Prevent write and optimization reordering (SMP and UP).

smp mb();
Prevent read, write and optimization reordering (SMP only).

smp rmb();
Prevent read and optimization reordering (SMP only).

smp wmb();
Prevent write and optimization reordering (SMP only).

Others:
Completions
RCU
Preemption
Interrupts
Per-cpu variables
RW semaphores
Futex
SeqLocks

More Related Content

What's hot

OpenMP and static code analysis
OpenMP and static code analysisOpenMP and static code analysis
OpenMP and static code analysisPVS-Studio
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Roman Elizarov
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practiceMikalai Alimenkou
 
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...Aymen Lachkhem
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register PackageDVClub
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010敬倫 林
 
Lock free programming - pro tips devoxx uk
Lock free programming - pro tips devoxx ukLock free programming - pro tips devoxx uk
Lock free programming - pro tips devoxx ukJean-Philippe BEMPEL
 
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CanSecWest
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)Simen Li
 
FreeRTOS basics (Real time Operating System)
FreeRTOS basics (Real time Operating System)FreeRTOS basics (Real time Operating System)
FreeRTOS basics (Real time Operating System)Naren Chandra
 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOSSirin Software
 
Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
 
Linux multiplexing
Linux multiplexingLinux multiplexing
Linux multiplexingMark Veltzer
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in PracticeAlina Dolgikh
 
Porting FreeRTOS on OpenRISC
Porting FreeRTOS   on   OpenRISCPorting FreeRTOS   on   OpenRISC
Porting FreeRTOS on OpenRISCYi-Chiao
 

What's hot (20)

OpenMP and static code analysis
OpenMP and static code analysisOpenMP and static code analysis
OpenMP and static code analysis
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!
 
FreeRTOS Course - Semaphore/Mutex Management
FreeRTOS Course - Semaphore/Mutex ManagementFreeRTOS Course - Semaphore/Mutex Management
FreeRTOS Course - Semaphore/Mutex Management
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
S emb t13-freertos
S emb t13-freertosS emb t13-freertos
S emb t13-freertos
 
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
 
UVM Update: Register Package
UVM Update: Register PackageUVM Update: Register Package
UVM Update: Register Package
 
FreeRTOS
FreeRTOSFreeRTOS
FreeRTOS
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
 
Lock free programming - pro tips devoxx uk
Lock free programming - pro tips devoxx ukLock free programming - pro tips devoxx uk
Lock free programming - pro tips devoxx uk
 
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
 
FreeRTOS basics (Real time Operating System)
FreeRTOS basics (Real time Operating System)FreeRTOS basics (Real time Operating System)
FreeRTOS basics (Real time Operating System)
 
Concurrent programming with RTOS
Concurrent programming with RTOSConcurrent programming with RTOS
Concurrent programming with RTOS
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 
Linux multiplexing
Linux multiplexingLinux multiplexing
Linux multiplexing
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
RCU
RCURCU
RCU
 
Porting FreeRTOS on OpenRISC
Porting FreeRTOS   on   OpenRISCPorting FreeRTOS   on   OpenRISC
Porting FreeRTOS on OpenRISC
 

Viewers also liked

Y12 hmk
Y12 hmkY12 hmk
Y12 hmkMrs G
 
Провери
ПровериПровери
Провериliillliiii
 
TAP Portugal MTMS Final Update
TAP Portugal MTMS Final UpdateTAP Portugal MTMS Final Update
TAP Portugal MTMS Final UpdateJos Quinta
 
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...Mr.Allah Dad Khan
 
Reconhecimento de fala em português brasileiro
Reconhecimento de fala em português brasileiroReconhecimento de fala em português brasileiro
Reconhecimento de fala em português brasileiroFabiano Weimar
 
Performance appraisal principles
Performance appraisal principlesPerformance appraisal principles
Performance appraisal principlesLearningade
 
Html5 the future of browsers
Html5 the future of browsersHtml5 the future of browsers
Html5 the future of browsersJatin Dabas
 
Daniel Green CV2016
Daniel Green CV2016Daniel Green CV2016
Daniel Green CV2016Daniel Green
 

Viewers also liked (13)

Mills
MillsMills
Mills
 
Y12 hmk
Y12 hmkY12 hmk
Y12 hmk
 
Провери
ПровериПровери
Провери
 
TAP Portugal MTMS Final Update
TAP Portugal MTMS Final UpdateTAP Portugal MTMS Final Update
TAP Portugal MTMS Final Update
 
IPv6
IPv6IPv6
IPv6
 
Unit 2
Unit 2Unit 2
Unit 2
 
CYRENWebSecurity
CYRENWebSecurityCYRENWebSecurity
CYRENWebSecurity
 
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...
Parsley insects A Lecture By Allah Dad Khan Provincial Coordinator IPM MINFAL...
 
ATT SERDJANE A
ATT SERDJANE AATT SERDJANE A
ATT SERDJANE A
 
Reconhecimento de fala em português brasileiro
Reconhecimento de fala em português brasileiroReconhecimento de fala em português brasileiro
Reconhecimento de fala em português brasileiro
 
Performance appraisal principles
Performance appraisal principlesPerformance appraisal principles
Performance appraisal principles
 
Html5 the future of browsers
Html5 the future of browsersHtml5 the future of browsers
Html5 the future of browsers
 
Daniel Green CV2016
Daniel Green CV2016Daniel Green CV2016
Daniel Green CV2016
 

Similar to Linux synchronization tools

Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfexcellentmobiles
 
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Sneeker Yeh
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410huangachou
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10huangachou
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).pptssuserf67e3a
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux InterruptsKernel TLV
 
Emulation Error Recovery
Emulation Error RecoveryEmulation Error Recovery
Emulation Error Recoverysomnathb1
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsJeff Smith
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsJeff Smith
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsJeff Smith
 
seminarembedded-150504150805-conversion-gate02.pdf
seminarembedded-150504150805-conversion-gate02.pdfseminarembedded-150504150805-conversion-gate02.pdf
seminarembedded-150504150805-conversion-gate02.pdfkarunyamittapally
 

Similar to Linux synchronization tools (20)

Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdf
 
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
 
Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
 
Memory model
Memory modelMemory model
Memory model
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
Apache Storm
Apache StormApache Storm
Apache Storm
 
Seminar
SeminarSeminar
Seminar
 
Emulation Error Recovery
Emulation Error RecoveryEmulation Error Recovery
Emulation Error Recovery
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
Pipelining In computer
Pipelining In computer Pipelining In computer
Pipelining In computer
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
 
ZooKeeper Recipes and Solutions
ZooKeeper Recipes and SolutionsZooKeeper Recipes and Solutions
ZooKeeper Recipes and Solutions
 
Real Time Operating Systems
Real Time Operating SystemsReal Time Operating Systems
Real Time Operating Systems
 
seminarembedded-150504150805-conversion-gate02.pdf
seminarembedded-150504150805-conversion-gate02.pdfseminarembedded-150504150805-conversion-gate02.pdf
seminarembedded-150504150805-conversion-gate02.pdf
 

Recently uploaded

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Linux synchronization tools

  • 1. Why Synchronization:  Critical region is a section of code that must be completely executed by the control path that enters it before any other control flow path can enter it. Thus kernel locking mechanism is required to provide: • Synchronization • Data integrity If kernel supports interleaving of control path flow, protecting the critical region becomes mandatory as one single control flow might not finish before other flow becomes active. Linux provides several synchronization tools in kernel space. Synchronization tools in kernel space?  Kernel space also has different execution paths. In case of preemptive kernel, interleaving becomes little more complicated and prominent.  Asynchronous events like Interrupt will always remain in system. Kernel preemption: In non-preemptive kernel, when process is executing in kernel space (kernel thread or user thread entering the kernel via system call), process was allowed to complete the execution unless it does self-relinquish of CPU. In preemptive kernel, process can be switched by scheduler before it finishes also, based on scheduling criteria. This was introduced to decrease the process latency and increase responsiveness. Kernel space execution paths:  Process / Thread  Interrupt  Bottom-half handlers – tasklets, softirqs Kernel locking for different paths: Process/Thread: - Between process/thread disabling kernel preemption will provide synchronicity. Interrupts: - Interrupts are asynchronous and requires synchronicity between interrupts, bottom- half and processes to protect the critical region shared between them. o Disabling interrupts/interrupt line can provide synchronicity Thus practically, disabling preemption and interrupt can provide synchronicity. Why other tools? > On SMP, above methods will not work. In case of SMP need of such tools becomes mandatory, as simultaneous execution of code flow can happen.
  • 2. Spinlocks: Although spinlock were designed for SMP systems but it is encouraged to be used in kernel code for uni-processor also to have code portability on SMP systems. What is spinlock? If kernel control path finds the lock available, it will acquire the lock and proceed. In case lock in un-available, it will spin on a tight loop forever until lock is made available. This means, the process flow will be executing continuously and blocking the CPU from any other code path execution. Linux Files related to spinlock implementation: Include/asm-arm/spinlock.h: architecture dependant code for spinlock implementation Include/linux/spinlock.h Kerel/spinlock.c Spinlock Usage: - Define spinlock variable Spinlock_t var= SPIN_LOCK (0) / SPIN_UNLOCKED (1) Spin_lock_init() - set the spinlock to 1 (SPIN_UNLOCKED) - To acquire the lock: • Spin_lock_irq() • Spin_lock_irqsave • Spin_lock_bh • Spin_lock • Spin_lock_trylock All spin lock calls: • Disable interrupts • Disable kernel preemption • Decrements the spinlock count (spinlock count can go to any large negative number indicating its demand) –> ( _raw_spin_lock present in architecture dependent) • Checks if count is equal to zero (if not equal to zero, keep looping). • In case of uni-processor, spinlock call just disables kernel preemption. – On uni- processor,
  • 3. Which spinlock call to use: If critical section is not shared by the interrupts and is only present in the process context (system call) then spin_lock can be used. Spin_lock_irqsave should be used in all other cases when critical section is shared between interrupts/process path. Spin_lock_trylock checks spinlock can be acquired or not and returns immediately. Caller has to decide on the action based on the return value. Spin_lock_bh performs actions as spin_lock_irq in addition to disabling softirq’s. Spinlock are not recursive. Therefore spinlock should not be re-acquired. Spinlock should be acquired and released without any wait/sleep. All calls that can lead to system sleep should not be used while spinlock is acquired as interrupts/preemption is disabled. 1. Spin_lock, 2. spin_lock_irq 3. spin_lock_irqsave spin_lock is to be used when there is no protection required from interrupts/BH/other processes. There is only one user for the critical section and will provide protection on SMP. In case protection required from interrupts, and sure that interrupts will be enabled at the time of accessing critical sections use 2nd option. This is because at the unlock stage,it will enable the interrupt despite of the state beforehand. In case the state of interrupts is unknown at the time of accessing critical section, use 3rd option. New Spinlock implementation in Linux world (ticket-spinlock): This is new spinlock implementation that is new to the latest linux version. The purpose of change: Spinlock on SMP systems can be acquired from multiple paths, thus the value of spinlock can be any large negative number. On release of spinlock, any one of the waiting path can acquire spinlock instead of the one that requested first for spinlock. New spinlock is named as ticket spinlock, that makes sure that first requester of spinlock gets the spinlock once it is released back to the system. Memory Optimization System in production can save memory from spinlock optimization by changing spinlock functions from inline functions to out-of-line function calls.
  • 4. In development system, this change might make debugging difficult as every time system profiling is done it will show spinlock function instead of the function in which spinlock API is used. Semaphore: How Semaphore works? Semaphore is lock based on count which defines the number of locks available in the system. Semaphore is based on the counter value (count ), which indicates number of process that can concurrently acquire the resource ( semaphore lock). Down – To acquire the lock before entering critical section. - Test weather the lock is available - If available proceed, - Else suspend the execution path (push process out of run queue and put in a wait queue) As semaphore wait pushes the control path in sleep, they can be used only in process context. Thus should be avoided in interrupt context like ISR, BH, exceptions. UP – To release the lock at the end of critical section - Release the lock - If other threads waiting for the lock, wake up processes - Else increment the count and exit - Files: Include/asm-mips/semaphore.h Arch/mips/kernel/semaphore.c API: Sema_init - initialize the semaphore count with given value Down – try to lock the critical section by decreasing the semaphore count , mark the thread as UNINTERRUPTIBLE and sleep (putting the thread in wait queue). Down_interruptible – same as down , with marking thread as INTERRUPTIBLE Down_trylock – Return immediately with success or failure Up – release the semaphore lock. Special (Binary) Sempahores: Binary semaphore are called as Mutex. Operations (API) on mutex remain same as semaphore with initial count value of 1.
  • 5. API: DECLARE_MUTEX DECLARE_MUTEX_LOCKED INIT_MUTEX INIT_MUTEX_LOCKED New Mutexes: It is different from the mutex name being used to define the binary semaphore previously. These mutexes are new implementation in linux from 2.6.16 kernel onwards. Features:  only one task can hold the lock  only the owner can unlock  multiple unlocks and recursive locks are not allowed  process may not exit with mutex held  memory areas where held locks reside must not be freed File: Kernel/mutex.c Include/linux/mutex.h Include/linux/lockdep.h Include/asm-generic/mutex-xxx.h API: DEFINE_MUTEX Mutex_init Mutex_lock Mutex_lock_interruptible Mutex_trylock Mutex_unlock Some other linux synchronization tools: Atomic Operations: To make read-modify-write operations as atomic, we need to provide some mechanism. Atomic operations are defined to meet this requirement. Atomic operations are dependant on the processor instructions provided for atomicity. Atomicity is based on the LL/SC (Load-link / Store-conditional). Load-link returns the current value of a memory location. A subsequent store-conditional to the same memory location will store a new value only if no updates have occurred to that location since the load-link. If any updates have occurred, the store-conditional is guaranteed to fail, even if the value read by the load-link has since been restored. File: Include/asm-mips/atomic.h
  • 6. API: atomic_t my_atomic_counter = ATOMIC_INIT(0); atomic read(v) Atomically read the value of v. atomic set(i, v) Atomically set the value of v to i. atomic add(i, v) Atomically add the i to the value of v. atomic sub(i, v) Atomically subtract the i from the value of v. atomic sub and test(i, v) Atomically subtract i from v and return true if and only if the result is zero. atomic inc(v) Atomically increase value of v by 1. atomic dec(v) Atomically decrease value of v by 1. atomic dec and test(v) Atomically decrease value of v by 1 and return true if and only if the result is zero. atomic inc and test(v) Atomically increase value of v by 1 and return true if and only if the result is zero. atomic add negative(i, v) Atomically add i to v and return true if and only if the result is negative. atomic add return(i, v) Atomically add the i to the value of v and return the result. atomic sub return(i, v) Atomically subtract the i from the value of v and return the result. atomic inc return(v) Atomically increase value of v by 1 and return the result. atomic dec return(v) Atomically decrease value of v by 1 and return the result.
  • 7. Memory barriers Memory barrier primitive ensures that the instruction before the primitive is completed before executing the instruction after the primitive. In MIPS, sync instruction provides the basic memory barrier instruction. API: Memory barriers are in Linux implemented as architecture-dependent macros (<asm/system.h>). The most common one are: barrier(); Prevent compile-time reordering by inserting optimization barrier (empty code, thus no performance loss). mb(); Prevent read, write and optimization reordering (SMP and UP). rmb(); Prevent read and optimization reordering (SMP and UP). wmb(); Prevent write and optimization reordering (SMP and UP). smp mb(); Prevent read, write and optimization reordering (SMP only). smp rmb(); Prevent read and optimization reordering (SMP only). smp wmb(); Prevent write and optimization reordering (SMP only). Others: Completions RCU Preemption Interrupts Per-cpu variables RW semaphores Futex SeqLocks