]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Report stacktraces SIGBUS, SIGSEGV, SIGILL, and SIGFPE by default
authorSean Christopherson <seanjc@google.com>
Thu, 16 Oct 2025 17:28:48 +0000 (10:28 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 20 Oct 2025 13:30:42 +0000 (06:30 -0700)
Register handlers for signals for all selftests that are likely happen due
to test (or kernel) bugs, and explicitly fail tests on unexpected signals
so that users get a stack trace, i.e. don't have to go spelunking to do
basic triage.

Register the handlers as early as possible, to catch as many unexpected
signals as possible, and also so that the common code doesn't clobber a
handler that's installed by test (or arch) code.

Tested-by: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Shivank Garg <shivankg@amd.com>
Tested-by: Shivank Garg <shivankg@amd.com>
Link: https://lore.kernel.org/r/20251016172853.52451-8-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/lib/kvm_util.c

index 203e33697492e3ec6f08e877628c4e279ddca95c..5744643d9ec3b5b71b786fcee3bafed8922985ac 100644 (file)
@@ -2290,11 +2290,35 @@ __weak void kvm_selftest_arch_init(void)
 {
 }
 
+static void report_unexpected_signal(int signum)
+{
+#define KVM_CASE_SIGNUM(sig)                                   \
+       case sig: TEST_FAIL("Unexpected " #sig " (%d)\n", signum)
+
+       switch (signum) {
+       KVM_CASE_SIGNUM(SIGBUS);
+       KVM_CASE_SIGNUM(SIGSEGV);
+       KVM_CASE_SIGNUM(SIGILL);
+       KVM_CASE_SIGNUM(SIGFPE);
+       default:
+               TEST_FAIL("Unexpected signal %d\n", signum);
+       }
+}
+
 void __attribute((constructor)) kvm_selftest_init(void)
 {
+       struct sigaction sig_sa = {
+               .sa_handler = report_unexpected_signal,
+       };
+
        /* Tell stdout not to buffer its content. */
        setbuf(stdout, NULL);
 
+       sigaction(SIGBUS, &sig_sa, NULL);
+       sigaction(SIGSEGV, &sig_sa, NULL);
+       sigaction(SIGILL, &sig_sa, NULL);
+       sigaction(SIGFPE, &sig_sa, NULL);
+
        guest_random_seed = last_guest_seed = random();
        pr_info("Random seed: 0x%x\n", guest_random_seed);