Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6032038iog; Thu, 23 Jun 2022 09:57:29 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vdzcxTWbAfgnBSICjXhUOGaIR0N4+VSqRid+bfIDSAgESeCPUcjAIVLg2TCP826T61FXT8 X-Received: by 2002:a17:906:c10:b0:6f4:6c70:b00f with SMTP id s16-20020a1709060c1000b006f46c70b00fmr9097703ejf.660.1656003449051; Thu, 23 Jun 2022 09:57:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003449; cv=none; d=google.com; s=arc-20160816; b=KYi/saRUtaz0lxequfJduWnY0E8Ehzaunl1bzOH5vH8iFva4qIiCh1UcFDP2DIL3Wi tX0NH27i35G8zRti9JJ7PCLnUMATqdFT5medBlQoGXRzhcuwlgQXxkcqD9tkB3vYHrdS GaD4XXDGUxLBfPAgqRh5wEVwdWTUWLdXxkrH48iyZbIOGHzGhUp5yRt1uDVA3uR1pA4E PXvIbyBFn/HWITqF4gh78fp6gqebKmOV0L4DKabH6JNEoDcLh3jR7hddg4X8gktGuNva i7u29lILe0VQaiZNCh0BK64HsSDqm1jXOFraVDrvcS0yvXOaq86yVhglYtpDNyBFo5n7 BCkA== 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=VRnOMr2rt+lAKK2CPqI7rKmrn40SaakQAeHmTnhuAG8=; b=Nqy1BtkfuBrCVE8J0Rg36pZaH6adOaZythGPHiN1rn65Qf+EaKlWNLXEdGB+sXMimB sNCdhdQPyS3D4LEGSzdCPRZ02Ca7thWOQPT6KlxNkqBMHrJoRWHbgeYn7d4BJsp+nxN7 BlSkktSPKcClQJTBGG0dOOa+KQbyxyTYBhKE0zJPCgLDJAepwYhZF3vbyStGd4etE/CV W49/7x04W+dqumwYWvpnGzLv2PlUS+vh0NArPNyMSRvdurC9NV5S9f+kXxUw4d6S+L7w qJSsaEXwtXstqGYjr/RQepAcMxvyq/TCC64mJtGiPPcbRsDDsFOofnFlgqreDOPYO5h5 /ttg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dxFRNb3U; 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 ho33-20020a1709070ea100b00722e679e588si6446125ejc.731.2022.06.23.09.57.03; Thu, 23 Jun 2022 09:57:29 -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=dxFRNb3U; 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 S231854AbiFWQzg (ORCPT + 99 others); Thu, 23 Jun 2022 12:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233910AbiFWQvw (ORCPT ); Thu, 23 Jun 2022 12:51:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6302C2656B; Thu, 23 Jun 2022 09:51:48 -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 EBCC961FC2; Thu, 23 Jun 2022 16:51:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2A6AC3411B; Thu, 23 Jun 2022 16:51:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003107; bh=05Y6AWTeW8ezSzmEjPC+H7znKCnSHRPXCjLjCPqM9cQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dxFRNb3U5LK8ZNPLxkKYVKv8IgxBXUnOWrDRYMovkf3MfLvOnx11oUxsLKjoy+f4f 1slGCyI6nr44wLzVQpdoT6NtvDne8kIiboty3zj8GLSNdNtDcZ8NK1PVRNvC2v5vbI 4fCSyTt1yJopombvnmfsDH/2H/4Dlf++Qod9FyYw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Ard Biesheuvel , "Jason A. Donenfeld" Subject: [PATCH 4.9 091/264] random: avoid superfluous call to RDRAND in CRNG extraction Date: Thu, 23 Jun 2022 18:41:24 +0200 Message-Id: <20220623164346.646040605@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@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 2ee25b6968b1b3c66ffa408de23d023c1bce81cf upstream. RDRAND is not fast. RDRAND is actually quite slow. We've known this for a while, which is why functions like get_random_u{32,64} were converted to use batching of our ChaCha-based CRNG instead. Yet CRNG extraction still includes a call to RDRAND, in the hot path of every call to get_random_bytes(), /dev/urandom, and getrandom(2). This call to RDRAND here seems quite superfluous. CRNG is already extracting things based on a 256-bit key, based on good entropy, which is then reseeded periodically, updated, backtrack-mutated, and so forth. The CRNG extraction construction is something that we're already relying on to be secure and solid. If it's not, that's a serious problem, and it's unlikely that mixing in a measly 32 bits from RDRAND is going to alleviate things. And in the case where the CRNG doesn't have enough entropy yet, we're already initializing the ChaCha key row with RDRAND in crng_init_try_arch_early(). Removing the call to RDRAND improves performance on an i7-11850H by 370%. In other words, the vast majority of the work done by extract_crng() prior to this commit was devoted to fetching 32 bits of RDRAND. Reviewed-by: Theodore Ts'o Acked-by: Ard Biesheuvel Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1074,7 +1074,7 @@ static void crng_reseed(struct crng_stat static void _extract_crng(struct crng_state *crng, __u32 out[CHACHA20_BLOCK_WORDS]) { - unsigned long v, flags, init_time; + unsigned long flags, init_time; if (crng_ready()) { init_time = READ_ONCE(crng->init_time); @@ -1084,8 +1084,6 @@ static void _extract_crng(struct crng_st &input_pool : NULL); } spin_lock_irqsave(&crng->lock, flags); - if (arch_get_random_long(&v)) - crng->state[14] ^= v; chacha20_block(&crng->state[0], out); if (crng->state[12] == 0) crng->state[13]++;