CS510 Concurrent Systems Class 1

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Bilgisayar Mühendisliği Bölümü GYTE - Bilgisayar Mühendisliği Bölümü Multithreading the SunOS Kernel J. R. Eykholt, S. R. Kleiman, S. Barton, R. Faulkner,
Kernel Synchronization
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS533 Concepts of Operating Systems Class 4 Linux Kernel Locking Issues.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS533 - Concepts of Operating Systems 1 CS533 Concepts of Operating Systems Class 8 Synchronization on Multiprocessors.
CS510 Concurrent Systems Class 1 Linux Kernel Locking Techniques.
Jonathan Walpole Computer Science Portland State University
CS533 Concepts of Operating Systems Class 17 Linux Kernel Locking Techniques.
CS533 Concepts of Operating Systems Linux Kernel Locking Techniques.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
CS510 Concurrent Systems Introduction to Concurrency.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
CS533 Concepts of Operating Systems Jonathan Walpole.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CS510 Concurrent Systems Jonathan Walpole. RCU Usage in Linux.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Kernel Synchronization in Linux Uni-processor and Multi-processor Environment By Kathryn Bean and Wafa’ Jaffal (Group A3)
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 3.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Linux Kernel Development Chapter 8. Kernel Synchronization Introduction Geum-Seo Koo Fri. Operating System Lab.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
CSE 120 Principles of Operating
Background on the need for Synchronization
Synchronization.
Kernel Synchronization II
Chapter 2: The Linux System Part 3
Threading And Parallel Programming Constructs
Thread Implementation Issues
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Kernel Synchronization I
CS510 Concurrent Systems Class 1a
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CS510 Concurrent Systems Jonathan Walpole.
Linux Kernel Locking Techniques
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CS510 Concurrent Systems Class 1 Linux Kernel Locking Techniques

Intro to kernel locking techniques (Linux) Why do we need locking in the kernel? Which problems are we trying to solve? What implementation choices do we have? Is there a one-size-fits-all solution? CS510 - Concurrent Systems

How does concurrency arise? True concurrency Multiple processors execute instructions simultaneously Pseudo concurrency Instructions of multiple execution sequences are interleaved CS510 - Concurrent Systems

Sources of pseudo concurrency Software-based preemption Voluntary preemption (sleep/yield) Involuntary preemption (preemptible kernel) Scheduler switches threads regardless of whether they are running in user or kernel mode The instructions of multiple threads running in kernel mode are interleaved Hardware preemption Interrupt/trap/fault/exception handlers can start executing at any time Reentrancy A function calls itself CS510 - Concurrent Systems

CS510 - Concurrent Systems Critical sections Sections of code that are subject to concurrent execution in which at least one execution path modifies shared data Locking can be used to provide mutually exclusive access to critical sections Various locking primitives exist in Linux Linux is a symmetric multiprocessing (SMP) preemptible kernel CS510 - Concurrent Systems

CS510 - Concurrent Systems Atomic operators Simplest synchronization primitives Primitive operations that are indivisible Two types methods that operate on integers methods that operate on bits Implementation Assembly language sequences that use the atomic read-modify-write instructions of the underlying CPU architecture CS510 - Concurrent Systems

Atomic integer operators atomic_t v; atomic_set(&v, 5); /* v = 5 (atomically) */ atomic_add(3, &v); /* v = v + 3 (atomically) */ atomic_dec(&v); /* v = v - 1 (atomically) */ printf("This will print 7: %d\n", atomic_read(&v)); Beware: Can only pass atomic_t to an atomic operator atomic_add(3,&v); and { atomic_add(1,&v); atomic_add1(2,&v); } are not same! CS510 - Concurrent Systems

CS510 - Concurrent Systems Spin locks Mutual exclusion for larger (than one operator) critical sections requires additional support Spin locks Single holder locks When lock is unavailable, acquiring process keeps trying CS510 - Concurrent Systems

CS510 - Concurrent Systems Basic use of spin locks spinlock_t mr_lock = SPIN_LOCK_UNLOCKED; spin_lock(&mr_lock); /* critical section ... */ spin_unlock(&mr_lock); spin_lock() Acquires the spinlock using atomic instructions required for SMP spin_unlock() Releases the spinlock CS510 - Concurrent Systems

What if the spin lock holder is interrupted? spinlock_t mr_lock = SPIN_LOCK_UNLOCKED; unsigned long flags; spin_lock_irqsave(&mr_lock, flags); /* critical section ... */ spin_unlock_irqrestore(&mr_lock, flags); spin_lock_irqsave() disables interrupts locally acquires the spinlock using instructions required for SMP spin_unlock_irqrestore() Restores interrupts to the state they were in when the lock was acquired CS510 - Concurrent Systems

What if we’re on a uniprocessor? Previous code compiles to: unsigned long flags; save_flags(flags); /* save previous CPU state */ cli(); /* disable interrupts */ … /* critical section ... */ restore_flags(flags); /* restore previous CPU state */ Hmm, why not just use: … sti(); /* enable interrupts */ CS510 - Concurrent Systems

Dealing with interrupt context Need to know if data being protected is accessed in interrupt context or just normal kernel context Interrupt handlers should not block! Interrupted spin lock holders should not be delayed Use appropriate primitives to manage hardware or software preemption Need to know if interrupts were already enabled or disabled Use appropriate primitives to save and restore CPU state CS510 - Concurrent Systems

