Skip to contentSkip to conversation

kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms

PATCH In review Samuel Wu 7 participants 42 messages Oct 23 – Nov 6

Subsystems (7)
Documentation VFS Kselftest BPF +3 more subsystems
lore
Share & tools
lore

Samuel Wu

author
#0 lore raw

v2 cover kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms

/proc/kallsyms is the primary interface for userspace to query the kernel symbol table. However, resolving even a handful of symbols or addresses requires reading and parsing the entire text file, which is often several megabytes and takes thousands of read() syscalls.

This patchseries adds an extensible KALLSYMS_QUERY ioctl() to /proc/kallsyms with two query modes:
1. KALLSYMS_QUERY_BY_SYMBOL: Resolves a NUL-terminated symbol name to its address. Searches vmlinux first with binary search, then falls back to loaded modules. Matches names exactly (without stripping .llvm.<hash> suffixes) and rejects ambiguous symbols with different addresses.
2. KALLSYMS_QUERY_BY_ADDR: Resolves a kernel address to the symbol's name, covering vmlinux, loaded modules, BPF JIT programs, and ftrace trampolines.

Access control matches reading /proc/kallsyms directly: the ioctl checks the kallsyms_show_value() result cached at open() time and returns -EPERM whenever symbol addresses would be masked to zero.

There were significant performance improvements with this patchseries:

1. libbpf typeless ksym resolution (8-CPU x86-64 Cuttlefish VM, BPF object with 11 typeless ksyms):

   Metric                      Text parse  KALLSYMS_QUERY   Change
   --------------------------  ----------  ---------------  ------------
   Resolve 11 typeless ksyms   157 ms      0.7 ms           ~220x faster

2. Perfetto symbolization & callstack sampling (8-CPU x86-64 VM, 175k-line /proc/kallsyms):

   Metric                      Text parse  KALLSYMS_QUERY   Change
   --------------------------  ----------  ---------------  ------------
   Init                        155.7 ms    0.44 ms          352x faster
   50k lookups                 25.2 ms     2.2 ms           11.7x faster
   traced_probes VmRSS         15.1 MB     4.8 MB           -68%
   traced_probes VmHWM         23.9 MB     4.8 MB           -80%

Changes in v2:
- Replaced the BPF syscall approach in libbpf with the more generalizable KALLSYMS_QUERY ioctl() on /proc/kallsyms, per Andrii
- Added KALLSYMS_QUERY_BY_ADDR for address-to-symbol lookup
- Added proc and bpf selftests and procfs documentation
- v1 link: https://lore.kernel.org/all/[email protected]/

Samuel Wu (6):
kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms
tools: Copy KALLSYMS_QUERY definitions to linux/fs.h docs/procfs: Document KALLSYMS_QUERY ioctl on /proc/kallsyms
libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl selftests/bpf: Test typeless module ksym resolution selftests/proc: Add KALLSYMS_QUERY tests

 Documentation/filesystems/proc.rst            |  15 ++
 include/uapi/linux/fs.h                       |  41 ++++
 kernel/kallsyms.c                             | 132 +++++++++++
 tools/include/uapi/linux/fs.h                 |  41 ++++
 tools/lib/bpf/libbpf.c                        |  78 ++++++-
 .../testing/selftests/bpf/prog_tests/ksyms.c  |   7 +
 .../testing/selftests/bpf/progs/test_ksyms.c  |   3 +
 tools/testing/selftests/proc/.gitignore       |   1 +
 tools/testing/selftests/proc/Makefile         |   1 +
 .../selftests/proc/proc-kallsyms-query.c      | 213 ++++++++++++++++++
 10 files changed, 530 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/proc/proc-kallsyms-query.c
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author
#1 lore raw

PATCH v2 1/6 kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +41 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
 	__u64 build_id_addr;		/* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY	_IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+	/*
+	 * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+	 * and ftrace symbols are not searched) by exact NUL-terminated name
+	 * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+	 * must be 0 on input.
+	 */
+	KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+	/*
+	 * Look up the symbol covering sym_addr and write its NUL-terminated
+	 * name to the sym_name_addr buffer of sym_name_size bytes. The same
+	 * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+	 * images, and freed module init functions still tracked by ftrace.
+	 * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+	 * always enough. On success, sym_name_size is set to the number of
+	 * bytes written (including the terminating NUL).
+	 */
+	KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+	__u64 size;		/* in - struct size for compatibility */
+	__u64 query_flags;	/* in - query mode (enum kallsyms_query_flags) */
+	__u64 sym_name_addr;	/* in - address of symbol name buffer */
+	__u64 sym_addr;		/* out (BY_SYMBOL) / in (BY_ADDR) */
+	__u64 sym_name_size;	/* in/out - name buffer size / bytes written */
+};
+
+#define KALLSYMS_QUERY_SIZE_VER0	40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +132 -0

149 lines not on this page · raw message

Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#2 lore raw
TL;DR: KALLSYMS_QUERY_BY_ADDR may return BPF JIT symbol names that bpf_jit_kallsyms=0 or bpf_jit_harden hide in /proc/kallsyms, possibly leaking JIT image locations to openers with show_value.
Quoted 1 line

kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms

TL;DR: KALLSYMS_QUERY_BY_ADDR may return BPF JIT symbol names that bpf_jit_kallsyms=0 or bpf_jit_harden hide in /proc/kallsyms, possibly leaking JIT image locations to openers with show_value.

Quoted 4 lines
kernel/kallsyms.c +0 -0
index aec2f06858af..7caaf3e421c7 100644

[ ... ]

Quoted 1 line
kernel/kallsyms.c +0 -0
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)

[ ... ]

Quoted 2 lines
kernel/kallsyms.c +2 -0
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{

[ ... ]

Quoted 7 lines
kernel/kallsyms.c +7 -0
+
+	/*
+	 * The text interface shows zero addresses to openers without permission.
+	 * A zeroed point query result is useless (and an address-to-symbol query
+	 * would leak KASLR), so fail instead.
+	 */
+	if (!iter->show_value)

[ ... ]

kernel/kallsyms.c const char *kdb_walk_kallsyms(loff_t *pos) · line 1019

Quoted 9 lines
kernel/kallsyms.c +9 -0
+	case KALLSYMS_QUERY_BY_ADDR:
+		if (!karg.sym_name_size)
+			return -EINVAL;
+		addr = karg.sym_addr;
+		/* Reject addresses that do not fit in unsigned long (32-bit). */
+		if (addr != karg.sym_addr)
+			return -ENOENT;
+		if (!kallsyms_lookup(addr, NULL, NULL, NULL, namebuf))
+			return -ENOENT;

Can KALLSYMS_QUERY_BY_ADDR hand out BPF JIT symbol names that /proc/kallsyms is configured to hide?

kallsyms_lookup() reaches kallsyms_lookup_buildid() and falls through to bpf_address_lookup() -> bpf_ksym_find(), which searches bpf_tree without any policy check. bpf_ksym_add() always inserts into bpf_tree (bpf_prog_kallsyms_add() only requires bpf_prog_kallsyms_candidate() and CAP_BPF).

The text listing of the same file goes through get_ksymbol_bpf() -> bpf_get_kallsym(), which starts with:

    if (!bpf_jit_kallsyms_enabled())
        return ret;

bpf_jit_kallsyms_enabled() returns false when net.core.bpf_jit_harden is non-zero or net.core.bpf_jit_kallsyms is 0. bpf_get_kallsym() is the only caller of bpf_jit_kallsyms_enabled() in the tree, so hiding the names in /proc/kallsyms is now the only thing these sysctls do. Commit af91acbc6299 ("bpf: Fix bpf jit kallsym access") says so directly:

It should only skip addr->name resolution for 'cat /proc/kallsyms'. That's what bpf_jit_kallsyms and bpf_jit_harden protect.

With this patch, a process whose open of /proc/kallsyms has show_value set can call ioctl(KALLSYMS_QUERY) with query_flags=KALLSYMS_QUERY_BY_ADDR and get back names like bpf_prog_<tag>_<name> (plus trampolines, dispatchers and __cfi_ prefixes) for addresses inside JIT images. That gives it an address->name oracle for finding JIT image locations, which these sysctls are meant to withhold from /proc/kallsyms.

The comment above the show_value check argues for matching the text interface ("The text interface shows zero addresses to openers without permission ... an address-to-symbol query would leak KASLR, so fail instead"), but the code applies only the show_value gate and skips the BPF-specific gate. The uapi comment and the commit message both say BY_ADDR covers "BPF JIT images", but neither mentions bpf_jit_kallsyms or bpf_jit_harden.

This is reachable whenever CONFIG_KALLSYMS and CONFIG_BPF_JIT are enabled, a JITed program loaded with CAP_BPF exists, and either sysctl hides BPF symbols. No later commit in the series changes kernel/ code to address this.

Should the BY_ADDR path return -ENOENT when kallsyms_lookup() resolves to a BPF address (for example, is_bpf_text_address(addr) is true and the address is not in vmlinux or a module) unless bpf_jit_kallsyms_enabled() is true?

---
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/39878338337

author
#3 lore raw
Quoted 7 lines · On Thu, Oct 22, 2026 at 5:51 PM <[email protected]> wrote:

[ ... ]

Should the BY_ADDR path return -ENOENT when kallsyms_lookup() resolves to a BPF address (for example, is_bpf_text_address(addr) is true and the address is not in vmlinux or a module) unless bpf_jit_kallsyms_enabled() is true?

Thanks for the catch. In v3, I'll update BY_ADDR to gate on bpf_jit_kallsyms_enabled() so hidden BPF JIT symbols return -ENOENT.

reviewer
#4 lore raw
In reply to Samuel Wu

On 10/22/26 5:16 PM, Samuel Wu wrote:

...

Quoted 10 lines
---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +0 -0
index 34c6f219462a..b43f227dad0e 100644

include/uapi/linux/fs.h struct procmap_query { · line 678

Quoted 5 lines
include/uapi/linux/fs.h +2 -0
@@ -674,6 +674,47 @@ struct procmap_query {
 	__u64 build_id_addr;		/* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY	_IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)

...

Documentation/userspace/ioctl/ioctl-number.rst doesn't seem to know about PROCFS_IOCTL_MAGIC and what range of @nr values that it uses.

Please update it.

Signature · 1 line
~Randy
author
#5 lore raw
Quoted 24 lines · Randy Dunlap

On 10/22/26 5:16 PM, Samuel Wu wrote:

...

Quoted 15 lines
---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +2 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
      __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY       _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)

...

Quoted 5 lines

Documentation/userspace/ioctl/ioctl-number.rst doesn't seem to know about PROCFS_IOCTL_MAGIC and what range of @nr values that it uses.

Please update it.

Thanks for the catch Randy! In v3, I'll add a 'f' 10-12 entry for PROCFS_IOCTL_MAGIC (PAGEMAP_SCAN, PROCMAP_QUERY, KALSYMS_QUERY) to Documentation/userspace/ioctl/ioctl-number.rst.

#6 lore raw
In reply to Samuel Wu
Quoted 91 lines · Samuel Wu

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +28 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
        __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+       /*
+        * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+        * and ftrace symbols are not searched) by exact NUL-terminated name
+        * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+        * must be 0 on input.
+        */
+       KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+       /*
+        * Look up the symbol covering sym_addr and write its NUL-terminated
+        * name to the sym_name_addr buffer of sym_name_size bytes. The same
+        * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+        * images, and freed module init functions still tracked by ftrace.
+        * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+        * always enough. On success, sym_name_size is set to the number of
+        * bytes written (including the terminating NUL).
+        */
+       KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and

include/uapi/linux/fs.h struct procmap_query { · line 714

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+       __u64 size;             /* in - struct size for compatibility */
+       __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+       __u64 sym_name_addr;    /* in - address of symbol name buffer */
+       __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+       __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
+};

it might be smart to have QUERY_BY_ADDR and QUERY_BY_NAME to be two separate ioctl commands with its own query in/out struct. This would remove addr vs name ambiguity in sym_name_addr field, and will allow us to evolve APIs independently, if necessary. They should still probably reuse the same type to represent symbol information, and have kallsyms_query struct provide a pointer to that struct (and its maximum size), which the kernel will fill out and set the size it actually knows about.

struct kallsyms_query_by_addr {
    __u64 size;
    __u64 query_flags;
    __u64 sym_addr; /* in */
    struct kallsyms_sym *sym; /* out */
    __u64 sym_len;
};
struct kallsyms_sym {
    __u64 sym_addr;
    __u64 sym_name;
    __u64 sym_name_len;
    __u64 sym_module;
    __u64 sym_module_len; /* if zero, no module requested/available */
};

e.g. for symbol you'd want to return its name, address, but also module name. and we should leave a way to extend it further with more optional information (that's the kallsyms extensibility bit I was talking about).

aslo, we should think about adding ways to paginate (or at least leave a possibility of that in the future) if there are multiple matches (e.g., for static functions with the same name resolving to multiple addresses); not sure how it should look like API wise, but it would be good to still return "any" or "first" match, but let user know there are multiple, so user can decide how to proceed...

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

for QUERY_BY_ADDR we should have a flag saying whether an exact match by address is requested or we should find a symbol that covers the address (that's most useful for symbolization). maybe even "at or after" address mode, so you can paginate (though probably explicitly designed batch API would be even more useful later)

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 57 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +36 -0
index aec2f06858af..7caaf3e421c7 100644
@@ -31,6 +31,7 @@
 #include <linux/kernel.h>
 #include <linux/bsearch.h>
 #include <linux/btf_ids.h>
+#include <linux/uaccess.h>
 
 #include "kallsyms_internal.h"
 
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)
 }
 #endif /* CONFIG_KGDB_KDB */
 
+struct kallsyms_mod_find_ctx {
+       const char *name;
+       unsigned long addr;
+       bool found;
+};
+
+static int kallsyms_mod_find_cb(void *data, const char *name, unsigned long addr)
+{
+       struct kallsyms_mod_find_ctx *ctx = data;
+
+       if (strcmp(name, ctx->name) != 0)
+               return 0;
+       if (ctx->found && ctx->addr != addr)
+               return -EADDRNOTAVAIL;
+       ctx->found = true;
+       ctx->addr = addr;
+       return 0;
+}
+
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{
+       char namebuf[KSYM_NAME_LEN];
+       struct kallsyms_query karg;
+       unsigned long addr;
+       unsigned int start;
+       unsigned int end;
+       unsigned int i;
+       __u64 usize;
+       long len;
+       int err;
+
+       BUILD_BUG_ON(sizeof(struct kallsyms_query) != KALLSYMS_QUERY_SIZE_VER0);
+
+       /*
+        * The text interface shows zero addresses to openers without permission.

kernel/kallsyms.c const char *kdb_walk_kallsyms(loff_t *pos) · line 970

Quoted 10 lines
kernel/kallsyms.c +10 -0
+        * A zeroed point query result is useless (and an address-to-symbol query
+        * would leak KASLR), so fail instead.
+        */
+       if (!iter->show_value)
+               return -EPERM;
+       if (copy_from_user(&usize, uarg, sizeof(usize)))
+               return -EFAULT;
+       if (usize > PAGE_SIZE)
+               return -E2BIG;
+       if (usize < KALLSYMS_QUERY_SIZE_VER0)

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

Quoted 6 lines
kernel/kallsyms.c +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

author
#7 lore raw
Quoted 129 lines · Andrii Nakryiko
Quoted 91 lines · Samuel Wu

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +28 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
        __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+       /*
+        * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+        * and ftrace symbols are not searched) by exact NUL-terminated name
+        * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+        * must be 0 on input.
+        */
+       KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+       /*
+        * Look up the symbol covering sym_addr and write its NUL-terminated
+        * name to the sym_name_addr buffer of sym_name_size bytes. The same
+        * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+        * images, and freed module init functions still tracked by ftrace.
+        * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+        * always enough. On success, sym_name_size is set to the number of
+        * bytes written (including the terminating NUL).
+        */
+       KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and

include/uapi/linux/fs.h struct procmap_query { · line 714

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+       __u64 size;             /* in - struct size for compatibility */
+       __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+       __u64 sym_name_addr;    /* in - address of symbol name buffer */
+       __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+       __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
+};

it might be smart to have QUERY_BY_ADDR and QUERY_BY_NAME to be two separate ioctl commands with its own query in/out struct. This would remove addr vs name ambiguity in sym_name_addr field, and will allow us to evolve APIs independently, if necessary. They should still probably reuse the same type to represent symbol information, and have kallsyms_query struct provide a pointer to that struct (and its maximum size), which the kernel will fill out and set the size it actually knows about.

struct kallsyms_query_by_addr {
    __u64 size;
    __u64 query_flags;
    __u64 sym_addr; /* in */
    struct kallsyms_sym *sym; /* out */
    __u64 sym_len;
};
struct kallsyms_sym {
    __u64 sym_addr;
    __u64 sym_name;
    __u64 sym_name_len;
    __u64 sym_module;
    __u64 sym_module_len; /* if zero, no module requested/available */
};
Quoted 4 lines

e.g. for symbol you'd want to return its name, address, but also module name. and we should leave a way to extend it further with more optional information (that's the kallsyms extensibility bit I was talking about).

Agree, two ioctls with a shared symbol type is cleaner. v3 plan is to have QUERY_BY_NAME and QUERY_BY_ADDR both fill a shared struct kallsyms_sym (address, size, name, module), passed by pointer so it can grow later.

Quoted 6 lines

aslo, we should think about adding ways to paginate (or at least leave a possibility of that in the future) if there are multiple matches (e.g., for static functions with the same name resolving to multiple addresses); not sure how it should look like API wise, but it would be good to still return "any" or "first" match, but let user know there are multiple, so user can decide how to proceed...

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

And an open question I'm unsure about: Should libbpf keep the light skeleton semantics (take the first match, no ambiguity check), or set ALL_MATCHES to keep today's check that fails on names with multiple addresses?

Quoted 5 lines

for QUERY_BY_ADDR we should have a flag saying whether an exact match by address is requested or we should find a symbol that covers the address (that's most useful for symbolization). maybe even "at or after" address mode, so you can paginate (though probably explicitly designed batch API would be even more useful later)

For v3 I think we can keep BY_ADDR to the covering lookup only. The result can fill in the covering symbol's (start address, size, name, module), and callers can check for an exact match themselves by comparing the start address with the queried address. A batch API can be added later when there is a user for it.

Quoted 64 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 62 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +41 -0
index aec2f06858af..7caaf3e421c7 100644
@@ -31,6 +31,7 @@
 #include <linux/kernel.h>
 #include <linux/bsearch.h>
 #include <linux/btf_ids.h>
+#include <linux/uaccess.h>
 
 #include "kallsyms_internal.h"
 
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)
 }
 #endif /* CONFIG_KGDB_KDB */
 
+struct kallsyms_mod_find_ctx {
+       const char *name;
+       unsigned long addr;
+       bool found;
+};
+
+static int kallsyms_mod_find_cb(void *data, const char *name, unsigned long addr)
+{
+       struct kallsyms_mod_find_ctx *ctx = data;
+
+       if (strcmp(name, ctx->name) != 0)
+               return 0;
+       if (ctx->found && ctx->addr != addr)
+               return -EADDRNOTAVAIL;
+       ctx->found = true;
+       ctx->addr = addr;
+       return 0;
+}
+
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{
+       char namebuf[KSYM_NAME_LEN];
+       struct kallsyms_query karg;
+       unsigned long addr;
+       unsigned int start;
+       unsigned int end;
+       unsigned int i;
+       __u64 usize;
+       long len;
+       int err;
+
+       BUILD_BUG_ON(sizeof(struct kallsyms_query) != KALLSYMS_QUERY_SIZE_VER0);
+
+       /*
+        * The text interface shows zero addresses to openers without permission.
+        * A zeroed point query result is useless (and an address-to-symbol query
+        * would leak KASLR), so fail instead.
+        */
+       if (!iter->show_value)
+               return -EPERM;
Quoted 8 lines

kernel/kallsyms.c const char *kdb_walk_kallsyms(loff_t *pos) · line 970

Quoted 5 lines
kernel/kallsyms.c +5 -0
+       if (copy_from_user(&usize, uarg, sizeof(usize)))
+               return -EFAULT;
+       if (usize > PAGE_SIZE)
+               return -E2BIG;
+       if (usize < KALLSYMS_QUERY_SIZE_VER0)

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
kernel/kallsyms.c +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

#8 lore raw
Quoted 153 lines · Samuel Wu
Quoted 129 lines · Andrii Nakryiko
Quoted 91 lines · Samuel Wu

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +28 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
        __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+       /*
+        * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+        * and ftrace symbols are not searched) by exact NUL-terminated name
+        * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+        * must be 0 on input.
+        */
+       KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+       /*
+        * Look up the symbol covering sym_addr and write its NUL-terminated
+        * name to the sym_name_addr buffer of sym_name_size bytes. The same
+        * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+        * images, and freed module init functions still tracked by ftrace.
+        * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+        * always enough. On success, sym_name_size is set to the number of
+        * bytes written (including the terminating NUL).
+        */
+       KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and

include/uapi/linux/fs.h struct procmap_query { · line 714

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+       __u64 size;             /* in - struct size for compatibility */
+       __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+       __u64 sym_name_addr;    /* in - address of symbol name buffer */
+       __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+       __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
+};

it might be smart to have QUERY_BY_ADDR and QUERY_BY_NAME to be two separate ioctl commands with its own query in/out struct. This would remove addr vs name ambiguity in sym_name_addr field, and will allow us to evolve APIs independently, if necessary. They should still probably reuse the same type to represent symbol information, and have kallsyms_query struct provide a pointer to that struct (and its maximum size), which the kernel will fill out and set the size it actually knows about.

struct kallsyms_query_by_addr {
    __u64 size;
    __u64 query_flags;
    __u64 sym_addr; /* in */
    struct kallsyms_sym *sym; /* out */
    __u64 sym_len;
};
struct kallsyms_sym {
    __u64 sym_addr;
    __u64 sym_name;
    __u64 sym_name_len;
    __u64 sym_module;
    __u64 sym_module_len; /* if zero, no module requested/available */
};
Quoted 4 lines

e.g. for symbol you'd want to return its name, address, but also module name. and we should leave a way to extend it further with more optional information (that's the kallsyms extensibility bit I was talking about).

Agree, two ioctls with a shared symbol type is cleaner. v3 plan is to have QUERY_BY_NAME and QUERY_BY_ADDR both fill a shared struct kallsyms_sym (address, size, name, module), passed by pointer so it can grow later.

Quoted 6 lines

aslo, we should think about adding ways to paginate (or at least leave a possibility of that in the future) if there are multiple matches (e.g., for static functions with the same name resolving to multiple addresses); not sure how it should look like API wise, but it would be good to still return "any" or "first" match, but let user know there are multiple, so user can decide how to proceed...

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 8 lines
Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

searches everything as in all modules, or you mean just full linear scan?

Quoted 5 lines

And an open question I'm unsure about: Should libbpf keep the light skeleton semantics (take the first match, no ambiguity check), or set ALL_MATCHES to keep today's check that fails on names with multiple addresses?

I'd say fail on ambiguity

Quoted 7 lines
Quoted 6 lines

for QUERY_BY_ADDR we should have a flag saying whether an exact match by address is requested or we should find a symbol that covers the address (that's most useful for symbolization). maybe even "at or after" address mode, so you can paginate (though probably explicitly designed batch API would be even more useful later)

Quoted 5 lines

For v3 I think we can keep BY_ADDR to the covering lookup only. The result can fill in the covering symbol's (start address, size, name, module), and callers can check for an exact match themselves by comparing the start address with the queried address. A batch API can be added later when there is a user for it.

There is a user for batch API, that's blazesym, wprof, retsnoop and other applications that use kallsyms for symbolization. I didn't insist on adding it from the get go just to reduce the scope, but there is definitely a use case for having fast way to dump all or parts of kallsyms

Quoted 6 lines
Quoted 6 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 3 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+

[...]

Quoted 8 lines
Quoted 3 lines

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

I'm not sure how size is redundant, I don't see ioctl subsystem accepting argument size as a separate parameter, so the size has to come from the user and be embedded in user-provided structure, if we want any sort of backward/forward compatibility. At least looking at PROCMAP_QUERY I don't see how we could have avoided it, so not sure how we can do that here.

Quoted 11 lines

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
include/uapi/linux/fs.h +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

author
#9 lore raw
Quoted 159 lines · Andrii Nakryiko
Quoted 157 lines · Samuel Wu
Quoted 129 lines · Andrii Nakryiko
Quoted 91 lines · Samuel Wu

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +28 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
        __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+       /*
+        * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+        * and ftrace symbols are not searched) by exact NUL-terminated name
+        * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+        * must be 0 on input.
+        */
+       KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+       /*
+        * Look up the symbol covering sym_addr and write its NUL-terminated
+        * name to the sym_name_addr buffer of sym_name_size bytes. The same
+        * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+        * images, and freed module init functions still tracked by ftrace.
+        * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+        * always enough. On success, sym_name_size is set to the number of
+        * bytes written (including the terminating NUL).
+        */
+       KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and

include/uapi/linux/fs.h struct procmap_query { · line 714

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+       __u64 size;             /* in - struct size for compatibility */
+       __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+       __u64 sym_name_addr;    /* in - address of symbol name buffer */
+       __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+       __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
+};

it might be smart to have QUERY_BY_ADDR and QUERY_BY_NAME to be two separate ioctl commands with its own query in/out struct. This would remove addr vs name ambiguity in sym_name_addr field, and will allow us to evolve APIs independently, if necessary. They should still probably reuse the same type to represent symbol information, and have kallsyms_query struct provide a pointer to that struct (and its maximum size), which the kernel will fill out and set the size it actually knows about.

struct kallsyms_query_by_addr {
    __u64 size;
    __u64 query_flags;
    __u64 sym_addr; /* in */
    struct kallsyms_sym *sym; /* out */
    __u64 sym_len;
};
struct kallsyms_sym {
    __u64 sym_addr;
    __u64 sym_name;
    __u64 sym_name_len;
    __u64 sym_module;
    __u64 sym_module_len; /* if zero, no module requested/available */
};
Quoted 4 lines

e.g. for symbol you'd want to return its name, address, but also module name. and we should leave a way to extend it further with more optional information (that's the kallsyms extensibility bit I was talking about).

Agree, two ioctls with a shared symbol type is cleaner. v3 plan is to have QUERY_BY_NAME and QUERY_BY_ADDR both fill a shared struct kallsyms_sym (address, size, name, module), passed by pointer so it can grow later.

Quoted 6 lines

aslo, we should think about adding ways to paginate (or at least leave a possibility of that in the future) if there are multiple matches (e.g., for static functions with the same name resolving to multiple addresses); not sure how it should look like API wise, but it would be good to still return "any" or "first" match, but let user know there are multiple, so user can decide how to proceed...

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Quoted 6 lines
Quoted 4 lines

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

searches everything as in all modules, or you mean just full linear scan?

I envision ALL_MATCHES to do some form of lower_bound() + upper_bound() on vmlinux since it's sorted, and then linear scan of all modules. Of course, just one of the two or both aforementioned things depending on the module filter (any, vmlinux, module).

Quoted 7 lines
Quoted 5 lines

And an open question I'm unsure about: Should libbpf keep the light skeleton semantics (take the first match, no ambiguity check), or set ALL_MATCHES to keep today's check that fails on names with multiple addresses?

I'd say fail on ambiguity

Ack

Quoted 13 lines
Quoted 7 lines
Quoted 6 lines

for QUERY_BY_ADDR we should have a flag saying whether an exact match by address is requested or we should find a symbol that covers the address (that's most useful for symbolization). maybe even "at or after" address mode, so you can paginate (though probably explicitly designed batch API would be even more useful later)

Quoted 5 lines

For v3 I think we can keep BY_ADDR to the covering lookup only. The result can fill in the covering symbol's (start address, size, name, module), and callers can check for an exact match themselves by comparing the start address with the queried address. A batch API can be added later when there is a user for it.

Quoted 5 lines

There is a user for batch API, that's blazesym, wprof, retsnoop and other applications that use kallsyms for symbolization. I didn't insist on adding it from the get go just to reduce the scope, but there is definitely a use case for having fast way to dump all or parts of kallsyms

Ahh, ok, hopefully reducing the scope for v3 is fine.

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 3 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+

[...]

Quoted 8 lines
Quoted 3 lines

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

Quoted 6 lines

I'm not sure how size is redundant, I don't see ioctl subsystem accepting argument size as a separate parameter, so the size has to come from the user and be embedded in user-provided structure, if we want any sort of backward/forward compatibility. At least looking at PROCMAP_QUERY I don't see how we could have avoided it, so not sure how we can do that here.

My understanding is that the size is encoded in the ioctl command
itself: _IOWR() puts sizeof(struct) into the command number when userspace is compiled, so _IOC_SIZE(cmd) is the struct size of the header the caller was built against.

That said, either way is fine. We could mirror PROCMAP_QUERY style for consistency.

Quoted 11 lines
Quoted 11 lines

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
include/uapi/linux/fs.h +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

#10 lore raw
Quoted 167 lines · Samuel Wu
Quoted 164 lines · Andrii Nakryiko
Quoted 161 lines · Samuel Wu
Quoted 129 lines · Andrii Nakryiko
Quoted 91 lines · Samuel Wu

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")
- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +28 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
        __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+       /*
+        * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+        * and ftrace symbols are not searched) by exact NUL-terminated name
+        * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+        * must be 0 on input.
+        */
+       KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+       /*
+        * Look up the symbol covering sym_addr and write its NUL-terminated
+        * name to the sym_name_addr buffer of sym_name_size bytes. The same
+        * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+        * images, and freed module init functions still tracked by ftrace.
+        * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+        * always enough. On success, sym_name_size is set to the number of
+        * bytes written (including the terminating NUL).
+        */
+       KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and

include/uapi/linux/fs.h struct procmap_query { · line 714

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+       __u64 size;             /* in - struct size for compatibility */
+       __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+       __u64 sym_name_addr;    /* in - address of symbol name buffer */
+       __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+       __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
+};

it might be smart to have QUERY_BY_ADDR and QUERY_BY_NAME to be two separate ioctl commands with its own query in/out struct. This would remove addr vs name ambiguity in sym_name_addr field, and will allow us to evolve APIs independently, if necessary. They should still probably reuse the same type to represent symbol information, and have kallsyms_query struct provide a pointer to that struct (and its maximum size), which the kernel will fill out and set the size it actually knows about.

struct kallsyms_query_by_addr {
    __u64 size;
    __u64 query_flags;
    __u64 sym_addr; /* in */
    struct kallsyms_sym *sym; /* out */
    __u64 sym_len;
};
struct kallsyms_sym {
    __u64 sym_addr;
    __u64 sym_name;
    __u64 sym_name_len;
    __u64 sym_module;
    __u64 sym_module_len; /* if zero, no module requested/available */
};
Quoted 4 lines

e.g. for symbol you'd want to return its name, address, but also module name. and we should leave a way to extend it further with more optional information (that's the kallsyms extensibility bit I was talking about).

Agree, two ioctls with a shared symbol type is cleaner. v3 plan is to have QUERY_BY_NAME and QUERY_BY_ADDR both fill a shared struct kallsyms_sym (address, size, name, module), passed by pointer so it can grow later.

Quoted 6 lines

aslo, we should think about adding ways to paginate (or at least leave a possibility of that in the future) if there are multiple matches (e.g., for static functions with the same name resolving to multiple addresses); not sure how it should look like API wise, but it would be good to still return "any" or "first" match, but let user know there are multiple, so user can decide how to proceed...

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

Quoted 6 lines
Quoted 1 line

searches everything as in all modules, or you mean just full linear scan?

I envision ALL_MATCHES to do some form of lower_bound() + upper_bound() on vmlinux since it's sorted, and then linear scan of all modules. Of course, just one of the two or both aforementioned things depending on the module filter (any, vmlinux, module).

no point in upper_bound() if it's sorted, you just start at lower_bound and go linearly from there. I don't think it's realistic to assume large number of duplicates, so I wouldn't optize for that, one log(N) lower bound + linear scan will be faster in most cases, probably

Quoted 57 lines
Quoted 8 lines
Quoted 6 lines

And an open question I'm unsure about: Should libbpf keep the light skeleton semantics (take the first match, no ambiguity check), or set ALL_MATCHES to keep today's check that fails on names with multiple addresses?

I'd say fail on ambiguity

Ack

Quoted 13 lines
Quoted 7 lines
Quoted 6 lines

