mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kunwu Chan <kunwu.chan@gmail.com>
To: dave@stgolabs.net, paulmck@kernel.org, josh@joshtriplett.org,
	frederic@kernel.org, neeraj.upadhyay@kernel.org,
	joelagnelf@nvidia.com, boqun@kernel.org, urezki@gmail.com
Cc: rostedt@goodmis.org, mathieu.desnoyers@efficios.com,
	jiangshanlai@gmail.com, qiang.zhang@linux.dev,
	linux-kernel@vger.kernel.org, rcu@vger.kernel.org,
	Kunwu Chan <kunwu.chan@gmail.com>,
	Kunwu Chan <chentao@kylinos.cn>
Subject: [PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test
Date: Wed, 18 Nov 2026 16:51:45 +0800	[thread overview]
Message-ID: <20261118085147.1668598-6-kunwu.chan@gmail.com> (raw)
In-Reply-To: <20261118085147.1668598-1-kunwu.chan@gmail.com>

Add rcutorture.kasan_debug to queue and drain an empty callback,
then free and read its rcu_head to trigger a KASAN report.

Check "Last potentially related work creation:" for rcu_test_kasan()
and the enqueue path. The intentional report makes kvm.sh exit 3
(runtime errors), even if rcutorture prints SUCCESS.

Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Suggested-by: Zqiang <qiang.zhang@linux.dev>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 .../admin-guide/kernel-parameters.txt         | 11 +++++
 kernel/rcu/rcutorture.c                       | 44 +++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index e3c5a6b68fc34..d88471dbdee40 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -6257,6 +6257,17 @@ Kernel parameters
 			accurately, from a timer handler.  Not all RCU
 			flavors take kindly to this sort of thing.
 
+	rcutorture.kasan_debug= [KNL]
+			Enable an intentional KASAN use-after-free test of
+			callback enqueue-stack recording. Requires
+			CONFIG_KASAN_GENERIC and a torture flavor with
+			callback and barrier operations. Inspect "Last
+			potentially related work creation" in the report
+			for the enqueue stack. Disabled by default.
+			Use a dedicated test run: the report taints the
+			kernel and suppresses later KASAN reports unless
+			kasan_multi_shot is enabled.
+
 	rcutorture.leakpointer= [KNL]
 			Leak an RCU-protected pointer out of the reader.
 			This can of course result in splats, and is
diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 0e8366eb2efb5..5051ec0a853b9 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -112,6 +112,7 @@ torture_param(int, gp_poll_wi_exp, 128,
 		   "Wait interval for expedited polled grace periods, us (default 128 us)");
 torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
 torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
+torture_param(bool, kasan_debug, false, "Enable KASAN aux-stack call_rcu() testing");
 torture_param(int, leakpointer, 0, "Leak pointer dereferences from readers");
 torture_param(int, n_barrier_cbs, 0, "# of callbacks/kthreads for barrier testing");
 torture_param(int, n_up_down, 32, "# of concurrent up/down hrtimer-based RCU readers");
@@ -4639,6 +4640,46 @@ static void rcu_test_debug_objects(void)
 	kfree(rhp);
 }
 
+/*
+ * Trigger a KASAN use-after-free report after draining a callback. Check
+ * "Last potentially related work creation" for the callback enqueue stack.
+ */
+static noinline void rcu_test_kasan(void)
+{
+	struct rcu_head *rhp;
+
+	if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
+		pr_alert("%s: !CONFIG_KASAN_GENERIC, not testing KASAN aux stack call_%s()\n",
+			 KBUILD_MODNAME, cur_ops->name);
+		return;
+	}
+
+	if (!cur_ops->call || !cur_ops->cb_barrier) {
+		pr_alert("%s: %s has no callback/barrier ops, skipping KASAN test\n",
+			 KBUILD_MODNAME, cur_ops->name);
+		return;
+	}
+
+	rhp = kmalloc_obj(*rhp);
+	if (!rhp) {
+		pr_alert("%s: KASAN aux-stack test skipped: allocation failed\n", KBUILD_MODNAME);
+		return;
+	}
+
+	pr_alert("%s: WARN: KASAN aux-stack call_%s() test starting.\n",
+		 KBUILD_MODNAME, cur_ops->name);
+
+	/* Keep the rcu_head allocated until all callbacks have completed. */
+	cur_ops->call(rhp, rcu_torture_leak_cb);
+	cur_ops->cb_barrier();
+
+	kfree(rhp);
+	/* Intentional use-after-free to display the recorded enqueue stack. */
+	(void)READ_ONCE(*(u8 *)rhp);
+	pr_alert("%s: WARN: KASAN aux-stack call_%s() test complete.\n",
+		 KBUILD_MODNAME, cur_ops->name);
+}
+
 static void rcutorture_sync(void)
 {
 	static unsigned long n;
@@ -5198,6 +5239,9 @@ rcu_torture_init(void)
 	if (object_debug)
 		rcu_test_debug_objects();
 
+	if (kasan_debug)
+		rcu_test_kasan();
+
 	if (cur_ops->gp_slow_register && !WARN_ON_ONCE(!cur_ops->gp_slow_unregister))
 		cur_ops->gp_slow_register(&rcu_fwd_cb_nodelay);
 
-- 
2.43.0


  parent reply	other threads:[~2026-11-18  8:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:51 [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests Kunwu Chan
2026-11-18  8:51 ` [PATCH v2 1/7] rcu-tasks: Record callback enqueue stack Kunwu Chan
2026-11-18  8:51 ` [PATCH v2 2/7] rcutorture: Require debug_objects flag for debug-objects test Kunwu Chan
2026-11-18  8:51 ` [PATCH v2 3/7] rcutorture: Test duplicate call_rcu_tasks() callback enqueue Kunwu Chan
2026-11-18  8:51 ` [PATCH v2 4/7] rcutorture: Test duplicate call_rcu_tasks_trace() " Kunwu Chan
2026-11-18  8:51 ` Kunwu Chan [this message]
2026-11-18  8:51 ` [PATCH v2 6/7] rcutorture: Add kasan-aux.sh aux-stack checker Kunwu Chan
2026-11-18  8:51 ` [PATCH v2 7/7] rcutorture: Run KASAN aux-stack checks from torture.sh Kunwu Chan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118085147.1668598-6-kunwu.chan@gmail.com \
    --to=kunwu.chan@gmail.com \
    --cc=boqun@kernel.org \
    --cc=chentao@kylinos.cn \
    --cc=dave@stgolabs.net \
    --cc=frederic@kernel.org \
    --cc=jiangshanlai@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=qiang.zhang@linux.dev \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=urezki@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®