When playing around with defining kfuncs in some custom modules, we noticed that if a BPF program calls two functions with the same signature in two different modules, the function from the wrong module may sometimes end up being called. Whether this happens depends on the order of the calls in the BPF program, which turns out to be due to the use of sort() inside __find_kfunc_desc_btf() in the verifier code. This series contains a fix for the issue (first patch), and a selftest to trigger it (last patch). The two middle commits refactor some of the selftest code to better handle building and loading multiple kernel modules as part of the testing. See the individual patch descriptions for more details. --- Simon Sundberg (2): selftests/bpf: Provide a generic [un]load_module helper selftests/bpf: Add test for kfunc module order Toke Høiland-Jørgensen (2): bpf: fix kfunc btf caching for modules selftests/bpf: Consolidate kernel modules into common directory kernel/bpf/verifier.c | 8 +++- tools/testing/selftests/bpf/Makefile | 35 +++++++------- .../testing/selftests/bpf/bpf_test_no_cfi/Makefile | 19 -------- tools/testing/selftests/bpf/bpf_testmod/Makefile | 20 -------- .../testing/selftests/bpf/prog_tests/core_reloc.c | 2 +- .../selftests/bpf/prog_tests/kfunc_module_order.c | 55 ++++++++++++++++++++++ tools/testing/selftests/bpf/progs/bad_struct_ops.c | 2 +- tools/testing/selftests/bpf/progs/cb_refs.c | 2 +- tools/testing/selftests/bpf/progs/epilogue_exit.c | 4 +- .../selftests/bpf/progs/epilogue_tailcall.c | 4 +- tools/testing/selftests/bpf/progs/iters_testmod.c | 2 +- tools/testing/selftests/bpf/progs/jit_probe_mem.c | 2 +- .../selftests/bpf/progs/kfunc_call_destructive.c | 2 +- .../testing/selftests/bpf/progs/kfunc_call_fail.c | 2 +- .../testing/selftests/bpf/progs/kfunc_call_race.c | 2 +- .../testing/selftests/bpf/progs/kfunc_call_test.c | 2 +- .../selftests/bpf/progs/kfunc_call_test_subprog.c | 2 +- .../selftests/bpf/progs/kfunc_module_order.c | 30 ++++++++++++ .../testing/selftests/bpf/progs/local_kptr_stash.c | 2 +- tools/testing/selftests/bpf/progs/map_kptr.c | 2 +- tools/testing/selftests/bpf/progs/map_kptr_fail.c | 2 +- tools/testing/selftests/bpf/progs/missed_kprobe.c | 2 +- .../selftests/bpf/progs/missed_kprobe_recursion.c | 2 +- tools/testing/selftests/bpf/progs/nested_acquire.c | 2 +- tools/testing/selftests/bpf/progs/pro_epilogue.c | 4 +- .../selftests/bpf/progs/pro_epilogue_goto_start.c | 4 +- tools/testing/selftests/bpf/progs/sock_addr_kern.c | 2 +- .../selftests/bpf/progs/struct_ops_detach.c | 2 +- .../selftests/bpf/progs/struct_ops_forgotten_cb.c | 2 +- .../selftests/bpf/progs/struct_ops_maybe_null.c | 2 +- .../bpf/progs/struct_ops_maybe_null_fail.c | 2 +- .../selftests/bpf/progs/struct_ops_module.c | 2 +- .../selftests/bpf/progs/struct_ops_multi_pages.c | 2 +- .../selftests/bpf/progs/struct_ops_nulled_out_cb.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/test_module_attach.c | 2 +- .../selftests/bpf/progs/test_tp_btf_nullable.c | 2 +- .../testing/selftests/bpf/progs/unsupported_ops.c | 2 +- tools/testing/selftests/bpf/progs/wq.c | 2 +- tools/testing/selftests/bpf/progs/wq_failures.c | 2 +- .../bpf/{bpf_testmod => test_kmods}/.gitignore | 0 tools/testing/selftests/bpf/test_kmods/Makefile | 26 ++++++++++ .../selftests/bpf/test_kmods/bpf_test_modorder_x.c | 39 +++++++++++++++ .../selftests/bpf/test_kmods/bpf_test_modorder_y.c | 39 +++++++++++++++ .../bpf_test_no_cfi.c | 0 .../bpf_testmod-events.h | 0 .../bpf/{bpf_testmod => test_kmods}/bpf_testmod.c | 0 .../bpf/{bpf_testmod => test_kmods}/bpf_testmod.h | 0 .../bpf_testmod_kfunc.h | 0 tools/testing/selftests/bpf/testing_helpers.c | 34 ++++++++----- tools/testing/selftests/bpf/testing_helpers.h | 2 + 51 files changed, 275 insertions(+), 108 deletions(-) --- base-commit: bcd28cfd04ebd3f871443e4746e511147686e517 change-id: 20241008-fix-kfunc-btf-caching-for-modules-b62603484ffb Best regards, -- Toke Høiland-Jørgensen <toke@xxxxxxxxxx>