Module Name:    src
Committed By:   riastradh
Date:           Fri Feb 24 11:21:29 UTC 2023

Modified Files:
        src/sys/kern: kern_mutex.c

Log Message:
mutex(9): Simplify membars.

- Elide macro indirection for membar_acquire.
- Use atomic_store_release instead of membar_release and store.

No functional change intended.  Possible very very very very minor
performance gain on architectures with a native store-release
instruction.

Note: It is possible there are some code paths that worked by
accident with this change which could, in theory, break now, such as
the logic I recently fixed in kern_descrip.c that assumed a
mutex_enter/exit cycle would serve as a store-before-store barrier:

        fp->f_... = ...;                // A

        /* fd_affix */
        mutex_enter(&fp->f_lock);
        fp->f_count++;
        mutex_exit(&fp->f_lock);
        ...
        ff->ff_file = fp;               // B

This logic was never correct, and is likely already broken in
practice on aarch64 because the mutex_exit stub already uses STLXR
instead of DMB ISH(ST); STXR.  This change only affects the slow path
of mutex_exit, so it doesn't change even the accidental guarantees
mutex_exit makes in all paths.


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/kern/kern_mutex.c

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

Modified files:

Index: src/sys/kern/kern_mutex.c
diff -u src/sys/kern/kern_mutex.c:1.103 src/sys/kern/kern_mutex.c:1.104
--- src/sys/kern/kern_mutex.c:1.103	Thu Feb 23 14:57:29 2023
+++ src/sys/kern/kern_mutex.c	Fri Feb 24 11:21:28 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_mutex.c,v 1.103 2023/02/23 14:57:29 riastradh Exp $	*/
+/*	$NetBSD: kern_mutex.c,v 1.104 2023/02/24 11:21:28 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 #define	__MUTEX_PRIVATE
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.103 2023/02/23 14:57:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.104 2023/02/24 11:21:28 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -167,12 +167,8 @@ do {									\
  */
 #ifdef __HAVE_ATOMIC_AS_MEMBAR
 #define	MUTEX_MEMBAR_ENTER()
-#define	MUTEX_MEMBAR_ACQUIRE()
-#define	MUTEX_MEMBAR_RELEASE()
 #else
 #define	MUTEX_MEMBAR_ENTER()		membar_enter()
-#define	MUTEX_MEMBAR_ACQUIRE()		membar_acquire()
-#define	MUTEX_MEMBAR_RELEASE()		membar_release()
 #endif
 
 /*
@@ -238,7 +234,7 @@ MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t c
 	MUTEX_INHERITDEBUG(oldown, mtx->mtx_owner);
 	MUTEX_INHERITDEBUG(newown, oldown);
 	rv = MUTEX_CAS(&mtx->mtx_owner, oldown, newown);
-	MUTEX_MEMBAR_ACQUIRE();
+	membar_acquire();
 	return rv;
 }
 
@@ -256,10 +252,9 @@ MUTEX_RELEASE(kmutex_t *mtx)
 {
 	uintptr_t newown;
 
-	MUTEX_MEMBAR_RELEASE();
 	newown = 0;
 	MUTEX_INHERITDEBUG(newown, mtx->mtx_owner);
-	mtx->mtx_owner = newown;
+	atomic_store_release(&mtx->mtx_owner, newown);
 }
 #endif	/* __HAVE_SIMPLE_MUTEXES */
 

Reply via email to