diff mbox series

[v5,11/15] tools/bpf/bpftool: use TASK_COMM_LEN_16 instead of hard-coded 16

Message ID 20211021034603.4458-2-laoar.shao@gmail.com (mailing list archive)
State Not Applicable
Delegated to: BPF
Headers show
Series extend task comm from 16 to 24 for CONFIG_BASE_FULL | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch
bpf/vmtest-bpf-next-PR fail merge-conflict
bpf/vmtest-bpf success VM_Test
bpf/vmtest-bpf-PR success PR summary

Commit Message

Yafang Shao Oct. 21, 2021, 3:45 a.m. UTC
Use TASK_COMM_LEN_16 instead of hard-coded 16 to make it more grepable.
It uses bpf_probe_read_kernel() to get task comm, which may return a
string without nul terminator. We should use bpf_probe_read_kernel_str()
instead.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Petr Mladek <pmladek@suse.com>
---
 tools/bpf/bpftool/Makefile                | 1 +
 tools/bpf/bpftool/main.h                  | 3 ++-
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++--
 tools/bpf/bpftool/skeleton/pid_iter.h     | 4 +++-
 4 files changed, 8 insertions(+), 4 deletions(-)

Comments

Andrii Nakryiko Oct. 21, 2021, 10:38 p.m. UTC | #1
On Wed, Oct 20, 2021 at 8:46 PM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> Use TASK_COMM_LEN_16 instead of hard-coded 16 to make it more grepable.
> It uses bpf_probe_read_kernel() to get task comm, which may return a
> string without nul terminator. We should use bpf_probe_read_kernel_str()
> instead.
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Cc: Petr Mladek <pmladek@suse.com>
> ---
>  tools/bpf/bpftool/Makefile                | 1 +
>  tools/bpf/bpftool/main.h                  | 3 ++-
>  tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++--
>  tools/bpf/bpftool/skeleton/pid_iter.h     | 4 +++-
>  4 files changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
> index d73232be1e99..33fbde84993c 100644
> --- a/tools/bpf/bpftool/Makefile
> +++ b/tools/bpf/bpftool/Makefile
> @@ -164,6 +164,7 @@ $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
>         $(QUIET_CLANG)$(CLANG) \
>                 -I$(if $(OUTPUT),$(OUTPUT),.) \
>                 -I$(srctree)/tools/include/uapi/ \
> +               -I$(srctree)/tools/include/ \

bpftool shouldn't rely on internal kernel headers for compilation. If
you want to have TASK_COMM_LEN_16 constant for grep-ability, just
#define it where appropriate

>                 -I$(LIBBPF_PATH) \
>                 -I$(srctree)/tools/lib \
>                 -g -O2 -Wall -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
> index 90caa42aac4c..5efa27188f68 100644
> --- a/tools/bpf/bpftool/main.h
> +++ b/tools/bpf/bpftool/main.h
> @@ -12,6 +12,7 @@
>  #include <linux/compiler.h>
>  #include <linux/kernel.h>
>  #include <linux/hashtable.h>
> +#include <linux/sched/task.h>
>  #include <tools/libc_compat.h>
>

[...]
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index d73232be1e99..33fbde84993c 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -164,6 +164,7 @@  $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
 	$(QUIET_CLANG)$(CLANG) \
 		-I$(if $(OUTPUT),$(OUTPUT),.) \
 		-I$(srctree)/tools/include/uapi/ \
+		-I$(srctree)/tools/include/ \
 		-I$(LIBBPF_PATH) \
 		-I$(srctree)/tools/lib \
 		-g -O2 -Wall -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 90caa42aac4c..5efa27188f68 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -12,6 +12,7 @@ 
 #include <linux/compiler.h>
 #include <linux/kernel.h>
 #include <linux/hashtable.h>
+#include <linux/sched/task.h>
 #include <tools/libc_compat.h>
 
 #include <bpf/libbpf.h>
@@ -124,7 +125,7 @@  struct obj_refs_table {
 
 struct obj_ref {
 	int pid;
-	char comm[16];
+	char comm[TASK_COMM_LEN_16];
 };
 
 struct obj_refs {
diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c
index d9b420972934..f70702fcb224 100644
--- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c
+++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c
@@ -71,8 +71,8 @@  int iter(struct bpf_iter__task_file *ctx)
 
 	e.pid = task->tgid;
 	e.id = get_obj_id(file->private_data, obj_type);
-	bpf_probe_read_kernel(&e.comm, sizeof(e.comm),
-			      task->group_leader->comm);
+	bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
+				  task->group_leader->comm);
 	bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
 
 	return 0;
diff --git a/tools/bpf/bpftool/skeleton/pid_iter.h b/tools/bpf/bpftool/skeleton/pid_iter.h
index 5692cf257adb..675b2916567e 100644
--- a/tools/bpf/bpftool/skeleton/pid_iter.h
+++ b/tools/bpf/bpftool/skeleton/pid_iter.h
@@ -3,10 +3,12 @@ 
 #ifndef __PID_ITER_H
 #define __PID_ITER_H
 
+#include <linux/sched/task.h>
+
 struct pid_iter_entry {
 	__u32 id;
 	int pid;
-	char comm[16];
+	char comm[TASK_COMM_LEN_16];
 };
 
 #endif