Source-Changes-HG archive

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

[src/trunk]: src Make it possible to use the scheduler lock as the rumpuser c...



details:   https://anonhg.NetBSD.org/src/rev/5a806769b831
branches:  trunk
changeset: 754975:5a806769b831
user:      pooka <pooka%NetBSD.org@localhost>
date:      Tue May 18 14:58:41 2010 +0000

description:
Make it possible to use the scheduler lock as the rumpuser condvar
interlock.  This is applicable in cases where the actual interlock
is the CPU the currently running thread is scheduled on.  Borrowing
the scheduler lock as the mutex mandated by pthread_cond_wait()
does away with need to have an additional mutex.  This both optimizes
runtime execution and simplifies code, as the extra lock typically
lead to quite some trickeries to avoid the dungeon collapsing due
to zaps from the wand of deadlock.

diffstat:

 lib/librumpuser/rumpuser_int.h           |  12 +++--
 lib/librumpuser/rumpuser_pth.c           |  21 +++++++----
 sys/rump/include/rump/rumpuser.h         |   6 +-
 sys/rump/librump/rumpkern/intr.c         |  40 ++++++---------------
 sys/rump/librump/rumpkern/locks.c        |  14 +++---
 sys/rump/librump/rumpkern/rump_private.h |  14 +++++--
 sys/rump/librump/rumpkern/scheduler.c    |  57 ++++++++++++++++++++++++++-----
 7 files changed, 100 insertions(+), 64 deletions(-)

diffs (truncated from 477 to 300 lines):

diff -r 0e4959bfd835 -r 5a806769b831 lib/librumpuser/rumpuser_int.h
--- a/lib/librumpuser/rumpuser_int.h    Tue May 18 14:32:38 2010 +0000
+++ b/lib/librumpuser/rumpuser_int.h    Tue May 18 14:58:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser_int.h,v 1.2 2010/03/22 09:39:02 pooka Exp $   */
+/*     $NetBSD: rumpuser_int.h,v 1.3 2010/05/18 14:58:41 pooka Exp $   */
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -25,6 +25,8 @@
  * SUCH DAMAGE.
  */
 
+#include <stdlib.h>
+
 #include <rump/rumpuser.h>
 
 extern kernel_lockfn rumpuser__klock;
@@ -34,9 +36,9 @@
 #define KLOCK_WRAP(a)                                                  \
 do {                                                                   \
        int nlocks;                                                     \
-       rumpuser__kunlock(0, &nlocks);                                  \
+       rumpuser__kunlock(0, &nlocks, NULL);                            \
        a;                                                              \
-       rumpuser__klock(nlocks);                                        \
+       rumpuser__klock(nlocks, NULL);                                  \
 } while (/*CONSTCOND*/0)
 
 #define DOCALL(rvtype, call)                                           \
@@ -54,9 +56,9 @@
 {                                                                      \
        rvtype rv;                                                      \
        int nlocks;                                                     \
-       rumpuser__kunlock(0, &nlocks);                                  \
+       rumpuser__kunlock(0, &nlocks, NULL);                            \
        rv = call;                                                      \
-       rumpuser__klock(nlocks);                                        \
+       rumpuser__klock(nlocks, NULL);                                  \
        if (rv == -1)                                                   \
                *error = errno;                                         \
        else                                                            \
diff -r 0e4959bfd835 -r 5a806769b831 lib/librumpuser/rumpuser_pth.c
--- a/lib/librumpuser/rumpuser_pth.c    Tue May 18 14:32:38 2010 +0000
+++ b/lib/librumpuser/rumpuser_pth.c    Tue May 18 14:58:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser_pth.c,v 1.1 2010/02/26 18:54:20 pooka Exp $   */
+/*     $NetBSD: rumpuser_pth.c,v 1.2 2010/05/18 14:58:41 pooka Exp $   */
 
 /*
  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: rumpuser_pth.c,v 1.1 2010/02/26 18:54:20 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_pth.c,v 1.2 2010/05/18 14:58:41 pooka Exp $");
 #endif /* !lint */
 
 #ifdef __linux__
@@ -131,7 +131,7 @@
        int error, dummy;
 
        /* unschedule from CPU.  we reschedule before running the interrupt */
-       rumpuser__kunlock(0, &dummy);
+       rumpuser__kunlock(0, &dummy, NULL);
        assert(dummy == 0);
 
        NOFAIL_ERRNO(pthread_mutex_lock(&rumpuser_aio_mtx.pthmtx));
@@ -169,9 +169,9 @@
 #endif
                        }
                }
