aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/futex.c
diff options
context:
space:
mode:
authorDarren Hart <dvhltc@us.ibm.com>2009-04-03 16:40:40 -0400
committerThomas Gleixner <tglx@linutronix.de>2009-04-06 05:14:02 -0400
commitf801073f87aa22ddf0e9146355fec3993163790f (patch)
tree5499760cfb1abcebdda8868ec0074ae3ff4da0d5 /kernel/futex.c
parent9121e4783cd5c7e2a407763f3b61c2d573891133 (diff)
futex: split out futex value validation code
Refactor the code to validate the expected futex value in order to reuse it with the requeue_pi code. Signed-off-by: Darren Hart <dvhltc@us.ibm.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'kernel/futex.c')
-rw-r--r--kernel/futex.c116
1 files changed, 72 insertions, 44 deletions
diff --git a/kernel/futex.c b/kernel/futex.c
index e76942e2a79f..dbe857aa4381 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -1398,42 +1398,29 @@ static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
1398 __set_current_state(TASK_RUNNING); 1398 __set_current_state(TASK_RUNNING);
1399} 1399}
1400 1400
1401static int futex_wait(u32 __user *uaddr, int fshared, 1401/**
1402 u32 val, ktime_t *abs_time, u32 bitset, int clockrt) 1402 * futex_wait_setup() - Prepare to wait on a futex
1403 * @uaddr: the futex userspace address
1404 * @val: the expected value
1405 * @fshared: whether the futex is shared (1) or not (0)
1406 * @q: the associated futex_q
1407 * @hb: storage for hash_bucket pointer to be returned to caller
1408 *
1409 * Setup the futex_q and locate the hash_bucket. Get the futex value and
1410 * compare it with the expected value. Handle atomic faults internally.
1411 * Return with the hb lock held and a q.key reference on success, and unlocked
1412 * with no q.key reference on failure.
1413 *
1414 * Returns:
1415 * 0 - uaddr contains val and hb has been locked
1416 * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlcoked
1417 */
1418static int futex_wait_setup(u32 __user *uaddr, u32 val, int fshared,
1419 struct futex_q *q, struct futex_hash_bucket **hb)
1403{ 1420{
1404 struct hrtimer_sleeper timeout, *to = NULL;
1405 DECLARE_WAITQUEUE(wait, current);
1406 struct restart_block *restart;
1407 struct futex_hash_bucket *hb;
1408 struct futex_q q;
1409 u32 uval; 1421 u32 uval;
1410 int ret; 1422 int ret;
1411 1423
1412 if (!bitset)
1413 return -EINVAL;
1414
1415 q.pi_state = NULL;
1416 q.bitset = bitset;
1417
1418 if (abs_time) {
1419 to = &timeout;
1420
1421 hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
1422 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1423 hrtimer_init_sleeper(to, current);
1424 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
1425 current->timer_slack_ns);
1426 }
1427
1428retry:
1429 q.key = FUTEX_KEY_INIT;
1430 ret = get_futex_key(uaddr, fshared, &q.key);
1431 if (unlikely(ret != 0))
1432 goto out;
1433
1434retry_private:
1435 hb = queue_lock(&q);
1436
1437 /* 1424 /*
1438 * Access the page AFTER the hash-bucket is locked. 1425 * Access the page AFTER the hash-bucket is locked.
1439 * Order is important: 1426 * Order is important:
@@ -1450,33 +1437,74 @@ retry_private:
1450 * A consequence is that futex_wait() can return zero and absorb 1437 * A consequence is that futex_wait() can return zero and absorb
1451 * a wakeup when *uaddr != val on entry to the syscall. This is 1438 * a wakeup when *uaddr != val on entry to the syscall. This is
1452 * rare, but normal. 1439 * rare, but normal.
1453 *
1454 * For shared futexes, we hold the mmap semaphore, so the mapping
1455 * cannot have changed since we looked it up in get_futex_key.
1456 */ 1440 */
1441retry:
1442 q->key = FUTEX_KEY_INIT;
1443 ret = get_futex_key(uaddr, fshared, &q->key);
1444 if (unlikely(ret != 0))
1445 goto out;
1446
1447retry_private:
1448 *hb = queue_lock(q);
1449
1457 ret = get_futex_value_locked(&uval, uaddr); 1450 ret = get_futex_value_locked(&uval, uaddr);
1458 1451
1459 if (unlikely(ret)) { 1452 if (ret) {
1460 queue_unlock(&q, hb); 1453 queue_unlock(q, *hb);
1461 1454
1462 ret = get_user(uval, uaddr); 1455 ret = get_user(uval, uaddr);
1463 if (ret) 1456 if (ret)
1464 goto out_put_key; 1457 goto out;
1465 1458
1466 if (!fshared) 1459 if (!fshared)
1467 goto retry_private; 1460 goto retry_private;
1468 1461
1469 put_futex_key(fshared, &q.key); 1462 put_futex_key(fshared, &q->key);
1470 goto retry; 1463 goto retry;
1471 } 1464 }
1472 ret = -EWOULDBLOCK;
1473 1465
1474 /* Only actually queue if *uaddr contained val. */ 1466 if (uval != val) {
1475 if (unlikely(uval != val)) { 1467 queue_unlock(q, *hb);
1476 queue_unlock(&q, hb); 1468 ret = -EWOULDBLOCK;
1477 goto out_put_key;
1478 } 1469 }
1479 1470
1471out:
1472 if (ret)
1473 put_futex_key(fshared, &q->key);
1474 return ret;
1475}
1476
1477static int futex_wait(u32 __user *uaddr, int fshared,
1478 u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
1479{
1480 struct hrtimer_sleeper timeout, *to = NULL;
1481 DECLARE_WAITQUEUE(wait, current);
1482 struct restart_block *restart;
1483 struct futex_hash_bucket *hb;
1484 struct futex_q q;
1485 int ret;
1486
1487 if (!bitset)
1488 return -EINVAL;
1489
1490 q.pi_state = NULL;
1491 q.bitset = bitset;
1492
1493 if (abs_time) {
1494 to = &timeout;
1495
1496 hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
1497 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1498 hrtimer_init_sleeper(to, current);
1499 hrtimer_set_expires_range_ns(&to->timer, *abs_time,
1500 current->timer_slack_ns);
1501 }
1502
1503 /* Prepare to wait on uaddr. */
1504 ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
1505 if (ret)
1506 goto out;
1507
1480 /* queue_me and wait for wakeup, timeout, or a signal. */ 1508 /* queue_me and wait for wakeup, timeout, or a signal. */
1481 futex_wait_queue_me(hb, &q, to, &wait); 1509 futex_wait_queue_me(hb, &q, to, &wait);
1482 1510