BPF List
 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
  2026-12-05 21:28 ` [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms Geyslan Gregório via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Geyslan Gregório via B4 Relay @ 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] 7+ messages in thread

* [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms
  2026-12-05 21:28 [PATCH bpf-next 0/3] libbpf: Load objects with weak typed ksyms on kernels without BTF Geyslan Gregório via B4 Relay
@ 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 via B4 Relay
  2026-12-05 21:28 ` [PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF Geyslan Gregório via B4 Relay
  2 siblings, 1 reply; 7+ messages in thread
From: Geyslan Gregório via B4 Relay @ 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

From: Geyslan Gregório <gg@condado.dev>

libbpf reads /proc/kallsyms when an object has a typeless ksym, and
kallsyms_cb() marks every variable ksym it finds there as set, typed
ones too. Typed ksyms are resolved through kernel BTF, and the
relocation of a typed ksym that is set uses its BTF id only.

A weak typed variable that kernel BTF does not have is left unresolved
by bpf_object__resolve_ksym_var_btf_id(). If kallsyms has a symbol of
that name, the extern is set all the same, and it is relocated as
BPF_PSEUDO_BTF_ID with id 0. The verifier rejects the program:

  pseudo btf_id 0 in ldimm64 isn't KIND_VAR or KIND_FUNC

For example, on a kernel whose BTF describes per-CPU variables only:

  extern const struct file_operations bpf_link_fops __ksym __weak;
  extern const void bpf_prog_active __ksym;

Have kallsyms_cb() skip typed ksyms. A function ksym always has a type,
so this replaces the check that skipped functions. A weak typed
variable missing from kernel BTF now reads as absent, as it already
does in an object with no typeless ksym.

Fixes: 2211c825e7b6 ("libbpf: Support weak typed ksyms.")
Signed-off-by: Geyslan Gregório <gg@condado.dev>
---
 tools/lib/bpf/libbpf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 2b188e64e356..6fe0143ea959 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9769,7 +9769,6 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
 		       const char *sym_name, void *ctx)
 {
 	struct bpf_object *obj = ctx;
-	const struct btf_type *t;
 	struct extern_desc *ext;
 	const char *res;
 
@@ -9781,8 +9780,10 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
 	if (!ext || ext->type != EXT_KSYM)
 		return 0;
 
-	t = btf__type_by_id(obj->btf, ext->btf_id);
-	if (!btf_is_var(t))
+	/* Typed ksyms, variables and functions, are resolved through kernel
+	 * BTF only.
+	 */
+	if (ext->ksym.type_id)
 		return 0;
 
 	if (ext->is_set && ext->ksym.addr != sym_addr) {

-- 
2.56.1



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

* [PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF
  2026-12-05 21:28 [PATCH bpf-next 0/3] libbpf: Load objects with weak typed ksyms on kernels without BTF 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 via B4 Relay
@ 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 via B4 Relay
  2 siblings, 1 reply; 7+ messages in thread
From: Geyslan Gregório via B4 Relay @ 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

From: Geyslan Gregório <gg@condado.dev>

An object that declares a typed ksym makes libbpf load vmlinux BTF, and
bpf_object__load() fails when the kernel was built without
CONFIG_DEBUG_INFO_BTF:

  libbpf: failed to find valid kernel BTF
  libbpf: Error loading vmlinux BTF: -ESRCH

It fails even when every typed ksym is weak and btf_custom_path serves
the CO-RE relocations. Weak ksyms are optional: when one is missing
from kernel BTF, libbpf leaves it unresolved, its address is 0, and a
call to an unresolved kfunc is poisoned and must be dead code. A kernel
without BTF has no kfunc and no typed variable to resolve to, so it is
the same case.

Have obj_needs_vmlinux_btf() tell whether vmlinux BTF is required, or
optional because weak typed ksyms are its only users. When it is
optional and libbpf finds no usable kernel BTF, go on without it.
bpf_object__resolve_externs() then skips the typed ksym lookup, and the
ksyms stay unresolved.

No usable kernel BTF is the -ESRCH of btf__load_vmlinux_btf(): nothing
in /sys/kernel/btf and no vmlinux image with BTF it can parse. Since
commit f2e4040c82d3 ("libbpf: Add some details for BTF parsing
failures") an error reading /sys/kernel/btf/vmlinux is returned as it
is, and it stays fatal here: the kernel has BTF, and its kfuncs must
not be dropped silently.

The same applies where the kernel has BTF and the process cannot see
/sys/kernel/btf, in a container without sysfs for example. Such an
object failed to load there, and now loads with its weak typed ksyms
unresolved. libbpf reports it at the info level.

An object that needs vmlinux BTF for another reason (a strong typed
ksym, CO-RE relocations without btf_custom_path, a BTF-typed program, a
struct_ops map) fails as before, and so do the callers that ask for
vmlinux BTF explicitly. The light skeleton loader is not changed.

Signed-off-by: Geyslan Gregório <gg@condado.dev>
---
 tools/lib/bpf/libbpf.c | 50 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 11 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6fe0143ea959..5e2bb95bda95 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -3736,58 +3736,83 @@ static bool map_needs_vmlinux_btf(struct bpf_map *map)
 	return bpf_map__is_struct_ops(map);
 }
 
-static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
+enum vmlinux_btf_need {
+	VMLINUX_BTF_NONE,
+	/* Only weak typed ksyms use it. Without kernel BTF they are left
+	 * unresolved, as on a kernel with BTF that lacks them.
+	 */
+	VMLINUX_BTF_OPTIONAL,
+	VMLINUX_BTF_REQUIRED,
+};
+
+static enum vmlinux_btf_need obj_needs_vmlinux_btf(const struct bpf_object *obj)
 {
 	struct bpf_program *prog;
 	struct bpf_map *map;
+	bool weak_ksyms = false;
 	int i;
 
 	/* CO-RE relocations need kernel BTF, only when btf_custom_path
 	 * is not specified
 	 */
 	if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
-		return true;
+		return VMLINUX_BTF_REQUIRED;
 
-	/* Support for typed ksyms needs kernel BTF */
+	/* Support for typed ksyms needs kernel BTF, except for weak ones */
 	for (i = 0; i < obj->nr_extern; i++) {
 		const struct extern_desc *ext;
 
 		ext = &obj->externs[i];
-		if (ext->type == EXT_KSYM && ext->ksym.type_id)
-			return true;
+		if (ext->type != EXT_KSYM || !ext->ksym.type_id)
+			continue;
+		if (!ext->is_weak)
+			return VMLINUX_BTF_REQUIRED;
+		weak_ksyms = true;
 	}
 
 	bpf_object__for_each_program(prog, obj) {
 		if (prog->load_strategy == BPF_PROG_LOAD_STRATEGY_DISABLED)
 			continue;
 		if (prog_needs_vmlinux_btf(prog))
-			return true;
+			return VMLINUX_BTF_REQUIRED;
 	}
 
 	bpf_object__for_each_map(map, obj) {
 		if (map_needs_vmlinux_btf(map))
-			return true;
+			return VMLINUX_BTF_REQUIRED;
 	}
 
-	return false;
+	return weak_ksyms ? VMLINUX_BTF_OPTIONAL : VMLINUX_BTF_NONE;
 }
 
 static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
 {
+	enum vmlinux_btf_need need = VMLINUX_BTF_REQUIRED;
 	int err;
 
 	/* btf_vmlinux could be loaded earlier */
 	if (obj->btf_vmlinux || obj->gen_loader)
 		return 0;
 
-	if (!force && !obj_needs_vmlinux_btf(obj))
+	if (!force)
+		need = obj_needs_vmlinux_btf(obj);
+	if (need == VMLINUX_BTF_NONE)
 		return 0;
 
 	obj->btf_vmlinux = btf__load_vmlinux_btf();
 	err = libbpf_get_error(obj->btf_vmlinux);
 	if (err) {
-		pr_warn("Error loading vmlinux BTF: %s\n", errstr(err));
 		obj->btf_vmlinux = NULL;
+		/* -ESRCH means libbpf found no usable kernel BTF, neither in
+		 * sysfs nor in a vmlinux image. Any other error comes from
+		 * /sys/kernel/btf/vmlinux, which exists but could not be read:
+		 * going on would silently drop kfuncs the kernel has.
+		 */
+		if (need == VMLINUX_BTF_OPTIONAL && err == -ESRCH) {
+			pr_info("no usable kernel BTF, weak typed ksyms are left unresolved\n");
+			return 0;
+		}
+		pr_warn("Error loading vmlinux BTF: %s\n", errstr(err));
 		return err;
 	}
 	return 0;
@@ -10078,7 +10103,10 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
 		if (err)
 			return -EINVAL;
 	}
-	if (need_vmlinux_btf) {
+	/* Without vmlinux BTF every typed ksym is weak, see
+	 * obj_needs_vmlinux_btf(), and is left unresolved.
+	 */
+	if (need_vmlinux_btf && obj->btf_vmlinux) {
 		err = bpf_object__resolve_ksyms_btf_id(obj);
 		if (err)
 			return -EINVAL;

-- 
2.56.1



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

* [PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF
  2026-12-05 21:28 [PATCH bpf-next 0/3] libbpf: Load objects with weak typed ksyms on kernels without BTF 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 via B4 Relay
  2026-12-05 21:28 ` [PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF Geyslan Gregório via B4 Relay
@ 2026-12-05 21:28 ` Geyslan Gregório via B4 Relay
  2026-12-05 21:40   ` sashiko-bot
  2 siblings, 1 reply; 7+ messages in thread
From: Geyslan Gregório via B4 Relay @ 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

From: Geyslan Gregório <gg@condado.dev>

Add a subtest that loads an object whose only users of vmlinux BTF are
weak typed ksyms, a variable and two kfuncs, when libbpf finds no
vmlinux BTF. The object is opened with btf_custom_path, which serves its
CO-RE relocation. It has to load, and a test run of its program has to
see the weak typed ksyms as absent and read the same TGID through CO-RE
as bpf_get_current_pid_tgid() returns.

The object also has a typeless ksym, so libbpf reads /proc/kallsyms,
which lists the typed variable. The variable has to stay absent.

A second object adds a strong typed ksym, which needs kernel BTF, so
its load has to fail with -ESRCH.

The test kernel has BTF, so the subtest runs in a child process that
hides /sys/kernel/btf in a mount namespace of its own, and passes a copy
of vmlinux BTF as btf_custom_path. libbpf also reads kernel BTF from a
vmlinux image in /boot or /lib/modules. Where it still finds one with
sysfs hidden, the subtest is skipped.

Signed-off-by: Geyslan Gregório <gg@condado.dev>
---
 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 ++++++++
 3 files changed, 213 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
index 1bc3089f10fc..d64bb2d7a322 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
@@ -1,6 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Google */
 
+#define _GNU_SOURCE
+#include <sched.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
 #include <test_progs.h>
 #include <bpf/libbpf.h>
 #include <bpf/btf.h>
@@ -8,6 +12,8 @@
 #include "test_ksyms_btf_null_check.skel.h"
 #include "test_ksyms_weak.skel.h"
 #include "test_ksyms_weak.lskel.h"
+#include "test_ksyms_weak_no_btf.skel.h"
+#include "test_ksyms_mixed_no_btf.skel.h"
 #include "test_ksyms_btf_write_check.skel.h"
 #include "test_ksyms_strong_typeless.lskel.h"
 
@@ -139,6 +145,135 @@ static void test_weak_syms_lskel(void)
 	test_ksyms_weak_lskel__destroy(skel);
 }
 
+enum {
+	NO_BTF_OK,
+	NO_BTF_SETUP,
+	NO_BTF_SKIP,
+	NO_BTF_OPEN,
+	NO_BTF_LOAD,
+	NO_BTF_RUN,
+	NO_BTF_TYPED_VAR,
+	NO_BTF_TYPELESS,
+	NO_BTF_KFUNC,
+	NO_BTF_CORE,
+	NO_BTF_MIXED,
+};
+
+/* Runs in a child, in a mount namespace of its own: with /sys/kernel/btf
+ * hidden, libbpf finds no kernel BTF, as on a kernel built without it.
+ */
+static int weak_syms_no_vmlinux_btf(const char *btf_file)
+{
+	LIBBPF_OPTS(bpf_object_open_opts, opts, .btf_custom_path = btf_file);
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	struct test_ksyms_mixed_no_btf *mixed;
+	struct test_ksyms_weak_no_btf *skel;
+	struct btf *btf;
+	int ret;
+
+	if (unshare(CLONE_NEWNS) ||
+	    mount("", "/", "", MS_REC | MS_PRIVATE, NULL) ||
+	    mount("none", "/sys/kernel/btf", "tmpfs", 0, NULL))
+		return NO_BTF_SETUP;
+
+	/* libbpf also takes kernel BTF from a vmlinux image, if it finds one */
+	btf = btf__load_vmlinux_btf();
+	if (!libbpf_get_error(btf)) {
+		btf__free(btf);
+		return NO_BTF_SKIP;
+	}
+
+	/* a strong typed ksym still needs kernel BTF */
+	mixed = test_ksyms_mixed_no_btf__open_opts(&opts);
+	if (!mixed)
+		return NO_BTF_OPEN;
+	ret = test_ksyms_mixed_no_btf__load(mixed);
+	test_ksyms_mixed_no_btf__destroy(mixed);
+	if (ret != -ESRCH)
+		return NO_BTF_MIXED;
+
+	skel = test_ksyms_weak_no_btf__open_opts(&opts);
+	if (!skel)
+		return NO_BTF_OPEN;
+
+	ret = NO_BTF_LOAD;
+	if (test_ksyms_weak_no_btf__load(skel))
+		goto cleanup;
+
+	/* Run the program in this task. The CO-RE read and the helper both
+	 * see the TGID of the initial PID namespace, whatever namespace the
+	 * test runs in.
+	 */
+	ret = NO_BTF_RUN;
+	if (bpf_prog_test_run_opts(bpf_program__fd(skel->progs.handler), &topts))
+		goto cleanup;
+
+	if (skel->data->out__typed_var != 0)
+		ret = NO_BTF_TYPED_VAR;
+	else if (skel->data->out__typeless == -1)
+		ret = NO_BTF_TYPELESS;
+	else if (skel->data->out__kfunc != 0)
+		ret = NO_BTF_KFUNC;
+	else if (skel->data->out__core_tgid != skel->data->out__tgid)
+		ret = NO_BTF_CORE;
+	else
+		ret = NO_BTF_OK;
+
+cleanup:
+	test_ksyms_weak_no_btf__destroy(skel);
+	return ret;
+}
+
+static void test_weak_syms_no_vmlinux_btf(void)
+{
+	char btf_file[] = "/tmp/ksyms_btf_no_vmlinux.btf.XXXXXX";
+	int fd = -1, status = 0;
+	const void *raw_data;
+	struct btf *btf;
+	__u32 raw_sz;
+	pid_t pid;
+
+	btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(btf, "vmlinux_btf"))
+		return;
+
+	fd = mkstemp(btf_file);
+	if (!ASSERT_OK_FD(fd, "btf_tmp"))
+		goto cleanup;
+
+	raw_data = btf__raw_data(btf, &raw_sz);
+	if (!ASSERT_OK_PTR(raw_data, "raw_data"))
+		goto cleanup;
+	if (!ASSERT_EQ(write(fd, raw_data, raw_sz), raw_sz, "btf_write"))
+		goto cleanup;
+
+	pid = fork();
+	if (!ASSERT_GE(pid, 0, "fork"))
+		goto cleanup;
+	if (pid == 0)
+		_exit(weak_syms_no_vmlinux_btf(btf_file));
+
+	if (!ASSERT_EQ(waitpid(pid, &status, 0), pid, "waitpid"))
+		goto cleanup;
+	if (!ASSERT_TRUE(WIFEXITED(status), "child_exited"))
+		goto cleanup;
+
+	if (WEXITSTATUS(status) == NO_BTF_SKIP) {
+		printf("%s:SKIP:kernel BTF is found outside of /sys/kernel/btf\n",
+		       __func__);
+		test__skip();
+		goto cleanup;
+	}
+	ASSERT_EQ(WEXITSTATUS(status), NO_BTF_OK, "weak ksyms without vmlinux BTF");
+
+cleanup:
+	if (fd >= 0) {
+		close(fd);
+		remove(btf_file);
+	}
+	btf__free(btf);
+}
+
 static void test_strong_typeless_lskel(void)
 {
 	struct test_ksyms_strong_typeless_lskel *skel;
@@ -199,6 +334,9 @@ void test_ksyms_btf(void)
 	if (test__start_subtest("weak_ksyms_lskel"))
 		test_weak_syms_lskel();
 
+	if (test__start_subtest("weak_ksyms_no_vmlinux_btf"))
+		test_weak_syms_no_vmlinux_btf();
+
 	if (test__start_subtest("strong_typeless_lskel"))
 		test_strong_typeless_lskel();
 
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c b/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c
new file mode 100644
index 000000000000..e9b597a0576e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A weak and a strong typed ksym. The strong one needs kernel BTF, so the
+ * object must not load on a kernel that libbpf finds no BTF for.
+ */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+
+int out__typed_vars = -1;
+
+extern const struct rq runqueues __ksym __weak;
+extern const int bpf_prog_active __ksym;
+
+SEC("raw_tp")
+int handler(const void *ctx)
+{
+	out__typed_vars = bpf_ksym_exists(&runqueues) &&
+			  bpf_per_cpu_ptr(&bpf_prog_active, 0);
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c b/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c
new file mode 100644
index 000000000000..709a92854a29
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test weak typed ksyms on a kernel that libbpf finds no BTF for.
+ *
+ * The CO-RE relocation is resolved against btf_custom_path, so the weak
+ * typed ksyms are the only users of kernel BTF. The typeless ksym makes
+ * libbpf read /proc/kallsyms, which lists runqueues too.
+ */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+
+int out__typed_var = -1;
+__u64 out__typeless = -1;
+int out__kfunc = -1;
+int out__core_tgid = -1;
+int out__tgid = -1;
+
+extern const struct rq runqueues __ksym __weak; /* typed */
+extern const void bpf_prog_active __ksym __weak; /* typeless */
+void bpf_rcu_read_lock(void) __ksym __weak;
+void bpf_rcu_read_unlock(void) __ksym __weak;
+
+SEC("raw_tp")
+int handler(const void *ctx)
+{
+	struct task_struct *task = (struct task_struct *)bpf_get_current_task();
+
+	out__typed_var = bpf_ksym_exists(&runqueues) ? 1 : 0;
+	out__typeless = (__u64)&bpf_prog_active;
+
+	if (bpf_ksym_exists(bpf_rcu_read_lock) &&
+	    bpf_ksym_exists(bpf_rcu_read_unlock)) {
+		/* dead code when the kfuncs are left unresolved */
+		bpf_rcu_read_lock();
+		bpf_rcu_read_unlock();
+		out__kfunc = 1;
+	} else {
+		out__kfunc = 0;
+	}
+
+	out__core_tgid = BPF_CORE_READ(task, tgid);
+	out__tgid = bpf_get_current_pid_tgid() >> 32;
+
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";

-- 
2.56.1



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

* Re: [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms
  2026-12-05 21:28 ` [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms Geyslan Gregório via B4 Relay
@ 2026-12-05 21:33   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-05 21:33 UTC (permalink / raw)
  To: Geyslan Gregório, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] libbpf: multi-line comment style violation in kallsyms_cb()
--

commit f31b5ccf34e4344898624b3e8f6fa70dfe8ca091
Author: Geyslan Gregório <gg@condado.dev>

libbpf: Do not resolve typed ksyms through kallsyms

This commit changes kallsyms_cb() to skip typed ksyms instead of only
skipping non-variable ksyms, so weak typed variables missing from kernel
BTF are not marked as resolved via /proc/kallsyms.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 2b188e64e3566..6fe0143ea9590 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c

[ ... ]

> @@ -9781,8 +9780,10 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
>  	if (!ext || ext->type != EXT_KSYM)
>  		return 0;
>  
> -	t = btf__type_by_id(obj->btf, ext->btf_id);
> -	if (!btf_is_var(t))
> +	/* Typed ksyms, variables and functions, are resolved through kernel
> +	 * BTF only.
> +	 */

[Severity: Low]
BPF code uses the kernel multi-line comment style where the opening /* sits
on its own line and the text starts on the next line.  Should this comment
move the opening /* to its own line (or be shortened to a single line)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-libbpf-weak-typed-ksyms-no-vmlinux-btf-v1-0-77cc642f7d4b@condado.dev?part=1

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

* Re: [PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF
  2026-12-05 21:28 ` [PATCH bpf-next 2/3] libbpf: Load objects with weak typed ksyms on kernels without BTF Geyslan Gregório via B4 Relay
@ 2026-12-05 21:36   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-05 21:36 UTC (permalink / raw)
  To: Geyslan Gregório, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] libbpf: newly added multi-line comments violate BPF comment
  style
--

commit 0a6249088ebcbf60499ca895e6e10512c2579665
Author: Geyslan Gregório <gg@condado.dev>

libbpf: Load objects with weak typed ksyms on kernels without BTF

This commit allows libbpf to load BPF objects whose only use of vmlinux
BTF is weak typed ksyms when no usable kernel BTF is available, leaving
those ksyms unresolved instead of failing with -ESRCH.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 6fe0143ea9590..5e2bb95bda953 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -3736,58 +3736,83 @@ static bool map_needs_vmlinux_btf(struct bpf_map *map)
>  	return bpf_map__is_struct_ops(map);
>  }
>  
> -static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
> +enum vmlinux_btf_need {
> +	VMLINUX_BTF_NONE,
> +	/* Only weak typed ksyms use it. Without kernel BTF they are left
> +	 * unresolved, as on a kernel with BTF that lacks them.
> +	 */
> +	VMLINUX_BTF_OPTIONAL,
> +	VMLINUX_BTF_REQUIRED,
> +};

[Severity: Low]
BPF code uses the kernel multi-line comment style with the opening /* on
its own line, even when surrounding code in the file uses the older
style.  The newly added multi-line comments here, in
bpf_object__load_vmlinux_btf(), and in bpf_object__resolve_externs()
should start the text on the line after /*.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-libbpf-weak-typed-ksyms-no-vmlinux-btf-v1-0-77cc642f7d4b@condado.dev?part=2

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

* Re: [PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF
  2026-12-05 21:28 ` [PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF Geyslan Gregório via B4 Relay
@ 2026-12-05 21:40   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-05 21:40 UTC (permalink / raw)
  To: Geyslan Gregório, bpf

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] selftests/bpf: multi-line comment style in
  weak_syms_no_vmlinux_btf()
- [Low] selftests/bpf: weak_syms_no_vmlinux_btf() fails on missing
  /sys/kernel/btf
- [Low] selftests/bpf: ineffective out__typeless check in
  weak_syms_no_vmlinux_btf()
--

commit 5bb212acbecdd5b5802324ccf479855425cb6236
Author: Geyslan Gregório <gg@condado.dev>

selftests/bpf: Test weak typed ksyms without vmlinux BTF

This commit adds a BPF selftest subtest that hides /sys/kernel/btf in a child
mount namespace to verify that objects with only weak typed ksyms load and
run using btf_custom_path, while an object with a strong typed ksym fails to
load with -ESRCH.

> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> index 1bc3089f10fca..d64bb2d7a3223 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c

[ ... ]

> @@ -139,6 +145,135 @@ static void test_weak_syms_lskel(void)

[ ... ]

> +/* Runs in a child, in a mount namespace of its own: with /sys/kernel/btf
> + * hidden, libbpf finds no kernel BTF, as on a kernel built without it.
> + */
> +static int weak_syms_no_vmlinux_btf(const char *btf_file)

[Severity: Low]
BPF code uses the multi-line comment style with the opening /* on its own
line and the text starting on the next line.  The comment above
bpf_prog_test_run_opts() in this same function uses the old style as well.

[ ... ]

> +	if (unshare(CLONE_NEWNS) ||
> +	    mount("", "/", "", MS_REC | MS_PRIVATE, NULL) ||
> +	    mount("none", "/sys/kernel/btf", "tmpfs", 0, NULL))
> +		return NO_BTF_SETUP;
> +
> +	/* libbpf also takes kernel BTF from a vmlinux image, if it finds one */
> +	btf = btf__load_vmlinux_btf();
> +	if (!libbpf_get_error(btf)) {
> +		btf__free(btf);
> +		return NO_BTF_SKIP;
> +	}

[Severity: Low]
Can mount() fail with ENOENT here when /sys/kernel/btf is absent?

When sysfs is not mounted, btf__load_vmlinux_btf() in the parent still
succeeds if a fallback vmlinux image with BTF exists in /boot or
/lib/modules.  The child then returns NO_BTF_SETUP and fails the subtest
instead of reaching the check below and skipping with NO_BTF_SKIP.

Ignoring ENOENT on the /sys/kernel/btf mount would treat it as already hidden
and let the subtest reach the skip check.

[ ... ]

> +	if (skel->data->out__typed_var != 0)
> +		ret = NO_BTF_TYPED_VAR;
> +	else if (skel->data->out__typeless == -1)
> +		ret = NO_BTF_TYPELESS;

[Severity: Low]
Can the out__typeless == -1 branch ever trigger?

If handler() never ran, out__typed_var is still -1 and triggers first.  Once
handler() runs, it overwrites out__typeless with either the resolved address
or 0 because bpf_prog_active is __weak in test_ksyms_weak_no_btf.c, so this
passes even when /proc/kallsyms resolution fails.

Comparing out__typeless against kallsyms_find("bpf_prog_active", &addr) as
test_basic() does, or dropping __weak from bpf_prog_active, would check
kallsyms resolution.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-libbpf-weak-typed-ksyms-no-vmlinux-btf-v1-0-77cc642f7d4b@condado.dev?part=3

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

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

Thread overview: 7+ 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 via B4 Relay
2026-12-05 21:28 ` [PATCH bpf-next 1/3] libbpf: Do not resolve typed ksyms through kallsyms 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 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 via B4 Relay
2026-12-05 21:40   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox