Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6137348iog; Thu, 23 Jun 2022 12:07:33 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tMkw6PHF6r86sARFT18kzD2sQUGA1gE7fmvEUC+lgOFdyJ2fPVbrGyl5VPLv6K/lc6CEDj X-Received: by 2002:a17:902:a701:b0:16a:65b:f9f1 with SMTP id w1-20020a170902a70100b0016a065bf9f1mr33373562plq.73.1656011253583; Thu, 23 Jun 2022 12:07:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011253; cv=none; d=google.com; s=arc-20160816; b=B5zniWIzbcdwrMm67Xr7XXENeSKyMSwQyKr2AmRT4/iBBENeYwvs1SZghc2uHv0WGV MlZ6Bl5LfPkda81M6YzXbSMFlegaZbc/fJ2BS1KgNDUPUqtafGVP/ao7byT2iWruVwb3 YX0k1rCcL/fDnjlBBXXNRdZbdM5/6b3HFlXMZpfrg16vGAt6PazV8s2eGanDqM6MzbJ+ t3EtNY6Il5L1y/1mjZLzTYQ4qOEUaCUEt5Kwy0USq+ueNMfN7RnIj8sgVG/k7V/aPTei GIR8tCNOmUgi4x4Zq+v2KlYklVmTicibj+p3n5+HpPnDuJb1xCU5tHXmCOgxIJrvnfNL 0LGw== 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=kdTF6u491dZWjos3XpCvHZZBxZZ809v6Z00UYrDMsXc=; b=hFr7DnRFWaGZDGz5bkIFpHIhSUvE50RiyD1MbXoLzZbo6C1qBn+bpXsFj+lDOcLYDG MYrHmRlP205OiPM5CqmS+RdvMAr6FNLBpbBc8GRI5n2HDn2Rdzb7rGraRdfFYCInlhEY GItyamS4lhnJgthNSLtVxsgNq8s9qq0/tHD/vLST8L0/XoWNkVUsdbI0cc95zybw3Ts+ jnAQnf4R79mZQE1rQv8k8Gvt7GaglhXAJ1rVdb1JBYx1cTZxpqdmoc/shis7KhpUOdLK 2uLv5zo/F8JavR1huab1+DTDwMGZZc7ZZXay/QH0zEJphd1XmMkLsgx8TLkM+uDy06Ts yrng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0n8nFJHn; 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 j11-20020a17090a2a8b00b001e312672c02si262281pjd.30.2022.06.23.12.07.20; Thu, 23 Jun 2022 12:07:33 -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=0n8nFJHn; 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 S236379AbiFWSK2 (ORCPT + 99 others); Thu, 23 Jun 2022 14:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235871AbiFWSGJ (ORCPT ); Thu, 23 Jun 2022 14:06:09 -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 1A8301B2; Thu, 23 Jun 2022 10:18:22 -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 7555561DE5; Thu, 23 Jun 2022 17:18:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D98BC341C4; Thu, 23 Jun 2022 17:18:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004701; bh=nwXc3XpthjYp9ixcMlB2623mIkpfvu/MjJP9HCed4NU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0n8nFJHnznllogMOTYXYJpwum/hH9Hlu8mwYXFtKhuQ9/6Ex4AKQ/YPHWk6Oa0es5 XkSHm8rYAxkCKaXrH1RkZoYveN7ahpJ6bjlZBPvY9PugXpZrsF2LIJtsyYfIxxp/tD V8bGtUYuZRf0sLholzvozIG1gmlSkThZ6PPo1oZA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.19 120/234] random: make consistent usage of crng_ready() Date: Thu, 23 Jun 2022 18:43:07 +0200 Message-Id: <20220623164346.453677394@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 a96cfe2d427064325ecbf56df8816c6b871ec285 upstream. Rather than sometimes checking `crng_init < 2`, we should always use the crng_ready() macro, so that should we change anything later, it's consistent. Additionally, that macro already has a likely() around it, which means we don't need to open code our own likely() and unlikely() annotations. Cc: Theodore Ts'o Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -125,18 +125,13 @@ static void try_to_generate_entropy(void */ int wait_for_random_bytes(void) { - if (likely(crng_ready())) - return 0; - - do { + while (!crng_ready()) { int ret; ret = wait_event_interruptible_timeout(crng_init_wait, crng_ready(), HZ); if (ret) return ret > 0 ? 0 : ret; - try_to_generate_entropy(); - } while (!crng_ready()); - + } return 0; } EXPORT_SYMBOL(wait_for_random_bytes); @@ -291,7 +286,7 @@ static void crng_reseed(void) ++next_gen; WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.birth, jiffies); - if (crng_init < 2) { + if (!crng_ready()) { crng_init = 2; finalize_init = true; } @@ -359,7 +354,7 @@ static void crng_make_state(u32 chacha_s * ready, we do fast key erasure with the base_crng directly, because * this is what crng_pre_init_inject() mutates during early init. */ - if (unlikely(!crng_ready())) { + if (!crng_ready()) { bool ready; spin_lock_irqsave(&base_crng.lock, flags); @@ -799,7 +794,7 @@ static void credit_entropy_bits(size_t n entropy_count = min_t(unsigned int, POOL_BITS, orig + add); } while (cmpxchg(&input_pool.entropy_count, orig, entropy_count) != orig); - if (crng_init < 2 && entropy_count >= POOL_MIN_BITS) + if (!crng_ready() && entropy_count >= POOL_MIN_BITS) crng_reseed(); } @@ -956,7 +951,7 @@ int __init rand_initialize(void) extract_entropy(base_crng.key, sizeof(base_crng.key)); ++base_crng.generation; - if (arch_init && trust_cpu && crng_init < 2) { + if (arch_init && trust_cpu && !crng_ready()) { crng_init = 2; pr_notice("crng init done (trusting CPU's manufacturer)\n"); } @@ -1545,7 +1540,7 @@ static long random_ioctl(struct file *f, case RNDRESEEDCRNG: if (!capable(CAP_SYS_ADMIN)) return -EPERM; - if (crng_init < 2) + if (!crng_ready()) return -ENODATA; crng_reseed(); return 0;