From patchwork Fri Sep 30 23:10:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 611072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3253AC433F5 for ; Fri, 30 Sep 2022 23:15:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231939AbiI3XPy (ORCPT ); Fri, 30 Sep 2022 19:15:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231473AbiI3XPw (ORCPT ); Fri, 30 Sep 2022 19:15:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43C45CEEB2; Fri, 30 Sep 2022 16:15:46 -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 ED162B82986; Fri, 30 Sep 2022 23:15:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9D20C433D6; Fri, 30 Sep 2022 23:15:42 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="lfnFBZfy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1664579740; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=tNSsweT9TTVejKwOARbU5QKMnoD9GUfRqiMtcILsJxo=; b=lfnFBZfytp82YCa4ht9ykeqc+yqNznvUWpO5CqL/0yr81o7fBldgTBvBuK0pJ7VeDv5Bhr I1CXTGthH5oXLSvIArTN4NOdy4dqoH6BiC5yvuUE7kyhSo1VUM6Nf6FRTzkFsjxyjcyfNh SzGaugAUL0nHwggvMYkSBCifY/5VajM= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id d1e3b0ec (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 30 Sep 2022 23:15:40 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Cc: "Jason A. Donenfeld" , Dominik Brodowski , Sebastian Andrzej Siewior , Sultan Alsawaf Subject: [PATCH 1/2] random: schedule jitter credit for next jiffy, not in two jiffies Date: Sat, 1 Oct 2022 01:10:49 +0200 Message-Id: <20220930231050.749824-1-Jason@zx2c4.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Counterintuitively, mod_timer(..., jiffies + 1) will cause the timer to fire not in the next jiffy, but in two jiffies. The way to cause the timer to fire in the next jiffy is with mod_timer(..., jiffies). Doing so then lets us bump the upper bound back up again. Fixes: 50ee7529ec45 ("random: try to actively add entropy rather than passively wait for it") Cc: Dominik Brodowski Cc: Sebastian Andrzej Siewior Cc: Sultan Alsawaf Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 64ee16ffb8b7..fdf15f5c87dd 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1205,7 +1205,7 @@ static void __cold entropy_timer(struct timer_list *timer) */ static void __cold try_to_generate_entropy(void) { - enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 30 }; + enum { NUM_TRIAL_SAMPLES = 8192, MAX_SAMPLES_PER_BIT = HZ / 15 }; struct entropy_timer_state stack; unsigned int i, num_different = 0; unsigned long last = random_get_entropy(); @@ -1224,7 +1224,7 @@ static void __cold try_to_generate_entropy(void) timer_setup_on_stack(&stack.timer, entropy_timer, 0); while (!crng_ready() && !signal_pending(current)) { if (!timer_pending(&stack.timer)) - mod_timer(&stack.timer, jiffies + 1); + mod_timer(&stack.timer, jiffies); mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); schedule(); stack.entropy = random_get_entropy(); From patchwork Fri Sep 30 23:10:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 611462 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77C80C4332F for ; Fri, 30 Sep 2022 23:15:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231841AbiI3XP4 (ORCPT ); Fri, 30 Sep 2022 19:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231936AbiI3XPx (ORCPT ); Fri, 30 Sep 2022 19:15:53 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 209F8733D1; Fri, 30 Sep 2022 16:15:47 -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 BC3B7B82A16; Fri, 30 Sep 2022 23:15:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F204CC433D7; Fri, 30 Sep 2022 23:15:43 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="LUGscwbH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1664579743; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jWzb8RyneGw8pW3WBofCymNzFIU3QNOzB4YZieczqrU=; b=LUGscwbH9pUzf0jPfr8WDVcbf7eayHBZVODzMAQ/fx6n/hkkm1guHFYb9wzyN4qcMI7IqA 2gmgxqevdKCe05Ild7d8AeEedqnActDgD9xQqqepRAtUJyF6q+XhsHgPKKHVqfkkCB4O3n hYI4xgy8kwepNNy43Feqq1QWrlSlKic= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id fd887418 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 30 Sep 2022 23:15:43 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org Cc: "Jason A. Donenfeld" , Dominik Brodowski , Sebastian Andrzej Siewior , Sultan Alsawaf Subject: [PATCH 2/2] random: spread out jitter callback to different CPUs Date: Sat, 1 Oct 2022 01:10:50 +0200 Message-Id: <20220930231050.749824-2-Jason@zx2c4.com> In-Reply-To: <20220930231050.749824-1-Jason@zx2c4.com> References: <20220930231050.749824-1-Jason@zx2c4.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Rather than merely hoping that the callback gets called on another CPU, arrange for that to actually happen, by round robining which CPU the timer fires on. This way, on multiprocessor machines, we exacerbate jitter by touching the same memory from multiple different cores. Cc: Dominik Brodowski Cc: Sebastian Andrzej Siewior Cc: Sultan Alsawaf Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index fdf15f5c87dd..74627b53179a 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1209,6 +1209,7 @@ static void __cold try_to_generate_entropy(void) struct entropy_timer_state stack; unsigned int i, num_different = 0; unsigned long last = random_get_entropy(); + int cpu = -1; for (i = 0; i < NUM_TRIAL_SAMPLES - 1; ++i) { stack.entropy = random_get_entropy(); @@ -1223,8 +1224,17 @@ static void __cold try_to_generate_entropy(void) stack.samples = 0; timer_setup_on_stack(&stack.timer, entropy_timer, 0); while (!crng_ready() && !signal_pending(current)) { - if (!timer_pending(&stack.timer)) - mod_timer(&stack.timer, jiffies); + if (!timer_pending(&stack.timer)) { + preempt_disable(); + do { + cpu = cpumask_next(cpu, cpu_online_mask); + if (cpu == nr_cpumask_bits) + cpu = cpumask_first(cpu_online_mask); + } while (cpu == smp_processor_id() && cpumask_weight(cpu_online_mask) > 1); + stack.timer.expires = jiffies; + add_timer_on(&stack.timer, cpu); + preempt_enable(); + } mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); schedule(); stack.entropy = random_get_entropy();