Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6147537iog; Thu, 23 Jun 2022 12:19:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vGnBwuc5M7DZQdVg1Z9qHKXKdl20JD/GvetHQ+kmq9rWNUZxZjHa9KtZ7dtiWVLQ6iFilW X-Received: by 2002:a17:906:6d98:b0:715:76eb:9e33 with SMTP id h24-20020a1709066d9800b0071576eb9e33mr9569397ejt.729.1656011972280; Thu, 23 Jun 2022 12:19:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011972; cv=none; d=google.com; s=arc-20160816; b=wfTlCgE/VZV9gtm+N9x6UMpnOT0ooOkYbnAfb+Ywil76Ntv2Akq1Bwk1eBZ/S8nX3/ GF9wfwZqN2cgBkWaJ6qCskWj7Pijgnm++tjXBeUmuJpRxJLN2EcH4aKTwzAudKFWwIO3 usXr9mdb3XtioBVSCnGPHlLWELKM00Q1rrdyQOR7c+//GPBBpKZx4Kjqe4iQVFNyz8MP GOr1IwHbtEScPB/VLMNd5X6gZN3iul7palaGH6rDh+YD2W+HKs9DhwnEmyMYCQ7qIb5L 12Rvi+YEa3vAHaB2FQcg2YMeZgowIKEWS+ce+nIfHHGlbnfX8XGor8oiTPEa25mP5hie l5Xg== 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=v4H1BpCNrePdmRYgfThAwrCUbT9suxCPzTsAnitXUL8=; b=sWx1ZnlHm7nkJKwBfuR1zxCe3IbhKLTASyHwd6cmZr096mp7OdccgnjqS7A1Ke3K/V qHdmbGNoc6CANh2Bubvr/ammEj33CFkJFolOaTIGhNOWGJHWx3UTzgEoWjIMS8KqS6jV cv76nBe39Biwqw1y0aivzL0qEUGTcR1noIHpELup/mk5Fm9yJnTsXDNOjuvt0iU3iUAc PuRmu6Ie8dD71n63H+02MX0MwaSovx6RwWjuD8l8g/Yp8jjKp1xGPIaFV+Ub0CdHpMmh DDPrN//3ESX9dTt01U3CckbDp9b+4JxErk4tocM8TijJq+rhWaGZ+XtyAOfpIbL1pZ6x vC7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=x8IRn3tv; 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 mp42-20020a1709071b2a00b00716646dc05fsi10226556ejc.860.2022.06.23.12.19.04; Thu, 23 Jun 2022 12:19:32 -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=x8IRn3tv; 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 S235570AbiFWR4V (ORCPT + 99 others); Thu, 23 Jun 2022 13:56:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235646AbiFWRw7 (ORCPT ); Thu, 23 Jun 2022 13:52:59 -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 55F07A8B6E; Thu, 23 Jun 2022 10:13: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 6E4E961D17; Thu, 23 Jun 2022 17:13:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A550C3411B; Thu, 23 Jun 2022 17:13:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004426; bh=MuUF0Ibb2Dea1LHOE/d3WkUCemoyfGQMOCwEgo8brko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x8IRn3tvZuGhJPDEw97SJGKzbwcmeltD2h6y2cjCMUYTzjyY+mResQK+b0i54sMTz TFHGX96ICRmePvo090f5i9yTT4mwbKGtKR7aXYVPnJKTg+Vaas5gdrIMsYOE266LNm oBmYBPplg8i8Y0FUQVui80j3TyWpk2omiApTYrv0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mark Rutland , Mark Brown , Theodore Tso , "Jason A. Donenfeld" Subject: [PATCH 4.19 033/234] random: split primary/secondary crng init paths Date: Thu, 23 Jun 2022 18:41:40 +0200 Message-Id: <20220623164344.003169695@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: Mark Rutland commit 5cbe0f13b51ac2fb2fd55902cff8d0077fc084c0 upstream. Currently crng_initialize() is used for both the primary CRNG and secondary CRNGs. While we wish to share common logic, we need to do a number of additional things for the primary CRNG, and this would be easier to deal with were these handled in separate functions. This patch splits crng_initialize() into crng_initialize_primary() and crng_initialize_secondary(), with common logic factored out into a crng_init_try_arch() helper. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland Cc: Mark Brown Cc: Theodore Ts'o Link: https://lore.kernel.org/r/20200210130015.17664-2-mark.rutland@arm.com Signed-off-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -783,27 +783,37 @@ static int __init parse_trust_cpu(char * } early_param("random.trust_cpu", parse_trust_cpu); -static void crng_initialize(struct crng_state *crng) +static bool crng_init_try_arch(struct crng_state *crng) { int i; - int arch_init = 1; + bool arch_init = true; unsigned long rv; - memcpy(&crng->state[0], "expand 32-byte k", 16); - if (crng == &primary_crng) - _extract_entropy(&input_pool, &crng->state[4], - sizeof(__u32) * 12, 0); - else - _get_random_bytes(&crng->state[4], sizeof(__u32) * 12); for (i = 4; i < 16; i++) { if (!arch_get_random_seed_long(&rv) && !arch_get_random_long(&rv)) { rv = random_get_entropy(); - arch_init = 0; + arch_init = false; } crng->state[i] ^= rv; } - if (trust_cpu && arch_init && crng == &primary_crng) { + + return arch_init; +} + +static void crng_initialize_secondary(struct crng_state *crng) +{ + memcpy(&crng->state[0], "expand 32-byte k", 16); + _get_random_bytes(&crng->state[4], sizeof(__u32) * 12); + crng_init_try_arch(crng); + crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; +} + +static void __init crng_initialize_primary(struct crng_state *crng) +{ + memcpy(&crng->state[0], "expand 32-byte k", 16); + _extract_entropy(&input_pool, &crng->state[4], sizeof(__u32) * 12, 0); + if (crng_init_try_arch(crng) && trust_cpu) { invalidate_batched_entropy(); numa_crng_init(); crng_init = 2; @@ -854,7 +864,7 @@ static void do_numa_crng_init(struct wor crng = kmalloc_node(sizeof(struct crng_state), GFP_KERNEL | __GFP_NOFAIL, i); spin_lock_init(&crng->lock); - crng_initialize(crng); + crng_initialize_secondary(crng); pool[i] = crng; } /* pairs with READ_ONCE() in select_crng() */ @@ -1792,7 +1802,7 @@ int __init rand_initialize(void) init_std_data(&input_pool); if (crng_need_final_init) crng_finalize_init(&primary_crng); - crng_initialize(&primary_crng); + crng_initialize_primary(&primary_crng); crng_global_init_time = jiffies; if (ratelimit_disable) { urandom_warning.interval = 0;