Skip to content

Commit 270bc34

Browse files
liuhangbinkernel-patches-bot
authored andcommitted
selftest/bpf: test pinning map with reused map fd
This add a test to make sure that we can still pin maps with reused map fd. Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
1 parent 9fa13e5 commit 270bc34

File tree

1 file changed

+45
-1
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+45
-1
lines changed

tools/testing/selftests/bpf/prog_tests/pinning.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_pinning(void)
3737
struct stat statbuf = {};
3838
struct bpf_object *obj;
3939
struct bpf_map *map;
40-
int err;
40+
int err, map_fd;
4141
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
4242
.pin_root_path = custpath,
4343
);
@@ -213,6 +213,50 @@ void test_pinning(void)
213213
if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno))
214214
goto out;
215215

216+
/* remove the custom pin path to re-test it with reuse fd below */
217+
err = unlink(custpinpath);
218+
if (CHECK(err, "unlink custpinpath", "err %d errno %d\n", err, errno))
219+
goto out;
220+
221+
err = rmdir(custpath);
222+
if (CHECK(err, "rmdir custpindir", "err %d errno %d\n", err, errno))
223+
goto out;
224+
225+
bpf_object__close(obj);
226+
227+
/* test pinning at custom path with reuse fd */
228+
obj = bpf_object__open_file(file, NULL);
229+
if (CHECK_FAIL(libbpf_get_error(obj))) {
230+
obj = NULL;
231+
goto out;
232+
}
233+
234+
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(__u32),
235+
sizeof(__u64), 1, 0);
236+
if (CHECK(map_fd < 0, "create pinmap manually", "fd %d\n", map_fd))
237+
goto out;
238+
239+
map = bpf_object__find_map_by_name(obj, "pinmap");
240+
if (CHECK(!map, "find map", "NULL map"))
241+
goto out;
242+
243+
err = bpf_map__reuse_fd(map, map_fd);
244+
if (CHECK(err, "reuse pinmap fd", "err %d errno %d\n", err, errno))
245+
goto out;
246+
247+
err = bpf_map__set_pin_path(map, custpinpath);
248+
if (CHECK(err, "set pin path", "err %d errno %d\n", err, errno))
249+
goto out;
250+
251+
err = bpf_object__load(obj);
252+
if (CHECK(err, "custom load", "err %d errno %d\n", err, errno))
253+
goto out;
254+
255+
/* check that pinmap was pinned at the custom path */
256+
err = stat(custpinpath, &statbuf);
257+
if (CHECK(err, "stat custpinpath", "err %d errno %d\n", err, errno))
258+
goto out;
259+
216260
out:
217261
unlink(pinpath);
218262
unlink(nopinpath);

0 commit comments

Comments
 (0)