Skip to contentSkip to conversation

RDMA/rxe: Fix error paths in MR alloc routines

PATCH Applied Bob Pearson 3 participants 7 messages Aug 5, 2022 – Sep 26, 2022

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

v5 cover RDMA/rxe: Fix error paths in MR alloc routines

Currently the rxe driver has incorrect code in error paths for allocating MR objects. The PD and umem are always freed in rxe_mr_cleanup() but in some error paths they are already freed or never set. This patch makes sure that the PD is always set and checks to see if umem is set before freeing it in rxe_mr_cleanup(). umem and maps are freed if an error occurs in an allocate mr call.

Fixes 3902b429ca14 ("Implement invalidate MW operations")

---
v5:
Dropped cleanup code from patch per Li Zhijian.
Split up into two small patches.
v4:
Added set mr->ibmr.pd back to avoid an -ENOMEM error causing rxe_put(mr_pd(mr)); to seg fault in rxe_mr_cleanup() since pd is not getting set in the error path.
v3:
Rebased to apply to current for-next after
Revert "RDMA/rxe: Create duplicate mapping tables for FMRs"
v2:
Moved setting mr->umem until after checks to avoid sending an ERR_PTR to ib_umem_release().
Cleaned up umem and map sets if errors occur in alloc mr calls. Rebased to current for-next.

Bob Pearson (2):
RDMA/rxe: Set pd early in mr alloc routines
RDMA/rxe: Test mr->umem before releaseing umem

 drivers/infiniband/sw/rxe/rxe_loc.h   |  6 +++---
 drivers/infiniband/sw/rxe/rxe_mr.c    | 15 +++++++--------
 drivers/infiniband/sw/rxe/rxe_verbs.c | 10 +++++++---
 3 files changed, 17 insertions(+), 14 deletions(-)

base-commit: 6b822d408b58c3c4f26dae93245c6b7d8b39e0f9

Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/2 RDMA/rxe: Set pd early in mr alloc routines

Move setting of pd in mr objects ahead of any possible errors so that it will always be set in rxe_mr_complete() to avoid seg faults when rxe_put(mr_pd(mr)) is called.

---
 drivers/infiniband/sw/rxe/rxe_loc.h   |  6 +++---
 drivers/infiniband/sw/rxe/rxe_mr.c    | 11 ++++-------
 drivers/infiniband/sw/rxe/rxe_verbs.c | 10 +++++++---
 3 files changed, 14 insertions(+), 13 deletions(-)
drivers/infiniband/sw/rxe/rxe_loc.h +3 -3
index 22f6cc31d1d6..c2a5c8814a48 100644
@@ -64,10 +64,10 @@ int rxe_mmap(struct ib_ucontext *context, struct vm_area_struct *vma);
 
 /* rxe_mr.c */
 u8 rxe_get_next_key(u32 last_key);
-void rxe_mr_init_dma(struct rxe_pd *pd, int access, struct rxe_mr *mr);
-int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
+void rxe_mr_init_dma(int access, struct rxe_mr *mr);
+int rxe_mr_init_user(struct rxe_dev *rxe, u64 start, u64 length, u64 iova,
 		     int access, struct rxe_mr *mr);
-int rxe_mr_init_fast(struct rxe_pd *pd, int max_pages, struct rxe_mr *mr);
+int rxe_mr_init_fast(int max_pages, struct rxe_mr *mr);
 int rxe_mr_copy(struct rxe_mr *mr, u64 iova, void *addr, int length,
 		enum rxe_mr_copy_dir dir);
 int copy_data(struct rxe_pd *pd, int access, struct rxe_dma_info *dma,
drivers/infiniband/sw/rxe/rxe_mr.c +4 -7
index 850b80f5ad8b..af34f198e645 100644
@@ -103,17 +103,16 @@ static int rxe_mr_alloc(struct rxe_mr *mr, int num_buf)
 	return -ENOMEM;
 }
 
-void rxe_mr_init_dma(struct rxe_pd *pd, int access, struct rxe_mr *mr)
+void rxe_mr_init_dma(int access, struct rxe_mr *mr)
 {
 	rxe_mr_init(access, mr);
 
-	mr->ibmr.pd = &pd->ibpd;
 	mr->access = access;
 	mr->state = RXE_MR_STATE_VALID;
 	mr->type = IB_MR_TYPE_DMA;
 }
 
