mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests
@ 2026-11-18  8:51 Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 1/7] rcu-tasks: Record callback enqueue stack Kunwu Chan
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan

This series records the enqueueing stack for RCU Tasks callbacks and
adds rcutorture coverage for duplicate callbacks and KASAN
auxiliary-stack recording.

Changes since v1, in response to feedback from Joel Fernandes,
Paul E. McKenney, and Zqiang:
v1: https://lore.kernel.org/all/20261106143913.3627722-1-kunwu.chan@gmail.com/

- Dropped the RCU_KASAN_AUX_TEST module, its Kconfig flavor choice,
  and the five *KASAN01 scenarios.  The verification is now an
  rcutorture self-test that uses the selected flavor's callback and
  barrier operations, enabled by the new
  rcutorture.kasan_debug boot parameter, so it can be run from
  kvm.sh --kasan with existing scenarios.

- rcu_test_debug_objects() now skips flavors that do not set
  ->debug_objects.  The flag previously guarded only the
  ->call()/->cb_barrier() sanity check, so torture_type=trivial
  with CONFIG_DEBUG_OBJECTS_RCU_HEAD=y and
  rcutorture.object_debug=1 invoked a NULL function pointer.
  This panic was reproduced before the fix and verified fixed
  after it, with identical rcutorture arguments.

- New in v2: kasan-aux.sh automates the auxiliary-stack checks using
  kvm.sh --kasan, existing scenarios, and the existing checkers,
  ending with a single kvm-recheck.sh pass over the filtered logs.
  kasan-aux.sh can run and check the auxiliary-stack test on its
  own.  The final patch also runs it from torture.sh when both RCU
  and KASAN testing are enabled.

Tested on x86_64 with kvm.sh --kasan, kasan_multi_shot, and
rcutorture.kasan_debug=1 (30-second runs, built kernels reused via
kvm-again.sh to select flavors):

  Scenario Flavor         "Last potentially related work creation"
  TASKS01  tasks          call_rcu_tasks_generic
  TASKS01  rcu            __call_rcu_common
  TASKS01  srcu           __call_srcu
  TASKS01  tasks-tracing  __call_srcu
  TINY01   rcu            call_rcu
  TINY01   srcu           call_srcu

kasan-aux.sh checks for rcu_test_kasan() in the auxiliary stack.  
All runs reached "End of test: SUCCESS" with one intentional report, 
which the torture summary flags (Bugs: 1) as it flags the 
intentional warnings from rcutorture.object_debug=1.

Also verified:
- tasks with object_debug=1 and kasan_debug=1, 3-minute run:
  the four intentional debugobjects warnings, then the KASAN
  report, then SUCCESS with barrier: 633/633.
- Direct kvm.sh runs of trivial, tasks-rude, and busted with one or
  both self-tests enabled: clean skip messages, no splats, exit code 0.
  kasan-aux.sh expects these flavors to skip.
- TASKS01 without KASAN and kasan_debug=1: test skipped,
  exit code 0.
- tasks with both self-tests disabled: no splats, exit code 0.
- kasan-aux.sh --check negatives: a TINY01 build with
  kasan_record_aux_stack() compiled out of call_rcu() fails with a
  missing auxiliary stack, and a non-KASAN run is rejected at the
  configuration check.

Keep this test out of runs intended to find real KASAN bugs:
KASAN suppresses reports after the first unless kasan_multi_shot
is also supplied, and the report taints the kernel.

The seventh patch runs kasan-aux.sh from torture.sh on the scenarios
selected by --configs-rcutorture, so the added kernel builds scale
with that selection; --no-kasan or dropping that patch avoids them
entirely.  Feedback on whether that default is worthwhile would be
welcome.

For example:

  tools/testing/selftests/rcutorture/bin/kvm.sh --configs TASKS01 \
    --kasan --duration 30s \
    --bootargs "rcutorture.kasan_debug=1 kasan_multi_shot"

  tools/testing/selftests/rcutorture/bin/kasan-aux.sh

This series was tested on top of [1] with Joel's RCU Tasks barrier
debugobjects fix [2] applied.

