Skip to contentSkip to conversation

random: remove unnecessary unlikely()

PATCH Applied Yangtao Li 3 participants 15 messages Jun 7, 2019 – Jan 7, 2020 Random number driver

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH 1/5 random: remove unnecessary unlikely()

WARN_ON() already contains an unlikely(), so it's not necessary to use unlikely.

---
 drivers/char/random.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/char/random.c +2 -4
index 5d5ea4ce1442..bebf622c61c4 100644
@@ -759,10 +759,9 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
 		} while (unlikely(entropy_count < pool_size-2 && pnfrac));
 	}
 
-	if (unlikely(entropy_count < 0)) {
+	if (WARN_ON(entropy_count < 0)) {
 		pr_warn("random: negative entropy/overflow: pool %s count %d\n",
 			r->name, entropy_count);
-		WARN_ON(1);
 		entropy_count = 0;
 	} else if (entropy_count > pool_size)
 		entropy_count = pool_size;
@@ -1465,10 +1464,9 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
 	if (ibytes < min)
 		ibytes = 0;
 
-	if (unlikely(entropy_count < 0)) {
+	if (WARN_ON(entropy_count < 0)) {
 		pr_warn("random: negative entropy count: pool %s count %d\n",
 			r->name, entropy_count);
-		WARN_ON(1);
 		entropy_count = 0;
 	}
 	nfrac = ibytes << (ENTROPY_SHIFT + 3);
Signature · 1 line
2.17.0
author
#1 lore raw

PATCH 2/5 random: convert to ENTROPY_BITS

Use DEFINE_SHOW_ATTRIBUTE macro to enhance code readability.

---
 drivers/char/random.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/char/random.c +4 -7
index bebf622c61c4..d714a458f088 100644
@@ -788,7 +788,7 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
 			if (entropy_bits < 128)
 				return;
 			crng_reseed(&primary_crng, r);
-			entropy_bits = r->entropy_count >> ENTROPY_SHIFT;
+			entropy_bits = ENTROPY_BITS(r);
 		}
 
 		/* initialize the blocking pool if necessary */
@@ -1396,8 +1396,7 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);
 static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes);
 static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
 {
-	if (!r->pull ||
-	    r->entropy_count >= (nbytes << (ENTROPY_SHIFT + 3)) ||
+	if (!r->pull || ENTROPY_BITS(r) >= (nbytes << 3) ||
 	    r->entropy_count > r->poolinfo->poolfracbits)
 		return;
 
@@ -1435,8 +1434,7 @@ static void push_to_pool(struct work_struct *work)
 					      push_work);
 	BUG_ON(!r);
 	_xfer_secondary_pool(r, random_read_wakeup_bits/8);
-	trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT,
-			   r->pull->entropy_count >> ENTROPY_SHIFT);
+	trace_push_to_pool(r->name, ENTROPY_BITS(r), ENTROPY_BITS(r->pull));
 }
 
 /*
@@ -1479,8 +1477,7 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
 		goto retry;
 
 	trace_debit_entropy(r->name, 8 * ibytes);
-	if (ibytes &&
-	    (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) {
+	if (ibytes && ENTROPY_BITS(r) < random_write_wakeup_bits) {
 		wake_up_interruptible(&random_write_wait);
 		kill_fasync(&fasync, SIGIO, POLL_OUT);
 	}
Signature · 1 line
2.17.0
maintainer
#2 lore raw

Re: [PATCH 2/5] random: convert to ENTROPY_BITS

Quoted 3 lines · Yangtao Li

Use DEFINE_SHOW_ATTRIBUTE macro to enhance code readability.

Applied with bits that no longer are applicable. Also, this patch as nothing at all with DEFINE_SHOW_ATTRIBUTE, so I dropped that from the description.

- Ted

author
#3 lore raw

PATCH 3/5 random: Add and use pr_fmt()

In reply to Yangtao Li

Prefix all printk/pr_<level> messages with "random: " to make the logging a bit more consistent.

Miscellanea:

o Convert a printks to pr_notice
o Whitespace to align to open parentheses
o Remove embedded "random: " from pr_* as pr_fmt adds it

---
 drivers/char/random.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
drivers/char/random.c +12 -11
index d714a458f088..f0c834af14a8 100644
@@ -307,6 +307,8 @@
  * Eastlake, Steve Crocker, and Jeff Schiller.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #include <linux/utsname.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
@@ -760,7 +762,7 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
 	}
 
 	if (WARN_ON(entropy_count < 0)) {
-		pr_warn("random: negative entropy/overflow: pool %s count %d\n",
+		pr_warn("negative entropy/overflow: pool %s count %d\n",
 			r->name, entropy_count);
 		entropy_count = 0;
 	} else if (entropy_count > pool_size)
@@ -883,7 +885,7 @@ static void crng_initialize(struct crng_state *crng)
 		invalidate_batched_entropy();
 		numa_crng_init();
 		crng_init = 2;
-		pr_notice("random: crng done (trusting CPU's manufacturer)\n");
+		pr_notice("crng done (trusting CPU's manufacturer)\n");
 	}
 	crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
 }
@@ -946,7 +948,7 @@ static int crng_fast_load(const char *cp, size_t len)
 		invalidate_batched_entropy();
 		crng_init = 1;
 		wake_up_interruptible(&crng_init_wait);
-		pr_notice("random: fast init done\n");
+		pr_notice("fast init done\n");
 	}
 	return 1;
 }
@@ -1031,15 +1033,15 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
 		crng_init = 2;
 		process_random_ready_list();
 		wake_up_interruptible(&crng_init_wait);
-		pr_notice("random: crng init done\n");
+		pr_notice("crng init done\n");
 		if (unseeded_warning.missed) {
-			pr_notice("random: %d get_random_xx warning(s) missed "
+			pr_notice("%d get_random_xx warning(s) missed "
 				  "due to ratelimiting\n",
 				  unseeded_warning.missed);
 			unseeded_warning.missed = 0;
 		}
 		if (urandom_warning.missed) {
-			pr_notice("random: %d urandom warning(s) missed "
+			pr_notice("%d urandom warning(s) missed "
 				  "due to ratelimiting\n",
 				  urandom_warning.missed);
 			urandom_warning.missed = 0;
@@ -1463,7 +1465,7 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
 		ibytes = 0;
 
 	if (WARN_ON(entropy_count < 0)) {
-		pr_warn("random: negative entropy count: pool %s count %d\n",
+		pr_warn("negative entropy count: pool %s count %d\n",
 			r->name, entropy_count);
 		entropy_count = 0;
 	}
@@ -1682,7 +1684,7 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller,
 	print_once = true;
 #endif
 	if (__ratelimit(&unseeded_warning))
-		pr_notice("random: %s called from %pS with crng_init=%d\n",
+		pr_notice("%s called from %pS with crng_init=%d\n",
 			  func_name, caller, crng_init);
 }
 
@@ -1964,9 +1966,8 @@ urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
 	if (!crng_ready() && maxwarn > 0) {
 		maxwarn--;
 		if (__ratelimit(&urandom_warning))
-			printk(KERN_NOTICE "random: %s: uninitialized "
-			       "urandom read (%zd bytes read)\n",
-			       current->comm, nbytes);
+			pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
+				  current->comm, nbytes);
 		spin_lock_irqsave(&primary_crng.lock, flags);
 		crng_init_cnt = 0;
 		spin_unlock_irqrestore(&primary_crng.lock, flags);
Signature · 1 line
2.17.0
#4 lore raw

Re: [PATCH 3/5] random: Add and use pr_fmt()

Quoted 8 lines · Yangtao Li

Prefix all printk/pr_<level> messages with "random: " to make the logging a bit more consistent.

Miscellanea:

o Convert a printks to pr_notice
o Whitespace to align to open parentheses
o Remove embedded "random: " from pr_* as pr_fmt adds it

[]

drivers/char/random.c

Quoted 1 line
drivers/char/random.c +0 -0

[]

drivers/char/random.c static void crng_reseed(struct crng_state *crng, struct entropy_store *r) · line 1039

Quoted 10 lines
drivers/char/random.c +2 -2
@@ -1031,15 +1033,15 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
 		crng_init = 2;
 		process_random_ready_list();
 		wake_up_interruptible(&crng_init_wait);
-		pr_notice("random: crng init done\n");
+		pr_notice("crng init done\n");
 		if (unseeded_warning.missed) {
-			pr_notice("random: %d get_random_xx warning(s) missed "
+			pr_notice("%d get_random_xx warning(s) missed "
 				  "due to ratelimiting\n",

Trivia:

It'd be nice to coalesce the format string fragments into a single line at the same time too.

pr_notice("%d get_random_xx warning(s) missed due to ratelimiting\n",
unseeded_warning.missed);

drivers/char/random.c static void crng_reseed(struct crng_state *crng, struct entropy_store *r) · line 1045

Quoted 6 lines
drivers/char/random.c +1 -1
 			unseeded_warning.missed = 0;
 		}
 		if (urandom_warning.missed) {
-			pr_notice("random: %d urandom warning(s) missed "
+			pr_notice("%d urandom warning(s) missed "
 				  "due to ratelimiting\n",

etc...

maintainer
#5 lore raw

Re: [PATCH 3/5] random: Add and use pr_fmt()

In reply to Yangtao Li
Quoted 10 lines · Yangtao Li

Prefix all printk/pr_<level> messages with "random: " to make the logging a bit more consistent.

Miscellanea:

o Convert a printks to pr_notice
o Whitespace to align to open parentheses
o Remove embedded "random: " from pr_* as pr_fmt adds it

Applied with adjustments, thanks.

- Ted

author
#6 lore raw

PATCH 4/5 random: fix typo in add_timer_randomness()

In reply to Yangtao Li

s/entimate/estimate

---
 drivers/char/random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
index f0c834af14a8..885707ac8e3b 100644
@@ -1247,7 +1247,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 	/*
 	 * delta is now minimum absolute delta.
 	 * Round down by 1 bit on general principles,
-	 * and limit entropy entimate to 12 bits.
+	 * and limit entropy estimate to 12 bits.
 	 */
 	credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
 }
