RDMA/rxe: Bound MR copy and flush chunks to MR pages
Subsystems (2)
PATCH 1/1 RDMA/rxe: Bound MR copy and flush chunks to MR pages
From: Andrew Stellman <[email protected]>
A fast-reg MR can use 4 KiB pages on a 64 KiB-page kernel, with each MR page in its own page_info entry. rxe_mr_copy_xarray() limits each chunk to the end of the system page, not the end of the MR page, so a copy that crosses an MR-page boundary continues in the same system page instead of moving to the next page_info entry. rxe_mr_flush_pmem_iova() has the same bound.
Limit each chunk to the end of the MR page when MR pages are smaller than PAGE_SIZE. For MR pages of PAGE_SIZE or larger the bound is unchanged.
On arm64 with 64 KiB pages (QEMU TCG, mainline 7cd8495c0f16), a test-only hook built an MR by hand with two 4 KiB pages at offsets 0x1000 and 0x3000 of two different system pages and called rxe_mr_copy() for 32 bytes starting 16 bytes before the MR-page boundary, checking that the last 16 bytes came from or went to the second MR page. Without this patch, both the read and the write check failed. With it, both passed, and they failed again with it reverted. A 32-byte read within one MR page passed on all three builds. The flush path was not run.
The issue was found by Claude Opus 5.5 running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with Claude Opus 5.5, and both were reviewed by me.
---
A reproducer is available on request.
drivers/infiniband/sw/rxe/rxe_mr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/infiniband/sw/rxe/rxe_mr.c +6 -2
@@ -333,12 +333,14 @@ static int rxe_mr_copy_xarray(struct rxe_mr *mr, u64 iova, void *addr,unsigned long index =rxe_mr_iova_to_index (mr, iova);struct rxe_mr_page *info = &mr->page_info[index];unsigned int page_offset =rxe_mr_iova_to_page_offset (mr, iova); + unsigned int chunk = min_t(unsigned int, PAGE_SIZE, +mr_page_size (mr)) - page_offset;if (!info->page)return -EFAULT ; page_offset += info->offset; - bytes = min_t(unsigned int, length, PAGE_SIZE - page_offset); + bytes = min_t(unsigned int, length, chunk); va =kmap_local_page (info->page);if (dir ==RXE_FROM_MR_OBJ )
@@ -519,12 +521,14 @@ static int rxe_mr_flush_pmem_iova(struct rxe_mr *mr, u64 iova, unsigned int lengunsigned long index =rxe_mr_iova_to_index (mr, iova);struct rxe_mr_page *info = &mr->page_info[index];unsigned int page_offset =rxe_mr_iova_to_page_offset (mr, iova); + unsigned int chunk = min_t(unsigned int, PAGE_SIZE, +mr_page_size (mr)) - page_offset;if (!info->page)return -EFAULT ; page_offset += info->offset; - bytes = min_t(unsigned int, length, PAGE_SIZE - page_offset); + bytes = min_t(unsigned int, length, chunk); va =kmap_local_page (info->page);arch_wb_cache_pmem (va + page_offset, bytes);
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 13 lines
From: Andrew Stellman <[email protected]>
A fast-reg MR can use 4 KiB pages on a 64 KiB-page kernel, with each MR page in its own
page_infoentry.rxe_mr_copy_xarray()limits each chunk to the end of the system page, not the end of the MR page, so a copy that crosses an MR-page boundary continues in the same system page instead of moving to the nextpage_infoentry.rxe_mr_flush_pmem_iova()has the same bound.Limit each chunk to the end of the MR page when MR pages are smaller than
PAGE_SIZE. For MR pages ofPAGE_SIZEor larger the bound is unchanged.
Quoted 8 lines
On arm64 with 64 KiB pages (QEMU TCG, mainline
7cd8495c0f16), a test-only hook built an MR by hand with two 4 KiB pages at offsets [ ... ]
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with Claude Opus 5.5, and both were reviewed by me.Fixes 12985e5915a0 ("RDMA/rxe: Fix iova-to-va conversion for MR page sizes != PAGE_SIZE")Signed-off-by Andrew Stellman <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 23 lines · Andrew Stellman
From: Andrew Stellman <[email protected]>
A fast-reg MR can use 4 KiB pages on a 64 KiB-page kernel, with each MR page in its own
page_infoentry.rxe_mr_copy_xarray()limits each chunk to the end of the system page, not the end of the MR page, so a copy that crosses an MR-page boundary continues in the same system page instead of moving to the nextpage_infoentry.rxe_mr_flush_pmem_iova()has the same bound.Limit each chunk to the end of the MR page when MR pages are smaller than
PAGE_SIZE. For MR pages ofPAGE_SIZEor larger the bound is unchanged.On arm64 with 64 KiB pages (QEMU TCG, mainline
7cd8495c0f16), a test-only hook built an MR by hand with two 4 KiB pages at offsets0x1000and0x3000of two different system pages and calledrxe_mr_copy()for 32 bytes starting 16 bytes before the MR-page boundary, checking that the last 16 bytes came from or went to the second MR page. Without this patch, both the read and the write check failed. With it, both passed, and they failed again with it reverted. A 32-byte read within one MR page passed on all three builds. The flush path was not run.
Quoted 5 lines
The issue was found by Claude Opus 5.5 running Quality Playbook, an LLM-driven code review tool:
https://github.com/andrewstellman/quality-playbook
The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with Claude Opus 5.5, and both were reviewed by me.
static int rxe_mr_fill_pages_from_sgt(struct rxe_mr *mr, struct sg_table *sgt) {
struct sg_page_iter sg_iter;
struct page *page;
bool persistent = !!(mr->access & IB_ACCESS_FLUSH_PERSISTENT);
WARN_ON(mr_page_size(mr) != PAGE_SIZE);
__sg_page_iter_start(&sg_iter, sgt->sgl, sgt->orig_nents, 0); if (!__sg_page_iter_next(&sg_iter))
return 0;
rxe_mr_fill_pages_from_sgt() assumes a 1:1 mapping between MR pages and kernel PAGE_SIZE pages returned by sg_page_iter. When mr_page_size(mr) differs from PAGE_SIZE—such as 4 KiB MR pages on a 64 KiB-page kernel—this triggers a WARN_ON() call trace in dmesg, and stepping in PAGE_SIZE increments risks misaligning MR page indices, offsets, or entry counts if the warning is simply removed.
Please also modify rxe_mr_fill_pages_from_sgt() to account for mr_page_size(mr) relative to PAGE_SIZE when populating MR page entries and offsets, and remove the obsolete WARN_ON() check.
Thanks a lot.
Yanjun Zhu
Quoted 45 lines
Fixes 12985e5915a0 ("RDMA/rxe: Fix iova-to-va conversion for MR page sizes != PAGE_SIZE")Signed-off-by Andrew Stellman <[email protected]>---
A reproducer is available on request.drivers/infiniband/sw/rxe/rxe_mr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/infiniband/sw/rxe/rxe_mr.c +6
-2@@ -333,12 +333,14 @@ static int rxe_mr_copy_xarray(struct rxe_mr *mr, u64 iova, void *addr,unsigned long index =rxe_mr_iova_to_index (mr, iova);struct rxe_mr_page *info = &mr->page_info[index];unsigned int page_offset =rxe_mr_iova_to_page_offset (mr, iova); + unsigned int chunk = min_t(unsigned int, PAGE_SIZE, +mr_page_size (mr)) - page_offset;if (!info->page)return -EFAULT ; page_offset += info->offset; - bytes = min_t(unsigned int, length, PAGE_SIZE - page_offset); + bytes = min_t(unsigned int, length, chunk); va =kmap_local_page (info->page);if (dir ==RXE_FROM_MR_OBJ )@@ -519,12 +521,14 @@ static int rxe_mr_flush_pmem_iova(struct rxe_mr *mr, u64 iova, unsigned int lengunsigned long index =rxe_mr_iova_to_index (mr, iova);struct rxe_mr_page *info = &mr->page_info[index];unsigned int page_offset =rxe_mr_iova_to_page_offset (mr, iova); + unsigned int chunk = min_t(unsigned int, PAGE_SIZE, +mr_page_size (mr)) - page_offset;if (!info->page)return -EFAULT ; page_offset += info->offset; - bytes = min_t(unsigned int, length, PAGE_SIZE - page_offset); + bytes = min_t(unsigned int, length, chunk); va =kmap_local_page (info->page);arch_wb_cache_pmem (va + page_offset, bytes);
Signature · 2 lines
Best Regards, Yanjun.Zhu
Quoted 3 lines · Zhu Yanjun
Please also modify
rxe_mr_fill_pages_from_sgt()to account formr_page_size(mr)relative toPAGE_SIZEwhen populating MR page entries and offsets, and remove the obsoleteWARN_ON()check.
Thanks for looking at this. As far as I can see, that case can't happen on current rdma for-rc and mainline. The only caller of rxe_mr_fill_pages_from_sgt() is rxe_mr_init_user(), which calls rxe_mr_init() first, and that sets mr->ibmr.page_size = PAGE_SIZE. Nothing changes it before the fill. The 4 KiB MR pages this patch is about come from fast registration, which goes through rxe_map_mr_sg() and rxe_set_page() instead.
So I'd like to keep this patch to the copy and flush bound.
Quoted 12 lines · Andrew Stellman
Quoted 3 lines · Zhu Yanjun
Please also modify
rxe_mr_fill_pages_from_sgt()to account formr_page_size(mr)relative toPAGE_SIZEwhen populating MR page entries and offsets, and remove the obsoleteWARN_ON()check.Thanks for looking at this. As far as I can see, that case can't happen on current rdma for-rc and mainline. The only caller of
rxe_mr_fill_pages_from_sgt()isrxe_mr_init_user(), which callsrxe_mr_init()first, and that setsmr->ibmr.page_size=PAGE_SIZE. Nothing changes it before the fill. The 4 KiB MR pages this patch is about come from fast registration, which goes throughrxe_map_mr_sg()andrxe_set_page()instead.
Quoted 1 line
So I'd like to keep this patch to the copy and flush bound.
Do you make tests in your local host? Or just use AI to make static analysis?
Yanjun Zhu
Signature · 2 lines
Best Regards, Yanjun.Zhu
Quoted 2 lines · Zhu Yanjun
Do you make tests in your local host? Or just use AI to make static analysis?
My last reply was from reading the source. After your question I ran a check in an arm64 QEMU guest on mainline 4f069ab1ec24 plus a test-only patch (logging and a root-only hook), with 64 KiB pages, KASAN and rxe:
- Five user MRs registered with ibv_reg_mr, including buffers that were 4 KiB aligned but not 64 KiB aligned. A pr_info at the top of rxe_mr_fill_pages_from_sgt() logged mr_page_size=65536 and PAGE_SIZE=65536 for all five, and the WARN_ON didn't fire.
- A root-only test hook called rxe_mr_init_fast() and then rxe_map_mr_sg() directly, with the MR page size set to 4 KiB. rxe_mr_fill_pages_from_sgt() wasn't called.
The v1 patch was tested on mainline 7cd8495c0f16 in an arm64 QEMU guest with 64 KiB pages and KASAN, using a root-only test hook that calls rxe_mr_copy() directly. A read and a write across a 4 KiB MR-page boundary failed without the patch, passed with it, and failed again with it reverted. The flush path was built but not run.
I can send the test-only probe patch and the user program (user-mr.c) if they'd help.
Quoted 17 lines · Andrew Stellman
Quoted 2 lines · Zhu Yanjun
Do you make tests in your local host? Or just use AI to make static analysis?
My last reply was from reading the source. After your question I ran a check in an arm64 QEMU guest on mainline
4f069ab1ec24plus a test-only patch (logging and a root-only hook), with 64 KiB pages, KASAN and rxe:- Five user MRs registered with
ibv_reg_mr, including buffers that were 4 KiB aligned but not 64 KiB aligned. Apr_infoat the top ofrxe_mr_fill_pages_from_sgt()logged mr_page_size=65536 and PAGE_SIZE=65536 for all five, and theWARN_ONdidn't fire.- A root-only test hook called
rxe_mr_init_fast()and thenrxe_map_mr_sg()directly, with the MR page size set to 4 KiB.rxe_mr_fill_pages_from_sgt()wasn't called.
Quoted 5 lines
The v1 patch was tested on mainline
7cd8495c0f16in an arm64 QEMU guest with 64 KiB pages and KASAN, using a root-only test hook that callsrxe_mr_copy()directly. A read and a write across a 4 KiB MR-page boundary failed without the patch, passed with it, and failed again with it reverted. The flush path was built but not run.
Below is the implementation of mr_page_size:
static inline unsigned int mr_page_size(struct rxe_mr *mr) {
return mr ? mr->ibmr.page_size : PAGE_SIZE;
}
In the function below, mr->ibmr.page_size is initialized:
void rxe_mr_init(int access, struct rxe_mr *mr) {
u32 key = mr->elem.index << 8 | rxe_get_next_key(-1);
/* set ibmr->l/rkey and also copy into private l/rkey
* - for user MRs these will always be the same
* - for cases where caller 'owns' the key portion
* - they may be different until REG_MR WQE is executed. */
mr->lkey = mr->ibmr.lkey = key;
mr->rkey = mr->ibmr.rkey = key;
mr->access = access;
mr->ibmr.page_size = PAGE_SIZE;
mr->page_mask = PAGE_MASK;
mr->page_shift = PAGE_SHIFT;
mr->state = RXE_MR_STATE_INVALID;
}
Can you find anywhere else where mr->ibmr.page_size is assigned a different value?
Thanks a lot.
Yanjun Zhu
Quoted 3 lines
I can send the test-only probe patch and the user program (
user-mr.c) if they'd help.
Signature · 2 lines
Best Regards, Yanjun.Zhu
Quoted 2 lines · Zhu Yanjun
Can you find anywhere else where
mr->ibmr.page_sizeis assigned a different value?
Yes. ib_map_mr_sg() in drivers/infiniband/core/verbs.c sets the ib_mr's page_size to its caller's page_size argument before it calls the driver's map_mr_sg. For rxe that's mr->ibmr.page_size, and rxe_map_mr_sg() reads it back with mr_page_size(). rxe_map_mr_sg() accepts a page size smaller than PAGE_SIZE when PAGE_SIZE is a multiple of it.
One in-tree caller that passes 4 KiB is nvme_rdma_map_sg_fr() in drivers/nvme/host/rdma.c, which calls ib_map_mr_sg(..., SZ_4K). On a 64 KiB-page kernel that's a fast-reg MR with 4 KiB MR pages.
This is from reading current mainline. I haven't run NVMe over RDMA on rxe; in my tests the 4 KiB MR page size was set by the test-only hook.
Quoted 9 lines · Andrew Stellman
Quoted 2 lines · Zhu Yanjun
Can you find anywhere else where
mr->ibmr.page_sizeis assigned a different value?Yes.
ib_map_mr_sg()indrivers/infiniband/core/verbs.csets the ib_mr'spage_sizeto its caller'spage_sizeargument before it calls the driver'smap_mr_sg. For rxe that'smr->ibmr.page_size, andrxe_map_mr_sg()reads it back withmr_page_size().rxe_map_mr_sg()accepts a page size smaller thanPAGE_SIZEwhenPAGE_SIZEis a multiple of it.
In rxe, mr_page_size() always returns PAGE_SIZE and never changes.
As a result, these commits only add implementation complexity without
bringing any practical benefit to rxe. If you intend to support different page_size
values in rxe, patches implementing that feature would be very welcome.
From my perspective, however, the current patch does not yet actually deliver that capability.
Thanks,
Yanjun Zhu
Quoted 8 lines
One in-tree caller that passes 4 KiB is
nvme_rdma_map_sg_fr()indrivers/nvme/host/rdma.c, which callsib_map_mr_sg(..., SZ_4K). On a 64 KiB-page kernel that's a fast-reg MR with 4 KiB MR pages.This is from reading current mainline. I haven't run NVMe over RDMA on rxe; in my tests the 4 KiB MR page size was set by the test-only hook.
Signature · 2 lines
Best Regards, Yanjun.Zhu