[PATCH 2/3] futex: avoid double wake up in futex_wake() on -RT

From: Sebastian Andrzej Siewior
Date: Tue Apr 07 2015 - 11:04:19 EST


futex_wake() wakes the waiter while holding the hb->lock. This leads to
a similar double wake up on -RT if the waiter has a higher priority than
the process perfroming the wake up.

This patch allocates space for one task and lets futex_wake() to wake up
the task once the hb->lock is dropped. If futex_wake() is used to wake
up more than once task then the behaviour remains unchanged. With one
waiter however we avoid the double wake up on -RT.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
---
kernel/futex.c | 33 ++++++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index b38abe3573a8..f3e633e16973 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -1088,16 +1088,12 @@ static void __unqueue_futex(struct futex_q *q)
hb_waiters_dec(hb);
}

-/*
- * The hash bucket lock must be held when this is called.
- * Afterwards, the futex_q must not be accessed.
- */
-static void wake_futex(struct futex_q *q)
+static struct task_struct *__wake_futex(struct futex_q *q)
{
struct task_struct *p = q->task;

if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
- return;
+ return NULL;

/*
* We set q->lock_ptr = NULL _before_ we wake up the task. If
@@ -1117,6 +1113,19 @@ static void wake_futex(struct futex_q *q)
*/
smp_wmb();
q->lock_ptr = NULL;
+ return p;
+}
+
+/*
+ * The hash bucket lock must be held when this is called.
+ * Afterwards, the futex_q must not be accessed.
+ */
+static void wake_futex(struct futex_q *q)
+{
+ struct task_struct *p = __wake_futex(q);
+
+ if (!p)
+ return;

wake_up_state(p, TASK_NORMAL);
put_task_struct(p);
@@ -1228,6 +1237,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
struct futex_hash_bucket *hb;
struct futex_q *this, *next;
union futex_key key = FUTEX_KEY_INIT;
+ struct task_struct *waiter = NULL;
int ret;

if (!bitset)
@@ -1256,14 +1266,23 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
if (!(this->bitset & bitset))
continue;

- wake_futex(this);
+ if (nr_wake == 1)
+ waiter = __wake_futex(this);
+ else
+ wake_futex(this);
if (++ret >= nr_wake)
break;
}
}

spin_unlock(&hb->lock);
+
out_put_key:
+ if (waiter) {
+ wake_up_state(waiter, TASK_NORMAL);
+ put_task_struct(waiter);
+ }
+
put_futex_key(&key);
out:
return ret;
--
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/