SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Linux locking mechanisms
Mark Veltzer
veltzer@gnu.org
Who am I?
● Linux kernel hacker
● Current maintainer of gnu grep(1)
● Free Source evangelist
● CTO of Hinbit
● Political philosopher (checkout my book “‫ןוטלשלטון‬
‫”ההמון‬ at book stores near you...)
● Jazz piano player
Why locking?
● To avoid race conditions in accessing shared memory.
● These occur because of: user space pre-emption which is
based on timer interrupts (userspace), multi-core (userspace),
interrupts in general (kernelspace), multi-core (kernelspace).
● Locking is not the only way to avoid such race conditions
● But this presentation is about locking and only about locking...
● In general locking is bad because it blocks your programs from
executing and so slows your program
● Avoid it when you can.
Avoiding locking - techniques
● Have each thread/CPU have it's own data.
● Use atomic operations (hardware) instead of locking (software).
● Lock free programming.
● RCU/COW.
● Readers/Writer locks.
● Not using the shared memory model but rather the actor model
for multi-processing/multi-threading.
● And many more techniques.
● Alas, we are here to talk about locking.
User space vs kernel space locking
● Is completely different
● Different mechanisms, different performance
considerations, different API
● But ultimately they work in concert.
User space locking
User space locking mechanisms
● Are not allowed to block interrupts. Ever!
● This is derived from the definition of what a secure operating system
is.
● If you have code in the kernel you can expose an API to user space
to block and allow interrupts.
● This is considered a bad idea.
● First of all because it allows user space bugs to lock up your
system.
● Second because it interferes with other kernel mechanisms (like
watchdogs, RCU and more).
● DON'T DO IT!
User space locking primitives
● pthread Spin lock
● Futex
● pthread mutex
● pthread Readers/writer lock.
● POSIX semaphore
● SYS V semaphore
User space spin lock - intro
● Is implemented as a simple TAS/CAS loop with CPU
relaxing and memory barrier.
● Pure user space implementation.
● DOES NOT DISABLE INTERRUPTS!
● Did I mention that it DOES NOT DISABLE
INTERRUPTS?!?
● It is interesting to note that IT DOES NOT DISABLE
INTERRUPTS.
● And finally note that NO INTERRUPTS ARE DISABLED
User space spin lock - issues
● The API is straight forward.
● The problem with this API is that IT DOES NOT
DISABLE INTERRUPTS
● This means that you may end up spinning for a
whole time slice (~1ms) if the two racing contexts are
on the same core.
● This may also happen if two context are on different
cores but one is pre-empted by some other context.
● This is really bad.
User space spin lock – when to
use?
● Use only when the two racing contexts are
running on two different cores and are the
highest priority contexts on these two cores.
● Usually this is only fulfilled on a dedicated RT
patched Linux system.
● Otherwise you get period spinning episodes.
● Kapish?!?
Futex – Fast user space locking
● The idea is to avoid trips to the kernel in the non
contended case.
● A mutex build half in user space and half in kernel
space.
● State of the lock is in user space.
● Wait list is in kernel space.
● Allows to lock/unlock without calling kernel space in
the non contended case.
● A Masterpiece of Linux engineering!
What happens when you die with a
lock held?
● Here are some suggestions:
– OS does nothing → deadlocks
– OS releases the lock → other contexts die because
of inconsistent data
– OS releases the lock and notifies the next context
locking the lock that the previous owner died →
This is what Linux does.
● This feature of locks is called robustness.
Linux has no threads
● Do you remember that Linux has no concept of a “thread”?
● Threads are just processes which happen to share a lot of
memory created with the clone(2) system call.
●
Don't tell this to user space developers in your company (they
tend to freak out about this).
●
This means that every locking mechanism in Linux can be
used for multi-processing as well as for multi-threading.
●
This is why futexes were made robust.
●
Futexes are robust by doing postmortem on dead processes and
examining the locks they leave behind in order to unlock them and
mark them as suspicious.
pthread_mutex
● Is now days just a wrapper for a futex.
● Could be used between processes (strange, but oh so true).
● Could be made robust using the undocumented API
pthread_mutexattr_setrobust(3).
● I found the documentation for this API on MSDN, of all
places…:)
● Supports recursiveness, two types of priority inheritance,
sharing between processes, priority ceiling and more.
● Makes lousy coffee, though...
Pthread readers/writer lock
● Is based on the futex.
● This means good performance.
● Standard, feature poor implementation.
● Build your own if you need more features.
● Could be used to synchronize processes and
threads.
POSIX semaphores
● Based on the futex.
● Again, good performance.
● Use this and not the Sys V version unless
you need the Sys V particular features.
● Could be used to synchronize both processes
and threads.
Sys V semaphore
● Reminder: Sys V is AT&T's version of UNIX
dating to circa 1983. In that version important
API's like this one were first introduced into the
UNIX world.
● Sys V semaphores are, however, crap.
● This is because they always go to the kernel.
Even in the non contended case.
● Do not use. Use POSIX semaphores instead.
Kernel locking
Kernel locking primitives
● Mutex
● Spinlock (3 types)
● Semaphore
● RW semaphores
Mutexes
● Go to sleep when finding the lock locked.
● This means they can only be used in contexts where you are
allowed to go to sleep.
● This means passive(user) context, kernel thread or workqueue
context and threaded IRQ context (?!?).
● Not allowed in IRQ handlers or tasklets.
● Has 3 modes: interruptible, killable and uninterruptible.
● Try to use interruptible as much as possible as bugs in kernel
code may cause non killable processes.
● Support priority inheritance under the RT patch.
Spin locks
● Most common kernel locking primitive.
● Are divided into 3 types: regular, BH and IRQ.
● Regular spin locks just turn off scheduling on the current CPU (in addition
to being a spin lock).
● BH turn off Bottom half mechanisms (including tasklets) on the current
CPU (in addition to being a spin lock).
● IRQ ones turn off interrupts on the local CPU (in addition to being a spin
lock).
● Sleeping, waiting or doing heavy computation with spin locks held is
considered reason for being banned from LKML.
● Turn into Mutexes under the RT patch and then support priority
inheritance.
Spin lock (irq version)
● Turning of IRQs is quite fast (IF, CLI, STI are
really fast on INTEL).
● Very brutal as it increases latency in real time
implementations.
● Try not to access data structure from IRQ
context so you won't have to use this.
● However, this is still one of the most common
locking primitives
When to use each?
● Passive vs Passive
– Use a mutex in interruptible mode (you are allowed to sleep in both).
– Or semaphore in interruptible mode.
– Or a regular spin lock. You are in no danger of spinning for long since scheduling on the current CPU is disabled. Interrupts may
come in and so do tasklets but these are quick.
●
Passive vs BH
– spinlock_bh
● Passive vs IRQ
– Use spin lock irq.
– The irq part prevents races on the current CPU.
– The spin lock part prevents races with other CPUs.
● BH vs BH
– Spinlock
● BH vs IRQ
– Spinlock irq
● IRQ vs IRQ
– Spinlock irq.
● See “Rusty Russells Unreliable Guide to Locking”
Semaphores
● semaphore.h
● Usually used as a mutex and not as a semaphore.
● Up and down methods do not accept ticket number but always
increase and decrease by 1.
● Ticket/permit count can be determined at creation time.
● Semaphores do not offer priority inheritance. Even under the
RT patch.
● This means that any system call that uses this is unfit to be
used in the critical path of a real time application.
● 3 modes of operation (like the mutex).
RW semaphores
● rwsem.h
● Offer more performance when number of readers
outnumbers number of writers.
● Again, does not support priority inheritance. Even
under the RT patch.
● Famous is the current->mm->mmap_sem that
protects each processes virtual memory description.
● Good reason not to use malloc(3) in real time
systems.
RW lock
● rwlock.h
● By Ingo Molnar (author of the real time patch).
● Supports priority inheritance.
● Use this instead of RW semaphores.
● Again, gives better performance when number
of readers out numbers number of writers.
The RT patch
● Runs all irq handlers in their own threads with
other interrupts enabled.
● Turns all spinlocks into mutexes to reduce
latency and allow high priority tasks to get the
CPU ASAP.
● If you really need a spin lock you can use the
raw spinlock API which will give you a true
spinlock even under the RT patch.

