Skip to contentSkip to conversation

MR cache fixes and refactoring

PATCH In review Leon Romanovsky 3 participants 13 messages Feb 27, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover MR cache fixes and refactoring

From: Leon Romanovsky <[email protected]>

Hi,

This series fixes various corner cases in the mlx5_ib MR cache implementation, see specific commit messages for more information.

Thanks

Jason Gunthorpe (8):
RDMA/mlx5: Rename the tracking variables for the MR cache RDMA/mlx5: Simplify how the MR cache bucket is located RDMA/mlx5: Always remove MRs from the cache before destroying them RDMA/mlx5: Fix MR cache size and limit debugfs RDMA/mlx5: Lock access to ent->available_mrs/limit when doing
queue_work
RDMA/mlx5: Fix locking in MR cache work queue
RDMA/mlx5: Revise how the hysteresis scheme works for cache filling RDMA/mlx5: Allow MRs to be created in the cache synchronously

Michael Guralnik (1):
RDMA/mlx5: Move asynchronous mkey creation to mlx5_ib

 drivers/infiniband/hw/mlx5/mlx5_ib.h         |  33 +-
 drivers/infiniband/hw/mlx5/mr.c              | 642 +++++++++++--------
 drivers/infiniband/hw/mlx5/odp.c             |   2 +-
 drivers/net/ethernet/mellanox/mlx5/core/mr.c |  22 +-
 include/linux/mlx5/driver.h                  |   6 -
 5 files changed, 404 insertions(+), 301 deletions(-)

--
2.24.1

#5 lore raw

PATCH 2/9 RDMA/mlx5: Rename the tracking variables for the MR cache

From: Jason Gunthorpe <[email protected]>

The old names do not clearly indicate the intent.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h | 19 +++++++---
 drivers/infiniband/hw/mlx5/mr.c      | 54 ++++++++++++++--------------
 2 files changed, 42 insertions(+), 31 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +15 -4
index fd812a24db25..2b566829427e 100644
@@ -703,15 +703,26 @@ struct mlx5_cache_ent {
 	u32			access_mode;
 	u32			page;
 
-	u32			size;
-	u32                     cur;
+	/*
+	 * - available_mrs is the length of list head, ie the number of MRs
+	 *   available for immediate allocation.
+	 * - total_mrs is available_mrs plus all in use MRs that could be
+	 *   returned to the cache.
+	 * - limit is the low water mark for available_mrs, 2* limit is the
+	 *   upper water mark.
+	 * - pending is the number of MRs currently being created
+	 */
+	u32 total_mrs;
+	u32 available_mrs;
+	u32 limit;
+	u32 pending;
+
+	/* Statistics */
 	u32                     miss;
-	u32			limit;
 
 	struct mlx5_ib_dev     *dev;
 	struct work_struct	work;
 	struct delayed_work	dwork;
-	int			pending;
 	struct completion	compl;
 };
 
drivers/infiniband/hw/mlx5/mr.c +27 -27

157 lines not on this page · raw message

Signature · 1 line
2.24.1
#6 lore raw

PATCH 3/9 RDMA/mlx5: Simplify how the MR cache bucket is located

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

There are many bad APIs here that are accepting a cache bucket index instead of a bucket pointer. Many of the callers already have a bucket pointer, so this results in a lot of confusing uses of order2idx().

Pass the struct mlx5_cache_ent into add_keys(), remove_keys(), and alloc_cached_mr().

Once the MR is in the cache, store the cache bucket pointer directly in the MR, replacing the 'bool allocated_from cache'.

In the end there is only one place that needs to form index from order, alloc_mr_from_cache(). Increase the safety of this function by disallowing it from accessing cache entries in the ODP special area.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   7 +-
 drivers/infiniband/hw/mlx5/mr.c      | 155 +++++++++++----------------
 drivers/infiniband/hw/mlx5/odp.c     |   2 +-
 3 files changed, 69 insertions(+), 95 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +4 -3
index 2b566829427e..7aef89cc472d 100644
@@ -621,8 +621,8 @@ struct mlx5_ib_mr {
 	struct ib_umem	       *umem;
 	struct mlx5_shared_mr_info	*smr_info;
 	struct list_head	list;
-	int			order;
-	bool			allocated_from_cache;
+	unsigned int		order;
+	struct mlx5_cache_ent  *cache_ent;
 	int			npages;
 	struct mlx5_ib_dev     *dev;
 	u32 out[MLX5_ST_SZ_DW(create_mkey_out)];
@@ -1276,7 +1276,8 @@ int mlx5_ib_get_cqe_size(struct ib_cq *ibcq);
 int mlx5_mr_cache_init(struct mlx5_ib_dev *dev);
 int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev);
 
-struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int entry);
+struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev,
+				       unsigned int entry);
 void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
 int mlx5_mr_cache_invalidate(struct mlx5_ib_mr *mr);
 
drivers/infiniband/hw/mlx5/mr.c +64 -91

390 lines not on this page · raw message

