Skip to content

Commit 0f4b537

Browse files
isilenceaxboe
authored andcommitted
io_uring: introduce struct io_ctx_config
There will be more information needed during ctx setup, and instead of passing a handful of pointers around, wrap them all into a new structure. Add a helper for encapsulating all configuration checks and preparation, that's also reused for ring resizing. Note, it indirectly adds a io_uring_sanitise_params() check to ring resizing, which is a good thing. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 929dbbb commit 0f4b537

3 files changed

Lines changed: 40 additions & 16 deletions

File tree

io_uring/io_uring.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,7 +3480,7 @@ static int io_uring_sanitise_params(struct io_uring_params *p)
34803480
return 0;
34813481
}
34823482

3483-
int io_uring_fill_params(struct io_uring_params *p)
3483+
static int io_uring_fill_params(struct io_uring_params *p)
34843484
{
34853485
unsigned entries = p->sq_entries;
34863486

@@ -3545,20 +3545,32 @@ int io_uring_fill_params(struct io_uring_params *p)
35453545
return 0;
35463546
}
35473547

3548-
static __cold int io_uring_create(struct io_uring_params *p,
3549-
struct io_uring_params __user *params)
3548+
int io_prepare_config(struct io_ctx_config *config)
35503549
{
3551-
struct io_ring_ctx *ctx;
3552-
struct io_uring_task *tctx;
3553-
struct file *file;
3550+
struct io_uring_params *p = &config->p;
35543551
int ret;
35553552

35563553
ret = io_uring_sanitise_params(p);
35573554
if (ret)
35583555
return ret;
35593556

35603557
ret = io_uring_fill_params(p);
3561-
if (unlikely(ret))
3558+
if (ret)
3559+
return ret;
3560+
3561+
return 0;
3562+
}
3563+
3564+
static __cold int io_uring_create(struct io_ctx_config *config)
3565+
{
3566+
struct io_uring_params *p = &config->p;
3567+
struct io_ring_ctx *ctx;
3568+
struct io_uring_task *tctx;
3569+
struct file *file;
3570+
int ret;
3571+
3572+
ret = io_prepare_config(config);
3573+
if (ret)
35623574
return ret;
35633575

35643576
ctx = io_ring_ctx_alloc(p);
@@ -3631,7 +3643,7 @@ static __cold int io_uring_create(struct io_uring_params *p,
36313643

36323644
p->features = IORING_FEAT_FLAGS;
36333645

3634-
if (copy_to_user(params, p, sizeof(*p))) {
3646+
if (copy_to_user(config->uptr, p, sizeof(*p))) {
36353647
ret = -EFAULT;
36363648
goto err;
36373649
}
@@ -3684,16 +3696,19 @@ static __cold int io_uring_create(struct io_uring_params *p,
36843696
*/
36853697
static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
36863698
{
3687-
struct io_uring_params p;
3699+
struct io_ctx_config config;
3700+
3701+
memset(&config, 0, sizeof(config));
36883702

3689-
if (copy_from_user(&p, params, sizeof(p)))
3703+
if (copy_from_user(&config.p, params, sizeof(config.p)))
36903704
return -EFAULT;
36913705

3692-
if (!mem_is_zero(&p.resv, sizeof(p.resv)))
3706+
if (!mem_is_zero(&config.p.resv, sizeof(config.p.resv)))
36933707
return -EINVAL;
36943708

3695-
p.sq_entries = entries;
3696-
return io_uring_create(&p, params);
3709+
config.p.sq_entries = entries;
3710+
config.uptr = params;
3711+
return io_uring_create(&config);
36973712
}
36983713

36993714
static inline int io_uring_allowed(void)

io_uring/io_uring.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#include <trace/events/io_uring.h>
1818
#endif
1919

20+
struct io_ctx_config {
21+
struct io_uring_params p;
22+
struct io_uring_params __user *uptr;
23+
};
24+
2025
#define IORING_FEAT_FLAGS (IORING_FEAT_SINGLE_MMAP |\
2126
IORING_FEAT_NODROP |\
2227
IORING_FEAT_SUBMIT_STABLE |\
@@ -136,7 +141,8 @@ static inline bool io_should_wake(struct io_wait_queue *iowq)
136141

137142
unsigned long rings_size(unsigned int flags, unsigned int sq_entries,
138143
unsigned int cq_entries, size_t *sq_offset);
139-
int io_uring_fill_params(struct io_uring_params *p);
144+
int io_prepare_config(struct io_ctx_config *config);
145+
140146
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32);
141147
int io_run_task_work_sig(struct io_ring_ctx *ctx);
142148
int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events);

io_uring/register.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,16 @@ static void io_register_free_rings(struct io_ring_ctx *ctx,
398398

399399
static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
400400
{
401+
struct io_ctx_config config;
401402
struct io_uring_region_desc rd;
402403
struct io_ring_ctx_rings o = { }, n = { }, *to_free = NULL;
403404
size_t size, sq_array_offset;
404405
unsigned i, tail, old_head;
405-
struct io_uring_params __p, *p = &__p;
406+
struct io_uring_params *p = &config.p;
406407
int ret;
407408

409+
memset(&config, 0, sizeof(config));
410+
408411
/* limited to DEFER_TASKRUN for now */
409412
if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN))
410413
return -EINVAL;
@@ -416,7 +419,7 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
416419
/* properties that are always inherited */
417420
p->flags |= (ctx->flags & COPY_FLAGS);
418421

419-
ret = io_uring_fill_params(p);
422+
ret = io_prepare_config(&config);
420423
if (unlikely(ret))
421424
return ret;
422425

0 commit comments

Comments
 (0)