Skip to content

Commit 5106dd6

Browse files
committed
io_uring: propagate issue_flags state down to file assignment
We'll need this in a future patch, when we could be assigning the file after the prep stage. While at it, get rid of the io_file_get() helper, it just makes the code harder to read. Signed-off-by: Jens Axboe <[email protected]>
1 parent 584b018 commit 5106dd6

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

fs/io_uring.c

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,9 @@ static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
11831183
struct io_uring_rsrc_update2 *up,
11841184
unsigned nr_args);
11851185
static void io_clean_op(struct io_kiocb *req);
1186-
static struct file *io_file_get(struct io_ring_ctx *ctx,
1187-
struct io_kiocb *req, int fd, bool fixed);
1186+
static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
1187+
unsigned issue_flags);
1188+
static inline struct file *io_file_get_normal(struct io_kiocb *req, int fd);
11881189
static void __io_queue_sqe(struct io_kiocb *req);
11891190
static void io_rsrc_put_work(struct work_struct *work);
11901191

@@ -1314,13 +1315,20 @@ static void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
13141315
}
13151316

13161317
static inline void io_req_set_rsrc_node(struct io_kiocb *req,
1317-
struct io_ring_ctx *ctx)
1318+
struct io_ring_ctx *ctx,
1319+
unsigned int issue_flags)
13181320
{
13191321
if (!req->fixed_rsrc_refs) {
13201322
req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
1321-
ctx->rsrc_cached_refs--;
1322-
if (unlikely(ctx->rsrc_cached_refs < 0))
1323-
io_rsrc_refs_refill(ctx);
1323+
1324+
if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1325+
lockdep_assert_held(&ctx->uring_lock);
1326+
ctx->rsrc_cached_refs--;
1327+
if (unlikely(ctx->rsrc_cached_refs < 0))
1328+
io_rsrc_refs_refill(ctx);
1329+
} else {
1330+
percpu_ref_get(req->fixed_rsrc_refs);
1331+
}
13241332
}
13251333
}
13261334

@@ -3330,7 +3338,8 @@ static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter
33303338
return 0;
33313339
}
33323340

