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"
28 int tids_want_signal; /* Which threads still want a signal. */
29 int signal_count; /* Sanity check number of signals received. */
30 volatile int iterate_on; /* Variable to set breakpoint on. */
31 siginfo_t first_siginfo; /* First observed siginfo_t. */
34 #define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on))
36 static struct perf_event_attr make_event_attr(void)
38 struct perf_event_attr attr = {
39 .type = PERF_TYPE_BREAKPOINT,
43 .bp_addr = (unsigned long)&ctx.iterate_on,
44 .bp_type = HW_BREAKPOINT_RW,
45 .bp_len = HW_BREAKPOINT_LEN_1,
46 .inherit = 1, /* Children inherit events ... */
47 .inherit_thread = 1, /* ... but only cloned with CLONE_THREAD. */
48 .remove_on_exec = 1, /* Required by sigtrap. */
49 .sigtrap = 1, /* Request synchronous SIGTRAP on event. */
50 .sig_data = TEST_SIG_DATA,
51 .exclude_kernel = 1, /* To allow */
52 .exclude_hv = 1, /* running as !root */
60 static bool attr_has_sigtrap(void)
64 const struct btf_type *t;
65 const struct btf_member *m;
69 btf = btf__load_vmlinux_btf();
71 /* should be an old kernel */
75 id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
79 t = btf__type_by_id(btf, id);
80 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
81 name = btf__name_by_offset(btf, m->name_off);
82 if (!strcmp(name, "sigtrap")) {
91 #else /* !HAVE_BPF_SKEL */
92 static bool attr_has_sigtrap(void)
94 struct perf_event_attr attr = {
95 .type = PERF_TYPE_SOFTWARE,
96 .config = PERF_COUNT_SW_DUMMY,
98 .remove_on_exec = 1, /* Required by sigtrap. */
99 .sigtrap = 1, /* Request synchronous SIGTRAP on event. */
104 fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
112 #endif /* HAVE_BPF_SKEL */
115 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
117 if (!__atomic_fetch_add(&ctx.signal_count, 1, __ATOMIC_RELAXED))
118 ctx.first_siginfo = *info;
119 __atomic_fetch_sub(&ctx.tids_want_signal, syscall(SYS_gettid), __ATOMIC_RELAXED);
122 static void *test_thread(void *arg)
124 pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
125 pid_t tid = syscall(SYS_gettid);
128 pthread_barrier_wait(barrier);
130 __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
131 for (i = 0; i < ctx.iterate_on - 1; i++)
132 __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
137 static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
141 pthread_barrier_wait(barrier);
142 for (i = 0; i < NUM_THREADS; i++)
143 TEST_ASSERT_EQUAL("pthread_join() failed", pthread_join(threads[i], NULL), 0);
148 static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
152 ctx.iterate_on = 3000;
154 TEST_ASSERT_EQUAL("misfired signal?", ctx.signal_count, 0);
155 TEST_ASSERT_EQUAL("enable failed", ioctl(fd, PERF_EVENT_IOC_ENABLE, 0), 0);
156 ret = run_test_threads(threads, barrier);
157 TEST_ASSERT_EQUAL("disable failed", ioctl(fd, PERF_EVENT_IOC_DISABLE, 0), 0);
159 TEST_ASSERT_EQUAL("unexpected sigtraps", ctx.signal_count, NUM_THREADS * ctx.iterate_on);
160 TEST_ASSERT_EQUAL("missing signals or incorrectly delivered", ctx.tids_want_signal, 0);
161 TEST_ASSERT_VAL("unexpected si_addr", ctx.first_siginfo.si_addr == &ctx.iterate_on);
162 #if 0 /* FIXME: enable when libc's signal.h has si_perf_{type,data} */
163 TEST_ASSERT_EQUAL("unexpected si_perf_type", ctx.first_siginfo.si_perf_type,
164 PERF_TYPE_BREAKPOINT);
165 TEST_ASSERT_EQUAL("unexpected si_perf_data", ctx.first_siginfo.si_perf_data,
172 static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
174 struct perf_event_attr attr = make_event_attr();
175 struct sigaction action = {};
176 struct sigaction oldact;
177 pthread_t threads[NUM_THREADS];
178 pthread_barrier_t barrier;
179 char sbuf[STRERR_BUFSIZE];
180 int i, fd, ret = TEST_FAIL;
182 if (!BP_SIGNAL_IS_SUPPORTED) {
183 pr_debug("Test not supported on this architecture");
187 pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
189 action.sa_flags = SA_SIGINFO | SA_NODEFER;
190 action.sa_sigaction = sigtrap_handler;
191 sigemptyset(&action.sa_mask);
192 if (sigaction(SIGTRAP, &action, &oldact)) {
193 pr_debug("FAILED sigaction(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
197 fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
199 if (attr_has_sigtrap()) {
200 pr_debug("FAILED sys_perf_event_open(): %s\n",
201 str_error_r(errno, sbuf, sizeof(sbuf)));
203 pr_debug("perf_event_attr doesn't have sigtrap\n");
206 goto out_restore_sigaction;
209 for (i = 0; i < NUM_THREADS; i++) {
210 if (pthread_create(&threads[i], NULL, test_thread, &barrier)) {
211 pr_debug("FAILED pthread_create(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
212 goto out_close_perf_event;
216 ret = run_stress_test(fd, threads, &barrier);
218 out_close_perf_event:
220 out_restore_sigaction:
221 sigaction(SIGTRAP, &oldact, NULL);
223 pthread_barrier_destroy(&barrier);
227 DEFINE_SUITE("Sigtrap", sigtrap);