Source-Changes-HG archive

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

[src/trunk]: src/sys/kern entropy(9): Fix another new race in entropy_account...



details:   https://anonhg.NetBSD.org/src/rev/35bff6bcb761
branches:  trunk
changeset: 364387:35bff6bcb761
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Mar 20 13:18:11 2022 +0000

description:
entropy(9): Fix another new race in entropy_account_cpu.

The consolidation xcall can preempt entropy_enter, between when it
unlocks the per-CPU state and when it calls entropy_account_cpu, with
the effect of setting ec->ec_pending=0.

Previously this was impossible because we called entropy_account_cpu
with the per-CPU state still locked, but that doesn't work now that
the global entropy lock is an adaptive lock which might sleep which
is forbidden while the per-CPU state is locked.

diffstat:

 sys/kern/kern_entropy.c |  8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diffs (29 lines):

diff -r 4780f06c0129 -r 35bff6bcb761 sys/kern/kern_entropy.c
--- a/sys/kern/kern_entropy.c   Sun Mar 20 13:17:44 2022 +0000
+++ b/sys/kern/kern_entropy.c   Sun Mar 20 13:18:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_entropy.c,v 1.45 2022/03/20 13:17:44 riastradh Exp $      */
+/*     $NetBSD: kern_entropy.c,v 1.46 2022/03/20 13:18:11 riastradh Exp $      */
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.45 2022/03/20 13:17:44 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.46 2022/03/20 13:18:11 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -750,7 +750,9 @@
        mutex_enter(&E->lock);
        ec0 = entropy_cpu_get(&lock);
        KASSERT(ec0 == ec);
-       if (E->needed != 0 && E->needed <= ec->ec_pending) {
+       if (ec->ec_pending == 0) {
+               /* Raced with consolidation xcall.  Nothing to do.  */
+       } else if (E->needed != 0 && E->needed <= ec->ec_pending) {
                /*
                 * If we have not yet attained full entropy but we can
                 * now, do so.  This way we disseminate entropy



Home | Main Index | Thread Index | Old Index