summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_bpf_ma.c
blob: 3494ca30fa7fe7ecd6c0b7c78b66eb72ff0425cd (plain)
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
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>

#include "bpf_experimental.h"
#include "bpf_misc.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

struct generic_map_value {
	void *data;
};

char _license[] SEC("license") = "GPL";

const unsigned int data_sizes[] = {16, 32, 64, 96, 128, 192, 256, 512, 1024, 2048, 4096};
const volatile unsigned int data_btf_ids[ARRAY_SIZE(data_sizes)] = {};

const unsigned int percpu_data_sizes[] = {8, 16, 32, 64, 96, 128, 192, 256, 512};
const volatile unsigned int percpu_data_btf_ids[ARRAY_SIZE(data_sizes)] = {};

int err = 0;
u32 pid = 0;

#define DEFINE_ARRAY_WITH_KPTR(_size) \
	struct bin_data_##_size { \
		char data[_size - sizeof(void *)]; \
	}; \
	/* See Commit 5d8d6634ccc, force btf generation for type bin_data_##_size */	\
	struct bin_data_##_size *__bin_data_##_size; \
	struct map_value_##_size { \
		struct bin_data_##_size __kptr * data; \
	}; \
	struct { \
		__uint(type, BPF_MAP_TYPE_ARRAY); \
		__type(key, int); \
		__type(value, struct map_value_##_size); \
		__uint(max_entries, 128); \
	} array_##_size SEC(".maps")

#define DEFINE_ARRAY_WITH_PERCPU_KPTR(_size) \
	struct percpu_bin_data_##_size { \
		char data[_size]; \
	}; \
	struct percpu_bin_data_##_size *__percpu_bin_data_##_size; \
	struct map_value_percpu_##_size { \
		struct percpu_bin_data_##_size __percpu_kptr * data; \
	}; \
	struct { \
		__uint(type, BPF_MAP_TYPE_ARRAY); \
		__type(key, int); \
		__type(value, struct map_value_percpu_##_size); \
		__uint(max_entries, 128); \
	} array_percpu_##_size SEC(".maps")

static __always_inline void batch_alloc(struct bpf_map *map, unsigned int batch, unsigned int idx)
{
	struct generic_map_value *value;
	unsigned int i, key;
	void *old, *new;

	for (i = 0; i < batch; i++) {
		key = i;
		value = bpf_map_lookup_elem(map, &key);
		if (!value) {
			err = 1;
			return;
		}
		new = bpf_obj_new_impl(data_btf_ids[idx], NULL);
		if (!new) {
			err = 2;
			return;
		}
		old = bpf_kptr_xchg(&value->data, new);
		if (old) {
			bpf_obj_drop(old);
			err = 3;
			return;
		}
	}
}

static __always_inline void batch_free(struct bpf_map *map, unsigned int batch, unsigned int idx)
{
	struct generic_map_value *value;
	unsigned int i, key;
	void *old;

	for (i = 0; i < batch; i++) {
		key = i;
		value = bpf_map_lookup_elem(map, &key);
		if (!value) {
			err = 4;
			return;
		}
		old = bpf_kptr_xchg(&value->data, NULL);
		if (!old) {
			err = 5;
			return;
		}
		bpf_obj_drop(old);
	}
}

static __always_inline void batch_percpu_alloc(struct bpf_map *map, unsigned int batch,
					       unsigned int idx)
{
	struct generic_map_value *value;
	unsigned int i, key;
	void *old, *new;

	for (i = 0; i < batch; i++) {
		key = i;
		value = bpf_map_lookup_elem(map, &key);
		if (!value) {
			err = 1;
			return;
		}
		/* per-cpu allocator may not be able to refill in time */
		new = bpf_percpu_obj_new_impl(percpu_data_btf_ids[idx], NULL);
		if (!new)
			continue;

		old = bpf_kptr_xchg(&value->data, new);
		if (old) {
			bpf_percpu_obj_drop(old);
			err = 2;
			return;
		}
	}
}

static __always_inline void batch_percpu_free(struct bpf_map *map, unsigned int batch,
					      unsigned int idx)
{
	struct generic_map_value *value;
	unsigned int i, key;
	void *old;

	for (i = 0; i < batch; i++) {
		key = i;
		value = bpf_map_lookup_elem(map, &key);
		if (!value) {
			err = 3;
			return;
		}
		old = bpf_kptr_xchg(&value->data, NULL);
		if (!old)
			continue;
		bpf_percpu_obj_drop(old);
	}
}

#define CALL_BATCH_ALLOC(size, batch, idx) \
	batch_alloc((struct bpf_map *)(&array_##size), batch, idx)

#define CALL_BATCH_ALLOC_FREE(size, batch, idx) \
	do { \
		batch_alloc((struct bpf_map *)(&array_##size), batch, idx); \
		batch_free((struct bpf_map *)(&array_##size), batch, idx); \
	} while (0)

#define CALL_BATCH_PERCPU_ALLOC(size, batch, idx) \
	batch_percpu_alloc((struct bpf_map *)(&array_percpu_##size), batch, idx)

#define CALL_BATCH_PERCPU_ALLOC_FREE(size, batch, idx) \
	do { \
		batch_percpu_alloc((struct bpf_map *)(&array_percpu_##size), batch, idx); \
		batch_percpu_free((struct bpf_map *)(&array_percpu_##size), batch, idx); \
	} while (0)

/* kptr doesn't support bin_data_8 which is a zero-sized array */
DEFINE_ARRAY_WITH_KPTR(16);
DEFINE_ARRAY_WITH_KPTR(32);
DEFINE_ARRAY_WITH_KPTR(64);
DEFINE_ARRAY_WITH_KPTR(96);
DEFINE_ARRAY_WITH_KPTR(128);
DEFINE_ARRAY_WITH_KPTR(192);
DEFINE_ARRAY_WITH_KPTR(256);
DEFINE_ARRAY_WITH_KPTR(512);
DEFINE_ARRAY_WITH_KPTR(1024);
DEFINE_ARRAY_WITH_KPTR(2048);
DEFINE_ARRAY_WITH_KPTR(4096);

DEFINE_ARRAY_WITH_PERCPU_KPTR(8);
DEFINE_ARRAY_WITH_PERCPU_KPTR(16);
DEFINE_ARRAY_WITH_PERCPU_KPTR(32);
DEFINE_ARRAY_WITH_PERCPU_KPTR(64);
DEFINE_ARRAY_WITH_PERCPU_KPTR(96);
DEFINE_ARRAY_WITH_PERCPU_KPTR(128);
DEFINE_ARRAY_WITH_PERCPU_KPTR(192);
DEFINE_ARRAY_WITH_PERCPU_KPTR(256);
DEFINE_ARRAY_WITH_PERCPU_KPTR(512);

SEC("?fentry/" SYS_PREFIX "sys_nanosleep")
int test_batch_alloc_free(void *ctx)
{
	if ((u32)bpf_get_current_pid_tgid() != pid)
		return 0;

	/* Alloc 128 16-bytes objects in batch to trigger refilling,
	 * then free 128 16-bytes objects in batch to trigger freeing.
	 */
	CALL_BATCH_ALLOC_FREE(16, 128, 0);
	CALL_BATCH_ALLOC_FREE(32, 128, 1);
	CALL_BATCH_ALLOC_FREE(64, 128, 2);
	CALL_BATCH_ALLOC_FREE(96, 128, 3);
	CALL_BATCH_ALLOC_FREE(128, 128, 4);
	CALL_BATCH_ALLOC_FREE(192, 128, 5);
	CALL_BATCH_ALLOC_FREE(256, 128, 6);
	CALL_BATCH_ALLOC_FREE(512, 64, 7);
	CALL_BATCH_ALLOC_FREE(1024, 32, 8);
	CALL_BATCH_ALLOC_FREE(2048, 16, 9);
	CALL_BATCH_ALLOC_FREE(4096, 8, 10);

	return 0;
}

SEC("?fentry/" SYS_PREFIX "sys_nanosleep")
int test_free_through_map_free(void *ctx)
{
	if ((u32)bpf_get_current_pid_tgid() != pid)
		return 0;

	/* Alloc 128 16-bytes objects in batch to trigger refilling,
	 * then free these objects through map free.
	 */
	CALL_BATCH_ALLOC(16, 128, 0);
	CALL_BATCH_ALLOC(32, 128, 1);
	CALL_BATCH_ALLOC(64, 128, 2);
	CALL_BATCH_ALLOC(96, 128, 3);
	CALL_BATCH_ALLOC(128, 128, 4);
	CALL_BATCH_ALLOC(192, 128, 5);
	CALL_BATCH_ALLOC(256, 128, 6);
	CALL_BATCH_ALLOC(512, 64, 7);
	CALL_BATCH_ALLOC(1024, 32, 8);
	CALL_BATCH_ALLOC(2048, 16, 9);
	CALL_BATCH_ALLOC(4096, 8, 10);

	return 0;
}

SEC("?fentry/" SYS_PREFIX "sys_nanosleep")
int test_batch_percpu_alloc_free(void *ctx)
{
	if ((u32)bpf_get_current_pid_tgid() != pid)
		return 0;

	/* Alloc 128 8-bytes per-cpu objects in batch to trigger refilling,
	 * then free 128 8-bytes per-cpu objects in batch to trigger freeing.
	 */
	CALL_BATCH_PERCPU_ALLOC_FREE(8, 128, 0);
	CALL_BATCH_PERCPU_ALLOC_FREE(16, 128, 1);
	CALL_BATCH_PERCPU_ALLOC_FREE(32, 128, 2);
	CALL_BATCH_PERCPU_ALLOC_FREE(64, 128, 3);
	CALL_BATCH_PERCPU_ALLOC_FREE(96, 128, 4);
	CALL_BATCH_PERCPU_ALLOC_FREE(128, 128, 5);
	CALL_BATCH_PERCPU_ALLOC_FREE(192, 128, 6);
	CALL_BATCH_PERCPU_ALLOC_FREE(256, 128, 7);
	CALL_BATCH_PERCPU_ALLOC_FREE(512, 64, 8);

	return 0;
}

SEC("?fentry/" SYS_PREFIX "sys_nanosleep")
int test_percpu_free_through_map_free(void *ctx)
{
	if ((u32)bpf_get_current_pid_tgid() != pid)
		return 0;

	/* Alloc 128 8-bytes per-cpu objects in batch to trigger refilling,
	 * then free these object through map free.
	 */
	CALL_BATCH_PERCPU_ALLOC(8, 128, 0);
	CALL_BATCH_PERCPU_ALLOC(16, 128, 1);
	CALL_BATCH_PERCPU_ALLOC(32, 128, 2);
	CALL_BATCH_PERCPU_ALLOC(64, 128, 3);
	CALL_BATCH_PERCPU_ALLOC(96, 128, 4);
	CALL_BATCH_PERCPU_ALLOC(128, 128, 5);
	CALL_BATCH_PERCPU_ALLOC(192, 128, 6);
	CALL_BATCH_PERCPU_ALLOC(256, 128, 7);
	CALL_BATCH_PERCPU_ALLOC(512, 64, 8);

	return 0;
}