Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Move rndpool_(maybe_)extract to rndq, rename to rnd_(try...



details:   https://anonhg.NetBSD.org/src/rev/54b8434e3969
branches:  trunk
changeset: 337360:54b8434e3969
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Apr 13 14:41:06 2015 +0000

description:
Move rndpool_(maybe_)extract to rndq, rename to rnd_(try)extract.

Make rnd_extract_data static.

diffstat:

 sys/dev/rnd_private.h   |   5 +-
 sys/kern/kern_rndq.c    |  67 +++++++++++++++++++++++++++++++++++++++-
 sys/kern/kern_rndsink.c |  79 ++++--------------------------------------------
 3 files changed, 74 insertions(+), 77 deletions(-)

diffs (236 lines):

diff -r 79661fd99f19 -r 54b8434e3969 sys/dev/rnd_private.h
--- a/sys/dev/rnd_private.h     Mon Apr 13 14:30:05 2015 +0000
+++ b/sys/dev/rnd_private.h     Mon Apr 13 14:41:06 2015 +0000
@@ -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
diff -r 79661fd99f19 -r 54b8434e3969 sys/kern/kern_rndq.c
--- a/sys/kern/kern_rndq.c      Mon Apr 13 14:30:05 2015 +0000
+++ b/sys/kern/kern_rndq.c      Mon Apr 13 14:41:06 2015 +0000
@@ -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_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 @@
        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)
 {
diff -r 79661fd99f19 -r 54b8434e3969 sys/kern/kern_rndsink.c
--- a/sys/kern/kern_rndsink.c   Mon Apr 13 14:30:05 2015 +0000
+++ b/sys/kern/kern_rndsink.c   Mon Apr 13 14:41:06 2015 +0000
@@ -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 @@
        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 @@
                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 @@
         * 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 @@
        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);



Home | Main Index | Thread Index | Old Index