bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH v2 bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage
       [not found] ` <20210126085923.469759-3-songliubraving@fb.com>
@ 2021-01-27 21:21   ` Andrii Nakryiko
  2021-01-27 21:43     ` Song Liu
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2021-01-27 21:21 UTC (permalink / raw)
  To: Song Liu
  Cc: bpf, Networking, open list, Ingo Molnar, Peter Ziljstra,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	john fastabend, KP Singh, Kernel Team, Hao Luo

On Tue, Jan 26, 2021 at 1:21 AM Song Liu <songliubraving@fb.com> wrote:
>
> Task local storage is enabled for tracing programs. Add two tests for
> task local storage without CONFIG_BPF_LSM.
>
> The first test measures the duration of a syscall by storing sys_enter
> time in task local storage.
>
> The second test checks whether the kernel allows allocating task local
> storage in exit_creds() (which it should not).
>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  .../bpf/prog_tests/task_local_storage.c       | 85 +++++++++++++++++++
>  .../selftests/bpf/progs/task_local_storage.c  | 56 ++++++++++++
>  .../bpf/progs/task_local_storage_exit_creds.c | 32 +++++++
>  3 files changed, 173 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/task_local_storage.c
>  create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
>  create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
> new file mode 100644
> index 0000000000000..a8e2d3a476145
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
> @@ -0,0 +1,85 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Facebook */
> +
> +#include <sys/types.h>
> +#include <unistd.h>
> +#include <test_progs.h>
> +#include "task_local_storage.skel.h"
> +#include "task_local_storage_exit_creds.skel.h"
> +
> +static unsigned int duration;
> +
> +static void check_usleep_duration(struct task_local_storage *skel,
> +                                 __u64 time_us)
> +{
> +       __u64 syscall_duration;
> +
> +       usleep(time_us);
> +
> +       /* save syscall_duration measure in usleep() */
> +       syscall_duration = skel->bss->syscall_duration;
> +
> +       /* time measured by the BPF program (in nanoseconds) should be
> +        * within +/- 20% of time_us * 1000.
> +        */
> +       CHECK(syscall_duration < 800 * time_us, "syscall_duration",
> +             "syscall_duration was too small\n");
> +       CHECK(syscall_duration > 1200 * time_us, "syscall_duration",
> +             "syscall_duration was too big\n");

this is going to be very flaky, especially in Travis CI. Can you
please use something more stable that doesn't rely on time?

> +}
> +
> +static void test_syscall_duration(void)
> +{
> +       struct task_local_storage *skel;
> +       int err;
> +
> +       skel = task_local_storage__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
> +               return;
> +
> +       skel->bss->target_pid = getpid();

you are getting process ID, but comparing it with thread ID in BPF
code. It will stop working properly if/when tests will be run in
separate threads, so please use gettid() instead.

> +
> +       err = task_local_storage__attach(skel);
> +       if (!ASSERT_OK(err, "skel_attach"))
> +               goto out;
> +
> +       check_usleep_duration(skel, 2000);
> +       check_usleep_duration(skel, 3000);
> +       check_usleep_duration(skel, 4000);
> +
> +out:
> +       task_local_storage__destroy(skel);
> +}
> +
> +static void test_exit_creds(void)
> +{
> +       struct task_local_storage_exit_creds *skel;
> +       int err;
> +
> +       skel = task_local_storage_exit_creds__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
> +               return;
> +
> +       err = task_local_storage_exit_creds__attach(skel);
> +       if (!ASSERT_OK(err, "skel_attach"))
> +               goto out;
> +
> +       /* trigger at least one exit_creds() */
> +       if (CHECK_FAIL(system("ls > /dev/null")))
> +               goto out;
> +
> +       /* sync rcu, so the following reads could get latest values */
> +       kern_sync_rcu();

what are we waiting for here? you don't detach anything... system() is
definitely going to complete by now, so whatever counter was or was
not updated will be reflected here. Seems like kern_sync_rcu() is not
needed?

> +       ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
> +       ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
> +out:
> +       task_local_storage_exit_creds__destroy(skel);
> +}
> +
> +void test_task_local_storage(void)
> +{
> +       if (test__start_subtest("syscall_duration"))
> +               test_syscall_duration();
> +       if (test__start_subtest("exit_creds"))
> +               test_exit_creds();
> +}

[...]

> +int valid_ptr_count = 0;
> +int null_ptr_count = 0;
> +
> +SEC("fentry/exit_creds")
> +int BPF_PROG(trace_exit_creds, struct task_struct *task)
> +{
> +       __u64 *ptr;
> +
> +       ptr = bpf_task_storage_get(&task_storage, task, 0,
> +                                  BPF_LOCAL_STORAGE_GET_F_CREATE);
> +       if (ptr)
> +               valid_ptr_count++;
> +       else
> +               null_ptr_count++;


use atomic increments?

> +       return 0;
> +}
> --
> 2.24.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 bpf-next 3/4] bpf: runqslower: prefer use local KBUILD_OUTPUT/vmlinux or vmlinux
       [not found] ` <20210126085923.469759-4-songliubraving@fb.com>
