Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 6 Oct 2025 02:22:10 GMT
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: e0b571d77364 - main - mtx: rename MTX_CONTESTED to MTX_WAITERS
Message-ID:  <202510060222.5962MAwu061876@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=e0b571d77364d0dcae5492d9f3b901d01d3e13ca

commit e0b571d77364d0dcae5492d9f3b901d01d3e13ca
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2025-10-06 02:00:50 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2025-10-06 02:19:38 +0000

    mtx: rename MTX_CONTESTED to MTX_WAITERS
    
    Using the word "contested" for the case where there are threads blocked
    on the lock is misleading at best (the lock is already contested if it
    is being held by one thread and wanted by another). It also diverges
    from naming used in other primitives (which refer to them as "waiters").
    
    Rename it for some consistency.
    
    There were uses of the flag outside of mutex code itself.
    
    This is an abuse of the interface. The netgraph thing looks suspicious
    at best, the sctp thing is fundamentally wrong. Fixing those up is left
    as an exercise for the reader.
    
    While here touch up stale commentary.
    
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/kern/kern_mutex.c          | 23 +++++++++--------------
 sys/netgraph/netflow/netflow.c |  6 +++---
 sys/netinet/sctp_lock_bsd.h    |  6 +++---
 sys/sys/mutex.h                |  6 +++---
 4 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c
index b7316ea5f387..d67c70984528 100644
--- a/sys/kern/kern_mutex.c
+++ b/sys/kern/kern_mutex.c
@@ -503,8 +503,8 @@ _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
 /*
  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
  *
- * We call this if the lock is either contested (i.e. we need to go to
- * sleep waiting for it), or if we need to recurse on it.
+ * We get here if lock profiling is enabled, the lock is already held by
+ * someone else or we are recursing on it.
  */
 #if LOCK_DEBUG > 0
 void
@@ -660,13 +660,8 @@ retry_turnstile:
 		}
 #endif
 
-		/*
-		 * If the mutex isn't already contested and a failure occurs
-		 * setting the contested bit, the mutex was either released
-		 * or the state of the MTX_RECURSED bit changed.
-		 */
-		if ((v & MTX_CONTESTED) == 0 &&
-		    !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
+		if ((v & MTX_WAITERS) == 0 &&
+		    !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_WAITERS)) {
 			goto retry_turnstile;
 		}
 
@@ -1029,8 +1024,8 @@ thread_lock_set(struct thread *td, struct mtx *new)
 /*
  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
  *
- * We are only called here if the lock is recursed, contested (i.e. we
- * need to wake up a blocked thread) or lockstat probe is active.
+ * We get here if lock profiling is enabled, the lock is already held by
+ * someone else or we are recursing on it.
  */
 #if LOCK_DEBUG > 0
 void
