Re: [PATCH] random: vDSO: Avoid call to memset() when zeroing reserved in __cvdso_getrandom_data()

From: Nathan Chancellor

Date: Thu Sep 17 2026 - 15:54:40 EST


On Thu, Sep 17, 2026 at 11:26:46AM +0200, Jason A. Donenfeld wrote:
> I don't suspect this is the right change. On x86_64, this changes to
> code from:
>
> rep stosq
>
> into:
>
> loc_2D9:
> mov dword ptr [rbx+rax*4+0Ch], 0
> add rax, 1
> cmp rax, 0Ch
> jbe short loc_2D9
>
> Which is a lot less compact. It seems like the actual solution is for
> gcc&clang to emit this inline memset mnemonic when the platform has a
> good one, and otherwise not. But disabling optimizations for all
> platforms, because it's broken on one, seems bad.

Yes, that is certainly fair criticism. That said, this optimization
happens in the middle end as far as I can tell, so I am not sure how
much target specific knowledge is available at that point.
Additionally...

> In this case, the compiler is being smart: it identifies a loop and
> rightly turns it into memset. But if this isn't a compilation
> environment that has an outline function, it should do something else.

As I mentioned in the commit message, compilers require all environments
(hosted or not) to provide memset(), so the "doing something else" is
nothing :)

GCC requires the freestanding environment provide memcpy, memmove,
memset and memcmp.

I guess another option is to just include a basic memset() like the one
in lib/string.c so that it is only used if the compiler makes this sort
of transformation, while leaving all other architectures alone.

--
Cheers,
Nathan