Skip to content

Commit

Permalink
futex: make futex_(get|put)_key() calls symmetric
Browse files Browse the repository at this point in the history
Impact: cleanup

This patch makes the calls to futex_get_key_refs() and futex_drop_key_refs()
explicitly symmetric by only "putting" keys we successfully "got".  Also
cleanup a couple return points that didn't "put" after a successful "get".

Build and boot tested on an x86_64 system.

Signed-off-by: Darren Hart <dvhltc@us.ibm.com>
Cc: <stable@kernel.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Darren Hart authored and Ingo Molnar committed Dec 30, 2008
1 parent b568636 commit 42d35d4
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions kernel/futex.c
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,8 @@ static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
}

spin_unlock(&hb->lock);
out:
put_futex_key(fshared, &key);
out:
return ret;
}

Expand All @@ -748,7 +748,7 @@ futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
goto out;
ret = get_futex_key(uaddr2, fshared, &key2);
if (unlikely(ret != 0))
goto out;
goto out_put_key1;

hb1 = hash_futex(&key1);
hb2 = hash_futex(&key2);
Expand All @@ -770,12 +770,12 @@ futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
* but we might get them from range checking
*/
ret = op_ret;
goto out;
goto out_put_keys;
#endif

if (unlikely(op_ret != -EFAULT)) {
ret = op_ret;
goto out;
goto out_put_keys;
}

/*
Expand All @@ -789,7 +789,7 @@ futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
ret = futex_handle_fault((unsigned long)uaddr2,
attempt);
if (ret)
goto out;
goto out_put_keys;
goto retry;
}

Expand Down Expand Up @@ -827,10 +827,11 @@ futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
spin_unlock(&hb1->lock);
if (hb1 != hb2)
spin_unlock(&hb2->lock);
out:
out_put_keys:
put_futex_key(fshared, &key2);
out_put_key1:
put_futex_key(fshared, &key1);

out:
return ret;
}

Expand All @@ -847,13 +848,13 @@ static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
struct futex_q *this, *next;
int ret, drop_count = 0;

retry:
retry:
ret = get_futex_key(uaddr1, fshared, &key1);
if (unlikely(ret != 0))
goto out;
ret = get_futex_key(uaddr2, fshared, &key2);
if (unlikely(ret != 0))
goto out;
goto out_put_key1;

hb1 = hash_futex(&key1);
hb2 = hash_futex(&key2);
Expand All @@ -875,7 +876,7 @@ static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
if (!ret)
goto retry;

return ret;
goto out_put_keys;
}
if (curval != *cmpval) {
ret = -EAGAIN;
Expand Down Expand Up @@ -920,9 +921,11 @@ static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
while (--drop_count >= 0)
drop_futex_key_refs(&key1);

out:
out_put_keys:
put_futex_key(fshared, &key2);
out_put_key1:
put_futex_key(fshared, &key1);
out:
return ret;
}

Expand Down Expand Up @@ -983,7 +986,7 @@ static int unqueue_me(struct futex_q *q)
int ret = 0;

/* In the common case we don't take the spinlock, which is nice. */
retry:
retry:
lock_ptr = q->lock_ptr;
barrier();
if (lock_ptr != NULL) {
Expand Down Expand Up @@ -1165,11 +1168,11 @@ static int futex_wait(u32 __user *uaddr, int fshared,

q.pi_state = NULL;
q.bitset = bitset;
retry:
retry:
q.key = FUTEX_KEY_INIT;
ret = get_futex_key(uaddr, fshared, &q.key);
if (unlikely(ret != 0))
goto out_release_sem;
goto out;

hb = queue_lock(&q);

Expand Down Expand Up @@ -1197,6 +1200,7 @@ static int futex_wait(u32 __user *uaddr, int fshared,

if (unlikely(ret)) {
queue_unlock(&q, hb);
put_futex_key(fshared, &q.key);

ret = get_user(uval, uaddr);

Expand All @@ -1206,7 +1210,7 @@ static int futex_wait(u32 __user *uaddr, int fshared,
}
ret = -EWOULDBLOCK;
if (uval != val)
goto out_unlock_release_sem;
goto out_unlock_put_key;

/* Only actually queue if *uaddr contained val. */
queue_me(&q, hb);
Expand Down Expand Up @@ -1298,11 +1302,11 @@ static int futex_wait(u32 __user *uaddr, int fshared,
return -ERESTART_RESTARTBLOCK;
}

out_unlock_release_sem:
out_unlock_put_key:
queue_unlock(&q, hb);

out_release_sem:
put_futex_key(fshared, &q.key);

out:
return ret;
}

Expand Down Expand Up @@ -1351,16 +1355,16 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
}

q.pi_state = NULL;
retry:
retry:
q.key = FUTEX_KEY_INIT;
ret = get_futex_key(uaddr, fshared, &q.key);
if (unlikely(ret != 0))
goto out_release_sem;
goto out;

retry_unlocked:
retry_unlocked:
hb = queue_lock(&q);

retry_locked:
retry_locked:
ret = lock_taken = 0;

/*
Expand All @@ -1381,14 +1385,14 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
*/
if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
ret = -EDEADLK;
goto out_unlock_release_sem;
goto out_unlock_put_key;
}

/*
* Surprise - we got the lock. Just return to userspace:
*/
if (unlikely(!curval))
goto out_unlock_release_sem;
goto out_unlock_put_key;

uval = curval;

Expand Down Expand Up @@ -1424,7 +1428,7 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
* We took the lock due to owner died take over.
*/
if (unlikely(lock_taken))
goto out_unlock_release_sem;
goto out_unlock_put_key;

/*
* We dont have the lock. Look up the PI state (or create it if
Expand Down Expand Up @@ -1463,7 +1467,7 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
goto retry_locked;
}
default:
goto out_unlock_release_sem;
goto out_unlock_put_key;
}
}

Expand Down Expand Up @@ -1554,16 +1558,17 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
destroy_hrtimer_on_stack(&to->timer);
return ret != -EINTR ? ret : -ERESTARTNOINTR;

out_unlock_release_sem:
out_unlock_put_key:
queue_unlock(&q, hb);

out_release_sem:
out_put_key:
put_futex_key(fshared, &q.key);
out:
if (to)
destroy_hrtimer_on_stack(&to->timer);
return ret;

uaddr_faulted:
uaddr_faulted:
/*
* We have to r/w *(int __user *)uaddr, and we have to modify it
* atomically. Therefore, if we continue to fault after get_user()
Expand All @@ -1576,7 +1581,7 @@ static int futex_lock_pi(u32 __user *uaddr, int fshared,
if (attempt++) {
ret = futex_handle_fault((unsigned long)uaddr, attempt);
if (ret)
goto out_release_sem;
goto out_put_key;
goto retry_unlocked;
}

Expand Down Expand Up @@ -1668,9 +1673,9 @@ static int futex_unlock_pi(u32 __user *uaddr, int fshared)

out_unlock:
spin_unlock(&hb->lock);
out:
put_futex_key(fshared, &key);

out:
return ret;

pi_faulted:
Expand Down

0 comments on commit 42d35d4

Please sign in to comment.