W4118 Operating Systems. Instructor: Junfeng Yang



Similar documents
A Survey of Parallel Processing in Linux

SYSTEM ecos Embedded Configurable Operating System

Instruction Set Architecture

Linux Process Scheduling Policy

Assembly Language: Function Calls" Jennifer Rexford!

CSC 2405: Computer Systems II

Lecture 6: Semaphores and Monitors

Kernel Synchronization and Interrupt Handling

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

Understanding Hardware Transactional Memory

Performance Evaluation and Optimization of A Custom Native Linux Threads Library

An Implementation Of Multiprocessor Linux

CS 33. Multithreaded Programming V. CS33 Intro to Computer Systems XXXVII 1 Copyright 2015 Thomas W. Doeppner. All rights reserved.

CPU performance monitoring using the Time-Stamp Counter register

Lecture 27 C and Assembly

Computer Organization and Architecture

Synchronization. Todd C. Mowry CS 740 November 24, Topics. Locks Barriers

W4118 Operating Systems. Instructor: Junfeng Yang

Linux scheduler history. We will be talking about the O(1) scheduler

Last Class: Semaphores

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Linux Driver Devices. Why, When, Which, How?

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Chapter 6, The Operating System Machine Level

Operating System Overview. Otto J. Anshus

Performance monitoring with Intel Architecture

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Priority Based Implementation in Pintos

CS414 SP 2007 Assignment 1

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Multi-core Programming System Overview

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

Shared Address Space Computing: Programming

Linux Scheduler. Linux Scheduler

Java Memory Model: Content

Lecture 6: Interrupts. CSC 469H1F Fall 2006 Angela Demke Brown

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

The Linux Kernel: Process Management. CS591 (Spring 2001)

Semaphores and Monitors: High-level Synchronization Constructs

Thomas Fahrig Senior Developer Hypervisor Team. Hypervisor Architecture Terminology Goals Basics Details

Linux Kernel Networking. Raoul Rivas

A Fast Inter-Kernel Communication and Synchronization Layer for MetalSVM

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Virtualization for Cloud Computing

Multi-Threading Performance on Commodity Multi-Core Processors

Intel DPDK Boosts Server Appliance Performance White Paper

An Introduction to the ARM 7 Architecture

Why Computers Are Getting Slower (and what we can do about it) Rik van Riel Sr. Software Engineer, Red Hat

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

1. Computer System Structure and Components

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 1 Computer System Overview

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

ELEC 377. Operating Systems. Week 1 Class 3

İSTANBUL AYDIN UNIVERSITY

8085 INSTRUCTION SET

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

MACHINE ARCHITECTURE & LANGUAGE

A Comparison Of Shared Memory Parallel Programming Models. Jace A Mogill David Haglin

Introduction to CUDA C

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Input / Output and I/O Strategies

Distributed Systems [Fall 2012]

Intel 8086 architecture

MICROPROCESSOR AND MICROCOMPUTER BASICS

The Native POSIX Thread Library for Linux

Under The Hood: The System Call

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

First-class User Level Threads

Chapter 3: Operating-System Structures. Common System Components

Instruction Set Architecture (ISA)

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

Chapter 7D The Java Virtual Machine

RTOS Debugger for ecos

Chapter 3 Operating-System Structures

Operating Systems. Lecture 03. February 11, 2013

Chapter 2: OS Overview

Monitors, Java, Threads and Processes

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Outline of this lecture G52CON: Concepts of Concurrency

Virtualization Technologies

x64 Servers: Do you want 64 or 32 bit apps with that server?

Introduction. What is an Operating System?

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

W4118 Operating Systems. Junfeng Yang

