Source-Changes-HG archive

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

[src/trunk]: src/sys/kern comments and assertions.



details:   https://anonhg.NetBSD.org/src/rev/9dfec38b06dc
branches:  trunk
changeset: 779739:9dfec38b06dc
user:      yamt <yamt%NetBSD.org@localhost>
date:      Fri Jun 15 13:51:40 2012 +0000

description:
comments and assertions.
no functional changes.

diffstat:

 sys/kern/kern_turnstile.c |  48 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 34 insertions(+), 14 deletions(-)

diffs (107 lines):

diff -r 2492308ae72f -r 9dfec38b06dc sys/kern/kern_turnstile.c
--- a/sys/kern/kern_turnstile.c Fri Jun 15 10:51:25 2012 +0000
+++ b/sys/kern/kern_turnstile.c Fri Jun 15 13:51:40 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_turnstile.c,v 1.31 2011/12/02 12:31:53 yamt Exp $ */
+/*     $NetBSD: kern_turnstile.c,v 1.32 2012/06/15 13:51:40 yamt Exp $ */
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2009 The NetBSD Foundation, Inc.
@@ -60,7 +60,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.31 2011/12/02 12:31:53 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.32 2012/06/15 13:51:40 yamt Exp $");
 
 #include <sys/param.h>
 #include <sys/lockdebug.h>
@@ -199,7 +199,9 @@
  *
  *     Lend our priority to lwps on the blocking chain.
  *
- *
+ *     If the current owner of the lock (l->l_wchan, set by sleepq_enqueue)
+ *     has a priority lower than ours (lwp_eprio(l)), lend our priority to
+ *     him to avoid priority inversions.
  */
 
 static void
@@ -225,29 +227,37 @@
                if (l->l_wchan == NULL)
                        break;
 
+               /*
+                * Ask syncobj the owner of the lock.
+                */
                owner = (*l->l_syncobj->sobj_owner)(l->l_wchan);
                if (owner == NULL)
                        break;
 
-               /* The owner may have changed as we have dropped the tc lock */
+               /*
+                * The owner may have changed as we have dropped the tc lock.
+                */
                if (cur == owner) {
                        /*
-                        * we own the lock: stop here, sleepq_block()
-                        * should wake up immediatly
+                        * We own the lock: stop here, sleepq_block()
+                        * should wake up immediatly.
                         */
                        break;
                }
-               if (l->l_mutex != owner->l_mutex)
-                       dolock = true;
-               else
-                       dolock = false;
+               /*
+                * Acquire owner->l_mutex if we don't have it yet.
+                * Because we already have another LWP lock (l->l_mutex) held,
+                * we need to play a try lock dance to avoid deadlock.
+                */
+               dolock = l->l_mutex != owner->l_mutex;
                if (l == owner || (dolock && !lwp_trylock(owner))) {
                        /*
-                        * restart from curlwp.
+                        * The owner was changed behind us or trylock failed.
+                        * Restart from curlwp.
+                        *
                         * Note that there may be a livelock here:
-                        * the owner may try grabing cur's lock (which is
-                        * the tc lock) while we're trying to grab
-                        * the owner's lock.
+                        * the owner may try grabing cur's lock (which is the
+                        * tc lock) while we're trying to grab the owner's lock.
                         */
                        lwp_unlock(l);
                        l = cur;
@@ -255,11 +265,20 @@
                        prio = lwp_eprio(l);
                        continue;
                }
+               /*
+                * If the owner's priority is already higher than ours,
+                * there's nothing to do anymore.
+                */
                if (prio <= lwp_eprio(owner)) {
                        if (dolock)
                                lwp_unlock(owner);
                        break;
                }
+               /*
+                * Lend our priority to the 'owner' LWP.
+                *
+                * Update lenders info for turnstile_unlendpri.
+                */
                ts = l->l_ts;
                KASSERT(ts->ts_inheritor == owner || ts->ts_inheritor == NULL);
                if (ts->ts_inheritor == NULL) {
@@ -273,6 +292,7 @@
                }
                if (dolock)
                        lwp_unlock(l);
+               LOCKDEBUG_BARRIER(owner->l_mutex, 1);
                l = owner;
        }
        LOCKDEBUG_BARRIER(l->l_mutex, 1);



Home | Main Index | Thread Index | Old Index