diff options
| -rw-r--r-- | kernel/futex.c | 137 |
1 files changed, 76 insertions, 61 deletions
diff --git a/kernel/futex.c b/kernel/futex.c index 248dd119a86e..b911adceb2c4 100644 --- a/kernel/futex.c +++ b/kernel/futex.c | |||
| @@ -89,36 +89,36 @@ struct futex_pi_state { | |||
| 89 | union futex_key key; | 89 | union futex_key key; |
| 90 | }; | 90 | }; |
| 91 | 91 | ||
| 92 | /* | 92 | /** |
| 93 | * We use this hashed waitqueue instead of a normal wait_queue_t, so | 93 | * struct futex_q - The hashed futex queue entry, one per waiting task |
| 94 | * @task: the task waiting on the futex | ||
| 95 | * @lock_ptr: the hash bucket lock | ||
| 96 | * @key: the key the futex is hashed on | ||
| 97 | * @pi_state: optional priority inheritance state | ||
| 98 | * @rt_waiter: rt_waiter storage for use with requeue_pi | ||
| 99 | * @requeue_pi_key: the requeue_pi target futex key | ||
| 100 | * @bitset: bitset for the optional bitmasked wakeup | ||
| 101 | * | ||
| 102 | * We use this hashed waitqueue, instead of a normal wait_queue_t, so | ||
| 94 | * we can wake only the relevant ones (hashed queues may be shared). | 103 | * we can wake only the relevant ones (hashed queues may be shared). |
| 95 | * | 104 | * |
| 96 | * A futex_q has a woken state, just like tasks have TASK_RUNNING. | 105 | * A futex_q has a woken state, just like tasks have TASK_RUNNING. |
| 97 | * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. | 106 | * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. |
| 98 | * The order of wakup is always to make the first condition true, then | 107 | * The order of wakup is always to make the first condition true, then |
| 99 | * wake up q->waiter, then make the second condition true. | 108 | * the second. |
| 109 | * | ||
| 110 | * PI futexes are typically woken before they are removed from the hash list via | ||
| 111 | * the rt_mutex code. See unqueue_me_pi(). | ||
| 100 | */ | 112 | */ |
| 101 | struct futex_q { | 113 | struct futex_q { |
| 102 | struct plist_node list; | 114 | struct plist_node list; |
| 103 | /* Waiter reference */ | ||
| 104 | struct task_struct *task; | ||
| 105 | 115 | ||
| 106 | /* Which hash list lock to use: */ | 116 | struct task_struct *task; |
| 107 | spinlock_t *lock_ptr; | 117 | spinlock_t *lock_ptr; |
| 108 | |||
| 109 | /* Key which the futex is hashed on: */ | ||
| 110 | union futex_key key; | 118 | union futex_key key; |
| 111 | |||
| 112 | /* Optional priority inheritance state: */ | ||
| 113 | struct futex_pi_state *pi_state; | 119 | struct futex_pi_state *pi_state; |
| 114 | |||
| 115 | /* rt_waiter storage for requeue_pi: */ | ||
| 116 | struct rt_mutex_waiter *rt_waiter; | 120 | struct rt_mutex_waiter *rt_waiter; |
| 117 | |||
| 118 | /* The expected requeue pi target futex key: */ | ||
| 119 | union futex_key *requeue_pi_key; | 121 | union futex_key *requeue_pi_key; |
| 120 | |||
| 121 | /* Bitset for the optional bitmasked wakeup */ | ||
| 122 | u32 bitset; | 122 | u32 bitset; |
| 123 | }; | 123 | }; |
| 124 | 124 | ||
| @@ -198,11 +198,12 @@ static void drop_futex_key_refs(union futex_key *key) | |||
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | /** | 200 | /** |
| 201 | * get_futex_key - Get parameters which are the keys for a futex. | 201 | * get_futex_key() - Get parameters which are the keys for a futex |
| 202 | * @uaddr: virtual address of the futex | 202 | * @uaddr: virtual address of the futex |
| 203 | * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED | 203 | * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED |
| 204 | * @key: address where result is stored. | 204 | * @key: address where result is stored. |
| 205 | * @rw: mapping needs to be read/write (values: VERIFY_READ, VERIFY_WRITE) | 205 | * @rw: mapping needs to be read/write (values: VERIFY_READ, |
| 206 | * VERIFY_WRITE) | ||
| 206 | * | 207 | * |
| 207 | * Returns a negative error code or 0 | 208 | * Returns a negative error code or 0 |
| 208 | * The key words are stored in *key on success. | 209 | * The key words are stored in *key on success. |
| @@ -288,8 +289,8 @@ void put_futex_key(int fshared, union futex_key *key) | |||
| 288 | drop_futex_key_refs(key); | 289 | drop_futex_key_refs(key); |
| 289 | } | 290 | } |
| 290 | 291 | ||
| 291 | /* | 292 | /** |
| 292 | * fault_in_user_writeable - fault in user address and verify RW access | 293 | * fault_in_user_writeable() - Fault in user address and verify RW access |
| 293 | * @uaddr: pointer to faulting user space address | 294 | * @uaddr: pointer to faulting user space address |
| 294 | * | 295 | * |
| 295 | * Slow path to fixup the fault we just took in the atomic write | 296 | * Slow path to fixup the fault we just took in the atomic write |
| @@ -309,8 +310,8 @@ static int fault_in_user_writeable(u32 __user *uaddr) | |||
| 309 | 310 | ||
| 310 | /** | 311 | /** |
| 311 | * futex_top_waiter() - Return the highest priority waiter on a futex | 312 | * futex_top_waiter() - Return the highest priority waiter on a futex |
| 312 | * @hb: the hash bucket the futex_q's reside in | 313 | * @hb: the hash bucket the futex_q's reside in |
| 313 | * @key: the futex key (to distinguish it from other futex futex_q's) | 314 | * @key: the futex key (to distinguish it from other futex futex_q's) |
| 314 | * | 315 | * |
| 315 | * Must be called with the hb lock held. | 316 | * Must be called with the hb lock held. |
| 316 | */ | 317 | */ |
| @@ -588,7 +589,7 @@ lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, | |||
| 588 | } | 589 | } |
| 589 | 590 | ||
| 590 | /** | 591 | /** |
| 591 | * futex_lock_pi_atomic() - atomic work required to acquire a pi aware futex | 592 | * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex |
| 592 | * @uaddr: the pi futex user address | 593 | * @uaddr: the pi futex user address |
| 593 | * @hb: the pi futex hash bucket | 594 | * @hb: the pi futex hash bucket |
| 594 | * @key: the futex key associated with uaddr and hb | 595 | * @key: the futex key associated with uaddr and hb |
| @@ -1011,9 +1012,9 @@ void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1, | |||
| 1011 | 1012 | ||
| 1012 | /** | 1013 | /** |
| 1013 | * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue | 1014 | * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue |
| 1014 | * q: the futex_q | 1015 | * @q: the futex_q |
| 1015 | * key: the key of the requeue target futex | 1016 | * @key: the key of the requeue target futex |
| 1016 | * hb: the hash_bucket of the requeue target futex | 1017 | * @hb: the hash_bucket of the requeue target futex |
| 1017 | * | 1018 | * |
| 1018 | * During futex_requeue, with requeue_pi=1, it is possible to acquire the | 1019 | * During futex_requeue, with requeue_pi=1, it is possible to acquire the |
| 1019 | * target futex if it is uncontended or via a lock steal. Set the futex_q key | 1020 | * target futex if it is uncontended or via a lock steal. Set the futex_q key |
| @@ -1350,6 +1351,25 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q) | |||
| 1350 | return hb; | 1351 | return hb; |
| 1351 | } | 1352 | } |
| 1352 | 1353 | ||
| 1354 | static inline void | ||
| 1355 | queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb) | ||
| 1356 | { | ||
| 1357 | spin_unlock(&hb->lock); | ||
| 1358 | drop_futex_key_refs(&q->key); | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | /** | ||
| 1362 | * queue_me() - Enqueue the futex_q on the futex_hash_bucket | ||
| 1363 | * @q: The futex_q to enqueue | ||
| 1364 | * @hb: The destination hash bucket | ||
| 1365 | * | ||
| 1366 | * The hb->lock must be held by the caller, and is released here. A call to | ||
| 1367 | * queue_me() is typically paired with exactly one call to unqueue_me(). The | ||
| 1368 | * exceptions involve the PI related operations, which may use unqueue_me_pi() | ||
| 1369 | * or nothing if the unqueue is done as part of the wake process and the unqueue | ||
| 1370 | * state is implicit in the state of woken task (see futex_wait_requeue_pi() for | ||
| 1371 | * an example). | ||
| 1372 | */ | ||
| 1353 | static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb) | 1373 | static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb) |
| 1354 | { | 1374 | { |
| 1355 | int prio; | 1375 | int prio; |
| @@ -1373,19 +1393,17 @@ static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb) | |||
| 1373 | spin_unlock(&hb->lock); | 1393 | spin_unlock(&hb->lock); |
| 1374 | } | 1394 | } |
| 1375 | 1395 | ||
| 1376 | static inline void | 1396 | /** |
| 1377 | queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb) | 1397 | * unqueue_me() - Remove the futex_q from its futex_hash_bucket |
| 1378 | { | 1398 | * @q: The futex_q to unqueue |
| 1379 | spin_unlock(&hb->lock); | 1399 | * |
| 1380 | drop_futex_key_refs(&q->key); | 1400 | * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must |
| 1381 | } | 1401 | * be paired with exactly one earlier call to queue_me(). |
| 1382 | 1402 | * | |
| 1383 | /* | 1403 | * Returns: |
| 1384 | * queue_me and unqueue_me must be called as a pair, each | 1404 | * 1 - if the futex_q was still queued (and we removed unqueued it) |
| 1385 | * exactly once. They are called with the hashed spinlock held. | 1405 | * 0 - if the futex_q was already removed by the waking thread |
| 1386 | */ | 1406 | */ |
| 1387 | |||
| 1388 | /* Return 1 if we were still queued (ie. 0 means we were woken) */ | ||
| 1389 | static int unqueue_me(struct futex_q *q) | 1407 | static int unqueue_me(struct futex_q *q) |
| 1390 | { | 1408 | { |
| 1391 | spinlock_t *lock_ptr; | 1409 | spinlock_t *lock_ptr; |
| @@ -1638,17 +1656,14 @@ out: | |||
| 1638 | static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q, | 1656 | static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q, |
| 1639 | struct hrtimer_sleeper *timeout) | 1657 | struct hrtimer_sleeper *timeout) |
| 1640 | { | 1658 | { |
| 1641 | queue_me(q, hb); | ||
| 1642 | |||
| 1643 | /* | 1659 | /* |
