Commit | Line | Data |
---|---|---|
36404b09 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include <linux/kernel.h> | |
3 | #include <linux/errno.h> | |
4 | #include <linux/file.h> | |
5 | #include <linux/slab.h> | |
e6130eba | 6 | #include <linux/nospec.h> |
36404b09 JA |
7 | #include <linux/io_uring.h> |
8 | ||
9 | #include <uapi/linux/io_uring.h> | |
10 | ||
36404b09 | 11 | #include "io_uring.h" |
e6130eba JA |
12 | #include "rsrc.h" |
13 | #include "filetable.h" | |
36404b09 JA |
14 | #include "msg_ring.h" |
15 | ||
16 | struct io_msg { | |
17 | struct file *file; | |
11373026 | 18 | struct file *src_file; |
36404b09 JA |
19 | u64 user_data; |
20 | u32 len; | |
e6130eba JA |
21 | u32 cmd; |
22 | u32 src_fd; | |
23 | u32 dst_fd; | |
24 | u32 flags; | |
36404b09 JA |
25 | }; |
26 | ||
11373026 PB |
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 | ||
e6130eba JA |
38 | static int io_msg_ring_data(struct io_kiocb *req) |
39 | { | |
40 | struct io_ring_ctx *target_ctx = req->file->private_data; | |
f2ccb5ae | 41 | struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); |
e6130eba JA |
42 | |
43 | if (msg->src_fd || msg->dst_fd || msg->flags) | |
44 | return -EINVAL; | |
45 | ||
b529c96a | 46 | if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) |
e6130eba JA |
47 | return 0; |
48 | ||
49 | return -EOVERFLOW; | |
50 | } | |
51 | ||
11373026 | 52 | static void io_double_unlock_ctx(struct io_ring_ctx *octx, |
e6130eba JA |
53 | unsigned int issue_flags) |
54 | { | |
e6130eba JA |
55 | mutex_unlock(&octx->uring_lock); |
56 | } | |
57 | ||
11373026 | 58 | static int io_double_lock_ctx(struct io_ring_ctx *octx, |
e6130eba JA |
59 | unsigned int issue_flags) |
60 | { | |
61 | /* | |
62 | * To ensure proper ordering between the two ctxs, we can only | |
63 | * attempt a trylock on the target. If that fails and we already have | |
64 | * the source ctx lock, punt to io-wq. | |
65 | */ | |
66 | if (!(issue_flags & IO_URING_F_UNLOCKED)) { | |
67 | if (!mutex_trylock(&octx->uring_lock)) | |
68 | return -EAGAIN; | |
69 | return 0; | |
70 | } | |
11373026 PB |
71 | mutex_lock(&octx->uring_lock); |
72 | return 0; | |
73 | } | |
e6130eba | 74 | |
11373026 PB |
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); | |
e6130eba | 90 | } |
11373026 PB |
91 | io_ring_submit_unlock(ctx, issue_flags); |
92 | return file; | |
e6130eba JA |
93 | } |
94 | ||
17211310 | 95 | static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags) |
e6130eba JA |
96 | { |
97 | struct io_ring_ctx *target_ctx = req->file->private_data; | |
f2ccb5ae | 98 | struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); |
11373026 | 99 | struct file *src_file = msg->src_file; |
e6130eba JA |
100 | int ret; |
101 | ||
11373026 PB |
102 | if (unlikely(io_double_lock_ctx(target_ctx, issue_flags))) |
103 | return -EAGAIN; | |
e6130eba JA |
104 | |
105 | ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd); | |
11373026 | 106 | if (ret < 0) |
e6130eba | 107 | goto out_unlock; |
17211310 | 108 | |
11373026 PB |
109 | msg->src_file = NULL; |
110 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
e6130eba JA |
111 | |
112 | if (msg->flags & IORING_MSG_RING_CQE_SKIP) | |
113 | goto out_unlock; | |
e6130eba JA |
114 | /* |
115 | * If this fails, the target still received the file descriptor but | |
116 | * wasn't notified of the fact. This means that if this request | |
117 | * completes with -EOVERFLOW, then the sender must ensure that a | |
118 | * later IORING_OP_MSG_RING delivers the message. | |
119 | */ | |
b529c96a | 120 | if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0)) |
e6130eba JA |
121 | ret = -EOVERFLOW; |
122 | out_unlock: | |
11373026 | 123 | io_double_unlock_ctx(target_ctx, issue_flags); |
e6130eba JA |
124 | return ret; |
125 | } | |
126 | ||
17211310 PB |
127 | static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags) |
128 | { | |
129 | struct io_ring_ctx *target_ctx = req->file->private_data; | |
130 | struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); | |
131 | struct io_ring_ctx *ctx = req->ctx; | |
132 | struct file *src_file = msg->src_file; | |
133 | ||
134 | if (target_ctx == ctx) | |
135 | return -EINVAL; | |
136 | if (!src_file) { | |
137 | src_file = io_msg_grab_file(req, issue_flags); | |
138 | if (!src_file) | |
139 | return -EBADF; | |
140 | msg->src_file = src_file; | |
141 | req->flags |= REQ_F_NEED_CLEANUP; | |
142 | } | |
143 | return io_msg_install_complete(req, issue_flags); | |
144 | } | |
145 | ||
36404b09 JA |
146 | int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
147 | { | |
f2ccb5ae | 148 | struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); |
36404b09 | 149 | |
e6130eba | 150 | if (unlikely(sqe->buf_index || sqe->personality)) |
36404b09 JA |
151 | return -EINVAL; |
152 | ||
11373026 | 153 | msg->src_file = NULL; |
36404b09 JA |
154 | msg->user_data = READ_ONCE(sqe->off); |
155 | msg->len = READ_ONCE(sqe->len); | |
e6130eba JA |
156 | msg->cmd = READ_ONCE(sqe->addr); |
157 | msg->src_fd = READ_ONCE(sqe->addr3); | |
158 | msg->dst_fd = READ_ONCE(sqe->file_index); | |
159 | msg->flags = READ_ONCE(sqe->msg_ring_flags); | |
160 | if (msg->flags & ~IORING_MSG_RING_CQE_SKIP) | |
161 | return -EINVAL; | |
162 | ||
36404b09 JA |
163 | return 0; |
164 | } | |
165 | ||
166 | int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
167 | { | |
f2ccb5ae | 168 | struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg); |
36404b09 JA |
169 | int ret; |
170 | ||
171 | ret = -EBADFD; | |
172 | if (!io_is_uring_fops(req->file)) | |
173 | goto done; | |
174 | ||
e6130eba JA |
175 | switch (msg->cmd) { |
176 | case IORING_MSG_DATA: | |
177 | ret = io_msg_ring_data(req); | |
178 | break; | |
179 | case IORING_MSG_SEND_FD: | |
180 | ret = io_msg_send_fd(req, issue_flags); | |
181 | break; | |
182 | default: | |
183 | ret = -EINVAL; | |
184 | break; | |
185 | } | |
36404b09 JA |
186 | |
187 | done: | |
4c979eae PB |
188 | if (ret == -EAGAIN) |
189 | return -EAGAIN; | |
36404b09 JA |
190 | if (ret < 0) |
191 | req_set_fail(req); | |
192 | io_req_set_res(req, ret, 0); | |
36404b09 JA |
193 | return IOU_OK; |
194 | } |