Skip to content

Commit 405d9b3

Browse files
author
Ming Lei
committed
io_uring: restore back registered wait arguments
JIRA: https://issues.redhat.com/browse/RHEL-106845 commit d617b31 Author: Pavel Begunkov <asml.silence@gmail.com> Date: Fri Nov 15 16:54:43 2024 +0000 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> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 43bc7cc commit 405d9b3

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
@@ -679,6 +679,11 @@ struct io_uring_region_desc {
679679
__u64 __resv[4];
680680
};
681681

682+
enum {
683+
/* expose the region as registered wait arguments */
684+
IORING_MEM_REGION_REG_WAIT_ARG = 1,
685+
};
686+
682687
struct io_uring_mem_region_reg {
683688
__u64 region_uptr; /* struct io_uring_region_desc * */
684689
__u64 flags;

io_uring/io_uring.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3206,7 +3206,19 @@ void __io_uring_cancel(bool cancel_all)
32063206
static struct io_uring_reg_wait *io_get_ext_arg_reg(struct io_ring_ctx *ctx,
32073207
const struct io_uring_getevents_arg __user *uarg)
32083208
{
3209-
return ERR_PTR(-EFAULT);
3209+
unsigned long size = sizeof(struct io_uring_reg_wait);
3210+
unsigned long offset = (uintptr_t)uarg;
3211+
unsigned long end;
3212+
3213+
if (unlikely(offset % sizeof(long)))
3214+
return ERR_PTR(-EFAULT);
3215+
3216+
/* also protects from NULL ->cq_wait_arg as the size would be 0 */
3217+
if (unlikely(check_add_overflow(offset, size, &end) ||
3218+
end > ctx->cq_wait_size))
3219+
return ERR_PTR(-EFAULT);
3220+
3221+
return ctx->cq_wait_arg + offset;
32103222
}
32113223

32123224
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)