1 / 22

Kernel synchronization methods

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

wilda
Download Presentation

Kernel synchronization methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Kernel synchronization methods Tao Yang 2007-04-27

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

  3. 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

  4. Atomic Operations • Basic operation • Instructions without interruption • indivisible instructions • two sets of interfaces for atomic operations • 1. Atomic Integer Operations • 2. Atomic Bitwise Operations

  5. 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

  6. Atomic Operations Atomic Integer Operations • Example: simple use of atomic integer operation

  7. 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

  8. Atomic Operations Atomic Bitwise Operations Table: Listing of Atomic Bitwise Operations

  9. 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:

  10. 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

  11. 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.

  12. 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:

  13. Reader-Writer Spin Locks • Table: Listing of Reader-Writer Spin Lock Methods

  14. 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:

  15. Semaphores • Table: Listing of Semaphore Methods

  16. 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:

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. Thank you Q & A

More Related