[1] https://lore.kernel.org/all/20261027070923.3054462-1-kunwu.chan@gmail.com/
[2] https://lore.kernel.org/all/20261104153046.965890-1-joelagnelf@nvidia.com/

 
Kunwu Chan (7):
  rcu-tasks: Record callback enqueue stack
  rcutorture: Require debug_objects flag for debug-objects test
  rcutorture: Test duplicate call_rcu_tasks() callback enqueue
  rcutorture: Test duplicate call_rcu_tasks_trace() callback enqueue
  rcutorture: Add KASAN aux-stack self-test
  rcutorture: Add kasan-aux.sh aux-stack checker
  rcutorture: Run KASAN aux-stack checks from torture.sh

 Documentation/RCU/torture.rst                 |  46 ++++
 .../admin-guide/kernel-parameters.txt         |  11 +
 kernel/rcu/rcutorture.c                       |  55 ++++-
 kernel/rcu/tasks.h                            |   4 +
 .../rcutorture/bin/kasan-aux-check.sh         | 158 ++++++++++++
 .../selftests/rcutorture/bin/kasan-aux.sh     | 228 ++++++++++++++++++
 .../selftests/rcutorture/bin/torture.sh       |   9 +
 7 files changed, 509 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh
 create mode 100755 tools/testing/selftests/rcutorture/bin/kasan-aux.sh

-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/7] rcu-tasks: Record callback enqueue stack
  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 ` Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 2/7] rcutorture: Require debug_objects flag for debug-objects test Kunwu Chan
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

Tree RCU records the code path that enqueued a given callback via
kasan_record_aux_stack(), but Tasks RCU does not.  KASAN reports
involving Tasks RCU callbacks therefore lack any hint as to which
code path did the enqueueing.  This commit therefore invokes
kasan_record_aux_stack() in call_rcu_tasks_generic(), covering both
call_rcu_tasks() and call_rcu_tasks_rude().

Suggested-by: Zqiang <qiang.zhang@linux.dev>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 kernel/rcu/tasks.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index a20db323612b6..fd43cd5d5a485 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -6,6 +6,7 @@
  */
 
 #ifdef CONFIG_TASKS_RCU_GENERIC
+#include <linux/kasan.h>
 #include <linux/mm.h>
 #include <linux/ratelimit.h>
 #include "rcu_segcblist.h"
@@ -366,6 +367,9 @@ static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
 	if (WARN_ON_ONCE(!func))
 		return;
 
+	/* Record the caller: the irq_work's stack says nothing about it. */
+	kasan_record_aux_stack(rhp);
+
 	if (debug_rcu_head_queue(rhp)) {
 		if (__ratelimit(&rcu_tasks_dup_rs)) {
 			pr_err("%s(): Duplicate CB for %s, %p->%pS()\n",
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 2/7] rcutorture: Require debug_objects flag for debug-objects test
  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 ` Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 3/7] rcutorture: Test duplicate call_rcu_tasks() callback enqueue Kunwu Chan
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

rcu_test_debug_objects() checks for missing callback and barrier
operations only when ->debug_objects is set.  With the flag clear,
execution falls through to the test even if those operations are
missing.

With CONFIG_DEBUG_OBJECTS_RCU_HEAD=y and rcutorture.object_debug=1,
the trivial flavor can therefore call a NULL function pointer.

Skip the test for flavors that do not set ->debug_objects.

Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 kernel/rcu/rcutorture.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 968343be4e0d1..caa550095705a 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -4603,8 +4603,13 @@ static void rcu_test_debug_objects(void)
 		return;
 	}
 
