From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from canpmsgout05.his.huawei.com (canpmsgout05.his.huawei.com [113.46.200.220]) (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 928933C6A41 for ; Fri, 17 Jul 2026 08:29:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.220 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784276986; cv=none; b=FNat9Qd/X+pyoP/TSr7WE18VoFLMVO28E2HRCn5jRdfR6PAeORL9Dxy2PRki1GoSlx72qO1rZ357jNapjCE+fKF+ChrxAVS9if7fO3YAG+ZpshPfPhyW74QsQZySnFDLD0nhabH2mQ1nfN8CaDlrGk7PZM3qgjWiJ+8e5ME6CiI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784276986; c=relaxed/simple; bh=zhGo+tcMcgAKnL0/vuCzbIzFtCQwBTE30hYNhFKOsIE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O6dcg8QO//AJniI3CEmc5FHnei0+d0Pnp1Tm2EQAyxkNCyEVyzIOIH755Td7Bl5aGum61IYp9ly1qCErhOC36BGxVzCarGex8LmpUEyF6b9/KmdEU9m6+vb5I23JFiie9RYkgEDCM06hjwQCU91a9wTQ+Fbecz8OXoDfQ45nAIQ= 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=NNSJk5wg; arc=none smtp.client-ip=113.46.200.220 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="NNSJk5wg" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=VE9CHMy89zGMCvA1XyDPyhnECyvEqJ1pSSqrrMa9pqc=; b=NNSJk5wgl4YQeX1jzDjj55SmFfVZHZRXwVRByI0CWbLf3wkv00RxtBppEULAIJqdAB1pxc9A0 Pxr+YCpwL4z0TUN5IveO2Z/hHPbV//V7/LPcrPqmfeMZQCscVvGx0ZVHc2nz0SK4POZM2dQOMpd rtW+LTkd8v1NsC+oZibJ+pQ= Received: from mail.maildlp.com (unknown [172.19.163.104]) by canpmsgout05.his.huawei.com (SkyGuard) with ESMTPS id 4h1jWc09nWz12LHf; Fri, 17 Jul 2026 16:19:56 +0800 (CST) Received: from dggpemr500006.china.huawei.com (unknown [7.185.36.185]) by mail.maildlp.com (Postfix) with ESMTPS id B67134057F; Fri, 17 Jul 2026 16:29:32 +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; Fri, 17 Jul 2026 16:29:32 +0800 From: Yao Kai To: CC: , , , , , , , Subject: [PATCH 2/2] futex/requeue: Prevent rcuwait use-after-free during requeue PI Date: Fri, 17 Jul 2026 16:49:22 +0800 Message-ID: <20260717084922.4153317-3-yaokai34@huawei.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260717084922.4153317-1-yaokai34@huawei.com> References: <20260717084922.4153317-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: kwepems500002.china.huawei.com (7.221.188.17) 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. requeue_pi_wake_futex() already saves q->task before publishing LOCKED and wakes the saved task afterward. 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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/futex/requeue.c b/kernel/futex/requeue.c index abc652b5b2dd..59e587775d9b 100644 --- a/kernel/futex/requeue.c +++ b/kernel/futex/requeue.c @@ -155,8 +155,13 @@ 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 already be invalid, so requeue_pi_wake_futex() wakes the saved + * task instead. + */ + if (unlikely(old == Q_REQUEUE_PI_WAIT) && + new != Q_REQUEUE_PI_LOCKED) rcuwait_wake_up(&q->requeue_wait); #endif } -- 2.43.0