Module Name: src Committed By: riastradh Date: Mon Apr 13 14:41:06 UTC 2015
Modified Files: src/sys/dev: rnd_private.h src/sys/kern: kern_rndq.c kern_rndsink.c Log Message: Move rndpool_(maybe_)extract to rndq, rename to rnd_(try)extract. Make rnd_extract_data static. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/sys/dev/rnd_private.h cvs rdiff -u -r1.43 -r1.44 src/sys/kern/kern_rndq.c cvs rdiff -u -r1.10 -r1.11 src/sys/kern/kern_rndsink.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/rnd_private.h diff -u src/sys/dev/rnd_private.h:1.5 src/sys/dev/rnd_private.h:1.6 --- src/sys/dev/rnd_private.h:1.5 Mon Apr 13 14:30:05 2015 +++ src/sys/dev/rnd_private.h Mon Apr 13 14:41:06 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: rnd_private.h,v 1.5 2015/04/13 14:30:05 riastradh Exp $ */ +/* $NetBSD: rnd_private.h,v 1.6 2015/04/13 14:41:06 riastradh Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -47,5 +47,6 @@ #define RND_EXTRACT_GOOD 1 /* return as many good bytes (short read ok) */ -uint32_t rnd_extract_data(void *, uint32_t, uint32_t); +bool rnd_extract(void *, size_t); +bool rnd_tryextract(void *, size_t); #endif Index: src/sys/kern/kern_rndq.c diff -u src/sys/kern/kern_rndq.c:1.43 src/sys/kern/kern_rndq.c:1.44 --- src/sys/kern/kern_rndq.c:1.43 Wed Apr 8 14:13:55 2015 +++ src/sys/kern/kern_rndq.c Mon Apr 13 14:41:06 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_rndq.c,v 1.43 2015/04/08 14:13:55 riastradh Exp $ */ +/* $NetBSD: kern_rndq.c,v 1.44 2015/04/13 14:41:06 riastradh Exp $ */ /*- * Copyright (c) 1997-2013 The NetBSD Foundation, Inc. @@ -32,7 +32,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.43 2015/04/08 14:13:55 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.44 2015/04/13 14:41:06 riastradh Exp $"); #include <sys/param.h> #include <sys/atomic.h> @@ -1148,7 +1148,7 @@ rnd_wake(void *arg) rnd_wakeup_readers(); } -u_int32_t +static uint32_t rnd_extract_data(void *p, u_int32_t len, u_int32_t flags) { static int timed_in; @@ -1226,6 +1226,67 @@ rnd_extract_data(void *p, u_int32_t len, return retval; } +/* + * Fill the buffer with as much entropy as we can. Return true if it + * has full entropy and false if not. + */ +bool +rnd_extract(void *buffer, size_t bytes) +{ + const size_t extracted = rnd_extract_data(buffer, bytes, + RND_EXTRACT_GOOD); + + if (extracted < bytes) { + (void)rnd_extract_data((uint8_t *)buffer + extracted, + bytes - extracted, RND_EXTRACT_ANY); + mutex_spin_enter(&rndpool_mtx); + rnd_getmore(bytes - extracted); + mutex_spin_exit(&rndpool_mtx); + return false; + } + + return true; +} + +/* + * If we have as much entropy as is requested, fill the buffer with it + * and return true. Otherwise, leave the buffer alone and return + * false. + */ + +CTASSERT(RND_ENTROPY_THRESHOLD <= 0xffffffffUL); +CTASSERT(RNDSINK_MAX_BYTES <= (0xffffffffUL - RND_ENTROPY_THRESHOLD)); +CTASSERT((RNDSINK_MAX_BYTES + RND_ENTROPY_THRESHOLD) <= + (0xffffffffUL / NBBY)); + +bool +rnd_tryextract(void *buffer, size_t bytes) +{ + bool ok; + + KASSERT(bytes <= RNDSINK_MAX_BYTES); + + const uint32_t bits_needed = ((bytes + RND_ENTROPY_THRESHOLD) * NBBY); + + mutex_spin_enter(&rndpool_mtx); + if (bits_needed <= rndpool_get_entropy_count(&rnd_pool)) { + const uint32_t extracted __diagused = + rndpool_extract_data(&rnd_pool, buffer, bytes, + RND_EXTRACT_GOOD); + + KASSERT(extracted == bytes); + + ok = true; + } else { + ok = false; + rnd_getmore(howmany(bits_needed - + rndpool_get_entropy_count(&rnd_pool), NBBY)); + } + mutex_spin_exit(&rndpool_mtx); + + return ok; +} + void rnd_seed(void *base, size_t len) { Index: src/sys/kern/kern_rndsink.c diff -u src/sys/kern/kern_rndsink.c:1.10 src/sys/kern/kern_rndsink.c:1.11 --- src/sys/kern/kern_rndsink.c:1.10 Sun Oct 26 18:22:32 2014 +++ src/sys/kern/kern_rndsink.c Mon Apr 13 14:41:06 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_rndsink.c,v 1.10 2014/10/26 18:22:32 tls Exp $ */ +/* $NetBSD: kern_rndsink.c,v 1.11 2015/04/13 14:41:06 riastradh Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_rndsink.c,v 1.10 2014/10/26 18:22:32 tls Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_rndsink.c,v 1.11 2015/04/13 14:41:06 riastradh Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -86,74 +86,6 @@ rndsinks_init(void) mutex_init(&rndsinks_lock, MUTEX_DEFAULT, IPL_VM); } -/* - * XXX Provisional -- rndpool_extract and rndpool_maybe_extract should - * move into kern_rndpool.c. - */ -extern rndpool_t rnd_pool; -extern kmutex_t rndpool_mtx; - -/* - * Fill the buffer with as much entropy as we can. Return true if it - * has full entropy and false if not. - */ -static bool -rndpool_extract(void *buffer, size_t bytes) -{ - const size_t extracted = rnd_extract_data(buffer, bytes, - RND_EXTRACT_GOOD); - - if (extracted < bytes) { - (void)rnd_extract_data((uint8_t *)buffer + extracted, - bytes - extracted, RND_EXTRACT_ANY); - mutex_spin_enter(&rndpool_mtx); - rnd_getmore(bytes - extracted); - mutex_spin_exit(&rndpool_mtx); - return false; - } - - return true; -} - -/* - * If we have as much entropy as is requested, fill the buffer with it - * and return true. Otherwise, leave the buffer alone and return - * false. - */ - -CTASSERT(RND_ENTROPY_THRESHOLD <= 0xffffffffUL); -CTASSERT(RNDSINK_MAX_BYTES <= (0xffffffffUL - RND_ENTROPY_THRESHOLD)); -CTASSERT((RNDSINK_MAX_BYTES + RND_ENTROPY_THRESHOLD) <= - (0xffffffffUL / NBBY)); - -static bool -rndpool_maybe_extract(void *buffer, size_t bytes) -{ - bool ok; - - KASSERT(bytes <= RNDSINK_MAX_BYTES); - - const uint32_t bits_needed = ((bytes + RND_ENTROPY_THRESHOLD) * NBBY); - - mutex_spin_enter(&rndpool_mtx); - if (bits_needed <= rndpool_get_entropy_count(&rnd_pool)) { - const uint32_t extracted __diagused = - rndpool_extract_data(&rnd_pool, buffer, bytes, - RND_EXTRACT_GOOD); - - KASSERT(extracted == bytes); - - ok = true; - } else { - ok = false; - rnd_getmore(howmany(bits_needed - - rndpool_get_entropy_count(&rnd_pool), NBBY)); - } - mutex_spin_exit(&rndpool_mtx); - - return ok; -} - void rndsinks_distribute(void) { @@ -167,7 +99,7 @@ rndsinks_distribute(void) KASSERT(rndsink->rsink_state == RNDSINK_QUEUED); /* Bail if we can't get some entropy for this rndsink. */ - if (!rndpool_maybe_extract(buffer, rndsink->rsink_bytes)) + if (!rnd_tryextract(buffer, rndsink->rsink_bytes)) break; /* @@ -225,9 +157,12 @@ rndsinks_enqueue(struct rndsink *rndsink * or something -- as soon as we get that much from the entropy * sources, distribute it. */ + { + extern kmutex_t rndpool_mtx; mutex_spin_enter(&rndpool_mtx); rnd_getmore(MAX(rndsink->rsink_bytes, 2 * sizeof(uint32_t))); mutex_spin_exit(&rndpool_mtx); + } switch (rndsink->rsink_state) { case RNDSINK_IDLE: @@ -331,7 +266,7 @@ rndsink_request(struct rndsink *rndsink, KASSERT(bytes == rndsink->rsink_bytes); mutex_spin_enter(&rndsinks_lock); - const bool full_entropy = rndpool_extract(buffer, bytes); + const bool full_entropy = rnd_extract(buffer, bytes); if (!full_entropy) rndsinks_enqueue(rndsink); mutex_spin_exit(&rndsinks_lock);