public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers
       [not found] <CGME20240513082306epcas5p2fd8ea6fd88b2c4ab1d17b1508fe2af97@epcas5p2.samsung.com>
@ 2024-05-13  8:22 ` Chenliang Li
       [not found]   ` <CGME20240513082308epcas5p3c38ce4d44fa1613988bbae84eaec41b9@epcas5p3.samsung.com>
                     ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:22 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Registered buffers are stored and processed in the form of bvec array,
each bvec element typically points to a PAGE_SIZE page but can also work
with hugepages. Specifically, a buffer consisting of a hugepage is
coalesced to use only one hugepage bvec entry during registration.
This coalescing feature helps to save both the space and DMA-mapping time.

However, currently the coalescing feature doesn't work for multi-hugepage
buffers. For a buffer with several 2M hugepages, we still split it into
thousands of 4K page bvec entries while in fact, we can just use a
handful of hugepage bvecs.

This patch series enables coalescing registered buffers with more than
one hugepages. It optimizes the DMA-mapping time and saves memory for
these kind of buffers.

Perf diff of 8M(4*2M) hugepage fixed buffer fio test:

fio/t/io_uring -d64 -s32 -c32 -b8388608 -p0 -B1 -F0 -n1 -O1 -r10 \
-R1 /dev/nvme0n1

Before          After           Symbol

5.90%                           [k] __blk_rq_map_sg
3.70%                           [k] dma_direct_map_sg
3.07%                           [k] dma_pool_alloc
1.12%                           [k] sg_next
                +0.44%          [k] dma_map_page_attrs

First three patches prepare for adding the multi-hugepage coalescing
into buffer registration, the 4th patch enables the feature. The 5th
patch add test cases for this feature in liburing.

-----------------
Changes since v2:

- Modify the loop iterator increment to make code cleaner
- Minor fix to the return procedure in coalesced buffer account
- Correct commit messages
- Add test cases in liburing

v2 : https://lore.kernel.org/io-uring/[email protected]/T/#t

Changes since v1:

- Split into 4 patches
- Fix code style issues
- Rearrange the change of code for cleaner look
- Add speciallized pinned page accounting procedure for coalesced
  buffers
- Reordered the newly add fields in imu struct for better compaction

v1 : https://lore.kernel.org/io-uring/[email protected]/T/#u

Chenliang Li (5):
  io_uring/rsrc: add hugepage buffer coalesce helpers
  io_uring/rsrc: store folio shift and mask into imu
  io_uring/rsrc: add init and account functions for coalesced imus
  io_uring/rsrc: enable multi-hugepage buffer coalescing
  liburing: add test cases for hugepage registered buffers

 io_uring/rsrc.c | 217 +++++++++++++++++++++++++++++++++++++++---------
 io_uring/rsrc.h |  12 +++
 2 files changed, 191 insertions(+), 38 deletions(-)


