Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Gather global pool and sources state into a cacheli...



details:   https://anonhg.NetBSD.org/src/rev/2413aa40a9c4
branches:  trunk
changeset: 807578:2413aa40a9c4
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Apr 14 13:23:25 2015 +0000

description:
Gather global pool and sources state into a cacheline-aligned struct.

diffstat:

 sys/kern/kern_rndq.c |  187 +++++++++++++++++++++++++-------------------------
 1 files changed, 92 insertions(+), 95 deletions(-)

diffs (truncated from 519 to 300 lines):

diff -r c78f97fcb74d -r 2413aa40a9c4 sys/kern/kern_rndq.c
--- a/sys/kern/kern_rndq.c      Tue Apr 14 13:15:36 2015 +0000
+++ b/sys/kern/kern_rndq.c      Tue Apr 14 13:23:25 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_rndq.c,v 1.57 2015/04/14 13:15:36 riastradh Exp $ */
+/*     $NetBSD: kern_rndq.c,v 1.58 2015/04/14 13:23:25 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.57 2015/04/14 13:15:36 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.58 2015/04/14 13:23:25 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -125,15 +125,13 @@
 static pool_cache_t rnd_mempc;
 
 /*
- * Our random pool.  This is defined here rather than using the general
- * purpose one defined in rndpool.c.
- *
- * Samples are collected and queued into a separate mutex-protected queue
- * (rnd_samples, see above), and processed in a timeout routine; therefore,
- * the mutex protecting the random pool is at IPL_SOFTCLOCK() as well.
+ * Global entropy pool and sources.
  */
-static rndpool_t rnd_pool;
-static kmutex_t  rndpool_mtx;
+static struct {
+       kmutex_t                lock;
+       rndpool_t               pool;
+       LIST_HEAD(, krndsource) sources;
+} rnd_global __cacheline_aligned;
 
 /*
  * This source is used to easily "remove" queue entries when the source
@@ -176,8 +174,6 @@
 static uint8_t         rnd_testbits[sizeof(rnd_rt.rt_b)];
 #endif
 
-static LIST_HEAD(, krndsource) rnd_sources;
-
 static rndsave_t       *boot_rsp;
 
 static inline void
@@ -272,19 +268,19 @@
 {
        krndsource_t *rs;
 
-       mutex_spin_enter(&rndpool_mtx);
-       LIST_FOREACH(rs, &rnd_sources, list) {
+       mutex_spin_enter(&rnd_global.lock);
+       LIST_FOREACH(rs, &rnd_global.sources, list) {
                if (!ISSET(rs->flags, RND_FLAG_HASCB))
                        continue;
                KASSERT(rs->get != NULL);
                KASSERT(rs->getarg != NULL);
                rs->get(byteswanted, rs->getarg);
                rnd_printf_verbose("rnd: entropy estimate %zu bits\n",
-                   rndpool_get_entropy_count(&rnd_pool));
+                   rndpool_get_entropy_count(&rnd_global.pool));
                rnd_printf_verbose("rnd: asking source %s for %zu bytes\n",
                    rs->name, byteswanted);
        }
-       mutex_spin_exit(&rndpool_mtx);
+       mutex_spin_exit(&rnd_global.lock);
 }
 
 /*
@@ -298,10 +294,10 @@
         * XXX This bookkeeping shouldn't be here -- this is not where
         * the rnd_initial_entropy state change actually happens.
         */
-       mutex_spin_enter(&rndpool_mtx);
-       const size_t entropy_count = rndpool_get_entropy_count(&rnd_pool);
+       mutex_spin_enter(&rnd_global.lock);
+       const size_t entropy_count = rndpool_get_entropy_count(&rnd_global.pool);
        if (entropy_count < RND_ENTROPY_THRESHOLD * 8) {
-               mutex_spin_exit(&rndpool_mtx);
+               mutex_spin_exit(&rnd_global.lock);
                return;
        } else {
 #ifdef RND_VERBOSE
@@ -311,7 +307,7 @@
 #endif
                rnd_initial_entropy = 1;
        }
-       mutex_spin_exit(&rndpool_mtx);
+       mutex_spin_exit(&rnd_global.lock);
 
        rndsinks_distribute();
 }
@@ -508,11 +504,11 @@
         */
        c = rnd_counter();
 
-       LIST_INIT(&rnd_sources);
+       LIST_INIT(&rnd_global.sources);
        SIMPLEQ_INIT(&rnd_samples.q);
 
