Commit 0d984734 authored by Brian Silverman's avatar Brian Silverman Committed by Jiri Slaby

futex: Fix a race condition between REQUEUE_PI and task death

commit 30a6b803 upstream.

free_pi_state and exit_pi_state_list both clean up futex_pi_state's.
exit_pi_state_list takes the hb lock first, and most callers of
free_pi_state do too. requeue_pi doesn't, which means free_pi_state
can free the pi_state out from under exit_pi_state_list. For example:

task A                            |  task B
exit_pi_state_list                |
  pi_state =                      |
      curr->pi_state_list->next   |
                                  |  futex_requeue(requeue_pi=1)
                                  |    // pi_state is the same as
                                  |    // the one in task A
                                  |    free_pi_state(pi_state)
                                  |      list_del_init(&pi_state->list)
                                  |      kfree(pi_state)
  list_del_init(&pi_state->list)  |

Move the free_pi_state calls in requeue_pi to before it drops the hb
locks which it's already holding.

[ tglx: Removed a pointless free_pi_state() call and the hb->lock held
  	debugging. The latter comes via a seperate patch ]
Signed-off-by: default avatarBrian Silverman <bsilver16384@gmail.com>
Cc: austin.linux@gmail.com
Cc: darren@dvhart.com
Cc: peterz@infradead.org
Cc: Mike Galbraith <mgalbraith@suse.de>
Link: http://lkml.kernel.org/r/1414282837-23092-1-git-send-email-bsilver16384@gmail.comSigned-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
parent be9382ca
...@@ -490,8 +490,14 @@ static struct futex_pi_state * alloc_pi_state(void) ...@@ -490,8 +490,14 @@ static struct futex_pi_state * alloc_pi_state(void)
return pi_state; return pi_state;
} }
/*
* Must be called with the hb lock held.
*/
static void free_pi_state(struct futex_pi_state *pi_state) static void free_pi_state(struct futex_pi_state *pi_state)
{ {
if (!pi_state)
return;
if (!atomic_dec_and_test(&pi_state->refcount)) if (!atomic_dec_and_test(&pi_state->refcount))
return; return;
...@@ -1405,15 +1411,6 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -1405,15 +1411,6 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
} }
retry: retry:
if (pi_state != NULL) {
/*
* We will have to lookup the pi_state again, so free this one
* to keep the accounting correct.
*/
free_pi_state(pi_state);
pi_state = NULL;
}
ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
if (unlikely(ret != 0)) if (unlikely(ret != 0))
goto out; goto out;
...@@ -1501,6 +1498,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -1501,6 +1498,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
case 0: case 0:
break; break;
case -EFAULT: case -EFAULT:
free_pi_state(pi_state);
pi_state = NULL;
double_unlock_hb(hb1, hb2); double_unlock_hb(hb1, hb2);
put_futex_key(&key2); put_futex_key(&key2);
put_futex_key(&key1); put_futex_key(&key1);
...@@ -1510,6 +1509,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -1510,6 +1509,8 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
goto out; goto out;
case -EAGAIN: case -EAGAIN:
/* The owner was exiting, try again. */ /* The owner was exiting, try again. */
free_pi_state(pi_state);
pi_state = NULL;
double_unlock_hb(hb1, hb2); double_unlock_hb(hb1, hb2);
put_futex_key(&key2); put_futex_key(&key2);
put_futex_key(&key1); put_futex_key(&key1);
...@@ -1586,6 +1587,7 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -1586,6 +1587,7 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
} }
out_unlock: out_unlock:
free_pi_state(pi_state);
double_unlock_hb(hb1, hb2); double_unlock_hb(hb1, hb2);
/* /*
...@@ -1602,8 +1604,6 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags, ...@@ -1602,8 +1604,6 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
out_put_key1: out_put_key1:
put_futex_key(&key1); put_futex_key(&key1);
out: out:
if (pi_state != NULL)
free_pi_state(pi_state);
return ret ? ret : task_count; return ret ? ret : task_count;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment