* [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