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

selftests/bpf: Fix flaky test btf_map_in_map/lookup_update

Recently, I frequently hit the following test failure:

[root@arch-fb-vm1 bpf]# ./test_progs -n 33/1
test_lookup_update:PASS:skel_open 0 nsec
[...]
test_lookup_update:PASS:sync_rcu 0 nsec
test_lookup_update:FAIL:map1_leak inner_map1 leaked!
#33/1 btf_map_in_map/lookup_update:FAIL
#33 btf_map_in_map:FAIL

In the test, after map is closed and then after two rcu grace periods,
it is assumed that map_id is not available to user space.

But the above assumption cannot be guaranteed. After zero or one
or two rcu grace periods in different siturations, the actual
freeing-map-work is put into a workqueue. Later on, when the work
is dequeued, the map will be actually freed.
See bpf_map_put() in kernel/bpf/syscall.c.

By using workqueue, there is no ganrantee that map will be actually
freed after a couple of rcu grace periods. This patch removed
such map leak detection and then the test can pass consistently.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20240322061353.632136-1-yonghong.song@linux.dev

authored by

Yonghong Song and committed by
Daniel Borkmann
14bb1e8c 770546ae

+1 -25
+1 -25
tools/testing/selftests/bpf/prog_tests/btf_map_in_map.c
··· 25 25 int map1_fd, map2_fd, map3_fd, map4_fd, map5_fd, map1_id, map2_id; 26 26 int outer_arr_fd, outer_hash_fd, outer_arr_dyn_fd; 27 27 struct test_btf_map_in_map *skel; 28 - int err, key = 0, val, i, fd; 28 + int err, key = 0, val, i; 29 29 30 30 skel = test_btf_map_in_map__open_and_load(); 31 31 if (CHECK(!skel, "skel_open", "failed to open&load skeleton\n")) ··· 101 101 map2_id = bpf_map_id(skel->maps.inner_map2); 102 102 CHECK(map1_id == 0, "map1_id", "failed to get ID 1\n"); 103 103 CHECK(map2_id == 0, "map2_id", "failed to get ID 2\n"); 104 - 105 - test_btf_map_in_map__destroy(skel); 106 - skel = NULL; 107 - 108 - /* we need to either wait for or force synchronize_rcu(), before 109 - * checking for "still exists" condition, otherwise map could still be 110 - * resolvable by ID, causing false positives. 111 - * 112 - * Older kernels (5.8 and earlier) freed map only after two 113 - * synchronize_rcu()s, so trigger two, to be entirely sure. 114 - */ 115 - CHECK(kern_sync_rcu(), "sync_rcu", "failed\n"); 116 - CHECK(kern_sync_rcu(), "sync_rcu", "failed\n"); 117 - 118 - fd = bpf_map_get_fd_by_id(map1_id); 119 - if (CHECK(fd >= 0, "map1_leak", "inner_map1 leaked!\n")) { 120 - close(fd); 121 - goto cleanup; 122 - } 123 - fd = bpf_map_get_fd_by_id(map2_id); 124 - if (CHECK(fd >= 0, "map2_leak", "inner_map2 leaked!\n")) { 125 - close(fd); 126 - goto cleanup; 127 - } 128 104 129 105 cleanup: 130 106 test_btf_map_in_map__destroy(skel);