xref: /linux/tools/testing/selftests/bpf/prog_tests/pinning_devmap_reuse.c (revision ae28ed4578e6d5a481e39c5a9827f27048661fdd)
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 }
51