mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: wusamuel@google.com,corbet@lwn.net,skhan@linuxfoundation.org,rdunlap@infradead.org,mcgrof@kernel.org,petr.pavlu@suse.com,da.gomez@kernel.org,samitolvanen@google.com,atomlin@atomlin.com,viro@zeniv.linux.org.uk,brauner@kernel.org,jack@suse.cz,ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,eddyz87@gmail.com,memxor@gmail.com,martin.lau@linux.dev,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,nathan@kernel.org,ndesaulniers@google.com,morbo@google.com,justinstitt@google.com
Cc: akpm@linux-foundation.org,pmladek@suse.com,david.laight.linux@gmail.com,wusamuel@google.com,kernel-team@android.com,linux-kernel@vger.kernel.org,linux-fsdevel@vger.kernel.org,linux-doc@vger.kernel.org,linux-modules@vger.kernel.org,bpf@vger.kernel.org,linux-kselftest@vger.kernel.org,llvm@lists.linux.dev,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v3 3/9] kallsyms: Add KALLSYMS_QUERY_BY_NAME ioctl to /proc/kallsyms
Date: Fri, 13 Nov 2026 22:55:50 +0000 (UTC)	[thread overview]
Message-ID: <2f27a1ffca14cd87a744c447c5fded67459c1497ebafe05350f4721ef994c3f0@mail.kernel.org> (raw)
In-Reply-To: <20261113221248.3535369-14-wusamuel@google.com>

