From nobody Wed Sep 30 13:55:26 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5734F3AE197 for ; Fri, 7 Aug 2026 08:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786092615; cv=none; b=GpDHVmvwu3rBtycwaKclEFxGfuDfNjN70oTmxq6Hj6qUDNrKg54KqzNuYIEsBzCqt+2GRD+p8vLfH5a5A1faP+OgVpEJX/DwIFAYNyTCiViotLMTZ4+3rqCVfgZThW1AiUuSAy33GM68zH0bpWsw9C/i1hS+74bbkhJM/wBlGrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786092615; c=relaxed/simple; bh=CTQZsGDI9TPZjkUIpf8+5qwmQrcb3M446ixCQasL3Os=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=kwxUX8MyFq1VzhzN3qJcC6Wwek7+Zk030lc4FaJYbwowpp94ZBEpx48jTAZjzZejG9hs49iMsmQsGQV4C6+Ut/sp4sANys9yS2OxJCAzaEWkfj9WP/DkbZW8FNSmLKIFEx9DYUtxOii2Hu1SczSdl93LaNgqu/ickxYTCx/wl14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ika3O+Xd; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ika3O+Xd" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2ceaf8a1265so46025705ad.2 for ; Fri, 07 Aug 2026 01:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786092614; x=1786697414; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=IFdBh9N9nroM19fp325fQEiej14i8CjED2CqbjDJhhw=; b=Ika3O+Xd4FLNbzziWlxZ++rQs2pvvzxDCsEdgHHWMdgrNBHWhOOPbNlnVE6ShL3bXJ MDq3HKp/sjYn7AWLTAp3P7KwU9ebLVS3eWesFWVn951LkXzDyxrDIyYeQAeVLvtslXmY rOsHQo3G5ZUgsY/Uurc6K+F8wXMkczq2b0dNLrPf4QmSHPAnuYPpoWTGKYKf3xsz2fup gVzodzaQcJZ2uC/VXKwA1Ew8dSyJR3w0C7MvwkpMMfqJJJM06DR88YxwO2fZyjen14xZ YalUUcBGLEHX8Y1ihTAUqaUutY0ikrc0cp3ZlcQ1IU7YQoop7IeYqdfW1Lfk4YdNqf94 Y//g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786092614; x=1786697414; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=IFdBh9N9nroM19fp325fQEiej14i8CjED2CqbjDJhhw=; b=OaTJxvm+h8QisTra+OmxBXT16dlAF3uYoVIx3/IgaoEiBeh+GxqHEw8DKHdBdKsECh ZvxQENkHnckCTxo4gQNX2xjIz2GE8RK2YiOOAe64TqfzWlUcokJy2CRcPZwOkxikr+Lt K9VTzk4/G8Y5HA0jmU6oXG3TOr03KAVLaZivKCJt4h4ho9eZ6Xw2A9DaE3Ev26ot6gka WOja/fILkYag+Qnkj3HmGGF3YhXCWe6eCDd/aWsC/v6xIZxsf+SsMf4zoNoVgRqN/OKA /gl++oJsUB/8uTxrqGEtqsRAMcbFDgKJ3g2aaVsONdLiuHDjM/6y+cCIgqbSL3OtMnG/ zwkw== X-Forwarded-Encrypted: i=1; AHgh+Ro19X77q4pCg13Ip8IgrwtdP+HSPC+uGixi7GaVVhfpILieV8LwjihvIaYCMmjx4wuEErxd7b6r90XszM4=@vger.kernel.org X-Gm-Message-State: AOJu0YymcP0An6W99lZU80Q5KpERRivicVgb5AzTfP/GCncqzqR+9OhL EDAaLxwENigNQuM2Wz2HdJ8GR0Iju8gw2VDsFwevgr1mTrb4b4+3u/r6 X-Gm-Gg: AR+sD13I2Fu7JXNZFOXXuPwJizYBtMTi8vNR1oj8TdenMoLxXI8ZDSRlU2/h588zVDa RL6fSMIgGE7g8ZMrCCCSPsQ9J42+aaidbLFDcceWWNtU5lrmf0e56XDcJxwfBkPp2LT42z+WYot QkUUPoiROKFf18UVpnqwskYm/YHiuemcU9DYAjmZ7YnvlGhNDOK7jHAByMw7929NkYlSCqLcb7C KlihcxFt9kz6tqUjqrPzWZjvrJbkkA05/VVYH1grmkAnHPlEr85jfb3l0Chi7rLzsrkQaI7m4AD WX096tuGfJQp4lygjfcC0OcpAEfMdemPGv11zWqyixVTBmw27S2KoDRV88LUh2F+xpRV0EUL2R5 OSW4ZYKmAo5SbyO9UTXe6qPsVlPX0bg3qCNt/9cI5AHwXuATkT4AFqT4D5ejRSYN2H8cuszmjvA 9FWqpJhe+BzFoU1mHOTbLEyHJYccW3dkaLwK7+iskYe+1RtUNXEj8xxuJvvpTqimLxacUfNx5/ X-Received: by 2002:a17:903:1a10:b0:2cc:f5b8:4c2e with SMTP id d9443c01a7336-2d0ca767b88mr280023755ad.9.1786092613585; Fri, 07 Aug 2026 01:50:13 -0700 (PDT) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d14ccadef6sm5692415ad.11.2026.08.07.01.50.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Aug 2026 01:50:12 -0700 (PDT) From: Zhan Xusheng X-Google-Original-From: Zhan Xusheng To: Thomas Gleixner , Ingo Molnar , Peter Zijlstra Cc: Darren Hart , Davidlohr Bueso , =?UTF-8?q?Andr=C3=A9=20Almeida?= , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhanxusheng@xiaomi.com Subject: [PATCH] selftests/futex: Add test for robust exit with a foreign futex owner Date: Fri, 7 Aug 2026 16:50:05 +0800 Message-ID: <20260807085005.1215188-1-zhanxusheng@xiaomi.com> X-Mailer: git-send-email 2.43.0 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 Content-Type: text/plain; charset="utf-8" commit 6d4514ca9cdf ("futex: Prevent robust futex exit race some more") fixed a lost wakeup in handle_futex_death(): when a robust waiter dies mid-lock (list_op_pending set) while the futex is owned by a third thread with FUTEX_WAITERS clear, the kernel must still wake the next waiter, otherwise a waiter parked behind that futex sleeps forever. Since the dying thread is not the owner, FUTEX_OWNER_DIED must not be set either. Add a regression test to robust_list.c reproducing this "state B" sequence: a waiter parks on a futex whose word is a foreign TID without FUTEX_WAITERS, and a robust thread then dies with that futex in its list_op_pending. It reuses the existing robust-list scaffolding. Without the fix the wakeup is lost and futex_wait() times out at FUTEX_TIMEOUT; with it handle_futex_death() issues the wakeup. Checked both ways: the test fails on a kernel without the fix and passes on one with it. Signed-off-by: Zhan Xusheng --- .../selftests/futex/functional/robust_list.c | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tools/testing/selftests/futex/functional/robust_list.c b/tools= /testing/selftests/futex/functional/robust_list.c index b3fab60181d5..be8149cc67f8 100644 --- a/tools/testing/selftests/futex/functional/robust_list.c +++ b/tools/testing/selftests/futex/functional/robust_list.c @@ -434,6 +434,85 @@ TEST(test_set_list_op_pending) ksft_test_result_fail("%s\n", __func__); } =20 +/* + * State B reproducer for commit 6d4514ca9cdf ("futex: Prevent robust futex + * exit race some more"). + * + * Model the sequence where the original owner unlocked and woke us, a thi= rd + * thread then took the futex uncontended (so the futex word holds a forei= gn + * TID with FUTEX_WAITERS clear), and we die mid-lock before acquiring it = or + * re-arming FUTEX_WAITERS. handle_futex_death() must still wake the next + * waiter, otherwise a waiter parked behind a foreign-owned futex whose + * FUTEX_WAITERS bit was lost sleeps forever. As the dying thread is not t= he + * owner, it must not set FUTEX_OWNER_DIED either. + */ +#define FOREIGN_TID 0x00c0ffee + +static int child_fn_foreign_owner(void *arg) +{ + struct lock_struct *lock =3D arg; + struct robust_list_head head; + + if (set_list(&head)) { + ksft_test_result_fail("set_robust_list error\n"); + return -1; + } + if ((unsigned int)gettid() =3D=3D FOREIGN_TID) { + ksft_test_result_fail("TID collided with FOREIGN_TID\n"); + return -1; + } + + /* We are mid-lock ... */ + head.list_op_pending =3D &lock->list; + /* ... and the futex is owned by a third thread, FUTEX_WAITERS clear. */ + atomic_store(&lock->futex, FOREIGN_TID); + + /* Let the parent enter futex_wait() before we die (see child_fn_lock). */ + pthread_barrier_wait(&barrier); + usleep(SLEEP_US); + + return 0; +} + +/* + * A waiter is parked on a futex whose word is a foreign TID with no + * FUTEX_WAITERS bit (state B). A robust thread dies with that futex in its + * list_op_pending. The kernel must wake the parked waiter even though the + * dying thread does not own the futex, and it must not set FUTEX_OWNER_DI= ED. + */ +TEST(test_robust_death_foreign_owner_wakeup) +{ + struct lock_struct lock =3D { .futex =3D 0 }; + struct timespec to =3D { .tv_sec =3D FUTEX_TIMEOUT, .tv_nsec =3D 0 }; + int ret, pid, wstatus; + + ret =3D pthread_barrier_init(&barrier, NULL, 2); + ASSERT_EQ(ret, 0); + + pid =3D create_child(&child_fn_foreign_owner, &lock); + ASSERT_NE(pid, -1); + + pthread_barrier_wait(&barrier); + + /* + * *futex =3D=3D FOREIGN_TID here, so this blocks. With the fix the dying + * child's robust walk wakes us; without it, futex_wait() times out. + */ + ret =3D futex_wait((futex_t *)&lock.futex, FOREIGN_TID, &to, 0); + ASSERT_EQ(ret, 0); + + /* The dying thread was not the owner: OWNER_DIED must not be set. */ + ASSERT_FALSE(lock.futex & FUTEX_OWNER_DIED); + + wait(&wstatus); + pthread_barrier_destroy(&barrier); + + if (!WEXITSTATUS(wstatus)) + ksft_test_result_pass("%s\n", __func__); + else + ksft_test_result_fail("%s\n", __func__); +} + #define CHILD_NR 10 =20 static int child_lock_holder(void *arg) --=20 2.43.0