Skip to content

Commit dbd6b64

Browse files
ChangSeokBaeIngo Molnar
authored andcommitted
selftests/x86: Consolidate redundant signal helper functions
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 <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ac3144f commit dbd6b64

18 files changed

+51
-315
lines changed

tools/testing/selftests/x86/amx.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sys/uio.h>
2121

2222
#include "../kselftest.h" /* For __cpuid_count() */
23+
#include "helpers.h"
2324

2425
#ifndef __x86_64__
2526
# error This test is 64-bit only
@@ -61,30 +62,6 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm)
6162
/* err() exits and will not return */
6263
#define fatal_error(msg, ...) err(1, "[FAIL]\t" msg, ##__VA_ARGS__)
6364

64-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
65-
int flags)
66-
{
67-
struct sigaction sa;
68-
69-
memset(&sa, 0, sizeof(sa));
70-
sa.sa_sigaction = handler;
71-
sa.sa_flags = SA_SIGINFO | flags;
72-
sigemptyset(&sa.sa_mask);
73-
if (sigaction(sig, &sa, 0))
74-
fatal_error("sigaction");
75-
}
76-
77-
static void clearhandler(int sig)
78-
{
79-
struct sigaction sa;
80-
81-
memset(&sa, 0, sizeof(sa));
82-
sa.sa_handler = SIG_DFL;
83-
sigemptyset(&sa.sa_mask);
84-
if (sigaction(sig, &sa, 0))
85-
fatal_error("sigaction");
86-
}
87-
8865
#define XFEATURE_XTILECFG 17
8966
#define XFEATURE_XTILEDATA 18
9067
#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG)

tools/testing/selftests/x86/corrupt_xstate_header.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <sys/wait.h>
1919

2020
#include "../kselftest.h" /* For __cpuid_count() */
21+
#include "helpers.h"
2122

2223
static inline int xsave_enabled(void)
2324
{
@@ -29,19 +30,6 @@ static inline int xsave_enabled(void)
2930
return ecx & (1U << 27);
3031
}
3132

32-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
33-
int flags)
34-
{
35-
struct sigaction sa;
36-
37-
memset(&sa, 0, sizeof(sa));
38-
sa.sa_sigaction = handler;
39-
sa.sa_flags = SA_SIGINFO | flags;
40-
sigemptyset(&sa.sa_mask);
41-
if (sigaction(sig, &sa, 0))
42-
err(1, "sigaction");
43-
}
44-
4533
static void sigusr1(int sig, siginfo_t *info, void *uc_void)
4634
{
4735
ucontext_t *uc = uc_void;

tools/testing/selftests/x86/entry_from_vm86.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,11 @@
2424
#include <errno.h>
2525
#include <sys/vm86.h>
2626

27+
#include "helpers.h"
28+
2729
static unsigned long load_addr = 0x10000;
2830
static int nerrs = 0;
2931

30-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
31-
int flags)
32-
{
33-
struct sigaction sa;
34-
memset(&sa, 0, sizeof(sa));
35-
sa.sa_sigaction = handler;
36-
sa.sa_flags = SA_SIGINFO | flags;
37-
sigemptyset(&sa.sa_mask);
38-
if (sigaction(sig, &sa, 0))
39-
err(1, "sigaction");
40-
}
41-
42-
static void clearhandler(int sig)
43-
{
44-
struct sigaction sa;
45-
memset(&sa, 0, sizeof(sa));
46-
sa.sa_handler = SIG_DFL;
47-
sigemptyset(&sa.sa_mask);
48-
if (sigaction(sig, &sa, 0))
49-
err(1, "sigaction");
50-
}
51-
5232
static sig_atomic_t got_signal;
5333

5434
static void sighandler(int sig, siginfo_t *info, void *ctx_void)

tools/testing/selftests/x86/fsgsbase.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <sys/wait.h>
2929
#include <setjmp.h>
3030

31+
#include "helpers.h"
32+
3133
#ifndef __x86_64__
3234
# error This test is 64-bit only
3335
#endif
@@ -39,28 +41,6 @@ static unsigned short *shared_scratch;
3941

4042
static int nerrs;
4143

