Received: by 2002:a05:6a10:144:0:0:0:0 with SMTP id 4csp1304595pxw; Sat, 9 Apr 2022 19:23:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXu7gef6Nr0Otx03SK3bahRvJnpRSu6Whf70plpz9A8LbtwzH955reimeHB6PuwupF8jSD X-Received: by 2002:a17:907:761c:b0:6d6:e553:7bd1 with SMTP id jx28-20020a170907761c00b006d6e5537bd1mr24485149ejc.5.1649557430915; Sat, 09 Apr 2022 19:23:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649557430; cv=none; d=google.com; s=arc-20160816; b=wC8yiVJ7Rs1dkMJTVrUuOL8FmbtvtsxHX89wx5/Hw9DLGV91k6BHbwpOET9XD1QYc5 Y/l7wENpopTnEqE/lUz4xbWmWwWUdfDZS4DOQHjlwYiQE6GXsF8XV/cWu/hO/yy77NJ4 roeLPLIyjtSKgjtnKWZC5g4ajPOIxK8Og1dUmfc4Ap+LlVsz5Qg/uQZCpy91s279nZLE Ve93dJDDigNFBn6YlPfWejZof3rsBTS7kWVDTjRXVohT+YLzzz0pKvvcnuz9LVdUhhVs U+GPULqeIiYS4j26bXQ2ZiXmn5Amv+LxnNtSORuRMDgVUBS4OVoTrMjlNXv4peMibwWA NnZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=Ht/BjZKfEwbNRvmZyePyMT+aylBpF12k4dVD0rKNtw8=; b=txocgfeFLWcBF/8FpVt8WANMnx0ZP1bHcHOnsHZEpAWVcoqP8E0hPhUlQVqxFWplHO CrcuxJK9oOyYmZmcSZE8ETBHYMYKmBs+Gh9/dUHUEI3TG8shQiasrw/PbJQOuH0QZ7ka q61MpwSzL3JqBHjZ8VrO+riMaS66T64gZ7jCllzWRQMmeOHauRrtWrHSWy3/Tul3jIMF MEIxlZgONTKxCgbp2knifYP/y87rnzvqb3jFGCnLFQS6TnBqmois3sUaIYIsFt1HWHMu u8uN8sXT9K5rxuuShIVOzkrugClXdkBsdsM4UD59PyHFTDr08dfINJnxZyx1pKN6hKy9 716w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=AuRq0wCt; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p20-20020aa7c894000000b0041d717577bfsi1287641eds.215.2022.04.09.19.23.21; Sat, 09 Apr 2022 19:23:50 -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=@linutronix.de header.s=2020 header.b=AuRq0wCt; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230498AbiDIXbp (ORCPT + 99 others); Sat, 9 Apr 2022 19:31:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231228AbiDIXbo (ORCPT ); Sat, 9 Apr 2022 19:31:44 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FEDCDEE; Sat, 9 Apr 2022 16:29:36 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1649546972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Ht/BjZKfEwbNRvmZyePyMT+aylBpF12k4dVD0rKNtw8=; b=AuRq0wCtYXKcm0ryJ67Fp/GBt6cO3SpVd7CkUPfzGNUT87YnBYIl6wooRCxo0V2LKVufAe WruAMWs3kz06ZmLlyY2BfPNF+Mn3kMGDmBnqUAIeDdyFzC3xXyK9l5c1+wSdGQ03tVQfPJ RV4AhVLGmS0YZcCYNSYMlG1vu/XcNd7tscR4pyhRiCZYkA/IYnCAHheqwKo1LRtrkVRuS0 9WKJUAPO4GRrNkF5PYwAo6aRPKyz3ID7dl7/kd4fGzDwHM3O+aAEkPYVddEU86X1kEeGN7 ochLJcl/0cW+kNW7GARWG+dZDnaeEFAyMnCOj52aFtVrAyVaSbB6B5xsdlKXaQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1649546972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Ht/BjZKfEwbNRvmZyePyMT+aylBpF12k4dVD0rKNtw8=; b=vUyIxyQVS1fiHfbV4PIICFMXMOmWDlpsE1+vbv1Qj49gakWkhrvZAc3R+9XH7ZlvihGyvm qbHedOcHogR2iNAA== To: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, 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 , John Stultz , Stephen Boyd , 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: Re: [PATCH RFC v1 00/10] archs/random: fallback to using sched_clock() if no cycle counter In-Reply-To: <20220408182145.142506-1-Jason@zx2c4.com> References: <20220408182145.142506-1-Jason@zx2c4.com> Date: Sun, 10 Apr 2022 01:29:32 +0200 Message-ID: <87wnfxhm3n.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 Jason, On Fri, Apr 08 2022 at 20:21, Jason A. Donenfeld wrote: > Sometimes the next best thing is architecture-defined. For example, > really old MIPS has the CP0 random register, which isn't a cycle > counter, but is at least something. However, some platforms don't even > have an architecture-defined fallback. In that case, what are we left > with? > > By my first guess, we have ktime_get_boottime_ns(), jiffies, and > sched_clock(). It seems like sched_clock() has already done a lot of > work in being always available with some incrementing value, falling > back to jiffies as necessary. So this series goes with that as a > fallback, for when the architecture doesn't define random_get_entropy in > its own way and when there's no working cycle counter. sched_clock() is a halfways sane option, but yes as Arnd pointed out: > Another option would be falling back to different things on different > platforms. For example, Arnd mentioned to me that on m68k, > ktime_get_ns() might be better than sched_clock(), because it doesn't > use CONFIG_GENERIC_SCHED_CLOCK and therefore is only as good as > jiffies. ktime_get_ns() or the slightly faster variant ktime_get_mono_fast_ns() are usable. In the worst case they are jiffies driven too, but systems with jiffies clocksource are pretty much museum pieces. It's slightly slower than get_cycles() and a get_cyles() based sched_clock(), but you get the most granular clock of the platform automatically, which has it's charm too :) The bad news is that depending on the clocksource frequency the lower bits might never change. Always true for clocksource jiffies. sched_clock() has the same issue. But the below uncompiled hack gives you access to the 'best' clocksource of a machine, i.e. the one which the platform decided to be the one which is giving the best resolution. The minimal bitwidth of that is AFAICT 20 bits. In the jiffies case this will at least advance every tick. The price, e.g. on x86 would be that RDTSC would be invoked via an indirect function call. Not the end of the world... Thanks, tglx --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -646,6 +646,11 @@ static void halt_fast_timekeeper(const s update_fast_timekeeper(&tkr_dummy, &tk_fast_raw); } +u32 ktime_read_raw_clock(void) +{ + return tk_clock_read(&tk_core.timekeeper.tkr_mono); +} + static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)