Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <yuka@yuka.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20250814180113.1245565-4-yuka@yuka.dev

authored by

Yureka Lilian and committed by
Andrii Nakryiko
7f8fa9d3 6c6b4146

+70
+50
tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c
··· 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 + }
+20
tools/testing/selftests/bpf/progs/test_pinning_devmap.c
··· 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");