Commit 62d101d5 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Martin KaFai Lau

selftests/bpf: Fix map_kptr test.

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: 2cbc469a ("selftests/bpf: Add C tests for kptr")
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarStanislav Fomichev <sdf@google.com>
Acked-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20230214235051.22938-1-alexei.starovoitov@gmail.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 5e53e5c7
...@@ -62,21 +62,23 @@ extern struct prog_test_ref_kfunc * ...@@ -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; 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; 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) static void test_kptr_unref(struct map_value *v)
{ {
struct prog_test_ref_kfunc *p; struct prog_test_ref_kfunc *p;
p = v->unref_ptr; p = v->unref_ptr;
/* store untrusted_ptr_or_null_ */ /* store untrusted_ptr_or_null_ */
v->unref_ptr = p; WRITE_ONCE(v->unref_ptr, p);
if (!p) if (!p)
return; return;
if (p->a + p->b > 100) if (p->a + p->b > 100)
return; return;
/* store untrusted_ptr_ */ /* store untrusted_ptr_ */
v->unref_ptr = p; WRITE_ONCE(v->unref_ptr, p);
/* store NULL */ /* store NULL */
v->unref_ptr = NULL; WRITE_ONCE(v->unref_ptr, NULL);
} }
static void test_kptr_ref(struct map_value *v) static void test_kptr_ref(struct map_value *v)
...@@ -85,7 +87,7 @@ 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; p = v->ref_ptr;
/* store ptr_or_null_ */ /* store ptr_or_null_ */
v->unref_ptr = p; WRITE_ONCE(v->unref_ptr, p);
if (!p) if (!p)
return; return;
if (p->a + p->b > 100) if (p->a + p->b > 100)
...@@ -99,7 +101,7 @@ static void test_kptr_ref(struct map_value *v) ...@@ -99,7 +101,7 @@ static void test_kptr_ref(struct map_value *v)
return; return;
} }
/* store ptr_ */ /* store ptr_ */
v->unref_ptr = p; WRITE_ONCE(v->unref_ptr, p);
bpf_kfunc_call_test_release(p); bpf_kfunc_call_test_release(p);
p = bpf_kfunc_call_test_acquire(&(unsigned long){0}); p = bpf_kfunc_call_test_acquire(&(unsigned long){0});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment