lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Fri, 4 Sep 2020 13:15:25 -0700
From:   Andrii Nakryiko <andrii.nakryiko@...il.com>
To:     Hao Luo <haoluo@...gle.com>
Cc:     Networking <netdev@...r.kernel.org>, bpf <bpf@...r.kernel.org>,
        open list <linux-kernel@...r.kernel.org>,
        "open list:KERNEL SELFTEST FRAMEWORK" 
        <linux-kselftest@...r.kernel.org>, Shuah Khan <shuah@...nel.org>,
        Alexei Starovoitov <ast@...nel.org>,
        Andrii Nakryiko <andriin@...com>,
        Daniel Borkmann <daniel@...earbox.net>,
        Martin KaFai Lau <kafai@...com>,
        Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
        John Fastabend <john.fastabend@...il.com>,
        KP Singh <kpsingh@...omium.org>,
        Quentin Monnet <quentin@...valent.com>,
        Steven Rostedt <rostedt@...dmis.org>,
        Ingo Molnar <mingo@...hat.com>, Andrey Ignatov <rdna@...com>,
        Jakub Sitnicki <jakub@...udflare.com>
Subject: Re: [PATCH bpf-next v2 6/6] bpf/selftests: Test for bpf_per_cpu_ptr()
 and bpf_this_cpu_ptr()

On Thu, Sep 3, 2020 at 3:35 PM Hao Luo <haoluo@...gle.com> wrote:
>
> Test bpf_per_cpu_ptr() and bpf_this_cpu_ptr(). Test two paths in the
> kernel. If the base pointer points to a struct, the returned reg is
> of type PTR_TO_BTF_ID. Direct pointer dereference can be applied on
> the returned variable. If the base pointer isn't a struct, the
> returned reg is of type PTR_TO_MEM, which also supports direct pointer
> dereference.
>
> Acked-by: Andrii Nakryiko <andriin@...com>
> Signed-off-by: Hao Luo <haoluo@...gle.com>
> ---
>  .../selftests/bpf/prog_tests/ksyms_btf.c      | 10 +++++++
>  .../selftests/bpf/progs/test_ksyms_btf.c      | 26 +++++++++++++++++++
>  2 files changed, 36 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> index 7b6846342449..22cc642dbc0e 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> @@ -58,6 +58,16 @@ void test_ksyms_btf(void)
>         CHECK(data->out__bpf_prog_active != bpf_prog_active_addr, "bpf_prog_active",
>               "got %llu, exp %llu\n", data->out__bpf_prog_active, bpf_prog_active_addr);
>
> +       CHECK(data->out__rq_cpu == -1, "rq_cpu",
> +             "got %u, exp != -1\n", data->out__rq_cpu);
> +       CHECK(data->out__percpu_bpf_prog_active == -1, "percpu_bpf_prog_active",
> +             "got %d, exp != -1\n", data->out__percpu_bpf_prog_active);
> +
> +       CHECK(data->out__this_rq_cpu == -1, "this_rq_cpu",
> +             "got %u, exp != -1\n", data->out__this_rq_cpu);
> +       CHECK(data->out__this_bpf_prog_active == -1, "this_bpf_prog_active",
> +             "got %d, exp != -1\n", data->out__this_bpf_prog_active);

see below for few suggestions to make these test more specific

out__this_bpf_prog_active it should always be > 0, no?

> +
>  cleanup:
>         test_ksyms_btf__destroy(skel);
>  }
> diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_btf.c b/tools/testing/selftests/bpf/progs/test_ksyms_btf.c
> index e04e31117f84..02d564349892 100644
> --- a/tools/testing/selftests/bpf/progs/test_ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf.c
> @@ -8,15 +8,41 @@
>  __u64 out__runqueues = -1;
>  __u64 out__bpf_prog_active = -1;
>
> +__u32 out__rq_cpu = -1; /* percpu struct fields */
> +int out__percpu_bpf_prog_active = -1; /* percpu int */
> +
> +__u32 out__this_rq_cpu = -1;
> +int out__this_bpf_prog_active = -1;
> +
>  extern const struct rq runqueues __ksym; /* struct type global var. */
>  extern const int bpf_prog_active __ksym; /* int type global var. */
>
>  SEC("raw_tp/sys_enter")
>  int handler(const void *ctx)
>  {
> +       struct rq *rq;
> +       int *active;
> +       __u32 cpu;
> +
>         out__runqueues = (__u64)&runqueues;
>         out__bpf_prog_active = (__u64)&bpf_prog_active;
>
> +       cpu = bpf_get_smp_processor_id();
> +
> +       /* test bpf_per_cpu_ptr() */
> +       rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu);
> +       if (rq)
> +               out__rq_cpu = rq->cpu;
> +       active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
> +       if (active)
> +               out__percpu_bpf_prog_active = *active;

this is equivalent to using bpf_this_cpu_ptr(), so:

1. you can compare value with out__this_xxx in user-space

2. it's interesting to also test that you can read value from some
other CPU. Can you add another variable and get value from CPU #0
always? E.g., for out__cpu_0_rq_cpu it should always be zero, right?

> +
> +       /* test bpf_this_cpu_ptr */
> +       rq = (struct rq *)bpf_this_cpu_ptr(&runqueues);
> +       out__this_rq_cpu = rq->cpu;
> +       active = (int *)bpf_this_cpu_ptr(&bpf_prog_active);
> +       out__this_bpf_prog_active = *active;
> +
>         return 0;
>  }
>
> --
> 2.28.0.526.ge36021eeef-goog
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