From nobody Sun Feb 8 11:26:06 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 195BA2874F5; Thu, 29 Jan 2026 14:38:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697486; cv=none; b=b6fLxhS3pLK2cWNjOAiNXXgKZ7NSJnv/Wve5joKFTSDUL29ApYXRNxysW4nm/OTqprw0o4dNkugngaXt6W1mWYewHm58dwBn7DLljAvQRWbYtONKWnAVQCXC5ufIvqBar2S65aZ5xY0xXITx5hwNLb7t1NrhF5fTdS8gJIesmOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769697486; c=relaxed/simple; bh=SUGfZS56d2WKVPFhyl+8hQaQtIjs8UXUWjfjEByZb14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=irTtTLPZixme2VAk3ZRDe1CHp5ypn5APIhBDwPzlYZx/GPK8GdT8wYNCqbLFrRDV9NkKDW1X/IcrqcUGSpsWsIsabItijyTD8Qd3wS94Vm/U3WekYSkLCNGTvqhbn+MILIDe3L86I2w0kDGe/u+M6ID0uJrcJzxfyV1DwTAXT2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lT6fub4z; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lT6fub4z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E67B8C19422; Thu, 29 Jan 2026 14:38:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769697486; bh=SUGfZS56d2WKVPFhyl+8hQaQtIjs8UXUWjfjEByZb14=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lT6fub4zYlgrI2eNTkAobf+9E0IMW11C5m7km178JRpAG1K1ZKxajTGNQBpcXA1qE G439qoOy5wd1BMeTklPjcsXYDRN6F8JCEgqc7rh+87UyFb5BqSOJYHO8JJor3kYCft zuk54l3izlFAfDeUKoqbK6B5Ez8ZzdSH+k+yvddVCxwccYTndRdKbDy2Vyfgn+eJhE vzyefc8V1ErDmrxo9ftEYAhpG5gGHc3IPfkSLMDvbG1DoKMf1ttrxJVBywsqiaNCNs uKf2fhuvfaJTlHkkqiZi+YXSqlvnjDZ95HzFQI/6QKJkCSMTJmaZQfsFyYhSXUCTMh mX1MtnC/8dnAg== From: Tzung-Bi Shih To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich Cc: Jonathan Corbet , Shuah Khan , Laurent Pinchart , Bartosz Golaszewski , Wolfram Sang , Jason Gunthorpe , Johan Hovold , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, chrome-platform@lists.linux.dev, tzungbi@kernel.org Subject: [PATCH 2/4] revocable: Add KUnit test for provider lifetime races Date: Thu, 29 Jan 2026 14:37:31 +0000 Message-ID: <20260129143733.45618-3-tzungbi@kernel.org> X-Mailer: git-send-email 2.53.0.rc1.217.geba53bf80e-goog In-Reply-To: <20260129143733.45618-1-tzungbi@kernel.org> References: <20260129143733.45618-1-tzungbi@kernel.org> 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" Add a test to verify that revocable_alloc() correctly handles race conditions where the provider is being released. The test covers three scenarios: 1. Allocating from a NULL provider. 2. Allocating from a provider that has been detached (pointer is NULL). 3. Allocating from a provider that is in the process of destruction (refcount is 0), simulating a race between revocable_alloc() and revocable_provider_release(). A way to run the test: $ ./tools/testing/kunit/kunit.py run \ --kconfig_add CONFIG_REVOCABLE_KUNIT_TEST=3Dy \ --kconfig_add CONFIG_PROVE_LOCKING=3Dy \ --kconfig_add CONFIG_DEBUG_KERNEL=3Dy \ --kconfig_add CONFIG_DEBUG_INFO=3Dy \ --kconfig_add CONFIG_DEBUG_INFO_DWARF5=3Dy \ --kconfig_add CONFIG_KASAN=3Dy \ --kconfig_add CONFIG_DETECT_HUNG_TASK=3Dy \ --kconfig_add CONFIG_DEFAULT_HUNG_TASK_TIMEOUT=3D"10" \ --arch=3Dx86_64 --raw_output=3Dall \ revocable_test Signed-off-by: Tzung-Bi Shih --- drivers/base/revocable_test.c | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/drivers/base/revocable_test.c b/drivers/base/revocable_test.c index 1622aae92fd3..7fc4d6a3dff6 100644 --- a/drivers/base/revocable_test.c +++ b/drivers/base/revocable_test.c @@ -14,9 +14,13 @@ * * - Try Access Macro: Same as "Revocation" but uses the * REVOCABLE_TRY_ACCESS_WITH() and REVOCABLE_TRY_ACCESS_SCOPED(). + * + * - Provider Use-after-free: Verifies revocable_alloc() correctly handles + * race conditions where the provider is being released. */ =20 #include +#include #include =20 static void revocable_test_basic(struct kunit *test) @@ -127,11 +131,48 @@ static void revocable_test_try_access_macro2(struct k= unit *test) revocable_free(rev); } =20 +static void revocable_test_provider_use_after_free(struct kunit *test) +{ + struct revocable_provider __rcu *rp; + struct revocable_provider *old_rp; + void *real_res =3D (void *)0x12345678; + struct revocable *rev; + + rp =3D revocable_provider_alloc(real_res); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rp); + + rev =3D revocable_alloc(NULL); + KUNIT_EXPECT_PTR_EQ(test, rev, NULL); + + /* Simulate the provider has been freed. */ + old_rp =3D rcu_replace_pointer(rp, NULL, 1); + rev =3D revocable_alloc(rp); + KUNIT_EXPECT_PTR_EQ(test, rev, NULL); + rcu_replace_pointer(rp, old_rp, 1); + + struct { + struct srcu_struct srcu; + void __rcu *res; + struct kref kref; + struct rcu_head rcu; + } *rp_internal =3D (void *)rp; + + /* Simulate the provider is releasing. */ + refcount_set(&rp_internal->kref.refcount, 0); + rev =3D revocable_alloc(rp); + KUNIT_EXPECT_PTR_EQ(test, rev, NULL); + refcount_set(&rp_internal->kref.refcount, 1); + + revocable_provider_revoke(&rp); + KUNIT_EXPECT_PTR_EQ(test, unrcu_pointer(rp), NULL); +} + static struct kunit_case revocable_test_cases[] =3D { KUNIT_CASE(revocable_test_basic), KUNIT_CASE(revocable_test_revocation), KUNIT_CASE(revocable_test_try_access_macro), KUNIT_CASE(revocable_test_try_access_macro2), + KUNIT_CASE(revocable_test_provider_use_after_free), {} }; =20 --=20 2.53.0.rc1.217.geba53bf80e-goog