drivers/infiniband/hw/mlx5/odp.c +1 -1
index 4216814ba871..224f480fc441 100644
@@ -197,7 +197,7 @@ static void dma_fence_odp_mr(struct mlx5_ib_mr *mr)
 	odp->private = NULL;
 	mutex_unlock(&odp->umem_mutex);
 
-	if (!mr->allocated_from_cache) {
+	if (!mr->cache_ent) {
 		mlx5_core_destroy_mkey(mr->dev->mdev, &mr->mmkey);
 		WARN_ON(mr->descs);
 	}
Signature · 1 line
2.24.1
#7 lore raw

PATCH 4/9 RDMA/mlx5: Always remove MRs from the cache before destroying them

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

The cache bucket tracks the total number of MRs that exists, both inside and outside of the cache. Removing a MR from the cache (by setting cache_ent to NULL) without updating total_mrs will cause the tracking to leak and be inflated.

Further fix the rereg_mr path to always destroy the MR. reg_create will always overwrite all the MR data in mlx5_ib_mr, so the MR must be completely destroyed, in all cases, before this function can be called. Detach the MR from the cache and unconditionally destroy it to avoid leaking HW mkeys.

Fixes afd1417404fb ("IB/mlx5: Use direct mkey destroy command upon UMR unreg failure")
Fixes 56e11d628c5d ("IB/mlx5: Added support for re-registration of MRs")
---
 drivers/infiniband/hw/mlx5/mr.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +13 -6
index 68e5a3955b1d..c951bb14de56 100644
@@ -464,6 +464,16 @@ static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_cache_ent *req_ent)
 	return mr;
 }
 
+static void detach_mr_from_cache(struct mlx5_ib_mr *mr)
+{
+	struct mlx5_cache_ent *ent = mr->cache_ent;
+
+	mr->cache_ent = NULL;
+	spin_lock_irq(&ent->lock);
+	ent->total_mrs--;
+	spin_unlock_irq(&ent->lock);
+}
+
 void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 {
 	struct mlx5_cache_ent *ent = mr->cache_ent;
@@ -473,7 +483,7 @@ void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 		return;
 
 	if (mlx5_mr_cache_invalidate(mr)) {
-		mr->cache_ent = NULL;
+		detach_mr_from_cache(mr);
 		destroy_mkey(dev, mr);
 		if (ent->available_mrs < ent->limit)
 			queue_work(dev->cache.wq, &ent->work);
@@ -1432,9 +1442,8 @@ int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
 		 * UMR can't be used - MKey needs to be replaced.
 		 */
 		if (mr->cache_ent)
-			err = mlx5_mr_cache_invalidate(mr);
-		else
-			err = destroy_mkey(dev, mr);
+			detach_mr_from_cache(mr);
+		err = destroy_mkey(dev, mr);
 		if (err)
 			goto err;
 
@@ -1446,8 +1455,6 @@ int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
 			mr = to_mmr(ib_mr);
 			goto err;
 		}
-
-		mr->cache_ent = NULL;
 	} else {
 		/*
 		 * Send a UMR WQE

--
2.24.1

#1 lore raw

PATCH 1/9 RDMA/mlx5: Move asynchronous mkey creation to mlx5_ib

In reply to Leon Romanovsky

From: Michael Guralnik <[email protected]>

As mlx5_ib is the only user of the mlx5_core_create_mkey_cb, move the logic inside mlx5_ib and cleanup the code in mlx5_core.

---
 drivers/infiniband/hw/mlx5/mr.c              | 25 ++++++++++++++++----
 drivers/net/ethernet/mellanox/mlx5/core/mr.c | 22 +++--------------
 include/linux/mlx5/driver.h                  |  6 -----
 3 files changed, 24 insertions(+), 29 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +21 -4
index 6fa0a83c19de..dea14477a676 100644
@@ -79,6 +79,25 @@ static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length)
 		length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
 }
 
+static int create_mkey_cb(struct mlx5_core_dev *dev, struct mlx5_ib_mr *mr,
+			  struct mlx5_async_ctx *async_ctx, u32 *in, int inlen,
+			  mlx5_async_cbk_t callback)
+{
+	void *mkc;
+	u8 key;
+
+	spin_lock_irq(&dev->priv.mkey_lock);
+	key = dev->priv.mkey_key++;
+	spin_unlock_irq(&dev->priv.mkey_lock);
+	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
+
+	MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
+	MLX5_SET(mkc, mkc, mkey_7_0, key);
+
+	return mlx5_cmd_exec_cb(async_ctx, in, inlen, mr->out, sizeof(mr->out),
+				callback, &mr->cb_work);
+}
+
 static void reg_mr_callback(int status, struct mlx5_async_work *context)
 {
 	struct mlx5_ib_mr *mr =
@@ -163,10 +182,8 @@ static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
 		spin_lock_irq(&ent->lock);
 		ent->pending++;
 		spin_unlock_irq(&ent->lock);
-		err = mlx5_core_create_mkey_cb(dev->mdev, &mr->mmkey,
-					       &dev->async_ctx, in, inlen,
-					       mr->out, sizeof(mr->out),
-					       reg_mr_callback, &mr->cb_work);
+		err = create_mkey_cb(dev->mdev, mr, &dev->async_ctx, in, inlen,
+				     reg_mr_callback);
 		if (err) {
 			spin_lock_irq(&ent->lock);
 			ent->pending--;
drivers/net/ethernet/mellanox/mlx5/core/mr.c +3 -19
index 42cc3c7ac5b6..83841e4119d7 100644
@@ -36,12 +36,9 @@
 #include <linux/mlx5/cmd.h>
 #include "mlx5_core.h"
 
-int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
-			     struct mlx5_core_mkey *mkey,
-			     struct mlx5_async_ctx *async_ctx, u32 *in,
-			     int inlen, u32 *out, int outlen,
-			     mlx5_async_cbk_t callback,
-			     struct mlx5_async_work *context)
+int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
+			  struct mlx5_core_mkey *mkey,
+			  u32 *in, int inlen)
 {
 	u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
 	u32 mkey_index;
@@ -57,10 +54,6 @@ int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
 	MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
 	MLX5_SET(mkc, mkc, mkey_7_0, key);
 
-	if (callback)
-		return mlx5_cmd_exec_cb(async_ctx, in, inlen, out, outlen,
-					callback, context);
-
 	err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
 	if (err)
 		return err;
@@ -75,15 +68,6 @@ int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
 		      mkey_index, key, mkey->key);
 	return 0;
 }
-EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
-
-int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
-			  struct mlx5_core_mkey *mkey,
-			  u32 *in, int inlen)
-{
-	return mlx5_core_create_mkey_cb(dev, mkey, NULL, in, inlen,
-					NULL, 0, NULL, NULL);
-}
 EXPORT_SYMBOL(mlx5_core_create_mkey);
 
 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev,
include/linux/mlx5/driver.h +0 -6
index f2b4225ed650..7225e9ca0f25 100644
@@ -947,12 +947,6 @@ struct mlx5_cmd_mailbox *mlx5_alloc_cmd_mailbox_chain(struct mlx5_core_dev *dev,
 						      gfp_t flags, int npages);
 void mlx5_free_cmd_mailbox_chain(struct mlx5_core_dev *dev,
 				 struct mlx5_cmd_mailbox *head);
-int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
-			     struct mlx5_core_mkey *mkey,
-			     struct mlx5_async_ctx *async_ctx, u32 *in,
-			     int inlen, u32 *out, int outlen,
-			     mlx5_async_cbk_t callback,
-			     struct mlx5_async_work *context);
 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
 			  struct mlx5_core_mkey *mkey,
 			  u32 *in, int inlen);
Signature · 1 line
2.24.1
#9 lore raw

PATCH 6/9 RDMA/mlx5: Lock access to ent->available_mrs/limit when doing queue_work

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

Accesses to these members needs to be locked. There is no reason not to hold a spinlock while calling queue_work(), so move the tests into a helper and always call it under lock.

The helper should be called when available_mrs is adjusted.

---
 drivers/infiniband/hw/mlx5/mr.c | 40 ++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 15 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +25 -15
index 00d7dc4cd91b..17dda58360c4 100644
@@ -120,6 +120,10 @@ static void reg_mr_callback(int status, struct mlx5_async_work *context)
 	list_add_tail(&mr->list, &ent->head);
 	ent->available_mrs++;
 	ent->total_mrs++;
+	/*
+	 * Creating is always done in response to some demand, so do not call
+	 * queue_adjust_cache_locked().
+	 */
 	spin_unlock_irqrestore(&ent->lock, flags);
 
 	if (!completion_done(&ent->compl))
@@ -352,6 +356,20 @@ static int someone_adding(struct mlx5_mr_cache *cache)
 	return 0;
 }
 
