Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Track lwp as the rumpuser mutex owner instead of pthread_t (...
details: https://anonhg.NetBSD.org/src/rev/777810901c3d
branches: trunk
changeset: 759196:777810901c3d
user: pooka <pooka%NetBSD.org@localhost>
date: Wed Dec 01 14:59:37 2010 +0000
description:
Track lwp as the rumpuser mutex owner instead of pthread_t (this
is done in rumpuser for simplicity, since on the kernel side things
we assume we have only one pointer of space). As a side-effect,
we can no longer know if the current thread is holding on to a
mutex locked without curlwp context (basically all mutexes inited
outside of mutex_init()). The only thing that called rumpuser_mutex_held()
for a non-kmutex was the giant lock. So, instead implement recursive
locking for the giant lock in the rump kernel and get rid of the
now-unused recursive pthread mutex in the hypercall interface.
diffstat:
lib/librumpuser/rumpuser_pth.c | 56 ++++++++++++--------
sys/rump/include/rump/rumpuser.h | 4 +-
sys/rump/librump/rumpkern/intr.c | 6 +-
sys/rump/librump/rumpkern/klock.c | 83 +++++++++++++++----------------
sys/rump/librump/rumpkern/locks.c | 6 +-
sys/rump/librump/rumpkern/ltsleep.c | 19 +++---
sys/rump/librump/rumpkern/rump.c | 8 +-
sys/rump/librump/rumpkern/rump_private.h | 7 +-
8 files changed, 96 insertions(+), 93 deletions(-)
diffs (truncated from 458 to 300 lines):
diff -r 79dbb75ebdb4 -r 777810901c3d lib/librumpuser/rumpuser_pth.c
--- a/lib/librumpuser/rumpuser_pth.c Wed Dec 01 13:27:15 2010 +0000
+++ b/lib/librumpuser/rumpuser_pth.c Wed Dec 01 14:59:37 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser_pth.c,v 1.3 2010/05/31 23:09:30 pooka Exp $ */
+/* $NetBSD: rumpuser_pth.c,v 1.4 2010/12/01 14:59:37 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.3 2010/05/31 23:09:30 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_pth.c,v 1.4 2010/12/01 14:59:37 pooka Exp $");
#endif /* !lint */
#ifdef __linux__
@@ -62,22 +62,20 @@
} \
} while (/*CONSTCOND*/0)
-#define RUMTX_INCRECURSION(mtx) ((mtx)->recursion++)
-#define RUMTX_DECRECURSION(mtx) ((mtx)->recursion--)
struct rumpuser_mtx {
pthread_mutex_t pthmtx;
- pthread_t owner;
- unsigned recursion;
+ struct lwp *owner;
+ int iskmutex;
};
-#define RURW_AMWRITER(rw) (pthread_equal(rw->writer, pthread_self()) \
+#define RURW_AMWRITER(rw) (rw->writer == rumpuser_get_curlwp() \
&& rw->readers == -1)
#define RURW_HASREAD(rw) (rw->readers > 0)
#define RURW_SETWRITE(rw) \
do { \
assert(rw->readers == 0); \
- rw->writer = pthread_self(); \
+ rw->writer = rumpuser_get_curlwp(); \
rw->readers = -1; \
} while (/*CONSTCOND*/0)
#define RURW_CLRWRITE(rw) \
@@ -104,7 +102,7 @@
pthread_rwlock_t pthrw;
pthread_spinlock_t spin;
int readers;
- pthread_t writer;
+ struct lwp *writer;
};
struct rumpuser_cv {
@@ -277,11 +275,19 @@
pthread_mutexattr_destroy(&att);
(*mtx)->owner = NULL;
- (*mtx)->recursion = 0;
+ (*mtx)->iskmutex = 0;
}
void
-rumpuser_mutex_recursive_init(struct rumpuser_mtx **mtx)
+rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx)
+{
+
+ rumpuser_mutex_init(mtx);
+ (*mtx)->iskmutex = 1;
+}
+
+void
+rumpuser_mutex_init_krecursive(struct rumpuser_mtx **mtx)
{
pthread_mutexattr_t mattr;
@@ -291,7 +297,7 @@
NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &mattr));
(*mtx)->owner = NULL;
- (*mtx)->recursion = 0;
+ (*mtx)->iskmutex = 1;
pthread_mutexattr_destroy(&mattr);
}
@@ -300,21 +306,22 @@
mtxenter(struct rumpuser_mtx *mtx)
{
- if (mtx->recursion++ == 0) {
- assert(mtx->owner == NULL);
- mtx->owner = pthread_self();
- } else {
- assert(pthread_equal(mtx->owner, pthread_self()));
- }
+ if (!mtx->iskmutex)
+ return;
+
+ assert(mtx->owner == NULL);
+ mtx->owner = rumpuser_get_curlwp();
}
static void
mtxexit(struct rumpuser_mtx *mtx)
{
+ if (!mtx->iskmutex)
+ return;
+
assert(mtx->owner != NULL);
- if (--mtx->recursion == 0)
- mtx->owner = NULL;
+ mtx->owner = NULL;
}
void
@@ -367,7 +374,12 @@
rumpuser_mutex_held(struct rumpuser_mtx *mtx)
{
- return mtx->recursion && pthread_equal(mtx->owner, pthread_self());
+ if (__predict_false(!mtx->iskmutex)) {
+ printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
+ abort();
+ }
+
+ return mtx->owner == rumpuser_get_curlwp();
}
void
@@ -481,7 +493,6 @@
cv->nwaiters++;
rumpuser__kunlock(0, &nlocks, mtx);
- assert(mtx->recursion == 1);
mtxexit(mtx);
NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
mtxenter(mtx);
@@ -494,7 +505,6 @@
{
cv->nwaiters++;
- assert(mtx->recursion == 1);
mtxexit(mtx);
NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
mtxenter(mtx);
diff -r 79dbb75ebdb4 -r 777810901c3d sys/rump/include/rump/rumpuser.h
--- a/sys/rump/include/rump/rumpuser.h Wed Dec 01 13:27:15 2010 +0000
+++ b/sys/rump/include/rump/rumpuser.h Wed Dec 01 14:59:37 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser.h,v 1.54 2010/11/30 14:23:24 pooka Exp $ */
+/* $NetBSD: rumpuser.h,v 1.55 2010/12/01 14:59:39 pooka Exp $ */
/*
* Copyright (c) 2007 Antti Kantee. All Rights Reserved.
@@ -126,7 +126,7 @@
struct rumpuser_mtx;
void rumpuser_mutex_init(struct rumpuser_mtx **);
-void rumpuser_mutex_recursive_init(struct rumpuser_mtx **);
+void rumpuser_mutex_init_kmutex(struct rumpuser_mtx **);
void rumpuser_mutex_enter(struct rumpuser_mtx *);
void rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *);
int rumpuser_mutex_tryenter(struct rumpuser_mtx *);
diff -r 79dbb75ebdb4 -r 777810901c3d sys/rump/librump/rumpkern/intr.c
--- a/sys/rump/librump/rumpkern/intr.c Wed Dec 01 13:27:15 2010 +0000
+++ b/sys/rump/librump/rumpkern/intr.c Wed Dec 01 14:59:37 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: intr.c,v 1.34 2010/09/07 18:25:38 pooka Exp $ */
+/* $NetBSD: intr.c,v 1.35 2010/12/01 14:59:38 pooka Exp $ */
/*
* Copyright (c) 2008 Antti Kantee. All Rights Reserved.
@@ -26,7 +26,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.34 2010/09/07 18:25:38 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.35 2010/12/01 14:59:38 pooka Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
@@ -119,7 +119,7 @@
rumpuser_cv_init(&clockcv);
rumpuser_mutex_init(&clockmtx);
- rumpuser_mutex_enter(clockmtx);
+ rumpuser_mutex_enter_nowrap(clockmtx);
for (;;) {
callout_hardclock();
diff -r 79dbb75ebdb4 -r 777810901c3d sys/rump/librump/rumpkern/klock.c
--- a/sys/rump/librump/rumpkern/klock.c Wed Dec 01 13:27:15 2010 +0000
+++ b/sys/rump/librump/rumpkern/klock.c Wed Dec 01 14:59:37 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: klock.c,v 1.2 2010/05/18 15:16:10 pooka Exp $ */
+/* $NetBSD: klock.c,v 1.3 2010/12/01 14:59:38 pooka Exp $ */
/*
* Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
@@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: klock.c,v 1.2 2010/05/18 15:16:10 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: klock.c,v 1.3 2010/12/01 14:59:38 pooka Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -42,57 +42,48 @@
* giant lock
*/
-static volatile int lockcnt;
+struct rumpuser_mtx *rump_giantlock;
+static int giantcnt;
+static struct lwp *giantowner;
-bool
-rump_kernel_isbiglocked()
+void
+rump_kernel_bigwrap(int *nlocks)
{
- return rumpuser_mutex_held(rump_giantlock) && lockcnt > 0;
+ KASSERT(giantcnt > 0 && curlwp == giantowner);
+ giantowner = NULL;
+ *nlocks = giantcnt;
+ giantcnt = 0;
}
void
-rump_kernel_unlock_allbutone(int *countp)
-{
- int minusone = lockcnt-1;
-
- KASSERT(rump_kernel_isbiglocked());
- if (minusone) {
- _kernel_unlock(minusone, countp);
- }
- KASSERT(lockcnt == 1);
- *countp = minusone;
-
- /*
- * We drop lockcnt to 0 since rumpuser doesn't know that the
- * kernel biglock is being used as the interlock for cv in
- * tsleep.
- */
- lockcnt = 0;
-}
-
-void
-rump_kernel_ununlock_allbutone(int nlocks)
+rump_kernel_bigunwrap(int nlocks)
{
- KASSERT(rumpuser_mutex_held(rump_giantlock) && lockcnt == 0);
- lockcnt = 1;
- _kernel_lock(nlocks);
+ KASSERT(giantowner == NULL);
+ giantowner = curlwp;
+ giantcnt = nlocks;
}
void
_kernel_lock(int nlocks)
{
-
- while (nlocks--) {
- if (!rumpuser_mutex_tryenter(rump_giantlock)) {
- struct lwp *l = curlwp;
+ struct lwp *l = curlwp;
- rump_unschedule_cpu1(l, NULL);
- rumpuser_mutex_enter_nowrap(rump_giantlock);
- rump_schedule_cpu(l);
+ while (nlocks) {
+ if (giantowner == l) {
+ giantcnt += nlocks;
+ nlocks = 0;
+ } else {
+ if (!rumpuser_mutex_tryenter(rump_giantlock)) {
+ rump_unschedule_cpu1(l, NULL);
+ rumpuser_mutex_enter_nowrap(rump_giantlock);
+ rump_schedule_cpu(l);
+ }
+ giantowner = l;
+ giantcnt = 1;
+ nlocks--;
}
- lockcnt++;
}
}
@@ -100,7 +91,7 @@
_kernel_unlock(int nlocks, int *countp)
{
- if (!rumpuser_mutex_held(rump_giantlock)) {
+ if (giantowner != curlwp) {
Home |
Main Index |
Thread Index |
Old Index