for QUERY_BY_ADDR we should have a flag saying whether an exact match by address is requested or we should find a symbol that covers the address (that's most useful for symbolization). maybe even "at or after" address mode, so you can paginate (though probably explicitly designed batch API would be even more useful later)

Quoted 5 lines

For v3 I think we can keep BY_ADDR to the covering lookup only. The result can fill in the covering symbol's (start address, size, name, module), and callers can check for an exact match themselves by comparing the start address with the queried address. A batch API can be added later when there is a user for it.

Quoted 5 lines

There is a user for batch API, that's blazesym, wprof, retsnoop and other applications that use kallsyms for symbolization. I didn't insist on adding it from the get go just to reduce the scope, but there is definitely a use case for having fast way to dump all or parts of kallsyms

Ahh, ok, hopefully reducing the scope for v3 is fine.

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 3 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+

[...]

Quoted 8 lines
Quoted 3 lines

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

Quoted 6 lines

I'm not sure how size is redundant, I don't see ioctl subsystem accepting argument size as a separate parameter, so the size has to come from the user and be embedded in user-provided structure, if we want any sort of backward/forward compatibility. At least looking at PROCMAP_QUERY I don't see how we could have avoided it, so not sure how we can do that here.

Quoted 5 lines

My understanding is that the size is encoded in the ioctl command
itself: _IOWR() puts sizeof(struct) into the command number when userspace is compiled, so _IOC_SIZE(cmd) is the struct size of the header the caller was built against.

oh, interesting, I didn't realize we embed struct size into ioctl cmd and match it exactly. I see that pm_scan_arg has the same __u64 size in the struct approach, I'd stick to that, and when we extend any of them we'd need to make sure to either ignore or hard-code the size part, hm..

Quoted 14 lines

That said, either way is fine. We could mirror PROCMAP_QUERY style for consistency.

Quoted 11 lines
Quoted 11 lines

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
include/uapi/linux/fs.h +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

author
#11 lore raw
Quoted 2 lines · Andrii Nakryiko

On Thu, Oct 29, 2026 at 3:36 PM Samuel Wu <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 4 lines

I envision ALL_MATCHES to do some form of lower_bound() + upper_bound() on vmlinux since it's sorted, and then linear scan of all modules. Of course, just one of the two or both aforementioned things depending on the module filter (any, vmlinux, module).

no point in upper_bound() if it's sorted, you just start at lower_bound and go linearly from there. I don't think it's realistic to assume large number of duplicates, so I wouldn't optize for that, one log(N) lower bound + linear scan will be faster in most cases, probably

Ack, lower_bound() + linear almost certainly will be faster in practice.

[...]

Quoted 11 lines
Quoted 5 lines

My understanding is that the size is encoded in the ioctl command
itself: _IOWR() puts sizeof(struct) into the command number when userspace is compiled, so _IOC_SIZE(cmd) is the struct size of the header the caller was built against.

oh, interesting, I didn't realize we embed struct size into ioctl cmd and match it exactly. I see that pm_scan_arg has the same __u64 size in the struct approach, I'd stick to that, and when we extend any of them we'd need to make sure to either ignore or hard-code the size part, hm..

Ack, will include the size in the struct for v3.

[...]

#12 lore raw
In reply to Andrii Nakryiko
Quoted 1 line · On Thu, 29 Oct 2026 15:54:01 -0700 Andrii Nakryiko <[email protected]>…

On Thu, Oct 29, 2026 at 3:36 PM Samuel Wu <[email protected]> wrote:

...

Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

Quoted 6 lines
Quoted 1 line

searches everything as in all modules, or you mean just full linear scan?

I envision ALL_MATCHES to do some form of lower_bound() + upper_bound() on vmlinux since it's sorted, and then linear scan of all modules. Of course, just one of the two or both aforementioned things depending on the module filter (any, vmlinux, module).

Quoted 5 lines

no point in upper_bound() if it's sorted, you just start at lower_bound and go linearly from there. I don't think it's realistic to assume large number of duplicates, so I wouldn't optize for that, one log(N) lower bound + linear scan will be faster in most cases, probably

Doing a system call for each symbol is probably fine and scales nicely. Just return the symbol number (in the by-address list or by-name list) so that the application can request the next one.
While the application probably isn't interested in symbols with a lot of duplicates, some symbols get duplicated thousands of times. So you have to allow for them.

(My kallsyms patches get scripts/kallsyms to mark when a symbol is a duplicate of the previous one, but after a short linear scan for the first it falls back on the binary search otherwise looking up some symbols takes a long time.)

...

Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 3 lines
include/uapi/linux/fs.h +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+  

[...]

Quoted 8 lines
Quoted 3 lines

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

I'm not sure how size is redundant, I don't see ioctl subsystem accepting argument size as a separate parameter, so the size has to come from the user and be embedded in user-provided structure, if we want any sort of backward/forward compatibility. At least looking at PROCMAP_QUERY I don't see how we could have avoided it, so not sure how we can do that here.

Quoted 4 lines
Quoted 4 lines

My understanding is that the size is encoded in the ioctl command
itself: _IOWR() puts sizeof(struct) into the command number when userspace is compiled, so _IOC_SIZE(cmd) is the struct size of the header the caller was built against.

Yep, I think that was added by the BSD kernels (but not SYSV) when they changed from 16bit to 32bit.
Linux mostly uses it, but not always.
If it were used consistently (or the high two direction bits are guaranteed unset) then the ioctl syscall wrapper can do the user copies.

You'll find that _IOC_SIZE() is used in many places.
Amongst other things it can select between 64bit and 32bit compat structures.

Quoted 8 lines

oh, interesting, I didn't realize we embed struct size into ioctl cmd and match it exactly. I see that pm_scan_arg has the same __u64 size in the struct approach, I'd stick to that, and when we extend any of them we'd need to make sure to either ignore or hard-code the size part, hm..

Quoted 2 lines

That said, either way is fine. We could mirror PROCMAP_QUERY style for consistency.

There is nothing to be consistent with.
These are the first ioctls for /proc/kallsyms.

I'd also point out the _IOC_SIZE() is limited to 14 bits. There is much less scope for applications causing grief with very out of range values - indeed you don't get past the switch statement.

David

Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
kernel/kallsyms.c +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+  

[...]

#13 lore raw
Quoted 26 lines · David Laight
Quoted 1 line · On Thu, 29 Oct 2026 15:54:01 -0700 Andrii Nakryiko <[email protected]>…

On Thu, Oct 29, 2026 at 3:36 PM Samuel Wu <[email protected]> wrote:

...

Quoted 20 lines
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines

Makes sense, but I'm thinking v3 doesn't need pagination or batching. I didn't see any performance wins when profiling libbpf, and it can be added later without breaking UAPI.

Quoted 3 lines

on input I'd also add module name as optional input argument, with "vmlinux" limiting to just vmlinux, set module name to just that name, and NULL/zero meaning "we don't care"

Quoted 6 lines
Quoted 4 lines

Makes sense, so here is a possible plan for QUERY_BY_NAME:
- Optional module filter: NULL = any, "vmlinux", or a module name
- Returns the first match by default (vmlinux first, then modules). An ALL_MATCHES flag searches everything and reports the number of matches

searches everything as in all modules, or you mean just full linear scan?

I envision ALL_MATCHES to do some form of lower_bound() + upper_bound() on vmlinux since it's sorted, and then linear scan of all modules. Of course, just one of the two or both aforementioned things depending on the module filter (any, vmlinux, module).

Quoted 7 lines
Quoted 5 lines

no point in upper_bound() if it's sorted, you just start at lower_bound and go linearly from there. I don't think it's realistic to assume large number of duplicates, so I wouldn't optize for that, one log(N) lower bound + linear scan will be faster in most cases, probably

Doing a system call for each symbol is probably fine and scales nicely.

Yep, I agree, for BY_NAME case it should be totally adequate. And I agree about not needing to do full count, just whether there is more is good enough, good suggestion!

Quoted 41 lines

Just return the symbol number (in the by-address list or by-name list) so that the application can request the next one.
While the application probably isn't interested in symbols with a lot of duplicates, some symbols get duplicated thousands of times. So you have to allow for them.

(My kallsyms patches get scripts/kallsyms to mark when a symbol is a duplicate of the previous one, but after a short linear scan for the first it falls back on the binary search otherwise looking up some symbols takes a long time.)

...

Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines

tl;dr, this is the right direction, but I think we should polish API a bit more to be more universally useful and extensible

Quoted 3 lines
(quoted diff) +3 -0
+
+#define KALLSYMS_QUERY_SIZE_VER0       40 /* sizeof first published struct */
+

[...]

Quoted 8 lines
Quoted 3 lines

PROCMAP_QUERY uses offsetofend() for the last defined field, any reason to have this #define and BUILD_BUG_ON here?

David pointed out that the size field in struct kallsyms_query is redundant. In v3, I can drop the size field and use _IOC_SIZE(cmd), keep the *_SIZE_VER0 define (since we need minimum size). Perfectly fine to switch to the PROCMAP_QUERY style though if preferred.

I'm not sure how size is redundant, I don't see ioctl subsystem accepting argument size as a separate parameter, so the size has to come from the user and be embedded in user-provided structure, if we want any sort of backward/forward compatibility. At least looking at PROCMAP_QUERY I don't see how we could have avoided it, so not sure how we can do that here.

Quoted 4 lines
Quoted 4 lines

My understanding is that the size is encoded in the ioctl command
itself: _IOWR() puts sizeof(struct) into the command number when userspace is compiled, so _IOC_SIZE(cmd) is the struct size of the header the caller was built against.

Quoted 8 lines

Yep, I think that was added by the BSD kernels (but not SYSV) when they changed from 16bit to 32bit.
Linux mostly uses it, but not always.
If it were used consistently (or the high two direction bits are guaranteed unset) then the ioctl syscall wrapper can do the user copies.

You'll find that _IOC_SIZE() is used in many places.
Amongst other things it can select between 64bit and 32bit compat structures.

I see the trend to stick to fixed sized __u64 and __u32, which allows to not worry about compat. I'd go with that (we have used that appraoch extensively in BPF syscall and it has never backfired, as far as I know)

Quoted 7 lines
Quoted 7 lines

oh, interesting, I didn't realize we embed struct size into ioctl cmd and match it exactly. I see that pm_scan_arg has the same __u64 size in the struct approach, I'd stick to that, and when we extend any of them we'd need to make sure to either ignore or hard-code the size part, hm..

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

That said, either way is fine. We could mirror PROCMAP_QUERY style for consistency.

There is nothing to be consistent with.
These are the first ioctls for /proc/kallsyms.

consistency in the sense of these ioctl-based interfaces into otherwise historically text-based interfaces.

I'm intending to fix PROCMPA_QUERY's embedded size (so that to user-space it stays the same no matter the evolution of PROCMAP_QUERY interface). I really think we should start with embedded size zero here and have size embedded in query struct itself. This is another thing that we've been doing in bpf() syscall for a really long while, and given how dynamic and complex that syscall is, we had a great success in building and evolving API in such a way that user-space code doesn't have to care all that much about multitude of different versions.

I'd really-really want us to not have KALLSYMS_QUERY_BY_NAME_{V1,V2,V3}, it's just a pain for user space. The evolution logic for versioning is simple: if user space provides too large struct, unrecognized (by kernel) fields should be zero. If user space provides too short struct (for initial version we can enforce minimal supported version from the get go), then we just not touch any fields added after that size. That's all there is to it. User space, on its part, just provides its knowledge of struct size, and then kernel will implicitly assume zeros where appropriate.

really, this is a good way to evolve API

Quoted 21 lines

I'd also point out the _IOC_SIZE() is limited to 14 bits. There is much less scope for applications causing grief with very out of range values - indeed you don't get past the switch statement.

David

Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks Andrii for all the thoughtful feedback!

Quoted 8 lines
Quoted 6 lines
(quoted diff) +6 -0
+               return -EINVAL;
+
+       err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+       if (err)
+               return err;
+

[...]

author
#14 lore raw
Quoted 3 lines · Andrii Nakryiko

On Fri, Oct 30, 2026 at 8:35 AM David Laight
<[email protected]> wrote:

[...]

Quoted 5 lines
Quoted 1 line

Doing a system call for each symbol is probably fine and scales nicely.

Yep, I agree, for BY_NAME case it should be totally adequate. And I agree about not needing to do full count, just whether there is more is good enough, good suggestion!

Makes sense, thanks both. Nearly all symbol names in /proc/kallsyms are unique (~99% on my test VM), so I'll have v3 keep the kernel side simple and let userspace apply its own duplicate policy via one-by-one
pagination: KALLSYMS_QUERY_BY_NAME takes pos (0 to start) and returns next_pos across all sources (vmlinux, modules, ftrace, BPF JIT, and kprobes). The kernel peeks ahead one match, so in the common, unique case a single ioctl() returns the symbol with next_pos == 0; when next_pos != 0, userspace can step through the remaining matches one ioctl() at a time. libbpf does that and still fails the load if the matches have different addresses.

Paired with a prefix flag (KALLSYMS_F_PREFIX), the same lower_bound + pos/next_pos also handles suffixed symbols cleanly since they're
sorted: querying <name>.llvm." (or "<name>." for .constprop, etc.) lands directly on the contiguous suffix entries in the sorted vmlinux table and userspace can choose to step through clones via next_pos.

[...]

Quoted 7 lines
Quoted 2 lines

You'll find that _IOC_SIZE() is used in many places.
Amongst other things it can select between 64bit and 32bit compat structures.

I see the trend to stick to fixed sized __u64 and __u32, which allows to not worry about compat. I'd go with that (we have used that appraoch extensively in BPF syscall and it has never backfired, as far as I know)

Ack, I'll update v3 to use __u64 fields throughout. There already is a precedent for this, and it is simpler to not fill holes that __u32 would leave.

[...]

Quoted 10 lines

I'm intending to fix PROCMPA_QUERY's embedded size (so that to user-space it stays the same no matter the evolution of PROCMAP_QUERY interface). I really think we should start with embedded size zero here and have size embedded in query struct itself. This is another thing that we've been doing in bpf() syscall for a really long while, and given how dynamic and complex that syscall is, we had a great success in building and evolving API in such a way that user-space code doesn't have to care all that much about multitude of different versions.

Quoted 9 lines

I'd really-really want us to not have KALLSYMS_QUERY_BY_NAME_{V1,V2,V3}, it's just a pain for user space. The evolution logic for versioning is simple: if user space provides too large struct, unrecognized (by kernel) fields should be zero. If user space provides too short struct (for initial version we can enforce minimal supported version from the get go), then we just not touch any fields added after that size. That's all there is to it. User space, on its part, just provides its knowledge of struct size, and then kernel will implicitly assume zeros where appropriate.

Ack. In v3, I'll update KALLSYMS_QUERY_BY_NAME and KALLSYMS_QUERY_BY_ADDR to be _IOC(..., 0), and so the sizes live in the structs (size; sym_struct_size for the nested struct kallsyms_sym). Sorry for the back-and-forth, David. Also, the nested kallsyms_sym needs its own size anyway, which _IOC_SIZE() cannot carry. That keeps userspace free of V1...Vn command variants as the struct grows.

#15 lore raw
In reply to Samuel Wu
Quoted 4 lines · On Thu, 22 Oct 2026 17:16:02 -0700 Samuel Wu <[email protected]> wrote:

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

Quoted 10 lines

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")

Hmmmm....
What happens when you have functions foo and foo.constprop0 In some sense they are duplicates.

Quoted 9 lines

- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

You could mmap the tables into userspace and have a ioctl that reported the offsets of the tables (etc).
Then user lookup would faster then kernel lookup!
Especially since you could throw a bit more memory at it to remove the linear scans.

Quoted 14 lines

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

Quoted 2 lines

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

Have you tried measuring it with my patches that speed up the kernel 'search by name' code?
That speeds the lookup by a factor of ~18 (testing the code in userspace).

You also should allow for 'find first' and 'find next'.
As well as letting the code iterate though functions with the same name it is absolutely trivial to enhance the 'lookup by name' code to match a prefix.
(Basically it needs to 'return -index' instead of 'return -1' when the binary scan fails.)

Quoted 46 lines

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +23 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
 	__u64 build_id_addr;		/* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY	_IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+	/*
+	 * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+	 * and ftrace symbols are not searched) by exact NUL-terminated name
+	 * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+	 * must be 0 on input.
+	 */
+	KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+	/*
+	 * Look up the symbol covering sym_addr and write its NUL-terminated
+	 * name to the sym_name_addr buffer of sym_name_size bytes. The same
+	 * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+	 * images, and freed module init functions still tracked by ftrace.
+	 * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+	 * always enough. On success, sym_name_size is set to the number of
+	 * bytes written (including the terminating NUL).
+	 */
+	KALLSYMS_QUERY_BY_ADDR = 0x02,
+};

include/uapi/linux/fs.h struct procmap_query { · line 709

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+	__u64 size;		/* in - struct size for compatibility */

I'm not sure you need a size here as well as the one in the ioctl command. It only complicates things when they differ.

If you need a new version they you add an extra case to the switch statement. Simple and easy,

include/uapi/linux/fs.h struct procmap_query { · line 713

Quoted 4 lines
include/uapi/linux/fs.h +4 -0
+	__u64 query_flags;	/* in - query mode (enum kallsyms_query_flags) */
+	__u64 sym_name_addr;	/* in - address of symbol name buffer */
+	__u64 sym_addr;		/* out (BY_SYMBOL) / in (BY_ADDR) */
+	__u64 sym_name_size;	/* in/out - name buffer size / bytes written */

Maybe separate the buffer size from the length of the symbol written? The in/out parameter will get annoying in code doing multiple lookups. Or just have the buffer size and require it to be '\0' terminated. (There is a strncpy_from_user() can that be used.)

The '\0' terminated string handling is likely to be noise against all the rest of the lookup code.

David

Quoted 162 lines
include/uapi/linux/fs.h +4 -0
+};
+
+#define KALLSYMS_QUERY_SIZE_VER0	40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +132 -0
index aec2f06858af..7caaf3e421c7 100644
@@ -31,6 +31,7 @@
 #include <linux/kernel.h>
 #include <linux/bsearch.h>
 #include <linux/btf_ids.h>
+#include <linux/uaccess.h>
 
 #include "kallsyms_internal.h"
 
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)
 }
 #endif	/* CONFIG_KGDB_KDB */
 
+struct kallsyms_mod_find_ctx {
+	const char *name;
+	unsigned long addr;
+	bool found;
+};
+
+static int kallsyms_mod_find_cb(void *data, const char *name, unsigned long addr)
+{
+	struct kallsyms_mod_find_ctx *ctx = data;
+
+	if (strcmp(name, ctx->name) != 0)
+		return 0;
+	if (ctx->found && ctx->addr != addr)
+		return -EADDRNOTAVAIL;
+	ctx->found = true;
+	ctx->addr = addr;
+	return 0;
+}
+
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{
+	char namebuf[KSYM_NAME_LEN];
+	struct kallsyms_query karg;
+	unsigned long addr;
+	unsigned int start;
+	unsigned int end;
+	unsigned int i;
+	__u64 usize;
+	long len;
+	int err;
+
+	BUILD_BUG_ON(sizeof(struct kallsyms_query) != KALLSYMS_QUERY_SIZE_VER0);
+
+	/*
+	 * The text interface shows zero addresses to openers without permission.
+	 * A zeroed point query result is useless (and an address-to-symbol query
+	 * would leak KASLR), so fail instead.
+	 */
+	if (!iter->show_value)
+		return -EPERM;
+	if (copy_from_user(&usize, uarg, sizeof(usize)))
+		return -EFAULT;
+	if (usize > PAGE_SIZE)
+		return -E2BIG;
+	if (usize < KALLSYMS_QUERY_SIZE_VER0)
+		return -EINVAL;
+
+	err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+	if (err)
+		return err;
+
+	switch (karg.query_flags) {
+	case KALLSYMS_QUERY_BY_SYMBOL:
+		if (karg.sym_addr || karg.sym_name_size)
+			return -EINVAL;
+
+		len = strncpy_from_user(namebuf, u64_to_user_ptr(karg.sym_name_addr),
+					sizeof(namebuf));
+		if (len < 0)
+			return len;
+		if (len == 0)
+			return -EINVAL;
+		if (len == sizeof(namebuf))
+			return -ENAMETOOLONG;
+
+		if (kallsyms_lookup_names(namebuf, &start, &end)) {
+			struct kallsyms_mod_find_ctx ctx = { .name = namebuf };
+
+			err = module_kallsyms_on_each_symbol(NULL, kallsyms_mod_find_cb, &ctx);
+			/* Check first: maps -EOPNOTSUPP (CONFIG_MODULES=n) to -ENOENT. */
+			if (!ctx.found)
+				return -ENOENT;
+			if (err)
+				return err;
+			addr = ctx.addr;
+		} else {
+			addr = kallsyms_sym_address(get_symbol_seq(start));
+			for (i = start + 1; i <= end; i++) {
+				if (kallsyms_sym_address(get_symbol_seq(i)) != addr)
+					return -EADDRNOTAVAIL;
+			}
+		}
+
+		karg.sym_addr = addr;
+		break;
+	case KALLSYMS_QUERY_BY_ADDR:
+		if (!karg.sym_name_size)
+			return -EINVAL;
+		addr = karg.sym_addr;
+		/* Reject addresses that do not fit in unsigned long (32-bit). */
+		if (addr != karg.sym_addr)
+			return -ENOENT;
+		if (!kallsyms_lookup(addr, NULL, NULL, NULL, namebuf))
+			return -ENOENT;
+
+		len = strlen(namebuf) + 1;
+		if (karg.sym_name_size < len)
+			return -ENAMETOOLONG;
+		if (copy_to_user(u64_to_user_ptr(karg.sym_name_addr), namebuf, len))
+			return -EFAULT;
+
+		karg.sym_name_size = len;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize)))
+		return -EFAULT;
+
+	return 0;
+}
+
+static long kallsyms_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct seq_file *m = file->private_data;
+
+	switch (_IOC_NR(cmd)) {
+	case _IOC_NR(KALLSYMS_QUERY):
+		if (!extensible_ioctl_valid(cmd, KALLSYMS_QUERY, KALLSYMS_QUERY_SIZE_VER0))
+			return -ENOIOCTLCMD;
+		return do_kallsyms_query(m->private, (void __user *)arg);
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 static const struct proc_ops kallsyms_proc_ops = {
 	.proc_open	= kallsyms_open,
 	.proc_read	= seq_read,
 	.proc_lseek	= seq_lseek,
 	.proc_release	= seq_release_private,
+	.proc_ioctl	= kallsyms_ioctl,
+#ifdef CONFIG_COMPAT
+	.proc_compat_ioctl = compat_ptr_ioctl,
+#endif
 };
 
 static int __init kallsyms_init(void)
author
#16 lore raw
Quoted 19 lines · David Laight
Quoted 4 lines · On Thu, 22 Oct 2026 17:16:02 -0700 Samuel Wu <[email protected]> wrote:

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

Quoted 10 lines

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")

Quoted 3 lines

Hmmmm....
What happens when you have functions foo and foo.constprop0 In some sense they are duplicates.

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Quoted 10 lines
Quoted 9 lines

- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

Quoted 5 lines

You could mmap the tables into userspace and have a ioctl that reported the offsets of the tables (etc).
Then user lookup would faster then kernel lookup!
Especially since you could throw a bit more memory at it to remove the linear scans.

I think that would increase the UAPI surface because it would set the table format, so I'd prefer not to. It would only cover vmlinux and not modules, and then we'd need to apply kptr_restrict checks to the mapping.

Quoted 14 lines
Quoted 14 lines

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

Quoted 6 lines
Quoted 2 lines

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

Have you tried measuring it with my patches that speed up the kernel 'search by name' code?
That speeds the lookup by a factor of ~18 (testing the code in userspace).

Not yet. Can you point me to the latest version? Happy to try it out.

Quoted 4 lines

You also should allow for 'find first' and 'find next'.
As well as letting the code iterate though functions with the same name it is absolutely trivial to enhance the 'lookup by name' code to match a prefix.

Agreed, that would be useful. In v3, the idea will be to have BY_NAME return the first match by default, and an ALL_MATCHES flag to report how many matches exist. Find next and prefix matching can be added later with breaking UAPI, and best to not include in v3 since there isn't yet a user.

Quoted 64 lines

(Basically it needs to 'return -index' instead of 'return -1' when the binary scan fails.)

Quoted 46 lines

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +23 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
      __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY       _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+     /*
+      * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+      * and ftrace symbols are not searched) by exact NUL-terminated name
+      * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+      * must be 0 on input.
+      */
+     KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+     /*
+      * Look up the symbol covering sym_addr and write its NUL-terminated
+      * name to the sym_name_addr buffer of sym_name_size bytes. The same
+      * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+      * images, and freed module init functions still tracked by ftrace.
+      * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+      * always enough. On success, sym_name_size is set to the number of
+      * bytes written (including the terminating NUL).
+      */
+     KALLSYMS_QUERY_BY_ADDR = 0x02,
+};

include/uapi/linux/fs.h struct procmap_query { · line 709

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+     __u64 size;             /* in - struct size for compatibility */

I'm not sure you need a size here as well as the one in the ioctl command. It only complicates things when they differ.

Quoted 2 lines

If you need a new version they you add an extra case to the switch statement. Simple and easy,

Good find thanks! Will update.

Quoted 9 lines

include/uapi/linux/fs.h struct procmap_query { · line 713

Quoted 4 lines
include/uapi/linux/fs.h +4 -0
+     __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+     __u64 sym_name_addr;    /* in - address of symbol name buffer */
+     __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+     __u64 sym_name_size;    /* in/out - name buffer size / bytes written */

Maybe separate the buffer size from the length of the symbol written? The in/out parameter will get annoying in code doing multiple lookups. Or just have the buffer size and require it to be '\0' terminated. (There is a strncpy_from_user() can that be used.)

That is simpler- in v3, we can have buffer sizes as input only, and names will always be NUL-terminated.

Quoted 168 lines

The '\0' terminated string handling is likely to be noise against all the rest of the lookup code.

David

Quoted 162 lines
include/uapi/linux/fs.h +4 -0
+};
+
+#define KALLSYMS_QUERY_SIZE_VER0     40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +132 -0
index aec2f06858af..7caaf3e421c7 100644
@@ -31,6 +31,7 @@
 #include <linux/kernel.h>
 #include <linux/bsearch.h>
 #include <linux/btf_ids.h>
+#include <linux/uaccess.h>
 
 #include "kallsyms_internal.h"
 
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)
 }
 #endif       /* CONFIG_KGDB_KDB */
 
+struct kallsyms_mod_find_ctx {
+     const char *name;
+     unsigned long addr;
+     bool found;
+};
+
+static int kallsyms_mod_find_cb(void *data, const char *name, unsigned long addr)
+{
+     struct kallsyms_mod_find_ctx *ctx = data;
+
+     if (strcmp(name, ctx->name) != 0)
+             return 0;
+     if (ctx->found && ctx->addr != addr)
+             return -EADDRNOTAVAIL;
+     ctx->found = true;
+     ctx->addr = addr;
+     return 0;
+}
+
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{
+     char namebuf[KSYM_NAME_LEN];
+     struct kallsyms_query karg;
+     unsigned long addr;
+     unsigned int start;
+     unsigned int end;
+     unsigned int i;
+     __u64 usize;
+     long len;
+     int err;
+
+     BUILD_BUG_ON(sizeof(struct kallsyms_query) != KALLSYMS_QUERY_SIZE_VER0);
+
+     /*
+      * The text interface shows zero addresses to openers without permission.
+      * A zeroed point query result is useless (and an address-to-symbol query
+      * would leak KASLR), so fail instead.
+      */
+     if (!iter->show_value)
+             return -EPERM;
+     if (copy_from_user(&usize, uarg, sizeof(usize)))
+             return -EFAULT;
+     if (usize > PAGE_SIZE)
+             return -E2BIG;
+     if (usize < KALLSYMS_QUERY_SIZE_VER0)
+             return -EINVAL;
+
+     err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+     if (err)
+             return err;
+
+     switch (karg.query_flags) {
+     case KALLSYMS_QUERY_BY_SYMBOL:
+             if (karg.sym_addr || karg.sym_name_size)
+                     return -EINVAL;
+
+             len = strncpy_from_user(namebuf, u64_to_user_ptr(karg.sym_name_addr),
+                                     sizeof(namebuf));
+             if (len < 0)
+                     return len;
+             if (len == 0)
+                     return -EINVAL;
+             if (len == sizeof(namebuf))
+                     return -ENAMETOOLONG;
+
+             if (kallsyms_lookup_names(namebuf, &start, &end)) {
+                     struct kallsyms_mod_find_ctx ctx = { .name = namebuf };
+
+                     err = module_kallsyms_on_each_symbol(NULL, kallsyms_mod_find_cb, &ctx);
+                     /* Check first: maps -EOPNOTSUPP (CONFIG_MODULES=n) to -ENOENT. */
+                     if (!ctx.found)
+                             return -ENOENT;
+                     if (err)
+                             return err;
+                     addr = ctx.addr;
+             } else {
+                     addr = kallsyms_sym_address(get_symbol_seq(start));
+                     for (i = start + 1; i <= end; i++) {
+                             if (kallsyms_sym_address(get_symbol_seq(i)) != addr)
+                                     return -EADDRNOTAVAIL;
+                     }
+             }
+
+             karg.sym_addr = addr;
+             break;
+     case KALLSYMS_QUERY_BY_ADDR:
+             if (!karg.sym_name_size)
+                     return -EINVAL;
+             addr = karg.sym_addr;
+             /* Reject addresses that do not fit in unsigned long (32-bit). */
+             if (addr != karg.sym_addr)
+                     return -ENOENT;
+             if (!kallsyms_lookup(addr, NULL, NULL, NULL, namebuf))
+                     return -ENOENT;
+
+             len = strlen(namebuf) + 1;
+             if (karg.sym_name_size < len)
+                     return -ENAMETOOLONG;
+             if (copy_to_user(u64_to_user_ptr(karg.sym_name_addr), namebuf, len))
+                     return -EFAULT;
+
+             karg.sym_name_size = len;
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize)))
+             return -EFAULT;
+
+     return 0;
+}
+
+static long kallsyms_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+     struct seq_file *m = file->private_data;
+
+     switch (_IOC_NR(cmd)) {
+     case _IOC_NR(KALLSYMS_QUERY):
+             if (!extensible_ioctl_valid(cmd, KALLSYMS_QUERY, KALLSYMS_QUERY_SIZE_VER0))
+                     return -ENOIOCTLCMD;
+             return do_kallsyms_query(m->private, (void __user *)arg);
+     default:
+             return -ENOIOCTLCMD;
+     }
+}
+
 static const struct proc_ops kallsyms_proc_ops = {
      .proc_open      = kallsyms_open,
      .proc_read      = seq_read,
      .proc_lseek     = seq_lseek,
      .proc_release   = seq_release_private,
+     .proc_ioctl     = kallsyms_ioctl,
+#ifdef CONFIG_COMPAT
+     .proc_compat_ioctl = compat_ptr_ioctl,
+#endif
 };
 
 static int __init kallsyms_init(void)
#17 lore raw
Quoted 22 lines · Samuel Wu
Quoted 19 lines · David Laight
Quoted 14 lines · On Thu, 22 Oct 2026 17:16:02 -0700 Samuel Wu <[email protected]> wrote:

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")

Quoted 8 lines
Quoted 3 lines

Hmmmm....
What happens when you have functions foo and foo.constprop0 In some sense they are duplicates.

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

foo and foo.constprop0 are different functions with different arguments, so I think we should distinguish them. The only special case is .llvm.<hash> because that suffix is added by compiler for LTO and I don't think the function itself is any different from the original

Quoted 49 lines
Quoted 10 lines
Quoted 9 lines

- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

Quoted 5 lines

You could mmap the tables into userspace and have a ioctl that reported the offsets of the tables (etc).
Then user lookup would faster then kernel lookup!
Especially since you could throw a bit more memory at it to remove the linear scans.

I think that would increase the UAPI surface because it would set the table format, so I'd prefer not to. It would only cover vmlinux and not modules, and then we'd need to apply kptr_restrict checks to the mapping.

Quoted 14 lines
Quoted 14 lines

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

Quoted 6 lines
Quoted 2 lines

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

Have you tried measuring it with my patches that speed up the kernel 'search by name' code?
That speeds the lookup by a factor of ~18 (testing the code in userspace).

Not yet. Can you point me to the latest version? Happy to try it out.

Quoted 4 lines

You also should allow for 'find first' and 'find next'.
As well as letting the code iterate though functions with the same name it is absolutely trivial to enhance the 'lookup by name' code to match a prefix.

Quoted 5 lines

Agreed, that would be useful. In v3, the idea will be to have BY_NAME return the first match by default, and an ALL_MATCHES flag to report how many matches exist. Find next and prefix matching can be added later with breaking UAPI, and best to not include in v3 since there isn't yet a user.

I do think it would be good to spend a bit of thought on how to support at least one-by-one pagination from the get go. The same mechanism can then be used by batch version. I.e., think what can we use to provide data back to user which they can use to continue search after the symbol we just returned (at least for fixed set of symbols in vmlinux and modules; we also have dynamic symbols added for [bpf] "module", etc, we should support those as much as possible with the least possible deviation, IMO)

Quoted 65 lines
Quoted 65 lines

(Basically it needs to 'return -index' instead of 'return -1' when the binary scan fails.)

Quoted 46 lines

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +23 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
      __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY       _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+     /*
+      * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+      * and ftrace symbols are not searched) by exact NUL-terminated name
+      * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+      * must be 0 on input.
+      */
+     KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+     /*
+      * Look up the symbol covering sym_addr and write its NUL-terminated
+      * name to the sym_name_addr buffer of sym_name_size bytes. The same
+      * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+      * images, and freed module init functions still tracked by ftrace.
+      * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+      * always enough. On success, sym_name_size is set to the number of
+      * bytes written (including the terminating NUL).
+      */
+     KALLSYMS_QUERY_BY_ADDR = 0x02,
+};