[-- Attachment #1: Type: text/plain, Size: 4770 bytes --]

> kallsyms: Add KALLSYMS_QUERY_BY_NAME ioctl to /proc/kallsyms

TL;DR:
- KALLSYMS_QUERY_BY_NAME may scan BPF ksyms quadratically in one syscall with
  no cond_resched(), possibly cancelling the speedup
- module_filter may return -ENOENT for init symbols that the unfiltered query
  finds via the ftrace section

> diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
> --- a/kernel/kallsyms.c
> +++ b/kernel/kallsyms.c

[ ... ]

> +static int kallsyms_find_name_other(unsigned int sec, unsigned int *idx,
> +				    const struct kallsyms_name_query *q,
> +				    struct kallsyms_sym_match *m)
> +{

[ ... ]

> +	do {
> +		if (sec == KSYM_SEC_BPF)
> +			err = bpf_get_kallsym(*idx, &m->addr, &type, m->name);
> +		else if (sec == KSYM_SEC_KPROBE)
> +			err = kprobe_get_kallsym(*idx, &m->addr, &type, m->name);
> +		else
> +			err = ftrace_mod_get_kallsym(*idx, &m->addr, &type,
> +						     m->name, m->modname,
> +						     &exported);
> +		if (!err && !strncmp(m->name, q->name, q->cmp_len))
> +			return 0;
> +		(*idx)++;
> +	} while (!err);

Is this loop quadratic in the number of BPF ksyms, with no reschedule point?

bpf_get_kallsym() restarts from the list head on every call and walks
forward to the index:

    list_for_each_entry_rcu(ksym, &bpf_kallsyms, lnode) {
        if (it++ != symnum)
            continue;

So one ioctl that scans the BPF section without a match does n(n+1)/2 list
steps inside a single syscall.  Every exhaustive query gets there, since
even a query whose matches are all in vmlinux or modules still scans the
ftrace, BPF and kprobe sections on the call that returns -ENOENT.

The uapi comment tells users to keep calling with pos + 1 until -ENOENT,
and the libbpf user later in this series (822e9da9a9b7 "libbpf: Resolve
typeless ksyms via KALLSYMS_QUERY_BY_NAME ioctl") does this twice per
typeless extern, once for the exact name and once for the "<name>.llvm."
prefix.

With read() of /proc/kallsyms the same getter cost is spread across
syscalls, because seq_read fills at most one seq_file buffer per call.  Here
all of it lands in each call that reaches the BPF section.  On hosts with
thousands of BPF programs and trampolines, does this cancel the speedup the
commit message describes?

On kernels without ARCH_HAS_PREEMPT_LAZY, or with ARCH_NO_PREEMPT, it is
also a long run with no reschedule, whereas kallsyms_on_each_symbol() and
kallsyms_on_each_match_symbol() in this file call cond_resched().

Would a helper that walks bpf_kallsyms once from a start index be better,
or at least a cond_resched() in this loop?  ftrace_mod_get_kallsym() has the
same restart-from-the-beginning pattern, although its lists are usually
small.

[ ... ]

> +	if (karg.module_filter) {
> +		len = kallsyms_copy_str_from_user(bufs->module_filter,
> +						  sizeof(bufs->module_filter),
> +						  karg.module_filter);
> +		if (len < 0)
> +			return len;
> +		if (!strcmp(bufs->module_filter, "vmlinux")) {
> +			end_sec = KSYM_SEC_VMLINUX + 1;
> +		} else {
> +			first_sec = KSYM_SEC_MODULE;
> +			end_sec = KSYM_SEC_MODULE + 1;
> +			q.modname = bufs->module_filter;
> +		}

Can module_filter return -ENOENT for a symbol that the unfiltered query
finds in the same module?

When a module name is given, only KSYM_SEC_MODULE is searched.  But
/proc/kallsyms also lists entries for a loaded module from the ftrace
section.  ftrace_free_mem(), called from do_init_module() for the module's
init text, runs allocate_ftrace_mod_map() when any ftrace_ops is registered,
and ftrace_mod_get_kallsym() returns those entries with
strscpy(module_name, mod_map->mod->name, ...).  They appear as, for example:

    ffffffffc0a01000 T foo_init [foo]

and remain until the module is unloaded, because ftrace_release_mod() frees
the map then.  After init, mod->kallsyms has switched to core_kallsyms, so
these init symbols are no longer in the module section.  That gives:

    BY_NAME("foo_init", module_filter = NULL)  -> match, sym_module = "foo"
    BY_NAME("foo_init", module_filter = "foo") -> -ENOENT

The uapi comment says the ioctl finds a symbol "in the entries of
/proc/kallsyms", so the filtered result does not look like the unfiltered
result limited to [foo].  kallsyms_find_name_other() does not check
q->modname for the ftrace section, so extending end_sec alone would not be
enough.

Should the filtered case also search KSYM_SEC_FTRACE and skip entries whose
m->modname differs from q->modname, or should the uapi comment document that
module_filter only covers the module's own symbol table?


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

  reply	other threads:[~2026-11-13 22:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 22:12 [PATCH bpf-next v3 0/9] kallsyms: Add KALLSYMS_QUERY_BY_NAME and KALLSYMS_QUERY_BY_ADDR ioctls Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 1/9] Documentation: ioctl-number: Add PROCFS_IOCTL_MAGIC range 0x10-0x11 Samuel Wu
2026-11-14  2:22   ` Randy Dunlap
2026-11-13 22:12 ` [PATCH bpf-next v3 2/9] module: Add module_kallsyms_find_name() Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 3/9] kallsyms: Add KALLSYMS_QUERY_BY_NAME ioctl to /proc/kallsyms Samuel Wu
2026-11-13 22:55   ` bot+bpf-ci [this message]
2026-11-13 22:12 ` [PATCH bpf-next v3 4/9] kallsyms: Add KALLSYMS_QUERY_BY_ADDR " Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 5/9] docs/procfs: Document KALLSYMS_QUERY_* ioctls on /proc/kallsyms Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 6/9] tools: Copy KALLSYMS_QUERY_* definitions to linux/fs.h Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 7/9] selftests/proc: Add KALLSYMS_QUERY_* ioctl tests Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 8/9] libbpf: Resolve typeless ksyms via KALLSYMS_QUERY_BY_NAME ioctl Samuel Wu
2026-11-13 22:12 ` [PATCH bpf-next v3 9/9] selftests/bpf: Test typeless module ksym resolution 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=2f27a1ffca14cd87a744c447c5fded67459c1497ebafe05350f4721ef994c3f0@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=atomlin@atomlin.com \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=da.gomez@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=david.laight.linux@gmail.com \
    --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=linux-modules@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=memxor@gmail.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=petr.pavlu@suse.com \
    --cc=pmladek@suse.com \
    --cc=rdunlap@infradead.org \
    --cc=samitolvanen@google.com \
    --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

all inboxes | Powered by JetHome®