[PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test
From: Kunwu Chan
Date: Wed Nov 18 2026 - 03:53:00 EST
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@xxxxxxxxxx>
Suggested-by: Zqiang <qiang.zhang@xxxxxxxxx>
Cc: Kunwu Chan <kunwu.chan@xxxxxxxxx>
Signed-off-by: Kunwu Chan <chentao@xxxxxxxxxx>
---
.../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