Skip to contentSkip to conversation

Support for mapping SQ/CQ rings into huge page

PATCH In review Jens Axboe 2 participants 7 messages May 13, 2023 – May 14, 2023 io_uring

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

v2 cover Support for mapping SQ/CQ rings into huge page

Hi,

io_uring SQ/CQ rings are allocated by the kernel from contigious, normal pages, and then the application mmap()'s the rings into userspace. This works fine, but does require contigious pages to be available for the given SQ and CQ ring sizes. As uptime increases on a given system, so does memory fragmentation. Entropy is invevitable.

This patchset adds support for the application passing in a pre-allocated huge page, and then placing the rings in that. This reduces the need for contigious pages, and also reduces the TLB pressure for larger rings.

The liburing huge.2 branch has support for using this trivially. Applications may use the normal ring init helpers and set IORING_SETUP_NO_MMAP, in which case a huge page will get allocated for them and used. Or they may use io_uring_queue_init_mem() and pass in a pre-allocated huge page, getting the amount of it used returned. This allows placing multiple rings into a single huge page.

Changes since v1:
- Mandate that we're using a single page. May be a normal page if we don't need a lot of memory, or a huge page if the ring itself takes up more space than a single normal page.

Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH v2 1/4 io_uring: remove sq/cq_off memset

We only have two reserved members we're not clearing, do so manually instead. This is in preparation for using one of these members for a new feature.

---
 io_uring/io_uring.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
io_uring/io_uring.c +4 -2
index 3bca7a79efda..3695c5e6fbf0 100644
@@ -3887,7 +3887,6 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
 	if (ret)
 		goto err;
 
-	memset(&p->sq_off, 0, sizeof(p->sq_off));
 	p->sq_off.head = offsetof(struct io_rings, sq.head);
 	p->sq_off.tail = offsetof(struct io_rings, sq.tail);
 	p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
@@ -3895,8 +3894,9 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
 	p->sq_off.flags = offsetof(struct io_rings, sq_flags);
 	p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
 	p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
+	p->sq_off.resv1 = 0;
+	p->sq_off.resv2 = 0;
 
-	memset(&p->cq_off, 0, sizeof(p->cq_off));
 	p->cq_off.head = offsetof(struct io_rings, cq.head);
 	p->cq_off.tail = offsetof(struct io_rings, cq.tail);
 	p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
@@ -3904,6 +3904,8 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
 	p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
 	p->cq_off.cqes = offsetof(struct io_rings, cqes);
 	p->cq_off.flags = offsetof(struct io_rings, cq_flags);
+	p->cq_off.resv1 = 0;
+	p->cq_off.resv2 = 0;
 
 	p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
 			IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v2 2/4 io_uring: return error pointer from io_mem_alloc()

In reply to Jens Axboe

In preparation for having more than one time of ring allocator, make the existing one return valid/error-pointer rather than just NULL.

---
 io_uring/io_uring.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
io_uring/io_uring.c +12 -6
index 3695c5e6fbf0..6266a870c89f 100644
@@ -2712,8 +2712,12 @@ static void io_mem_free(void *ptr)
 static void *io_mem_alloc(size_t size)
 {
 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
+	void *ret;
 
-	return (void *) __get_free_pages(gfp, get_order(size));
+	ret = (void *) __get_free_pages(gfp, get_order(size));
+	if (ret)
+		return ret;
+	return ERR_PTR(-ENOMEM);
 }
 
 static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries,
@@ -3673,6 +3677,7 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 {
 	struct io_rings *rings;
 	size_t size, sq_array_offset;
+	void *ptr;
 
 	/* make sure these are sane, as we already accounted them */
 	ctx->sq_entries = p->sq_entries;
@@ -3683,8 +3688,8 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 		return -EOVERFLOW;
 
 	rings = io_mem_alloc(size);
-	if (!rings)
-		return -ENOMEM;
+	if (IS_ERR(rings))
+		return PTR_ERR(rings);
 
 	ctx->rings = rings;
 	ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
@@ -3703,13 +3708,14 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 		return -EOVERFLOW;
 	}
 
-	ctx->sq_sqes = io_mem_alloc(size);
-	if (!ctx->sq_sqes) {
+	ptr = io_mem_alloc(size);
+	if (IS_ERR(ptr)) {
 		io_mem_free(ctx->rings);
 		ctx->rings = NULL;
-		return -ENOMEM;
+		return PTR_ERR(ptr);
 	}
 
+	ctx->sq_sqes = io_mem_alloc(size);
 	return 0;
 }
 
Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH 2/4] io_uring: return error pointer from io_mem_alloc()

Hi Jens,

Quoted 51 lines · Jens Axboe

In preparation for having more than one time of ring allocator, make the existing one return valid/error-pointer rather than just NULL.

