Fix pinning devmaps
Subsystems (4)
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.
Pramukh Naduthota (2):
Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned
devmaps
Add selftests for devmap pinning
tools/lib/bpf/libbpf.c | 8 +++++++- .../testing/selftests/bpf/prog_tests/devmap.c | 21 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 ++++++++++++++++ 3 files changed, 45 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 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
Martin KaFai Lau
reviewerRe: [PATCH bpf 1/2] Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned devmaps
Quoted 25 lines · Pramukh Naduthota
Ignore
BPF_F_RDONLY_PROGwhen 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 addingRDONLY_PROGto the users's flags when testing against the pinned mapFixes 57a00f41644f ("libbpf: Add auto-pinning of maps when loading BPF objects")Signed-off-by Pramukh Naduthota <[email protected]>--- tools/lib/bpf/libbpf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)tools/lib/bpf/libbpf.c +1
-0@@ -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);
tools/lib/bpf/libbpf.c static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) · line 4837
Quoted 7 lines
tools/lib/bpf/libbpf.c +4
-0@@ -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 ;
May be set BPF_F_RDONLY_PROG in effective_flags only when map->def.map_flags does not have both BPF_F_RDONLY_PROG and BPF_F_WRONLY_PROG set? Just in case the devmap may support setting them during map creation in the future.
Quoted 10 lines
tools/lib/bpf/libbpf.c +2
-1+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); }
Re: [PATCH bpf 1/2] Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned devmaps
Quoted 30 lines · Martin KaFai Lau
Quoted 28 lines · Pramukh Naduthota
Ignore
BPF_F_RDONLY_PROGwhen 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 addingRDONLY_PROGto the users's flags when testing against the pinned mapFixes 57a00f41644f ("libbpf: Add auto-pinning of maps when loading BPF objects")Signed-off-by Pramukh Naduthota <[email protected]>--- tools/lib/bpf/libbpf.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)tools/lib/bpf/libbpf.c +1
-0@@ -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; }
Quoted 9 lines
tools/lib/bpf/libbpf.c static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd) · line 4837
Quoted 4 lines
tools/lib/bpf/libbpf.c +4
-0+/* 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 ;May be set
BPF_F_RDONLY_PROGineffective_flagsonly whenmap->def.map_flagsdoes not have bothBPF_F_RDONLY_PROGandBPF_F_WRONLY_PROGset? Just in case the devmap may support setting them during map creation in the future.
Would it be sane to just ignore BPF_F_RDONLY|BPF_F_WRONLY|BPF_F_RDONLY_PROG|BPF_F_WRONLY_PROG flags during this comparison? Those flags don't really change compatibility of two maps in terms of their definition, right? Just that for some combination of those flags BPF program load might fail (e.g., if it is trying to modify BPF_F_RDONLY_PROG map). But that will be pretty obvious from BPF the verifier log?
Quoted 11 lines
Quoted 10 lines
tools/lib/bpf/libbpf.c +2
-1+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); }
Re: [PATCH bpf 1/2] Ignore RDONLY_PROG for devmaps in libbpf to allow re-loading of pinned devmaps
Do you have any more thoughts on this?
For my 2c:
- It looks like the kernel is making the map BPF_F_RDONLY_PROG because "Lookup returns a pointer straight to dev->ifindex", so I don't know
if we have to
worry about the kernel allowing write access to devmaps
- I like having the guardrails in libbpf, but I'm not sure what the exact contract
is for what libbpf will let me reload
I'll spin out a V2 similar to what I had if nobody has any strong opinions on the exact behavior of this check
- Pramukh
PATCH 2/2 Add selftests for devmap pinning
In reply to Pramukh NaduthotaAdd a test for devmap pinning.
--- .../testing/selftests/bpf/prog_tests/devmap.c | 21 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 +++++++++++++++ 2 files changed, 38 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 +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Google +#include "testing_helpers.h" +#include "test_progs.h" +#include "test_pinned_devmap.skel.h" +#include "test_pinned_devmap_rdonly_prog.skel.h" + +void test_devmap_pinning (void ) +{ +struct test_pinned_devmap *ptr; + +ASSERT_OK_PTR (ptr =test_pinned_devmap__open_and_load (),"first load" ); +test_pinned_devmap__destroy (ptr); +ASSERT_OK_PTR (test_pinned_devmap__open_and_load (),"re-load" ); +} + +void test_devmap (void ) +{ +if (test__start_subtest ("pinned_devmap" )) +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); +} dev_map SEC(".maps"); + + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.30.2
Re: [PATCH bpf 2/2] Add selftests for devmap pinning
Quoted 1 line
+#include "
test_pinned_devmap_rdonly_prog.skel.h"
Oops, it looks like I forgot to clean up from an earlier idea. I'll send out a v2 with that removed, but I'd still appreciated feedback on whether this is the right fix for devmap pinning.
Re: [PATCH bpf 2/2] Add selftests for devmap pinning
In reply to Pramukh NaduthotaQuoted 17 lines · Pramukh Naduthota
Add a test for devmap pinning.
Signed-off-by Pramukh Naduthota <[email protected]>--- .../testing/selftests/bpf/prog_tests/devmap.c | 21 +++++++++++++++++++ .../selftests/bpf/progs/test_pinned_devmap.c | 17 +++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/devmap.c create mode 100644 tools/testing/selftests/bpf/progs/test_pinned_devmap.ctools/testing/selftests/bpf/prog_tests/devmap.c new file +0
-0
tools/testing/selftests/bpf/prog_tests/devmap.c line 2
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +2
-0@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Google
nit: use /* */ for copyright line
tools/testing/selftests/bpf/prog_tests/devmap.c line 8
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +6
-0+#include "testing_helpers.h" +#include "test_progs.h" +#include "test_pinned_devmap.skel.h" +#include "test_pinned_devmap_rdonly_prog.skel.h" + +void test_devmap_pinning (void )
static
tools/testing/selftests/bpf/prog_tests/devmap.c line 12
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +4
-0+{ +struct test_pinned_devmap *ptr; + +ASSERT_OK_PTR (ptr =test_pinned_devmap__open_and_load (),"first load" );
nit: don't be too clever inside ASSERT_OK_PTR(), do assignment outside and then check ptr
tools/testing/selftests/bpf/prog_tests/devmap.c line 20
Quoted 8 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 ) +{ +if (test__start_subtest ("pinned_devmap" )) +test_devmap_pinning ();
if it's just one subtest then there isn't much point in making it a subtest
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/devmap.c new file +1
-0+}tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +4
-0@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ +#include <stddef.h> +#include <linux/bpf.h>
tools/testing/selftests/bpf/progs/test_pinned_devmap.c line 14
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +10
-0+#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); +} dev_map SEC(".maps");
please use a bit more specific name to minimize potential interference with other parallel tests
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_pinned_devmap.c new file +3
-1+ + +char _license[] SEC("license") = "GPL"; --2.30.2