From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (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 01C221D54FA; Sat, 11 Jul 2026 04:41:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744894; cv=none; b=XQsneMOV7k1HJ9DI3BZjAzq3kneeGb5+yL4Wag22+HsFCxfkulpdDKcgWqFcT54yGkXHP3LTrwDkcMFxdjhotWcBQhnG+88HFEUy1hNmsb8TbVvL/mmOl1gGdbmo6jVU/Utvo1O0/e2aJWy1ZCrSnTN2bdYxwU9pEEFPVWyiNZU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744894; c=relaxed/simple; bh=Ley3/Ev0Q2dr2jIjm9hqbGpVu2QNbexoD9klE+g+ihI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XYIlC+SCOfaXbU2snUCBRUkHS2SKPY4lfKm8dtljRQrxaRTWn7swCdDPZAmSgSSBeaxIJNKg9iiGX14U6kVqTCloMlKx8frg4EYhaTLMzUCL+aHI+V7gEMZUiqlrmq3zvoW5UarsCFoMOHUUSScDfemBn8oaj6apQastSVUbTCU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=VX6klacL; arc=none smtp.client-ip=80.241.56.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="VX6klacL" Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA512) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4gxwyJ1tdCzMlFS; Sat, 11 Jul 2026 06:41:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744888; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gu1/GeFwQRCYl7cwa5Py8T4WqePQUyQJElSNNKRQt2U=; b=VX6klacLvGbraNVI0tsWnJLZ4Z7xEg0Tn9pY1gdHncVs4jXsgW9wvOZFd/RZvnSPLF2AIp LQ/dLfoa4gyQa/96mQvY+Y+qogB3zBhKlJPOAbwRZrh2mehLMDUFpUimdprjJXK4+m1rWs mdhTYOKZQ2QKw45NErA97e2UnWA0DTsK3GbZ96V0hjJ2EU6K+Ww+JAcCejJFhJzYF9+NRb G0HtLdTwxmnqSol6GpF+8nImSCDwJhbalio5DZzXsCpgvBUiFRxZqEiDd53cojfm564ybg Hjs4jB/v610TbOsLdFpyUwmR/8J55l0eJlfCxH77pQlesIUEvWVEa3bhOI0Y5A== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of m@mavick.dev designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=m@mavick.dev From: Eugene Mavick Date: Sat, 11 Jul 2026 12:40:22 +0800 Subject: [PATCH v3 4/5] kunit: add test for ref_trace_final_put Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260711-refcount-final-put-trace-v3-4-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Developer-Signature: v=1; a=openpgp-sha256; l=5255; i=m@mavick.dev; h=from:subject:message-id; bh=Ley3/Ev0Q2dr2jIjm9hqbGpVu2QNbexoD9klE+g+ihI=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFmBJ23LzK0+r1K90OyeKclYb566l3tPX09W++Rwll225 woFDid1lLIwiHExyIopstRkJbhnrJyVaHx0Ug/MHFYmkCEMXJwCMJGwXEaGDfEX3BawGGj8eBP4 lMG29MP9ythbz1blvNjKwrKYtcOFkZFh4pInC6ceuWIdc2R5uEex6eXVhw4aPbsleEarw13oj+x JZgA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4gxwyJ1tdCzMlFS Add a KUnit test suite for the ref_trace_final_put tracepoint. The test registers a probe function and triggers both refcount_t and percpu_ref final put paths, verifying that the tracepoint fires correctly and that the recorded fields match expected values. Signed-off-by: Eugene Mavick --- lib/Kconfig | 10 ++++ lib/tests/Makefile | 1 + lib/tests/ref_trace_kunit.c | 141 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/lib/Kconfig b/lib/Kconfig index 00a9509636c1..7e2746be6d16 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -52,6 +52,16 @@ config PACKING_KUNIT_TEST When in doubt, say N. +config REF_TRACE_KUNIT_TEST + bool "ref_trace kunit test" if !KUNIT_ALL_TESTS + depends on KUNIT && TRACEPOINTS + default KUNIT_ALL_TESTS + help + This option enables the KUnit test suite for the ref_trace_final_put + tracepoint. + + If unsure, say N. + config BITREVERSE tristate diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 7e9c2fa52e35..828a030ad8c7 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -57,5 +57,6 @@ obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o +obj-$(CONFIG_REF_TRACE_KUNIT_TEST) += ref_trace_kunit.o obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/ diff --git a/lib/tests/ref_trace_kunit.c b/lib/tests/ref_trace_kunit.c new file mode 100644 index 000000000000..8f896adc96d1 --- /dev/null +++ b/lib/tests/ref_trace_kunit.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct data { + unsigned long caller; + const char *fn; + const void *obj; + atomic_t count; +}; + +struct data capture; + +const void *chk_obj; + +#define test_init() \ + do { \ + KUNIT_EXPECT_FALSE( \ + test, register_trace_ref_trace_final_put(probe, NULL)); \ + \ + atomic_set_release(&capture.count, 0); \ + \ + chk_obj = &obj; \ + } while (0) + + +#define test_exit(func_name) \ + do { \ + /* wait for probe completion */ \ + int notimeout = wait_var_event_timeout( \ + &capture.count, \ + atomic_read_acquire(&capture.count), \ + 10 \ + ); \ + \ + KUNIT_ASSERT_TRUE(test, notimeout); \ + \ + unregister_trace_ref_trace_final_put(probe, NULL); \ + tracepoint_synchronize_unregister(); \ + \ + KUNIT_EXPECT_EQ(test, atomic_read_acquire(&capture.count), 1); \ + /* \ + * caller testing may be flaky \ + * due to compile optimisations so omit \ + */ \ + KUNIT_EXPECT_STREQ(test, capture.fn, #func_name); \ + KUNIT_EXPECT_PTR_EQ(test, capture.obj, &obj); \ + } while (0) + +static void probe( + void *ignore, + unsigned long caller, + const char *fn, + const void *obj) +{ + //prevent non test func final_puts from changing captured values + if (chk_obj != obj) + return; + + capture.caller = caller; + capture.fn = fn; + capture.obj = obj; + + atomic_inc_return_release(&capture.count); //increase count +} + +static void test_refcount_sub_and_test(struct kunit *test) +{ + refcount_t obj; + + test_init(); + refcount_set(&obj, 2); + + KUNIT_EXPECT_FALSE(test, refcount_dec_and_test(&obj)); + KUNIT_EXPECT_TRUE(test, refcount_dec_and_test(&obj)); + + test_exit(__refcount_sub_and_test); +} + +static void test_refcount_dec_if_one(struct kunit *test) +{ + refcount_t obj; + + test_init(); + refcount_set(&obj, 2); + + KUNIT_EXPECT_FALSE(test, refcount_dec_and_test(&obj)); + KUNIT_EXPECT_TRUE(test, refcount_dec_if_one(&obj)); + + test_exit(refcount_dec_if_one); +} +static void dummy_release(struct percpu_ref *ref) {} + +static void test_percpu_ref_put_many(struct kunit *test) +{ + struct percpu_ref obj; + + test_init(); + + KUNIT_ASSERT_FALSE(test, percpu_ref_init(&obj, dummy_release, 0, GFP_KERNEL)); + + percpu_ref_get(&obj); + percpu_ref_get(&obj); + + percpu_ref_put(&obj); + percpu_ref_put(&obj); + + percpu_ref_switch_to_atomic_sync(&obj); + + percpu_ref_put(&obj); + + test_exit(percpu_ref_put_many); + percpu_ref_exit(&obj); +} + +static struct kunit_case __refdata ref_trace_test_cases[] = { + KUNIT_CASE(test_refcount_sub_and_test), + KUNIT_CASE(test_refcount_dec_if_one), + KUNIT_CASE(test_percpu_ref_put_many), + {} +}; + +static struct kunit_suite ref_trace_test_suite = { + .name = "ref-trace", + .test_cases = ref_trace_test_cases +}; + +kunit_test_suites(&ref_trace_test_suite); + +MODULE_AUTHOR("Eugene Mavick "); +MODULE_DESCRIPTION("KUnit test for ref_trace"); +MODULE_LICENSE("GPL"); -- 2.51.2