Technical requirementsCritical sections, exclusive execution, and atomicityWhat is a critical section?A classic case – the global i ++Concepts – the lockCritical sections – a summary of key pointsData races – a more formal definitionConcurrency concerns within the Linux kernelMulticore SMP systems and data racesPreemptible kernels, blocking I/O, and data racesHardware interrupts and data racesLocking guidelines and deadlockMutex or spinlock? Which to use whenDetermining which lock to use – in theoryDetermining which lock to use – in practiceUsing the mutex lockInitializing the mutex lockCorrectly using the mutex lockMutex lock and unlock APIs and their usageMutex lock – via [un]interruptible sleep?Mutex locking – an example driverThe mutex lock – a few remaining pointsMutex lock API variantsThe semaphore and the mutexPriority inversion and the RT-mutexInternal designUsing the spinlockSpinlock – simple usageSpinlock – an example driverTest – sleep in an atomic contextTesting the buggy module on a 6.1 debug kernelLocking and interruptsScenario 1 – driver method and hardware interrupt handler run serialized, sequentiallyScenario 2 – driver method and hardware interrupt handler run interleavedScenario 2 on a single-core (UP) systemScenario 2 on a multicore (SMP) systemSolving the issue on UP and SMP with the spin_[un]lock_irq() API variantScenario 3 – some interrupts masked, driver method and hardware interrupt handler run interleavedInterrupt handling, bottom halves, and lockingInterrupt handling on Linux – a summary of key pointsBottom halves and lockingUsing spinlocks – a quick summaryLocking – common mistakes and guidelinesCommon mistakesLocking guidelinesSolutionsSummaryQuestionsFurther reading