Re: [PATCH v2 2/2] module/kallsyms: sort function symbols and use binary search
From: Stanislaw Gruszka
Date: Tue Nov 17 2026 - 06:18:01 EST
Hi,
My apologies for the very long delay in responding.
I wanted to understand the livepatch relocation issue before getting
back to this patch, but I kept struggling with it and putting it off.
Eventually, I got some help from AI to understand how livepatch
relocations work and why reordering symtab entries breaks them.
On Tue, Apr 28, 2026 at 10:23:30AM +0200, Stanislaw Gruszka wrote:
> > >> I'm not sure if it is ok to limit the search only to function symbols
> > >> when the address lies in MOD_TEXT. The text can theoretically contain
> > >> non-function symbols.
> > >
> > > Yes, the patch assumes that the only valid symbols in the MOD_TEXT
> > > are functions. If there are defined OBJECT symbols in .text, the patch
> > > would break lookup for those.
> > >
> > > While it’s theoretically possible (e.g. hand-written assembly placing
> > > data in .text ?), I’m not sure this is a practical concern. In general,
> > > having data in executable segments is discouraged for security reasons.
> > >
> > >> Could this optimization be adjusted to sort all
> > >> MOD_TEXT symbols (excluding anonymous and mapping symbols) and move them
> > >> to the front of the symbol table?
> > >
> > > That’s possible. We could track .text sections indices in
> > > __layout_sections() and include all valid symbols from those sections,
> > > and also reorder typetab accordingly.
> > >
> > > However, this adds complexity. I would prefer to first confirm whether
> > > OBJECT symbols in MOD_TEXT is a real issue before going in that direction.
It turns out that identifying MOD_TEXT symbols is straightforward.
The section's memory type is already encoded in
sec->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT, where sec is obtained from
the symbol's st_shndx.
> > I'm not aware of specific OBJECT symbols that end up in MOD_TEXT.
> > Nonetheless, it is a valid case and it is preferable that an
> > optimization doesn't break their lookup by address.
> >
> > In general, I'm worried about the several edge cases and inconsistencies
> > that this optimization introduces. This also includes the fact that it
> > doesn't work for livepatch modules.
> >
> > An alternative could be to keep the symbol table untouched and have
> > a separate array with symbol indexes that is sorted by their addresses,
> > but it requires evaluation if the additional memory usage is worth it.
I've implemented this approach. As a bonus, sorting during module
loading is actually faster, since only the indexes need to be moved
instead of entire symbol table entries.
To get a rough estimate of the additional memory usage, I ran the
following command on my system:
awk 'NF == 4 && ($2 == "T" || $2 == "t")' /proc/kallsyms | wc -l
It reports 83593 symbols, which would correspond to roughly 326kB
of additional memory for 32-bit indexes across all loaded modules.
For comparison, the total size of loaded modules on this system is:
lsmod | awk 'NR > 1 { sum += $2 } END { printf "%.2f MB\n", sum / 1024 / 1024 }'
49.74 MB
Thus, the additional index would account for roughly 0.64% of the
total memory occupied by modules.
> I personally prefer not to over-engineer for cases that may never occur.
> That said, your concerns about edge cases, consistency, and livepatch
> handling are valid.
>
> Let me take some time to think this through and experiment with possible
> solutions that handle these cases properly. I can’t work on this
> immediately, but I’ll get back to it.
I'll post v3 shortly.
Regards
Stanislaw