Linux Documentation
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Samuel Wu <wusamuel@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	kernel-team@android.com, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org,
	bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
	llvm@lists.linux.dev
Subject: Re: [PATCH bpf-next v2 1/6] kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms
Date: Fri, 30 Oct 2026 10:02:15 +0000	[thread overview]
Message-ID: <20261030100215.6f8e0d4f@pumpkin> (raw)
In-Reply-To: <CAG2KctpGPHo-S3AEvBhK5HPUnDHfoJu6Z-r+cS1hQ4Ps=B3DmQ@mail.gmail.com>

On Thu, 29 Oct 2026 15:05:45 -0700
Samuel Wu <wusamuel@google.com> wrote:

> On Wed, Oct 28, 2026 at 6:40 AM David Laight
> <david.laight.linux@gmail.com> wrote:
> >
> > On Thu, 22 Oct 2026 17:16:02 -0700
> > Samuel Wu <wusamuel@google.com> 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")  
> >
> > 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.

> 
> > > - 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.  
> 
> 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.

> > >
> > > 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).  
> >
> > 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.

> 
> > 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.

> 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.

> 
> > (Basically it needs to 'return -index' instead of 'return -1' when the
> > binary scan fails.)
> >
> >  
> > >
> > > 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)
> > >
> > > Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> > > Signed-off-by: Samuel Wu <wusamuel@google.com>
> > > ---
> > >  include/uapi/linux/fs.h |  41 +++++++++++++
> > >  kernel/kallsyms.c       | 132 ++++++++++++++++++++++++++++++++++++++++
> > >  2 files changed, 173 insertions(+)
> > >
> > > diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
> > > index 34c6f219462a..b43f227dad0e 100644
> > > --- a/include/uapi/linux/fs.h
> > > +++ b/include/uapi/linux/fs.h
> > > @@ -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 */  
> >
> > 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,  
> 
> 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.

> > > +     __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

> > > +     __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.
> 
> > The '\0' terminated string handling is likely to be noise against
> > all the rest of the lookup code.
> >
> > David
> >  
> > > +};
> > > +
> > > +#define KALLSYMS_QUERY_SIZE_VER0     40 /* sizeof first published struct */
> > > +
> > >  /*
> > >   * Shutdown the filesystem.
> > >   */
> > > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
> > > index aec2f06858af..7caaf3e421c7 100644
> > > --- a/kernel/kallsyms.c
> > > +++ b/kernel/kallsyms.c
> > > @@ -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)  
> >  


  parent reply	other threads:[~2026-10-30 10:02 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23  0:16 [PATCH bpf-next v2 0/6] kallsyms: Add KALLSYMS_QUERY ioctl to /proc/kallsyms Samuel Wu
2026-10-23  0:16 ` [PATCH bpf-next v2 1/6] " Samuel Wu
2026-10-23  0:51   ` bot+bpf-ci
2026-10-27 21:31     ` Samuel Wu
2026-10-25 23:29   ` Randy Dunlap
2026-10-27 21:33     ` Samuel Wu
2026-10-27 22:24   ` Andrii Nakryiko
2026-10-29 21:58     ` Samuel Wu
2026-10-29 22:09       ` Andrii Nakryiko
2026-10-29 22:35         ` Samuel Wu
2026-10-29 22:54           ` Andrii Nakryiko
2026-10-30  0:23             ` Samuel Wu
2026-10-30 15:35             ` David Laight
2026-10-28 13:40   ` David Laight
2026-10-29 22:05     ` Samuel Wu
2026-10-29 22:16       ` Andrii Nakryiko
2026-10-30  0:28         ` Samuel Wu
2026-10-30 10:13           ` David Laight
2026-10-30 10:02       ` David Laight [this message]
2026-10-23  0:16 ` [PATCH bpf-next v2 2/6] tools: Copy KALLSYMS_QUERY definitions to linux/fs.h Samuel Wu
2026-10-23  0:16 ` [PATCH bpf-next v2 3/6] docs/procfs: Document KALLSYMS_QUERY ioctl on /proc/kallsyms Samuel Wu
2026-10-23  0:16 ` [PATCH bpf-next v2 4/6] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY ioctl Samuel Wu
2026-10-23  0:51   ` bot+bpf-ci
2026-10-27 21:45     ` Samuel Wu
2026-10-26  9:28   ` Jiri Olsa
2026-10-27 22:07     ` Samuel Wu
2026-10-28 14:01       ` David Laight
2026-10-29 21:35         ` Samuel Wu
2026-10-29 22:33           ` David Laight
2026-10-23  0:16 ` [PATCH bpf-next v2 5/6] selftests/bpf: Test typeless module ksym resolution Samuel Wu
2026-10-23  0:16 ` [PATCH bpf-next v2 6/6] selftests/proc: Add KALLSYMS_QUERY tests Samuel Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261030100215.6f8e0d4f@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=song@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wusamuel@google.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox