Skip to content

Commit

Permalink
random: simplify arithmetic function flow in account()
Browse files Browse the repository at this point in the history
commit a254a0e upstream.

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
zx2c4 authored and gregkh committed May 30, 2022
1 parent a0653a9 commit 6d2d29f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,28 +1293,23 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);
*/
static size_t account(size_t nbytes, int min)
{
int entropy_count, orig, have_bytes;
int entropy_count, orig;
size_t ibytes, nfrac;

BUG_ON(input_pool.entropy_count > POOL_FRACBITS);

/* Can we pull enough? */
retry:
entropy_count = orig = READ_ONCE(input_pool.entropy_count);
ibytes = nbytes;
/* never pull more than available */
have_bytes = entropy_count >> (POOL_ENTROPY_SHIFT + 3);

if (have_bytes < 0)
have_bytes = 0;
ibytes = min_t(size_t, ibytes, have_bytes);
if (ibytes < min)
ibytes = 0;

if (WARN_ON(entropy_count < 0)) {
pr_warn("negative entropy count: count %d\n", entropy_count);
entropy_count = 0;
}

/* never pull more than available */
ibytes = min_t(size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT + 3));
if (ibytes < min)
ibytes = 0;
nfrac = ibytes << (POOL_ENTROPY_SHIFT + 3);
if ((size_t)entropy_count > nfrac)
entropy_count -= nfrac;
Expand Down

0 comments on commit 6d2d29f

Please sign in to comment.