Bottom halves, softirqs and tasklets Softirqs, tasklets and BHs are deferrable functions think of them as delayed interrupt handling that is scheduled Softirqs – the basic building block Statically allocated can not be interrupted by another softirq on the same CPU non-preemptive scheduling of softirqs softirqs of the same type can run concurrently on different CPUs synchronize with each other using spin-locks Tasklets – built on softirqs dynamically allocated can not be interrupted by another tasklet on the same CPU tasklets of the same type can not run concurrently on different CPUs BHs – built on softirqs (static, not concurrent) CS510 - Concurrent Systems

Spin locks and deferred functions spin_lock_bh() implements the standard spinlock disables softirqs needed for code outside a softirq that manipulates data also used inside a softirq spin_unlock_bh() Releases the spinlock Enables softirqs CS510 - Concurrent Systems

CS510 - Concurrent Systems Spin lock rules Do not try to re-acquire a spinlock you already hold! it leads to self deadlock! Spinlocks should not be held for a long time Excessive spinning wastes CPU cycles! What is “a long time”? Do not sleep while holding a spinlock! Someone spinning waiting for you will waste a lot of CPU never call any function that touches user memory, allocates memory, calls a semaphore function or any of the schedule functions while holding a spinlock! All these can block. CS510 - Concurrent Systems

CS510 - Concurrent Systems Semaphores Semaphores are locks that are safe to hold for longer periods of time contention for semaphores causes blocking not spinning should not be used for short duration critical sections! Why? safe to sleep with! Can be used to synchronize with user contexts that might block or be preempted Semaphores can allow concurrency for more than one process at a time, if necessary CS510 - Concurrent Systems

Semaphore implementation Implemented as a wait queue and a usage count wait queue: list of processes blocking on the semaphore usage count: number of concurrently allowed holders if negative, the semaphore is unavailable, and absolute value of usage count is the number of processes currently on the wait queue if initialized to 1, the semaphore is a mutex CS510 - Concurrent Systems

CS510 - Concurrent Systems Semaphore operations Down() attempts to acquire the semaphore by decrementing the usage count and testing if its negative blocks if usage count is negative Up() releases the semaphore by incrementing the usage count and waking up one or more tasks blocked on it CS510 - Concurrent Systems

Can you be interrupted when blocked? down_interruptible() Returns –EINTR if signal received while blocked Returns 0 on success down_trylock() attempts to acquire the semaphore on failure it returns nonzero instead of blocking CS510 - Concurrent Systems

CS510 - Concurrent Systems Reader/writer Locks No need to synchronize concurrent readers unless a writer is present reader/writer locks allow multiple concurrent readers but only a single writer (with no concurrent readers) Both spin locks and semaphores have reader/writer variants CS510 - Concurrent Systems

Reader/writer spin locks (rwlock) rwlock_t mr_rwlock = RW_LOCK_UNLOCKED; read_lock(&mr_rwlock); /* critical section (read only) ... */ read_unlock(&mr_rwlock); write_lock(&mr_rwlock); /* critical section (read and write) ... */ write_unlock(&mr_rwlock); CS510 - Concurrent Systems

Reader/writer semaphores (rw_semaphore) struct rw_semaphore mr_rwsem; init_rwsem(&mr_rwsem); down_read(&mr_rwsem); /* critical region (read only) ... */ up_read(&mr_rwsem); down_write(&mr_rwsem); /* critical region (read and write) ... */ up_write(&mr_rwsem); CS510 - Concurrent Systems

Reader/writer lock warnings reader locks cannot be automatically upgraded to the writer variant attempting to acquire exclusive access while holding reader access will deadlock! if you know you will need to write eventually obtain the writer variant of the lock from the beginning or, release the reader lock and re-acquire the lock as a writer CS510 - Concurrent Systems

Big reader locks (br_lock) Specialized form of reader/writer lock very fast to acquire for reading very slow to acquire for writing good for read-mostly scenarios Implemented using per-CPU locks readers acquire their own CPU’s lock writers must acquire all CPUs’ locks CS510 - Concurrent Systems

CS510 - Concurrent Systems Big kernel lock (BKL) A global kernel lock - kernel_flag used to be the only SMP lock mostly replaced with fine-grain localized locks Implemented as a recursive spin lock Reacquiring it when held will not deadlock Usage … but don’t ;) lock_kernel(); /* critical region ... */ unlock_kernel(); CS510 - Concurrent Systems

Preemptible kernel issues Have to be careful of legacy code that assumes per-CPU data is implicitly protected from preemption May need to use new preempt_disable() and preempt_enable() calls Calls are nestable for each n preempt_disable() calls, preemption will not be re-enabled until the nth preempt_enable() call CS510 - Concurrent Systems

CS510 - Concurrent Systems Conclusions Wow! Why does one system need so many different ways of doing synchronization? Actually, there are more ways to do synchronization in Linux, this is just “locking” CS510 - Concurrent Systems

CS510 - Concurrent Systems Conclusions One size does not fit all: need to be aware of different contexts in which code executes (user, kernel, interrupt etc) and the implications this has for whether hardware or software preemption or blocking can occur the cost of synchronization is important, particularly its impact on scalability You only use more than one CPU because you hope to execute faster! Each synchronization technique makes a different performance vs. complexity trade-off Real-time characteristics and I/O are becoming more and more important in general purpose systems CS510 - Concurrent Systems