public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com
Subject: [RFC 12/16] io_uring: convert pointer init to io_region_slice
Date: Thu,  6 Nov 2025 17:01:51 +0000	[thread overview]
Message-ID: <a98d764a5883e116f1f6a3166800db2ba1623b0d.1762447538.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1762447538.git.asml.silence@gmail.com>

Use io_region_slice() to initialise ctx ring pointers. The helper
performs bound checks and other sanitisation, which is safer and will be
especially helpful when ring placement gets more complicated in coming
patches. It also extends struct io_scq_dim with all intermediate offsets
and sizes to fully describe rings.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 io_uring/io_uring.c | 47 ++++++++++++++++++++++++---------------------
 io_uring/io_uring.h |  2 ++
 2 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 9aef41f6ce23..4f38a0b587fd 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2759,9 +2759,7 @@ static void io_rings_free(struct io_ring_ctx *ctx)
 int rings_size(unsigned int flags, unsigned int sq_entries,
 	       unsigned int cq_entries, struct io_scq_dim *dims)
 {
-	size_t off, sq_array_size;
-	size_t cq_size, cqe_size;
-	size_t sqe_size;
+	size_t cqe_size, off, sqe_size;
 
 	dims->sq_array_offset = SIZE_MAX;
 
@@ -2773,18 +2771,18 @@ int rings_size(unsigned int flags, unsigned int sq_entries,
 		cqe_size *= 2;
 
 	dims->sq_size = array_size(sqe_size, sq_entries);
-	if (dims->sq_size == SIZE_MAX)
+	dims->sq_array_size = array_size(sizeof(u32), sq_entries);
+	dims->cq_size = array_size(cqe_size, cq_entries);
+
+	if (dims->sq_size == SIZE_MAX || dims->cq_size == SIZE_MAX ||
+	    dims->sq_array_size == SIZE_MAX)
 		return -EOVERFLOW;
 
 	off = sizeof(struct io_rings);
 	off = L1_CACHE_ALIGN(off);
 	dims->cq_offset = off;
 
-	cq_size = array_size(cqe_size, cq_entries);
-	if (cq_size == SIZE_MAX)
-		return -EOVERFLOW;
-
-	off = size_add(off, cq_size);
+	off = size_add(off, dims->cq_size);
 	if (off == SIZE_MAX)
 		return -EOVERFLOW;
 
@@ -2809,12 +2807,7 @@ int rings_size(unsigned int flags, unsigned int sq_entries,
 	}
 
 	dims->sq_array_offset = off;
-
-	sq_array_size = array_size(sizeof(u32), sq_entries);
-	if (sq_array_size == SIZE_MAX)
-		return -EOVERFLOW;
-
-	if (check_add_overflow(off, sq_array_size, &off))
+	if (check_add_overflow(off, dims->sq_array_size, &off))
 		return -EOVERFLOW;
 
 	dims->cq_comp_size = off;
@@ -3375,7 +3368,6 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 	struct io_scq_dim *dims = &config->dims;
 	struct io_uring_region_desc rd;
 	struct io_rings *rings;
-	void *ptr;
 	int ret;
 
 	/* make sure these are sane, as we already accounted them */
@@ -3391,12 +3383,19 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 	ret = io_create_region(ctx, &ctx->ring_region, &rd, IORING_OFF_CQ_RING);
 	if (ret)
 		return ret;
-	ptr = io_region_get_ptr(&ctx->ring_region);
-	ctx->rings = rings = ptr;
-	ctx->cqes = ptr + config->dims.cq_offset;
 
-	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
-		ctx->sq_array = ptr + dims->sq_array_offset;
+	ctx->rings = io_region_slice(&ctx->ring_region, 0, sizeof(struct io_rings));
+	ctx->cqes = io_region_slice(&ctx->ring_region, dims->cq_offset, dims->cq_size);
+	if (!ctx->rings || !ctx->cqes)
+		return -EFAULT;
+
+	if (!(ctx->flags & IORING_SETUP_NO_SQARRAY)) {
+		ctx->sq_array = io_region_slice(&ctx->ring_region,
+						dims->sq_array_offset,
+						dims->sq_array_size);
+		if (!ctx->sq_array)
+			return -EFAULT;
+	}
 
 	memset(&rd, 0, sizeof(rd));
 	rd.size = PAGE_ALIGN(dims->sq_size);
@@ -3409,8 +3408,12 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
 		io_rings_free(ctx);
 		return ret;
 	}
-	ctx->sq_sqes = io_region_get_ptr(&ctx->sq_region);
 
+	ctx->sq_sqes = io_region_slice(&ctx->sq_region, 0, dims->sq_size);
+	if (!ctx->sq_sqes)
+		return -EFAULT;
+
+	rings = ctx->rings;
 	memset(rings, 0, sizeof(*rings));
 	WRITE_ONCE(rings->sq_ring_mask, ctx->sq_entries - 1);
 	WRITE_ONCE(rings->cq_ring_mask, ctx->cq_entries - 1);
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index 80228c5a843c..ed57ab4161db 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -19,8 +19,10 @@
 
 struct io_scq_dim {
 	size_t sq_array_offset;
+	size_t sq_array_size;
 	size_t sq_size;
 	size_t cq_offset;
+	size_t cq_size;
 
 	/* Compound array mmap'ed together with CQ. */
 	size_t cq_comp_size;
-- 
2.49.0


  parent reply	other threads:[~2025-11-06 17:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-06 17:01 [RFC 00/16] Introduce ring flexible placement Pavel Begunkov
2025-11-06 17:01 ` [RFC 01/16] io_uring: add helper calculating region byte size Pavel Begunkov
2025-11-06 17:01 ` [RFC 02/16] io_uring: pass sq entires in the params struct Pavel Begunkov
2025-11-06 17:01 ` [RFC 03/16] io_uring: use mem_is_zero to check ring params Pavel Begunkov
2025-11-06 17:01 ` [RFC 04/16] io_uring: move flags check to io_uring_sanitise_params Pavel Begunkov
2025-11-06 17:01 ` [RFC 05/16] io_uring: introduce struct io_ctx_config Pavel Begunkov
2025-11-06 17:01 ` [RFC 06/16] io_uring: split out config init helper Pavel Begunkov
2025-11-06 17:01 ` [RFC 07/16] io_uring: add structure keeping ring offsets Pavel Begunkov
2025-11-06 17:01 ` [RFC 08/16] io_uring: pre-calculate scq offsets Pavel Begunkov
2025-11-06 17:01 ` [RFC 09/16] io_uring: inroduce helper for setting user offset Pavel Begunkov
2025-11-06 17:01 ` [RFC 10/16] io_uring: separate cqe array from headers Pavel Begunkov
2025-11-06 17:01 ` [RFC 11/16] io_uring/region: introduce io_region_slice Pavel Begunkov
2025-11-06 17:01 ` Pavel Begunkov [this message]
2025-11-06 17:01 ` [RFC 13/16] io_uring: refactor rings_size() Pavel Begunkov
2025-11-06 17:01 ` [RFC 14/16] io_uring: extract io_create_mem_region Pavel Begunkov
2025-11-06 17:01 ` [RFC 15/16] io_uring: allow creating mem region at setup Pavel Begunkov
2025-11-06 17:01 ` [RFC 16/16] io_uring: introduce SCQ placement Pavel Begunkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a98d764a5883e116f1f6a3166800db2ba1623b0d.1762447538.git.asml.silence@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=io-uring@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox