Skip to content

Commit 1137302

Browse files
isilenceaxboe
authored andcommitted
io_uring: get rid of double locking
We don't need to take both uring_locks at once, msg_ring can be split in two parts, first getting a file from the filetable of the first ring and then installing it into the second one. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/a80ecc2bc99c3b3f2cf20015d618b7c51419a797.1670384893.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 77e443a commit 1137302

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

io_uring/msg_ring.c

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
struct io_msg {
1717
struct file *file;
18+
struct file *src_file;
1819
u64 user_data;
1920
u32 len;
2021
u32 cmd;
@@ -23,6 +24,17 @@ struct io_msg {
2324
u32 flags;
2425
};
2526

27+
void io_msg_ring_cleanup(struct io_kiocb *req)
28+
{
29+
struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
30+
31+
if (WARN_ON_ONCE(!msg->src_file))
32+
return;
33+
34+
fput(msg->src_file);
35+
msg->src_file = NULL;
36+
}
37+
2638
static int io_msg_ring_data(struct io_kiocb *req)
2739
{
2840
struct io_ring_ctx *target_ctx = req->file->private_data;
@@ -37,17 +49,13 @@ static int io_msg_ring_data(struct io_kiocb *req)
3749
return -EOVERFLOW;
3850
}
3951

40-
static void io_double_unlock_ctx(struct io_ring_ctx *ctx,
41-
struct io_ring_ctx *octx,
52+
static void io_double_unlock_ctx(struct io_ring_ctx *octx,
4253
unsigned int issue_flags)
4354
{
44-
if (issue_flags & IO_URING_F_UNLOCKED)
45-
mutex_unlock(&ctx->uring_lock);
4655
mutex_unlock(&octx->uring_lock);
4756
}
4857

49-
static int io_double_lock_ctx(struct io_ring_ctx *ctx,
50-
struct io_ring_ctx *octx,
58+
static int io_double_lock_ctx(struct io_ring_ctx *octx,
5159
unsigned int issue_flags)
5260
{
5361
/*
@@ -60,52 +68,56 @@ static int io_double_lock_ctx(struct io_ring_ctx *ctx,
6068
return -EAGAIN;
6169
return 0;
6270
}
71+
mutex_lock(&octx->uring_lock);
72+
return 0;
73+
}
6374

64-
/* Always grab smallest value ctx first. We know ctx != octx. */
65-
if (ctx < octx) {
66-
mutex_lock(&ctx->uring_lock);
67-
mutex_lock(&octx->uring_lock);
68-
} else {
69-
mutex_lock(&octx->uring_lock);
70-
mutex_lock(&ctx->uring_lock);
75+
static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
76+
{
77+
struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
78+
struct io_ring_ctx *ctx = req->ctx;
79+
struct file *file = NULL;
80+
unsigned long file_ptr;
81+
int idx = msg->src_fd;
82+
83+
io_ring_submit_lock(ctx, issue_flags);
84+
if (likely(idx < ctx->nr_user_files)) {
85+
idx = array_index_nospec(idx, ctx->nr_user_files);
86+
file_ptr = io_fixed_file_slot(&ctx->file_table, idx)->file_ptr;
87+
file = (struct file *) (file_ptr & FFS_MASK);
88+
if (file)
89+
get_file(file);
7190
}
72-
73-
return 0;
91+
io_ring_submit_unlock(ctx, issue_flags);
92+
return file;
7493
}
7594

7695
static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
7796
{
7897
struct io_ring_ctx *target_ctx = req->file->private_data;
7998
struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
8099
struct io_ring_ctx *ctx = req->ctx;
81-
unsigned long file_ptr;
82-
struct file *src_file;
100+
struct file *src_file = msg->src_file;
83101
int ret;
84102

85103
if (target_ctx == ctx)
86104
return -EINVAL;
105+
if (!src_file) {
106+
src_file = io_msg_grab_file(req, issue_flags);
107+
if (!src_file)
108+
return -EBADF;
109+
msg->src_file = src_file;
110+
req->flags |= REQ_F_NEED_CLEANUP;
111+
}
87112

88-
ret = io_double_lock_ctx(ctx, target_ctx, issue_flags);
89-
if (unlikely(ret))
90-
return ret;
91-
92-
ret = -EBADF;
93-
if (unlikely(msg->src_fd >= ctx->nr_user_files))
94-
goto out_unlock;
95-
96-
msg->src_fd = array_index_nospec(msg->src_fd, ctx->nr_user_files);
97-
file_ptr = io_fixed_file_slot(&ctx->file_table, msg->src_fd)->file_ptr;
98-
if (!file_ptr)
99-
goto out_unlock;
100-
101-
src_file = (struct file *) (file_ptr & FFS_MASK);
102-
get_file(src_file);
113+
if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
114+
return -EAGAIN;
103115

104116
ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
105-
if (ret < 0) {
106-
fput(src_file);
117+
if (ret < 0)
107118
goto out_unlock;
108-
}
119+
msg->src_file = NULL;
120+
req->flags &= ~REQ_F_NEED_CLEANUP;
109121

110122
if (msg->flags & IORING_MSG_RING_CQE_SKIP)
111123
goto out_unlock;
@@ -119,7 +131,7 @@ static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
119131
if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
120132
ret = -EOVERFLOW;
121133
out_unlock:
122-
io_double_unlock_ctx(ctx, target_ctx, issue_flags);
134+
io_double_unlock_ctx(target_ctx, issue_flags);
123135
return ret;
124136
}
125137

@@ -130,6 +142,7 @@ int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
130142
if (unlikely(sqe->buf_index || sqe->personality))
131143
return -EINVAL;
132144

145+
msg->src_file = NULL;
133146
msg->user_data = READ_ONCE(sqe->off);
134147
msg->len = READ_ONCE(sqe->len);
135148
msg->cmd = READ_ONCE(sqe->addr);

io_uring/msg_ring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
44
int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags);
5+
void io_msg_ring_cleanup(struct io_kiocb *req);

io_uring/opdef.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ const struct io_op_def io_op_defs[] = {
445445
.name = "MSG_RING",
446446
.prep = io_msg_ring_prep,
447447
.issue = io_msg_ring,
448+
.cleanup = io_msg_ring_cleanup,
448449
},
449450
[IORING_OP_FSETXATTR] = {
450451
.needs_file = 1,

0 commit comments

Comments
 (0)