+/*
+ * Check if the bucket is outside the high/low water mark and schedule an async
+ * update. The cache refill has hysteresis, once the low water mark is hit it is
+ * refilled up to the high mark.
+ */
+static void queue_adjust_cache_locked(struct mlx5_cache_ent *ent)
+{
+	lockdep_assert_held(&ent->lock);
+
+	if (ent->available_mrs < ent->limit ||
+	    ent->available_mrs > 2 * ent->limit)
+		queue_work(ent->dev->cache.wq, &ent->work);
+}
+
 static void __cache_work_func(struct mlx5_cache_ent *ent)
 {
 	struct mlx5_ib_dev *dev = ent->dev;
@@ -447,9 +465,8 @@ struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev,
 					      list);
 			list_del(&mr->list);
 			ent->available_mrs--;
+			queue_adjust_cache_locked(ent);
 			spin_unlock_irq(&ent->lock);
-			if (ent->available_mrs < ent->limit)
-				queue_work(cache->wq, &ent->work);
 			return mr;
 		}
 	}
@@ -472,14 +489,12 @@ static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_cache_ent *req_ent)
 					      list);
 			list_del(&mr->list);
 			ent->available_mrs--;
+			queue_adjust_cache_locked(ent);
 			spin_unlock_irq(&ent->lock);
-			if (ent->available_mrs < ent->limit)
-				queue_work(dev->cache.wq, &ent->work);
 			break;
 		}
+		queue_adjust_cache_locked(ent);
 		spin_unlock_irq(&ent->lock);
