diff mbox series

[-mm,5/5] bpf/progs: replace hard-coded 16 with TASK_COMM_LEN

Message ID 20211204095256.78042-6-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Phase 2 of task comm cleanups | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR fail merge-conflict
netdev/tree_selection success Not a local patch, async

Commit Message

Yafang Shao Dec. 4, 2021, 9:52 a.m. UTC
The comm used in these bpf progs should have the same size with the comm
field in struct task_struct defined in linux/sched.h. We'd better define
the size as TASK_COMM_LEN to make it more grepable.

The bpf progs kernel code can inlcude vmlinux.h generated by BTF CO-RE
to use TASK_COMM_LEN, while the userspace code can't include it so
TASK_COMM_LEN is specifically defined in the userspace code.

In order to fix redefinitions caused by the new included vmlinux.h, some
headers are removed and some structs are renamed.

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: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Michal Miroslaw <mirq-linux@rere.qmqm.pl>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Kees Cook <keescook@chromium.org>
Cc: Petr Mladek <pmladek@suse.com>
---
 tools/testing/selftests/bpf/prog_tests/ringbuf.c      |  9 +++++----
 .../testing/selftests/bpf/prog_tests/ringbuf_multi.c  |  8 +++++---
 .../selftests/bpf/prog_tests/sk_storage_tracing.c     |  3 ++-
 .../testing/selftests/bpf/prog_tests/test_overhead.c  |  3 ++-
 .../selftests/bpf/prog_tests/trampoline_count.c       |  3 ++-
 .../selftests/bpf/progs/test_core_reloc_kernel.c      | 11 +++++------
 tools/testing/selftests/bpf/progs/test_ringbuf.c      |  8 ++++----
 .../testing/selftests/bpf/progs/test_ringbuf_multi.c  |  8 ++++----
 .../selftests/bpf/progs/test_sk_storage_tracing.c     |  4 ++--
 9 files changed, 31 insertions(+), 26 deletions(-)

Comments

