Module Name:    src
Committed By:   pooka
Date:           Sat Apr 27 13:59:46 UTC 2013

Modified Files:
        src/lib/librumpuser: rumpuser_pth.c rumpuser_pth_dummy.c
        src/sys/rump/include/rump: rumpuser.h
        src/sys/rump/librump/rumpkern: locks.c locks_up.c

Log Message:
Try to make sure that the appropriate calls to mutex_enter() takes
a spin mutex (i.e. does not relinquish cpu context while trying to
take the mutex).

Bump the hypercall interface version number.  I'll be doing a bunch
of other cleanups to simplify the interface for the benefit of
alternative hypervisor implementations.  I'll be riding this bump
and doing a second one only after I'm finished with all of the
changes.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/lib/librumpuser/rumpuser_pth.c
cvs rdiff -u -r1.7 -r1.8 src/lib/librumpuser/rumpuser_pth_dummy.c
cvs rdiff -u -r1.75 -r1.76 src/sys/rump/include/rump/rumpuser.h
cvs rdiff -u -r1.55 -r1.56 src/sys/rump/librump/rumpkern/locks.c
cvs rdiff -u -r1.6 -r1.7 src/sys/rump/librump/rumpkern/locks_up.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/librumpuser/rumpuser_pth.c
diff -u src/lib/librumpuser/rumpuser_pth.c:1.12 src/lib/librumpuser/rumpuser_pth.c:1.13
--- src/lib/librumpuser/rumpuser_pth.c:1.12	Mon Feb 11 16:02:31 2013
+++ src/lib/librumpuser/rumpuser_pth.c	Sat Apr 27 13:59:46 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rumpuser_pth.c,v 1.12 2013/02/11 16:02:31 pooka Exp $	*/
+/*	$NetBSD: rumpuser_pth.c,v 1.13 2013/04/27 13:59:46 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
@@ -28,7 +28,7 @@
 #include "rumpuser_port.h"
 
 #if !defined(lint)
-__RCSID("$NetBSD: rumpuser_pth.c,v 1.12 2013/02/11 16:02:31 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_pth.c,v 1.13 2013/04/27 13:59:46 pooka Exp $");
 #endif /* !lint */
 
 #include <assert.h>
@@ -58,10 +58,12 @@ do {									\
 	}								\
 } while (/*CONSTCOND*/0)
 
+#define MTX_KMUTEX 0x1
+#define MTX_ISSPIN 0x2
 struct rumpuser_mtx {
 	pthread_mutex_t pthmtx;
 	struct lwp *owner;
-	int iskmutex;
+	int flags;
 };
 
 #define RURW_AMWRITER(rw) (rw->writer == rumpuser_get_curlwp()		\
@@ -295,22 +297,22 @@ rumpuser_mutex_init(struct rumpuser_mtx 
 	pthread_mutexattr_destroy(&att);
 
 	(*mtx)->owner = NULL;
-	(*mtx)->iskmutex = 0;
+	(*mtx)->flags = MTX_ISSPIN;
 }
 
 void
-rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx)
+rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx, int isspin)
 {
 
 	rumpuser_mutex_init(mtx);
-	(*mtx)->iskmutex = 1;
+	(*mtx)->flags = MTX_KMUTEX | (isspin ? MTX_ISSPIN : 0);
 }
 
 static void
 mtxenter(struct rumpuser_mtx *mtx)
 {
 
-	if (!mtx->iskmutex)
+	if (!(mtx->flags & MTX_KMUTEX))
 		return;
 
 	assert(mtx->owner == NULL);
@@ -321,7 +323,7 @@ static void
 mtxexit(struct rumpuser_mtx *mtx)
 {
 
-	if (!mtx->iskmutex)
+	if (!(mtx->flags & MTX_KMUTEX))
 		return;
 
 	assert(mtx->owner != NULL);
@@ -332,6 +334,11 @@ void
 rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
 {
 
+	if (mtx->flags & MTX_ISSPIN) {
+		rumpuser_mutex_enter_nowrap(mtx);
+		return;
+	}
+
 	if (pthread_mutex_trylock(&mtx->pthmtx) != 0)
 		KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
 	mtxenter(mtx);
@@ -341,6 +348,7 @@ void
 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
 {
 
+	assert(mtx->flags & MTX_ISSPIN);
 	NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx));
 	mtxenter(mtx);
 }
@@ -378,7 +386,7 @@ struct lwp *
 rumpuser_mutex_owner(struct rumpuser_mtx *mtx)
 {
 
-	if (__predict_false(!mtx->iskmutex)) {
+	if (__predict_false(!(mtx->flags & MTX_KMUTEX))) {
 		printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
 		abort();
 	}

Index: src/lib/librumpuser/rumpuser_pth_dummy.c
diff -u src/lib/librumpuser/rumpuser_pth_dummy.c:1.7 src/lib/librumpuser/rumpuser_pth_dummy.c:1.8
--- src/lib/librumpuser/rumpuser_pth_dummy.c:1.7	Tue Nov  6 18:33:00 2012
+++ src/lib/librumpuser/rumpuser_pth_dummy.c	Sat Apr 27 13:59:46 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rumpuser_pth_dummy.c,v 1.7 2012/11/06 18:33:00 pooka Exp $	*/
+/*	$NetBSD: rumpuser_pth_dummy.c,v 1.8 2013/04/27 13:59:46 pooka Exp $	*/
 
 /*
  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
@@ -29,7 +29,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: rumpuser_pth_dummy.c,v 1.7 2012/11/06 18:33:00 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_pth_dummy.c,v 1.8 2013/04/27 13:59:46 pooka Exp $");
 #endif /* !lint */
 
 #include <sys/time.h>
@@ -111,7 +111,7 @@ rumpuser_mutex_init(struct rumpuser_mtx 
 }
 
 void
-rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx)
+rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx, int isspin)
 {
 
 	*mtx = calloc(1, sizeof(struct rumpuser_mtx));

Index: src/sys/rump/include/rump/rumpuser.h
diff -u src/sys/rump/include/rump/rumpuser.h:1.75 src/sys/rump/include/rump/rumpuser.h:1.76
--- src/sys/rump/include/rump/rumpuser.h:1.75	Fri Mar  8 19:04:28 2013
+++ src/sys/rump/include/rump/rumpuser.h	Sat Apr 27 13:59:46 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rumpuser.h,v 1.75 2013/03/08 19:04:28 pooka Exp $	*/
+/*	$NetBSD: rumpuser.h,v 1.76 2013/04/27 13:59:46 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -38,7 +38,7 @@
 #include <stdint.h>
 #endif
 
-#define RUMPUSER_VERSION 15
+#define RUMPUSER_VERSION 16
 int rumpuser_getversion(void);
 
 int rumpuser_daemonize_begin(void);
@@ -143,7 +143,7 @@ int  rumpuser_thread_join(void *);
 struct rumpuser_mtx;
 
 void rumpuser_mutex_init(struct rumpuser_mtx **);
-void rumpuser_mutex_init_kmutex(struct rumpuser_mtx **);
+void rumpuser_mutex_init_kmutex(struct rumpuser_mtx **, int);
 void rumpuser_mutex_enter(struct rumpuser_mtx *);
 void rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *);
 int  rumpuser_mutex_tryenter(struct rumpuser_mtx *);

Index: src/sys/rump/librump/rumpkern/locks.c
diff -u src/sys/rump/librump/rumpkern/locks.c:1.55 src/sys/rump/librump/rumpkern/locks.c:1.56
--- src/sys/rump/librump/rumpkern/locks.c:1.55	Tue Dec  6 18:04:31 2011
+++ src/sys/rump/librump/rumpkern/locks.c	Sat Apr 27 13:59:46 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: locks.c,v 1.55 2011/12/06 18:04:31 njoly Exp $	*/
+/*	$NetBSD: locks.c,v 1.56 2013/04/27 13:59:46 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.55 2011/12/06 18:04:31 njoly Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.56 2013/04/27 13:59:46 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
@@ -90,10 +90,29 @@ static lockops_t rw_lockops = {
 void
 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
 {
+	int isspin;
+
+	/*
+	 * Try to figure out if the caller wanted a spin mutex or
+	 * not with this easy set of conditionals.  The difference
+	 * between a spin mutex and an adaptive mutex for a rump
+	 * kernel is that the hypervisor does not relinquish the
+	 * rump kernel CPU context for a spin mutex.  The
+	 * hypervisor itself may block even when "spinning".
+	 */
+	if (type == MUTEX_SPIN) {
+		isspin = 1;
+	} else if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
+	    ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
+	    ipl == IPL_SOFTSERIAL) {
+		isspin = 0;
+	} else {
+		isspin = 1;
+	}
 
 	CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
 
-	rumpuser_mutex_init_kmutex((struct rumpuser_mtx **)mtx);
+	rumpuser_mutex_init_kmutex((struct rumpuser_mtx **)mtx, isspin);
 	ALLOCK(mtx, &mutex_lockops);
 }
 
@@ -113,7 +132,15 @@ mutex_enter(kmutex_t *mtx)
 	rumpuser_mutex_enter(RUMPMTX(mtx));
 	LOCKED(mtx, false);
 }
-__strong_alias(mutex_spin_enter,mutex_enter);
+
+void
+mutex_spin_enter(kmutex_t *mtx)
+{
+
+	WANTLOCK(mtx, false, false);
+	rumpuser_mutex_enter_nowrap(RUMPMTX(mtx));
+	LOCKED(mtx, false);
+}
 
 int
 mutex_tryenter(kmutex_t *mtx)

Index: src/sys/rump/librump/rumpkern/locks_up.c
diff -u src/sys/rump/librump/rumpkern/locks_up.c:1.6 src/sys/rump/librump/rumpkern/locks_up.c:1.7
--- src/sys/rump/librump/rumpkern/locks_up.c:1.6	Sat Apr 28 18:04:02 2012
+++ src/sys/rump/librump/rumpkern/locks_up.c	Sat Apr 27 13:59:46 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: locks_up.c,v 1.6 2012/04/28 18:04:02 stacktic Exp $	*/
+/*	$NetBSD: locks_up.c,v 1.7 2013/04/27 13:59:46 pooka Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks_up.c,v 1.6 2012/04/28 18:04:02 stacktic Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks_up.c,v 1.7 2013/04/27 13:59:46 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -71,6 +71,13 @@ mutex_init(kmutex_t *mtx, kmutex_type_t 
 	checkncpu();
 
 	/*
+	 * In uniprocessor locking we don't need to differentiate
+	 * between spin mutexes and adaptive ones.  We could
+	 * replace mutex_enter() with a NOP for spin mutexes, but
+	 * not bothering with that for now.
+	 */
+
+	/*
 	 * XXX: pool_cache would be nice, but not easily possible,
 	 * as pool cache init wants to call mutex_init() ...
 	 */

Reply via email to