2020-08-22 03:31:39

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v1 8/8] bpf/selftests: Test for bpf_per_cpu_ptr()

On Wed, Aug 19, 2020 at 3:42 PM Hao Luo <[email protected]> wrote:
>
> Test bpf_per_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.
>
> Signed-off-by: Hao Luo <[email protected]>
> ---

Acked-by: Andrii Nakryiko <[email protected]>

> .../testing/selftests/bpf/prog_tests/ksyms_btf.c | 4 ++++
> .../testing/selftests/bpf/progs/test_ksyms_btf.c | 15 ++++++++++++++-
> 2 files changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> index 1dad61ba7e99..bdedd4a76b42 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> @@ -71,6 +71,10 @@ void test_ksyms_btf(void)
> "got %llu, exp %llu\n", data->out__runqueues, runqueues_addr);
> 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 %u\n", data->out__rq_cpu, 1);
> + CHECK(data->out__process_counts == -1, "process_counts",
> + "got %lu, exp != -1", data->out__process_counts);
>
> 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..78cf1ebb753d 100644
> --- a/tools/testing/selftests/bpf/progs/test_ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf.c
> @@ -7,16 +7,29 @@
>
> __u64 out__runqueues = -1;
> __u64 out__bpf_prog_active = -1;
> +__u32 out__rq_cpu = -1;
> +unsigned long out__process_counts = -1;

try to not use long for variables, it is 32-bit integer in user-space
but always 64-bit in BPF. This causes problems when using skeleton on
32-bit architecture.

>
> -extern const struct rq runqueues __ksym; /* struct type global var. */
> +extern const struct rq runqueues __ksym; /* struct type percpu var. */
> extern const int bpf_prog_active __ksym; /* int type global var. */
> +extern const unsigned long process_counts __ksym; /* int type percpu var. */
>
> SEC("raw_tp/sys_enter")
> int handler(const void *ctx)
> {
> + struct rq *rq;
> + unsigned long *count;
> +
> out__runqueues = (__u64)&runqueues;
> out__bpf_prog_active = (__u64)&bpf_prog_active;
>
> + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 1);
> + if (rq)
> + out__rq_cpu = rq->cpu;

this is awesome!

Are there any per-cpu variables that are arrays? Would be nice to test
those too.


> + count = (unsigned long *)bpf_per_cpu_ptr(&process_counts, 1);
> + if (count)
> + out__process_counts = *count;
> +
> return 0;
> }
>
> --
> 2.28.0.220.ged08abb693-goog
>


2020-08-28 03:43:43

by Hao Luo

[permalink] [raw]
Subject: Re: [PATCH bpf-next v1 8/8] bpf/selftests: Test for bpf_per_cpu_ptr()

Thanks for taking a look!

On Fri, Aug 21, 2020 at 8:30 PM Andrii Nakryiko
<[email protected]> wrote:
>
> On Wed, Aug 19, 2020 at 3:42 PM Hao Luo <[email protected]> wrote:
> >
> > Test bpf_per_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.
> >
> > Signed-off-by: Hao Luo <[email protected]>
> > ---
>
> Acked-by: Andrii Nakryiko <[email protected]>
>
[...]
> >
> > __u64 out__runqueues = -1;
> > __u64 out__bpf_prog_active = -1;
> > +__u32 out__rq_cpu = -1;
> > +unsigned long out__process_counts = -1;
>
> try to not use long for variables, it is 32-bit integer in user-space
> but always 64-bit in BPF. This causes problems when using skeleton on
> 32-bit architecture.
>

Ack. I will use another variable of type 'int' instead.

> >
> > -extern const struct rq runqueues __ksym; /* struct type global var. */
> > +extern const struct rq runqueues __ksym; /* struct type percpu var. */
> > extern const int bpf_prog_active __ksym; /* int type global var. */
> > +extern const unsigned long process_counts __ksym; /* int type percpu var. */
> >
> > SEC("raw_tp/sys_enter")
> > int handler(const void *ctx)
> > {
> > + struct rq *rq;
> > + unsigned long *count;
> > +
> > out__runqueues = (__u64)&runqueues;
> > out__bpf_prog_active = (__u64)&bpf_prog_active;
> >
> > + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 1);
> > + if (rq)
> > + out__rq_cpu = rq->cpu;
>
> this is awesome!
>
> Are there any per-cpu variables that are arrays? Would be nice to test
> those too.
>
>

There are currently per-cpu arrays, but not common. There is a
'pmc_prev_left' in arch/x86, I can add that in this test.

[...]

