Kernel Locking Techniques by Robert Love presented by Scott Price.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
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,
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
CS533 Concepts of Operating Systems Class 4 Linux Kernel Locking Issues.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
CS510 Concurrent Systems Class 1
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 Concurrency: Deadlock and Starvation Chapter 6.
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.
CS533 Concepts of Operating Systems Class 17 Linux Kernel Locking Techniques.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
CS533 Concepts of Operating Systems Linux Kernel Locking Techniques.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
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 CSE 411 Multi-processor Operating Systems Multi-processor Operating Systems Dec Lecture 30 Instructor: Bhuvan Urgaonkar.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
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.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS533 Concepts of Operating Systems Jonathan Walpole.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
The read-copy-update mechanism for supporting real-time applications on shared-memory multiprocessor systems with Linux Guniguntala et al.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Kernel Synchronization in Linux Uni-processor and Multi-processor Environment By Kathryn Bean and Wafa’ Jaffal (Group A3)
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Linux Kernel Development Chapter 8. Kernel Synchronization Introduction Geum-Seo Koo Fri. Operating System Lab.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Deadlock and Starvation
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Deadlock and Starvation
Kernel Synchronization II
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Lecture 2 Part 2 Process Synchronization
Conditions for Deadlock
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
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CS510 Concurrent Systems Jonathan Walpole.
Linux Kernel Locking Techniques
CSE 153 Design of Operating Systems Winter 2019
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

Kernel Locking Techniques by Robert Love presented by Scott Price

Introduction Locking can be very tough to implement correctly Poorly designed locking can result into code that is hard to read and performs poorly. Locking is needed to synchronize code paths in the kernel. Critical sections require combinations of concurrent or re-entrance protection and proper ordering of events Improper designed locking can result in crashes and other oddities. ++i shared is dangerous so all shared memory in the kernel needs to have some type of locking. two threads can access i at the same time and could increment i once or could increment i twice.

I++ example No Lock Lock

SMP Locks in Uniprocessor Kernel Interrupt handlers, preemptible kernel and any code that can block can all create locking issues If your running SMP machine or not, anyone could use your code. So you have to make sure code is safe on both SMP and unicore combinations of CONFIG_SMP and CONFIG_PREEMPT in varying locking support Lock everything appropriate and make sure all situations are covered.

Bad things that Happen When we don’t Lock Kernel

Complex locking primitives are built off atomic operations. They are the building blocks of kernel locks Atomic operations are like add and subtract but perform in one uninterruptible operation two types of Atomic operators exist. Ones that work on integers and ones that work on bits You cannot pass an atomic type to anything but an atomic operation. Some architectural limitations do not expect atomic operations to have more then 24 bits Atomic Operations

Atomic Operations and interrupts When we have shared data that is accessed by normal kernel code and Interrupt handler code, we need to have atomic operations to both acquire the lock and disable interrupts at the same time. If we do not use an atomic operations to both lock and disable interrupts, an interrupt can possibly happen during a locking phase. The interrupt will try to acquire the lock and can possibly deadlock Besides this issue we can use locks without disabling interrupts

Spinlocks Are a Simple single-holder lock. If process attempts to acquire a lock and it is not available, the process will keep trying to acquire the lock (spinning) until the lock is available. Once the lock is acquired you can process the critical region and release the lock SMPUnicore

Spinlocks Spinlocks should only be used when the lock is not going to be held very long. Or other processes will spin doing nothing. Do not use spin locks on processes that will go to sleep. Or else a deadlock could occur. spin_lock_irqsave() and spin_unlock_irqrestore() are atomic operations that acquire the lock and disable interrupts with one atomic operation we need to both acquire the lock and disable interrupts in one operation or else we could have a race condition

Semaphores Semaphores are sleeping locks because they can cause a task to sleep on contention instead of spin. They are used when you are going to take hold of a lock for a long time There is overhead for putting task to sleep and waking task up. This should not be used for locks that are held for a short amount of time. Semaphores have a queue for how many threads are waiting. If the number is positive, this is the amount of threads that are waiting on the queue. If negative, the semaphore is unavailable and the absolute value is the usage count

Semaphore Semaphores manipulate 2 methods up(), down() they increment/decrement the wait queue up_interruptible() the calling process is added to the wait queue and blocked down_interruptible() the process obtains the semaphore up and down increment of semaphores have to atomic operations

Reader/Writer Locks is safe for multiple threads to read data concurrently. As long as nothing modifies the data R/W locks allow multiple concurrent readers but only a single writer. If data is access, it naturally divides into clear reading and writing patterns especially with greater amount of reading time, then writing time. If so R/W locks are preferred R/W spinlock is called rwlock, similar to spinlock with the exception of separate R/W locking R/W locks can give appreciable optimization Attempting to acquire exclusive access while holding reader access will deadlock

reader/writer locks spinlock version semaphore version

Provide a spinning lock that is very fast to acquire for reading but incredibly slow to acquire for writing Ideal for situations with many readers and a few writers Only used for special cases. Big-Reader Locks

Global kernel lock BKL Still exist today but lots of work went into removing it for more fine-grained localized locks a spinning lock that is recursive so two consecutive request will not deadlock the process like a spinlock will do A process can sleep and enter the scheduler while holding. When a process is holding, if another process enters the scheduler, the lock is dropped so other processes can obtain it. It exist but shouldn't use other locks are more efficient Big-Kernel locks

Preemptive Control Linux kernel is fully preemptible Allows processes to be preempted by higher priority processes even if there is a process running in the kernel Preemptible kernel creates synchronization issues of SMP Kernel preemption is synchronized by SMP locks so most issues are solved writing SMP safe code. Introduces a few new locking issues For preemptible situations, we can disable and enable the preemption if needed

Conclusion SMP reliability and scalability in the linux kernel are improving since SMP was introduce Future holds better performance Kernel developers should do their part by writing code that implements smart, sane, proper locking with an eye to both scalability and reliability!! OR ELSE!

This could happen!!!