How To Write A Windows Operating System (Windows) (For Linux) (Windows 2) (Programming) (Operating System) (Permanent) (Powerbook) (Unix) (Amd64) (Win2) (X

OPERATING SYSTEMS STRUCTURES

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Transcription:

W4118 Operating Systems Instructor: Junfeng Yang

Learning goals of this lecture Different flavors of synchronization primitives and when to use them, in the context of Linux kernel How synchronization primitives are implemented for real Portable tricks: useful in other context as well (when you write a high performance server) Optimize for common case 2

Synchronization is complex and subtle Already learned this from the code examples we ve seen Kernel synchronization is even more complex and subtle Higher requirements: performance, protection Code heavily optimized, fast path often in assembly, fit within one cache line 3

Recall: Layered approach to synchronization Hardware provides simple low-level atomic operations, upon which we can build high-level, synchronization primitives, upon which we can implement critical sections and build correct multi-threaded/multi-process programs Properly synchronized application High-level synchronization primitives Hardware-provided low-level atomic operations 4

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 5

Architectural dependency Implementation of synchronization primitives: highly architecture dependent Hardware provides atomic operations Most hardware platforms provide test-and-set or similar: examine and modify a memory location atomically Some don t, but would inform if operation attempted was atomic 6

Memory barrier motivation Evil compiler! Reorder code as long as it correctly maintains data flow dependencies within a function and with called functions Evil hardware! Reorder instruction execution as long as it correctly maintains register flow dependencies Reorder memory modification as long as it correctly maintains data flow dependencies 7

Memory barrier definition Memory Barriers: instructions to compiler and/or hardware to complete all pending accesses before issuing any more Prevent compiler/hardware reordering Read memory barriers: prevent reordering of read instructions Write memory barriers: prevent reordering of write instructions 8

Linux barrier operations barrier prevent only compiler reordering mb prevents load and store reordering rmb prevents load reordering wmb prevents store reordering smp_mb prevent load and store reordering only in SMP kernel smp_rmb prevent load reordering only in SMP kernels smp_wmb prevent store reordering only in SMP kernels set_mb performs assignment and prevents load and store reordering include/asm-i386/system.h 9

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Mutex Futex 10

Atomic operations Some instructions not atomic in hardware (smp) Read-modify-write instructions that touch memory twice, e.g., inc, xchg Most hardware provides a way to make these instructions atomic Intel lock prefix: appears to lock the memory bus Execute at memory speed 11

Linux atomic operations ATOMIC_INIT initialize an atomic_t variable atomic_read examine value atomically atomic_set change value atomically atomic_inc increment value atomically atomic_dec decrement value atomically atomic_add - add to value atomically atomic_sub subtract from value atomically atomic_inc_and_test increment value and test for zero atomic_dec_and_test decrement from value and test for zero atomic_sub_and_test subtract from value and test for zero atomic_set_mask mask bits atomically atomic_clear_mask clear bits atomically include/asm-i386/atomic.h 12

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 13

Linux interrupt operations local_irq_disable - disables interrupts on the current CPU local_irq_enable - enable interrupts on the current CPU local_save_flags - return the interrupt state of the processor local_restore_flags - restore the interrupt state of the processor Dealing with the full interrupt state of the system is officially discouraged. Locks should be used 14

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Mutex Futex 15

Linux spin lock operations spin_lock_init initialize a spin lock before using it for the first time spin_lock acquire a spin lock, spin waiting if it is not available spin_unlock release a spin lock spin_unlock_wait spin waiting for spin lock to become available, but don't acquire it spin_trylock acquire a spin lock if it is currently free, otherwise return error spin_is_locked return spin lock state include/asm-i386/spinlock.h and kernel/spinlock.c 16

Spin lock usage rules Spin locks should not be held for long periods because waiting tasks on other CPUs are spinning, and thus wasting CPU execution time Remember, don t call blocking operations (any function that may call schedule()) when holding a spin lock 17

Linux spin lock implementation raw_spin_lock_string 1: lock; decb %0 # atomically decrement jns 3f # if clear sign bit (>=0) jump forward to 3 2: rep; nop # wait cmpb $0, %0 # spin compare to 0 jle 2b # go back to 2 if <= 0 (locked) jmp 1b # unlocked; go back to 1 to try again 3: # we have acquired the lock spin_unlock merely writes 1 into the lock field. 18

Variant of spin locks and operations Spin locks that serialize with interrupts Read-write spin locks (rwlock_t) Read-write spin locks that serialize with interrupts Big reader lock (brlock) Sequential lock (seqlock) 19

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 20

Completions Simple way to ensure execution order: wait and wake-up semantics wait_for_complete(struct completion*) wait for another thread to call complete() compute(struct completion*) wake up threads waiting inside wait_for_complete() Implemented using spinlock and wait_queue include/linux/completion.h and kernel/sched.c 21

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 22

Linux semaphore operations up release the semaphore down get the semaphore (can block) down_interruptible get the semaphore, can be woken up if interrupt arrives down_trylock try to get the semaphore without blocking, otherwise return an error include/asm-i386/semaphore.h and arch/i386/kernel/semaphore.c 23

Linux semaphore implementation Goal: optimize for uncontended (common) case Implementation idea Uncontended case: use atomic operations Contended case: use spin locks and wait queues 24

Linux semaphore structure Struct semaphore count (atomic_t): > 0: free; = 0: in use, no waiters; < 0: in use, waiters sleepers: 0 (none) 1 (some), occasionally 2 struct semaphore atomic_t count int sleepers wait_queue_head_t wait wait: wait queue lock prev next implementation requires lower-level synchronization primitives atomic updates, spinlock, interrupt disabling 25

Contrived (buggy) semaphore implementation up (struct semaphore* s) { if(atomic_inc_positive(&s->count)) return; wake_up(&s->wait); } down (struct smaphore *s) { if(!atomic_dec_negative(&s->count)) return; // uncontended // contended add_wait_queue_exclusive(&s- >wait, self); } Common case: only one atomic instruction Problem Concurrent calls to up() and down()? Concurrent calls to down()? 26

The real down() inline down: movl $sem, %ecx # why does this work? lock; decl (%ecx)# atomically decr sem count jns 1f # if not negative jump to 1 lea %ecx, %eax # move into eax call down_failed # 1: # we have the semaphore down_failed: pushl %edx pushl %ecx call down popl %ecx popl %edx ret # push edx onto stack (C) # push ecx onto stack # call C function # pop ecx # pop edx include/asm-i386/semaphore.h and arch/i386/kernel/semaphore.c 27

down() tsk->state = TASK_UNINTERRUPTIBLE; spin_lock_irqsave(&sem->wait.lock, flags); add_wait_queue_exclusive_locked(&sem->wait, &wait); sem->sleepers++; for (;;) { int sleepers = sem->sleepers; /* * Add "everybody else" into it. They aren't playing, * because we own the spinlock in the wait_queue head */ if (!atomic_add_negative(sleepers - 1, &sem->count)) { sem->sleepers = 0; break; } sem->sleepers = 1; /* us - see -1 above */ spin_unlock_irqrestore(&sem->wait.lock, flags); schedule(); spin_lock_irqsave(&sem->wait.lock, flags); tsk->state = TASK_UNINTERRUPTIBLE; } remove_wait_queue_locked(&sem->wait, &wait); wake_up_locked(&sem->wait); spin_unlock_irqrestore(&sem->wait.lock, flags); tsk->state = TASK_RUNNING; Linux/lib/semaphore-sleepers.c 28

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 29

Futex motivation Synchronization of kernel-level threads: expensive Each synchronization operation traps into kernel Futex: optimize for the uncontended (common) case Uncontended no kernel involvement Contended trap into kernel 30

Futex implementation Borrows from Linux kernel semaphore implementation Data structure An aligned integer in user space A wait queue in kernel space Operations Uncontended case: atomic operations, user space Contended case: spin locks and wait queues, kernel space pthread mutex, semaphore, and condition variables use futex 31

Outline Low-level synchronization primitives in Linux Memory barrier Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Completion Semaphore Futex Mutex 32

Linux mutexes: motivation Introduced in 2.6.16. 90% of semaphores in kernel are used as mutexes, 9% of semaphores should be spin_locks. Andrew Morton Slow paths are more critical for highly contended semaphores on SMP Mutexes are simpler 33

Linux mutex operations mutex_unlock release the mutex mutex_lock get the mutex (can block) mutex_lock_interruptible get the mutex, but allow interrupts mutex_trylock try to get the mutex without blocking, otherwise return an error mutex_is_locked determine if mutex is locked 34