requeue.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/plist.h>
  3. #include <linux/sched/signal.h>
  4. #include "futex.h"
  5. #include "../locking/rtmutex_common.h"
  6. /*
  7. * On PREEMPT_RT, the hash bucket lock is a 'sleeping' spinlock with an
  8. * underlying rtmutex. The task which is about to be requeued could have
  9. * just woken up (timeout, signal). After the wake up the task has to
  10. * acquire hash bucket lock, which is held by the requeue code. As a task
  11. * can only be blocked on _ONE_ rtmutex at a time, the proxy lock blocking
  12. * and the hash bucket lock blocking would collide and corrupt state.
  13. *
  14. * On !PREEMPT_RT this is not a problem and everything could be serialized
  15. * on hash bucket lock, but aside of having the benefit of common code,
  16. * this allows to avoid doing the requeue when the task is already on the
  17. * way out and taking the hash bucket lock of the original uaddr1 when the
  18. * requeue has been completed.
  19. *
  20. * The following state transitions are valid:
  21. *
  22. * On the waiter side:
  23. * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_IGNORE
  24. * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_WAIT
  25. *
  26. * On the requeue side:
  27. * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_INPROGRESS
  28. * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_DONE/LOCKED
  29. * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_NONE (requeue failed)
  30. * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_DONE/LOCKED
  31. * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_IGNORE (requeue failed)
  32. *
  33. * The requeue side ignores a waiter with state Q_REQUEUE_PI_IGNORE as this
  34. * signals that the waiter is already on the way out. It also means that
  35. * the waiter is still on the 'wait' futex, i.e. uaddr1.
  36. *
  37. * The waiter side signals early wakeup to the requeue side either through
  38. * setting state to Q_REQUEUE_PI_IGNORE or to Q_REQUEUE_PI_WAIT depending
  39. * on the current state. In case of Q_REQUEUE_PI_IGNORE it can immediately
  40. * proceed to take the hash bucket lock of uaddr1. If it set state to WAIT,
  41. * which means the wakeup is interleaving with a requeue in progress it has
  42. * to wait for the requeue side to change the state. Either to DONE/LOCKED
  43. * or to IGNORE. DONE/LOCKED means the waiter q is now on the uaddr2 futex
  44. * and either blocked (DONE) or has acquired it (LOCKED). IGNORE is set by
  45. * the requeue side when the requeue attempt failed via deadlock detection
  46. * and therefore the waiter q is still on the uaddr1 futex.
  47. */
  48. enum {
  49. Q_REQUEUE_PI_NONE = 0,
  50. Q_REQUEUE_PI_IGNORE,
  51. Q_REQUEUE_PI_IN_PROGRESS,
  52. Q_REQUEUE_PI_WAIT,
  53. Q_REQUEUE_PI_DONE,
  54. Q_REQUEUE_PI_LOCKED,
  55. };
  56. const struct futex_q futex_q_init = {
  57. /* list gets initialized in futex_queue()*/
  58. .wake = futex_wake_mark,
  59. .key = FUTEX_KEY_INIT,
  60. .bitset = FUTEX_BITSET_MATCH_ANY,
  61. .requeue_state = ATOMIC_INIT(Q_REQUEUE_PI_NONE),
  62. };
  63. /**
  64. * requeue_futex() - Requeue a futex_q from one hb to another
  65. * @q: the futex_q to requeue
  66. * @hb1: the source hash_bucket
  67. * @hb2: the target hash_bucket
  68. * @key2: the new key for the requeued futex_q
  69. */
  70. static inline
  71. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  72. struct futex_hash_bucket *hb2, union futex_key *key2)
  73. {
  74. /*
  75. * If key1 and key2 hash to the same bucket, no need to
  76. * requeue.
  77. */
  78. if (likely(&hb1->chain != &hb2->chain)) {
  79. plist_del(&q->list, &hb1->chain);
  80. futex_hb_waiters_dec(hb1);
  81. futex_hb_waiters_inc(hb2);
  82. plist_add(&q->list, &hb2->chain);
  83. q->lock_ptr = &hb2->lock;
  84. }
  85. q->key = *key2;
  86. }
  87. static inline bool futex_requeue_pi_prepare(struct futex_q *q,
  88. struct futex_pi_state *pi_state)
  89. {
  90. int old, new;
  91. /*
  92. * Set state to Q_REQUEUE_PI_IN_PROGRESS unless an early wakeup has
  93. * already set Q_REQUEUE_PI_IGNORE to signal that requeue should
  94. * ignore the waiter.
  95. */
  96. old = atomic_read_acquire(&q->requeue_state);
  97. do {
  98. if (old == Q_REQUEUE_PI_IGNORE)
  99. return false;
  100. /*
  101. * futex_proxy_trylock_atomic() might have set it to
  102. * IN_PROGRESS and a interleaved early wake to WAIT.
  103. *
  104. * It was considered to have an extra state for that
  105. * trylock, but that would just add more conditionals
  106. * all over the place for a dubious value.
  107. */
  108. if (old != Q_REQUEUE_PI_NONE)
  109. break;
  110. new = Q_REQUEUE_PI_IN_PROGRESS;
  111. } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
  112. q->pi_state = pi_state;
  113. return true;
  114. }
  115. static inline void futex_requeue_pi_complete(struct futex_q *q, int locked)
  116. {
  117. int old, new;
  118. old = atomic_read_acquire(&q->requeue_state);
  119. do {
  120. if (old == Q_REQUEUE_PI_IGNORE)
  121. return;
  122. if (locked >= 0) {
  123. /* Requeue succeeded. Set DONE or LOCKED */
  124. WARN_ON_ONCE(old != Q_REQUEUE_PI_IN_PROGRESS &&
  125. old != Q_REQUEUE_PI_WAIT);
  126. new = Q_REQUEUE_PI_DONE + locked;
  127. } else if (old == Q_REQUEUE_PI_IN_PROGRESS) {
  128. /* Deadlock, no early wakeup interleave */
  129. new = Q_REQUEUE_PI_NONE;
  130. } else {
  131. /* Deadlock, early wakeup interleave. */
  132. WARN_ON_ONCE(old != Q_REQUEUE_PI_WAIT);
  133. new = Q_REQUEUE_PI_IGNORE;
  134. }
  135. } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
  136. #ifdef CONFIG_PREEMPT_RT
  137. /* If the waiter interleaved with the requeue let it know */
  138. if (unlikely(old == Q_REQUEUE_PI_WAIT))
  139. rcuwait_wake_up(&q->requeue_wait);
  140. #endif
  141. }
  142. static inline int futex_requeue_pi_wakeup_sync(struct futex_q *q)
  143. {
  144. int old, new;
  145. old = atomic_read_acquire(&q->requeue_state);
  146. do {
  147. /* Is requeue done already? */
  148. if (old >= Q_REQUEUE_PI_DONE)
  149. return old;
  150. /*
  151. * If not done, then tell the requeue code to either ignore
  152. * the waiter or to wake it up once the requeue is done.
  153. */
  154. new = Q_REQUEUE_PI_WAIT;
  155. if (old == Q_REQUEUE_PI_NONE)
  156. new = Q_REQUEUE_PI_IGNORE;
  157. } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
  158. /* If the requeue was in progress, wait for it to complete */
  159. if (old == Q_REQUEUE_PI_IN_PROGRESS) {
  160. #ifdef CONFIG_PREEMPT_RT
  161. rcuwait_wait_event(&q->requeue_wait,
  162. atomic_read(&q->requeue_state) != Q_REQUEUE_PI_WAIT,
  163. TASK_UNINTERRUPTIBLE);
  164. #else
  165. (void)atomic_cond_read_relaxed(&q->requeue_state, VAL != Q_REQUEUE_PI_WAIT);
  166. #endif
  167. }
  168. /*
  169. * Requeue is now either prohibited or complete. Reread state
  170. * because during the wait above it might have changed. Nothing
  171. * will modify q->requeue_state after this point.
  172. */
  173. return atomic_read(&q->requeue_state);
  174. }
  175. /**
  176. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  177. * @q: the futex_q
  178. * @key: the key of the requeue target futex
  179. * @hb: the hash_bucket of the requeue target futex
  180. *
  181. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  182. * target futex if it is uncontended or via a lock steal.
  183. *
  184. * 1) Set @q::key to the requeue target futex key so the waiter can detect
  185. * the wakeup on the right futex.
  186. *
  187. * 2) Dequeue @q from the hash bucket.
  188. *
  189. * 3) Set @q::rt_waiter to NULL so the woken up task can detect atomic lock
  190. * acquisition.
  191. *
  192. * 4) Set the q->lock_ptr to the requeue target hb->lock for the case that
  193. * the waiter has to fixup the pi state.
  194. *
  195. * 5) Complete the requeue state so the waiter can make progress. After
  196. * this point the waiter task can return from the syscall immediately in
  197. * case that the pi state does not have to be fixed up.
  198. *
  199. * 6) Wake the waiter task.
  200. *
  201. * Must be called with both q->lock_ptr and hb->lock held.
  202. */
  203. static inline
  204. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
  205. struct futex_hash_bucket *hb)
  206. {
  207. q->key = *key;
  208. __futex_unqueue(q);
  209. WARN_ON(!q->rt_waiter);
  210. q->rt_waiter = NULL;
  211. q->lock_ptr = &hb->lock;
  212. /* Signal locked state to the waiter */
  213. futex_requeue_pi_complete(q, 1);
  214. wake_up_state(q->task, TASK_NORMAL);
  215. }
  216. /**
  217. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  218. * @pifutex: the user address of the to futex
  219. * @hb1: the from futex hash bucket, must be locked by the caller
  220. * @hb2: the to futex hash bucket, must be locked by the caller
  221. * @key1: the from futex key
  222. * @key2: the to futex key
  223. * @ps: address to store the pi_state pointer
  224. * @exiting: Pointer to store the task pointer of the owner task
  225. * which is in the middle of exiting
  226. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  227. *
  228. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  229. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  230. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  231. * hb1 and hb2 must be held by the caller.
  232. *
  233. * @exiting is only set when the return value is -EBUSY. If so, this holds
  234. * a refcount on the exiting task on return and the caller needs to drop it
  235. * after waiting for the exit to complete.
  236. *
  237. * Return:
  238. * - 0 - failed to acquire the lock atomically;
  239. * - >0 - acquired the lock, return value is vpid of the top_waiter
  240. * - <0 - error
  241. */
  242. static int
  243. futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
  244. struct futex_hash_bucket *hb2, union futex_key *key1,
  245. union futex_key *key2, struct futex_pi_state **ps,
  246. struct task_struct **exiting, int set_waiters)
  247. {
  248. struct futex_q *top_waiter;
  249. u32 curval;
  250. int ret;
  251. if (futex_get_value_locked(&curval, pifutex))
  252. return -EFAULT;
  253. if (unlikely(should_fail_futex(true)))
  254. return -EFAULT;
  255. /*
  256. * Find the top_waiter and determine if there are additional waiters.
  257. * If the caller intends to requeue more than 1 waiter to pifutex,
  258. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  259. * as we have means to handle the possible fault. If not, don't set
  260. * the bit unnecessarily as it will force the subsequent unlock to enter
  261. * the kernel.
  262. */
  263. top_waiter = futex_top_waiter(hb1, key1);
  264. /* There are no waiters, nothing for us to do. */
  265. if (!top_waiter)
  266. return 0;
  267. /*
  268. * Ensure that this is a waiter sitting in futex_wait_requeue_pi()
  269. * and waiting on the 'waitqueue' futex which is always !PI.
  270. */
  271. if (!top_waiter->rt_waiter || top_waiter->pi_state)
  272. return -EINVAL;
  273. /* Ensure we requeue to the expected futex. */
  274. if (!futex_match(top_waiter->requeue_pi_key, key2))
  275. return -EINVAL;
  276. /* Ensure that this does not race against an early wakeup */
  277. if (!futex_requeue_pi_prepare(top_waiter, NULL))
  278. return -EAGAIN;
  279. /*
  280. * Try to take the lock for top_waiter and set the FUTEX_WAITERS bit
  281. * in the contended case or if @set_waiters is true.
  282. *
  283. * In the contended case PI state is attached to the lock owner. If
  284. * the user space lock can be acquired then PI state is attached to
  285. * the new owner (@top_waiter->task) when @set_waiters is true.
  286. */
  287. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  288. exiting, set_waiters);
  289. if (ret == 1) {
  290. /*
  291. * Lock was acquired in user space and PI state was
  292. * attached to @top_waiter->task. That means state is fully
  293. * consistent and the waiter can return to user space
  294. * immediately after the wakeup.
  295. */
  296. requeue_pi_wake_futex(top_waiter, key2, hb2);
  297. } else if (ret < 0) {
  298. /* Rewind top_waiter::requeue_state */
  299. futex_requeue_pi_complete(top_waiter, ret);
  300. } else {
  301. /*
  302. * futex_lock_pi_atomic() did not acquire the user space
  303. * futex, but managed to establish the proxy lock and pi
  304. * state. top_waiter::requeue_state cannot be fixed up here
  305. * because the waiter is not enqueued on the rtmutex
  306. * yet. This is handled at the callsite depending on the
  307. * result of rt_mutex_start_proxy_lock() which is
  308. * guaranteed to be reached with this function returning 0.
  309. */
  310. }
  311. return ret;
  312. }
  313. /**
  314. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  315. * @uaddr1: source futex user address
  316. * @flags1: futex flags (FLAGS_SHARED, etc.)
  317. * @uaddr2: target futex user address
  318. * @flags2: futex flags (FLAGS_SHARED, etc.)
  319. * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  320. * @nr_requeue: number of waiters to requeue (0-INT_MAX)
  321. * @cmpval: @uaddr1 expected value (or %NULL)
  322. * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
  323. * pi futex (pi to pi requeue is not supported)
  324. *
  325. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  326. * uaddr2 atomically on behalf of the top waiter.
  327. *
  328. * Return:
  329. * - >=0 - on success, the number of tasks requeued or woken;
  330. * - <0 - on error
  331. */
  332. int futex_requeue(u32 __user *uaddr1, unsigned int flags1,
  333. u32 __user *uaddr2, unsigned int flags2,
  334. int nr_wake, int nr_requeue, u32 *cmpval, int requeue_pi)
  335. {
  336. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  337. int task_count = 0, ret;
  338. struct futex_pi_state *pi_state = NULL;
  339. struct futex_hash_bucket *hb1, *hb2;
  340. struct futex_q *this, *next;
  341. DEFINE_WAKE_Q(wake_q);
  342. if (nr_wake < 0 || nr_requeue < 0)
  343. return -EINVAL;
  344. /*
  345. * When PI not supported: return -ENOSYS if requeue_pi is true,
  346. * consequently the compiler knows requeue_pi is always false past
  347. * this point which will optimize away all the conditional code
  348. * further down.
  349. */
  350. if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
  351. return -ENOSYS;
  352. if (requeue_pi) {
  353. /*
  354. * Requeue PI only works on two distinct uaddrs. This
  355. * check is only valid for private futexes. See below.
  356. */
  357. if (uaddr1 == uaddr2)
  358. return -EINVAL;
  359. /*
  360. * futex_requeue() allows the caller to define the number
  361. * of waiters to wake up via the @nr_wake argument. With
  362. * REQUEUE_PI, waking up more than one waiter is creating
  363. * more problems than it solves. Waking up a waiter makes
  364. * only sense if the PI futex @uaddr2 is uncontended as
  365. * this allows the requeue code to acquire the futex
  366. * @uaddr2 before waking the waiter. The waiter can then
  367. * return to user space without further action. A secondary
  368. * wakeup would just make the futex_wait_requeue_pi()
  369. * handling more complex, because that code would have to
  370. * look up pi_state and do more or less all the handling
  371. * which the requeue code has to do for the to be requeued
  372. * waiters. So restrict the number of waiters to wake to
  373. * one, and only wake it up when the PI futex is
  374. * uncontended. Otherwise requeue it and let the unlock of
  375. * the PI futex handle the wakeup.
  376. *
  377. * All REQUEUE_PI users, e.g. pthread_cond_signal() and
  378. * pthread_cond_broadcast() must use nr_wake=1.
  379. */
  380. if (nr_wake != 1)
  381. return -EINVAL;
  382. /*
  383. * requeue_pi requires a pi_state, try to allocate it now
  384. * without any locks in case it fails.
  385. */
  386. if (refill_pi_state_cache())
  387. return -ENOMEM;
  388. }
  389. retry:
  390. ret = get_futex_key(uaddr1, flags1, &key1, FUTEX_READ);
  391. if (unlikely(ret != 0))
  392. return ret;
  393. ret = get_futex_key(uaddr2, flags2, &key2,
  394. requeue_pi ? FUTEX_WRITE : FUTEX_READ);
  395. if (unlikely(ret != 0))
  396. return ret;
  397. /*
  398. * The check above which compares uaddrs is not sufficient for
  399. * shared futexes. We need to compare the keys:
  400. */
  401. if (requeue_pi && futex_match(&key1, &key2))
  402. return -EINVAL;
  403. hb1 = futex_hash(&key1);
  404. hb2 = futex_hash(&key2);
  405. retry_private:
  406. futex_hb_waiters_inc(hb2);
  407. double_lock_hb(hb1, hb2);
  408. if (likely(cmpval != NULL)) {
  409. u32 curval;
  410. ret = futex_get_value_locked(&curval, uaddr1);
  411. if (unlikely(ret)) {
  412. double_unlock_hb(hb1, hb2);
  413. futex_hb_waiters_dec(hb2);
  414. ret = get_user(curval, uaddr1);
  415. if (ret)
  416. return ret;
  417. if (!(flags1 & FLAGS_SHARED))
  418. goto retry_private;
  419. goto retry;
  420. }
  421. if (curval != *cmpval) {
  422. ret = -EAGAIN;
  423. goto out_unlock;
  424. }
  425. }
  426. if (requeue_pi) {
  427. struct task_struct *exiting = NULL;
  428. /*
  429. * Attempt to acquire uaddr2 and wake the top waiter. If we
  430. * intend to requeue waiters, force setting the FUTEX_WAITERS
  431. * bit. We force this here where we are able to easily handle
  432. * faults rather in the requeue loop below.
  433. *
  434. * Updates topwaiter::requeue_state if a top waiter exists.
  435. */
  436. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  437. &key2, &pi_state,
  438. &exiting, nr_requeue);
  439. /*
  440. * At this point the top_waiter has either taken uaddr2 or
  441. * is waiting on it. In both cases pi_state has been
  442. * established and an initial refcount on it. In case of an
  443. * error there's nothing.
  444. *
  445. * The top waiter's requeue_state is up to date:
  446. *
  447. * - If the lock was acquired atomically (ret == 1), then
  448. * the state is Q_REQUEUE_PI_LOCKED.
  449. *
  450. * The top waiter has been dequeued and woken up and can
  451. * return to user space immediately. The kernel/user
  452. * space state is consistent. In case that there must be
  453. * more waiters requeued the WAITERS bit in the user
  454. * space futex is set so the top waiter task has to go
  455. * into the syscall slowpath to unlock the futex. This
  456. * will block until this requeue operation has been
  457. * completed and the hash bucket locks have been
  458. * dropped.
  459. *
  460. * - If the trylock failed with an error (ret < 0) then
  461. * the state is either Q_REQUEUE_PI_NONE, i.e. "nothing
  462. * happened", or Q_REQUEUE_PI_IGNORE when there was an
  463. * interleaved early wakeup.
  464. *
  465. * - If the trylock did not succeed (ret == 0) then the
  466. * state is either Q_REQUEUE_PI_IN_PROGRESS or
  467. * Q_REQUEUE_PI_WAIT if an early wakeup interleaved.
  468. * This will be cleaned up in the loop below, which
  469. * cannot fail because futex_proxy_trylock_atomic() did
  470. * the same sanity checks for requeue_pi as the loop
  471. * below does.
  472. */
  473. switch (ret) {
  474. case 0:
  475. /* We hold a reference on the pi state. */
  476. break;
  477. case 1:
  478. /*
  479. * futex_proxy_trylock_atomic() acquired the user space
  480. * futex. Adjust task_count.
  481. */
  482. task_count++;
  483. ret = 0;
  484. break;
  485. /*
  486. * If the above failed, then pi_state is NULL and
  487. * waiter::requeue_state is correct.
  488. */
  489. case -EFAULT:
  490. double_unlock_hb(hb1, hb2);
  491. futex_hb_waiters_dec(hb2);
  492. ret = fault_in_user_writeable(uaddr2);
  493. if (!ret)
  494. goto retry;
  495. return ret;
  496. case -EBUSY:
  497. case -EAGAIN:
  498. /*
  499. * Two reasons for this:
  500. * - EBUSY: Owner is exiting and we just wait for the
  501. * exit to complete.
  502. * - EAGAIN: The user space value changed.
  503. */
  504. double_unlock_hb(hb1, hb2);
  505. futex_hb_waiters_dec(hb2);
  506. /*
  507. * Handle the case where the owner is in the middle of
  508. * exiting. Wait for the exit to complete otherwise
  509. * this task might loop forever, aka. live lock.
  510. */
  511. wait_for_owner_exiting(ret, exiting);
  512. cond_resched();
  513. goto retry;
  514. default:
  515. goto out_unlock;
  516. }
  517. }
  518. plist_for_each_entry_safe(this, next, &hb1->chain, list) {
  519. if (task_count - nr_wake >= nr_requeue)
  520. break;
  521. if (!futex_match(&this->key, &key1))
  522. continue;
  523. /*
  524. * FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI should always
  525. * be paired with each other and no other futex ops.
  526. *
  527. * We should never be requeueing a futex_q with a pi_state,
  528. * which is awaiting a futex_unlock_pi().
  529. */
  530. if ((requeue_pi && !this->rt_waiter) ||
  531. (!requeue_pi && this->rt_waiter) ||
  532. this->pi_state) {
  533. ret = -EINVAL;
  534. break;
  535. }
  536. /* Plain futexes just wake or requeue and are done */
  537. if (!requeue_pi) {
  538. if (++task_count <= nr_wake)
  539. this->wake(&wake_q, this);
  540. else
  541. requeue_futex(this, hb1, hb2, &key2);
  542. continue;
  543. }
  544. /* Ensure we requeue to the expected futex for requeue_pi. */
  545. if (!futex_match(this->requeue_pi_key, &key2)) {
  546. ret = -EINVAL;
  547. break;
  548. }
  549. /*
  550. * Requeue nr_requeue waiters and possibly one more in the case
  551. * of requeue_pi if we couldn't acquire the lock atomically.
  552. *
  553. * Prepare the waiter to take the rt_mutex. Take a refcount
  554. * on the pi_state and store the pointer in the futex_q
  555. * object of the waiter.
  556. */
  557. get_pi_state(pi_state);
  558. /* Don't requeue when the waiter is already on the way out. */
  559. if (!futex_requeue_pi_prepare(this, pi_state)) {
  560. /*
  561. * Early woken waiter signaled that it is on the
  562. * way out. Drop the pi_state reference and try the
  563. * next waiter. @this->pi_state is still NULL.
  564. */
  565. put_pi_state(pi_state);
  566. continue;
  567. }
  568. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  569. this->rt_waiter,
  570. this->task);
  571. if (ret == 1) {
  572. /*
  573. * We got the lock. We do neither drop the refcount
  574. * on pi_state nor clear this->pi_state because the
  575. * waiter needs the pi_state for cleaning up the
  576. * user space value. It will drop the refcount
  577. * after doing so. this::requeue_state is updated
  578. * in the wakeup as well.
  579. */
  580. requeue_pi_wake_futex(this, &key2, hb2);
  581. task_count++;
  582. } else if (!ret) {
  583. /* Waiter is queued, move it to hb2 */
  584. requeue_futex(this, hb1, hb2, &key2);
  585. futex_requeue_pi_complete(this, 0);
  586. task_count++;
  587. } else {
  588. /*
  589. * rt_mutex_start_proxy_lock() detected a potential
  590. * deadlock when we tried to queue that waiter.
  591. * Drop the pi_state reference which we took above
  592. * and remove the pointer to the state from the
  593. * waiters futex_q object.
  594. */
  595. this->pi_state = NULL;
  596. put_pi_state(pi_state);
  597. futex_requeue_pi_complete(this, ret);
  598. /*
  599. * We stop queueing more waiters and let user space
  600. * deal with the mess.
  601. */
  602. break;
  603. }
  604. }
  605. /*
  606. * We took an extra initial reference to the pi_state in
  607. * futex_proxy_trylock_atomic(). We need to drop it here again.
  608. */
  609. put_pi_state(pi_state);
  610. out_unlock:
  611. double_unlock_hb(hb1, hb2);
  612. wake_up_q(&wake_q);
  613. futex_hb_waiters_dec(hb2);
  614. return ret ? ret : task_count;
  615. }
  616. /**
  617. * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex
  618. * @hb: the hash_bucket futex_q was original enqueued on
  619. * @q: the futex_q woken while waiting to be requeued
  620. * @timeout: the timeout associated with the wait (NULL if none)
  621. *
  622. * Determine the cause for the early wakeup.
  623. *
  624. * Return:
  625. * -EWOULDBLOCK or -ETIMEDOUT or -ERESTARTNOINTR
  626. */
  627. static inline
  628. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  629. struct futex_q *q,
  630. struct hrtimer_sleeper *timeout)
  631. {
  632. int ret;
  633. /*
  634. * With the hb lock held, we avoid races while we process the wakeup.
  635. * We only need to hold hb (and not hb2) to ensure atomicity as the
  636. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  637. * It can't be requeued from uaddr2 to something else since we don't
  638. * support a PI aware source futex for requeue.
  639. */
  640. WARN_ON_ONCE(&hb->lock != q->lock_ptr);
  641. /*
  642. * We were woken prior to requeue by a timeout or a signal.
  643. * Unqueue the futex_q and determine which it was.
  644. */
  645. plist_del(&q->list, &hb->chain);
  646. futex_hb_waiters_dec(hb);
  647. /* Handle spurious wakeups gracefully */
  648. ret = -EWOULDBLOCK;
  649. if (timeout && !timeout->task)
  650. ret = -ETIMEDOUT;
  651. else if (signal_pending(current))
  652. ret = -ERESTARTNOINTR;
  653. return ret;
  654. }
  655. /**
  656. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  657. * @uaddr: the futex we initially wait on (non-pi)
  658. * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
  659. * the same type, no requeueing from private to shared, etc.
  660. * @val: the expected value of uaddr
  661. * @abs_time: absolute timeout
  662. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
  663. * @uaddr2: the pi futex we will take prior to returning to user-space
  664. *
  665. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  666. * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
  667. * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
  668. * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
  669. * without one, the pi logic would not know which task to boost/deboost, if
  670. * there was a need to.
  671. *
  672. * We call schedule in futex_wait_queue() when we enqueue and return there
  673. * via the following--
  674. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  675. * 2) wakeup on uaddr2 after a requeue
  676. * 3) signal
  677. * 4) timeout
  678. *
  679. * If 3, cleanup and return -ERESTARTNOINTR.
  680. *
  681. * If 2, we may then block on trying to take the rt_mutex and return via:
  682. * 5) successful lock
  683. * 6) signal
  684. * 7) timeout
  685. * 8) other lock acquisition failure
  686. *
  687. * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
  688. *
  689. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  690. *
  691. * Return:
  692. * - 0 - On success;
  693. * - <0 - On error
  694. */
  695. int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  696. u32 val, ktime_t *abs_time, u32 bitset,
  697. u32 __user *uaddr2)
  698. {
  699. struct hrtimer_sleeper timeout, *to;
  700. struct rt_mutex_waiter rt_waiter;
  701. struct futex_hash_bucket *hb;
  702. union futex_key key2 = FUTEX_KEY_INIT;
  703. struct futex_q q = futex_q_init;
  704. struct rt_mutex_base *pi_mutex;
  705. int res, ret;
  706. if (!IS_ENABLED(CONFIG_FUTEX_PI))
  707. return -ENOSYS;
  708. if (uaddr == uaddr2)
  709. return -EINVAL;
  710. if (!bitset)
  711. return -EINVAL;
  712. to = futex_setup_timer(abs_time, &timeout, flags,
  713. current->timer_slack_ns);
  714. /*
  715. * The waiter is allocated on our stack, manipulated by the requeue
  716. * code while we sleep on uaddr.
  717. */
  718. rt_mutex_init_waiter(&rt_waiter);
  719. ret = get_futex_key(uaddr2, flags, &key2, FUTEX_WRITE);
  720. if (unlikely(ret != 0))
  721. goto out;
  722. q.bitset = bitset;
  723. q.rt_waiter = &rt_waiter;
  724. q.requeue_pi_key = &key2;
  725. /*
  726. * Prepare to wait on uaddr. On success, it holds hb->lock and q
  727. * is initialized.
  728. */
  729. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  730. if (ret)
  731. goto out;
  732. /*
  733. * The check above which compares uaddrs is not sufficient for
  734. * shared futexes. We need to compare the keys:
  735. */
  736. if (futex_match(&q.key, &key2)) {
  737. futex_q_unlock(hb);
  738. ret = -EINVAL;
  739. goto out;
  740. }
  741. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  742. futex_wait_queue(hb, &q, to);
  743. switch (futex_requeue_pi_wakeup_sync(&q)) {
  744. case Q_REQUEUE_PI_IGNORE:
  745. /* The waiter is still on uaddr1 */
  746. spin_lock(&hb->lock);
  747. ret = handle_early_requeue_pi_wakeup(hb, &q, to);
  748. spin_unlock(&hb->lock);
  749. break;
  750. case Q_REQUEUE_PI_LOCKED:
  751. /* The requeue acquired the lock */
  752. if (q.pi_state && (q.pi_state->owner != current)) {
  753. spin_lock(q.lock_ptr);
  754. ret = fixup_pi_owner(uaddr2, &q, true);
  755. /*
  756. * Drop the reference to the pi state which the
  757. * requeue_pi() code acquired for us.
  758. */
  759. put_pi_state(q.pi_state);
  760. spin_unlock(q.lock_ptr);
  761. /*
  762. * Adjust the return value. It's either -EFAULT or
  763. * success (1) but the caller expects 0 for success.
  764. */
  765. ret = ret < 0 ? ret : 0;
  766. }
  767. break;
  768. case Q_REQUEUE_PI_DONE:
  769. /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */
  770. pi_mutex = &q.pi_state->pi_mutex;
  771. ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
  772. /*
  773. * See futex_unlock_pi()'s cleanup: comment.
  774. */
  775. if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
  776. ret = 0;
  777. spin_lock(q.lock_ptr);
  778. debug_rt_mutex_free_waiter(&rt_waiter);
  779. /*
  780. * Fixup the pi_state owner and possibly acquire the lock if we
  781. * haven't already.
  782. */
  783. res = fixup_pi_owner(uaddr2, &q, !ret);
  784. /*
  785. * If fixup_pi_owner() returned an error, propagate that. If it
  786. * acquired the lock, clear -ETIMEDOUT or -EINTR.
  787. */
  788. if (res)
  789. ret = (res < 0) ? res : 0;
  790. futex_unqueue_pi(&q);
  791. spin_unlock(q.lock_ptr);
  792. if (ret == -EINTR) {
  793. /*
  794. * We've already been requeued, but cannot restart
  795. * by calling futex_lock_pi() directly. We could
  796. * restart this syscall, but it would detect that
  797. * the user space "val" changed and return
  798. * -EWOULDBLOCK. Save the overhead of the restart
  799. * and return -EWOULDBLOCK directly.
  800. */
  801. ret = -EWOULDBLOCK;
  802. }
  803. break;
  804. default:
  805. BUG();
  806. }
  807. out:
  808. if (to) {
  809. hrtimer_cancel(&to->timer);
  810. destroy_hrtimer_on_stack(&to->timer);
  811. }
  812. return ret;
  813. }