Skip to contentSkip to conversation

MR cache enhancement

PATCH In review Leon Romanovsky 2 participants 11 messages Dec 30, 2021 – Jan 7, 2022 1 message not in the archive

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

cover MR cache enhancement

From: Leon Romanovsky <[email protected]>

Changelog:
v1:
* Based on DM revert https://lore.kernel.org/all/[email protected]
v0: https://lore.kernel.org/all/[email protected]

---------------------------------------------------------
Hi,

This series from Aharon refactors mlx5 MR cache management logic to speedup deregistration significantly.

Thanks

Aharon Landau (7):
RDMA/mlx5: Merge similar flows of allocating MR from the cache RDMA/mlx5: Replace cache list with Xarray
RDMA/mlx5: Store in the cache mkeys instead of mrs RDMA/mlx5: Reorder calls to pcie_relaxed_ordering_enabled() RDMA/mlx5: Change the cache structure to an RB-tree RDMA/mlx5: Delay the deregistration of a non-cache mkey RDMA/mlx5: Rename the mkey cache variables and functions

 drivers/infiniband/hw/mlx5/main.c    |    4 +-
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   76 +-
 drivers/infiniband/hw/mlx5/mr.c      | 1021 +++++++++++++++++---------
 drivers/infiniband/hw/mlx5/odp.c     |   72 +-
 include/linux/mlx5/driver.h          |    7 +-
 5 files changed, 741 insertions(+), 439 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/7 RDMA/mlx5: Merge similar flows of allocating MR from the cache

From: Aharon Landau <[email protected]>

When allocating an MR from the cache, the driver calls to get_cache_mr(), and in case of failure, retries with create_cache_mr(). This is the flow of mlx5_mr_cache_alloc(), so use it instead.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  3 +-
 drivers/infiniband/hw/mlx5/mr.c      | 51 +++++-----------------------
 drivers/infiniband/hw/mlx5/odp.c     | 11 ++++--
 3 files changed, 19 insertions(+), 46 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +2 -1
index 48eda21a358f..9c3cf6f26ad1 100644
@@ -1344,7 +1344,8 @@ 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,
-				       unsigned int entry, int access_flags);
+				       struct mlx5_cache_ent *ent,
+				       int access_flags);
 
 int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
 			    struct ib_mr_status *mr_status);
drivers/infiniband/hw/mlx5/mr.c +9 -42

82 lines not on this page · raw message