include/uapi/linux/fs.h struct procmap_query { · line 709

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+     __u64 size;             /* in - struct size for compatibility */

I'm not sure you need a size here as well as the one in the ioctl command. It only complicates things when they differ.

Quoted 4 lines
Quoted 2 lines

If you need a new version they you add an extra case to the switch statement. Simple and easy,

Good find thanks! Will update.

think about user space application compiled against UAPI headers from older kernel version. For that application size will be smaller and that's ok, newer kernel should honor this and only fill out smaller part of v2 struct. Versioning is done by appending fields. This is a well established pattern that provides a pretty good forward/backward compat story, let's keep it.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
include/uapi/linux/fs.h +4 -0
+     __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */
+     __u64 sym_name_addr;    /* in - address of symbol name buffer */
+     __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+     __u64 sym_name_size;    /* in/out - name buffer size / bytes written */
Quoted 8 lines
Quoted 4 lines

Maybe separate the buffer size from the length of the symbol written? The in/out parameter will get annoying in code doing multiple lookups. Or just have the buffer size and require it to be '\0' terminated. (There is a strncpy_from_user() can that be used.)

That is simpler- in v3, we can have buffer sizes as input only, and names will always be NUL-terminated.

user code can be easily written by always setting buffer size, even if kallsyms_query is reused. This is the pattern with PROCMAP_QUERY, let's keep it consistent.

also, returning actual size even if buffer is too short is a good way for user space to "probe" and increase buffer if necessary. unless it adds tons of complexity, this is a good and flexible UAPI

Quoted 5 lines
Quoted 5 lines

The '\0' terminated string handling is likely to be noise against all the rest of the lookup code.

David

[...]

author
#18 lore raw
Quoted 13 lines · Andrii Nakryiko
Quoted 5 lines · Samuel Wu

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

foo and foo.constprop0 are different functions with different arguments, so I think we should distinguish them. The only special case is .llvm.<hash> because that suffix is added by compiler for LTO and I don't think the function itself is any different from the original

[...]

Quoted 8 lines

I do think it would be good to spend a bit of thought on how to support at least one-by-one pagination from the get go. The same mechanism can then be used by batch version. I.e., think what can we use to provide data back to user which they can use to continue search after the symbol we just returned (at least for fixed set of symbols in vmlinux and modules; we also have dynamic symbols added for [bpf] "module", etc, we should support those as much as possible with the least possible deviation, IMO)

Let me sit on this a bit and figure out how to distill all of this into a clean interface. I'll keep the .llvm.<hash>, pagination, and batching in mind for the design.

[...]

Quoted 3 lines

user code can be easily written by always setting buffer size, even if kallsyms_query is reused. This is the pattern with PROCMAP_QUERY, let's keep it consistent.

Ack, will have size in the struct for v3.

Quoted 3 lines

also, returning actual size even if buffer is too short is a good way for user space to "probe" and increase buffer if necessary. unless it adds tons of complexity, this is a good and flexible UAPI

Ack.

#19 lore raw
Quoted 31 lines · On Thu, 29 Oct 2026 17:28:01 -0700 Samuel Wu <[email protected]> wrote:
Quoted 13 lines · Andrii Nakryiko
Quoted 5 lines · Samuel Wu

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

foo and foo.constprop0 are different functions with different arguments, so I think we should distinguish them. The only special case is .llvm.<hash> because that suffix is added by compiler for LTO and I don't think the function itself is any different from the original

[...]

Quoted 8 lines

I do think it would be good to spend a bit of thought on how to support at least one-by-one pagination from the get go. The same mechanism can then be used by batch version. I.e., think what can we use to provide data back to user which they can use to continue search after the symbol we just returned (at least for fixed set of symbols in vmlinux and modules; we also have dynamic symbols added for [bpf] "module", etc, we should support those as much as possible with the least possible deviation, IMO)

Let me sit on this a bit and figure out how to distill all of this into a clean interface. I'll keep the .llvm.<hash>, pagination, and batching in mind for the design.

Quoted 7 lines

[...]

Quoted 3 lines

user code can be easily written by always setting buffer size, even if kallsyms_query is reused. This is the pattern with PROCMAP_QUERY, let's keep it consistent.

Ack, will have size in the struct for v3.

It just adds code.
If you use the size from the ioctl (which will match the structure the application passes) then you don't need to do the extra get_user() just to get the length.
It isn't really hard to have:

	case KALLSYMS_QUERY_v1:
	case KALLSYMS_QUERY:
		return do_kallsys_query(m->private, cmd, (void __user *)arg)

It also saves all the problems of the two values mismatching.

That is why there is a length (and the r/w flags) embedded in the ioctl cmd value.

David

Quoted 6 lines
Quoted 4 lines

also, returning actual size even if buffer is too short is a good way for user space to "probe" and increase buffer if necessary. unless it adds tons of complexity, this is a good and flexible UAPI

Ack.

#20 lore raw
In reply to Samuel Wu
Quoted 21 lines · On Thu, 29 Oct 2026 15:05:45 -0700 Samuel Wu <[email protected]> wrote:
Quoted 19 lines · David Laight
Quoted 14 lines · On Thu, 22 Oct 2026 17:16:02 -0700 Samuel Wu <[email protected]> wrote:

Add an extensible KALLSYMS_QUERY ioctl() interface to /proc/kallsyms to support fast point queries without scanning the entire text file. Exactly one query mode must be set in query_flags.

When KALLSYMS_QUERY_BY_SYMBOL is set in query_flags, KALLSYMS_QUERY resolves a NUL-terminated symbol name in vmlinux or loaded modules and then populates sym_addr:
- Searches vmlinux first via kallsyms_lookup_names() (binary search), which takes precedence over loaded modules, and falls back to module_kallsyms_on_each_symbol() under module_mutex for module symbols
- Matches names exactly, like kallsyms_lookup_name(), disregarding the .llvm.<hash> matching, following precedent set by commit fb6a421fb615 ("kallsyms: Match symbols exactly with CONFIG_LTO_CLANG")

Quoted 7 lines
Quoted 3 lines

Hmmmm....
What happens when you have functions foo and foo.constprop0 In some sense they are duplicates.

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Only that the user might really want one of the copies. Sometimes there is only the copy.

Quoted 17 lines
Quoted 11 lines
Quoted 10 lines

- Resolves 0x0 symbol addresses in both vmlinux and modules
- Rejects ambiguous symbols (multiple entries with the same name at different addresses within vmlinux, or across loaded modules) with -EADDRNOTAVAIL, while allowing same-address aliases

Userspace symbolizers resolve kernel addresses to symbol names with /proc/kallsyms. This is typically done by reading the entire /proc/kallsyms text file, and keeping a compressed sorted copy of it in order to resolve addresses at runtime.

Quoted 5 lines

You could mmap the tables into userspace and have a ioctl that reported the offsets of the tables (etc).
Then user lookup would faster then kernel lookup!
Especially since you could throw a bit more memory at it to remove the linear scans.

Quoted 4 lines

I think that would increase the UAPI surface because it would set the table format, so I'd prefer not to. It would only cover vmlinux and not modules, and then we'd need to apply kptr_restrict checks to the mapping.

True.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

When KALLSYMS_QUERY_BY_ADDR is set in query_flags, KALLSYMS_QUERY resolves one address to the symbol that contains it:
- sym_addr holds the address to resolve on input.
- sym_name_addr must point to a user buffer of sym_name_size bytes that receives the NUL-terminated symbol name. On output, sym_name_size holds the number of bytes written (including the terminating NUL). -ENAMETOOLONG is returned if the buffer is too small; a buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is always large enough.
- Symbols are resolved via kallsyms_lookup(), the same lookup as %pS, covering vmlinux, loaded modules, BPF JIT images, and freed module init functions still tracked by ftrace.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

On an 8 CPU Cuttlefish (x86-64 VM), using this patchseries to load a BPF program with 11 typeless ksyms has ~220x speedup (157ms -> 0.7ms).

Have you tried measuring it with my patches that speed up the kernel 'search by name' code?
That speeds the lookup by a factor of ~18 (testing the code in userspace).

Not yet. Can you point me to the latest version? Happy to try it out.

I've just forwarded you all the patches.
(They include patches to the tests and scripts/kallsyms) I'll copy you (and the other people getting this email) for v3.

Quoted 9 lines
Quoted 5 lines

You also should allow for 'find first' and 'find next'.
As well as letting the code iterate though functions with the same name it is absolutely trivial to enhance the 'lookup by name' code to match a prefix.

Agreed, that would be useful. In v3, the idea will be to have BY_NAME return the first match by default, and an ALL_MATCHES flag to report how many matches exist.

Reporting the count is expensive, reporting that there is another with the same same is cheap.
Some symbols (that probably wont be looked up) occur 100s of times in a normal kernel, build allyesconfig (I tested the code with it) and the worst case is about 46000 times.

So a bit that indicates 'duplicates' exist would be reasonable. But without the ability to do a 'read next' it isn't much use.

Another bit could be used/reserved for 'the next symbol is foo.bar'. That would need returning even if foo itself is missing.

Quoted 3 lines

Find next and prefix matching can be added
later with breaking UAPI, and best to not include in v3 since there isn't yet a user.

The main thing it needs is a field to include the 'symbol number' in the ioctl structure.
It'll save churn to reserve a field now - and won't really make much difference to anything else.

Quoted 65 lines
Quoted 65 lines

(Basically it needs to 'return -index' instead of 'return -1' when the binary scan fails.)

Quoted 46 lines

On an 8 CPU x86-64 VM with a 175k line kallsyms, there are broad performance and memory savings for the symbolization and callstack sampling tool, Perfetto:
- Init time: ~350x speedup (155.7ms -> 0.44ms)
- 50k symbolizations: ~12x speedup (25.2ms -> 2.2ms)
- VmRSS: -68% (15.1MB -> 4.8MB)

---
 include/uapi/linux/fs.h |  41 +++++++++++++
 kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+)
include/uapi/linux/fs.h +23 -0
index 34c6f219462a..b43f227dad0e 100644
@@ -674,6 +674,47 @@ struct procmap_query {
      __u64 build_id_addr;            /* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY       _IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+     /*
+      * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+      * and ftrace symbols are not searched) by exact NUL-terminated name
+      * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+      * must be 0 on input.
+      */
+     KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+     /*
+      * Look up the symbol covering sym_addr and write its NUL-terminated
+      * name to the sym_name_addr buffer of sym_name_size bytes. The same
+      * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+      * images, and freed module init functions still tracked by ftrace.
+      * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+      * always enough. On success, sym_name_size is set to the number of
+      * bytes written (including the terminating NUL).
+      */
+     KALLSYMS_QUERY_BY_ADDR = 0x02,
+};

include/uapi/linux/fs.h struct procmap_query { · line 709

Quoted 10 lines
include/uapi/linux/fs.h +10 -0
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+     __u64 size;             /* in - struct size for compatibility */  

I'm not sure you need a size here as well as the one in the ioctl command. It only complicates things when they differ.

Quoted 4 lines
Quoted 2 lines

If you need a new version they you add an extra case to the switch statement. Simple and easy,

Good find thanks! Will update.

You'll need to pass the ioctl cmd through as well copy_struct_from_user() will handle the size from the ioctl requests being shorter because it is an old ioctl format and zero the extra for you.

Quoted 1 line
Quoted 1 line

include/uapi/linux/fs.h struct procmap_query { · line 710

Quoted 1 line
include/uapi/linux/fs.h +1 -0
+     __u64 query_flags;      /* in - query mode (enum kallsyms_query_flags) */

There is no need for a command here - use the ioctl cmd. You might want a flag for a qualifier, but there are plenty of ioctl command values.

David

Quoted 180 lines
Quoted 8 lines

include/uapi/linux/fs.h struct procmap_query { · line 713

Quoted 3 lines
include/uapi/linux/fs.h +3 -0
+     __u64 sym_name_addr;    /* in - address of symbol name buffer */
+     __u64 sym_addr;         /* out (BY_SYMBOL) / in (BY_ADDR) */
+     __u64 sym_name_size;    /* in/out - name buffer size / bytes written */  

Maybe separate the buffer size from the length of the symbol written? The in/out parameter will get annoying in code doing multiple lookups. Or just have the buffer size and require it to be '\0' terminated. (There is a strncpy_from_user() can that be used.)

That is simpler- in v3, we can have buffer sizes as input only, and names will always be NUL-terminated.

Quoted 168 lines

The '\0' terminated string handling is likely to be noise against all the rest of the lookup code.

David

Quoted 162 lines
include/uapi/linux/fs.h +4 -0
+};
+
+#define KALLSYMS_QUERY_SIZE_VER0     40 /* sizeof first published struct */
+
 /*
  * Shutdown the filesystem.
  */
kernel/kallsyms.c +132 -0
index aec2f06858af..7caaf3e421c7 100644
@@ -31,6 +31,7 @@
 #include <linux/kernel.h>
 #include <linux/bsearch.h>
 #include <linux/btf_ids.h>
+#include <linux/uaccess.h>
 
 #include "kallsyms_internal.h"
 
@@ -922,11 +923,142 @@ const char *kdb_walk_kallsyms(loff_t *pos)
 }
 #endif       /* CONFIG_KGDB_KDB */
 
+struct kallsyms_mod_find_ctx {
+     const char *name;
+     unsigned long addr;
+     bool found;
+};
+
+static int kallsyms_mod_find_cb(void *data, const char *name, unsigned long addr)
+{
+     struct kallsyms_mod_find_ctx *ctx = data;
+
+     if (strcmp(name, ctx->name) != 0)
+             return 0;
+     if (ctx->found && ctx->addr != addr)
+             return -EADDRNOTAVAIL;
+     ctx->found = true;
+     ctx->addr = addr;
+     return 0;
+}
+
+static int do_kallsyms_query(struct kallsym_iter *iter, void __user *uarg)
+{
+     char namebuf[KSYM_NAME_LEN];
+     struct kallsyms_query karg;
+     unsigned long addr;
+     unsigned int start;
+     unsigned int end;
+     unsigned int i;
+     __u64 usize;
+     long len;
+     int err;
+
+     BUILD_BUG_ON(sizeof(struct kallsyms_query) != KALLSYMS_QUERY_SIZE_VER0);
+
+     /*
+      * The text interface shows zero addresses to openers without permission.
+      * A zeroed point query result is useless (and an address-to-symbol query
+      * would leak KASLR), so fail instead.
+      */
+     if (!iter->show_value)
+             return -EPERM;
+     if (copy_from_user(&usize, uarg, sizeof(usize)))
+             return -EFAULT;
+     if (usize > PAGE_SIZE)
+             return -E2BIG;
+     if (usize < KALLSYMS_QUERY_SIZE_VER0)
+             return -EINVAL;
+
+     err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+     if (err)
+             return err;
+
+     switch (karg.query_flags) {
+     case KALLSYMS_QUERY_BY_SYMBOL:
+             if (karg.sym_addr || karg.sym_name_size)
+                     return -EINVAL;
+
+             len = strncpy_from_user(namebuf, u64_to_user_ptr(karg.sym_name_addr),
+                                     sizeof(namebuf));
+             if (len < 0)
+                     return len;
+             if (len == 0)
+                     return -EINVAL;
+             if (len == sizeof(namebuf))
+                     return -ENAMETOOLONG;
+
+             if (kallsyms_lookup_names(namebuf, &start, &end)) {
+                     struct kallsyms_mod_find_ctx ctx = { .name = namebuf };
+
+                     err = module_kallsyms_on_each_symbol(NULL, kallsyms_mod_find_cb, &ctx);
+                     /* Check first: maps -EOPNOTSUPP (CONFIG_MODULES=n) to -ENOENT. */
+                     if (!ctx.found)
+                             return -ENOENT;
+                     if (err)
+                             return err;
+                     addr = ctx.addr;
+             } else {
+                     addr = kallsyms_sym_address(get_symbol_seq(start));
+                     for (i = start + 1; i <= end; i++) {
+                             if (kallsyms_sym_address(get_symbol_seq(i)) != addr)
+                                     return -EADDRNOTAVAIL;
+                     }
+             }
+
+             karg.sym_addr = addr;
+             break;
+     case KALLSYMS_QUERY_BY_ADDR:
+             if (!karg.sym_name_size)
+                     return -EINVAL;
+             addr = karg.sym_addr;
+             /* Reject addresses that do not fit in unsigned long (32-bit). */
+             if (addr != karg.sym_addr)
+                     return -ENOENT;
+             if (!kallsyms_lookup(addr, NULL, NULL, NULL, namebuf))
+                     return -ENOENT;
+
+             len = strlen(namebuf) + 1;
+             if (karg.sym_name_size < len)
+                     return -ENAMETOOLONG;
+             if (copy_to_user(u64_to_user_ptr(karg.sym_name_addr), namebuf, len))
+                     return -EFAULT;
+
+             karg.sym_name_size = len;
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize)))
+             return -EFAULT;
+
+     return 0;
+}
+
+static long kallsyms_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+     struct seq_file *m = file->private_data;
+
+     switch (_IOC_NR(cmd)) {
+     case _IOC_NR(KALLSYMS_QUERY):
+             if (!extensible_ioctl_valid(cmd, KALLSYMS_QUERY, KALLSYMS_QUERY_SIZE_VER0))
+                     return -ENOIOCTLCMD;
+             return do_kallsyms_query(m->private, (void __user *)arg);
+     default:
+             return -ENOIOCTLCMD;
+     }
+}
+
 static const struct proc_ops kallsyms_proc_ops = {
      .proc_open      = kallsyms_open,
      .proc_read      = seq_read,
      .proc_lseek     = seq_lseek,
      .proc_release   = seq_release_private,
+     .proc_ioctl     = kallsyms_ioctl,
+#ifdef CONFIG_COMPAT
+     .proc_compat_ioctl = compat_ptr_ioctl,
+#endif
 };
 
 static int __init kallsyms_init(void)  
author
#21 lore raw
Quoted 3 lines · David Laight

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 3 lines

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Only that the user might really want one of the copies. Sometimes there is only the copy.

Good point. I'll have v3 add KALLSYMS_F_PREFIX + pos/next_pos pagination to cover these. A prefix query for "foo." also finds the copies when plain foo does not exist (expanded on this in my reply to Andrii's Oct 30 mail in this thread).

[...]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Have you tried measuring it with my patches that speed up the kernel 'search by name' code?
That speeds the lookup by a factor of ~18 (testing the code in userspace).

Not yet. Can you point me to the latest version? Happy to try it out.

I've just forwarded you all the patches.
(They include patches to the tests and scripts/kallsyms) I'll copy you (and the other people getting this email) for v3.

Thanks for forwarding the patches! I'll be looking out for your v3. I tested them across baseline, baseline + my series, and baseline + my series + your series, using the same two benchmarks and setups as v2 patchseries:

Loading a BPF program with 11 typeless ksyms via libbpf (8-CPU x86-64 Cuttlefish VM, 273k /proc/kallsyms lines; BY_NAME, unfiltered, so each query also scans ~35.8k module symbols for ambiguity):
- Base: 156.1 ms
- Base + my v3 series: 2.05 ms
- Base + my v3 series + your v2 series: 1.95 ms

Perfetto symbolizer (8-CPU x86-64 VM, 175k /proc/kallsyms lines; BY_ADDR; init / 50k cached symbolizations):
- Base: 129.1 ms / 28.0 ms
- Base + my v3 series: 0.04 ms / 3.4 ms
- Base + my v3 series + your v2 series: 0.04 ms / 3.1 ms

I'm seeing your series help most when the vmlinux name search
dominates: on the Cuttlefish VM, a vmlinux-filtered BY_NAME call is 5-7x faster (hit: 8.3 us -> 1.7 us, miss: 9.5 us -> 1.4 us).

[...]

Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ __u64 query_flags; /* in - query mode (enum kallsyms_query_flags) */

There is no need for a command here - use the ioctl cmd.

Agreed, done in v3

#22 lore raw
Quoted 10 lines · On Tue, 3 Nov 2026 17:14:26 -0800 Samuel Wu <[email protected]> wrote:
Quoted 3 lines · David Laight

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 3 lines

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Quoted 7 lines
Quoted 2 lines

Only that the user might really want one of the copies. Sometimes there is only the copy.

