All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] libbpf: Load objects with weak typed ksyms on kernels without BTF
@ 2026-12-05 21:28 ` Geyslan Gregório via B4 Relay
  0 siblings, 0 replies; 11+ messages in thread
From: Geyslan Gregório @ 2026-12-05 21:28 UTC (permalink / raw)
  To: bpf
  Cc: Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
	Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Samuel Wu, Shuah Khan, linux-kselftest,
	linux-kernel, Geyslan Gregório

A program can declare a kfunc __weak, test it with bpf_ksym_exists() and
call it only where it exists. Across kernels with BTF this works: where
the kfunc is missing, libbpf leaves it unresolved and the object loads.

On a kernel built without CONFIG_DEBUG_INFO_BTF it does not. Any typed
ksym makes libbpf load vmlinux BTF, and the load fails with -ESRCH even
when every typed ksym is weak and btf_custom_path serves CO-RE. A tool
that runs on such kernels with an external BTF file cannot use a weak
kfunc at all. We hit this with bpf_preempt_disable() and
bpf_preempt_enable(), declared weak in a tracing program that also runs
on Ubuntu 20.04 kernels older than 5.4.0-92, which have no BTF.

Patch 1 is a fix that stands on its own. When an object has a typeless
ksym, libbpf reads /proc/kallsyms and marks the typed variables it
finds there as resolved. A weak typed variable that kernel BTF lacks is
then relocated with BTF id 0, and the verifier rejects the program.
Patch 2 would run into it on a kernel without BTF, where every weak
typed variable is unresolved.

Patch 2 makes vmlinux BTF optional when weak typed ksyms are its only
users and libbpf finds no usable kernel BTF (-ESRCH): nothing in
/sys/kernel/btf and no vmlinux image with BTF it can parse. Those ksyms
are left unresolved, as on a kernel with BTF that lacks them.

What patch 2 does not change:

- kfuncs still need BTF. A reachable call to an unresolved weak kfunc
  is poisoned and the verifier rejects it, as on any kernel that lacks
  the kfunc.
- ksyms are not resolved against btf_custom_path. It stays CO-RE only,
  as libbpf.h documents.
- If /sys/kernel/btf/vmlinux exists but cannot be read, the load still
  fails, so a kernel with kfuncs does not lose them silently.
- Objects that need vmlinux BTF for anything else fail as before.
  Light skeletons are not touched.

One more case changes with it: where the kernel has BTF and the process
cannot see /sys/kernel/btf, such an object used to fail and now loads
with its weak typed ksyms unresolved.

cilium/ebpf has done the same since 2025 [1]: on a kernel without BTF
it poisons every weak kfunc call instead of failing the load.

Patch 3 adds a ksyms_btf subtest. The test kernel has BTF, so a child
hides /sys/kernel/btf in its own mount namespace and passes a copy of
vmlinux BTF as btf_custom_path. An object with weak typed ksyms and a
typeless ksym has to load and run with the typed ones absent. One that
adds a strong typed ksym has to fail.

Two series in review touch the same code:

- Ihor's "libbpf: Resolve kfunc variants by prototype" [2] replaces
  find_ksym_btf_id() in the kfunc path. Patch 2 skips the typed ksym
  lookup before that point, so neither series needs a change for the
  other.
- Samuel's "libbpf: Resolve typeless ksyms via KALLSYMS_QUERY_BY_NAME
  ioctl" [3] queries typeless ksyms only and keeps kallsyms_cb() as the
  fallback. Patch 1 makes the fallback do the same.

btf__load_vmlinux_btf() still prints its two warnings before libbpf goes
on without vmlinux BTF.

How this series was made: I wrote it with Claude Code, an LLM coding
agent. From my instructions it traced the failure, wrote the patches
and this letter, searched the bpf archive for earlier discussion, and
built and ran the tests below. I reviewed the code, the tests and the
results, and I answer for all of it.

Tested on x86_64 with the selftests config, in QEMU with the BPF CI
root filesystem:

1. A kernel built without CONFIG_DEBUG_INFO_BTF, its BTF for
   btf_custom_path encoded by pahole from its DWARF, and a small loader
   linked with libbpf of bpf-next ("base"), with patch 2 only and with
   the series:

   object                             base     patch 2   series
   A: weak typed ksyms and a          -ESRCH   verifier  loads
      typeless ksym, btf_custom_path           rejects
   A without btf_custom_path          -ESRCH   -ESRCH    -ESRCH
   weak and strong typed ksyms        -ESRCH   -ESRCH    -ESRCH
   strong typed ksyms                 -ESRCH   -ESRCH    -ESRCH
   weak kfunc called without a guard  -ESRCH   verifier  verifier
                                               rejects   rejects

   A is the object of patch 3. With the series its typed ksyms read as
   absent and its CO-RE read is correct. With patch 2 only the verifier
   says "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is
   specified in Kconfig". For the unguarded kfunc call the load log
   reads "kfunc 'bpf_rcu_read_lock' is referenced but wasn't resolved".

2. The same kernel built with BTF. All five load with each libbpf, and
   the ksyms resolve. Also there:

   - The example of patch 1 fails with base and with patch 2 only
     ("pseudo btf_id 0 in ldimm64 isn't KIND_VAR or KIND_FUNC") and
     loads with the series.
   - With /sys/kernel/btf hidden, A behaves as on the kernel without
     BTF.
   - With a garbage or an empty file in place of
     /sys/kernel/btf/vmlinux, A fails to load with and without the
     series.

3. "./test_progs -d @DENYLIST" on the kernel with BTF, linked
   statically, with the series and with the same tests on libbpf of
   bpf-next:

     series:  Summary: 894/9278 PASSED, 169 SKIPPED, 4/8 FAILED
     base:    Summary: 893/9277 PASSED, 169 SKIPPED, 5/9 FAILED

   The runs differ in the new subtest only. The subtest also fails
   with patch 2 only, and passes in a new PID namespace ("unshare --pid
   --fork --mount-proc"). attach_probe/uprobe-lib, file_reader,
   uprobe_autoattach and three verifier_passes fixups subtests fail in
   both runs.

[1] https://github.com/cilium/ebpf/pull/1774
[2] https://lore.kernel.org/bpf/20261124224811.16134-4-ihor.solodrai@linux.dev/
[3] https://lore.kernel.org/bpf/20261202001344.2721429-21-wusamuel@google.com/

---
Geyslan Gregório (3):
      libbpf: Do not resolve typed ksyms through kallsyms
      libbpf: Load objects with weak typed ksyms on kernels without BTF
      selftests/bpf: Test weak typed ksyms without vmlinux BTF

 tools/lib/bpf/libbpf.c                             |  57 ++++++---
 tools/testing/selftests/bpf/prog_tests/ksyms_btf.c | 138 +++++++++++++++++++++
 .../selftests/bpf/progs/test_ksyms_mixed_no_btf.c  |  25 ++++
 .../selftests/bpf/progs/test_ksyms_weak_no_btf.c   |  50 ++++++++
 4 files changed, 256 insertions(+), 14 deletions(-)
---
base-commit: 653033585181fecef93476a030e8b6a4abad65cc
change-id: 20261205-libbpf-weak-typed-ksyms-no-vmlinux-btf-b19f8c5a8928

Best regards,
-- 
Geyslan Gregório <gg@condado.dev>


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-05 21:40 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 21:28 [PATCH bpf-next 0/3] libbpf: Load objects with weak typed ksyms on kernels without BTF Geyslan Gregório
2026-12-05 21:28 ` Geyslan Gregório via B4 Relay
2026-12-05 21:28 ` [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms Geyslan Gregório
2026-12-05 21:28   ` Geyslan Gregório via B4 Relay
2026-12-05 21:33   ` sashiko-bot
2026-12-05 21:28 ` [PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF Geyslan Gregório
2026-12-05 21:28   ` Geyslan Gregório via B4 Relay
2026-12-05 21:36   ` sashiko-bot
2026-12-05 21:28 ` [PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF Geyslan Gregório
2026-12-05 21:28   ` Geyslan Gregório via B4 Relay
2026-12-05 21:40   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.