Download presentation
Presentation is loading. Please wait.
1
Kernel Synchronization I
David Ferry, Chris Gill, Brian Kocoloski CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63143
2
CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization
3
CSE 422S – Operating Systems Organization
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 422S – Operating Systems Organization
4
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 X = X + 1 store X Thread 2: “finish time” CSE 422S – Operating Systems Organization
5
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 Node B Next Node C Next Deleter: Reader: Node = A->Next; A->Next = B->Next; Kfree(Node); Value = Node->val; CSE 422S – Operating Systems Organization
6
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 422S – Operating Systems Organization
7
CSE 422S – Operating Systems Organization
Locking Thread 1: lock( A ); lock( B ); //critical section unlock( A ); unlock( B ); Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); Lock A Lock B CSE 422S – Operating Systems Organization
8
CSE 422S – Operating Systems Organization
Locking Thread 1: lock( A ); lock( B ); //critical section unlock( A ); unlock( B ); Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); Lock A Lock B CSE 422S – Operating Systems Organization
9
CSE 422S – Operating Systems Organization
Deadlock 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 ); Thread 2: lock( B ); lock( A ); //critical section unlock( B ); unlock( A ); Lock A Lock B Deadlock! CSE 422S – Operating Systems Organization
10
CSE 422S – Operating Systems Organization
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 ); Thread 2: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); Lock A Lock B CSE 422S – Operating Systems Organization
11
CSE 422S – Operating Systems Organization
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 ); Thread 2: lock( A ); lock( B ); //critical section unlock( B ); unlock( A ); Lock A Lock B CSE 422S – Operating Systems Organization
12
Data Structure Locking Example
Node A Next Node B Next Node C Next Deleter: Reader: lock( list_lock ); Node = A->Next; A->Next = B->Next; Value = Node->val; kfree(Node); unlock( list_lock ); unlock(list_lock); CSE 422S – Operating Systems Organization
13
How else can deadlock occur?
Can deadlock occur on a single core? CSE 422S – Operating Systems Organization
14
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 422S – Operating Systems Organization
15
CSE 422S – Operating Systems Organization
Deadlock 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 422S – Operating Systems Organization
16
CSE 422S – Operating Systems Organization
Deadlock Deadlock occurs when a process can never progress while waiting for a lock, e.g.: Thread 1: spin_lock( A ); //critical section … <interrupt> Interrupt Handler: spin_lock( A ); //deadlock Lock A CSE 422S – Operating Systems Organization
17
CSE 422S – Operating Systems Organization
Deadlock 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 422S – Operating Systems Organization
18
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 422S – Operating Systems Organization
19
Shared Data Synchronization
Accessing shared data requires appropriate synchronization Protect data, not code! CSE 422S – Operating Systems Organization
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.