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
290
291
292
293
294
|
// SPDX-License-Identifier: GPL-2.0
/*
* Ring-buffer memory mapping tests
*
* Copyright (c) 2024 Vincent Donnefort <vdonnefort@google.com>
*/
#include <fcntl.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/trace_mmap.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "../user_events/user_events_selftests.h" /* share tracefs setup */
#include "../kselftest_harness.h"
#define TRACEFS_ROOT "/sys/kernel/tracing"
static int __tracefs_write(const char *path, const char *value)
{
int fd, ret;
fd = open(path, O_WRONLY | O_TRUNC);
if (fd < 0)
return fd;
ret = write(fd, value, strlen(value));
close(fd);
return ret == -1 ? -errno : 0;
}
static int __tracefs_write_int(const char *path, int value)
{
char *str;
int ret;
if (asprintf(&str, "%d", value) < 0)
return -1;
ret = __tracefs_write(path, str);
free(str);
return ret;
}
#define tracefs_write_int(path, value) \
ASSERT_EQ(__tracefs_write_int((path), (value)), 0)
#define tracefs_write(path, value) \
ASSERT_EQ(__tracefs_write((path), (value)), 0)
static int tracefs_reset(void)
{
if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0))
return -1;
if (__tracefs_write(TRACEFS_ROOT"/trace", ""))
return -1;
if (__tracefs_write(TRACEFS_ROOT"/set_event", ""))
return -1;
if (__tracefs_write(TRACEFS_ROOT"/current_tracer", "nop"))
return -1;
return 0;
}
struct tracefs_cpu_map_desc {
struct trace_buffer_meta *meta;
int cpu_fd;
};
int tracefs_cpu_map(struct tracefs_cpu_map_desc *desc, int cpu)
{
int page_size = getpagesize();
char *cpu_path;
void *map;
if (asprintf(&cpu_path,
TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw",
cpu) < 0)
return -ENOMEM;
desc->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK);
free(cpu_path);
if (desc->cpu_fd < 0)
return -ENODEV;
map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, desc->cpu_fd, 0);
if (map == MAP_FAILED)
return -errno;
desc->meta = (struct trace_buffer_meta *)map;
return 0;
}
void tracefs_cpu_unmap(struct tracefs_cpu_map_desc *desc)
{
munmap(desc->meta, desc->meta->meta_page_size);
close(desc->cpu_fd);
}
FIXTURE(map) {
struct tracefs_cpu_map_desc map_desc;
bool umount;
};
FIXTURE_VARIANT(map) {
int subbuf_size;
};
FIXTURE_VARIANT_ADD(map, subbuf_size_4k) {
.subbuf_size = 4,
};
FIXTURE_VARIANT_ADD(map, subbuf_size_8k) {
.subbuf_size = 8,
};
FIXTURE_SETUP(map)
{
int cpu = sched_getcpu();
cpu_set_t cpu_mask;
bool fail, umount;
char *message;
if (getuid() != 0)
SKIP(return, "Skipping: %s", "Please run the test as root");
if (!tracefs_enabled(&message, &fail, &umount)) {
if (fail) {
TH_LOG("Tracefs setup failed: %s", message);
ASSERT_FALSE(fail);
}
SKIP(return, "Skipping: %s", message);
}
self->umount = umount;
ASSERT_GE(cpu, 0);
ASSERT_EQ(tracefs_reset(), 0);
tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size);
ASSERT_EQ(tracefs_cpu_map(&self->map_desc, cpu), 0);
/*
* Ensure generated events will be found on this very same ring-buffer.
*/
CPU_ZERO(&cpu_mask);
CPU_SET(cpu, &cpu_mask);
ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0);
}
FIXTURE_TEARDOWN(map)
{
tracefs_reset();
if (self->umount)
tracefs_unmount();
tracefs_cpu_unmap(&self->map_desc);
}
TEST_F(map, meta_page_check)
{
struct tracefs_cpu_map_desc *desc = &self->map_desc;
int cnt = 0;
ASSERT_EQ(desc->meta->entries, 0);
ASSERT_EQ(desc->meta->overrun, 0);
ASSERT_EQ(desc->meta->read, 0);
ASSERT_EQ(desc->meta->reader.id, 0);
ASSERT_EQ(desc->meta->reader.read, 0);
ASSERT_EQ(ioctl(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
ASSERT_EQ(desc->meta->reader.id, 0);
tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1);
for (int i = 0; i < 16; i++)
tracefs_write_int(TRACEFS_ROOT"/trace_marker", i);
again:
ASSERT_EQ(ioctl(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0);
ASSERT_EQ(desc->meta->entries, 16);
ASSERT_EQ(desc->meta->overrun, 0);
ASSERT_EQ(desc->meta->read, 16);
ASSERT_EQ(desc->meta->reader.id, 1);
if (!(cnt++))
goto again;
}
TEST_F(map, data_mmap)
{
struct tracefs_cpu_map_desc *desc = &self->map_desc;
unsigned long meta_len, data_len;
void *data;
meta_len = desc->meta->meta_page_size;
data_len = desc->meta->subbuf_size * desc->meta->nr_subbufs;
/* Map all the available subbufs */
data = mmap(NULL, data_len, PROT_READ, MAP_SHARED,
desc->cpu_fd, meta_len);
ASSERT_NE(data, MAP_FAILED);
munmap(data, data_len);
/* Map all the available subbufs - 1 */
data_len -= desc->meta->subbuf_size;
data = mmap(NULL, data_len, PROT_READ, MAP_SHARED,
desc->cpu_fd, meta_len);
ASSERT_NE(data, MAP_FAILED);
munmap(data, data_len);
/* Overflow the available subbufs by 1 */
meta_len += desc->meta->subbuf_size * 2;
data = mmap(NULL, data_len, PROT_READ, MAP_SHARED,
desc->cpu_fd, meta_len);
ASSERT_EQ(data, MAP_FAILED);
}
FIXTURE(snapshot) {
bool umount;
};
FIXTURE_SETUP(snapshot)
{
bool fail, umount;
struct stat sb;
char *message;
if (getuid() != 0)
SKIP(return, "Skipping: %s", "Please run the test as root");
if (stat(TRACEFS_ROOT"/snapshot", &sb))
SKIP(return, "Skipping: %s", "snapshot not available");
if (!tracefs_enabled(&message, &fail, &umount)) {
if (fail) {
TH_LOG("Tracefs setup failed: %s", message);
ASSERT_FALSE(fail);
}
SKIP(return, "Skipping: %s", message);
}
self->umount = umount;
}
FIXTURE_TEARDOWN(snapshot)
{
__tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger",
"!snapshot");
tracefs_reset();
if (self->umount)
tracefs_unmount();
}
TEST_F(snapshot, excludes_map)
{
struct tracefs_cpu_map_desc map_desc;
int cpu = sched_getcpu();
ASSERT_GE(cpu, 0);
tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger",
"snapshot");
ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), -EBUSY);
}
TEST_F(snapshot, excluded_by_map)
{
struct tracefs_cpu_map_desc map_desc;
int cpu = sched_getcpu();
ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), 0);
ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger",
"snapshot"), -EBUSY);
ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/snapshot",
"1"), -EBUSY);
}
TEST_HARNESS_MAIN
|