All of lore.kernel.org
 help / color / mirror / Atom feed
From: Amit Pundir <amit.pundir@linaro.org>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: Stable <stable@vger.kernel.org>,
	"Jason A. Donenfeld" <Jason@zx2c4.com>,
	Theodore Ts'o <tytso@mit.edu>
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	[thread overview]
Message-ID: <1535487205-26280-5-git-send-email-amit.pundir@linaro.org> (raw)
In-Reply-To: <1535487205-26280-1-git-send-email-amit.pundir@linaro.org>

From: "Jason A. Donenfeld" <Jason@zx2c4.com>

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 <Jason@zx2c4.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
---
 drivers/char/random.c  | 55 +++++++++++++++++++++++++-------------------------
 include/linux/random.h | 17 ++++++++++++++--
 2 files changed, 42 insertions(+), 30 deletions(-)

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);
-- 
2.7.4

  parent reply	other threads:[~2018-08-29  0:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-28 20:13 [PATCH for-4.9.y 00/14] Few upstream fixes from OnePlus6's kernel tree Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 01/14] cfq: Give a chance for arming slice idle timer in case of group_idle Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 02/14] kthread: Fix use-after-free if kthread fork fails Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 03/14] kthread: fix boot hang (regression) on MIPS/OpenRISC Amit Pundir
2018-08-28 20:13   ` [OpenRISC] " Amit Pundir
2018-08-28 20:13 ` Amit Pundir [this message]
2018-09-16 13:29   ` [PATCH for-4.9.y 04/14] random: convert get_random_int/long into get_random_u32/u64 Greg KH
2018-08-28 20:13 ` [PATCH for-4.9.y 05/14] staging: rt5208: Fix a sleep-in-atomic bug in xd_copy_page Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 06/14] staging/rts5208: Fix read overflow in memcpy Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 07/14] IB/rxe: do not copy extra stack memory to skb Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 08/14] block,blkcg: use __GFP_NOWARN for best-effort allocations in blkcg Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 09/14] nl80211: fix null-ptr dereference on invalid mesh configuration Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 10/14] locking/rwsem-xadd: Fix missed wakeup due to reordering of load Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 11/14] selinux: use GFP_NOWAIT in the AVC kmem_caches Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 12/14] locking/osq_lock: Fix osq_lock queue corruption Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 13/14] mm, vmscan: clear PGDAT_WRITEBACK when zone is balanced Amit Pundir
2018-08-28 20:13 ` [PATCH for-4.9.y 14/14] mm: remove seemingly spurious reclaimability check from laptop_mode gating Amit Pundir
2018-09-16 13:36 ` [PATCH for-4.9.y 00/14] Few upstream fixes from OnePlus6's kernel tree Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1535487205-26280-5-git-send-email-amit.pundir@linaro.org \
    --to=amit.pundir@linaro.org \
    --cc=Jason@zx2c4.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.