42-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
43-
int flags)
44-
{
45-
struct sigaction sa;
46-
memset(&sa, 0, sizeof(sa));
47-
sa.sa_sigaction = handler;
48-
sa.sa_flags = SA_SIGINFO | flags;
49-
sigemptyset(&sa.sa_mask);
50-
if (sigaction(sig, &sa, 0))
51-
err(1, "sigaction");
52-
}
53-
54-
static void clearhandler(int sig)
55-
{
56-
struct sigaction sa;
57-
memset(&sa, 0, sizeof(sa));
58-
sa.sa_handler = SIG_DFL;
59-
sigemptyset(&sa.sa_mask);
60-
if (sigaction(sig, &sa, 0))
61-
err(1, "sigaction");
62-
}
63-
6444
static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
6545
{
6646
ucontext_t *ctx = (ucontext_t*)ctx_void;

tools/testing/selftests/x86/helpers.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
#ifndef __SELFTESTS_X86_HELPERS_H
33
#define __SELFTESTS_X86_HELPERS_H
44

5+
#include <signal.h>
6+
#include <string.h>
7+
58
#include <asm/processor-flags.h>
69

10+
#include "../kselftest.h"
11+
712
static inline unsigned long get_eflags(void)
813
{
914
#ifdef __x86_64__
@@ -22,4 +27,27 @@ static inline void set_eflags(unsigned long eflags)
2227
#endif
2328
}
2429

30+
static inline void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), int flags)
31+
{
32+
struct sigaction sa;
33+
34+
memset(&sa, 0, sizeof(sa));
35+
sa.sa_sigaction = handler;
36+
sa.sa_flags = SA_SIGINFO | flags;
37+
sigemptyset(&sa.sa_mask);
38+
if (sigaction(sig, &sa, 0))
39+
ksft_exit_fail_msg("sigaction failed");
40+
}
41+
42+
static inline void clearhandler(int sig)
43+
{
44+
struct sigaction sa;
45+
46+
memset(&sa, 0, sizeof(sa));
47+
sa.sa_handler = SIG_DFL;
48+
sigemptyset(&sa.sa_mask);
49+
if (sigaction(sig, &sa, 0))
50+
ksft_exit_fail_msg("sigaction failed");
51+
}
52+
2553
#endif /* __SELFTESTS_X86_HELPERS_H */

tools/testing/selftests/x86/ioperm.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,9 @@
2020
#include <sched.h>
2121
#include <sys/io.h>
2222

23-
static int nerrs = 0;
24-
25-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
26-
int flags)
27-
{
28-
struct sigaction sa;
29-
memset(&sa, 0, sizeof(sa));
30-
sa.sa_sigaction = handler;
31-
sa.sa_flags = SA_SIGINFO | flags;
32-
sigemptyset(&sa.sa_mask);
33-
if (sigaction(sig, &sa, 0))
34-
err(1, "sigaction");
35-
36-
}
23+
#include "helpers.h"
3724

38-
static void clearhandler(int sig)
39-
{
40-
struct sigaction sa;
41-
memset(&sa, 0, sizeof(sa));
42-
sa.sa_handler = SIG_DFL;
43-
sigemptyset(&sa.sa_mask);
44-
if (sigaction(sig, &sa, 0))
45-
err(1, "sigaction");
46-
}
25+
static int nerrs = 0;
4726

4827
static jmp_buf jmpbuf;
4928

tools/testing/selftests/x86/iopl.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,9 @@
2020
#include <sched.h>
2121
#include <sys/io.h>
2222

23-
static int nerrs = 0;
24-
25-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
26-
int flags)
27-
{
28-
struct sigaction sa;
29-
memset(&sa, 0, sizeof(sa));
30-
sa.sa_sigaction = handler;
31-
sa.sa_flags = SA_SIGINFO | flags;
32-
sigemptyset(&sa.sa_mask);
33-
if (sigaction(sig, &sa, 0))
34-
err(1, "sigaction");
35-
36-
}
23+
#include "helpers.h"
3724

38-
static void clearhandler(int sig)
39-
{
40-
struct sigaction sa;
41-
memset(&sa, 0, sizeof(sa));
42-
sa.sa_handler = SIG_DFL;
43-
sigemptyset(&sa.sa_mask);
44-
if (sigaction(sig, &sa, 0))
45-
err(1, "sigaction");
46-
}
25+
static int nerrs = 0;
4726

