mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
	arnd@arndb.de, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org
Subject: Re: [PATCH v6 13/17] x86: use fallback for random_get_entropy() instead of zero
Date: Mon, 25 Apr 2022 15:41:00 +0200	[thread overview]
Message-ID: <Ymak7LJd6GnFxsOo@zx2c4.com> (raw)
In-Reply-To: <871qxl2vdw.ffs@tglx>

Hi Thomas,

On Mon, Apr 25, 2022 at 02:35:39PM +0200, Thomas Gleixner wrote:
> On Sat, Apr 23 2022 at 23:26, Jason A. Donenfeld wrote:
> 
> Please follow the guidelines of the tip maintainers when touching x86
> code. https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#patch-subject
> https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#changelog

Will fix up the message and topic.

> > In the event that random_get_entropy() can't access a cycle counter or
> > similar, falling back to returning 0 is really not the best we can do.
> > Instead, at least calling random_get_entropy_fallback() would be
> > preferable, because that always needs to return _something_, even
> > falling back to jiffies eventually. It's not as though
> > random_get_entropy_fallback() is super high precision or guaranteed to
> > be entropic, but basically anything that's not zero all the time is
> > better than returning zero all the time.
> >
> > If CONFIG_X86_TSC=n, then it's possible that we're running on a 486
> > with no RDTSC, so we only need the fallback code for that case.
> 
> There are also 586 CPUs which lack TSC.

Will note this in the rewritten commit message.

