Subject: Re: [PATCH v4 bpf-next] bpf/selftests: fold test_current_pid_tgid_new_ns into test_progs.

From: Carlos Antonio Neira Bustos <[email-masked]>
Date: Thu, 6 Aug 2020 10:03:20 -0400

On Wed, Aug 05, 2020 at 01:15:05PM -0700, Andrii Nakryiko wrote:
> On Wed, Aug 5, 2020 at 1:06 PM Carlos Neira <[email-masked]> wrote:
> >
> > Currently tests for bpf_get_ns_current_pid_tgid() are outside test_progs.
> > This change folds a test case into test_progs.
> >
> > Changes from V3:
> >  - STAT(2) check changed from CHECK_FAIL to CHECK.
> >  - Changed uses of _open_ to _open_and_load.
> >  - Fixed error codes were not being returned on exit.
> >  - Removed unnecessary dependency on Makefile
> >
> > Signed-off-by: Carlos Neira <[email-masked]>
> > ---
> 
> bpf-next is closed, you'll have to re-submit once it opens in roughly
> two weeks. Looks good overall, few minor things below, please
> incorporate into next revision with my ack:
> 
> Acked-by: Andrii Nakryiko <[email-masked]>
> 
> >  tools/testing/selftests/bpf/.gitignore        |   2 +-
> >  tools/testing/selftests/bpf/Makefile          |   4 +-
> >  .../bpf/prog_tests/ns_current_pid_tgid.c      |  85 ----------
> >  .../bpf/prog_tests/ns_current_pidtgid.c       |  54 ++++++
> >  .../bpf/progs/test_ns_current_pid_tgid.c      |  37 ----
> >  .../bpf/progs/test_ns_current_pidtgid.c       |  25 +++
> >  .../bpf/test_current_pid_tgid_new_ns.c        | 159 ------------------
> >  .../bpf/test_ns_current_pidtgid_newns.c       |  91 ++++++++++
> >  8 files changed, 173 insertions(+), 284 deletions(-)
> >  delete mode 100644 tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
> >  create mode 100644 tools/testing/selftests/bpf/prog_tests/ns_current_pidtgid.c
> >  delete mode 100644 tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
> >  create mode 100644 tools/testing/selftests/bpf/progs/test_ns_current_pidtgid.c
> >  delete mode 100644 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c
> >  create mode 100644 tools/testing/selftests/bpf/test_ns_current_pidtgid_newns.c
> >
> > diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftests/bpf/.gitignore
> > index 1bb204cee853..022055f23592 100644
> > --- a/tools/testing/selftests/bpf/.gitignore
> > +++ b/tools/testing/selftests/bpf/.gitignore
> > @@ -30,8 +30,8 @@ test_tcpnotify_user
> >  test_libbpf
> >  test_tcp_check_syncookie_user
> >  test_sysctl
> > -test_current_pid_tgid_new_ns
> >  xdping
> > +test_ns_current_pidtgid_newns
> >  test_cpp
> >  *.skel.h
> >  /no_alu32
> > diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> > index e7a8cf83ba48..92fb616cdd27 100644
> > --- a/tools/testing/selftests/bpf/Makefile
> > +++ b/tools/testing/selftests/bpf/Makefile
> > @@ -36,8 +36,8 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
> >         test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
> >         test_cgroup_storage \
> >         test_netcnt test_tcpnotify_user test_sock_fields test_sysctl \
> > -       test_progs-no_alu32 \
> > -       test_current_pid_tgid_new_ns
> > +       test_progs-no_alu32\
> 
> accidentally removed space?
> 
> > +       test_ns_current_pidtgid_newns
> >
> >  # Also test bpf-gcc, if present
> >  ifneq ($(BPF_GCC),)
> 
> [...]
> 
> > +
> > +void test_ns_current_pidtgid(void)
> > +{
> > +       struct test_ns_current_pidtgid__bss  *bss;
> > +       struct test_ns_current_pidtgid *skel;
> > +       int err, duration = 0;
> > +       struct stat st;
> > +       __u64 id;
> > +
> > +       skel = test_ns_current_pidtgid__open_and_load();
> > +       CHECK(!skel, "skel_open_load", "failed to load skeleton\n");
> > +               goto cleanup;
> > +
> > +       pid_t tid = syscall(SYS_gettid);
> > +       pid_t pid = getpid();
> 
> hm... I probably missed this last time. This is not a valid C89
> standard-compliant code, all variables have to be declared up top,
> please split variable declaration and initialization.
> 
> > +
> > +       id = (__u64) tid << 32 | pid;
> > +
> > +       err = stat("/proc/self/ns/pid", &st);
> > +       if (CHECK(err, "stat", "failed /proc/self/ns/pid: %d", err))
> > +               goto cleanup;
> > +
> 
> [...]
> 
> > diff --git a/tools/testing/selftests/bpf/progs/test_ns_current_pidtgid.c b/tools/testing/selftests/bpf/progs/test_ns_current_pidtgid.c
> > new file mode 100644
> > index 000000000000..9818a56510d9
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/test_ns_current_pidtgid.c
> > @@ -0,0 +1,25 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/* Copyright (c) 2019 Carlos Neira [email-masked] */
> > +
> > +#include <linux/bpf.h>
> > +#include <stdint.h>
> > +#include <bpf/bpf_helpers.h>
> > +
> > +__u64 user_pid_tgid = 0;
> > +__u64 dev = 0;
> > +__u64 ino = 0;
> > +
> > +SEC("raw_tracepoint/sys_enter")
> > +int handler(const void *ctx)
> > +{
> > +       struct bpf_pidns_info nsdata;
> > +
> > +       if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata,
> > +                  sizeof(struct bpf_pidns_info)))
> > +               return 0;
> > +       user_pid_tgid = (__u64)nsdata.tgid << 32 | nsdata.pid;
> 
> nit: good idea to put () around << expression when combined with other
> bitwise operators.
> 
> > +
> > +       return 0;
> > +}
> > +
> > +char _license[] SEC("license") = "GPL";
> 
> [...]
> 
> > +static int newns_pidtgid(void *arg)
> > +{
> > +       struct test_ns_current_pidtgid__bss  *bss;
> > +       struct test_ns_current_pidtgid *skel;
> > +       int pidns_fd = 0, err = 0;
> > +       pid_t pid, tid;
> > +       struct stat st;
> > +       __u64 id;
> > +
> > +       skel = test_ns_current_pidtgid__open_and_load();
> > +       if (!skel) {
> > +               perror("Failed to load skeleton");
> > +               goto cleanup;
> > +       }
> > +
> > +       tid = syscall(SYS_gettid);
> > +       pid = getpid();
> > +       id = (__u64) tid << 32 | pid;
> 
> see, you don't do it here :)
> 
> 
> > +
> > +       if (stat("/proc/self/ns/pid", &st)) {
> > +               printf("Failed to stat /proc/self/ns/pid: %s\n",
> > +                       strerror(errno));
> > +               goto cleanup;
> > +       }
> > +
> > +       bss = skel->bss;
> > +       bss->dev = st.st_dev;
> > +       bss->ino = st.st_ino;
> > +       bss->user_pid_tgid = 0;
> > +
> 
> [...]

Andrii,

Thank you very much, I'll incorporate these changes on the next revision.

Bests.