From nobody Tue Jun 23 22:23:23 2026 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 28BA1C433F5 for ; Thu, 24 Feb 2022 21:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234932AbiBXVbP (ORCPT ); Thu, 24 Feb 2022 16:31:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233103AbiBXVbM (ORCPT ); Thu, 24 Feb 2022 16:31:12 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 400E31CB02 for ; Thu, 24 Feb 2022 13:30:42 -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 ams.source.kernel.org (Postfix) with ESMTPS id DEC1DB82987 for ; Thu, 24 Feb 2022 21:30:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F545C340E9; Thu, 24 Feb 2022 21:30:38 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="jwiIR4DP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1645738237; 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=m8ShCrRN09bCwvZgwqSvS4YRLtf5+D1RwmqrPBor2dw=; b=jwiIR4DPK7vQmfa36TyTuRfCCU2cWvOV2lAqkp6O4zl15kpws2qKtOYjXRx4iDamYqHx/x rZXYPtSGpFUDSkj2X9cECXrAHkxwDqU95TrHJimTQyogxKXpbIlguCTaKlB1wJOelCov2F WzjrmisUbkXMlBeCLX81mbP+L0Cxylc= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 69b224fe (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Thu, 24 Feb 2022 21:30:36 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Theodore Ts'o , Dominik Brodowski Subject: [PATCH v2] random: unify cycles_t and jiffies usage and types Date: Thu, 24 Feb 2022 22:30:30 +0100 Message-Id: <20220224213030.75400-1-Jason@zx2c4.com> In-Reply-To: <20220224173948.34045-1-Jason@zx2c4.com> References: <20220224173948.34045-1-Jason@zx2c4.com> 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" random_get_entropy() returns a cycles_t, not an unsigned long, which is sometimes 64 bits on various 32-bit platforms, including x86. Conversely, jiffies is always unsigned long. This commit fixes things to use cycles_t for fields that use random_get_entropy() and unsigned long for fields that use jiffies. It's also good to mix in a cycles_t and a jiffies in the same way for both add_device_randomness and add_timer_randomness, rather than using xor in one case. Finally, we unify the order of these volatile reads, always reading the more precise cycles counter, and then jiffies, so that the cycle counter is as close to the event as possible. Cc: Theodore Ts'o Cc: Dominik Brodowski Signed-off-by: Jason A. Donenfeld --- Changes v1->v2: - [Dominik] Use unsigned long, not long, for jiffies, except when it represents a delta. - Also unify how jiffies and cycles combine, where they're used, and the order in which they're read. - Stylistic fixes. drivers/char/random.c | 46 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 19bf44b9ba0f..12a1b2cfd702 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1000,12 +1000,6 @@ int __init rand_initialize(void) return 0; } =20 -/* There is one of these per entropy source */ -struct timer_rand_state { - cycles_t last_time; - long last_delta, last_delta2; -}; - /* * Add device- or boot-specific data to the input pool to help * initialize it. @@ -1016,19 +1010,26 @@ struct timer_rand_state { */ void add_device_randomness(const void *buf, size_t size) { - unsigned long time =3D random_get_entropy() ^ jiffies; - unsigned long flags; + cycles_t cycles =3D random_get_entropy(); + unsigned long now =3D jiffies, flags; =20 if (crng_init =3D=3D 0 && size) crng_pre_init_inject(buf, size, false); =20 spin_lock_irqsave(&input_pool.lock, flags); + _mix_pool_bytes(&cycles, sizeof(cycles)); + _mix_pool_bytes(&now, sizeof(now)); _mix_pool_bytes(buf, size); - _mix_pool_bytes(&time, sizeof(time)); spin_unlock_irqrestore(&input_pool.lock, flags); } EXPORT_SYMBOL(add_device_randomness); =20 +/* There is one of these per entropy source */ +struct timer_rand_state { + unsigned long last_time; + long last_delta, last_delta2; +}; + /* * This function adds entropy to the entropy "pool" by using timing * delays. It uses the timer_rand_state structure to make an estimate @@ -1037,29 +1038,26 @@ EXPORT_SYMBOL(add_device_randomness); * The number "num" is also added to the pool - it should somehow describe * the type of event which just happened. This is currently 0-255 for * keyboard scan codes, and 256 upwards for interrupts. - * */ static void add_timer_randomness(struct timer_rand_state *state, unsigned = int num) { - struct { - long jiffies; - unsigned int cycles; - unsigned int num; - } sample; + cycles_t cycles =3D random_get_entropy(); + unsigned long now =3D jiffies, flags; long delta, delta2, delta3; =20 - sample.jiffies =3D jiffies; - sample.cycles =3D random_get_entropy(); - sample.num =3D num; - mix_pool_bytes(&sample, sizeof(sample)); + spin_lock_irqsave(&input_pool.lock, flags); + _mix_pool_bytes(&cycles, sizeof(cycles)); + _mix_pool_bytes(&now, sizeof(now)); + _mix_pool_bytes(&num, sizeof(num)); + spin_unlock_irqrestore(&input_pool.lock, flags); =20 /* * Calculate number of bits of randomness we probably added. * We take into account the first, second and third-order deltas * in order to make our estimate. */ - delta =3D sample.jiffies - READ_ONCE(state->last_time); - WRITE_ONCE(state->last_time, sample.jiffies); + delta =3D now - READ_ONCE(state->last_time); + WRITE_ONCE(state->last_time, now); =20 delta2 =3D delta - READ_ONCE(state->last_delta); WRITE_ONCE(state->last_delta, delta); @@ -1291,10 +1289,10 @@ static void mix_interrupt_randomness(struct work_st= ruct *work) void add_interrupt_randomness(int irq) { enum { MIX_INFLIGHT =3D 1U << 31 }; + cycles_t cycles =3D random_get_entropy(); + unsigned long now =3D jiffies; struct fast_pool *fast_pool =3D this_cpu_ptr(&irq_randomness); struct pt_regs *regs =3D get_irq_regs(); - unsigned long now =3D jiffies; - cycles_t cycles =3D random_get_entropy(); unsigned int new_count; =20 if (cycles =3D=3D 0) @@ -1356,7 +1354,7 @@ static void entropy_timer(struct timer_list *t) static void try_to_generate_entropy(void) { struct { - unsigned long now; + cycles_t now; struct timer_list timer; } stack; =20 --=20 2.35.1