@ 2021-01-27 21:22   ` Andrii Nakryiko
  0 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2021-01-27 21:22 UTC (permalink / raw)
  To: Song Liu
  Cc: bpf, Networking, open list, Ingo Molnar, Peter Ziljstra,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	john fastabend, KP Singh, Kernel Team, Hao Luo

On Tue, Jan 26, 2021 at 1:11 AM Song Liu <songliubraving@fb.com> wrote:
>
> Update the Makefile to prefer using KBUILD_OUTPUT/vmlinux (for selftests)
> or ../../../vmlinux. These two files should have latest definitions for
> vmlinux.h.
>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  tools/bpf/runqslower/Makefile | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/runqslower/Makefile b/tools/bpf/runqslower/Makefile
> index 4d5ca54fcd4c8..1f75c95d4d023 100644
> --- a/tools/bpf/runqslower/Makefile
> +++ b/tools/bpf/runqslower/Makefile
> @@ -19,7 +19,9 @@ CFLAGS := -g -Wall
>
>  # Try to detect best kernel BTF source
>  KERNEL_REL := $(shell uname -r)
> -VMLINUX_BTF_PATHS := /sys/kernel/btf/vmlinux /boot/vmlinux-$(KERNEL_REL)
> +VMLINUX_BTF_PATHS := $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \

O= overrides KBUILD_OUTPUT=, so please handle it first. See
selftests/bpf/Makefile.

> +       ../../../vmlinux /sys/kernel/btf/vmlinux \
> +       /boot/vmlinux-$(KERNEL_REL)
>  VMLINUX_BTF_PATH := $(or $(VMLINUX_BTF),$(firstword                           \
>                                           $(wildcard $(VMLINUX_BTF_PATHS))))
>
> --
> 2.24.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 bpf-next 4/4] bpf: runqslower: use task local storage
       [not found] ` <20210126085923.469759-5-songliubraving@fb.com>
