1 // SPDX-License-Identifier: GPL-2.0
3 * Basic test for sigtrap support.
5 * Copyright (C) 2021, Google LLC.
11 #include <linux/hw_breakpoint.h>
12 #include <linux/string.h>
15 #include <sys/ioctl.h>
16 #include <sys/syscall.h>
23 #include "../perf-sys.h"
26 * PowerPC and S390 do not support creation of instruction breakpoints using the
27 * perf_event interface.
29 * Just disable the test for these architectures until these issues are
32 #if defined(__powerpc__) || defined(__s390x__)
33 #define BP_ACCOUNT_IS_SUPPORTED 0
35 #define BP_ACCOUNT_IS_SUPPORTED 1
41 int tids_want_signal; /* Which threads still want a signal. */
42 int signal_count; /* Sanity check number of signals received. */
43 volatile int iterate_on; /* Variable to set breakpoint on. */
44 siginfo_t first_siginfo; /* First observed siginfo_t. */
47 #define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on))
49 static struct perf_event_attr make_event_attr(void)
51 struct perf_event_attr attr = {
52 .type = PERF_TYPE_BREAKPOINT,
56 .bp_addr = (unsigned long)&ctx.iterate_on,
57 .bp_type = HW_BREAKPOINT_RW,
58 .bp_len = HW_BREAKPOINT_LEN_1,
59 .inherit = 1, /* Children inherit events ... */
60 .inherit_thread = 1, /* ... but only cloned with CLONE_THREAD. */
61 .remove_on_exec = 1, /* Required by sigtrap. */
62 .sigtrap = 1, /* Request synchronous SIGTRAP on event. */
63 .sig_data = TEST_SIG_DATA,
64 .exclude_kernel = 1, /* To allow */
65 .exclude_hv = 1, /* running as !root */
71 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
73 if (!__atomic_fetch_add(&ctx.signal_count, 1, __ATOMIC_RELAXED))
74 ctx.first_siginfo = *info;
75 __atomic_fetch_sub(&ctx.tids_want_signal, syscall(SYS_gettid), __ATOMIC_RELAXED);
78 static void *test_thread(void *arg)
80 pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
81 pid_t tid = syscall(SYS_gettid);
84 pthread_barrier_wait(barrier);
86 __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
87 for (i = 0; i < ctx.iterate_on - 1; i++)
88 __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
93 static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
97 pthread_barrier_wait(barrier);
98 for (i = 0; i < NUM_THREADS; i++)
99 TEST_ASSERT_EQUAL("pthread_join() failed", pthread_join(threads[i], NULL), 0);
104 static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
108 ctx.iterate_on = 3000;
110 TEST_ASSERT_EQUAL("misfired signal?", ctx.signal_count, 0);
111 TEST_ASSERT_EQUAL("enable failed", ioctl(fd, PERF_EVENT_IOC_ENABLE, 0), 0);
112 ret = run_test_threads(threads, barrier);
113 TEST_ASSERT_EQUAL("disable failed", ioctl(fd, PERF_EVENT_IOC_DISABLE, 0), 0);
115 TEST_ASSERT_EQUAL("unexpected sigtraps", ctx.signal_count, NUM_THREADS * ctx.iterate_on);
116 TEST_ASSERT_EQUAL("missing signals or incorrectly delivered", ctx.tids_want_signal, 0);
117 TEST_ASSERT_VAL("unexpected si_addr", ctx.first_siginfo.si_addr == &ctx.iterate_on);
118 #if 0 /* FIXME: enable when libc's signal.h has si_perf_{type,data} */
119 TEST_ASSERT_EQUAL("unexpected si_perf_type", ctx.first_siginfo.si_perf_type,
120 PERF_TYPE_BREAKPOINT);
121 TEST_ASSERT_EQUAL("unexpected si_perf_data", ctx.first_siginfo.si_perf_data,
128 static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
130 struct perf_event_attr attr = make_event_attr();
131 struct sigaction action = {};
132 struct sigaction oldact;
133 pthread_t threads[NUM_THREADS];
134 pthread_barrier_t barrier;
135 char sbuf[STRERR_BUFSIZE];
136 int i, fd, ret = TEST_FAIL;
138 if (!BP_ACCOUNT_IS_SUPPORTED) {
139 pr_debug("Test not supported on this architecture");
143 pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
145 action.sa_flags = SA_SIGINFO | SA_NODEFER;
146 action.sa_sigaction = sigtrap_handler;
147 sigemptyset(&action.sa_mask);
148 if (sigaction(SIGTRAP, &action, &oldact)) {
149 pr_debug("FAILED sigaction(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
153 fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
155 pr_debug("FAILED sys_perf_event_open(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
156 goto out_restore_sigaction;
159 for (i = 0; i < NUM_THREADS; i++) {
160 if (pthread_create(&threads[i], NULL, test_thread, &barrier)) {
161 pr_debug("FAILED pthread_create(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
162 goto out_close_perf_event;
166 ret = run_stress_test(fd, threads, &barrier);
168 out_close_perf_event:
170 out_restore_sigaction:
171 sigaction(SIGTRAP, &oldact, NULL);
173 pthread_barrier_destroy(&barrier);
177 DEFINE_SUITE("Sigtrap", sigtrap);