summaryrefslogtreecommitdiff
path: root/tools/perf/tests/sigtrap.c
blob: e6fd934b027a3d0ca36f434135cbdc245acd666d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: GPL-2.0
/*
 * Basic test for sigtrap support.
 *
 * Copyright (C) 2021, Google LLC.
 */

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <linux/hw_breakpoint.h>
#include <linux/string.h>
#include <pthread.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>

#include "cloexec.h"
#include "debug.h"
#include "event.h"
#include "tests.h"
#include "../perf-sys.h"

#define NUM_THREADS 5

static struct {
	int tids_want_signal;		/* Which threads still want a signal. */
	int signal_count;		/* Sanity check number of signals received. */
	volatile int iterate_on;	/* Variable to set breakpoint on. */
	siginfo_t first_siginfo;	/* First observed siginfo_t. */
} ctx;

#define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on))

static struct perf_event_attr make_event_attr(void)
{
	struct perf_event_attr attr = {
		.type		= PERF_TYPE_BREAKPOINT,
		.size		= sizeof(attr),
		.sample_period	= 1,
		.disabled	= 1,
		.bp_addr	= (unsigned long)&ctx.iterate_on,
		.bp_type	= HW_BREAKPOINT_RW,
		.bp_len		= HW_BREAKPOINT_LEN_1,
		.inherit	= 1, /* Children inherit events ... */
		.inherit_thread = 1, /* ... but only cloned with CLONE_THREAD. */
		.remove_on_exec = 1, /* Required by sigtrap. */
		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
		.sig_data	= TEST_SIG_DATA,
		.exclude_kernel = 1, /* To allow */
		.exclude_hv     = 1, /* running as !root */
	};
	return attr;
}

#ifdef HAVE_BPF_SKEL
#include <bpf/btf.h>

static struct btf *btf;

static bool btf__available(void)
{
	if (btf == NULL)
		btf = btf__load_vmlinux_btf();

	return btf != NULL;
}

static void btf__exit(void)
{
	btf__free(btf);
	btf = NULL;
}

static const struct btf_member *__btf_type__find_member_by_name(int type_id, const char *member_name)
{
	const struct btf_type *t = btf__type_by_id(btf, type_id);
	const struct btf_member *m;
	int i;

	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
		const char *current_member_name = btf__name_by_offset(btf, m->name_off);
		if (!strcmp(current_member_name, member_name))
			return m;
	}

	return NULL;
}

static bool attr_has_sigtrap(void)
{
	int id;

	if (!btf__available()) {
		/* should be an old kernel */
		return false;
	}

	id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
	if (id < 0)
		return false;

	return __btf_type__find_member_by_name(id, "sigtrap") != NULL;
}

static bool kernel_with_sleepable_spinlocks(void)
{
	const struct btf_member *member;
	const struct btf_type *type;
	const char *type_name;
	int id;

	if (!btf__available())
		return false;

	id = btf__find_by_name_kind(btf, "spinlock", BTF_KIND_STRUCT);
	if (id < 0)
		return false;

	// Only RT has a "lock" member for "struct spinlock"
	member = __btf_type__find_member_by_name(id, "lock");
	if (member == NULL)
		return false;

	// But check its type as well
	type = btf__type_by_id(btf, member->type);
	if (!type || !btf_is_struct(type))
		return false;

	type_name = btf__name_by_offset(btf, type->name_off);
	return type_name && !strcmp(type_name, "rt_mutex_base");
}
#else  /* !HAVE_BPF_SKEL */
static bool attr_has_sigtrap(void)
{
	struct perf_event_attr attr = {
		.type		= PERF_TYPE_SOFTWARE,
		.config		= PERF_COUNT_SW_DUMMY,
		.size		= sizeof(attr),
		.remove_on_exec = 1, /* Required by sigtrap. */
		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
	};
	int fd;
	bool ret = false;

	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
	if (fd >= 0) {
		ret = true;
		close(fd);
	}

	return ret;
}

static bool kernel_with_sleepable_spinlocks(void)
{
	return false;
}

static void btf__exit(void)
{
}
#endif  /* HAVE_BPF_SKEL */

static void
sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
{
	if (!__atomic_fetch_add(&ctx.signal_count, 1, __ATOMIC_RELAXED))
		ctx.first_siginfo = *info;
	__atomic_fetch_sub(&ctx.tids_want_signal, syscall(SYS_gettid), __ATOMIC_RELAXED);
}

