Skip to content

Commit 7f8fa9d

Browse files
yuyuyurekaanakryiko
authored andcommitted
selftests/bpf: Add test for DEVMAP reuse
The test covers basic re-use of a pinned DEVMAP map, with both matching and mismatching parameters. Signed-off-by: Yureka Lilian <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6c6b414 commit 7f8fa9d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
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)