diff mbox series

[bpf-next,v3,12/13] selftests/bpf: Add C tests for kptr

Message ID 20220320155510.671497-13-memxor@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Introduce typed pointer support in BPF maps | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: netdev@vger.kernel.org songliubraving@fb.com linux-kselftest@vger.kernel.org shuah@kernel.org yhs@fb.com john.fastabend@gmail.com kafai@fb.com kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: spaces preferred around that '*' (ctx:WxV) WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: externs should be avoided in .c files WARNING: line length of 82 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns WARNING: line length of 88 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 90 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary

Commit Message

Kumar Kartikeya Dwivedi March 20, 2022, 3:55 p.m. UTC
This uses the __kptr and __kptr_ref macros as well, and tries to test
the stuff that is supposed to work, since we have negative tests in
test_verifier suite. Also include some code to test map-in-map support,
such that the inner_map_meta matches the kptr_off_tab of map added as
element.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../selftests/bpf/prog_tests/map_kptr.c       |  20 ++
 tools/testing/selftests/bpf/progs/map_kptr.c  | 194 ++++++++++++++++++
 2 files changed, 214 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/map_kptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/map_kptr.c

Comments

Andrii Nakryiko March 22, 2022, 9 p.m. UTC | #1
On Sun, Mar 20, 2022 at 8:56 AM Kumar Kartikeya Dwivedi
<memxor@gmail.com> wrote:
>
> This uses the __kptr and __kptr_ref macros as well, and tries to test
> the stuff that is supposed to work, since we have negative tests in
> test_verifier suite. Also include some code to test map-in-map support,
> such that the inner_map_meta matches the kptr_off_tab of map added as
> element.
>
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/map_kptr.c       |  20 ++
>  tools/testing/selftests/bpf/progs/map_kptr.c  | 194 ++++++++++++++++++
>  2 files changed, 214 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/map_kptr.c
>  create mode 100644 tools/testing/selftests/bpf/progs/map_kptr.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/map_kptr.c b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
> new file mode 100644
> index 000000000000..688732295ce9
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
> @@ -0,0 +1,20 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +
> +#include "map_kptr.skel.h"
> +
> +void test_map_kptr(void)
> +{
> +       struct map_kptr *skel;
> +       char buf[24];
> +       int key = 0;
> +
> +       skel = map_kptr__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
> +               return;
> +       ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_map), &key, buf, 0),
> +                 "bpf_map_update_elem hash_map");
> +       ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_malloc_map), &key, buf, 0),
> +                 "bpf_map_update_elem hash_malloc_map");


nit: it's quite messy and verbose, please do the operation outside of
ASSERT_OK() and just validate error:

err = bpf_map_update_elem(...);
ASSERT_OK(err, "hash_map_update");

And keep those ASSERT_XXX() string descriptors relatively short (see
how they are used internally in ASSERT_XXX() macros).

> +       map_kptr__destroy(skel);
> +}

[...]

> +
> +extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
> +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;
> +
> +static __always_inline

nit: no need for __always_inline everywhere, just `static void` will
do the right thing.

> +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;
> +       if (!p)
> +               return;
> +       if (p->a + p->b > 100)
> +               return;
> +       /* store untrusted_ptr_ */
> +       v->unref_ptr = p;
> +       /* store NULL */
> +       v->unref_ptr = NULL;
> +}
> +

[...]
Jiri Olsa March 24, 2022, 9:10 a.m. UTC | #2
On Sun, Mar 20, 2022 at 09:25:09PM +0530, Kumar Kartikeya Dwivedi wrote:

SNIP

