Fix pinning devmaps
Subsystems (4)
v2 cover Fix pinning devmaps
Fix devmap pinning and reloading. The kernel adds BPF_F_RDONLY_PROG to all devmaps when created, but libbpf checks that user flags match pinned map flags when using LIBBPF_PIN_BY_NAME, so reusing pinned devmaps doesn't work, failing with an error like:
libbpf: couldn't reuse pinned map at '/sys/fs/bpf/dev_map':
parameter mismatch
Work around this by ignoring RDONLY_PROG in the compat check in libbpf.
Changes since v1:
- Fixed a broken import
- Fixed style issues
Pramukh Naduthota (2):
Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned
devmaps
Add a selftest for devmap pinning.
tools/lib/bpf/libbpf.c | 8 +++++++- .../testing/selftests/bpf/prog_tests/devmap.c | 20 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 ++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/devmap.c create mode 100644 tools/testing/selftests/bpf/progs/test_pinned_devmap.c
Signature · 1 line
2.30.2
PATCH v2 1/2 Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned devmaps
Ignore BPF_F_RDONLY_PROG when checking for compatibility for devmaps. The kernel adds the flag to all devmap creates, and this breaks pinning behavior, as libbpf will then check the actual vs user supplied flags and see this difference. Work around this by adding RDONLY_PROG to the users's flags when testing against the pinned map
--- tools/lib/bpf/libbpf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +7 -1
@@ -4818,6 +4818,7 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)char msg[STRERR_BUFSIZE ];__u32 map_info_len;int err; +unsigned int effective_flags = map->def.map_flags; map_info_len =sizeof (map_info);
@@ -4830,11 +4831,16 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)return false; } +/* The kernel adds RDONLY_PROG to devmaps */ +if (map->def.type ==BPF_MAP_TYPE_DEVMAP || + map->def.type ==BPF_MAP_TYPE_DEVMAP_HASH ) + effective_flags |=BPF_F_RDONLY_PROG ; +return (map_info.type == map->def.type && map_info.key_size == map->def.key_size && map_info.value_size == map->def.value_size && map_info.max_entries == map->def.max_entries && - map_info.map_flags == map->def.map_flags && + map_info.map_flags == effective_flags && map_info.map_extra == map->map_extra); }
Signature · 1 line
2.30.2
PATCH v2 2/2 Add a selftest for devmap pinning.
In reply to Pramukh NaduthotaAdd a selftest
--- .../testing/selftests/bpf/prog_tests/devmap.c | 20 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 ++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/devmap.c create mode 100644 tools/testing/selftests/bpf/progs/test_pinned_devmap.c
tools/testing/selftests/bpf/prog_tests/devmap.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include "testing_helpers.h" +#include "test_progs.h" +#include "test_pinned_devmap.skel.h" + +void test_devmap_pinning (void ) +{ +struct test_pinned_devmap *ptr; + + ptr = test_pinned_devmap__open_and_load() +ASSERT_OK_PTR (ptr,"first load" ); +test_pinned_devmap__destroy (ptr); +ASSERT_OK_PTR (test_pinned_devmap__open_and_load (),"re-load" ); +} + +void test_devmap (void ) +{ +test_devmap_pinning (); +}
tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include <stddef.h> +#include <linux/bpf.h> +#include <linux/types.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); + __uint(max_entries, 32); + __type(key, int); + __type(value, int); + __uint(pinning, LIBBPF_PIN_BY_NAME); +} repinned_dev_map SEC(".maps"); + + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.30.2
Daniel Borkmann
maintainerRe: [PATCH net-next v2 2/2] Add a selftest for devmap pinning.
Quoted 18 lines · Pramukh Naduthota
Add a selftest
Signed-off-by Pramukh Naduthota <[email protected]>--- .../testing/selftests/bpf/prog_tests/devmap.c | 20 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 ++++++++++++++++ 2 files changed, 37 insertions(+)create mode 100644
tools/testing/selftests/bpf/prog_tests/devmap.ccreate mode 100644tools/testing/selftests/bpf/progs/test_pinned_devmap.ctools/testing/selftests/bpf/prog_tests/devmap.c new file +2
-0@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */
tools/testing/selftests/bpf/prog_tests/devmap.c line 12
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +10
-0+#include "testing_helpers.h" +#include "test_progs.h" +#include "test_pinned_devmap.skel.h" + +void test_devmap_pinning (void ) +{ +struct test_pinned_devmap *ptr; + + ptr = test_pinned_devmap__open_and_load() +ASSERT_OK_PTR (ptr,"first load" );
Looks like you never actually compiled your selftest? :(
[...]
TEST-OBJ [test_progs] rcu_read_lock.test.o
TEST-OBJ [test_progs] btf_dump.test.o
In file included from /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:4:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c: In function ‘test_devmap_pinning’:
./test_progs.h:352:35: error: expected expression before ‘{’ token
352 | #define ASSERT_OK_PTR(ptr, name) ({ \
| ^
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:12:2: note: in expansion of macro ‘ASSERT_OK_PTR’
12 | ASSERT_OK_PTR(ptr, "first load");
| ^~~~~~~~~~~~~
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:11:8: error: called object is not a function or function pointer
11 | ptr = test_pinned_devmap__open_and_load()
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ make: *** [Makefile:539: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/devmap.test.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf'
Error: Process completed with exit code 2.
Quoted 32 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +8
-0+test_pinned_devmap__destroy (ptr); +ASSERT_OK_PTR (test_pinned_devmap__open_and_load (),"re-load" ); +} + +void test_devmap (void ) +{ +test_devmap_pinning (); +}tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +17
-0@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include <stddef.h> +#include <linux/bpf.h> +#include <linux/types.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); + __uint(max_entries, 32); + __type(key, int); + __type(value, int); + __uint(pinning, LIBBPF_PIN_BY_NAME); +} repinned_dev_map SEC(".maps"); + + +char _license[] SEC("license") = "GPL";
Re: [PATCH net-next v2 2/2] Add a selftest for devmap pinning.
Sorry, looks like I didn't run the tests again after fixing my checkpatch errors. Still new to this, and am quite mortified.
Is there a better way to fix this than sending out a v3 of my patch?
Quoted 85 lines · Daniel Borkmann
Quoted 18 lines · Pramukh Naduthota
Add a selftest
Signed-off-by Pramukh Naduthota <[email protected]>--- .../testing/selftests/bpf/prog_tests/devmap.c | 20 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 ++++++++++++++++ 2 files changed, 37 insertions(+)create mode 100644
tools/testing/selftests/bpf/prog_tests/devmap.ccreate mode 100644tools/testing/selftests/bpf/progs/test_pinned_devmap.ctools/testing/selftests/bpf/prog_tests/devmap.c new file +2
-0@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ tools/testing/selftests/bpf/prog_tests/devmap.c line 12
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +10
-0+#include "testing_helpers.h" +#include "test_progs.h" +#include "test_pinned_devmap.skel.h" + +void test_devmap_pinning (void ) +{ +struct test_pinned_devmap *ptr; + + ptr = test_pinned_devmap__open_and_load() +ASSERT_OK_PTR (ptr,"first load" );Looks like you never actually compiled your selftest? :(
[...]
TEST-OBJ [test_progs] rcu_read_lock.test.o
TEST-OBJ [test_progs] btf_dump.test.o
In file included from/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:4:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c: In function ‘test_devmap_pinning’:
./test_progs.h:352:35: error: expected expression before ‘{’ token352 | #define ASSERT_OK_PTR(ptr, name) ({ \ | ^ /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:12:2: note: in expansion of macro ‘ASSERT_OK_PTR’ 12 | ASSERT_OK_PTR(ptr, "first load"); | ^~~~~~~~~~~~~ /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/devmap.c:11:8: error: called object is not a function or function pointer 11 | ptr = test_pinned_devmap__open_and_load() | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~make: *** [Makefile:539:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/devmap.test.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf'
Error: Process completed with exit code 2.Quoted 32 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +8
-0+test_pinned_devmap__destroy (ptr); +ASSERT_OK_PTR (test_pinned_devmap__open_and_load (),"re-load" ); +} + +void test_devmap (void ) +{ +test_devmap_pinning (); +}tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +17
-0@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include <stddef.h> +#include <linux/bpf.h> +#include <linux/types.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP_HASH); + __uint(max_entries, 32); + __type(key, int); + __type(value, int); + __uint(pinning, LIBBPF_PIN_BY_NAME); +} repinned_dev_map SEC(".maps"); + + +char _license[] SEC("license") = "GPL";
Re: [PATCH net-next v2 2/2] Add a selftest for devmap pinning.
Quoted 4 lines · Pramukh Naduthota
Sorry, looks like I didn't run the tests again after fixing my checkpatch errors. Still new to this, and am quite mortified.
Is there a better way to fix this than sending out a v3 of my patch?
You'll need to send a v3 and be sure to build/run the selftests. Also for future reference please don't top post it. And thanks for working on the fix.