static void *test_thread(void *arg)
{
	pthread_barrier_t *barrier = (pthread_barrier_t *)arg;
	pid_t tid = syscall(SYS_gettid);
	int i;

	pthread_barrier_wait(barrier);

	__atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);
	for (i = 0; i < ctx.iterate_on - 1; i++)
		__atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED);

	return NULL;
}

static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier)
{
	int i;

	pthread_barrier_wait(barrier);
	for (i = 0; i < NUM_THREADS; i++)
		TEST_ASSERT_EQUAL("pthread_join() failed", pthread_join(threads[i], NULL), 0);

	return TEST_OK;
}

static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier)
{
	int ret, expected_sigtraps;

	ctx.iterate_on = 3000;

	TEST_ASSERT_EQUAL("misfired signal?", ctx.signal_count, 0);
	TEST_ASSERT_EQUAL("enable failed", ioctl(fd, PERF_EVENT_IOC_ENABLE, 0), 0);
	ret = run_test_threads(threads, barrier);
	TEST_ASSERT_EQUAL("disable failed", ioctl(fd, PERF_EVENT_IOC_DISABLE, 0), 0);

	expected_sigtraps = NUM_THREADS * ctx.iterate_on;

	if (ctx.signal_count < expected_sigtraps && kernel_with_sleepable_spinlocks()) {
		pr_debug("Expected %d sigtraps, got %d, running on a kernel with sleepable spinlocks.\n",
			 expected_sigtraps, ctx.signal_count);
		pr_debug("See https://lore.kernel.org/all/e368f2c848d77fbc8d259f44e2055fe469c219cf.camel@gmx.de/\n");
		return TEST_SKIP;
	} else
		TEST_ASSERT_EQUAL("unexpected sigtraps", ctx.signal_count, expected_sigtraps);

	TEST_ASSERT_EQUAL("missing signals or incorrectly delivered", ctx.tids_want_signal, 0);
	TEST_ASSERT_VAL("unexpected si_addr", ctx.first_siginfo.si_addr == &ctx.iterate_on);
#if 0 /* FIXME: enable when libc's signal.h has si_perf_{type,data} */
	TEST_ASSERT_EQUAL("unexpected si_perf_type", ctx.first_siginfo.si_perf_type,
			  PERF_TYPE_BREAKPOINT);
	TEST_ASSERT_EQUAL("unexpected si_perf_data", ctx.first_siginfo.si_perf_data,
			  TEST_SIG_DATA);
#endif

	return ret;
}

static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
	struct perf_event_attr attr = make_event_attr();
	struct sigaction action = {};
	struct sigaction oldact;
	pthread_t threads[NUM_THREADS];
	pthread_barrier_t barrier;
	char sbuf[STRERR_BUFSIZE];
	int i, fd, ret = TEST_FAIL;

	if (!BP_SIGNAL_IS_SUPPORTED) {
		pr_debug("Test not supported on this architecture");
		return TEST_SKIP;
	}

	pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);

	action.sa_flags = SA_SIGINFO | SA_NODEFER;
	action.sa_sigaction = sigtrap_handler;
	sigemptyset(&action.sa_mask);
	if (sigaction(SIGTRAP, &action, &oldact)) {
		pr_debug("FAILED sigaction(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
		goto out;
	}

	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
	if (fd < 0) {
		if (attr_has_sigtrap()) {
			pr_debug("FAILED sys_perf_event_open(): %s\n",
				 str_error_r(errno, sbuf, sizeof(sbuf)));
		} else {
			pr_debug("perf_event_attr doesn't have sigtrap\n");
			ret = TEST_SKIP;
		}
		goto out_restore_sigaction;
	}

	for (i = 0; i < NUM_THREADS; i++) {
		if (pthread_create(&threads[i], NULL, test_thread, &barrier)) {
			pr_debug("FAILED pthread_create(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
			goto out_close_perf_event;
		}
	}

	ret = run_stress_test(fd, threads, &barrier);

out_close_perf_event:
	close(fd);
out_restore_sigaction:
	sigaction(SIGTRAP, &oldact, NULL);
out:
	pthread_barrier_destroy(&barrier);
	btf__exit();
	return ret;
}

DEFINE_SUITE("Sigtrap", sigtrap);