Skip to content

Commit d617b31

Browse files
isilenceaxboe
authored andcommitted
io_uring: restore back registered wait arguments
Now we've got a more generic region registration API, place IORING_ENTER_EXT_ARG_REG and re-enable it. First, the user has to register a region with the IORING_MEM_REGION_REG_WAIT_ARG flag set. It can only be done for a ring in a disabled state, aka IORING_SETUP_R_DISABLED, to avoid races with already running waiters. With that we should have stable constant values for ctx->cq_wait_{size,arg} in io_get_ext_arg_reg() and hence no READ_ONCE required. The other API difference is that we're now passing byte offsets instead of indexes. The user _must_ align all offsets / pointers to the native word size, failing to do so might but not necessarily has to lead to a failure usually returned as -EFAULT. liburing will be hiding this details from users. Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Link: https://lore.kernel.org/r/81822c1b4ffbe8ad391b4f9ad1564def0d26d990.1731689588.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 93238e6 commit d617b31

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

include/linux/io_uring_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ struct io_ring_ctx {
324324
unsigned cq_entries;
325325
struct io_ev_fd __rcu *io_ev_fd;
326326
unsigned cq_extra;
327+
328+
void *cq_wait_arg;
329+
size_t cq_wait_size;
327330
} ____cacheline_aligned_in_smp;
328331

329332
/*

include/uapi/linux/io_uring.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ struct io_uring_region_desc {
663663
__u64 __resv[4];
664664
};
665665

666+
enum {
667+
/* expose the region as registered wait arguments */
668+
IORING_MEM_REGION_REG_WAIT_ARG = 1,
669+
};
670+
666671
struct io_uring_mem_region_reg {
667672
__u64 region_uptr; /* struct io_uring_region_desc * */
668673
__u64 flags;

io_uring/io_uring.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3195,7 +3195,19 @@ void __io_uring_cancel(bool cancel_all)
31953195
static struct io_uring_reg_wait *io_get_ext_arg_reg(struct io_ring_ctx *ctx,
31963196
const struct io_uring_getevents_arg __user *uarg)
31973197
{
3198-
return ERR_PTR(-EFAULT);
3198+
unsigned long size = sizeof(struct io_uring_reg_wait);
3199+
unsigned long offset = (uintptr_t)uarg;
3200+
unsigned long end;
3201+
3202+
if (unlikely(offset % sizeof(long)))
3203+
return ERR_PTR(-EFAULT);
3204+
3205+
/* also protects from NULL ->cq_wait_arg as the size would be 0 */
3206+
if (unlikely(check_add_overflow(offset, size, &end) ||
3207+
end > ctx->cq_wait_size))
3208+
return ERR_PTR(-EFAULT);
3209+
3210+
return ctx->cq_wait_arg + offset;
31993211
}
32003212

32013213
static int io_validate_ext_arg(struct io_ring_ctx *ctx, unsigned flags,

io_uring/register.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,16 @@ static int io_register_mem_region(struct io_ring_ctx *ctx, void __user *uarg)
588588

589589
if (memchr_inv(&reg.__resv, 0, sizeof(reg.__resv)))
590590
return -EINVAL;
591-
if (reg.flags)
591+
if (reg.flags & ~IORING_MEM_REGION_REG_WAIT_ARG)
592+
return -EINVAL;
593+
594+
/*
595+
* This ensures there are no waiters. Waiters are unlocked and it's
596+
* hard to synchronise with them, especially if we need to initialise
597+
* the region.
598+
*/
599+
if ((reg.flags & IORING_MEM_REGION_REG_WAIT_ARG) &&
600+
!(ctx->flags & IORING_SETUP_R_DISABLED))
592601
return -EINVAL;
593602

594603
ret = io_create_region(ctx, &ctx->param_region, &rd);
@@ -598,6 +607,11 @@ static int io_register_mem_region(struct io_ring_ctx *ctx, void __user *uarg)
598607
io_free_region(ctx, &ctx->param_region);
599608
return -EFAULT;
600609
}
610+
611+
if (reg.flags & IORING_MEM_REGION_REG_WAIT_ARG) {
612+
ctx->cq_wait_arg = io_region_get_ptr(&ctx->param_region);
613+
ctx->cq_wait_size = rd.size;
614+
}
601615
return 0;
602616
}
603617

0 commit comments

Comments
 (0)