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

selftests/bpf: Fix spurious test failures in core_retro selftest

core_retro selftest uses BPF program that's triggered on sys_enter
system-wide, but has no protection from some unrelated process doing syscall
while selftest is running. This leads to occasional test failures with
unexpected PIDs being returned. Fix that by filtering out all processes that
are not test_progs process.

Fixes: fcda189a5133 ("selftests/bpf: Add test relying only on CO-RE and no recent kernel features")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200731204957.2047119-1-andriin@fb.com

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
b5cc46cd 5a6b1a20

+19 -2
+6 -2
tools/testing/selftests/bpf/prog_tests/core_retro.c
··· 6 6 7 7 void test_core_retro(void) 8 8 { 9 - int err, zero = 0, res, duration = 0; 9 + int err, zero = 0, res, duration = 0, my_pid = getpid(); 10 10 struct test_core_retro *skel; 11 11 12 12 /* load program */ 13 13 skel = test_core_retro__open_and_load(); 14 14 if (CHECK(!skel, "skel_load", "skeleton open/load failed\n")) 15 + goto out_close; 16 + 17 + err = bpf_map_update_elem(bpf_map__fd(skel->maps.exp_tgid_map), &zero, &my_pid, 0); 18 + if (CHECK(err, "map_update", "failed to set expected PID: %d\n", errno)) 15 19 goto out_close; 16 20 17 21 /* attach probe */ ··· 30 26 if (CHECK(err, "map_lookup", "failed to lookup result: %d\n", errno)) 31 27 goto out_close; 32 28 33 - CHECK(res != getpid(), "pid_check", "got %d != exp %d\n", res, getpid()); 29 + CHECK(res != my_pid, "pid_check", "got %d != exp %d\n", res, my_pid); 34 30 35 31 out_close: 36 32 test_core_retro__destroy(skel);
+13
tools/testing/selftests/bpf/progs/test_core_retro.c
··· 13 13 __uint(max_entries, 1); 14 14 __type(key, int); 15 15 __type(value, int); 16 + } exp_tgid_map SEC(".maps"); 17 + 18 + struct { 19 + __uint(type, BPF_MAP_TYPE_ARRAY); 20 + __uint(max_entries, 1); 21 + __type(key, int); 22 + __type(value, int); 16 23 } results SEC(".maps"); 17 24 18 25 SEC("tp/raw_syscalls/sys_enter") ··· 28 21 struct task_struct *task = (void *)bpf_get_current_task(); 29 22 int tgid = BPF_CORE_READ(task, tgid); 30 23 int zero = 0; 24 + int real_tgid = bpf_get_current_pid_tgid() >> 32; 25 + int *exp_tgid = bpf_map_lookup_elem(&exp_tgid_map, &zero); 26 + 27 + /* only pass through sys_enters from test process */ 28 + if (!exp_tgid || *exp_tgid != real_tgid) 29 + return 0; 31 30 32 31 bpf_map_update_elem(&results, &zero, &tgid, 0); 33 32