From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: davem@davemloft.net, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@kernel.org, netdev@vger.kernel.org,
bpf@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH bpf-next] selftests/bpf: Fix map_kptr test.
Date: Wed, 15 Feb 2023 11:26:09 +0100 [thread overview]
Message-ID: <20230215102609.5o2isbowvtoungws@apollo> (raw)
In-Reply-To: <20230214235051.22938-1-alexei.starovoitov@gmail.com>
On Wed, Feb 15, 2023 at 12:50:51AM CET, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> The compiler is optimizing out majority of unref_ptr read/writes, so the test
> wasn't testing much. For example, one could delete '__kptr' tag from
> 'struct prog_test_ref_kfunc __kptr *unref_ptr;' and the test would still "pass".
>
> Convert it to volatile stores. Confirmed by comparing bpf asm before/after.
>
> Fixes: 2cbc469a6fc3 ("selftests/bpf: Add C tests for kptr")
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
There's also the same test in the test_verifier suite, so there's still coverage
for this case.
> tools/testing/selftests/bpf/progs/map_kptr.c | 12 +++++++-----
> 1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/progs/map_kptr.c b/tools/testing/selftests/bpf/progs/map_kptr.c
> index eb8217803493..228ec45365a8 100644
> --- a/tools/testing/selftests/bpf/progs/map_kptr.c
> +++ b/tools/testing/selftests/bpf/progs/map_kptr.c
> @@ -62,21 +62,23 @@ extern struct prog_test_ref_kfunc *
> bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **p, int a, int b) __ksym;
> extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
>
> +#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *) &(x)) = (val))
> +
> static void test_kptr_unref(struct map_value *v)
> {
> struct prog_test_ref_kfunc *p;
>
> p = v->unref_ptr;
> /* store untrusted_ptr_or_null_ */
> - v->unref_ptr = p;
> + WRITE_ONCE(v->unref_ptr, p);
> if (!p)
> return;
> if (p->a + p->b > 100)
> return;
> /* store untrusted_ptr_ */
> - v->unref_ptr = p;
> + WRITE_ONCE(v->unref_ptr, p);
> /* store NULL */
> - v->unref_ptr = NULL;
> + WRITE_ONCE(v->unref_ptr, NULL);
> }
>
> static void test_kptr_ref(struct map_value *v)
> @@ -85,7 +87,7 @@ static void test_kptr_ref(struct map_value *v)
>
> p = v->ref_ptr;
> /* store ptr_or_null_ */
> - v->unref_ptr = p;
> + WRITE_ONCE(v->unref_ptr, p);
> if (!p)
> return;
> if (p->a + p->b > 100)
> @@ -99,7 +101,7 @@ static void test_kptr_ref(struct map_value *v)
> return;
> }
> /* store ptr_ */
> - v->unref_ptr = p;
> + WRITE_ONCE(v->unref_ptr, p);
> bpf_kfunc_call_test_release(p);
>
> p = bpf_kfunc_call_test_acquire(&(unsigned long){0});
> --
> 2.30.2
>
next prev parent reply other threads:[~2023-02-15 10:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-14 23:50 [PATCH bpf-next] selftests/bpf: Fix map_kptr test Alexei Starovoitov
2023-02-15 3:02 ` Stanislav Fomichev
2023-02-15 3:20 ` Alexei Starovoitov
2023-02-15 17:36 ` Stanislav Fomichev
2023-02-15 10:26 ` Kumar Kartikeya Dwivedi [this message]
2023-02-15 17:30 ` patchwork-bot+netdevbpf
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=20230215102609.5o2isbowvtoungws@apollo \
--to=memxor@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=netdev@vger.kernel.org \
/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