Skip to contentSkip to conversation

rcu: Record callback enqueue stacks and add torture tests

PATCH New Kunwu Chan 1 participant 8 messages Nov 18

Subsystems (5)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

v2 cover rcu: Record callback enqueue stacks and add torture tests

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/[email protected]/

- 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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

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
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/7 rcu-tasks: Record callback enqueue stack

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().

---
 kernel/rcu/tasks.h | 4 ++++
 1 file changed, 4 insertions(+)
kernel/rcu/tasks.h +4 -0
index a20db323612b6..fd43cd5d5a485 100644
@@ -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",
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/7 rcutorture: Require debug_objects flag for debug-objects test

In reply to 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.

---
 kernel/rcu/rcutorture.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/rcu/rcutorture.c +7 -2
index 968343be4e0d1..caa550095705a 100644
@@ -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);
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/7 rcutorture: Test duplicate call_rcu_tasks() callback enqueue

In reply to Kunwu Chan

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

---
 kernel/rcu/rcutorture.c | 1 +
 1 file changed, 1 insertion(+)
kernel/rcu/rcutorture.c +1 -0
index caa550095705a..3dcc3d4318ad1 100644
@@ -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"
 };
 
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v2 4/7 rcutorture: Test duplicate call_rcu_tasks_trace() callback enqueue

In reply to 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().

---
 kernel/rcu/rcutorture.c | 1 +
 1 file changed, 1 insertion(+)
kernel/rcu/rcutorture.c +1 -0
index 3dcc3d4318ad1..0e8366eb2efb5 100644
@@ -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"
 };
 
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v2 5/7 rcutorture: Add KASAN aux-stack self-test

In reply to 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.

---
 .../admin-guide/kernel-parameters.txt         | 11 +++++
 kernel/rcu/rcutorture.c                       | 44 +++++++++++++++++++
 2 files changed, 55 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +11 -0
index e3c5a6b68fc34..d88471dbdee40 100644
@@ -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
kernel/rcu/rcutorture.c +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#6 lore raw

PATCH v2 6/7 rcutorture: Add kasan-aux.sh aux-stack checker

In reply to 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.

---
 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
Documentation/RCU/torture.rst +32 -0
index 1ad5cc7938113..15a430f5111f4 100644
@@ -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'".
tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh new file +158 -0

158 lines not on this page · raw message

tools/testing/selftests/rcutorture/bin/kasan-aux.sh new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#7 lore raw

PATCH v2 7/7 rcutorture: Run KASAN aux-stack checks from torture.sh

In reply to 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.

---
 Documentation/RCU/torture.rst                     | 14 ++++++++++++++
 tools/testing/selftests/rcutorture/bin/torture.sh |  9 +++++++++
 2 files changed, 23 insertions(+)
Documentation/RCU/torture.rst +14 -0
index 15a430f5111f4..8c8dcf6449bf0 100644
@@ -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'".
tools/testing/selftests/rcutorture/bin/torture.sh +9 -0
index 90b5e113a0b03..e04ed2c27f533 100755
@@ -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
Signature · 1 line
2.43.0