Skip to contentSkip to conversation

tools/bpftool: fix PID fetching with a lot of results

PATCH Applied Andrii Nakryiko 4 participants 5 messages Dec 4, 2020 – Dec 8, 2020

Subsystems (4)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 tools/bpftool: fix PID fetching with a lot of results

In case of having so many PID results that they don't fit into a singe page (4096) bytes, bpftool will erroneously conclude that it got corrupted data due to 4096 not being a multiple of struct pid_iter_entry, so the last entry will be partially truncated. Fix this by sizing the buffer to fit exactly N entries with no truncation in the middle of record.

Fixes d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")
---
 tools/bpf/bpftool/pids.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/pids.c +2 -2
index df7d8ec76036..477e55d59c34 100644
@@ -89,9 +89,9 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
 
 int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
 {
-	char buf[4096];
-	struct pid_iter_bpf *skel;
 	struct pid_iter_entry *e;
+	char buf[4096 / sizeof(*e) * sizeof(*e)];
+	struct pid_iter_bpf *skel;
 	int err, ret, fd = -1, i;
 	libbpf_print_fn_t default_print;
 
Signature · 1 line
2.24.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrii Nakryiko

In case of having so many PID results that they don't fit into a singe page (4096) bytes, bpftool will erroneously conclude that it got corrupted data due to 4096 not being a multiple of struct pid_iter_entry, so the last entry will be partially truncated. Fix this by sizing the buffer to fit exactly N entries with no truncation in the middle of record.

Fixes d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")

Ack with one nit below.

Quoted 9 lines
---
  tools/bpf/bpftool/pids.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/pids.c +0 -0
index df7d8ec76036..477e55d59c34 100644

tools/bpf/bpftool/pids.c libbpf_print_none(__maybe_unused enum libbpf_print_level level, · line 94

Quoted 8 lines
tools/bpf/bpftool/pids.c +2 -2
@@ -89,9 +89,9 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
 
  int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
  {
-	char buf[4096];
-	struct pid_iter_bpf *skel;
  	struct pid_iter_entry *e;
+	char buf[4096 / sizeof(*e) * sizeof(*e)];
+	struct pid_iter_bpf *skel;

No need to move "struct pid_iter_bpf *skel", right?

Quoted 4 lines
tools/bpf/bpftool/pids.c +0 -0
  	int err, ret, fd = -1, i;
  	libbpf_print_fn_t default_print;
 
#2 lore raw
Quoted 27 lines · Yonghong Song
Quoted 8 lines · Andrii Nakryiko

In case of having so many PID results that they don't fit into a singe page (4096) bytes, bpftool will erroneously conclude that it got corrupted data due to 4096 not being a multiple of struct pid_iter_entry, so the last entry will be partially truncated. Fix this by sizing the buffer to fit exactly N entries with no truncation in the middle of record.

Fixes d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")

Ack with one nit below.

Quoted 10 lines
---
  tools/bpf/bpftool/pids.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/pids.c +0 -0
index df7d8ec76036..477e55d59c34 100644
@@ -89,9 +89,9 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
 
Quoted 9 lines

tools/bpf/bpftool/pids.c libbpf_print_none(__maybe_unused enum libbpf_print_level level, · line 94

Quoted 7 lines
tools/bpf/bpftool/pids.c +2 -2
  int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
  {
-     char buf[4096];
-     struct pid_iter_bpf *skel;
      struct pid_iter_entry *e;
+     char buf[4096 / sizeof(*e) * sizeof(*e)];
+     struct pid_iter_bpf *skel;

No need to move "struct pid_iter_bpf *skel", right?

It's actually a move of struct pid_iter_entry *e; in from of char buf[], to be able to use sizeof(*e) instead of sizeof(struct pid_iter_bpf). It's just that diff tool didn't catch this properly :)

Quoted 5 lines
Quoted 5 lines
tools/bpf/bpftool/pids.c +0 -0
      int err, ret, fd = -1, i;
      libbpf_print_fn_t default_print;
 
#3 lore raw
Quoted 38 lines · Andrii Nakryiko
Quoted 27 lines · Yonghong Song
Quoted 8 lines · Andrii Nakryiko

In case of having so many PID results that they don't fit into a singe page (4096) bytes, bpftool will erroneously conclude that it got corrupted data due to 4096 not being a multiple of struct pid_iter_entry, so the last entry will be partially truncated. Fix this by sizing the buffer to fit exactly N entries with no truncation in the middle of record.

Fixes d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")

Ack with one nit below.

Quoted 10 lines
---
   tools/bpf/bpftool/pids.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/pids.c +0 -0
index df7d8ec76036..477e55d59c34 100644
@@ -89,9 +89,9 @@ libbpf_print_none(__maybe_unused enum libbpf_print_level level,
 
Quoted 9 lines

tools/bpf/bpftool/pids.c libbpf_print_none(__maybe_unused enum libbpf_print_level level, · line 94

Quoted 7 lines
tools/bpf/bpftool/pids.c +2 -2
   int build_obj_refs_table(struct obj_refs_table *table, enum bpf_obj_type type)
   {
-     char buf[4096];
-     struct pid_iter_bpf *skel;
       struct pid_iter_entry *e;
+     char buf[4096 / sizeof(*e) * sizeof(*e)];
+     struct pid_iter_bpf *skel;

No need to move "struct pid_iter_bpf *skel", right?

Quoted 3 lines

It's actually a move of struct pid_iter_entry *e; in from of char buf[], to be able to use sizeof(*e) instead of sizeof(struct pid_iter_bpf). It's just that diff tool didn't catch this properly :)

Indeed. Looking at the final code, no unnecessary code churn.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
tools/bpf/bpftool/pids.c +0 -0
       int err, ret, fd = -1, i;
       libbpf_print_fn_t default_print;
 
#4 lore raw
In reply to Andrii Nakryiko
This patch was applied to bpf/bpf.git (refs/heads/master):

Hello:

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

Quoted 10 lines · On Fri, 4 Dec 2020 15:20:01 -0800 you wrote:

In case of having so many PID results that they don't fit into a singe page (4096) bytes, bpftool will erroneously conclude that it got corrupted data due to 4096 not being a multiple of struct pid_iter_entry, so the last entry will be partially truncated. Fix this by sizing the buffer to fit exactly N entries with no truncation in the middle of record.

Fixes d53dee3fe013 ("tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs")

[...]

Here is the summary with links:
- [bpf] tools/bpftool: fix PID fetching with a lot of results

    https://git.kernel.org/bpf/bpf/c/932c60558109

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