libbpf: support pre-initializing .bss global variables
Subsystems (6)
PATCH 1/1 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 eba9c5f498a1 ("libbpf: Refactor global data map initialization")
Signed-off-by Andrii Nakryiko <[email protected]>
--- tools/lib/bpf/libbpf.c | 4 -- .../selftests/bpf/prog_tests/skeleton.c | 45 ++++++++++++++++--- .../selftests/bpf/progs/test_skeleton.c | 19 ++++++-- 3 files changed, 55 insertions(+), 13 deletions(-)
tools/lib/bpf/libbpf.c +0 -4
@@ -3564,10 +3564,6 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)char *cp, errmsg[STRERR_BUFSIZE ];int err, zero =0 ; -/* kernel already zero-initializes .bss map. */ -if (map_type ==LIBBPF_MAP_BSS ) -return 0 ; - err =bpf_map_update_elem (map->fd, &zero, map->mmaped,0 );if (err) { err = -errno;
tools/testing/selftests/bpf/prog_tests/skeleton.c +40 -5
73 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_skeleton.c +15 -4
@@ -10,16 +10,26 @@ struct s {long long b; } __attribute__((packed)); -int in1 =0 ; -long long in2 =0 ; +/* .data section */ +int in1 =-1 ; +long long in2 =-1 ; + +/* .bss section */ char in3 ='\0' ; long long in4 __attribute__((aligned(64))) = 0;struct s in5 = {}; -long long out2 =0 ; +/* .rodata section */ +const volatile int in6 =0 ; + +/* .data section */ +int out1 =-1 ; +long long out2 =-1 ; + +/* .bss section */ char out3 =0 ;long long out4 =0 ; -int out1 =0 ; +int out6 =0 ; extern bool CONFIG_BPF_SYSCALL __kconfig; extern int LINUX_KERNEL_VERSION __kconfig;
@@ -36,6 +46,7 @@ int handler(const void *ctx) out3 = in3; out4 = in4; out5 = in5; + out6 = in6; bpf_syscall =CONFIG_BPF_SYSCALL ; kern_ver =LINUX_KERNEL_VERSION ;
Signature · 1 line
2.24.1
Alexei Starovoitov
AppliedQuoted 8 lines · Andrii Nakryiko
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 eba9c5f498a1 ("libbpf: Refactor global data map initialization")Signed-off-by Andrii Nakryiko <[email protected]>
Applied. Thanks