projects/linux·blob master · refs

358 lines · 8.8 KiB

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>
6#include <linux/nospec.h>
7#include <linux/io_uring.h>
8
9#include <uapi/linux/io_uring.h>
10
11#include "io_uring.h"
12#include "rsrc.h"
13#include "filetable.h"
14#include "msg_ring.h"
15
16/* All valid masks for MSG_RING */
17#define IORING_MSG_RING_MASK (IORING_MSG_RING_CQE_SKIP | \
18 IORING_MSG_RING_FLAGS_PASS)
19
20struct io_msg {
21 struct file *file;
22 struct file *src_file;
23 struct callback_head tw;
24 u64 user_data;
25 u32 len;
26 u32 cmd;
27 u32 src_fd;
28 union {
29 u32 dst_fd;
30 u32 cqe_flags;
31 };
32 u32 flags;
33};
34
35static void io_double_unlock_ctx(struct io_ring_ctx *octx)
36{
37 mutex_unlock(&octx->uring_lock);
38}
39
40static int io_lock_external_ctx(struct io_ring_ctx *octx,
41 unsigned int issue_flags)
42{
43 /*
44 * To ensure proper ordering between the two ctxs, we can only
45 * attempt a trylock on the target. If that fails and we already have
46 * the source ctx lock, punt to io-wq.
47 */
48 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
49 if (!mutex_trylock(&octx->uring_lock))
50 return -EAGAIN;
51 return 0;
52 }
53 mutex_lock(&octx->uring_lock);
54 return 0;
55}
56
57void io_msg_ring_cleanup(struct io_kiocb *req)
58{
59 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
60
61 if (WARN_ON_ONCE(!msg->src_file))
62 return;
63
64 fput(msg->src_file);
65 msg->src_file = NULL;
66}
67
68static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx)
69{
70 return target_ctx->int_flags & IO_RING_F_TASK_COMPLETE;
71}
72
73static void io_msg_tw_complete(struct io_tw_req tw_req, io_tw_token_t tw)
74{
75 struct io_kiocb *req = tw_req.req;
76 struct io_ring_ctx *ctx = req->ctx;
77
78 io_add_aux_cqe(ctx, req->cqe.user_data, req->cqe.res, req->cqe.flags);
79 kfree_rcu(req, rcu_head);
80 percpu_ref_put(&ctx->refs);
81}
82
83static void io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,
84 int res, u32 cflags, u64 user_data)
85{
86 req->opcode = IORING_OP_NOP;
87 req->cqe.user_data = user_data;
88 io_req_set_res(req, res, cflags);
89 percpu_ref_get(&ctx->refs);
90 req->ctx = ctx;
91 req->tctx = NULL;
92 req->io_task_work.func = io_msg_tw_complete;
93 io_req_task_work_add_remote(req, IOU_F_TWQ_LAZY_WAKE);
94}
95
96static int io_msg_ring_cqe_flags(struct io_ring_ctx *target_ctx,
97 const struct io_msg *msg, u32 *flags)
98{
99 *flags = 0;
100
101 if (!(msg->flags & IORING_MSG_RING_FLAGS_PASS))
102 return 0;
103
104 *flags = msg->cqe_flags;
105 if ((*flags & IORING_CQE_F_32) &&
106 !(target_ctx->flags & (IORING_SETUP_CQE32 |
107 IORING_SETUP_CQE_MIXED)))
108 return -EINVAL;
109
110 return 0;
111}
112
113static int io_msg_data_remote(struct io_ring_ctx *target_ctx,
114 struct io_msg *msg)
115{
116 struct io_kiocb *target;
117 u32 flags;
118 int ret;
119
120 ret = io_msg_ring_cqe_flags(target_ctx, msg, &flags);
121 if (ret)
122 return ret;
123
124 target = kmem_cache_alloc(req_cachep, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
125 if (unlikely(!target))
126 return -ENOMEM;
127
128 io_msg_remote_post(target_ctx, target, msg->len, flags, msg->user_data);
129 return 0;
130}
131
132static int __io_msg_ring_data(struct io_ring_ctx *target_ctx,
133 struct io_msg *msg, unsigned int issue_flags)
134{
135 u32 flags = 0;
136 int ret;
137
138 if (msg->src_fd || msg->flags & ~IORING_MSG_RING_FLAGS_PASS)
139 return -EINVAL;
140 if (!(msg->flags & IORING_MSG_RING_FLAGS_PASS) && msg->dst_fd)
141 return -EINVAL;
142 /*
143 * Keep IORING_SETUP_R_DISABLED check before submitter_task load
144 * in io_msg_data_remote() -> io_req_task_work_add_remote()
145 */
146 if (smp_load_acquire(&target_ctx->flags) & IORING_SETUP_R_DISABLED)
147 return -EBADFD;
148
149 if (io_msg_need_remote(target_ctx))
150 return io_msg_data_remote(target_ctx, msg);
151
152 ret = io_msg_ring_cqe_flags(target_ctx, msg, &flags);
153 if (ret)
154 return ret;
155
156 ret = -EOVERFLOW;
157 if (target_ctx->flags & IORING_SETUP_IOPOLL) {
158 if (unlikely(io_lock_external_ctx(target_ctx, issue_flags)))
159 return -EAGAIN;
160 }
161 if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, flags))
162 ret = 0;
163 if (target_ctx->flags & IORING_SETUP_IOPOLL)
164 io_double_unlock_ctx(target_ctx);
165 return ret;
166}
167
168static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
169{
170 struct io_ring_ctx *target_ctx = req->file->private_data;
171 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
172
173 return __io_msg_ring_data(target_ctx, msg, issue_flags);
174}
175
176static int io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
177{
178 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
179 struct io_ring_ctx *ctx = req->ctx;
180 struct io_rsrc_node *node;
181 int ret = -EBADF;
182
183 io_ring_submit_lock(ctx, issue_flags);
184 node = io_rsrc_node_lookup(&ctx->file_table.data, msg->src_fd);
185 if (node) {
186 msg->src_file = io_slot_file(node);
187 if (msg->src_file)
188 get_file(msg->src_file);
189 req->flags |= REQ_F_NEED_CLEANUP;
190 ret = 0;
191 }
192 io_ring_submit_unlock(ctx, issue_flags);
193 return ret;
194}
195
196static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
197{
198 struct io_ring_ctx *target_ctx = req->file->private_data;
199 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
200 struct file *src_file = msg->src_file;
201 int ret;
202
203 if (unlikely(io_lock_external_ctx(target_ctx, issue_flags)))
204 return -EAGAIN;
205
206 ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
207 if (ret < 0)
208 goto out_unlock;
209
210 msg->src_file = NULL;
211 req->flags &= ~REQ_F_NEED_CLEANUP;
212
213 if (msg->flags & IORING_MSG_RING_CQE_SKIP)
214 goto out_unlock;
215 /*
216 * If this fails, the target still received the file descriptor but
217 * wasn't notified of the fact. This means that if this request
218 * completes with -EOVERFLOW, then the sender must ensure that a
219 * later IORING_OP_MSG_RING delivers the message.
220 */
221 if (!io_post_aux_cqe(target_ctx, msg->user_data, ret, 0))
222 ret = -EOVERFLOW;
223out_unlock:
224 io_double_unlock_ctx(target_ctx);
225 return ret;
226}
227
228static void io_msg_tw_fd_complete(struct callback_head *head)
229{
230 struct io_msg *msg = container_of(head, struct io_msg, tw);
231 struct io_kiocb *req = cmd_to_io_kiocb(msg);
232 int ret = -EOWNERDEAD;
233
234 if (!(current->flags & PF_EXITING))
235 ret = io_msg_install_complete(req, IO_URING_F_UNLOCKED);
236 if (ret < 0)
237 req_set_fail(req);
238 io_req_queue_tw_complete(req, ret);
239}
240
241static int io_msg_fd_remote(struct io_kiocb *req)
242{
243 struct io_ring_ctx *ctx = req->file->private_data;
244 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
245 struct task_struct *task = ctx->submitter_task;
246
247 init_task_work(&msg->tw, io_msg_tw_fd_complete);
248 if (task_work_add(task, &msg->tw, TWA_SIGNAL))
249 return -EOWNERDEAD;
250
251 return IOU_ISSUE_SKIP_COMPLETE;
252}
253
254static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
255{
256 struct io_ring_ctx *target_ctx = req->file->private_data;
257 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
258 struct io_ring_ctx *ctx = req->ctx;
259
260 if (msg->len)
261 return -EINVAL;
262 if (target_ctx == ctx)
263 return -EINVAL;
264 /*
265 * Keep IORING_SETUP_R_DISABLED check before submitter_task load
266 * in io_msg_fd_remote()
267 */
268 if (smp_load_acquire(&target_ctx->flags) & IORING_SETUP_R_DISABLED)
269 return -EBADFD;
270 if (!msg->src_file) {
271 int ret = io_msg_grab_file(req, issue_flags);
272 if (unlikely(ret))
273 return ret;
274 }
275
276 if (io_msg_need_remote(target_ctx))
277 return io_msg_fd_remote(req);
278 return io_msg_install_complete(req, issue_flags);
279}
280
281static int __io_msg_ring_prep(struct io_msg *msg, const struct io_uring_sqe *sqe)
282{
283 if (unlikely(sqe->buf_index || sqe->personality))
284 return -EINVAL;
285
286 msg->src_file = NULL;
287 msg->user_data = READ_ONCE(sqe->off);
288 msg->len = READ_ONCE(sqe->len);
289 msg->cmd = READ_ONCE(sqe->addr);
290 msg->src_fd = READ_ONCE(sqe->addr3);
291 msg->dst_fd = READ_ONCE(sqe->file_index);
292 msg->flags = READ_ONCE(sqe->msg_ring_flags);
293 if (msg->flags & ~IORING_MSG_RING_MASK)
294 return -EINVAL;
295
296 return 0;
297}
298
299int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
300{
301 return __io_msg_ring_prep(io_kiocb_to_cmd(req, struct io_msg), sqe);
302}
303
304int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
305{
306 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
307 int ret;
308
309 ret = -EBADFD;
310 if (!io_is_uring_fops(req->file))
311 goto done;
312
313 switch (msg->cmd) {
314 case IORING_MSG_DATA:
315 ret = io_msg_ring_data(req, issue_flags);
316 break;
317 case IORING_MSG_SEND_FD:
318 ret = io_msg_send_fd(req, issue_flags);
319 break;
320 default:
321 ret = -EINVAL;
322 break;
323 }
324
325done:
326 if (ret < 0) {
327 if (ret == -EAGAIN || ret == IOU_ISSUE_SKIP_COMPLETE)
328 return ret;
329 req_set_fail(req);
330 }
331 io_req_set_res(req, ret, 0);
332 return IOU_COMPLETE;
333}
334
335int io_uring_sync_msg_ring(struct io_uring_sqe *sqe)
336{
337 struct io_msg io_msg = { };
338 int ret;
339
340 ret = __io_msg_ring_prep(&io_msg, sqe);
341 if (unlikely(ret))
342 return ret;
343
344 /*
345 * Only data sending supported, not IORING_MSG_SEND_FD as that one
346 * doesn't make sense without a source ring to send files from.
347 */
348 if (io_msg.cmd != IORING_MSG_DATA)
349 return -EINVAL;
350
351 CLASS(fd, f)(sqe->fd);
352 if (fd_empty(f))
353 return -EBADF;
354 if (!io_is_uring_fops(fd_file(f)))
355 return -EBADFD;
356 return __io_msg_ring_data(fd_file(f)->private_data,
357 &io_msg, IO_URING_F_UNLOCKED);
358}