Skip to content

Commit 81d8191

Browse files
committed
io_uring: abstract out a bit of the ring filling logic
Abstract out a io_uring_fill_params() helper, which fills out the necessary bits of struct io_uring_params. Add it to io_uring.h as well, in preparation for having another internal user of it. Signed-off-by: Jens Axboe <[email protected]>
1 parent 09d0a8e commit 81d8191

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

io_uring/io_uring.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,14 +3498,8 @@ static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
34983498
O_RDWR | O_CLOEXEC, NULL);
34993499
}
35003500

3501-
static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
3502-
struct io_uring_params __user *params)
3501+
int io_uring_fill_params(unsigned entries, struct io_uring_params *p)
35033502
{
3504-
struct io_ring_ctx *ctx;
3505-
struct io_uring_task *tctx;
3506-
struct file *file;
3507-
int ret;
3508-
35093503
if (!entries)
35103504
return -EINVAL;
35113505
if (entries > IORING_MAX_ENTRIES) {
@@ -3547,6 +3541,42 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
35473541
p->cq_entries = 2 * p->sq_entries;
35483542
}
35493543

3544+
p->sq_off.head = offsetof(struct io_rings, sq.head);
3545+
p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3546+
p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3547+
p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3548+
p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3549+
p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3550+
p->sq_off.resv1 = 0;
3551+
if (!(p->flags & IORING_SETUP_NO_MMAP))
3552+
p->sq_off.user_addr = 0;
3553+
3554+
p->cq_off.head = offsetof(struct io_rings, cq.head);
3555+
p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3556+
p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3557+
p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3558+
p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3559+
p->cq_off.cqes = offsetof(struct io_rings, cqes);
3560+
p->cq_off.flags = offsetof(struct io_rings, cq_flags);
3561+
p->cq_off.resv1 = 0;
3562+
if (!(p->flags & IORING_SETUP_NO_MMAP))
3563+
p->cq_off.user_addr = 0;
3564+
3565+
return 0;
3566+
}
3567+
3568+
static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
3569+
struct io_uring_params __user *params)
3570+
{
3571+
struct io_ring_ctx *ctx;
3572+
struct io_uring_task *tctx;
3573+
struct file *file;
3574+
int ret;
3575+
3576+
ret = io_uring_fill_params(entries, p);
3577+
if (unlikely(ret))
3578+
return ret;
3579+
35503580
ctx = io_ring_ctx_alloc(p);
35513581
if (!ctx)
35523582
return -ENOMEM;
@@ -3630,6 +3660,9 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
36303660
if (ret)
36313661
goto err;
36323662

3663+
if (!(p->flags & IORING_SETUP_NO_SQARRAY))
3664+
p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
3665+
36333666
ret = io_sq_offload_create(ctx, p);
36343667
if (ret)
36353668
goto err;
@@ -3638,29 +3671,6 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,
36383671
if (ret)
36393672
goto err;
36403673

3641-
p->sq_off.head = offsetof(struct io_rings, sq.head);
3642-
p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3643-
p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3644-
p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3645-
p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3646-
p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3647-
if (!(ctx->flags & IORING_SETUP_NO_SQARRAY))
3648-
p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
3649-
p->sq_off.resv1 = 0;
3650-
if (!(ctx->flags & IORING_SETUP_NO_MMAP))
3651-
p->sq_off.user_addr = 0;
3652-
3653-
p->cq_off.head = offsetof(struct io_rings, cq.head);
3654-
p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3655-
p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3656-
p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3657-
p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3658-
p->cq_off.cqes = offsetof(struct io_rings, cqes);
3659-
p->cq_off.flags = offsetof(struct io_rings, cq_flags);
3660-
p->cq_off.resv1 = 0;
3661-
if (!(ctx->flags & IORING_SETUP_NO_MMAP))
3662-
p->cq_off.user_addr = 0;
3663-
36643674
p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
36653675
IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
36663676
IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |

io_uring/io_uring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static inline bool io_should_wake(struct io_wait_queue *iowq)
7070

7171
unsigned long rings_size(unsigned int flags, unsigned int sq_entries,
7272
unsigned int cq_entries, size_t *sq_offset);
73+
int io_uring_fill_params(unsigned entries, struct io_uring_params *p);
7374
bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow);
7475
int io_run_task_work_sig(struct io_ring_ctx *ctx);
7576
void io_req_defer_failed(struct io_kiocb *req, s32 res);

0 commit comments

Comments
 (0)