Signature · 1 line
2.17.0
author
#8 lore raw

PATCH 5/5 random: remove some dead code of poolinfo

In reply to Yangtao Li

Since it is not being used, so delete it.

---
 drivers/char/random.c | 27 ---------------------------
 1 file changed, 27 deletions(-)
drivers/char/random.c +0 -27
index 885707ac8e3b..d83401e35f71 100644
@@ -441,33 +441,6 @@ static const struct poolinfo {
 	/* was: x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 */
 	/* x^32 + x^26 + x^19 + x^14 + x^7 + x + 1 */
 	{ S(32),	26,	19,	14,	7,	1 },
-#if 0
-	/* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1  -- 115 */
-	{ S(2048),	1638,	1231,	819,	411,	1 },
-
-	/* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */
-	{ S(1024),	817,	615,	412,	204,	1 },
-
-	/* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */
-	{ S(1024),	819,	616,	410,	207,	2 },
-
-	/* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */
-	{ S(512),	411,	308,	208,	104,	1 },
-
-	/* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */
-	{ S(512),	409,	307,	206,	102,	2 },
-	/* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */
-	{ S(512),	409,	309,	205,	103,	2 },
-
-	/* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */
-	{ S(256),	205,	155,	101,	52,	1 },
-
-	/* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */
-	{ S(128),	103,	78,	51,	27,	2 },
-
-	/* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */
-	{ S(64),	52,	39,	26,	14,	1 },
-#endif
 };
 
 /*
Signature · 1 line
2.17.0
maintainer Applied
#9 lore raw

Re: [PATCH 5/5] random: remove some dead code of poolinfo

Quoted 3 lines · Yangtao Li

Since it is not being used, so delete it.

Applied, thanks. I also dropped the 32-word poolinfo definition since we've dropped the /dev/random blocking pool. (We only need the 128 word poolinfo now for the 4 kbit input pool.)

- Ted

#12 lore raw
Quoted 5 lines · Joe Perches
Quoted 1 line · Frank Lee

ping...

Ted?

Quoted 7 lines

Is there some reason this patch series was not applied?

https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Ted?

Do you object to this series going into the tree at all? If not, do you want someone else to pick it up?