base-commit: 59b28a6e37e650c0d601ed87875b6217140cda5d
-- 
2.34.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 1/5] io_uring/rsrc: add hugepage buffer coalesce helpers
       [not found]   ` <CGME20240513082308epcas5p3c38ce4d44fa1613988bbae84eaec41b9@epcas5p3.samsung.com>
@ 2024-05-13  8:22     ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:22 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Introduce helper functions to check whether a buffer can
be coalesced or not, and gather folio data for later use.

The coalescing optimizes time and space consumption caused
by mapping and storing multi-hugepage fixed buffers.

A coalescable multi-hugepage buffer should fully cover its folios
(except potentially the first and last one), and these folios should
have the same size. These requirements are for easier later process,
also we need same size'd chunks in io_import_fixed for fast iov_iter
adjust.

Signed-off-by: Chenliang Li <[email protected]>
---
 io_uring/rsrc.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++
 io_uring/rsrc.h | 10 +++++++
 2 files changed, 88 insertions(+)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 65417c9553b1..d08224c0c5b0 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -871,6 +871,84 @@ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
 	return ret;
 }
 
+static bool __io_sqe_buffer_try_coalesce(struct page **pages, int nr_pages,
+					 struct io_imu_folio_data *data)
+{
+	struct folio *folio = page_folio(pages[0]);
+	unsigned int count = 1;
+	int i;
+
+	data->nr_pages_mid = folio_nr_pages(folio);
+	if (data->nr_pages_mid == 1)
+		return false;
+
+	data->folio_shift = folio_shift(folio);
+	data->folio_size = folio_size(folio);
+	data->nr_folios = 1;
+	/*
+	 * Check if pages are contiguous inside a folio, and all folios have
+	 * the same page count except for the head and tail.
+	 */
+	for (i = 1; i < nr_pages; i++) {
+		if (page_folio(pages[i]) == folio &&
+			pages[i] == pages[i-1] + 1) {
+			count++;
+			continue;
+		}
+
+		if (data->nr_folios == 1)
+			data->nr_pages_head = count;
+		else if (count != data->nr_pages_mid)
+			return false;
+
+		folio = page_folio(pages[i]);
+		if (folio_size(folio) != data->folio_size)
+			return false;
+
+		count = 1;
+		data->nr_folios++;
+	}
+	if (data->nr_folios == 1)
+		data->nr_pages_head = count;
+
+	return true;
+}
+
+static bool io_sqe_buffer_try_coalesce(struct page **pages, int nr_pages,
+				       struct io_imu_folio_data *data)
+{
+	int i, j;
+
+	if (nr_pages <= 1 ||
+		!__io_sqe_buffer_try_coalesce(pages, nr_pages, data))
+		return false;
+
+	/*
+	 * The pages are bound to the folio, it doesn't
+	 * actually unpin them but drops all but one reference,
+	 * which is usually put down by io_buffer_unmap().
+	 * Note, needs a better helper.
+	 */
+	if (data->nr_pages_head > 1)
+		unpin_user_pages(&pages[1], data->nr_pages_head - 1);
+
+	j = data->nr_pages_head;
+	nr_pages -= data->nr_pages_head;
+	for (i = 1; i < data->nr_folios; i++) {
+		unsigned int nr_unpin;
+
+		nr_unpin = min_t(unsigned int, nr_pages - 1,
+					data->nr_pages_mid - 1);
+		if (nr_unpin == 0)
+			break;
+		unpin_user_pages(&pages[j+1], nr_unpin);
+		j += data->nr_pages_mid;
+		nr_pages -= data->nr_pages_mid;
+	}
+
+	return true;
+}
+
 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 				  struct io_mapped_ubuf **pimu,
 				  struct page **last_hpage)
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index c032ca3436ca..b2a9d66b76dd 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -50,6 +50,16 @@ struct io_mapped_ubuf {
 	struct bio_vec	bvec[] __counted_by(nr_bvecs);
 };
 
+struct io_imu_folio_data {
+	/* Head folio can be partially included in the fixed buf */
+	unsigned int	nr_pages_head;
+	/* For non-head/tail folios, has to be fully included */
+	unsigned int	nr_pages_mid;
+	unsigned int	nr_folios;
+	unsigned int	folio_shift;
+	size_t		folio_size;
+};
+
 void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v3 2/5] io_uring/rsrc: store folio shift and mask into imu
       [not found]   ` <CGME20240513082310epcas5p27576a80eae3ee09e40102b179ce46fa9@epcas5p2.samsung.com>
@ 2024-05-13  8:22     ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:22 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Store the folio shift and folio mask into imu struct and use it in
iov_iter adjust, as we will have non PAGE_SIZE'd chunks if a
multi-hugepage buffer get coalesced.

Signed-off-by: Chenliang Li <[email protected]>
---
 io_uring/rsrc.c | 6 ++++--
 io_uring/rsrc.h | 2 ++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index d08224c0c5b0..578d382ca9bc 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1015,6 +1015,8 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 	imu->ubuf = (unsigned long) iov->iov_base;
 	imu->ubuf_end = imu->ubuf + iov->iov_len;
 	imu->nr_bvecs = nr_pages;
+	imu->folio_shift = PAGE_SHIFT;
+	imu->folio_mask = PAGE_MASK;
 	*pimu = imu;
 	ret = 0;
 
