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;
27 void io_msg_ring_cleanup(struct io_kiocb *req)
29 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
31 if (WARN_ON_ONCE(!msg->src_file))
38 static int io_msg_ring_data(struct io_kiocb *req)
40 struct io_ring_ctx *target_ctx = req->file->private_data;
41 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
43 if (msg->src_fd || msg->dst_fd || msg->flags)
45 if (target_ctx->flags & IORING_SETUP_R_DISABLED)
48 if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0, true))
54 static void io_double_unlock_ctx(struct io_ring_ctx *octx,
55 unsigned int issue_flags)
57 mutex_unlock(&octx->uring_lock);
60 static int io_double_lock_ctx(struct io_ring_ctx *octx,
61 unsigned int issue_flags)
64 * To ensure proper ordering between the two ctxs, we can only
65 * attempt a trylock on the target. If that fails and we already have
66 * the source ctx lock, punt to io-wq.
68 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
69 if (!mutex_trylock(&octx->uring_lock))
73 mutex_lock(&octx->uring_lock);
77 static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
79 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
80 struct io_ring_ctx *ctx = req->ctx;
81 struct file *file = NULL;
82 unsigned long file_ptr;
83 int idx = msg->src_fd;
85 io_ring_submit_lock(ctx, issue_flags);
86 if (likely(idx < ctx->nr_user_files)) {
87 idx = array_index_nospec(idx, ctx->nr_user_files);
88 file_ptr = io_fixed_file_slot(&ctx->file_table, idx)->file_ptr;
89 file = (struct file *) (file_ptr & FFS_MASK);
93 io_ring_submit_unlock(ctx, issue_flags);
97 static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
99 struct io_ring_ctx *target_ctx = req->file->private_data;
100 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
101 struct file *src_file = msg->src_file;
104 if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
107 ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
111 msg->src_file = NULL;
112 req->flags &= ~REQ_F_NEED_CLEANUP;
114 if (msg->flags & IORING_MSG_RING_CQE_SKIP)
117 * If this fails, the target still received the file descriptor but
118 * wasn't notified of the fact. This means that if this request
119 * completes with -EOVERFLOW, then the sender must ensure that a
120 * later IORING_OP_MSG_RING delivers the message.
122 if (!io_post_aux_cqe(target_ctx, msg->user_data, ret, 0, true))
125 io_double_unlock_ctx(target_ctx, issue_flags);
129 static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
131 struct io_ring_ctx *target_ctx = req->file->private_data;
132 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
133 struct io_ring_ctx *ctx = req->ctx;
134 struct file *src_file = msg->src_file;
136 if (target_ctx == ctx)
139 src_file = io_msg_grab_file(req, issue_flags);
142 msg->src_file = src_file;
143 req->flags |= REQ_F_NEED_CLEANUP;
145 return io_msg_install_complete(req, issue_flags);
148 int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
150 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
152 if (unlikely(sqe->buf_index || sqe->personality))
155 msg->src_file = NULL;
156 msg->user_data = READ_ONCE(sqe->off);
157 msg->len = READ_ONCE(sqe->len);
158 msg->cmd = READ_ONCE(sqe->addr);
159 msg->src_fd = READ_ONCE(sqe->addr3);
160 msg->dst_fd = READ_ONCE(sqe->file_index);
161 msg->flags = READ_ONCE(sqe->msg_ring_flags);
162 if (msg->flags & ~IORING_MSG_RING_CQE_SKIP)
168 int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
170 struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
174 if (!io_is_uring_fops(req->file))
178 case IORING_MSG_DATA:
179 ret = io_msg_ring_data(req);
181 case IORING_MSG_SEND_FD:
182 ret = io_msg_send_fd(req, issue_flags);
194 io_req_set_res(req, ret, 0);