Skip to content

Commit eb76ff6

Browse files
isilenceaxboe
authored andcommitted
io_uring: pre-calculate scq layout
Move ring layouts calculations into io_prepare_config(), so that more misconfiguration checking can be done earlier before creating a ctx. It also deduplicates some code with ring resizing. And as a bonus, now it initialises params->sq_off.array, which is closer to all other user offset init, and also applies it to ring resizing, which was previously missing it. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 001b76b commit eb76ff6

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

io_uring/io_uring.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,8 @@ static void io_rings_free(struct io_ring_ctx *ctx)
27572757
ctx->sq_sqes = NULL;
27582758
}
27592759

2760-
int rings_size(unsigned int flags, unsigned int sq_entries,
2761-
unsigned int cq_entries, struct io_rings_layout *rl)
2760+
static int rings_size(unsigned int flags, unsigned int sq_entries,
2761+
unsigned int cq_entries, struct io_rings_layout *rl)
27622762
{
27632763
struct io_rings *rings;
27642764
size_t sqe_size;
@@ -3353,21 +3353,18 @@ bool io_is_uring_fops(struct file *file)
33533353
}
33543354

33553355
static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3356-
struct io_uring_params *p)
3356+
struct io_ctx_config *config)
33573357
{
3358+
struct io_uring_params *p = &config->p;
3359+
struct io_rings_layout *rl = &config->layout;
33583360
struct io_uring_region_desc rd;
3359-
struct io_rings_layout __rl, *rl = &__rl;
33603361
struct io_rings *rings;
33613362
int ret;
33623363

33633364
/* make sure these are sane, as we already accounted them */
33643365
ctx->sq_entries = p->sq_entries;
33653366
ctx->cq_entries = p->cq_entries;
33663367

3367-
ret = rings_size(ctx->flags, p->sq_entries, p->cq_entries, rl);
3368-
if (ret)
3369-
return ret;
3370-
33713368
memset(&rd, 0, sizeof(rd));
33723369
rd.size = PAGE_ALIGN(rl->rings_size);
33733370
if (ctx->flags & IORING_SETUP_NO_MMAP) {
@@ -3378,7 +3375,6 @@ static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx,
33783375
if (ret)
33793376
return ret;
33803377
ctx->rings = rings = io_region_get_ptr(&ctx->ring_region);
3381-
33823378
if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
33833379
ctx->sq_array = (u32 *)((char *)rings + rl->sq_array_offset);
33843380

@@ -3560,6 +3556,14 @@ int io_prepare_config(struct io_ctx_config *config)
35603556
if (ret)
35613557
return ret;
35623558

3559+
ret = rings_size(p->flags, p->sq_entries, p->cq_entries,
3560+
&config->layout);
3561+
if (ret)
3562+
return ret;
3563+
3564+
if (!(p->flags & IORING_SETUP_NO_SQARRAY))
3565+
p->sq_off.array = config->layout.sq_array_offset;
3566+
35633567
return 0;
35643568
}
35653569

@@ -3632,13 +3636,10 @@ static __cold int io_uring_create(struct io_ctx_config *config)
36323636
mmgrab(current->mm);
36333637
ctx->mm_account = current->mm;
36343638

3635-
ret = io_allocate_scq_urings(ctx, p);
3639+
ret = io_allocate_scq_urings(ctx, config);
36363640
if (ret)
36373641
goto err;
36383642

3639-
if (!(p->flags & IORING_SETUP_NO_SQARRAY))
3640-
p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
3641-
36423643
ret = io_sq_offload_create(ctx, p);
36433644
if (ret)
36443645
goto err;

io_uring/io_uring.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct io_rings_layout {
2727

2828
struct io_ctx_config {
2929
struct io_uring_params p;
30+
struct io_rings_layout layout;
3031
struct io_uring_params __user *uptr;
3132
};
3233

@@ -147,8 +148,6 @@ static inline bool io_should_wake(struct io_wait_queue *iowq)
147148
#define IORING_MAX_ENTRIES 32768
148149
#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
149150

150-
int rings_size(unsigned int flags, unsigned int sq_entries,
151-
unsigned int cq_entries, struct io_rings_layout *rl);
152151
int io_prepare_config(struct io_ctx_config *config);
153152

154153
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32);

io_uring/register.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,6 @@ static int io_register_resize_rings(struct io_ring_ctx *ctx, void __user *arg)
423423
if (unlikely(ret))
424424
return ret;
425425

426-
ret = rings_size(p->flags, p->sq_entries, p->cq_entries, rl);
427-
if (ret)
428-
return ret;
429-
430426
memset(&rd, 0, sizeof(rd));
431427
rd.size = PAGE_ALIGN(rl->rings_size);
432428
if (p->flags & IORING_SETUP_NO_MMAP) {

0 commit comments

Comments
 (0)