From 454598f8cef2fc2dead0e139e6844d7b5da624e8 Mon Sep 17 00:00:00 2001
From: Sebastian Panceac <sebastian@resin.io>
Date: Fri, 13 Jul 2018 15:49:58 +0300
Subject: [PATCH] Revert "random: fix crng_ready() test"

This reverts commit 43838a23a05fbd13e47d750d3dfd77001536dd33.

The reason for reverting this commit is that entropy gets acquired very
hard on Orange Pi Plus2.

The hardware crypto module is not supported yet for H3 SoC in this kernel version.
It is expected to be included in 4.18 kernel.

As NetworkManager is depending on randomness to succesfully set its connections, if we don't revert
this commit, the board can get blocked for minutes until it succesfully connects to a network.

This patch should be reverted immediately when upgrading to a kernel where hw crypto is supported.

Upstream-Status: Inappropriate [configuration]
Signed-off-by: Sebastian Panceac <sebastian@resin.io>
---
 drivers/char/random.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index cd888d4..02fa271 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -428,7 +428,7 @@ struct crng_state primary_crng = {
  * its value (from 0->1->2).
  */
 static int crng_init = 0;
-#define crng_ready() (likely(crng_init > 1))
+#define crng_ready() (likely(crng_init > 0))
 static int crng_init_cnt = 0;
 static unsigned long crng_global_init_time = 0;
 #define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE)
@@ -843,7 +843,7 @@ static int crng_fast_load(const char *cp, size_t len)
 
 	if (!spin_trylock_irqsave(&primary_crng.lock, flags))
 		return 0;
-	if (crng_init != 0) {
+	if (crng_ready()) {
 		spin_unlock_irqrestore(&primary_crng.lock, flags);
 		return 0;
 	}
@@ -966,6 +966,7 @@ static void _extract_crng(struct crng_state *crng,
 	if (crng_ready() &&
 	    (time_after(crng_global_init_time, crng->init_time) ||
 	     time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL)))
+
 		crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL);
 	spin_lock_irqsave(&crng->lock, flags);
 	if (arch_get_random_long(&v))
@@ -1245,7 +1246,7 @@ void add_interrupt_randomness(int irq, int irq_flags)
 	fast_mix(fast_pool);
 	add_interrupt_bench(cycles);
 
-	if (unlikely(crng_init == 0)) {
+	if (!crng_ready()) {
 		if ((fast_pool->count >= 64) &&
 		    crng_fast_load((char *) fast_pool->pool,
 				   sizeof(fast_pool->pool))) {
@@ -2314,7 +2315,7 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
 {
 	struct entropy_store *poolp = &input_pool;
 
-	if (unlikely(crng_init == 0)) {
+	if (!crng_ready()) {
 		crng_fast_load(buffer, count);
 		return;
 	}
-- 
2.7.4

