]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/x86: Consolidate redundant signal helper functions
authorChang S. Bae <chang.seok.bae@intel.com>
Wed, 26 Feb 2025 01:07:21 +0000 (17:07 -0800)
committerIngo Molnar <mingo@kernel.org>
Wed, 26 Feb 2025 12:05:28 +0000 (13:05 +0100)
The x86 selftests frequently register and clean up signal handlers, but
the sethandler() and clearhandler() functions have been redundantly
copied across multiple .c files.

Move these functions to helpers.h to enable reuse across tests,
eliminating around 250 lines of duplicate code.

Converge the error handling by using ksft_exit_fail_msg(), which is
functionally equivalent with err() within the selftest framework.

This change is a prerequisite for the upcoming xstate selftest, which
requires signal handling for registering and cleaning up handlers.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20250226010731.2456-2-chang.seok.bae@intel.com
18 files changed:
tools/testing/selftests/x86/amx.c
tools/testing/selftests/x86/corrupt_xstate_header.c
tools/testing/selftests/x86/entry_from_vm86.c
tools/testing/selftests/x86/fsgsbase.c
tools/testing/selftests/x86/helpers.h
tools/testing/selftests/x86/ioperm.c
tools/testing/selftests/x86/iopl.c
tools/testing/selftests/x86/ldt_gdt.c
tools/testing/selftests/x86/mov_ss_trap.c
tools/testing/selftests/x86/ptrace_syscall.c
tools/testing/selftests/x86/sigaltstack.c
tools/testing/selftests/x86/sigreturn.c
tools/testing/selftests/x86/single_step_syscall.c
tools/testing/selftests/x86/syscall_arg_fault.c
tools/testing/selftests/x86/syscall_nt.c
tools/testing/selftests/x86/sysret_rip.c
tools/testing/selftests/x86/test_vsyscall.c
tools/testing/selftests/x86/unwind_vdso.c

index 1fdf35a4d7f632388cd21fe491036fcd65488620..0f355f331f41c7c26b8cb39b3c1b11754dbaa6bd 100644 (file)
@@ -20,6 +20,7 @@
 #include <sys/uio.h>
 
 #include "../kselftest.h" /* For __cpuid_count() */
+#include "helpers.h"
 
 #ifndef __x86_64__
 # error This test is 64-bit only
@@ -61,30 +62,6 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)
 /* err() exits and will not return */
 #define fatal_error(msg, ...)  err(1, "[FAIL]\t" msg, ##__VA_ARGS__)
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               fatal_error("sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               fatal_error("sigaction");
-}
-
 #define XFEATURE_XTILECFG      17
 #define XFEATURE_XTILEDATA     18
 #define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)
index cf9ce8fbb656cc11997daa83a090be9ecbb53039..93a89a5997ca86d80a4ca65151eb3a5cf93cb198 100644 (file)
@@ -18,6 +18,7 @@
 #include <sys/wait.h>
 
 #include "../kselftest.h" /* For __cpuid_count() */
+#include "helpers.h"
 
 static inline int xsave_enabled(void)
 {
@@ -29,19 +30,6 @@ static inline int xsave_enabled(void)
        return ecx & (1U << 27);
 }
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static void sigusr1(int sig, siginfo_t *info, void *uc_void)
 {
        ucontext_t *uc = uc_void;
index d1e919b0c1dc887606b6f0f63e4c71c5bbc6795c..5cb8393737d056cb13622d694c5d7351ca3711a8 100644 (file)
 #include <errno.h>
 #include <sys/vm86.h>
 
+#include "helpers.h"
+
 static unsigned long load_addr = 0x10000;
 static int nerrs = 0;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static sig_atomic_t got_signal;
 
 static void sighandler(int sig, siginfo_t *info, void *ctx_void)
index 50cf32de631397edc935414be478bf0143993769..0a75252d31b6a879ec530c69159da4d73004e391 100644 (file)
@@ -28,6 +28,8 @@
 #include <sys/wait.h>
 #include <setjmp.h>
 
+#include "helpers.h"
+
 #ifndef __x86_64__
 # error This test is 64-bit only
 #endif
@@ -39,28 +41,6 @@ static unsigned short *shared_scratch;
 
 static int nerrs;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
 {
        ucontext_t *ctx = (ucontext_t*)ctx_void;
index 4ef42c4559a9eb991a96171aaf0d91c487d9943d..6deaad035161a83e17b7d8a5923779f9f37a7f76 100644 (file)
@@ -2,8 +2,13 @@
 #ifndef __SELFTESTS_X86_HELPERS_H
 #define __SELFTESTS_X86_HELPERS_H
 
+#include <signal.h>
+#include <string.h>
+
 #include <asm/processor-flags.h>
 
+#include "../kselftest.h"
+
 static inline unsigned long get_eflags(void)
 {
 #ifdef __x86_64__
@@ -22,4 +27,27 @@ static inline void set_eflags(unsigned long eflags)
 #endif
 }
 
+static inline void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), int flags)
+{
+       struct sigaction sa;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_sigaction = handler;
+       sa.sa_flags = SA_SIGINFO | flags;
+       sigemptyset(&sa.sa_mask);
+       if (sigaction(sig, &sa, 0))
+               ksft_exit_fail_msg("sigaction failed");
+}
+
+static inline void clearhandler(int sig)
+{
+       struct sigaction sa;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = SIG_DFL;
+       sigemptyset(&sa.sa_mask);
+       if (sigaction(sig, &sa, 0))
+               ksft_exit_fail_msg("sigaction failed");
+}
+
 #endif /* __SELFTESTS_X86_HELPERS_H */