-
-		queue_work(dev->cache.wq, &ent->work);
 	}
 
 	if (!mr)
@@ -501,7 +516,6 @@ static void detach_mr_from_cache(struct mlx5_ib_mr *mr)
 void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 {
 	struct mlx5_cache_ent *ent = mr->cache_ent;
-	int shrink = 0;
 
 	if (!ent)
 		return;
@@ -509,20 +523,14 @@ void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 	if (mlx5_mr_cache_invalidate(mr)) {
 		detach_mr_from_cache(mr);
 		destroy_mkey(dev, mr);
-		if (ent->available_mrs < ent->limit)
-			queue_work(dev->cache.wq, &ent->work);
 		return;
 	}
 
 	spin_lock_irq(&ent->lock);
 	list_add_tail(&mr->list, &ent->head);
 	ent->available_mrs++;
-	if (ent->available_mrs > 2 * ent->limit)
-		shrink = 1;
+	queue_adjust_cache_locked(ent);
 	spin_unlock_irq(&ent->lock);
-
-	if (shrink)
-		queue_work(dev->cache.wq, &ent->work);
 }
 
 static void clean_keys(struct mlx5_ib_dev *dev, int c)
@@ -638,7 +646,9 @@ int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
 			ent->limit = dev->mdev->profile->mr_cache[i].limit;
 		else
 			ent->limit = 0;
-		queue_work(cache->wq, &ent->work);
+		spin_lock_irq(&ent->lock);
+		queue_adjust_cache_locked(ent);
+		spin_unlock_irq(&ent->lock);
 	}
 
 	mlx5_mr_cache_debugfs_init(dev);
Signature · 1 line
2.24.1
#10 lore raw

PATCH 7/9 RDMA/mlx5: Fix locking in MR cache work queue

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

All of the members of mlx5_cache_ent must be accessed while holding the spinlock, add the missing spinlock in the __cache_work_func().

Using cache->stopped and flush_workqueue() is an inherently racy way to shutdown self-scheduling work on a queue. Replace it with ent->disabled under lock, and always check disabled before queuing any new work. Use cancel_work_sync() to shutdown the queue.

Use READ_ONCE/WRITE_ONCE for dev->last_add to manage concurrency as coherency is less important here.

Split fill_delay from the bitfield. C bitfield updates are not atomic and this is just a mess. Use READ_ONCE/WRITE_ONCE, but this could also use test_bit()/set_bit().

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   5 +-
 drivers/infiniband/hw/mlx5/mr.c      | 121 +++++++++++++++++----------
 2 files changed, 80 insertions(+), 46 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +3 -2