Good point. I'll have v3 add KALLSYMS_F_PREFIX + pos/next_pos pagination to cover these. A prefix query for "foo." also finds the copies when plain foo does not exist (expanded on this in my reply to Andrii's Oct 30 mail in this thread).

I'm busy rebasing my patches - they got stamped all over :-( I should get a v3 that applies by the end of the week.

I will add a 'prefix' variant for you.
The 'next symbol matches' flag is exactly that (actually it is a this symbol is a duplicate to avoid issues at the start of the array). So in this case that application would always need to read the next symbol (by name).

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

David

author
#23 lore raw
Quoted 22 lines · David Laight
Quoted 10 lines · On Tue, 3 Nov 2026 17:14:26 -0800 Samuel Wu <[email protected]> wrote:
Quoted 3 lines · David Laight

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 3 lines

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Quoted 7 lines
Quoted 2 lines

Only that the user might really want one of the copies. Sometimes there is only the copy.

Good point. I'll have v3 add KALLSYMS_F_PREFIX + pos/next_pos pagination to cover these. A prefix query for "foo." also finds the copies when plain foo does not exist (expanded on this in my reply to Andrii's Oct 30 mail in this thread).

Quoted 8 lines

I'm busy rebasing my patches - they got stamped all over :-( I should get a v3 that applies by the end of the week.

I will add a 'prefix' variant for you.
The 'next symbol matches' flag is exactly that (actually it is a this symbol is a duplicate to avoid issues at the start of the array). So in this case that application would always need to read the next symbol (by name).

Thanks, that would be helpful. For my v3, I'll keep the lower_bound() approach so the two series stay independent, but I can switch over once your series lands, so there is only one search over the name table.

Quoted 2 lines

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

What pitfalls were you anticipating? If the current symbol's position is returned, userspace needs another ioctl() call and an -ENOENT check, to learn that there are no more matches. Alternatively, returning the next position means every call pays for a look-ahead, which for an unfiltered query walks the module symbols even when userspace only wants the first match.

Andrii, do you have a preference between the two?

#24 lore raw
Quoted 9 lines · On Wed, 4 Nov 2026 16:05:52 -0800 Samuel Wu <[email protected]> wrote:
Quoted 2 lines

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

What pitfalls were you anticipating? If the current symbol's position is returned, userspace needs another ioctl() call and an -ENOENT check, to learn that there are no more matches. Alternatively, returning the next position means every call pays for a look-ahead, which for an unfiltered query walks the module symbols even when userspace only wants the first match.

I was thinking more of the kernel api.
The 'position' is just an array index.
So a 'read by symbol name' would return the array index of that symbol. A 'read by symbol name index' would return the name (uncompressed) and do the lookup (binary search and linear scan) to find the value.

I've used a bit in the 'sorted by name array' to indicate that the symbol is a duplicate of the previous one.
So checking an 'exact match' for being a duplicate is cheap. But for a partial match you start needing the name buffer to be in-out on the read-by position request.
So you start needing 'read by position' and 'read by position with name compare'.
It is likely that the caller will need to validate the name anyway.

An 'interesting' idea would be an ioctl that returned the value of the nth symbol along with the name of the n+1th.
That might get confusing though.

David

Quoted 2 lines

Andrii, do you have a preference between the two?

author
#25 lore raw
Quoted 7 lines · David Laight
Quoted 3 lines · On Wed, 4 Nov 2026 16:05:52 -0800 Samuel Wu <[email protected]> wrote:
Quoted 2 lines

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

Quoted 8 lines
Quoted 6 lines

What pitfalls were you anticipating? If the current symbol's position is returned, userspace needs another ioctl() call and an -ENOENT check, to learn that there are no more matches. Alternatively, returning the next position means every call pays for a look-ahead, which for an unfiltered query walks the module symbols even when userspace only wants the first match.

I was thinking more of the kernel api.

Ahh ok, makes more sense now. I misunderstood your previous reply to be about the ioctl(), not the internal kernel functions.

Quoted 5 lines

The 'position' is just an array index.
So a 'read by symbol name' would return the array index of that symbol. A 'read by symbol name index' would return the name (uncompressed) and do the lookup (binary search and linear scan) to find the value.

Quoted 8 lines

I've used a bit in the 'sorted by name array' to indicate that the symbol is a duplicate of the previous one.
So checking an 'exact match' for being a duplicate is cheap. But for a partial match you start needing the name buffer to be in-out on the read-by position request.
So you start needing 'read by position' and 'read by position with name compare'.
It is likely that the caller will need to validate the name anyway.

Got it, thanks for clarifying! That works with QUERY_BY_NAME in v3:
- In the UAPI, the input query string (kallsyms_query_by_name.sym_name) and the output matched name (kallsyms_query_by_name ->kallsyms_sym.sym_name) are separate buffers, so the name buffer is not in-out.
- In kernel/kallsyms.c, the vmlinux lookup only needs the two helpers you described:
1. lookup by name/prefix -> index
2. index -> uncompressed name + address

Quoted 3 lines

An 'interesting' idea would be an ioctl that returned the value of the nth symbol along with the name of the n+1th.
That might get confusing though.

Agreed, it's simpler to have the ioctl() return a single symbol.

#26 lore raw
In reply to Samuel Wu
Quoted 42 lines · Samuel Wu
Quoted 22 lines · David Laight
Quoted 10 lines · On Tue, 3 Nov 2026 17:14:26 -0800 Samuel Wu <[email protected]> wrote:
Quoted 3 lines · David Laight

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 3 lines

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Quoted 7 lines
Quoted 2 lines

Only that the user might really want one of the copies. Sometimes there is only the copy.

Good point. I'll have v3 add KALLSYMS_F_PREFIX + pos/next_pos pagination to cover these. A prefix query for "foo." also finds the copies when plain foo does not exist (expanded on this in my reply to Andrii's Oct 30 mail in this thread).

Quoted 8 lines

I'm busy rebasing my patches - they got stamped all over :-( I should get a v3 that applies by the end of the week.

I will add a 'prefix' variant for you.
The 'next symbol matches' flag is exactly that (actually it is a this symbol is a duplicate to avoid issues at the start of the array). So in this case that application would always need to read the next symbol (by name).

Thanks, that would be helpful. For my v3, I'll keep the lower_bound() approach so the two series stay independent, but I can switch over once your series lands, so there is only one search over the name table.

Quoted 2 lines

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

Quoted 8 lines

What pitfalls were you anticipating? If the current symbol's position is returned, userspace needs another ioctl() call and an -ENOENT check, to learn that there are no more matches. Alternatively, returning the next position means every call pays for a look-ahead, which for an unfiltered query walks the module symbols even when userspace only wants the first match.

Andrii, do you have a preference between the two?

no perfect solution, but given you don't always care about duplicates, I'd probably return the found symbol's position, which can be used for further pagination (if necessary, user just does +1). so, let's return current symbol index, and have way to say "find by name/prefix 'blah' starting from position X", where X will be previous symbol index + 1?

but tbh, you have to write some code on how to do these "typical" queries and see whether API is convenient and meaningful, hard to know upfront what makes the most sense

author
#27 lore raw
Quoted 51 lines · Andrii Nakryiko
Quoted 49 lines · Samuel Wu
Quoted 22 lines · David Laight
Quoted 10 lines · On Tue, 3 Nov 2026 17:14:26 -0800 Samuel Wu <[email protected]> wrote:
Quoted 3 lines · David Laight

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <[email protected]> wrote:

[...]

Quoted 4 lines
Quoted 3 lines

BY_NAME matches names exactly, like kallsyms_lookup_name(), so they would be different symbols at different addresses. Please correct me if I'm missing something here!

Quoted 7 lines
Quoted 2 lines

Only that the user might really want one of the copies. Sometimes there is only the copy.

Good point. I'll have v3 add KALLSYMS_F_PREFIX + pos/next_pos pagination to cover these. A prefix query for "foo." also finds the copies when plain foo does not exist (expanded on this in my reply to Andrii's Oct 30 mail in this thread).

Quoted 8 lines

I'm busy rebasing my patches - they got stamped all over :-( I should get a v3 that applies by the end of the week.

I will add a 'prefix' variant for you.
The 'next symbol matches' flag is exactly that (actually it is a this symbol is a duplicate to avoid issues at the start of the array). So in this case that application would always need to read the next symbol (by name).

Thanks, that would be helpful. For my v3, I'll keep the lower_bound() approach so the two series stay independent, but I can switch over once your series lands, so there is only one search over the name table.

Quoted 2 lines

I think it might be more sensible (less pitfalls later on) to return the symbol number - not that of the next one.

What pitfalls were you anticipating? If the current symbol's position is returned, userspace needs another ioctl() call and an -ENOENT check, to learn that there are no more matches. Alternatively, returning the next position means every call pays for a look-ahead, which for an unfiltered query walks the module symbols even when userspace only wants the first match.

Quoted 7 lines
Quoted 1 line

Andrii, do you have a preference between the two?

no perfect solution, but given you don't always care about duplicates, I'd probably return the found symbol's position, which can be used for further pagination (if necessary, user just does +1). so, let's return current symbol index, and have way to say "find by name/prefix 'blah' starting from position X", where X will be previous symbol index + 1?

Ack, seems reasonable to me. Thanks Andrii

author
#28 lore raw

PATCH v2 2/6 tools: Copy KALLSYMS_QUERY definitions to linux/fs.h

In reply to Samuel Wu

Copy the KALLSYMS_QUERY ioctl definitions from include/uapi/linux/fs.h to tools/include/uapi/linux/fs.h for libbpf and the proc and bpf selftests. Only the new definitions are copied; other differences between the two headers are left alone.

---
 tools/include/uapi/linux/fs.h | 41 +++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
tools/include/uapi/linux/fs.h +41 -0
index f0a26309b6d5..d224516e6d6e 100644
@@ -567,4 +567,45 @@ struct procmap_query {
 	__u64 build_id_addr;		/* in */
 };
 
+/* /proc/kallsyms ioctl */
+#define KALLSYMS_QUERY	_IOWR(PROCFS_IOCTL_MAGIC, 18, struct kallsyms_query)
+
+/* Exactly one query mode must be set in query_flags. */
+enum kallsyms_query_flags {
+	/*
+	 * Look up a symbol in vmlinux or loaded modules (vmlinux first; BPF
+	 * and ftrace symbols are not searched) by exact NUL-terminated name
+	 * at sym_name_addr and return sym_addr. sym_addr and sym_name_size
+	 * must be 0 on input.
+	 */
+	KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+	/*
+	 * Look up the symbol covering sym_addr and write its NUL-terminated
+	 * name to the sym_name_addr buffer of sym_name_size bytes. The same
+	 * symbols as %pS are searched: vmlinux, loaded modules, BPF JIT
+	 * images, and freed module init functions still tracked by ftrace.
+	 * A buffer of KSYM_NAME_LEN (512 at the time of writing) bytes is
+	 * always enough. On success, sym_name_size is set to the number of
+	 * bytes written (including the terminating NUL).
+	 */
+	KALLSYMS_QUERY_BY_ADDR = 0x02,
+};
+
+/*
+ * Returns -ENOENT if no symbol matches, -EADDRNOTAVAIL if the name maps to
+ * more than one address, -ENAMETOOLONG if the name does not fit in
+ * KSYM_NAME_LEN (512 at the time of writing) or sym_name_size bytes, and
+ * -EPERM if kallsyms addresses are hidden from the opener of /proc/kallsyms
+ * (e.g. kptr_restrict).
+ */
+struct kallsyms_query {
+	__u64 size;		/* in - struct size for compatibility */
+	__u64 query_flags;	/* in - query mode (enum kallsyms_query_flags) */
+	__u64 sym_name_addr;	/* in - address of symbol name buffer */
+	__u64 sym_addr;		/* out (BY_SYMBOL) / in (BY_ADDR) */
+	__u64 sym_name_size;	/* in/out - name buffer size / bytes written */
+};
+
+#define KALLSYMS_QUERY_SIZE_VER0	40 /* sizeof first published struct */
+
 #endif /* _UAPI_LINUX_FS_H */
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author
#29 lore raw

PATCH v2 3/6 docs/procfs: Document KALLSYMS_QUERY ioctl on /proc/kallsyms

In reply to Samuel Wu

Document the existence of the KALLSYMS_QUERY ioctl() in the section describing the /proc/kallsyms file. Refer the user to the UAPI header for low-level details of this programmatic interface. Add a "kallsyms" row to the /proc file table.

---
 Documentation/filesystems/proc.rst | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Documentation/filesystems/proc.rst +15 -0
index c102b62023cd..a0e2fe14a01a 100644
@@ -761,6 +761,7 @@ files are there, and which are missing.
  ioports      I/O port usage
  irq 	      Masks for irq to cpu affinity			(2.4)(smp?)
  isapnp       ISA PnP (Plug&Play) Info				(2.4)
+ kallsyms     Kernel symbol table with dynamic queries (see text)
  kcore        Kernel core image (can be ELF or A.OUT(deprecated in 2.4))
  kmsg         Kernel messages
  ksyms        Kernel symbol table
@@ -1368,6 +1369,20 @@ Provides counts of softirq handlers serviced since boot time, for each CPU.
     HRTIMER:         0          0          0          0
 	RCU:      1678       1769       2178       2250
 
+
+kallsyms
+~~~~~~~~
+
+The /proc/kallsyms file lists all symbols in vmlinux and loaded modules.
+
+In addition to sequential text reading, /proc/kallsyms provides an ioctl()-based
+API (`KALLSYMS_QUERY`) for querying specific symbols and addresses. This
+interface is binary and is meant for efficient extraction of data without
+parsing the entire text file. `struct kallsyms_query`, defined in the
+linux/fs.h UAPI header, serves as the input/output argument to the
+`KALLSYMS_QUERY` ioctl() command. See comments in the linux/fs.h UAPI header
+for details on query semantics, supported flags, and return values.
+
 1.3 Networking info in /proc/net
 --------------------------------
 
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author
#30 lore raw

PATCH v2 4/6 libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

In reply to Samuel Wu

When resolving typeless extern ksyms (!ext->ksym.type_id), libbpf currently reads and parses the entire /proc/kallsyms text file via libbpf_kallsyms_parse(). The kallsyms text file is commonly and quite easily several MB, which means with 4kB page sizes, parsing the file requires a few thousand read() syscalls. This is inefficient, especially in the common case when only a few symbols are needed.

Add bpf_object__query_ksyms() to resolve typeless extern ksyms via the KALLSYMS_QUERY ioctl on /proc/kallsyms. Modules are still searched if a symbol is not found in vmlinux, and ambiguous symbols are still an error. The lookup follows kallsyms_lookup_name() semantics, so there are two differences from parsing the text file:
- A name that exists in both vmlinux and a module resolves to the vmlinux symbol, instead of failing as ambiguous.
- Names are matched exactly, so an extern does not match a <name>.llvm.<hash> symbol on CONFIG_LTO_CLANG kernels.

Fall back to reading and parsing the full text kallsyms only if the ioctl is not accessible: -ENOTTY on older kernels without the ioctl, or -EPERM if kallsyms addresses are hidden from the opener (e.g. kptr_restrict). A symbol that is not found is left unresolved, so a missing strong extern still fails with -ESRCH and a missing weak extern still defaults to zero, without a full text scan.

---
 tools/lib/bpf/libbpf.c | 78 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 76 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +76 -2
index 8fcb8cf39cc0..0e94d2335bd7 100644
@@ -31,6 +31,7 @@
 #include <linux/bpf.h>
 #include <linux/btf.h>
 #include <linux/filter.h>
+#include <linux/fs.h>
 #include <linux/limits.h>
 #include <linux/perf_event.h>
 #include <linux/bpf_perf_event.h>
@@ -59,6 +60,23 @@
 #define BPF_FS_MAGIC		0xcafe4a11
 #endif
 
+#ifndef KALLSYMS_QUERY
+/* 'f' corresponds to PROCFS_IOCTL_MAGIC (include/uapi/linux/fs.h) */
+#define KALLSYMS_QUERY		_IOWR('f', 18, struct kallsyms_query)
+
+enum kallsyms_query_flags {
+	KALLSYMS_QUERY_BY_SYMBOL = 0x01,
+};
+
+struct kallsyms_query {
+	__u64 size;
+	__u64 query_flags;
+	__u64 sym_name_addr;
+	__u64 sym_addr;
+	__u64 sym_name_size;
+};
+#endif
+
 #define MAX_EVENT_NAME_LEN	64
 
 #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
@@ -9644,8 +9662,64 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
 	return 0;
 }
 
-static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+	struct kallsyms_query q;
+	struct extern_desc *ext;
+	int err = 0;
+	int fd;
+	int i;
+
+	fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return -errno;
+
+	for (i = 0; i < obj->nr_extern; i++) {
+		ext = &obj->externs[i];
+		if (ext->type != EXT_KSYM || ext->ksym.type_id)
+			continue;
+
+		q = (struct kallsyms_query){
+			.size = sizeof(q),
+			.query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+			.sym_name_addr = ptr_to_u64(ext->name),
+		};
+
+		if (ioctl(fd, KALLSYMS_QUERY, &q) < 0) {
+			err = -errno;
+			if (err == -ENOENT) {
+				err = 0;
+				continue;
+			}
+			if (err == -EADDRNOTAVAIL)
+				pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+					ext->name);
+			break;
+		}
+		ext->is_set = true;
+		ext->ksym.addr = q.sym_addr;
+		pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+			 ext->name, ext->ksym.addr);
+	}
+	close(fd);
+	return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+	int err;
+
+	err = bpf_object__query_ksyms(obj);
+	/*
+	 * Fall back to parsing /proc/kallsyms only if the ioctl is not
+	 * accessible: not present (-ENOTTY on older kernels) or kallsyms
+	 * addresses hidden from this opener (-EPERM, e.g. kptr_restrict).
+	 */
+	if (err != -ENOTTY && err != -EPERM)
+		return err;
+
+	pr_debug("KALLSYMS_QUERY unavailable (%s), falling back to /proc/kallsyms\n",
+		 errstr(err));
 	return libbpf_kallsyms_parse(kallsyms_cb, obj);
 }
 