-               rumpuser__klock(0);
+               rumpuser__klock(0, NULL);
                biodone(rua->rua_bp, (size_t)rv, error);
-               rumpuser__kunlock(0, &dummy);
+               rumpuser__kunlock(0, &dummy, NULL);
 
                rua->rua_bp = NULL;
 
@@ -443,12 +443,15 @@
 void
 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
 {
+       int nlocks;
 
        cv->nwaiters++;
+       rumpuser__kunlock(0, &nlocks, mtx);
        assert(mtx->recursion == 1);
        mtxexit(mtx);
-       KLOCK_WRAP(NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx)));
+       NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
        mtxenter(mtx);
+       rumpuser__klock(nlocks, mtx);
        cv->nwaiters--;
 }
 
@@ -469,15 +472,17 @@
        int64_t sec, int64_t nsec)
 {
        struct timespec ts;
-       int rv;
+       int rv, nlocks;
 
        /* LINTED */
        ts.tv_sec = sec; ts.tv_nsec = nsec;
 
        cv->nwaiters++;
+       rumpuser__kunlock(0, &nlocks, mtx);
        mtxexit(mtx);
-       KLOCK_WRAP(rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts));
+       rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts);
        mtxenter(mtx);
+       rumpuser__klock(nlocks, mtx);
        cv->nwaiters--;
        if (rv != 0 && rv != ETIMEDOUT)
                abort();
diff -r 0e4959bfd835 -r 5a806769b831 sys/rump/include/rump/rumpuser.h
--- a/sys/rump/include/rump/rumpuser.h  Tue May 18 14:32:38 2010 +0000
+++ b/sys/rump/include/rump/rumpuser.h  Tue May 18 14:58:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser.h,v 1.40 2010/04/28 00:33:45 pooka Exp $      */
+/*     $NetBSD: rumpuser.h,v 1.41 2010/05/18 14:58:41 pooka Exp $      */
 
 /*
  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
@@ -40,8 +40,8 @@
 struct pollfd;
 struct sockaddr;
 
-typedef void (*kernel_lockfn)(int);
-typedef void (*kernel_unlockfn)(int, int *);
+typedef void (*kernel_lockfn)(int, void *);
+typedef void (*kernel_unlockfn)(int, int *, void *);
 
 int rumpuser_getfileinfo(const char *, uint64_t *, int *, int *);
 #define RUMPUSER_FT_OTHER 0
diff -r 0e4959bfd835 -r 5a806769b831 sys/rump/librump/rumpkern/intr.c
--- a/sys/rump/librump/rumpkern/intr.c  Tue May 18 14:32:38 2010 +0000
+++ b/sys/rump/librump/rumpkern/intr.c  Tue May 18 14:58:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: intr.c,v 1.27 2010/05/12 16:48:21 pooka Exp $  */
+/*     $NetBSD: intr.c,v 1.28 2010/05/18 14:58:42 pooka Exp $  */
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -26,13 +26,14 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.27 2010/05/12 16:48:21 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.28 2010/05/18 14:58:42 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/cpu.h>
 #include <sys/kernel.h>
 #include <sys/kmem.h>
 #include <sys/kthread.h>
+#include <sys/malloc.h>
 #include <sys/intr.h>
 #include <sys/timetc.h>
 
@@ -57,7 +58,6 @@
        LIST_ENTRY(softint) si_entries;
 };
 
-static struct rumpuser_mtx *si_mtx;
 struct softint_lev {
        struct rumpuser_cv *si_cv;
        LIST_HEAD(, softint) si_pending;
@@ -140,9 +140,9 @@
 }
 
 /*
- * Soft interrupt execution thread.  Note that we run without a CPU
- * context until we start processing the interrupt.  This is to avoid
- * lock recursion.
+ * Soft interrupt execution thread.  This thread is pinned to the
+ * same CPU that scheduled the interrupt, so we don't need to do
+ * lock against si_lvl.
  */
 static void
 sithread(void *arg)
