[PATCH 11/11] random: simplify accounting code

From: Greg Price
Date: Thu Nov 07 2013 - 19:05:09 EST


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.

Cc: "Theodore Ts'o" <tytso@xxxxxxx>
Cc: Jiri Kosina <jkosina@xxxxxxx>
Signed-off-by: Greg Price <price@xxxxxxx>
---
drivers/char/random.c | 23 ++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 87d3728..3f343ff 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -840,8 +840,6 @@ static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
static size_t account(struct entropy_store *r, size_t nbytes, int min,
int reserved)
{
- unsigned long flags;
- int wakeup_write = 0;
int entropy_count, orig;

BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
@@ -850,24 +848,19 @@ static size_t account(struct entropy_store *r, size_t nbytes, int min,

retry:
entropy_count = orig = ACCESS_ONCE(r->entropy_count);
- if (entropy_count / 8 < min + reserved) {
+ /* If limited, never pull more than available */
+ if (r->limit)
+ nbytes = min_t(size_t, nbytes, entropy_count/8 - reserved);
+ if (nbytes < min)
nbytes = 0;
- } else {
- /* If limited, never pull more than available */
- if (r->limit)
- nbytes = min_t(size_t, nbytes, entropy_count/8 - reserved);
- entropy_count = max_t(int, entropy_count - nbytes*8, 0);
- if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
- goto retry;
-
- if (entropy_count < random_write_wakeup_thresh)
- wakeup_write = 1;
- }
+ entropy_count = max_t(int, entropy_count - nbytes*8, 0);
+ if (nbytes && cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
+ goto retry;

DEBUG_ENT("debiting %zu entropy credits from %s%s\n",
nbytes * 8, r->name, r->limit ? "" : " (unlimited)");

- if (wakeup_write) {
+ if (nbytes && entropy_count < random_write_wakeup_thresh) {
wake_up_interruptible(&random_write_wait);
kill_fasync(&fasync, SIGIO, POLL_OUT);
}
--
1.8.3.2
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/