From: Hao Luo <haoluo@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@vger.kernel.org>, Shuah Khan <shuah@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andriin@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Quentin Monnet <quentin@isovalent.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ingo Molnar <mingo@redhat.com>, Andrey Ignatov <rdna@fb.com>,
Jakub Sitnicki <jakub@cloudflare.com>
Subject: Re: [PATCH bpf-next v2 6/6] bpf/selftests: Test for bpf_per_cpu_ptr() and bpf_this_cpu_ptr()
Date: Mon, 14 Sep 2020 09:59:22 -0700 [thread overview]
Message-ID: <CA+khW7j_jZPtpO0_z51EfuUnN-Kxt2CytGG695=D0jR7my7pBg@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzZ9krnVzAR=0oQMe+f96cZff5MSdV3_EHiS-mSNF8MieQ@mail.gmail.com>
Thanks for taking a look!
On Fri, Sep 4, 2020 at 1:15 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Sep 3, 2020 at 3:35 PM Hao Luo <haoluo@google.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@fb.com>
> > Signed-off-by: Hao Luo <haoluo@google.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?
>
I could be wrong, but I remember raw_trace_point is not tracked by
bpf_prog_active. So I used bpf_prog_active >= 0 to be safe.
> > +
> > 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?
>
Ack. That makes sense. You are right, out__cpu_0_rq_cpu is always zero.
> > +
> > + /* 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
> >
prev parent reply other threads:[~2020-09-14 17:00 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-03 22:33 [PATCH bpf-next v2 0/6] bpf: BTF support for ksyms Hao Luo
2020-09-03 22:33 ` [PATCH bpf-next v2 1/6] bpf: Introduce pseudo_btf_id Hao Luo
2020-09-04 19:05 ` Andrii Nakryiko
2020-09-14 4:55 ` Hao Luo
2020-09-03 22:33 ` [PATCH bpf-next v2 2/6] bpf/libbpf: BTF support for typed ksyms Hao Luo
2020-09-04 19:34 ` Andrii Nakryiko
2020-09-14 4:56 ` Hao Luo
2020-09-03 22:33 ` [PATCH bpf-next v2 3/6] bpf/selftests: ksyms_btf to test " Hao Luo
2020-09-04 19:49 ` Andrii Nakryiko
2020-09-14 4:58 ` Hao Luo
2020-09-14 22:06 ` Andrii Nakryiko
2020-09-03 22:33 ` [PATCH bpf-next v2 4/6] bpf: Introduce bpf_per_cpu_ptr() Hao Luo
2020-09-04 20:04 ` Andrii Nakryiko
2020-09-14 5:01 ` Hao Luo
2020-09-14 18:09 ` Andrii Nakryiko
2020-09-03 22:33 ` [PATCH bpf-next v2 5/6] bpf: Introduce bpf_this_cpu_ptr() Hao Luo
2020-09-04 20:09 ` Andrii Nakryiko
2020-09-14 5:04 ` Hao Luo
2020-09-03 22:33 ` [PATCH bpf-next v2 6/6] bpf/selftests: Test for bpf_per_cpu_ptr() and bpf_this_cpu_ptr() Hao Luo
2020-09-04 20:15 ` Andrii Nakryiko
2020-09-14 16:59 ` Hao Luo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+khW7j_jZPtpO0_z51EfuUnN-Kxt2CytGG695=D0jR7my7pBg@mail.gmail.com' \
--to=haoluo@google.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=quentin@isovalent.com \
--cc=rdna@fb.com \
--cc=rostedt@goodmis.org \
--cc=shuah@kernel.org \
--cc=songliubraving@fb.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).