[Devel] [PATCH hci-8.1 v2] fs/fuse kio: build the rdma sgl from folios

Liu Kui kui.liu at virtuozzo.com
Wed Nov 18 18:57:02 MSK 2026


pcs_sgl_buf_init_from_msg() assumes there is only one 4k page per
bio_vec segment, it counts the number of bio_vec segments and uses it
for allocating the 4k page array and the scatterlist. However since
commit ed62e3c51fa2 ("fs/fuse kio: teach pcs_umem about hugepage-backed
mappings") a bio_vec segment over a hugepage-backed buffer can cover
more than one 4k page, so the page array and the scatterlist
allocated based on the number of segments are too small to hold all 4k
pages, resulting in sg_next() returning NULL and sg_set_page()
oopsing:

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  RIP: pcs_sgl_buf_init_from_msg+0x1ee/0x300 [fuse_kio_pcs]
   pcs_rdma_rw_init_from_msg
   rio_submit_rdma_read
   rio_handle_rx_immediate
   rio_handle_rx
   pcs_rdma_xmit
   rpc_queue_work

Fix it by switching to folios, so a scatterlist entry can still hold
an entire message segment regardless of whether it is hugepage backed.
The scatterlist now holds the folio references, so the page array and
its cleanup callback can be removed. Size the scatterlist by the segment
count, with extra entries when a segment exceeds the device DMA segment
limit. An inline array of two entries covers a hugepage-backed buffer
without an allocation.

Keep track of the DMA mapped entry count returned by ib_dma_map_sg()
separately, so that the unmap and the folio puts are done correctly.

A kvec over vmalloc memory is not physically contiguous and
iov_iter_extract_pages() only returns its first page, so reject kvec
iterators and let the MR fallback handle them. The only one that gets
here is the fiemap reply buffer.

https://virtuozzo.atlassian.net/browse/VSTOR-155302

Fixes: ed62e3c51fa2 ("fs/fuse kio: teach pcs_umem about hugepage-backed mappings")
Feature: vStorage
Signed-off-by: Liu Kui <kui.liu at virtuozzo.com>
---
Fix for the NULL deref in pcs_sgl_buf_init_from_msg() on a kernel-direct
client with hugepage-backed MR pools.

https://virtuozzo.atlassian.net/browse/VSTOR-155302
---
Changes in v2:
- reject kvec iterators and leave them to the MR fallback, a kvec over
  vmalloc memory is not physically contiguous (fiemap reply buffer)
- name the commit that introduced the hugepage-backed bio_vec segments,
  add the Fixes and Feature tags
- drop the page-size clause from the max_seg check, the count is then an
  upper bound however the length is split over the segments
- Link to v1: http://lore.virtuozzo.com/20261118-b4-vstor-155302-sgl-folio-v1-1-30d40962e230@virtuozzo.com
---
 fs/fuse/kio/pcs/pcs_rdma_rw.c | 209 +++++++++++++++++++++++-------------------
 fs/fuse/kio/pcs/pcs_rdma_rw.h |  12 +--
 2 files changed, 119 insertions(+), 102 deletions(-)

diff --git a/fs/fuse/kio/pcs/pcs_rdma_rw.c b/fs/fuse/kio/pcs/pcs_rdma_rw.c
index eaf456002584..dc85bf7ff379 100644
--- a/fs/fuse/kio/pcs/pcs_rdma_rw.c
+++ b/fs/fuse/kio/pcs/pcs_rdma_rw.c
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/highmem.h>
+#include <linux/mm.h>
 
 #include "pcs_rdma_rw.h"
 
@@ -33,160 +34,175 @@ static unsigned int dma_dir_to_it_dir(enum dma_data_direction dir)
 	return DMA_TO_DEVICE ? WRITE : READ;
 }
 
