Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Mix legacy (maps) and modern (vars) BPF in one test

Add selftest that combines two BPF programs within single BPF object
file such that one of the programs is using global variables, but can be
skipped at runtime on old kernels that don't support global data.
Another BPF program is written with the goal to be runnable on very old
kernels and only relies on explicitly accessed BPF maps.

Such test, run against old kernels (e.g., libbpf CI will run it against 4.9
kernel that doesn't support global data), allows to test the approach
and ensure that libbpf doesn't make unnecessary assumption about
necessary kernel features.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20211123200105.387855-2-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
e4f7ac90 16e0c35c

+138
+65
tools/testing/selftests/bpf/prog_tests/legacy_printk.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2021 Facebook */ 3 + #include <test_progs.h> 4 + #include "test_legacy_printk.skel.h" 5 + 6 + static int execute_one_variant(bool legacy) 7 + { 8 + struct test_legacy_printk *skel; 9 + int err, zero = 0, my_pid = getpid(), res, map_fd; 10 + 11 + skel = test_legacy_printk__open(); 12 + if (!ASSERT_OK_PTR(skel, "skel_open")) 13 + return -errno; 14 + 15 + bpf_program__set_autoload(skel->progs.handle_legacy, legacy); 16 + bpf_program__set_autoload(skel->progs.handle_modern, !legacy); 17 + 18 + err = test_legacy_printk__load(skel); 19 + /* no ASSERT_OK, we expect one of two variants can fail here */ 20 + if (err) 21 + goto err_out; 22 + 23 + if (legacy) { 24 + map_fd = bpf_map__fd(skel->maps.my_pid_map); 25 + err = bpf_map_update_elem(map_fd, &zero, &my_pid, BPF_ANY); 26 + if (!ASSERT_OK(err, "my_pid_map_update")) 27 + goto err_out; 28 + err = bpf_map_lookup_elem(map_fd, &zero, &res); 29 + } else { 30 + skel->bss->my_pid_var = my_pid; 31 + } 32 + 33 + err = test_legacy_printk__attach(skel); 34 + if (!ASSERT_OK(err, "skel_attach")) 35 + goto err_out; 36 + 37 + usleep(1); /* trigger */ 38 + 39 + if (legacy) { 40 + map_fd = bpf_map__fd(skel->maps.res_map); 41 + err = bpf_map_lookup_elem(map_fd, &zero, &res); 42 + if (!ASSERT_OK(err, "res_map_lookup")) 43 + goto err_out; 44 + } else { 45 + res = skel->bss->res_var; 46 + } 47 + 48 + if (!ASSERT_GT(res, 0, "res")) { 49 + err = -EINVAL; 50 + goto err_out; 51 + } 52 + 53 + err_out: 54 + test_legacy_printk__destroy(skel); 55 + return err; 56 + } 57 + 58 + void test_legacy_printk(void) 59 + { 60 + /* legacy variant should work everywhere */ 61 + ASSERT_OK(execute_one_variant(true /* legacy */), "legacy_case"); 62 + 63 + /* execute modern variant, can fail the load on old kernels */ 64 + execute_one_variant(false); 65 + }
+73
tools/testing/selftests/bpf/progs/test_legacy_printk.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2021 Facebook */ 3 + 4 + #include <linux/bpf.h> 5 + #define BPF_NO_GLOBAL_DATA 6 + #include <bpf/bpf_helpers.h> 7 + 8 + char LICENSE[] SEC("license") = "GPL"; 9 + 10 + struct { 11 + __uint(type, BPF_MAP_TYPE_ARRAY); 12 + __type(key, int); 13 + __type(value, int); 14 + __uint(max_entries, 1); 15 + } my_pid_map SEC(".maps"); 16 + 17 + struct { 18 + __uint(type, BPF_MAP_TYPE_ARRAY); 19 + __type(key, int); 20 + __type(value, int); 21 + __uint(max_entries, 1); 22 + } res_map SEC(".maps"); 23 + 24 + volatile int my_pid_var = 0; 25 + volatile int res_var = 0; 26 + 27 + SEC("tp/raw_syscalls/sys_enter") 28 + int handle_legacy(void *ctx) 29 + { 30 + int zero = 0, *my_pid, cur_pid, *my_res; 31 + 32 + my_pid = bpf_map_lookup_elem(&my_pid_map, &zero); 33 + if (!my_pid) 34 + return 1; 35 + 36 + cur_pid = bpf_get_current_pid_tgid() >> 32; 37 + if (cur_pid != *my_pid) 38 + return 1; 39 + 40 + my_res = bpf_map_lookup_elem(&res_map, &zero); 41 + if (!my_res) 42 + return 1; 43 + 44 + if (*my_res == 0) 45 + /* use bpf_printk() in combination with BPF_NO_GLOBAL_DATA to 46 + * force .rodata.str1.1 section that previously caused 47 + * problems on old kernels due to libbpf always tried to 48 + * create a global data map for it 49 + */ 50 + bpf_printk("Legacy-case bpf_printk test, pid %d\n", cur_pid); 51 + *my_res = 1; 52 + 53 + return *my_res; 54 + } 55 + 56 + SEC("tp/raw_syscalls/sys_enter") 57 + int handle_modern(void *ctx) 58 + { 59 + int zero = 0, cur_pid; 60 + 61 + cur_pid = bpf_get_current_pid_tgid() >> 32; 62 + if (cur_pid != my_pid_var) 63 + return 1; 64 + 65 + if (res_var == 0) 66 + /* we need bpf_printk() to validate libbpf logic around unused 67 + * global maps and legacy kernels; see comment in handle_legacy() 68 + */ 69 + bpf_printk("Modern-case bpf_printk test, pid %d\n", cur_pid); 70 + res_var = 1; 71 + 72 + return res_var; 73 + }