2020-09-01 18:14:25

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v1 8/8] bpf/selftests: Test for bpf_per_cpu_ptr()

On Thu, Aug 27, 2020 at 8:42 PM Hao Luo <[email protected]> wrote:
>
> Thanks for taking a look!
>
> On Fri, Aug 21, 2020 at 8:30 PM Andrii Nakryiko
> <[email protected]> wrote:
> >
> > On Wed, Aug 19, 2020 at 3:42 PM Hao Luo <[email protected]> wrote:
> > >
> > > Test bpf_per_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.
> > >
> > > Signed-off-by: Hao Luo <[email protected]>
> > > ---
> >
> > Acked-by: Andrii Nakryiko <[email protected]>
> >
> [...]
> > >
> > > __u64 out__runqueues = -1;
> > > __u64 out__bpf_prog_active = -1;
> > > +__u32 out__rq_cpu = -1;
> > > +unsigned long out__process_counts = -1;
> >
> > try to not use long for variables, it is 32-bit integer in user-space
> > but always 64-bit in BPF. This causes problems when using skeleton on
> > 32-bit architecture.
> >
>
> Ack. I will use another variable of type 'int' instead.

__u64 is fine as well

>
> > >
> > > -extern const struct rq runqueues __ksym; /* struct type global var. */
> > > +extern const struct rq runqueues __ksym; /* struct type percpu var. */
> > > extern const int bpf_prog_active __ksym; /* int type global var. */
> > > +extern const unsigned long process_counts __ksym; /* int type percpu var. */
> > >
> > > SEC("raw_tp/sys_enter")
> > > int handler(const void *ctx)
> > > {
> > > + struct rq *rq;
> > > + unsigned long *count;
> > > +
> > > out__runqueues = (__u64)&runqueues;
> > > out__bpf_prog_active = (__u64)&bpf_prog_active;
> > >
> > > + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 1);
> > > + if (rq)
> > > + out__rq_cpu = rq->cpu;
> >
> > this is awesome!
> >
> > Are there any per-cpu variables that are arrays? Would be nice to test
> > those too.
> >
> >
>
> There are currently per-cpu arrays, but not common. There is a
> 'pmc_prev_left' in arch/x86, I can add that in this test.

arch-specific variables are bad, because selftests will be failing on
other architectures; let's not do this then.

>
> [...]

2020-09-01 19:49:12

by Hao Luo

[permalink] [raw]
Subject: Re: [PATCH bpf-next v1 8/8] bpf/selftests: Test for bpf_per_cpu_ptr()

On Tue, Sep 1, 2020 at 11:12 AM Andrii Nakryiko
<[email protected]> wrote:
>
> On Thu, Aug 27, 2020 at 8:42 PM Hao Luo <[email protected]> wrote:
> >
[...]
> > > >
> > > > -extern const struct rq runqueues __ksym; /* struct type global var. */
> > > > +extern const struct rq runqueues __ksym; /* struct type percpu var. */
> > > > extern const int bpf_prog_active __ksym; /* int type global var. */
> > > > +extern const unsigned long process_counts __ksym; /* int type percpu var. */
> > > >
> > > > SEC("raw_tp/sys_enter")
> > > > int handler(const void *ctx)
> > > > {
> > > > + struct rq *rq;
> > > > + unsigned long *count;
> > > > +
> > > > out__runqueues = (__u64)&runqueues;
> > > > out__bpf_prog_active = (__u64)&bpf_prog_active;
> > > >
> > > > + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 1);
> > > > + if (rq)
> > > > + out__rq_cpu = rq->cpu;
> > >
> > > this is awesome!
> > >
> > > Are there any per-cpu variables that are arrays? Would be nice to test
> > > those too.
> > >
> > >
> >
> > There are currently per-cpu arrays, but not common. There is a
> > 'pmc_prev_left' in arch/x86, I can add that in this test.
>
> arch-specific variables are bad, because selftests will be failing on
> other architectures; let's not do this then.
>

Yeah, no problem. Though not going to add this arch-specific variable
in the posted patches, I tried array-typed ksyms locally in my test
environment. It worked fine, except that the array size is not
checked. For instance, if there is a percpu array in kernel as

DEFINE_PER_CPU(u32[64], foo);

we can declare a ksym of different size and it passes libbpf checks
and kernel verification.

extern u32 foo[128] __ksyms;

It seems that bpf_core_types_are_compat() doesn't check nr_elem. But
it seems the kernel verifier does check out-of-bounds accesses, so
this may not be a real problem. Just want to list what I saw.

> >
> > [...]