Kernel Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Tutorial 3 - Linux Interrupt Handling -
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Mutual Exclusion.
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.
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
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.
CS510 Concurrent Systems Class 1
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Introduction to Embedded Systems
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
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.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
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.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Processes and threads.
Microprocessor Systems Design I
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Kernel Synchronization
Atomic Operations in Hardware
Atomic Operations in Hardware
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Process Description and Control
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Implementing Mutual Exclusion
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
Basic Synchronization
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
CSCI1600: Embedded and Real Time Software
CSE 542: Operating Systems
Kernel Synchronization
Presentation transcript:

Kernel Synchronization with material from Understanding the Linux Kernel (O’Reilly) Printed on Friday, April 14, 2017 at 4:01 AM.

Synchronization In The Kernel Past lectures: Synchronization constructs Today’s lecture: What does this stuff look like in an OS? We have source code for Linux… We mostly run on x86 platforms Lets look at some specifics.

Prevention of context switch Disabling Interrupts Key observations: On a uni-processor, an operation is atomic if no context-switch is allowed in the middle of the operation Context switch occurs because of: Internal events: system calls and exceptions External events: interrupts Mutual exclusion can be achieved by preventing context switch Prevention of context switch Eliminate internal events: easy (under program control) Eliminate external events: disable interrupts Hardware delays the processing of interrupts until interrupts are enabled

Lock Implementation: A Naïve Solution Lock::Acquire() { disable interrupts; } Lock::Release() { enable interrupts;} Will this work on a uni-processor? What is wrong with this solution? Once interrupts are disabled, the thread can’t be stopped  Can starve other threads Critical sections can be arbitrarily long  Can’t bound the amount of time needed to respond to interrupts But this is used all over the place in uniprocessor OSes to do short tasks. It is a large source of bugs. What would typical failure conditions be?

Disabling Interrupts in Linux Usually some small number of interrupt levels, statically assigned (e.g., reset = 0, timer = 1, network = 3, disk = 4, software = 7) When you “disable interrupts” you disable them for your level and higher. When you reenable interrupts, you need to do so at the previous level. Where do you store the level in the meantime? A. Local variable B. Global variable C. Hardware register unsigned long flags; local_irq_save( flags ); // Disable & save do_whatever; local_irq_restore( flags ); // Reenable

Make sure to release your locks along every possible execution path. Using Locks Correctly Make sure to release your locks along every possible execution path. unsigned long flags; local_irq_save( flags ); // Disable & save … if(somethingBad) { local_irq_restore( flags ); return ERROR_BAD_THING; } local_irq_restore( flags ); // Reenable return 0;

Entering Linux An OS is a server that responds to requests, from user code and from devices. How to enter the Linux kernel int 0x80, which is the system call instruction on the x86 by convention. External device sends a signal to a programmable interrupt controller (PIC) by using an IRQ (interrupt request) line, and that interrupt is enabled. A process in kernel mode causes a page fault. A process in a multi-processor system executing in kernel mode raises an inter-processor interrupt. Interrupt, exception, or softirq handling can interrupt a process running in kernel mode, but when the handler terminates, the kernel control path of the process resumes. Abstractly what’s going on here is that there’s a set of resources (the mutex lock and some empty buffers) that both processes are trying to acquire. There’s a possible set of interleavings of these processes that leads to deadlock. — Both will be in the waiting state and neither can leave the waiting state without the other one executing (which can’t happen!). — This assumes that these two processes are the only uses using the buffers (and that there’s only one empty buffer). Deadlock here is still the user’s fault. — The problem is due to a bug in the way they are coordinating their access to a set of shared resources.

Linux Synchronization Primitives Technique Description Scope Atomic Operation Atomic read-modify-write instruction All CPUs Memory barrier Avoid instruction re-ordering Local CPU Spin lock Lock with busy wait (readers/writers spin locks) Semaphore Lock with blocking wait (R/W semaphores) Local interrupt disable Forbid interrupt handling on single CPU Local softirq disable Forbid deferrable function handling on a single CPU Global interrupt disable Forbid interrupt and softirq handling on all CPUs Abstractly what’s going on here is that there’s a set of resources (the mutex lock and some empty buffers) that both processes are trying to acquire. There’s a possible set of interleavings of these processes that leads to deadlock. — Both will be in the waiting state and neither can leave the waiting state without the other one executing (which can’t happen!). — This assumes that these two processes are the only uses using the buffers (and that there’s only one empty buffer). Deadlock here is still the user’s fault. — The problem is due to a bug in the way they are coordinating their access to a set of shared resources.

