Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Don't do broken lazy initialization. Just do it up...



details:   https://anonhg.NetBSD.org/src/rev/ad06fb031c3c
branches:  trunk
changeset: 807405:ad06fb031c3c
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Wed Apr 08 14:08:49 2015 +0000

description:
Don't do broken lazy initialization.  Just do it up front.

diffstat:

 sys/kern/kern_rndq.c |  32 ++++++++++++++------------------
 1 files changed, 14 insertions(+), 18 deletions(-)

diffs (89 lines):

diff -r 4a40ddf762d0 -r ad06fb031c3c sys/kern/kern_rndq.c
--- a/sys/kern/kern_rndq.c      Wed Apr 08 14:04:47 2015 +0000
+++ b/sys/kern/kern_rndq.c      Wed Apr 08 14:08:49 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_rndq.c,v 1.40 2015/04/08 14:04:47 riastradh Exp $ */
+/*     $NetBSD: kern_rndq.c,v 1.41 2015/04/08 14:08:49 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.40 2015/04/08 14:04:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.41 2015/04/08 14:08:49 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -435,6 +435,7 @@
        kmutex_t        lock;
        struct callout  callout;
        struct callout  stop_callout;
+       krndsource_t    source;
 } rnd_skew __cacheline_aligned;
 
 static void rnd_skew_intr(void *);
@@ -466,6 +467,7 @@
 {
        krndsource_t *skewsrcp = priv;
 
+       KASSERT(skewsrcp == &rnd_skew.source);
        if (RND_ENABLED(skewsrcp)) {
                /* Measure for 30s */
                if (mutex_tryenter(&rnd_skew.lock)) {
@@ -479,8 +481,7 @@
 static void
 rnd_skew_intr(void *arg)
 {
-       static krndsource_t skewsrc;
-       static int live, flipflop;
+       static int flipflop;
 
        /*
         * Even on systems with seemingly stable clocks, the
@@ -488,24 +489,12 @@
         * about every 2 calls.
         *
         */
-       if (__predict_false(!live)) {
-               /* XXX must be spin, taken with rndpool_mtx held */
-               mutex_init(&rnd_skew.lock, MUTEX_DEFAULT, IPL_VM);
-               rndsource_setcb(&skewsrc, rnd_skew_get, &skewsrc);
-               rndsource_setenable(&skewsrc, rnd_skew_enable);
-               rnd_attach_source(&skewsrc, "callout", RND_TYPE_SKEW,
-                                 RND_FLAG_COLLECT_VALUE|
-                                 RND_FLAG_ESTIMATE_VALUE|
-                                 RND_FLAG_HASCB|RND_FLAG_HASENABLE);
-               live = 1;
-               return;
-       }
        mutex_spin_enter(&rnd_skew.lock);
        flipflop = !flipflop;
 
-       if (RND_ENABLED(&skewsrc)) {
+       if (RND_ENABLED(&rnd_skew.source)) {
                if (flipflop) {
-                       rnd_add_uint32(&skewsrc, rnd_counter());
+                       rnd_add_uint32(&rnd_skew.source, rnd_counter());
                        callout_schedule(&rnd_skew.callout, hz / 10);
                } else {
                        callout_schedule(&rnd_skew.callout, 1);
@@ -577,10 +566,17 @@
         *
         */
 #if defined(__HAVE_CPU_COUNTER)
+       /* IPL_VM because taken while rndpool_mtx 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);
        callout_setfunc(&rnd_skew.callout, rnd_skew_intr, NULL);
        callout_setfunc(&rnd_skew.stop_callout, rnd_skew_stop_intr, NULL);
+       rndsource_setcb(&rnd_skew.source, rnd_skew_get, &rnd_skew.source);
+       rndsource_setenable(&rnd_skew.source, rnd_skew_enable);
+       rnd_attach_source(&rnd_skew.source, "callout", RND_TYPE_SKEW,
+           RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|
+           RND_FLAG_HASCB|RND_FLAG_HASENABLE);
        rnd_skew_intr(NULL);
 #endif
 



Home | Main Index | Thread Index | Old Index