---
 io_uring/io_uring.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
io_uring/io_uring.c +8 -4
index 3695c5e6fbf0..6266a870c89f 100644
@@ -2712,8 +2712,12 @@ static void io_mem_free(void *ptr)
 static void *io_mem_alloc(size_t size)
 {
        gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
+       void *ret;
 
-       return (void *) __get_free_pages(gfp, get_order(size));
+       ret = (void *) __get_free_pages(gfp, get_order(size));
+       if (ret)
+               return ret;
+       return ERR_PTR(-ENOMEM);
 }
 
 static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries,
@@ -3673,6 +3677,7 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 {
        struct io_rings *rings;
        size_t size, sq_array_offset;
+       void *ptr;
 
        /* make sure these are sane, as we already accounted them */
        ctx->sq_entries = p->sq_entries;
@@ -3683,8 +3688,8 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
                return -EOVERFLOW;
 
        rings = io_mem_alloc(size);
-       if (!rings)
-               return -ENOMEM;
+       if (IS_ERR(rings))
+               return PTR_ERR(rings);
 
        ctx->rings = rings;
        ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
@@ -3703,13 +3708,14 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
                return -EOVERFLOW;
        }
 
-       ctx->sq_sqes = io_mem_alloc(size);

io_uring/io_uring.c static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, · line 3718

