From: sashiko-bot@kernel.org
To: "David Laight" <david.laight.linux@gmail.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Petr Mladek" <pmladek@suse.com>, "Kees Cook" <kees@kernel.org>,
linux-kbuild@vger.kernel.org, bpf@vger.kernel.org,
"Jim Cromie" <jim.cromie@gmail.com>,
"Lorenzo Stoakes" <ljs@kernel.org>
Cc: "Luis Chamberlain" <mcgrof@kernel.org>,
"Andrey Grodzovsky" <andrey.grodzovsky@crowdstrike.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
nathan@kernel.org, nsc@kernel.org
Subject: Re: [PATCH v2 next 06/15] kallsyms: Optimise symbol name search
Date: Sun, 18 Oct 2026 16:15:56 +0000 [thread overview]
Message-ID: <sashiko-outbox-176273@kernel.org> (raw)
In-Reply-To: <20261018155206.132411-7-david.laight.linux@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] kallsyms: race in set_kallsyms_off_size() causes NULL dereference
- [High] kallsyms: set_kallsyms_off_size() sleeps in atomic context
--
commit d36232e927821c82911022ce5bae7cb3de465edd
Author: David Laight <david.laight.linux@gmail.com>
kallsyms: Optimise symbol name search
This commit optimizes symbol name lookups by indexing the compressed
names table directly instead of the array of symbol values. It supports
both 24-bit and 32-bit offset tables, selecting between them using a
static branch initialized on the first symbol lookup.
> diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
> index d18d78e626db2..31451feac8221 100644
> --- a/kernel/kallsyms.c
> +++ b/kernel/kallsyms.c
[ ... ]
> @@ -185,14 +216,19 @@ unsigned long kallsyms_sym_address(int idx)
> return (unsigned long)offset_to_ptr(kallsyms_offsets + idx);
> }
>
> -static unsigned int get_symbol_seq(int index)
> -{
> - unsigned int i, seq = 0;
> +static DEFINE_STATIC_KEY_TRUE(kallsyms_off_size_24);
>
> - for (i = 0; i < 3; i++)
> - seq = (seq << 8) | kallsyms_seqs_of_names[3 * index + i];
> +static inline unsigned int get_symbol_name_offset(unsigned long index)
> +{
> + if (static_branch_likely(&kallsyms_off_size_24))
> + return kallsyms_off24_of_names[index].v;
> + return kallsyms_off32_of_names[index];
> +}
>
> - return seq;
> +static void set_kallsyms_off_size(void)
> +{
> + if (kallsyms_off_size != 24)
> + static_branch_disable(&kallsyms_off_size_24);
> }
[Severity: High]
Can calling static_branch_disable() in set_kallsyms_off_size() sleep in
an atomic or early-boot context when kallsyms_off_size != 24?
static_branch_disable() calls static_key_disable() in
kernel/jump_label.c, which acquires cpus_read_lock() and
jump_label_mutex:
kernel/jump_label.c:static_key_disable() {
cpus_read_lock();
static_key_disable_cpuslocked(key);
cpus_read_unlock();
}
However, kallsyms_lookup_name() -> kallsyms_lookup_names() can be called
first from contexts where sleeping is not allowed:
- lib/codetag.c:get_symbol() calls kallsyms_lookup_name() under
preempt_disable():
preempt_disable();
ret = mod ?
(void *)find_kallsyms_symbol_value(mod, buf) :
(void *)kallsyms_lookup_name(buf);
preempt_enable();
- mm/alloc_tag.c:alloc_tag_sec_init() calls kallsyms_lookup_name() during
start_kernel() -> mm_core_init() while interrupts are disabled and
before poking_init().
- Non-sleepable runtime or debugger callers such as
bpf_kallsyms_lookup_name(), kdbgetsymval(), and xmon also invoke
kallsyms_lookup_name().
>
> static int kallsyms_lookup_names(const char *name,
> @@ -201,15 +237,16 @@ static int kallsyms_lookup_names(const char *name,
> {
> int ret;
> int low, mid, high;
> - unsigned int seq, off;
> + unsigned int off;
> +
> + DO_ONCE_LITE(set_kallsyms_off_size);
>
> low = 0;
> high = kallsyms_num_syms - 1;
>
> while (low <= high) {
> mid = low + (high - low) / 2;
> - seq = get_symbol_seq(mid);
> - off = get_symbol_offset(seq);
> + off = get_symbol_name_offset(mid);
[Severity: High]
Does DO_ONCE_LITE(set_kallsyms_off_size) allow a concurrent caller to
dereference NULL in get_symbol_name_offset() when kallsyms_off_size == 32?
In scripts/kallsyms.c:write_src(), only kallsyms_off24_of_names or
kallsyms_off32_of_names is emitted, and both are declared __weak in
kernel/kallsyms_internal.h:
extern struct { unsigned int v:24 __packed; } __packed kallsyms_off24_of_names[] __weak;
extern u32 kallsyms_off32_of_names[] __weak;
When kallsyms_off_size == 32, kallsyms_off24_of_names resolves to NULL,
while kallsyms_off_size_24 starts enabled via
DEFINE_STATIC_KEY_TRUE(kallsyms_off_size_24).
In include/linux/once_lite.h, __ONCE_LITE_IF() sets __already_done = true
before executing set_kallsyms_off_size() and does not wait for it to
finish:
if (unlikely(__ret_cond) && unlikely(!__already_done)) {
__already_done = true;
__ret_once = true;
}
If two CPUs call kallsyms_lookup_names() concurrently for the first time
while the first CPU is still inside set_kallsyms_off_size() ->
static_branch_disable(&kallsyms_off_size_24), the second CPU sees
__already_done == true and proceeds directly to
get_symbol_name_offset(mid). Since kallsyms_off_size_24 has not been
patched yet, get_symbol_name_offset() evaluates
kallsyms_off24_of_names[index].v and dereferences NULL.
[ ... ]
>
>
>
>
>
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261018155206.132411-1-david.laight.linux@gmail.com?part=6
next prev parent reply other threads:[~2026-10-18 16:15 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-18 15:51 [PATCH v2 next 00/15] kallsyms: optimise symbol search by name David Laight
2026-10-18 15:51 ` [PATCH v2 next 01/15] kallsyms: scripts/kallsyms.c: Avoid overrunning on-stack buffer David Laight
2026-10-18 15:51 ` [PATCH v2 next 02/15] kallsyms: Fix selftest startup delay David Laight
2026-10-18 15:51 ` [PATCH v2 next 03/15] kallsyms: Improve selftest test_perf_kallsyms_lookup_name() output David Laight
2026-10-18 16:03 ` sashiko-bot
2026-10-19 16:07 ` kernel test robot
2026-10-19 17:08 ` David Laight
2026-10-18 15:51 ` [PATCH v2 next 04/15] kallsyms: scripts/kallsyms: Check for output file write errors David Laight
2026-10-18 15:51 ` [PATCH v2 next 05/15] kallsyms: Match compressed tokens on the fly during binary search David Laight
2026-10-18 15:51 ` [PATCH v2 next 06/15] kallsyms: Optimise symbol name search David Laight
2026-10-18 16:15 ` sashiko-bot [this message]
2026-10-18 17:04 ` David Laight
2026-10-18 15:51 ` [PATCH v2 next 07/15] kallsyms: Optimise detection of duplicate symbol names David Laight
2026-10-18 15:51 ` [PATCH v2 next 08/15] kallsyms: Support changing the density of the kallsyms_markers[] array David Laight
2026-10-18 15:52 ` [PATCH v2 next 09/15] kallsyms: Add a kernel parameter for the marker shift David Laight
2026-10-18 15:52 ` [PATCH v2 next 10/15] kallsyms: scripts/kallsyms: Rename some variables for clarity David Laight
2026-10-18 15:52 ` [PATCH v2 next 11/15] kallsyms: scripts/kallsyms: Keep full expansion of tokens David Laight
2026-10-18 16:00 ` sashiko-bot
2026-10-18 16:37 ` David Laight
2026-10-18 15:52 ` [PATCH v2 next 12/15] kallsyms: scripts/kallsyms: Optimise compress_symbols() David Laight
2026-10-18 15:52 ` [PATCH v2 next 13/15] kallsyms: scripts/kallsyms: Reuse tokens that have no references David Laight
2026-10-18 15:52 ` [PATCH 14/15] kallsyms: scripts/kallsyms: Reduce the output file size David Laight
2026-10-18 16:05 ` sashiko-bot
2026-10-18 16:47 ` David Laight
2026-10-18 15:52 ` [PATCH 15/15] kallsyms: scripts/kallsyms: Improve compression by about 2% David Laight
2026-10-18 16:05 ` sashiko-bot
2026-10-18 17:28 ` David Laight
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=sashiko-outbox-176273@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andrey.grodzovsky@crowdstrike.com \
--cc=bpf@vger.kernel.org \
--cc=david.laight.linux@gmail.com \
--cc=jim.cromie@gmail.com \
--cc=kees@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mcgrof@kernel.org \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sashiko-reviews@lists.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