git: 183b64dfd84a - stable/14 - linuxkpi: spinlock: Simplify code

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 04 Jun 2024 11:35:29 UTC
The branch stable/14 has been updated by manu:

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

commit 183b64dfd84ad7fd504d04f40c90edad809083c1
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2024-05-15 09:00:04 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2024-06-04 11:22:40 +0000

    linuxkpi: spinlock: Simplify code
    
    Just use a typedef for spinlock_t, no need to create a useless
    structure.
    
    Reviewed by:            bz, emaste
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D45205
    
    (cherry picked from commit ae38a1a1bfdf320089c254e4dbffdf4769d89110)
    
    linuxkpi: Fix spin_lock_init
    
    Some linux code re-init some spinlock so add MTX_NEW to mtx_init.
    
    Reported by:    David Wolfskill <david@catwhisker.org>
    Fixes:          ae38a1a1bfdf ("linuxkpi: spinlock: Simplify code")
    
    (cherry picked from commit cff79fd02636f34010d8b835cc9e55401fa76e74)
---
 .../linuxkpi/common/include/linux/spinlock.h       | 34 +++++++---------------
 sys/compat/linuxkpi/common/include/linux/wait.h    |  2 +-
 sys/compat/linuxkpi/common/src/linux_compat.c      |  8 ++---
 sys/compat/linuxkpi/common/src/linux_idr.c         |  2 +-
 sys/dev/mlx4/mlx4_en/mlx4_en_tx.c                  |  8 ++---
 5 files changed, 20 insertions(+), 34 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/spinlock.h b/sys/compat/linuxkpi/common/include/linux/spinlock.h
index 4bc1e2a2d431..2992e41c9c02 100644
--- a/sys/compat/linuxkpi/common/include/linux/spinlock.h
+++ b/sys/compat/linuxkpi/common/include/linux/spinlock.h
@@ -41,9 +41,7 @@
 #include <linux/bottom_half.h>
 #include <linux/lockdep.h>
 
-typedef struct {
-	struct mtx m;
-} spinlock_t;
+typedef struct mtx spinlock_t;
 
 /*
  * By defining CONFIG_SPIN_SKIP LinuxKPI spinlocks and asserts will be
@@ -59,7 +57,7 @@ typedef struct {
 #define	spin_lock(_l) do {			\
 	if (SPIN_SKIP())			\
 		break;				\
-	mtx_lock(&(_l)->m);			\
+	mtx_lock(_l);				\
 	local_bh_disable();			\
 } while (0)
 
@@ -76,7 +74,7 @@ typedef struct {
 	if (SPIN_SKIP())			\
 		break;				\
 	local_bh_enable();			\
-	mtx_unlock(&(_l)->m);			\
+	mtx_unlock(_l);				\
 } while (0)
 
 #define	spin_unlock_bh(_l) do {			\
@@ -93,7 +91,7 @@ typedef struct {
 	if (SPIN_SKIP()) {			\
 		__ret = 1;			\
 	} else {				\
-		__ret = mtx_trylock(&(_l)->m);	\
+		__ret = mtx_trylock(_l);	\
 		if (likely(__ret != 0))		\
 			local_bh_disable();	\
 	}					\
@@ -111,7 +109,7 @@ typedef struct {
 #define	spin_lock_nested(_l, _n) do {		\
 	if (SPIN_SKIP())			\
 		break;				\
-	mtx_lock_flags(&(_l)->m, MTX_DUPOK);	\
+	mtx_lock_flags(_l, MTX_DUPOK);		\
 	local_bh_disable();			\
 } while (0)
 
@@ -141,31 +139,19 @@ typedef struct {
 #define	_spin_lock_name(...)		__spin_lock_name(__VA_ARGS__)
 #define	spin_lock_name(name)		_spin_lock_name(name, __FILE__, __LINE__)
 
-#define	spin_lock_init(lock)	linux_spin_lock_init(lock, spin_lock_name("lnxspin"))
+#define	spin_lock_init(lock)	mtx_init(lock, spin_lock_name("lnxspin"), \
+				  NULL, MTX_DEF | MTX_NOWITNESS | MTX_NEW)
 
-static inline void
-linux_spin_lock_init(spinlock_t *lock, const char *name)
-{
-
-	memset(lock, 0, sizeof(*lock));
-	mtx_init(&lock->m, name, NULL, MTX_DEF | MTX_NOWITNESS);
-}
-
-static inline void
-spin_lock_destroy(spinlock_t *lock)
-{
-
-       mtx_destroy(&lock->m);
-}
+#define	spin_lock_destroy(_l)	mtx_destroy(_l)
 
 #define	DEFINE_SPINLOCK(lock)					\
 	spinlock_t lock;					\
-	MTX_SYSINIT(lock, &(lock).m, spin_lock_name("lnxspin"), MTX_DEF)
+	MTX_SYSINIT(lock, &lock, spin_lock_name("lnxspin"), MTX_DEF)
 
 #define	assert_spin_locked(_l) do {		\
 	if (SPIN_SKIP())			\
 		break;				\
-	mtx_assert(&(_l)->m, MA_OWNED);		\
+	mtx_assert(_l, MA_OWNED);		\
 } while (0)
 
 #define	atomic_dec_and_lock_irqsave(cnt, lock, flags) \
diff --git a/sys/compat/linuxkpi/common/include/linux/wait.h b/sys/compat/linuxkpi/common/include/linux/wait.h
index d50003d44955..b815050b6faa 100644
--- a/sys/compat/linuxkpi/common/include/linux/wait.h
+++ b/sys/compat/linuxkpi/common/include/linux/wait.h
@@ -113,7 +113,7 @@ extern wait_queue_func_t default_wake_function;
 	MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF)
 
 #define	init_waitqueue_head(wqh) do {					\
-	mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"),		\
+	mtx_init(&(wqh)->lock, spin_lock_name("wqhead"),		\
 	    NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS);			\
 	INIT_LIST_HEAD(&(wqh)->task_list);				\
 } while (0)
diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c
index 36ed7b84cc94..b0691eb0f79a 100644
--- a/sys/compat/linuxkpi/common/src/linux_compat.c
+++ b/sys/compat/linuxkpi/common/src/linux_compat.c
@@ -382,9 +382,9 @@ linux_kq_assert_lock(void *arg, int what)
 	spinlock_t *s = arg;
 
 	if (what == LA_LOCKED)
-		mtx_assert(&s->m, MA_OWNED);
+		mtx_assert(s, MA_OWNED);
 	else
-		mtx_assert(&s->m, MA_NOTOWNED);
+		mtx_assert(s, MA_NOTOWNED);
 #endif
 }
 
@@ -1094,7 +1094,7 @@ linux_file_kqfilter_read_event(struct knote *kn, long hint)
 {
 	struct linux_file *filp = kn->kn_hook;
 
-	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
+	mtx_assert(&filp->f_kqlock, MA_OWNED);
 
 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_READ) ? 1 : 0);
 }
@@ -1104,7 +1104,7 @@ linux_file_kqfilter_write_event(struct knote *kn, long hint)
 {
 	struct linux_file *filp = kn->kn_hook;
 
-	mtx_assert(&filp->f_kqlock.m, MA_OWNED);
+	mtx_assert(&filp->f_kqlock, MA_OWNED);
 
 	return ((filp->f_kqflags & LINUX_KQ_FLAG_NEED_WRITE) ? 1 : 0);
 }
diff --git a/sys/compat/linuxkpi/common/src/linux_idr.c b/sys/compat/linuxkpi/common/src/linux_idr.c
index 657d85606f50..b80f8c168d45 100644
--- a/sys/compat/linuxkpi/common/src/linux_idr.c
+++ b/sys/compat/linuxkpi/common/src/linux_idr.c
@@ -70,7 +70,7 @@ idr_preload_dequeue_locked(struct linux_idr_cache *lic)
 	struct idr_layer *retval;
 
 	/* check if wrong thread is trying to dequeue */
