From patchwork Tue Aug 28 20:13:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Pundir X-Patchwork-Id: 145360 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp1589143ljw; Tue, 28 Aug 2018 13:13:44 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbBUUmuSNqk3+PeL9KLBL2puWTT5qZ1gUYKfIoaCy6QPOKiEmdcYsimhoJ4c45EHWB+toqX X-Received: by 2002:a17:902:e190:: with SMTP id cd16-v6mr2879357plb.305.1535487224859; Tue, 28 Aug 2018 13:13:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535487224; cv=none; d=google.com; s=arc-20160816; b=sYH5MV3GJvisi1gt1vJ6hVFVus5xwVYvggf90gvI7tD1jgx49+/biK9duAI+YJBK0G J54xGlwPDJ+6FzRub4WQRHlxqXpXOIsOWO23sEYKZMFtsaTYxLgBox/a7glLenCiQGpN Tio+2qIHeZUgxb1jCBfbpC6R1ESSLEIdpC1uMFbAfjbZbQQYJkdOp32cR2TDgKNCXeZl gzClLI+dGXD2jLbVB7ENUi1bqI+dkAyhUWizgKt1aBX2q4y56tH2i4ovXEc+A+G2cu5D cbT9wCgiRbwVVSfdP3KoLqzeLHTqHY+nTTnmugaBmG3MEP2c3Ju5blw48CUK87EyIKKr 5qWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=NXgqaTe7MzaXWf5UCSOBEWEUXIZMj00LRl/OCoFVpiQ=; b=USAgDPHDgI+9PPTfD3y7yE6Qurk9ZuVJ7Hmc/LZLML8xN+ekkr1MPW2zmaB5tfsc0Q uXzRlL1A5MYI/N4skuHi9vNCUAOiZaSHxtvTFUjvakOMQ5+xR/42jIrYeTngtB5dogak tRXY52eb4Q3TnS/hpQjUJ4+lAbSE+4G9lIC5NucO/VSw6E/mDdzBrp6W8l8LQVJ2Xm0y TUHU4pX6UlBmYkpHit3j1RrALu0i2Ydzz00RXtPJrKzXmwLr9W3guu5g04FPZLyZ/eJf bARhATVvF3M/JR/C5vU/t3ZShXDdh7RyosdG6Ll8mV3xtOrt60lum4h6+X5SUCtaTsfE SKAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=P1RhRRGN; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10-v6si1906969pgf.312.2018.08.28.13.13.44; Tue, 28 Aug 2018 13:13:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=P1RhRRGN; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727104AbeH2AG6 (ORCPT + 13 others); Tue, 28 Aug 2018 20:06:58 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:44521 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726961AbeH2AG6 (ORCPT ); Tue, 28 Aug 2018 20:06:58 -0400 Received: by mail-pl1-f194.google.com with SMTP id ba4-v6so1195711plb.11 for ; Tue, 28 Aug 2018 13:13:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NXgqaTe7MzaXWf5UCSOBEWEUXIZMj00LRl/OCoFVpiQ=; b=P1RhRRGNgEF0EFv8KeZpXvlzy85Lqi2EHv1y8fSZAjxIepSxvmFEKbL8tuUI9khPvc /5hOklkclEjNW+C2RAGyihzjYX3zO1Np4mon2c2XyVGxU9GJRHIhTTQxcWBspMI6LQD0 n6/rZmp5Wh2bbn5kpSGC7fszsF0L3LM58B8gk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NXgqaTe7MzaXWf5UCSOBEWEUXIZMj00LRl/OCoFVpiQ=; b=GnZ8XHNsLoleZp1DL7J6F3Axd2y0aZrCUeu3J0fxMXA6S31YYJgmTI52w4EHkdWSBB 0e67YpzoaNqFRE0+jrKytrql1lFtTGBHKejjcRWiGLmhYpqXVUXs+dPXJyvz5Jcmqy6a 9auWOu52N6WPGGWN2yhMtNbmQyXk7ov38h75k1lP4n9iQJvrc242y0n0LbGEvH1r/kcg cT1/ESkH7AfYBYggCJ89Y5pdKeKBPKRf7EdkXtvofY3mZnKbLzNiJYu17/SdeabuU54U PoA7xzlpTLzb1aJec3uBhlGdErBvrArDwYXhsdRovlt+fLIxvN/iDPzVzCPjX+TOMgn5 VE3Q== X-Gm-Message-State: APzg51DaJJxfmrCuwcn/tny8iSASReEXFfh0eyDINm8VLj1KZQBWqCIs Z4jMMbLGPoezT810zyq6Q4ji+w== X-Received: by 2002:a17:902:850c:: with SMTP id bj12-v6mr2987043plb.330.1535487223130; Tue, 28 Aug 2018 13:13:43 -0700 (PDT) Received: from localhost.localdomain ([49.207.48.21]) by smtp.gmail.com with ESMTPSA id t86-v6sm3098181pfe.109.2018.08.28.13.13.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Aug 2018 13:13:41 -0700 (PDT) From: Amit Pundir To: Greg KH Cc: Stable , "Jason A. Donenfeld" , Theodore Ts'o Subject: [PATCH for-4.9.y 04/14] random: convert get_random_int/long into get_random_u32/u64 Date: Wed, 29 Aug 2018 01:43:15 +0530 Message-Id: <1535487205-26280-5-git-send-email-amit.pundir@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1535487205-26280-1-git-send-email-amit.pundir@linaro.org> References: <1535487205-26280-1-git-send-email-amit.pundir@linaro.org> Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@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: Amit Pundir --- drivers/char/random.c | 55 +++++++++++++++++++++++++------------------------- include/linux/random.h | 17 ++++++++++++++-- 2 files changed, 42 insertions(+), 30 deletions(-) -- 2.7.4 diff --git a/drivers/char/random.c b/drivers/char/random.c index 81b65d0e7563..464b95a63dc5 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -2110,8 +2110,8 @@ 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; }; @@ -2121,52 +2121,51 @@ struct batched_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; - if (arch_get_random_long(&ret)) +#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_long); - if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) { - extract_crng((u8 *)batch->entropy_long); + 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; if (arch_get_random_int(&ret)) return ret; - batch = &get_cpu_var(batched_entropy_int); - if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) { - extract_crng((u8 *)batch->entropy_int); + 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 diff --git a/include/linux/random.h b/include/linux/random.h index 16ab429735a7..ed5c3838780d 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -42,8 +42,21 @@ extern void get_random_bytes_arch(void *buf, int nbytes); 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); u32 prandom_u32(void);