From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB4C5C433FE for ; Thu, 21 Apr 2022 21:06:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441795AbiDUVJD (ORCPT ); Thu, 21 Apr 2022 17:09:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239054AbiDUVJC (ORCPT ); Thu, 21 Apr 2022 17:09:02 -0400 Received: from elvis.franken.de (elvis.franken.de [193.175.24.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3E17447AE4; Thu, 21 Apr 2022 14:06:11 -0700 (PDT) Received: from uucp (helo=alpha) by elvis.franken.de with local-bsmtp (Exim 3.36 #1) id 1nhe01-0007Hb-00; Thu, 21 Apr 2022 23:06:01 +0200 Received: by alpha.franken.de (Postfix, from userid 1000) id 79792C01A0; Thu, 21 Apr 2022 23:05:25 +0200 (CEST) Date: Thu, 21 Apr 2022 23:05:25 +0200 From: Thomas Bogendoerfer To: "Jason A. Donenfeld" Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, tglx@linutronix.de, arnd@arndb.de, Theodore Ts'o , Dominik Brodowski , Russell King , Catalin Marinas , Will Deacon , Geert Uytterhoeven , Paul Walmsley , Palmer Dabbelt , Albert Ou , "David S . Miller" , Richard Weinberger , Anton Ivanov , Johannes Berg , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Chris Zankel , Max Filippov , Stephen Boyd , Dinh Nguyen , linux-arm-kernel@lists.infradead.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org, linux-xtensa@linux-xtensa.org, "Maciej W . Rozycki" Subject: Re: [PATCH v5 04/11] mips: use fallback for random_get_entropy() instead of just c0 random Message-ID: <20220421210525.GA16228@alpha.franken.de> References: <20220419111650.1582274-1-Jason@zx2c4.com> <20220419111650.1582274-5-Jason@zx2c4.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220419111650.1582274-5-Jason@zx2c4.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-m68k@vger.kernel.org On Tue, Apr 19, 2022 at 01:16:43PM +0200, Jason A. Donenfeld wrote: > For situations in which we don't have a c0 counter register available, > we've been falling back to reading the c0 "random" register, which is > usually bounded by the amount of TLB entries and changes every other > cycle or so. This means it wraps extremely often. We can do better by > combining this fast-changing counter with a potentially slower-changing > counter from random_get_entropy_fallback() in the more significant bits. > This commit combines the two, taking into account that the changing bits > are in a different bit position depending on the CPU model. In addition, > we previously were falling back to 0 for ancient CPUs that Linux does > not support anyway; remove that dead path entirely. > > Cc: Thomas Gleixner > Cc: Arnd Bergmann > Cc: Thomas Bogendoerfer > Cc: Maciej W. Rozycki > Signed-off-by: Jason A. Donenfeld > --- > ThomasB - I dropped your Ack from v4, because this is pretty different > from v4 now. > > Maciej - you mentioned you had a test rig. Think you could provide a > "Tested-by" if this approach works? > > arch/mips/include/asm/timex.h | 16 +++++++--------- > 1 file changed, 7 insertions(+), 9 deletions(-) > > diff --git a/arch/mips/include/asm/timex.h b/arch/mips/include/asm/timex.h > index b05bb70a2e46..e3f5460a923b 100644 > --- a/arch/mips/include/asm/timex.h > +++ b/arch/mips/include/asm/timex.h > @@ -80,21 +80,19 @@ static inline cycles_t get_cycles(void) > /* > * Like get_cycles - but where c0_count is not available we desperately > * use c0_random in an attempt to get at least a little bit of entropy. > - * > - * R6000 and R6000A neither have a count register nor a random register. > - * That leaves no entropy source in the CPU itself. > */ > static inline unsigned long random_get_entropy(void) > { > - unsigned int prid = read_c0_prid(); > - unsigned int imp = prid & PRID_IMP_MASK; > + unsigned int c0_random; > > - if (can_use_mips_counter(prid)) > + if (can_use_mips_counter(read_c0_prid())) > return read_c0_count(); > - else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A)) > - return read_c0_random(); > + > + if (cpu_has_3kex) > + c0_random = (read_c0_random() >> 8) & 0x3f; > else > - return 0; /* no usable register */ > + c0_random = read_c0_random() & 0x3f; > + return (random_get_entropy_fallback() << 6) | (0x3f - c0_random); > } > #define random_get_entropy random_get_entropy > > -- > 2.35.1 Acked-by: Thomas Bogendoerfer -- Crap can work. Given enough thrust pigs will fly, but it's not necessarily a good idea. [ RFC1925, 2.3 ]