@@ -9922,7 +9996,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
 			return -EINVAL;
 	}
 	if (need_kallsyms) {
-		err = bpf_object__read_kallsyms_file(obj);
+		err = bpf_object__resolve_typeless_ksyms(obj);
 		if (err)
 			return -EINVAL;
 	}
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#31 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

TL;DR: - Possible regression: ioctl lookup ignores .llvm. suffixed symbols, so typeless ksyms may fail to load or resolve to 0 on LTO kernels - Fallback to kallsyms parsing may miss -EACCES…
Quoted 1 line

libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

TL;DR:
- Possible regression: ioctl lookup ignores .llvm. suffixed symbols, so typeless ksyms may fail to load or resolve to 0 on LTO kernels
- Fallback to kallsyms parsing may miss -EACCES (SELinux/seccomp), failing loads that worked before

Quoted 4 lines
tools/lib/bpf/libbpf.c +0 -0
index 8fcb8cf39cc0..0e94d2335bd7 100644

[ ... ]

Quoted 29 lines
tools/lib/bpf/libbpf.c +24 -1
@@ -9644,8 +9662,64 @@ static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
 	return 0;
 }
 
-static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+	struct kallsyms_query q;
+	struct extern_desc *ext;
+	int err = 0;
+	int fd;
+	int i;
+
+	fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return -errno;
+
+	for (i = 0; i < obj->nr_extern; i++) {
+		ext = &obj->externs[i];
+		if (ext->type != EXT_KSYM || ext->ksym.type_id)
+			continue;
+
+		q = (struct kallsyms_query){
+			.size = sizeof(q),
+			.query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+			.sym_name_addr = ptr_to_u64(ext->name),
+		};
+
+		if (ioctl(fd, KALLSYMS_QUERY, &q) < 0) {

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9698

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+			err = -errno;
+			if (err == -ENOENT) {
+				err = 0;
+				continue;
+			}
+			if (err == -EADDRNOTAVAIL)
+				pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+					ext->name);
+			break;
+		}

Can this regress typeless ksyms on CONFIG_LTO_CLANG kernels? The old kallsyms_cb() deliberately matched data symbols that carry a suffix:

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

That was added by commit c56e59776f46 ("libbpf: Handle <orig_name>.llvm.<hash> symbol properly") after test_ksyms failed on a CONFIG_LTO_CLANG_THIN kernel, where bpf_link_fops had been renamed to bpf_link_fops.llvm.<hash>. That commit also lists tk_core, fake_dst_ops and __fs_reclaim_map as real renamed symbols.

On the kernel side, KALLSYMS_QUERY only matches names exactly:
kallsyms_lookup_names() does a strcmp() on the expanded name, and kallsyms_mod_find_cb() also uses strcmp(). So a lookup for "bpf_link_fops" returns -ENOENT when only "bpf_link_fops.llvm.<hash>" exists.

Here -ENOENT is treated as "not found", err is cleared and the loop continues. bpf_object__resolve_typeless_ksyms() then falls back only on -ENOTTY or -EPERM, so libbpf_kallsyms_parse() is never reached. On a kernel that has this ioctl:

- a strong "extern const void foo __ksym;" now makes bpf_object__load() fail with "extern 'foo' (strong): not resolved" (-ESRCH), where it loaded before
- a weak one silently gets address 0 instead of the real address

The selftests helper kallsyms_find() in trace_helpers.c still strips ".llvm." from 'd' symbols, so it and libbpf now disagree about what test_ksyms (bpf_link_fops) should resolve to.

The commit message notes this difference, but gives no reason for dropping behavior that libbpf added on purpose. libbpf kept the .llvm. handling elsewhere to avoid regressions, and commit f8a05692de06 says "we'd like to not regress".

Would it work to fall back to libbpf_kallsyms_parse() for typeless externs that are still unresolved after the ioctl pass? That only costs extra time when a symbol is actually missing. Alternatively, the kernel query could match the .llvm. suffix for data symbols. I did not find a fix for this in the rest of the series.

Quoted 11 lines
tools/lib/bpf/libbpf.c +10 -0
+		ext->is_set = true;
+		ext->ksym.addr = q.sym_addr;
+		pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+			 ext->name, ext->ksym.addr);
+	}
+	close(fd);
+	return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9719

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+	int err;
+
+	err = bpf_object__query_ksyms(obj);
+	/*
+	 * Fall back to parsing /proc/kallsyms only if the ioctl is not
+	 * accessible: not present (-ENOTTY on older kernels) or kallsyms
+	 * addresses hidden from this opener (-EPERM, e.g. kptr_restrict).
+	 */
+	if (err != -ENOTTY && err != -EPERM)
+		return err;

Does this fallback miss other ways the ioctl can be blocked while reading /proc/kallsyms is still allowed?

The old code never issued an ioctl on this file. Now every KALLSYMS_QUERY goes through security_file_ioctl(). For this command selinux_file_ioctl() takes the default case and calls ioctl_has_perm(cred, file, FILE__IOCTL, (u16)cmd), which returns -EACCES when the policy grants read on the kallsyms label but not ioctl, or limits ioctl commands with allowxperm. A seccomp filter that makes ioctl() return an errno other than EPERM has the same effect.

In those setups bpf_object__query_ksyms() returns -EACCES (or similar), bpf_object__resolve_typeless_ksyms() returns it as-is, and bpf_object__resolve_externs() turns it into -EINVAL:

    err = bpf_object__resolve_typeless_ksyms(obj);
    if (err)
        return -EINVAL;

So an object with any typeless ksym now fails to load, even though parsing the text file would have worked as it did before this patch.

The failure is also hard to diagnose. Apart from -EADDRNOTAVAIL, nothing from the ioctl or from open() is reported. The open() failure path is a bare:

    fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
    if (fd < 0)
        return -errno;

without the "failed to open /proc/kallsyms" pr_warn() that libbpf_kallsyms_parse() prints, so the user only sees "failed to load object".

Should the fallback cover any error other than -EADDRNOTAVAIL (and other real lookup errors), or at least -EACCES as well, with a pr_warn() for the errors that are not handled?

---
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/39878338337

author
#32 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

On Thu, Oct 22, 2026 at 5:51 PM <[email protected]> wrote:

[...]

Quoted 5 lines

Would it work to fall back to libbpf_kallsyms_parse() for typeless externs that are still unresolved after the ioctl pass? That only costs extra time when a symbol is actually missing. Alternatively, the kernel query could match the .llvm. suffix for data symbols. I did not find a fix for this in the rest of the series.

Yes, for strong externs. In v3, if a strong extern returns -ENOENT, I'll fallback to /proc/kallsyms to scanning .llvm.<hash> suffix, keeping the same semantics as before.

Weak misses must default to zero without falling back to a scan. Otherwise, all objects marked __weak would pay the full /proc/kallsyms scanning cost. This is a slight semantic change, as a weak symbol with a ThinLTO-renamed static now resolves to zero. However, this is the behavior of the light skeleton (gen_loader.c), so perhaps it's acceptable. I'll note this in the v3 commit message.

[...]

Quoted 3 lines

Should the fallback cover any error other than -EADDRNOTAVAIL (and other real lookup errors), or at least -EACCES as well, with a pr_warn() for the errors that are not handled?

I'll update v3 to log pr_warn() and return on open() failure or lookup errors. And then I think I should reset partial state and fall back to /proc/kallsyms on ENOTTY, EPERM, EACCES, and EOPNOTSUPP.

#33 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

In reply to Samuel Wu

On Thu, Oct 22, 2026 at 05:16:05PM -0700, Samuel Wu wrote:

SNIP

Quoted 37 lines
tools/lib/bpf/libbpf.c +37 -0
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+	struct kallsyms_query q;
+	struct extern_desc *ext;
+	int err = 0;
+	int fd;
+	int i;
+
+	fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return -errno;
+
+	for (i = 0; i < obj->nr_extern; i++) {
+		ext = &obj->externs[i];
+		if (ext->type != EXT_KSYM || ext->ksym.type_id)
+			continue;
+
+		q = (struct kallsyms_query){
+			.size = sizeof(q),
+			.query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+			.sym_name_addr = ptr_to_u64(ext->name),
+		};
+
+			err = -errno;
+			if (err == -ENOENT) {
+				err = 0;
+				continue;
+			}
+			if (err == -EADDRNOTAVAIL)
+				pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+					ext->name);
+			break;
+		}
+		ext->is_set = true;
+		ext->ksym.addr = q.sym_addr;
+		pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+			 ext->name, ext->ksym.addr);

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9712

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -0
+	}
+	close(fd);
+	return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+	int err;
+
+	err = bpf_object__query_ksyms(obj);

hi,
we could also add similar support for libbpf_available_kallsyms_parse and probably in some other places like bpf_get_ksyms.. which makes me think it could be useful to have KALLSYMS_QUERY support for multiple symbols

thanks,
jirka

Quoted 25 lines
tools/lib/bpf/libbpf.c +11 -1
+	/*
+	 * Fall back to parsing /proc/kallsyms only if the ioctl is not
+	 * accessible: not present (-ENOTTY on older kernels) or kallsyms
+	 * addresses hidden from this opener (-EPERM, e.g. kptr_restrict).
+	 */
+	if (err != -ENOTTY && err != -EPERM)
+		return err;
+
+	pr_debug("KALLSYMS_QUERY unavailable (%s), falling back to /proc/kallsyms\n",
+		 errstr(err));
 	return libbpf_kallsyms_parse(kallsyms_cb, obj);
 }
@@ -9922,7 +9996,7 @@ static int bpf_object__resolve_externs(struct bpf_object *obj,
 			return -EINVAL;
 	}
 	if (need_kallsyms) {
-		err = bpf_object__read_kallsyms_file(obj);
+		err = bpf_object__resolve_typeless_ksyms(obj);
 		if (err)
 			return -EINVAL;
 	}
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author
#34 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

Quoted 58 lines · Jiri Olsa

On Thu, Oct 22, 2026 at 05:16:05PM -0700, Samuel Wu wrote:

SNIP

Quoted 37 lines
tools/lib/bpf/libbpf.c +37 -0
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+     struct kallsyms_query q;
+     struct extern_desc *ext;
+     int err = 0;
+     int fd;
+     int i;
+
+     fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+     if (fd < 0)
+             return -errno;
+
+     for (i = 0; i < obj->nr_extern; i++) {
+             ext = &obj->externs[i];
+             if (ext->type != EXT_KSYM || ext->ksym.type_id)
+                     continue;
+
+             q = (struct kallsyms_query){
+                     .size = sizeof(q),
+                     .query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+                     .sym_name_addr = ptr_to_u64(ext->name),
+             };
+
+                     err = -errno;
+                     if (err == -ENOENT) {
+                             err = 0;
+                             continue;
+                     }
+                     if (err == -EADDRNOTAVAIL)
+                             pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+                                     ext->name);
+                     break;
+             }
+             ext->is_set = true;
+             ext->ksym.addr = q.sym_addr;
+             pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+                      ext->name, ext->ksym.addr);

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9712

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -0
+     }
+     close(fd);
+     return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+     int err;
+
+     err = bpf_object__query_ksyms(obj);

hi,
we could also add similar support for libbpf_available_kallsyms_parse and probably in some other places like bpf_get_ksyms.. which makes me think it could be useful to have KALLSYMS_QUERY support for multiple symbols

Quoted 3 lines

thanks,
jirka

Thanks for lookiing over this Jiri!

Using KALLSYMS_QUERY for libbpf_available_kallsyms_parse() would theoretically by O(m*log(n)) instead of O(n*log(m)), where m is the number of tracefs symbols, and n is lines in kallsyms, with n >> m. But AFAICT, libbpf_available_kallsyms_parse() would only be called in pre-6.5 kernels, for kernels without commit 83f74441 (ftrace: Show all functions with addresses in available_filter_functions_addrs); so this ioctl could never be called.

And regarding bpf_get_ksyms(), it loads all traceable functions and needs the full syms table, so a /proc/kallsyms should fits better.

I did profile a slightly different form of querying multiple symbols at once [1], but didn't see much benefit. struct kallsyms_query is extensible via size and query_flags, so we can update in the future as needed.

[1]: https://lore.kernel.org/all/CAG2KctozPSev+EXSpHitM3JWfjufQaEjsPJHS_1HJ1wSm7sJ1w@mail.gmail.com/

[...]

#35 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

Quoted 65 lines · On Tue, 27 Oct 2026 15:07:44 -0700 Samuel Wu <[email protected]> wrote:
Quoted 58 lines · Jiri Olsa

On Thu, Oct 22, 2026 at 05:16:05PM -0700, Samuel Wu wrote:

SNIP

Quoted 37 lines
tools/lib/bpf/libbpf.c +37 -0
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+     struct kallsyms_query q;
+     struct extern_desc *ext;
+     int err = 0;
+     int fd;
+     int i;
+
+     fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+     if (fd < 0)
+             return -errno;
+
+     for (i = 0; i < obj->nr_extern; i++) {
+             ext = &obj->externs[i];
+             if (ext->type != EXT_KSYM || ext->ksym.type_id)
+                     continue;
+
+             q = (struct kallsyms_query){
+                     .size = sizeof(q),
+                     .query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+                     .sym_name_addr = ptr_to_u64(ext->name),
+             };
+
+                     err = -errno;
+                     if (err == -ENOENT) {
+                             err = 0;
+                             continue;
+                     }
+                     if (err == -EADDRNOTAVAIL)
+                             pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+                                     ext->name);
+                     break;
+             }
+             ext->is_set = true;
+             ext->ksym.addr = q.sym_addr;
+             pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+                      ext->name, ext->ksym.addr);

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9712

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -0
+     }
+     close(fd);
+     return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+     int err;
+
+     err = bpf_object__query_ksyms(obj);  

hi,
we could also add similar support for libbpf_available_kallsyms_parse and probably in some other places like bpf_get_ksyms.. which makes me think it could be useful to have KALLSYMS_QUERY support for multiple symbols

Quoted 3 lines

thanks,
jirka

Thanks for lookiing over this Jiri!

Quoted 7 lines

Using KALLSYMS_QUERY for libbpf_available_kallsyms_parse() would theoretically by O(m*log(n)) instead of O(n*log(m)), where m is the number of tracefs symbols, and n is lines in kallsyms, with n >> m. But AFAICT, libbpf_available_kallsyms_parse() would only be called in pre-6.5 kernels, for kernels without commit 83f74441 (ftrace: Show all functions with addresses in available_filter_functions_addrs); so this ioctl could never be called.

Isn't that code also O(n*log(m)) ?
I've got a patch (I might send it after the merge window) that changes one of the ftrace functions to look up every symbol by name, not just when only asked to look up one.
It can also use the 'callback' function to detect duplicates. That should be massively faster - especially with my ~18 fold improvement (also waiting for the merge window to end before posting a new (not much changed) version).

Does look the the code could do with getting the 'symbol name table index' for the symbol so that it can read the entries sequentially from that point onwards.

David

Quoted 13 lines

And regarding bpf_get_ksyms(), it loads all traceable functions and needs the full syms table, so a /proc/kallsyms should fits better.

I did profile a slightly different form of querying multiple symbols at once [1], but didn't see much benefit. struct kallsyms_query is extensible via size and query_flags, so we can update in the future as needed.

[1]: https://lore.kernel.org/all/CAG2KctozPSev+EXSpHitM3JWfjufQaEjsPJHS_1HJ1wSm7sJ1w@mail.gmail.com/

[...]

author
#36 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

Quoted 77 lines · David Laight
Quoted 65 lines · On Tue, 27 Oct 2026 15:07:44 -0700 Samuel Wu <[email protected]> wrote:
Quoted 58 lines · Jiri Olsa

On Thu, Oct 22, 2026 at 05:16:05PM -0700, Samuel Wu wrote:

SNIP

Quoted 37 lines
tools/lib/bpf/libbpf.c +37 -0
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+     struct kallsyms_query q;
+     struct extern_desc *ext;
+     int err = 0;
+     int fd;
+     int i;
+
+     fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+     if (fd < 0)
+             return -errno;
+
+     for (i = 0; i < obj->nr_extern; i++) {
+             ext = &obj->externs[i];
+             if (ext->type != EXT_KSYM || ext->ksym.type_id)
+                     continue;
+
+             q = (struct kallsyms_query){
+                     .size = sizeof(q),
+                     .query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+                     .sym_name_addr = ptr_to_u64(ext->name),
+             };
+
+                     err = -errno;
+                     if (err == -ENOENT) {
+                             err = 0;
+                             continue;
+                     }
+                     if (err == -EADDRNOTAVAIL)
+                             pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+                                     ext->name);
+                     break;
+             }
+             ext->is_set = true;
+             ext->ksym.addr = q.sym_addr;
+             pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+                      ext->name, ext->ksym.addr);

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9712

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -0
+     }
+     close(fd);
+     return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+     int err;
+
+     err = bpf_object__query_ksyms(obj);

