All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Nathan Chancellor <nathan@kernel.org>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Andreas Schwab <schwab@linux-m68k.org>,
	Andy Lutomirski <luto@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>, Theodore Ts'o <tytso@mit.edu>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Huacai Chen <chenhuacai@kernel.org>,
	WANG Xuerui <kernel@xen0n.name>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
	linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
	linux-s390@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH v2] random: vDSO: Avoid call to memset() when zeroing reserved in __cvdso_getrandom_data()
Date: Tue, 29 Sep 2026 18:44:15 +0100	[thread overview]
Message-ID: <20260929184415.1920fe91@pumpkin> (raw)
In-Reply-To: <20260929165939.GA1890629@ax162>

On Tue, 29 Sep 2026 18:59:39 +0200
Nathan Chancellor <nathan@kernel.org> wrote:

> On Sun, Sep 27, 2026 at 08:02:25AM +0100, David Laight wrote:
> > On Sat, 26 Sep 2026 14:32:49 +0200
> > "Jason A. Donenfeld" <Jason@zx2c4.com> wrote:  
> > > Do we even need to return a value at all? Might as well just make the
> > > function two lines:
> > > 
> > > +       for (char *d = dst; size--;)
> > > +               *d++ = value;  
> > 
> > Wouldn't it be better to add a barrier() or similar in there to
> > stop the compiler playing unwanted games>  
> 
> Wouldn't that just result in the same code generation issue that Jason
> pointed out on my v1?
> 
>   https://lore.kernel.org/aquyVkdYceiWdckX@zx2c4.com/

I missed that one going past....
You only get 'rep stosq' because the compiler first converts it to memset(). 

> Maybe that doesn't matter because modern compilers have better options?

A lot of cpu will run the 'rep stosq' very slowly (it has a big fixed cost).

At a guess the loop is 3 clocks (possibly 2; but that usually needs
you to use negative offsets from the end - and gcc doesn't like that).
That is comparable to a mispredicted branch (and you might get two of them).
It still might actually be faster than the 'rep stosq' version!

I suspect the fastest code is to unroll the loop.
	xor %eax, %eax
	movl %eax, 12(%rbx)
	movq %rax, 16(%rbx)
	movq %rax, 24(%rbx)
	movq %rax, 32(%rbx)
	movq %rax, 40(%rbx)
	movq %rax, 48(%rbx)
	movq %rax, 56(%rbx)
8 clocks on old cpu, 4 on newer ones.
But more likely to be limited be I-cache reads.

If you are going to use 'rep stos' then you might as well write
13 32bit words.

David



WARNING: multiple messages have this Message-ID (diff)
From: David Laight <david.laight.linux@gmail.com>
To: Nathan Chancellor <nathan@kernel.org>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Andreas Schwab <schwab@linux-m68k.org>,
	Andy Lutomirski <luto@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>, Theodore Ts'o <tytso@mit.edu>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Huacai Chen <chenhuacai@kernel.org>,
	WANG Xuerui <kernel@xen0n.name>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
	linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
	linux-s390@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH v2] random: vDSO: Avoid call to memset() when zeroing reserved in __cvdso_getrandom_data()
Date: Tue, 29 Sep 2026 18:44:15 +0100	[thread overview]
Message-ID: <20260929184415.1920fe91@pumpkin> (raw)
In-Reply-To: <20260929165939.GA1890629@ax162>

On Tue, 29 Sep 2026 18:59:39 +0200
Nathan Chancellor <nathan@kernel.org> wrote:

> On Sun, Sep 27, 2026 at 08:02:25AM +0100, David Laight wrote:
> > On Sat, 26 Sep 2026 14:32:49 +0200
> > "Jason A. Donenfeld" <Jason@zx2c4.com> wrote:  
> > > Do we even need to return a value at all? Might as well just make the
> > > function two lines:
> > > 
> > > +       for (char *d = dst; size--;)
> > > +               *d++ = value;  
> > 
> > Wouldn't it be better to add a barrier() or similar in there to
> > stop the compiler playing unwanted games>  
> 
> Wouldn't that just result in the same code generation issue that Jason
> pointed out on my v1?
> 
>   https://lore.kernel.org/aquyVkdYceiWdckX@zx2c4.com/

I missed that one going past....
You only get 'rep stosq' because the compiler first converts it to memset(). 

> Maybe that doesn't matter because modern compilers have better options?

A lot of cpu will run the 'rep stosq' very slowly (it has a big fixed cost).

