diff mbox series

[net-next] samples/bpf: Fix map interation in xdp1_user

Message ID 20221013200922.17167-1-gerhard@engleder-embedded.com (mailing list archive)
State Accepted
Commit 05ee658c654bacda03f7fecef367e62aaf8e1cfe
Delegated to: BPF
Headers show
Series [net-next] samples/bpf: Fix map interation in xdp1_user | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: sdf@google.com andrii@kernel.org yhs@fb.com haoluo@google.com jolsa@kernel.org kpsingh@kernel.org song@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-10 fail Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 fail Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 fail Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 fail Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 fail Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc

Commit Message

Gerhard Engleder Oct. 13, 2022, 8:09 p.m. UTC
BPF map iteration in xdp1_user results in endless loop without any
output, because the return value of bpf_map_get_next_key() is checked
against the wrong value.

Other call locations of bpf_map_get_next_key() check for equal 0 for
continuing the iteration. xdp1_user checks against unequal -1. This is
wrong for a function which can return arbitrary negative errno values,
because a return value of e.g. -2 results in an endless loop.

With this fix xdp1_user is printing statistics again:
proto 0:          1 pkt/s
proto 0:          1 pkt/s
proto 17:     107383 pkt/s
proto 17:     881655 pkt/s
proto 17:     882083 pkt/s
proto 17:     881758 pkt/s

Signed-off-by: Gerhard Engleder <gerhard@engleder-embedded.com>
---
 samples/bpf/xdp1_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Song Liu Oct. 17, 2022, 4:27 p.m. UTC | #1
On Thu, Oct 13, 2022 at 2:01 PM Gerhard Engleder
<gerhard@engleder-embedded.com> wrote:
>
> BPF map iteration in xdp1_user results in endless loop without any
> output, because the return value of bpf_map_get_next_key() is checked
> against the wrong value.
>
> Other call locations of bpf_map_get_next_key() check for equal 0 for
> continuing the iteration. xdp1_user checks against unequal -1. This is
> wrong for a function which can return arbitrary negative errno values,
> because a return value of e.g. -2 results in an endless loop.
>
> With this fix xdp1_user is printing statistics again:
> proto 0:          1 pkt/s
> proto 0:          1 pkt/s
> proto 17:     107383 pkt/s
> proto 17:     881655 pkt/s
> proto 17:     882083 pkt/s
> proto 17:     881758 pkt/s
>
> Signed-off-by: Gerhard Engleder <gerhard@engleder-embedded.com>

Acked-by: Song Liu <song@kernel.org>

> ---
>  samples/bpf/xdp1_user.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
> index ac370e638fa3..281dc964de8d 100644
> --- a/samples/bpf/xdp1_user.c
> +++ b/samples/bpf/xdp1_user.c
> @@ -51,7 +51,7 @@ static void poll_stats(int map_fd, int interval)
>
>                 sleep(interval);
>
> -               while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
> +               while (bpf_map_get_next_key(map_fd, &key, &key) == 0) {
>                         __u64 sum = 0;
>
>                         assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
> --
> 2.30.2
>
patchwork-bot+netdevbpf@kernel.org Oct. 19, 2022, 6 p.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Martin KaFai Lau <martin.lau@kernel.org>:

On Thu, 13 Oct 2022 22:09:22 +0200 you wrote:
> BPF map iteration in xdp1_user results in endless loop without any
> output, because the return value of bpf_map_get_next_key() is checked
> against the wrong value.
> 
> Other call locations of bpf_map_get_next_key() check for equal 0 for
> continuing the iteration. xdp1_user checks against unequal -1. This is
> wrong for a function which can return arbitrary negative errno values,
> because a return value of e.g. -2 results in an endless loop.
> 
> [...]

Here is the summary with links:
  - [net-next] samples/bpf: Fix map interation in xdp1_user
    https://git.kernel.org/bpf/bpf-next/c/05ee658c654b

You are awesome, thank you!
diff mbox series

Patch

diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
index ac370e638fa3..281dc964de8d 100644
--- a/samples/bpf/xdp1_user.c
+++ b/samples/bpf/xdp1_user.c
@@ -51,7 +51,7 @@  static void poll_stats(int map_fd, int interval)
 
 		sleep(interval);
 
-		while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
+		while (bpf_map_get_next_key(map_fd, &key, &key) == 0) {
 			__u64 sum = 0;
 
 			assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);