-       rndpool_init(&rnd_pool);
-       mutex_init(&rndpool_mtx, MUTEX_DEFAULT, IPL_VM);
+       rndpool_init(&rnd_global.pool);
+       mutex_init(&rnd_global.lock, MUTEX_DEFAULT, IPL_VM);
 
        rnd_mempc = pool_cache_init(sizeof(rnd_sample_t), 0, 0, 0,
                                    "rndsample", NULL, IPL_VM,
@@ -533,11 +529,11 @@
         * XXX more things to add would be nice.
         */
        if (c) {
-               mutex_spin_enter(&rndpool_mtx);
-               rndpool_add_data(&rnd_pool, &c, sizeof(c), 1);
+               mutex_spin_enter(&rnd_global.lock);
+               rndpool_add_data(&rnd_global.pool, &c, sizeof(c), 1);
                c = rnd_counter();
-               rndpool_add_data(&rnd_pool, &c, sizeof(c), 1);
-               mutex_spin_exit(&rndpool_mtx);
+               rndpool_add_data(&rnd_global.pool, &c, sizeof(c), 1);
+               mutex_spin_exit(&rnd_global.lock);
        }
 
        /*
@@ -547,7 +543,7 @@
         *
         */
 #if defined(__HAVE_CPU_COUNTER)
-       /* IPL_VM because taken while rndpool_mtx is held.  */
+       /* IPL_VM because taken while rnd_global.lock is held.  */
        mutex_init(&rnd_skew.lock, MUTEX_DEFAULT, IPL_VM);
        callout_init(&rnd_skew.callout, CALLOUT_MPSAFE);
        callout_init(&rnd_skew.stop_callout, CALLOUT_MPSAFE);
@@ -564,15 +560,15 @@
        rnd_printf_verbose("rnd: initialised (%u)%s", RND_POOLBITS,
            c ? " with counter\n" : "\n");
        if (boot_rsp != NULL) {
-               mutex_spin_enter(&rndpool_mtx);
-               rndpool_add_data(&rnd_pool, boot_rsp->data,
+               mutex_spin_enter(&rnd_global.lock);
+               rndpool_add_data(&rnd_global.pool, boot_rsp->data,
                    sizeof(boot_rsp->data),
                    MIN(boot_rsp->entropy, RND_POOLBITS / 2));
-               if (rndpool_get_entropy_count(&rnd_pool) >
+               if (rndpool_get_entropy_count(&rnd_global.pool) >
                    RND_ENTROPY_THRESHOLD * 8) {
                        rnd_initial_entropy = 1;
                }
-                mutex_spin_exit(&rndpool_mtx);
+                mutex_spin_exit(&rnd_global.lock);
                rnd_printf("rnd: seeded with %d bits\n",
                    MIN(boot_rsp->entropy, RND_POOLBITS / 2));
                memset(boot_rsp, 0, sizeof(*boot_rsp));
@@ -674,8 +670,8 @@
 
        rs->state = rnd_sample_allocate(rs);
 
-       mutex_spin_enter(&rndpool_mtx);
-       LIST_INSERT_HEAD(&rnd_sources, rs, list);
+       mutex_spin_enter(&rnd_global.lock);
+       LIST_INSERT_HEAD(&rnd_global.sources, rs, list);
 
 #ifdef RND_VERBOSE
        rnd_printf_verbose("rnd: %s attached as an entropy source (",
@@ -696,8 +692,8 @@
         * entropy per source-attach timestamp.  I am skeptical,
         * but we count 1 bit per source here.
         */
-       rndpool_add_data(&rnd_pool, &ts, sizeof(ts), 1);
-       mutex_spin_exit(&rndpool_mtx);
+       rndpool_add_data(&rnd_global.pool, &ts, sizeof(ts), 1);
+       mutex_spin_exit(&rnd_global.lock);
 }
 
 /*
@@ -708,9 +704,9 @@
 {
        rnd_sample_t *sample;
 
-       mutex_spin_enter(&rndpool_mtx);
+       mutex_spin_enter(&rnd_global.lock);
        LIST_REMOVE(source, list);
-       mutex_spin_exit(&rndpool_mtx);
+       mutex_spin_exit(&rnd_global.lock);
 
        /*
         * If there are samples queued up "remove" them from the sample queue
@@ -822,9 +818,9 @@
         * timestamp, just directly add the data.
         */
        if (__predict_false(rs == NULL)) {
-               mutex_spin_enter(&rndpool_mtx);
-               rndpool_add_data(&rnd_pool, data, len, entropy);
-               mutex_spin_exit(&rndpool_mtx);
+               mutex_spin_enter(&rnd_global.lock);
+               rndpool_add_data(&rnd_global.pool, data, len, entropy);
+               mutex_spin_exit(&rnd_global.lock);
        } else {
                rnd_add_data_ts(rs, data, len, entropy, rnd_counter());
        }
@@ -1035,9 +1031,9 @@
        mutex_spin_exit(&rnd_samples.lock);
 
        /* Don't thrash the rndpool mtx either.  Hold, add all samples. */
-       mutex_spin_enter(&rndpool_mtx);
+       mutex_spin_enter(&rnd_global.lock);
 
-       pool_entropy = rndpool_get_entropy_count(&rnd_pool);
+       pool_entropy = rndpool_get_entropy_count(&rnd_global.pool);
 
        while ((sample = SIMPLEQ_FIRST(&dq_samples))) {
                int sample_count;
@@ -1080,13 +1076,13 @@
                }
 
                if (source->flags & RND_FLAG_COLLECT_VALUE) {
-                       rndpool_add_data(&rnd_pool, sample->values,
+                       rndpool_add_data(&rnd_global.pool, sample->values,
                                         sample_count *
                                             sizeof(sample->values[1]),
                                         0);
                }
                if (source->flags & RND_FLAG_COLLECT_TIME) {
-                       rndpool_add_data(&rnd_pool, sample->ts,
+                       rndpool_add_data(&rnd_global.pool, sample->ts,
                                         sample_count *
                                             sizeof(sample->ts[1]),
                                         0);
@@ -1096,8 +1092,8 @@
                source->total += sample->entropy;
 skip:          SIMPLEQ_INSERT_TAIL(&df_samples, sample, next);
        }
-       rndpool_set_entropy_count(&rnd_pool, pool_entropy);
-       mutex_spin_exit(&rndpool_mtx);
+       rndpool_set_entropy_count(&rnd_global.pool, pool_entropy);
+       mutex_spin_exit(&rnd_global.lock);
 
        /*
         * If we filled the pool past the threshold, wake anyone
@@ -1145,10 +1141,10 @@
        int entropy_count;
        uint32_t retval;
 
-       mutex_spin_enter(&rndpool_mtx);
+       mutex_spin_enter(&rnd_global.lock);
        if (__predict_false(!timed_in)) {
                if (boottime.tv_sec) {
-                       rndpool_add_data(&rnd_pool, &boottime,
+                       rndpool_add_data(&rnd_global.pool, &boottime,
                                         sizeof(boottime), 0);
                }
                timed_in++;
@@ -1157,19 +1153,19 @@
                uint32_t c;
 
                rnd_printf_verbose("rnd: WARNING! initial entropy low (%u).\n",
-                      rndpool_get_entropy_count(&rnd_pool));
+                      rndpool_get_entropy_count(&rnd_global.pool));
                /* Try once again to put something in the pool */
                c = rnd_counter();
-               rndpool_add_data(&rnd_pool, &c, sizeof(c), 1);
+               rndpool_add_data(&rnd_global.pool, &c, sizeof(c), 1);
        }
 
 #ifdef DIAGNOSTIC
        while (!rnd_tested) {
-               entropy_count = rndpool_get_entropy_count(&rnd_pool);
+               entropy_count = rndpool_get_entropy_count(&rnd_global.pool);
                rnd_printf_verbose("rnd: starting statistical RNG test,"
                    " entropy = %d.\n",
                    entropy_count);
-               if (rndpool_extract_data(&rnd_pool, rnd_rt.rt_b,
+               if (rndpool_extract_data(&rnd_global.pool, rnd_rt.rt_b,
                    sizeof(rnd_rt.rt_b), RND_EXTRACT_ANY)
                    != sizeof(rnd_rt.rt_b)) {
                        panic("rnd: could not get bits for statistical test");
@@ -1195,18 +1191,18 @@
                        continue;
                }
                memset(&rnd_rt, 0, sizeof(rnd_rt));
-               rndpool_add_data(&rnd_pool, rnd_testbits, sizeof(rnd_testbits),
-                                entropy_count);
+               rndpool_add_data(&rnd_global.pool, rnd_testbits,
+                   sizeof(rnd_testbits), entropy_count);
                memset(rnd_testbits, 0, sizeof(rnd_testbits));
                rnd_printf_verbose("rnd: statistical RNG test done,"
                    " entropy = %d.\n",
-                   rndpool_get_entropy_count(&rnd_pool));
+                   rndpool_get_entropy_count(&rnd_global.pool));
                rnd_tested++;
        }
 #endif
-       entropy_count = rndpool_get_entropy_count(&rnd_pool);
-       retval = rndpool_extract_data(&rnd_pool, p, len, flags);
-       mutex_spin_exit(&rndpool_mtx);
+       entropy_count = rndpool_get_entropy_count(&rnd_global.pool);
+       retval = rndpool_extract_data(&rnd_global.pool, p, len, flags);



Home | Main Index | Thread Index | Old Index