Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6159514iog; Thu, 23 Jun 2022 12:35:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sArasHwwiVZCs2Zbgn0+2gIgXoRe9X3kLLOFrQoO6ZJc7wXHHeY2yIBaQzsaU/+e729o0v X-Received: by 2002:a17:902:7c09:b0:16a:370a:e960 with SMTP id x9-20020a1709027c0900b0016a370ae960mr15416914pll.92.1656012935169; Thu, 23 Jun 2022 12:35:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656012935; cv=none; d=google.com; s=arc-20160816; b=PMxuKS7JHgqOGRcKBG6MlBb9/Df3bEUK7jYAJ47tn4IzyNiDKU9qpXqMP+mCN2cD/k Nlv2rNkcS3xhYBInIwM4FjqWx5/kdiKToXWhxcd9KsSWy267S+5wMiv2ZfH3lnaDfC5+ aHXRBzkvZIZMeAKxDAKR/3hRpuYg5aDp6D3cXVKlgm3jhFs3gix29VFzkpVZW2GbCOxd QoA1XopptmUSuMLNqYMlGiy7gShsVoflF0+YeXMzk2CpbaVBL4lf/1me7dbeUyIgzBeO JugI/OZSBksQJ5Fw19LOZQnajsilfH8qeGTPR30IGWFR79IiHO/MqJPqGpjVG8rQ6ZW0 XozQ== 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=uia1s1oMFREXkCU1yJl5JW9S0LQciHLmPBGJYyqDoqU=; b=o0yxlzFfjYX40BJ0CGJAUpA4uhSZCBMHJKqV08nVGgDtYqaQ/zxuF/L9T9JGyosHnX ySL93JTEK1AdsiprBWBBryfysxR/mgqTO/31JmpWyouVpBkqYVEcX64lYTwHgpVd2idU rk6jwiJdZqdsbpgEtpJTETKV4K84f0hKacxC4SJVHPi9aIalH3692MismbJdX5oTn+q+ +9V0Nn6S19xeo+a8KbnxU7YWq8EB63soheS0eJYaiHdDzym5KbWmrrGQdpQsDdj2Cf8K 6juZt5GcG/RjOdHsKRIVM5m99pwgOpB9WvstxJKw+7MzvhNRQOWKKihEGTh5Rv+jwVWG 1j7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SFLuci17; 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 g22-20020a056a001a1600b005254e555901si101687pfv.90.2022.06.23.12.35.23; Thu, 23 Jun 2022 12:35:35 -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=SFLuci17; 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 S236271AbiFWR7F (ORCPT + 99 others); Thu, 23 Jun 2022 13:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235324AbiFWRyC (ORCPT ); Thu, 23 Jun 2022 13:54:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D17AD19F; Thu, 23 Jun 2022 10:15:02 -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 68D8A61DEF; Thu, 23 Jun 2022 17:15:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DD6CC3411B; Thu, 23 Jun 2022 17:15:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004501; bh=nfQG9vZxTehggJVNcEgcwmRBt5LvwAP7cGduuYxmCCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SFLuci17pcBy0AZbSehxdh0IdaEHCfQyZr/oQ/oFdnE4d7Pq9XBFaAsdU0DPLDxeQ ud5xBr4w/QJyx/Ol8YJDNtV1cN7prvKLnCoiyYbMEhZ8McFfAw81HYjQHSr4qBCjyX lfkgSuWrbOqWkkShyDZt/+l7PyfefQ0EPa/KPvSc= 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.19 058/234] random: avoid superfluous call to RDRAND in CRNG extraction Date: Thu, 23 Jun 2022 18:42:05 +0200 Message-Id: <20220623164344.706327415@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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 @@ -1023,7 +1023,7 @@ static void crng_reseed(struct crng_stat static void _extract_crng(struct crng_state *crng, __u8 out[CHACHA20_BLOCK_SIZE]) { - unsigned long v, flags, init_time; + unsigned long flags, init_time; if (crng_ready()) { init_time = READ_ONCE(crng->init_time); @@ -1033,8 +1033,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]++;