Skip to content

Commit f0a42ab

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
selftests/bpf: Test all valid alloc sizes for bpf mem allocator
Add a test to test all possible and valid allocation size for bpf memory allocator. For each possible allocation size, the test uses the following two steps to test the alloc and free path: 1) allocate N (N > high_watermark) objects to trigger the refill executed in irq_work. 2) free N objects to trigger the freeing executed in irq_work. Signed-off-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/r/20230908133923.2675053-5-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent c930472 commit f0a42ab

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3+
#define _GNU_SOURCE
4+
#include <sched.h>
5+
#include <pthread.h>
6+
#include <stdbool.h>
7+
#include <bpf/btf.h>
8+
#include <test_progs.h>
9+
10+
#include "test_bpf_ma.skel.h"
11+
12+
void test_test_bpf_ma(void)
13+
{
14+
struct test_bpf_ma *skel;
15+
struct btf *btf;
16+
int i, err;
17+
18+
skel = test_bpf_ma__open();
19+
if (!ASSERT_OK_PTR(skel, "open"))
20+
return;
21+
22+
btf = bpf_object__btf(skel->obj);
23+
if (!ASSERT_OK_PTR(btf, "btf"))
24+
goto out;
25+
26+
for (i = 0; i < ARRAY_SIZE(skel->rodata->data_sizes); i++) {
27+
char name[32];
28+
int id;
29+
30+
snprintf(name, sizeof(name), "bin_data_%u", skel->rodata->data_sizes[i]);
31+
id = btf__find_by_name_kind(btf, name, BTF_KIND_STRUCT);
32+
if (!ASSERT_GT(id, 0, "bin_data"))
33+
goto out;
34+
skel->rodata->data_btf_ids[i] = id;
35+
}
36+
37+
err = test_bpf_ma__load(skel);
38+
if (!ASSERT_OK(err, "load"))
39+
goto out;
40+
41+
err = test_bpf_ma__attach(skel);
42+
if (!ASSERT_OK(err, "attach"))
43+
goto out;
44+
45+
skel->bss->pid = getpid();
46+
usleep(1);
47+
ASSERT_OK(skel->bss->err, "test error");
48+
out:
49+
test_bpf_ma__destroy(skel);
50+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include <bpf/bpf_helpers.h>
6+
7+
#include "bpf_experimental.h"
8+
#include "bpf_misc.h"
9+
10+
#ifndef ARRAY_SIZE
11+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
12+
#endif
13+
14+
struct generic_map_value {
15+
void *data;
16+
};
17+
18+
char _license[] SEC("license") = "GPL";
19+
20+
const unsigned int data_sizes[] = {8, 16, 32, 64, 96, 128, 192, 256, 512, 1024, 2048, 4096};
21+
const volatile unsigned int data_btf_ids[ARRAY_SIZE(data_sizes)] = {};
22+
23+
int err = 0;
24+
int pid = 0;
25+
26+
#define DEFINE_ARRAY_WITH_KPTR(_size) \
27+
struct bin_data_##_size { \
28+
char data[_size - sizeof(void *)]; \
29+
}; \
30+
struct map_value_##_size { \
31+
struct bin_data_##_size __kptr * data; \
32+
/* To emit BTF info for bin_data_xx */ \
33+
struct bin_data_##_size not_used; \
34+
}; \
35+
struct { \
36+
__uint(type, BPF_MAP_TYPE_ARRAY); \
37+
__type(key, int); \
38+
__type(value, struct map_value_##_size); \
39+
__uint(max_entries, 128); \
40+
} array_##_size SEC(".maps");
41+
42+
static __always_inline void batch_alloc_free(struct bpf_map *map, unsigned int batch,
43+
unsigned int idx)
44+
{
45+
struct generic_map_value *value;
46+
unsigned int i, key;
47+
void *old, *new;
48+
49+
for (i = 0; i < batch; i++) {
50+
key = i;
51+
value = bpf_map_lookup_elem(map, &key);
52+
if (!value) {
53+
err = 1;
54+
return;
55+
}
56+
new = bpf_obj_new_impl(data_btf_ids[idx], NULL);
57+
if (!new) {
58+
err = 2;
59+
return;
60+
}
61+
old = bpf_kptr_xchg(&value->data, new);
62+
if (old) {
63+
bpf_obj_drop(old);
64+
err = 3;
65+
return;
66+
}
67+
}
68+
for (i = 0; i < batch; i++) {
69+
key = i;
70+
value = bpf_map_lookup_elem(map, &key);
71+
if (!value) {
72+
err = 4;
73+
return;
74+
}
75+
old = bpf_kptr_xchg(&value->data, NULL);
76+
if (!old) {
77+
err = 5;
78+
return;
79+
}
80+
bpf_obj_drop(old);
81+
}
82+
}
83+
84+
#define CALL_BATCH_ALLOC_FREE(size, batch, idx) \
85+
batch_alloc_free((struct bpf_map *)(&array_##size), batch, idx)
86+
87+
DEFINE_ARRAY_WITH_KPTR(8);
88+
DEFINE_ARRAY_WITH_KPTR(16);
89+
DEFINE_ARRAY_WITH_KPTR(32);
90+
DEFINE_ARRAY_WITH_KPTR(64);
91+
DEFINE_ARRAY_WITH_KPTR(96);
92+
DEFINE_ARRAY_WITH_KPTR(128);
93+
DEFINE_ARRAY_WITH_KPTR(192);
94+
DEFINE_ARRAY_WITH_KPTR(256);
95+
DEFINE_ARRAY_WITH_KPTR(512);
96+
DEFINE_ARRAY_WITH_KPTR(1024);
97+
DEFINE_ARRAY_WITH_KPTR(2048);
98+
DEFINE_ARRAY_WITH_KPTR(4096);
99+
100+
SEC("fentry/" SYS_PREFIX "sys_nanosleep")
101+
int test_bpf_mem_alloc_free(void *ctx)
102+
{
103+
if ((u32)bpf_get_current_pid_tgid() != pid)
104+
return 0;
105+
106+
/* Alloc 128 8-bytes objects in batch to trigger refilling,
107+
* then free 128 8-bytes objects in batch to trigger freeing.
108+
*/
109+
CALL_BATCH_ALLOC_FREE(8, 128, 0);
110+
CALL_BATCH_ALLOC_FREE(16, 128, 1);
111+
CALL_BATCH_ALLOC_FREE(32, 128, 2);
112+
CALL_BATCH_ALLOC_FREE(64, 128, 3);
113+
CALL_BATCH_ALLOC_FREE(96, 128, 4);
114+
CALL_BATCH_ALLOC_FREE(128, 128, 5);
115+
CALL_BATCH_ALLOC_FREE(192, 128, 6);
116+
CALL_BATCH_ALLOC_FREE(256, 128, 7);
117+
CALL_BATCH_ALLOC_FREE(512, 64, 8);
118+
CALL_BATCH_ALLOC_FREE(1024, 32, 9);
119+
CALL_BATCH_ALLOC_FREE(2048, 16, 10);
120+
CALL_BATCH_ALLOC_FREE(4096, 8, 11);
121+
122+
return 0;
123+
}

0 commit comments

Comments
 (0)