Locking in Linux kernel
Locking in Linux kernel
2
3
1
“CPU” running endlessly 3
entrance 4 4
1st floor
Human exit
• Interrupt handlers
• Exception handlers
• User-space threads in kernel(system calls)
• Kernel threads(idle, work queue, pdflush…)
• Bottom halves(soft irq, tasklet,BH...)
Post-KCP: Is mm subsystem a KCP?
NO, But mm codes are called by KCP.
OS review: Kernel Control Paths
What is the composition of a kernel?
int i = 0
KCP 1: KCP 2:
load i; // i = 0
... load i; // i = 0
inc i; inc i;
store i; // i = 1 ...
store i; // i = 1
i = 1, wrong
The result is wrong because of accessing “i” at the same time.
Locking in Linux
What is Locking? A Simple example. (cont.)
int i = 0
KCP 1: KCP 2:
<locking starts>
load i; // i = 0 <locking starts>... failed.
inc i; waiting..
store i; // i = 1 waiting..
<locking ends> <locking succeeds>
load i; //now i = 1
inc i;
store i; // i = 2
<locking ends> x86 lock directive.:)
i = 2, right!
Locking in Linux
Another example...
My heart
will panic.
! ! Give me
Give me ! your heart.
your heart. !
Locking releases.
KCP 1 KCP 2
• Locking the queue • Locking the queue
• Succeeded: acquired lock • Failed: waiting…
• Access queue • Waiting…
• Unlock the queue • …
• Succeeded: acquired lock
• Access queue
• Unlock the queue.
Cited from LKD by R. Love
Locking in Linux
Locking(Sync.) is important.
Let's get into the Locking details.
Locking in Linux
• 1 Atomics operations
• 2 Memory barriers
• 3 Spin locks
• 4 Reader-writer spin locks
• 5 Semaphores
• 6 Reader-writer semaphores
Locking in Linux
Various Locking mechanisms. (cont.)
– 7 Condition(Completion) Variables
– 8 Sequence locks
– 9 Mask Interrupts(local and global)
– 10 Mask Bottom Halves
– 11 Disable Kernel Preemption
– 12 Read-Copy Update
–
– Big Kernel Lock - Historical, will be removed
– FUTEX ? - NO
1 Atomics Operations
• atomic ops is for the concurrency caused by MP. not for other
concurrencies caused by preemption, sleep...
• atomic operations mechanisms(SMP env):
– cpu guaranteed atomic ops: read/write a byte, alined word...
– lock prefix: add, adc, and, cmpxchg, cmpxch8b, dec, inc, neg, not,
or, sbb, sub ,xor, xadd, btc,bts, btr
– xchg is automatically added lock prefix.
– cache coherency protocols.
1 Atomics Operations
<asm/atomic.h> <asm/bitops.h>
up() complete()
lock; inc %0 spin_lock(cv->wait.lock)
... //inc cv->done
spin_lock(sem->wait.lock) //wait queue ops;
//..., wait queue ops; spin_unlock(cv->wait.lock)
spin_unlock(sem->wait.lock)
Writers Readers
10 Mask interrupts(local and global)
<linux/interrupt.h><asm/system.h><kernel/irq/manage.c><asm/processor.h>
(old copy)
write create a copy
Change new copy
Big Kernel Lock: history
• Linux 2.0 - BKL about 1996 - SMP
• BSD/OS 4.x:
• FreeBSD 4.x: XXX – Giant (2000 -)
• goal : fine-grained locking
•
• Dragonfly BSD: forked from FreeBSD 4.x
• goal: lockless mem allocator and scheduling system
FUTEX
• Fast User Space Mutex
• It's for user-space threads synchronization.
• It's not a locking mechanism for kernel.
• It is implemented in kernel.
Relation of different locks implementations
simple complex
mem barriers
con.variable
preempt disable
RCU
disable_bh
Locking in Linux kernel
– init/Kconfig
– fs/Kconfig.binfmt
– fs/Kconfig
– drivers/Kconfig.binfmt
– lib/Kconfig
– ...
• CONFIG_DEBUG_KERNEL
– CONFIG_DEBUG_SPINLOCK, CONFIG_SPINLOCK_SLEEP
– CONFIG_DEBUG_STACKOVERFLOW, CONFIG_4KSTACKS
– CONFIG_KDB(patches)
– ...
Locking in Linux kernel