// SPDX-License-Identifier: GPL-2.0 /* * nmi_stall.c - generic kprobe controlled-window staller for the * kernel/irq NMI verification (2026-11-09), following the r4_stall * methodology (controlled window; does not measure natural hit rates). * * Params: * symbol= function to probe (required) * off= kprobe offset inside symbol (instruction offset, hex ok) * stall_us= max busy-wait duration once armed+hit (default 40s) * arm= sysfs gate (0600): stall only while armed * release= sysfs gate (0600): end the stall * * The pre_handler stalls the FIRST armed hit until release=1 (or timeout). * Later hits are counted only. */ #define pr_fmt(fmt) "NMI1STALL: " fmt #include #include #include #include #include #include static char *symbol = ""; module_param(symbol, charp, 0400); MODULE_PARM_DESC(symbol, "kprobe target symbol"); static unsigned long off; module_param(off, ulong, 0400); MODULE_PARM_DESC(off, "kprobe offset inside symbol"); static int stall_us = 40000000; module_param(stall_us, int, 0600); MODULE_PARM_DESC(stall_us, "max stall duration in us"); static int arm; module_param(arm, int, 0600); MODULE_PARM_DESC(arm, "1 = stall next hit"); static int release; module_param(release, int, 0600); MODULE_PARM_DESC(release, "1 = end the stall"); static atomic_t hits = ATOMIC_INIT(0); static bool consumed; static int stall_pre(struct kprobe *p, struct pt_regs *regs) { int h = atomic_inc_return(&hits); if (!READ_ONCE(arm) || consumed) { if (h <= 5) pr_warn("hit #%d (not stalled, arm=%d)\n", h, READ_ONCE(arm)); return 0; } WRITE_ONCE(consumed, true); pr_warn("STALL HIT at %s+0x%lx stall_us=%d\n", p->symbol_name, off, stall_us); /* hard deadline so a dead orchestrator cannot hang the VM forever */ for (int waited = 0; waited < stall_us && !READ_ONCE(release); waited += 1000) udelay(1000); pr_warn("RELEASED (release=%d waited_hits=%d)\n", READ_ONCE(release), atomic_read(&hits)); return 0; } static struct kprobe kp; static int __init nmi_stall_init(void) { if (!symbol[0]) { pr_err("no symbol given\n"); return -EINVAL; } kp.symbol_name = symbol; kp.offset = off; kp.pre_handler = stall_pre; if (register_kprobe(&kp)) { pr_err("cannot register kprobe on %s+0x%lx\n", symbol, off); return -EINVAL; } pr_warn("armed-able: %s+0x%lx stall_us=%d (waiting for arm=1)\n", symbol, off, stall_us); return 0; } static void __exit nmi_stall_exit(void) { unregister_kprobe(&kp); pr_warn("removed hits=%d stalled=%d\n", atomic_read(&hits), consumed); } module_init(nmi_stall_init); module_exit(nmi_stall_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("generic kprobe stall for kernel/irq NMI verification");