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

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Fix map_kptr test failure

On my arm64 machine, I get the following failure:
...
tester_init:PASS:tester_log_buf 0 nsec
process_subtest:PASS:obj_open_mem 0 nsec
process_subtest:PASS:specs_alloc 0 nsec
serial_test_map_kptr:PASS:rcu_tasks_trace_gp__open_and_load 0 nsec
...
test_map_kptr_success:PASS:map_kptr__open_and_load 0 nsec
test_map_kptr_success:PASS:test_map_kptr_ref1 refcount 0 nsec
test_map_kptr_success:FAIL:test_map_kptr_ref1 retval unexpected error: 2 (errno 2)
test_map_kptr_success:PASS:test_map_kptr_ref2 refcount 0 nsec
test_map_kptr_success:FAIL:test_map_kptr_ref2 retval unexpected error: 1 (errno 2)
...
#201/21 map_kptr/success-map:FAIL

In serial_test_map_kptr(), before test_map_kptr_success(), one
kern_sync_rcu() is used to have some delay for freeing the map.
But in my environment, one kern_sync_rcu() seems not enough and
caused the test failure.

In bpf_map_free_in_work() in syscall.c, the queue time for
queue_work(system_dfl_wq, &map->work)
may be longer than expected. This may cause the test failure
since test_map_kptr_success() expects all previous maps having been freed.

Since it is not clear how long queue_work() time takes, a bpf prog
is added to count the reference after bpf_kfunc_call_test_acquire().
If the number of references is 2 (for initial ref and the one just
acquired), all previous maps should have been released. This will
resolve the above 'retval unexpected error' issue.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/bpf/20260116052245.3692405-1-yonghong.song@linux.dev

authored by

Yonghong Song and committed by
Andrii Nakryiko
efad162f 47d440d0

+41
+23
tools/testing/selftests/bpf/prog_tests/map_kptr.c
··· 131 131 return 0; 132 132 } 133 133 134 + static void wait_for_map_release(void) 135 + { 136 + LIBBPF_OPTS(bpf_test_run_opts, lopts); 137 + struct map_kptr *skel; 138 + int ret; 139 + 140 + skel = map_kptr__open_and_load(); 141 + if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load")) 142 + return; 143 + 144 + do { 145 + ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.count_ref), &lopts); 146 + ASSERT_OK(ret, "count_ref ret"); 147 + ASSERT_OK(lopts.retval, "count_ref retval"); 148 + } while (skel->bss->num_of_refs != 2); 149 + 150 + map_kptr__destroy(skel); 151 + } 152 + 134 153 void serial_test_map_kptr(void) 135 154 { 136 155 struct rcu_tasks_trace_gp *skel; ··· 167 148 168 149 ASSERT_OK(kern_sync_rcu_tasks_trace(skel), "sync rcu_tasks_trace"); 169 150 ASSERT_OK(kern_sync_rcu(), "sync rcu"); 151 + wait_for_map_release(); 152 + 170 153 /* Observe refcount dropping to 1 on bpf_map_free_deferred */ 171 154 test_map_kptr_success(false); 172 155 173 156 ASSERT_OK(kern_sync_rcu_tasks_trace(skel), "sync rcu_tasks_trace"); 174 157 ASSERT_OK(kern_sync_rcu(), "sync rcu"); 158 + wait_for_map_release(); 159 + 175 160 /* Observe refcount dropping to 1 on synchronous delete elem */ 176 161 test_map_kptr_success(true); 177 162 }
+18
tools/testing/selftests/bpf/progs/map_kptr.c
··· 487 487 return 0; 488 488 } 489 489 490 + int num_of_refs; 491 + 492 + SEC("syscall") 493 + int count_ref(void *ctx) 494 + { 495 + struct prog_test_ref_kfunc *p; 496 + unsigned long arg = 0; 497 + 498 + p = bpf_kfunc_call_test_acquire(&arg); 499 + if (!p) 500 + return 1; 501 + 502 + num_of_refs = p->cnt.refs.counter; 503 + 504 + bpf_kfunc_call_test_release(p); 505 + return 0; 506 + } 507 + 490 508 SEC("syscall") 491 509 int test_ls_map_kptr_ref1(void *ctx) 492 510 {