Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3726001pxb; Tue, 19 Apr 2022 08:39:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQbaXbQXZOnr4EJ6rZtfJc9nXWmN7dVjGtrq2J9Z1VPYIxTofAGqlXe7ZVjo8f9LbBJiD3 X-Received: by 2002:a17:907:62a6:b0:6ef:8118:d3e2 with SMTP id nd38-20020a17090762a600b006ef8118d3e2mr12462412ejc.605.1650382773394; Tue, 19 Apr 2022 08:39:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650382773; cv=none; d=google.com; s=arc-20160816; b=znfqSe9uowWkRcp9YTfW9b8Lbn8eA/zAUdf1fefQipMilIDXn97h6j5UMiL5Zcu1Oe Mx1s9b5Jbpajf40NI0rcdshsXnpA/Zi6vxWVlhc3r7X8D/KXMlQHnh1Lihnrd/sNGTYz JnNLh1ET+J0+P0HIq39Mgf6NsklLoPBM35hH4oEHcLp6h+oi2x9yjYLOdyFt3aAGEFS0 o9eu04mwztv60HJnJ2F6SfvvbSXiWbkIyedL2xMhGSVNqitLzh2YaTkD1XlxaGaigImP O7fLDimCW01jzEZXA0KBtaM5gK8uzHx86izcg4e3SHpSpERTpqm6+inUcHH5leOcbbyH dXjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=0EoN7tnN1LCPqubEUIru3BR6P29FWyCRZEgkgRKqrIs=; b=Pya7yuOA1yWvd9sbZt449IxFB2oZxvVRtaFYX0FhwdSoc1egZLGQr2Myqlwy25dZRf XQpzaTqwsMc5vrKa2KYxAb33v992bl7cHs/9KlJw8OjE2apqOfYuloUH4ppWM29LH/wY swOtQARwTj2UHMfvyO20/T2M2Po/ocGoEos9G9QKi6D94YOH4qNhIBRPs83o41jAW11W JvOdTITof9q6AOT5ZkCN0H8eOkphg97gNjfwMgQbl2BLN/y3zu26/EDAbvvex6aNrbeR 4h/oYzAtuS62EY3/dxkwbIUxp2k/GFnhz64Y0gGUBGmALbsxsdHxU4lztT0F3iWl8dfq ziSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b="eKu71o/w"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a1709064d4900b006df76385c23si8046940ejv.195.2022.04.19.08.38.59; Tue, 19 Apr 2022 08:39:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b="eKu71o/w"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351003AbiDSLUU (ORCPT + 99 others); Tue, 19 Apr 2022 07:20:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350979AbiDSLUQ (ORCPT ); Tue, 19 Apr 2022 07:20:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6175524BE0; Tue, 19 Apr 2022 04:17:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A656F61303; Tue, 19 Apr 2022 11:17:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0E8EC385A7; Tue, 19 Apr 2022 11:17:28 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="eKu71o/w" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1650367046; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=0EoN7tnN1LCPqubEUIru3BR6P29FWyCRZEgkgRKqrIs=; b=eKu71o/wuLb8HMfrlfRkyKaK3L3SGE3X4RYEu0LRXYWKWx//EBIj6NogsRI1FlmFf/WRDb KY7tvUqrgUIPUY2sMJPkRYudWOx36rh7R+nR0QmtE8WSLPjSn46Y+Jyp+wJaVp7f6O9o9O YBM8ta6GUXOcD1Fk09CXNIL2XCb+2Mc= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 0623ad04 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Tue, 19 Apr 2022 11:17:26 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, tglx@linutronix.de, arnd@arndb.de Cc: "Jason A. Donenfeld" , Theodore Ts'o , Dominik Brodowski , Russell King , Catalin Marinas , Will Deacon , Geert Uytterhoeven , Thomas Bogendoerfer , 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 Subject: [PATCH v5 00/11] archs/random: fallback to best raw ktime when no cycle counter Date: Tue, 19 Apr 2022 13:16:39 +0200 Message-Id: <20220419111650.1582274-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Hi folks, The RNG uses a function called random_get_entropy() basically anytime that it needs to timestamp an event. For example, an interrupt comes in, and we mix a random_get_entropy() into the entropy pool somehow. Somebody mashes their keyboard or moves their mouse around? We mix a random_get_entropy() into the entropy pool. It's one of the main varieties of input. Unfortunately, it's always 0 on a few platforms. The RNG has accumulated various hacks to deal with this, but in general it's not great. Surely we can do better than 0. In fact, *anything* that's not the same exact value all the time would be better than 0. Even a counter that increments once per hour would be better than 0! I think you get the idea. On most platforms, random_get_entropy() is aliased to get_cycles(), which makes sense for platforms where get_cycles() is defined. RDTSC, for example, has all the characteristics we care about for this function: it's fast to acquire (i.e. acceptable in an irq handler), pretty high precision, available, forms a 2-monotone distribution, etc. But for platforms without that, what is the next best thing? Sometimes the next best thing is architecture-defined. For example, really old MIPS has the C0 random register, which isn't quite a cycle counter, but is at least something. However, some platforms don't even have an architecture-defined fallback. Fortunately, the timekeeping subsystem has already solved this problem of trying to determine what the least bad clock is on constrained systems, falling back to jiffies in the worst case. By exporting the raw clock, we can get a decent fallback function for when there's no cycle counter or architecture-specific function. This series makes the RNG more useful on: m68k, RISC-V, MIPS, ARM32, NIOS II, SPARC32, Xtensa, and Usermode Linux. Previously these platforms would, in certain circumstances, but out of luck with regards to having any type of event timestamping source in the RNG. Finally, note that this series isn't about "jitter entropy" or other ways of initializing the RNG. That's a different topic for a different thread. Please don't let this discussion veer off into that. Here, I'm just trying to find a good fallback counter/timer for platforms without get_cycles(), a question with limited scope. If this (or a future revision) looks good to you all and receives the requisite acks, my plan was to take these through the random.git tree for 5.19, so that I can then build on top of it. Thanks, Jason Changes v4->v5: - Do not prototype symbol with 'extern', according to style guide. - On MIPS, combine random_get_entropy_fallback() with the c0 random register in a way that matches the format of the c0 random value, so that we get the best of a high precision cycle counter and of larger period timer, joined together. As a result, Thomas Bogendoerfer's ack on v4 of patch 4 has been dropped, since this is a substantial change. Changes v3->v4: - Use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL. Changes v2->v3: - Name the fallback function random_get_entropy_fallback(), so that it can be changed out as needed. - Include header with prototype in timekeeping.c to avoid compiler warning. - Export fallback function symbol. Changes v1->v2: - Use ktime_read_raw_clock() instead of sched_clock(), per Thomas' suggestion. - Drop arm64 change. - Cleanup header inclusion ordering problem. Cc: Thomas Gleixner Cc: Arnd Bergmann Cc: Theodore Ts'o Cc: Dominik Brodowski Cc: Russell King Cc: Catalin Marinas Cc: Will Deacon Cc: Geert Uytterhoeven Cc: Thomas Bogendoerfer Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: David S. Miller Cc: Richard Weinberger Cc: Anton Ivanov Cc: Johannes Berg Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: H. Peter Anvin Cc: Chris Zankel Cc: Max Filippov Cc: Stephen Boyd Cc: Dinh Nguyen Cc: linux-arm-kernel@lists.infradead.org Cc: linux-m68k@lists.linux-m68k.org Cc: linux-mips@vger.kernel.org Cc: linux-riscv@lists.infradead.org Cc: sparclinux@vger.kernel.org Cc: linux-um@lists.infradead.org Cc: x86@kernel.org Cc: linux-xtensa@linux-xtensa.org Jason A. Donenfeld (11): timekeeping: add raw clock fallback for random_get_entropy() m68k: use fallback for random_get_entropy() instead of zero riscv: use fallback for random_get_entropy() instead of zero mips: use fallback for random_get_entropy() instead of just c0 random arm: use fallback for random_get_entropy() instead of zero nios2: use fallback for random_get_entropy() instead of zero x86: use fallback for random_get_entropy() instead of zero um: use fallback for random_get_entropy() instead of zero sparc: use fallback for random_get_entropy() instead of zero xtensa: use fallback for random_get_entropy() instead of zero random: insist on random_get_entropy() existing in order to simplify arch/arm/include/asm/timex.h | 1 + arch/m68k/include/asm/timex.h | 2 +- arch/mips/include/asm/timex.h | 16 +++--- arch/nios2/include/asm/timex.h | 2 + arch/riscv/include/asm/timex.h | 2 +- arch/sparc/include/asm/timex_32.h | 4 +- arch/um/include/asm/timex.h | 9 +--- arch/x86/include/asm/timex.h | 10 ++++ arch/xtensa/include/asm/timex.h | 6 +-- drivers/char/random.c | 89 ++++++++++--------------------- include/linux/timex.h | 8 +++ kernel/time/timekeeping.c | 10 ++++ 12 files changed, 74 insertions(+), 85 deletions(-) -- 2.35.1