xref: /linux/tools/perf/tests/sigtrap.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Basic test for sigtrap support.
4  *
5  * Copyright (C) 2021, Google LLC.
6  */
7 
8 #include <errno.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <linux/hw_breakpoint.h>
12 #include <linux/string.h>
13 #include <pthread.h>
14 #include <signal.h>
15 #include <sys/ioctl.h>
16 #include <sys/syscall.h>
17 #include <unistd.h>
18 
19 #include "cloexec.h"
20 #include "debug.h"
21 #include "event.h"
22 #include "tests.h"
23 #include "../perf-sys.h"
24 
25 #define NUM_THREADS 5
26 
27 static struct {
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. */
32 } ctx;
33 
34 #define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on))
35 
36 static struct perf_event_attr make_event_attr(void)
37 {
38 	struct perf_event_attr attr = {
39 		.type		= PERF_TYPE_BREAKPOINT,
40 		.size		= sizeof(attr),
41 		.sample_period	= 1,
42 		.disabled	= 1,
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 */
53 	};
54 	return attr;
55 }
56 
57 #ifdef HAVE_BPF_SKEL
58 #include <bpf/btf.h>
59 
60 static struct btf *btf;
61 
62 static bool btf__available(void)
63 {
64 	if (btf == NULL)
65 		btf = btf__load_vmlinux_btf();
66 
67 	return btf != NULL;
68 }
69 
70 static void btf__exit(void)
71 {
72 	btf__free(btf);
73 	btf = NULL;
74 }
75 
76 static const struct btf_member *__btf_type__find_member_by_name(int type_id, const char *member_name)
77 {
78 	const struct btf_type *t = btf__type_by_id(btf, type_id);
79 	const struct btf_member *m;
80 	int i;
81 
82 	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
83 		const char *current_member_name = btf__name_by_offset(btf, m->name_off);
84 		if (!strcmp(current_member_name, member_name))
85 			return m;
86 	}
87 
88 	return NULL;
89 }
90 
91 static bool attr_has_sigtrap(void)
92 {
93 	int id;
94 
95 	if (!btf__available()) {
96 		/* should be an old kernel */
97 		return false;
98 	}
99 
100 	id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
101 	if (id < 0)
102 		return false;
103 
104 	return __btf_type__find_member_by_name(id, "sigtrap") != NULL;
105 }
106 
107 static bool kernel_with_sleepable_spinlocks(void)
108 {
109 	const struct btf_member *member;
110 	const struct btf_type *type;
111 	const char *type_name;
112 	int id;
113 
114 	if (!btf__available())
115 		return false;
116 
117 	id = btf__find_by_name_kind(btf, "spinlock", BTF_KIND_STRUCT);
118 	if (id < 0)
119 		return false;
120 
121 	// Only RT has a "lock" member for "struct spinlock"
122 	member = __btf_type__find_member_by_name(id, "lock");
123 	if (member == NULL)
124 		return false;
125 
126 	// But check its type as well
127 	type = btf__type_by_id(btf, member->type);
128 	if (!type || !btf_is_struct(type))
129 		return false;
130 
131 	type_name = btf__name_by_offset(btf, type->name_off);
132 	return type_name && !strcmp(type_name, "rt_mutex_base");
133 }
134 #else  /* !HAVE_BPF_SKEL */
135 static bool attr_has_sigtrap(void)
136 {
137 	struct perf_event_attr attr = {
138 		.type		= PERF_TYPE_SOFTWARE,
139 		.config		= PERF_COUNT_SW_DUMMY,
140 		.size		= sizeof(attr),
141 		.remove_on_exec = 1, /* Required by sigtrap. */
142 		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
143 	};
144 	int fd;
145 	bool ret = false;
146 
147 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
148 	if (fd >= 0) {
149 		ret = true;
150 		close(fd);
151 	}
152 
153 	return ret;
154 }
155 
156 static bool kernel_with_sleepable_spinlocks(void)
157 {
158 	return false;
159 }
160 
161 static void btf__exit(void)
162 {
163 }
164 #endif  /* HAVE_BPF_SKEL */
165 
166 static void
167 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
168 {
169 	if (!__atomic_fetch_add(&ctx.signal_count, 1, __ATOMIC_RELAXED))
170 		ctx.first_siginfo = *info;
171 	__atomic_fetch_sub(&ctx.tids_want_signal, syscall(SYS_gettid), __ATOMIC_RELAXED);
172 }
173 
174 static void *test_thread(void *arg)
175 {
176 	pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
177 	pid_t tid = syscall(SYS_gettid);
178 	int i;
179 
180 	pthread_barrier_wait(barrier);
181 
182 	__atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
183 	for (i = 0; i < ctx.iterate_on - 1; i++)
184 		__atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
185 
186 	return NULL;
187 }
188 
189 static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
190 {
191 	int i;
192 
193 	pthread_barrier_wait(barrier);
194 	for (i = 0; i < NUM_THREADS; i++)
195 		TEST_ASSERT_EQUAL("pthread_join() failed", pthread_join(threads[i], NULL), 0);
196 
197 	return TEST_OK;
198 }
199 
200 static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
201 {
202 	int ret, expected_sigtraps;
203 
204 	ctx.iterate_on = 3000;
205 
206 	TEST_ASSERT_EQUAL("misfired signal?", ctx.signal_count, 0);
207 	TEST_ASSERT_EQUAL("enable failed", ioctl(fd, PERF_EVENT_IOC_ENABLE, 0), 0);
208 	ret = run_test_threads(threads, barrier);
209 	TEST_ASSERT_EQUAL("disable failed", ioctl(fd, PERF_EVENT_IOC_DISABLE, 0), 0);
210 
211 	expected_sigtraps = NUM_THREADS * ctx.iterate_on;
212 
213 	if (ctx.signal_count < expected_sigtraps && kernel_with_sleepable_spinlocks()) {
214 		pr_debug("Expected %d sigtraps, got %d, running on a kernel with sleepable spinlocks.\n",
215 			 expected_sigtraps, ctx.signal_count);
216 		pr_debug("See https://lore.kernel.org/all/e368f2c848d77fbc8d259f44e2055fe469c219cf.camel@gmx.de/\n");
217 		return TEST_SKIP;
218 	} else
219 		TEST_ASSERT_EQUAL("unexpected sigtraps", ctx.signal_count, expected_sigtraps);
220 
221 	TEST_ASSERT_EQUAL("missing signals or incorrectly delivered", ctx.tids_want_signal, 0);
222 	TEST_ASSERT_VAL("unexpected si_addr", ctx.first_siginfo.si_addr == &ctx.iterate_on);
223 #if 0 /* FIXME: enable when libc's signal.h has si_perf_{type,data} */
224 	TEST_ASSERT_EQUAL("unexpected si_perf_type", ctx.first_siginfo.si_perf_type,
225 			  PERF_TYPE_BREAKPOINT);
226 	TEST_ASSERT_EQUAL("unexpected si_perf_data", ctx.first_siginfo.si_perf_data,
227 			  TEST_SIG_DATA);
228 #endif
229 
230 	return ret;
231 }
232 
233 static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
234 {
235 	struct perf_event_attr attr = make_event_attr();
236 	struct sigaction action = {};
237 	struct sigaction oldact;
238 	pthread_t threads[NUM_THREADS];
239 	pthread_barrier_t barrier;
240 	char sbuf[STRERR_BUFSIZE];
241 	int i, fd, ret = TEST_FAIL;
242 
243 	if (!BP_SIGNAL_IS_SUPPORTED) {
244 		pr_debug("Test not supported on this architecture");
245 		return TEST_SKIP;
246 	}
247 
248 	pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
249 
250 	action.sa_flags = SA_SIGINFO | SA_NODEFER;
251 	action.sa_sigaction = sigtrap_handler;
252 	sigemptyset(&action.sa_mask);
253 	if (sigaction(SIGTRAP, &action, &oldact)) {
254 		pr_debug("FAILED sigaction(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
255 		goto out;
256 	}
257 
258 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
259 	if (fd < 0) {
260 		if (attr_has_sigtrap()) {
261 			pr_debug("FAILED sys_perf_event_open(): %s\n",
262 				 str_error_r(errno, sbuf, sizeof(sbuf)));
263 		} else {
264 			pr_debug("perf_event_attr doesn't have sigtrap\n");
265 			ret = TEST_SKIP;
266 		}
267 		goto out_restore_sigaction;
268 	}
269 
270 	for (i = 0; i < NUM_THREADS; i++) {
271 		if (pthread_create(&threads[i], NULL, test_thread, &barrier)) {
272 			pr_debug("FAILED pthread_create(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
273 			goto out_close_perf_event;
274 		}
275 	}
276 
277 	ret = run_stress_test(fd, threads, &barrier);
278 
279 out_close_perf_event:
280 	close(fd);
281 out_restore_sigaction:
282 	sigaction(SIGTRAP, &oldact, NULL);
283 out:
284 	pthread_barrier_destroy(&barrier);
285 	btf__exit();
286 	return ret;
287 }
288 
289 DEFINE_SUITE("Sigtrap", sigtrap);
290