futex-requeue-pi.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Futex Requeue PI
  2. ----------------
  3. Requeueing of tasks from a non-PI futex to a PI futex requires
  4. special handling in order to ensure the underlying rt_mutex is never
  5. left without an owner if it has waiters; doing so would break the PI
  6. boosting logic [see rt-mutex-desgin.txt] For the purposes of
  7. brevity, this action will be referred to as "requeue_pi" throughout
  8. this document. Priority inheritance is abbreviated throughout as
  9. "PI".
  10. Motivation
  11. ----------
  12. Without requeue_pi, the glibc implementation of
  13. pthread_cond_broadcast() must resort to waking all the tasks waiting
  14. on a pthread_condvar and letting them try to sort out which task
  15. gets to run first in classic thundering-herd formation. An ideal
  16. implementation would wake the highest-priority waiter, and leave the
  17. rest to the natural wakeup inherent in unlocking the mutex
  18. associated with the condvar.
  19. Consider the simplified glibc calls:
  20. /* caller must lock mutex */
  21. pthread_cond_wait(cond, mutex)
  22. {
  23. lock(cond->__data.__lock);
  24. unlock(mutex);
  25. do {
  26. unlock(cond->__data.__lock);
  27. futex_wait(cond->__data.__futex);
  28. lock(cond->__data.__lock);
  29. } while(...)
  30. unlock(cond->__data.__lock);
  31. lock(mutex);
  32. }
  33. pthread_cond_broadcast(cond)
  34. {
  35. lock(cond->__data.__lock);
  36. unlock(cond->__data.__lock);
  37. futex_requeue(cond->data.__futex, cond->mutex);
  38. }
  39. Once pthread_cond_broadcast() requeues the tasks, the cond->mutex
  40. has waiters. Note that pthread_cond_wait() attempts to lock the
  41. mutex only after it has returned to user space. This will leave the
  42. underlying rt_mutex with waiters, and no owner, breaking the
  43. previously mentioned PI-boosting algorithms.
  44. In order to support PI-aware pthread_condvar's, the kernel needs to
  45. be able to requeue tasks to PI futexes. This support implies that
  46. upon a successful futex_wait system call, the caller would return to
  47. user space already holding the PI futex. The glibc implementation
  48. would be modified as follows:
  49. /* caller must lock mutex */
  50. pthread_cond_wait_pi(cond, mutex)
  51. {
  52. lock(cond->__data.__lock);
  53. unlock(mutex);
  54. do {
  55. unlock(cond->__data.__lock);
  56. futex_wait_requeue_pi(cond->__data.__futex);
  57. lock(cond->__data.__lock);
  58. } while(...)
  59. unlock(cond->__data.__lock);
  60. /* the kernel acquired the mutex for us */
  61. }
  62. pthread_cond_broadcast_pi(cond)
  63. {
  64. lock(cond->__data.__lock);
  65. unlock(cond->__data.__lock);
  66. futex_requeue_pi(cond->data.__futex, cond->mutex);
  67. }
  68. The actual glibc implementation will likely test for PI and make the
  69. necessary changes inside the existing calls rather than creating new
  70. calls for the PI cases. Similar changes are needed for
  71. pthread_cond_timedwait() and pthread_cond_signal().
  72. Implementation
  73. --------------
  74. In order to ensure the rt_mutex has an owner if it has waiters, it
  75. is necessary for both the requeue code, as well as the waiting code,
  76. to be able to acquire the rt_mutex before returning to user space.
  77. The requeue code cannot simply wake the waiter and leave it to
  78. acquire the rt_mutex as it would open a race window between the
  79. requeue call returning to user space and the waiter waking and
  80. starting to run. This is especially true in the uncontended case.
  81. The solution involves two new rt_mutex helper routines,
  82. rt_mutex_start_proxy_lock() and rt_mutex_finish_proxy_lock(), which
  83. allow the requeue code to acquire an uncontended rt_mutex on behalf
  84. of the waiter and to enqueue the waiter on a contended rt_mutex.
  85. Two new system calls provide the kernel<->user interface to
  86. requeue_pi: FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI.
  87. FUTEX_WAIT_REQUEUE_PI is called by the waiter (pthread_cond_wait()
  88. and pthread_cond_timedwait()) to block on the initial futex and wait
  89. to be requeued to a PI-aware futex. The implementation is the
  90. result of a high-speed collision between futex_wait() and
  91. futex_lock_pi(), with some extra logic to check for the additional
  92. wake-up scenarios.
  93. FUTEX_CMP_REQUEUE_PI is called by the waker
  94. (pthread_cond_broadcast() and pthread_cond_signal()) to requeue and
  95. possibly wake the waiting tasks. Internally, this system call is
  96. still handled by futex_requeue (by passing requeue_pi=1). Before
  97. requeueing, futex_requeue() attempts to acquire the requeue target
  98. PI futex on behalf of the top waiter. If it can, this waiter is
  99. woken. futex_requeue() then proceeds to requeue the remaining
  100. nr_wake+nr_requeue tasks to the PI futex, calling
  101. rt_mutex_start_proxy_lock() prior to each requeue to prepare the
  102. task as a waiter on the underlying rt_mutex. It is possible that
  103. the lock can be acquired at this stage as well, if so, the next
  104. waiter is woken to finish the acquisition of the lock.
  105. FUTEX_CMP_REQUEUE_PI accepts nr_wake and nr_requeue as arguments, but
  106. their sum is all that really matters. futex_requeue() will wake or
  107. requeue up to nr_wake + nr_requeue tasks. It will wake only as many
  108. tasks as it can acquire the lock for, which in the majority of cases
  109. should be 0 as good programming practice dictates that the caller of
  110. either pthread_cond_broadcast() or pthread_cond_signal() acquire the
  111. mutex prior to making the call. FUTEX_CMP_REQUEUE_PI requires that
  112. nr_wake=1. nr_requeue should be INT_MAX for broadcast and 0 for
  113. signal.