Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6030190iog; Thu, 23 Jun 2022 09:54:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uwP45X+rLLibnDqz6V8998nkugynxo4O3hL3U9+Up45pxNOZfQZZOzKXLYq9sdj6ppCTDe X-Received: by 2002:a05:6402:201:b0:431:665f:11f1 with SMTP id t1-20020a056402020100b00431665f11f1mr12320284edv.378.1656003283076; Thu, 23 Jun 2022 09:54:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656003283; cv=none; d=google.com; s=arc-20160816; b=tkVOka7aJupzsLGNNoktDoHRaWsz6/1oeCRWu+af90RNxMNUSuExcZpOnj75u1AOyu jVjmDv4pR4QsHoDBT6Yj9NfymnQqe5sTIhb6H1iHVQpsINFYmVjS7027/wjwSE5QauI5 bAQj2YLdJVmD9Z0MubEBWagGMY/atXuQXysIA5/N1oLX8GUTW30bFJ1694aa2E04nP0T n4FEmU+TjHSjXYJpRDNM/+3SYYf0K8RTQyXtldBfKpU0Tf/mld3xF7IPb7TCZ6eSGxbJ ey3cUSG1OC2K0648TeiPHgsLQpa+XnF+kWsLQbpp5C7zU2tVM89eWHE6JnwZBAYAYffV iYeQ== 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=z+hlkCkqrhNFYY4lGd8SKQs/sXRpRxYFl4D0Klv1RU8=; b=Y65jjxiMB2WNNnfB94uOcRLeWGwOMrcxJa8FmyFF74DTR1YuQ1KyXbTVEWZo+Fws5k BPGONkxXUr79TqKi3OjU/Eq4wx+eM9KHx2boeydM5QvCwZE2sWeXG7rYV2pxhy9syaLt Wcuk+B3GUL/IFlfkc42QoAgp8d6Pz+fa6znLjkoOvoByR7ME6Hyt1feYefDWWkvqaPBm zoWkgtqmmxrTSTpoEMHXv7HnD+dXisgwwk2r/9Dviy2NaX6HdiilZGGa9HjwzBnUcBR5 72sxQhijf4fMBD+F0eiVka4JbNSYin+ixZS2eB5/CNft1WIPasXBS5LTKRdsplMfGo/x 1uGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="xHC/jaPe"; 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 y7-20020a170906518700b0071235bb5185si1609583ejk.712.2022.06.23.09.54.17; Thu, 23 Jun 2022 09:54:43 -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="xHC/jaPe"; 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 S232804AbiFWQsf (ORCPT + 99 others); Thu, 23 Jun 2022 12:48:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232091AbiFWQrQ (ORCPT ); Thu, 23 Jun 2022 12:47:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 246F449B5E; Thu, 23 Jun 2022 09:47:15 -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 A82E561F91; Thu, 23 Jun 2022 16:47:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71F04C341C4; Thu, 23 Jun 2022 16:47:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656002833; bh=JGZcpj+IYPHOuDGpesA9sgFvi7K45kzC1jyunNeIqVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xHC/jaPeAdNQFo1WGpmpuokJIu1TTOVTbknwi5W7yxZ+c+gx07/9QcAIaxxtEFJKX Kbc8achk0km4/KXpyGbaeo5LHx51F/3l1fdNFGS54HwkCDBTcmtEUEEqFfb6lCeA+K 1ycJxfMJsPKV/rLhrbOd+lVV2QkgLuKy67uK4e0c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Jason A. Donenfeld" , Theodore Tso Subject: [PATCH 4.9 006/264] random: convert get_random_int/long into get_random_u32/u64 Date: Thu, 23 Jun 2022 18:39:59 +0200 Message-Id: <20220623164344.239578504@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@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 c440408cf6901eeb2c09563397e24a9097907078 upstream. Many times, when a user wants a random number, he wants a random number of a guaranteed size. So, thinking of get_random_int and get_random_long in terms of get_random_u32 and get_random_u64 makes it much easier to achieve this. It also makes the code simpler. On 32-bit platforms, get_random_int and get_random_long are both aliased to get_random_u32. On 64-bit platforms, int->u32 and long->u64. Signed-off-by: Jason A. Donenfeld Cc: Greg Kroah-Hartman Cc: Theodore Ts'o Signed-off-by: Theodore Ts'o Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 63 ++++++++++++++++++++++++++----------------------- include/linux/random.h | 17 +++++++++++-- 2 files changed, 49 insertions(+), 31 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -2089,57 +2089,62 @@ struct ctl_table random_table[] = { struct batched_entropy { union { - unsigned long entropy_long[CHACHA20_BLOCK_SIZE / sizeof(unsigned long)]; - unsigned int entropy_int[CHACHA20_BLOCK_SIZE / sizeof(unsigned int)]; + u64 entropy_u64[CHACHA20_BLOCK_SIZE / sizeof(u64)]; + u32 entropy_u32[CHACHA20_BLOCK_SIZE / sizeof(u32)]; }; unsigned int position; }; /* * Get a random word for internal kernel use only. The quality of the random - * number is good as /dev/urandom, but there is no backtrack protection, with - * the goal of being quite fast and not depleting entropy. + * number is either as good as RDRAND or as good as /dev/urandom, with the + * goal of being quite fast and not depleting entropy. */ -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_long); -unsigned long get_random_long(void) +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64); +u64 get_random_u64(void) { - unsigned long ret; + u64 ret; struct batched_entropy *batch; - batch = &get_cpu_var(batched_entropy_long); - if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) { - extract_crng((u8 *)batch->entropy_long); +#if BITS_PER_LONG == 64 + if (arch_get_random_long((unsigned long *)&ret)) + return ret; +#else + if (arch_get_random_long((unsigned long *)&ret) && + arch_get_random_long((unsigned long *)&ret + 1)) + return ret; +#endif + + batch = &get_cpu_var(batched_entropy_u64); + if (batch->position % ARRAY_SIZE(batch->entropy_u64) == 0) { + extract_crng((u8 *)batch->entropy_u64); batch->position = 0; } - ret = batch->entropy_long[batch->position++]; - put_cpu_var(batched_entropy_long); + ret = batch->entropy_u64[batch->position++]; + put_cpu_var(batched_entropy_u64); return ret; } -EXPORT_SYMBOL(get_random_long); +EXPORT_SYMBOL(get_random_u64); -#if BITS_PER_LONG == 32 -unsigned int get_random_int(void) -{ - return get_random_long(); -} -#else -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_int); -unsigned int get_random_int(void) +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32); +u32 get_random_u32(void) { - unsigned int ret; + u32 ret; struct batched_entropy *batch; - batch = &get_cpu_var(batched_entropy_int); - if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) { - extract_crng((u8 *)batch->entropy_int); + if (arch_get_random_int(&ret)) + return ret; + + batch = &get_cpu_var(batched_entropy_u32); + if (batch->position % ARRAY_SIZE(batch->entropy_u32) == 0) { + extract_crng((u8 *)batch->entropy_u32); batch->position = 0; } - ret = batch->entropy_int[batch->position++]; - put_cpu_var(batched_entropy_int); + ret = batch->entropy_u32[batch->position++]; + put_cpu_var(batched_entropy_u32); return ret; } -#endif -EXPORT_SYMBOL(get_random_int); +EXPORT_SYMBOL(get_random_u32); /** * randomize_page - Generate a random, page aligned address --- a/include/linux/random.h +++ b/include/linux/random.h @@ -42,8 +42,21 @@ extern void get_random_bytes_arch(void * extern const struct file_operations random_fops, urandom_fops; #endif -unsigned int get_random_int(void); -unsigned long get_random_long(void); +u32 get_random_u32(void); +u64 get_random_u64(void); +static inline unsigned int get_random_int(void) +{ + return get_random_u32(); +} +static inline unsigned long get_random_long(void) +{ +#if BITS_PER_LONG == 64 + return get_random_u64(); +#else + return get_random_u32(); +#endif +} + unsigned long randomize_page(unsigned long start, unsigned long range); /*