Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Synchronization in Linux Kernel Programming
Rating: 4.6 out of 5(206 ratings)
2,534 students

Synchronization in Linux Kernel Programming

Linux Kernel Programming - Synchronization and Concurrency
Created byLinux Trainer
Last updated 11/2020
English

What you'll learn

  • Synchronization concepts in Linux Kernel

Course content

11 sections120 lectures4h 31m total length
  • Problem1:01

    Illustrate a memory race in kernel programming, where a function returns a pointer; two processes see null and allocate memory, overwriting each other and causing memory loss; session discusses remedies.

  • Introduction to concurrency3:01

    Explore concurrency and context switching, distinguishing the illusion of parallelism on a single core from true parallelism on multi-core systems, and learn to identify cores and processor usage.

  • Background of Multiprocessing7:53

    Explain how multiprocessors evolved from private per-CPU operating systems to a single symmetric multiprocessing kernel with per-region locks, solving system-call bottlenecks and avoiding the big kernel lock.

  • Preemption and context switch in Linux Kernel3:43
  • Preemption in user and kernel space2:47

    Preemption forcefully switches running processes between user space and kernel space, making user programs preemptible and avoiding kernel lockups when loops run in kernel space with the config_preempt option.

  • When can kernel preemption happen0:46

    Trigger kernel preemption when returning to kernel space from an interrupt handler, or when a kernel task calls schedule or blocks and calls schedule, causing a context switch.

  • Example of kernel preemption2:12
  • Reentrancy3:12

    Explore the kernel control path, system calls and interrupts, and how the linux kernel remains re-entrant to support concurrent kernel mode execution on a uniprocessor, using locking for shared data.

  • Synchronization Race Condition and critical regions2:10

    Learn how synchronization prevents race conditions in the Linux kernel by protecting global data in critical regions, with examples of non re-entrant functions and preemption.

  • Causes of concurrency1:02

    Identify the main causes of concurrency in the Linux kernel, including interrupts, softirq and tasklets, preemption, sleeping, and symmetric multiprocessing.

  • Solution for concurrency2:11
  • Find out maximum number of processors in Kernel2:23

    Determine the maximum number of CPUs the smp kernel can support using the nr_cpus variable and kernel configuration, override it with a kernel parameter, and check online CPUs via num_online_cpus.

  • Find out which processor is running kernel control path1:37

    Identify the processor running the kernel control path by using SNP_processor_id to obtain the current processor number, print it, and verify it against user-space observations.

  • Linux Kernel Module Example of processor id of Kernel Thread1:39

    Explore how a Linux kernel thread prints its processor id across init, thread function, and exit, revealing scheduler-driven switching between processors.

  • Linux Kernel Module Example of processor id on uniprocessor system2:54

    Explore how a uniprocessor system handles kernel and user processes through a linux kernel module example, illustrating proc cpuinfo shows one processor, and how scheduling and preemption enable apparent multi-tasking.

Requirements

  • Should be able to write/understand Hello World Linux Kernel Module
  • Should be able to write/understand Linux Kernel Modules for /proc filesystem

Description

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

Who this course is for:

  • Linux Kernel Developers interested in learning various synchronization techniques