diff mbox series

[bpf-next] selftests/bpf: Fix the snprintf test

Message ID 20210428152501.1024509-1-revest@chromium.org (mailing list archive)
State Accepted
Commit f80f88f0e2f2ef9cd805fad1bbf676b0ecd4b55c
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: Fix the snprintf test | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 7 maintainers not CCed: linux-kselftest@vger.kernel.org netdev@vger.kernel.org yhs@fb.com kafai@fb.com john.fastabend@gmail.com songliubraving@fb.com shuah@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 25 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Florent Revest April 28, 2021, 3:25 p.m. UTC
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>
---
 tools/testing/selftests/bpf/prog_tests/snprintf.c | 2 ++
 tools/testing/selftests/bpf/progs/test_snprintf.c | 5 +++++
 2 files changed, 7 insertions(+)

Comments

Andrii Nakryiko April 30, 2021, 5:46 p.m. UTC | #1
On Wed, Apr 28, 2021 at 8:25 AM Florent Revest <revest@chromium.org> wrote:
>
> 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>
> ---

Applied to bpf tree. Thanks.

>  tools/testing/selftests/bpf/prog_tests/snprintf.c | 2 ++
>  tools/testing/selftests/bpf/progs/test_snprintf.c | 5 +++++
>  2 files changed, 7 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c
> index a958c22aec75..dffbcaa1ec98 100644
> --- a/tools/testing/selftests/bpf/prog_tests/snprintf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
> @@ -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;
>
> diff --git a/tools/testing/selftests/bpf/progs/test_snprintf.c b/tools/testing/selftests/bpf/progs/test_snprintf.c
> index 951a0301c553..e35129bea0a0 100644
> --- a/tools/testing/selftests/bpf/progs/test_snprintf.c
> +++ b/tools/testing/selftests/bpf/progs/test_snprintf.c
> @@ -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",
> --
> 2.31.1.498.g6c1eba8ee3d-goog
>
patchwork-bot+netdevbpf@kernel.org April 30, 2021, 5:50 p.m. UTC | #2
Hello:

This patch was applied to bpf/bpf.git (refs/heads/master):

On Wed, 28 Apr 2021 17:25:01 +0200 you wrote:
> 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'.
> 
> [...]

Here is the summary with links:
  - [bpf-next] selftests/bpf: Fix the snprintf test
    https://git.kernel.org/bpf/bpf/c/f80f88f0e2f2

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c
index a958c22aec75..dffbcaa1ec98 100644
--- a/tools/testing/selftests/bpf/prog_tests/snprintf.c
+++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
@@ -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;
 
diff --git a/tools/testing/selftests/bpf/progs/test_snprintf.c b/tools/testing/selftests/bpf/progs/test_snprintf.c
index 951a0301c553..e35129bea0a0 100644
--- a/tools/testing/selftests/bpf/progs/test_snprintf.c
+++ b/tools/testing/selftests/bpf/progs/test_snprintf.c
@@ -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",