index 7aef89cc472d..08554bd8941e 100644
@@ -703,6 +703,8 @@ struct mlx5_cache_ent {
 	u32			access_mode;
 	u32			page;
 
+	u8 disabled:1;
+
 	/*
 	 * - available_mrs is the length of list head, ie the number of MRs
 	 *   available for immediate allocation.
@@ -729,7 +731,6 @@ struct mlx5_cache_ent {
 struct mlx5_mr_cache {
 	struct workqueue_struct *wq;
 	struct mlx5_cache_ent	ent[MAX_MR_CACHE_ENTRIES];
-	int			stopped;
 	struct dentry		*root;
 	unsigned long		last_add;
 };
@@ -999,10 +1000,10 @@ struct mlx5_ib_dev {
 	 */
 	struct mutex			cap_mask_mutex;
 	u8				ib_active:1;
-	u8				fill_delay:1;
 	u8				is_rep:1;
 	u8				lag_active:1;
 	u8				wc_support:1;
+	u8				fill_delay;
 	struct umr_common		umrc;
 	/* sync used page count stats
 	 */
drivers/infiniband/hw/mlx5/mr.c +77 -44

233 lines not on this page · raw message

Signature · 1 line
2.24.1
#11 lore raw

PATCH 8/9 RDMA/mlx5: Revise how the hysteresis scheme works for cache filling

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

Currently if the work queue is running then it is in 'hysteresis' mode and will fill until the cache reaches the high water mark. This implicit state is very tricky and doesn't interact with pending very well.

Instead of self re-scheduling the work queue after the add_keys() has started to create the new MR, have the queue scheduled from reg_mr_callback() only after the requested MR has been added.

This avoids the bad design of an in-rush of queue'd work doing back to back add_keys() until EAGAIN then sleeping. The add_keys() will be paced one at a time as they complete, slowly filling up the cache.

Also, fix pending to be only manipulated under lock.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  1 +
 drivers/infiniband/hw/mlx5/mr.c      | 41 ++++++++++++++++++----------
 2 files changed, 27 insertions(+), 15 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +1 -0
index 08554bd8941e..e997837e600c 100644
@@ -704,6 +704,7 @@ struct mlx5_cache_ent {
 	u32			page;
 
 	u8 disabled:1;
+	u8 fill_to_high_water:1;
 
 	/*
 	 * - available_mrs is the length of list head, ie the number of MRs
drivers/infiniband/hw/mlx5/mr.c +26 -15

77 lines not on this page · raw message

Signature · 1 line
2.24.1
#12 lore raw

PATCH 9/9 RDMA/mlx5: Allow MRs to be created in the cache synchronously

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

If the cache is completely out of MRs, and we are running in cache mode, then directly, and synchronously, create an MR that is compatible with the cache bucket using a sleeping mailbox command. This ensures that the thread that is waiting for the MR absolutely will get one.

When a MR allocated in this way becomes freed then it is compatible with the cache bucket and will be recycled back into it.

Deletes the very buggy ent->compl scheme to create a synchronous MR allocation.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   1 -
 drivers/infiniband/hw/mlx5/mr.c      | 173 ++++++++++++++++-----------
 2 files changed, 105 insertions(+), 69 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +0 -1
index e997837e600c..0ea8d57827fb 100644
@@ -726,7 +726,6 @@ struct mlx5_cache_ent {
 	struct mlx5_ib_dev     *dev;
 	struct work_struct	work;
 	struct delayed_work	dwork;
-	struct completion	compl;
 };
 
 struct mlx5_mr_cache {
drivers/infiniband/hw/mlx5/mr.c +105 -68

257 lines not on this page · raw message

Signature · 1 line
2.24.1
#8 lore raw

PATCH 5/9 RDMA/mlx5: Fix MR cache size and limit debugfs

In reply to Leon Romanovsky

From: Jason Gunthorpe <[email protected]>

The size_write function is supposed to adjust the total_mr's to match the user's request, but lacks locking and safety checking.

total_mrs can only be adjusted by at most available_mrs. mrs already assigned to users cannot be revoked. Ensure that the user provides a target value within the range of available_mrs and within the high/low water mark.

limit_write has confusing and wrong sanity checking, and doesn't have the ability to deallocate on limit reduction.

Since both functions use the same algorithm to adjust the available_mrs, consolidate it into one function and write it correctly. Fix the locking and by holding the spinlock for all accesses to ent->X.

Always fail if the user provides a malformed string.

Fixes e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
---
 drivers/infiniband/hw/mlx5/mr.c | 152 ++++++++++++++++++--------------
 1 file changed, 88 insertions(+), 64 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +88 -64
index c951bb14de56..00d7dc4cd91b 100644
@@ -126,7 +126,7 @@ static void reg_mr_callback(int status, struct mlx5_async_work *context)
 		complete(&ent->compl);
 }
 
-static int add_keys(struct mlx5_cache_ent *ent, int num)
+static int add_keys(struct mlx5_cache_ent *ent, unsigned int num)
 {
 	int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
 	struct mlx5_ib_mr *mr;
@@ -185,30 +185,54 @@ static int add_keys(struct mlx5_cache_ent *ent, int num)
 	return err;
 }
 
-static void remove_keys(struct mlx5_cache_ent *ent, int num)
+static void remove_cache_mr(struct mlx5_cache_ent *ent)
 {
-	struct mlx5_ib_mr *tmp_mr;
 	struct mlx5_ib_mr *mr;
-	LIST_HEAD(del_list);
-	int i;
 
-	for (i = 0; i < num; i++) {
-		spin_lock_irq(&ent->lock);
-		if (list_empty(&ent->head)) {
-			spin_unlock_irq(&ent->lock);
-			break;
-		}
-		mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
-		list_move(&mr->list, &del_list);
-		ent->available_mrs--;
-		ent->total_mrs--;
+	spin_lock_irq(&ent->lock);
+	if (list_empty(&ent->head)) {
 		spin_unlock_irq(&ent->lock);
-		mlx5_core_destroy_mkey(ent->dev->mdev, &mr->mmkey);
+		return;
 	}
+	mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
+	list_del(&mr->list);
+	ent->available_mrs--;
+	ent->total_mrs--;
+	spin_unlock_irq(&ent->lock);
+	mlx5_core_destroy_mkey(ent->dev->mdev, &mr->mmkey);
+	kfree(mr);
+}
 
-	list_for_each_entry_safe(mr, tmp_mr, &del_list, list) {
-		list_del(&mr->list);
-		kfree(mr);
+static int resize_available_mrs(struct mlx5_cache_ent *ent, unsigned int target,
+				bool limit_fill)
+{
+	int err;
+
+	lockdep_assert_held(&ent->lock);
+
+	while (true) {
+		if (limit_fill)
+			target = ent->limit * 2;
+		if (target == ent->available_mrs + ent->pending)
+			return 0;
+		if (target > ent->available_mrs + ent->pending) {
+			u32 todo = target - (ent->available_mrs + ent->pending);
+
+			spin_unlock_irq(&ent->lock);
+			err = add_keys(ent, todo);
+			if (err == -EAGAIN)
+				usleep_range(3000, 5000);
+			spin_lock_irq(&ent->lock);
+			if (err) {
+				if (err != -EAGAIN)
+					return err;
+			} else
+				return 0;
+		} else {
+			spin_unlock_irq(&ent->lock);
+			remove_cache_mr(ent);
+			spin_lock_irq(&ent->lock);
+		}
 	}
 }
 
@@ -216,33 +240,38 @@ static ssize_t size_write(struct file *filp, const char __user *buf,
 			  size_t count, loff_t *pos)
 {
 	struct mlx5_cache_ent *ent = filp->private_data;
-	char lbuf[20] = {0};
-	u32 var;
+	u32 target;
 	int err;
 
-	count = min(count, sizeof(lbuf) - 1);
-	if (copy_from_user(lbuf, buf, count))
-		return -EFAULT;
-
-	if (sscanf(lbuf, "%u", &var) != 1)
-		return -EINVAL;
-
-	if (var < ent->limit)
-		return -EINVAL;
-
-	if (var > ent->total_mrs) {
-		do {
-			err = add_keys(ent, var - ent->total_mrs);
-			if (err && err != -EAGAIN)
-				return err;
+	err = kstrtou32_from_user(buf, count, 0, &target);
+	if (err)
+		return err;
 
-			usleep_range(3000, 5000);
-		} while (err);
-	} else if (var < ent->total_mrs) {
-		remove_keys(ent, ent->total_mrs - var);
+	/*
+	 * Target is the new value of total_mrs the user requests, however we
+	 * cannot free MRs that are in use. Compute the target value for
+	 * available_mrs.
+	 */
+	spin_lock_irq(&ent->lock);
+	if (target < ent->total_mrs - ent->available_mrs) {
+		err = -EINVAL;
+		goto err_unlock;
+	}
+	target = target - (ent->total_mrs - ent->available_mrs);
+	if (target < ent->limit || target > ent->limit*2) {
+		err = -EINVAL;
+		goto err_unlock;
 	}
+	err = resize_available_mrs(ent, target, false);
+	if (err)
+		goto err_unlock;
+	spin_unlock_irq(&ent->lock);
 
 	return count;
+
+err_unlock:
+	spin_unlock_irq(&ent->lock);
+	return err;
 }
 
 static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
@@ -270,28 +299,23 @@ static ssize_t limit_write(struct file *filp, const char __user *buf,
 			   size_t count, loff_t *pos)
 {
 	struct mlx5_cache_ent *ent = filp->private_data;
-	char lbuf[20] = {0};
 	u32 var;
 	int err;
 
-	count = min(count, sizeof(lbuf) - 1);
-	if (copy_from_user(lbuf, buf, count))
-		return -EFAULT;
-
-	if (sscanf(lbuf, "%u", &var) != 1)
-		return -EINVAL;
-
-	if (var > ent->total_mrs)
-		return -EINVAL;
+	err = kstrtou32_from_user(buf, count, 0, &var);
+	if (err)
+		return err;
 
+	/*
+	 * Upon set we immediately fill the cache to high water mark implied by
+	 * the limit.
+	 */
+	spin_lock_irq(&ent->lock);
 	ent->limit = var;
-
-	if (ent->available_mrs < ent->limit) {
-		err = add_keys(ent, 2 * ent->limit - ent->available_mrs);
-		if (err)
-			return err;
-	}
-
+	err = resize_available_mrs(ent, 0, true);
+	spin_unlock_irq(&ent->lock);
+	if (err)
+		return err;
 	return count;
 }
 
@@ -356,20 +380,20 @@ static void __cache_work_func(struct mlx5_cache_ent *ent)
 		}
 	} else if (ent->available_mrs > 2 * ent->limit) {
 		/*
-		 * The remove_keys() logic is performed as garbage collection
-		 * task. Such task is intended to be run when no other active
-		 * processes are running.
+		 * The remove_cache_mr() logic is performed as garbage
+		 * collection task. Such task is intended to be run when no
+		 * other active processes are running.
 		 *
 		 * The need_resched() will return TRUE if there are user tasks
 		 * to be activated in near future.
 		 *
-		 * In such case, we don't execute remove_keys() and postpone
-		 * the garbage collection work to try to run in next cycle,
-		 * in order to free CPU resources to other tasks.
+		 * In such case, we don't execute remove_cache_mr() and postpone
+		 * the garbage collection work to try to run in next cycle, in
+		 * order to free CPU resources to other tasks.
 		 */
 		if (!need_resched() && !someone_adding(cache) &&
 		    time_after(jiffies, cache->last_add + 300 * HZ)) {
-			remove_keys(ent, 1);
+			remove_cache_mr(ent);
 			if (ent->available_mrs > ent->limit)
 				queue_work(cache->wq, &ent->work);
 		} else {

--
2.24.1

#2 lore raw

Re: [PATCH mlx5-next 1/9] RDMA/mlx5: Move asynchronous mkey creation to mlx5_ib

In reply to Leon Romanovsky
Quoted 5 lines · Leon Romanovsky

From: Michael Guralnik <[email protected]>

As mlx5_ib is the only user of the mlx5_core_create_mkey_cb, move the logic inside mlx5_ib and cleanup the code in mlx5_core.

I have a WIP series that is moving the whole mr.c to mlx5_ib.
https://git.kernel.org/pub/scm/linux/kernel/git/saeed/linux.git/log/?h=topic/mr-relocate

Quoted 20 lines
---
 drivers/infiniband/hw/mlx5/mr.c              | 25 ++++++++++++++++
----
 drivers/net/ethernet/mellanox/mlx5/core/mr.c | 22 +++--------------
 include/linux/mlx5/driver.h                  |  6 -----
 3 files changed, 24 insertions(+), 29 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +1 -0
index 6fa0a83c19de..dea14477a676 100644
@@ -79,6 +79,25 @@ static bool use_umr_mtt_update(struct mlx5_ib_mr
 *mr, u64 start, u64 length)
 		length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
 }
 
+static int create_mkey_cb(struct mlx5_core_dev *dev, struct

drivers/infiniband/hw/mlx5/mr.c static bool use_umr_mtt_update(struct mlx5_ib_mr · line 93

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +8 -0
 mlx5_ib_mr *mr,
+			  struct mlx5_async_ctx *async_ctx, u32 *in,
 int inlen,
+			  mlx5_async_cbk_t callback)
+{
+	void *mkc;
+	u8 key;
+
+	spin_lock_irq(&dev->priv.mkey_lock);
+	key = dev->priv.mkey_key++;

you know i don't like mlx5_ib sniffing around mlx5_core->priv ..

this is handled correctly in my series, i can rebase it and make it ready in a couple of days.. let me know if this will be good enough for you.

Quoted 104 lines
drivers/infiniband/hw/mlx5/mr.c +7 -0
+	spin_unlock_irq(&dev->priv.mkey_lock);
+	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
+
+	MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
+	MLX5_SET(mkc, mkc, mkey_7_0, key);
+
+	return mlx5_cmd_exec_cb(async_ctx, in, inlen, mr->out,

sizeof(mr->out),

drivers/infiniband/hw/mlx5/mr.c +3 -0
+				callback, &mr->cb_work);
+}
+
 static void reg_mr_callback(int status, struct mlx5_async_work

*context)
{

drivers/infiniband/hw/mlx5/mr.c +0 -4
 	struct mlx5_ib_mr *mr =
@@ -163,10 +182,8 @@ static int add_keys(struct mlx5_ib_dev *dev, int
 c, int num)
 		spin_lock_irq(&ent->lock);
 		ent->pending++;
 		spin_unlock_irq(&ent->lock);
-		err = mlx5_core_create_mkey_cb(dev->mdev, &mr->mmkey,
-					       &dev->async_ctx, in,
 inlen,
-					       mr->out, sizeof(mr-
 >out),
-					       reg_mr_callback, &mr-
Quoted 1 line

cb_work);

drivers/infiniband/hw/mlx5/mr.c +1 -0
+		err = create_mkey_cb(dev->mdev, mr, &dev->async_ctx,

in, inlen,

drivers/infiniband/hw/mlx5/mr.c +1 -0
+				     reg_mr_callback);
 		if (err) {
 			spin_lock_irq(&ent->lock);
 			ent->pending--;
drivers/net/ethernet/mellanox/mlx5/core/mr.c +3 -10
index 42cc3c7ac5b6..83841e4119d7 100644
@@ -36,12 +36,9 @@
 #include <linux/mlx5/cmd.h>
 #include "mlx5_core.h"
 
-int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
-			     struct mlx5_core_mkey *mkey,
-			     struct mlx5_async_ctx *async_ctx, u32 *in,
-			     int inlen, u32 *out, int outlen,
-			     mlx5_async_cbk_t callback,
-			     struct mlx5_async_work *context)
+int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
+			  struct mlx5_core_mkey *mkey,
+			  u32 *in, int inlen)
 {
 	u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
 	u32 mkey_index;
@@ -57,10 +54,6 @@ int mlx5_core_create_mkey_cb(struct mlx5_core_dev
 *dev,
 	MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
 	MLX5_SET(mkc, mkc, mkey_7_0, key);
 
-	if (callback)
-		return mlx5_cmd_exec_cb(async_ctx, in, inlen, out,
 outlen,
-					callback, context);
-
 	err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
drivers/net/ethernet/mellanox/mlx5/core/mr.c +0 -9
 	if (err)
 		return err;
@@ -75,15 +68,6 @@ int mlx5_core_create_mkey_cb(struct mlx5_core_dev
 *dev,
 		      mkey_index, key, mkey->key);
 	return 0;
 }
-EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
-
-int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
-			  struct mlx5_core_mkey *mkey,
-			  u32 *in, int inlen)
-{
-	return mlx5_core_create_mkey_cb(dev, mkey, NULL, in, inlen,
-					NULL, 0, NULL, NULL);
-}
 EXPORT_SYMBOL(mlx5_core_create_mkey);
 

int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev,

include/linux/mlx5/driver.h +0 -6
index f2b4225ed650..7225e9ca0f25 100644
@@ -947,12 +947,6 @@ struct mlx5_cmd_mailbox
 *mlx5_alloc_cmd_mailbox_chain(struct mlx5_core_dev *dev,
 						      gfp_t flags, int
 npages);
 void mlx5_free_cmd_mailbox_chain(struct mlx5_core_dev *dev,
 				 struct mlx5_cmd_mailbox *head);
-int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
-			     struct mlx5_core_mkey *mkey,
-			     struct mlx5_async_ctx *async_ctx, u32 *in,
-			     int inlen, u32 *out, int outlen,
-			     mlx5_async_cbk_t callback,
-			     struct mlx5_async_work *context);
 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
include/linux/mlx5/driver.h +0 -0
 			  struct mlx5_core_mkey *mkey,
 			  u32 *in, int inlen);