@ 2021-01-27 21:30   ` Andrii Nakryiko
  0 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2021-01-27 21:30 UTC (permalink / raw)
  To: Song Liu
  Cc: bpf, Networking, open list, Ingo Molnar, Peter Ziljstra,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	john fastabend, KP Singh, Kernel Team, Hao Luo

On Tue, Jan 26, 2021 at 1:11 AM Song Liu <songliubraving@fb.com> wrote:
>
> Replace hashtab with task local storage in runqslower. This improves the
> performance of these BPF programs. The following table summarizes average
> runtime of these programs, in nanoseconds:
>
>                           task-local   hash-prealloc   hash-no-prealloc
> handle__sched_wakeup             125             340               3124
> handle__sched_wakeup_new        2812            1510               2998
> handle__sched_switch             151             208                991
>
> Note that, task local storage gives better performance than hashtab for
> handle__sched_wakeup and handle__sched_switch. On the other hand, for
> handle__sched_wakeup_new, task local storage is slower than hashtab with
> prealloc. This is because handle__sched_wakeup_new accesses the data for
> the first time, so it has to allocate the data for task local storage.
> Once the initial allocation is done, subsequent accesses, as those in
> handle__sched_wakeup, are much faster with task local storage. If we
> disable hashtab prealloc, task local storage is much faster for all 3
> functions.
>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>  tools/bpf/runqslower/runqslower.bpf.c | 31 ++++++++++++++++-----------
>  1 file changed, 19 insertions(+), 12 deletions(-)
>
> diff --git a/tools/bpf/runqslower/runqslower.bpf.c b/tools/bpf/runqslower/runqslower.bpf.c
> index 1f18a409f0443..a597a23d79939 100644
> --- a/tools/bpf/runqslower/runqslower.bpf.c
> +++ b/tools/bpf/runqslower/runqslower.bpf.c
> @@ -11,9 +11,9 @@ const volatile __u64 min_us = 0;
>  const volatile pid_t targ_pid = 0;
>
>  struct {
> -       __uint(type, BPF_MAP_TYPE_HASH);
> -       __uint(max_entries, 10240);
> -       __type(key, u32);
> +       __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
> +       __uint(map_flags, BPF_F_NO_PREALLOC);
> +       __type(key, int);
>         __type(value, u64);
>  } start SEC(".maps");
>
> @@ -25,15 +25,18 @@ struct {
>
>  /* record enqueue timestamp */
>  __always_inline
> -static int trace_enqueue(u32 tgid, u32 pid)
> +static int trace_enqueue(struct task_struct *t)
>  {
> -       u64 ts;
> +       u32 pid = t->pid;
> +       u64 *ptr;
>
>         if (!pid || (targ_pid && targ_pid != pid))
>                 return 0;
>
> -       ts = bpf_ktime_get_ns();
> -       bpf_map_update_elem(&start, &pid, &ts, 0);
> +       ptr = bpf_task_storage_get(&start, t, 0,
> +                                  BPF_LOCAL_STORAGE_GET_F_CREATE);
> +       if (ptr)
> +               *ptr = bpf_ktime_get_ns();

nit: if (!ptr) return 0; is more in line with general handling of
error/unusual conditions. Keeps main logic sequential (especially if
we need to add some extra steps later.


>         return 0;
>  }
>
> @@ -43,7 +46,7 @@ int handle__sched_wakeup(u64 *ctx)
>         /* TP_PROTO(struct task_struct *p) */
>         struct task_struct *p = (void *)ctx[0];
>
> -       return trace_enqueue(p->tgid, p->pid);
> +       return trace_enqueue(p);
>  }
>
>  SEC("tp_btf/sched_wakeup_new")

[...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage
  2021-01-27 21:21   ` [PATCH v2 bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage Andrii Nakryiko
@ 2021-01-27 21:43     ` Song Liu
  2021-01-27 23:30       ` Andrii Nakryiko
  0 siblings, 1 reply; 5+ messages in thread
From: Song Liu @ 2021-01-27 21:43 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Networking, open list, Ingo Molnar, Peter Ziljstra,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	john fastabend, KP Singh, Kernel Team, Hao Luo



> On Jan 27, 2021, at 1:21 PM, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> 
> On Tue, Jan 26, 2021 at 1:21 AM Song Liu <songliubraving@fb.com> wrote:
>> 
>> Task local storage is enabled for tracing programs. Add two tests for
>> task local storage without CONFIG_BPF_LSM.
>> 
>> The first test measures the duration of a syscall by storing sys_enter
>> time in task local storage.
>> 
>> The second test checks whether the kernel allows allocating task local
>> storage in exit_creds() (which it should not).
>> 
>> Signed-off-by: Song Liu <songliubraving@fb.com>
>> ---
>> .../bpf/prog_tests/task_local_storage.c       | 85 +++++++++++++++++++
>> .../selftests/bpf/progs/task_local_storage.c  | 56 ++++++++++++
>> .../bpf/progs/task_local_storage_exit_creds.c | 32 +++++++
>> 3 files changed, 173 insertions(+)
>> create mode 100644 tools/testing/selftests/bpf/prog_tests/task_local_storage.c
>> create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
>> create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c
>> 
>> diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
>> new file mode 100644
>> index 0000000000000..a8e2d3a476145
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
>> @@ -0,0 +1,85 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2021 Facebook */
>> +
>> +#include <sys/types.h>
>> +#include <unistd.h>
>> +#include <test_progs.h>
>> +#include "task_local_storage.skel.h"
>> +#include "task_local_storage_exit_creds.skel.h"
>> +
>> +static unsigned int duration;
>> +
>> +static void check_usleep_duration(struct task_local_storage *skel,
>> +                                 __u64 time_us)
>> +{
>> +       __u64 syscall_duration;
>> +
>> +       usleep(time_us);
>> +
>> +       /* save syscall_duration measure in usleep() */
>> +       syscall_duration = skel->bss->syscall_duration;
>> +
>> +       /* time measured by the BPF program (in nanoseconds) should be
>> +        * within +/- 20% of time_us * 1000.
>> +        */
>> +       CHECK(syscall_duration < 800 * time_us, "syscall_duration",
>> +             "syscall_duration was too small\n");
>> +       CHECK(syscall_duration > 1200 * time_us, "syscall_duration",
>> +             "syscall_duration was too big\n");
> 
> this is going to be very flaky, especially in Travis CI. Can you
> please use something more stable that doesn't rely on time?

Let me try. 

> 
>> +}
>> +
>> +static void test_syscall_duration(void)
>> +{
>> +       struct task_local_storage *skel;
>> +       int err;
>> +
>> +       skel = task_local_storage__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
>> +               return;
>> +
>> +       skel->bss->target_pid = getpid();
> 
> you are getting process ID, but comparing it with thread ID in BPF
> code. It will stop working properly if/when tests will be run in
> separate threads, so please use gettid() instead.

Will fix. 

> 
>> +
>> +       err = task_local_storage__attach(skel);
>> +       if (!ASSERT_OK(err, "skel_attach"))
>> +               goto out;
>> +
>> +       check_usleep_duration(skel, 2000);
>> +       check_usleep_duration(skel, 3000);
>> +       check_usleep_duration(skel, 4000);
>> +
>> +out:
>> +       task_local_storage__destroy(skel);
>> +}
>> +
>> +static void test_exit_creds(void)
>> +{
>> +       struct task_local_storage_exit_creds *skel;
>> +       int err;
>> +
>> +       skel = task_local_storage_exit_creds__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
>> +               return;
>> +
>> +       err = task_local_storage_exit_creds__attach(skel);
>> +       if (!ASSERT_OK(err, "skel_attach"))
>> +               goto out;
>> +
>> +       /* trigger at least one exit_creds() */
>> +       if (CHECK_FAIL(system("ls > /dev/null")))
>> +               goto out;
>> +
>> +       /* sync rcu, so the following reads could get latest values */
>> +       kern_sync_rcu();
> 
> what are we waiting for here? you don't detach anything... system() is
> definitely going to complete by now, so whatever counter was or was
> not updated will be reflected here. Seems like kern_sync_rcu() is not
> needed?

IIUC, without sync_ruc(), even system() is finished, the kernel may not 
have called exit_creds() for the "ls" task yet. Then the following check
for null_ptr_count != 0 would fail. 

> 
>> +       ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
>> +       ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
>> +out:
>> +       task_local_storage_exit_creds__destroy(skel);
>> +}
>> +
>> +void test_task_local_storage(void)
>> +{
>> +       if (test__start_subtest("syscall_duration"))
>> +               test_syscall_duration();
>> +       if (test__start_subtest("exit_creds"))
>> +               test_exit_creds();
>> +}
> 
> [...]
> 
>> +int valid_ptr_count = 0;
>> +int null_ptr_count = 0;
>> +
>> +SEC("fentry/exit_creds")
>> +int BPF_PROG(trace_exit_creds, struct task_struct *task)
>> +{
>> +       __u64 *ptr;
>> +
>> +       ptr = bpf_task_storage_get(&task_storage, task, 0,
>> +                                  BPF_LOCAL_STORAGE_GET_F_CREATE);
>> +       if (ptr)
>> +               valid_ptr_count++;
>> +       else
>> +               null_ptr_count++;
> 
> 
> use atomic increments?

Do you mean __sync_fetch_and_add()? 

> 
>> +       return 0;
>> +}
>> --
>> 2.24.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage
  2021-01-27 21:43     ` Song Liu
@ 2021-01-27 23:30       ` Andrii Nakryiko
  0 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2021-01-27 23:30 UTC (permalink / raw)
  To: Song Liu
  Cc: bpf, Networking, open list, Ingo Molnar, Peter Ziljstra,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	john fastabend, KP Singh, Kernel Team, Hao Luo

On Wed, Jan 27, 2021 at 1:43 PM Song Liu <songliubraving@fb.com> wrote:
>
>
>
> > On Jan 27, 2021, at 1:21 PM, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> >
> > On Tue, Jan 26, 2021 at 1:21 AM Song Liu <songliubraving@fb.com> wrote:
> >>
> >> Task local storage is enabled for tracing programs. Add two tests for
> >> task local storage without CONFIG_BPF_LSM.
> >>
> >> The first test measures the duration of a syscall by storing sys_enter
> >> time in task local storage.
> >>
> >> The second test checks whether the kernel allows allocating task local
> >> storage in exit_creds() (which it should not).
> >>
> >> Signed-off-by: Song Liu <songliubraving@fb.com>
> >> ---
> >> .../bpf/prog_tests/task_local_storage.c       | 85 +++++++++++++++++++
> >> .../selftests/bpf/progs/task_local_storage.c  | 56 ++++++++++++
> >> .../bpf/progs/task_local_storage_exit_creds.c | 32 +++++++
> >> 3 files changed, 173 insertions(+)
> >> create mode 100644 tools/testing/selftests/bpf/prog_tests/task_local_storage.c
> >> create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
> >> create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c
> >>
> >> diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
> >> new file mode 100644
> >> index 0000000000000..a8e2d3a476145
> >> --- /dev/null
> >> +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
> >> @@ -0,0 +1,85 @@
> >> +// SPDX-License-Identifier: GPL-2.0
> >> +/* Copyright (c) 2021 Facebook */
> >> +
> >> +#include <sys/types.h>
> >> +#include <unistd.h>
> >> +#include <test_progs.h>
> >> +#include "task_local_storage.skel.h"
> >> +#include "task_local_storage_exit_creds.skel.h"
> >> +
> >> +static unsigned int duration;
> >> +
> >> +static void check_usleep_duration(struct task_local_storage *skel,
> >> +                                 __u64 time_us)
> >> +{
> >> +       __u64 syscall_duration;
> >> +
> >> +       usleep(time_us);
> >> +
> >> +       /* save syscall_duration measure in usleep() */
> >> +       syscall_duration = skel->bss->syscall_duration;
> >> +
> >> +       /* time measured by the BPF program (in nanoseconds) should be
> >> +        * within +/- 20% of time_us * 1000.
> >> +        */
> >> +       CHECK(syscall_duration < 800 * time_us, "syscall_duration",
> >> +             "syscall_duration was too small\n");
> >> +       CHECK(syscall_duration > 1200 * time_us, "syscall_duration",
> >> +             "syscall_duration was too big\n");
> >
> > this is going to be very flaky, especially in Travis CI. Can you
> > please use something more stable that doesn't rely on time?
>
> Let me try.
>
> >
> >> +}
> >> +
> >> +static void test_syscall_duration(void)
> >> +{
> >> +       struct task_local_storage *skel;
> >> +       int err;
> >> +
> >> +       skel = task_local_storage__open_and_load();
> >> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
> >> +               return;
> >> +
> >> +       skel->bss->target_pid = getpid();
> >
> > you are getting process ID, but comparing it with thread ID in BPF
> > code. It will stop working properly if/when tests will be run in
> > separate threads, so please use gettid() instead.
>
> Will fix.
>
> >
> >> +
> >> +       err = task_local_storage__attach(skel);
> >> +       if (!ASSERT_OK(err, "skel_attach"))
> >> +               goto out;
> >> +
> >> +       check_usleep_duration(skel, 2000);
> >> +       check_usleep_duration(skel, 3000);
> >> +       check_usleep_duration(skel, 4000);
> >> +
> >> +out:
> >> +       task_local_storage__destroy(skel);
> >> +}
> >> +
> >> +static void test_exit_creds(void)
> >> +{
> >> +       struct task_local_storage_exit_creds *skel;
> >> +       int err;
> >> +
> >> +       skel = task_local_storage_exit_creds__open_and_load();
> >> +       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
> >> +               return;
> >> +
> >> +       err = task_local_storage_exit_creds__attach(skel);
> >> +       if (!ASSERT_OK(err, "skel_attach"))
> >> +               goto out;
> >> +
> >> +       /* trigger at least one exit_creds() */
> >> +       if (CHECK_FAIL(system("ls > /dev/null")))
> >> +               goto out;
> >> +
> >> +       /* sync rcu, so the following reads could get latest values */
> >> +       kern_sync_rcu();
> >
> > what are we waiting for here? you don't detach anything... system() is
> > definitely going to complete by now, so whatever counter was or was
> > not updated will be reflected here. Seems like kern_sync_rcu() is not
> > needed?
>
> IIUC, without sync_ruc(), even system() is finished, the kernel may not
> have called exit_creds() for the "ls" task yet. Then the following check
> for null_ptr_count != 0 would fail.

Oh, so waiting for exit_creds() invocation which can get delayed, I
see. Would be good to make the above comment a bit more detailed,
thanks!

>
> >
> >> +       ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count");
> >> +       ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count");
> >> +out:
> >> +       task_local_storage_exit_creds__destroy(skel);
> >> +}
> >> +
> >> +void test_task_local_storage(void)
> >> +{
> >> +       if (test__start_subtest("syscall_duration"))
> >> +               test_syscall_duration();
> >> +       if (test__start_subtest("exit_creds"))
> >> +               test_exit_creds();
> >> +}
> >
> > [...]
> >
> >> +int valid_ptr_count = 0;
> >> +int null_ptr_count = 0;
> >> +
> >> +SEC("fentry/exit_creds")
> >> +int BPF_PROG(trace_exit_creds, struct task_struct *task)
> >> +{
> >> +       __u64 *ptr;
> >> +
> >> +       ptr = bpf_task_storage_get(&task_storage, task, 0,
> >> +                                  BPF_LOCAL_STORAGE_GET_F_CREATE);
> >> +       if (ptr)
> >> +               valid_ptr_count++;
> >> +       else
> >> +               null_ptr_count++;
> >
> >
> > use atomic increments?
>
> Do you mean __sync_fetch_and_add()?

yep

>
> >
> >> +       return 0;
> >> +}
> >> --
> >> 2.24.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-01-27 23:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20210126085923.469759-1-songliubraving@fb.com>
     [not found] ` <20210126085923.469759-3-songliubraving@fb.com>
2021-01-27 21:21   ` [PATCH v2 bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage Andrii Nakryiko
2021-01-27 21:43     ` Song Liu
2021-01-27 23:30       ` Andrii Nakryiko
     [not found] ` <20210126085923.469759-4-songliubraving@fb.com>
2021-01-27 21:22   ` [PATCH v2 bpf-next 3/4] bpf: runqslower: prefer use local KBUILD_OUTPUT/vmlinux or vmlinux Andrii Nakryiko
     [not found] ` <20210126085923.469759-5-songliubraving@fb.com>
2021-01-27 21:30   ` [PATCH v2 bpf-next 4/4] bpf: runqslower: use task local storage Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).