Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6141244iog; Thu, 23 Jun 2022 12:12:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1upcwicavh9uBAdEcP/fDCE8Dz7M0mw+V175BNXMeEpD/R7oQE+gbUA47JOvP9ZrdNFbKUf X-Received: by 2002:a63:7e45:0:b0:40c:8f6a:4d94 with SMTP id o5-20020a637e45000000b0040c8f6a4d94mr8793803pgn.562.1656011529028; Thu, 23 Jun 2022 12:12:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011529; cv=none; d=google.com; s=arc-20160816; b=0Zra05r0v536xM4qNcz+qm1wtguOj/FIV7geSOW/shB4r8GIcBnFKNv+EA8L4miTYp DKRbKTpa5Kfe4RBFfYLok4shxZFm1x+pAKbbnCkrQPmILTDOzAhOiuo03p5FHTrDOQ9k 3dgIpnVKU5GyLsQ09LcIXP6CwW8N0d2eJi4bBJ0y2zVHsVfuxoI8n6ACrbXjnAzmHZPr QbHcy7f+r0sFGrVksEvZ2iUIaLt3GIG1aosk0c5xu5q4tzEUR6xumdw06wergMYhwo4B da9esIQ/axlsI3GJd7hYPNvSdIBOnf3+hNvlHs6FX3owPzJClMCIlw2KSmZz3up7G5hr 1dww== 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=8q7mZ9YAQdn3y1lqkcNjxnfD2zlZlpLFCbeKeLA6bns=; b=aE5oJm6m2evrdCZL2z9wwX1vYlKgUwHAOkfpFm6Dve3ppHL3SyvYkucX2fxQc3aBc+ pYpeLM7jaszH6ckeVjs3xYyjHG1EKWU0XxBKXWwe4fPfIxyDAxMU/4RwWda1cxIY+15Y OYhnZ95O1ZObbF6u5cgCeUiGZz186aBg2AtQIMYxiw5uZck8KlIAQC7Pn/OQzAHt64lL YLdJYgvzOAwSeEJ9FjA7marPJPu+C71mrvMUX1u+LKh8QHd2pk2sDJn4EbKuucJMhFb3 MKxSm2GTYixMxzONKujdgIaj0LJIEeAYZdgPbo0Kv9D83/E97gr1NqGo8FB6EIdBa9VJ pKQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KwHTph92; 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 y65-20020a633244000000b0040504f9e815si29536416pgy.81.2022.06.23.12.11.57; Thu, 23 Jun 2022 12:12:09 -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=KwHTph92; 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 S235972AbiFWSGM (ORCPT + 99 others); Thu, 23 Jun 2022 14:06:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235965AbiFWSFg (ORCPT ); Thu, 23 Jun 2022 14:05:36 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE1F2A1E22; Thu, 23 Jun 2022 10:17:39 -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 ams.source.kernel.org (Postfix) with ESMTPS id 6F0EBB824BD; Thu, 23 Jun 2022 17:17:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD828C3411B; Thu, 23 Jun 2022 17:17:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004657; bh=TCJl6FuUorjJdZNz0hMu0SoH6UjitZkvOShRYmM2Qac=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KwHTph92F3/LfeBqG2cSBBSF9/i7GIMHuXAxp8hBFbKjHd1dpiuSbH8rE2azK4K9W En40DgbJwVTkwnK8BKsuW1jxwHCMvhnrnzGHToNR3bSV2+/oqeBfH8LG42FUwaEs46 hIL05iTa1lvlXxL2WU2g7LSMI+99IQR5Hv/KXXy0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sebastian Andrzej Siewior , Dominik Brodowski , Eric Biggers , Andy Lutomirski , =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= , "Jason A. Donenfeld" Subject: [PATCH 4.19 081/234] random: remove batched entropy locking Date: Thu, 23 Jun 2022 18:42:28 +0200 Message-Id: <20220623164345.350543448@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 77760fd7f7ae3dfd03668204e708d1568d75447d upstream. Rather than use spinlocks to protect batched entropy, we can instead disable interrupts locally, since we're dealing with per-cpu data, and manage resets with a basic generation counter. At the same time, we can't quite do this on PREEMPT_RT, where we still want spinlocks-as- mutexes semantics. So we use a local_lock_t, which provides the right behavior for each. Because this is a per-cpu lock, that generation counter is still doing the necessary CPU-to-CPU communication. This should improve performance a bit. It will also fix the linked splat that Jonathan received with a PROVE_RAW_LOCK_NESTING=y. Reviewed-by: Sebastian Andrzej Siewior Reviewed-by: Dominik Brodowski Reviewed-by: Eric Biggers Suggested-by: Andy Lutomirski Reported-by: Jonathan Neuschäfer Tested-by: Jonathan Neuschäfer Link: https://lore.kernel.org/lkml/YfMa0QgsjCVdRAvJ@latitude/ Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 58 +++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1719,13 +1719,15 @@ struct ctl_table random_table[] = { }; #endif /* CONFIG_SYSCTL */ +static atomic_t batch_generation = ATOMIC_INIT(0); + struct batched_entropy { union { u64 entropy_u64[CHACHA20_BLOCK_SIZE / sizeof(u64)]; u32 entropy_u32[CHACHA20_BLOCK_SIZE / sizeof(u32)]; }; unsigned int position; - spinlock_t batch_lock; + int generation; }; /* @@ -1736,9 +1738,7 @@ struct batched_entropy { * wait_for_random_bytes() should be called and return 0 at least once at any * point prior. */ -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { - .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u64.lock), -}; +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64); u64 get_random_u64(void) { @@ -1746,67 +1746,63 @@ u64 get_random_u64(void) unsigned long flags; struct batched_entropy *batch; static void *previous; + int next_gen; warn_unseeded_randomness(&previous); + local_irq_save(flags); batch = raw_cpu_ptr(&batched_entropy_u64); - spin_lock_irqsave(&batch->batch_lock, flags); - if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { + + next_gen = atomic_read(&batch_generation); + if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0 || + next_gen != batch->generation) { extract_crng((u8 *)batch->entropy_u64); batch->position = 0; + batch->generation = next_gen; } + ret = batch->entropy_u64[batch->position++]; - spin_unlock_irqrestore(&batch->batch_lock, flags); + local_irq_restore(flags); return ret; } EXPORT_SYMBOL(get_random_u64); -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { - .batch_lock = __SPIN_LOCK_UNLOCKED(batched_entropy_u32.lock), -}; +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32); + u32 get_random_u32(void) { u32 ret; unsigned long flags; struct batched_entropy *batch; static void *previous; + int next_gen; warn_unseeded_randomness(&previous); + local_irq_save(flags); batch = raw_cpu_ptr(&batched_entropy_u32); - spin_lock_irqsave(&batch->batch_lock, flags); - if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { + + next_gen = atomic_read(&batch_generation); + if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0 || + next_gen != batch->generation) { extract_crng((u8 *)batch->entropy_u32); batch->position = 0; + batch->generation = next_gen; } + ret = batch->entropy_u32[batch->position++]; - spin_unlock_irqrestore(&batch->batch_lock, flags); + local_irq_restore(flags); return ret; } 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 - * simply resetting the counter to zero so that it's re-extracted on the - * next usage. */ + * bumping the generation counter. + */ static void invalidate_batched_entropy(void) { - int cpu; - unsigned long flags; - - for_each_possible_cpu(cpu) { - struct batched_entropy *batched_entropy; - - batched_entropy = per_cpu_ptr(&batched_entropy_u32, cpu); - spin_lock_irqsave(&batched_entropy->batch_lock, flags); - batched_entropy->position = 0; - spin_unlock(&batched_entropy->batch_lock); - - batched_entropy = per_cpu_ptr(&batched_entropy_u64, cpu); - spin_lock(&batched_entropy->batch_lock); - batched_entropy->position = 0; - spin_unlock_irqrestore(&batched_entropy->batch_lock, flags); - } + atomic_inc(&batch_generation); } /**