From nobody Fri Sep 19 01:11:34 2025 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 857F0C4321E for ; Wed, 30 Nov 2022 20:03:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229726AbiK3UDJ (ORCPT ); Wed, 30 Nov 2022 15:03:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbiK3UDH (ORCPT ); Wed, 30 Nov 2022 15:03:07 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59F2A23BFA for ; Wed, 30 Nov 2022 12:03:06 -0800 (PST) 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 sin.source.kernel.org (Postfix) with ESMTPS id 84398CE1B0A for ; Wed, 30 Nov 2022 20:03:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05C5AC433C1; Wed, 30 Nov 2022 20:03:01 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="ddpmDtlY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1669838580; 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=1vH/NcoP2zEaCp8SEXsclpeZY780y8IHRUsr5heJWX8=; b=ddpmDtlYq+odsPu2QUDUygcoSXrMGP7Ot8jHYu9zRWQ7eoYjl7RNrwfXvlQvn1x73rQ7MG 752Dg0fz3wnQqDVH7C7Oum214dOKVkJxUdPm/zWbCmGduUSzYDzXm8BzbIcHoLcH9k5caG FQWwjAwpue1bClbkErdNk0hj3Xmls0Q= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id dd68b3be (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 30 Nov 2022 20:03:00 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Eric Biggers Subject: [PATCH v2] random: align entropy_timer_state to cache line Date: Wed, 30 Nov 2022 21:02:53 +0100 Message-Id: <20221130200253.511606-1-Jason@zx2c4.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The theory behind the jitter dance is that multiple things are poking at the same cache line. This only works, however, if what's being poked at is actually all in the same cache line. Ensure this is the case by aligning the struct on the stack to the cache line size. We can't use ____cacheline_aligned on a stack variable, because gcc assumes 16 byte alignment when only 8 byte alignment is provided by the kernel, which means gcc could technically do something pathological like `(rsp & ~48) - 64`. It doesn't, but rather than risk it, just do the stack alignment manually with PTR_ALIGN and an oversized buffer. Fixes: 50ee7529ec45 ("random: try to actively add entropy rather than passi= vely wait for it") Cc: Eric Biggers Signed-off-by: Jason A. Donenfeld --- drivers/char/random.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index acb9548a870e..46bb81c2da6e 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1265,29 +1265,30 @@ static void __cold entropy_timer(struct timer_list = *timer) static void __cold try_to_generate_entropy(void) { enum { NUM_TRIAL_SAMPLES =3D 8192, MAX_SAMPLES_PER_BIT =3D HZ / 15 }; - struct entropy_timer_state stack; + u8 stack_bytes[sizeof(struct entropy_timer_state) + SMP_CACHE_BYTES - 1]; + struct entropy_timer_state *stack =3D PTR_ALIGN((void *)stack_bytes, SMP_= CACHE_BYTES); unsigned int i, num_different =3D 0; unsigned long last =3D random_get_entropy(); int cpu =3D -1; =20 for (i =3D 0; i < NUM_TRIAL_SAMPLES - 1; ++i) { - stack.entropy =3D random_get_entropy(); - if (stack.entropy !=3D last) + stack->entropy =3D random_get_entropy(); + if (stack->entropy !=3D last) ++num_different; - last =3D stack.entropy; + last =3D stack->entropy; } - stack.samples_per_bit =3D DIV_ROUND_UP(NUM_TRIAL_SAMPLES, num_different += 1); - if (stack.samples_per_bit > MAX_SAMPLES_PER_BIT) + stack->samples_per_bit =3D DIV_ROUND_UP(NUM_TRIAL_SAMPLES, num_different = + 1); + if (stack->samples_per_bit > MAX_SAMPLES_PER_BIT) return; =20 - atomic_set(&stack.samples, 0); - timer_setup_on_stack(&stack.timer, entropy_timer, 0); + atomic_set(&stack->samples, 0); + timer_setup_on_stack(&stack->timer, entropy_timer, 0); while (!crng_ready() && !signal_pending(current)) { /* * Check !timer_pending() and then ensure that any previous callback has= finished * executing by checking try_to_del_timer_sync(), before queueing the ne= xt one. */ - if (!timer_pending(&stack.timer) && try_to_del_timer_sync(&stack.timer) = >=3D 0) { + if (!timer_pending(&stack->timer) && try_to_del_timer_sync(&stack->timer= ) >=3D 0) { struct cpumask timer_cpus; unsigned int num_cpus; =20 @@ -1312,20 +1313,20 @@ static void __cold try_to_generate_entropy(void) } while (cpu =3D=3D smp_processor_id() && num_cpus > 1); =20 /* Expiring the timer at `jiffies` means it's the next tick. */ - stack.timer.expires =3D jiffies; + stack->timer.expires =3D jiffies; =20 - add_timer_on(&stack.timer, cpu); + add_timer_on(&stack->timer, cpu); =20 preempt_enable(); } - mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); + mix_pool_bytes(&stack->entropy, sizeof(stack->entropy)); schedule(); - stack.entropy =3D random_get_entropy(); + stack->entropy =3D random_get_entropy(); } - mix_pool_bytes(&stack.entropy, sizeof(stack.entropy)); + mix_pool_bytes(&stack->entropy, sizeof(stack->entropy)); =20 - del_timer_sync(&stack.timer); - destroy_timer_on_stack(&stack.timer); + del_timer_sync(&stack->timer); + destroy_timer_on_stack(&stack->timer); } =20 =20 --=20 2.38.1