+/* Drop the reference each of the first cnt entries holds on its folio. */
+static void sgl_put_folios(struct scatterlist *sgl, size_t cnt)
+{
+	struct scatterlist *sg;
+	int i;
+
+	for_each_sg(sgl, sg, cnt, i)
+		folio_put(page_folio(sg_page(sg)));
+}
+
+/* Build a scatterlist over the message range [offset, end_offset), one entry per folio. */
 int pcs_sgl_buf_init_from_msg(struct pcs_sgl_buf *sbuf, unsigned int dir, struct pcs_msg *msg,
-			      size_t offset, size_t end_offset, gfp_t gfp, bool allow_gaps)
+			      size_t offset, size_t end_offset, gfp_t gfp, bool allow_gaps,
+			      unsigned int max_seg)
 {
 	struct iov_iter it;
-	size_t msg_offset;
+	size_t msg_offset, foff, len, off;
+	struct page *page, **pp = &page;
 	struct scatterlist *sg;
-	int ret, i;
+	struct folio *folio;
+	ssize_t got;
+	int ret, cnt;
 
 	if (offset >= end_offset || end_offset > msg->size)
 		return -EINVAL;
 
-	sbuf->pg_cnt = 0;
+	/* count the entries, one per segment unless a segment is longer than max_seg */
 	sbuf->sg_cnt = 0;
-	sbuf->page_clean = put_page;
-
-	iov_iter_kvec(&it, dir, NULL, 0, 0);
 	msg_offset = offset;
 	while (msg_offset < end_offset) {
-		size_t len;
+		msg->get_iter(msg, msg_offset, &it, dir);
 
-		if (!iov_iter_count(&it))
-			msg->get_iter(msg, msg_offset, &it, dir);
+		/* a kvec may be over vmalloc memory, leave it to the MR fallback */
+		if (iov_iter_is_kvec(&it))
+			return -EINVAL;
 
-		len = iov_iter_single_seg_count(&it);
+		len = iov_iter_count(&it);
 		if (len > end_offset - msg_offset)
 			len = end_offset - msg_offset;
 
-		iov_iter_advance(&it, len);
+		/* may overcount here, which is rare and harmless */
+		sbuf->sg_cnt += it.nr_segs;
 		msg_offset += len;
-		sbuf->sg_cnt++;
-	}
 
-	sbuf->pages = RE_NULL(kzalloc(sizeof(struct page*) * sbuf->sg_cnt,
-				      gfp));
-	if (!sbuf->pages)
-		return -ENOMEM;
+		/* This can only happen to a hugepage over umem. Though the default dma max
+		 * segment size is 64kB if the driver doesn't configure it, most modern IB
+		 * drivers use a much larger segment size, so this is a very rare case.
+		 * The sum is an upper bound however the length is split over the segments,
+		 * we may overcount, which again is harmless.
+		 */
+		if (unlikely(len > max_seg))
+			sbuf->sg_cnt += DIV_ROUND_UP(len, max_seg);
+	}
 
-	sbuf->sgl = RE_NULL(kzalloc(sizeof(struct scatterlist) * sbuf->sg_cnt,
-				    gfp));
-	if (!sbuf->sgl) {
-		ret = -ENOMEM;
-		goto fail_pg;
+	if (sbuf->sg_cnt <= ARRAY_SIZE(sbuf->inline_sgl)) {
+		sbuf->sgl = sbuf->inline_sgl;
+	} else {
+		sbuf->sgl = RE_NULL(kzalloc(sizeof(struct scatterlist) * sbuf->sg_cnt, gfp));
+		if (!sbuf->sgl)
+			return -ENOMEM;
 	}
 	sg_init_table(sbuf->sgl, sbuf->sg_cnt);
 
+	/* fill the entries, one folio reference each, and count the 4k pages */
 	iov_iter_kvec(&it, dir, NULL, 0, 0);
+	sbuf->pg_cnt = 0;
 	msg_offset = offset;
 	sg = sbuf->sgl;
+	cnt = 0;
 	while (msg_offset < end_offset) {
-		size_t off, len;
+		if (WARN_ON_ONCE(!sg)) {
+			ret = -EINVAL;
+			goto fail;
+		}
 
 		if (!iov_iter_count(&it))
 			msg->get_iter(msg, msg_offset, &it, dir);
 
-		len = iov_iter_get_pages2(&it, sbuf->pages + sbuf->pg_cnt,
-					 PAGE_SIZE, 1, &off);
-		if (len <= 0) {
-			ret = -EINVAL;
-			goto fail_sgl;
-		}
-
+		/* a segment never spans folios */
+		len = iov_iter_single_seg_count(&it);
 		if (len > end_offset - msg_offset)
 			len = end_offset - msg_offset;
 
-		sg_set_page(sg, sbuf->pages[sbuf->pg_cnt++], len, off);
+		/* find the page under the current position, no reference is taken */
+		got = iov_iter_extract_pages(&it, &pp, 1, 1, 0, &off);
+		if (got != 1) {
+			ret = got < 0 ? got : -EINVAL;
+			goto fail;
+		}
+
+		folio = page_folio(page);
+		foff = folio_page_idx(folio, page) * PAGE_SIZE + off;
+		len = min_t(size_t, len, max_seg);
+		iov_iter_advance(&it, len - 1);
 
 		if (!allow_gaps &&
-		    ((msg_offset != offset && !IS_ALIGNED(off, PAGE_SIZE)) ||
-		     ((msg_offset + len != end_offset) && !IS_ALIGNED(off + len, PAGE_SIZE)))) {
-			iov_iter_revert(&it, len);
+		    ((msg_offset != offset && !IS_ALIGNED(foff, PAGE_SIZE)) ||
+		     ((msg_offset + len != end_offset) && !IS_ALIGNED(foff + len, PAGE_SIZE)))) {
 			ret = -EINVAL;
-			goto fail_sgl;
+			goto fail;
 		}
 
-		sg = sg_next(sg);
+		folio_get(folio);
+		sg_set_folio(sg, folio, len, foff);
+		sbuf->pg_cnt += DIV_ROUND_UP((foff & (PAGE_SIZE - 1)) + len, PAGE_SIZE);
+		cnt++;
 		msg_offset += len;
+		sg = sg_next(sg);
 	}
 
+	/* the count is an upper bound, close the list at the last used entry */
+	sg_mark_end(&sbuf->sgl[cnt - 1]);
+	sbuf->sg_cnt = cnt;
+
 	return 0;
 
-fail_sgl:
-	kfree(sbuf->sgl);
-fail_pg:
-	for (i = 0; i < sbuf->pg_cnt; i++)
-		sbuf->page_clean(sbuf->pages[i]);
-	kfree(sbuf->pages);
+fail:
+	sgl_put_folios(sbuf->sgl, cnt);
+	if (sbuf->sgl != sbuf->inline_sgl)
+		kfree(sbuf->sgl);
 
 	return ret;
 }
 