Alexei Starovoitov Dec. 4, 2021, 4:44 p.m. UTC | #1
On Sat, Dec 4, 2021 at 1:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
>  static int process_sample(void *ctx, void *data, size_t len)
>  {
> -       struct sample *s = data;
> +       struct sample_ringbuf *s = data;

This is becoming pointless churn.
Nack.

> index 145028b52ad8..7b1bb73c3501 100644
> --- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> @@ -1,8 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  // Copyright (c) 2019 Facebook
>
> -#include <linux/bpf.h>
> -#include <stdint.h>
> +#include <vmlinux.h>
>  #include <stdbool.h>
>  #include <bpf/bpf_helpers.h>
>  #include <bpf/bpf_core_read.h>
> @@ -23,11 +22,11 @@ struct core_reloc_kernel_output {
>         int comm_len;
>  };
>
> -struct task_struct {
> +struct task_struct_reloc {

Churn that is not even compile tested.
Yafang Shao Dec. 5, 2021, 2:44 a.m. UTC | #2
On Sun, Dec 5, 2021 at 12:44 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Sat, Dec 4, 2021 at 1:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> >  static int process_sample(void *ctx, void *data, size_t len)
> >  {
> > -       struct sample *s = data;
> > +       struct sample_ringbuf *s = data;
>
> This is becoming pointless churn.
> Nack.
>
> > index 145028b52ad8..7b1bb73c3501 100644
> > --- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > @@ -1,8 +1,7 @@
> >  // SPDX-License-Identifier: GPL-2.0
> >  // Copyright (c) 2019 Facebook
> >
> > -#include <linux/bpf.h>
> > -#include <stdint.h>
> > +#include <vmlinux.h>
> >  #include <stdbool.h>
> >  #include <bpf/bpf_helpers.h>
> >  #include <bpf/bpf_core_read.h>
> > @@ -23,11 +22,11 @@ struct core_reloc_kernel_output {
> >         int comm_len;
> >  };
> >
> > -struct task_struct {
> > +struct task_struct_reloc {
>
> Churn that is not even compile tested.

It is strange that I have successfully compiled it....
Below is the compile log,

$ cat make.log | grep test_core_reloc_kernel
  CLNG-BPF [test_maps] test_core_reloc_kernel.o
  GEN-SKEL [test_progs] test_core_reloc_kernel.skel.h
  CLNG-BPF [test_maps] test_core_reloc_kernel.o
  GEN-SKEL [test_progs-no_alu32] test_core_reloc_kernel.skel.h

Also there's no error in the compile log.
Alexei Starovoitov Dec. 5, 2021, 3:13 a.m. UTC | #3
On Sat, Dec 4, 2021 at 6:45 PM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> On Sun, Dec 5, 2021 at 12:44 AM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Sat, Dec 4, 2021 at 1:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> > >
> > >  static int process_sample(void *ctx, void *data, size_t len)
> > >  {
> > > -       struct sample *s = data;
> > > +       struct sample_ringbuf *s = data;
> >
> > This is becoming pointless churn.
> > Nack.
> >
> > > index 145028b52ad8..7b1bb73c3501 100644
> > > --- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > > +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > > @@ -1,8 +1,7 @@
> > >  // SPDX-License-Identifier: GPL-2.0
> > >  // Copyright (c) 2019 Facebook
> > >
> > > -#include <linux/bpf.h>
> > > -#include <stdint.h>
> > > +#include <vmlinux.h>
> > >  #include <stdbool.h>
> > >  #include <bpf/bpf_helpers.h>
> > >  #include <bpf/bpf_core_read.h>
> > > @@ -23,11 +22,11 @@ struct core_reloc_kernel_output {
> > >         int comm_len;
> > >  };
> > >
> > > -struct task_struct {
> > > +struct task_struct_reloc {
> >
> > Churn that is not even compile tested.
>
> It is strange that I have successfully compiled it....
> Below is the compile log,
>
> $ cat make.log | grep test_core_reloc_kernel
>   CLNG-BPF [test_maps] test_core_reloc_kernel.o
>   GEN-SKEL [test_progs] test_core_reloc_kernel.skel.h
>   CLNG-BPF [test_maps] test_core_reloc_kernel.o
>   GEN-SKEL [test_progs-no_alu32] test_core_reloc_kernel.skel.h
>
> Also there's no error in the compile log.

and ran the tests too?
Yafang Shao Dec. 5, 2021, 7:25 a.m. UTC | #4
On Sun, Dec 5, 2021 at 11:13 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Sat, Dec 4, 2021 at 6:45 PM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > On Sun, Dec 5, 2021 at 12:44 AM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > >
> > > On Sat, Dec 4, 2021 at 1:53 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> > > >
> > > >  static int process_sample(void *ctx, void *data, size_t len)
> > > >  {
> > > > -       struct sample *s = data;
> > > > +       struct sample_ringbuf *s = data;
> > >
> > > This is becoming pointless churn.
> > > Nack.
> > >
> > > > index 145028b52ad8..7b1bb73c3501 100644
> > > > --- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > > > +++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
> > > > @@ -1,8 +1,7 @@
> > > >  // SPDX-License-Identifier: GPL-2.0
> > > >  // Copyright (c) 2019 Facebook
> > > >
> > > > -#include <linux/bpf.h>
> > > > -#include <stdint.h>
> > > > +#include <vmlinux.h>
> > > >  #include <stdbool.h>
> > > >  #include <bpf/bpf_helpers.h>
> > > >  #include <bpf/bpf_core_read.h>
> > > > @@ -23,11 +22,11 @@ struct core_reloc_kernel_output {
> > > >         int comm_len;
> > > >  };
> > > >
> > > > -struct task_struct {
> > > > +struct task_struct_reloc {
> > >
> > > Churn that is not even compile tested.
> >
> > It is strange that I have successfully compiled it....
> > Below is the compile log,
> >
> > $ cat make.log | grep test_core_reloc_kernel
> >   CLNG-BPF [test_maps] test_core_reloc_kernel.o
> >   GEN-SKEL [test_progs] test_core_reloc_kernel.skel.h
> >   CLNG-BPF [test_maps] test_core_reloc_kernel.o
> >   GEN-SKEL [test_progs-no_alu32] test_core_reloc_kernel.skel.h
> >
> > Also there's no error in the compile log.
>
> and ran the tests too?

My bad. I thought it was just a name change, which will work well if
it can be compiled successfully.
But it seems the task_struct in this file is a dummy struct, which
will be relocated to the real task_struct defined in the kernel.
We can't include vmlinux.h in this file, as it is for the relocation test.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
index 9a80fe8a6427..39e43245db0a 100644
--- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
@@ -15,14 +15,15 @@ 
 #include "test_ringbuf.lskel.h"
 
 #define EDONE 7777
+#define TASK_COMM_LEN 16
 
 static int duration = 0;
 
-struct sample {
+struct sample_ringbuf {
 	int pid;
 	int seq;
 	long value;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 static int sample_cnt;
@@ -39,7 +40,7 @@  static int atomic_xchg(int *cnt, int val)
 
 static int process_sample(void *ctx, void *data, size_t len)
 {
-	struct sample *s = data;
+	struct sample_ringbuf *s = data;
 
 	atomic_inc(&sample_cnt);
 
@@ -83,7 +84,7 @@  static void *poll_thread(void *input)
 
 void test_ringbuf(void)
 {
-	const size_t rec_sz = BPF_RINGBUF_HDR_SZ + sizeof(struct sample);
+	const size_t rec_sz = BPF_RINGBUF_HDR_SZ + sizeof(struct sample_ringbuf);
 	pthread_t thread;
 	long bg_ret = -1;
 	int err, cnt, rb_fd;
diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
index e945195b24c9..cb592555513f 100644
--- a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
@@ -4,19 +4,21 @@ 
 #include <sys/epoll.h>
 #include "test_ringbuf_multi.skel.h"
 
+#define TASK_COMM_LEN 16
+
 static int duration = 0;
 
-struct sample {
+struct sample_ringbuf {
 	int pid;
 	int seq;
 	long value;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 static int process_sample(void *ctx, void *data, size_t len)
 {
 	int ring = (unsigned long)ctx;
-	struct sample *s = data;
+	struct sample_ringbuf *s = data;
 
 	switch (s->seq) {
 	case 0:
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c b/tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c
index 547ae53cde74..dbbdbf4400d7 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c
@@ -11,11 +11,12 @@ 
 
 #define LO_ADDR6 "::1"
 #define TEST_COMM "test_progs"
+#define TASK_COMM_LEN 16
 
 struct sk_stg {
 	__u32 pid;
 	__u32 last_notclose_state;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 static struct test_sk_storage_tracing *skel;
diff --git a/tools/testing/selftests/bpf/prog_tests/test_overhead.c b/tools/testing/selftests/bpf/prog_tests/test_overhead.c
index 123c68c1917d..7fe60ec12fc4 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_overhead.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_overhead.c
@@ -6,6 +6,7 @@ 
 #include <test_progs.h>
 
 #define MAX_CNT 100000
+#define TASK_COMM_LEN 16
 
 static __u64 time_get_ns(void)
 {
@@ -67,7 +68,7 @@  void test_test_overhead(void)
 	struct bpf_object *obj;
 	struct bpf_link *link;
 	int err, duration = 0;
-	char comm[16] = {};
+	char comm[TASK_COMM_LEN] = {};
 
 	if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L)))
 		return;
diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_count.c b/tools/testing/selftests/bpf/prog_tests/trampoline_count.c
index fc146671b20a..da83f7408aa8 100644
--- a/tools/testing/selftests/bpf/prog_tests/trampoline_count.c
+++ b/tools/testing/selftests/bpf/prog_tests/trampoline_count.c
@@ -5,6 +5,7 @@ 
 #include <test_progs.h>
 
 #define MAX_TRAMP_PROGS 38
+#define TASK_COMM_LEN 16
 
 struct inst {
 	struct bpf_object *obj;
@@ -51,7 +52,7 @@  void serial_test_trampoline_count(void)
 	int err, i = 0, duration = 0;
 	struct bpf_object *obj;
 	struct bpf_link *link;
-	char comm[16] = {};
+	char comm[TASK_COMM_LEN] = {};
 
 	/* attach 'allowed' trampoline programs */
 	for (i = 0; i < MAX_TRAMP_PROGS; i++) {
diff --git a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
index 145028b52ad8..7b1bb73c3501 100644
--- a/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
+++ b/tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c
@@ -1,8 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (c) 2019 Facebook
 
-#include <linux/bpf.h>
-#include <stdint.h>
+#include <vmlinux.h>
 #include <stdbool.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_core_read.h>
@@ -23,11 +22,11 @@  struct core_reloc_kernel_output {
 	int comm_len;
 };
 
-struct task_struct {
+struct task_struct_reloc {
 	int pid;
 	int tgid;
-	char comm[16];
-	struct task_struct *group_leader;
+	char comm[TASK_COMM_LEN];
+	struct task_struct_reloc *group_leader;
 };
 
 #define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
@@ -35,7 +34,7 @@  struct task_struct {
 SEC("raw_tracepoint/sys_enter")
 int test_core_kernel(void *ctx)
 {
-	struct task_struct *task = (void *)bpf_get_current_task();
+	struct task_struct_reloc *task = (void *)bpf_get_current_task();
 	struct core_reloc_kernel_output *out = (void *)&data.out;
 	uint64_t pid_tgid = bpf_get_current_pid_tgid();
 	uint32_t real_tgid = (uint32_t)pid_tgid;
diff --git a/tools/testing/selftests/bpf/progs/test_ringbuf.c b/tools/testing/selftests/bpf/progs/test_ringbuf.c
index eaa7d9dba0be..45b28965f3a5 100644
--- a/tools/testing/selftests/bpf/progs/test_ringbuf.c
+++ b/tools/testing/selftests/bpf/progs/test_ringbuf.c
@@ -1,16 +1,16 @@ 
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (c) 2020 Facebook
 
-#include <linux/bpf.h>
+#include <vmlinux.h>
 #include <bpf/bpf_helpers.h>
 
 char _license[] SEC("license") = "GPL";
 
-struct sample {
+struct sample_ringbuf {
 	int pid;
 	int seq;
 	long value;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 struct {
@@ -39,7 +39,7 @@  SEC("fentry/__x64_sys_getpgid")
 int test_ringbuf(void *ctx)
 {
 	int cur_pid = bpf_get_current_pid_tgid() >> 32;
-	struct sample *sample;
+	struct sample_ringbuf *sample;
 	int zero = 0;
 
 	if (cur_pid != pid)
diff --git a/tools/testing/selftests/bpf/progs/test_ringbuf_multi.c b/tools/testing/selftests/bpf/progs/test_ringbuf_multi.c
index 197b86546dca..7c9db148f168 100644
--- a/tools/testing/selftests/bpf/progs/test_ringbuf_multi.c
+++ b/tools/testing/selftests/bpf/progs/test_ringbuf_multi.c
@@ -1,16 +1,16 @@ 
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (c) 2020 Facebook
 
-#include <linux/bpf.h>
+#include <vmlinux.h>
 #include <bpf/bpf_helpers.h>
 
 char _license[] SEC("license") = "GPL";
 
-struct sample {
+struct sample_ringbuf {
 	int pid;
 	int seq;
 	long value;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 struct ringbuf_map {
@@ -55,7 +55,7 @@  SEC("tp/syscalls/sys_enter_getpgid")
 int test_ringbuf(void *ctx)
 {
 	int cur_pid = bpf_get_current_pid_tgid() >> 32;
-	struct sample *sample;
+	struct sample_ringbuf *sample;
 	void *rb;
 	int zero = 0;
 
diff --git a/tools/testing/selftests/bpf/progs/test_sk_storage_tracing.c b/tools/testing/selftests/bpf/progs/test_sk_storage_tracing.c
index 6dc1f28fc4b6..cc41a09e3130 100644
--- a/tools/testing/selftests/bpf/progs/test_sk_storage_tracing.c
+++ b/tools/testing/selftests/bpf/progs/test_sk_storage_tracing.c
@@ -9,7 +9,7 @@ 
 struct sk_stg {
 	__u32 pid;
 	__u32 last_notclose_state;
-	char comm[16];
+	char comm[TASK_COMM_LEN];
 };
 
 struct {
@@ -27,7 +27,7 @@  struct {
 	__type(value, int);
 } del_sk_stg_map SEC(".maps");
 
-char task_comm[16] = "";
+char task_comm[TASK_COMM_LEN] = "";
 
 SEC("tp_btf/inet_sock_set_state")
 int BPF_PROG(trace_inet_sock_set_state, struct sock *sk, int oldstate,