-	if (mtx_owned(&lic->lock.m) == 0)
+	if (mtx_owned(&lic->lock) == 0)
 		return (NULL);
 
 	retval = lic->head;
diff --git a/sys/dev/mlx4/mlx4_en/mlx4_en_tx.c b/sys/dev/mlx4/mlx4_en/mlx4_en_tx.c
index ee8ed0da240d..9a73c7571fd7 100644
--- a/sys/dev/mlx4/mlx4_en/mlx4_en_tx.c
+++ b/sys/dev/mlx4/mlx4_en/mlx4_en_tx.c
@@ -91,8 +91,8 @@ int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
 	ring->size_mask = size - 1;
 	ring->stride = stride;
 	ring->inline_thold = MAX(MIN_PKT_LEN, MIN(priv->prof->inline_thold, MAX_INLINE));
-	mtx_init(&ring->tx_lock.m, "mlx4 tx", NULL, MTX_DEF);
-	mtx_init(&ring->comp_lock.m, "mlx4 comp", NULL, MTX_DEF);
+	mtx_init(&ring->tx_lock, "mlx4 tx", NULL, MTX_DEF);
+	mtx_init(&ring->comp_lock, "mlx4 comp", NULL, MTX_DEF);
 
 	tmp = size * sizeof(struct mlx4_en_tx_info);
 	ring->tx_info = kzalloc_node(tmp, GFP_KERNEL, node);
@@ -205,8 +205,8 @@ void mlx4_en_destroy_tx_ring(struct mlx4_en_priv *priv,
 	for (x = 0; x != ring->size; x++)
 		bus_dmamap_destroy(ring->dma_tag, ring->tx_info[x].dma_map);
 	vfree(ring->tx_info);
-	mtx_destroy(&ring->tx_lock.m);
-	mtx_destroy(&ring->comp_lock.m);
+	mtx_destroy(&ring->tx_lock);
+	mtx_destroy(&ring->comp_lock);
 	bus_dma_tag_destroy(ring->dma_tag);
 	kfree(ring);
 	*pring = NULL;