@@ -1207,7 +1202,7 @@ _mtx_destroy(volatile uintptr_t *c)
 	if (!mtx_owned(m))
 		MPASS(mtx_unowned(m));
 	else {
-		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
+		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_WAITERS)) == 0);
 
 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) {
@@ -1359,8 +1354,8 @@ db_show_mtx(const struct lock_object *lock)
 		db_printf("DESTROYED");
 	else {
 		db_printf("OWNED");
-		if (m->mtx_lock & MTX_CONTESTED)
-			db_printf(", CONTESTED");
+		if (m->mtx_lock & MTX_WAITERS)
+			db_printf(", WAITERS");
 		if (m->mtx_lock & MTX_RECURSED)
 			db_printf(", RECURSED");
 	}
diff --git a/sys/netgraph/netflow/netflow.c b/sys/netgraph/netflow/netflow.c
index 978d6fd0b54d..05c6062463be 100644
--- a/sys/netgraph/netflow/netflow.c
+++ b/sys/netgraph/netflow/netflow.c
@@ -960,7 +960,7 @@ struct ngnf_show_header *resp)
 
 		list_id = 0;
 		TAILQ_FOREACH(fle, &hsh->head, fle_hash) {
-			if (hsh->mtx.mtx_lock & MTX_CONTESTED) {
+			if (hsh->mtx.mtx_lock & MTX_WAITERS) {
 				resp->hash_id = i;
 				resp->list_id = list_id;
 				mtx_unlock(&hsh->mtx);
@@ -1111,7 +1111,7 @@ ng_netflow_expire(void *arg)
 			 * Interrupt thread wants this entry!
 			 * Quick! Quick! Bail out!
 			 */
-			if (hsh->mtx.mtx_lock & MTX_CONTESTED)
+			if (hsh->mtx.mtx_lock & MTX_WAITERS)
 				break;
 
 			/*
@@ -1150,7 +1150,7 @@ ng_netflow_expire(void *arg)
 			 * Interrupt thread wants this entry!
 			 * Quick! Quick! Bail out!
 			 */
-			if (hsh->mtx.mtx_lock & MTX_CONTESTED)
+			if (hsh->mtx.mtx_lock & MTX_WAITERS)
 				break;
 
 			/*
diff --git a/sys/netinet/sctp_lock_bsd.h b/sys/netinet/sctp_lock_bsd.h
index ec66be0cf371..a60983cb30e3 100644
--- a/sys/netinet/sctp_lock_bsd.h
+++ b/sys/netinet/sctp_lock_bsd.h
@@ -263,10 +263,10 @@
 } while (0)
 
 #define SCTP_INP_LOCK_CONTENDED(_inp)					\
-	((_inp)->inp_mtx.mtx_lock & MTX_CONTESTED)
+	((_inp)->inp_mtx.mtx_lock & MTX_WAITERS)
 
 #define SCTP_INP_READ_CONTENDED(_inp)					\
-	((_inp)->inp_rdata_mtx.mtx_lock & MTX_CONTESTED)
+	((_inp)->inp_rdata_mtx.mtx_lock & MTX_WAITERS)
 
 #ifdef SCTP_LOCK_LOGGING
 #define SCTP_INP_RLOCK(_inp)	do { 					\
@@ -337,7 +337,7 @@
 } while (0)
 
 #define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp)				\
-	((_inp)->inp_create_mtx.mtx_lock & MTX_CONTESTED)
+	((_inp)->inp_create_mtx.mtx_lock & MTX_WAITERS)
 
 /*
  * For the majority of things (once we have found the association) we will
diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h
index 83300d4eb593..4f6b45d78a88 100644
--- a/sys/sys/mutex.h
+++ b/sys/sys/mutex.h
@@ -68,9 +68,9 @@
  */
 #define	MTX_UNOWNED	0x00000000	/* Cookie for free mutex */
 #define	MTX_RECURSED	0x00000001	/* lock recursed (for MTX_DEF only) */
-#define	MTX_CONTESTED	0x00000002	/* lock contested (for MTX_DEF only) */
+#define	MTX_WAITERS	0x00000002	/* lock has waiters (for MTX_DEF only) */
 #define	MTX_DESTROYED	0x00000004	/* lock destroyed */
-#define	MTX_FLAGMASK	(MTX_RECURSED | MTX_CONTESTED | MTX_DESTROYED)
+#define	MTX_FLAGMASK	(MTX_RECURSED | MTX_WAITERS | MTX_DESTROYED)
 
 /*
  * Prototypes
@@ -217,7 +217,7 @@ void	_thread_lock(struct thread *);
 #define _mtx_obtain_lock_fetch(mp, vp, tid)				\
 	atomic_fcmpset_acq_ptr(&(mp)->mtx_lock, vp, (tid))
 
-/* Try to release mtx_lock if it is unrecursed and uncontested. */
+/* Try to release mtx_lock if it is unrecursed and without waiters. */
 #define _mtx_release_lock(mp, tid)					\
 	atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), MTX_UNOWNED)
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202510060222.5962MAwu061876>