Skip to content

Commit

Permalink
futex: fix reference leak
Browse files Browse the repository at this point in the history
Catalin noticed that (38d47c1: futex: rely on get_user_pages() for
shared futexes) caused an mm_struct leak.

Some tracing with the function graph tracer quickly pointed out that
futex_wait() has exit paths with unbalanced reference counts.

This regression was discovered by kmemleak.

Reported-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Tested-by: "Pallipadi, Venkatesh" <venkatesh.pallipadi@intel.com>
Tested-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Feb 11, 2009
1 parent 6c6f1f0 commit 2fff78c
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions kernel/futex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ static int futex_wait(u32 __user *uaddr, int fshared,
u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
{
struct task_struct *curr = current;
struct restart_block *restart;
DECLARE_WAITQUEUE(wait, curr);
struct futex_hash_bucket *hb;
struct futex_q q;
Expand Down Expand Up @@ -1216,11 +1217,13 @@ static int futex_wait(u32 __user *uaddr, int fshared,

if (!ret)
goto retry;
return ret;
goto out;
}
ret = -EWOULDBLOCK;
if (uval != val)
goto out_unlock_put_key;
if (unlikely(uval != val)) {
queue_unlock(&q, hb);
goto out_put_key;
}

/* Only actually queue if *uaddr contained val. */
queue_me(&q, hb);
Expand Down Expand Up @@ -1284,38 +1287,38 @@ static int futex_wait(u32 __user *uaddr, int fshared,
*/

/* If we were woken (and unqueued), we succeeded, whatever. */
ret = 0;
if (!unqueue_me(&q))
return 0;
goto out_put_key;
ret = -ETIMEDOUT;
if (rem)
return -ETIMEDOUT;
goto out_put_key;

/*
* We expect signal_pending(current), but another thread may
* have handled it for us already.
*/
ret = -ERESTARTSYS;
if (!abs_time)
return -ERESTARTSYS;
else {
struct restart_block *restart;
restart = &current_thread_info()->restart_block;
restart->fn = futex_wait_restart;
restart->futex.uaddr = (u32 *)uaddr;
restart->futex.val = val;
restart->futex.time = abs_time->tv64;
restart->futex.bitset = bitset;
restart->futex.flags = 0;

if (fshared)
restart->futex.flags |= FLAGS_SHARED;
if (clockrt)
restart->futex.flags |= FLAGS_CLOCKRT;
return -ERESTART_RESTARTBLOCK;
}
goto out_put_key;

out_unlock_put_key:
queue_unlock(&q, hb);
put_futex_key(fshared, &q.key);
restart = &current_thread_info()->restart_block;
restart->fn = futex_wait_restart;
restart->futex.uaddr = (u32 *)uaddr;
restart->futex.val = val;
restart->futex.time = abs_time->tv64;
restart->futex.bitset = bitset;
restart->futex.flags = 0;

if (fshared)
restart->futex.flags |= FLAGS_SHARED;
if (clockrt)
restart->futex.flags |= FLAGS_CLOCKRT;

ret = -ERESTART_RESTARTBLOCK;

out_put_key:
put_futex_key(fshared, &q.key);
out:
return ret;
}
Expand Down

0 comments on commit 2fff78c

Please sign in to comment.