Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 936f4e3a97bea275d23cb7b2ad2cf50640675a7b Mon Sep 17 00:00:00 2001
- From: Neil Shepperd <nshepperd@gmail.com>
- Date: Tue, 1 Sep 2015 14:22:47 +1000
- Subject: [PATCH] Implement BSD-style /dev/[u]random behaviour
- ---
- drivers/char/random.c | 32 +++++++++++---------------------
- 1 file changed, 11 insertions(+), 21 deletions(-)
- diff --git a/drivers/char/random.c b/drivers/char/random.c
- index d0da5d8..cebed43 100644
- --- a/drivers/char/random.c
- +++ b/drivers/char/random.c
- @@ -281,6 +281,7 @@
- #define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))
- #define SEC_XFER_SIZE 512
- #define EXTRACT_SIZE 10
- +#define MINIMUM_ENTROPY_BITS 256
- #define DEBUG_RANDOM_BOOT 0
- @@ -468,6 +469,7 @@ static struct entropy_store blocking_pool = {
- static struct entropy_store nonblocking_pool = {
- .poolinfo = &poolinfo_table[1],
- .name = "nonblocking",
- + .limit = 1,
- .pull = &input_pool,
- .lock = __SPIN_LOCK_UNLOCKED(nonblocking_pool.lock),
- .pool = nonblocking_pool_data,
- @@ -1036,22 +1038,20 @@ static void push_to_pool(struct work_struct *work)
- static size_t account(struct entropy_store *r, size_t nbytes, int min,
- int reserved)
- {
- - int entropy_count, orig;
- - size_t ibytes, nfrac;
- + int entropy_count;
- + size_t ibytes;
- BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
- /* Can we pull enough? */
- -retry:
- - entropy_count = orig = ACCESS_ONCE(r->entropy_count);
- + entropy_count = ACCESS_ONCE(r->entropy_count);
- ibytes = nbytes;
- - /* If limited, never pull more than available */
- + /* If limited, block until we have MINIMUM_ENTROPY_BITS bits in the pool */
- if (r->limit) {
- - int have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
- -
- - if ((have_bytes -= reserved) < 0)
- - have_bytes = 0;
- - ibytes = min_t(size_t, ibytes, have_bytes);
- + int have_bits = entropy_count >> ENTROPY_SHIFT;
- + if (have_bits < MINIMUM_ENTROPY_BITS) {
- + ibytes = 0;
- + }
- }
- if (ibytes < min)
- ibytes = 0;
- @@ -1060,18 +1060,8 @@ retry:
- pr_warn("random: negative entropy count: pool %s count %d\n",
- r->name, entropy_count);
- WARN_ON(1);
- - entropy_count = 0;
- }
- - nfrac = ibytes << (ENTROPY_SHIFT + 3);
- - if ((size_t) entropy_count > nfrac)
- - entropy_count -= nfrac;
- - else
- - entropy_count = 0;
- -
- - if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
- - goto retry;
- - trace_debit_entropy(r->name, 8 * ibytes);
- if (ibytes &&
- (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) {
- wake_up_interruptible(&random_write_wait);
- @@ -1343,7 +1333,7 @@ void get_random_bytes_arch(void *buf, int nbytes)
- if (!arch_get_random_long(&v))
- break;
- -
- +
- memcpy(p, &v, chunk);
- p += chunk;
- nbytes -= chunk;
- --
- 2.5.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement