[PATCH 6/9] random: simplify accounting logic

From: Greg Price
Date: Wed Nov 13 2013 - 03:08:43 EST


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: "Theodore Ts'o" <tytso@xxxxxxx>
Cc: Jiri Kosina <jkosina@xxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Signed-off-by: Greg Price <price@xxxxxxx>
---
drivers/char/random.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index fe7ecdd..5dffca8 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -988,14 +988,10 @@ retry:
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;

--
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/