Skip to content

Commit c5e9f6a

Browse files
committed
io_uring: unify getting ctx from passed in file descriptor
io_uring_enter() and io_uring_register() end up having duplicated code for getting a ctx from a passed in file descriptor, for either a registered ring descriptor or a normal file descriptor. Move the io_uring_register_get_file() into io_uring.c and name it a bit more generically, and use it from both callsites rather than have that logic and handling duplicated. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b4d893d commit c5e9f6a

6 files changed

Lines changed: 40 additions & 58 deletions

File tree

io_uring/bpf-ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int bpf_io_reg(void *kdata, struct bpf_link *link)
181181
struct file *file;
182182
int ret = -EBUSY;
183183

184-
file = io_uring_register_get_file(ops->ring_fd, false);
184+
file = io_uring_ctx_get_file(ops->ring_fd, false);
185185
if (IS_ERR(file))
186186
return PTR_ERR(file);
187187
ctx = file->private_data;

io_uring/io_uring.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,39 +2543,54 @@ static int io_get_ext_arg(struct io_ring_ctx *ctx, unsigned flags,
25432543
#endif
25442544
}
25452545

2546-
SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2547-
u32, min_complete, u32, flags, const void __user *, argp,
2548-
size_t, argsz)
2546+
/*
2547+
* Given an 'fd' value, return the ctx associated with if. If 'registered' is
2548+
* true, then the registered index is used. Otherwise, the normal fd table.
2549+
* Caller must call fput() on the returned file if it isn't a registered file,
2550+
* unless it's an ERR_PTR.
2551+
*/
2552+
struct file *io_uring_ctx_get_file(unsigned int fd, bool registered)
25492553
{
2550-
struct io_ring_ctx *ctx;
25512554
struct file *file;
2552-
long ret;
2553-
2554-
if (unlikely(flags & ~IORING_ENTER_FLAGS))
2555-
return -EINVAL;
25562555

2557-
/*
2558-
* Ring fd has been registered via IORING_REGISTER_RING_FDS, we
2559-
* need only dereference our task private array to find it.
2560-
*/
2561-
if (flags & IORING_ENTER_REGISTERED_RING) {
2556+
if (registered) {
2557+
/*
2558+
* Ring fd has been registered via IORING_REGISTER_RING_FDS, we
2559+
* need only dereference our task private array to find it.
2560+
*/
25622561
struct io_uring_task *tctx = current->io_uring;
25632562

25642563
if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
2565-
return -EINVAL;
2564+
return ERR_PTR(-EINVAL);
25662565
fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
25672566
file = tctx->registered_rings[fd];
2568-
if (unlikely(!file))
2569-
return -EBADF;
25702567
} else {
25712568
file = fget(fd);
2572-
if (unlikely(!file))
2573-
return -EBADF;
2574-
ret = -EOPNOTSUPP;
2575-
if (unlikely(!io_is_uring_fops(file)))
2576-
goto out;
25772569
}
25782570

2571+
if (unlikely(!file))
2572+
return ERR_PTR(-EBADF);
2573+
if (io_is_uring_fops(file))
2574+
return file;
2575+
fput(file);
2576+
return ERR_PTR(-EOPNOTSUPP);
2577+
}
2578+
2579+
2580+
SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2581+
u32, min_complete, u32, flags, const void __user *, argp,
2582+
size_t, argsz)
2583+
{
2584+
struct io_ring_ctx *ctx;
2585+
struct file *file;
2586+
long ret;
2587+
2588+
if (unlikely(flags & ~IORING_ENTER_FLAGS))
2589+
return -EINVAL;
2590+
2591+
file = io_uring_ctx_get_file(fd, flags & IORING_ENTER_REGISTERED_RING);
2592+
if (IS_ERR(file))
2593+
return PTR_ERR(file);
25792594
ctx = file->private_data;
25802595
ret = -EBADFD;
25812596
/*

io_uring/io_uring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ void io_req_track_inflight(struct io_kiocb *req);
173173
struct file *io_file_get_normal(struct io_kiocb *req, int fd);
174174
struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
175175
unsigned issue_flags);
176+
struct file *io_uring_ctx_get_file(unsigned int fd, bool registered);
176177

177178
void io_req_task_queue(struct io_kiocb *req);
178179
void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw);

io_uring/register.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -938,39 +938,6 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
938938
return ret;
939939
}
940940

941-
/*
942-
* Given an 'fd' value, return the ctx associated with if. If 'registered' is
943-
* true, then the registered index is used. Otherwise, the normal fd table.
944-
* Caller must call fput() on the returned file if it isn't a registered file,
945-
* unless it's an ERR_PTR.
946-
*/
947-
struct file *io_uring_register_get_file(unsigned int fd, bool registered)
948-
{
949-
struct file *file;
950-
951-
if (registered) {
952-
/*
953-
* Ring fd has been registered via IORING_REGISTER_RING_FDS, we
954-
* need only dereference our task private array to find it.
955-
*/
956-
struct io_uring_task *tctx = current->io_uring;
957-
958-
if (unlikely(!tctx || fd >= IO_RINGFD_REG_MAX))
959-
return ERR_PTR(-EINVAL);
960-
fd = array_index_nospec(fd, IO_RINGFD_REG_MAX);
961-
file = tctx->registered_rings[fd];
962-
} else {
963-
file = fget(fd);
964-
}
965-
966-
if (unlikely(!file))
967-
return ERR_PTR(-EBADF);
968-
if (io_is_uring_fops(file))
969-
return file;
970-
fput(file);
971-
return ERR_PTR(-EOPNOTSUPP);
972-
}
973-
974941
static int io_uring_register_send_msg_ring(void __user *arg, unsigned int nr_args)
975942
{
976943
struct io_uring_sqe sqe;
@@ -1025,7 +992,7 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
1025992
if (fd == -1)
1026993
return io_uring_register_blind(opcode, arg, nr_args);
1027994

1028-
file = io_uring_register_get_file(fd, use_registered_ring);
995+
file = io_uring_ctx_get_file(fd, use_registered_ring);
1029996
if (IS_ERR(file))
1030997
return PTR_ERR(file);
1031998
ctx = file->private_data;

io_uring/register.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44

55
int io_eventfd_unregister(struct io_ring_ctx *ctx);
66
int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id);
7-
struct file *io_uring_register_get_file(unsigned int fd, bool registered);
87

98
#endif

io_uring/rsrc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg)
12691269
return -EINVAL;
12701270

12711271
registered_src = (buf.flags & IORING_REGISTER_SRC_REGISTERED) != 0;
1272-
file = io_uring_register_get_file(buf.src_fd, registered_src);
1272+
file = io_uring_ctx_get_file(buf.src_fd, registered_src);
12731273
if (IS_ERR(file))
12741274
return PTR_ERR(file);
12751275

0 commit comments

Comments
 (0)