diff mbox series

[for-next,v10,11/11] RDMA/rxe: Cleanup rxe_pool.c

Message ID 20220225195750.37802-12-rpearsonhpe@gmail.com (mailing list archive)
State Superseded
Delegated to: Jason Gunthorpe
Headers show
Series Fix race conditions in rxe_pool | expand

Commit Message

Bob Pearson Feb. 25, 2022, 7:57 p.m. UTC
Minor cleanup of rxe_pool.c. Add document comment headers for
the subroutines. Increase alignment for pool elements.

Signed-off-by: Bob Pearson <rpearsonhpe@gmail.com>
---
 drivers/infiniband/sw/rxe/rxe_pool.c | 110 ++++++++++++++++++++++-----
 1 file changed, 93 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c
index 20b97a90b4c8..8c207c90304a 100644
--- a/drivers/infiniband/sw/rxe/rxe_pool.c
+++ b/drivers/infiniband/sw/rxe/rxe_pool.c
@@ -1,14 +1,20 @@ 
 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 /*
+ * Copyright (c) 2022 Hewlett Packard Enterprise, Inc. All rights reserved.
  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  */
 
 #include "rxe.h"
 
-#define RXE_POOL_TIMEOUT	(200)
-#define RXE_MAX_POOL_TIMEOUTS	(3)
-#define RXE_POOL_ALIGN		(16)
+#define RXE_POOL_TIMEOUT	200		/* jiffies */
+#define RXE_MAX_POOL_TIMEOUTS	3
+
+#ifdef L1_CACHE_BYTES
+#define RXE_POOL_ALIGN		L1_CACHE_BYTES
+#else
+#define RXE_POOL_ALIGN		64
+#endif
 
 static const struct rxe_type_info {
 	const char *name;
@@ -21,7 +27,7 @@  static const struct rxe_type_info {
 	u32 max_elem;
 } rxe_type_info[RXE_NUM_TYPES] = {
 	[RXE_TYPE_UC] = {
-		.name		= "rxe-uc",
+		.name		= "uc",
 		.size		= sizeof(struct rxe_ucontext),
 		.elem_offset	= offsetof(struct rxe_ucontext, elem),
 		.min_index	= 1,
@@ -29,7 +35,7 @@  static const struct rxe_type_info {
 		.max_elem	= UINT_MAX,
 	},
 	[RXE_TYPE_PD] = {
-		.name		= "rxe-pd",
+		.name		= "pd",
 		.size		= sizeof(struct rxe_pd),
 		.elem_offset	= offsetof(struct rxe_pd, elem),
 		.min_index	= 1,
@@ -37,7 +43,7 @@  static const struct rxe_type_info {
 		.max_elem	= UINT_MAX,
 	},
 	[RXE_TYPE_AH] = {
-		.name		= "rxe-ah",
+		.name		= "ah",
 		.size		= sizeof(struct rxe_ah),
 		.elem_offset	= offsetof(struct rxe_ah, elem),
 		.min_index	= RXE_MIN_AH_INDEX,
@@ -45,7 +51,7 @@  static const struct rxe_type_info {
 		.max_elem	= RXE_MAX_AH_INDEX - RXE_MIN_AH_INDEX + 1,
 	},
 	[RXE_TYPE_SRQ] = {
-		.name		= "rxe-srq",
+		.name		= "srq",
 		.size		= sizeof(struct rxe_srq),
 		.elem_offset	= offsetof(struct rxe_srq, elem),
 		.min_index	= RXE_MIN_SRQ_INDEX,
@@ -53,7 +59,7 @@  static const struct rxe_type_info {
 		.max_elem	= RXE_MAX_SRQ_INDEX - RXE_MIN_SRQ_INDEX + 1,
 	},
 	[RXE_TYPE_QP] = {
-		.name		= "rxe-qp",
+		.name		= "qp",
 		.size		= sizeof(struct rxe_qp),
 		.elem_offset	= offsetof(struct rxe_qp, elem),
 		.cleanup	= rxe_qp_cleanup,
@@ -62,7 +68,7 @@  static const struct rxe_type_info {
 		.max_elem	= RXE_MAX_QP_INDEX - RXE_MIN_QP_INDEX + 1,
 	},
 	[RXE_TYPE_CQ] = {
-		.name		= "rxe-cq",
+		.name		= "cq",
 		.size		= sizeof(struct rxe_cq),
 		.elem_offset	= offsetof(struct rxe_cq, elem),
 		.cleanup	= rxe_cq_cleanup,
@@ -71,7 +77,7 @@  static const struct rxe_type_info {
 		.max_elem	= UINT_MAX,
 	},
 	[RXE_TYPE_MR] = {
-		.name		= "rxe-mr",
+		.name		= "mr",
 		.size		= sizeof(struct rxe_mr),
 		.elem_offset	= offsetof(struct rxe_mr, elem),
 		.cleanup	= rxe_mr_cleanup,
@@ -81,7 +87,7 @@  static const struct rxe_type_info {
 		.max_elem	= RXE_MAX_MR_INDEX - RXE_MIN_MR_INDEX + 1,
 	},
 	[RXE_TYPE_MW] = {
-		.name		= "rxe-mw",
+		.name		= "mw",
 		.size		= sizeof(struct rxe_mw),
 		.elem_offset	= offsetof(struct rxe_mw, elem),
 		.min_index	= RXE_MIN_MW_INDEX,
@@ -90,6 +96,12 @@  static const struct rxe_type_info {
 	},
 };
 
+/**
+ * rxe_pool_init - initialize a rxe object pool
+ * @rxe: rxe device pool belongs to
+ * @pool: object pool
+ * @type: pool type
+ */
 void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
 		   enum rxe_elem_type type)
 {
@@ -113,6 +125,10 @@  void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
 	pool->limit.min = info->min_index;
 }
 
+/**
+ * rxe_pool_cleanup - free any remaining pool resources
+ * @pool: object pool
+ */
 void rxe_pool_cleanup(struct rxe_pool *pool)
 {
 	struct rxe_pool_elem *elem;
@@ -135,9 +151,15 @@  void rxe_pool_cleanup(struct rxe_pool *pool)
 
 	if (elem_count || obj_count)
 		pr_warn("Freed %d indices and %d objects from pool %s\n",
-			elem_count, obj_count, pool->name + 4);
+			elem_count, obj_count, pool->name);
 }
 
+/**
+ * rxe_alloc - allocate a new pool object
+ * @pool: object pool
+ *
+ * Returns: object on success else NULL
+ */
 void *rxe_alloc(struct rxe_pool *pool)
 {
 	struct rxe_pool_elem *elem;
@@ -178,6 +200,13 @@  void *rxe_alloc(struct rxe_pool *pool)
 	return NULL;
 }
 
+/**
+ * __rxe_add_to_pool - add rdma-core allocated object to rxe object pool
+ * @pool: object pool
+ * @elem: rxe_pool_elem embedded in object
+ *
+ * Returns: 0 on success else an error
+ */
 int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem)
 {
 	int err;
@@ -208,6 +237,13 @@  int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem)
 	return -EINVAL;
 }
 
+/**
+ * rxe_pool_get - find object in pool with given index
+ * @pool: object pool
+ * @index: index
+ *
+ * Returns: object on success else NULL
+ */
 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index)
 {
 	struct rxe_pool_elem *elem;
@@ -224,6 +260,16 @@  void *rxe_pool_get_index(struct rxe_pool *pool, u32 index)
 	return obj;
 }
 
+/**
+ * rxe_elem_release - remove object index and complete
+ * @kref: kref embedded in pool element
+ * @flags: flags for lock release
+ *
+ * Context: called holding pool->xa.xa_lock which must be
+ *	    dropped. Called from __rxe_drop_ref() when the ref count
+ *	    reaches zero. Completes the object scheduling
+ *	    a waiter on the object.
+ */
 static void rxe_elem_release(struct kref *kref, unsigned long flags)
 	__releases(&pool->xa.xa_lock)
 {
@@ -238,13 +284,28 @@  static void rxe_elem_release(struct kref *kref, unsigned long flags)
 	complete(&elem->complete);
 }
 
+/**
+ * __rxe_add_ref - gets a kref on the object unless ref count is zero
+ * @elem: rxe_pool_elem embedded in object
+ *
+ * Returns: 1 if reference is added else 0 because
+ *	    ref count has reached zero
+ */
 int __rxe_add_ref(struct rxe_pool_elem *elem)
 {
 	return kref_get_unless_zero(&elem->ref_cnt);
 }
 
-/* local copy of kref_put_lock_irqsave same as kref_put_lock
- * except for _irqsave locks
+/**
+ * kref_put_lock_irqsave - local copy of kref_put_lock
+ * @kref: kref embedded in elem
+ * @release: cleanup function called when ref count reaches zero
+ * @lock: irqsave spinlock to take if ref count reaches zero
+ *
+ * Same as kref_put_lock except for _irqsave locks
+ *
+ * Returns: 1 if ref count reaches zero and release is called
+ *	    while holding the lock else 0.
  */
 static int kref_put_lock_irqsave(struct kref *kref,
 		 void (*release)(struct kref *kref, unsigned long flags),
@@ -259,6 +320,15 @@  static int kref_put_lock_irqsave(struct kref *kref,
 	return 0;
 }
 
+/**
+ * __rxe_drop_ref - puts a kref on the object
+ * @elem: rxe_pool_elem embedded in object
+ *
+ * Puts a kref on the object and if ref count reaches zero
+ * takes the lock and calls release() which must free the lock.
+ *
+ * Returns: 1 if ref count reaches zero and release called else 0
+ */
 int __rxe_drop_ref(struct rxe_pool_elem *elem)
 {
 	struct rxe_pool *pool = elem->pool;
@@ -267,6 +337,12 @@  int __rxe_drop_ref(struct rxe_pool_elem *elem)
 			&pool->xa.xa_lock);
 }
 
+/**
+ * __rxe_drop_wait - put a kref on object and wait for completion
+ * @elem: rxe_pool_elem embedded in object
+ *
+ * Returns: non-zero value if completion is reached else 0 if timeout
+ */
 int __rxe_drop_wait(struct rxe_pool_elem *elem)
 {
 	struct rxe_pool *pool = elem->pool;
@@ -275,7 +351,7 @@  int __rxe_drop_wait(struct rxe_pool_elem *elem)
 
 	if (elem->enabled) {
 		pr_warn_once("%s#%d: should be disabled by now\n",
-			     elem->pool->name + 4, elem->index);
+			     elem->pool->name, elem->index);
 		elem->enabled = false;
 	}
 
@@ -285,11 +361,11 @@  int __rxe_drop_wait(struct rxe_pool_elem *elem)
 		ret = wait_for_completion_timeout(&elem->complete, timeout);
 		if (!ret) {
 			pr_warn("Timed out waiting for %s#%d\n",
-				pool->name + 4, elem->index);
+				pool->name, elem->index);
 			if (++pool->timeouts == RXE_MAX_POOL_TIMEOUTS) {
 				timeout = 0;
 				pr_warn("Reached max %s timeouts.\n",
-					pool->name + 4);
+					pool->name);
 			}
 		}
 	}