Quoted 10 lines
io_uring/io_uring.c +4 -2
-       if (!ctx->sq_sqes) {
+       ptr = io_mem_alloc(size);
+       if (IS_ERR(ptr)) {
                io_mem_free(ctx->rings);
                ctx->rings = NULL;
-               return -ENOMEM;
+               return PTR_ERR(ptr);
        }
 
+       ctx->sq_sqes = io_mem_alloc(size);

Should be 'ptr' rather than 'io_mem_alloc(size)' here.

Signature · 1 line
Dmitry Kadashev
author maintainer
#4 lore raw

Re: [PATCH 2/4] io_uring: return error pointer from io_mem_alloc()

Quoted 63 lines · Dmitry Kadashev

Hi Jens,

Quoted 60 lines · On Sat, May 13, 2023 at 9:19?PM Jens Axboe <[email protected]> wrote:

In preparation for having more than one time of ring allocator, make the existing one return valid/error-pointer rather than just NULL.

---
 io_uring/io_uring.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
io_uring/io_uring.c +11 -6
index 3695c5e6fbf0..6266a870c89f 100644
@@ -2712,8 +2712,12 @@ static void io_mem_free(void *ptr)
 static void *io_mem_alloc(size_t size)
 {
        gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
+       void *ret;
 
-       return (void *) __get_free_pages(gfp, get_order(size));
+       ret = (void *) __get_free_pages(gfp, get_order(size));
+       if (ret)
+               return ret;
+       return ERR_PTR(-ENOMEM);
 }
 
 static unsigned long rings_size(struct io_ring_ctx *ctx, unsigned int sq_entries,
@@ -3673,6 +3677,7 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 {
        struct io_rings *rings;
        size_t size, sq_array_offset;
+       void *ptr;
 
        /* make sure these are sane, as we already accounted them */
        ctx->sq_entries = p->sq_entries;
@@ -3683,8 +3688,8 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
                return -EOVERFLOW;
 
        rings = io_mem_alloc(size);
-       if (!rings)
-               return -ENOMEM;
+       if (IS_ERR(rings))
+               return PTR_ERR(rings);
 
        ctx->rings = rings;
        ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
@@ -3703,13 +3708,14 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
                return -EOVERFLOW;
        }
 
-       ctx->sq_sqes = io_mem_alloc(size);
-       if (!ctx->sq_sqes) {
+       ptr = io_mem_alloc(size);
+       if (IS_ERR(ptr)) {
                io_mem_free(ctx->rings);
                ctx->rings = NULL;
-               return -ENOMEM;
+               return PTR_ERR(ptr);
        }
 
Quoted 3 lines

io_uring/io_uring.c static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, · line 3718

Quoted 1 line
io_uring/io_uring.c +1 -0
+       ctx->sq_sqes = io_mem_alloc(size);

Should be 'ptr' rather than 'io_mem_alloc(size)' here.

Indeed, good catch. Patch 4 does correct that so the final result is correct, must've happened during a split rebase a while back. I'll fix up patch 2 and 4 so that it's correct after patch 2 as well.

Signature · 1 line
Jens Axboe
author maintainer
#5 lore raw

PATCH v2 3/4 io_uring: add ring freeing helper

In reply to Jens Axboe

We do rings and sqes separately, move them into a helper that does both the freeing and clearing of the memory.

---
 io_uring/io_uring.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
io_uring/io_uring.c +11 -6
index 6266a870c89f..5433e8d6c481 100644
@@ -2709,6 +2709,14 @@ static void io_mem_free(void *ptr)
 		free_compound_page(page);
 }
 
+static void io_rings_free(struct io_ring_ctx *ctx)
+{
+	io_mem_free(ctx->rings);
+	io_mem_free(ctx->sq_sqes);
+	ctx->rings = NULL;
+	ctx->sq_sqes = NULL;
+}
+
 static void *io_mem_alloc(size_t size)
 {
 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
@@ -2873,8 +2881,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
 		mmdrop(ctx->mm_account);
 		ctx->mm_account = NULL;
 	}
-	io_mem_free(ctx->rings);
-	io_mem_free(ctx->sq_sqes);
+	io_rings_free(ctx);
 
 	percpu_ref_exit(&ctx->refs);
 	free_uid(ctx->user);
@@ -3703,15 +3710,13 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 	else
 		size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
 	if (size == SIZE_MAX) {
-		io_mem_free(ctx->rings);
-		ctx->rings = NULL;
+		io_rings_free(ctx);
 		return -EOVERFLOW;
 	}
 
 	ptr = io_mem_alloc(size);
 	if (IS_ERR(ptr)) {
-		io_mem_free(ctx->rings);
-		ctx->rings = NULL;
+		io_rings_free(ctx);
 		return PTR_ERR(ptr);
 	}
 
Signature · 1 line
2.39.2
author maintainer
#6 lore raw

PATCH v2 4/4 io_uring: support for user allocated memory for rings/sqes

In reply to Jens Axboe

Currently io_uring applications must call mmap(2) twice to map the rings themselves, and the sqes array. This works fine, but it does not support using huge pages to back the rings/sqes.

Provide a way for the application to pass in pre-allocated memory for the rings/sqes, which can then suitably be allocated from shmfs or via mmap to get huge page support.

Particularly for larger rings, this reduces the TLBs needed.

If an application wishes to take advantage of that, it must pre-allocate the memory needed for the sq/cq ring, and the sqes. The former must be passed in via the io_uring_params->cq_off.user_data field, while the latter is passed in via the io_uring_params->sq_off.user_data field. Then it must set IORING_SETUP_NO_MMAP in the io_uring_params->flags field, and io_uring will then map the existing memory into the kernel for shared use. The application must not call mmap(2) to map rings as it otherwise would have, that will now fail with -EINVAL if this setup flag was used.

The pages used for the rings and sqes must be contigious. The intent here is clearly that huge pages should be used, otherwise the normal setup procedure works fine as-is. The application may use one huge page for both the rings and sqes.

Outside of those initialization changes, everything works like it did before.

---
 include/linux/io_uring_types.h |  10 +++
 include/uapi/linux/io_uring.h  |   9 ++-
 io_uring/io_uring.c            | 108 ++++++++++++++++++++++++++++++---
 3 files changed, 115 insertions(+), 12 deletions(-)
include/linux/io_uring_types.h +10 -0
index 1b2a20a42413..f04ce513fadb 100644
@@ -211,6 +211,16 @@ struct io_ring_ctx {
 		unsigned int		compat: 1;
 
 		enum task_work_notify_mode	notify_method;
+
+		/*
+		 * If IORING_SETUP_NO_MMAP is used, then the below holds
+		 * the gup'ed pages for the two rings, and the sqes.
+		 */
+		unsigned short		n_ring_pages;
+		unsigned short		n_sqe_pages;
+		struct page		**ring_pages;
+		struct page		**sqe_pages;
+
 		struct io_rings			*rings;
 		struct task_struct		*submitter_task;
 		struct percpu_ref		refs;
include/uapi/linux/io_uring.h +7 -2
index 0716cb17e436..2edba9a274de 100644
@@ -173,6 +173,11 @@ enum {
  */
 #define IORING_SETUP_DEFER_TASKRUN	(1U << 13)
 
+/*
+ * Application provides the memory for the rings
+ */
+#define IORING_SETUP_NO_MMAP		(1U << 14)
+
 enum io_uring_op {
 	IORING_OP_NOP,
 	IORING_OP_READV,
@@ -406,7 +411,7 @@ struct io_sqring_offsets {
 	__u32 dropped;
 	__u32 array;
 	__u32 resv1;
-	__u64 resv2;
+	__u64 user_addr;
 };
 
 /*
@@ -425,7 +430,7 @@ struct io_cqring_offsets {
 	__u32 cqes;
 	__u32 flags;
 	__u32 resv1;
-	__u64 resv2;
+	__u64 user_addr;
 };
 
 /*
io_uring/io_uring.c +98 -10

157 lines not on this page · raw message

Signature · 1 line
2.39.2