xref: /linux/io_uring/msg_ring.c (revision 6b9c4a05ae2b539bfd4d64a46eb861a57cc08351)
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 
20 struct 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 
35 static void io_double_unlock_ctx(struct io_ring_ctx *octx)
36 {
37 	mutex_unlock(&octx->uring_lock);
38 }
39 
40 static 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 
57 void 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 
68 static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx)
69 {
70 	return target_ctx->task_complete;
71 }
72 
73 static void io_msg_tw_complete(struct io_kiocb *req, io_tw_token_t tw)
74 {
75 	struct io_ring_ctx *ctx = req->ctx;
76 
77 	io_add_aux_cqe(ctx, req->cqe.user_data, req->cqe.res, req->cqe.flags);
78 	kfree_rcu(req, rcu_head);
79 	percpu_ref_put(&ctx->refs);
80 }
81 
82 static int io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,
83 			      int res, u32 cflags, u64 user_data)
84 {
85 	if (!READ_ONCE(ctx->submitter_task)) {
86 		kfree_rcu(req, rcu_head);
87 		return -EOWNERDEAD;
88 	}
89 	req->opcode = IORING_OP_NOP;
90 	req->cqe.user_data = user_data;
91 	io_req_set_res(req, res, cflags);
92 	percpu_ref_get(&ctx->refs);
93 	req->ctx = ctx;
94 	req->tctx = NULL;
95 	req->io_task_work.func = io_msg_tw_complete;
96 	io_req_task_work_add_remote(req, IOU_F_TWQ_LAZY_WAKE);
97 	return 0;
98 }
99 
100 static int io_msg_data_remote(struct io_ring_ctx *target_ctx,
101 			      struct io_msg *msg)
102 {
103 	struct io_kiocb *target;
104 	u32 flags = 0;
105 
106 	target = kmem_cache_alloc(req_cachep, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO)  ;
107 	if (unlikely(!target))
108 		return -ENOMEM;
109 
110 	if (msg->flags & IORING_MSG_RING_FLAGS_PASS)
111 		flags = msg->cqe_flags;
112 
113 	return io_msg_remote_post(target_ctx, target, msg->len, flags,
114 					msg->user_data);
115 }
116 
117 static int __io_msg_ring_data(struct io_ring_ctx *target_ctx,
118 			      struct io_msg *msg, unsigned int issue_flags)
119 {
120 	u32 flags = 0;
121 	int ret;
122 
123 	if (msg->src_fd || msg->flags & ~IORING_MSG_RING_FLAGS_PASS)
124 		return -EINVAL;
125 	if (!(msg->flags & IORING_MSG_RING_FLAGS_PASS) && msg->dst_fd)
126 		return -EINVAL;
127 	if (target_ctx->flags & IORING_SETUP_R_DISABLED)
128 		return -EBADFD;
129 
130 	if (io_msg_need_remote(target_ctx))
131 		return io_msg_data_remote(target_ctx, msg);
132 
133 	if (msg->flags & IORING_MSG_RING_FLAGS_PASS)
134 		flags = msg->cqe_flags;
135 
136 	ret = -EOVERFLOW;
137 	if (target_ctx->flags & IORING_SETUP_IOPOLL) {
138 		if (unlikely(io_lock_external_ctx(target_ctx, issue_flags)))
139 			return -EAGAIN;
140 	}
141 	if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, flags))
142 		ret = 0;
143 	if (target_ctx->flags & IORING_SETUP_IOPOLL)
144 		io_double_unlock_ctx(target_ctx);
145 	return ret;
146 }
147 
148 static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
149 {
150 	struct io_ring_ctx *target_ctx = req->file->private_data;
151 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
152 
153 	return __io_msg_ring_data(target_ctx, msg, issue_flags);
154 }
155 
156 static int io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
157 {
158 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
159 	struct io_ring_ctx *ctx = req->ctx;
160 	struct io_rsrc_node *node;
161 	int ret = -EBADF;
162 
163 	io_ring_submit_lock(ctx, issue_flags);
164 	node = io_rsrc_node_lookup(&ctx->file_table.data, msg->src_fd);
165 	if (node) {
166 		msg->src_file = io_slot_file(node);
167 		if (msg->src_file)
168 			get_file(msg->src_file);
169 		req->flags |= REQ_F_NEED_CLEANUP;
170 		ret = 0;
171 	}
172 	io_ring_submit_unlock(ctx, issue_flags);
173 	return ret;
174 }
175 
176 static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
177 {
178 	struct io_ring_ctx *target_ctx = req->file->private_data;
179 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
180 	struct file *src_file = msg->src_file;
181 	int ret;
182 
183 	if (unlikely(io_lock_external_ctx(target_ctx, issue_flags)))
184 		return -EAGAIN;
185 
186 	ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
187 	if (ret < 0)
188 		goto out_unlock;
189 
190 	msg->src_file = NULL;
191 	req->flags &= ~REQ_F_NEED_CLEANUP;
192 
193 	if (msg->flags & IORING_MSG_RING_CQE_SKIP)
194 		goto out_unlock;
195 	/*
196 	 * If this fails, the target still received the file descriptor but
197 	 * wasn't notified of the fact. This means that if this request
198 	 * completes with -EOVERFLOW, then the sender must ensure that a
199 	 * later IORING_OP_MSG_RING delivers the message.
200 	 */
201 	if (!io_post_aux_cqe(target_ctx, msg->user_data, ret, 0))
202 		ret = -EOVERFLOW;
203 out_unlock:
204 	io_double_unlock_ctx(target_ctx);
205 	return ret;
206 }
207 
208 static void io_msg_tw_fd_complete(struct callback_head *head)
209 {
210 	struct io_msg *msg = container_of(head, struct io_msg, tw);
211 	struct io_kiocb *req = cmd_to_io_kiocb(msg);
212 	int ret = -EOWNERDEAD;
213 
214 	if (!(current->flags & PF_EXITING))
215 		ret = io_msg_install_complete(req, IO_URING_F_UNLOCKED);
216 	if (ret < 0)
217 		req_set_fail(req);
218 	io_req_queue_tw_complete(req, ret);
219 }
220 
221 static int io_msg_fd_remote(struct io_kiocb *req)
222 {
223 	struct io_ring_ctx *ctx = req->file->private_data;
224 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
225 	struct task_struct *task = READ_ONCE(ctx->submitter_task);
226 
227 	if (unlikely(!task))
228 		return -EOWNERDEAD;
229 
230 	init_task_work(&msg->tw, io_msg_tw_fd_complete);
231 	if (task_work_add(task, &msg->tw, TWA_SIGNAL))
232 		return -EOWNERDEAD;
233 
234 	return IOU_ISSUE_SKIP_COMPLETE;
235 }
236 
237 static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
238 {
239 	struct io_ring_ctx *target_ctx = req->file->private_data;
240 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
241 	struct io_ring_ctx *ctx = req->ctx;
242 
243 	if (msg->len)
244 		return -EINVAL;
245 	if (target_ctx == ctx)
246 		return -EINVAL;
247 	if (target_ctx->flags & IORING_SETUP_R_DISABLED)
248 		return -EBADFD;
249 	if (!msg->src_file) {
250 		int ret = io_msg_grab_file(req, issue_flags);
251 		if (unlikely(ret))
252 			return ret;
253 	}
254 
255 	if (io_msg_need_remote(target_ctx))
256 		return io_msg_fd_remote(req);
257 	return io_msg_install_complete(req, issue_flags);
258 }
259 
260 static int __io_msg_ring_prep(struct io_msg *msg, const struct io_uring_sqe *sqe)
261 {
262 	if (unlikely(sqe->buf_index || sqe->personality))
263 		return -EINVAL;
264 
265 	msg->src_file = NULL;
266 	msg->user_data = READ_ONCE(sqe->off);
267 	msg->len = READ_ONCE(sqe->len);
268 	msg->cmd = READ_ONCE(sqe->addr);
269 	msg->src_fd = READ_ONCE(sqe->addr3);
270 	msg->dst_fd = READ_ONCE(sqe->file_index);
271 	msg->flags = READ_ONCE(sqe->msg_ring_flags);
272 	if (msg->flags & ~IORING_MSG_RING_MASK)
273 		return -EINVAL;
274 
275 	return 0;
276 }
277 
278 int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
279 {
280 	return __io_msg_ring_prep(io_kiocb_to_cmd(req, struct io_msg), sqe);
281 }
282 
283 int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
284 {
285 	struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
286 	int ret;
287 
288 	ret = -EBADFD;
289 	if (!io_is_uring_fops(req->file))
290 		goto done;
291 
292 	switch (msg->cmd) {
293 	case IORING_MSG_DATA:
294 		ret = io_msg_ring_data(req, issue_flags);
295 		break;
296 	case IORING_MSG_SEND_FD:
297 		ret = io_msg_send_fd(req, issue_flags);
298 		break;
299 	default:
300 		ret = -EINVAL;
301 		break;
302 	}
303 
304 done:
305 	if (ret < 0) {
306 		if (ret == -EAGAIN || ret == IOU_ISSUE_SKIP_COMPLETE)
307 			return ret;
308 		req_set_fail(req);
309 	}
310 	io_req_set_res(req, ret, 0);
311 	return IOU_COMPLETE;
312 }
313 
314 int io_uring_sync_msg_ring(struct io_uring_sqe *sqe)
315 {
316 	struct io_msg io_msg = { };
317 	int ret;
318 
319 	ret = __io_msg_ring_prep(&io_msg, sqe);
320 	if (unlikely(ret))
321 		return ret;
322 
323 	/*
324 	 * Only data sending supported, not IORING_MSG_SEND_FD as that one
325 	 * doesn't make sense without a source ring to send files from.
326 	 */
327 	if (io_msg.cmd != IORING_MSG_DATA)
328 		return -EINVAL;
329 
330 	CLASS(fd, f)(sqe->fd);
331 	if (fd_empty(f))
332 		return -EBADF;
333 	if (!io_is_uring_fops(fd_file(f)))
334 		return -EBADFD;
335 	return  __io_msg_ring_data(fd_file(f)->private_data,
336 				   &io_msg, IO_URING_F_UNLOCKED);
337 }
338