230 likes | 529 Views
Kernel synchronization methods. Tao Yang 2007-04-27. Overview. critical regions: Code paths that access and manipulate shared data race condition: situation that two threads of execution be simultaneously in the same critical region
E N D
Kernel synchronization methods Tao Yang 2007-04-27
Overview • critical regions: Code paths that access and manipulate shared data • race condition: situation that two threads of execution be simultaneously in the same critical region • Synchronization: ensure that race conditions do not occur and prevent unsafe concurrency • Synchronization issues and how to prevent race conditions • synchronization methods’ interface, behavior, and use
General Line • Atomic Operations • Spin Locks • Reader-Writer Spin Locks • Semaphores • Reader-Writer Semaphores • Spin Locks VS Semaphores • Completion Variables • BKL: The Big Kernel Lock • Preemption Disabling • Ordering and Barriers
Atomic Operations • Basic operation • Instructions without interruption • indivisible instructions • two sets of interfaces for atomic operations • 1. Atomic Integer Operations • 2. Atomic Bitwise Operations
Atomic Operations Atomic Integer Operations • The atomic integer methods use a special data type: atomic_t • To protect concurrent access to the atomic type Figure: Old layout of the 32-bit atomic_t on SPARC
Atomic Operations Atomic Integer Operations • Example: simple use of atomic integer operation
Atomic Operations Atomic Bitwise Operations • operate at the bit level • operate on a generic pointer • No equivalent of the atomic integer's atomic_t type
Atomic Operations Atomic Bitwise Operations Table: Listing of Atomic Bitwise Operations
Spin Locks • When critical region span multiple function and more complex • For example, data be removed from one structure, formatted and parsed, and added to another structure • We need a more general method of synchronization--lock • Spin lock • A lock that can be held by only one thread of execution at any same time • Key point: while a spin lock is held, other thread of execution can’t acquire the lock, the thread will busy loops-spins-waiting for the lock to become available • It is not wise to hold a spin lock for a long time • The basic use of a spin lock is:
Spin Locks • Warning: Spin Locks Are Not Recursive • You will deadlock if you attempt to acquire a spin lock you’ve already held • Use in interrupt handlers • First: disable local interrupt requests • Otherwise: double-acquire deadlock • an interface that conveniently disables interrupts and acquires the lock: • The routine spin_lock_irqsave() saves the current state of interrupts, disables them locally, and then obtains the given lock
Spin Locks Table: Listing of Spin Lock Methods • What do we lock? • It is important that we protect data but not code • Big Fat Rule: Locks code regions are hard to understand and prone to race conditions. Lock data, not code.
Reader-Writer Spin Locks • When we need reader-writer spin locks? • Sometimes, lock usage can be clearly divided into readers and writers • What rules we should follow when using reader-writer spin locks? • One or more readers can concurrently hold the reader lock • Only one writer can hold the writer lock with no concurrent readers • Methods: • Initialization: • Reader lock: • Writer lock: • "upgrade" a read lock to a write lock will deadlocks:
Reader-Writer Spin Locks • Table: Listing of Reader-Writer Spin Lock Methods
Semaphores • Semaphores in Linux are sleeping locks • Semaphores are suited to locks be held for long time • You can sleep while holding a semaphore • You cannot acquire a spin lock while you’re holding a semaphore • Creating and Initializing Semaphores: • Declare semaphores Statically: • Initialize a dynamically created semaphore:
Semaphores • Table: Listing of Semaphore Methods
Reader-Writer Semaphores • Similar to spin locks, semaphores also have Reader-Writer Semaphores • If there are no writers any number of readers can concurrently hold the read lock • If we can separate clearly between write paths and read paths in code we should use this Reader-writer semaphores • Creating and Initializing reader-writer Semaphores: • Declare reader-writer semaphores Statically: • Initialize a dynamically created reader-writer semaphore:
Spin Locks Versus Semaphores • A spin lock can be used in interrupt context • A semaphore can be held while a task sleeps Table: What to Use: Spin Locks Versus Semaphores
Completion Variables • Similar to semaphores • One task waits on the completion variable while another task performs some work • When the other task has completed the work, it uses the completion variable to wake up any waiting tasks • Example: parent process and the child process Table: Completion Variables Methods
BKL: The Big Kernel Lock • The Big Kernel Lock (BKL) is a global spin lock • You can sleep while holding the BKL • The BKL is a recursive lock • You can use the BKL only in process context • It is evil • It helped the transition easy from kernel version 2.0 to 2.2 • But now it is a scalability burden • Too often the BKL is protecting code instead of data • Use of the BKL in kernel is discouraged
Preemption Disabling • Problems: • For the kernel is preemptive, a process can stop running to allow a process of higher priority to run • A task can run in the same critical region at same time with another task which is preempted • Solution: • Uses spin locks as markers of nonpreemptive regions Table: Kernel Preemption Related Functions
Synchronization Summarization • the simplest method of ensuring synchronization, atomic operations • spin locks, the most common lock in the kernel lightweight single-holder lock that busy waits while contended • semaphores, a sleeping lock, and less common
Thank you Q & A