Kernel Synchronization I David Ferry Chris Gill Brian
Kernel Synchronization I David Ferry, Chris Gill, Brian Kocoloski CSE 422 S - Operating Systems Organization Washington University in St. Louis, MO 63143 1
Discussion • Recall that the Linux kernel supports both SMP and is preemptive – SMP: symmetric multiprocessing (i. e. ; multiple cores can execute in the kernel concurrently) – Preemptive: (almost) any thread of execution can be preempted and replaced with another • SMP, preemption, and interrupts all create challenges related to safely managing shared data structures CSE 422 S – Operating Systems Organization 2
Simple Example • (From LKD pp. 162) – Assume a user has a bank account with $105 – There are two ATM cards associated with the account – Simultaneously, one card tries to withdraw $100 while the other tries to withdraw $10 – What will happen? CSE 422 S – Operating Systems Organization 3
Single Variable Race Condition If concurrent processes access variable X, new value of X may depend on which one “wins the race” (and the other one overwrites its changes) Thread 1: load X Thread 2: load X X = X + 1 store X “finish time” CSE 422 S – Operating Systems Organization 4
Data Structure Race Condition If two threads are accessing a list, the one that “loses the race” may see invalid data, or worse Node A Next Deleter: Node = A->Next; A->Next = B->Next; Kfree(Node); Node B Next Node C Next Reader: Node = A->Next; Value = Node->val; CSE 422 S – Operating Systems Organization 5
Challenges beyond Races • How do we deal with this problem? By locking shared data structures • Next lecture will discuss various locking mechanisms • For now, we will focus on problems that may arise from locking CSE 422 S – Operating Systems Organization 6
Locking Thread 1: lock( A ); lock( B ); //critical section unlock( A ); unlock( B ); Lock A Lock B Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); CSE 422 S – Operating Systems Organization 7
Locking Thread 1: lock( A ); lock( B ); //critical section unlock( A ); unlock( B ); Lock A Lock B Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); CSE 422 S – Operating Systems Organization 8
Deadlock occurs when a process can never progress while waiting for a lock, e. g. : Thread 1: lock( A ); lock( B ); //critical section unlock( A ); unlock( B ); Lock A Deadlock! Lock B Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); CSE 422 S – Operating Systems Organization 9
Lock Ordering Acquire and release locks in the same order! (Does not solve all deadlocks, but it’s a good place to start) Thread 1: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); Lock A Lock B Thread 2: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); CSE 422 S – Operating Systems Organization 10
Lock Ordering Acquire and release locks in the same order! (Does not solve all deadlocks, but it’s a good place to start) Thread 1: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); Lock A Lock B Thread 2: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); CSE 422 S – Operating Systems Organization 11
Data Structure Locking Example Node A Next Node B Next Deleter: lock( list_lock ); Node = A->Next; A->Next = B->Next; kfree(Node); unlock(list_lock); Node C Next Reader: lock( list_lock ); Node = A->Next; Value = Node->val; unlock( list_lock ); CSE 422 S – Operating Systems Organization 12
How else can deadlock occur? Can deadlock occur on a single core? CSE 422 S – Operating Systems Organization 13
How else can deadlock occur? Can deadlock occur on a single core? Yes! If the same lock can be accessed from process and interrupt context, it can lead to deadlock. CSE 422 S – Operating Systems Organization 14
Deadlock occurs when a process can never progress while waiting for a lock, e. g. : Thread 1: spin_lock( A ); //critical section …. Lock A Taking a lock disables kernel preemption, - so, it will not be scheduled out for another thread But it does not disable hardware interrupts! CSE 422 S – Operating Systems Organization 15
Deadlock occurs when a process can never progress while waiting for a lock, e. g. : Thread 1: spin_lock( A ); //critical section … <interrupt> Lock A Interrupt Handler: spin_lock( A ); //deadlock CSE 422 S – Operating Systems Organization 16
Deadlock occurs when a process can never progress while waiting for a lock, e. g. : Thread 1: spin_lock_irqsave( A ); //critical section <interrupts blocked> spin_unlock_irqrestore( A ); Lock A spin_lock_irqsave - takes lock (disables preemption) - disables interrupts on current processor CSE 422 S – Operating Systems Organization 17
Linux is a Preemptive Kernel Many different contexts can access the same shared data structures, and thus need to perform synchronization • User space processes executing system calls • Kernel threads • Interrupts, softirqs/tasklets Any data structure that can be accessed from different contexts, or from multiple processors, needs to be protected CSE 422 S – Operating Systems Organization 18
Shared Data Synchronization Accessing shared data requires appropriate synchronization Protect data, not code! CSE 422 S – Operating Systems Organization 19
- Slides: 19