@@ -1153,12 +1155,12 @@ int io_import_fixed(int ddir, struct iov_iter *iter,
 
 			/* skip first vec */
 			offset -= bvec->bv_len;
-			seg_skip = 1 + (offset >> PAGE_SHIFT);
+			seg_skip = 1 + (offset >> imu->folio_shift);
 
 			iter->bvec = bvec + seg_skip;
 			iter->nr_segs -= seg_skip;
 			iter->count -= bvec->bv_len + offset;
-			iter->iov_offset = offset & ~PAGE_MASK;
+			iter->iov_offset = offset & ~imu->folio_mask;
 		}
 	}
 
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index b2a9d66b76dd..93da02e652bc 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -46,7 +46,9 @@ struct io_mapped_ubuf {
 	u64		ubuf;
 	u64		ubuf_end;
 	unsigned int	nr_bvecs;
+	unsigned int	folio_shift;
 	unsigned long	acct_pages;
+	unsigned long	folio_mask;
 	struct bio_vec	bvec[] __counted_by(nr_bvecs);
 };
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v3 3/5] io_uring/rsrc: add init and account functions for coalesced imus
       [not found]   ` <CGME20240513082311epcas5p3556d301a1f1faca0c6b613555324861e@epcas5p3.samsung.com>
@ 2024-05-13  8:22     ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:22 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Introduce two functions to separate the coalesced imu alloc and
accounting path from the original one. This helps to keep the original
code path clean.

Signed-off-by: Chenliang Li <[email protected]>
---
 io_uring/rsrc.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 578d382ca9bc..53fac5f27bbf 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -871,6 +871,45 @@ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
 	return ret;
 }
 
+static int io_coalesced_buffer_account_pin(struct io_ring_ctx *ctx,
+					   struct page **pages,
+					   struct io_mapped_ubuf *imu,
+					   struct page **last_hpage,
+					   struct io_imu_folio_data *data)
+{
+	int i, j, ret;
+
+	imu->acct_pages = 0;
+	j = 0;
+	for (i = 0; i < data->nr_folios; i++) {
+		struct page *hpage = pages[j];
+
+		if (hpage == *last_hpage)
+			continue;
+		*last_hpage = hpage;
+		/*
+		 * Already checked the page array in try coalesce,
+		 * so pass in nr_pages=0 here to waive that.
+		 */
+		if (headpage_already_acct(ctx, pages, 0, hpage))
+			continue;
+		imu->acct_pages += data->nr_pages_mid;
+		if (i)
+			j += data->nr_pages_mid;
+		else
+			j = data->nr_pages_head;
+	}
+
+	if (!imu->acct_pages)
+		return 0;
+
+	ret = io_account_mem(ctx, imu->acct_pages);
+	if (!ret)
+		return 0;
+	imu->acct_pages = 0;
+	return ret;
+}
+
 static bool __io_sqe_buffer_try_coalesce(struct page **pages, int nr_pages,
 					 struct io_imu_folio_data *data)
 {
@@ -949,6 +988,56 @@ static bool io_sqe_buffer_try_coalesce(struct page **pages, int nr_pages,
 	return true;
 }
 
+static int io_coalesced_imu_alloc(struct io_ring_ctx *ctx, struct iovec *iov,
+				  struct io_mapped_ubuf **pimu,
+				  struct page **last_hpage, struct page **pages,
+				  struct io_imu_folio_data *data)
+{
+	struct io_mapped_ubuf *imu = NULL;
+	unsigned long off;
+	size_t size, vec_len;
+	int ret, i, j;
+
+	ret = -ENOMEM;
+	imu = kvmalloc(struct_size(imu, bvec, data->nr_folios), GFP_KERNEL);
+	if (!imu)
+		return ret;
+
+	ret = io_coalesced_buffer_account_pin(ctx, pages, imu, last_hpage,
+						data);
+	if (ret) {
+		unpin_user_page(pages[0]);
+		j = data->nr_pages_head;
+		for (i = 1; i < data->nr_folios; i++) {
+			unpin_user_page(pages[j]);
+			j += data->nr_pages_mid;
+		}
+		return ret;
+	}
+	off = (unsigned long) iov->iov_base & ~PAGE_MASK;
+	size = iov->iov_len;
+	/* store original address for later verification */
+	imu->ubuf = (unsigned long) iov->iov_base;
+	imu->ubuf_end = imu->ubuf + iov->iov_len;
+	imu->nr_bvecs = data->nr_folios;
+	imu->folio_shift = data->folio_shift;
+	imu->folio_mask = ~((1UL << data->folio_shift) - 1);
+	*pimu = imu;
+	ret = 0;
+
+	vec_len = min_t(size_t, size, PAGE_SIZE * data->nr_pages_head - off);
+	bvec_set_page(&imu->bvec[0], pages[0], vec_len, off);
+	size -= vec_len;
+	j = data->nr_pages_head;
+	for (i = 1; i < data->nr_folios; i++) {
+		vec_len = min_t(size_t, size, data->folio_size);
+		bvec_set_page(&imu->bvec[i], pages[j], vec_len, 0);
+		size -= vec_len;
+		j += data->nr_pages_mid;
+	}
+	return ret;
+}
+
 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 				  struct io_mapped_ubuf **pimu,
 				  struct page **last_hpage)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing
       [not found]   ` <CGME20240513082313epcas5p2a781d3393e4bf92d13d04ac62bb28fb7@epcas5p2.samsung.com>
