From e4e3993934a48ec5ed6abfa9faed025df4a713c3 Mon Sep 17 00:00:00 2001 From: BFE Engineering Date: Sun, 15 Nov 2026 01:13:52 -0600 Subject: [PATCH] Add test-only kernel hang module for bench fault F5 [skip ci] A debugfs write to /sys/kernel/debug/warden_hang/trigger turns interrupts off and never returns, so warden-io stops beating and the HPMCU trips the relays and resets the board (d07 F5). The module is patch 95 and is built only when WARDEN_MODULES_COLLECT lists drivers/warden-test (the test flavour): build-kernel.sh enables CONFIG_WARDEN_TEST_HANG for that request, asserts that it survived olddefconfig, and fails when a collected module's vermagic is not this kernel's release. No defconfig or symbol list names the symbol, so the release configuration does not enable it. Covers: F5 (d07), AT-M4-01 and AT-M4-12 (shared F5), R-03, R-16. Tests: tests/test-hang/run-test-hang-tests.sh (offline checks; the configuration checks run with KERNEL_TARBALL and CROSS_COMPILE on the farm). Co-Authored-By: Claude Haiku 5.5 Claude-Session: https://claude.ai/code/session_01B1W6qGNpGNtFT7nhShpH56 --- build/build-kernel.sh | 36 +++++++- patches/95-test-hang-module.patch | 122 +++++++++++++++++++++++++ patches/README.md | 1 + tests/test-hang/run-test-hang-tests.sh | 121 ++++++++++++++++++++++++ 4 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 patches/95-test-hang-module.patch create mode 100755 tests/test-hang/run-test-hang-tests.sh diff --git a/build/build-kernel.sh b/build/build-kernel.sh index bd9d413..c24b082 100755 --- a/build/build-kernel.sh +++ b/build/build-kernel.sh @@ -34,6 +34,10 @@ # `make modules` (the zImage target alone generates no # Module.symvers, so per-dir M= builds cannot link) and # collects the listed dirs' modules into $WORK/modules-out. +# Every collected module must carry this kernel's vermagic +# (include/config/kernel.release), or the build fails. The +# test-only drivers/warden-test directory (bench fault F5) also +# enables CONFIG_WARDEN_TEST_HANG; no other caller lists it. # # Requires: `python` (not python3) on PATH: the SDK quirk; the CI runner provides # a project-local venv. Builds are SERIAL on the shared SDK box: never run two. @@ -133,6 +137,17 @@ if [ -n "${WARDEN_KCONFIG_FRAGMENT:-}" ]; then log "merging kconfig fragment $(basename "$FRAG")" ( cd "$SRC" && ARCH=arm ./scripts/kconfig/merge_config.sh -m .config "$FRAG" ) fi +# Test-only modules. A directory listed in WARDEN_MODULES_COLLECT whose Kconfig +# symbol defaults to n is enabled here, before olddefconfig, so the configuration +# (and KCONFIG_SHA256) follows the request. A release or debug build never lists +# one, so its configuration does not change through this block. +for d in ${WARDEN_MODULES_COLLECT:-}; do + case "$d" in + drivers/warden-test) + ( cd "$SRC" && ./scripts/config --file .config --module WARDEN_TEST_HANG ) + ;; + esac +done # CROSS_COMPILE defaults to the Luckfox SDK uclibc prefix (set SDK_TC to its bin/), # but the kernel is freestanding, so a caller may override with a generic arm cross # toolchain instead: e.g. CROSS_COMPILE=arm-linux-gnueabihf- (in Debian's @@ -143,6 +158,16 @@ if [ -n "${SDK_TC:-}" ]; then export PATH="$SDK_TC:$PATH"; fi command -v "${CROSS_COMPILE}gcc" >/dev/null \ || { echo "cross toolchain ${CROSS_COMPILE}gcc not on PATH (set SDK_TC, or CROSS_COMPILE to one that is)" >&2; exit 1; } make -C "$SRC" ARCH=arm CROSS_COMPILE="$CROSS_COMPILE" olddefconfig >/dev/null +# A requested test module whose symbol did not survive olddefconfig (an unmet +# dependency drops it without a word) fails here, not as a missing .ko later. +for d in ${WARDEN_MODULES_COLLECT:-}; do + case "$d" in + drivers/warden-test) + grep -qx 'CONFIG_WARDEN_TEST_HANG=m' "$SRC/.config" \ + || { echo "FATAL: $d needs CONFIG_WARDEN_TEST_HANG=m, which did not survive olddefconfig" >&2; exit 1; } + ;; + esac +done # Took-effect assertion for every symbol the platform needs. olddefconfig drops # a defconfig line whose dependencies are unmet, or whose symbol does not exist, @@ -223,14 +248,23 @@ if [ -n "${WARDEN_MODULES_COLLECT:-}" ]; then rm -rf "$MODOUT"; mkdir -p "$MODOUT" log "building modules (full set: needed for Module.symvers)" make -C "$SRC" ARCH=arm CROSS_COMPILE="$CROSS_COMPILE" CC="$KCC" -j"$JOBS" modules + # The kernel's own release string; every collected module must carry it. + KREL="$(tr -d '[:space:]' < "$SRC/include/config/kernel.release")" + [ -n "$KREL" ] || { echo "FATAL: include/config/kernel.release is empty" >&2; exit 1; } for d in $WARDEN_MODULES_COLLECT; do [ -d "$SRC/$d" ] || { echo "FATAL: WARDEN_MODULES_COLLECT dir '$d' not in tree" >&2; exit 1; } n=0 while IFS= read -r ko; do cp "$ko" "$MODOUT/"; n=$((n + 1)) + # vermagic is a NUL-separated .modinfo string; one printable run per line. + vm="$(tr -c '[:print:]' '\n' < "$ko" | grep -m1 '^vermagic=' || true)" + case "$vm" in + "vermagic=$KREL "*) ;; + *) echo "FATAL: $(basename "$ko") vermagic '${vm#vermagic=}' is not this kernel's '$KREL'" >&2; exit 1 ;; + esac done < <(find "$SRC/$d" -name '*.ko') [ "$n" -gt 0 ] || { echo "FATAL: no .ko produced under '$d'" >&2; exit 1; } - log "collected $n module(s) from $d" + log "collected $n module(s) from $d (vermagic $KREL)" done ls -la "$MODOUT" fi diff --git a/patches/95-test-hang-module.patch b/patches/95-test-hang-module.patch new file mode 100644 index 0000000..2c8aeeb --- /dev/null +++ b/patches/95-test-hang-module.patch @@ -0,0 +1,122 @@ +diff --git a/drivers/Kconfig b/drivers/Kconfig +index 4915a63..979a6e7 100644 +--- a/drivers/Kconfig ++++ b/drivers/Kconfig +@@ -251,4 +251,6 @@ source "drivers/hte/Kconfig" + + source "drivers/cdx/Kconfig" + ++source "drivers/warden-test/Kconfig" ++ + endmenu +diff --git a/drivers/Makefile b/drivers/Makefile +index 8e1ffa4..e120fc2 100644 +--- a/drivers/Makefile ++++ b/drivers/Makefile +@@ -197,3 +197,6 @@ obj-$(CONFIG_DPLL) += dpll/ + + obj-$(CONFIG_DIBS) += dibs/ + obj-$(CONFIG_S390) += s390/ ++ ++# Test-only modules (bench fault F5): built only when the test flavour enables them. ++obj-$(CONFIG_WARDEN_TEST_HANG) += warden-test/ +diff --git a/drivers/warden-test/Kconfig b/drivers/warden-test/Kconfig +new file mode 100644 +index 0000000..3c6b7d8 +--- /dev/null ++++ b/drivers/warden-test/Kconfig +@@ -0,0 +1,16 @@ ++# SPDX-License-Identifier: GPL-2.0-only ++# ++# Test-only modules for the WardenOS bench. The SDK builds a symbol here only ++# when the caller lists its directory in WARDEN_MODULES_COLLECT (build/build-kernel.sh), ++# which only the test flavour does. Release and debug kernels never enable them. ++ ++config WARDEN_TEST_HANG ++ tristate "Warden test: kernel hang on a debugfs write (bench fault F5)" ++ depends on DEBUG_FS ++ help ++ Test-only. Creates /sys/kernel/debug/warden_hang/trigger. A write to it ++ turns interrupts off and never returns, so warden-io stops beating and ++ the board is reset by its watchdog (d07 fault F5). Never enable this in ++ a release or debug kernel. ++ ++ If unsure, say N. +diff --git a/drivers/warden-test/Makefile b/drivers/warden-test/Makefile +new file mode 100644 +index 0000000..c586de7 +--- /dev/null ++++ b/drivers/warden-test/Makefile +@@ -0,0 +1,4 @@ ++# SPDX-License-Identifier: GPL-2.0-only ++# Test-only modules. Built only when their Kconfig symbol is =m, which build/build-kernel.sh ++# sets for the test flavour (WARDEN_MODULES_COLLECT lists this directory). ++obj-$(CONFIG_WARDEN_TEST_HANG) += warden_hang.o +diff --git a/drivers/warden-test/warden_hang.c b/drivers/warden-test/warden_hang.c +new file mode 100644 +index 0000000..3307532 +--- /dev/null ++++ b/drivers/warden-test/warden_hang.c +@@ -0,0 +1,62 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* ++ * Warden test-only kernel hang (bench fault F5, d07 "Fault Matrix"). ++ * ++ * A write to /sys/kernel/debug/warden_hang/trigger turns interrupts off on the ++ * only core and never returns. Nothing runs after that: warden-io stops ++ * beating, the HPMCU trips the relays once their deadline passes, and the ++ * watchdog resets the board about 90 s after the hang. ++ * ++ * Test flavour only. build/build-kernel.sh enables CONFIG_WARDEN_TEST_HANG ++ * when WARDEN_MODULES_COLLECT lists drivers/warden-test, and the module is ++ * then collected into oem.img with the kernel's vermagic. Nothing loads it on ++ * its own: the bench runner inserts it for the F5 case. ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static struct dentry *hang_dir; ++ ++static void __noreturn warden_hang_spin(void) ++{ ++ local_irq_disable(); ++ for (;;) ++ cpu_relax(); ++} ++ ++static ssize_t trigger_write(struct file *file, const char __user *buf, ++ size_t count, loff_t *ppos) ++{ ++ /* Logged before interrupts go off; the spin below never returns. */ ++ pr_emerg("warden_hang: F5 kernel hang requested, interrupts off\n"); ++ warden_hang_spin(); ++} ++ ++static const struct file_operations trigger_fops = { ++ .owner = THIS_MODULE, ++ .write = trigger_write, ++ .llseek = noop_llseek, ++}; ++ ++static int __init warden_hang_init(void) ++{ ++ hang_dir = debugfs_create_dir("warden_hang", NULL); ++ debugfs_create_file("trigger", 0200, hang_dir, NULL, &trigger_fops); ++ return 0; ++} ++ ++static void __exit warden_hang_exit(void) ++{ ++ debugfs_remove_recursive(hang_dir); ++} ++ ++module_init(warden_hang_init); ++module_exit(warden_hang_exit); ++ ++MODULE_LICENSE("GPL"); ++MODULE_DESCRIPTION("Warden test-only kernel hang (bench fault F5)"); diff --git a/patches/README.md b/patches/README.md index 357a2ca..3e06f73 100644 --- a/patches/README.md +++ b/patches/README.md @@ -31,6 +31,7 @@ with `../build/warden_defconfig` -> `zImage` + `rv1106-warden.dtb`). | `80-misc-thermal-rtc-adc.patch` | tsadc, rtc-rockchip, saradc, trng, otp, gmac, goodix touch | | `82-arch-timer-vdso-phys-counter.patch` | arch timer: keep the arm32 vDSO clock off the virtual counter while the kernel times on the physical one (CNTVOFF left non-zero by the boot firmware, #35); checked by `tests/arch-timer/check-vdso-counter.py` | | `90-kconfig-no-python-symbol.patch` | drops `DRM_MSM_VALIDATE_XML` so the expanded `.config` (and `KCONFIG_SHA256`) does not depend on the host python (#19) | +| `95-test-hang-module.patch` | test-only kernel hang module for bench fault F5 in `drivers/warden-test/` (`CONFIG_WARDEN_TEST_HANG`, default n); enabled only when `WARDEN_MODULES_COLLECT` lists `drivers/warden-test`, never in a release or debug build | ## License diff --git a/tests/test-hang/run-test-hang-tests.sh b/tests/test-hang/run-test-hang-tests.sh new file mode 100755 index 0000000..4cbe797 --- /dev/null +++ b/tests/test-hang/run-test-hang-tests.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash +# Tests for the test-only kernel hang module (bench fault F5): patch 95, +# the WARDEN_MODULES_COLLECT wiring in build/build-kernel.sh, and the vermagic gate. +# +# What a regression here costs: a release kernel that quietly carries the hang +# trigger, or a test image whose module is missing or stale (a hand-copied .ko +# that does not match the kernel). The offline checks always run. The checks +# that expand the kernel configuration need the pinned tarball and a cross +# compiler, so they run only when KERNEL_TARBALL and CROSS_COMPILE name them +# (the build farm does this); otherwise they are skipped with a notice. +set -uo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO="$(cd "$HERE/../.." && pwd)" +TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT +FAIL=0 +ok() { printf '[PASS] %s\n' "$1"; } +bad() { printf '[FAIL] %s\n' "$1"; FAIL=1; } +skip() { printf '[SKIP] %s\n' "$1"; } + +PATCH="$REPO/patches/95-test-hang-module.patch" +BK="$REPO/build/build-kernel.sh" + +# --- offline: the patch and the build wiring ------------------------------- + +if bash -n "$BK"; then ok "build-kernel.sh parses"; else bad "build-kernel.sh does not parse"; fi + +[ -f "$PATCH" ] && ok "patch 95 present" || bad "patch 95 missing" + +for f in drivers/warden-test/Kconfig drivers/warden-test/Makefile drivers/warden-test/warden_hang.c; do + grep -q "^+++ b/$f\$" "$PATCH" && ok "patch 95 adds $f" || bad "patch 95 does not add $f" +done +grep -q '^+source "drivers/warden-test/Kconfig"$' "$PATCH" \ + && ok "drivers/Kconfig sources the test Kconfig" || bad "drivers/Kconfig does not source the test Kconfig" +grep -q '^+obj-$(CONFIG_WARDEN_TEST_HANG) += warden-test/$' "$PATCH" \ + && ok "drivers/Makefile descends into warden-test only under the symbol" \ + || bad "drivers/Makefile does not descend into warden-test under CONFIG_WARDEN_TEST_HANG" +grep -q 'config WARDEN_TEST_HANG' "$PATCH" && grep -q 'depends on DEBUG_FS' "$PATCH" \ + && ok "CONFIG_WARDEN_TEST_HANG is a tristate under DEBUG_FS" \ + || bad "CONFIG_WARDEN_TEST_HANG is missing or not under DEBUG_FS" +grep -q 'default y' "$PATCH" && bad "a test symbol defaults to y in patch 95" \ + || ok "patch 95 sets no default y" + +if grep -q 'WARDEN_TEST_HANG' "$REPO/build/warden_defconfig" "$REPO/build/required-symbols.txt" \ + "$REPO/build/probe-symbols.txt"; then + bad "a defconfig or symbol list names CONFIG_WARDEN_TEST_HANG (release must not)" +else + ok "no defconfig or symbol list names CONFIG_WARDEN_TEST_HANG" +fi + +test "$(grep -c 'drivers/warden-test)' "$BK")" -eq 2 \ + && ok "build-kernel.sh enables the symbol and asserts it, for drivers/warden-test only" \ + || bad "build-kernel.sh does not have exactly the two drivers/warden-test blocks" +grep -q 'is not this kernel' "$BK" && ok "build-kernel.sh checks each module's vermagic" \ + || bad "build-kernel.sh has no vermagic gate" + +# ASCII only in everything this change adds (documentation and comments included). +if LC_ALL=C grep -n -P '[^\x09\x0a\x20-\x7e]' "$BK" "$PATCH" "$REPO/patches/README.md" \ + "$REPO/tests/test-hang/run-test-hang-tests.sh" >/dev/null; then + bad "non-ASCII byte in a file this change touches" +else + ok "ASCII only in the files this change touches" +fi + +# --- configuration expansion: needs the tarball and a cross compiler --------- + +if [ -z "${KERNEL_TARBALL:-}" ]; then + skip "configuration checks: KERNEL_TARBALL not set (run on the build farm)" +elif [ ! -f "$KERNEL_TARBALL" ] && ! bash "$REPO/build/fetch-kernel-tarball.sh" "$KERNEL_TARBALL" >"$TMP/fetch.log" 2>&1; then + bad "pinned kernel tarball could not be fetched into $KERNEL_TARBALL (see $TMP/fetch.log)" +elif [ -z "${CROSS_COMPILE:-}" ] || ! command -v "${CROSS_COMPILE}gcc" >/dev/null 2>&1; then + skip "configuration checks: no cross compiler (set CROSS_COMPILE)" +elif ! command -v python >/dev/null 2>&1; then + skip "configuration checks: no python on PATH (the SDK quirk)" +else + # Patches 05 and 95 both touch the two hook files; they must apply in order. + mkdir -p "$TMP/hooks" + tar -C "$TMP/hooks" -xJf "$KERNEL_TARBALL" linux-6.18.46/drivers/Kconfig linux-6.18.46/drivers/Makefile \ + && cd "$TMP/hooks/linux-6.18.46" \ + && git init -q . && git add -A && git -c user.name=t -c user.email=t@local commit -q -m base \ + && git apply --include='drivers/Kconfig' --include='drivers/Makefile' "$REPO/patches/05-build-wiring.patch" \ + && git apply --whitespace=nowarn "$PATCH" \ + && grep -q '^source "drivers/warden-test/Kconfig"$' drivers/Kconfig \ + && grep -q '^obj-$(CONFIG_WARDEN_TEST_HANG) += warden-test/$' drivers/Makefile \ + && ok "patch 95 applies after patch 05 on pristine 6.18.46" \ + || bad "patch 95 does not apply after patch 05 on pristine 6.18.46" + cd "$HERE" || exit 1 + + # Release: the symbol stays unset. Test: it is =m only when the directory is listed. + cfg() { # : expand the configuration only + local out="$1"; shift + env -u WARDEN_KCONFIG_FRAGMENT -u WARDEN_MODULES_COLLECT "$@" \ + WORK="$TMP/work-$(basename "$out" .config)" CONFIG_OUT="$out" WARDEN_CONFIG_ONLY=1 \ + KERNEL_TARBALL="$KERNEL_TARBALL" CROSS_COMPILE="$CROSS_COMPILE" \ + bash "$BK" >"$out.log" 2>&1 + } + if cfg "$TMP/release.config"; then + if grep -q '^CONFIG_WARDEN_TEST_HANG=' "$TMP/release.config"; then + bad "release configuration enables CONFIG_WARDEN_TEST_HANG" + else + ok "release configuration leaves CONFIG_WARDEN_TEST_HANG out" + fi + else + bad "release configuration expansion failed (see $TMP/release.config.log)" + fi + if cfg "$TMP/test.config" WARDEN_MODULES_COLLECT=drivers/warden-test; then + grep -qx 'CONFIG_WARDEN_TEST_HANG=m' "$TMP/test.config" \ + && ok "test configuration sets CONFIG_WARDEN_TEST_HANG=m" \ + || bad "test configuration does not set CONFIG_WARDEN_TEST_HANG=m" + if [ -f "$TMP/release.config" ] && [ "$(sha256sum < "$TMP/test.config")" != "$(sha256sum < "$TMP/release.config")" ]; then + ok "test and release configurations differ (KCONFIG_SHA256 follows the request)" + else + bad "test configuration equals the release configuration" + fi + else + bad "test configuration expansion failed (see $TMP/test.config.log)" + fi +fi + +if [ "$FAIL" -eq 0 ]; then echo "test-hang: all checks passed"; else echo "test-hang: FAILED"; fi +exit "$FAIL"