synthesis
play

Synthesis A Lock-Free Multiprocessor OS Kernel Josh Triplett April - PowerPoint PPT Presentation

Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Synthesis A Lock-Free Multiprocessor OS Kernel Josh Triplett April 19, 2006 Locking a Linked List Implementing a Spinlock Atomic


  1. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  2. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  3. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  4. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  5. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  6. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Problems with disabling software interrupts • More expensive • Queue maintenance • Doesn’t protect against access from hardware interrupts • Still only for short critical sections • Can cause process hangs if not released • Coarse-grained (still only one “lock”) • Still insufficient for multiprocessors • Still need real synchronization

  7. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  8. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  9. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  10. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  11. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  12. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  13. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  14. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  15. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Busywaiting with a spinlock static DEFINE_SPINLOCK(list_lock); spin_lock(&list_lock); node->next = head; head = node; spin_unlock(&list_lock); • Assumes every thread has a processor • Nothing better to do than wait • Spinning impacts other processors • Variants reduce impact, add queuing overhead

  16. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  17. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  18. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  19. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  20. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  21. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  22. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  23. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Blocking with a semaphore static DECLARE_MUTEX(list_lock); while(down_interruptible(&list_lock) == -EINTR) ; node->next = head; head = node; up(&list_lock); • Queuing overhead • Goes through scheduler • Can block

  24. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential Correctness Problems with Mutual Exclusion • Deadlocks • Priority Inversion

  25. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential Correctness Problems with Mutual Exclusion • Deadlocks • Priority Inversion

  26. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  27. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  28. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  29. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  30. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  31. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking summary • Provides correctness (if used correctly) • Adds overhead • Causes problems • Various different tradeoffs • Alternatives? • Shared data structures need synchronization, right?

  32. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Spinlock implementation A first attempt: void spin_lock(spinlock_t *lock) { while(lock->counter != 0) ; lock->counter = 1; }

  33. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Spinlock implementation A first attempt: void spin_lock(spinlock_t *lock) { while(lock->counter != 0) ; lock->counter = 1; }

  34. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Spinlock implementation A first attempt: void spin_lock(spinlock_t *lock) { while(lock->counter != 0) ; lock->counter = 1; }

  35. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential race condition Thread 1 Thread 2 while(lock->counter != 0) ; while(lock->counter != 0) ; lock->counter = 1; lock->counter = 1;

  36. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential race condition Thread 1 Thread 2 while(lock->counter != 0) ; while(lock->counter != 0) ; lock->counter = 1; lock->counter = 1;

  37. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential race condition Thread 1 Thread 2 while(lock->counter != 0) ; while(lock->counter != 0) ; lock->counter = 1; lock->counter = 1;

  38. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential race condition Thread 1 Thread 2 while(lock->counter != 0) ; while(lock->counter != 0) ; lock->counter = 1; lock->counter = 1;

  39. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Potential race condition Thread 1 Thread 2 while(lock->counter != 0) ; while(lock->counter != 0) ; lock->counter = 1; lock->counter = 1;

  40. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Fixing this race condition? • Can’t just disable interrupts: insufficient for multiprocessors • Can’t use semaphores: we don’t want to block

  41. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Fixing this race condition? • Can’t just disable interrupts: insufficient for multiprocessors • Can’t use semaphores: we don’t want to block

  42. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking the lock void spin_lock(spinlock_t *lock) { spin_lock(lock->lock); while(lock->counter != 0) ; lock->counter = 1; spin_unlock(lock->lock); }

  43. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Locking the lock void spin_lock(spinlock_t *lock) { spin_lock(lock->lock); while(lock->counter != 0) ; lock->counter = 1; spin_unlock(lock->lock); } Something wrong here!

  44. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Fixing the semaphore • Alternatives? • Shared data structures need synchronization, right?

  45. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Fixing the semaphore • Alternatives? • Shared data structures need synchronization, right?

  46. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Atomic instructions • One instruction, indivisible • Bus locking protocols • No interleaving • What do we have available?

  47. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Atomic instructions • One instruction, indivisible • Bus locking protocols • No interleaving • What do we have available?

  48. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Atomic instructions • One instruction, indivisible • Bus locking protocols • No interleaving • What do we have available?

  49. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Atomic instructions • One instruction, indivisible • Bus locking protocols • No interleaving • What do we have available?

  50. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Test and set • Set value to 1, return previous value void spin_lock(spinlock_t *lock) { while(atomic_test_and_set(lock->counter)) ; }

  51. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Exchange • Swap two values atomically • Can implement test and set with exchange: • Exchange with variable containing 1 • Return variable

  52. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Exchange • Swap two values atomically • Can implement test and set with exchange: • Exchange with variable containing 1 • Return variable

  53. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Exchange • Swap two values atomically • Can implement test and set with exchange: • Exchange with variable containing 1 • Return variable

  54. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Exchange • Swap two values atomically • Can implement test and set with exchange: • Exchange with variable containing 1 • Return variable

  55. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Synchronization from atomic instructions • Fundamental synchronization mechanism • Build synchronization abstractions on them • Build OS data structures on the synchronization abstractions

  56. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Synchronization from atomic instructions • Fundamental synchronization mechanism • Build synchronization abstractions on them • Build OS data structures on the synchronization abstractions

  57. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Synchronization from atomic instructions • Fundamental synchronization mechanism • Build synchronization abstractions on them • Build OS data structures on the synchronization abstractions

  58. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Abstractions? From the Exokernel paper: The exokernel architecture is founded on and motivated by a single, simple, and old observation: the lower the level of a primitive, the more efficiently it can be implemented, and the more latitude it grants to implementors of higher-level abstractions.

  59. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Using atomic instructions directly? • Skip implementing synchronization abstractions • Build OS data structures on atomic instructions

  60. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Using atomic instructions directly? • Skip implementing synchronization abstractions • Build OS data structures on atomic instructions

  61. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list revisited Prepend an element to a linked list: node->next = head; head = node; • What would work here? • Atomic arithmetic doesn’t help • Atomic test and set doesn’t help • Would atomic exchange work?

  62. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list revisited Prepend an element to a linked list: node->next = head; head = node; • What would work here? • Atomic arithmetic doesn’t help • Atomic test and set doesn’t help • Would atomic exchange work?

  63. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list revisited Prepend an element to a linked list: node->next = head; head = node; • What would work here? • Atomic arithmetic doesn’t help • Atomic test and set doesn’t help • Would atomic exchange work?

  64. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list revisited Prepend an element to a linked list: node->next = head; head = node; • What would work here? • Atomic arithmetic doesn’t help • Atomic test and set doesn’t help • Would atomic exchange work?

  65. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list with exchange? node->next = node; atomic_exchange(node->next, head) • This won’t work. • It references two memory operands • Atomic exchange can only handle one memory operand

  66. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list with exchange? node->next = node; atomic_exchange(node->next, head) • This won’t work. • It references two memory operands • Atomic exchange can only handle one memory operand

  67. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list with exchange? node->next = node; atomic_exchange(node->next, head) • This won’t work. • It references two memory operands • Atomic exchange can only handle one memory operand

  68. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list with exchange? node->next = node; atomic_exchange(node->next, head) • This won’t work. • It references two memory operands • Atomic exchange can only handle one memory operand

  69. Locking a Linked List Implementing a Spinlock Atomic Instructions Linked list revisited Synthesis Summary Linked list with exchange? node->next = node; atomic_exchange(node->next, head) • This won’t work. • It references two memory operands • Atomic exchange can only handle one memory operand

Recommend


More recommend