Contenu connexe

Tendances

High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
hugo lu
 

Tendances (20)

Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIO
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
Docker storage drivers by Jérôme Petazzoni
Docker storage drivers by Jérôme PetazzoniDocker storage drivers by Jérôme Petazzoni
Docker storage drivers by Jérôme Petazzoni
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Yet another introduction to Linux RCU
Yet another introduction to Linux RCUYet another introduction to Linux RCU
Yet another introduction to Linux RCU
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
 
Static partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-VStatic partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-V
 
[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?
 
New Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using TracingNew Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using Tracing
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
Understanding DPDK
Understanding DPDKUnderstanding DPDK
Understanding DPDK
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Trace kernel code tips
Trace kernel code tipsTrace kernel code tips
Trace kernel code tips
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 

Similaire à Linux Locking Mechanisms

Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410Linux kernel development_ch9-10_20120410
Linux kernel development_ch9-10_20120410
huangachou
 
Linux kernel development chapter 10
Linux kernel development chapter 10Linux kernel development chapter 10
Linux kernel development chapter 10
huangachou
 
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
excellentmobiles
 

Similaire à Linux Locking Mechanisms (20)

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
 
Realtime
RealtimeRealtime
Realtime
 
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
 
Java under the hood
Java under the hoodJava under the hood
Java under the hood
 
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
 
Kernel
KernelKernel
Kernel
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
 
Streams
StreamsStreams
Streams
 
Efficient Buffer Management
Efficient Buffer ManagementEfficient Buffer Management
Efficient Buffer Management
 
Multicore
MulticoreMulticore
Multicore
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势
 
epoll() - The I/O Hero
epoll() - The I/O Heroepoll() - The I/O Hero
epoll() - The I/O Hero
 
Keeping Latency Low and Throughput High with Application-level Priority Manag...
Keeping Latency Low and Throughput High with Application-level Priority Manag...Keeping Latency Low and Throughput High with Application-level Priority Manag...
Keeping Latency Low and Throughput High with Application-level Priority Manag...
 
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)
 
