From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A9A73B71A8 for ; Thu, 20 Aug 2026 07:49:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787212174; cv=none; b=V4yRq0T2jkTqRY+h1juUkX0GsjDTLiD+87k+B4Y7nOsw8fhZqU1JQbhHQ4VOoZUJDzJR/XhwxGNr+Mhkpc+vg6zM2KUOEK7VLK4GMm/5iHyXBv6upYFzr5mZP9y1dzOiIdeR4S9Mt6LijKiHtf3C0GxSjIqzD45yw2skcmUB7NE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787212174; c=relaxed/simple; bh=VmRPK1OW5CGGp7GfgcX2D08ZzwZ3lQfrpCkx6no2D8Q=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=MN1cLXit8aQY5bfEBbgAlhUB86Xivcb7X7eH5NRu6lkWR4os3R9pngeajgEYcJ/PjSMu2O+bFPf0t2Dbmdmzq2t78aWPu4azLZ8c3Dzn+GbI86up92vaBj4W8IvIOP8FjXMs0NbbN4YGFaflx15tS3zHeUvIyhOfu5Umpdq7mnY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=JSLQVZiC; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="JSLQVZiC" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=NqPKXOdJNJNTAT3bjiynnAA/qT0zYV6ulFU8NZ9/Mtk=; b=JSLQVZiCmkp1QOiJtjrTKIQaZy Pk1ardlUEay+zxL9xPk+uGMXunSZGSFffCZky5drF2afEz4QapvQCgw3BZwKxFZAJXE9CiXFulrYF 050Hl/mhfXr41ugVvPzf6xeEDZpktFvDwZXK5E5Ux+zfbgYd/1Ojneo/dEnRtw9m7EiwR1B5fV6qo s4/DGhswiIj1qQ6AXUm6c5OvV9dLqjPFIs7WmAAnC+HCvjfnBhAlWUm2D6hwATKbP/QZDsXBknTJD fYOsybpdF3w/yBK3atc9bAj+pFPEk6Py9ipzeX0FqU2yEB1R0q0nNv+GsgOzZm6Qz/U96mx1YUkqt D77PO4vQ==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wwxWi-0000000F7sN-1Xld; Thu, 20 Aug 2026 07:49:28 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id E12EF3006FA; Thu, 20 Aug 2026 09:49:27 +0200 (CEST) Date: Thu, 20 Aug 2026 09:49:27 +0200 From: Peter Zijlstra To: Thomas Gleixner Cc: linux-kernel@vger.kernel.org, Yao Kai , mingo@kernel.org, andrealmeid@igalia.com, dave@stgolabs.net, dvhart@infradead.org Subject: [PATCH] futex: Fix might_sleep() warning in futex_pivot_pending() Message-ID: <20260820074927.GH1246887@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline A younger me put a WARN in might_sleep() to warn about nested sleep loops. This younger me also build a wait-loop variant that can deal with it. This wait-loop variant doesn't have all the fancy wrappers, since it isn't used much. It also lacks wait-bit support. Add the wait-bit support and use it to fix the nested wait issue. Fixes: 8e7ff730dd96 ("futex: Fix race in futex_pivot_pending() during private hash resize") Reported-by: syzbot+350a93852ac854927f45@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=350a93852ac854927f45 Signed-off-by: Peter Zijlstra (Intel) --- include/linux/wait.h | 1 + include/linux/wait_bit.h | 1 + kernel/futex/core.c | 27 ++++++++++++++++++++++++++- kernel/sched/wait.c | 15 +++++++++++++++ kernel/sched/wait_bit.c | 15 ++++++++++++--- 5 files changed, 55 insertions(+), 4 deletions(-) --- a/include/linux/wait.h +++ b/include/linux/wait.h @@ -1228,6 +1228,7 @@ long prepare_to_wait_event(struct wait_q void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry); long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout); int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); +int woken_wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); #define DEFINE_WAIT_FUNC(name, function) \ --- a/include/linux/wait_bit.h +++ b/include/linux/wait_bit.h @@ -32,6 +32,7 @@ int out_of_line_wait_on_bit_timeout(unsi int out_of_line_wait_on_bit_lock(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode); struct wait_queue_head *bit_waitqueue(unsigned long *word, int bit); extern void __init wait_bit_init(void); +extern struct wait_bit_key *__var_wake_key(struct wait_queue_entry *wq_entry, void *arg); int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -46,6 +46,7 @@ #include #include #include +#include #include @@ -1886,11 +1887,35 @@ static int futex_hash_allocate(unsigned futex_hash_bucket_init(&fph->queues[i]); if (custom) { + struct wait_bit_queue_entry __wbq_entry; + struct wait_queue_head *__wq_head; + /* * Only let prctl() wait / retry; don't unduly delay clone(). */ again: - wait_var_event(mm, futex_pivot_pending(mm)); + __wq_head = __var_waitqueue(mm); + init_wait_var_entry(&__wbq_entry, mm, 0); + __wbq_entry.wq_entry.func = woken_wake_bit_function; + add_wait_queue(__wq_head, &__wbq_entry.wq_entry); + + /* + * add_wait_queue() futex_ref_put() + * MB (this) MB (implied) + * futex_pivot_pending() wake_up_var() + * waitqueue_active() + * + * Notably, it must not be possible to see + * !futex_pivot_pending() && !waitqueue_active(). + */ + smp_mb(); + + while (!futex_pivot_pending(mm) && + wait_woken(&__wbq_entry.wq_entry, TASK_UNINTERRUPTIBLE, + MAX_SCHEDULE_TIMEOUT)) + /* empty */; + + remove_wait_queue(__wq_head, &__wbq_entry.wq_entry); } scoped_guard(mutex, &mm->futex.phash.lock) { --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -5,6 +5,7 @@ * (C) 2004 Nadia Yvette Chambers, Oracle */ #include "sched.h" +#include void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *key) { @@ -463,3 +464,17 @@ int woken_wake_function(struct wait_queu return default_wake_function(wq_entry, mode, sync, key); } EXPORT_SYMBOL(woken_wake_function); + +int woken_wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg) +{ + struct wait_bit_key *key = __var_wake_key(wq_entry, arg); + if (!key) + return 0; + + /* Pairs with the smp_store_mb() in wait_woken(). */ + smp_mb(); /* C */ + wq_entry->flags |= WQ_FLAG_WOKEN; + + return default_wake_function(wq_entry, mode, sync, key); +} +EXPORT_SYMBOL(woken_wake_bit_function); --- a/kernel/sched/wait_bit.c +++ b/kernel/sched/wait_bit.c @@ -167,9 +167,7 @@ wait_queue_head_t *__var_waitqueue(void } EXPORT_SYMBOL(__var_waitqueue); -static int -var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode, - int sync, void *arg) +struct wait_bit_key *__var_wake_key(struct wait_queue_entry *wq_entry, void *arg) { struct wait_bit_key *key = arg; struct wait_bit_queue_entry *wbq_entry = @@ -177,6 +175,17 @@ var_wake_function(struct wait_queue_entr if (wbq_entry->key.flags != key->flags || wbq_entry->key.bit_nr != key->bit_nr) + return NULL; + + return key; +} + +static int +var_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode, + int sync, void *arg) +{ + struct wait_bit_key *key = __var_wake_key(wq_entry, arg); + if (!key) return 0; return autoremove_wake_function(wq_entry, mode, sync, key);