@ 2024-05-13  8:22     ` Chenliang Li
  2024-05-13 12:11       ` Anuj gupta
  0 siblings, 1 reply; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:22 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Modify the original buffer registration path to expand the
one-hugepage coalescing feature to work with multi-hugepage
buffers. Separated from previous patches to make it more
easily reviewed.

Signed-off-by: Chenliang Li <[email protected]>
---
 io_uring/rsrc.c | 44 ++++++++------------------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 53fac5f27bbf..5e5c1d6f3501 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1047,7 +1047,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 	unsigned long off;
 	size_t size;
 	int ret, nr_pages, i;
-	struct folio *folio = NULL;
+	struct io_imu_folio_data data;
 
 	*pimu = (struct io_mapped_ubuf *)&dummy_ubuf;
 	if (!iov->iov_base)
@@ -1062,30 +1062,11 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 		goto done;
 	}
 
-	/* If it's a huge page, try to coalesce them into a single bvec entry */
-	if (nr_pages > 1) {
-		folio = page_folio(pages[0]);
-		for (i = 1; i < nr_pages; i++) {
-			/*
-			 * Pages must be consecutive and on the same folio for
-			 * this to work
-			 */
-			if (page_folio(pages[i]) != folio ||
-			    pages[i] != pages[i - 1] + 1) {
-				folio = NULL;
-				break;
-			}
-		}
-		if (folio) {
-			/*
-			 * The pages are bound to the folio, it doesn't
-			 * actually unpin them but drops all but one reference,
-			 * which is usually put down by io_buffer_unmap().
-			 * Note, needs a better helper.
-			 */
-			unpin_user_pages(&pages[1], nr_pages - 1);
-			nr_pages = 1;
-		}
+	/* If it's huge page(s), try to coalesce them into fewer bvec entries */
+	if (io_sqe_buffer_try_coalesce(pages, nr_pages, &data)) {
+		ret = io_coalesced_imu_alloc(ctx, iov, pimu, last_hpage,
+						pages, &data);
+		goto done;
 	}
 
 	imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
@@ -1109,10 +1090,6 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 	*pimu = imu;
 	ret = 0;
 
-	if (folio) {
-		bvec_set_page(&imu->bvec[0], pages[0], size, off);
-		goto done;
-	}
 	for (i = 0; i < nr_pages; i++) {
 		size_t vec_len;
 
@@ -1218,23 +1195,18 @@ int io_import_fixed(int ddir, struct iov_iter *iter,
 		 * we know that:
 		 *
 		 * 1) it's a BVEC iter, we set it up
-		 * 2) all bvecs are PAGE_SIZE in size, except potentially the
+		 * 2) all bvecs are the same in size, except potentially the
 		 *    first and last bvec
 		 *
 		 * So just find our index, and adjust the iterator afterwards.
 		 * If the offset is within the first bvec (or the whole first
 		 * bvec, just use iov_iter_advance(). This makes it easier
 		 * since we can just skip the first segment, which may not
-		 * be PAGE_SIZE aligned.
+		 * be folio_size aligned.
 		 */
 		const struct bio_vec *bvec = imu->bvec;
 
 		if (offset < bvec->bv_len) {
-			/*
-			 * Note, huge pages buffers consists of one large
-			 * bvec entry and should always go this way. The other
-			 * branch doesn't expect non PAGE_SIZE'd chunks.
-			 */
 			iter->bvec = bvec;
 			iter->nr_segs = bvec->bv_len;
 			iter->count -= offset;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v3 5/5] liburing: add test cases for hugepage registered buffers
       [not found]   ` <CGME20240513082314epcas5p309fa70575596792b5c9923ce76a3778f@epcas5p3.samsung.com>
@ 2024-05-13  8:23     ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-13  8:23 UTC (permalink / raw)
  To: axboe, asml.silence
  Cc: io-uring, peiwei.li, joshi.k, kundan.kumar, gost.dev,
	Chenliang Li

Add a test file for hugepage registered buffers, to make sure the
fixed buffer coalescing feature works safe and soundly.

Testcases include reading/writing with single/multiple hugepage buffers.

Signed-off-by: Chenliang Li <[email protected]>
---
 test/Makefile         |   1 +
 test/fixed-hugepage.c | 236 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 237 insertions(+)
 create mode 100644 test/fixed-hugepage.c

diff --git a/test/Makefile b/test/Makefile
index 94bdc25..364514d 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -88,6 +88,7 @@ test_srcs := \
 	file-update.c \
 	file-verify.c \
 	fixed-buf-iter.c \
+	fixed-hugepage.c \
 	fixed-link.c \
 	fixed-reuse.c \
 	fpos.c \
diff --git a/test/fixed-hugepage.c b/test/fixed-hugepage.c
new file mode 100644
index 0000000..4fe45a2
--- /dev/null
+++ b/test/fixed-hugepage.c
@@ -0,0 +1,236 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Test fixed buffers consisting of hugepages.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+#include "liburing.h"
+#include "helpers.h"
+
+#define PAGE_SIZE	4096
+#define HUGEPAGE_SIZE	(512 * PAGE_SIZE)
+#define NR_BUFS		1
+#define IN_FD		"/dev/urandom"
+#define OUT_FD		"/dev/zero"
+
+static int open_files(int *fd_in, int *fd_out)
+{
+	*fd_in = open(IN_FD, O_RDONLY, 0644);
+	if (*fd_in < 0) {
+		perror("open in");
+		return 1;
+	}
+
+	*fd_out = open(OUT_FD, O_RDWR, 0644);
+	if (*fd_out < 0) {
+		perror("open out");
+		return 1;
+	}
+
+	return 0;
+}
+
+static int mmap_hugebufs(struct iovec *iov, int nr_bufs, size_t buf_size)
+{
+	int i;
+
+	for (i = 0; i < nr_bufs; i++) {
+		void *base = NULL;
+
+		base = mmap(NULL, buf_size, PROT_READ | PROT_WRITE,
+				MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
+		if (!base || base == MAP_FAILED) {
+			fprintf(stderr, "Error in mmapping the %dth buffer", i);
+			return 1;
+		}
+
+		iov[i].iov_base = base;
+		iov[i].iov_len = buf_size;
+		memset(iov[i].iov_base, 0, buf_size);
+	}
+
+	return 0;
+}
+
+static int do_read(struct io_uring *ring, int fd, struct iovec *iov,
+				int nr_bufs, size_t buf_size)
+{
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	int i, ret;
+
+	for (i = 0; i < nr_bufs; i++) {
+		sqe = io_uring_get_sqe(ring);
+		if (!sqe) {
+			fprintf(stderr, "Could not get SQE.\n");
+			return 1;
+		}
+
+		io_uring_prep_read_fixed(sqe, fd, iov[i].iov_base, buf_size, 0, i);
+		io_uring_submit(ring);
+
+		ret = io_uring_wait_cqe(ring, &cqe);
+		if (ret < 0) {
+			fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
+			return 1;
+		}
+
+		if (cqe->res < 0) {
+			fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
+			return 1;
+		}
+
+		io_uring_cqe_seen(ring, cqe);
+	}
+
+	return 0;
+}
+
+static int do_write(struct io_uring *ring, int fd, struct iovec *iov,
+				int nr_bufs, size_t buf_size)
+{
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	int i, ret;
+
+	for (i = 0; i < nr_bufs; i++) {
+		sqe = io_uring_get_sqe(ring);
+		if (!sqe) {
+			fprintf(stderr, "Could not get SQE.\n");
+			return 1;
+		}
+
+		io_uring_prep_write_fixed(sqe, fd, iov[i].iov_base, buf_size, 0, i);
+		io_uring_submit(ring);
+
+		ret = io_uring_wait_cqe(ring, &cqe);
+		if (ret < 0) {
+			fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
+			return 1;
+		}
+
+		if (cqe->res < 0) {
+			fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
+			return 1;
+		}
+
+		io_uring_cqe_seen(ring, cqe);
+	}
+
+	return 0;
+}
+
+static int test_one_hugepage(struct io_uring *ring, int fd_in, int fd_out)
+{
+	struct iovec iov[NR_BUFS];
+	int ret;
+	size_t buf_size = HUGEPAGE_SIZE;
+
+	ret = mmap_hugebufs(iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "Buffer allocating for one hugepage failed.");
+		return 1;
+	}
+
+	ret = io_uring_register_buffers(ring, iov, NR_BUFS);
+	if (ret) {
+		fprintf(stderr, "Error registering buffers for one hugepage test: %s", strerror(-ret));
+		return 1;
+	}
+
+	ret = do_read(ring, fd_in, iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "One hugepage read test failed");
+		return ret;
+	}
+
+	ret = do_write(ring, fd_out, iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "One hugepages write test failed");
+		return ret;
+	}
+
+	ret = io_uring_unregister_buffers(ring);
+	if (ret) {
+		fprintf(stderr, "Error unregistering buffers for one hugepage test: %s", strerror(-ret));
+		return 1;
+	}
+
+	return 0;
+}
+
+static int test_multi_hugepages(struct io_uring *ring, int fd_in, int fd_out)
+{
+	struct iovec iov[NR_BUFS];
+	int ret;
+	size_t buf_size = 4 * HUGEPAGE_SIZE;
+
+	ret = mmap_hugebufs(iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "mmap multi hugepages failed.");
+		return 1;
+	}
+
+	ret = io_uring_register_buffers(ring, iov, NR_BUFS);
+	if (ret) {
+		fprintf(stderr, "Error registering buffers for multi hugepages test: %s", strerror(-ret));
+		return 1;
+	}
+
+	ret = do_read(ring, fd_in, iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "multi hugepages read test failed");
+		return ret;
+	}
+
+	ret = do_write(ring, fd_out, iov, NR_BUFS, buf_size);
+	if (ret) {
+		fprintf(stderr, "multi hugepages write test failed");
+		return ret;
+	}
+
+	ret = io_uring_unregister_buffers(ring);
+	if (ret) {
+		fprintf(stderr, "Error unregistering buffers for multi hugepages test: %s", strerror(-ret));
+		return 1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	struct io_uring ring;
+	int ret, fd_in, fd_out;
+
+	if (argc > 1)
+		return T_EXIT_SKIP;
+
+	if (open_files(&fd_in, &fd_out))
+		return T_EXIT_FAIL;
+
+	ret = t_create_ring(8, &ring, 0);
+	if (ret == T_SETUP_SKIP)
+		return T_EXIT_SKIP;
+	else if (ret < 0)
+		return T_EXIT_FAIL;
+
+	ret = test_one_hugepage(&ring, fd_in, fd_out);
+	if (ret) {
+		fprintf(stderr, "Test one hugepage failed");
+		return T_EXIT_FAIL;
+	}
+
+	ret = test_multi_hugepages(&ring, fd_in, fd_out);
+	if (ret) {
+		fprintf(stderr, "Test multi hugepages failed");
+		return T_EXIT_FAIL;
+	}
+
+	io_uring_queue_exit(&ring);
+	return T_EXIT_PASS;
+}

base-commit: 23b43f44f882e48a725ad87f8f22722c40743dec
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers
  2024-05-13  8:22 ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Chenliang Li
                     ` (4 preceding siblings ...)
       [not found]   ` <CGME20240513082314epcas5p309fa70575596792b5c9923ce76a3778f@epcas5p3.samsung.com>
@ 2024-05-13 12:09   ` Anuj gupta
  2024-05-13 13:40     ` Jens Axboe
                       ` (2 more replies)
  5 siblings, 3 replies; 12+ messages in thread
From: Anuj gupta @ 2024-05-13 12:09 UTC (permalink / raw)
  To: Chenliang Li
  Cc: axboe, asml.silence, io-uring, peiwei.li, joshi.k, kundan.kumar,
	gost.dev

On Mon, May 13, 2024 at 1:59 PM Chenliang Li <[email protected]> wrote:
>
> Registered buffers are stored and processed in the form of bvec array,
> each bvec element typically points to a PAGE_SIZE page but can also work
> with hugepages. Specifically, a buffer consisting of a hugepage is
> coalesced to use only one hugepage bvec entry during registration.
> This coalescing feature helps to save both the space and DMA-mapping time.
>
> However, currently the coalescing feature doesn't work for multi-hugepage
> buffers. For a buffer with several 2M hugepages, we still split it into
> thousands of 4K page bvec entries while in fact, we can just use a
> handful of hugepage bvecs.
>
> This patch series enables coalescing registered buffers with more than
> one hugepages. It optimizes the DMA-mapping time and saves memory for
> these kind of buffers.
>
> Perf diff of 8M(4*2M) hugepage fixed buffer fio test:
>
> fio/t/io_uring -d64 -s32 -c32 -b8388608 -p0 -B1 -F0 -n1 -O1 -r10 \
> -R1 /dev/nvme0n1

It seems you modified t/io_uring to allocate from hugepages. It would be nice
to mention that part here.
--
Anuj Gupta

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing
  2024-05-13  8:22     ` [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing Chenliang Li
@ 2024-05-13 12:11       ` Anuj gupta
  0 siblings, 0 replies; 12+ messages in thread
From: Anuj gupta @ 2024-05-13 12:11 UTC (permalink / raw)
  To: Chenliang Li
  Cc: axboe, asml.silence, io-uring, peiwei.li, joshi.k, kundan.kumar,
	gost.dev

On Mon, May 13, 2024 at 2:04 PM Chenliang Li <[email protected]> wrote:
>
> Modify the original buffer registration path to expand the
> one-hugepage coalescing feature to work with multi-hugepage
> buffers. Separated from previous patches to make it more
> easily reviewed.

The last line should not be a part of the commit description IMO.
--
Anuj Gupta

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers
  2024-05-13 12:09   ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage " Anuj gupta
@ 2024-05-13 13:40     ` Jens Axboe
       [not found]       ` <CGME20240514001813epcas5p455c8a3dd6f2164626a526c05f7fd92c4@epcas5p4.samsung.com>
       [not found]     ` <CGME20240514001443epcas5p362c60c233ed2aecdb5e144099b44d9be@epcas5p3.samsung.com>
       [not found]     ` <CGME20240514001620epcas5p10d8c08ffc3dbd746213df21e47df19f7@epcas5p1.samsung.com>
  2 siblings, 1 reply; 12+ messages in thread
From: Jens Axboe @ 2024-05-13 13:40 UTC (permalink / raw)
  To: Anuj gupta, Chenliang Li
  Cc: asml.silence, io-uring, peiwei.li, joshi.k, kundan.kumar,
	gost.dev

On 5/13/24 6:09 AM, Anuj gupta wrote:
> On Mon, May 13, 2024 at 1:59?PM Chenliang Li <[email protected]> wrote:
>>
>> Registered buffers are stored and processed in the form of bvec array,
>> each bvec element typically points to a PAGE_SIZE page but can also work
>> with hugepages. Specifically, a buffer consisting of a hugepage is
>> coalesced to use only one hugepage bvec entry during registration.
>> This coalescing feature helps to save both the space and DMA-mapping time.
>>
>> However, currently the coalescing feature doesn't work for multi-hugepage
>> buffers. For a buffer with several 2M hugepages, we still split it into
>> thousands of 4K page bvec entries while in fact, we can just use a
>> handful of hugepage bvecs.
>>
>> This patch series enables coalescing registered buffers with more than
>> one hugepages. It optimizes the DMA-mapping time and saves memory for
>> these kind of buffers.
>>
>> Perf diff of 8M(4*2M) hugepage fixed buffer fio test:
>>
>> fio/t/io_uring -d64 -s32 -c32 -b8388608 -p0 -B1 -F0 -n1 -O1 -r10 \
>> -R1 /dev/nvme0n1
> 
> It seems you modified t/io_uring to allocate from hugepages. It would be nice
> to mention that part here.

Yes, please just send a separate series/patch for both liburing and fio.
This series should be strictly the kernel side changes required, then
reference/link the postings for the t/io_uring and liburing test case(s)
in the cover letter.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing
       [not found]     ` <CGME20240514001443epcas5p362c60c233ed2aecdb5e144099b44d9be@epcas5p3.samsung.com>
@ 2024-05-14  0:14       ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-14  0:14 UTC (permalink / raw)
  To: anuj1072538
  Cc: asml.silence, axboe, cliang01.li, gost.dev, io-uring, joshi.k,
	kundan.kumar, peiwei.li

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="y", Size: 440 bytes --]

On Mon, 13 May 2024 17:41:14 +0530, Anuj Gupta wrote:
> On Mon, May 13, 2024 at 2:04 PM Chenliang Li <[email protected]> wrote:
>>
>> Modify the original buffer registration path to expand the
>> one-hugepage coalescing feature to work with multi-hugepage
>> buffers. Separated from previous patches to make it more
>> easily reviewed.

> The last line should not be a part of the commit description IMO.

Will delete that. Thanks.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers
       [not found]     ` <CGME20240514001620epcas5p10d8c08ffc3dbd746213df21e47df19f7@epcas5p1.samsung.com>
@ 2024-05-14  0:16       ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-14  0:16 UTC (permalink / raw)
  To: anuj1072538
  Cc: asml.silence, axboe, cliang01.li, gost.dev, io-uring, joshi.k,
	kundan.kumar, peiwei.li

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="y", Size: 1260 bytes --]

