From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755249AbbDGPEQ (ORCPT ); Tue, 7 Apr 2015 11:04:16 -0400 Received: from www.linutronix.de ([62.245.132.108]:38683 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755093AbbDGPEN (ORCPT ); Tue, 7 Apr 2015 11:04:13 -0400 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Darren Hart , Steven Rostedt , fredrik.markstrom@windriver.com, Davidlohr Bueso , Manfred Spraul , Sebastian Andrzej Siewior Subject: [PATCH 2/3] futex: avoid double wake up in futex_wake() on -RT Date: Tue, 7 Apr 2015 17:03:49 +0200 Message-Id: <1428419030-20030-3-git-send-email-bigeasy@linutronix.de> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1428419030-20030-1-git-send-email-bigeasy@linutronix.de> References: <1428419030-20030-1-git-send-email-bigeasy@linutronix.de> X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001,URIBL_BLOCKED=0.001 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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