Kernel Critical Sections

Similar documents
Kernels and Locking. Luca Abeni

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Developing Real-Time Applications

Operating Systems. Synchronisation Part I

Real Time Operating Systems and Middleware

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Synchronization Spinlocks - Semaphores

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

Synchronization for Concurrent Tasks

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Real-Time Performance of Linux. OS Latency

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Linux kernel synchronization. Don Porter CSE 506

Dealing with Issues for Interprocess Communication

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Locks. Dongkun Shin, SKKU

Spinlocks. Spinlocks. Message Systems, Inc. April 8, 2011

Synchronization for Concurrent Tasks

Operating Systems. Synchronization

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Synchronization COMPSCI 386

Prof. Dr. Hasan Hüseyin

Lecture 5: Synchronization w/locks

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronising Threads

An Implementation Of Multiprocessor Linux

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CHAPTER 6: PROCESS SYNCHRONIZATION

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

More on Synchronization and Deadlock

Synchronization I. Jo, Heeseung

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Multiprocessor Systems. Chapter 8, 8.1

Concurrency and Race Conditions (Linux Device Drivers, 3rd Edition (

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Multiprocessor System. Multiprocessor Systems. Bus Based UMA. Types of Multiprocessors (MPs) Cache Consistency. Bus Based UMA. Chapter 8, 8.

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

The University of Texas at Arlington

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Multiprocessor Systems. COMP s1

Computer Core Practice1: Operating System Week10. locking Protocol & Atomic Operation in Linux

Process/Thread Synchronization

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Deadlocks. Tore Larsen. With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Building Concurrency Primitives

Process/Thread Synchronization

Mutex Implementation

Lesson 6: Process Synchronization

Synchronization Principles

Chapter 6 Process Synchronization

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Linux kernel synchronization

The University of Texas at Arlington

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions

Multiprocessors and Locking

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

LOCKDEP, AN INSIDE OUT PERSPECTIVE. Nahim El

Concurrency and Synchronisation

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

Concurrency and Synchronisation

Chapter 6: Process Synchronization

[537] Locks. Tyler Harter

IV. Process Synchronisation

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

Concurrency Race Conditions and Deadlocks

March 10, Linux Live Patching. Adrien schischi Schildknecht. Why? Who? How? When? (consistency model) Conclusion

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6: Process Synchronization

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 5: Process Synchronization

Process Synchronization (Part I)

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Chapter 5: Process Synchronization

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Systèmes d Exploitation Avancés

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Problem Set 2. CS347: Operating Systems

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Synchronization I. To do

Implementing Transactional Memory in Kernel space

CSE 120 Principles of Operating Systems

Tasks. Task Implementation and management

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and

Linux Synchronization Mechanisms in Driver Development. Dr. B. Thangaraju & S. Parimala

COMP 3430 Robert Guderian

Transcription:

Kernel Critical Sections Real Time Operating Systems and Middleware Luca Abeni luca.abeni@unitn.it

Critical Sections in Kernel Code Old Linux kernels used to be non-preemptable... Kernel Big critical section Mutual exclusion was not a problem... Then, multiprocessor systems changed everything First solution: Big Kernel Lock very bad! Removed BKL, and preemptable kernels,... Multiple tasks can execute inside the kernel simultaneously mutual exclusion is an issue! Multiple critical sections inside the kernel

Enforcing Mutual Exclusion Mutual exclusion is traditionally enforced using mutexes Mutexes are blocking synchronisation objects A task trying to acquire a locked mutex is blocked......and the scheduler is invoked! Good solution for user-space applications... But blocking is sometimes bad when in the kernel!

Blocking is Bad When... Atomic Context Code in task context can sleep (task blocked)...but some code does not run in a task context (example: IRQ handlers)! Other situations (ex: interrupts disabled) Efficiency small critical sections using mutexes, a task would block for a very short time Busy-waiting can be more efficient (less context switches)!

Summing up... In some particular situations......we need a way to enforce mutual exclusion without blocking any task This is only useful in kernel programming Remember: in general cases, busy-waiting is bad! So, the kernel provides a spinning lock mechanism To be used when sleeping/blocking is not an option Originally developed for multiprocessor systems

Spinlocks - The Origin spinlock: Spinning Lock Protects shared data structures in the kernel Behaviour: similar to mutex (locked / unlocked) But does not sleep! Basic idea: busy waiting (spin instead of blocking) Might neeed to disable interrupts in some cases

Spinlocks - Operations Basic operations on spinlocks: similar to mutexes Biggest difference: lock() on a locked spinlock lock() on an unlocked spinlock: change its state lock() on a locked spinlock: spin until it is unlocked Only useful on multiprocessor systems unlock() on a locked spinlock: change its state unlock() on an unlocked spinlock: error!!!

Spinlocks - Implementation int lock = 1; void lock(int *sl) { while (TestAndSet(sl, 0) == 0); } void unlock(int *sl) { *sl = 1; } A possible algorithm (using test and set) lock: decb %0 jns 3 2: cmpb $0,%0 jle 2 jmp lock 3:... unlock: movb $1,%0 Assembler implementation (in Linux)

Spinlocks and Livelocks Trying to lock a locked spinlock results in spinning spinlocks must be locked for a very short time If an interrupt handler interrupts a task holding a spinlock, livelocks are possible... τ i gets a spinlock SL An interrupt handler interrupts τ i......and tries to get the spinlock SL The interrupt handler spins waiting for SL But τ i cannot release it!!!

Avoiding Livelocks Resource shared with ISRs possible livelocks What to do? The ISR should not run during the critical section! When a spinlock is used to protect data structures shared with interrupt handlers, the spinlock must disable the execution of such handlers! In this way, the kernel cannot be interrupted when it holds the spinlock!

Spinlocks in Linux Defining a spinlock: spinlock t my lock; Initialising: spin lock init(&my lock); Acquiring a spinlock: spin lock(&my lock); Releasing a spinlock: spin unlock(&my lock); With interrupt disabling: spin lock irq(&my lock), spin lock bh(&my lock), spin lock irqsave(&my lock, flags) spin unlock irq(&my lock),...

Spinlocks - Evolution On UP systems, traditional spinlocks are no-ops The irq variations are translated in cli/sti This works assuming only on execution flow in the kernel non-preemptable kernel Kernel preemptability changes things a little bit: Preemption counter, initialised to 0: number of spinlocks currently locked spin lock() increases the counter spin unlock() decreases the counter

Spinlocks and Kernel Preemption preemption counter: increased when entering a critical section, decreased on exit When exiting a critical section, check if the scheduler can be invoked If the preemption counter returns to 0, spin unlock() calls schedule()......and returns to user-space! Preemption can only happen on spin unlock() (interrupt handlers lock/unlock at least one spinlock...)

Spinlocks and Kernel Preemption In preemptable kernels, spinlocks behaviour changes a little bit: spin lock() disables preemption spin unlock() might re-enable preemption (if no other spinlock is locked) spin unlock() is a preemption point Spinlocks are not optimised away on UP anymore Become similar to mutexes with the Non-Preemptive Protocol (NPP) Again, they must be held for very short times!!!

Sleeping in Atomic Context atomic context: CPU context in which it is not possible to modify the state of the current task Interrupt handlers Scheduler code Critical sections protected by spinlocks... Do not call possibly-blocking functions from atomic context!!!