On Mon, 13 May 2024 17:39:37 +0530, Anuj Gupta wrote:
> On Mon, May 13, 2024 at 1:59 PM Chenliang Li <[email protected]> wrote:
>>
>> Registered buffers are stored and processed in the form of bvec array,
>> each bvec element typically points to a PAGE_SIZE page but can also work
>> with hugepages. Specifically, a buffer consisting of a hugepage is
>> coalesced to use only one hugepage bvec entry during registration.
>> This coalescing feature helps to save both the space and DMA-mapping time.
>>
>> However, currently the coalescing feature doesn't work for multi-hugepage
>> buffers. For a buffer with several 2M hugepages, we still split it into
>> thousands of 4K page bvec entries while in fact, we can just use a
>> handful of hugepage bvecs.
>>
>> This patch series enables coalescing registered buffers with more than
>> one hugepages. It optimizes the DMA-mapping time and saves memory for
>> these kind of buffers.
>>
>> Perf diff of 8M(4*2M) hugepage fixed buffer fio test:
>>
>> fio/t/io_uring -d64 -s32 -c32 -b8388608 -p0 -B1 -F0 -n1 -O1 -r10 \
>> -R1 /dev/nvme0n1
> 
> It seems you modified t/io_uring to allocate from hugepages. It would be nice
> to mention that part here.

