Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6133039iog; Thu, 23 Jun 2022 12:02:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uUhCmeQV4v0LO6pF5M7Px9t4ROVS8a3q8lemD+aXnpUoKj2qQwyX2kQiJS5jAIP2HkHyQJ X-Received: by 2002:a17:902:7582:b0:16a:307a:5965 with SMTP id j2-20020a170902758200b0016a307a5965mr17282851pll.159.1656010978808; Thu, 23 Jun 2022 12:02:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656010978; cv=none; d=google.com; s=arc-20160816; b=Ys4R0RWvfyiXes1Sft8j4UgAk1AtoYZv4JwtkQ6LLfxadE7ohZCXjs3Ya8K5IMOM+x TZ9B3gjaCTEQfjDbfeBeY4UcTkY5lGNbVhcAA5sP+5jeZcF65UMaAlmNmSyCeFr3UlkB Y4UMhdbF/yCwz0CzGN+wIx6bo2ihKx6nuTU1t+O5qj2I7dIqFA6U1KpUcCzgOMidAmGq ABeiMx90ErtgsXAytfJgpWgD6rbgvU2OqtZ+s8zf4B3sKfF8XQEh5OX0rt4ihVVZH0qv 9jzlSwcVqRWu+n9RVx6zXlw2rzTCl2m7TeXTyINDhQvlQyygIPTrBl4oKslEHLoW0ixR 3Zng== 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=4nOjJ272P+dF1zV97hkqVVEU5tc/SQ+7zE1MRyv+InE=; b=EYq33LCz06f8AAN/1QNj9A9XwSTIlLUruC7h9Mx8k9rj9Jz5CFd4Lz4smzjI6G3aH5 o8R9FQqane/ZPiwl2WqrJ2O3EB2FOpX6FJZ45Gc/az+ZQHrk/pMVczokmhDFKN9bfFqv aAx+9aT8+5fK8zVKG8wJi38CRqnEiOS6iNmCNgZIirGfMDglNRH3+zCKVsZyl1BLAcpQ HGpaRJ1xEUyONlQhCqoiTqJ3BkwmEP2XktFBNHfS8fq/sKGxezkU1XJX4Wa6erSBT80n yeGk6RZUSnCbQazlu3S76B8KLecR6z42pBeNQHeKltXZ13VKf552oCx2RKVRFmzpBY15 LDZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VwTzUmpY; 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 e6-20020a170902ef4600b0016a52b34991si491178plx.113.2022.06.23.12.02.47; Thu, 23 Jun 2022 12:02:58 -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=VwTzUmpY; 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 S235553AbiFWSDq (ORCPT + 99 others); Thu, 23 Jun 2022 14:03:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234185AbiFWSBe (ORCPT ); Thu, 23 Jun 2022 14:01:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5712B3D16; Thu, 23 Jun 2022 10:16:43 -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 36CE261DED; Thu, 23 Jun 2022 17:16:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A3B3C3411B; Thu, 23 Jun 2022 17:16:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004602; bh=HYKlJalBN/zBqjl5rAmql1I6nxTBkUuGJ+YGBbJGJJY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VwTzUmpYgBrYPComHuplo5qKmCGpGKD30MM7CPWgFz/ExBIZzm7wy/QKRIZnUvIXQ bg+9XXqmFqGu1Wn/1iCmaXoyf1R9gwAdjO0OKjnZLYz0wDnAyrAAOJozXGSe7LZQQP PaM3lJ8T2TwSMpFclxIUvWN7uC8cWZu9ydY6KihE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Theodore Tso , Eric Biggers , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.19 091/234] random: tie batched entropy generation to base_crng generation Date: Thu, 23 Jun 2022 18:42:38 +0200 Message-Id: <20220623164345.633845719@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 0791e8b655cc373718f0f58800fdc625a3447ac5 upstream. Now that we have an explicit base_crng generation counter, we don't need a separate one for batched entropy. Rather, we can just move the generation forward every time we change crng_init state or update the base_crng key. Cc: Theodore Ts'o Reviewed-by: Eric Biggers Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -428,8 +428,6 @@ static DEFINE_PER_CPU(struct crng, crngs static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); -static void invalidate_batched_entropy(void); - /* * crng_fast_load() can be called by code in the interrupt service * path. So we can't afford to dilly-dally. Returns the number of @@ -452,7 +450,7 @@ static size_t crng_fast_load(const void src++; crng_init_cnt++; len--; ret++; } if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { - invalidate_batched_entropy(); + ++base_crng.generation; crng_init = 1; } spin_unlock_irqrestore(&base_crng.lock, flags); @@ -529,7 +527,6 @@ static void crng_reseed(void) WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.birth, jiffies); if (crng_init < 2) { - invalidate_batched_entropy(); crng_init = 2; finalize_init = true; } @@ -1254,8 +1251,9 @@ int __init rand_initialize(void) mix_pool_bytes(utsname(), sizeof(*(utsname()))); extract_entropy(base_crng.key, sizeof(base_crng.key)); + ++base_crng.generation; + if (arch_init && trust_cpu && crng_init < 2) { - invalidate_batched_entropy(); crng_init = 2; pr_notice("crng init done (trusting CPU's manufacturer)\n"); } @@ -1593,8 +1591,6 @@ struct ctl_table random_table[] = { }; #endif /* CONFIG_SYSCTL */ -static atomic_t batch_generation = ATOMIC_INIT(0); - struct batched_entropy { union { /* @@ -1607,8 +1603,8 @@ struct batched_entropy { u64 entropy_u64[CHACHA20_BLOCK_SIZE * 3 / (2 * sizeof(u64))]; u32 entropy_u32[CHACHA20_BLOCK_SIZE * 3 / (2 * sizeof(u32))]; }; + unsigned long generation; unsigned int position; - int generation; }; /* @@ -1627,14 +1623,14 @@ u64 get_random_u64(void) unsigned long flags; struct batched_entropy *batch; static void *previous; - int next_gen; + unsigned long next_gen; warn_unseeded_randomness(&previous); local_irq_save(flags); batch = raw_cpu_ptr(&batched_entropy_u64); - next_gen = atomic_read(&batch_generation); + next_gen = READ_ONCE(base_crng.generation); if (batch->position >= ARRAY_SIZE(batch->entropy_u64) || next_gen != batch->generation) { _get_random_bytes(batch->entropy_u64, sizeof(batch->entropy_u64)); @@ -1660,14 +1656,14 @@ u32 get_random_u32(void) unsigned long flags; struct batched_entropy *batch; static void *previous; - int next_gen; + unsigned long next_gen; warn_unseeded_randomness(&previous); local_irq_save(flags); batch = raw_cpu_ptr(&batched_entropy_u32); - next_gen = atomic_read(&batch_generation); + next_gen = READ_ONCE(base_crng.generation); if (batch->position >= ARRAY_SIZE(batch->entropy_u32) || next_gen != batch->generation) { _get_random_bytes(batch->entropy_u32, sizeof(batch->entropy_u32)); @@ -1683,15 +1679,6 @@ u32 get_random_u32(void) } EXPORT_SYMBOL(get_random_u32); -/* It's important to invalidate all potential batched entropy that might - * be stored before the crng is initialized, which we can do lazily by - * bumping the generation counter. - */ -static void invalidate_batched_entropy(void) -{ - atomic_inc(&batch_generation); -} - /** * randomize_page - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.