drivers/infiniband/hw/mlx5/odp.c +8 -3
index 91eb615b89ee..0972afc3e952 100644
@@ -407,6 +407,7 @@ static void mlx5_ib_page_fault_resume(struct mlx5_ib_dev *dev,
 static struct mlx5_ib_mr *implicit_get_child_mr(struct mlx5_ib_mr *imr,
 						unsigned long idx)
 {
+	struct mlx5_ib_dev *dev = mr_to_mdev(imr);
 	struct ib_umem_odp *odp;
 	struct mlx5_ib_mr *mr;
 	struct mlx5_ib_mr *ret;
@@ -418,13 +419,14 @@ static struct mlx5_ib_mr *implicit_get_child_mr(struct mlx5_ib_mr *imr,
 	if (IS_ERR(odp))
 		return ERR_CAST(odp);
 
-	mr = mlx5_mr_cache_alloc(
-		mr_to_mdev(imr), MLX5_IMR_MTT_CACHE_ENTRY, imr->access_flags);
+	mr = mlx5_mr_cache_alloc(dev, &dev->cache.ent[MLX5_IMR_MTT_CACHE_ENTRY],
+				 imr->access_flags);
 	if (IS_ERR(mr)) {
 		ib_umem_odp_release(odp);
 		return mr;
 	}
 
+	mr->access_flags = imr->access_flags;
 	mr->ibmr.pd = imr->ibmr.pd;
 	mr->ibmr.device = &mr_to_mdev(imr)->ib_dev;
 	mr->umem = &odp->umem;
@@ -493,12 +495,15 @@ struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
 	if (IS_ERR(umem_odp))
 		return ERR_CAST(umem_odp);
 
-	imr = mlx5_mr_cache_alloc(dev, MLX5_IMR_KSM_CACHE_ENTRY, access_flags);
+	imr = mlx5_mr_cache_alloc(dev,
+				  &dev->cache.ent[MLX5_IMR_KSM_CACHE_ENTRY],
+				  access_flags);
 	if (IS_ERR(imr)) {
 		ib_umem_odp_release(umem_odp);
 		return imr;
 	}
 
+	imr->access_flags = access_flags;
 	imr->ibmr.pd = &pd->ibpd;
 	imr->ibmr.iova = 0;
 	imr->umem = &umem_odp->umem;
Signature · 1 line
2.33.1
maintainer
#2 lore raw

Re: [PATCH rdma-next v1 1/7] RDMA/mlx5: Merge similar flows of allocating MR from the cache

drivers/infiniband/hw/mlx5/mr.c static void cache_work_func(struct work_struct *work) · line 584

Quoted 5 lines · Leon Romanovsky
drivers/infiniband/hw/mlx5/mr.c +4 -0
 	if (list_empty(&ent->head)) {
+		if (ent->limit) {
+			queue_adjust_cache_locked(ent);
+			ent->miss++;
+		}

I don't this this addition of ent->limit makes any sense.

The only case it triggers is the two ODP callers and they should definately increment miss.

queue_adjust_cache_locked() is an effective NOP if limit is zero as available_mrs should be zero too.

Jason

#3 lore raw

PATCH 2/7 RDMA/mlx5: Replace cache list with Xarray

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

The Xarray allows us to store the cached mkeys in memory efficient way and internal xa_lock is used to protect the indexes. It helps us to get rid of ent->lock as it is not required anymore.

Entries are reserved in the Xarray using xa_cmpxchg before calling to the upcoming callbacks to avoid allocations in interrupt context. The xa_cmpxchg can sleep when using GFP_KERNEL, so we call it in a loop to ensure one reserved entry for each process trying to reserve.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  13 +-
 drivers/infiniband/hw/mlx5/mr.c      | 253 ++++++++++++++++-----------
 2 files changed, 153 insertions(+), 113 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +3 -10
index 9c3cf6f26ad1..213894053bfe 100644
@@ -755,11 +755,9 @@ struct umr_common {
 };
 
 struct mlx5_cache_ent {
-	struct list_head	head;
-	/* sync access to the cahce entry
-	 */
-	spinlock_t		lock;
-
+	struct xarray		mkeys;
+	unsigned long		stored;
+	unsigned long		reserved;
 
 	char                    name[4];
 	u32                     order;
@@ -771,18 +769,13 @@ struct mlx5_cache_ent {
 	u8 fill_to_high_water:1;
 
 	/*
-	 * - 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;
drivers/infiniband/hw/mlx5/mr.c +150 -103

497 lines not on this page · raw message

Signature · 1 line
2.33.1
maintainer
#4 lore raw

Re: [PATCH rdma-next v1 2/7] RDMA/mlx5: Replace cache list with Xarray

Quoted 14 lines · Leon Romanovsky
drivers/infiniband/hw/mlx5/mr.c +2 -2
index 2cba55bb7825..8936b504ff99 100644
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -147,14 +147,17 @@ static void create_mkey_callback(int status, struct mlx5_async_work *context)
 	struct mlx5_cache_ent *ent = mr->cache_ent;
 	struct mlx5_ib_dev *dev = ent->dev;
 	unsigned long flags;
+	void *old;
 
 	if (status) {
 		mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
 		kfree(mr);
-		spin_lock_irqsave(&ent->lock, flags);
-		ent->pending--;

drivers/infiniband/hw/mlx5/mr.c static void create_mkey_callback(int status, struct mlx5_async_work *context) · line 163

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +5 -1
+		xa_lock_irqsave(&ent->mkeys, flags);
+		ent->reserved--;
+		old = __xa_erase(&ent->mkeys, ent->reserved);
+		WARN_ON(old != NULL);
 		WRITE_ONCE(dev->fill_delay, 1);
-		spin_unlock_irqrestore(&ent->lock, flags);
+		xa_unlock_irqrestore(&ent->mkeys, flags);
 		mod_timer(&dev->delay_timer, jiffies + HZ);
 		return;
 	}

Since push_reserve_mkey was put in a function these stack manipulation should be too, especially since it is open coded again in the err_undo_reserve label below

Quoted 26 lines
drivers/infiniband/hw/mlx5/mr.c +7 -5
@@ -166,14 +169,14 @@ static void create_mkey_callback(int status, struct mlx5_async_work *context)
 
 	WRITE_ONCE(dev->cache.last_add, jiffies);
 
-	spin_lock_irqsave(&ent->lock, flags);
-	list_add_tail(&mr->list, &ent->head);
-	ent->available_mrs++;
+	xa_lock_irqsave(&ent->mkeys, flags);
+	old = __xa_store(&ent->mkeys, ent->stored, mr, GFP_ATOMIC);
+	WARN_ON(old != NULL);
+	ent->stored++;
 	ent->total_mrs++;
 	/* If we are doing fill_to_high_water then keep going. */
 	queue_adjust_cache_locked(ent);
-	ent->pending--;
-	spin_unlock_irqrestore(&ent->lock, flags);
+	xa_unlock_irqrestore(&ent->mkeys, flags);
 }
 
 static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc)
@@ -196,12 +199,48 @@ static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc)
 	return mr;
 }
 
+static int _push_reserve_mkey(struct mlx5_cache_ent *ent)
+{

drivers/infiniband/hw/mlx5/mr.c static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc) · line 213

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +10 -0
+	unsigned long to_reserve;
+	void *old;
+
+	while (true) {
+		to_reserve = ent->reserved;
+		old = __xa_cmpxchg(&ent->mkeys, to_reserve, NULL, XA_ZERO_ENTRY,
+				  GFP_KERNEL);
+
+		if (xa_is_err(old))
+			return xa_err(old);

Comment that this below is needed because xa_cmpxchg could drop the lock and thus ent->reserved can change.

drivers/infiniband/hw/mlx5/mr.c static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc) · line 215

Quoted 1 line
drivers/infiniband/hw/mlx5/mr.c +1 -0
+		if (to_reserve != ent->reserved || old != NULL) {

WARN_ON(old != NULL) ? This can't happen right?

drivers/infiniband/hw/mlx5/mr.c static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc) · line 236

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +10 -0
+static int push_reserve_mkey(struct mlx5_cache_ent *ent)
+{
+	int ret;
+
+	xa_lock_irq(&ent->mkeys);
+	ret = _push_reserve_mkey(ent);
+	xa_unlock_irq(&ent->mkeys);
+
+	return ret;
+}

Why not just use one function and a simple goto unwind?

drivers/infiniband/hw/mlx5/mr.c static int add_keys(struct mlx5_cache_ent *ent, unsigned int num) · line 265

Quoted 7 lines
drivers/infiniband/hw/mlx5/mr.c +6 -0
+
+		xa_lock_irq(&ent->mkeys);
+		err = _push_reserve_mkey(ent);
+		if (err)
+			goto err_unlock;
+		if ((ent->reserved - ent->stored) > MAX_PENDING_REG_MR) {
 			err = -EAGAIN;

Maybe this check should be inside push_reserve_mkey before it does any xarray operation?

Quoted 6 lines
drivers/infiniband/hw/mlx5/mr.c +1 -0
@@ -286,40 +335,42 @@ static struct mlx5_ib_mr *create_cache_mr(struct mlx5_cache_ent *ent)
 static void remove_cache_mr_locked(struct mlx5_cache_ent *ent)
 {
 	struct mlx5_ib_mr *mr;
+	void *old;
 

drivers/infiniband/hw/mlx5/mr.c static struct mlx5_ib_mr *create_cache_mr(struct mlx5_cache_ent *ent) · line 349

Quoted 10 lines
drivers/infiniband/hw/mlx5/mr.c +8 -0
+	if (!ent->stored)
 		return;
+	ent->stored--;
+	mr = __xa_store(&ent->mkeys, ent->stored, XA_ZERO_ENTRY, GFP_KERNEL);
+	WARN_ON(mr == NULL || xa_is_err(mr));
+	ent->reserved--;
+	old = __xa_erase(&ent->mkeys, ent->reserved);
+	WARN_ON(old != NULL);
 	ent->total_mrs--;
+	xa_unlock_irq(&ent->mkeys);

This should avoid the double XA operations if reserved == stored.

drivers/infiniband/hw/mlx5/mr.c struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, · line 646

Quoted 6 lines
drivers/infiniband/hw/mlx5/mr.c +6 -0
+		mr = __xa_store(&ent->mkeys, ent->stored, XA_ZERO_ENTRY,
+				GFP_KERNEL);
+		WARN_ON(mr == NULL || xa_is_err(mr));
+		ent->reserved--;
+		old = __xa_erase(&ent->mkeys, ent->reserved);
+		WARN_ON(old != NULL);

'pop' should really be its own function too

drivers/infiniband/hw/mlx5/mr.c struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, · line 662

Quoted 12 lines
drivers/infiniband/hw/mlx5/mr.c +4 -3
@@ -601,41 +655,35 @@ struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev,
 static void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
 {
 	struct mlx5_cache_ent *ent = mr->cache_ent;
+	void *old;
 
-	spin_lock_irq(&ent->lock);
-	list_add_tail(&mr->list, &ent->head);
-	ent->available_mrs++;
+	xa_lock_irq(&ent->mkeys);
+	old = __xa_store(&ent->mkeys, ent->stored, mr, 0);
+	WARN_ON(old != NULL);

This allocates memory, errors now have to be handled by falling back to dereg_mr.

Shouldn't this adjust reserved too?

Jason

#5 lore raw

PATCH 3/7 RDMA/mlx5: Store in the cache mkeys instead of mrs

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

Currently, the driver stores the entire mlx5_ib_mr struct in the cache, although the only use of the cached MR is the mkey. Store only the mkey in the cache.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  25 ++---
 drivers/infiniband/hw/mlx5/mr.c      | 161 +++++++++++++--------------
 2 files changed, 83 insertions(+), 103 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +8 -17
index 213894053bfe..cfc77d43c7a8 100644
@@ -641,6 +641,7 @@ struct mlx5_ib_mkey {
 	unsigned int ndescs;
 	struct wait_queue_head wait;
 	refcount_t usecount;
+	struct mlx5_cache_ent *cache_ent;
 };
 
 #define MLX5_IB_MTT_PRESENT (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE)
@@ -663,20 +664,9 @@ struct mlx5_ib_mr {
 	struct ib_mr ibmr;
 	struct mlx5_ib_mkey mmkey;
 
-	/* User MR data */
-	struct mlx5_cache_ent *cache_ent;
-	/* Everything after cache_ent is zero'd when MR allocated */
 	struct ib_umem *umem;
 
 	union {
-		/* Used only while the MR is in the cache */
-		struct {
-			u32 out[MLX5_ST_SZ_DW(create_mkey_out)];
-			struct mlx5_async_work cb_work;
-			/* Cache list element */
-			struct list_head list;
-		};
-
 		/* Used only by kernel MRs (umem == NULL) */
 		struct {
 			void *descs;
@@ -716,12 +706,6 @@ struct mlx5_ib_mr {
 	};
 };
 
-/* Zero the fields in the mr that are variant depending on usage */
-static inline void mlx5_clear_mr(struct mlx5_ib_mr *mr)
-{
-	memset_after(mr, 0, cache_ent);
-}
-
 static inline bool is_odp_mr(struct mlx5_ib_mr *mr)
 {
 	return IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) && mr->umem &&
@@ -785,6 +769,13 @@ struct mlx5_cache_ent {
 	struct delayed_work	dwork;
 };
 
+struct mlx5_async_create_mkey {
+	u32 out[MLX5_ST_SZ_DW(create_mkey_out)];
+	struct mlx5_async_work cb_work;
+	struct mlx5_cache_ent *ent;
+	u32 mkey;
+};
+
 struct mlx5_mr_cache {
 	struct workqueue_struct *wq;
 	struct mlx5_cache_ent	ent[MAX_MR_CACHE_ENTRIES];
drivers/infiniband/hw/mlx5/mr.c +75 -86

361 lines not on this page · raw message

Signature · 1 line
2.33.1
#6 lore raw

PATCH 4/7 RDMA/mlx5: Reorder calls to pcie_relaxed_ordering_enabled()

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

The mkc is the key for the mkey cache, hence, created in each attempt to get a cache mkey, while pcie_relaxed_ordering_enabled() is called during the setting of the mkc, but used only for cases where IB_ACCESS_RELAXED_ORDERING is set.

pcie_relaxed_ordering_enabled() is an expensive call (26 us). Reorder the code so the driver will call it only when it is needed.

---
 drivers/infiniband/hw/mlx5/mr.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +7 -7
index 204c37a37421..182bdd537e43 100644
@@ -68,7 +68,6 @@ static void set_mkc_access_pd_addr_fields(void *mkc, int acc, u64 start_addr,
 					  struct ib_pd *pd)
 {
 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
-	bool ro_pci_enabled = pcie_relaxed_ordering_enabled(dev->mdev->pdev);
 
 	MLX5_SET(mkc, mkc, a, !!(acc & IB_ACCESS_REMOTE_ATOMIC));
 	MLX5_SET(mkc, mkc, rw, !!(acc & IB_ACCESS_REMOTE_WRITE));
@@ -76,12 +75,13 @@ static void set_mkc_access_pd_addr_fields(void *mkc, int acc, u64 start_addr,
 	MLX5_SET(mkc, mkc, lw, !!(acc & IB_ACCESS_LOCAL_WRITE));
 	MLX5_SET(mkc, mkc, lr, 1);
 
-	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_write))
-		MLX5_SET(mkc, mkc, relaxed_ordering_write,
-			 (acc & IB_ACCESS_RELAXED_ORDERING) && ro_pci_enabled);
-	if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_read))
-		MLX5_SET(mkc, mkc, relaxed_ordering_read,
-			 (acc & IB_ACCESS_RELAXED_ORDERING) && ro_pci_enabled);
+	if ((acc & IB_ACCESS_RELAXED_ORDERING) &&
+	    pcie_relaxed_ordering_enabled(dev->mdev->pdev)) {
+		if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_write))
+			MLX5_SET(mkc, mkc, relaxed_ordering_write, 1);
+		if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_read))
+			MLX5_SET(mkc, mkc, relaxed_ordering_read, 1);
+	}
 
 	MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
 	MLX5_SET(mkc, mkc, qpn, 0xffffff);
Signature · 1 line
2.33.1
#7 lore raw

PATCH 5/7 RDMA/mlx5: Change the cache structure to an RB-tree

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

Currently, the cache structure is a linear array held within mlx5_ib_dev. Therefore, limits to the number of entries.

The existing entries are dedicated to mkeys of size 2^x and with no access_flags and later in the series, we allow caching mkeys with different attributes.

In this patch, we change the cache structure to an RB-tree of Xarray of mkeys. The tree key is the mkc used to create the stored mkeys.

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |  22 +-
 drivers/infiniband/hw/mlx5/mr.c      | 486 ++++++++++++++++++---------
 drivers/infiniband/hw/mlx5/odp.c     |  71 ++--
 include/linux/mlx5/driver.h          |   5 +-
 4 files changed, 381 insertions(+), 203 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +12 -10
index cfc77d43c7a8..ce1f48cc8370 100644
@@ -744,10 +744,7 @@ struct mlx5_cache_ent {
 	unsigned long		reserved;
 
 	char                    name[4];
-	u32                     order;
-	u32			xlt;
-	u32			access_mode;
-	u32			page;
+	unsigned int		ndescs;
 
 	u8 disabled:1;
 	u8 fill_to_high_water:1;
@@ -767,6 +764,9 @@ struct mlx5_cache_ent {
 	struct mlx5_ib_dev     *dev;
 	struct work_struct	work;
 	struct delayed_work	dwork;
+
+	struct rb_node		node;
+	void			*mkc;
 };
 
 struct mlx5_async_create_mkey {
@@ -778,7 +778,8 @@ struct mlx5_async_create_mkey {
 
 struct mlx5_mr_cache {
 	struct workqueue_struct *wq;
-	struct mlx5_cache_ent	ent[MAX_MR_CACHE_ENTRIES];
+	struct rb_root		cache_root;
+	struct mutex		cache_lock;
 	struct dentry		*root;
 	unsigned long		last_add;
 };
@@ -1327,9 +1328,12 @@ 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,
-				       struct mlx5_cache_ent *ent,
-				       int access_flags);
+int mlx5_acc_flags_to_ent_flags(struct mlx5_ib_dev *dev, int access_flags);
+void mlx5_set_cache_mkc(struct mlx5_ib_dev *dev, void *mkc, int access_flags,
+			unsigned int access_mode, unsigned int page_shift);
+struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int *in,
+				       int inlen, unsigned int ndescs,
+				       unsigned int access_mode);
 
 int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
 			    struct ib_mr_status *mr_status);
@@ -1353,7 +1357,6 @@ int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev, struct mlx5_ib_pf_eq *eq);
 void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *ibdev);
 int __init mlx5_ib_odp_init(void);
 void mlx5_ib_odp_cleanup(void);
