Synchronization concepts in Linux Kernel
Update: Sep 15: Added RCU Section
What you will learn in this course
- Various concepts related to concurrency like: preemption, context switch, reentrancy, critical section, race condition
- Various Synchronization techniques
- Per CPU Variables
- Atomic Variables
- Spinlocks
- Semaphores
- Mutexes
- Read Write Locks
- Sequence Locks
- Read Copy Update(RCU)
API’s/Macros/Structures:
- spinlock_t, DEFINE_SPINLOCK, spin_lock, spin_unlock, spin_trylock, spin_lock_irqsave, spin_unlock_irqrestore,spin_lock_irq, spin_unlock_irq
- atomic_t, atomic64_t, ATOMIC_INIT, atomic_inc, atomic_dec, atomic_set, atomic_read, atomic_add, atomic_sub,
atomic_dec_and_test, atomic_inc_and_test, atomic_sub_and_test, atomic_add_negative,atomic_add_return, atomic_sub_return, atomic_inc_return, atomic_dec_return,atomic_fetch_add, atomic_fetch_sub, atomic_cmpxchg, atomic_xchg,set_bit, clear_bit, change_bit, test_and_set_bit, test_and_clear_bit, test_and_change_bit,
- NR_CPUS,num_online_cpus,smp_processor_id,get_cpu,put_cpu,DEFINE_PER_CPU,get_cpu_var, put_cpu_var, per_cpu, for_each_online_cpu, alloc_percpu, free_percpu, per_cpu_ptr
- rcu_read_lock, rcu_read_unlock, synchronize_rcu, call_rcu, rcu_assign_pointer, rcu_dereference
- seqlock_t, seqcount_t, DEFINE_SEQLOCK, seqlock_init, write_seqlock, write_sequnlock
- struct rw_semaphore, DECLARE_RWSEM, init_rwsem, down_read, up_read, down_write, up_write, down_read_trylock, down_write_trylock, downgrade_write
- struct rwlock_t, DEFINE_RWLOCK, rwlock_init, read_lock, read_unlock, write_lock, write_unlock
- struct mutex, DEFINE_MUTEX, mutex_init, mutex_lock, mutex_unlock, mutex_trylock, mutex_lock_interruptible, mutex_unlock_interruptible, mutex_is_locked
- struct semaphore, sema_init, DEFINE_SEMAPHORE, down, up, down_interruptible, down_trylock, down_timeout, down_killable
Commands used in the course
- nproc
- ps -eaF
- ps aux
Concurrency
1
Problem
2
Introduction to concurrency
3
Background of Multiprocessing
4
Preemption and context switch in Linux Kernel
5
Preemption in user and kernel space
6
When can kernel preemption happen
7
Example of kernel preemption
8
Reentrancy
9
Synchronization Race Condition and critical regions
10
Causes of concurrency
11
Solution for concurrency
12
Find out maximum number of processors in Kernel
13
Find out which processor is running kernel control path
14
Linux Kernel Module Example of processor id of Kernel Thread
15
Linux Kernel Module Example of processor id on uniprocessor system
Per CPU Variables
1
Introduction
2
Example of Per CPU variables
3
New Interface of Per CPU Variables
4
Example of new interface of Per CPU Variables
5
Example of per cpu
6
Example of for_each_online_cpu
7
Allocating per cpu data at runtime
8
Problems with Per CPU Variables
Atomic Operators
1
Problem Read Modify Write
2
Introduction to Atomic Operators
3
Example of Atomic Operators
4
Common uses of Atomic Operations
5
Atomic Operation and test
6
Atomic add subtract and return
7
More Atomic Operations
8
64-bit Atomic Operations
9
Atomic bitwise operations
10
Atomic bitwise operations with return value
11
Non Atomic bitwise operations
Spin Locks
1
Introduction
2
Spinlock API's
3
Initializing spinlock
4
Spinlock Example of two kernel threads
5
What happens if i acquire a lock which is already held by CPU
6
Implement busyloop using spinlock in char drivers
7
spin_trylock
8
Can i use spinlock if resource is shared between process and interrupt context
9
Is kernel preemption disabled when spinlock is acquired
10
Important points to consider while using spinlock
11
Example of calling msleep in critical section
12
Will spinlock exists on uniprocessor system
13
Implementation of spinlock
Semaphore
1
Introduction
2
Implementation of semaphore
3
Types of semaphores
4
Can I use counting semaphores in critical section
5
Semaphore API
6
Linux Kernel Module Example using semaphore API
7
Linux Kernel Module Example using down and up
8
Linux Kernel Module Example calling down twice
9
Linux Kernel Module Example of producer consumer
10
down_interruptible
11
down_trylock
12
down_timeout
13
down_killable
14
Important points while using semaphore
15
spinlock vs semaphore`
Mutex
1
Introduction
2
Mutex vs semaphore
3
Mutex Implementation in Linux Kernel
4
Mutex API
5
Linux Kernel Module Example using mutex API dynamic initialization
6
Linux Kernel Module Example using mutex API static initialization
7
Linux Kernel Module Example demonstrating calling sleep in critical section
8
mutex_trylock
9
What happens when other thread calls unlock mutex
10
Trying recursive mutex locks
11
mutex_is_locked
12
Which one do you choose between semaphore and mutex
13
Which one do you choose between spinlock and mutex
Read Write Locks
1
Problem Statement
2
Solution
3
Introduction to ReadWrite Spinlock
4
Linux Kernel Module Example using RW Spinlock API
5
What happens when we call read lock and write lock one after another
6
Recursive read locks
7
What happens when writer is waiting and reader arrives
8
Linux Kernel Module Example using RWLOCKS with 3 kernel threads
9
Linux Kernel Module Example using RWLOCKS with 4 kernel threads
Semaphores RWLocks
1
Introduction to ReadWrite Semaphores
2
ReadWrite Semaphore API
3
down_read_trylock and down_write_trylock
4
downgrade_write
5
Recursive write locks
6
Linux Kernel Module Example using multiple threads
Sequential Locks
1
Problem
You can view and review the lecture materials indefinitely, like an on-demand channel.
Definitely! If you have an internet connection, courses on Udemy are available on any device at any time. If you don't have an internet connection, some instructors also let their students download course lectures. That's up to the instructor though, so make sure you get on their good side!