From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com
Subject: [PATCH 2/7] io_uring: sanity check sizes before attempting allocation
Date: Thu, 16 Oct 2025 14:23:18 +0100 [thread overview]
Message-ID: <902410e76d5bd6e6be991de1dfbcb9e2fbb2bdb2.1760620698.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1760620698.git.asml.silence@gmail.com>
It's a good practice to validate parameters before doing any heavy stuff
like queue allocations. Do that for io_allocate_scq_urings().
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
io_uring/io_uring.c | 28 ++++++++++++----------------
1 file changed, 12 insertions(+), 16 deletions(-)
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index f9fc297e2fce..1e8566b39b52 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -3606,21 +3606,27 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
{
struct io_uring_region_desc rd;
struct io_rings *rings;
- size_t size, sq_array_offset;
- size_t sqe_size;
+ size_t sq_array_offset;
+ size_t sq_size, cq_size, sqe_size;
int ret;
/* make sure these are sane, as we already accounted them */
ctx->sq_entries = p->sq_entries;
ctx->cq_entries = p->cq_entries;
- size = rings_size(ctx->flags, p->sq_entries, p->cq_entries,
+ sqe_size = sizeof(struct io_uring_sqe);
+ if (p->flags & IORING_SETUP_SQE128)
+ sqe_size *= 2;
+ sq_size = array_size(sqe_size, p->sq_entries);
+ if (sq_size == SIZE_MAX)
+ return -EOVERFLOW;
+ cq_size = rings_size(ctx->flags, p->sq_entries, p->cq_entries,
&sq_array_offset);
- if (size == SIZE_MAX)
+ if (cq_size == SIZE_MAX)
return -EOVERFLOW;
memset(&rd, 0, sizeof(rd));
- rd.size = PAGE_ALIGN(size);
+ rd.size = PAGE_ALIGN(cq_size);
if (ctx->flags & IORING_SETUP_NO_MMAP) {
rd.user_addr = p->cq_off.user_addr;
rd.flags |= IORING_MEM_REGION_TYPE_USER;
@@ -3637,18 +3643,8 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
rings->sq_ring_entries = p->sq_entries;
rings->cq_ring_entries = p->cq_entries;
- sqe_size = sizeof(struct io_uring_sqe);
- if (p->flags & IORING_SETUP_SQE128)
- sqe_size *= 2;
-
- size = array_size(sqe_size, p->sq_entries);
- if (size == SIZE_MAX) {
- io_rings_free(ctx);
- return -EOVERFLOW;
- }
-
memset(&rd, 0, sizeof(rd));
- rd.size = PAGE_ALIGN(size);
+ rd.size = PAGE_ALIGN(sq_size);
if (ctx->flags & IORING_SETUP_NO_MMAP) {
rd.user_addr = p->sq_off.user_addr;
rd.flags |= IORING_MEM_REGION_TYPE_USER;
--
2.49.0
next prev parent reply other threads:[~2025-10-16 13:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-16 13:23 [PATCH 0/7] random region / rings cleanups Pavel Begunkov
2025-10-16 13:23 ` [PATCH 1/7] io_uring: deduplicate array_size in io_allocate_scq_urings Pavel Begunkov
2025-10-16 13:23 ` Pavel Begunkov [this message]
2025-10-16 13:23 ` [PATCH 3/7] io_uring: use no mmap safe region helpers on resizing Pavel Begunkov
2025-10-16 13:23 ` [PATCH 4/7] io_uring: remove extra args from io_register_free_rings Pavel Begunkov
2025-10-16 13:23 ` [PATCH 5/7] io_uring: don't free never created regions Pavel Begunkov
2025-10-16 13:23 ` [PATCH 6/7] io_uring/kbuf: use io_create_region for kbuf creation Pavel Begunkov
2025-10-16 13:23 ` [PATCH 7/7] io_uring: only publish fully handled mem region Pavel Begunkov
2025-10-17 21:01 ` [PATCH 0/7] random region / rings cleanups Gabriel Krisman Bertazi
2025-10-20 16:38 ` Jens Axboe
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=902410e76d5bd6e6be991de1dfbcb9e2fbb2bdb2.1760620698.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