Skip to content

Fix BPF struct_ops BTF cleanup race condition

Gregory Bell bpf oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest1 patch 10 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/1bpf: Fix BTF module cleanup race condition in struct_opskernel/bpf/btf.c+6 -116 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next 0/1] Fix BPF struct_ops BTF cleanup race condition

This patch fixes a race condition in BPF struct_ops where consecutive test runs fail due to a timing mismatch between BTF object cleanup and BTF module removal.

When running BPF struct_ops tests consecutively, the second test fails because BTF object references take longer to be released while their associated module is removed from btf_modules list immediately during module unload. This creates a race condition where the second test retrieves the cached BTF object but cannot find valid module information, causing btf_try_get_module() to return NULL.

The fix synchronizes these cleanup operations by waiting for active BTF references to be released before completing module cleanup, eliminating the race condition.

This issue is reproducible with any of the following tests: global_map_resize, pro_epilogue, struct_ops_module and struct_ops_multi

./test_progs -t global_map_resize; ./test_progs -t global_map_resize

#134/1 global_map_resize/global_map_resize_bss:OK
#134/2 global_map_resize/global_map_resize_data:OK
#134/3 global_map_resize/global_map_resize_invalid:OK
#134 global_map_resize:OK
Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED global_map_resize_bss_subtest:PASS:test_global_map_resize__open 0 nsec
libbpf: map 'test_glo.bss': cannot be resized, last var must be an array
libbpf: map 'test_glo.bss': failed to adjust resized BTF, clearing BTF key/value info: -EINVAL global_map_resize_bss_subtest:PASS:bpf_map__set_value_size 0 nsec global_map_resize_bss_subtest:PASS:resize 0 nsec global_map_resize_bss_subtest:PASS:percpu_arr_resize 0 nsec global_map_resize_bss_subtest:PASS:array_len 0 nsec global_map_resize_bss_subtest:PASS:bpf_map__initial_value (ptr) 0 nsec global_map_resize_bss_subtest:PASS:bpf_map__initial_value (size) 0 nsec
libbpf: map 'st_ops_resize': failed to create: -EINVAL
libbpf: failed to load BPF skeleton 'test_global_map_resize': -EINVAL global_map_resize_bss_subtest:FAIL:test_global_map_resize__load unexpected error: -22 (errno 22)
#134/1 global_map_resize/global_map_resize_bss:FAIL
global_map_resize_data_subtest:PASS:test_global_map_resize__open 0 nsec global_map_resize_data_subtest:PASS:bpf_map__set_value_size 0 nsec global_map_resize_data_subtest:PASS:resize 0 nsec global_map_resize_data_subtest:PASS:percpu_arr_resize 0 nsec global_map_resize_data_subtest:PASS:array_len 0 nsec global_map_resize_data_subtest:PASS:bpf_map__initial_value (ptr) 0 nsec global_map_resize_data_subtest:PASS:bpf_map__initial_value (size) 0 nsec
libbpf: map 'st_ops_resize': failed to create: -EINVAL
libbpf: failed to load BPF skeleton 'test_global_map_resize': -EINVAL global_map_resize_data_subtest:FAIL:test_global_map_resize__load unexpected error: -22 (errno 22)
#134/2 global_map_resize/global_map_resize_data:FAIL
#134/3 global_map_resize/global_map_resize_invalid:OK
#134 global_map_resize:FAIL

After the fix, consecutive runs work reliably without requiring delays.

Gregory Bell (1):
bpf: Fix BTF module cleanup race condition in struct_ops

 kernel/bpf/btf.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Signature · 1 line
2.52.0