synchronizaÚon methods
Tao Yang
2007-04-
27
Overview
• critical regions: Code paths that access and manipulate shared
dala
• 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
Thread 1
atomic increment I (7 -> 8)
• two sets of interfaces for atomic
operations
e 1. Atomic Integer Operations
u 2. Atomic Bitwise Operations
Thread 2
mt (7)
:ncrement : (7 ->
8)
Thread 2
atomic increment i (8 -> 9)
4
Atomic
Operations
Atomic Inte;;er Operations
• The atomic integer methods use a special data
type: .«» i
• To protect concufrent access to the atomic type
(bit)
31
32-bit atomic
I
signed 24-bit integer
Layout ol ho 32-bit alomic_t on 5
ARC
7 0
lock
Figure: Old layout of the 32-bit atomic
t on SPARC
Atomic
Operations
Atomic Integer Operations
Operations are all simple:
atomic_set(&v, 4):
atomic_add(2,
&v); atomic
inc(&v);
• Example: simple use of atomic integer operation
atomic_t v: /- oezine v -/
atomic_t u ATOMIC_INIT(0): /* define u and initialize it to
zero *7
/* v 4 (atomically) */
/* v v + 2 - 6 (atomically)
’/
/* v = v + 1 = 7 (atomically)
*/
If you ever need to convert an atoa›tc_t to an 1n1-, use atozaic_zeact{ )
printk(”%dn’, atomic read(&v)); /* will print ’7’ */
Atomic
Operations
Atomic Bitwise Operations
• operate at the b’‹t level
• operate on a generic pointer
■ No equivalent of the atomic integer's atomic t
type
long word = 0:
set_bitl0, &word);
set bit(1, &word);
printk("%uln",
word);
clear_bit(1,
&word):
change_bit(0,
&word);
/’ bit zero is now set (atomically) */
/* bit one is now set (atomically) */
/* will print "3’ */
/• bit one is now unset {atomically) */
/* bit eero is flipped; now it is unset (atomically)
*/
/* atomically sets bit zero and returns the previous value (zero)
*/ if (test_and_set_bit(0, 4word)) (
/* never true */
/* the following is legal. you can mix atomic bit instructions
with normal C */
7
Atomic Operations
Atomic Bitwise Operations
Table: Listing of Atomic Bitwise
Operations
Atomic Bitwise Operation
void set bin (I nt n r , void
*actdr)
Description
AtomTcally set the nr -rh
bit starting from actaz
Atamical!y clear the nx
th blt starting from
adds
AtomTcally fllp the
value of the n<-ch bit
starting FOR addr
Atomically set the n -
th bit starting from
aclctz and return the
previous value
Atomically clear the m
a
— th bit starting from
actar and return the
previous value
Atom1caIIy fllp the nr-
£h bit starting from
adar and return the
previous value
Atomically return
the value of the nz-
void ciear bit(int nr, void *adix)
void change_biz(int nr, void
*addr)
int test_and_set_bit(int nr, void
*addr)
int test_and_cleai_bit(int nr,
void *addr)
int test and change bit(int nr,
void *addr)
int t9st_bit(int nr, void *addr)
8
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 s ›in ILJfi/
lrL]Rf L() saves the current state
of interrupts, disables them
locally, and then obtains the
given lock
Spin Locks
• What do we lock?
It is important that we
protect data but not
code
a Big Fat Rule:
Locks code regions are
hard to understand and
prone to race
conditions. Lock data,
not code.
ggetho
d
opin_lock()
spin_lock_irq()
Table: Listing of Spin Lock
Methods
Description
spin_lock irqaave()
spin_un1ock()
spin unlock izq()
spin_un1ock_irqrestoze
11
spin_lock_init()
spin_trylock()
spin_is_1ocked()
Acquires given lock
Disables local interrupts and
acquires given lock
Saves current state of local
interrupts, disables locsl
Interrupts, and acquires given lock
Releases given lock
Releases given lock and enables
local Interrupts
Releases given lock and restores
local interrupts to glven
previous state
Dynamically InItIaIizes glven
spin1ock_t
Tries to acquire glven lock; If
unavailable, returns nonzero
Returns nonzero if the given lock
is currently acquired, otherwise It
returns zero
Reader-Writer Spin Locks
• When we need reader-wfiter spin locks?
u Sometimes, lock usage can be clearly divided into readers and writers
• What rules we should follow when using reader-writer spin
locks?
u One or more readers can concurrently hold the reader lock
u Only one writer can hold the writer lock with no concurrent
readers
• Methods:
Initialization:
Reader lock:
Writer lock:
Reader-Writer Soin Locks
■ Table: Listing
of Reader-Writer
Spin Lock
Methods
Acqu lres cjiven lock for reacling
Disa bl es Ioca I lnLerru pts and
a cq u ires p iven lock foi reed i
ng
1
1
S a re•s the ciai rent sta te cif Isea
1
i nteiropt s, d i sa b les Ie ca 1
I nt erru pts, a n d a c q u Ices tlne
g iu
e
n I cock fc›r react i ng
Rel e a s e s
I oak
Rel eas es g iven Iock
loca I inLerru pts
for re std! eg
a
n
d e
n
a bl es
Rel e a s e s g iven Iock and restores
laca I i n Let ru pts Lo the g iven
previous state
Acqu fres g iven lock for writ Ing
D isa bl es Ioca I
interrupts ance
a cqcJires L h e gi ven lock for
vvr i ti r1g
S ave s current s t a t e o f Ioca
I interru pts, disa
bIes loca I interrupts,
a net acquires Lhe g Iv
e
n Iock
for wrl
ting
Rel e a s e s gI ven Iock
Rel eases given Iock and
ena bl es
lac a I i n t e r r u p t s
Rel eas es g iven Iock a
n
d
reaLores loca I
interrupts to gi ven previous
Tries Lo a oqui ire q iven
Iock for 'n/ri ti ng ; If unava
I la b Ie, returns nonzero
Init ia 1i zes g iuen - cc n z
ReLcJrns non z ero i f the g i ven
lock
Is current I y a cqul red, or e Ise It
13
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:
u Declare semaphores Statically:
Initialize a dynamically created semaphore:
» -=
•-
Sema hores
• Table:
Listing of
Semaphore
Methods
Method
sema init(struct semaphore *,
int)
init_MUTEX(struct semaphore •)
init MUTEX LOCKED(struct
semaphore *)
down_interruptiblelstruct
down(struct semaphore *)
down trylock(struct semaphore *)
Description
Inltializes the
dynamically created
semaphore to the given
count
InftIaIIzes the
dynamically created
semaphore with a count
of one
Initializes the
dynamically created
semaphore with a count
of zero (so It s initially
locked)
Trles to acquire the
given semaphore and
enter interruptlble
sleep If it Is contended
Trles to acquire the
given semaphore and
enter uninterruptibTe
sleep if it is contended
Tries to acquire the
given semaphore and
Immediately return
nonzero if it is
contended
Releases the glven
semaphore and wakes
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
Requirement
Loss overhead locking,
Shoft lock hold titre
Long Ioci‹ hold time
Need to lock f‹-on
inteirript
context
Recommended Lock
Spin loco is pi-efeiied
Spin IoCk is p‹
felled Sen np!
oie i oi-
efeiied Spin lock i
iec‹iii ed
Compleüon Variables
• Similar to semaphores
u One task waits on the completion variable while another task performs
some work
u 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
I
gethod
Table: Completion Variables
Methods
Descripôon
Unit completion (stru
t
conolet:on *)
wait for comoletion(struct
complète(stiuct completïcn
Initial izes the
given dynamlcally
created completian
vaFiable
Waits for the glven
completion variable to
be signaled
’) Signale any
waiting tasks to wake up
18
BI«?L: The Bid; I«?ernel 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
u 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
e 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
Function
pzeempt di sable
()
Description
Disables kernel preemption by
Incrementlng the preemptlon
counter
Decrement the preemption
counter and check and service
any pending reschedules if the
count is now zero
preempt_enable()
preempt_enable_no_resched Enables kernel preemption but do
not check for any
pending reschedules
Returns the preemption
count
2
u
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
linux kernel_synchrnizatoin_and_locking_mechanism-related_ppt.pptx

linux kernel_synchrnizatoin_and_locking_mechanism-related_ppt.pptx

  • 1.
  • 2.
    Overview • critical regions:Code paths that access and manipulate shared dala • 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 • AtomicOperations • 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 • Basicoperation • Instructions without interruption • indivisible instructions Thread 1 atomic increment I (7 -> 8) • two sets of interfaces for atomic operations e 1. Atomic Integer Operations u 2. Atomic Bitwise Operations Thread 2 mt (7) :ncrement : (7 -> 8) Thread 2 atomic increment i (8 -> 9) 4
  • 5.
    Atomic Operations Atomic Inte;;er Operations •The atomic integer methods use a special data type: .«» i • To protect concufrent access to the atomic type (bit) 31 32-bit atomic I signed 24-bit integer Layout ol ho 32-bit alomic_t on 5 ARC 7 0 lock Figure: Old layout of the 32-bit atomic t on SPARC
  • 6.
    Atomic Operations Atomic Integer Operations Operationsare all simple: atomic_set(&v, 4): atomic_add(2, &v); atomic inc(&v); • Example: simple use of atomic integer operation atomic_t v: /- oezine v -/ atomic_t u ATOMIC_INIT(0): /* define u and initialize it to zero *7 /* v 4 (atomically) */ /* v v + 2 - 6 (atomically) ’/ /* v = v + 1 = 7 (atomically) */ If you ever need to convert an atoa›tc_t to an 1n1-, use atozaic_zeact{ ) printk(”%dn’, atomic read(&v)); /* will print ’7’ */
  • 7.
    Atomic Operations Atomic Bitwise Operations •operate at the b’‹t level • operate on a generic pointer ■ No equivalent of the atomic integer's atomic t type long word = 0: set_bitl0, &word); set bit(1, &word); printk("%uln", word); clear_bit(1, &word): change_bit(0, &word); /’ bit zero is now set (atomically) */ /* bit one is now set (atomically) */ /* will print "3’ */ /• bit one is now unset {atomically) */ /* bit eero is flipped; now it is unset (atomically) */ /* atomically sets bit zero and returns the previous value (zero) */ if (test_and_set_bit(0, 4word)) ( /* never true */ /* the following is legal. you can mix atomic bit instructions with normal C */ 7
  • 8.
    Atomic Operations Atomic BitwiseOperations Table: Listing of Atomic Bitwise Operations Atomic Bitwise Operation void set bin (I nt n r , void *actdr) Description AtomTcally set the nr -rh bit starting from actaz Atamical!y clear the nx th blt starting from adds AtomTcally fllp the value of the n<-ch bit starting FOR addr Atomically set the n - th bit starting from aclctz and return the previous value Atomically clear the m a — th bit starting from actar and return the previous value Atom1caIIy fllp the nr- £h bit starting from adar and return the previous value Atomically return the value of the nz- void ciear bit(int nr, void *adix) void change_biz(int nr, void *addr) int test_and_set_bit(int nr, void *addr) int test_and_cleai_bit(int nr, void *addr) int test and change bit(int nr, void *addr) int t9st_bit(int nr, void *addr) 8
  • 9.
    Spin Locks • Whencritical 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 s ›in ILJfi/ lrL]Rf L() saves the current state of interrupts, disables them locally, and then obtains the given lock
  • 11.
    Spin Locks • Whatdo we lock? It is important that we protect data but not code a Big Fat Rule: Locks code regions are hard to understand and prone to race conditions. Lock data, not code. ggetho d opin_lock() spin_lock_irq() Table: Listing of Spin Lock Methods Description spin_lock irqaave() spin_un1ock() spin unlock izq() spin_un1ock_irqrestoze 11 spin_lock_init() spin_trylock() spin_is_1ocked() Acquires given lock Disables local interrupts and acquires given lock Saves current state of local interrupts, disables locsl Interrupts, and acquires given lock Releases given lock Releases given lock and enables local Interrupts Releases given lock and restores local interrupts to glven previous state Dynamically InItIaIizes glven spin1ock_t Tries to acquire glven lock; If unavailable, returns nonzero Returns nonzero if the given lock is currently acquired, otherwise It returns zero
  • 12.
    Reader-Writer Spin Locks •When we need reader-wfiter spin locks? u Sometimes, lock usage can be clearly divided into readers and writers • What rules we should follow when using reader-writer spin locks? u One or more readers can concurrently hold the reader lock u Only one writer can hold the writer lock with no concurrent readers • Methods: Initialization: Reader lock: Writer lock:
  • 13.
    Reader-Writer Soin Locks ■Table: Listing of Reader-Writer Spin Lock Methods Acqu lres cjiven lock for reacling Disa bl es Ioca I lnLerru pts and a cq u ires p iven lock foi reed i ng 1 1 S a re•s the ciai rent sta te cif Isea 1 i nteiropt s, d i sa b les Ie ca 1 I nt erru pts, a n d a c q u Ices tlne g iu e n I cock fc›r react i ng Rel e a s e s I oak Rel eas es g iven Iock loca I inLerru pts for re std! eg a n d e n a bl es Rel e a s e s g iven Iock and restores laca I i n Let ru pts Lo the g iven previous state Acqu fres g iven lock for writ Ing D isa bl es Ioca I interrupts ance a cqcJires L h e gi ven lock for vvr i ti r1g S ave s current s t a t e o f Ioca I interru pts, disa bIes loca I interrupts, a net acquires Lhe g Iv e n Iock for wrl ting Rel e a s e s gI ven Iock Rel eases given Iock and ena bl es lac a I i n t e r r u p t s Rel eas es g iven Iock a n d reaLores loca I interrupts to gi ven previous Tries Lo a oqui ire q iven Iock for 'n/ri ti ng ; If unava I la b Ie, returns nonzero Init ia 1i zes g iuen - cc n z ReLcJrns non z ero i f the g i ven lock Is current I y a cqul red, or e Ise It 13
  • 14.
    Semaphores • Semaphores inLinux 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: u Declare semaphores Statically: Initialize a dynamically created semaphore: » -= •-
  • 15.
    Sema hores • Table: Listingof Semaphore Methods Method sema init(struct semaphore *, int) init_MUTEX(struct semaphore •) init MUTEX LOCKED(struct semaphore *) down_interruptiblelstruct down(struct semaphore *) down trylock(struct semaphore *) Description Inltializes the dynamically created semaphore to the given count InftIaIIzes the dynamically created semaphore with a count of one Initializes the dynamically created semaphore with a count of zero (so It s initially locked) Trles to acquire the given semaphore and enter interruptlble sleep If it Is contended Trles to acquire the given semaphore and enter uninterruptibTe sleep if it is contended Tries to acquire the given semaphore and Immediately return nonzero if it is contended Releases the glven semaphore and wakes
  • 16.
    Reader—Writer Semaphores • Similarto 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 VersusSemaphores • 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 Requirement Loss overhead locking, Shoft lock hold titre Long Ioci‹ hold time Need to lock f‹-on inteirript context Recommended Lock Spin loco is pi-efeiied Spin IoCk is p‹ felled Sen np! oie i oi- efeiied Spin lock i iec‹iii ed
  • 18.
    Compleüon Variables • Similarto semaphores u One task waits on the completion variable while another task performs some work u 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 I gethod Table: Completion Variables Methods Descripôon Unit completion (stru t conolet:on *) wait for comoletion(struct complète(stiuct completïcn Initial izes the given dynamlcally created completian vaFiable Waits for the glven completion variable to be signaled ’) Signale any waiting tasks to wake up 18
  • 19.
    BI«?L: The Bid;I«?ernel 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 u 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: Forthe kernel is preemptive, a process can stop running to allow a process of higher priority to run e 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 Function pzeempt di sable () Description Disables kernel preemption by Incrementlng the preemptlon counter Decrement the preemption counter and check and service any pending reschedules if the count is now zero preempt_enable() preempt_enable_no_resched Enables kernel preemption but do not check for any pending reschedules Returns the preemption count 2 u
  • 21.
    Synchronization Summarization • thesimplest 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