> +static __always_inline
> +void test_kptr(struct map_value *v)
> +{
> +	test_kptr_unref(v);
> +	test_kptr_ref(v);
> +	test_kptr_get(v);
> +}
> +
> +SEC("tc")
> +int test_map_kptr(struct __sk_buff *ctx)
> +{
> +	void *maps[] = {
> +		&array_map,
> +		&hash_map,
> +		&hash_malloc_map,
> +		&lru_hash_map,
> +	};
> +	struct map_value *v;
> +	int i, key = 0;
> +
> +	for (i = 0; i < sizeof(maps) / sizeof(*maps); i++) {
> +		v = bpf_map_lookup_elem(&array_map, &key);

hi,
I was just quickly checking on the usage, so I might be missing something,
but should this be lookup to maps[i] instead of array_map ?

similar below in test_map_in_map_kptr

jirka

> +		if (!v)
> +			return 0;
> +		test_kptr(v);
> +	}
> +	return 0;
> +}
> +
> +SEC("tc")
> +int test_map_in_map_kptr(struct __sk_buff *ctx)
> +{
> +	void *map_of_maps[] = {
> +		&array_of_array_maps,
> +		&array_of_hash_maps,
> +		&array_of_hash_malloc_maps,
> +		&array_of_lru_hash_maps,
> +		&hash_of_array_maps,
> +		&hash_of_hash_maps,
> +		&hash_of_hash_malloc_maps,
> +		&hash_of_lru_hash_maps,
> +	};
> +	struct map_value *v;
> +	int i, key = 0;
> +	void *map;
> +
> +	for (i = 0; i < sizeof(map_of_maps) / sizeof(*map_of_maps); i++) {
> +		map = bpf_map_lookup_elem(&array_of_array_maps, &key);
> +		if (!map)
> +			return 0;
> +		v = bpf_map_lookup_elem(map, &key);
> +		if (!v)
> +			return 0;
> +		test_kptr(v);
> +	}
> +	return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
> -- 
> 2.35.1
>
Kumar Kartikeya Dwivedi March 25, 2022, 2:52 p.m. UTC | #3
On Thu, Mar 24, 2022 at 02:40:14PM IST, Jiri Olsa wrote:
> On Sun, Mar 20, 2022 at 09:25:09PM +0530, Kumar Kartikeya Dwivedi wrote:
>
> SNIP
>
> > +static __always_inline
> > +void test_kptr(struct map_value *v)
> > +{
> > +	test_kptr_unref(v);
> > +	test_kptr_ref(v);
> > +	test_kptr_get(v);
> > +}
> > +
> > +SEC("tc")
> > +int test_map_kptr(struct __sk_buff *ctx)
> > +{
> > +	void *maps[] = {
> > +		&array_map,
> > +		&hash_map,
> > +		&hash_malloc_map,
> > +		&lru_hash_map,
> > +	};
> > +	struct map_value *v;
> > +	int i, key = 0;
> > +
> > +	for (i = 0; i < sizeof(maps) / sizeof(*maps); i++) {
> > +		v = bpf_map_lookup_elem(&array_map, &key);
>
> hi,
> I was just quickly checking on the usage, so I might be missing something,
> but should this be lookup to maps[i] instead of array_map ?
>
> similar below in test_map_in_map_kptr
>

My bad, it's a braino. Will fix in v4.
Thanks!

> jirka
>
> > +		if (!v)
> > +			return 0;
> > +		test_kptr(v);
> > +	}
> > +	return 0;
> > +}
> > +
> > +SEC("tc")
> > +int test_map_in_map_kptr(struct __sk_buff *ctx)
> > +{
> > +	void *map_of_maps[] = {
> > +		&array_of_array_maps,
> > +		&array_of_hash_maps,
> > +		&array_of_hash_malloc_maps,
> > +		&array_of_lru_hash_maps,
> > +		&hash_of_array_maps,
> > +		&hash_of_hash_maps,
> > +		&hash_of_hash_malloc_maps,
> > +		&hash_of_lru_hash_maps,
> > +	};
> > +	struct map_value *v;
> > +	int i, key = 0;
> > +	void *map;
> > +
> > +	for (i = 0; i < sizeof(map_of_maps) / sizeof(*map_of_maps); i++) {
> > +		map = bpf_map_lookup_elem(&array_of_array_maps, &key);
> > +		if (!map)
> > +			return 0;
> > +		v = bpf_map_lookup_elem(map, &key);
> > +		if (!v)
> > +			return 0;
> > +		test_kptr(v);
> > +	}
> > +	return 0;
> > +}
> > +
> > +char _license[] SEC("license") = "GPL";
> > --
> > 2.35.1
> >

--
Kartikeya
Kumar Kartikeya Dwivedi March 25, 2022, 2:52 p.m. UTC | #4
On Wed, Mar 23, 2022 at 02:30:57AM IST, Andrii Nakryiko wrote:
> On Sun, Mar 20, 2022 at 8:56 AM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > This uses the __kptr and __kptr_ref macros as well, and tries to test
> > the stuff that is supposed to work, since we have negative tests in
> > test_verifier suite. Also include some code to test map-in-map support,
> > such that the inner_map_meta matches the kptr_off_tab of map added as
> > element.
> >
> > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > ---
> >  .../selftests/bpf/prog_tests/map_kptr.c       |  20 ++
> >  tools/testing/selftests/bpf/progs/map_kptr.c  | 194 ++++++++++++++++++
> >  2 files changed, 214 insertions(+)
> >  create mode 100644 tools/testing/selftests/bpf/prog_tests/map_kptr.c
> >  create mode 100644 tools/testing/selftests/bpf/progs/map_kptr.c
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/map_kptr.c b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
> > new file mode 100644
> > index 000000000000..688732295ce9
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
> > @@ -0,0 +1,20 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +#include <test_progs.h>
> > +
> > +#include "map_kptr.skel.h"
> > +
> > +void test_map_kptr(void)
> > +{
> > +       struct map_kptr *skel;
> > +       char buf[24];
> > +       int key = 0;
> > +
> > +       skel = map_kptr__open_and_load();
> > +       if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
> > +               return;
> > +       ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_map), &key, buf, 0),
> > +                 "bpf_map_update_elem hash_map");
> > +       ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_malloc_map), &key, buf, 0),
> > +                 "bpf_map_update_elem hash_malloc_map");
>
>
> nit: it's quite messy and verbose, please do the operation outside of
> ASSERT_OK() and just validate error:
>
> err = bpf_map_update_elem(...);
> ASSERT_OK(err, "hash_map_update");
>
> And keep those ASSERT_XXX() string descriptors relatively short (see
> how they are used internally in ASSERT_XXX() macros).
>

