[RFC PATCH bpf-next v3 0/2] libbpf: Improve BPF load performance by selectively loading kmod BTFs
From: Fuyu Zhao
Date: Wed Aug 19 2026 - 05:05:10 EST
Currently, during BPF object loading, load_module_btfs() unconditionally
iterates over all kernel module BTFs and loads each one. This introduces
unnecessary overhead when a BPF program only needs BTFs from a specific,
small subset of modules. In environments with hundreds of modules,
loading all module BTFs can measurably increase the loading time.
In our Android testing, BPF programs are loaded on demand rather than
preloaded to avoid unnecessary memory usage from unused programs. With
93 module BTFs present, the total BPF loading time exceeds 300 ms, with
module BTF loading accounting for around 69% of the total loading time.
The existing module qualification in SEC(), such as
SEC("fentry/mymod:foo"), does not address this issue. It only affects
BTF ID lookup after module BTFs have already been loaded by
load_module_btfs(), and does not reduce the number of module BTFs loaded.
This series adds kmod_btf_names and kmod_btf_names_cnt fields to
bpf_object_open_opts, allowing users to specify a list of kernel modules
whose BTFs should be loaded. libbpf uses this information to skip
unrelated module BTFs during iteration and stops iterating once all
requested module BTFs have been loaded. Without these options, the
existing behavior remains unchanged.
Performance impact (BPF skeleton open and load time):
Modules loaded | Default (load all) | With kmod_btf_names | Speedup
---------------|--------------------|---------------------|--------
1 | 35.6 ms | 35.6 ms | Baseline
10 | 37.1 ms | 35.8 ms | +3.5%
100 | 46.7 ms | 36.7 ms | +21.4%
300 | 65.1 ms | 38.7 ms | +40.6%
Changelog:
v3:
- Use bpf_object_open_opts to specify kernel module BTFs to load rather
than introducing a new .kmod_btfs ELF section, as suggested by Andrii.
v2:
- Link: https://lore.kernel.org/bpf/20260813032613.2755-1-zhaofuyu@xxxxxxxx/
- Addressed issues with allocation error handling, multiple .kmod_btfs
sections, the transient stack pointer in is_kmod_btf_needed(), and
selftest dependency and comment formatting. (sashiko-bot)
- Removed KMODS_BTF_LOADED and KMODS_BTF_UNLOADED and simplified the
related logic.
v1:
- Link: https://lore.kernel.org/bpf/20260806042042.3239428-1-zhaofuyu@xxxxxxxx/
Fuyu Zhao (2):
libbpf: support selective kernel module BTF loading via
bpf_object_open_opts
selftests/bpf: add tests for selective kmod BTF loading
tools/lib/bpf/libbpf.c | 133 ++++++++++++
tools/lib/bpf/libbpf.h | 16 +-
.../selftests/bpf/prog_tests/kmod_btfs.c | 196 ++++++++++++++++++
tools/testing/selftests/bpf/progs/kmod_btfs.c | 13 ++
4 files changed, 357 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/kmod_btfs.c
create mode 100644 tools/testing/selftests/bpf/progs/kmod_btfs.c
--
2.34.1