Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2399559ioo; Sat, 28 May 2022 12:24:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwN6z+jz9ntGKzBZc8/iWDUqolLCFpq5t+wib7AQfPvczqcbTd+C5VBeIS4XfqfQEGhc24A X-Received: by 2002:a63:4545:0:b0:3fa:9d25:91b8 with SMTP id u5-20020a634545000000b003fa9d2591b8mr22221998pgk.80.1653765868303; Sat, 28 May 2022 12:24:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653765868; cv=none; d=google.com; s=arc-20160816; b=LBSCFg/a1mFlfx0ODKp6HCm0Sgz9IYnGeCCLCnZUJLHM10/dJ2gXYFmqqAGYaz9bld ucm9wZw8WraNBvxQ4pHcmWVe7GiYpBp6unS7tUc7Sp9e/bNOZpuBeYUPBOGKBHzWvSVO eGvA3+kT7DCIK7V7p2qhCXWK96BtLlaGE9b4qGu3/FxBq/H4FUphnTaRhZ551AZkkrwV GjAYtyFWT4Il7CzeAqlpk755vu1zqAPuVJEoN2XJ5aibep3paMNQ7ga+QcuF9rZs5/eR el9D7z9/UCt08Z6+LicCjKQqRjoPBnxBC88RKQtrRgfsTBF4WVGF6DKffyQ3PwXhNA0U dr1Q== 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=W9pgRurCBWnmV9cIhxkx96tNv6g1okLpaBtMRzbkE/0=; b=wqdqvFs/Va52FKG6g0liyWFJdikfaovyYpMRsX28MNrG1Z8WHdnQqqYUQTUi49l8pV iHtP27+uBbJZRUxufQoklbUYEcSIRxevS4PklkEy4xY5X+RYo+1F46A8ZLHzIBBo93Mk no0SZkoof9Uhh0Lh8ZA8/t5fykklZrQdodth/Q1kHXyht5VC9BvRU6EhDAVbR/eSzH1n nk5h6lX0eXKbN00KmDF1cxXW/+kUBvjQWY5MWPQgeERcuEZxdoK/IyLAUmq1tM+E87y/ b/gRU2E24qtI6O4dAXts/1CU7bv1f+ATmXSBPcksklk4ME7LCRUwy0FIi9cDQTPgsHJq kQLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=n1tLtarb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d7-20020a056a0010c700b004fa843103dbsi10293554pfu.193.2022.05.28.12.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 May 2022 12:24:28 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=n1tLtarb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 40BFD4DF73; Sat, 28 May 2022 11:54:53 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350234AbiE0IzJ (ORCPT + 99 others); Fri, 27 May 2022 04:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350189AbiE0IyK (ORCPT ); Fri, 27 May 2022 04:54:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44F08544F6; Fri, 27 May 2022 01:52:59 -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 05FD161D3B; Fri, 27 May 2022 08:52:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB374C36AE7; Fri, 27 May 2022 08:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653641578; bh=yLXlgNDwc3M6yVpHiI52bGwxKJCHaWRA55AS1O+jWyU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n1tLtarbEIg94fDtNH02PVVMvdQaSzezrh+yKMJSPIH5GHTshYwBZzPzGwo9fwBiw gO3dSljv8MIJ9V1vXP0EYXP/TMUnDxaIgALHVAjnAm//ceA7/QqpSdWUdK/3/9yn6z agoeaesTvytaLlUTt2JUj5/tckGhY2738Nvu533s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 5.18 30/47] random: move initialization out of reseeding hot path Date: Fri, 27 May 2022 10:50:10 +0200 Message-Id: <20220527084806.466052475@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220527084801.223648383@linuxfoundation.org> References: <20220527084801.223648383@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=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 68c9c8b192c6dae9be6278e98ee44029d5da2d31 upstream. Initialization happens once -- by way of credit_init_bits() -- and then it never happens again. Therefore, it doesn't need to be in crng_reseed(), which is a hot path that is called multiple times. It also doesn't make sense to have there, as initialization activity is better associated with initialization routines. After the prior commit, crng_reseed() now won't be called by multiple concurrent callers, which means that we can safely move the "finialize_init" logic into crng_init_bits() unconditionally. Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -264,7 +264,6 @@ static void crng_reseed(void) unsigned long flags; unsigned long next_gen; u8 key[CHACHA_KEY_SIZE]; - bool finalize_init = false; extract_entropy(key, sizeof(key)); @@ -281,28 +280,10 @@ static void crng_reseed(void) ++next_gen; WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.birth, jiffies); - if (!crng_ready()) { + if (!crng_ready()) crng_init = CRNG_READY; - finalize_init = true; - } spin_unlock_irqrestore(&base_crng.lock, flags); memzero_explicit(key, sizeof(key)); - if (finalize_init) { - process_random_ready_list(); - wake_up_interruptible(&crng_init_wait); - kill_fasync(&fasync, SIGIO, POLL_IN); - pr_notice("crng init done\n"); - if (unseeded_warning.missed) { - pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n", - unseeded_warning.missed); - unseeded_warning.missed = 0; - } - if (urandom_warning.missed) { - pr_notice("%d urandom warning(s) missed due to ratelimiting\n", - urandom_warning.missed); - urandom_warning.missed = 0; - } - } } /* @@ -834,10 +815,25 @@ static void credit_init_bits(size_t nbit new = min_t(unsigned int, POOL_BITS, orig + add); } while (cmpxchg(&input_pool.init_bits, orig, new) != orig); - if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) - crng_reseed(); - else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { + if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) { + crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ + process_random_ready_list(); + wake_up_interruptible(&crng_init_wait); + kill_fasync(&fasync, SIGIO, POLL_IN); + pr_notice("crng init done\n"); + if (unseeded_warning.missed) { + pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n", + unseeded_warning.missed); + unseeded_warning.missed = 0; + } + if (urandom_warning.missed) { + pr_notice("%d urandom warning(s) missed due to ratelimiting\n", + urandom_warning.missed); + urandom_warning.missed = 0; + } + } else if (orig < POOL_EARLY_BITS && new >= POOL_EARLY_BITS) { spin_lock_irqsave(&base_crng.lock, flags); + /* Check if crng_init is CRNG_EMPTY, to avoid race with crng_reseed(). */ if (crng_init == CRNG_EMPTY) { extract_entropy(base_crng.key, sizeof(base_crng.key)); crng_init = CRNG_EARLY;