tech-kern archive

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

New diagnostic routine - mutex_ownable()



While working on getting the localcount(9) stuff whipped into shape, I ran across a situation where it is desirable to ensure that the current process/lwp does not already own a mutex.

We cannot use !mutex_owned() since that doesn't return the desired result for a spin mutex, so I'm proposing to add a new routine called mutex_ownable(). This does nothing in normal kernels, but for LOCKDEBUG kernels it does a mutex_enter() followed immediately by mutex_exit(). If the current process already owns the mutex, the system will panic with a "locking against myself" error; otherwise mutex_ownable() just returns 1, enabling its use as

	KASSERT(mutex_ownable(mtx));

Diffs are attached (including man-page and sets-list updates).

Comments?  Any reason why this cannot be committed?

(Thanks to riastradh@ for the idea and initial review.)


+------------------+--------------------------+----------------------------+
| Paul Goyette     | PGP Key fingerprint:     | E-mail addresses:          |
| (Retired)        | FA29 0E3B 35AF E8AE 6651 | paul at whooppee dot com   |
| Kernel Developer | 0786 F758 55DE 53BA 7731 | pgoyette at netbsd dot org |
+------------------+--------------------------+----------------------------+
Index: sys/mutex.h
===================================================================
RCS file: /cvsroot/src/sys/sys/mutex.h,v
retrieving revision 1.20
diff -u -p -r1.20 mutex.h
--- sys/mutex.h	8 Feb 2010 09:54:27 -0000	1.20
+++ sys/mutex.h	30 Apr 2017 00:38:44 -0000
@@ -204,6 +204,7 @@ void	mutex_spin_exit(kmutex_t *);
 int	mutex_tryenter(kmutex_t *);
 
 int	mutex_owned(kmutex_t *);
+int	mutex_ownable(kmutex_t *);
 lwp_t	*mutex_owner(kmutex_t *);
 
 void	mutex_obj_init(void);