-static void pcs_sgl_buf_free_page(struct page *page)
-{
-	__free_page(page);
-}
-
+/* Build a scatterlist of size bytes over freshly allocated 4k folios, one per entry. */
 int pcs_sgl_buf_init(struct pcs_sgl_buf *sbuf, size_t size, gfp_t gfp)
 {
 	struct scatterlist *sg;
-	int ret, i;
+	int i;
 
 	if (size == 0)
 		return -EINVAL;
 
-	sbuf->pg_cnt = 0;
 	sbuf->sg_cnt = PAGE_ALIGN(size) >> PAGE_SHIFT;
-	sbuf->page_clean = pcs_sgl_buf_free_page;
+	sbuf->pg_cnt = sbuf->sg_cnt;
 
-	sbuf->pages = RE_NULL(kzalloc(sizeof(struct page*) * sbuf->sg_cnt,
-				      gfp));
-	if (!sbuf->pages)
-		return -ENOMEM;
-
-	sbuf->sgl = RE_NULL(kzalloc(sizeof(struct scatterlist) * sbuf->sg_cnt,
-				    gfp));
-	if (!sbuf->sgl) {
-		ret = -ENOMEM;
-		goto fail_pg;
+	if (sbuf->sg_cnt <= ARRAY_SIZE(sbuf->inline_sgl)) {
+		sbuf->sgl = sbuf->inline_sgl;
+	} else {
+		sbuf->sgl = RE_NULL(kzalloc(sizeof(struct scatterlist) * sbuf->sg_cnt, gfp));
+		if (!sbuf->sgl)
+			return -ENOMEM;
 	}
 	sg_init_table(sbuf->sgl, sbuf->sg_cnt);
 
 	for_each_sg(sbuf->sgl, sg, sbuf->sg_cnt, i) {
 		size_t sg_len = min_t(size_t, size, PAGE_SIZE);
-		sbuf->pages[sbuf->pg_cnt] = RE_NULL(alloc_page(gfp));
-		if (!sbuf->pages[sbuf->pg_cnt]) {
-			ret = -ENOMEM;
-			goto fail_sgl;
+		struct folio *folio = RE_NULL(folio_alloc(gfp, 0));
+
+		if (!folio) {
+			sgl_put_folios(sbuf->sgl, i);
+			if (sbuf->sgl != sbuf->inline_sgl)
+				kfree(sbuf->sgl);
+			return -ENOMEM;
 		}
 		BUG_ON(!sg_len);
-		sg_set_page(sg, sbuf->pages[sbuf->pg_cnt], sg_len, 0);
+		sg_set_folio(sg, folio, sg_len, 0);
 		size -= sg_len;
-		sbuf->pg_cnt++;
 	}
 
 	return 0;
-
-fail_sgl:
-	kfree(sbuf->sgl);
-fail_pg:
-	for (i = 0; i < sbuf->pg_cnt; i++)
-		sbuf->page_clean(sbuf->pages[i]);
-	kfree(sbuf->pages);
-
-	return ret;
 }
 
+/* Drop the folio references and free the list. */
 void pcs_sgl_buf_destroy(struct pcs_sgl_buf *sbuf)
 {
-	int i;
-
-	kfree(sbuf->sgl);
-
-	for (i = 0; i < sbuf->pg_cnt; i++)
-		sbuf->page_clean(sbuf->pages[i]);
-	kfree(sbuf->pages);
+	sgl_put_folios(sbuf->sgl, sbuf->sg_cnt);
+	if (sbuf->sgl != sbuf->inline_sgl)
+		kfree(sbuf->sgl);
 }
 
 int pcs_ib_mr_pool_init(struct pcs_ib_mr_pool *pool, struct ib_pd *pd,
@@ -291,7 +307,7 @@ int pcs_rdma_rw_init_from_msg(struct pcs_rdma_rw *rw, struct ib_device *dev,
 	rw->dir = dir;
 
 	ret = pcs_sgl_buf_init_from_msg(&rw->sbuf, dma_dir_to_it_dir(dir), msg, offset, end_offset,
-					gfp, true);
+					gfp, true, ib_dma_max_seg_size(dev));
 	if (ret)
 		return ret;
 
@@ -301,11 +317,11 @@ int pcs_rdma_rw_init_from_msg(struct pcs_rdma_rw *rw, struct ib_device *dev,
 		ret = ret < 0 ? ret : -EIO;
 		goto fail_sgl;
 	}
-	rw->sbuf.sg_cnt = ret;
+	rw->sbuf.dma_cnt = ret;
 
-	rw->nr_wrs = DIV_ROUND_UP(rw->sbuf.sg_cnt, sge_per_wr);
+	rw->nr_wrs = DIV_ROUND_UP(rw->sbuf.dma_cnt, sge_per_wr);
 
-	rw->sges = RE_NULL(kzalloc(sizeof(struct ib_sge) * rw->sbuf.sg_cnt, gfp));
+	rw->sges = RE_NULL(kzalloc(sizeof(struct ib_sge) * rw->sbuf.dma_cnt, gfp));
 	if (!rw->sges) {
 		ret = -ENOMEM;
 		goto fail_dma;
@@ -319,7 +335,7 @@ int pcs_rdma_rw_init_from_msg(struct pcs_rdma_rw *rw, struct ib_device *dev,
 
 	sg = rw->sbuf.sgl;
 	sge = rw->sges;
-	sge_cnt = rw->sbuf.sg_cnt;
+	sge_cnt = rw->sbuf.dma_cnt;
 	for (i = 0; i < rw->nr_wrs; i++) {
 		struct ib_rdma_wr *wr = rw->wrs + i;
 		int num_sge = min_t(size_t, sge_cnt, sge_per_wr);
@@ -388,14 +404,15 @@ static int pcs_rdma_mr_init_common(struct pcs_rdma_mr *mr, struct ib_device *dev
 	mr->pd = pd;
 	mr->dir = dir;
 	mr->size = size;
+	/* the MR page list is sized in 4k pages, an entry may span many */
 	mr->ib_mr_pool = ib_mr_pool && ib_mr_pool->mr_type == IB_MR_TYPE_MEM_REG &&
-		ib_mr_pool->max_num_sg >= mr->sbuf.sg_cnt ? ib_mr_pool : NULL;
+		ib_mr_pool->max_num_sg >= mr->sbuf.pg_cnt ? ib_mr_pool : NULL;
 
 	ret = RE_INV(ib_dma_map_sg(dev, mr->sbuf.sgl, mr->sbuf.sg_cnt,
 				   dir));
 	if (ret <= 0)
 		return ret < 0 ? ret : -EIO;
-	mr->sbuf.sg_cnt = ret;
+	mr->sbuf.dma_cnt = ret;
 
 	if (mr->ib_mr_pool) {
 		mr->mr = RE_NULL(pcs_ib_mr_pool_get(mr->ib_mr_pool));
@@ -405,16 +422,16 @@ static int pcs_rdma_mr_init_common(struct pcs_rdma_mr *mr, struct ib_device *dev
 		}
 	} else { /* fallback */
 		mr->mr = RE_PTR_INV(ib_alloc_mr(pd, IB_MR_TYPE_MEM_REG,
-						mr->sbuf.sg_cnt));
+						mr->sbuf.pg_cnt));
 		if (IS_ERR(mr->mr)) {
 			ret = PTR_ERR(mr->mr);
 			goto fail_dma;
 		}
 	}
 
-	ret = RE_INV(ib_map_mr_sg(mr->mr, mr->sbuf.sgl, mr->sbuf.sg_cnt,
+	ret = RE_INV(ib_map_mr_sg(mr->mr, mr->sbuf.sgl, mr->sbuf.dma_cnt,
 				  NULL, PAGE_SIZE));
-	if (ret != mr->sbuf.sg_cnt) {
+	if (ret != mr->sbuf.dma_cnt) {
 		ret = ret < 0 ? ret : -EIO;
 		goto fail_mr;
 	}
@@ -457,7 +474,7 @@ int pcs_rdma_mr_init_from_msg(struct pcs_rdma_mr *mr, struct ib_device *dev,
 	int ret;
 
 	ret = pcs_sgl_buf_init_from_msg(&mr->sbuf, dma_dir_to_it_dir(dir), msg, offset, end_offset,
-					gfp, false);
+					gfp, false, ib_dma_max_seg_size(dev));
 	if (ret)
 		return ret;
 
@@ -527,7 +544,7 @@ void pcs_rdma_mr_sync_for_cpu(struct pcs_rdma_mr *mr, size_t size)
 	struct scatterlist *sg;
 	unsigned int i;
 
-	for_each_sg(mr->sbuf.sgl, sg, mr->sbuf.sg_cnt, i) {
+	for_each_sg(mr->sbuf.sgl, sg, mr->sbuf.dma_cnt, i) {
 		size_t sg_len = min_t(size_t, size, sg_dma_len(sg));
 		ib_dma_sync_single_for_cpu(mr->dev,
 					   sg_dma_address(sg),
@@ -544,7 +561,7 @@ void pcs_rdma_mr_sync_for_device(struct pcs_rdma_mr *mr, size_t size)
 	struct scatterlist *sg;
 	unsigned int i;
 
-	for_each_sg(mr->sbuf.sgl, sg, mr->sbuf.sg_cnt, i) {
+	for_each_sg(mr->sbuf.sgl, sg, mr->sbuf.dma_cnt, i) {
 		size_t sg_len = min_t(size_t, size, sg_dma_len(sg));
 		ib_dma_sync_single_for_device(mr->dev,
 					      sg_dma_address(sg),
diff --git a/fs/fuse/kio/pcs/pcs_rdma_rw.h b/fs/fuse/kio/pcs/pcs_rdma_rw.h
index c8529ed16f13..6b53e93a67ee 100644
--- a/fs/fuse/kio/pcs/pcs_rdma_rw.h
+++ b/fs/fuse/kio/pcs/pcs_rdma_rw.h
@@ -47,16 +47,16 @@ extern u32 rdmaio_io_failing;
 
 struct pcs_sgl_buf
 {
-	struct page **pages;
-	size_t pg_cnt;
-	void (*page_clean)(struct page *page);
-
-	struct scatterlist *sgl;
+	struct scatterlist *sgl; /* each entry holds a reference on its folio */
 	size_t sg_cnt;
+	size_t dma_cnt; /* entries after dma mapping, may be fewer than sg_cnt */
+	size_t pg_cnt; /* 4k pages covered, sizes the MR page list */
+	struct scatterlist inline_sgl[2]; /* enough for a hugepage-backed range */
 };
 
 int pcs_sgl_buf_init_from_msg(struct pcs_sgl_buf *sbuf, unsigned int dir, struct pcs_msg *msg,
-			      size_t offset, size_t end_offset, gfp_t gfp, bool allow_gaps);
+			      size_t offset, size_t end_offset, gfp_t gfp, bool allow_gaps,
+			      unsigned int max_seg);
 int pcs_sgl_buf_init(struct pcs_sgl_buf *sbuf, size_t size, gfp_t gfp);
 void pcs_sgl_buf_destroy(struct pcs_sgl_buf *sbuf);
 

---
base-commit: d816dd0e4cdc45208ba0c488d295c5b2f943e079
change-id: 20261117-b4-vstor-155302-sgl-folio-6c93819b902a

Best regards,
--  
Liu Kui <kui.liu at virtuozzo.com>



More information about the Devel mailing list