selftests/bpf: Fix the snprintf test
authorFlorent Revest <revest@chromium.org>
Wed, 28 Apr 2021 15:25:01 +0000 (17:25 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 30 Apr 2021 17:36:59 +0000 (10:36 -0700)
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: c2e39c6bdc7e ("selftests/bpf: Add a series of tests for bpf_snprintf")
Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210428152501.1024509-1-revest@chromium.org
tools/testing/selftests/bpf/prog_tests/snprintf.c
tools/testing/selftests/bpf/progs/test_snprintf.c

index a958c22aec75cdd3fb7cf22a66e2e75e23899fa9..dffbcaa1ec9804269bec40347acf1477efa57c88 100644 (file)
@@ -43,6 +43,8 @@ void test_snprintf_positive(void)
        if (!ASSERT_OK_PTR(skel, "skel_open"))
                return;
 
+       skel->bss->pid = getpid();
+
        if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach"))
                goto cleanup;
 
index 951a0301c553a324d5b6429ea67fc62f9e395eea..e35129bea0a065f8a26834b6f90b5f21b8186241 100644 (file)
@@ -5,6 +5,8 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
+__u32 pid = 0;
+
 char num_out[64] = {};
 long num_ret = 0;
 
@@ -42,6 +44,9 @@ int handler(const void *ctx)
        static const char str1[] = "str1";
        static const char longstr[] = "longstr";
 
+       if ((int)bpf_get_current_pid_tgid() != pid)
+               return 0;
+
        /* Integer types */
        num_ret  = BPF_SNPRINTF(num_out, sizeof(num_out),
                                "%d %u %x %li %llu %lX",