Skip to content

Commit f80f88f

Browse files
FlorentRevestanakryiko
authored andcommitted
selftests/bpf: Fix the snprintf test
The BPF program for the snprintf selftest runs on all syscall entries. On busy multicore systems this can cause concurrency issues. For example it was observed that sometimes the userspace part of the test reads " 4 0000" instead of " 4 000" (extra '0' at the end) which seems to happen just before snprintf on another core sets end[-1] = '\0'. This patch adds a pid filter to the test to ensure that no bpf_snprintf() will write over the test's output buffers while the userspace reads the values. Fixes: c2e39c6 ("selftests/bpf: Add a series of tests for bpf_snprintf") Reported-by: Andrii Nakryiko <[email protected]> Signed-off-by: Florent Revest <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d4eecfb commit f80f88f

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void test_snprintf_positive(void)
4343
if (!ASSERT_OK_PTR(skel, "skel_open"))
4444
return;
4545

46+
skel->bss->pid = getpid();
47+
4648
if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach"))
4749
goto cleanup;
4850

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <bpf/bpf_helpers.h>
66
#include <bpf/bpf_tracing.h>
77

8+
__u32 pid = 0;
9+
810
char num_out[64] = {};
911
long num_ret = 0;
1012

@@ -42,6 +44,9 @@ int handler(const void *ctx)
4244
static const char str1[] = "str1";
4345
static const char longstr[] = "longstr";
4446

47+
if ((int)bpf_get_current_pid_tgid() != pid)
48+
return 0;
49+
4550
/* Integer types */
4651
num_ret = BPF_SNPRINTF(num_out, sizeof(num_out),
4752
"%d %u %x %li %llu %lX",

0 commit comments

Comments
 (0)