Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Fix LOCKDEBUG problems pointed out by drochner@



details:   https://anonhg.NetBSD.org/src/rev/781097aee663
branches:  trunk
changeset: 778723:781097aee663
user:      tls <tls%NetBSD.org@localhost>
date:      Tue Apr 10 15:12:40 2012 +0000

description:
Fix LOCKDEBUG problems pointed out by drochner@

1) Lock ordering in cprng_strong_destroy had us take a spin mutex then
   an adaptive mutex.  Can't do that.  Reordering this requires changing
   cprng_strong_reseed to tryenter the cprng's own mutex and skip the
   reseed on failure, or we could deadlock.

2) Can't free memory with a valid mutex in it.

diffstat:

 sys/kern/subr_cprng.c |  12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diffs (48 lines):

diff -r d5fa9839e4d6 -r 781097aee663 sys/kern/subr_cprng.c
--- a/sys/kern/subr_cprng.c     Tue Apr 10 14:02:27 2012 +0000
+++ b/sys/kern/subr_cprng.c     Tue Apr 10 15:12:40 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_cprng.c,v 1.6 2012/04/10 14:02:28 tls Exp $ */
+/*     $NetBSD: subr_cprng.c,v 1.7 2012/04/10 15:12:40 tls Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
 
 #include <sys/cprng.h>
 
-__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.6 2012/04/10 14:02:28 tls Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.7 2012/04/10 15:12:40 tls Exp $");
 
 void
 cprng_init(void)
@@ -90,7 +90,10 @@
        uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
        uint32_t cc = cprng_counter();
 
-       mutex_enter(&c->mtx);
+       if (!mutex_tryenter(&c->mtx)) {
+           return;
+       }
+
        if (c->reseed.len != sizeof(key)) {
                panic("cprng_strong_reseed: bad entropy length %d "
                      " (expected %d)", (int)c->reseed.len, (int)sizeof(key));
@@ -268,8 +271,8 @@
 void
 cprng_strong_destroy(cprng_strong_t *c)
 {
+       mutex_enter(&c->mtx);
        mutex_spin_enter(&c->reseed.mtx);
-       mutex_enter(&c->mtx);
 
        if (c->flags & CPRNG_USE_CV) {
                KASSERT(!cv_has_waiters(&c->cv));
@@ -281,6 +284,7 @@
                rndsink_detach(&c->reseed);
        }
        mutex_spin_exit(&c->reseed.mtx);
+       mutex_destroy(&c->reseed.mtx);
 
        nist_ctr_drbg_destroy(&c->drbg);
 



Home | Main Index | Thread Index | Old Index