From mboxrd@z Thu Jan 1 00:00:00 1970 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: 8bit Content-Type: text/plain X-ClientProxiedBy: kwepems500001.china.huawei.com (7.221.188.70) To dggpemr500006.china.huawei.com (7.185.36.185) 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 = 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 futex_q *q, int locked) } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new)); #ifdef CONFIG_PREEMPT_RT - /* If the waiter interleaved with the requeue let it know */ - if (unlikely(old == 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 == Q_REQUEUE_PI_WAIT) && + new != Q_REQUEUE_PI_LOCKED) rcuwait_wake_up(&q->requeue_wait); #endif } -- 2.43.0