requeue.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. struct task_struct *task;
  208. q->key = *key;
  209. __futex_unqueue(q);
  210. WARN_ON(!q->rt_waiter);
  211. q->rt_waiter = NULL;
  212. q->lock_ptr = &hb->lock;
  213. task = READ_ONCE(q->task);
  214. /* Signal locked state to the waiter */
  215. futex_requeue_pi_complete(q, 1);
  216. wake_up_state(task, TASK_NORMAL);
  217. }
  218. /**
  219. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  220. * @pifutex: the user address of the to futex
  221. * @hb1: the from futex hash bucket, must be locked by the caller
  222. * @hb2: the to futex hash bucket, must be locked by the caller
  223. * @key1: the from futex key
  224. * @key2: the to futex key
  225. * @ps: address to store the pi_state pointer
  226. * @exiting: Pointer to store the task pointer of the owner task
  227. * which is in the middle of exiting
  228. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  229. *
  230. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  231. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  232. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  233. * hb1 and hb2 must be held by the caller.
  234. *
  235. * @exiting is only set when the return value is -EBUSY. If so, this holds
  236. * a refcount on the exiting task on return and the caller needs to drop it
  237. * after waiting for the exit to complete.
  238. *
  239. * Return:
  240. * - 0 - failed to acquire the lock atomically;
  241. * - >0 - acquired the lock, return value is vpid of the top_waiter
  242. * - <0 - error
  243. */
  244. static int
  245. futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
  246. struct futex_hash_bucket *hb2, union futex_key *key1,
  247. union futex_key *key2, struct futex_pi_state **ps,
  248. struct task_struct **exiting, int set_waiters)
  249. {
  250. struct futex_q *top_waiter;
  251. u32 curval;
  252. int ret;
  253. if (futex_get_value_locked(&curval, pifutex))
  254. return -EFAULT;
  255. if (unlikely(should_fail_futex(true)))
  256. return -EFAULT;
  257. /*
  258. * Find the top_waiter and determine if there are additional waiters.
  259. * If the caller intends to requeue more than 1 waiter to pifutex,
  260. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  261. * as we have means to handle the possible fault. If not, don't set
  262. * the bit unnecessarily as it will force the subsequent unlock to enter
  263. * the kernel.
  264. */
  265. top_waiter = futex_top_waiter(hb1, key1);
  266. /* There are no waiters, nothing for us to do. */
  267. if (!top_waiter)
  268. return 0;
  269. /*
  270. * Ensure that this is a waiter sitting in futex_wait_requeue_pi()
  271. * and waiting on the 'waitqueue' futex which is always !PI.
  272. */
  273. if (!top_waiter->rt_waiter || top_waiter->pi_state)
  274. return -EINVAL;
  275. /* Ensure we requeue to the expected futex. */
  276. if (!futex_match(top_waiter->requeue_pi_key, key2))
  277. return -EINVAL;
  278. /* Ensure that this does not race against an early wakeup */
  279. if (!futex_requeue_pi_prepare(top_waiter, NULL))
  280. return -EAGAIN;
  281. /*
  282. * Try to take the lock for top_waiter and set the FUTEX_WAITERS bit
  283. * in the contended case or if @set_waiters is true.
  284. *
  285. * In the contended case PI state is attached to the lock owner. If
  286. * the user space lock can be acquired then PI state is attached to
  287. * the new owner (@top_waiter->task) when @set_waiters is true.
  288. */
  289. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  290. exiting, set_waiters);
  291. if (ret == 1) {
  292. /*
  293. * Lock was acquired in user space and PI state was
  294. * attached to @top_waiter->task. That means state is fully
  295. * consistent and the waiter can return to user space
  296. * immediately after the wakeup.
  297. */
  298. requeue_pi_wake_futex(top_waiter, key2, hb2);
  299. } else if (ret < 0) {
  300. /* Rewind top_waiter::requeue_state */
  301. futex_requeue_pi_complete(top_waiter, ret);
  302. } else {
  303. /*
  304. * futex_lock_pi_atomic() did not acquire the user space
  305. * futex, but managed to establish the proxy lock and pi
  306. * state. top_waiter::requeue_state cannot be fixed up here
  307. * because the waiter is not enqueued on the rtmutex
  308. * yet. This is handled at the callsite depending on the
  309. * result of rt_mutex_start_proxy_lock() which is
  310. * guaranteed to be reached with this function returning 0.
  311. */
  312. }
  313. return ret;
  314. }
  315. /**
  316. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  317. * @uaddr1: source futex user address
  318. * @flags1: futex flags (FLAGS_SHARED, etc.)
  319. * @uaddr2: target futex user address
  320. * @flags2: futex flags (FLAGS_SHARED, etc.)
  321. * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  322. * @nr_requeue: number of waiters to requeue (0-INT_MAX)
  323. * @cmpval: @uaddr1 expected value (or %NULL)
  324. * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
  325. * pi futex (pi to pi requeue is not supported)
  326. *
  327. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  328. * uaddr2 atomically on behalf of the top waiter.
  329. *
  330. * Return:
  331. * - >=0 - on success, the number of tasks requeued or woken;
  332. * - <0 - on error
  333. */
  334. int futex_requeue(u32 __user *uaddr1, unsigned int flags1,
  335. u32 __user *uaddr2, unsigned int flags2,
  336. int nr_wake, int nr_requeue, u32 *cmpval, int requeue_pi)
  337. {
  338. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  339. int task_count = 0, ret;
  340. struct futex_pi_state *pi_state = NULL;
  341. struct futex_hash_bucket *hb1, *hb2;
  342. struct futex_q *this, *next;
  343. DEFINE_WAKE_Q(wake_q);
  344. if (nr_wake < 0 || nr_requeue < 0)
  345. return -EINVAL;
  346. /*
  347. * When PI not supported: return -ENOSYS if requeue_pi is true,
  348. * consequently the compiler knows requeue_pi is always false past
  349. * this point which will optimize away all the conditional code
  350. * further down.
  351. */
  352. if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi)
  353. return -ENOSYS;
  354. if (requeue_pi) {
  355. /*
  356. * Requeue PI only works on two distinct uaddrs. This
  357. * check is only valid for private futexes. See below.
  358. */
  359. if (uaddr1 == uaddr2)
  360. return -EINVAL;
  361. /*
  362. * futex_requeue() allows the caller to define the number
  363. * of waiters to wake up via the @nr_wake argument. With
  364. * REQUEUE_PI, waking up more than one waiter is creating
  365. * more problems than it solves. Waking up a waiter makes
  366. * only sense if the PI futex @uaddr2 is uncontended as
  367. * this allows the requeue code to acquire the futex
  368. * @uaddr2 before waking the waiter. The waiter can then
  369. * return to user space without further action. A secondary
  370. * wakeup would just make the futex_wait_requeue_pi()
  371. * handling more complex, because that code would have to
  372. * look up pi_state and do more or less all the handling
  373. * which the requeue code has to do for the to be requeued
  374. * waiters. So restrict the number of waiters to wake to
  375. * one, and only wake it up when the PI futex is
  376. * uncontended. Otherwise requeue it and let the unlock of
  377. * the PI futex handle the wakeup.
  378. *
  379. * All REQUEUE_PI users, e.g. pthread_cond_signal() and
  380. * pthread_cond_broadcast() must use nr_wake=1.
  381. */
  382. if (nr_wake != 1)
  383. return -EINVAL;
  384. /*
  385. * requeue_pi requires a pi_state, try to allocate it now
  386. * without any locks in case it fails.
  387. */
  388. if (refill_pi_state_cache())
  389. return -ENOMEM;
  390. }
  391. retry:
  392. ret = get_futex_key(uaddr1, flags1, &key1, FUTEX_READ);
  393. if (unlikely(ret != 0))
  394. return ret;
  395. ret = get_futex_key(uaddr2, flags2, &key2,
  396. requeue_pi ? FUTEX_WRITE : FUTEX_READ);
  397. if (unlikely(ret != 0))
  398. return ret;
  399. /*
  400. * The check above which compares uaddrs is not sufficient for
  401. * shared futexes. We need to compare the keys:
  402. */
  403. if (requeue_pi && futex_match(&key1, &key2))
  404. return -EINVAL;
  405. hb1 = futex_hash(&key1);
  406. hb2 = futex_hash(&key2);
  407. retry_private:
  408. futex_hb_waiters_inc(hb2);
  409. double_lock_hb(hb1, hb2);
  410. if (likely(cmpval != NULL)) {
  411. u32 curval;
  412. ret = futex_get_value_locked(&curval, uaddr1);
  413. if (unlikely(ret)) {
  414. double_unlock_hb(hb1, hb2);
  415. futex_hb_waiters_dec(hb2);
  416. ret = get_user(curval, uaddr1);
  417. if (ret)
  418. return ret;
  419. if (!(flags1 & FLAGS_SHARED))
  420. goto retry_private;
  421. goto retry;
  422. }
  423. if (curval != *cmpval) {
  424. ret = -EAGAIN;
  425. goto out_unlock;
  426. }
  427. }
  428. if (requeue_pi) {
  429. struct task_struct *exiting = NULL;
  430. /*
  431. * Attempt to acquire uaddr2 and wake the top waiter. If we
  432. * intend to requeue waiters, force setting the FUTEX_WAITERS
  433. * bit. We force this here where we are able to easily handle
  434. * faults rather in the requeue loop below.
  435. *
  436. * Updates topwaiter::requeue_state if a top waiter exists.
  437. */
  438. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  439. &key2, &pi_state,
  440. &exiting, nr_requeue);
  441. /*
  442. * At this point the top_waiter has either taken uaddr2 or
  443. * is waiting on it. In both cases pi_state has been
  444. * established and an initial refcount on it. In case of an
  445. * error there's nothing.
  446. *
  447. * The top waiter's requeue_state is up to date:
  448. *
  449. * - If the lock was acquired atomically (ret == 1), then
  450. * the state is Q_REQUEUE_PI_LOCKED.
  451. *
  452. * The top waiter has been dequeued and woken up and can
  453. * return to user space immediately. The kernel/user
  454. * space state is consistent. In case that there must be
  455. * more waiters requeued the WAITERS bit in the user
  456. * space futex is set so the top waiter task has to go
  457. * into the syscall slowpath to unlock the futex. This
  458. * will block until this requeue operation has been
  459. * completed and the hash bucket locks have been
  460. * dropped.
  461. *
  462. * - If the trylock failed with an error (ret < 0) then
  463. * the state is either Q_REQUEUE_PI_NONE, i.e. "nothing
  464. * happened", or Q_REQUEUE_PI_IGNORE when there was an
  465. * interleaved early wakeup.
  466. *
  467. * - If the trylock did not succeed (ret == 0) then the
  468. * state is either Q_REQUEUE_PI_IN_PROGRESS or
  469. * Q_REQUEUE_PI_WAIT if an early wakeup interleaved.
  470. * This will be cleaned up in the loop below, which
  471. * cannot fail because futex_proxy_trylock_atomic() did
  472. * the same sanity checks for requeue_pi as the loop
  473. * below does.
  474. */
  475. switch (ret) {
  476. case 0:
  477. /* We hold a reference on the pi state. */
  478. break;
  479. case 1:
  480. /*
  481. * futex_proxy_trylock_atomic() acquired the user space
  482. * futex. Adjust task_count.
  483. */
  484. task_count++;
  485. ret = 0;
  486. break;
  487. /*
  488. * If the above failed, then pi_state is NULL and
  489. * waiter::requeue_state is correct.
  490. */
  491. case -EFAULT:
  492. double_unlock_hb(hb1, hb2);
  493. futex_hb_waiters_dec(hb2);
  494. ret = fault_in_user_writeable(uaddr2);
  495. if (!ret)
  496. goto retry;
  497. return ret;
  498. case -EBUSY:
  499. case -EAGAIN:
  500. /*
  501. * Two reasons for this:
  502. * - EBUSY: Owner is exiting and we just wait for the
  503. * exit to complete.
  504. * - EAGAIN: The user space value changed.
  505. */
  506. double_unlock_hb(hb1, hb2);
  507. futex_hb_waiters_dec(hb2);
  508. /*
  509. * Handle the case where the owner is in the middle of
  510. * exiting. Wait for the exit to complete otherwise
  511. * this task might loop forever, aka. live lock.
  512. */
  513. wait_for_owner_exiting(ret, exiting);
  514. cond_resched();
  515. goto retry;
  516. default:
  517. goto out_unlock;
  518. }
  519. }
  520. plist_for_each_entry_safe(this, next, &hb1->chain, list) {
  521. if (task_count - nr_wake >= nr_requeue)
  522. break;
  523. if (!futex_match(&this->key, &key1))
  524. continue;
  525. /*
  526. * FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI should always
  527. * be paired with each other and no other futex ops.
  528. *
  529. * We should never be requeueing a futex_q with a pi_state,
  530. * which is awaiting a futex_unlock_pi().
  531. */
  532. if ((requeue_pi && !this->rt_waiter) ||
  533. (!requeue_pi && this->rt_waiter) ||
  534. this->pi_state) {
  535. ret = -EINVAL;
  536. break;
  537. }
  538. /* Plain futexes just wake or requeue and are done */
  539. if (!requeue_pi) {
  540. if (++task_count <= nr_wake)
  541. this->wake(&wake_q, this);
  542. else
  543. requeue_futex(this, hb1, hb2, &key2);
  544. continue;
  545. }
  546. /* Ensure we requeue to the expected futex for requeue_pi. */
  547. if (!futex_match(this->requeue_pi_key, &key2)) {
  548. ret = -EINVAL;
  549. break;
  550. }
  551. /*
  552. * Requeue nr_requeue waiters and possibly one more in the case
  553. * of requeue_pi if we couldn't acquire the lock atomically.
  554. *
  555. * Prepare the waiter to take the rt_mutex. Take a refcount
  556. * on the pi_state and store the pointer in the futex_q
  557. * object of the waiter.
  558. */
  559. get_pi_state(pi_state);
  560. /* Don't requeue when the waiter is already on the way out. */
  561. if (!futex_requeue_pi_prepare(this, pi_state)) {
  562. /*
  563. * Early woken waiter signaled that it is on the
  564. * way out. Drop the pi_state reference and try the
  565. * next waiter. @this->pi_state is still NULL.
  566. */
  567. put_pi_state(pi_state);
  568. continue;
  569. }
  570. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  571. this->rt_waiter,
  572. this->task);
  573. if (ret == 1) {
  574. /*
  575. * We got the lock. We do neither drop the refcount
  576. * on pi_state nor clear this->pi_state because the
  577. * waiter needs the pi_state for cleaning up the
  578. * user space value. It will drop the refcount
  579. * after doing so. this::requeue_state is updated
  580. * in the wakeup as well.
  581. */
  582. requeue_pi_wake_futex(this, &key2, hb2);
  583. task_count++;
  584. } else if (!ret) {
  585. /* Waiter is queued, move it to hb2 */
  586. requeue_futex(this, hb1, hb2, &key2);
  587. futex_requeue_pi_complete(this, 0);
  588. task_count++;
  589. } else {
  590. /*
  591. * rt_mutex_start_proxy_lock() detected a potential
  592. * deadlock when we tried to queue that waiter.
  593. * Drop the pi_state reference which we took above
  594. * and remove the pointer to the state from the
  595. * waiters futex_q object.
  596. */
  597. this->pi_state = NULL;
  598. put_pi_state(pi_state);
  599. futex_requeue_pi_complete(this, ret);
  600. /*
  601. * We stop queueing more waiters and let user space
  602. * deal with the mess.
  603. */
  604. break;
  605. }
  606. }
  607. /*
  608. * We took an extra initial reference to the pi_state in
  609. * futex_proxy_trylock_atomic(). We need to drop it here again.
  610. */
  611. put_pi_state(pi_state);
  612. out_unlock:
  613. double_unlock_hb(hb1, hb2);
  614. wake_up_q(&wake_q);
  615. futex_hb_waiters_dec(hb2);
  616. return ret ? ret : task_count;
  617. }
  618. /**
  619. * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex
  620. * @hb: the hash_bucket futex_q was original enqueued on
  621. * @q: the futex_q woken while waiting to be requeued
  622. * @timeout: the timeout associated with the wait (NULL if none)
  623. *
  624. * Determine the cause for the early wakeup.
  625. *
  626. * Return:
  627. * -EWOULDBLOCK or -ETIMEDOUT or -ERESTARTNOINTR
  628. */
  629. static inline
  630. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  631. struct futex_q *q,
  632. struct hrtimer_sleeper *timeout)
  633. {
  634. int ret;
  635. /*
  636. * With the hb lock held, we avoid races while we process the wakeup.
  637. * We only need to hold hb (and not hb2) to ensure atomicity as the
  638. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  639. * It can't be requeued from uaddr2 to something else since we don't
  640. * support a PI aware source futex for requeue.
  641. */
  642. WARN_ON_ONCE(&hb->lock != q->lock_ptr);
  643. /*
  644. * We were woken prior to requeue by a timeout or a signal.
  645. * Unqueue the futex_q and determine which it was.
  646. */
  647. plist_del(&q->list, &hb->chain);
  648. futex_hb_waiters_dec(hb);
  649. /* Handle spurious wakeups gracefully */
  650. ret = -EWOULDBLOCK;
  651. if (timeout && !timeout->task)
  652. ret = -ETIMEDOUT;
  653. else if (signal_pending(current))
  654. ret = -ERESTARTNOINTR;
  655. return ret;
  656. }
  657. /**
  658. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  659. * @uaddr: the futex we initially wait on (non-pi)
  660. * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
  661. * the same type, no requeueing from private to shared, etc.
  662. * @val: the expected value of uaddr
  663. * @abs_time: absolute timeout
  664. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
  665. * @uaddr2: the pi futex we will take prior to returning to user-space
  666. *
  667. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  668. * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
  669. * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
  670. * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
  671. * without one, the pi logic would not know which task to boost/deboost, if
  672. * there was a need to.
  673. *
  674. * We call schedule in futex_wait_queue() when we enqueue and return there
  675. * via the following--
  676. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  677. * 2) wakeup on uaddr2 after a requeue
  678. * 3) signal
  679. * 4) timeout
  680. *
  681. * If 3, cleanup and return -ERESTARTNOINTR.
  682. *
  683. * If 2, we may then block on trying to take the rt_mutex and return via:
  684. * 5) successful lock
  685. * 6) signal
  686. * 7) timeout
  687. * 8) other lock acquisition failure
  688. *
  689. * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
  690. *
  691. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  692. *
  693. * Return:
  694. * - 0 - On success;
  695. * - <0 - On error
  696. */
  697. int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  698. u32 val, ktime_t *abs_time, u32 bitset,
  699. u32 __user *uaddr2)
  700. {
  701. struct hrtimer_sleeper timeout, *to;
  702. struct rt_mutex_waiter rt_waiter;
  703. struct futex_hash_bucket *hb;
  704. union futex_key key2 = FUTEX_KEY_INIT;
  705. struct futex_q q = futex_q_init;
  706. struct rt_mutex_base *pi_mutex;
  707. int res, ret;
  708. if (!IS_ENABLED(CONFIG_FUTEX_PI))
  709. return -ENOSYS;
  710. if (uaddr == uaddr2)
  711. return -EINVAL;
  712. if (!bitset)
  713. return -EINVAL;
  714. to = futex_setup_timer(abs_time, &timeout, flags,
  715. current->timer_slack_ns);
  716. /*
  717. * The waiter is allocated on our stack, manipulated by the requeue
  718. * code while we sleep on uaddr.
  719. */
  720. rt_mutex_init_waiter(&rt_waiter);
  721. ret = get_futex_key(uaddr2, flags, &key2, FUTEX_WRITE);
  722. if (unlikely(ret != 0))
  723. goto out;
  724. q.bitset = bitset;
  725. q.rt_waiter = &rt_waiter;
  726. q.requeue_pi_key = &key2;
  727. /*
  728. * Prepare to wait on uaddr. On success, it holds hb->lock and q
  729. * is initialized.
  730. */
  731. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  732. if (ret)
  733. goto out;
  734. /*
  735. * The check above which compares uaddrs is not sufficient for
  736. * shared futexes. We need to compare the keys:
  737. */
  738. if (futex_match(&q.key, &key2)) {
  739. futex_q_unlock(hb);
  740. ret = -EINVAL;
  741. goto out;
  742. }
  743. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  744. futex_wait_queue(hb, &q, to);
  745. switch (futex_requeue_pi_wakeup_sync(&q)) {
  746. case Q_REQUEUE_PI_IGNORE:
  747. /* The waiter is still on uaddr1 */
  748. spin_lock(&hb->lock);
  749. ret = handle_early_requeue_pi_wakeup(hb, &q, to);
  750. spin_unlock(&hb->lock);
  751. break;
  752. case Q_REQUEUE_PI_LOCKED:
  753. /* The requeue acquired the lock */
  754. if (q.pi_state && (q.pi_state->owner != current)) {
  755. spin_lock(q.lock_ptr);
  756. ret = fixup_pi_owner(uaddr2, &q, true);
  757. /*
  758. * Drop the reference to the pi state which the
  759. * requeue_pi() code acquired for us.
  760. */
  761. put_pi_state(q.pi_state);
  762. spin_unlock(q.lock_ptr);
  763. /*
  764. * Adjust the return value. It's either -EFAULT or
  765. * success (1) but the caller expects 0 for success.
  766. */
  767. ret = ret < 0 ? ret : 0;
  768. }
  769. break;
  770. case Q_REQUEUE_PI_DONE:
  771. /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */
  772. pi_mutex = &q.pi_state->pi_mutex;
  773. ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter);
  774. /*
  775. * See futex_unlock_pi()'s cleanup: comment.
  776. */
  777. if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter))
  778. ret = 0;
  779. spin_lock(q.lock_ptr);
  780. debug_rt_mutex_free_waiter(&rt_waiter);
  781. /*
  782. * Fixup the pi_state owner and possibly acquire the lock if we
  783. * haven't already.
  784. */
  785. res = fixup_pi_owner(uaddr2, &q, !ret);
  786. /*
  787. * If fixup_pi_owner() returned an error, propagate that. If it
  788. * acquired the lock, clear -ETIMEDOUT or -EINTR.
  789. */
  790. if (res)
  791. ret = (res < 0) ? res : 0;
  792. futex_unqueue_pi(&q);
  793. spin_unlock(q.lock_ptr);
  794. if (ret == -EINTR) {
  795. /*
  796. * We've already been requeued, but cannot restart
  797. * by calling futex_lock_pi() directly. We could
  798. * restart this syscall, but it would detect that
  799. * the user space "val" changed and return
  800. * -EWOULDBLOCK. Save the overhead of the restart
  801. * and return -EWOULDBLOCK directly.
  802. */
  803. ret = -EWOULDBLOCK;
  804. }
  805. break;
  806. default:
  807. BUG();
  808. }
  809. out:
  810. if (to) {
  811. hrtimer_cancel(&to->timer);
  812. destroy_hrtimer_on_stack(&to->timer);
  813. }
  814. return ret;
  815. }