An End to Order
An End to OrderAn End to Order
An End to Order
 
Intro to operating_system
Intro to operating_systemIntro to operating_system
Intro to operating_system
 
An End to Order (many cores with java, session two)
An End to Order (many cores with java, session two)An End to Order (many cores with java, session two)
An End to Order (many cores with java, session two)
 
JDD 2017: Brace yourself! Storm is coming! (Łukasz Gebel, Michał Koziorowski)
JDD 2017: Brace yourself! Storm is coming! (Łukasz Gebel, Michał Koziorowski)JDD 2017: Brace yourself! Storm is coming! (Łukasz Gebel, Michał Koziorowski)
JDD 2017: Brace yourself! Storm is coming! (Łukasz Gebel, Michał Koziorowski)
 

Plus de Kernel TLV

Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
Kernel TLV
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
Kernel TLV
 

Plus de Kernel TLV (20)

DPDK In Depth
DPDK In DepthDPDK In Depth
DPDK In Depth
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
SGX Trusted Execution Environment
SGX Trusted Execution EnvironmentSGX Trusted Execution Environment
SGX Trusted Execution Environment
 
Fun with FUSE
Fun with FUSEFun with FUSE
Fun with FUSE
 
Kernel Proc Connector and Containers
Kernel Proc Connector and ContainersKernel Proc Connector and Containers
Kernel Proc Connector and Containers
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
 
Present Absence of Linux Filesystem Security
Present Absence of Linux Filesystem SecurityPresent Absence of Linux Filesystem Security
Present Absence of Linux Filesystem Security
 
OpenWrt From Top to Bottom
OpenWrt From Top to BottomOpenWrt From Top to Bottom
OpenWrt From Top to Bottom
 
Make Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsMake Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance Tools
 
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
 
File Systems: Why, How and Where
File Systems: Why, How and WhereFile Systems: Why, How and Where
File Systems: Why, How and Where
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
 
KernelTLV Speaker Guidelines
KernelTLV Speaker GuidelinesKernelTLV Speaker Guidelines
KernelTLV Speaker Guidelines
 
Userfaultfd: Current Features, Limitations and Future Development
Userfaultfd: Current Features, Limitations and Future DevelopmentUserfaultfd: Current Features, Limitations and Future Development
Userfaultfd: Current Features, Limitations and Future Development
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use Cases
 
DMA Survival Guide
DMA Survival GuideDMA Survival Guide
DMA Survival Guide
 
FD.IO Vector Packet Processing
FD.IO Vector Packet ProcessingFD.IO Vector Packet Processing
FD.IO Vector Packet Processing
 
