Skip to content

Commit 5b2c5a9

Browse files
KAGA-KOKOgregkh
authored andcommitted
futex: Provide and use pi_state_update_owner()
commit c5cade2 upstream Updating pi_state::owner is done at several places with the same code. Provide a function for it and use that at the obvious places. This is also a preparation for a bug fix to avoid yet another copy of the same code or alternatively introducing a completely unpenetratable mess of gotos. Originally-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5ede8ee commit 5b2c5a9

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

‎kernel/futex.c‎

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,29 @@ static struct futex_pi_state *alloc_pi_state(void)
765765
return pi_state;
766766
}
767767

768+
static void pi_state_update_owner(struct futex_pi_state *pi_state,
769+
struct task_struct *new_owner)
770+
{
771+
struct task_struct *old_owner = pi_state->owner;
772+
773+
lockdep_assert_held(&pi_state->pi_mutex.wait_lock);
774+
775+
if (old_owner) {
776+
raw_spin_lock(&old_owner->pi_lock);
777+
WARN_ON(list_empty(&pi_state->list));
778+
list_del_init(&pi_state->list);
779+
raw_spin_unlock(&old_owner->pi_lock);
780+
}
781+
782+
if (new_owner) {
783+
raw_spin_lock(&new_owner->pi_lock);
784+
WARN_ON(!list_empty(&pi_state->list));
785+
list_add(&pi_state->list, &new_owner->pi_state_list);
786+
pi_state->owner = new_owner;
787+
raw_spin_unlock(&new_owner->pi_lock);
788+
}
789+
}
790+
768791
static void get_pi_state(struct futex_pi_state *pi_state)
769792
{
770793
WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount));
@@ -1523,26 +1546,15 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_
15231546
ret = -EINVAL;
15241547
}
15251548

1526-
if (ret)
1527-
goto out_unlock;
1528-
1529-
/*
1530-
* This is a point of no return; once we modify the uval there is no
1531-
* going back and subsequent operations must not fail.
1532-
*/
1533-
1534-
raw_spin_lock(&pi_state->owner->pi_lock);
1535-
WARN_ON(list_empty(&pi_state->list));
1536-
list_del_init(&pi_state->list);
1537-
raw_spin_unlock(&pi_state->owner->pi_lock);
1538-
1539-
raw_spin_lock(&new_owner->pi_lock);
1540-
WARN_ON(!list_empty(&pi_state->list));
1541-
list_add(&pi_state->list, &new_owner->pi_state_list);
1542-
pi_state->owner = new_owner;
1543-
raw_spin_unlock(&new_owner->pi_lock);
1544-
1545-
postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1549+
if (!ret) {
1550+
/*
1551+
* This is a point of no return; once we modified the uval
1552+
* there is no going back and subsequent operations must
1553+
* not fail.
1554+
*/
1555+
pi_state_update_owner(pi_state, new_owner);
1556+
postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
1557+
}
15461558

15471559
out_unlock:
15481560
raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
@@ -2435,19 +2447,7 @@ static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
24352447
* We fixed up user space. Now we need to fix the pi_state
24362448
* itself.
24372449
*/
2438-
if (pi_state->owner != NULL) {
2439-
raw_spin_lock(&pi_state->owner->pi_lock);
2440-
WARN_ON(list_empty(&pi_state->list));
2441-
list_del_init(&pi_state->list);
2442-
raw_spin_unlock(&pi_state->owner->pi_lock);
2443-
}
2444-
2445-
pi_state->owner = newowner;
2446-
2447-
raw_spin_lock(&newowner->pi_lock);
2448-
WARN_ON(!list_empty(&pi_state->list));
2449-
list_add(&pi_state->list, &newowner->pi_state_list);
2450-
raw_spin_unlock(&newowner->pi_lock);
2450+
pi_state_update_owner(pi_state, newowner);
24512451
raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
24522452

24532453
return argowner == current;

0 commit comments

Comments
 (0)