Skip to contentSkip to conversation

random setup cleanups

PATCH Applied Pavel Begunkov 2 participants 6 messages Nov 10, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

cover random setup cleanups

Simple cleanups extracted from another series. Nothing noteworthy apart from that I needed Patch 1 in a couple of places already, but it's still a good follow up to recent fixes.

Pavel Begunkov (4):
io_uring: add helper calculating region byte size
io_uring: pass sq entires in the params struct
io_uring: use mem_is_zero to check ring params
io_uring: move flags check to io_uring_sanitise_params

 io_uring/io_uring.c | 22 ++++++++++++----------
 io_uring/io_uring.h |  2 +-
 io_uring/memmap.c   |  4 ++--
 io_uring/memmap.h   |  5 +++++
 io_uring/register.c |  2 +-
 5 files changed, 21 insertions(+), 14 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/4 io_uring: add helper calculating region byte size

There has been type related issues with region size calculation, add an utility helper function that returns the size and handles type conversions right.

---
 io_uring/memmap.c | 4 ++--
 io_uring/memmap.h | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)
io_uring/memmap.c +2 -2
index 24da17a5f08f..dc4bfc5b6fb8 100644
@@ -134,7 +134,7 @@ static int io_region_init_ptr(struct io_mapped_region *mr)
 static int io_region_pin_pages(struct io_mapped_region *mr,
 			       struct io_uring_region_desc *reg)
 {
-	unsigned long size = mr->nr_pages << PAGE_SHIFT;
+	size_t size = io_region_size(mr);
 	struct page **pages;
 	int nr_pages;
 
@@ -154,7 +154,7 @@ static int io_region_allocate_pages(struct io_mapped_region *mr,
 				    unsigned long mmap_offset)
 {
 	gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN;
-	size_t size = (size_t) mr->nr_pages << PAGE_SHIFT;
+	size_t size = io_region_size(mr);
 	unsigned long nr_allocated;
 	struct page **pages;
 
io_uring/memmap.h +5 -0
index a6c63ca2c6f1..d2a940682d19 100644
@@ -43,4 +43,9 @@ static inline void io_region_publish(struct io_ring_ctx *ctx,
 	*dst_region = *src_region;
 }
 
+static inline size_t io_region_size(struct io_mapped_region *mr)
+{
+	return (size_t)mr->nr_pages << PAGE_SHIFT;
+}
+
 #endif
Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/4 io_uring: pass sq entires in the params struct

In reply to Pavel Begunkov

There is no need to pass the user requested number of SQ entries separately from the main parameter structure io_uring_params. Initialise it at the beginning and stop passing it in favour of struct io_uring_params::sq_entries.

---
 io_uring/io_uring.c | 11 +++++++----
 io_uring/io_uring.h |  2 +-
 io_uring/register.c |  2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)
io_uring/io_uring.c +7 -4
index d11d0e9723a1..023b0e3a829c 100644
@@ -3479,8 +3479,10 @@ static int io_uring_sanitise_params(struct io_uring_params *p)
 	return 0;
 }
 
-int io_uring_fill_params(unsigned entries, struct io_uring_params *p)
+int io_uring_fill_params(struct io_uring_params *p)
 {
+	unsigned entries = p->sq_entries;
+
 	if (!entries)
 		return -EINVAL;
 	if (entries > IORING_MAX_ENTRIES) {
@@ -3542,7 +3544,7 @@ int io_uring_fill_params(unsigned entries, struct io_uring_params *p)
 	return 0;
 }
 
-static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
+static __cold int io_uring_create(struct io_uring_params *p,
 				  struct io_uring_params __user *params)
 {
 	struct io_ring_ctx *ctx;
@@ -3554,7 +3556,7 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
 	if (ret)
 		return ret;
 
-	ret = io_uring_fill_params(entries, p);
+	ret = io_uring_fill_params(p);
 	if (unlikely(ret))
 		return ret;
 
@@ -3693,7 +3695,8 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 
 	if (p.flags & ~IORING_SETUP_FLAGS)
 		return -EINVAL;
-	return io_uring_create(entries, &p, params);
+	p.sq_entries = entries;
+	return io_uring_create(&p, params);
 }
 
 static inline int io_uring_allowed(void)
io_uring/io_uring.h +1 -1
index 23c268ab1c8f..b2251446497a 100644
@@ -136,7 +136,7 @@ static inline bool io_should_wake(struct io_wait_queue *iowq)
 
 unsigned long rings_size(unsigned int flags, unsigned int sq_entries,
 			 unsigned int cq_entries, size_t *sq_offset);
-int io_uring_fill_params(unsigned entries, struct io_uring_params *p);
+int io_uring_fill_params(struct io_uring_params *p);
 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32);
 int io_run_task_work_sig(struct io_ring_ctx *ctx);
 int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events);