WiFi and the Beast
WiFi and the BeastWiFi and the Beast
WiFi and the Beast
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDK
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and Drivers
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 

Dernier (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

Linux Locking Mechanisms

  • 1. Linux locking mechanisms Mark Veltzer veltzer@gnu.org
  • 2. Who am I? ● Linux kernel hacker ● Current maintainer of gnu grep(1) ● Free Source evangelist ● CTO of Hinbit ● Political philosopher (checkout my book “‫ןוטלשלטון‬ ‫”ההמון‬ at book stores near you...) ● Jazz piano player
  • 3. Why locking? ● To avoid race conditions in accessing shared memory. ● These occur because of: user space pre-emption which is based on timer interrupts (userspace), multi-core (userspace), interrupts in general (kernelspace), multi-core (kernelspace). ● Locking is not the only way to avoid such race conditions ● But this presentation is about locking and only about locking... ● In general locking is bad because it blocks your programs from executing and so slows your program ● Avoid it when you can.
  • 4. Avoiding locking - techniques ● Have each thread/CPU have it's own data. ● Use atomic operations (hardware) instead of locking (software). ● Lock free programming. ● RCU/COW. ● Readers/Writer locks. ● Not using the shared memory model but rather the actor model for multi-processing/multi-threading. ● And many more techniques. ● Alas, we are here to talk about locking.
  • 5. User space vs kernel space locking ● Is completely different ● Different mechanisms, different performance considerations, different API ● But ultimately they work in concert.
  • 7. User space locking mechanisms ● Are not allowed to block interrupts. Ever! ● This is derived from the definition of what a secure operating system is. ● If you have code in the kernel you can expose an API to user space to block and allow interrupts. ● This is considered a bad idea. ● First of all because it allows user space bugs to lock up your system. ● Second because it interferes with other kernel mechanisms (like watchdogs, RCU and more). ● DON'T DO IT!
  • 8. User space locking primitives ● pthread Spin lock ● Futex ● pthread mutex ● pthread Readers/writer lock. ● POSIX semaphore ● SYS V semaphore
  • 9. User space spin lock - intro ● Is implemented as a simple TAS/CAS loop with CPU relaxing and memory barrier. ● Pure user space implementation. ● DOES NOT DISABLE INTERRUPTS! ● Did I mention that it DOES NOT DISABLE INTERRUPTS?!? ● It is interesting to note that IT DOES NOT DISABLE INTERRUPTS. ● And finally note that NO INTERRUPTS ARE DISABLED
  • 10. User space spin lock - issues ● The API is straight forward. ● The problem with this API is that IT DOES NOT DISABLE INTERRUPTS ● This means that you may end up spinning for a whole time slice (~1ms) if the two racing contexts are on the same core. ● This may also happen if two context are on different cores but one is pre-empted by some other context. ● This is really bad.
  • 11. User space spin lock – when to use? ● Use only when the two racing contexts are running on two different cores and are the highest priority contexts on these two cores. ● Usually this is only fulfilled on a dedicated RT patched Linux system. ● Otherwise you get period spinning episodes. ● Kapish?!?
  • 12. Futex – Fast user space locking ● The idea is to avoid trips to the kernel in the non contended case. ● A mutex build half in user space and half in kernel space. ● State of the lock is in user space. ● Wait list is in kernel space. ● Allows to lock/unlock without calling kernel space in the non contended case. ● A Masterpiece of Linux engineering!
  • 13. What happens when you die with a lock held? ● Here are some suggestions: – OS does nothing → deadlocks – OS releases the lock → other contexts die because of inconsistent data – OS releases the lock and notifies the next context locking the lock that the previous owner died → This is what Linux does. ● This feature of locks is called robustness.
  • 14. Linux has no threads ● Do you remember that Linux has no concept of a “thread”? ● Threads are just processes which happen to share a lot of memory created with the clone(2) system call. ● Don't tell this to user space developers in your company (they tend to freak out about this). ● This means that every locking mechanism in Linux can be used for multi-processing as well as for multi-threading. ● This is why futexes were made robust. ● Futexes are robust by doing postmortem on dead processes and examining the locks they leave behind in order to unlock them and mark them as suspicious.
  • 15. pthread_mutex ● Is now days just a wrapper for a futex. ● Could be used between processes (strange, but oh so true). ● Could be made robust using the undocumented API pthread_mutexattr_setrobust(3). ● I found the documentation for this API on MSDN, of all places…:) ● Supports recursiveness, two types of priority inheritance, sharing between processes, priority ceiling and more. ● Makes lousy coffee, though...
  • 16. Pthread readers/writer lock ● Is based on the futex. ● This means good performance. ● Standard, feature poor implementation. ● Build your own if you need more features. ● Could be used to synchronize processes and threads.
  • 17. POSIX semaphores ● Based on the futex. ● Again, good performance. ● Use this and not the Sys V version unless you need the Sys V particular features. ● Could be used to synchronize both processes and threads.
  • 18. Sys V semaphore ● Reminder: Sys V is AT&T's version of UNIX dating to circa 1983. In that version important API's like this one were first introduced into the UNIX world. ● Sys V semaphores are, however, crap. ● This is because they always go to the kernel. Even in the non contended case. ● Do not use. Use POSIX semaphores instead.
  • 20. Kernel locking primitives ● Mutex ● Spinlock (3 types) ● Semaphore ● RW semaphores
  • 21. Mutexes ● Go to sleep when finding the lock locked. ● This means they can only be used in contexts where you are allowed to go to sleep. ● This means passive(user) context, kernel thread or workqueue context and threaded IRQ context (?!?). ● Not allowed in IRQ handlers or tasklets. ● Has 3 modes: interruptible, killable and uninterruptible. ● Try to use interruptible as much as possible as bugs in kernel code may cause non killable processes. ● Support priority inheritance under the RT patch.
  • 22. Spin locks ● Most common kernel locking primitive. ● Are divided into 3 types: regular, BH and IRQ. ● Regular spin locks just turn off scheduling on the current CPU (in addition to being a spin lock). ● BH turn off Bottom half mechanisms (including tasklets) on the current CPU (in addition to being a spin lock). ● IRQ ones turn off interrupts on the local CPU (in addition to being a spin lock). ● Sleeping, waiting or doing heavy computation with spin locks held is considered reason for being banned from LKML. ● Turn into Mutexes under the RT patch and then support priority inheritance.
  • 23. Spin lock (irq version) ● Turning of IRQs is quite fast (IF, CLI, STI are really fast on INTEL). ● Very brutal as it increases latency in real time implementations. ● Try not to access data structure from IRQ context so you won't have to use this. ● However, this is still one of the most common locking primitives
  • 24. When to use each? ● Passive vs Passive – Use a mutex in interruptible mode (you are allowed to sleep in both). – Or semaphore in interruptible mode. – Or a regular spin lock. You are in no danger of spinning for long since scheduling on the current CPU is disabled. Interrupts may come in and so do tasklets but these are quick. ● Passive vs BH – spinlock_bh ● Passive vs IRQ – Use spin lock irq. – The irq part prevents races on the current CPU. – The spin lock part prevents races with other CPUs. ● BH vs BH – Spinlock ● BH vs IRQ – Spinlock irq ● IRQ vs IRQ – Spinlock irq. ● See “Rusty Russells Unreliable Guide to Locking”
  • 25. Semaphores ● semaphore.h ● Usually used as a mutex and not as a semaphore. ● Up and down methods do not accept ticket number but always increase and decrease by 1. ● Ticket/permit count can be determined at creation time. ● Semaphores do not offer priority inheritance. Even under the RT patch. ● This means that any system call that uses this is unfit to be used in the critical path of a real time application. ● 3 modes of operation (like the mutex).
  • 26. RW semaphores ● rwsem.h ● Offer more performance when number of readers outnumbers number of writers. ● Again, does not support priority inheritance. Even under the RT patch. ● Famous is the current->mm->mmap_sem that protects each processes virtual memory description. ● Good reason not to use malloc(3) in real time systems.
  • 27. RW lock ● rwlock.h ● By Ingo Molnar (author of the real time patch). ● Supports priority inheritance. ● Use this instead of RW semaphores. ● Again, gives better performance when number of readers out numbers number of writers.
  • 28. The RT patch ● Runs all irq handlers in their own threads with other interrupts enabled. ● Turns all spinlocks into mutexes to reduce latency and allow high priority tasks to get the CPU ASAP. ● If you really need a spin lock you can use the raw spinlock API which will give you a true spinlock even under the RT patch.