Skip to contentSkip to conversation

Enable module kfuncs to override vmlinux kfuncs

PATCH Superseded Song Chen 1 participant 1 message Apr 30 BPF

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Song Chen

author
#0 lore raw

cover Enable module kfuncs to override vmlinux kfuncs

This patchset implements kfunc live-patching by allowing kernel modules to override vmlinux kfuncs with the same name. This addresses the use case suggested by Alexei where modules can provide enhanced, optimized, or fixed versions of existing kfuncs without modifying the kernel, see [1].

Currently, when a kfunc exists in both vmlinux and a module, the vmlinux version is always used, making it impossible for modules to override kernel implementations. This limitation prevents useful scenarios like:
- Testing improved kfunc implementations before upstreaming
- Applying targeted fixes via modules without kernel recompilation
- Providing vendor-specific optimizations for certain kfuncs

The solution involves two coordinated changes:

Patch 1 modifies libbpf's find_ksym_btf_id() to search module BTFs before vmlinux BTF, and encodes the kfunc source (vmlinux or module) in the instruction's 'off' field during relocation.

Patch 2 updates the verifier to use insn->off for kfunc address
resolution: when off == 0, it looks up the kfunc in vmlinux via kallsyms; when off > 0, it searches in the corresponding module's symbol table.

Together, these changes enable module kfuncs to take precedence over vmlinux kfuncs, while maintaining backward compatibility for programs that don't use module BTFs.

[1]:https://lore.kernel.org/all/CAADnVQLVQW-Mb_d+zT+aSpPOZ042erPVcSHVBx9PMGET+a754Q@mail.gmail.com/

Song Chen (2):
tools/lib/bpf/libbpf: Prioritize module kfuncs over vmlinux kfuncs kernel/bpf/verifier: Support module kfunc resolution via instruction
offset

 kernel/bpf/verifier.c  |  5 ++++-
 tools/lib/bpf/libbpf.c | 34 ++++++++++++++++++----------------
 2 files changed, 22 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0