Skip to contentSkip to conversation

rcu: Record callback enqueue stacks and add torture tests

PATCH New Kunwu Chan 1 participant 8 messages Nov 28

Subsystems (5)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

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

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

kasan-aux.sh runs the auxiliary-stack self-test on existing scenarios and checks for the enqueue caller in the intentional KASAN report. Patch 6 runs it from torture.sh when both RCU torture and KASAN testing are enabled. The extra builds follow --configs-rcutorture. --no-kasan disables both ordinary KASAN torture and the auxiliary-stack checks.

Changes since v2 [1]

- Rebased onto Paul's dev branch at 68dcfb0e34ee. The previous callback stack and duplicate-enqueue series is already in dev. Joel's Tasks RCU barrier debugobjects fix is included in f9857faffbb2 ("rcu-tasks: Detect duplicate callback enqueue").
- Dropped v2 patch 4/7. Zqiang's 6abf78ac6a2c ("rcutorture: Make tasks-tracing support duplicate call testing") already sets debug_objects for Tasks Trace.
- Adjusted the torture.sh patch to keep dev's NMI boot parameters and QEMU arguments for ordinary RCU torture. The dedicated aux-stack run uses its own boot parameters.
- Added patch 7/7 to accept the expected KASAN aux-stack skip in the TRIVIAL-PREEMPT scenario. Codex found and helped fix the checker omission while comparing the rebased series with v2.

On the rebased x86-64 tree, I built the RCU objects with Generic KASAN enabled and checked torture.sh argument handling. A 30-second QEMU run of TRIVIAL-PREEMPT skipped the KASAN self-test and reached SUCCESS.

[1] 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: Add KASAN aux-stack self-test
rcutorture: Add kasan-aux.sh aux-stack checker
rcutorture: Run KASAN aux-stack checks from torture.sh
rcutorture: Accept TRIVIAL-PREEMPT skips in kasan-aux.sh

 Documentation/RCU/torture.rst                 |  46 ++++
 .../admin-guide/kernel-parameters.txt         |  11 +
 kernel/rcu/rcutorture.c                       |  54 ++++-
 kernel/rcu/tasks.h                            |   4 +
 .../rcutorture/bin/kasan-aux-check.sh         | 158 ++++++++++++
 .../selftests/rcutorture/bin/kasan-aux.sh     | 229 ++++++++++++++++++
 .../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

base-commit: 68dcfb0e34ee3a9628f6b5ce7e06931b5248bb9e

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 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 c1e456e08e668..ab05131a12b18 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"
@@ -364,6 +365,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 v3 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 67fc770abc102..1648e6369d809 100644
@@ -4774,8 +4774,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 v3 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 1648e6369d809..8c264aaf1727a 100644
@@ -1327,6 +1327,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 v3 4/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 a49523c52c759..6425e84194f76 100644
@@ -6227,6 +6227,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
#5 lore raw

PATCH v3 5/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
#6 lore raw

PATCH v3 6/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 0498fce14ccd6..98edd2f3e747a 100755
@@ -514,6 +514,15 @@ then
 	torture_set "rcutorture" tools/testing/selftests/rcutorture/bin/kvm.sh --allcpus \
 		--duration "$duration_rcutorture" --configs "$configs_rcutorture" \
 		${qemu_args:+--qemu-args "$qemu_args"} --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
author
#7 lore raw

PATCH v3 7/7 rcutorture: Accept TRIVIAL-PREEMPT skips in kasan-aux.sh

In reply to Kunwu Chan

The TRIVIAL-PREEMPT scenario selects trivial-preempt, which has no callback or barrier operations. The kernel correctly skips the KASAN aux-stack self-test, but kasan-aux.sh rejects the saved flavor and kasan-aux-check.sh does not recognize the expected skip.

Accept trivial-preempt in both scripts and require its skip message and a successful torture run, as for the other skip-only flavors.

---
 tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh | 2 +-
 tools/testing/selftests/rcutorture/bin/kasan-aux.sh       | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/rcutorture/bin/kasan-aux-check.sh +1 -1
index 7a6f425975207..5518ca507557f 100755
@@ -25,7 +25,7 @@ BEGIN {
 	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)$/;
+	skip_mode = flavor ~ /^(tasks-rude|busted|trivial(-preempt)?)$/;
 }
 
 function fail(message)
tools/testing/selftests/rcutorture/bin/kasan-aux.sh +2 -1
index ae7896658e94d..6b6d9971f03d7 100755
@@ -157,7 +157,8 @@ check_results () {
 			}
 		END {
 			if (!valid || commands != 1 || !multi_shot ||
-			    flavor !~ /^(rcu|srcu|srcud|tasks|tasks-tracing|tasks-rude|busted|trivial)$/)
+			    (flavor !~ /^(rcu|srcu|srcud|tasks|tasks-tracing)$/ &&
+			     flavor !~ /^(tasks-rude|busted|trivial(-preempt)?)$/))
 				exit 1;
 			print flavor;
 		}' "$dir/qemu-cmd")
Signature · 1 line
2.43.0