Ok.

> > +       map_kptr__destroy(skel);
> > +}
>
> [...]
>
> > +
> > +extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
> > +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;
> > +
> > +static __always_inline
>
> nit: no need for __always_inline everywhere, just `static void` will
> do the right thing.
>

Ok, will drop.

> > +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;
> > +       if (!p)
> > +               return;
> > +       if (p->a + p->b > 100)
> > +               return;
> > +       /* store untrusted_ptr_ */
> > +       v->unref_ptr = p;
> > +       /* store NULL */
> > +       v->unref_ptr = NULL;
> > +}
> > +
>
> [...]

--
Kartikeya
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/map_kptr.c b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
new file mode 100644
index 000000000000..688732295ce9
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/map_kptr.c
@@ -0,0 +1,20 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+
+#include "map_kptr.skel.h"
+
+void test_map_kptr(void)
+{
+	struct map_kptr *skel;
+	char buf[24];
+	int key = 0;
+
+	skel = map_kptr__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
+		return;
+	ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_map), &key, buf, 0),
+		  "bpf_map_update_elem hash_map");
+	ASSERT_OK(bpf_map_update_elem(bpf_map__fd(skel->maps.hash_malloc_map), &key, buf, 0),
+		  "bpf_map_update_elem hash_malloc_map");
+	map_kptr__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/map_kptr.c b/tools/testing/selftests/bpf/progs/map_kptr.c
new file mode 100644
index 000000000000..75df3dc05db2
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/map_kptr.c
@@ -0,0 +1,194 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+
+struct map_value {
+	struct prog_test_ref_kfunc __kptr *unref_ptr;
+	struct prog_test_ref_kfunc __kptr_ref *ref_ptr;
+};
+
+struct array_map {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__type(key, int);
+	__type(value, struct map_value);
+	__uint(max_entries, 1);
+} array_map SEC(".maps");
+
+struct hash_map {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, int);
+	__type(value, struct map_value);
+	__uint(max_entries, 1);
+} hash_map SEC(".maps");
+
+struct hash_malloc_map {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__type(key, int);
+	__type(value, struct map_value);
+	__uint(max_entries, 1);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+} hash_malloc_map SEC(".maps");
+
+struct lru_hash_map {
+	__uint(type, BPF_MAP_TYPE_LRU_HASH);
+	__type(key, int);
+	__type(value, struct map_value);
+	__uint(max_entries, 1);
+} lru_hash_map SEC(".maps");
+
+#define DEFINE_MAP_OF_MAP(map_type, inner_map_type, name)       \
+	struct {                                                \
+		__uint(type, map_type);                         \
+		__uint(max_entries, 1);                         \
+		__uint(key_size, sizeof(int));                  \
+		__uint(value_size, sizeof(int));                \
+		__array(values, struct inner_map_type);         \
+	} name SEC(".maps") = {                                 \
+		.values = { [0] = &inner_map_type },            \
+	}
+
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_map, array_of_array_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_map, array_of_hash_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, hash_malloc_map, array_of_hash_malloc_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_ARRAY_OF_MAPS, lru_hash_map, array_of_lru_hash_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, array_map, hash_of_array_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_map, hash_of_hash_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, hash_malloc_map, hash_of_hash_malloc_maps);
+DEFINE_MAP_OF_MAP(BPF_MAP_TYPE_HASH_OF_MAPS, lru_hash_map, hash_of_lru_hash_maps);
+
+extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
+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;
+
+static __always_inline
+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;
+	if (!p)
+		return;
+	if (p->a + p->b > 100)
+		return;
+	/* store untrusted_ptr_ */
+	v->unref_ptr = p;
+	/* store NULL */
+	v->unref_ptr = NULL;
+}
+
+static __always_inline
+void test_kptr_ref(struct map_value *v)
+{
+	struct prog_test_ref_kfunc *p;
+
+	p = v->ref_ptr;
+	/* store ptr_or_null_ */
+	v->unref_ptr = p;
+	if (!p)
+		return;
+	if (p->a + p->b > 100)
+		return;
+	/* store NULL */
+	p = bpf_kptr_xchg(&v->ref_ptr, NULL);
+	if (!p)
+		return;
+	if (p->a + p->b > 100) {
+		bpf_kfunc_call_test_release(p);
+		return;
+	}
+	/* store ptr_ */
+	v->unref_ptr = p;
+	bpf_kfunc_call_test_release(p);
+
+	p = bpf_kfunc_call_test_acquire(&(unsigned long){0});
+	if (!p)
+		return;
+	/* store ptr_ */
+	p = bpf_kptr_xchg(&v->ref_ptr, p);
+	if (!p)
+		return;
+	if (p->a + p->b > 100) {
+		bpf_kfunc_call_test_release(p);
+		return;
+	}
+	bpf_kfunc_call_test_release(p);
+}
+
+static __always_inline
+void test_kptr_get(struct map_value *v)
+{
+	struct prog_test_ref_kfunc *p;
+
+	p = bpf_kfunc_call_test_kptr_get(&v->ref_ptr, 0, 0);
+	if (!p)
+		return;
+	if (p->a + p->b > 100) {
+		bpf_kfunc_call_test_release(p);
+		return;
+	}
+	bpf_kfunc_call_test_release(p);
+}
+
+static __always_inline
+void test_kptr(struct map_value *v)
+{
+	test_kptr_unref(v);
+	test_kptr_ref(v);
+	test_kptr_get(v);
+}
+
+SEC("tc")
+int test_map_kptr(struct __sk_buff *ctx)
+{
+	void *maps[] = {
+		&array_map,
+		&hash_map,
+		&hash_malloc_map,
+		&lru_hash_map,
+	};
+	struct map_value *v;
+	int i, key = 0;
+
+	for (i = 0; i < sizeof(maps) / sizeof(*maps); i++) {
+		v = bpf_map_lookup_elem(&array_map, &key);
+		if (!v)
+			return 0;
+		test_kptr(v);
+	}
+	return 0;
+}
+
+SEC("tc")
+int test_map_in_map_kptr(struct __sk_buff *ctx)
+{
+	void *map_of_maps[] = {
+		&array_of_array_maps,
+		&array_of_hash_maps,
+		&array_of_hash_malloc_maps,
+		&array_of_lru_hash_maps,
+		&hash_of_array_maps,
+		&hash_of_hash_maps,
+		&hash_of_hash_malloc_maps,
+		&hash_of_lru_hash_maps,
+	};
+	struct map_value *v;
+	int i, key = 0;
+	void *map;
+
+	for (i = 0; i < sizeof(map_of_maps) / sizeof(*map_of_maps); i++) {
+		map = bpf_map_lookup_elem(&array_of_array_maps, &key);
+		if (!map)
+			return 0;
+		v = bpf_map_lookup_elem(map, &key);
+		if (!v)
+			return 0;
+		test_kptr(v);
+	}
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";