[PATCH 12/17] rcutorture: Check NMI coverage

From: Kunwu Chan

Date: Tue Dec 01 2026 - 11:22:46 EST


Periodic statistics use printk continuations and can split across
console lines. Emit a single NMI coverage summary after the perf
events and statistics thread stop and the callback barrier completes.

Require one valid summary followed by SUCCESS from the same flavor.
Return 0 if the requested coverage is present or 4 if it is absent.
Return 1 if the record is invalid or missing, or the run failed.

Signed-off-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
---
kernel/rcu/rcutorture.c | 22 ++++++
.../rcutorture/bin/kvm-recheck-nmi.sh | 72 +++++++++++++++++++
2 files changed, 94 insertions(+)
create mode 100755 tools/testing/selftests/rcutorture/bin/kvm-recheck-nmi.sh

diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
index 8905abe8725e..60c2f4cdccc6 100644
--- a/kernel/rcu/rcutorture.c
+++ b/kernel/rcu/rcutorture.c
@@ -3468,6 +3468,27 @@ rcu_torture_stats_print(void)
writer_gp_snap = writer_gp;
}

+/* Print one complete record after the NMI sources stop and callbacks drain. */
+static void rcu_torture_nmi_coverage_print(void)
+{
+ unsigned long count, samples = 0;
+ unsigned int too_old = 0;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(rcu_torture_nmi_pipe); i++) {
+ count = atomic_long_read(&rcu_torture_nmi_pipe[i]);
+ samples += count;
+ if (i > 1 && count)
+ too_old = 1;
+ }
+ pr_alert("%s%s NMI coverage: reads=%lu samples=%lu too-old=%u calls=%lu callbacks=%lu\n",
+ torture_type, TORTURE_FLAG,
+ (unsigned long)atomic_long_read(&n_rcu_torture_nmi_read),
+ samples, too_old,
+ (unsigned long)atomic_long_read(&n_rcu_torture_nmi_call),
+ (unsigned long)atomic_long_read(&n_rcu_torture_nmi_cb));
+}
+
/*
* Periodically prints torture statistics, if periodic statistics printing
* was specified via the stat_interval module parameter.
@@ -4730,6 +4751,7 @@ rcu_torture_cleanup(void)
rcu_torture_mem_dump_obj();

rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
+ rcu_torture_nmi_coverage_print();

if (err_segs_recorded) {
pr_alert("Failure/close-call rcutorture reader segments:\n");
diff --git a/tools/testing/selftests/rcutorture/bin/kvm-recheck-nmi.sh b/tools/testing/selftests/rcutorture/bin/kvm-recheck-nmi.sh
new file mode 100755
index 000000000000..8a0e2fbc0660
--- /dev/null
+++ b/tools/testing/selftests/rcutorture/bin/kvm-recheck-nmi.sh
@@ -0,0 +1,72 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Usage: kvm-recheck-nmi.sh resdir [readers|calls|both]
+# Exit codes are 0 for coverage, 1 for failure, and 4 for untested.
+#
+# Author: Kunwu Chan <kunwu.chan@xxxxxxxxx>
+
+resdir="${1-}"
+mode="${2-both}"
+case "$mode" in
+readers|calls|both) ;;
+*) echo "Usage: $0 resdir [readers|calls|both]" >&2; exit 1 ;;
+esac
+if test -z "$resdir" || test ! -f "$resdir/console.log" ||
+ test ! -r "$resdir/console.log"
+then
+ echo "FAIL: unreadable console.log in $resdir" >&2
+ exit 1
+fi
+
+LC_ALL=C awk -v mode="$mode" '
+function fail(message) { print "FAIL: " message; exit 1 }
+function skip(message) { print "SKIP: " message; exit 4 }
+{
+ sub(/\r$/, "")
+ sub(/^\[[[:space:]]*[0-9]+\.[0-9]+\][[:space:]]*/, "")
+ sub(/^\[[[:space:]]*[CT][0-9]+\][[:space:]]*/, "")
+}
+/printk messages dropped/ { dropped = 1 }
+/^[^[:space:]]+-torture:[[:space:]]+NMI coverage:([[:space:]]|$)/ {
+ if (++summaries != 1 || results || NF != 8 ||
+ $4 !~ /^reads=(0|[1-9][0-9]*)$/ ||
+ $5 !~ /^samples=(0|[1-9][0-9]*)$/ ||
+ $6 !~ /^too-old=[01]$/ ||
+ $7 !~ /^calls=(0|[1-9][0-9]*)$/ ||
+ $8 !~ /^callbacks=(0|[1-9][0-9]*)$/) {
+ malformed = 1
+ next
+ }
+ flavor = $1
+ reads = substr($4, 7)
+ samples = substr($5, 9)
+ too_old = ($6 == "too-old=1")
+ calls = substr($7, 7)
+ callbacks = substr($8, 11)
+}
+/^[^[:space:]]+-torture:(---)?[[:space:]]+End of test:/ {
+ result_flavor = $1
+ sub(/---$/, "", result_flavor)
+ if (++results != 1 || result_flavor != flavor || $5 !~ /^SUCCESS:?$/)
+ bad_result = 1
+}
+END {
+ if (dropped) fail("console reports dropped printk messages")
+ if (!summaries) fail("missing final NMI coverage summary")
+ if (malformed) fail("invalid, duplicate or misplaced NMI coverage summary")
+ if (bad_result || !results) fail("missing matching successful test completion")
+ if (too_old) fail("NMI reader observed a too-old object")
+ # Compare decimal strings exactly, including counts beyond awk precision.
+ if (length(samples) > length(reads) ||
+ (length(samples) == length(reads) && "n" samples > "n" reads))
+ fail("NMI object samples exceed reader count")
+ if (mode != "readers" && "n" calls != "n" callbacks)
+ fail("NMI callback count differs after cleanup")
+ if (mode != "calls" && samples == "0")
+ skip("no protected object sampled from NMI")
+ if (mode != "readers" && calls == "0")
+ skip("no callback submitted from NMI")
+ printf "PASS: NMI reads=%s object-samples=%s calls=%s callbacks=%s\n", \
+ reads, samples, calls, callbacks
+}' < "$resdir/console.log"
--
2.43.0