Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2931832iog; Mon, 20 Jun 2022 07:45:05 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tL348ehC7sAt8+Okr3xY9/CTTuTQDGdfvCMGpnPnjb7CTZYJcPgN65VCpcfe9sngm+EzV2 X-Received: by 2002:a17:902:ecc9:b0:163:f779:f97a with SMTP id a9-20020a170902ecc900b00163f779f97amr23559165plh.167.1655736305016; Mon, 20 Jun 2022 07:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655736305; cv=none; d=google.com; s=arc-20160816; b=Wbn7/DX1KrEzrnXYnyc9cyCwjSBI51XZhyQInjomqaSyqxj3l+264Tc/MtuW4YPihO RBl2/V6ThnlBRvLjlE6ffU0yqhDd0pNByNUt3hXo9EFcFQhr8ReDRQ4kJJVejO1EXHff rGhnTwc0uU7XM3vQR4Kc1SbavqMtKoJR33fDeEaC6k0YZUqMWu/54ORx1jInuzLegYHS sFRkk3yiOal/S35hrbOF7qfobezCLG1WZsYu3vASCwVjn2W+MiksBuVHdMIXgtAhT/RH 2aYd/6nnG6LObflNkpa0DjeO3hm5qmyZrxhxAjw+aGKeleixbIi6Ujnsf0hnJTMLNvYO uN9w== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GBsq33IG7w/nIU2lbNpbZ3GrPr8LsCxxakp6ynYFGX4=; b=r3rjZbwBk1Tix683DHZV36yZHD029BOWPpo0+lL6qg9CaCJtVhD994eKidf2EBUgf3 CDcwN4d9Wmvu/NOnHoeY2vNXCuWFSAVVIcWaYlVNbCrmQfueEsGjbuYfZjU758WfJ4ao k7EnuL0gbI4D1GAbejmzAtqN8+fnZZe5uZFKPKqsLo9n726S7AxPlBXRRKXTxK5XZLiP 5+v8mQFaoeqiPX4Sums/s+w2couyoEYLc63hszkB4IP9sqc26XF2wbiH+3lVHDnvo7yK CoeOyBXNdHLJ4OG60kY6gGgTVId+QicAytrxuDV5xY3RkHUium4AJJQ09mexBA0qfQut 0RPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=W2oNKjbM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id on10-20020a17090b1d0a00b001e866c2c666si21755586pjb.94.2022.06.20.07.44.52; Mon, 20 Jun 2022 07:45:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=W2oNKjbM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245292AbiFTNuV (ORCPT + 99 others); Mon, 20 Jun 2022 09:50:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348435AbiFTNsA (ORCPT ); Mon, 20 Jun 2022 09:48:00 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 239692ED77; Mon, 20 Jun 2022 06:17:42 -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 ams.source.kernel.org (Postfix) with ESMTPS id 32049B811C7; Mon, 20 Jun 2022 13:17:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8613EC3411B; Mon, 20 Jun 2022 13:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655731055; bh=Af/mC/zq4tKhaB0LwhH5Vk0R8nnqO8wO9ms7HejQAZU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W2oNKjbMQ4P8qx0z9w9bfJZl3Zqx2orA9I1JQwplwhIUstHC8Mul8szqs+G73ylUd gwtVpvvZ4WJ8SZqzCUgGQigHdoMEdvU15aECPYblxAU2Mb3z4ZAIK3Z18PRumd5kuK PtotSEwmaiLIJ/48h+iAi/MGmHR8inXQHaFYrjmU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Gleixner , Arnd Bergmann , "Maciej W. Rozycki" , Thomas Bogendoerfer , "Jason A. Donenfeld" Subject: [PATCH 5.4 138/240] mips: use fallback for random_get_entropy() instead of just c0 random Date: Mon, 20 Jun 2022 14:50:39 +0200 Message-Id: <20220620124743.007639709@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620124737.799371052@linuxfoundation.org> References: <20220620124737.799371052@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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-kernel@vger.kernel.org From: "Jason A. Donenfeld" commit 1c99c6a7c3c599a68321b01b9ec243215ede5a68 upstream. 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 Tested-by: Maciej W. Rozycki Acked-by: Thomas Bogendoerfer Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- arch/mips/include/asm/timex.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) --- a/arch/mips/include/asm/timex.h +++ b/arch/mips/include/asm/timex.h @@ -76,25 +76,24 @@ static inline cycles_t get_cycles(void) else return 0; /* no usable counter */ } +#define get_cycles get_cycles /* * 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