xref: /openbmc/linux/tools/perf/tests/sigtrap.c (revision 2cf1c348)
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 /*
26  * PowerPC and S390 do not support creation of instruction breakpoints using the
27  * perf_event interface.
28  *
29  * Just disable the test for these architectures until these issues are
30  * resolved.
31  */
32 #if defined(__powerpc__) || defined(__s390x__)
33 #define BP_ACCOUNT_IS_SUPPORTED 0
34 #else
35 #define BP_ACCOUNT_IS_SUPPORTED 1
36 #endif
37 
38 #define NUM_THREADS 5
39 
40 static struct {
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. */
45 } ctx;
46 
47 #define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on))
48 
49 static struct perf_event_attr make_event_attr(void)
50 {
51 	struct perf_event_attr attr = {
52 		.type		= PERF_TYPE_BREAKPOINT,
53 		.size		= sizeof(attr),
54 		.sample_period	= 1,
55 		.disabled	= 1,
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 */
66 	};
67 	return attr;
68 }
69 
70 static void
71 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
72 {
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);
76 }
77 
78 static void *test_thread(void *arg)
79 {
80 	pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
81 	pid_t tid = syscall(SYS_gettid);
82 	int i;
83 
84 	pthread_barrier_wait(barrier);
85 
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);
89 
90 	return NULL;
91 }
92 
93 static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
94 {
95 	int i;
96 
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);
100 
101 	return TEST_OK;
102 }
103 
104 static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
105 {
106 	int ret;
107 
108 	ctx.iterate_on = 3000;
109 
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);
114 
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,
122 			  TEST_SIG_DATA);
123 #endif
124 
125 	return ret;
126 }
127 
128 static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
129 {
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;
137 
138 	if (!BP_ACCOUNT_IS_SUPPORTED) {
139 		pr_debug("Test not supported on this architecture");
140 		return TEST_SKIP;
141 	}
142 
143 	pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
144 
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)));
150 		goto out;
151 	}
152 
153 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
154 	if (fd < 0) {
155 		pr_debug("FAILED sys_perf_event_open(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
156 		goto out_restore_sigaction;
157 	}
158 
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;
163 		}
164 	}
165 
166 	ret = run_stress_test(fd, threads, &barrier);
167 
168 out_close_perf_event:
169 	close(fd);
170 out_restore_sigaction:
171 	sigaction(SIGTRAP, &oldact, NULL);
172 out:
173 	pthread_barrier_destroy(&barrier);
174 	return ret;
175 }
176 
177 DEFINE_SUITE("Sigtrap", sigtrap);
178