-int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
+int rxe_mr_init_user(struct rxe_dev *rxe, u64 start, u64 length, u64 iova,
 		     int access, struct rxe_mr *mr)
 {
 	struct rxe_map		**map;
@@ -125,7 +124,7 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
 	int err;
 	int i;
 
-	umem = ib_umem_get(pd->ibpd.device, start, length, access);
+	umem = ib_umem_get(&rxe->ib_dev, start, length, access);
 	if (IS_ERR(umem)) {
 		pr_warn("%s: Unable to pin memory region err = %d\n",
 			__func__, (int)PTR_ERR(umem));
@@ -175,7 +174,6 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
 		}
 	}
 
-	mr->ibmr.pd = &pd->ibpd;
 	mr->umem = umem;
 	mr->access = access;
 	mr->length = length;
@@ -197,7 +195,7 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
 	return err;
 }
 
-int rxe_mr_init_fast(struct rxe_pd *pd, int max_pages, struct rxe_mr *mr)
+int rxe_mr_init_fast(int max_pages, struct rxe_mr *mr)
 {
 	int err;
 
@@ -208,7 +206,6 @@ int rxe_mr_init_fast(struct rxe_pd *pd, int max_pages, struct rxe_mr *mr)
 	if (err)
 		goto err1;
 
-	mr->ibmr.pd = &pd->ibpd;
 	mr->max_buf = max_pages;
 	mr->state = RXE_MR_STATE_FREE;
 	mr->type = IB_MR_TYPE_MEM_REG;
drivers/infiniband/sw/rxe/rxe_verbs.c +7 -3
index e264cf69bf55..6c13be14d723 100644
@@ -903,7 +903,9 @@ static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
 		return ERR_PTR(-ENOMEM);
 
 	rxe_get(pd);
-	rxe_mr_init_dma(pd, access, mr);
+	mr->ibmr.pd = ibpd;
+
+	rxe_mr_init_dma(access, mr);
 	rxe_finalize(mr);
 
 	return &mr->ibmr;
@@ -928,8 +930,9 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
 
 
 	rxe_get(pd);
+	mr->ibmr.pd = ibpd;
 
-	err = rxe_mr_init_user(pd, start, length, iova, access, mr);
+	err = rxe_mr_init_user(rxe, start, length, iova, access, mr);
 	if (err)
 		goto err3;
 
@@ -962,8 +965,9 @@ static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
 	}
 
 	rxe_get(pd);
+	mr->ibmr.pd = ibpd;
 
-	err = rxe_mr_init_fast(pd, max_num_sg, mr);
+	err = rxe_mr_init_fast(max_num_sg, mr);
 	if (err)
 		goto err2;
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v5 1/2] RDMA/rxe: Set pd early in mr alloc routines

Quoted 5 lines · Bob Pearson

Move setting of pd in mr objects ahead of any possible errors so that it will always be set in rxe_mr_complete() to avoid seg faults when rxe_put(mr_pd(mr)) is called.

Ignore I missed the for-next

maintainer Applied
#3 lore raw

Re: [PATCH v5 1/2] RDMA/rxe: Set pd early in mr alloc routines

In reply to Bob Pearson
Quoted 2 lines · Bob Pearson

Move setting of pd in mr objects ahead of any possible errors so that it will always be set in rxe_mr_complete() to avoid

This means to say rxe_mr_cleanup()

Quoted 8 lines

seg faults when rxe_put(mr_pd(mr)) is called.

---
 drivers/infiniband/sw/rxe/rxe_loc.h   |  6 +++---
 drivers/infiniband/sw/rxe/rxe_mr.c    | 11 ++++-------
 drivers/infiniband/sw/rxe/rxe_verbs.c | 10 +++++++---
 3 files changed, 14 insertions(+), 13 deletions(-)

Applied to for-next

I dropped the next patch since it is a NOP due to the protection in ib_umem_release()

Thanks,
Jason

#4 lore raw

PATCH v5 2/2 RDMA/rxe: Test mr->umem before releasing umem

In reply to Bob Pearson

