Re: [GIT PULL] string fixes for v6.15-rc1
From: Linus Torvalds
Date: Sun Apr 06 2025 - 15:32:17 EST
On Sun, 6 Apr 2025 at 12:04, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> When the function implementation is the stupid version, there is *no*
> advantage to a function call.
Side note: the reason we don't do -freestanding is that some functions
we really *really* want the compiler to optimize away the function
entirely.
Things like regular 'strlen()' are commonly used on compile-time fixed
strings. It needs to be dealt with by the compiler.
Same goes for the basic memcpy and memset functions.
So what the kernel really wants is basically "-freestanding" but with
a very targeted set of functions still dealt with by the compiler -
not ever because they should be turned into function calls, but
because they can be simplified to some entirely different form at
compile time.
(There are a _couple_ of other cases, but not many: we end up having
things like "constant prefix matching" patterns, where we want the
compiler to not only notice that 'strlen()' on a constant string is
just a compile-time constant value, but then also want to simplify
'strncmp()' or 'memcmp()' with a small constant size to basically be a
much simpler thing)
But we do not want the compiler to create out-of-line function calls
for code that wasn't a function call to begin with. That's pretty
much the exact *opposite* of what we actually want.
We did try "-freestanding" at some point, because it's the obvious way
to avoid the compiler messing this very basic thing up, but those
"just deal with simple cases directly" are really really important.
I wish there was a version of -freestanding that did that "only
simplify calls" version, not the "recognize patterns and turn them
into calls that didn't exist".
Linus