From nobody Fri Jul 24 23:30:48 2026 Received: from canpmsgout08.his.huawei.com (canpmsgout08.his.huawei.com [113.46.200.223]) (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 1F8CD3E556B for ; Wed, 22 Jul 2026 08:31:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784709096; cv=none; b=pvuuKc3V06a1x75Ypgn+Ha+wplBkJnJw7zlx3/a7LE1CucKfYJStuYo7jgYXVNft+Bke+kg0AloGZgD8HUGxgi2w2ktTVkR5cmtD0HVP5qV96pk3r+f5CdmLHJOfm3OENZmwBVYKfFQL1bimrcXySiPHuacRoDqX3rlHvwttiGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784709096; c=relaxed/simple; bh=foT2FNu7ZkA+F9PgGOqoR4/4s4Nr8C2fBYFrms4DKAU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QyCGbrfYM363OoXxFC4Smd0uNDLuoNeTf1XikUwZ9nlEOVhwUMf3F14KM8llKukyEcGncoUs7ETkz5e1zrKiKXZLUPLxalU0xG/ndhbwD5z2v8Z5mgyxMFsQMC3sjFAfLP0CBcv/qmO9MsBrhrlBIGrJWYGP/o1b1Zp9o/2UDZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=KSgY9vnS; arc=none smtp.client-ip=113.46.200.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="KSgY9vnS" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=SgS/V+WXikCppzBSnMYHrNuEXt834COmPlQW8/AER7Q=; b=KSgY9vnSptGCThq463Cd3NLPUuqXTqGYIUzAUAOKHm7q+HCbLURfxLXKqkKI4pz5ZK9ugJdwt oavxqWdhZMI9Bxob0NNWUxwJvVaCgBeSPiPPiqpiguNlHWfWUuOo7nCxNyxb2iQ3hKyr+wueray SCZ049pJKjKNFBdqoD1g1Ig= Received: from mail.maildlp.com (unknown [172.19.163.200]) by canpmsgout08.his.huawei.com (SkyGuard) with ESMTPS id 4h4nKk3sGWzmV99; Wed, 22 Jul 2026 16:22:02 +0800 (CST) Received: from dggpemr500006.china.huawei.com (unknown [7.185.36.185]) by mail.maildlp.com (Postfix) with ESMTPS id 61A4140563; Wed, 22 Jul 2026 16:31:25 +0800 (CST) Received: from localhost.localdomain (10.50.85.180) by dggpemr500006.china.huawei.com (7.185.36.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 22 Jul 2026 16:31:24 +0800 From: Yao Kai To: CC: , , , , , , , Subject: [PATCH v2 1/2] futex/requeue: Fix rtmutex schedule preparation for requeue PI Date: Wed, 22 Jul 2026 16:51:39 +0800 Message-ID: <20260722085140.1949077-2-yaokai34@huawei.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260722085140.1949077-1-yaokai34@huawei.com> References: <20260722085140.1949077-1-yaokai34@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500001.china.huawei.com (7.221.188.70) To dggpemr500006.china.huawei.com (7.185.36.185) Content-Type: text/plain; charset="utf-8" A waiter requeued onto a PI futex can reach rt_mutex_wait_proxy_lock() without rtmutex schedule preparation: WARNING: CPU: 0 PID: 293 at kernel/sched/core.c:7606 RIP: rt_mutex_schedule+0x43/0x50 Call Trace: rt_mutex_slowlock_block.constprop.0+0x5b/0x320 rt_mutex_wait_proxy_lock+0x3e/0x80 futex_wait_requeue_pi+0x3ba/0x590 do_futex+0x171/0x1f0 rt_mutex_schedule() requires current->sched_rt_mutex to be set. Normally, rt_mutex_pre_schedule() sets it before an rtmutex waiter can schedule. With requeue PI, another task can enqueue the waiter after its futex_q becomes visible: waiter requeue task ------ ------------ futex_wait_requeue_pi() futex_wait_setup() futex_queue(&q) futex_requeue() rt_mutex_start_proxy_lock() enqueue rt_waiter install pi_blocked_on requeue_futex() plist_del(&q->list) futex_do_wait() plist_node_empty(&q->list) skip schedule() plist_add(&q->list) futex_requeue_pi_complete() IN_PROGRESS -> DONE futex_requeue_pi_wakeup_sync() // DONE rt_mutex_wait_proxy_lock() rt_mutex_schedule() futex_do_wait() mistakes the temporary removal for a wakeup and skips schedule(). The proxy waiter can nevertheless remain blocked on the target rtmutex and subsequently enter rt_mutex_schedule() with current->sched_rt_mutex clear. Call rt_mutex_pre_schedule() and rt_mutex_post_schedule() directly around rt_mutex_wait_proxy_lock() so this second blocking point has the required scheduler preparation. Fixes: d14f9e930b90 ("locking/rtmutex: Use rt_mutex specific scheduler help= ers") Suggested-by: Sebastian Andrzej Siewior Cc: stable@vger.kernel.org Signed-off-by: Yao Kai --- kernel/futex/requeue.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/futex/requeue.c b/kernel/futex/requeue.c index 79823ad13683..f7889fb2fce4 100644 --- a/kernel/futex/requeue.c +++ b/kernel/futex/requeue.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later =20 #include +#include #include =20 #include "futex.h" @@ -865,7 +866,14 @@ int futex_wait_requeue_pi(u32 __user *uaddr, unsigned = int flags, case Q_REQUEUE_PI_DONE: /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */ pi_mutex =3D &q.pi_state->pi_mutex; + /* + * Requeue temporarily removes q from the hash bucket, so + * futex_do_wait() may skip schedule() even though the proxy + * waiter still has to block on the rtmutex. + */ + rt_mutex_pre_schedule(); ret =3D rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter); + rt_mutex_post_schedule(); =20 /* * See futex_unlock_pi()'s cleanup: comment. --=20 2.43.0 From nobody Fri Jul 24 23:30:48 2026 Received: from canpmsgout06.his.huawei.com (canpmsgout06.his.huawei.com [113.46.200.221]) (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 BA10042E8E1 for ; Wed, 22 Jul 2026 08:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.221 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784709093; cv=none; b=bht3L5OwTzEsR+87kK1CADHpE91RiANOuRpYFf43vt+vsRGBNI6RqEQ3+xZHB2vqyqm0VRPkPXeBDRfuc0VVrnFdXWMif7Qbj3uMmxoF1Jj4q/B+IvLsjJT+98n1rD37nEpDH90tot8UkBnGFFh7GCU5wf+d4KuG7KbKxDcW+vM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784709093; c=relaxed/simple; bh=sqxJK90AN51JpZRRjJgCqvedLeE/0azwakvlnxjKfpc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ULaVUMis/K2aqP3Zg00ABKllMnRJ84jRoVe8DzYj+aX77jCHTx7cVBISmG3g4bwcjdAIngzMHI5L53fhSQzkk6j0mkqBNEOM1pV2zE/w9FNDjETYPNiYsCBFVEHMUwWIdzO/fFUWhb6C6IOm8hp90jyNAZ6e6YHI9TuMbx6SeHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=g7CkHUXB; arc=none smtp.client-ip=113.46.200.221 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="g7CkHUXB" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=q0D5PTgn7lPAbukOK6gCoSXhDxdqVt4yL8nvAA071wQ=; b=g7CkHUXBkfdRHojebwkSIlZzaIhepNFKrkc5Lu207caHpmifQdmmGcLTaap77df9KtxFIcgCX Ykdukot9QfU8Rtf9QQpYZTjfpTT5oSZiW/5EC8Yl9T5xISfkR4gwsMhJoRtRHZIwd+JA7xxdaNb B1ShFs1qGNYc6Ee2KAmgQoA= Received: from mail.maildlp.com (unknown [172.19.163.0]) by canpmsgout06.his.huawei.com (SkyGuard) with ESMTPS id 4h4nKm3pN0zRhQm; Wed, 22 Jul 2026 16:22:04 +0800 (CST) Received: from dggpemr500006.china.huawei.com (unknown [7.185.36.185]) by mail.maildlp.com (Postfix) with ESMTPS id F28EC40537; Wed, 22 Jul 2026 16:31:25 +0800 (CST) Received: from localhost.localdomain (10.50.85.180) by dggpemr500006.china.huawei.com (7.185.36.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 22 Jul 2026 16:31:25 +0800 From: Yao Kai To: CC: , , , , , , , Subject: [PATCH v2 2/2] futex/requeue: Prevent rcuwait use-after-free during requeue PI Date: Wed, 22 Jul 2026 16:51:40 +0800 Message-ID: <20260722085140.1949077-3-yaokai34@huawei.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260722085140.1949077-1-yaokai34@huawei.com> References: <20260722085140.1949077-1-yaokai34@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500001.china.huawei.com (7.221.188.70) To dggpemr500006.china.huawei.com (7.185.36.185) Content-Type: text/plain; charset="utf-8" On PREEMPT_RT, FUTEX_CMP_REQUEUE_PI can trigger a KASAN report: BUG: KASAN: slab-out-of-bounds in _raw_spin_lock_irqsave+0x76/0xe0 Call Trace: _raw_spin_lock_irqsave+0x76/0xe0 try_to_wake_up+0xab/0x1540 rcuwait_wake_up+0x39/0x60 futex_requeue+0x18c3/0x1e10 The futex_q used by futex_wait_requeue_pi() is allocated on the waiter's stack. An early wakeup can race with a PI requeue as follows: waiter requeue task ------ ------------ futex_wait_requeue_pi() futex_do_wait() schedule() * timeout/signal wakes waiter * futex_requeue_pi_wakeup_sync() IN_PROGRESS -> WAIT rcuwait_wait_event() requeue_pi_wake_futex() task =3D READ_ONCE(q->task) futex_requeue_pi_complete() WAIT -> LOCKED return LOCKED return // q lifetime ends rcuwait_wake_up() futex_requeue_pi_complete() publishes LOCKED before calling rcuwait_wake_up(). Once the waiter observes LOCKED, it can return from futex_wait_requeue_pi() and let q go out of scope before rcuwait_wake_up() reads q->requeue_wait.task and passes the stale pointer to try_to_wake_up(). Skip rcuwait_wake_up() for Q_REQUEUE_PI_LOCKED. This state is only published by requeue_pi_wake_futex(), which saves q->task before futex_requeue_pi_complete() and calls wake_up_state(task, TASK_NORMAL) afterwards. If the waiter is already blocked in rcuwait_wait_event(), TASK_NORMAL includes TASK_UNINTERRUPTIBLE and wakes it. If the wakeup runs before the waiter blocks, the waiter observes LOCKED and does not schedule. The other completion states retain their rcuwait wakeup. Fixes: 07d91ef510fb1 ("futex: Prevent requeue_pi() lock nesting issue on RT= ") Cc: stable@vger.kernel.org Signed-off-by: Yao Kai --- kernel/futex/requeue.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kernel/futex/requeue.c b/kernel/futex/requeue.c index f7889fb2fce4..9b32f19f320a 100644 --- a/kernel/futex/requeue.c +++ b/kernel/futex/requeue.c @@ -155,8 +155,16 @@ static inline void futex_requeue_pi_complete(struct fu= tex_q *q, int locked) } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new)); =20 #ifdef CONFIG_PREEMPT_RT - /* If the waiter interleaved with the requeue let it know */ - if (unlikely(old =3D=3D Q_REQUEUE_PI_WAIT)) + /* + * If the waiter interleaved with the requeue, let it know. For LOCKED, + * q may be invalid as soon as the state is published. Only + * requeue_pi_wake_futex() publishes LOCKED; it saves q->task before + * futex_requeue_pi_complete() and follows it with + * wake_up_state(TASK_NORMAL), which also wakes the TASK_UNINTERRUPTIBLE + * rcuwait waiter. + */ + if (unlikely(old =3D=3D Q_REQUEUE_PI_WAIT) && + new !=3D Q_REQUEUE_PI_LOCKED) rcuwait_wake_up(&q->requeue_wait); #endif } --=20 2.43.0