Skip to content

Commit

Permalink
random: simplify accounting logic
Browse files Browse the repository at this point in the history
This logic is exactly equivalent to the old logic, but it should
be easier to see what it's doing.

The equivalence depends on one fact from outside this function:
when 'r->limit' is false, 'reserved' is zero.  (Well, two facts;
the other is that 'reserved' is never negative.)

Cc: Jiri Kosina <jkosina@suse.cz>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Greg Price <price@mit.edu>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
  • Loading branch information
Greg Price authored and Theodore Ts'o committed Mar 20, 2014
1 parent 19fa5be commit ee1de40
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,10 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
ibytes = 0;
} else {
/* If limited, never pull more than available */
if (r->limit && ibytes + reserved >= have_bytes)
ibytes = have_bytes - reserved;

if (have_bytes >= ibytes + reserved)
entropy_count -= ibytes << (ENTROPY_SHIFT + 3);
else
entropy_count = reserved << (ENTROPY_SHIFT + 3);

if (r->limit)
ibytes = min_t(size_t, ibytes, have_bytes - reserved);
entropy_count = max_t(int, 0,
entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
goto retry;

Expand Down

0 comments on commit ee1de40

Please sign in to comment.