Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1678883

[PATCH] random: reorder READ_ONCE() in get_random_uXX

From Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Newsgroups linux.kernel
Subject [PATCH] random: reorder READ_ONCE() in get_random_uXX
Date 2017-06-30 16:40 +0200
Message-ID <tY5fs-6Dz-35@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


Since commit 4a072c71f49b ("random: silence compiler warnings and fix
race") there is a READ_ONCE().
I doubt that this required here but I don't have the time to argue with
Jason. Instead I push the access further down so we don't read crng_init
if we can leave the function after arch_get_random_XXX(). This is
something the compiler did before the READ_ONCE() got it.

Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 drivers/char/random.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 01a260f67437..5e3765f6a61d 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -2041,7 +2041,7 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64);
 u64 get_random_u64(void)
 {
 	u64 ret;
-	bool use_lock = READ_ONCE(crng_init) < 2;
+	bool use_lock;
 	unsigned long flags = 0;
 	struct batched_entropy *batch;
 
@@ -2053,6 +2053,7 @@ u64 get_random_u64(void)
 	    arch_get_random_long((unsigned long *)&ret + 1))
 	    return ret;
 #endif
+	use_lock = READ_ONCE(crng_init) < 2;
 
 	batch = &get_cpu_var(batched_entropy_u64);
 	if (use_lock)
@@ -2073,13 +2074,14 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32);
 u32 get_random_u32(void)
 {
 	u32 ret;
-	bool use_lock = READ_ONCE(crng_init) < 2;
+	bool use_lock;
 	unsigned long flags = 0;
 	struct batched_entropy *batch;
 
 	if (arch_get_random_int(&ret))
 		return ret;
 
+	use_lock = READ_ONCE(crng_init) < 2;
 	batch = &get_cpu_var(batched_entropy_u32);
 	if (use_lock)
 		read_lock_irqsave(&batched_entropy_reset_lock, flags);
-- 
2.13.2

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH] random: reorder READ_ONCE() in get_random_uXX Sebastian Andrzej Siewior <bigeasy@linutronix.de> - 2017-06-30 16:40 +0200
  Re: [PATCH] random: reorder READ_ONCE() in get_random_uXX "Jason A. Donenfeld" <Jason@zx2c4.com> - 2017-06-30 17:20 +0200

csiph-web