hi,
we could also add similar support for libbpf_available_kallsyms_parse and probably in some other places like bpf_get_ksyms.. which makes me think it could be useful to have KALLSYMS_QUERY support for multiple symbols

Quoted 3 lines

thanks,
jirka

Thanks for lookiing over this Jiri!

Quoted 7 lines

Using KALLSYMS_QUERY for libbpf_available_kallsyms_parse() would theoretically by O(m*log(n)) instead of O(n*log(m)), where m is the number of tracefs symbols, and n is lines in kallsyms, with n >> m. But AFAICT, libbpf_available_kallsyms_parse() would only be called in pre-6.5 kernels, for kernels without commit 83f74441 (ftrace: Show all functions with addresses in available_filter_functions_addrs); so this ioctl could never be called.

Quoted 1 line

Isn't that code also O(n*log(m)) ?

Yes, sorry maybe I wasn't clear before, but the current code is O(n*log(m)), and the ioctl approach would be better since it's m lookups of O(log(n)) for vmlinux symbols, where n >> m. However, this is a red herring because that path only runs on pre-6.5 kernels, which won't have this ioctl.

Quoted 7 lines

I've got a patch (I might send it after the merge window) that changes one of the ftrace functions to look up every symbol by name, not just when only asked to look up one.
It can also use the 'callback' function to detect duplicates. That should be massively faster - especially with my ~18 fold improvement (also waiting for the merge window to end before posting a new (not much changed) version).

Excellent, I'll keep an eye out for it!

Quoted 20 lines

Does look the the code could do with getting the 'symbol name table index' for the symbol so that it can read the entries sequentially from that point onwards.

David

Quoted 13 lines

And regarding bpf_get_ksyms(), it loads all traceable functions and needs the full syms table, so a /proc/kallsyms should fits better.

I did profile a slightly different form of querying multiple symbols at once [1], but didn't see much benefit. struct kallsyms_query is extensible via size and query_flags, so we can update in the future as needed.

[1]: https://lore.kernel.org/all/CAG2KctozPSev+EXSpHitM3JWfjufQaEjsPJHS_1HJ1wSm7sJ1w@mail.gmail.com/

[...]

#37 lore raw

Re: [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl

Quoted 79 lines · On Thu, 29 Oct 2026 14:35:36 -0700 Samuel Wu <[email protected]> wrote:
Quoted 77 lines · David Laight
Quoted 72 lines · On Tue, 27 Oct 2026 15:07:44 -0700 Samuel Wu <[email protected]> wrote:
Quoted 58 lines · Jiri Olsa

On Thu, Oct 22, 2026 at 05:16:05PM -0700, Samuel Wu wrote:

SNIP

Quoted 37 lines
tools/lib/bpf/libbpf.c +37 -0
+static int bpf_object__query_ksyms(struct bpf_object *obj)
+{
+     struct kallsyms_query q;
+     struct extern_desc *ext;
+     int err = 0;
+     int fd;
+     int i;
+
+     fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+     if (fd < 0)
+             return -errno;
+
+     for (i = 0; i < obj->nr_extern; i++) {
+             ext = &obj->externs[i];
+             if (ext->type != EXT_KSYM || ext->ksym.type_id)
+                     continue;
+
+             q = (struct kallsyms_query){
+                     .size = sizeof(q),
+                     .query_flags = KALLSYMS_QUERY_BY_SYMBOL,
+                     .sym_name_addr = ptr_to_u64(ext->name),
+             };
+
+                     err = -errno;
+                     if (err == -ENOENT) {
+                             err = 0;
+                             continue;
+                     }
+                     if (err == -EADDRNOTAVAIL)
+                             pr_warn("extern (ksym) '%s': resolution is ambiguous\n",
+                                     ext->name);
+                     break;
+             }
+             ext->is_set = true;
+             ext->ksym.addr = q.sym_addr;
+             pr_debug("extern (ksym) '%s': resolved in kernel to 0x%llx\n",
+                      ext->name, ext->ksym.addr);

tools/lib/bpf/libbpf.c static int kallsyms_cb(unsigned long long sym_addr, char sym_type, · line 9712

Quoted 10 lines
tools/lib/bpf/libbpf.c +9 -0
+     }
+     close(fd);
+     return err;
+}
+
+static int bpf_object__resolve_typeless_ksyms(struct bpf_object *obj)
 {
+     int err;
+
+     err = bpf_object__query_ksyms(obj);  

hi,
we could also add similar support for libbpf_available_kallsyms_parse and probably in some other places like bpf_get_ksyms.. which makes me think it could be useful to have KALLSYMS_QUERY support for multiple symbols

Quoted 3 lines

thanks,
jirka

Thanks for lookiing over this Jiri!

Using KALLSYMS_QUERY for libbpf_available_kallsyms_parse() would theoretically by O(m*log(n)) instead of O(n*log(m)), where m is the number of tracefs symbols, and n is lines in kallsyms, with n >> m. But AFAICT, libbpf_available_kallsyms_parse() would only be called in pre-6.5 kernels, for kernels without commit 83f74441 (ftrace: Show all functions with addresses in available_filter_functions_addrs); so this ioctl could never be called.

Quoted 7 lines
Quoted 1 line

Isn't that code also O(n*log(m)) ?

Yes, sorry maybe I wasn't clear before, but the current code is O(n*log(m)), and the ioctl approach would be better since it's m lookups of O(log(n)) for vmlinux symbols, where n >> m. However, this is a red herring because that path only runs on pre-6.5 kernels, which won't have this ioctl.

I was thinking of the ftrace_lookup_symbols() code (called from bbf_kprobe_multi_link_attach) which O(n*log(m)) (the wrong way around).

David

Quoted 31 lines
Quoted 8 lines

I've got a patch (I might send it after the merge window) that changes one of the ftrace functions to look up every symbol by name, not just when only asked to look up one.
It can also use the 'callback' function to detect duplicates. That should be massively faster - especially with my ~18 fold improvement (also waiting for the merge window to end before posting a new (not much changed) version).

Excellent, I'll keep an eye out for it!

Quoted 20 lines

Does look the the code could do with getting the 'symbol name table index' for the symbol so that it can read the entries sequentially from that point onwards.

David

Quoted 13 lines

And regarding bpf_get_ksyms(), it loads all traceable functions and needs the full syms table, so a /proc/kallsyms should fits better.

I did profile a slightly different form of querying multiple symbols at once [1], but didn't see much benefit. struct kallsyms_query is extensible via size and query_flags, so we can update in the future as needed.

[1]: https://lore.kernel.org/all/CAG2KctozPSev+EXSpHitM3JWfjufQaEjsPJHS_1HJ1wSm7sJ1w@mail.gmail.com/

[...]

author
#38 lore raw

PATCH v2 5/6 selftests/bpf: Test typeless module ksym resolution

In reply to Samuel Wu

Typeless module ksyms are resolved through kallsyms, but the existing module ksym tests (ksyms_module, ksym_race) only use typed ksyms, which are resolved through BTF. With KALLSYMS_QUERY, libbpf resolves module symbols through the module lookup path of the ioctl.

Add a weak typeless extern for bpf_testmod_ksym_percpu to the ksyms test and check that its address matches /proc/kallsyms when bpf_testmod is loaded.

---
 tools/testing/selftests/bpf/prog_tests/ksyms.c | 7 +++++++
 tools/testing/selftests/bpf/progs/test_ksyms.c | 3 +++
 2 files changed, 10 insertions(+)
tools/testing/selftests/bpf/prog_tests/ksyms.c +7 -0
index dc7aab532fb1..62760284f746 100644
@@ -11,6 +11,7 @@ void test_ksyms(void)
 	struct test_ksyms *skel;
 	struct test_ksyms__data *data;
 	__u64 link_fops_addr, per_cpu_start_addr;
+	__u64 testmod_ksym_addr;
 	struct stat st;
 	__u64 btf_size;
 	int err;
@@ -48,6 +49,12 @@ void test_ksyms(void)
 	ASSERT_EQ(data->out__btf_size, btf_size, "btf_size");
 	ASSERT_EQ(data->out__per_cpu_start, per_cpu_start_addr, "__per_cpu_start");
 
+	if (env.has_testmod &&
+	    ASSERT_OK(kallsyms_find("bpf_testmod_ksym_percpu", &testmod_ksym_addr),
+		      "bpf_testmod_ksym_percpu: kallsyms_find"))
+		ASSERT_EQ(data->out__bpf_testmod_ksym, testmod_ksym_addr,
+			  "bpf_testmod_ksym_percpu");
+
 cleanup:
 	test_ksyms__destroy(skel);
 }
tools/testing/selftests/bpf/progs/test_ksyms.c +3 -0
index 6c9cbb5a3bdf..69309f2a2d0a 100644
@@ -9,6 +9,7 @@ __u64 out__bpf_link_fops = -1;
 __u64 out__bpf_link_fops1 = -1;
 __u64 out__btf_size = -1;
 __u64 out__per_cpu_start = -1;
+__u64 out__bpf_testmod_ksym = -1;
 
 extern const void bpf_link_fops __ksym;
 extern const void __start_BTF __ksym;
@@ -16,6 +17,7 @@ extern const void __stop_BTF __ksym;
 extern const void __per_cpu_start __ksym;
 /* non-existing symbol, weak, default to zero */
 extern const void bpf_link_fops1 __ksym __weak;
+extern const void bpf_testmod_ksym_percpu __ksym __weak;
 
 SEC("raw_tp/sys_enter")
 int handler(const void *ctx)
@@ -25,6 +27,7 @@ int handler(const void *ctx)
 	out__per_cpu_start = (__u64)&__per_cpu_start;
 
 	out__bpf_link_fops1 = (__u64)&bpf_link_fops1;
+	out__bpf_testmod_ksym = (__u64)&bpf_testmod_ksym_percpu;
 
 	return 0;
 }
Signature · 1 line
2.56.0.257.gf7497c7d27-goog
author
#39 lore raw

PATCH v2 6/6 selftests/proc: Add KALLSYMS_QUERY tests

In reply to Samuel Wu

Add a selftest for the KALLSYMS_QUERY ioctl() on /proc/kallsyms.

For symbol-to-address lookup (KALLSYMS_QUERY_BY_SYMBOL), cover valid lookups, missing symbols (-ENOENT), input validation (-EINVAL, -EFAULT, -ENAMETOOLONG, -E2BIG), and extensible struct size handling.

For address-to-symbol lookup (KALLSYMS_QUERY_BY_ADDR), cover lookups at exact symbol start and interior offsets, buffer truncation (-ENAMETOOLONG), unmapped addresses (-ENOENT), and input validation (-EINVAL, -EFAULT).

---
 tools/testing/selftests/proc/.gitignore       |   1 +
 tools/testing/selftests/proc/Makefile         |   1 +
 .../selftests/proc/proc-kallsyms-query.c      | 213 ++++++++++++++++++
 3 files changed, 215 insertions(+)
 create mode 100644 tools/testing/selftests/proc/proc-kallsyms-query.c
tools/testing/selftests/proc/.gitignore +1 -0
index 9c9735570abf..1762c28f524c 100644
@@ -4,6 +4,7 @@
 /fd-003-kthread
 /proc-2-is-kthread
 /proc-fsconfig-hidepid
+/proc-kallsyms-query
 /proc-loadavg-001
 /proc-maps-race
 /proc-multiple-procfs
tools/testing/selftests/proc/Makefile +1 -0
index a7de2bb6d8be..dc3c28e6b83b 100644
@@ -29,6 +29,7 @@ TEST_GEN_PROGS += setns-sysvipc
 TEST_GEN_PROGS += thread-self
 TEST_GEN_PROGS += proc-multiple-procfs
 TEST_GEN_PROGS += proc-fsconfig-hidepid
+TEST_GEN_PROGS += proc-kallsyms-query
 TEST_GEN_PROGS += proc-pidns
 
 include ../lib.mk
tools/testing/selftests/proc/proc-kallsyms-query.c new file +213 -0

213 lines not on this page · raw message

Signature · 1 line
2.56.0.257.gf7497c7d27-goog
#40 lore raw

Re: [PATCH bpf-next v2 6/6] selftests/proc: Add KALLSYMS_QUERY tests

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/proc: main() bypasses kselftest reporting and ksft_exit_*() wrappers - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/proc: main() bypasses kselftest reporting and ksft_exit_*() wrappers
- [Medium] selftests/proc: main() skips silently with bare KSFT_SKIP and no reason message --

commit 309c42ade27ce014cdfb65d22ab97d40bf7dcd32
Author: Samuel Wu <[email protected]>

selftests/proc: Add KALLSYMS_QUERY tests

This commit adds a selftest in tools/testing/selftests/proc/proc-kallsyms-query.c to exercise symbol-to-address and address-to-symbol lookups via the kallsyms_query ioctl on /proc/kallsyms.

Quoted 5 lines
tools/testing/selftests/proc/proc-kallsyms-query.c new file +0 -0
new file mode 100644
index 0000000000000..229cde432d6aa

[ ... ]

Quoted 22 lines
tools/testing/selftests/proc/proc-kallsyms-query.c new file +22 -0
+int main(void)
+{
+	const char *sym_name = "vfs_read";
+	char resolved_name[KSYM_NAME_LEN];
+	char long_name[KSYM_NAME_LEN + 1];
+	size_t page_size = sysconf(_SC_PAGESIZE);
+	unsigned long long exp_addr;
+	struct kallsyms_query_ext q1;
+	struct kallsyms_query q;
+	char *page_buf;
+	int err;
+	int fd;
+
+	fd = open("/proc/kallsyms", O_RDONLY | O_CLOEXEC);
+	if (fd < 0) {
+		if (errno == ENOENT)
+			return KSFT_SKIP;
+		ksft_exit_fail_msg("open(/proc/kallsyms) failed: %s\n",
+				   strerror(errno));
+	}
+
+	if (!find_vmlinux_symbol(sym_name, &exp_addr)) {

tools/testing/selftests/proc/proc-kallsyms-query.c line 114

Quoted 10 lines
tools/testing/selftests/proc/proc-kallsyms-query.c new file +10 -0
+		close(fd);
+		return KSFT_SKIP;
+	}
+
+	/* CASE 1: Valid vmlinux symbol query (or -EPERM when addresses are hidden) */
+	err = query_symbol(fd, sym_name, &q);
+	if (err == -ENOTTY) {
+		close(fd);
+		return KSFT_SKIP;
+	}

[Severity: Medium]
In main() in tools/testing/selftests/proc/proc-kallsyms-query.c, when opening /proc/kallsyms fails because the file does not exist, when find_vmlinux_symbol() does not find vfs_read, or when query_symbol() fails because the ioctl is not supported by the running kernel, the function returns directly without printing a skip message.

Unlike the hidden-address check right below that calls ksft_exit_skip() with a reason string, returning silently here leaves test logs with no indication of which prerequisite was missing.

Should these three skip paths call ksft_exit_skip() with a descriptive reason message instead?

Quoted 7 lines
tools/testing/selftests/proc/proc-kallsyms-query.c new file +7 -0
+	if (!exp_addr) {
+		assert(err == -EPERM);
+		close(fd);
+		ksft_exit_skip("kallsyms addresses are hidden, only -EPERM checked\n");
+	}
+	assert(err == 0);
+	assert(q.sym_addr == exp_addr);

[ ... ]

tools/testing/selftests/proc/proc-kallsyms-query.c line 213

Quoted 7 lines
tools/testing/selftests/proc/proc-kallsyms-query.c new file +7 -0
+	/* CASE 16: KALLSYMS_QUERY_BY_ADDR invalid inputs return -EINVAL or -EFAULT */
+	assert(query_addr(fd, exp_addr, resolved_name, 0, &q) == -EINVAL);
+	assert(query_addr(fd, exp_addr, NULL, sizeof(resolved_name), &q) == -EFAULT);
+
+	close(fd);
+	return 0;
+}

[Severity: Medium]
Although tools/testing/selftests/proc/proc-kallsyms-query.c includes kselftest.h and uses ksft_exit_fail_msg() and ksft_exit_skip() in main(), it never calls ksft_print_header() or ksft_set_plan(), evaluates all 16 test cases with libc assert(), and exits main() with a bare return 0.

When run under kselftest runners or automated test parsers, this produces no plan, per-test result lines, or summary output on completion, and aborts the process via assert() on failure instead of reporting structured test failure results.

Could main() use the kselftest.h reporting helpers (such as ksft_print_header(), ksft_set_plan(), ksft_test_result(), and ksft_exit_pass() or ksft_finished()) or the kselftest_harness.h macros instead of raw assert() and return 0?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#41 lore raw

Re: [PATCH bpf-next v2 6/6] selftests/proc: Add KALLSYMS_QUERY tests

On Thu, Oct 22, 2026 at 5:25 PM <[email protected]> wrote:

[ ... ]

Quoted 2 lines

Should these three skip paths call ksft_exit_skip() with a descriptive reason message instead?

Yes, I'll update v3 to use ksft_exit_skip() in the three skip paths.

Quoted 4 lines

Could main() use the kselftest.h reporting helpers (such as ksft_print_header(), ksft_set_plan(), ksft_test_result(), and ksft_exit_pass() or ksft_finished()) or the kselftest_harness.h macros instead of raw assert() and return 0?

I kept assert() to match existing tests in selftests/proc/, but I have no problem switching to these helpers if maintainers prefer that.