-	if (WARN_ON_ONCE(cur_ops->debug_objects &&
-			(!cur_ops->call || !cur_ops->cb_barrier)))
+	if (!cur_ops->debug_objects) {
+		pr_alert("%s: %s does not support debug-object testing\n",
+			 KBUILD_MODNAME, cur_ops->name);
+		return;
+	}
+
+	if (WARN_ON_ONCE(!cur_ops->call || !cur_ops->cb_barrier))
 		return;
 
 	struct rcu_head *rhp = kmalloc_obj(*rhp);
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 3/7] rcutorture: Test duplicate call_rcu_tasks() callback enqueue
  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 ` Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 4/7] rcutorture: Test duplicate call_rcu_tasks_trace() " Kunwu Chan
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

Enable the existing duplicate-callback test for RCU Tasks by
setting ->debug_objects.

Suggested-by: Zqiang <qiang.zhang@linux.dev>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 kernel/rcu/rcutorture.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index caa550095705a..3dcc3d4318ad1 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -1221,6 +1221,7 @@ static struct rcu_torture_ops tasks_ops = {
 	.get_gp_data	= rcu_tasks_get_gp_data,
 	.irq_capable	= 1,
 	.slow_gps	= 1,
+	.debug_objects	= 1,
 	.name		= "tasks"
 };
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 4/7] rcutorture: Test duplicate call_rcu_tasks_trace() callback enqueue
  2026-11-18  8:51 [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests Kunwu Chan
                   ` (2 preceding siblings ...)
  2026-11-18  8:51 ` [PATCH v2 3/7] rcutorture: Test duplicate call_rcu_tasks() callback enqueue Kunwu Chan
@ 2026-11-18  8:51 ` Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test Kunwu Chan
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

Enable the existing duplicate-callback test for tasks-tracing by
setting ->debug_objects.  call_rcu_tasks_trace() is implemented
in terms of call_srcu(), so the detection is provided by
__call_srcu().