index 57ec5e99edb938d932fe81092660d137616cf2ad..69d5fb7050c25edf93b847942b3397a54764093b 100644 (file)
 #include <sched.h>
 #include <sys/io.h>
 
-static int nerrs = 0;
-
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-
-}
+#include "helpers.h"
 
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
+static int nerrs = 0;
 
 static jmp_buf jmpbuf;
 
index 7e3e09c1abac67b94217cc0fc6a0b1987b7e24e5..457b6715542bd7b84fcfd98697f702f92e80dc1a 100644 (file)
 #include <sched.h>
 #include <sys/io.h>
 
-static int nerrs = 0;
-
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-
-}
+#include "helpers.h"
 
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
+static int nerrs = 0;
 
 static jmp_buf jmpbuf;
 
index 3a29346e1452d51cd3cb363d5967841065068286..bb99a71380a5fe1a43303d95b4dcda407633c9f3 100644 (file)
@@ -26,6 +26,8 @@
 #include <asm/prctl.h>
 #include <sys/prctl.h>
 
+#include "helpers.h"
+
 #define AR_ACCESSED            (1<<8)
 
 #define AR_TYPE_RODATA         (0 * (1<<9))
@@ -506,20 +508,6 @@ static void fix_sa_restorer(int sig)
 }
 #endif
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-
-       fix_sa_restorer(sig);
-}
-
 static jmp_buf jmpbuf;
 
 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
@@ -549,9 +537,11 @@ static void do_multicpu_tests(void)
        }
 
        sethandler(SIGSEGV, sigsegv, 0);
+       fix_sa_restorer(SIGSEGV);
 #ifdef __i386__
        /* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
        sethandler(SIGILL, sigsegv, 0);
+       fix_sa_restorer(SIGILL);
 #endif
 
        printf("[RUN]\tCross-CPU LDT invalidation\n");
index cc3de6ff9fba1814d068ee2abc57c86efd8a110d..f22cb6b382f9ce4d47375b16461551d75e70133f 100644 (file)
@@ -36,7 +36,7 @@
 #include <setjmp.h>
 #include <sys/prctl.h>
 
-#define X86_EFLAGS_RF (1UL << 16)
+#include "helpers.h"
 
 #if __x86_64__
 # define REG_IP REG_RIP
@@ -94,18 +94,6 @@ static void enable_watchpoint(void)
        }
 }
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static char const * const signames[] = {
        [SIGSEGV] = "SIGSEGV",
        [SIGBUS] = "SIBGUS",
index 12aaa063196e7406a08188a64570d46ce407ec91..360ec88d5432c8fa403fac41b6bc02ace8479f4a 100644 (file)
@@ -15,6 +15,8 @@
 #include <asm/ptrace-abi.h>
 #include <sys/auxv.h>
 
+#include "helpers.h"
+
 /* Bitness-agnostic defines for user_regs_struct fields. */
 #ifdef __x86_64__
 # define user_syscall_nr       orig_rax
