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>
9 #include <uapi/linux/io_uring.h>
13 #include "filetable.h"
18 struct file *src_file;
19 struct callback_head tw;
28 void io_msg_ring_cleanup(struct io_kiocb *req)
30 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
32 if (WARN_ON_ONCE(!msg->src_file))
39 static void io_msg_tw_complete(struct callback_head *head)
41 struct io_msg *msg = container_of(head, struct io_msg, tw);
42 struct io_kiocb *req = cmd_to_io_kiocb(msg);
43 struct io_ring_ctx *target_ctx = req->file->private_data;
46 if (current->flags & PF_EXITING)
48 else if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
53 io_req_queue_tw_complete(req, ret);
56 static int io_msg_ring_data(struct io_kiocb *req)
58 struct io_ring_ctx *target_ctx = req->file->private_data;
59 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
61 if (msg->src_fd || msg->dst_fd || msg->flags)
64 if (target_ctx->task_complete && current != target_ctx->submitter_task) {
65 init_task_work(&msg->tw, io_msg_tw_complete);
66 if (task_work_add(target_ctx->submitter_task, &msg->tw,
70 atomic_or(IORING_SQ_TASKRUN, &target_ctx->rings->sq_flags);
71 return IOU_ISSUE_SKIP_COMPLETE;
74 if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
80 static void io_double_unlock_ctx(struct io_ring_ctx *octx,
81 unsigned int issue_flags)
83 mutex_unlock(&octx->uring_lock);
86 static int io_double_lock_ctx(struct io_ring_ctx *octx,
87 unsigned int issue_flags)
90 * To ensure proper ordering between the two ctxs, we can only
91 * attempt a trylock on the target. If that fails and we already have
92 * the source ctx lock, punt to io-wq.
94 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
95 if (!mutex_trylock(&octx->uring_lock))
99 mutex_lock(&octx->uring_lock);
103 static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
105 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
106 struct io_ring_ctx *ctx = req->ctx;
107 struct file *file = NULL;
108 unsigned long file_ptr;
109 int idx = msg->src_fd;
111 io_ring_submit_lock(ctx, issue_flags);
112 if (likely(idx < ctx->nr_user_files)) {
113 idx = array_index_nospec(idx, ctx->nr_user_files);
114 file_ptr = io_fixed_file_slot(&ctx->file_table, idx)->file_ptr;
115 file = (struct file *) (file_ptr & FFS_MASK);
119 io_ring_submit_unlock(ctx, issue_flags);
123 static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
125 struct io_ring_ctx *target_ctx = req->file->private_data;
126 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
127 struct file *src_file = msg->src_file;
130 if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
133 ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
137 msg->src_file = NULL;
138 req->flags &= ~REQ_F_NEED_CLEANUP;
140 if (msg->flags & IORING_MSG_RING_CQE_SKIP)
143 * If this fails, the target still received the file descriptor but
144 * wasn't notified of the fact. This means that if this request
145 * completes with -EOVERFLOW, then the sender must ensure that a
146 * later IORING_OP_MSG_RING delivers the message.
148 if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
151 io_double_unlock_ctx(target_ctx, issue_flags);
155 static void io_msg_tw_fd_complete(struct callback_head *head)
157 struct io_msg *msg = container_of(head, struct io_msg, tw);
158 struct io_kiocb *req = cmd_to_io_kiocb(msg);
159 int ret = -EOWNERDEAD;
161 if (!(current->flags & PF_EXITING))
162 ret = io_msg_install_complete(req, IO_URING_F_UNLOCKED);
165 io_req_queue_tw_complete(req, ret);
168 static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
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 struct io_ring_ctx *ctx = req->ctx;
173 struct file *src_file = msg->src_file;
175 if (target_ctx == ctx)
178 src_file = io_msg_grab_file(req, issue_flags);
181 msg->src_file = src_file;
182 req->flags |= REQ_F_NEED_CLEANUP;
185 if (target_ctx->task_complete && current != target_ctx->submitter_task) {
186 init_task_work(&msg->tw, io_msg_tw_fd_complete);
187 if (task_work_add(target_ctx->submitter_task, &msg->tw,
191 return IOU_ISSUE_SKIP_COMPLETE;
193 return io_msg_install_complete(req, issue_flags);
196 int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
198 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
200 if (unlikely(sqe->buf_index || sqe->personality))
203 msg->src_file = NULL;
204 msg->user_data = READ_ONCE(sqe->off);
205 msg->len = READ_ONCE(sqe->len);
206 msg->cmd = READ_ONCE(sqe->addr);
207 msg->src_fd = READ_ONCE(sqe->addr3);
208 msg->dst_fd = READ_ONCE(sqe->file_index);
209 msg->flags = READ_ONCE(sqe->msg_ring_flags);
210 if (msg->flags & ~IORING_MSG_RING_CQE_SKIP)
216 int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
218 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
222 if (!io_is_uring_fops(req->file))
226 case IORING_MSG_DATA:
227 ret = io_msg_ring_data(req);
229 case IORING_MSG_SEND_FD:
230 ret = io_msg_send_fd(req, issue_flags);
239 if (ret == -EAGAIN || ret == IOU_ISSUE_SKIP_COMPLETE)
243 io_req_set_res(req, ret, 0);