-void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent);
 void mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,
 			   struct mlx5_ib_mr *mr, int flags);
 
@@ -1372,7 +1375,6 @@ static inline int mlx5r_odp_create_eq(struct mlx5_ib_dev *dev,
 static inline void mlx5_ib_odp_cleanup_one(struct mlx5_ib_dev *ibdev) {}
 static inline int mlx5_ib_odp_init(void) { return 0; }
 static inline void mlx5_ib_odp_cleanup(void)				    {}
-static inline void mlx5_odp_init_mr_cache_entry(struct mlx5_cache_ent *ent) {}
 static inline void mlx5_odp_populate_xlt(void *xlt, size_t idx, size_t nentries,
 					 struct mlx5_ib_mr *mr, int flags) {}
 
drivers/infiniband/hw/mlx5/mr.c +328 -158

656 lines not on this page · raw message

drivers/infiniband/hw/mlx5/odp.c +40 -31

127 lines not on this page · raw message

include/linux/mlx5/driver.h +1 -4
index a623ec635947..c33f71134136 100644
@@ -699,10 +699,7 @@ enum {
 };
 
 enum {
-	MR_CACHE_LAST_STD_ENTRY = 20,
-	MLX5_IMR_MTT_CACHE_ENTRY,
-	MLX5_IMR_KSM_CACHE_ENTRY,
-	MAX_MR_CACHE_ENTRIES
+	MAX_MR_CACHE_ENTRIES = 21,
 };
 
 struct mlx5_profile {
Signature · 1 line
2.33.1
#8 lore raw

PATCH 6/7 RDMA/mlx5: Delay the deregistration of a non-cache mkey

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

When restarting an application with many non-cached mkeys, all the mkeys will be destroyed and then recreated.

This process takes a long time (about 20 seconds for deregistration and 28 seconds for registration of 100,000 MRs).

To shorten the restart runtime, insert the mkeys temporarily into the cache and schedule a delayed work to destroy them later. If there is no fitting entry to these mkeys, create a temporary entry that fits them.

If 30 seconds have passed and no user reclaimed the temporarily cached mkeys, the scheduled work will destroy the mkeys and the temporary entries.

When restarting an application, the mkeys will still be in the cache when trying to reg them again, therefore, the registration will be faster (4 seconds for deregistration and 5 seconds or registration of 100,000 MRs).

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   3 +
 drivers/infiniband/hw/mlx5/mr.c      | 131 ++++++++++++++++++++++++++-
 2 files changed, 132 insertions(+), 2 deletions(-)
drivers/infiniband/hw/mlx5/mlx5_ib.h +3 -0
index ce1f48cc8370..8ebe1edce190 100644
@@ -639,6 +639,7 @@ struct mlx5_ib_mkey {
 	u32 key;
 	enum mlx5_mkey_type type;
 	unsigned int ndescs;
+	unsigned int access_mode;
 	struct wait_queue_head wait;
 	refcount_t usecount;
 	struct mlx5_cache_ent *cache_ent;
@@ -746,6 +747,7 @@ struct mlx5_cache_ent {
 	char                    name[4];
 	unsigned int		ndescs;
 
+	u8 is_tmp:1;
 	u8 disabled:1;
 	u8 fill_to_high_water:1;
 
@@ -782,6 +784,7 @@ struct mlx5_mr_cache {
 	struct mutex		cache_lock;
 	struct dentry		*root;
 	unsigned long		last_add;
+	struct delayed_work	remove_ent_dwork;
 };
 
 struct mlx5_ib_port_resources {
drivers/infiniband/hw/mlx5/mr.c +129 -2

181 lines not on this page · raw message

Signature · 1 line
2.33.1
#9 lore raw

PATCH 7/7 RDMA/mlx5: Rename the mkey cache variables and functions

In reply to Leon Romanovsky

From: Aharon Landau <[email protected]>

After replacing the MR cache with an Mkey cache, rename the variables and functions to fit the new meaning.

---
 drivers/infiniband/hw/mlx5/main.c    |  4 +-
 drivers/infiniband/hw/mlx5/mlx5_ib.h | 20 +++---
 drivers/infiniband/hw/mlx5/mr.c      | 97 ++++++++++++++--------------
 drivers/infiniband/hw/mlx5/odp.c     |  8 +--
 include/linux/mlx5/driver.h          |  4 +-
 5 files changed, 67 insertions(+), 66 deletions(-)
drivers/infiniband/hw/mlx5/main.c +2 -2
index 5ec8bd2f0b2f..74f32b563109 100644
@@ -4034,7 +4034,7 @@ static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
 {
 	int err;
 
-	err = mlx5_mr_cache_cleanup(dev);
+	err = mlx5_mkey_cache_cleanup(dev);
 	if (err)
 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
 
@@ -4131,7 +4131,7 @@ static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
 	dev->umrc.pd = pd;
 
 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
-	ret = mlx5_mr_cache_init(dev);
+	ret = mlx5_mkey_cache_init(dev);
 	if (ret) {
 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
 		goto error_4;
drivers/infiniband/hw/mlx5/mlx5_ib.h +10 -10
index 8ebe1edce190..93065492dcb8 100644
@@ -752,12 +752,12 @@ struct mlx5_cache_ent {
 	u8 fill_to_high_water:1;
 
 	/*
-	 * - total_mrs is available_mrs plus all in use MRs that could be
+	 * - total_mkeys is stored mkeys plus all in use mkeys that could be
 	 *   returned to the cache.
-	 * - limit is the low water mark for available_mrs, 2* limit is the
+	 * - limit is the low water mark for available_mkeys, 2 * limit is the
 	 *   upper water mark.
 	 */
-	u32 total_mrs;
+	u32 total_mkeys;
 	u32 limit;
 
 	/* Statistics */
@@ -778,7 +778,7 @@ struct mlx5_async_create_mkey {
 	u32 mkey;
 };
 
-struct mlx5_mr_cache {
+struct mlx5_mkey_cache {
 	struct workqueue_struct *wq;
 	struct rb_root		cache_root;
 	struct mutex		cache_lock;
@@ -1081,7 +1081,7 @@ struct mlx5_ib_dev {
 	struct mlx5_ib_resources	devr;
 
 	atomic_t			mkey_var;
-	struct mlx5_mr_cache		cache;
+	struct mlx5_mkey_cache		cache;
 	struct timer_list		delay_timer;
 	/* Prevents soft lock on massive reg MRs */
 	struct mutex			slow_path_mutex;
@@ -1328,15 +1328,15 @@ void mlx5_ib_populate_pas(struct ib_umem *umem, size_t page_size, __be64 *pas,
 			  u64 access_flags);
 void mlx5_ib_copy_pas(u64 *old, u64 *new, int step, int num);
 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);
+int mlx5_mkey_cache_init(struct mlx5_ib_dev *dev);
+int mlx5_mkey_cache_cleanup(struct mlx5_ib_dev *dev);
 
 int mlx5_acc_flags_to_ent_flags(struct mlx5_ib_dev *dev, int access_flags);
 void mlx5_set_cache_mkc(struct mlx5_ib_dev *dev, void *mkc, int access_flags,
 			unsigned int access_mode, unsigned int page_shift);
-struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int *in,
-				       int inlen, unsigned int ndescs,
-				       unsigned int access_mode);
+struct mlx5_ib_mr *mlx5_mkey_cache_alloc(struct mlx5_ib_dev *dev, int *in,
+					 int inlen, unsigned int ndescs,
+					 unsigned int access_mode);
 
 int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
 			    struct ib_mr_status *mr_status);
drivers/infiniband/hw/mlx5/mr.c +49 -48

320 lines not on this page · raw message

drivers/infiniband/hw/mlx5/odp.c +4 -4
index 89aaf783fe25..ddb5f77905d5 100644
@@ -434,8 +434,8 @@ static struct mlx5_ib_mr *implicit_get_child_mr(struct mlx5_ib_mr *imr,
 			   mlx5_acc_flags_to_ent_flags(dev, imr->access_flags),
 			   MLX5_MKC_ACCESS_MODE_MTT, PAGE_SHIFT);
 
-	mr = mlx5_mr_cache_alloc(dev, in, inlen, MLX5_IMR_MTT_ENTRIES,
-				 MLX5_MKC_ACCESS_MODE_MTT);
+	mr = mlx5_mkey_cache_alloc(dev, in, inlen, MLX5_IMR_MTT_ENTRIES,
+				   MLX5_MKC_ACCESS_MODE_MTT);
 	if (IS_ERR(mr)) {
 		ib_umem_odp_release(odp);
 		kfree(in);
@@ -528,8 +528,8 @@ struct mlx5_ib_mr *mlx5_ib_alloc_implicit_mr(struct mlx5_ib_pd *pd,
 			   mlx5_acc_flags_to_ent_flags(dev, access_flags),
 			   MLX5_MKC_ACCESS_MODE_KSM, PAGE_SHIFT);
 
-	imr = mlx5_mr_cache_alloc(dev, in, inlen, mlx5_imr_ksm_entries,
-				  MLX5_MKC_ACCESS_MODE_KSM);
+	imr = mlx5_mkey_cache_alloc(dev, in, inlen, mlx5_imr_ksm_entries,
+				    MLX5_MKC_ACCESS_MODE_KSM);
 	if (IS_ERR(imr)) {
 		ib_umem_odp_release(umem_odp);
 		kfree(in);
include/linux/mlx5/driver.h +2 -2
index c33f71134136..51b30c11116e 100644
@@ -699,7 +699,7 @@ enum {
 };
 
 enum {
-	MAX_MR_CACHE_ENTRIES = 21,
+	MAX_MKEY_CACHE_ENTRIES = 21,
 };
 
 struct mlx5_profile {
@@ -708,7 +708,7 @@ struct mlx5_profile {
 	struct {
 		int	size;
 		int	limit;
-	} mr_cache[MAX_MR_CACHE_ENTRIES];
+	} mr_cache[MAX_MKEY_CACHE_ENTRIES];
 };
 
 struct mlx5_hca_cap {
Signature · 1 line
2.33.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw

Re: [PATCH rdma-next v1 6/7] RDMA/mlx5: Delay the deregistration of a non-cache mkey

Quoted 28 lines · Hillf Danton

On Thu, 30 Dec 2021 13:23:23 +0200

Quoted 27 lines

From: Aharon Landau <[email protected]>

When restarting an application with many non-cached mkeys, all the mkeys will be destroyed and then recreated.

This process takes a long time (about 20 seconds for deregistration and 28 seconds for registration of 100,000 MRs).

To shorten the restart runtime, insert the mkeys temporarily into the cache and schedule a delayed work to destroy them later. If there is no fitting entry to these mkeys, create a temporary entry that fits them.

If 30 seconds have passed and no user reclaimed the temporarily cached mkeys, the scheduled work will destroy the mkeys and the temporary entries.

When restarting an application, the mkeys will still be in the cache when trying to reg them again, therefore, the registration will be faster (4 seconds for deregistration and 5 seconds or registration of 100,000 MRs).

---
 drivers/infiniband/hw/mlx5/mlx5_ib.h |   3 +
 drivers/infiniband/hw/mlx5/mr.c      | 131 ++++++++++++++++++++++++++-
 2 files changed, 132 insertions(+), 2 deletions(-)

<...>

Quoted 13 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+	if (!ent->is_tmp)
+		mr->mmkey.cache_ent = ent;
+	else {
+		ent->total_mrs--;
+		cancel_delayed_work(&ent->dev->cache.remove_ent_dwork);
+		queue_delayed_work(ent->dev->cache.wq,
+				   &ent->dev->cache.remove_ent_dwork,
+				   msecs_to_jiffies(30 * 1000));
+	}

Nit: collapse cancel and queue into mod_delayed_work().

Quoted 1 line

}

<...>

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	INIT_WORK(&ent->work, cache_work_func);
+	INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);

More important IMHO is to cut work in a seperate patch given that dwork can be queued with zero delay and both work callbacks are simple wrappers of __cache_work_func().

Thanks, I'll collect more feedback and resubmit.

Quoted 2 lines

Hillf