pi-futex.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Lightweight PI-futexes
  2. ----------------------
  3. We are calling them lightweight for 3 reasons:
  4. - in the user-space fastpath a PI-enabled futex involves no kernel work
  5. (or any other PI complexity) at all. No registration, no extra kernel
  6. calls - just pure fast atomic ops in userspace.
  7. - even in the slowpath, the system call and scheduling pattern is very
  8. similar to normal futexes.
  9. - the in-kernel PI implementation is streamlined around the mutex
  10. abstraction, with strict rules that keep the implementation
  11. relatively simple: only a single owner may own a lock (i.e. no
  12. read-write lock support), only the owner may unlock a lock, no
  13. recursive locking, etc.
  14. Priority Inheritance - why?
  15. ---------------------------
  16. The short reply: user-space PI helps achieving/improving determinism for
  17. user-space applications. In the best-case, it can help achieve
  18. determinism and well-bound latencies. Even in the worst-case, PI will
  19. improve the statistical distribution of locking related application
  20. delays.
  21. The longer reply:
  22. -----------------
  23. Firstly, sharing locks between multiple tasks is a common programming
  24. technique that often cannot be replaced with lockless algorithms. As we
  25. can see it in the kernel [which is a quite complex program in itself],
  26. lockless structures are rather the exception than the norm - the current
  27. ratio of lockless vs. locky code for shared data structures is somewhere
  28. between 1:10 and 1:100. Lockless is hard, and the complexity of lockless
  29. algorithms often endangers to ability to do robust reviews of said code.
  30. I.e. critical RT apps often choose lock structures to protect critical
  31. data structures, instead of lockless algorithms. Furthermore, there are
  32. cases (like shared hardware, or other resource limits) where lockless
  33. access is mathematically impossible.
  34. Media players (such as Jack) are an example of reasonable application
  35. design with multiple tasks (with multiple priority levels) sharing
  36. short-held locks: for example, a highprio audio playback thread is
  37. combined with medium-prio construct-audio-data threads and low-prio
  38. display-colory-stuff threads. Add video and decoding to the mix and
  39. we've got even more priority levels.
  40. So once we accept that synchronization objects (locks) are an
  41. unavoidable fact of life, and once we accept that multi-task userspace
  42. apps have a very fair expectation of being able to use locks, we've got
  43. to think about how to offer the option of a deterministic locking
  44. implementation to user-space.
  45. Most of the technical counter-arguments against doing priority
  46. inheritance only apply to kernel-space locks. But user-space locks are
  47. different, there we cannot disable interrupts or make the task
  48. non-preemptible in a critical section, so the 'use spinlocks' argument
  49. does not apply (user-space spinlocks have the same priority inversion
  50. problems as other user-space locking constructs). Fact is, pretty much
  51. the only technique that currently enables good determinism for userspace
  52. locks (such as futex-based pthread mutexes) is priority inheritance:
  53. Currently (without PI), if a high-prio and a low-prio task shares a lock
  54. [this is a quite common scenario for most non-trivial RT applications],
  55. even if all critical sections are coded carefully to be deterministic
  56. (i.e. all critical sections are short in duration and only execute a
  57. limited number of instructions), the kernel cannot guarantee any
  58. deterministic execution of the high-prio task: any medium-priority task
  59. could preempt the low-prio task while it holds the shared lock and
  60. executes the critical section, and could delay it indefinitely.
  61. Implementation:
  62. ---------------
  63. As mentioned before, the userspace fastpath of PI-enabled pthread
  64. mutexes involves no kernel work at all - they behave quite similarly to
  65. normal futex-based locks: a 0 value means unlocked, and a value==TID
  66. means locked. (This is the same method as used by list-based robust
  67. futexes.) Userspace uses atomic ops to lock/unlock these mutexes without
  68. entering the kernel.
  69. To handle the slowpath, we have added two new futex ops:
  70. FUTEX_LOCK_PI
  71. FUTEX_UNLOCK_PI
  72. If the lock-acquire fastpath fails, [i.e. an atomic transition from 0 to
  73. TID fails], then FUTEX_LOCK_PI is called. The kernel does all the
  74. remaining work: if there is no futex-queue attached to the futex address
  75. yet then the code looks up the task that owns the futex [it has put its
  76. own TID into the futex value], and attaches a 'PI state' structure to
  77. the futex-queue. The pi_state includes an rt-mutex, which is a PI-aware,
  78. kernel-based synchronization object. The 'other' task is made the owner
  79. of the rt-mutex, and the FUTEX_WAITERS bit is atomically set in the
  80. futex value. Then this task tries to lock the rt-mutex, on which it
  81. blocks. Once it returns, it has the mutex acquired, and it sets the
  82. futex value to its own TID and returns. Userspace has no other work to
  83. perform - it now owns the lock, and futex value contains
  84. FUTEX_WAITERS|TID.
  85. If the unlock side fastpath succeeds, [i.e. userspace manages to do a
  86. TID -> 0 atomic transition of the futex value], then no kernel work is
  87. triggered.
  88. If the unlock fastpath fails (because the FUTEX_WAITERS bit is set),
  89. then FUTEX_UNLOCK_PI is called, and the kernel unlocks the futex on the
  90. behalf of userspace - and it also unlocks the attached
  91. pi_state->rt_mutex and thus wakes up any potential waiters.
  92. Note that under this approach, contrary to previous PI-futex approaches,
  93. there is no prior 'registration' of a PI-futex. [which is not quite
  94. possible anyway, due to existing ABI properties of pthread mutexes.]
  95. Also, under this scheme, 'robustness' and 'PI' are two orthogonal
  96. properties of futexes, and all four combinations are possible: futex,
  97. robust-futex, PI-futex, robust+PI-futex.
  98. More details about priority inheritance can be found in
  99. Documentation/rt-mutex.txt.