Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6135696iog; Thu, 23 Jun 2022 12:05:37 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s1DHq+BXVW3RmVcN/Qhxrhzd3GCWszqIIHsFs3Da5znXmpnrixG5Pw+8TSajJl8HNdw+51 X-Received: by 2002:a05:6402:3893:b0:435:26a1:a52 with SMTP id fd19-20020a056402389300b0043526a10a52mr12408904edb.261.1656011136814; Thu, 23 Jun 2022 12:05:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011136; cv=none; d=google.com; s=arc-20160816; b=cA5LDpADmcm7ie4Z8MBaAzk8BERdpehtBJ409/dY9/sz0gb3I3XfXFn0+M4EpoW01U tbB/Tspp/qgUWgAH6VXXS1kripvBQvlq+UfmR9UupLUXFRE0rnNbBuE9O3jU3j0YHwCe Xb+amXJ/adApXzq2b0QQuPR/jdBfBAzfEcMH9mR1ILEkwAu3cbiUbGdEtWhiaJP+1Ezd UhgYdugmzGwwtAYr2W7VtE4eZ4wdvBLhVdCVavNy4s8MBUtGnbhkzAy1Gi/8do8w2lxC 9/uFFJeTNjz7jrql1iveB5/J34OLEQ8NYQbXSr7xLH+tFX2qSzdHnd/WEnDsLsPjCfxM WN0A== 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=Ig/boKh/s3BkhpqEmyTTTy/D0HbWmUPfe51JuKwvLOU=; b=r4x1XNdZPEhnUCvEQ0tlR7nlFcNu5VknHCDr1+zd5AmMulwaaJnhVnCmwXHVNBfu9h yqI30crjwj3Dxp9pWLQcmPUlgOnxO01AAE5hQpdzAAIpoAR/fobmvfNXGCMXXWyuQAfd 6yhQSkz3uQlUiHdioAVdqL6AeufVWTCo9RUGG56P4ri7MWMW6m2rAXIe5UMzkFulnJ04 zqc91j0+SM5KhfQ22njJQtXSqiZAYLnmMUBnjRmubrwU67u+aKaHq+fm4t533GliAhif Vc0myAnUS9qGC1POVYlVnYOHWR6XiyQmTARByzkB+b7Iq6TJBcexu60oyGXZepQh5h9l NyKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OsPYeaRE; 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 nb36-20020a1709071ca400b00722f72e1c15si8123513ejc.428.2022.06.23.12.05.10; Thu, 23 Jun 2022 12:05:36 -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=OsPYeaRE; 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 S236462AbiFWSNx (ORCPT + 99 others); Thu, 23 Jun 2022 14:13:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236632AbiFWSMe (ORCPT ); Thu, 23 Jun 2022 14:12:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E59ABEAAA; Thu, 23 Jun 2022 10:20:53 -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 C102561E07; Thu, 23 Jun 2022 17:20:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A560BC3411B; Thu, 23 Jun 2022 17:20:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004852; bh=dYx8PdSREqiQ5TasPNKE71jnKx6CBu7ljjQxcwdD4tw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OsPYeaRE0OPtzK/Jtc95j2TOA3eZrY2vhs9oKcmZPfhwMuTYuZFnppSjuGJUFBeeZ fQuvpZpae1aGuLFfw5tnbQOFb7C2wiRrlFRzPxjq+oxN1w7MZ9Uvut5JnMrqo1lMsj +gavF5ric/TvdpK6FiuVQJfryz5PnILUj2erIXTE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dominik Brodowski , Theodore Tso , Thomas Gleixner , "Jason A. Donenfeld" Subject: [PATCH 4.19 131/234] random: make random_get_entropy() return an unsigned long Date: Thu, 23 Jun 2022 18:43:18 +0200 Message-Id: <20220623164346.765491277@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 b0c3e796f24b588b862b61ce235d3c9417dc8983 upstream. Some implementations were returning type `unsigned long`, while others that fell back to get_cycles() were implicitly returning a `cycles_t` or an untyped constant int literal. That makes for weird and confusing code, and basically all code in the kernel already handled it like it was an `unsigned long`. I recently tried to handle it as the largest type it could be, a `cycles_t`, but doing so doesn't really help with much. Instead let's just make random_get_entropy() return an unsigned long all the time. This also matches the commonly used `arch_get_random_long()` function, so now RDRAND and RDTSC return the same sized integer, which means one can fallback to the other more gracefully. Cc: Dominik Brodowski Cc: Theodore Ts'o Acked-by: Thomas Gleixner Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 20 +++++++------------- include/linux/timex.h | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -1010,7 +1010,7 @@ int __init rand_initialize(void) */ void add_device_randomness(const void *buf, size_t size) { - cycles_t cycles = random_get_entropy(); + unsigned long cycles = random_get_entropy(); unsigned long flags, now = jiffies; if (crng_init == 0 && size) @@ -1041,8 +1041,7 @@ struct timer_rand_state { */ static void add_timer_randomness(struct timer_rand_state *state, unsigned int num) { - cycles_t cycles = random_get_entropy(); - unsigned long flags, now = jiffies; + unsigned long cycles = random_get_entropy(), now = jiffies, flags; long delta, delta2, delta3; spin_lock_irqsave(&input_pool.lock, flags); @@ -1297,8 +1296,7 @@ static void mix_interrupt_randomness(str void add_interrupt_randomness(int irq) { enum { MIX_INFLIGHT = 1U << 31 }; - cycles_t cycles = random_get_entropy(); - unsigned long now = jiffies; + unsigned long cycles = random_get_entropy(), now = jiffies; struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness); struct pt_regs *regs = get_irq_regs(); unsigned int new_count; @@ -1311,16 +1309,12 @@ void add_interrupt_randomness(int irq) if (cycles == 0) cycles = get_reg(fast_pool, regs); - if (sizeof(cycles) == 8) + if (sizeof(unsigned long) == 8) { irq_data.u64[0] = cycles ^ rol64(now, 32) ^ irq; - else { + irq_data.u64[1] = regs ? instruction_pointer(regs) : _RET_IP_; + } else { irq_data.u32[0] = cycles ^ irq; irq_data.u32[1] = now; - } - - if (sizeof(unsigned long) == 8) - irq_data.u64[1] = regs ? instruction_pointer(regs) : _RET_IP_; - else { irq_data.u32[2] = regs ? instruction_pointer(regs) : _RET_IP_; irq_data.u32[3] = get_reg(fast_pool, regs); } @@ -1367,7 +1361,7 @@ static void entropy_timer(struct timer_l static void try_to_generate_entropy(void) { struct { - cycles_t cycles; + unsigned long cycles; struct timer_list timer; } stack; --- a/include/linux/timex.h +++ b/include/linux/timex.h @@ -75,7 +75,7 @@ * By default we use get_cycles() for this purpose, but individual * architectures may override this in their asm/timex.h header file. */ -#define random_get_entropy() get_cycles() +#define random_get_entropy() ((unsigned long)get_cycles()) #endif /*