Skip to content

Commit a254a0e

Browse files
committed
random: simplify arithmetic function flow in account()
Now that have_bytes is never modified, we can simplify this function. First, we move the check for negative entropy_count to be first. That ensures that subsequent reads of this will be non-negative. Then, have_bytes and ibytes can be folded into their one use site in the min_t() function. Suggested-by: Dominik Brodowski <linux@dominikbrodowski.net> Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 248045b commit a254a0e

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

drivers/char/random.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,28 +1293,23 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);
12931293
*/
12941294
static size_t account(size_t nbytes, int min)
12951295
{
1296-
int entropy_count, orig, have_bytes;
1296+
int entropy_count, orig;
12971297
size_t ibytes, nfrac;
12981298

12991299
BUG_ON(input_pool.entropy_count > POOL_FRACBITS);
13001300

13011301
/* Can we pull enough? */
13021302
retry:
13031303
entropy_count = orig = READ_ONCE(input_pool.entropy_count);
1304-
ibytes = nbytes;
1305-
/* never pull more than available */
1306-
have_bytes = entropy_count >> (POOL_ENTROPY_SHIFT + 3);
1307-
1308-
if (have_bytes < 0)
1309-
have_bytes = 0;
1310-
ibytes = min_t(size_t, ibytes, have_bytes);
1311-
if (ibytes < min)
1312-
ibytes = 0;
1313-
13141304
if (WARN_ON(entropy_count < 0)) {
13151305
pr_warn("negative entropy count: count %d\n", entropy_count);
13161306
entropy_count = 0;
13171307
}
1308+
1309+
/* never pull more than available */
1310+
ibytes = min_t(size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT + 3));
1311+
if (ibytes < min)
1312+
ibytes = 0;
13181313
nfrac = ibytes << (POOL_ENTROPY_SHIFT + 3);
13191314
if ((size_t)entropy_count > nfrac)
13201315
entropy_count -= nfrac;

0 commit comments

Comments
 (0)