3333-
static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3341+
static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
3342+
unsigned int issue_flags)
33343343
{
33353344
struct io_mapped_ubuf *imu = req->imu;
33363345
u16 index, buf_index = req->buf_index;
@@ -3340,7 +3349,7 @@ static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
33403349

33413350
if (unlikely(buf_index >= ctx->nr_user_bufs))
33423351
return -EFAULT;
3343-
io_req_set_rsrc_node(req, ctx);
3352+
io_req_set_rsrc_node(req, ctx, issue_flags);
33443353
index = array_index_nospec(buf_index, ctx->nr_user_bufs);
33453354
imu = READ_ONCE(ctx->user_bufs[index]);
33463355
req->imu = imu;
@@ -3502,7 +3511,7 @@ static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req,
35023511
ssize_t ret;
35033512

35043513
if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
3505-
ret = io_import_fixed(req, rw, iter);
3514+
ret = io_import_fixed(req, rw, iter, issue_flags);
35063515
if (ret)
35073516
return ERR_PTR(ret);
35083517
return NULL;
@@ -4394,8 +4403,10 @@ static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
43944403
if (issue_flags & IO_URING_F_NONBLOCK)
43954404
return -EAGAIN;
43964405

4397-
in = io_file_get(req->ctx, req, sp->splice_fd_in,
4398-
(sp->flags & SPLICE_F_FD_IN_FIXED));
4406+
if (sp->flags & SPLICE_F_FD_IN_FIXED)
4407+
in = io_file_get_fixed(req, sp->splice_fd_in, IO_URING_F_UNLOCKED);
4408+
else
4409+
in = io_file_get_normal(req, sp->splice_fd_in);
43994410
if (!in) {
44004411
ret = -EBADF;
44014412
goto done;
@@ -4434,8 +4445,10 @@ static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
44344445
if (issue_flags & IO_URING_F_NONBLOCK)
44354446
return -EAGAIN;
44364447

4437-
in = io_file_get(req->ctx, req, sp->splice_fd_in,
4438-
(sp->flags & SPLICE_F_FD_IN_FIXED));
4448+
if (sp->flags & SPLICE_F_FD_IN_FIXED)
4449+
in = io_file_get_fixed(req, sp->splice_fd_in, IO_URING_F_UNLOCKED);
4450+
else
4451+
in = io_file_get_normal(req, sp->splice_fd_in);
44394452
if (!in) {
44404453
ret = -EBADF;
44414454
goto done;
@@ -5973,7 +5986,7 @@ static void io_poll_remove_entries(struct io_kiocb *req)
59735986
* either spurious wakeup or multishot CQE is served. 0 when it's done with
59745987
* the request, then the mask is stored in req->result.
59755988
*/
5976-
static int io_poll_check_events(struct io_kiocb *req)
5989+
static int io_poll_check_events(struct io_kiocb *req, bool locked)
59775990
{
59785991
struct io_ring_ctx *ctx = req->ctx;
59795992
struct io_poll_iocb *poll = io_poll_get_single(req);
@@ -6030,7 +6043,7 @@ static void io_poll_task_func(struct io_kiocb *req, bool *locked)
60306043
struct io_ring_ctx *ctx = req->ctx;
60316044
int ret;
60326045

6033-
ret = io_poll_check_events(req);
6046+
ret = io_poll_check_events(req, *locked);
60346047
if (ret > 0)
60356048
return;
60366049

@@ -6055,7 +6068,7 @@ static void io_apoll_task_func(struct io_kiocb *req, bool *locked)
60556068
struct io_ring_ctx *ctx = req->ctx;
60566069
int ret;
60576070

6058-
ret = io_poll_check_events(req);
6071+
ret = io_poll_check_events(req, *locked);
60596072
if (ret > 0)
60606073
return;
60616074

@@ -7460,46 +7473,43 @@ static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file
74607473
file_slot->file_ptr = file_ptr;
74617474
}
74627475

7463-
static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
7464-
struct io_kiocb *req, int fd)
7476+
static inline struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
7477+
unsigned int issue_flags)
74657478
{
7466-
struct file *file;
7479+
struct io_ring_ctx *ctx = req->ctx;
7480+
struct file *file = NULL;
74677481
unsigned long file_ptr;
74687482

7483+
if (issue_flags & IO_URING_F_UNLOCKED)
7484+
mutex_lock(&ctx->uring_lock);
7485+
74697486
if (unlikely((unsigned int)fd >= ctx->nr_user_files))
7470-
return NULL;
7487+
goto out;
74717488
fd = array_index_nospec(fd, ctx->nr_user_files);
74727489
file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
74737490
file = (struct file *) (file_ptr & FFS_MASK);
74747491
file_ptr &= ~FFS_MASK;
74757492
/* mask in overlapping REQ_F and FFS bits */
74767493
req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT);
7477-
io_req_set_rsrc_node(req, ctx);
7494+
io_req_set_rsrc_node(req, ctx, 0);
7495+
out:
7496+
if (issue_flags & IO_URING_F_UNLOCKED)
7497+
mutex_unlock(&ctx->uring_lock);
74787498
return file;
74797499
}
74807500

7481-
static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
7482-
struct io_kiocb *req, int fd)
7501+
static struct file *io_file_get_normal(struct io_kiocb *req, int fd)
74837502
{
74847503
struct file *file = fget(fd);
74857504

7486-
trace_io_uring_file_get(ctx, req, req->user_data, fd);
7505+
trace_io_uring_file_get(req->ctx, req, req->user_data, fd);
74877506

74887507
/* we don't allow fixed io_uring files */
74897508
if (file && unlikely(file->f_op == &io_uring_fops))
74907509
io_req_track_inflight(req);
74917510
return file;
74927511
}
74937512

7494-
static inline struct file *io_file_get(struct io_ring_ctx *ctx,
7495-
struct io_kiocb *req, int fd, bool fixed)
7496-
{
7497-
if (fixed)
7498-
return io_file_get_fixed(ctx, req, fd);
7499-
else
7500-
return io_file_get_normal(ctx, req, fd);
7501-
}
7502-
75037513
static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
75047514
{
75057515
struct io_kiocb *prev = req->timeout.prev;
@@ -7749,8 +7759,10 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
77497759
blk_start_plug_nr_ios(&state->plug, state->submit_nr);
77507760
}
77517761

7752-
req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
7753-
(sqe_flags & IOSQE_FIXED_FILE));
7762+
if (req->flags & REQ_F_FIXED_FILE)
7763+
req->file = io_file_get_fixed(req, READ_ONCE(sqe->fd), 0);
7764+
else
7765+
req->file = io_file_get_normal(req, READ_ONCE(sqe->fd));
77547766
if (unlikely(!req->file))
77557767
return -EBADF;
77567768
}

0 commit comments

Comments
 (0)