#3 lore raw

Re: [PATCH mlx5-next 1/9] RDMA/mlx5: Move asynchronous mkey creation to mlx5_ib

Quoted 41 lines · Saeed Mahameed
Quoted 5 lines · Leon Romanovsky

From: Michael Guralnik <[email protected]>

As mlx5_ib is the only user of the mlx5_core_create_mkey_cb, move the logic inside mlx5_ib and cleanup the code in mlx5_core.

I have a WIP series that is moving the whole mr.c to mlx5_ib.
https://git.kernel.org/pub/scm/linux/kernel/git/saeed/linux.git/log/?h=topic/mr-relocate

Quoted 17 lines
 drivers/infiniband/hw/mlx5/mr.c              | 25 ++++++++++++++++
 drivers/net/ethernet/mellanox/mlx5/core/mr.c | 22 +++--------------
 include/linux/mlx5/driver.h                  |  6 -----
 3 files changed, 24 insertions(+), 29 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +2 -0
index 6fa0a83c19de..dea14477a676 100644
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -79,6 +79,25 @@ static bool use_umr_mtt_update(struct mlx5_ib_mr
 *mr, u64 start, u64 length)
 		length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
 }
 
+static int create_mkey_cb(struct mlx5_core_dev *dev, struct

drivers/infiniband/hw/mlx5/mr.c static bool use_umr_mtt_update(struct mlx5_ib_mr · line 93

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +8 -0
 mlx5_ib_mr *mr,
+			  struct mlx5_async_ctx *async_ctx, u32 *in,
 int inlen,
+			  mlx5_async_cbk_t callback)
+{
+	void *mkc;
+	u8 key;
+
+	spin_lock_irq(&dev->priv.mkey_lock);
+	key = dev->priv.mkey_key++;

you know i don't like mlx5_ib sniffing around mlx5_core->priv ..

Quoted 3 lines

this is handled correctly in my series, i can rebase it and make it ready in a couple of days.. let me know if this will be good enough for you.

How about Michael just take the two relevant patches into this series

{IB,net}/mlx5: Setup mkey variant before mr create command invocation {IB,net}/mlx5: Assign mkey variant in mlx5_ib only

And this partially moves toward your series. It will be more than a few days to rebase and check all the parts of your series I think.

Jason

#4 lore raw

Re: [PATCH mlx5-next 1/9] RDMA/mlx5: Move asynchronous mkey creation to mlx5_ib

Quoted 51 lines · Jason Gunthorpe
Quoted 45 lines · Saeed Mahameed
Quoted 6 lines · Leon Romanovsky

From: Michael Guralnik <[email protected]>

As mlx5_ib is the only user of the mlx5_core_create_mkey_cb, move the
logic inside mlx5_ib and cleanup the code in mlx5_core.

I have a WIP series that is moving the whole mr.c to mlx5_ib.
https://git.kernel.org/pub/scm/linux/kernel/git/saeed/linux.git/log/?h=topic/mr-relocate

Quoted 23 lines
 drivers/infiniband/hw/mlx5/mr.c              | 25
++++++++++++++++
 drivers/net/ethernet/mellanox/mlx5/core/mr.c | 22 +++-----------
---
 include/linux/mlx5/driver.h                  |  6 -----
 3 files changed, 24 insertions(+), 29 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +1 -0
index 6fa0a83c19de..dea14477a676 100644
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -79,6 +79,25 @@ static bool use_umr_mtt_update(struct

mlx5_ib_mr
*mr, u64 start, u64 length)

drivers/infiniband/hw/mlx5/mr.c +1 -0
 		length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
 }
 
+static int create_mkey_cb(struct mlx5_core_dev *dev, struct

mlx5_ib_mr *mr,

drivers/infiniband/hw/mlx5/mr.c +1 -0
+			  struct mlx5_async_ctx *async_ctx, u32 *in,

int inlen,

drivers/infiniband/hw/mlx5/mr.c static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length) · line 90

Quoted 7 lines
drivers/infiniband/hw/mlx5/mr.c +7 -0
+			  mlx5_async_cbk_t callback)
+{
+	void *mkc;
+	u8 key;
+
+	spin_lock_irq(&dev->priv.mkey_lock);
+	key = dev->priv.mkey_key++;

you know i don't like mlx5_ib sniffing around mlx5_core->priv ..

Quoted 4 lines

this is handled correctly in my series, i can rebase it and make it ready in a couple of days.. let me know if this will be good enough for
you.

Quoted 5 lines

How about Michael just take the two relevant patches into this series

{IB,net}/mlx5: Setup mkey variant before mr create command invocation {IB,net}/mlx5: Assign mkey variant in mlx5_ib only

Also, IB/mlx5: Replace spinlock protected write with atomic var

it is a good one :)

Quoted 2 lines

And this partially moves toward your series. It will be more than a few days to rebase and check all the parts of your series I think.

Okay.

-Saeed.