[PATCH cleanups/resubmit 2/4] random: squelch sh compiler warning and ensure safe optimization

From: Jason A. Donenfeld
Date: Thu Jun 08 2017 - 08:30:01 EST


Odd versions of gcc for the sh4 architecture will actually warn about
flags being used while uninitialized, so we set them to zero. Non crazy
gccs will optimize that out again, so it doesn't make a difference.

Next, over aggressive gccs could inline the expression that defines
use_lock, which could then introduce a race resulting in a lock
imbalance. By using READ_ONCE, we prevent that fate. Finally, we make
that assignment const, so that gcc can still optimize a nice amount.

This patch was previously submitted as part of v5, but v4 was picked up,
so this represents a delta between v4 and v5.

Signed-off-by: Jason A. Donenfeld <Jason@xxxxxxxxx>
---
drivers/char/random.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 3bdeef13afda..cef9358ecf20 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -2066,8 +2066,8 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64);
u64 get_random_u64(void)
{
u64 ret;
- bool use_lock = crng_init < 2;
- unsigned long flags;
+ const bool use_lock = READ_ONCE(crng_init) < 2;
+ unsigned long flags = 0;
struct batched_entropy *batch;
#ifdef CONFIG_WARN_UNSEEDED_RANDOM
static void *previous = NULL;
@@ -2110,8 +2110,8 @@ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32);
u32 get_random_u32(void)
{
u32 ret;
- bool use_lock = crng_init < 2;
- unsigned long flags;
+ const bool use_lock = READ_ONCE(crng_init) < 2;
+ unsigned long flags = 0;
struct batched_entropy *batch;
#ifdef CONFIG_WARN_UNSEEDED_RANDOM
static void *previous = NULL;
--
2.13.0