Skip to content

Commit caf6249

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Support pre-initializing .bss global variables
Remove invalid assumption in libbpf that .bss map doesn't have to be updated in kernel. With addition of skeleton and memory-mapped initialization image, .bss doesn't have to be all zeroes when BPF map is created, because user-code might have initialized those variables from user-space. Fixes: eba9c5f ("libbpf: Refactor global data map initialization") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 22eb787 commit caf6249

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,10 +3564,6 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
35643564
char *cp, errmsg[STRERR_BUFSIZE];
35653565
int err, zero = 0;
35663566

3567-
/* kernel already zero-initializes .bss map. */
3568-
if (map_type == LIBBPF_MAP_BSS)
3569-
return 0;
3570-
35713567
err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
35723568
if (err) {
35733569
err = -errno;

tools/testing/selftests/bpf/prog_tests/skeleton.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ void test_skeleton(void)
1515
int duration = 0, err;
1616
struct test_skeleton* skel;
1717
struct test_skeleton__bss *bss;
18+
struct test_skeleton__data *data;
19+
struct test_skeleton__rodata *rodata;
1820
struct test_skeleton__kconfig *kcfg;
1921

2022
skel = test_skeleton__open();
@@ -24,13 +26,45 @@ void test_skeleton(void)
2426
if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n"))
2527
goto cleanup;
2628

29+
bss = skel->bss;
30+
data = skel->data;
31+
rodata = skel->rodata;
32+
33+
/* validate values are pre-initialized correctly */
34+
CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1);
35+
CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1);
36+
CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL);
37+
CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL);
38+
39+
CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0);
40+
CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0);
41+
CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
42+
CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
43+
44+
CHECK(rodata->in6 != 0, "in6", "got %d != exp %d\n", rodata->in6, 0);
45+
CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
46+
47+
/* validate we can pre-setup global variables, even in .bss */
48+
data->in1 = 10;
49+
data->in2 = 11;
50+
bss->in3 = 12;
51+
bss->in4 = 13;
52+
rodata->in6 = 14;
53+
2754
err = test_skeleton__load(skel);
2855
if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
2956
goto cleanup;
3057

31-
bss = skel->bss;
32-
bss->in1 = 1;
33-
bss->in2 = 2;
58+
/* validate pre-setup values are still there */
59+
CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10);
60+
CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
61+
CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
62+
CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
63+
CHECK(rodata->in6 != 14, "in6", "got %d != exp %d\n", rodata->in6, 14);
64+
65+
/* now set new values and attach to get them into outX variables */
66+
data->in1 = 1;
67+
data->in2 = 2;
3468
bss->in3 = 3;
3569
bss->in4 = 4;
3670
bss->in5.a = 5;
@@ -44,14 +78,15 @@ void test_skeleton(void)
4478
/* trigger tracepoint */
4579
usleep(1);
4680

47-
CHECK(bss->out1 != 1, "res1", "got %d != exp %d\n", bss->out1, 1);
48-
CHECK(bss->out2 != 2, "res2", "got %lld != exp %d\n", bss->out2, 2);
81+
CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1);
82+
CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2);
4983
CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
5084
CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
5185
CHECK(bss->handler_out5.a != 5, "res5", "got %d != exp %d\n",
5286
bss->handler_out5.a, 5);
5387
CHECK(bss->handler_out5.b != 6, "res6", "got %lld != exp %d\n",
5488
bss->handler_out5.b, 6);
89+
CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14);
5590

5691
CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1",
5792
"got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL);

tools/testing/selftests/bpf/progs/test_skeleton.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ struct s {
1010
long long b;
1111
} __attribute__((packed));
1212

13-
int in1 = 0;
14-
long long in2 = 0;
13+
/* .data section */
14+
int in1 = -1;
15+
long long in2 = -1;
16+
17+
/* .bss section */
1518
char in3 = '\0';
1619
long long in4 __attribute__((aligned(64))) = 0;
1720
struct s in5 = {};
1821

19-
long long out2 = 0;
22+
/* .rodata section */
23+
const volatile int in6 = 0;
24+
25+
/* .data section */
26+
int out1 = -1;
27+
long long out2 = -1;
28+
29+
/* .bss section */
2030
char out3 = 0;
2131
long long out4 = 0;
22-
int out1 = 0;
32+
int out6 = 0;
2333

2434
extern bool CONFIG_BPF_SYSCALL __kconfig;
2535
extern int LINUX_KERNEL_VERSION __kconfig;
@@ -36,6 +46,7 @@ int handler(const void *ctx)
3646
out3 = in3;
3747
out4 = in4;
3848
out5 = in5;
49+
out6 = in6;
3950

4051
bpf_syscall = CONFIG_BPF_SYSCALL;
4152
kern_ver = LINUX_KERNEL_VERSION;

0 commit comments

Comments
 (0)