Scope of Synch Primitives The test&set instruction is local to a single CPU in a multi-CPU system. A. True B. False

Linux provides wrappers for these operations. Atomic Operations Assembly language instructions that make 0 or 1 aligned memory access (a 32-bit aligned access has the last 2 address bits equal to zero). Read-modify-write instructions (like inc and dec) with the lock prefix. Locks the memory bus. Linux provides wrappers for these operations. atomic_set(v,i) sets *v=i atomic_inc_and_test(v) Add 1 to *v and return 1 if value is now zero, 0 otherwise.

Compiler reorders your memory accesses. Memory Barriers Compiler reorders your memory accesses. Memory barrier says, “wait here until all outstanding memory operations have completed.” rmb() expands to asm volatile(“lock;addl $0,0(%%esp)”:::”memory”); volatile – disables compiler reorder of instruction memory – forces compiler to assume any RAM can change from this instruction. lock prefix locks memory bus, and requires all previous reads to complete. Example use new->next = list_element->next; wmb(); list_element->next = new;

Spin Locks CPU “spins,” executing instructions waiting for lock to free. Only useful on multi-processors Insures only one kernel thread runs a routine at a time Value of 1 means lock is free spin_lock(spinlock_t slp) { 1: lock; decb slp jns 3f 2: cmpb $0, slp pause // p4-reduces power jle 2b // back compat rep;nop jmp 1b 3:

R/W Spin Locks – space optimized data structure Many locks in kernel, so they should be small. Interpret bit ranges. Unlocked flag 2’s complement reader count 24b 23b 0b 0x01000000 – Idle, not locked and no readers 0x00000000 – Aquired for writing, no readers 0x00FFFFFE – Aquired by 2 readers read_lock() = lock; subl $1, (rwlp)\n jns 1f read_unlock() = lock; incl rwlp rw_lock() = lock; subl %0x01000000, (rwlp)\n jz 1f rw_unlock() = lock; addl $0x01000000, rwlp

down() – acquire up() – release down_interruptable() Semaphores Kernel semaphores suspend a waiting process, so can’t be called from interrupt handlers and deferrable functions. atomic_t count; // 1=available 0=busy -1=one waiting wait_queue; // wait queue list int sleepers; //flag, 1 if sleepers, optimization down() – acquire up() – release down_interruptable() Used by device drivers. Suspend me, but if I get a signal, take me off the wait queue & return error. Read/write semaphores. Allows multiple readers. Queues waiters, so it is fair to writers. Semaphore implementation only locks when manipulating the sleep queue.

Linux Use of Semaphores Linux uses semaphores in which processing path (primarily)? A. Interrupt Handlers B. System call service routines C. Deferrable kernel functions D. Device drivers E. Kernel threads

Completions Solving race condition with temporary Semaphores Thread A, CPU 0 Alloc semaphore S Pass sema to B down(S) free(S) Thread B, CPU 1 Accept sema S up(S) up(S) and free(S) execute concurrently. lock only protects sleep queue, not whole semaphore Lock protects entire Completion. Slower, but safer.

Local Interrupt Disable local_irq_disable()/local_irq_enable() Disables and reenables interrupts Executes cli/sti on x86 But interrupts can be nested, so what interrupt level to we return to? unsigned long flags; local_irq_disable(flags); …Read or update a data structure… local_irq_enable(flags); Disable clears the IF flag of the eflags register, and saves register in variable. Enable restores previous register value.

Uses of Synchronization in the Kernel Short Kernel Critical Sections Disable interrupts (& grab spin lock on MPs) Long Critical Sections Separated into “top” and “bottom” top - disable interrupts (& grab spin lock on MPs) bottom – bottom halves don’t interrupt each other, so no sync needed on UP. On MP uses a lock to protect data structures from concurrent access. Interrupt Protection levels top-half interrupt handlers bottom-half interrupt handlers kernel-system service routines user-mode programs (preemptible) increasing priority