At a guess the loop is 3 clocks (possibly 2; but that usually needs
you to use negative offsets from the end - and gcc doesn't like that).
That is comparable to a mispredicted branch (and you might get two of them).
It still might actually be faster than the 'rep stosq' version!

I suspect the fastest code is to unroll the loop.
	xor %eax, %eax
	movl %eax, 12(%rbx)
	movq %rax, 16(%rbx)
	movq %rax, 24(%rbx)
	movq %rax, 32(%rbx)
	movq %rax, 40(%rbx)
	movq %rax, 48(%rbx)
	movq %rax, 56(%rbx)
8 clocks on old cpu, 4 on newer ones.
But more likely to be limited be I-cache reads.

If you are going to use 'rep stos' then you might as well write
13 32bit words.

David


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2026-09-29 17:44 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 21:46 [PATCH v2] random: vDSO: Avoid call to memset() when zeroing reserved in __cvdso_getrandom_data() Nathan Chancellor
2026-09-25 21:46 ` Nathan Chancellor
2026-09-25 21:53 ` Nick Desaulniers
2026-09-25 21:53   ` Nick Desaulniers
2026-09-25 21:56   ` Nick Desaulniers
2026-09-25 21:56     ` Nick Desaulniers
2026-09-25 22:00     ` Nick Desaulniers
2026-09-25 22:00       ` Nick Desaulniers
2026-09-25 22:19       ` Nathan Chancellor
2026-09-25 22:19         ` Nathan Chancellor
2026-09-26  6:34       ` Christophe Leroy (CS GROUP)
2026-09-26  6:34         ` Christophe Leroy (CS GROUP)
2026-09-26 10:02       ` Jason A. Donenfeld
2026-09-26 10:02         ` Jason A. Donenfeld
2026-09-26 10:55         ` Christophe Leroy (CS GROUP)
2026-09-26 10:55           ` Christophe Leroy (CS GROUP)
2026-09-26 12:29           ` Jason A. Donenfeld
2026-09-26 12:29             ` Jason A. Donenfeld
2026-09-29 18:24             ` Nick Desaulniers
2026-09-29 18:24               ` Nick Desaulniers
2026-09-30 13:38               ` Nathan Chancellor
2026-09-30 13:38                 ` Nathan Chancellor
2026-09-30 14:21                 ` Jason A. Donenfeld
2026-09-30 14:21                   ` Jason A. Donenfeld
2026-09-30 14:44                   ` Jason A. Donenfeld
2026-09-30 14:44                     ` Jason A. Donenfeld
2026-09-30 15:13                     ` Nathan Chancellor
2026-09-30 15:13                       ` Nathan Chancellor
2026-09-30 15:16                       ` Jason A. Donenfeld
2026-09-30 15:16                         ` Jason A. Donenfeld
2026-10-01  4:41                         ` Christophe Leroy (CS GROUP)
2026-10-01  4:41                           ` Christophe Leroy (CS GROUP)
2026-10-01  9:25                           ` Jason A. Donenfeld
2026-10-01  9:25                             ` Jason A. Donenfeld
2026-10-01 10:20                             ` Nathan Chancellor
2026-10-01 10:20                               ` Nathan Chancellor
2026-10-01 10:48                               ` Jason A. Donenfeld
2026-10-01 10:48                                 ` Jason A. Donenfeld
2026-10-01 11:03                                 ` Nathan Chancellor
2026-10-01 11:03                                   ` Nathan Chancellor
2026-10-02  9:49                                   ` Nathan Chancellor
2026-10-02  9:49                                     ` Nathan Chancellor
2026-10-02 10:13                                     ` Jason A. Donenfeld
2026-10-02 10:13                                       ` Jason A. Donenfeld
2026-10-02 18:21                                       ` Christophe Leroy (CS GROUP)
2026-10-02 18:21                                         ` Christophe Leroy (CS GROUP)
2026-10-02  9:18                                 ` Christophe Leroy (CS GROUP)
2026-10-02  9:18                                   ` Christophe Leroy (CS GROUP)
2026-10-02  9:27                                   ` Jason A. Donenfeld
2026-10-02  9:27                                     ` Jason A. Donenfeld
2026-09-26 12:39           ` Nathan Chancellor
2026-09-26 12:39             ` Nathan Chancellor
2026-09-25 21:56 ` sashiko-bot
2026-09-26  9:39 ` Andreas Schwab
2026-09-26  9:39   ` Andreas Schwab
2026-09-26 12:19   ` Nathan Chancellor
2026-09-26 12:19     ` Nathan Chancellor
2026-09-26 12:32     ` Jason A. Donenfeld
2026-09-26 12:32       ` Jason A. Donenfeld
2026-09-26 12:49       ` Nathan Chancellor
2026-09-26 12:49         ` Nathan Chancellor
2026-09-27  7:02       ` David Laight
2026-09-27  7:02         ` David Laight
2026-09-29 16:59         ` Nathan Chancellor
2026-09-29 16:59           ` Nathan Chancellor
2026-09-29 17:44           ` David Laight [this message]
2026-09-29 17:44             ` David Laight
2026-10-02 10:57 ` Christophe Leroy (CS GROUP)
2026-10-02 10:57   ` Christophe Leroy (CS GROUP)
2026-10-02 10:58   ` Jason A. Donenfeld
2026-10-02 10:58     ` Jason A. Donenfeld
2026-10-02 11:01     ` LEROY Christophe
2026-10-02 11:01       ` LEROY Christophe
2026-10-02 11:02     ` Christophe Leroy (CS GROUP)
2026-10-02 11:02       ` Christophe Leroy (CS GROUP)

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=20260929184415.1920fe91@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=agordeev@linux.ibm.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=borntraeger@linux.ibm.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=chenhuacai@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=hpa@zytor.com \
    --cc=justinstitt@google.com \
    --cc=kernel@xen0n.name \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=llvm@lists.linux.dev \
    --cc=loongarch@lists.linux.dev \
    --cc=luto@kernel.org \
    --cc=maddy@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=mpe@ellerman.id.au \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=npiggin@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=schwab@linux-m68k.org \
    --cc=svens@linux.ibm.com \
    --cc=tglx@kernel.org \
    --cc=tytso@mit.edu \
    --cc=vincenzo.frascino@arm.com \
    --cc=will@kernel.org \
    --cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.