@@ -155,16 +155,9 @@
        struct softint_lev *si_lvlp, *si_lvl;
        struct cpu_data *cd = &curcpu()->ci_data;
 
-       rump_unschedule();
-
        si_lvlp = cd->cpu_softcpu;
        si_lvl = &si_lvlp[mylevel];
 
-       /*
-        * XXX: si_mtx is unnecessary, and should open an interface
-        * which allows to use schedmtx for the cv wait
-        */
-       rumpuser_mutex_enter_nowrap(si_mtx);
        for (;;) {
                if (!LIST_EMPTY(&si_lvl->si_pending)) {
                        si = LIST_FIRST(&si_lvl->si_pending);
@@ -175,28 +168,19 @@
                        si->si_flags &= ~SI_ONLIST;
                        LIST_REMOVE(si, si_entries);
                        if (si->si_flags & SI_KILLME) {
-                               rumpuser_mutex_exit(si_mtx);
-                               rump_schedule();
                                softint_disestablish(si);
-                               rump_unschedule();
-                               rumpuser_mutex_enter_nowrap(si_mtx);
                                continue;
                        }
                } else {
-                       rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx);
+                       rump_schedlock_cv_wait(si_lvl->si_cv);
                        continue;
                }
-               rumpuser_mutex_exit(si_mtx);
 
-               rump_schedule();
                if (!mpsafe)
                        KERNEL_LOCK(1, curlwp);
                func(funarg);
                if (!mpsafe)
                        KERNEL_UNLOCK_ONE(curlwp);
-               rump_unschedule();
-
-               rumpuser_mutex_enter_nowrap(si_mtx);
        }
 
        panic("sithread unreachable");
@@ -206,7 +190,6 @@
 rump_intr_init()
 {
 
-       rumpuser_mutex_init(&si_mtx);
        cv_init(&lbolt, "oh kath ra");
 }
 
@@ -233,6 +216,9 @@
        }
        cd->cpu_softcpu = slev;
 
+       /* softint might run on different physical CPU */
+       membar_sync();
+
        for (i = 0; i < SOFTINT_COUNT; i++) {
                rv = kthread_create(PRI_NONE,
                    KTHREAD_MPSAFE | KTHREAD_INTR, ci,
@@ -261,7 +247,7 @@
 {
        struct softint *si;
 
-       si = kmem_alloc(sizeof(*si), KM_SLEEP);
+       si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
        si->si_func = func;
        si->si_arg = arg;
        si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
@@ -295,13 +281,11 @@
 {
        struct softint *si = cook;
 
-       rumpuser_mutex_enter(si_mtx);
        if (si->si_flags & SI_ONLIST) {
                si->si_flags |= SI_KILLME;
                return;
        }
-       rumpuser_mutex_exit(si_mtx);
-       kmem_free(si, sizeof(*si));
+       free(si, M_TEMP);
 }
 
 void
diff -r 0e4959bfd835 -r 5a806769b831 sys/rump/librump/rumpkern/locks.c
--- a/sys/rump/librump/rumpkern/locks.c Tue May 18 14:32:38 2010 +0000
+++ b/sys/rump/librump/rumpkern/locks.c Tue May 18 14:58:41 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: locks.c,v 1.39 2010/04/14 10:34:54 pooka Exp $ */
+/*     $NetBSD: locks.c,v 1.40 2010/05/18 14:58:42 pooka Exp $ */
 
 /*
  * Copyright (c) 2007, 2008 Antti Kantee.  All Rights Reserved.
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.39 2010/04/14 10:34:54 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.40 2010/05/18 14:58:42 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
@@ -332,7 +332,7 @@
                if (!rumpuser_mutex_tryenter(rump_giantlock)) {
                        struct lwp *l = curlwp;
 
-                       rump_unschedule_cpu1(l);
+                       rump_unschedule_cpu1(l, NULL);
                        rumpuser_mutex_enter_nowrap(rump_giantlock);
                        rump_schedule_cpu(l);
                }



Home | Main Index | Thread Index | Old Index