Skip to content

Commit 0fb7a01

Browse files
gnpricetytso
authored andcommitted
random: simplify accounting code
With this we handle "reserved" in just one place. As a bonus the code becomes less nested, and the "wakeup_write" flag variable becomes unnecessary. The variable "flags" was already unused. This code behaves identically to the previous version except in two pathological cases that don't occur. If the argument "nbytes" is already less than "min", then we didn't previously enforce "min". If r->limit is false while "reserved" is nonzero, then we previously applied "reserved" in checking whether we had enough bits, even though we don't apply it to actually limit how many we take. The callers of account() never exercise either of these cases. Before the previous commit, it was possible for "nbytes" to be less than "min" if userspace chose a pathological configuration, but no longer. Cc: Jiri Kosina <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Signed-off-by: Greg Price <[email protected]> Signed-off-by: "Theodore Ts'o" <[email protected]>
1 parent 8c2aa33 commit 0fb7a01

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

drivers/char/random.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,6 @@ static void push_to_pool(struct work_struct *work)
964964
static size_t account(struct entropy_store *r, size_t nbytes, int min,
965965
int reserved)
966966
{
967-
unsigned long flags;
968-
int wakeup_write = 0;
969967
int have_bytes;
970968
int entropy_count, orig;
971969
size_t ibytes;
@@ -977,24 +975,19 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,
977975
entropy_count = orig = ACCESS_ONCE(r->entropy_count);
978976
have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
979977
ibytes = nbytes;
980-
if (have_bytes < min + reserved) {
978+
/* If limited, never pull more than available */
979+
if (r->limit)
980+
ibytes = min_t(size_t, ibytes, have_bytes - reserved);
981+
if (ibytes < min)
981982
ibytes = 0;
982-
} else {
983-
/* If limited, never pull more than available */
984-
if (r->limit)
985-
ibytes = min_t(size_t, ibytes, have_bytes - reserved);
986-
entropy_count = max_t(int, 0,
987-
entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
988-
if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
989-
goto retry;
990-
991-
if ((r->entropy_count >> ENTROPY_SHIFT)
992-
< random_write_wakeup_thresh)
993-
wakeup_write = 1;
994-
}
983+
entropy_count = max_t(int, 0,
984+
entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
985+
if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
986+
goto retry;
995987

996988
trace_debit_entropy(r->name, 8 * ibytes);
997-
if (wakeup_write) {
989+
if (ibytes &&
990+
(r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_thresh) {
998991
wake_up_interruptible(&random_write_wait);
999992
kill_fasync(&fasync, SIGIO, POLL_OUT);
1000993
}

0 commit comments

Comments
 (0)