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/security.h> 7 #include <linux/nospec.h> 8 9 #include <uapi/linux/io_uring.h> 10 11 #include "io_uring.h" 12 #include "alloc_cache.h" 13 #include "rsrc.h" 14 #include "kbuf.h" 15 #include "uring_cmd.h" 16 #include "poll.h" 17 18 void io_cmd_cache_free(const void *entry) 19 { 20 struct io_async_cmd *ac = (struct io_async_cmd *)entry; 21 22 io_vec_free(&ac->vec); 23 kfree(ac); 24 } 25 26 static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags) 27 { 28 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 29 struct io_async_cmd *ac = req->async_data; 30 31 if (issue_flags & IO_URING_F_UNLOCKED) 32 return; 33 34 io_alloc_cache_vec_kasan(&ac->vec); 35 if (ac->vec.nr > IO_VEC_CACHE_SOFT_CAP) 36 io_vec_free(&ac->vec); 37 38 if (io_alloc_cache_put(&req->ctx->cmd_cache, ac)) { 39 ioucmd->sqe = NULL; 40 io_req_async_data_clear(req, REQ_F_NEED_CLEANUP); 41 } else { 42 io_vec_free(&ac->vec); 43 } 44 } 45 46 void io_uring_cmd_cleanup(struct io_kiocb *req) 47 { 48 io_req_uring_cleanup(req, 0); 49 } 50 51 bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx, 52 struct io_uring_task *tctx) 53 { 54 struct hlist_node *tmp; 55 struct io_kiocb *req; 56 bool ret = false; 57 58 lockdep_assert_held(&ctx->uring_lock); 59 60 hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd, 61 hash_node) { 62 struct io_uring_cmd *cmd = io_kiocb_to_cmd(req, 63 struct io_uring_cmd); 64 struct file *file = req->file; 65 66 if (tctx && req->tctx != tctx) 67 continue; 68 69 if (cmd->flags & IORING_URING_CMD_CANCELABLE) { 70 file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL | 71 IO_URING_F_COMPLETE_DEFER); 72 ret = true; 73 } 74 } 75 io_submit_flush_completions(ctx); 76 return ret; 77 } 78 79 static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd, 80 unsigned int issue_flags) 81 { 82 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 83 struct io_ring_ctx *ctx = req->ctx; 84 85 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) 86 return; 87 88 cmd->flags &= ~IORING_URING_CMD_CANCELABLE; 89 io_ring_submit_lock(ctx, issue_flags); 90 hlist_del_init(&req->hash_node); 91 io_ring_submit_unlock(ctx, issue_flags); 92 } 93 94 /* 95 * Mark this command as cancelable, then io_uring_try_cancel_uring_cmd() 96 * will try to cancel this issued command by sending ->uring_cmd() with 97 * issue_flags of IO_URING_F_CANCEL. 98 * 99 * The command is guaranteed to not be done when calling ->uring_cmd() 100 * with IO_URING_F_CANCEL, but it is driver's responsibility to deal 101 * with race between io_uring canceling and normal completion. 102 */ 103 void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, 104 unsigned int issue_flags) 105 { 106 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 107 struct io_ring_ctx *ctx = req->ctx; 108 109 /* 110 * Doing cancelations on IOPOLL requests are not supported. Both 111 * because they can't get canceled in the block stack, but also 112 * because iopoll completion data overlaps with the hash_node used 113 * for tracking. 114 */ 115 if (req->flags & REQ_F_IOPOLL) 116 return; 117 118 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) { 119 cmd->flags |= IORING_URING_CMD_CANCELABLE; 120 io_ring_submit_lock(ctx, issue_flags); 121 hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd); 122 io_ring_submit_unlock(ctx, issue_flags); 123 } 124 } 125 EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable); 126 127 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, 128 io_req_tw_func_t task_work_cb, 129 unsigned flags) 130 { 131 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 132 133 if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT)) 134 return; 135 136 req->io_task_work.func = task_work_cb; 137 __io_req_task_work_add(req, flags); 138 } 139 EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task); 140 141 /* 142 * Called by consumers of io_uring_cmd, if they originally returned 143 * -EIOCBQUEUED upon receiving the command. 144 */ 145 void __io_uring_cmd_done(struct io_uring_cmd *ioucmd, unsigned issue_flags) 146 { 147 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 148 149 if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT)) 150 return; 151 152 io_uring_cmd_del_cancelable(ioucmd, issue_flags); 153 154 io_req_uring_cleanup(req, issue_flags); 155 if (req->flags & REQ_F_IOPOLL) { 156 /* order with io_do_iopoll() checking ->iopoll_completed */ 157 smp_store_release(&req->iopoll_completed, 1); 158 } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) { 159 if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED)) 160 return; 161 io_req_complete_defer(req); 162 } else { 163 req->io_task_work.func = io_req_task_complete; 164 io_req_task_work_add(req); 165 } 166 } 167 EXPORT_SYMBOL_GPL(__io_uring_cmd_done); 168 169 int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 170 { 171 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 172 struct io_async_cmd *ac; 173 174 if (sqe->__pad1) 175 return -EINVAL; 176 177 ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags); 178 if (ioucmd->flags & ~IORING_URING_CMD_MASK) 179 return -EINVAL; 180 181 if (ioucmd->flags & IORING_URING_CMD_FIXED) { 182 if (ioucmd->flags & IORING_URING_CMD_MULTISHOT) 183 return -EINVAL; 184 req->buf_index = READ_ONCE(sqe->buf_index); 185 } 186 187 if (!!(ioucmd->flags & IORING_URING_CMD_MULTISHOT) != 188 !!(req->flags & REQ_F_BUFFER_SELECT)) 189 return -EINVAL; 190 191 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op); 192 193 ac = io_uring_alloc_async_data(&req->ctx->cmd_cache, req); 194 if (!ac) 195 return -ENOMEM; 196 if (ac->vec.iovec) 197 req->flags |= REQ_F_NEED_CLEANUP; 198 ioucmd->sqe = sqe; 199 return 0; 200 } 201 202 /* 203 * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each 204 * slot. 205 */ 206 static inline size_t uring_sqe_size(struct io_kiocb *req) 207 { 208 if (req->ctx->flags & IORING_SETUP_SQE128 || 209 req->opcode == IORING_OP_URING_CMD128) 210 return 2 * sizeof(struct io_uring_sqe); 211 return sizeof(struct io_uring_sqe); 212 } 213 214 void io_uring_cmd_sqe_copy(struct io_kiocb *req) 215 { 216 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 217 struct io_async_cmd *ac = req->async_data; 218 219 /* Should not happen, as REQ_F_SQE_COPIED covers this */ 220 if (WARN_ON_ONCE(ioucmd->sqe == ac->sqes)) 221 return; 222 memcpy(ac->sqes, ioucmd->sqe, uring_sqe_size(req)); 223 ioucmd->sqe = ac->sqes; 224 } 225 226 int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) 227 { 228 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 229 struct io_ring_ctx *ctx = req->ctx; 230 struct file *file = req->file; 231 int ret; 232 233 if (!file->f_op->uring_cmd) 234 return -EOPNOTSUPP; 235 236 ret = security_uring_cmd(ioucmd); 237 if (ret) 238 return ret; 239 240 if (ctx->flags & IORING_SETUP_SQE128 || 241 req->opcode == IORING_OP_URING_CMD128) 242 issue_flags |= IO_URING_F_SQE128; 243 if (ctx->flags & (IORING_SETUP_CQE32 | IORING_SETUP_CQE_MIXED)) 244 issue_flags |= IO_URING_F_CQE32; 245 if (io_is_compat(ctx)) 246 issue_flags |= IO_URING_F_COMPAT; 247 if (ctx->flags & IORING_SETUP_IOPOLL && file->f_op->uring_cmd_iopoll) { 248 req->flags |= REQ_F_IOPOLL; 249 issue_flags |= IO_URING_F_IOPOLL; 250 req->iopoll_completed = 0; 251 if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { 252 /* make sure every req only blocks once */ 253 req->flags &= ~REQ_F_IOPOLL_STATE; 254 req->iopoll_start = ktime_get_ns(); 255 } 256 } 257 258 ret = file->f_op->uring_cmd(ioucmd, issue_flags); 259 if (ret == -EAGAIN) { 260 ioucmd->flags |= IORING_URING_CMD_REISSUE; 261 return ret; 262 } 263 if (ret == -EIOCBQUEUED) 264 return ret; 265 if (ret < 0) 266 req_set_fail(req); 267 io_req_uring_cleanup(req, issue_flags); 268 io_req_set_res(req, ret, 0); 269 return IOU_COMPLETE; 270 } 271 272 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, 273 struct iov_iter *iter, 274 struct io_uring_cmd *ioucmd, 275 unsigned int issue_flags) 276 { 277 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 278 279 if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED))) 280 return -EINVAL; 281 282 return io_import_reg_buf(req, iter, ubuf, len, rw, issue_flags); 283 } 284 EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); 285 286 int io_uring_cmd_import_fixed_vec(struct io_uring_cmd *ioucmd, 287 const struct iovec __user *uvec, 288 size_t uvec_segs, 289 int ddir, struct iov_iter *iter, 290 unsigned issue_flags) 291 { 292 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 293 struct io_async_cmd *ac = req->async_data; 294 int ret; 295 296 if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED))) 297 return -EINVAL; 298 299 ret = io_prep_reg_iovec(req, &ac->vec, uvec, uvec_segs); 300 if (ret) 301 return ret; 302 303 return io_import_reg_vec(ddir, iter, req, &ac->vec, uvec_segs, 304 issue_flags); 305 } 306 EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed_vec); 307 308 void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd) 309 { 310 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 311 312 io_queue_iowq(req); 313 } 314 315 int io_cmd_poll_multishot(struct io_uring_cmd *cmd, 316 unsigned int issue_flags, __poll_t mask) 317 { 318 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 319 int ret; 320 321 if (likely(req->flags & REQ_F_APOLL_MULTISHOT)) 322 return 0; 323 324 req->flags |= REQ_F_APOLL_MULTISHOT; 325 mask &= ~EPOLLONESHOT; 326 327 ret = io_arm_apoll(req, issue_flags, mask); 328 return ret == IO_APOLL_OK ? -EIOCBQUEUED : -ECANCELED; 329 } 330 331 bool io_uring_cmd_post_mshot_cqe32(struct io_uring_cmd *cmd, 332 unsigned int issue_flags, 333 struct io_uring_cqe cqe[2]) 334 { 335 struct io_kiocb *req = cmd_to_io_kiocb(cmd); 336 337 if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_MULTISHOT))) 338 return false; 339 return io_req_post_cqe32(req, cqe); 340 } 341 342 /* 343 * Work with io_uring_mshot_cmd_post_cqe() together for committing the 344 * provided buffer upfront 345 */ 346 struct io_br_sel io_uring_cmd_buffer_select(struct io_uring_cmd *ioucmd, 347 unsigned buf_group, size_t *len, 348 unsigned int issue_flags) 349 { 350 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 351 352 if (!(ioucmd->flags & IORING_URING_CMD_MULTISHOT)) 353 return (struct io_br_sel) { .val = -EINVAL }; 354 355 if (WARN_ON_ONCE(!io_do_buffer_select(req))) 356 return (struct io_br_sel) { .val = -EINVAL }; 357 358 return io_buffer_select(req, len, buf_group, issue_flags); 359 } 360 EXPORT_SYMBOL_GPL(io_uring_cmd_buffer_select); 361 362 /* 363 * Return true if this multishot uring_cmd needs to be completed, otherwise 364 * the event CQE is posted successfully. 365 * 366 * This function must use `struct io_br_sel` returned from 367 * io_uring_cmd_buffer_select() for committing the buffer in the same 368 * uring_cmd submission context. 369 */ 370 bool io_uring_mshot_cmd_post_cqe(struct io_uring_cmd *ioucmd, 371 struct io_br_sel *sel, unsigned int issue_flags) 372 { 373 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); 374 unsigned int cflags = 0; 375 376 if (!(ioucmd->flags & IORING_URING_CMD_MULTISHOT)) 377 return true; 378 379 if (sel->val > 0) { 380 cflags = io_put_kbuf(req, sel->val, sel->buf_list); 381 if (io_req_post_cqe(req, sel->val, cflags | IORING_CQE_F_MORE)) 382 return false; 383 } 384 385 io_kbuf_recycle(req, sel->buf_list, issue_flags); 386 if (sel->val < 0) 387 req_set_fail(req); 388 io_req_set_res(req, sel->val, cflags); 389 return true; 390 } 391 EXPORT_SYMBOL_GPL(io_uring_mshot_cmd_post_cqe); 392