Index: kern/kern_mutex.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_mutex.c,v
retrieving revision 1.64
diff -u -p -r1.64 kern_mutex.c
--- kern/kern_mutex.c	26 Jan 2017 04:11:56 -0000	1.64
+++ kern/kern_mutex.c	30 Apr 2017 00:38:44 -0000
@@ -831,6 +831,24 @@ mutex_owner(kmutex_t *mtx)
 }
 
 /*
+ * mutex_ownable:
+ *
+ *	When compiled with LOCKDEBUG defined, ensure that the
+ *	mutex is available.  We cannot use !mutex_owned()
+ *	since that won't work correctly for spin mutexes.
+ */
+int
+mutex_ownable(kmutex_t *mtx)
+{
+
+#ifdef LOCKDEBUG
+	mutex_enter(mtx);
+	mutex_exit(mtx);
+#endif
+	return 1;
+}
+
+/*
  * mutex_tryenter:
  *
  *	Try to acquire the mutex; return non-zero if we did.
Index: ../share/man/man9/Makefile
===================================================================
RCS file: /cvsroot/src/share/man/man9/Makefile,v
retrieving revision 1.409.2.2
diff -u -p -r1.409.2.2 Makefile
--- ../share/man/man9/Makefile	28 Apr 2017 06:00:33 -0000	1.409.2.2
+++ ../share/man/man9/Makefile	30 Apr 2017 00:38:44 -0000
@@ -516,6 +516,7 @@ MLINKS+=module.9 module_autoload.9 \
 MLINKS+=mstohz.9 hztoms.9
 MLINKS+=mutex.9 mutex_init.9 mutex.9 mutex_destroy.9 mutex.9 mutex_enter.9 \
 	mutex.9 mutex_exit.9 mutex.9 mutex_tryenter.9 mutex.9 mutex_owned.9 \
+	mutex.9 mutex_ownable.9 \
 	mutex.9 mutex_spin_enter.9 mutex.9 mutex_spin_exit.9
 MLINKS+=m_tag.9 m_tag_copy.9 \
 	m_tag.9 m_tag_copy_chain.9 \
Index: ../share/man/man9/mutex.9
===================================================================
RCS file: /cvsroot/src/share/man/man9/mutex.9,v
retrieving revision 1.26
diff -u -p -r1.26 mutex.9
--- ../share/man/man9/mutex.9	4 Sep 2013 10:17:58 -0000	1.26
+++ ../share/man/man9/mutex.9	30 Apr 2017 00:38:44 -0000
@@ -36,6 +36,7 @@
 .Nm mutex_destroy ,
 .Nm mutex_enter ,
 .Nm mutex_exit ,
+.Nm mutex_ownable ,
 .Nm mutex_owned ,
 .Nm mutex_spin_enter ,
 .Nm mutex_spin_exit ,
@@ -52,6 +53,8 @@
 .Ft void
 .Fn mutex_exit "kmutex_t *mtx"
 .Ft int
+.Fn mutex_ownable "kmutex_t *mtx"
+.Ft int
 .Fn mutex_owned "kmutex_t *mtx"
 .Ft void
 .Fn mutex_spin_enter "kmutex_t *mtx"
@@ -172,6 +175,18 @@ if it is not already equal or higher.
 Release a mutex.
 The mutex must have been previously acquired by the caller.
 Mutexes may be released out of order as needed.
+.It Fn mutex_ownable "mtx"
+.Pp
+When compiled with LOCKDEBUG (see
+.Xr options 4 ) ,
+ensure that the current process can successfully acquire mtx.
+If mtx is already owned by the current process, the system will panic
+with a "locking against myself" error.
+.Pp
+This function is needed because
+.Fn mutex_owned
+does not differentiate if a spin mutex is owned by the current process
+vs owned by another process.
 .It Fn mutex_owned "mtx"
 .Pp
 For adaptive mutexes, return non-zero if the current LWP holds the mutex.
cvs diff: Diffing ../share/man/man9/man9.i386
cvs diff: Diffing ../share/man/man9/man9.sun3
cvs diff: Diffing ../share/man/man9/man9.x86
cvs diff: Diffing ../distrib/sets/lists/comp
Index: ../distrib/sets/lists/comp/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/comp/mi,v
retrieving revision 1.2125.2.2
diff -u -p -r1.2125.2.2 mi
--- ../distrib/sets/lists/comp/mi	28 Apr 2017 06:00:33 -0000	1.2125.2.2
+++ ../distrib/sets/lists/comp/mi	30 Apr 2017 00:38:45 -0000
@@ -10836,6 +10836,7 @@
 ./usr/share/man/cat9/mutex_enter.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/mutex_exit.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/mutex_init.0		comp-sys-catman		.cat
+./usr/share/man/cat9/mutex_ownable.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/mutex_owned.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/mutex_spin_enter.0		comp-sys-catman		.cat
 ./usr/share/man/cat9/mutex_spin_exit.0		comp-sys-catman		.cat
@@ -18278,6 +18279,7 @@
 ./usr/share/man/html9/mutex_enter.html		comp-sys-htmlman	html
 ./usr/share/man/html9/mutex_exit.html		comp-sys-htmlman	html
 ./usr/share/man/html9/mutex_init.html		comp-sys-htmlman	html
+./usr/share/man/html9/mutex_ownable.html	comp-sys-htmlman	html
 ./usr/share/man/html9/mutex_owned.html		comp-sys-htmlman	html
 ./usr/share/man/html9/mutex_spin_enter.html	comp-sys-htmlman	html
 ./usr/share/man/html9/mutex_spin_exit.html	comp-sys-htmlman	html
@@ -25876,6 +25878,7 @@
 ./usr/share/man/man9/mutex_enter.9		comp-sys-man		.man
 ./usr/share/man/man9/mutex_exit.9		comp-sys-man		.man
 ./usr/share/man/man9/mutex_init.9		comp-sys-man		.man
+./usr/share/man/man9/mutex_ownable.9		comp-sys-man		.man
 ./usr/share/man/man9/mutex_owned.9		comp-sys-man		.man
 ./usr/share/man/man9/mutex_spin_enter.9		comp-sys-man		.man
 ./usr/share/man/man9/mutex_spin_exit.9		comp-sys-man		.man


Home | Main Index | Thread Index | Old Index