Suggested-by: Zqiang <qiang.zhang@linux.dev>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 kernel/rcu/rcutorture.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 3dcc3d4318ad1..0e8366eb2efb5 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -1315,6 +1315,7 @@ static struct rcu_torture_ops tasks_tracing_ops = {
 	.irq_capable	= 1,
 	.slow_gps	= 1,
 	.nmi_capable	= 1,
+	.debug_objects	= 1,
 	.name		= "tasks-tracing"
 };
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test
  2026-11-18  8:51 [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests Kunwu Chan
                   ` (3 preceding siblings ...)
  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
  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
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 6/7] rcutorture: Add kasan-aux.sh aux-stack checker
  2026-11-18  8:51 [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests Kunwu Chan
                   ` (4 preceding siblings ...)
  2026-11-18  8:51 ` [PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test Kunwu Chan
@ 2026-11-18  8:51 ` Kunwu Chan
  2026-11-18  8:51 ` [PATCH v2 7/7] rcutorture: Run KASAN aux-stack checks from torture.sh Kunwu Chan
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

kasan-aux.sh runs dedicated aux-stack tests with kvm.sh --kasan,
existing scenarios, and the rcutorture.kasan_debug=1 and
kasan_multi_shot boot parameters.  It then verifies the recorded
caller in each intentional report's auxiliary stack, retaining the
report body and using kvm-recheck.sh for the ordinary checks.
Flavors without callback operations are expected to skip the
self-test.  Saved results can be rechecked with --check using
their batches list and QEMU commands.

Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Suggested-by: Joel Fernandes <joelagnelf@nvidia.com>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 Documentation/RCU/torture.rst                 |  32 +++
 .../rcutorture/bin/kasan-aux-check.sh         | 158 ++++++++++++
 .../selftests/rcutorture/bin/kasan-aux.sh     | 228 ++++++++++++++++++
 3 files changed, 418 insertions(+)
 create mode 100755 tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh
 create mode 100755 tools/testing/selftests/rcutorture/bin/kasan-aux.sh

diff --git a/Documentation/RCU/torture.rst b/Documentation/RCU/torture.rst
index 1ad5cc7938113..15a430f5111f4 100644
--- a/Documentation/RCU/torture.rst
+++ b/Documentation/RCU/torture.rst
@@ -212,6 +212,38 @@ Note that --gdb limits you to one scenario per kvm.sh run and requires
 that you have another window open from which to run ``gdb`` as instructed
 by the script.
 
+The callback enqueue-stack self-test intentionally triggers a KASAN
+use-after-free report.  Run it separately from ordinary KASAN bug hunts::
+
+	tools/testing/selftests/rcutorture/bin/kasan-aux.sh
+
+This uses ``kvm.sh --kasan``, existing scenarios, and the
+``rcutorture.kasan_debug=1`` and ``kasan_multi_shot`` boot parameters.
+The default 30-second runs cover Tree RCU, RCU Tasks, RCU Tasks Trace,
+Tree SRCU, and, except on arm64, Tiny RCU and Tiny SRCU.  Use ``--configs``
+and ``--duration`` to override the scenario list and duration.
+
+These scenarios cover the callback enqueue implementations that record
+KASAN auxiliary stacks, rather than all RCU configuration combinations.
+Flavors with no callback operations, such as Tasks Rude, are expected
+to skip the self-test, and the checker requires their skip message and
+a successful ordinary run.  Any other skip fails the check, even if
+the ordinary torture run succeeds.
+
+The script requires exactly one intentional report per supported scenario, with
+``rcu_test_kasan()`` in its "Last potentially related work creation"
+stack, followed by successful completion of the same torture flavor.
+Only the verified report's diagnostic markers are filtered; the body
+is retained for the existing checks, including the configuration,
+build, and console checks that ``kvm-recheck.sh`` runs.  The original
+logs and the underlying ``kvm.sh`` runtime-error status are preserved.
+Saved results can be rechecked with ``kasan-aux.sh --check
+results-directory``, using the saved ``batches`` file and QEMU commands.
+As with the intentional warnings tested by ``srcu_lockdep.sh``, ordinary
+``kvm-recheck.sh`` and ``kvm-find-errors.sh`` still report the raw
+diagnostic; use the dedicated check to distinguish the expected report
+from failures.
+
 Kernel boot arguments can also be supplied, for example, to control
 rcutorture's module parameters.  For example, to test a change to RCU's
 CPU stall-warning code, use "--bootargs 'rcutorture.stall_cpu=30'".
diff --git a/tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh b/tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh
new file mode 100755
index 0000000000000..7a6f425975207
--- /dev/null
+++ b/tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh
@@ -0,0 +1,158 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Verify the intentional rcutorture KASAN report and remove its diagnostics.
+# Flavors without callback operations must instead show the expected skip.
+# Leave all other console output for the usual torture error checks.
+#
+# Authors: Kunwu Chan <kunwu.chan@gmail.com>
+#
+# Usage: kasan-aux-check.sh console.log flavor
+
+if test "$#" -ne 2 || ! test -r "$1"
+then
+	echo "Usage: $0 console.log flavor" >&2
+	exit 1
+fi
+
+awk -v flavor="$2" '
+BEGIN {
+	timestamp = "[[:space:]]*[0-9]+\\.[0-9]+";
+	caller = "[[:space:]]*[CT][[:space:]]*[0-9]+";
+	log_prefix = "^[[:space:]]*\\[(" timestamp "|" caller ")\\][[:space:]]*";
+	frame_prefix = "^(\\[<[[:xdigit:]]+>\\][[:space:]]*)?\\??[[:space:]]*";
+	frame_suffix = "([.][[:alnum:]_.]+)?\\+0x[[:xdigit:]]+/0x[[:xdigit:]]+([[:space:]]|$)";
+	test_name = "rcutorture: WARN: KASAN aux-stack call_" flavor "()";
+	success = flavor "-torture:--- End of test: SUCCESS: ";
+	skip_name = "rcutorture: " flavor " has no callback/barrier ops, skipping KASAN test";
+	skip_mode = flavor ~ /^(tasks-rude|busted|trivial)$/;
+}
+
+function fail(message)
+{
+	print "kasan-aux-check.sh: " message " at line " NR > "/dev/stderr";
+	failed = 1;
+	exit 1;
+}
+
+# Strip printk timestamps/caller IDs for matching, retaining the original line.
+function message(line)
+{
+	sub(/\r$/, "", line);
+	sub(/^<[0-9]+>/, "", line);
+	while (sub(log_prefix, "", line))
+		;
+	sub(/^[[:space:]]+/, "", line);
+	sub(/[[:space:]]+$/, "", line);
+	return line;
+}
+
+function frame(line, symbol)
+{
+	return line ~ (frame_prefix symbol frame_suffix);
+}
+
+{
+	line = message($0);
+	if (skip_mode) {
+		if (line == skip_name) {
+			if (++skips != 1 || succeeded)
+				fail("unexpected or late skip message");
+		} else if (line ~ /skipping KASAN test|KASAN aux-stack test skipped|not testing KASAN aux stack/)
+			fail("unexpected skip message");
+		if (line ~ /End of test: SUCCESS:/) {
+			if (index(line, success) != 1 || ++succeeded != 1 || skips != 1)
+				fail("unexpected SUCCESS marker");
+		} else if (line ~ /End of test:/)
+			fail("unexpected test result");
+		if (line ~ /^rcutorture: WARN: KASAN aux-stack/ || line ~ /BUG: KASAN:/)
+			fail("unexpected aux-stack test output");
+		print;
+		next;
+	}
+	if (line ~ /skipping KASAN test|KASAN aux-stack test skipped|not testing KASAN aux stack/)
+		fail("KASAN test was skipped");
+	if (line ~ /^rcutorture: WARN: KASAN aux-stack call_[-[:alnum:]_]+\(\) test starting\.$/) {
+		if (line != test_name " test starting." ||
+		    ++started != 1 || reports || completed || succeeded)
+			fail("unexpected test start");
+		next;
+	}
+	if (line ~ /^rcutorture: WARN: KASAN aux-stack call_[-[:alnum:]_]+\(\) test complete\.$/) {
+		if (line != test_name " test complete." ||
+		    ++completed != 1 || started != 1 || reports != 1 || in_report)
+			fail("unexpected test completion");
+		next;
+	}
+	if (line ~ /End of test: SUCCESS:/) {
+		if (index(line, success) != 1 ||
+		    ++succeeded != 1 || completed != 1 || in_report)
+			fail("unexpected SUCCESS marker");
+	}
+	else if (line ~ /End of test:/)
+		fail("unexpected test result");
+	if (line ~ /BUG: KASAN:/) {
+		if (line !~ ("^BUG: KASAN: slab-use-after-free in rcu_test_kasan" frame_suffix))
+			fail("unexpected KASAN report");
+		if (in_report || !separator || started != 1 || completed || ++reports != 1)
+			fail("unexpected or unbounded KASAN report");
+		report_separator = message(separator);
+		printf "%s", before_report;
+		before_report = "";
+		separator = "";
+		in_report = 1;
+		next;
+	}
+	if (in_report) {
+		if (line == report_separator) {
+			if (reads != 1 || traces != 1 || aux_stacks != 1 || !test_frame)
+				fail("incomplete KASAN report or missing caller in auxiliary stack");
+			in_report = 0;
+			next;
+		}
+		if (line ~ /^(Read|Write) of size /) {
+			if (line !~ /^Read of size 1 at addr / || ++reads != 1)
+				fail("unexpected KASAN access");
+		}
+		if (line == "Last potentially related work creation:") {
+			if (++aux_stacks != 1)
+				fail("duplicate auxiliary stack");
+			in_aux = 1;
+		}
+		else if (in_aux) {
+			if (!frame(line, "[[:alnum:]_]+"))
+				in_aux = 0;
+			else if (frame(line, "rcu_test_kasan"))
+				test_frame = 1;
+		}
+		# Suppress only the main trace title in the expected report.
+		if (line ~ /^Call [Tt]race:$/ && ++traces == 1)
+			next;
+		print;
+		next;
+	}
+	if (started && !reports && line ~ /^=+$/) {
+		if (separator) {
+			print separator;
+			printf "%s", before_report;
+		}
+		separator = $0;
+		before_report = "";
+		next;
+	}
+	if (separator) {
+		before_report = before_report $0 "\n";
+		next;
+	}
+	print;
+}
+
+END {
+	if (failed)
+		exit 1;
+	if (skip_mode) {
+		if (skips != 1 || succeeded != 1)
+			fail("missing skip message or SUCCESS marker");
+	} else if (in_report || started != 1 || completed != 1 || succeeded != 1 || reports != 1)
+		fail("missing test markers or truncated KASAN report");
+}' < "$1" || exit 1
diff --git a/tools/testing/selftests/rcutorture/bin/kasan-aux.sh b/tools/testing/selftests/rcutorture/bin/kasan-aux.sh
new file mode 100755
index 0000000000000..ae7896658e94d
--- /dev/null
+++ b/tools/testing/selftests/rcutorture/bin/kasan-aux.sh
@@ -0,0 +1,228 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Run and check the intentional KASAN callback enqueue-stack tests.
+#
+# Authors: Kunwu Chan <kunwu.chan@gmail.com>
+
+scriptname=$0
+RCUTORTURE="$(pwd)/tools/testing/selftests/rcutorture"; export RCUTORTURE
+PATH=${RCUTORTURE}/bin:$PATH; export PATH
+. functions.sh
+
+usage () {
+	echo "Usage: $scriptname [ --datestamp string ] [ --duration time ]"
+	echo "       [ --configs \"existing scenario list\" ]"
+	echo "       $scriptname --check results-directory"
+	exit 1
+}
+
+ds="$(date +%Y.%m.%d-%H.%M.%S)-kasan-aux"
+duration=30s
+configs="TREE03 TASKS01 TRACE01 SRCU-P"
+if test "$(uname -m)" != aarch64
+then
+	configs="$configs TINY01 SRCU-T"
+fi
+checkdir=
+while test $# -gt 0
+do
+	case "$1" in
+	--check)
+		checkarg --check "(results directory)" "$#" "$2" '.' '^--'
+		checkdir=$2
+		shift
+		;;
+	--configs)
+		checkarg --configs "(scenario list)" "$#" "$2" '^[^/.a-z]\+$' '^--'
+		configs=$2
+		shift
+		;;
+	--datestamp)
+		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[a-zA-Z0-9._/-]*$' '^--'
+		ds=$2
+		shift
+		;;
+	--duration)
+		checkarg --duration "(time)" "$#" "$2" '^[0-9][0-9]*[smhd]\?$' '^--'
+		duration=$2
+		shift
+		;;
+	"")
+		;;
+	*)
+		usage
+		;;
+	esac
+	shift
+done
+
+T="$(mktemp -d "${TMPDIR-/tmp}/kasan-aux.sh.XXXXXX")" || exit 1
+trap 'rm -rf "$T"' EXIT
+nsupported=0
+
+# Keep raw logs intact and let the ordinary recheck handle the verified output.
+check_results () {
+	local rundir config dir copy flavor f configs
+	local work="$T/results"
+
+	rundir=$(cd "$1" && pwd) || return 1
+	if ! test -s "$rundir/batches" || ! test -r "$rundir/log"
+	then
+		echo "$rundir: missing batches or log"
+		return 1
+	fi
+	configs=$(awk '!/^#/ { print $2 }' "$rundir/batches")
+	if test -z "$configs"
+	then
+		echo "$rundir: empty scenario list"
+		return 1
+	fi
+	mkdir "$work" || return 1
+	ln -s "$rundir/log" "$work/log" || return 1
+	echo rcu > "$work/torture_suite"
+	if test -f "$rundir/re-run"
+	then
+		ln -s "$rundir/re-run" "$work/re-run" || return 1
+	fi
+	for config in $configs
+	do
+		dir="$rundir/$config"
+		copy="$work/$config"
+		mkdir "$copy" || return 1
+		# Link inputs only, so recheck cannot overwrite saved diagnostics.
+		for f in .config ConfigFragment ConfigFragment.input Make.defconfig.out Make.out qemu-cmd qemu-retval
+		do
+			if ! test -r "$dir/$f"
+			then
+				echo "$config: missing $f"
+				return 1
+			fi
+			ln -s "$dir/$f" "$copy/$f" || return 1
+		done
+		for f in Make.oldconfig.err Warnings vmlinux vmlinux.xz
+		do
+			ln -s "$dir/$f" "$copy/$f" || return 1
+		done
+		if ! grep -q '^CONFIG_KASAN_GENERIC=y$' "$dir/.config" ||
+		   test "$(cat "$dir/qemu-retval")" != 0
+		then
+			echo "$config: Generic KASAN required and QEMU must exit normally"
+			return 1
+		fi
+		# kvm.sh saves a single QEMU command with a double-quoted -append string.
+		# Read it once, retaining the last flavor parameter, as the kernel does.
+		if ! flavor=$(awk '
+			BEGIN { flavor = "rcu"; }
+			/^[[:space:]]*(#|$)/ { next; }
+			{
+				if (++commands != 1 ||
+				    !match($0, /(^|[[:space:]])-append[[:space:]]+"/))
+					exit 1;
+				args = substr($0, RSTART + RLENGTH);
+				if (!sub(/"[^"]*$/, "", args))
+					exit 1;
+				# Only the plain kvm.sh-generated format is supported.
+				if (args ~ /\\/)
+					exit 1;
+				# Keep quoted kernel parameter values together.
+				while (args ~ /[^[:space:]]/) {
+					sub(/^[[:space:]]+/, "", args);
+					if (!match(args, /^([^"[:space:]]|"[^"]*")+/))
+						exit 1;
+					word = substr(args, 1, RLENGTH);
+					args = substr(args, RLENGTH + 1);
+					if (args !~ /^([[:space:]]|$)/)
+						exit 1;
+					whole_quote = sub(/^"/, "", word);
+					equals = index(word, "=");
+					param = equals ? substr(word, 1, equals - 1) : word;
+					value = equals ? substr(word, equals + 1) : "";
+					value_quote = sub(/^"/, "", value);
+					if (whole_quote || value_quote) {
+						if (equals)
+							sub(/"$/, "", value);
+						else
+							sub(/"$/, "", param);
+					}
+					if (param == "--" && !equals)
+						break;
+					gsub(/-/, "_", param);
+					if (param == "rcutorture.torture_type")
+						flavor = value;
+					if (param == "kasan_multi_shot")
+						multi_shot = 1;
+				}
+				valid = 1;
+			}
+		END {
+			if (!valid || commands != 1 || !multi_shot ||
+			    flavor !~ /^(rcu|srcu|srcud|tasks|tasks-tracing|tasks-rude|busted|trivial)$/)
+				exit 1;
+			print flavor;
+		}' "$dir/qemu-cmd")
+		then
+			echo "$config: invalid saved boot arguments or missing kasan_multi_shot"
+			return 1
+		fi
+		case "$flavor" in
+		rcu|srcu|srcud|tasks|tasks-tracing)
+			nsupported=$((nsupported+1))
+			;;
+		esac
+		if ! TMPDIR="$T" kasan-aux-check.sh "$dir/console.log" "$flavor" > "$copy/console.log"
+		then
+			return 1
+		fi
+		# kvm-recheck.sh does not propagate parse-build.sh exit status.
+		TMPDIR="$T" parse-build.sh "$dir/Make.out" "$config" || return 1
+	done
+	if ! TMPDIR="$T" kvm-recheck.sh "$work" 2> "$T/recheck.err" || test -s "$T/recheck.err"
+	then
+		cat "$T/recheck.err"
+		return 1
+	fi
+	echo "Summary: KASAN aux-stack checks passed"
+}
+
+if test -n "$checkdir"
+then
+	check_results "$checkdir"
+	exit $?
+fi
+
+rundir="$RCUTORTURE/res/$ds"
+if test -e "$rundir"
+then
+	echo "Results directory already exists: $rundir"
+	exit 1
+fi
+kvm.sh --allcpus --kasan --kconfig "CONFIG_KASAN_GENERIC=y" \
+	--configs "$configs" --duration "$duration" --trust-make \
+	--bootargs "rcutorture.kasan_debug=1 rcutorture.object_debug=0 kasan_multi_shot" \
+	--datestamp "$ds"
+ret=$?
+err=0
+if ! check_results "$rundir" > "$T/check.out" 2>&1
+then
+	err=1
+fi
+# Scenarios with callback operations produce the intentional reports, so
+# kvm.sh must report them as runtime errors.  Skip-only selections exit 0.
+if test "$nsupported" -gt 0
+then
+	expected=3
+else
+	expected=0
+fi
+if test "$ret" -ne "$expected"
+then
+	echo "Expected kvm.sh exit status $expected, got $ret"
+	err=1
+fi
+cat "$T/check.out"
+if test -d "$rundir"
+then
+	cat "$T/check.out" >> "$rundir/log"
+fi
+exit "$err"
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 7/7] rcutorture: Run KASAN aux-stack checks from torture.sh
  2026-11-18  8:51 [PATCH v2 0/7] rcu: Record callback enqueue stacks and add torture tests Kunwu Chan
                   ` (5 preceding siblings ...)
  2026-11-18  8:51 ` [PATCH v2 6/7] rcutorture: Add kasan-aux.sh aux-stack checker Kunwu Chan
@ 2026-11-18  8:51 ` Kunwu Chan
  6 siblings, 0 replies; 8+ messages in thread
From: Kunwu Chan @ 2026-11-18  8:51 UTC (permalink / raw)
  To: dave, paulmck, josh, frederic, neeraj.upadhyay, joelagnelf,
	boqun, urezki
  Cc: rostedt, mathieu.desnoyers, jiangshanlai, qiang.zhang,
	linux-kernel, rcu, Kunwu Chan, Kunwu Chan

Invoke kasan-aux.sh when torture.sh has both RCU-torture and KASAN
testing enabled, reusing the existing --do-kasan switch, the
torture_one bookkeeping, and the --configs-rcutorture scenario
selection, so the added kernel builds scale with that selection.
--no-kasan disables both ordinary KASAN and aux-stack testing.

Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Suggested-by: Joel Fernandes <joelagnelf@nvidia.com>
Cc: Kunwu Chan <kunwu.chan@gmail.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
---
 Documentation/RCU/torture.rst                     | 14 ++++++++++++++
 tools/testing/selftests/rcutorture/bin/torture.sh |  9 +++++++++
 2 files changed, 23 insertions(+)

diff --git a/Documentation/RCU/torture.rst b/Documentation/RCU/torture.rst
index 15a430f5111f4..8c8dcf6449bf0 100644
--- a/Documentation/RCU/torture.rst
+++ b/Documentation/RCU/torture.rst
@@ -244,6 +244,20 @@ As with the intentional warnings tested by ``srcu_lockdep.sh``, ordinary
 diagnostic; use the dedicated check to distinguish the expected report
 from failures.
 
+``torture.sh`` includes these dedicated runs when both RCU torture and
+KASAN testing are enabled, as they are by default, using the scenarios
+selected by ``--configs-rcutorture`` and 30-second run durations.
+``--no-kasan`` disables both ordinary KASAN and auxiliary-stack testing.
+For example::
+
+	tools/testing/selftests/rcutorture/bin/torture.sh \
+		--do-none --do-rcutorture --do-kasan \
+		--configs-rcutorture TREE03 --duration 2
+
+This runs ordinary KASAN torture with TREE03, followed by the dedicated
+auxiliary-stack verification of TREE03.  To run only the auxiliary
+checks, invoke ``kasan-aux.sh`` directly.
+
 Kernel boot arguments can also be supplied, for example, to control
 rcutorture's module parameters.  For example, to test a change to RCU's
 CPU stall-warning code, use "--bootargs 'rcutorture.stall_cpu=30'".
diff --git a/tools/testing/selftests/rcutorture/bin/torture.sh b/tools/testing/selftests/rcutorture/bin/torture.sh
index 90b5e113a0b03..e04ed2c27f533 100755
--- a/tools/testing/selftests/rcutorture/bin/torture.sh
+++ b/tools/testing/selftests/rcutorture/bin/torture.sh
@@ -515,6 +515,15 @@ if test "$do_rcutorture" = "yes"
 then
 	torture_bootargs="rcupdate.rcu_cpu_stall_suppress_at_boot=1 torture.disable_onoff_at_boot rcupdate.rcu_task_stall_timeout=30000"
 	torture_set "rcutorture" tools/testing/selftests/rcutorture/bin/kvm.sh --allcpus --duration "$duration_rcutorture" --configs "$configs_rcutorture" --trust-make
+
+	# Check intentional reports separately from the ordinary KASAN bug hunts.
+	# kasan-aux.sh supplies its own boot arguments and rejects --bootargs.
+	if test "$do_kasan" = "yes"
+	then
+		torture_bootargs=""
+		curflavor=rcu-aux-kasan
+		torture_one tools/testing/selftests/rcutorture/bin/kasan-aux.sh --configs "$configs_rcutorture"
+	fi
 fi
 
 # Test atomic SRCU across Tree SRCU (SRCU-N and SRCU-P) and Tiny SRCU
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-18  8:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v2 5/7] rcutorture: Add KASAN aux-stack self-test Kunwu Chan
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

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®