In rxe_mr_cleanup() test mr->umem before calling ib_umem_release() since in some error paths it may not be set.

---
 drivers/infiniband/sw/rxe/rxe_mr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/sw/rxe/rxe_mr.c +3 -1
index af34f198e645..f0726e8ee855 100644
@@ -627,7 +627,9 @@ void rxe_mr_cleanup(struct rxe_pool_elem *elem)
 	int i;
 
 	rxe_put(mr_pd(mr));
-	ib_umem_release(mr->umem);
+
+	if (mr->umem)
+		ib_umem_release(mr->umem);
 
 	if (mr->map) {
 		for (i = 0; i < mr->num_map; i++)
Signature · 1 line
2.34.1
#5 lore raw

RE: [PATCH v5 for-next 2/2] RDMA/rxe: Test mr->umem before releasing umem

Quoted 25 lines

-----Original Message-----
From: Bob Pearson <[email protected]>
Sent: Saturday, August 6, 2022 2:32 AM
To: [email protected]; [email protected]; Li, Zhijian/李 智坚 <[email protected]>; [email protected]; [email protected]
Cc: Bob Pearson <[email protected]>
Subject: [PATCH v5 for-next 2/2] RDMA/rxe: Test mr->umem before releasing umem

In rxe_mr_cleanup() test mr->umem before calling ib_umem_release() since in some error paths it may not be set.

---
 drivers/infiniband/sw/rxe/rxe_mr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/sw/rxe/rxe_mr.c +0 -0
index af34f198e645..f0726e8ee855 100644

drivers/infiniband/sw/rxe/rxe_mr.c void rxe_mr_cleanup(struct rxe_pool_elem *elem) · line 632

Quoted 7 lines
drivers/infiniband/sw/rxe/rxe_mr.c +3 -1
@@ -627,7 +627,9 @@ void rxe_mr_cleanup(struct rxe_pool_elem *elem)
 	int i;
 
 	rxe_put(mr_pd(mr));
-	ib_umem_release(mr->umem);
+
+	if (mr->umem)
+		ib_umem_release(mr->umem);

It's safe to pass a NULL to ib_umem_release() where it has a such check.

Thanks
Zhijian

Quoted 5 lines
drivers/infiniband/sw/rxe/rxe_mr.c +0 -1
 	if (mr->map) {
 		for (i = 0; i < mr->num_map; i++)
--

2.34.1

#6 lore raw

Re: [PATCH v5 for-next 2/2] RDMA/rxe: Test mr->umem before releasing umem

Quoted 36 lines · On 8/22/22 00:50, [email protected] wrote:
Quoted 25 lines

-----Original Message-----
From: Bob Pearson <[email protected]>
Sent: Saturday, August 6, 2022 2:32 AM
To: [email protected]; [email protected]; Li, Zhijian/李 智坚 <[email protected]>; [email protected]; [email protected]
Cc: Bob Pearson <[email protected]>
Subject: [PATCH v5 for-next 2/2] RDMA/rxe: Test mr->umem before releasing umem

In rxe_mr_cleanup() test mr->umem before calling ib_umem_release() since in some error paths it may not be set.

---
 drivers/infiniband/sw/rxe/rxe_mr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/infiniband/sw/rxe/rxe_mr.c +0 -0
index af34f198e645..f0726e8ee855 100644

drivers/infiniband/sw/rxe/rxe_mr.c void rxe_mr_cleanup(struct rxe_pool_elem *elem) · line 632

Quoted 7 lines
drivers/infiniband/sw/rxe/rxe_mr.c +3 -1
@@ -627,7 +627,9 @@ void rxe_mr_cleanup(struct rxe_pool_elem *elem)
 	int i;
 
 	rxe_put(mr_pd(mr));
-	ib_umem_release(mr->umem);
+
+	if (mr->umem)
+		ib_umem_release(mr->umem);

It's safe to pass a NULL to ib_umem_release() where it has a such check.

Quoted 9 lines

Thanks
Zhijian

Quoted 5 lines
drivers/infiniband/sw/rxe/rxe_mr.c +0 -1
 	if (mr->map) {
 		for (i = 0; i < mr->num_map; i++)
--

2.34.1

OK we can just drop this patch.

Bob