> > +static inline unsigned long random_get_entropy(void)
> > +{
> > +#ifndef CONFIG_X86_TSC
> > +	if (!cpu_feature_enabled(X86_FEATURE_TSC))
> > +		return random_get_entropy_fallback();
> > +#endif
> 
> Please get rid of this ifdeffery. While you are right, that anything
> with CONFIG_X86_TSC=y should have a TSC, there is virt ....
> 
> cpu_feature_enabled() is runtime patched and only evaluated before
> alternative patching, so the win of this ifdef is marginally, if even
> noticable.
> 
> We surely can think about making TSC mandatory, but not selectively in a
> particalur context.

This would be a regression of sorts from the current code, which reads:

    static inline cycles_t get_cycles(void)
    {
    #ifndef CONFIG_X86_TSC
            if (!boot_cpu_has(X86_FEATURE_TSC))
                    return 0;
    #endif
            return rdtsc();
    }

So my ifdef is just copying that one. I can make it into a `if
(IS_ENABLED(CONFIG_X86_TSC))` thing, if you'd prefer that. But on
systems where CONFIG_X86_TSC=n, including the extra code there seems
kind of undesirable. Consider the current interrupt handler random.c
code on x86, whose first lines (usually) compile to:

.text:0000000000001A70 add_interrupt_randomness proc near
.text:0000000000001A70                 movsxd  rcx, edi
.text:0000000000001A73                 rdtsc
.text:0000000000001A75                 shl     rdx, 20h
.text:0000000000001A79                 mov     rdi, [rsp+0]
.text:0000000000001A7D                 or      rax, rdx
.text:0000000000001A80                 mov     rdx, offset irq_randomness
.text:0000000000001A87                 mov     rsi, gs:__irq_regs
.text:0000000000001A8F                 add     rdx, gs:this_cpu_off

I'm not sure what advantage we'd get by changing that from the ifdefs to
unconditionally including that if statement. Your argument about virt
can't be valid, since the current get_cycles() code uses the ifdefs. And
if you're arguing from the basis that CONFIG_X86_TSC=y is not reliable,
then why does CONFIG_X86_TSC even exist in the first place?

Rather the stronger argument you could make would be that moving from
boot_cpu_has() to cpu_feature_enabled() (Borislav's suggestion) means
that there's now a static branch here, so the actual cost to the
assembly is a few meaningless nops, which you don't think would play a
part in quantizing when rdtsc is called. If this is actually what you
have in mind, and you find that ifdef ugly enough that this would be
worth it, I can understand, and I'll make that change for v7. But I
don't understand you to currently be making that argument, so I'm not
quite sure what your position is.

Could you clarify what you mean here and what your expectations are? And
how does that point of view tie into the fact that get_cycles()
currently uses the ifdef?

Thanks,
Jason

  reply	other threads:[~2022-04-25 13:41 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-23 21:26 [PATCH v6 00/17] archs/random: fallback to best raw ktime when no cycle counter Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 01/17] ia64: define get_cycles macro for arch-override Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 02/17] s390: " Jason A. Donenfeld
2022-04-25  9:43   ` Heiko Carstens
2022-04-25  9:48     ` Jason A. Donenfeld
2022-04-25 10:21       ` Heiko Carstens
2022-04-23 21:26 ` [PATCH v6 03/17] parisc: " Jason A. Donenfeld
2022-04-24 18:35   ` Helge Deller
2022-04-23 21:26 ` [PATCH v6 04/17] alpha: " Jason A. Donenfeld
2022-04-26 20:31   ` Matt Turner
2022-04-23 21:26 ` [PATCH v6 05/17] powerpc: " Jason A. Donenfeld
2022-04-28  5:19   ` Michael Ellerman
2022-04-23 21:26 ` [PATCH v6 06/17] timekeeping: add raw clock fallback for random_get_entropy() Jason A. Donenfeld
2022-04-25 12:37   ` Thomas Gleixner
2022-04-25 13:22     ` Jason A. Donenfeld
2022-05-02  9:37       ` Thomas Gleixner
2022-05-05  0:19         ` [PATCH v7] timekeeping: Add " Jason A. Donenfeld
2022-05-05  0:29           ` Jason A. Donenfeld
2022-05-06  3:20           ` [timekeeping] 3aeaac747d: PANIC:early_exception kernel test robot
2022-05-06  7:59             ` Thomas Gleixner
2022-05-06 10:12               ` Jason A. Donenfeld
2022-05-06 16:01                 ` Thomas Gleixner
2022-05-06 10:21         ` [PATCH v8] timekeeping: Add raw clock fallback for random_get_entropy() Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 07/17] m68k: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 08/17] riscv: " Jason A. Donenfeld
2022-04-25 14:55   ` Palmer Dabbelt
2022-04-25 15:02     ` Jason A. Donenfeld
2022-04-25 15:20       ` Palmer Dabbelt
2022-04-23 21:26 ` [PATCH v6 09/17] mips: use fallback for random_get_entropy() instead of just c0 random Jason A. Donenfeld
2022-04-26 20:28   ` Maciej W. Rozycki
2022-04-27  1:29     ` Jason A. Donenfeld
2022-06-24 13:04       ` Jason A. Donenfeld
2022-06-24 13:25         ` Maciej W. Rozycki
2022-09-25 10:00           ` Jason A. Donenfeld
2022-09-25 14:17             ` Maciej W. Rozycki
2022-04-23 21:26 ` [PATCH v6 10/17] arm: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-04-25 14:30   ` Russell King (Oracle)
2022-04-23 21:26 ` [PATCH v6 11/17] openrisc: " Jason A. Donenfeld
2022-04-27 21:42   ` Stafford Horne
2022-04-27 23:26     ` Jason A. Donenfeld
2022-04-29  0:16   ` [PATCH v7 11/17] openrisc: account for 0 starting value in random_get_entropy() Jason A. Donenfeld
2022-04-30  1:19     ` Stafford Horne
2022-04-30  1:29       ` Jason A. Donenfeld
2022-04-30 22:11         ` Stafford Horne
2022-04-30 22:34           ` Jason A. Donenfeld
2022-04-30 22:44             ` Stafford Horne
2022-04-23 21:26 ` [PATCH v6 12/17] nios2: use fallback for random_get_entropy() instead of zero Jason A. Donenfeld
2022-05-23 13:56   ` Dinh Nguyen
2022-04-23 21:26 ` [PATCH v6 13/17] x86: " Jason A. Donenfeld
2022-04-25 12:35   ` Thomas Gleixner
2022-04-25 13:41     ` Jason A. Donenfeld [this message]
2022-04-25 13:51       ` Jason A. Donenfeld
2022-04-25 17:03       ` Thomas Gleixner
2022-04-25 17:24         ` Jason A. Donenfeld
2022-04-26  8:33           ` [PATCH v7 13/17] x86/asm: " Jason A. Donenfeld
2022-05-02  9:40             ` Thomas Gleixner
2022-04-23 21:26 ` [PATCH v6 14/17] um: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 15/17] sparc: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 16/17] xtensa: " Jason A. Donenfeld
2022-04-23 21:26 ` [PATCH v6 17/17] random: insist on random_get_entropy() existing in order to simplify Jason A. Donenfeld

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=Ymak7LJd6GnFxsOo@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --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 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®