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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <sched.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/syscall.h>
#include <test_progs.h>
#include "arena_race.skel.h"
struct free_thread_ctx {
struct arena_race *skel;
int err;
__u32 retval;
};
struct fault_thread_ctx {
__u64 *addr;
int stop;
};
static int run_prog(struct bpf_program *prog, const char *name)
{
LIBBPF_OPTS(bpf_test_run_opts, opts);
int err;
err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
return ASSERT_OK(err, name) && ASSERT_OK(opts.retval, name) ? 0 : -1;
}
/* Trigger the sleepable free page path. */
static void *run_free_thread(void *arg)
{
LIBBPF_OPTS(bpf_test_run_opts, opts);
struct free_thread_ctx *ctx = arg;
ctx->skel->bss->target_tid = sys_gettid();
ctx->err = bpf_prog_test_run_opts(
bpf_program__fd(ctx->skel->progs.free_page), &opts);
ctx->retval = opts.retval;
return NULL;
}
static __thread sigjmp_buf reader_env;
static __thread bool reader_armed;
/* Only the reader expects SIGBUS; anywhere else it keeps its default action. */
static void reader_sigbus(int sig)
{
if (!reader_armed) {
signal(sig, SIG_DFL);
raise(sig);
return;
}
siglongjmp(reader_env, 1);
}
/* Continuously fault in the address. */
static void *fault_reader_thread(void *arg)
{
struct fault_thread_ctx *ctx = arg;
__u64 *addr;
/* Touching a page while BPF frees it raises SIGBUS; skip that read. */
sigsetjmp(reader_env, 1);
reader_armed = true;
while (!READ_ONCE(ctx->stop)) {
addr = __atomic_load_n(&ctx->addr, __ATOMIC_ACQUIRE);
(void)READ_ONCE(*addr);
}
reader_armed = false;
return NULL;
}
static int wait_for(int *p)
{
__u64 deadline = get_time_ns() + 5ULL * 1000 * 1000 * 1000;
while (!READ_ONCE(*p)) {
if (get_time_ns() > deadline)
return -ETIMEDOUT;
}
return 0;
}
/* Skip the subtest, saying why, if @func cannot be traced with fentry. */
static bool require_fentry_target(const char *test, const char *func)
{
if (libbpf_find_vmlinux_btf_id(func, BPF_TRACE_FENTRY) > 0)
return true;
printf("%s:SKIP: %s is not an fentry target\n", test, func);
test__skip();
return false;
}
static struct arena_race *setup_arena(__u64 **addr, bool trace_zap)
{
struct arena_race *skel;
size_t arena_sz;
char *base;
int err;
skel = arena_race__open();
if (!ASSERT_OK_PTR(skel, "open"))
return NULL;
err = bpf_program__set_autoload(skel->progs.trace_zap, trace_zap);
if (!ASSERT_OK(err, "trace_zap_autoload"))
goto err_out;
err = arena_race__load(skel);
if (!ASSERT_OK(err, "load"))
goto err_out;
err = arena_race__attach(skel);
if (!ASSERT_OK(err, "attach"))
goto err_out;
if (run_prog(skel->progs.alloc_old, "alloc_old"))
goto err_out;
if (skel->bss->skip) {
printf("%s:SKIP: compiler doesn't support arena_cast\n", __func__);
test__skip();
goto err_out;
}
base = bpf_map__initial_value(skel->maps.arena, &arena_sz);
if (!ASSERT_OK_PTR(base, "arena_base"))
goto err_out;
*addr = (__u64 *)(base + getpagesize());
if (!ASSERT_EQ((unsigned long)skel->bss->ptr, (unsigned long)*addr,
"arena_ptr"))
goto err_out;
return skel;
err_out:
arena_race__destroy(skel);
return NULL;
}
static void test_realloc_during_free(bool deferred)
{
struct free_thread_ctx ctx = {};
struct arena_race *skel;
pthread_t thread;
__u64 *addr;
bool thread_created = false, zap_seen = false, completed = false;
int err;
if (!require_fentry_target(__func__, "zap_vma_range"))
return;
skel = setup_arena(&addr, true);
if (!skel)
return;
/* Map the page into user space, so that the free has to zap it. */
if (!ASSERT_EQ(READ_ONCE(*addr), skel->rodata->old_marker, "old_marker"))
goto out;
/* Pause the free after its TLB flush, before it releases the range. */
skel->bss->pause_on_zap = 1;
if (deferred) {
/*
* Test the nonsleepable free path that gets
* deferred to a worker in the kernel. We do
* so by triggering the arena operation from
* a nonsleepable tracepoint context.
*/
skel->bss->trigger_pid_tgid =
((__u64)getpid() << 32) | (__u32)sys_gettid();
skel->bss->trigger_syscall = SYS_getpgid;
skel->bss->deferred_free = 1;
if (!ASSERT_GE(syscall(SYS_getpgid, 0), 0, "deferred_free"))
goto release;
} else {
/*
* Test the sleepable arena free path through a
* syscall test prog.
*/
ctx.skel = skel;
err = pthread_create(&thread, NULL, run_free_thread, &ctx);
if (!ASSERT_OK(err, "pthread_create")) {
skel->bss->release = 1;
goto out;
}
thread_created = true;
}
/* Wait until the free reaches the user zap. */
err = wait_for(&skel->bss->zap_entered);
if (!ASSERT_OK(err, "zap_entered"))
goto release;
zap_seen = true;
/* The range must stay allocated until the free completes. */
run_prog(skel->progs.try_realloc, "realloc_during_free");
ASSERT_NULL(skel->bss->realloc_ptr, "realloc_during_free");
release:
skel->bss->release = 1;
if (thread_created) {
ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
thread_created = false;
completed = ASSERT_OK(ctx.err, "free_run") &&
ASSERT_OK(ctx.retval, "free_retval");
} else if (skel->bss->target_tid) {
err = wait_for(&skel->bss->worker_exited);
completed = ASSERT_OK(err, "worker_exited");
}
ASSERT_FALSE(skel->bss->timed_out, "zap_timed_out");
if (zap_seen && completed &&
!run_prog(skel->progs.try_realloc, "realloc_after_free")) {
ASSERT_EQ((unsigned long)skel->bss->realloc_ptr,
(unsigned long)addr, "realloc_after_free");
ASSERT_EQ(*addr, skel->rodata->new_marker, "new_marker");
}
out:
arena_race__destroy(skel);
}
/*
* Force a race between a faulting thread in userspace and a
* free operation on the arena.
*/
static void test_fault_free_realloc(void)
{
struct sigaction sigbus = { .sa_handler = reader_sigbus }, old_sigbus;
struct fault_thread_ctx fault = {};
struct arena_race *skel;
pthread_t fault_thread;
bool fault_created = false;
__u64 *addr;
__u64 expected, value;
int err, i;
skel = setup_arena(&addr, false);
if (!skel)
return;
skel->bss->realloc_after_free = 1;
if (!ASSERT_OK(sigaction(SIGBUS, &sigbus, &old_sigbus), "sigaction_sigbus"))
goto out_destroy;
fault.addr = addr;
err = pthread_create(&fault_thread, NULL, fault_reader_thread, &fault);
if (!ASSERT_OK(err, "pthread_create_fault"))
goto out;
fault_created = true;
for (i = 0; i < 1000; i++) {
LIBBPF_OPTS(bpf_test_run_opts, opts);
/* Keep deferred frees from earlier iterations on separate slots. */
if (i) {
skel->bss->page_idx = i;
if (run_prog(skel->progs.alloc_old, "alloc_next"))
goto out;
addr = (__u64 *)(unsigned long)skel->bss->ptr;
__atomic_store_n(&fault.addr, addr, __ATOMIC_RELEASE);
}
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.free_page),
&opts);
if (err) {
ASSERT_OK(err, "free_realloc");
break;
}
if (opts.retval) {
ASSERT_OK(opts.retval, "free_realloc");
break;
}
/* The reader may populate the slot first, or free may defer. */
if (!skel->bss->realloc_ptr)
continue;
value = *addr;
expected = skel->bss->current_marker;
if (value != expected) {
ASSERT_EQ(value, expected, "marker_after_realloc");
break;
}
}
WRITE_ONCE(fault.stop, 1);
if (fault_created) {
ASSERT_OK(pthread_join(fault_thread, NULL), "pthread_join_fault");
fault_created = false;
}
out:
WRITE_ONCE(fault.stop, 1);
if (fault_created)
pthread_join(fault_thread, NULL);
sigaction(SIGBUS, &old_sigbus, NULL);
out_destroy:
arena_race__destroy(skel);
}
void serial_test_arena_race(void)
{
cpu_set_t cpuset;
int err;
err = sched_getaffinity(0, sizeof(cpuset), &cpuset);
if (!ASSERT_OK(err, "sched_getaffinity"))
return;
if (CPU_COUNT(&cpuset) < 2) {
printf("%s:SKIP: at least two runnable CPUs are required\n", __func__);
test__skip();
return;
}
if (test__start_subtest("realloc_during_free"))
test_realloc_during_free(false);
if (test__start_subtest("realloc_during_deferred_free"))
test_realloc_during_free(true);
if (test__start_subtest("fault_free_realloc"))
test_fault_free_realloc();
}
|