@@ -93,18 +95,6 @@ static siginfo_t wait_trap(pid_t chld)
        return si;
 }
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static void setsigign(int sig, int flags)
 {
        struct sigaction sa;
@@ -116,16 +106,6 @@ static void setsigign(int sig, int flags)
                err(1, "sigaction");
 }
 
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 #ifdef __x86_64__
 # define REG_BP REG_RBP
 #else
index f689af75e979eaa4e86b3944ee2d25a3e496e8cf..0ae1b784498cce6b897a9de060d8ba2848e79420 100644 (file)
@@ -14,6 +14,8 @@
 #include <sys/resource.h>
 #include <setjmp.h>
 
+#include "helpers.h"
+
 /* sigaltstack()-enforced minimum stack */
 #define ENFORCED_MINSIGSTKSZ   2048
 
@@ -27,30 +29,6 @@ static bool sigalrm_expected;
 
 static unsigned long at_minstack_size;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static int setup_altstack(void *start, unsigned long size)
 {
        stack_t ss;
index 0b75b29f794b7127b246d0f2484747d1a90f62eb..26ef562f4232a39fa7ed68828a2b3b3dcdaa7e7c 100644 (file)
@@ -46,6 +46,8 @@
 #include <sys/ptrace.h>
 #include <sys/user.h>
 
+#include "helpers.h"
+
 /* Pull in AR_xyz defines. */
 typedef unsigned int u32;
 typedef unsigned short u16;
@@ -138,28 +140,6 @@ static unsigned short LDT3(int idx)
        return (idx << 3) | 7;
 }
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static void add_ldt(const struct user_desc *desc, unsigned short *var,
                    const char *name)
 {
index 9a30f443e92862631040c321dda42b0de87c0988..280d7a22b9c9b64b75b5d50f98dd02ea555f822f 100644 (file)
 
 #include "helpers.h"
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static volatile sig_atomic_t sig_traps, sig_eflags;
 sigjmp_buf jmpbuf;
 
index 48ab065a76f9bcd92f1037bfb548938419eb0c8a..f67a2df335ba062489e5911e16f87cb5a193e2b1 100644 (file)
 
 #include "helpers.h"
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static sigjmp_buf jmpbuf;
 
 static volatile sig_atomic_t n_errs;
index a108b80dd082309ed24e0c3fd687c3353210c71b..f9c9814160f09fbd47b64d07c5280e22d3547b51 100644 (file)
 
 static unsigned int nerrs;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
 {
 }
index b30de9aaa6d41dfad194e55dd97c6458627a4229..5fb531e3ad7c3b8d1a07fecaccf7241aa4185cbf 100644 (file)
@@ -22,6 +22,8 @@
 #include <sys/mman.h>
 #include <assert.h>
 
+#include "helpers.h"
+
 /*
  * These items are in clang_helpers_64.S, in order to avoid clang inline asm
  * limitations:
@@ -31,28 +33,6 @@ extern const char test_page[];
 
 static void const *current_test_page_addr = test_page;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
-static void clearhandler(int sig)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = SIG_DFL;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 /* State used by our signal handlers. */
 static gregset_t initial_regs;
 
index 6de11b4df4583b54f16b0e3656f67c402a661130..05e1e6774fba322015b05dcdb0731fd8f3805e56 100644 (file)
@@ -310,19 +310,6 @@ static void test_getcpu(int cpu)
 static jmp_buf jmpbuf;
 static volatile unsigned long segv_err;
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               ksft_exit_fail_msg("sigaction failed\n");
-}
-
 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
 {
        ucontext_t *ctx = (ucontext_t *)ctx_void;
index 4c311e1af4c7a850ccf1f1b35d9820d010557281..9cc17588d8189c27b9454dac972798759e64eb78 100644 (file)
@@ -43,18 +43,6 @@ int main()
 #include <dlfcn.h>
 #include <unwind.h>
 
-static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
-                      int flags)
-{
-       struct sigaction sa;
-       memset(&sa, 0, sizeof(sa));
-       sa.sa_sigaction = handler;
-       sa.sa_flags = SA_SIGINFO | flags;
-       sigemptyset(&sa.sa_mask);
-       if (sigaction(sig, &sa, 0))
-               err(1, "sigaction");
-}
-
 static volatile sig_atomic_t nerrs;
 static unsigned long sysinfo;
 static bool got_sysinfo = false;