4827
static jmp_buf jmpbuf;
4928

tools/testing/selftests/x86/ldt_gdt.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <asm/prctl.h>
2727
#include <sys/prctl.h>
2828

29+
#include "helpers.h"
30+
2931
#define AR_ACCESSED (1<<8)
3032

3133
#define AR_TYPE_RODATA (0 * (1<<9))
@@ -506,20 +508,6 @@ static void fix_sa_restorer(int sig)
506508
}
507509
#endif
508510

509-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
510-
int flags)
511-
{
512-
struct sigaction sa;
513-
memset(&sa, 0, sizeof(sa));
514-
sa.sa_sigaction = handler;
515-
sa.sa_flags = SA_SIGINFO | flags;
516-
sigemptyset(&sa.sa_mask);
517-
if (sigaction(sig, &sa, 0))
518-
err(1, "sigaction");
519-
520-
fix_sa_restorer(sig);
521-
}
522-
523511
static jmp_buf jmpbuf;
524512

525513
static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
@@ -549,9 +537,11 @@ static void do_multicpu_tests(void)
549537
}
550538

551539
sethandler(SIGSEGV, sigsegv, 0);
540+
fix_sa_restorer(SIGSEGV);
552541
#ifdef __i386__
553542
/* True 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
554543
sethandler(SIGILL, sigsegv, 0);
544+
fix_sa_restorer(SIGILL);
555545
#endif
556546

557547
printf("[RUN]\tCross-CPU LDT invalidation\n");

tools/testing/selftests/x86/mov_ss_trap.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <setjmp.h>
3737
#include <sys/prctl.h>
3838

39-
#define X86_EFLAGS_RF (1UL << 16)
39+
#include "helpers.h"
4040

4141
#if __x86_64__
4242
# define REG_IP REG_RIP
@@ -94,18 +94,6 @@ static void enable_watchpoint(void)
9494
}
9595
}
9696

97-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
98-
int flags)
99-
{
100-
struct sigaction sa;
101-
memset(&sa, 0, sizeof(sa));
102-
sa.sa_sigaction = handler;
103-
sa.sa_flags = SA_SIGINFO | flags;
104-
sigemptyset(&sa.sa_mask);
105-
if (sigaction(sig, &sa, 0))
106-
err(1, "sigaction");
107-
}
108-
10997
static char const * const signames[] = {
11098
[SIGSEGV] = "SIGSEGV",
11199
[SIGBUS] = "SIBGUS",

tools/testing/selftests/x86/ptrace_syscall.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <asm/ptrace-abi.h>
1616
#include <sys/auxv.h>
1717

18+
#include "helpers.h"
19+
1820
/* Bitness-agnostic defines for user_regs_struct fields. */
1921
#ifdef __x86_64__
2022
# define user_syscall_nr orig_rax
@@ -93,18 +95,6 @@ static siginfo_t wait_trap(pid_t chld)
9395
return si;
9496
}
9597

96-
static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
97-
int flags)
98-
{
99-
struct sigaction sa;
100-
memset(&sa, 0, sizeof(sa));
101-
sa.sa_sigaction = handler;
102-
sa.sa_flags = SA_SIGINFO | flags;
103-
sigemptyset(&sa.sa_mask);
104-
if (sigaction(sig, &sa, 0))
105-
err(1, "sigaction");
106-
}
107-
10898
static void setsigign(int sig, int flags)
10999
{
110100
struct sigaction sa;
@@ -116,16 +106,6 @@ static void setsigign(int sig, int flags)
116106
err(1, "sigaction");
117107
}
118108

119-
static void clearhandler(int sig)
120-
{
121-
struct sigaction sa;
122-
memset(&sa, 0, sizeof(sa));
123-
sa.sa_handler = SIG_DFL;
124-
sigemptyset(&sa.sa_mask);
125-
if (sigaction(sig, &sa, 0))
126-
err(1, "sigaction");
127-
}
128-
129109
#ifdef __x86_64__
130110
# define REG_BP REG_RBP
131111
#else

0 commit comments

Comments
 (0)