git: ae38a1a1bfdf - main - linuxkpi: spinlock: Simplify code

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 16 May 2024 07:41:24 UTC
The branch main has been updated by manu:

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

commit ae38a1a1bfdf320089c254e4dbffdf4769d89110
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2024-05-15 09:00:04 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2024-05-16 07:39:36 +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
---
 .../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..3f6eb4bb70f6 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)
 
-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 a6eb7bb17e16..4c639c1f8459 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 dc64da0d7cf5..583e2c237198 100644
--- a/sys/compat/linuxkpi/common/src/linux_idr.c
+++ b/sys/compat/linuxkpi/common/src/linux_idr.c
@@ -69,7 +69,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;