Download Kernel Synchronization in Operating Systems: Primitives and Control Paths and more Assignments Operating Systems in PDF only on Docsity!
CS 3210 Fall 2003^ KernelSynchronization
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Kernel Synchronization z^ kernel as server^ z^ concurrent requests are possible^ z^ synchronization is (usually) required z^ avoid race conditions^ z^ correctness violated by timing changes^ z^ identify, secure critical section (mutex) z^ kernel vs. userland synch primitives^ z^ example: semaphore system call z^ synchronization is complex, subtle z^ hierarchy of primitives^ z^ lowest level: hardware primitives^ z^ higher level: built using lower-level^ z^ e.g. semaphores use atomic inc, spinlocks, waitqueues
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
When Synch Is Not Necessary z^ simplifying assumptions (2.4 uniprocessor)^ z^ kernel is non pre-emptive^ z^ process holds cpu while in kernel^ z^ unless it blocks, relinquishes cpu, or interrupt^ z^ locking only needed against interrupts z^ smp kernels require locking^ z^ smp locks compile out for uniprocessor kernels^ z^ no overhead! z^ 2.5 introduces limited kernel pre-emption^ z^ to reduce scheduler, interrupt latency^ z^ make Linux more responsive for real-time apps^ z^ introduces preemption points in kernel code^ z^ Solaris is fully kernel preemptive
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Linux Synch Primitives z^ Atomic operations^ z^ memory bus lock, read-modify-write ops z^ Memory barriers^ z^ avoids compiler, cpu instruction re-ordering z^ Interrupt/softirq disabling^ z^ Local, global z^ Spin locks^ z^ only on SMP systems; keep them short!^ z^ general, read/write, big reader z^ Semaphores^ z^ general, read/write
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Memory Barriers z^ Compilers and hw re-order memory accesses^ z^ as an (unobservable?) optimization^ z^ true on SMP and even UP systems! z^ Memory barrier – instruction to hw/compiler to complete allpending accesses before issuing more^ z^ read memory barrier – acts on read requests^ z^ write memory barrier – acts on write requests z^ Linux macros – mb(), rmb(), wmb(), smp versions z^ Intel –^ z^ certain instructions act as barriers: lock, iret, control regs^ z^ rmb – asm volatile("lock;addl $0,0(%%esp)":::"memory")^ z^ add 0 to top of stack with lock prefix^ z^ wmb – Intel never re-orders writes, just for compiler
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Spin Locks z^ busy wait – meaningless on UP z^ spinlock_t struct^ z^ int lock field – 1 for unlock, <= 0 locked z^ macros^ z^ spin_lock_init(),^ z^ spin_lock(),^ z^ spin_unlock()^ z^ spin_unlock_wait(),^ z^ spin_is_locked(),^ z^ spin_try_lock()
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Read/Write Spin Locks z^ allow multiple readers, single writer z^ rwlock_t – really two “fields”^ z^ high order 12 bits:1 no writer, 0 otherwise^ z^ low order 24 bits: # of readers (2’s comp)^ z^ available: 0x01000000^ z^ one writer: 0x00000000^ z^ one reader: 0x00ffffff z^ implementation: similar to basic spinlock z^ reader priority
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
The Big Reader Lock z^ reader optimized r/w spinlock z^ r/w spinlock suffers cache contention^ z^ on lock and unlock because of write to rwlock_t z^ per-CPU, cache-aligned lock arrays^ z^ one for reader portion, another for writer portion z^ to read: set bit in reader array, spin on writer^ z^ acquire when writer lock free; very fast! z^ to write: set bit and scan ALL reader bits^ z^ acquire when reader bits all free; very slow!
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Semaphores z^ up() is easy^ z^ atomically increment; wake_up() if necessary z^ uncontended down() is easy^ z^ atomically decrement; continue z^ contended down() is really complex!^ z^ basically increment sleepers and sleep^ z^ loop because of potentially concurrent ups/downs z^ still in down() path when lock is acquired
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Read/Write Semaphores z^ new in 2.4; allows increased concurrency z^ FIFO, writer priority z^ complex implementation^ z^ similar to regular semaphores z^ ops:^ z^ down_read(), down_write()^ z^ up_read(), up_write()
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Local Interrupt Disabling z^ basic primitive in original UNIX z^ doesn’t protect against other CPUs z^ Intel: “interrupts enabled bit”^ z^ cli to clear (disable), sti to set (enable) z^ enabling is often wrong; need to restore^ z^ local_irq_save()^ z^ local_irq_restore() z^ SPARC is weird: interrupt flag part of registerwindow; must restore in same context
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Global Interrupt Disabling z^ deprecated in 2.4; gone in 2.6 z^ __global_cli()^ z^ wait for top and bottom halves to complete^ z^ disable local interrupts^ z^ grab spinlock^ z^ disable all interrupts z^ tricky cases z^ really bad for performance
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Choosing Synch Primitives z^ avoid synch if possible! (clever instruction ordering)^ z^ example: inserting in linked list (needs barrier still) z^ use atomics or rw spinlocks if possible z^ use semaphores if you need to sleep^ z^ can’t sleep in interrupt context^ z^ don’t sleep holding a spinlock! z^ complicated matrix of choices for protecting datastructures accessed by deferred functions^ z^ Love has a nice summary
CS 3210 Operating System DesignHutto, College of Computing, Georgia Tech
Example Race Conditions z^ reference counters for sharing structs^ z^ get/put functions^ z^ deallocate when 0 z^ memory map semaphore z^ slab cache list semaphore z^ inode semaphore