Source-Changes-HG archive

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

[src/trunk]: src/sys/kern mutex(9): Simplify membars.



details:   https://anonhg.NetBSD.org/src/rev/d8e0b39ea252
branches:  trunk
changeset: 373685:d8e0b39ea252
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Fri Feb 24 11:21:28 2023 +0000

description:
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.

diffstat:

 sys/kern/kern_mutex.c |  13 ++++---------
 1 files changed, 4 insertions(+), 9 deletions(-)

diffs (52 lines):

diff -r a28c65422264 -r d8e0b39ea252 sys/kern/kern_mutex.c
--- a/sys/kern/kern_mutex.c     Fri Feb 24 11:19:15 2023 +0000
+++ b/sys/kern/kern_mutex.c     Fri Feb 24 11:21:28 2023 +0000
@@ -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 @@
  */
 #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_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 @@
 {
        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 */
 



Home | Main Index | Thread Index | Old Index