Skip to content

Commit 3ec8560

Browse files
committed
Merge branch 'libbpf-fix-reuse-of-devmap'
Yureka Lilian says: ==================== libbpf: fix reuse of DEVMAP changes in v3: - instead of setting BPF_F_RDONLY_PROG on both sides, just clear BPF_F_RDONLY_PROG in map_info.map_flags as suggested by Andrii Nakryiko - in the test, use ASSERT_* instead of CHECK - shorten the test by using open_and_load from the skel - in the test, drop NULL check before unloading/destroying bpf objs - start the commit messages with "libbpf" and "selftests/bpf" respectively instead of just "bpf" changes in v2: - preserve compatibility with older kernels - add a basic selftest covering the re-use of DEVMAP maps ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents abdaf49 + 7f8fa9d commit 3ec8560

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,6 +5093,16 @@ static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
50935093
return false;
50945094
}
50955095

5096+
/*
5097+
* bpf_get_map_info_by_fd() for DEVMAP will always return flags with
5098+
* BPF_F_RDONLY_PROG set, but it generally is not set at map creation time.
5099+
* Thus, ignore the BPF_F_RDONLY_PROG flag in the flags returned from
5100+
* bpf_get_map_info_by_fd() when checking for compatibility with an
5101+
* existing DEVMAP.
5102+
*/
5103+
if (map->def.type == BPF_MAP_TYPE_DEVMAP || map->def.type == BPF_MAP_TYPE_DEVMAP_HASH)
5104+
map_info.map_flags &= ~BPF_F_RDONLY_PROG;
5105+
50965106
return (map_info.type == map->def.type &&
50975107
map_info.key_size == map->def.key_size &&
50985108
map_info.value_size == map->def.value_size &&
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <sys/types.h>
4+
#include <sys/stat.h>
5+
#include <unistd.h>
6+
#include <test_progs.h>
7+
8+
9+
#include "test_pinning_devmap.skel.h"
10+
11+
void test_pinning_devmap_reuse(void)
12+
{
13+
const char *pinpath1 = "/sys/fs/bpf/pinmap1";
14+
const char *pinpath2 = "/sys/fs/bpf/pinmap2";
15+
struct test_pinning_devmap *skel1 = NULL, *skel2 = NULL;
16+
int err;
17+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
18+
19+
/* load the object a first time */
20+
skel1 = test_pinning_devmap__open_and_load();
21+
if (!ASSERT_OK_PTR(skel1, "skel_load1"))
22+
goto out;
23+
24+
/* load the object a second time, re-using the pinned map */
25+
skel2 = test_pinning_devmap__open_and_load();
26+
if (!ASSERT_OK_PTR(skel2, "skel_load2"))
27+
goto out;
28+
29+
/* we can close the reference safely without
30+
* the map's refcount falling to 0
31+
*/
32+
test_pinning_devmap__destroy(skel1);
33+
skel1 = NULL;
34+
35+
/* now, swap the pins */
36+
err = renameat2(0, pinpath1, 0, pinpath2, RENAME_EXCHANGE);
37+
if (!ASSERT_OK(err, "swap pins"))
38+
goto out;
39+
40+
/* load the object again, this time the re-use should fail */
41+
skel1 = test_pinning_devmap__open_and_load();
42+
if (!ASSERT_ERR_PTR(skel1, "skel_load3"))
43+
goto out;
44+
45+
out:
46+
unlink(pinpath1);
47+
unlink(pinpath2);
48+
test_pinning_devmap__destroy(skel1);
49+
test_pinning_devmap__destroy(skel2);
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_DEVMAP);
8+
__uint(max_entries, 1);
9+
__type(key, __u32);
10+
__type(value, __u32);
11+
__uint(pinning, LIBBPF_PIN_BY_NAME);
12+
} pinmap1 SEC(".maps");
13+
14+
struct {
15+
__uint(type, BPF_MAP_TYPE_DEVMAP);
16+
__uint(max_entries, 2);
17+
__type(key, __u32);
18+
__type(value, __u32);
19+
__uint(pinning, LIBBPF_PIN_BY_NAME);
20+
} pinmap2 SEC(".maps");

0 commit comments

Comments
 (0)