Re: [PATCH bpf-next v4 09/10] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY_BY_NAME ioctl

From: bot+bpf-ci

Date: Tue Dec 01 2026 - 19:59:23 EST


> libbpf: Resolve typeless ksyms via KALLSYMS_QUERY_BY_NAME ioctl

TL;DR: Dropping the old 'd'-only restriction on ".llvm." matches may make some
typeless ksym externs fail with -ENOTUNIQ or bind new addresses on ThinLTO
kernels. Standalone libbpf builds may also lack the linux/fs.h definitions.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -31,6 +31,7 @@
> #include <linux/bpf.h>
> #include <linux/btf.h>
> #include <linux/filter.h>
> +#include <linux/fs.h>
> #include <linux/limits.h>

This isn't a bug, but libbpf.c now depends on KALLSYMS_QUERY_BY_NAME,
KALLSYMS_QUERY_NAME_PREFIX, struct kallsyms_query_by_name and struct
kallsyms_query_sym coming from <linux/fs.h>.

The in-tree build is fine since tools/include/uapi is used. The
stand-alone build from the mirror would pick up the system <linux/fs.h>,
and with a header older than this series libbpf.c would fail to compile.

libbpf.c usually guards constants from headers it does not carry with
fallbacks, for example #ifndef BPF_FS_MAGIC and #ifndef MFD_CLOEXEC.
Should local definitions be added here, or should the commit message
state that the mirror must start shipping linux/fs.h (and the
linux/mount.h and linux/fscrypt.h it includes)?

[ ... ]

> +static int query_ksym_by_name(int fd, struct extern_desc *ext, bool lto_prefix)
> +{
[ ... ]
> + if (lto_prefix) {
> + n = snprintf(prefix_buf, sizeof(prefix_buf), "%s.llvm.", ext->name);
> + if (n < 0 || n >= sizeof(prefix_buf))
> + return 0;
> + q.query_name = ptr_to_u64(prefix_buf);
> + }
> +
> + while (!ioctl(fd, KALLSYMS_QUERY_BY_NAME, &q)) {
> + if (!ext->is_set) {
> + ext->is_set = true;
> + ext->ksym.addr = sym.sym_addr;
> + pr_debug("extern (ksym) '%s': set to 0x%llx\n", ext->name, ext->ksym.addr);
> + } else if (sym.sym_addr != ext->ksym.addr) {
> + pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n",
> + ext->name, ext->ksym.addr, (unsigned long long)sym.sym_addr);
> + return -ENOTUNIQ;
> + }
> + q.pos++;
> + }

Was dropping the 'd' restriction on the "<name>.llvm." match intended?

The text parser this replaces only did that for 'd' (static data)
symbols:

res = strstr(sym_name, ".llvm.");
if (sym_type == 'd' && res)
ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name);
else
ext = find_extern_by_name(obj, sym_name);

That restriction came from c56e59776f46 ("libbpf: Handle
<orig_name>.llvm.<hash> symbol properly"). KALLSYMS_QUERY_BY_NAME does
not filter by type and struct kallsyms_query_sym does not return the
type, so the old rule cannot be kept. The commit message ("exact names,
plus "<name>.llvm." prefix matches") does not mention this.

On Clang ThinLTO kernels, promoted static functions, bss and rodata
appear as 't'/'b'/'r' foo.llvm.<hash>, which gives two visible changes.

First, suppose there is an exact global symbol foo (type 'T') and an
unrelated static foo.llvm.<hash> of type 't', 'b' or 'r' at another
address. The old code resolved extern foo uniquely. Now the exact
lookup sets ext->ksym.addr, the prefix lookup finds a different address
and returns -ENOTUNIQ, and bpf_object__resolve_typeless_ksyms() skips
the text fallback for -ENOTUNIQ. Can this make
bpf_object__resolve_externs() return -EINVAL for an object that loaded
before?

Second, a weak typeless extern that used to stay 0, because only a
non-'d' foo.llvm.<hash> existed, now binds to that symbol's address.
Does this change `if (&foo)` style feature checks?

Also, the ambiguity message prints ext->name rather than the symbol that
matched, so the user sees "'foo': resolution is ambiguous" without
knowing which foo.llvm.<hash> caused it. The old message printed
sym_name. Should the matched name be reported?

If the 'd' restriction was dropped intentionally, should the changelog
say so and explain why ambiguity against non-data .llvm. symbols should
fail the load?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46779970959