From: David Laight <david.laight.linux@gmail.com>
To: Jim Cromie <jim.cromie@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Lorenzo Stoakes <ljs@kernel.org>, Kees Cook <kees@kernel.org>,
Masahiro Yamada <masahiroy@kernel.org>,
linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
bpf@vger.kernel.org
Subject: Re: [PATCH v2 3/3] kallsyms: Match compressed tokens on the fly during binary search
Date: Tue, 22 Sep 2026 10:03:51 +0100 [thread overview]
Message-ID: <20260922100351.04555f43@pumpkin> (raw)
In-Reply-To: <20260922-ksyms-tune-v2-3-a333ee31eac7@gmail.com>
On Tue, 22 Sep 2026 01:19:21 -0600
Jim Cromie <jim.cromie@gmail.com> wrote:
> kallsyms_lookup_names() runs a binary search across kallsyms_names[],
> a packed array of ~130k encoded kernel symbols. For each of the ~17
> comparisons in the search, it currently decompresses the candidate
> symbol into a temporary buffer on the stack before calling strcmp().
>
> Comparing raw tokens directly in compressed space is impossible. The
> BPE token table assigns values by frequency, not alphabetical order
> (e.g. token 0x05 might expand to "zebra" while 0x42 expands to "apple"),
> so comparing raw token values scrambles lexicographical order.
>
> However, full string expansion is equally wasteful: roughly 16 of the
> 17 binary search steps fail within the first two characters.
>
> Introduce kallsyms_strcmp_symbol() to compare ASCII queries against
> compressed tokens on the fly. It walks kallsyms_token_index and
> kallsyms_token_table incrementally, matching characters directly and
> bailing out on the first character mismatch without expanding subsequent
> tokens.
>
> This optimization:
>
> 0. Avoids decompressing non-matching tokens, short-circuiting ~94% of
> binary search character expansions without adding any tables in
> .rodata.
>
> 1. Drops the 512-byte namebuf buffer from the kernel stack in
> kallsyms_lookup_names().
>
> 2. Leaves sequential address ordering and kallsyms_expand_symbol()
> streaming invariants intact for /proc/kallsyms and table walks.
>
> Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
...
> +/*
> + * Compare an uncompressed ASCII string against a compressed symbol table entry.
> + * Returns negative if name < sym, positive if name > sym, 0 if equal.
> + * Exits immediately on the first mismatched character without decompressing
> + * the rest of the symbol name.
> + */
> +static int kallsyms_strcmp_symbol(unsigned int off, const char *name)
> +{
> + int skipped_first = 0;
> + const char *tptr;
> + unsigned int len;
> + const u8 *data = get_symbol_data(off, &len);
> +
> + while (len) {
> + tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
> + data++;
> + len--;
> +
> + while (*tptr) {
> + if (skipped_first) {
> + int diff = (unsigned char)*name - (unsigned char)*tptr;
> +
> + if (diff != 0)
> + return diff;
> + name++;
> + } else {
> + skipped_first = 1;
> + }
> + tptr++;
> + }
> + }
> +
> + return (unsigned char)*name - '\0';
> +}
Since len can't be zero you can move the test to the bottom and remove the
skipped_first test completely. Something like:
tptr = &kallsyms_token_table[kallsyms_token_index[*data++]] + 1;
for (;;) {
do {
int diff = (unsigned char)*name++ - (unsigned char)*tptr++;
if (diff)
return diff;
} while (*tptr);
if (!--len)
break;
tptr = &kallsyms_token_table[kallsyms_token_index[*data++]];
}
return (unsigned char)*name;
Also 'char' is now 'unsigned char' in all kernel builds you don't
need the casts.
But I'd make the types explicitly 'unsigned char' just in case.
David
>
> /*
> * Find the offset on the compressed stream given an index in the
...
next prev parent reply other threads:[~2026-09-22 9:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 7:19 [PATCH v2 0/3] kallsyms: Accelerate symbol name lookups by ~19x Jim Cromie
2026-09-22 7:19 ` [PATCH v2 1/3] kallsyms: Add test_kallsyms_perf module to benchmark lookup latency Jim Cromie
2026-09-22 7:19 ` [PATCH v2 2/3] kallsyms: Add dynamic lookup index for batch resolution Jim Cromie
2026-09-22 7:19 ` [PATCH v2 3/3] kallsyms: Match compressed tokens on the fly during binary search Jim Cromie
2026-09-22 9:03 ` David Laight [this message]
2026-09-22 8:41 ` [PATCH v2 0/3] kallsyms: Accelerate symbol name lookups by ~19x 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=20260922100351.04555f43@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=bpf@vger.kernel.org \
--cc=jim.cromie@gmail.com \
--cc=kees@kernel.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=masahiroy@kernel.org \
/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®