RDMA/irdma: Check MR contiguity before PBLE alloc
Subsystems (2)
PATCH 1/1 RDMA/irdma: Check MR contiguity before PBLE alloc
The original logic would attempt to allocate PBLEs, then would check whether the region was contiguous _after_, and if so, would free the PBLEs and use a direct mapping instead.
These unnecessary temporary PBLE allocations can technically fail (PBLEs are a finite resource), which can cause an MR registration failure even if the region is contiguous and should have succeeded.
Fix this by checking whether the region is contiguous before allocating any PBLEs, and use the ib_umem_is_contiguous helper while we're at it.
Ring MRs still have their own logic. That will be addressed next.
--- drivers/infiniband/hw/irdma/verbs.c | 50 +++-------------------------- drivers/infiniband/hw/irdma/verbs.h | 1 + 2 files changed, 5 insertions(+), 46 deletions(-)
drivers/infiniband/hw/irdma/verbs.c +4 -46
@@ -2917,42 +2917,6 @@ static bool irdma_check_mem_contiguous(u64 *arr, u32 npages, u32 pg_size)return true; } -/** -* irdma_check_mr_contiguous - check if MR is physically contiguous -* @palloc: pbl allocation struct -* @pg_size: page size -*/ -static bool irdma_check_mr_contiguous (struct irdma_pble_alloc *palloc, -u32 pg_size) -{ -struct irdma_pble_level2 *lvl2 = &palloc->level2; -struct irdma_pble_info *leaf = lvl2->leaf; -u64 *arr =NULL ; -u64 *start_addr =NULL ; -int i; -bool ret; - -if (palloc->level ==PBLE_LEVEL_1 ) { - arr = palloc->level1.addr; - ret =irdma_check_mem_contiguous (arr, palloc->total_cnt, - pg_size); -return ret; - } - - start_addr = leaf->addr; - -for (i =0 ; i < lvl2->leaf_cnt; i++, leaf++) { - arr = leaf->addr; -if ((*start_addr + ((u64 )i * pg_size *PBLE_PER_PAGE )) != *arr) -return false; - ret =irdma_check_mem_contiguous (arr, leaf->cnt, pg_size); -if (!ret) -return false; - } - -return true; -} -/** * irdma_setup_pbles - copy user pg address to pble's * @rf: RDMA PCI function
@@ -3417,22 +3381,13 @@ static int irdma_reg_user_mr_type_mem(struct irdma_mr *iwmr, int access,u8 lvl;int err; - lvl = iwmr->page_cnt !=1 ?PBLE_LEVEL_1 |PBLE_LEVEL_2 :PBLE_LEVEL_0 ; + lvl = iwmr->is_contig ?PBLE_LEVEL_0 :PBLE_LEVEL_1 |PBLE_LEVEL_2 ; iwmr->access = access; err =irdma_setup_pbles (iwdev->rf, iwmr, lvl);if (err)return err; -if (lvl) { - err =irdma_check_mr_contiguous (&iwpbl->pble_alloc, - iwmr->page_size); -if (err) { -irdma_free_pble (iwdev->rf->pble_rsrc, &iwpbl->pble_alloc); - iwpbl->pbl_allocated = false; - } - } -if (create_stag) { stag =irdma_create_stag (iwdev);if (!stag) {
@@ -3487,6 +3442,8 @@ static struct irdma_mr *irdma_alloc_iwmr(struct ib_umem *region, pgsz_bitmap = (reg_type ==IRDMA_MEMREG_TYPE_MEM ) ? iwdev->rf->sc_dev.hw_attrs.page_size_cap :SZ_4K ; + iwmr->is_contig =ib_umem_is_contiguous (region); +/* Call best_pgsz after is_contiguous to restore correct umem->iova. */ iwmr->page_size =ib_umem_find_best_pgsz (region, pgsz_bitmap, virt);if (unlikely (!iwmr->page_size)) {kfree (iwmr);
@@ -3846,6 +3803,7 @@ static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, iwmr->region = region; iwmr->ibmr.iova = virt; iwmr->ibmr.pd = pd; + iwmr->is_contig =ib_umem_is_contiguous (region); iwmr->page_size =ib_umem_find_best_pgsz (region, iwdev->rf->sc_dev.hw_attrs.page_size_cap, virt);
drivers/infiniband/hw/irdma/verbs.h +1 -0
@@ -112,6 +112,7 @@ struct irdma_mr {int access; bool is_hwreg:1; bool dma_mr:1; + bool is_contig:1;u16 type;u32 page_cnt;u64 page_size;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
The original logic would attempt to allocate PBLEs, then would check whether the region was contiguous _after_, and if so, would free the PBLEs and use a direct mapping instead.
These unnecessary temporary PBLE allocations can technically fail (PBLEs are a finite resource), which can cause an MR registration failure even if the region is contiguous and should have succeeded.
Quoted 7 lines
Fix this by checking whether the region is contiguous before allocating any PBLEs, and use the
ib_umem_is_contiguoushelper while we're at it.Ring MRs still have their own logic. That will be addressed next.
Signed-off-by Jacob Moroni <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Leon Romanovsky
AppliedQuoted 10 lines · Jacob Moroni
The original logic would attempt to allocate PBLEs, then would check whether the region was contiguous _after_, and if so, would free the PBLEs and use a direct mapping instead.
These unnecessary temporary PBLE allocations can technically fail (PBLEs are a finite resource), which can cause an MR registration failure even if the region is contiguous and should have succeeded.
[...]
Applied, thanks!
[1/1] RDMA/irdma: Check MR contiguity before PBLE alloc
https://git.kernel.org/rdma/rdma/c/023b4b6ea490b3
Best regards,
Signature · 1 line
Leon Romanovsky <[email protected]>
Quoted 10 lines · Jacob Moroni
drivers/infiniband/hw/irdma/verbs.c +2
-0@@ -3487,6 +3442,8 @@ static struct irdma_mr *irdma_alloc_iwmr(struct ib_umem *region, pgsz_bitmap = (reg_type ==IRDMA_MEMREG_TYPE_MEM ) ? iwdev->rf->sc_dev.hw_attrs.page_size_cap :SZ_4K ; + iwmr->is_contig =ib_umem_is_contiguous (region); +/* Call best_pgsz after is_contiguous to restore correct umem->iova. */ iwmr->page_size =ib_umem_find_best_pgsz (region, pgsz_bitmap, virt);if (unlikely (!iwmr->page_size)) {kfree (iwmr);
drivers/infiniband/hw/irdma/verbs.c static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, · line 3809
Quoted 7 lines
drivers/infiniband/hw/irdma/verbs.c +1
-0@@ -3846,6 +3803,7 @@ static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, iwmr->region = region; iwmr->ibmr.iova = virt; iwmr->ibmr.pd = pd; + iwmr->is_contig =ib_umem_is_contiguous (region); iwmr->page_size =ib_umem_find_best_pgsz (region, iwdev->rf->sc_dev.hw_attrs.page_size_cap, virt);
You should not call ib_umem_find_best_pgsz() after ib_umem_is_contiguous(), it is nonsense to compute a page_size once the contiguous optimization is being used.
Once you decide to use contiguous the start dma address should go straight into the MR structure and just avoid all the pbl stuff.
Jason
Quoted 14 lines · Jason Gunthorpe
Quoted 13 lines · Jacob Moroni
drivers/infiniband/hw/irdma/verbs.c +2
-0@@ -3487,6 +3442,8 @@ static struct irdma_mr *irdma_alloc_iwmr(struct ib_umem *region, pgsz_bitmap = (reg_type ==IRDMA_MEMREG_TYPE_MEM ) ? iwdev->rf->sc_dev.hw_attrs.page_size_cap :SZ_4K ; + iwmr->is_contig =ib_umem_is_contiguous (region); +/* Call best_pgsz after is_contiguous to restore correct umem->iova. */ iwmr->page_size =ib_umem_find_best_pgsz (region, pgsz_bitmap, virt);if (unlikely (!iwmr->page_size)) {kfree (iwmr);@@ -3846,6 +3803,7 @@ static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, iwmr->region = region; iwmr->ibmr.iova = virt; iwmr->ibmr.pd = pd;
Quoted 8 lines
drivers/infiniband/hw/irdma/verbs.c static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, · line 3809
Quoted 4 lines
drivers/infiniband/hw/irdma/verbs.c +1
-0+ iwmr->is_contig =ib_umem_is_contiguous (region); iwmr->page_size =ib_umem_find_best_pgsz (region, iwdev->rf->sc_dev.hw_attrs.page_size_cap, virt);You should not call
ib_umem_find_best_pgsz()afterib_umem_is_contiguous(), it is nonsense to compute apage_sizeonce the contiguous optimization is being used.
ok, I dropped the patch from mu wip/leon-for-next branch for now.
Thanks
Quoted 5 lines
Once you decide to use contiguous the start dma address should go straight into the MR structure and just avoid all the pbl stuff.
Jason
Okay, I will respin to skip the best_pgsz call. I should have it out soon.
Just to run this by you: The HW still requires a page size in contiguous mode and expects the DMA address to be aligned to that size. So, I'll use SZ_4K which should be safe in all cases and then ALIGN_DOWN the value of ib_umem_start_dma_addr().
Thanks,
Jake
Quoted 34 lines · Leon Romanovsky
Quoted 14 lines · Jason Gunthorpe
Quoted 13 lines · Jacob Moroni
drivers/infiniband/hw/irdma/verbs.c +2
-0@@ -3487,6 +3442,8 @@ static struct irdma_mr *irdma_alloc_iwmr(struct ib_umem *region, pgsz_bitmap = (reg_type ==IRDMA_MEMREG_TYPE_MEM ) ? iwdev->rf->sc_dev.hw_attrs.page_size_cap :SZ_4K ; + iwmr->is_contig =ib_umem_is_contiguous (region); +/* Call best_pgsz after is_contiguous to restore correct umem->iova. */ iwmr->page_size =ib_umem_find_best_pgsz (region, pgsz_bitmap, virt);if (unlikely (!iwmr->page_size)) {kfree (iwmr);@@ -3846,6 +3803,7 @@ static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, iwmr->region = region; iwmr->ibmr.iova = virt; iwmr->ibmr.pd = pd;Quoted 8 lines
drivers/infiniband/hw/irdma/verbs.c static int irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len, · line 3809
Quoted 4 lines
drivers/infiniband/hw/irdma/verbs.c +1
-0+ iwmr->is_contig =ib_umem_is_contiguous (region); iwmr->page_size =ib_umem_find_best_pgsz (region, iwdev->rf->sc_dev.hw_attrs.page_size_cap, virt);You should not call
ib_umem_find_best_pgsz()afterib_umem_is_contiguous(), it is nonsense to compute apage_sizeonce the contiguous optimization is being used.ok, I dropped the patch from mu wip/leon-for-next branch for now.
Thanks
Quoted 5 lines
Once you decide to use contiguous the start dma address should go straight into the MR structure and just avoid all the pbl stuff.
Jason
Quoted 6 lines · Jacob Moroni
Okay, I will respin to skip the
best_pgszcall. I should have it out soon.Just to run this by you: The HW still requires a page size in contiguous mode and expects the DMA address to be aligned to that size. So, I'll use
SZ_4Kwhich should be safe in all cases and thenALIGN_DOWNthe value ofib_umem_start_dma_addr().
That should be OK, it has a way to disable any unalignement right?
MR IOVA does not have to be page aligned, but the HW has to provide security.
Jason
Yep, it still uses the lower page offset bits from the incoming VA as the page offset, similar to a PBLE-backed region, with bounds being properly checked as [VA, VA+len).
I ran a test to confirm (nice to have in general) that start/end are accessible while -1/+1 outside fail, regardless of whether the region itself is aligned or not.
Thanks,
Jake