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