xref: /openbmc/linux/tools/perf/tests/sigtrap.c (revision ac73d4bf)
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 bool attr_has_sigtrap(void)
61 {
62 	bool ret = false;
63 	struct btf *btf;
64 	const struct btf_type *t;
65 	const struct btf_member *m;
66 	const char *name;
67 	int i, id;
68 
69 	btf = btf__load_vmlinux_btf();
70 	if (btf == NULL) {
71 		/* should be an old kernel */
72 		return false;
73 	}
74 
75 	id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
76 	if (id < 0)
77 		goto out;
78 
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")) {
83 			ret = true;
84 			break;
85 		}
86 	}
87 out:
88 	btf__free(btf);
89 	return ret;
90 }
91 #else  /* !HAVE_BPF_SKEL */
92 static bool attr_has_sigtrap(void)
93 {
94 	struct perf_event_attr attr = {
95 		.type		= PERF_TYPE_SOFTWARE,
96 		.config		= PERF_COUNT_SW_DUMMY,
97 		.size		= sizeof(attr),
98 		.remove_on_exec = 1, /* Required by sigtrap. */
99 		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
100 	};
101 	int fd;
102 	bool ret = false;
103 
104 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
105 	if (fd >= 0) {
106 		ret = true;
107 		close(fd);
108 	}
109 
110 	return ret;
111 }
112 #endif  /* HAVE_BPF_SKEL */
113 
114 static void
115 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
116 {
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);
120 }
121 
122 static void *test_thread(void *arg)
123 {
124 	pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
125 	pid_t tid = syscall(SYS_gettid);
126 	int i;
127 
128 	pthread_barrier_wait(barrier);
129 
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);
133 
134 	return NULL;
135 }
136 
137 static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
138 {
139 	int i;
140 
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);
144 
145 	return TEST_OK;
146 }
147 
148 static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
149 {
150 	int ret;
151 
152 	ctx.iterate_on = 3000;
153 
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);
158 
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,
166 			  TEST_SIG_DATA);
167 #endif
168 
169 	return ret;
170 }
171 
172 static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
173 {
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;
181 
182 	if (!BP_SIGNAL_IS_SUPPORTED) {
183 		pr_debug("Test not supported on this architecture");
184 		return TEST_SKIP;
185 	}
186 
187 	pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
188 
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)));
194 		goto out;
195 	}
196 
197 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
198 	if (fd < 0) {
199 		if (attr_has_sigtrap()) {
200 			pr_debug("FAILED sys_perf_event_open(): %s\n",
201 				 str_error_r(errno, sbuf, sizeof(sbuf)));
202 		} else {
203 			pr_debug("perf_event_attr doesn't have sigtrap\n");
204 			ret = TEST_SKIP;
205 		}
206 		goto out_restore_sigaction;
207 	}
208 
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;
213 		}
214 	}
215 
216 	ret = run_stress_test(fd, threads, &barrier);
217 
218 out_close_perf_event:
219 	close(fd);
220 out_restore_sigaction:
221 	sigaction(SIGTRAP, &oldact, NULL);
222 out:
223 	pthread_barrier_destroy(&barrier);
224 	return ret;
225 }
226 
227 DEFINE_SUITE("Sigtrap", sigtrap);
228