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/io_uring/cmd.h> 6 #include <linux/io_uring/net.h> 7 #include <linux/security.h> 8 #include <linux/nospec.h> 9 #include <net/sock.h> 10 11 #include <uapi/linux/io_uring.h> 12 #include <asm/ioctls.h> 13 14 #include "io_uring.h" 15 #include "alloc_cache.h" 16 #include "rsrc.h" 17 #include "uring_cmd.h" 18 19 static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags) 20 { 21 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 22 struct io_uring_cmd_data *cache = req->async_data; 23 24 if (cache->op_data) { 25 kfree(cache->op_data); 26 cache->op_data = NULL; 27 } 28 29 if (issue_flags & IO_URING_F_UNLOCKED) 30 return; 31 if (io_alloc_cache_put(&req->ctx->uring_cache, cache)) { 32 ioucmd->sqe = NULL; 33 req->async_data = NULL; 34 req->flags &= ~REQ_F_ASYNC_DATA; 35 } 36 } 37 38 bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx, 39 struct io_uring_task *tctx, bool cancel_all) 40 { 41 struct hlist_node *tmp; 42 struct io_kiocb *req; 43 bool ret = false; 44 45 lockdep_assert_held(&ctx->uring_lock); 46 47 hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd, 48 hash_node) { 49 struct io_uring_cmd *cmd = io_kiocb_to_cmd(req, 50 struct io_uring_cmd); 51 struct file *file = req->file; 52 53 if (!cancel_all && req->tctx != tctx) 54 continue; 55 56 if (cmd->flags & IORING_URING_CMD_CANCELABLE) { 57 /* ->sqe isn't available if no async data */ 58 if (!req_has_async_data(req)) 59 cmd->sqe = NULL; 60 file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL | 61 IO_URING_F_COMPLETE_DEFER); 62 ret = true; 63 } 64 } 65 io_submit_flush_completions(ctx); 66 return ret; 67 } 68 69 static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd, 70 unsigned int issue_flags) 71 { 72 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 73 struct io_ring_ctx *ctx = req->ctx; 74 75 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) 76 return; 77 78 cmd->flags &= ~IORING_URING_CMD_CANCELABLE; 79 io_ring_submit_lock(ctx, issue_flags); 80 hlist_del(&req->hash_node); 81 io_ring_submit_unlock(ctx, issue_flags); 82 } 83 84 /* 85 * Mark this command as concelable, then io_uring_try_cancel_uring_cmd() 86 * will try to cancel this issued command by sending ->uring_cmd() with 87 * issue_flags of IO_URING_F_CANCEL. 88 * 89 * The command is guaranteed to not be done when calling ->uring_cmd() 90 * with IO_URING_F_CANCEL, but it is driver's responsibility to deal 91 * with race between io_uring canceling and normal completion. 92 */ 93 void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, 94 unsigned int issue_flags) 95 { 96 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 97 struct io_ring_ctx *ctx = req->ctx; 98 99 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) { 100 cmd->flags |= IORING_URING_CMD_CANCELABLE; 101 io_ring_submit_lock(ctx, issue_flags); 102 hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd); 103 io_ring_submit_unlock(ctx, issue_flags); 104 } 105 } 106 EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable); 107 108 static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts) 109 { 110 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 111 unsigned int flags = IO_URING_F_COMPLETE_DEFER; 112 113 if (io_should_terminate_tw()) 114 flags |= IO_URING_F_TASK_DEAD; 115 116 /* task_work executor checks the deffered list completion */ 117 ioucmd->task_work_cb(ioucmd, flags); 118 } 119 120 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, 121 void (*task_work_cb)(struct io_uring_cmd *, unsigned), 122 unsigned flags) 123 { 124 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 125 126 ioucmd->task_work_cb = task_work_cb; 127 req->io_task_work.func = io_uring_cmd_work; 128 __io_req_task_work_add(req, flags); 129 } 130 EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task); 131 132 static inline void io_req_set_cqe32_extra(struct io_kiocb *req, 133 u64 extra1, u64 extra2) 134 { 135 req->big_cqe.extra1 = extra1; 136 req->big_cqe.extra2 = extra2; 137 } 138 139 /* 140 * Called by consumers of io_uring_cmd, if they originally returned 141 * -EIOCBQUEUED upon receiving the command. 142 */ 143 void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2, 144 unsigned issue_flags) 145 { 146 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 147 148 io_uring_cmd_del_cancelable(ioucmd, issue_flags); 149 150 if (ret < 0) 151 req_set_fail(req); 152 153 io_req_set_res(req, ret, 0); 154 if (req->ctx->flags & IORING_SETUP_CQE32) 155 io_req_set_cqe32_extra(req, res2, 0); 156 io_req_uring_cleanup(req, issue_flags); 157 if (req->ctx->flags & IORING_SETUP_IOPOLL) { 158 /* order with io_iopoll_req_issued() checking ->iopoll_complete */ 159 smp_store_release(&req->iopoll_completed, 1); 160 } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) { 161 if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED)) 162 return; 163 io_req_complete_defer(req); 164 } else { 165 req->io_task_work.func = io_req_task_complete; 166 io_req_task_work_add(req); 167 } 168 } 169 EXPORT_SYMBOL_GPL(io_uring_cmd_done); 170 171 static void io_uring_cmd_cache_sqes(struct io_kiocb *req) 172 { 173 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 174 struct io_uring_cmd_data *cache = req->async_data; 175 176 memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx)); 177 ioucmd->sqe = cache->sqes; 178 } 179 180 static int io_uring_cmd_prep_setup(struct io_kiocb *req, 181 const struct io_uring_sqe *sqe) 182 { 183 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 184 struct io_uring_cmd_data *cache; 185 186 cache = io_uring_alloc_async_data(&req->ctx->uring_cache, req); 187 if (!cache) 188 return -ENOMEM; 189 cache->op_data = NULL; 190 191 ioucmd->sqe = sqe; 192 /* defer memcpy until we need it */ 193 if (unlikely(req->flags & REQ_F_FORCE_ASYNC)) 194 io_uring_cmd_cache_sqes(req); 195 return 0; 196 } 197 198 int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 199 { 200 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 201 202 if (sqe->__pad1) 203 return -EINVAL; 204 205 ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags); 206 if (ioucmd->flags & ~IORING_URING_CMD_MASK) 207 return -EINVAL; 208 209 if (ioucmd->flags & IORING_URING_CMD_FIXED) { 210 struct io_ring_ctx *ctx = req->ctx; 211 struct io_rsrc_node *node; 212 u16 index = READ_ONCE(sqe->buf_index); 213 214 node = io_rsrc_node_lookup(&ctx->buf_table, index); 215 if (unlikely(!node)) 216 return -EFAULT; 217 /* 218 * Pi node upfront, prior to io_uring_cmd_import_fixed() 219 * being called. This prevents destruction of the mapped buffer 220 * we'll need at actual import time. 221 */ 222 io_req_assign_buf_node(req, node); 223 } 224 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op); 225 226 return io_uring_cmd_prep_setup(req, sqe); 227 } 228 229 int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) 230 { 231 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 232 struct io_ring_ctx *ctx = req->ctx; 233 struct file *file = req->file; 234 int ret; 235 236 if (!file->f_op->uring_cmd) 237 return -EOPNOTSUPP; 238 239 ret = security_uring_cmd(ioucmd); 240 if (ret) 241 return ret; 242 243 if (ctx->flags & IORING_SETUP_SQE128) 244 issue_flags |= IO_URING_F_SQE128; 245 if (ctx->flags & IORING_SETUP_CQE32) 246 issue_flags |= IO_URING_F_CQE32; 247 if (ctx->compat) 248 issue_flags |= IO_URING_F_COMPAT; 249 if (ctx->flags & IORING_SETUP_IOPOLL) { 250 if (!file->f_op->uring_cmd_iopoll) 251 return -EOPNOTSUPP; 252 issue_flags |= IO_URING_F_IOPOLL; 253 req->iopoll_completed = 0; 254 } 255 256 ret = file->f_op->uring_cmd(ioucmd, issue_flags); 257 if (ret == -EAGAIN) { 258 struct io_uring_cmd_data *cache = req->async_data; 259 260 if (ioucmd->sqe != cache->sqes) 261 io_uring_cmd_cache_sqes(req); 262 return -EAGAIN; 263 } else if (ret == -EIOCBQUEUED) { 264 return -EIOCBQUEUED; 265 } 266 267 if (ret < 0) 268 req_set_fail(req); 269 io_req_uring_cleanup(req, issue_flags); 270 io_req_set_res(req, ret, 0); 271 return IOU_OK; 272 } 273 274 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, 275 struct iov_iter *iter, void *ioucmd) 276 { 277 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 278 struct io_rsrc_node *node = req->buf_node; 279 280 /* Must have had rsrc_node assigned at prep time */ 281 if (node) 282 return io_import_fixed(rw, iter, node->buf, ubuf, len); 283 284 return -EFAULT; 285 } 286 EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); 287 288 void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd) 289 { 290 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 291 292 io_req_queue_iowq(req); 293 } 294 295 static inline int io_uring_cmd_getsockopt(struct socket *sock, 296 struct io_uring_cmd *cmd, 297 unsigned int issue_flags) 298 { 299 bool compat = !!(issue_flags & IO_URING_F_COMPAT); 300 int optlen, optname, level, err; 301 void __user *optval; 302 303 level = READ_ONCE(cmd->sqe->level); 304 if (level != SOL_SOCKET) 305 return -EOPNOTSUPP; 306 307 optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval)); 308 optname = READ_ONCE(cmd->sqe->optname); 309 optlen = READ_ONCE(cmd->sqe->optlen); 310 311 err = do_sock_getsockopt(sock, compat, level, optname, 312 USER_SOCKPTR(optval), 313 KERNEL_SOCKPTR(&optlen)); 314 if (err) 315 return err; 316 317 /* On success, return optlen */ 318 return optlen; 319 } 320 321 static inline int io_uring_cmd_setsockopt(struct socket *sock, 322 struct io_uring_cmd *cmd, 323 unsigned int issue_flags) 324 { 325 bool compat = !!(issue_flags & IO_URING_F_COMPAT); 326 int optname, optlen, level; 327 void __user *optval; 328 sockptr_t optval_s; 329 330 optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval)); 331 optname = READ_ONCE(cmd->sqe->optname); 332 optlen = READ_ONCE(cmd->sqe->optlen); 333 level = READ_ONCE(cmd->sqe->level); 334 optval_s = USER_SOCKPTR(optval); 335 336 return do_sock_setsockopt(sock, compat, level, optname, optval_s, 337 optlen); 338 } 339 340 #if defined(CONFIG_NET) 341 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) 342 { 343 struct socket *sock = cmd->file->private_data; 344 struct sock *sk = sock->sk; 345 struct proto *prot = READ_ONCE(sk->sk_prot); 346 int ret, arg = 0; 347 348 if (!prot || !prot->ioctl) 349 return -EOPNOTSUPP; 350 351 switch (cmd->cmd_op) { 352 case SOCKET_URING_OP_SIOCINQ: 353 ret = prot->ioctl(sk, SIOCINQ, &arg); 354 if (ret) 355 return ret; 356 return arg; 357 case SOCKET_URING_OP_SIOCOUTQ: 358 ret = prot->ioctl(sk, SIOCOUTQ, &arg); 359 if (ret) 360 return ret; 361 return arg; 362 case SOCKET_URING_OP_GETSOCKOPT: 363 return io_uring_cmd_getsockopt(sock, cmd, issue_flags); 364 case SOCKET_URING_OP_SETSOCKOPT: 365 return io_uring_cmd_setsockopt(sock, cmd, issue_flags); 366 default: 367 return -EOPNOTSUPP; 368 } 369 } 370 EXPORT_SYMBOL_GPL(io_uring_cmd_sock); 371 #endif 372