Re: [PATCH bpf-next v3 3/9] kallsyms: Add KALLSYMS_QUERY_BY_NAME ioctl to /proc/kallsyms

From: Andrii Nakryiko

Date: Wed Nov 25 2026 - 15:25:51 EST


On Fri, Nov 13, 2026 at 2:14 PM Samuel Wu <wusamuel@xxxxxxxxxx> wrote:
>
> To find a few kernel symbols, users such as libbpf parse all of
> /proc/kallsyms, which is several MB and text based.
>
> Add the KALLSYMS_QUERY_BY_NAME ioctl. It finds a symbol by exact name or
> by prefix in the entries of /proc/kallsyms, with vmlinux matches first,
> and optionally in a single module with module_filter, or all modules by
> default. Each call returns one match and its position; pos + 1 continues
> the search, until -ENOENT. The output returns the address, the size, and
> optional name and module buffers. A buffer that is too small gives
> -ENAMETOOLONG and the required size.
>
> vmlinux symbols are found with a lower-bound binary search on the sorted
> names, module symbols in one pass, and ftrace, BPF, and kprobe symbols
> with the per-index getters that read() uses.
>
> For versioning, the struct carries their sizes, so the command does not
> change when the struct grows.
>
> The command uses 'f' 0x30; register 0x30 in ioctl-number.rst.
>
> Suggested-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
> Signed-off-by: Samuel Wu <wusamuel@xxxxxxxxxx>
> ---
> .../userspace-api/ioctl/ioctl-number.rst | 1 +
> include/uapi/linux/fs.h | 43 +++
> kernel/kallsyms.c | 319 ++++++++++++++++++
> 3 files changed, 363 insertions(+)
>
> diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
> index fbc3537fc9ee..f5dbb597257a 100644
> --- a/Documentation/userspace-api/ioctl/ioctl-number.rst
> +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
> @@ -250,6 +250,7 @@ Code Seq# Include File Comments
> 'f' 00-0F fs/ocfs2/ocfs2_fs.h conflict!
> 'f' 10-11 linux/fs.h conflict! procfs ioctls (PROCFS_IOCTL_MAGIC)
> 'f' 13-27 linux/fscrypt.h
> +'f' 30 linux/fs.h procfs kallsyms ioctls (PROCFS_IOCTL_MAGIC)
> 'f' 81-8F linux/fsverity.h
> 'g' 00-0F linux/usb/gadgetfs.h
> 'g' 20-2F linux/usb/g_printer.h
> diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
> index a46c33692aa2..987569dcdbea 100644
> --- a/include/uapi/linux/fs.h
> +++ b/include/uapi/linux/fs.h
> @@ -674,6 +674,49 @@ struct procmap_query {
> __u64 build_id_addr; /* in */
> };
>
> +/* /proc/kallsyms ioctls. -EPERM if the opener cannot see kernel addresses. */
> +#define KALLSYMS_QUERY_BY_NAME _IOC(_IOC_READ | _IOC_WRITE, PROCFS_IOCTL_MAGIC, 48, 0)
> +
> +enum kallsyms_query_flags {
> + /* Match query_name as a prefix. */
> + KALLSYMS_QUERY_NAME_PREFIX = 0x01,
> +};
> +
> +/*
> + * Symbol returned by a query. Each buffer pointer and its *_len must both be
> + * set or both be 0. When set, the kernel sets *_len to the string size
> + * including the NUL, even if the buffer is too small and the ioctl fails with
> + * -ENAMETOOLONG. The module name of a vmlinux symbol is "".
> + */
> +struct kallsyms_query_sym {
> + __u64 sym_addr; /* out - start address */
> + __u64 sym_size; /* out - bytes to next symbol, 0 if unknown */
> + __u64 sym_name; /* in - user ptr to name buffer, or 0 */
> + __u64 sym_name_len; /* in/out - buffer size / string size */
> + __u64 sym_module; /* in - user ptr to module name buffer, or 0 */
> + __u64 sym_module_len; /* in/out - buffer size / string size */
> +};
> +
> +/*
> + * Find a symbol by exact name, or by prefix with KALLSYMS_QUERY_NAME_PREFIX,
> + * in the entries of /proc/kallsyms, except undefined module symbols. vmlinux
> + * matches come first. A module_filter of "vmlinux" selects the core kernel.
> + *
> + * Each call returns one match and sets pos to its position, even on
> + * -ENAMETOOLONG. Start with pos 0 and use pos + 1 for the next match, until
> + * -ENOENT. Use pos only with the same query. Matches can be skipped or
> + * repeated if symbols change between calls.
> + */
> +struct kallsyms_query_by_name {
> + __u64 size; /* in - sizeof(struct kallsyms_query_by_name) */
> + __u64 query_flags; /* in - enum kallsyms_query_flags */
> + __u64 query_name; /* in - user ptr to name or prefix */
> + __u64 module_filter; /* in - user ptr to loaded module name, or 0 */

similar to the comment on the previous patch, how does the user
express that it only wants to search vmlinux image and not consult
module?

and naming nit: query_module to be consistent with "query_name"?

> + __u64 pos; /* in/out - start position / match position */
> + __u64 sym; /* in - user ptr to struct kallsyms_query_sym */
> + __u64 sym_struct_size; /* in - sizeof(struct kallsyms_query_sym) */

I'd name it "sym_size" for consistency with "sym" naming, and the
comment/doc here is unambiguous about what that is (struct size, not
the symbol's recorded size). seems fine as far as UX goes


other than nits, this API looks great

[...]

> +/* On -ENAMETOOLONG, *ksym is still copied, to return the required sizes. */
> +static int kallsyms_copy_sym_to_user(u64 usym, u64 usym_size,
> + struct kallsyms_query_sym *ksym,
> + const struct kallsyms_sym_match *m)
> +{
> + size_t copy_sz = min_t(size_t, usym_size, sizeof(*ksym));
> + bool too_long = false;
> + int err;
> +
> + ksym->sym_addr = m->addr;
> + ksym->sym_size = m->size;
> + err = kallsyms_copy_str_to_user(ksym->sym_name, &ksym->sym_name_len,
> + m->name);
> + if (err == -EFAULT)
> + return -EFAULT;
> + too_long |= (err == -ENAMETOOLONG);

nit: if (err == -ENAMETOOLONG) too_long = true; is just as short

but also you are eating any other error, maybe write it so that
original err is preserved as is, not special-casing ENAMETOOLONG? if
you get EFAULT later, original error still takes precedence

> +
> + err = kallsyms_copy_str_to_user(ksym->sym_module, &ksym->sym_module_len,
> + m->modname);
> + if (err == -EFAULT)
> + return -EFAULT;
> + too_long |= (err == -ENAMETOOLONG);
> +
> + if (copy_to_user(u64_to_user_ptr(usym), ksym, copy_sz))
> + return -EFAULT;
> +
> + return too_long ? -ENAMETOOLONG : 0;
> +}
> +

[...]