Yeah I forgot to mention that. Thanks for pointing out.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers
       [not found]       ` <CGME20240514001813epcas5p455c8a3dd6f2164626a526c05f7fd92c4@epcas5p4.samsung.com>
@ 2024-05-14  0:18         ` Chenliang Li
  0 siblings, 0 replies; 12+ messages in thread
From: Chenliang Li @ 2024-05-14  0:18 UTC (permalink / raw)
  To: axboe
  Cc: anuj1072538, asml.silence, cliang01.li, gost.dev, io-uring,
	joshi.k, kundan.kumar, peiwei.li

On Mon, 13 May 2024 07:40:13 -0600, Jens Axboe wrote:
> Yes, please just send a separate series/patch for both liburing and fio.
> This series should be strictly the kernel side changes required, then
> reference/link the postings for the t/io_uring and liburing test case(s)
> in the cover letter.

Sure, will send them separately.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-14  0:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20240513082306epcas5p2fd8ea6fd88b2c4ab1d17b1508fe2af97@epcas5p2.samsung.com>
2024-05-13  8:22 ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Chenliang Li
     [not found]   ` <CGME20240513082308epcas5p3c38ce4d44fa1613988bbae84eaec41b9@epcas5p3.samsung.com>
2024-05-13  8:22     ` [PATCH v3 1/5] io_uring/rsrc: add hugepage buffer coalesce helpers Chenliang Li
     [not found]   ` <CGME20240513082310epcas5p27576a80eae3ee09e40102b179ce46fa9@epcas5p2.samsung.com>
2024-05-13  8:22     ` [PATCH v3 2/5] io_uring/rsrc: store folio shift and mask into imu Chenliang Li
     [not found]   ` <CGME20240513082311epcas5p3556d301a1f1faca0c6b613555324861e@epcas5p3.samsung.com>
2024-05-13  8:22     ` [PATCH v3 3/5] io_uring/rsrc: add init and account functions for coalesced imus Chenliang Li
     [not found]   ` <CGME20240513082313epcas5p2a781d3393e4bf92d13d04ac62bb28fb7@epcas5p2.samsung.com>
2024-05-13  8:22     ` [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing Chenliang Li
2024-05-13 12:11       ` Anuj gupta
     [not found]   ` <CGME20240513082314epcas5p309fa70575596792b5c9923ce76a3778f@epcas5p3.samsung.com>
2024-05-13  8:23     ` [PATCH v3 5/5] liburing: add test cases for hugepage registered buffers Chenliang Li
2024-05-13 12:09   ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage " Anuj gupta
2024-05-13 13:40     ` Jens Axboe
     [not found]       ` <CGME20240514001813epcas5p455c8a3dd6f2164626a526c05f7fd92c4@epcas5p4.samsung.com>
2024-05-14  0:18         ` Chenliang Li
     [not found]     ` <CGME20240514001443epcas5p362c60c233ed2aecdb5e144099b44d9be@epcas5p3.samsung.com>
2024-05-14  0:14       ` [PATCH v3 4/5] io_uring/rsrc: enable multi-hugepage buffer coalescing Chenliang Li
     [not found]     ` <CGME20240514001620epcas5p10d8c08ffc3dbd746213df21e47df19f7@epcas5p1.samsung.com>
2024-05-14  0:16       ` [PATCH v3 0/5] io_uring/rsrc: coalescing multi-hugepage registered buffers Chenliang Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox