Skip to contentSkip to conversation

RDMA/siw: Honor the sg_offset in siw_map_mr_sg()

PATCH Applied Andrew Stellman 4 participants 4 messages Nov 22 – Nov 26

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

PATCH 1/1 RDMA/siw: Honor the sg_offset in siw_map_mr_sg()

siw_map_mr_sg() builds the first PBL entry from sg_dma_address() and sg_dma_len() of the first scatterlist entry and ignores *sg_off. ib_sg_to_pages() includes *sg_off in mr->iova, which siw stores as mem->va, so siw_pbl_get_buffer() resolves an address in the MR to a location *sg_off bytes before it. rdma_rw_ctx_init() passes its sg_offset to ib_map_mr_sg(), and isert can pass a nonzero offset.

A test module registered a one page buffer on siw with ib_map_mr_sg() and sg_offset 0x200, which gave mr->iova at page offset 0x200 and mr->length 3584. A user space client then wrote 16 bytes with an RDMA WRITE at mr->iova. The bytes landed at page offset 0x000 of the buffer instead of 0x200. With sg_offset 0 they landed at page offset 0x000, as expected.

Start the first PBL entry *sg_off bytes into the first scatterlist entry, and reject an offset past the end of that entry, as ib_sg_to_pages() does. With this change the bytes land at page offset 0x200.

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 and this changelog were drafted with Claude Opus 5.5 and reviewed by me.

Fixes 303ae1cdfdf7 ("rdma/siw: application interface")

---
Tested in QEMU on v7.4-rc3 with KASAN, with an in-kernel test module and a librdmacm client over siw. A reproducer is available on request.

 drivers/infiniband/sw/siw/siw_verbs.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/infiniband/sw/siw/siw_verbs.c +9 -2
index 80ef4cb95fc..c5f67c8e7ce 100644
@@ -1505,8 +1505,15 @@ int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
 			return -EINVAL;
 		}
 		if (i == 0) {
-			pble->addr = sg_dma_address(slp);
-			pble->size = sg_dma_len(slp);
+			unsigned int off = sg_off ? *sg_off : 0;
+
+			/* The MR starts *sg_off bytes into the first entry */
+			if (off > sg_dma_len(slp)) {
+				siw_dbg_mem(mem, "sg_off %u beyond first SGE\n", off);
+				return -EINVAL;
+			}
+			pble->addr = sg_dma_address(slp) + off;
+			pble->size = sg_dma_len(slp) - off;
 			pble->pbl_off = 0;
 			pbl_size = pble->size;
 			pbl->num_buf = 1;
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

siw_map_mr_sg() builds the first PBL entry from sg_dma_address() and sg_dma_len() of the first scatterlist entry and ignores *sg_off. ib_sg_to_pages() includes *sg_off in mr->iova, which siw stores as mem->va, so siw_pbl_get_buffer() resolves an address in the MR to a location *sg_off bytes before it. rdma_rw_ctx_init() passes its sg_offset to ib_map_mr_sg(), and isert can pass a nonzero offset.

A test module registered a one page buffer on siw with ib_map_mr_sg() and sg_offset 0x200, which gave mr->iova at page offset 0x200 and mr->length 3584. A user space client then wrote 16 bytes with an RDMA WRITE at mr->iova. The bytes landed at page offset 0x000 of the buffer instead of 0x200. With sg_offset 0 they landed at page offset 0x000, as expected.

Start the first PBL entry *sg_off bytes into the first scatterlist entry, and reject an offset past the end of that entry, as ib_sg_to_pages() does. With this change the bytes land at page offset 0x200.

Quoted 8 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 and this changelog were drafted with Claude Opus 5.5 and reviewed by me.

Fixes 303ae1cdfdf7 ("rdma/siw: application interface")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Andrew Stellman
Quoted 8 lines · Andrew Stellman

siw_map_mr_sg() builds the first PBL entry from sg_dma_address() and sg_dma_len() of the first scatterlist entry and ignores *sg_off. ib_sg_to_pages() includes *sg_off in mr->iova, which siw stores as mem->va, so siw_pbl_get_buffer() resolves an address in the MR to a location *sg_off bytes before it. rdma_rw_ctx_init() passes its sg_offset to ib_map_mr_sg(), and isert can pass a nonzero offset.

[...]

Applied, thanks!

[1/1] RDMA/siw: Honor the sg_offset in siw_map_mr_sg()

      https://git.kernel.org/rdma/rdma/c/1b295d105a9bca

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Andrew Stellman
Quoted 46 lines · Andrew Stellman

siw_map_mr_sg() builds the first PBL entry from sg_dma_address() and sg_dma_len() of the first scatterlist entry and ignores *sg_off. ib_sg_to_pages() includes *sg_off in mr->iova, which siw stores as mem->va, so siw_pbl_get_buffer() resolves an address in the MR to a location *sg_off bytes before it. rdma_rw_ctx_init() passes its sg_offset to ib_map_mr_sg(), and isert can pass a nonzero offset.

A test module registered a one page buffer on siw with ib_map_mr_sg() and sg_offset 0x200, which gave mr->iova at page offset 0x200 and mr->length 3584. A user space client then wrote 16 bytes with an RDMA WRITE at mr->iova. The bytes landed at page offset 0x000 of the buffer instead of 0x200. With sg_offset 0 they landed at page offset 0x000, as expected.

Start the first PBL entry *sg_off bytes into the first scatterlist entry, and reject an offset past the end of that entry, as ib_sg_to_pages() does. With this change the bytes land at page offset 0x200.

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 and this changelog were drafted with Claude Opus 5.5 and reviewed by me.

Fixes 303ae1cdfdf7 ("rdma/siw: application interface")

---
Tested in QEMU on v7.4-rc3 with KASAN, with an in-kernel test module and a librdmacm client over siw. A reproducer is available on request.

  drivers/infiniband/sw/siw/siw_verbs.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
drivers/infiniband/sw/siw/siw_verbs.c +2 -2
index 80ef4cb95fc..c5f67c8e7ce 100644
@@ -1505,8 +1505,15 @@ int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
  			return -EINVAL;
  		}
  		if (i == 0) {
-			pble->addr = sg_dma_address(slp);
-			pble->size = sg_dma_len(slp);
+			unsigned int off = sg_off ? *sg_off : 0;
+

drivers/infiniband/sw/siw/siw_verbs.c int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle, · line 1519

Quoted 10 lines
drivers/infiniband/sw/siw/siw_verbs.c +7 -0
+			/* The MR starts *sg_off bytes into the first entry */
+			if (off > sg_dma_len(slp)) {
+				siw_dbg_mem(mem, "sg_off %u beyond first SGE\n", off);
+				return -EINVAL;
+			}
+			pble->addr = sg_dma_address(slp) + off;
+			pble->size = sg_dma_len(slp) - off;
  			pble->pbl_off = 0;
  			pbl_size = pble->size;
  			pbl->num_buf = 1;

Excellent catch. Thanks very much, and sorry for the late reply.