io_uring/register.c +1 -1
index 023f5e7a18da..afb924ceb9b6 100644
@@ -416,7 +416,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
 	/* properties that are always inherited */
 	p.flags |= (ctx->flags & COPY_FLAGS);
 
-	ret = io_uring_fill_params(p.sq_entries, &p);
+	ret = io_uring_fill_params(&p);
 	if (unlikely(ret))
 		return ret;
 
Signature · 1 line
2.49.0
#3 lore raw

PATCH 3/4 io_uring: use mem_is_zero to check ring params

In reply to Pavel Begunkov

mem_is_zero() does the job without hand rolled loops, use that to verify reserved fields of ring params.

---
 io_uring/io_uring.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
io_uring/io_uring.c +3 -5
index 023b0e3a829c..af7b4cbe9850 100644
@@ -3684,14 +3684,12 @@ static __cold int io_uring_create(struct io_uring_params *p,
 static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 {
 	struct io_uring_params p;
-	int i;
 
 	if (copy_from_user(&p, params, sizeof(p)))
 		return -EFAULT;
-	for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
-		if (p.resv[i])
-			return -EINVAL;
-	}
+
+	if (!mem_is_zero(&p.resv, sizeof(p.resv)))
+		return -EINVAL;
 
 	if (p.flags & ~IORING_SETUP_FLAGS)
 		return -EINVAL;
Signature · 1 line
2.49.0
#4 lore raw

PATCH 4/4 io_uring: move flags check to io_uring_sanitise_params

In reply to Pavel Begunkov

io_uring_sanitise_params() sanitises most of the setup flags invariants, move the IORING_SETUP_FLAGS check from io_uring_setup() into it.

---
 io_uring/io_uring.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
io_uring/io_uring.c +3 -2
index af7b4cbe9850..7e069d56b8a1 100644
@@ -3430,6 +3430,9 @@ static int io_uring_sanitise_params(struct io_uring_params *p)
 {
 	unsigned flags = p->flags;
 
+	if (flags & ~IORING_SETUP_FLAGS)
+		return -EINVAL;
+
 	/* There is no way to mmap rings without a real fd */
 	if ((flags & IORING_SETUP_REGISTERED_FD_ONLY) &&
 	    !(flags & IORING_SETUP_NO_MMAP))
@@ -3691,8 +3694,6 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
 	if (!mem_is_zero(&p.resv, sizeof(p.resv)))
 		return -EINVAL;
 
-	if (p.flags & ~IORING_SETUP_FLAGS)
-		return -EINVAL;
 	p.sq_entries = entries;
 	return io_uring_create(&p, params);
 }
Signature · 1 line
2.49.0
maintainer Applied
#5 lore raw
In reply to Pavel Begunkov
Quoted 11 lines · Pavel Begunkov

Simple cleanups extracted from another series. Nothing noteworthy apart from that I needed Patch 1 in a couple of places already, but it's still a good follow up to recent fixes.

Pavel Begunkov (4):
io_uring: add helper calculating region byte size
io_uring: pass sq entires in the params struct
io_uring: use mem_is_zero to check ring params
io_uring: move flags check to io_uring_sanitise_params

[...]

Applied, thanks!

[1/4] io_uring: add helper calculating region byte size

      commit: c5b09a6d1dfab3ac97230cc91a32d19a8f692232
[2/4] io_uring: pass sq entires in the params struct
      (no commit info)
[3/4] io_uring: use mem_is_zero to check ring params
      commit: 44d5ba73f6147b36316c713094ef56a7d93e5076
[4/4] io_uring: move flags check to io_uring_sanitise_params
      commit: 4d7352becf5609047112f91883071a289c44e221

Best regards,

Signature · 1 line
Jens Axboe