1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/fs.h> 5 #include <linux/file.h> 6 #include <linux/blk-mq.h> 7 #include <linux/mm.h> 8 #include <linux/slab.h> 9 #include <linux/fsnotify.h> 10 #include <linux/poll.h> 11 #include <linux/nospec.h> 12 #include <linux/compat.h> 13 #include <linux/io_uring/cmd.h> 14 #include <linux/indirect_call_wrapper.h> 15 16 #include <uapi/linux/io_uring.h> 17 18 #include "io_uring.h" 19 #include "opdef.h" 20 #include "kbuf.h" 21 #include "alloc_cache.h" 22 #include "rsrc.h" 23 #include "poll.h" 24 #include "rw.h" 25 26 struct io_rw { 27 /* NOTE: kiocb has the file as the first member, so don't do it here */ 28 struct kiocb kiocb; 29 u64 addr; 30 u32 len; 31 rwf_t flags; 32 }; 33 34 static bool io_file_supports_nowait(struct io_kiocb *req, __poll_t mask) 35 { 36 /* If FMODE_NOWAIT is set for a file, we're golden */ 37 if (req->flags & REQ_F_SUPPORT_NOWAIT) 38 return true; 39 /* No FMODE_NOWAIT, if we can poll, check the status */ 40 if (io_file_can_poll(req)) { 41 struct poll_table_struct pt = { ._key = mask }; 42 43 return vfs_poll(req->file, &pt) & mask; 44 } 45 /* No FMODE_NOWAIT support, and file isn't pollable. Tough luck. */ 46 return false; 47 } 48 49 #ifdef CONFIG_COMPAT 50 static int io_iov_compat_buffer_select_prep(struct io_rw *rw) 51 { 52 struct compat_iovec __user *uiov; 53 compat_ssize_t clen; 54 55 uiov = u64_to_user_ptr(rw->addr); 56 if (!access_ok(uiov, sizeof(*uiov))) 57 return -EFAULT; 58 if (__get_user(clen, &uiov->iov_len)) 59 return -EFAULT; 60 if (clen < 0) 61 return -EINVAL; 62 63 rw->len = clen; 64 return 0; 65 } 66 #endif 67 68 static int io_iov_buffer_select_prep(struct io_kiocb *req) 69 { 70 struct iovec __user *uiov; 71 struct iovec iov; 72 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 73 74 if (rw->len != 1) 75 return -EINVAL; 76 77 #ifdef CONFIG_COMPAT 78 if (req->ctx->compat) 79 return io_iov_compat_buffer_select_prep(rw); 80 #endif 81 82 uiov = u64_to_user_ptr(rw->addr); 83 if (copy_from_user(&iov, uiov, sizeof(*uiov))) 84 return -EFAULT; 85 rw->len = iov.iov_len; 86 return 0; 87 } 88 89 static int __io_import_iovec(int ddir, struct io_kiocb *req, 90 struct io_async_rw *io, 91 unsigned int issue_flags) 92 { 93 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 94 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 95 struct iovec *iov; 96 void __user *buf; 97 int nr_segs, ret; 98 size_t sqe_len; 99 100 buf = u64_to_user_ptr(rw->addr); 101 sqe_len = rw->len; 102 103 if (!def->vectored || req->flags & REQ_F_BUFFER_SELECT) { 104 if (io_do_buffer_select(req)) { 105 buf = io_buffer_select(req, &sqe_len, issue_flags); 106 if (!buf) 107 return -ENOBUFS; 108 rw->addr = (unsigned long) buf; 109 rw->len = sqe_len; 110 } 111 112 return import_ubuf(ddir, buf, sqe_len, &io->iter); 113 } 114 115 if (io->free_iovec) { 116 nr_segs = io->free_iov_nr; 117 iov = io->free_iovec; 118 } else { 119 iov = &io->fast_iov; 120 nr_segs = 1; 121 } 122 ret = __import_iovec(ddir, buf, sqe_len, nr_segs, &iov, &io->iter, 123 req->ctx->compat); 124 if (unlikely(ret < 0)) 125 return ret; 126 if (iov) { 127 req->flags |= REQ_F_NEED_CLEANUP; 128 io->free_iov_nr = io->iter.nr_segs; 129 kfree(io->free_iovec); 130 io->free_iovec = iov; 131 } 132 return 0; 133 } 134 135 static inline int io_import_iovec(int rw, struct io_kiocb *req, 136 struct io_async_rw *io, 137 unsigned int issue_flags) 138 { 139 int ret; 140 141 ret = __io_import_iovec(rw, req, io, issue_flags); 142 if (unlikely(ret < 0)) 143 return ret; 144 145 iov_iter_save_state(&io->iter, &io->iter_state); 146 return 0; 147 } 148 149 static void io_rw_recycle(struct io_kiocb *req, unsigned int issue_flags) 150 { 151 struct io_async_rw *rw = req->async_data; 152 153 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 154 return; 155 156 io_alloc_cache_kasan(&rw->free_iovec, &rw->free_iov_nr); 157 if (io_alloc_cache_put(&req->ctx->rw_cache, rw)) { 158 req->async_data = NULL; 159 req->flags &= ~REQ_F_ASYNC_DATA; 160 } 161 } 162 163 static void io_req_rw_cleanup(struct io_kiocb *req, unsigned int issue_flags) 164 { 165 /* 166 * Disable quick recycling for anything that's gone through io-wq. 167 * In theory, this should be fine to cleanup. However, some read or 168 * write iter handling touches the iovec AFTER having called into the 169 * handler, eg to reexpand or revert. This means we can have: 170 * 171 * task io-wq 172 * issue 173 * punt to io-wq 174 * issue 175 * blkdev_write_iter() 176 * ->ki_complete() 177 * io_complete_rw() 178 * queue tw complete 179 * run tw 180 * req_rw_cleanup 181 * iov_iter_count() <- look at iov_iter again 182 * 183 * which can lead to a UAF. This is only possible for io-wq offload 184 * as the cleanup can run in parallel. As io-wq is not the fast path, 185 * just leave cleanup to the end. 186 * 187 * This is really a bug in the core code that does this, any issue 188 * path should assume that a successful (or -EIOCBQUEUED) return can 189 * mean that the underlying data can be gone at any time. But that 190 * should be fixed seperately, and then this check could be killed. 191 */ 192 if (!(req->flags & (REQ_F_REISSUE | REQ_F_REFCOUNT))) { 193 req->flags &= ~REQ_F_NEED_CLEANUP; 194 io_rw_recycle(req, issue_flags); 195 } 196 } 197 198 static int io_rw_alloc_async(struct io_kiocb *req) 199 { 200 struct io_ring_ctx *ctx = req->ctx; 201 struct io_async_rw *rw; 202 203 rw = io_uring_alloc_async_data(&ctx->rw_cache, req); 204 if (!rw) 205 return -ENOMEM; 206 if (rw->free_iovec) 207 req->flags |= REQ_F_NEED_CLEANUP; 208 rw->bytes_done = 0; 209 return 0; 210 } 211 212 static int io_prep_rw_setup(struct io_kiocb *req, int ddir, bool do_import) 213 { 214 struct io_async_rw *rw; 215 216 if (io_rw_alloc_async(req)) 217 return -ENOMEM; 218 219 if (!do_import || io_do_buffer_select(req)) 220 return 0; 221 222 rw = req->async_data; 223 return io_import_iovec(ddir, req, rw, 0); 224 } 225 226 static inline void io_meta_save_state(struct io_async_rw *io) 227 { 228 io->meta_state.seed = io->meta.seed; 229 iov_iter_save_state(&io->meta.iter, &io->meta_state.iter_meta); 230 } 231 232 static inline void io_meta_restore(struct io_async_rw *io, struct kiocb *kiocb) 233 { 234 if (kiocb->ki_flags & IOCB_HAS_METADATA) { 235 io->meta.seed = io->meta_state.seed; 236 iov_iter_restore(&io->meta.iter, &io->meta_state.iter_meta); 237 } 238 } 239 240 static int io_prep_rw_pi(struct io_kiocb *req, struct io_rw *rw, int ddir, 241 u64 attr_ptr, u64 attr_type_mask) 242 { 243 struct io_uring_attr_pi pi_attr; 244 struct io_async_rw *io; 245 int ret; 246 247 if (copy_from_user(&pi_attr, u64_to_user_ptr(attr_ptr), 248 sizeof(pi_attr))) 249 return -EFAULT; 250 251 if (pi_attr.rsvd) 252 return -EINVAL; 253 254 io = req->async_data; 255 io->meta.flags = pi_attr.flags; 256 io->meta.app_tag = pi_attr.app_tag; 257 io->meta.seed = pi_attr.seed; 258 ret = import_ubuf(ddir, u64_to_user_ptr(pi_attr.addr), 259 pi_attr.len, &io->meta.iter); 260 if (unlikely(ret < 0)) 261 return ret; 262 req->flags |= REQ_F_HAS_METADATA; 263 io_meta_save_state(io); 264 return ret; 265 } 266 267 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, 268 int ddir, bool do_import) 269 { 270 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 271 unsigned ioprio; 272 u64 attr_type_mask; 273 int ret; 274 275 rw->kiocb.ki_pos = READ_ONCE(sqe->off); 276 /* used for fixed read/write too - just read unconditionally */ 277 req->buf_index = READ_ONCE(sqe->buf_index); 278 279 ioprio = READ_ONCE(sqe->ioprio); 280 if (ioprio) { 281 ret = ioprio_check_cap(ioprio); 282 if (ret) 283 return ret; 284 285 rw->kiocb.ki_ioprio = ioprio; 286 } else { 287 rw->kiocb.ki_ioprio = get_current_ioprio(); 288 } 289 rw->kiocb.dio_complete = NULL; 290 rw->kiocb.ki_flags = 0; 291 292 rw->addr = READ_ONCE(sqe->addr); 293 rw->len = READ_ONCE(sqe->len); 294 rw->flags = READ_ONCE(sqe->rw_flags); 295 ret = io_prep_rw_setup(req, ddir, do_import); 296 297 if (unlikely(ret)) 298 return ret; 299 300 attr_type_mask = READ_ONCE(sqe->attr_type_mask); 301 if (attr_type_mask) { 302 u64 attr_ptr; 303 304 /* only PI attribute is supported currently */ 305 if (attr_type_mask != IORING_RW_ATTR_FLAG_PI) 306 return -EINVAL; 307 308 attr_ptr = READ_ONCE(sqe->attr_ptr); 309 ret = io_prep_rw_pi(req, rw, ddir, attr_ptr, attr_type_mask); 310 } 311 return ret; 312 } 313 314 int io_prep_read(struct io_kiocb *req, const struct io_uring_sqe *sqe) 315 { 316 return io_prep_rw(req, sqe, ITER_DEST, true); 317 } 318 319 int io_prep_write(struct io_kiocb *req, const struct io_uring_sqe *sqe) 320 { 321 return io_prep_rw(req, sqe, ITER_SOURCE, true); 322 } 323 324 static int io_prep_rwv(struct io_kiocb *req, const struct io_uring_sqe *sqe, 325 int ddir) 326 { 327 const bool do_import = !(req->flags & REQ_F_BUFFER_SELECT); 328 int ret; 329 330 ret = io_prep_rw(req, sqe, ddir, do_import); 331 if (unlikely(ret)) 332 return ret; 333 if (do_import) 334 return 0; 335 336 /* 337 * Have to do this validation here, as this is in io_read() rw->len 338 * might have chanaged due to buffer selection 339 */ 340 return io_iov_buffer_select_prep(req); 341 } 342 343 int io_prep_readv(struct io_kiocb *req, const struct io_uring_sqe *sqe) 344 { 345 return io_prep_rwv(req, sqe, ITER_DEST); 346 } 347 348 int io_prep_writev(struct io_kiocb *req, const struct io_uring_sqe *sqe) 349 { 350 return io_prep_rwv(req, sqe, ITER_SOURCE); 351 } 352 353 static int io_prep_rw_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe, 354 int ddir) 355 { 356 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 357 struct io_ring_ctx *ctx = req->ctx; 358 struct io_rsrc_node *node; 359 struct io_async_rw *io; 360 int ret; 361 362 ret = io_prep_rw(req, sqe, ddir, false); 363 if (unlikely(ret)) 364 return ret; 365 366 node = io_rsrc_node_lookup(&ctx->buf_table, req->buf_index); 367 if (!node) 368 return -EFAULT; 369 io_req_assign_buf_node(req, node); 370 371 io = req->async_data; 372 ret = io_import_fixed(ddir, &io->iter, node->buf, rw->addr, rw->len); 373 iov_iter_save_state(&io->iter, &io->iter_state); 374 return ret; 375 } 376 377 int io_prep_read_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) 378 { 379 return io_prep_rw_fixed(req, sqe, ITER_DEST); 380 } 381 382 int io_prep_write_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe) 383 { 384 return io_prep_rw_fixed(req, sqe, ITER_SOURCE); 385 } 386 387 /* 388 * Multishot read is prepared just like a normal read/write request, only 389 * difference is that we set the MULTISHOT flag. 390 */ 391 int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) 392 { 393 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 394 int ret; 395 396 /* must be used with provided buffers */ 397 if (!(req->flags & REQ_F_BUFFER_SELECT)) 398 return -EINVAL; 399 400 ret = io_prep_rw(req, sqe, ITER_DEST, false); 401 if (unlikely(ret)) 402 return ret; 403 404 if (rw->addr || rw->len) 405 return -EINVAL; 406 407 req->flags |= REQ_F_APOLL_MULTISHOT; 408 return 0; 409 } 410 411 void io_readv_writev_cleanup(struct io_kiocb *req) 412 { 413 lockdep_assert_held(&req->ctx->uring_lock); 414 io_rw_recycle(req, 0); 415 } 416 417 static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) 418 { 419 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 420 421 if (rw->kiocb.ki_pos != -1) 422 return &rw->kiocb.ki_pos; 423 424 if (!(req->file->f_mode & FMODE_STREAM)) { 425 req->flags |= REQ_F_CUR_POS; 426 rw->kiocb.ki_pos = req->file->f_pos; 427 return &rw->kiocb.ki_pos; 428 } 429 430 rw->kiocb.ki_pos = 0; 431 return NULL; 432 } 433 434 static bool io_rw_should_reissue(struct io_kiocb *req) 435 { 436 #ifdef CONFIG_BLOCK 437 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 438 umode_t mode = file_inode(req->file)->i_mode; 439 struct io_async_rw *io = req->async_data; 440 struct io_ring_ctx *ctx = req->ctx; 441 442 if (!S_ISBLK(mode) && !S_ISREG(mode)) 443 return false; 444 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && 445 !(ctx->flags & IORING_SETUP_IOPOLL))) 446 return false; 447 /* 448 * If ref is dying, we might be running poll reap from the exit work. 449 * Don't attempt to reissue from that path, just let it fail with 450 * -EAGAIN. 451 */ 452 if (percpu_ref_is_dying(&ctx->refs)) 453 return false; 454 455 io_meta_restore(io, &rw->kiocb); 456 iov_iter_restore(&io->iter, &io->iter_state); 457 return true; 458 #else 459 return false; 460 #endif 461 } 462 463 static void io_req_end_write(struct io_kiocb *req) 464 { 465 if (req->flags & REQ_F_ISREG) { 466 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 467 468 kiocb_end_write(&rw->kiocb); 469 } 470 } 471 472 /* 473 * Trigger the notifications after having done some IO, and finish the write 474 * accounting, if any. 475 */ 476 static void io_req_io_end(struct io_kiocb *req) 477 { 478 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 479 480 if (rw->kiocb.ki_flags & IOCB_WRITE) { 481 io_req_end_write(req); 482 fsnotify_modify(req->file); 483 } else { 484 fsnotify_access(req->file); 485 } 486 } 487 488 static void __io_complete_rw_common(struct io_kiocb *req, long res) 489 { 490 if (res == req->cqe.res) 491 return; 492 if (res == -EAGAIN && io_rw_should_reissue(req)) { 493 req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE; 494 } else { 495 req_set_fail(req); 496 req->cqe.res = res; 497 } 498 } 499 500 static inline int io_fixup_rw_res(struct io_kiocb *req, long res) 501 { 502 struct io_async_rw *io = req->async_data; 503 504 /* add previously done IO, if any */ 505 if (req_has_async_data(req) && io->bytes_done > 0) { 506 if (res < 0) 507 res = io->bytes_done; 508 else 509 res += io->bytes_done; 510 } 511 return res; 512 } 513 514 void io_req_rw_complete(struct io_kiocb *req, struct io_tw_state *ts) 515 { 516 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 517 struct kiocb *kiocb = &rw->kiocb; 518 519 if ((kiocb->ki_flags & IOCB_DIO_CALLER_COMP) && kiocb->dio_complete) { 520 long res = kiocb->dio_complete(rw->kiocb.private); 521 522 io_req_set_res(req, io_fixup_rw_res(req, res), 0); 523 } 524 525 io_req_io_end(req); 526 527 if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)) 528 req->cqe.flags |= io_put_kbuf(req, req->cqe.res, 0); 529 530 io_req_rw_cleanup(req, 0); 531 io_req_task_complete(req, ts); 532 } 533 534 static void io_complete_rw(struct kiocb *kiocb, long res) 535 { 536 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb); 537 struct io_kiocb *req = cmd_to_io_kiocb(rw); 538 539 if (!kiocb->dio_complete || !(kiocb->ki_flags & IOCB_DIO_CALLER_COMP)) { 540 __io_complete_rw_common(req, res); 541 io_req_set_res(req, io_fixup_rw_res(req, res), 0); 542 } 543 req->io_task_work.func = io_req_rw_complete; 544 __io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE); 545 } 546 547 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) 548 { 549 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb); 550 struct io_kiocb *req = cmd_to_io_kiocb(rw); 551 552 if (kiocb->ki_flags & IOCB_WRITE) 553 io_req_end_write(req); 554 if (unlikely(res != req->cqe.res)) { 555 if (res == -EAGAIN && io_rw_should_reissue(req)) { 556 req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE; 557 return; 558 } 559 req->cqe.res = res; 560 } 561 562 /* order with io_iopoll_complete() checking ->iopoll_completed */ 563 smp_store_release(&req->iopoll_completed, 1); 564 } 565 566 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) 567 { 568 /* IO was queued async, completion will happen later */ 569 if (ret == -EIOCBQUEUED) 570 return; 571 572 /* transform internal restart error codes */ 573 if (unlikely(ret < 0)) { 574 switch (ret) { 575 case -ERESTARTSYS: 576 case -ERESTARTNOINTR: 577 case -ERESTARTNOHAND: 578 case -ERESTART_RESTARTBLOCK: 579 /* 580 * We can't just restart the syscall, since previously 581 * submitted sqes may already be in progress. Just fail 582 * this IO with EINTR. 583 */ 584 ret = -EINTR; 585 break; 586 } 587 } 588 589 INDIRECT_CALL_2(kiocb->ki_complete, io_complete_rw_iopoll, 590 io_complete_rw, kiocb, ret); 591 } 592 593 static int kiocb_done(struct io_kiocb *req, ssize_t ret, 594 unsigned int issue_flags) 595 { 596 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 597 unsigned final_ret = io_fixup_rw_res(req, ret); 598 599 if (ret >= 0 && req->flags & REQ_F_CUR_POS) 600 req->file->f_pos = rw->kiocb.ki_pos; 601 if (ret >= 0 && (rw->kiocb.ki_complete == io_complete_rw)) { 602 __io_complete_rw_common(req, ret); 603 /* 604 * Safe to call io_end from here as we're inline 605 * from the submission path. 606 */ 607 io_req_io_end(req); 608 io_req_set_res(req, final_ret, io_put_kbuf(req, ret, issue_flags)); 609 io_req_rw_cleanup(req, issue_flags); 610 return IOU_OK; 611 } else { 612 io_rw_done(&rw->kiocb, ret); 613 } 614 615 return IOU_ISSUE_SKIP_COMPLETE; 616 } 617 618 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) 619 { 620 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; 621 } 622 623 /* 624 * For files that don't have ->read_iter() and ->write_iter(), handle them 625 * by looping over ->read() or ->write() manually. 626 */ 627 static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter) 628 { 629 struct kiocb *kiocb = &rw->kiocb; 630 struct file *file = kiocb->ki_filp; 631 ssize_t ret = 0; 632 loff_t *ppos; 633 634 /* 635 * Don't support polled IO through this interface, and we can't 636 * support non-blocking either. For the latter, this just causes 637 * the kiocb to be handled from an async context. 638 */ 639 if (kiocb->ki_flags & IOCB_HIPRI) 640 return -EOPNOTSUPP; 641 if ((kiocb->ki_flags & IOCB_NOWAIT) && 642 !(kiocb->ki_filp->f_flags & O_NONBLOCK)) 643 return -EAGAIN; 644 645 ppos = io_kiocb_ppos(kiocb); 646 647 while (iov_iter_count(iter)) { 648 void __user *addr; 649 size_t len; 650 ssize_t nr; 651 652 if (iter_is_ubuf(iter)) { 653 addr = iter->ubuf + iter->iov_offset; 654 len = iov_iter_count(iter); 655 } else if (!iov_iter_is_bvec(iter)) { 656 addr = iter_iov_addr(iter); 657 len = iter_iov_len(iter); 658 } else { 659 addr = u64_to_user_ptr(rw->addr); 660 len = rw->len; 661 } 662 663 if (ddir == READ) 664 nr = file->f_op->read(file, addr, len, ppos); 665 else 666 nr = file->f_op->write(file, addr, len, ppos); 667 668 if (nr < 0) { 669 if (!ret) 670 ret = nr; 671 break; 672 } 673 ret += nr; 674 if (!iov_iter_is_bvec(iter)) { 675 iov_iter_advance(iter, nr); 676 } else { 677 rw->addr += nr; 678 rw->len -= nr; 679 if (!rw->len) 680 break; 681 } 682 if (nr != len) 683 break; 684 } 685 686 return ret; 687 } 688 689 /* 690 * This is our waitqueue callback handler, registered through __folio_lock_async() 691 * when we initially tried to do the IO with the iocb armed our waitqueue. 692 * This gets called when the page is unlocked, and we generally expect that to 693 * happen when the page IO is completed and the page is now uptodate. This will 694 * queue a task_work based retry of the operation, attempting to copy the data 695 * again. If the latter fails because the page was NOT uptodate, then we will 696 * do a thread based blocking retry of the operation. That's the unexpected 697 * slow path. 698 */ 699 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, 700 int sync, void *arg) 701 { 702 struct wait_page_queue *wpq; 703 struct io_kiocb *req = wait->private; 704 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 705 struct wait_page_key *key = arg; 706 707 wpq = container_of(wait, struct wait_page_queue, wait); 708 709 if (!wake_page_match(wpq, key)) 710 return 0; 711 712 rw->kiocb.ki_flags &= ~IOCB_WAITQ; 713 list_del_init(&wait->entry); 714 io_req_task_queue(req); 715 return 1; 716 } 717 718 /* 719 * This controls whether a given IO request should be armed for async page 720 * based retry. If we return false here, the request is handed to the async 721 * worker threads for retry. If we're doing buffered reads on a regular file, 722 * we prepare a private wait_page_queue entry and retry the operation. This 723 * will either succeed because the page is now uptodate and unlocked, or it 724 * will register a callback when the page is unlocked at IO completion. Through 725 * that callback, io_uring uses task_work to setup a retry of the operation. 726 * That retry will attempt the buffered read again. The retry will generally 727 * succeed, or in rare cases where it fails, we then fall back to using the 728 * async worker threads for a blocking retry. 729 */ 730 static bool io_rw_should_retry(struct io_kiocb *req) 731 { 732 struct io_async_rw *io = req->async_data; 733 struct wait_page_queue *wait = &io->wpq; 734 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 735 struct kiocb *kiocb = &rw->kiocb; 736 737 /* 738 * Never retry for NOWAIT or a request with metadata, we just complete 739 * with -EAGAIN. 740 */ 741 if (req->flags & (REQ_F_NOWAIT | REQ_F_HAS_METADATA)) 742 return false; 743 744 /* Only for buffered IO */ 745 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) 746 return false; 747 748 /* 749 * just use poll if we can, and don't attempt if the fs doesn't 750 * support callback based unlocks 751 */ 752 if (io_file_can_poll(req) || 753 !(req->file->f_op->fop_flags & FOP_BUFFER_RASYNC)) 754 return false; 755 756 wait->wait.func = io_async_buf_func; 757 wait->wait.private = req; 758 wait->wait.flags = 0; 759 INIT_LIST_HEAD(&wait->wait.entry); 760 kiocb->ki_flags |= IOCB_WAITQ; 761 kiocb->ki_flags &= ~IOCB_NOWAIT; 762 kiocb->ki_waitq = wait; 763 return true; 764 } 765 766 static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter) 767 { 768 struct file *file = rw->kiocb.ki_filp; 769 770 if (likely(file->f_op->read_iter)) 771 return file->f_op->read_iter(&rw->kiocb, iter); 772 else if (file->f_op->read) 773 return loop_rw_iter(READ, rw, iter); 774 else 775 return -EINVAL; 776 } 777 778 static bool need_complete_io(struct io_kiocb *req) 779 { 780 return req->flags & REQ_F_ISREG || 781 S_ISBLK(file_inode(req->file)->i_mode); 782 } 783 784 static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type) 785 { 786 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 787 struct kiocb *kiocb = &rw->kiocb; 788 struct io_ring_ctx *ctx = req->ctx; 789 struct file *file = req->file; 790 int ret; 791 792 if (unlikely(!(file->f_mode & mode))) 793 return -EBADF; 794 795 if (!(req->flags & REQ_F_FIXED_FILE)) 796 req->flags |= io_file_get_flags(file); 797 798 kiocb->ki_flags = file->f_iocb_flags; 799 ret = kiocb_set_rw_flags(kiocb, rw->flags, rw_type); 800 if (unlikely(ret)) 801 return ret; 802 kiocb->ki_flags |= IOCB_ALLOC_CACHE; 803 804 /* 805 * If the file is marked O_NONBLOCK, still allow retry for it if it 806 * supports async. Otherwise it's impossible to use O_NONBLOCK files 807 * reliably. If not, or it IOCB_NOWAIT is set, don't retry. 808 */ 809 if (kiocb->ki_flags & IOCB_NOWAIT || 810 ((file->f_flags & O_NONBLOCK && !(req->flags & REQ_F_SUPPORT_NOWAIT)))) 811 req->flags |= REQ_F_NOWAIT; 812 813 if (ctx->flags & IORING_SETUP_IOPOLL) { 814 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) 815 return -EOPNOTSUPP; 816 817 kiocb->private = NULL; 818 kiocb->ki_flags |= IOCB_HIPRI; 819 kiocb->ki_complete = io_complete_rw_iopoll; 820 req->iopoll_completed = 0; 821 if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { 822 /* make sure every req only blocks once*/ 823 req->flags &= ~REQ_F_IOPOLL_STATE; 824 req->iopoll_start = ktime_get_ns(); 825 } 826 } else { 827 if (kiocb->ki_flags & IOCB_HIPRI) 828 return -EINVAL; 829 kiocb->ki_complete = io_complete_rw; 830 } 831 832 if (req->flags & REQ_F_HAS_METADATA) { 833 struct io_async_rw *io = req->async_data; 834 835 /* 836 * We have a union of meta fields with wpq used for buffered-io 837 * in io_async_rw, so fail it here. 838 */ 839 if (!(req->file->f_flags & O_DIRECT)) 840 return -EOPNOTSUPP; 841 kiocb->ki_flags |= IOCB_HAS_METADATA; 842 kiocb->private = &io->meta; 843 } 844 845 return 0; 846 } 847 848 static int __io_read(struct io_kiocb *req, unsigned int issue_flags) 849 { 850 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 851 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 852 struct io_async_rw *io = req->async_data; 853 struct kiocb *kiocb = &rw->kiocb; 854 ssize_t ret; 855 loff_t *ppos; 856 857 if (io_do_buffer_select(req)) { 858 ret = io_import_iovec(ITER_DEST, req, io, issue_flags); 859 if (unlikely(ret < 0)) 860 return ret; 861 } 862 ret = io_rw_init_file(req, FMODE_READ, READ); 863 if (unlikely(ret)) 864 return ret; 865 req->cqe.res = iov_iter_count(&io->iter); 866 867 if (force_nonblock) { 868 /* If the file doesn't support async, just async punt */ 869 if (unlikely(!io_file_supports_nowait(req, EPOLLIN))) 870 return -EAGAIN; 871 kiocb->ki_flags |= IOCB_NOWAIT; 872 } else { 873 /* Ensure we clear previously set non-block flag */ 874 kiocb->ki_flags &= ~IOCB_NOWAIT; 875 } 876 877 ppos = io_kiocb_update_pos(req); 878 879 ret = rw_verify_area(READ, req->file, ppos, req->cqe.res); 880 if (unlikely(ret)) 881 return ret; 882 883 ret = io_iter_do_read(rw, &io->iter); 884 885 /* 886 * Some file systems like to return -EOPNOTSUPP for an IOCB_NOWAIT 887 * issue, even though they should be returning -EAGAIN. To be safe, 888 * retry from blocking context for either. 889 */ 890 if (ret == -EOPNOTSUPP && force_nonblock) 891 ret = -EAGAIN; 892 893 if (ret == -EAGAIN) { 894 /* If we can poll, just do that. */ 895 if (io_file_can_poll(req)) 896 return -EAGAIN; 897 /* IOPOLL retry should happen for io-wq threads */ 898 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) 899 goto done; 900 /* no retry on NONBLOCK nor RWF_NOWAIT */ 901 if (req->flags & REQ_F_NOWAIT) 902 goto done; 903 ret = 0; 904 } else if (ret == -EIOCBQUEUED) { 905 return IOU_ISSUE_SKIP_COMPLETE; 906 } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock || 907 (req->flags & REQ_F_NOWAIT) || !need_complete_io(req)) { 908 /* read all, failed, already did sync or don't want to retry */ 909 goto done; 910 } 911 912 /* 913 * Don't depend on the iter state matching what was consumed, or being 914 * untouched in case of error. Restore it and we'll advance it 915 * manually if we need to. 916 */ 917 iov_iter_restore(&io->iter, &io->iter_state); 918 io_meta_restore(io, kiocb); 919 920 do { 921 /* 922 * We end up here because of a partial read, either from 923 * above or inside this loop. Advance the iter by the bytes 924 * that were consumed. 925 */ 926 iov_iter_advance(&io->iter, ret); 927 if (!iov_iter_count(&io->iter)) 928 break; 929 io->bytes_done += ret; 930 iov_iter_save_state(&io->iter, &io->iter_state); 931 932 /* if we can retry, do so with the callbacks armed */ 933 if (!io_rw_should_retry(req)) { 934 kiocb->ki_flags &= ~IOCB_WAITQ; 935 return -EAGAIN; 936 } 937 938 req->cqe.res = iov_iter_count(&io->iter); 939 /* 940 * Now retry read with the IOCB_WAITQ parts set in the iocb. If 941 * we get -EIOCBQUEUED, then we'll get a notification when the 942 * desired page gets unlocked. We can also get a partial read 943 * here, and if we do, then just retry at the new offset. 944 */ 945 ret = io_iter_do_read(rw, &io->iter); 946 if (ret == -EIOCBQUEUED) 947 return IOU_ISSUE_SKIP_COMPLETE; 948 /* we got some bytes, but not all. retry. */ 949 kiocb->ki_flags &= ~IOCB_WAITQ; 950 iov_iter_restore(&io->iter, &io->iter_state); 951 } while (ret > 0); 952 done: 953 /* it's faster to check here then delegate to kfree */ 954 return ret; 955 } 956 957 int io_read(struct io_kiocb *req, unsigned int issue_flags) 958 { 959 int ret; 960 961 ret = __io_read(req, issue_flags); 962 if (ret >= 0) 963 return kiocb_done(req, ret, issue_flags); 964 965 return ret; 966 } 967 968 int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags) 969 { 970 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 971 unsigned int cflags = 0; 972 int ret; 973 974 /* 975 * Multishot MUST be used on a pollable file 976 */ 977 if (!io_file_can_poll(req)) 978 return -EBADFD; 979 980 ret = __io_read(req, issue_flags); 981 982 /* 983 * If we get -EAGAIN, recycle our buffer and just let normal poll 984 * handling arm it. 985 */ 986 if (ret == -EAGAIN) { 987 /* 988 * Reset rw->len to 0 again to avoid clamping future mshot 989 * reads, in case the buffer size varies. 990 */ 991 if (io_kbuf_recycle(req, issue_flags)) 992 rw->len = 0; 993 if (issue_flags & IO_URING_F_MULTISHOT) 994 return IOU_ISSUE_SKIP_COMPLETE; 995 return -EAGAIN; 996 } else if (ret <= 0) { 997 io_kbuf_recycle(req, issue_flags); 998 if (ret < 0) 999 req_set_fail(req); 1000 } else if (!(req->flags & REQ_F_APOLL_MULTISHOT)) { 1001 cflags = io_put_kbuf(req, ret, issue_flags); 1002 } else { 1003 /* 1004 * Any successful return value will keep the multishot read 1005 * armed, if it's still set. Put our buffer and post a CQE. If 1006 * we fail to post a CQE, or multishot is no longer set, then 1007 * jump to the termination path. This request is then done. 1008 */ 1009 cflags = io_put_kbuf(req, ret, issue_flags); 1010 rw->len = 0; /* similarly to above, reset len to 0 */ 1011 1012 if (io_req_post_cqe(req, ret, cflags | IORING_CQE_F_MORE)) { 1013 if (issue_flags & IO_URING_F_MULTISHOT) { 1014 /* 1015 * Force retry, as we might have more data to 1016 * be read and otherwise it won't get retried 1017 * until (if ever) another poll is triggered. 1018 */ 1019 io_poll_multishot_retry(req); 1020 return IOU_ISSUE_SKIP_COMPLETE; 1021 } 1022 return -EAGAIN; 1023 } 1024 } 1025 1026 /* 1027 * Either an error, or we've hit overflow posting the CQE. For any 1028 * multishot request, hitting overflow will terminate it. 1029 */ 1030 io_req_set_res(req, ret, cflags); 1031 io_req_rw_cleanup(req, issue_flags); 1032 if (issue_flags & IO_URING_F_MULTISHOT) 1033 return IOU_STOP_MULTISHOT; 1034 return IOU_OK; 1035 } 1036 1037 static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb) 1038 { 1039 struct inode *inode; 1040 bool ret; 1041 1042 if (!(req->flags & REQ_F_ISREG)) 1043 return true; 1044 if (!(kiocb->ki_flags & IOCB_NOWAIT)) { 1045 kiocb_start_write(kiocb); 1046 return true; 1047 } 1048 1049 inode = file_inode(kiocb->ki_filp); 1050 ret = sb_start_write_trylock(inode->i_sb); 1051 if (ret) 1052 __sb_writers_release(inode->i_sb, SB_FREEZE_WRITE); 1053 return ret; 1054 } 1055 1056 int io_write(struct io_kiocb *req, unsigned int issue_flags) 1057 { 1058 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; 1059 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 1060 struct io_async_rw *io = req->async_data; 1061 struct kiocb *kiocb = &rw->kiocb; 1062 ssize_t ret, ret2; 1063 loff_t *ppos; 1064 1065 ret = io_rw_init_file(req, FMODE_WRITE, WRITE); 1066 if (unlikely(ret)) 1067 return ret; 1068 req->cqe.res = iov_iter_count(&io->iter); 1069 1070 if (force_nonblock) { 1071 /* If the file doesn't support async, just async punt */ 1072 if (unlikely(!io_file_supports_nowait(req, EPOLLOUT))) 1073 goto ret_eagain; 1074 1075 /* Check if we can support NOWAIT. */ 1076 if (!(kiocb->ki_flags & IOCB_DIRECT) && 1077 !(req->file->f_op->fop_flags & FOP_BUFFER_WASYNC) && 1078 (req->flags & REQ_F_ISREG)) 1079 goto ret_eagain; 1080 1081 kiocb->ki_flags |= IOCB_NOWAIT; 1082 } else { 1083 /* Ensure we clear previously set non-block flag */ 1084 kiocb->ki_flags &= ~IOCB_NOWAIT; 1085 } 1086 1087 ppos = io_kiocb_update_pos(req); 1088 1089 ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res); 1090 if (unlikely(ret)) 1091 return ret; 1092 1093 if (unlikely(!io_kiocb_start_write(req, kiocb))) 1094 return -EAGAIN; 1095 kiocb->ki_flags |= IOCB_WRITE; 1096 1097 if (likely(req->file->f_op->write_iter)) 1098 ret2 = req->file->f_op->write_iter(kiocb, &io->iter); 1099 else if (req->file->f_op->write) 1100 ret2 = loop_rw_iter(WRITE, rw, &io->iter); 1101 else 1102 ret2 = -EINVAL; 1103 1104 /* 1105 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just 1106 * retry them without IOCB_NOWAIT. 1107 */ 1108 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) 1109 ret2 = -EAGAIN; 1110 /* no retry on NONBLOCK nor RWF_NOWAIT */ 1111 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) 1112 goto done; 1113 if (!force_nonblock || ret2 != -EAGAIN) { 1114 /* IOPOLL retry should happen for io-wq threads */ 1115 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) 1116 goto ret_eagain; 1117 1118 if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) { 1119 trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2, 1120 req->cqe.res, ret2); 1121 1122 /* This is a partial write. The file pos has already been 1123 * updated, setup the async struct to complete the request 1124 * in the worker. Also update bytes_done to account for 1125 * the bytes already written. 1126 */ 1127 iov_iter_save_state(&io->iter, &io->iter_state); 1128 io->bytes_done += ret2; 1129 1130 if (kiocb->ki_flags & IOCB_WRITE) 1131 io_req_end_write(req); 1132 return -EAGAIN; 1133 } 1134 done: 1135 return kiocb_done(req, ret2, issue_flags); 1136 } else { 1137 ret_eagain: 1138 iov_iter_restore(&io->iter, &io->iter_state); 1139 io_meta_restore(io, kiocb); 1140 if (kiocb->ki_flags & IOCB_WRITE) 1141 io_req_end_write(req); 1142 return -EAGAIN; 1143 } 1144 } 1145 1146 void io_rw_fail(struct io_kiocb *req) 1147 { 1148 int res; 1149 1150 res = io_fixup_rw_res(req, req->cqe.res); 1151 io_req_set_res(req, res, req->cqe.flags); 1152 } 1153 1154 static int io_uring_classic_poll(struct io_kiocb *req, struct io_comp_batch *iob, 1155 unsigned int poll_flags) 1156 { 1157 struct file *file = req->file; 1158 1159 if (req->opcode == IORING_OP_URING_CMD) { 1160 struct io_uring_cmd *ioucmd; 1161 1162 ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); 1163 return file->f_op->uring_cmd_iopoll(ioucmd, iob, poll_flags); 1164 } else { 1165 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); 1166 1167 return file->f_op->iopoll(&rw->kiocb, iob, poll_flags); 1168 } 1169 } 1170 1171 static u64 io_hybrid_iopoll_delay(struct io_ring_ctx *ctx, struct io_kiocb *req) 1172 { 1173 struct hrtimer_sleeper timer; 1174 enum hrtimer_mode mode; 1175 ktime_t kt; 1176 u64 sleep_time; 1177 1178 if (req->flags & REQ_F_IOPOLL_STATE) 1179 return 0; 1180 1181 if (ctx->hybrid_poll_time == LLONG_MAX) 1182 return 0; 1183 1184 /* Using half the running time to do schedule */ 1185 sleep_time = ctx->hybrid_poll_time / 2; 1186 1187 kt = ktime_set(0, sleep_time); 1188 req->flags |= REQ_F_IOPOLL_STATE; 1189 1190 mode = HRTIMER_MODE_REL; 1191 hrtimer_setup_sleeper_on_stack(&timer, CLOCK_MONOTONIC, mode); 1192 hrtimer_set_expires(&timer.timer, kt); 1193 set_current_state(TASK_INTERRUPTIBLE); 1194 hrtimer_sleeper_start_expires(&timer, mode); 1195 1196 if (timer.task) 1197 io_schedule(); 1198 1199 hrtimer_cancel(&timer.timer); 1200 __set_current_state(TASK_RUNNING); 1201 destroy_hrtimer_on_stack(&timer.timer); 1202 return sleep_time; 1203 } 1204 1205 static int io_uring_hybrid_poll(struct io_kiocb *req, 1206 struct io_comp_batch *iob, unsigned int poll_flags) 1207 { 1208 struct io_ring_ctx *ctx = req->ctx; 1209 u64 runtime, sleep_time; 1210 int ret; 1211 1212 sleep_time = io_hybrid_iopoll_delay(ctx, req); 1213 ret = io_uring_classic_poll(req, iob, poll_flags); 1214 runtime = ktime_get_ns() - req->iopoll_start - sleep_time; 1215 1216 /* 1217 * Use minimum sleep time if we're polling devices with different 1218 * latencies. We could get more completions from the faster ones. 1219 */ 1220 if (ctx->hybrid_poll_time > runtime) 1221 ctx->hybrid_poll_time = runtime; 1222 1223 return ret; 1224 } 1225 1226 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) 1227 { 1228 struct io_wq_work_node *pos, *start, *prev; 1229 unsigned int poll_flags = 0; 1230 DEFINE_IO_COMP_BATCH(iob); 1231 int nr_events = 0; 1232 1233 /* 1234 * Only spin for completions if we don't have multiple devices hanging 1235 * off our complete list. 1236 */ 1237 if (ctx->poll_multi_queue || force_nonspin) 1238 poll_flags |= BLK_POLL_ONESHOT; 1239 1240 wq_list_for_each(pos, start, &ctx->iopoll_list) { 1241 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); 1242 int ret; 1243 1244 /* 1245 * Move completed and retryable entries to our local lists. 1246 * If we find a request that requires polling, break out 1247 * and complete those lists first, if we have entries there. 1248 */ 1249 if (READ_ONCE(req->iopoll_completed)) 1250 break; 1251 1252 if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) 1253 ret = io_uring_hybrid_poll(req, &iob, poll_flags); 1254 else 1255 ret = io_uring_classic_poll(req, &iob, poll_flags); 1256 1257 if (unlikely(ret < 0)) 1258 return ret; 1259 else if (ret) 1260 poll_flags |= BLK_POLL_ONESHOT; 1261 1262 /* iopoll may have completed current req */ 1263 if (!rq_list_empty(&iob.req_list) || 1264 READ_ONCE(req->iopoll_completed)) 1265 break; 1266 } 1267 1268 if (!rq_list_empty(&iob.req_list)) 1269 iob.complete(&iob); 1270 else if (!pos) 1271 return 0; 1272 1273 prev = start; 1274 wq_list_for_each_resume(pos, prev) { 1275 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); 1276 1277 /* order with io_complete_rw_iopoll(), e.g. ->result updates */ 1278 if (!smp_load_acquire(&req->iopoll_completed)) 1279 break; 1280 nr_events++; 1281 req->cqe.flags = io_put_kbuf(req, req->cqe.res, 0); 1282 if (req->opcode != IORING_OP_URING_CMD) 1283 io_req_rw_cleanup(req, 0); 1284 } 1285 if (unlikely(!nr_events)) 1286 return 0; 1287 1288 pos = start ? start->next : ctx->iopoll_list.first; 1289 wq_list_cut(&ctx->iopoll_list, prev, start); 1290 1291 if (WARN_ON_ONCE(!wq_list_empty(&ctx->submit_state.compl_reqs))) 1292 return 0; 1293 ctx->submit_state.compl_reqs.first = pos; 1294 __io_submit_flush_completions(ctx); 1295 return nr_events; 1296 } 1297 1298 void io_rw_cache_free(const void *entry) 1299 { 1300 struct io_async_rw *rw = (struct io_async_rw *) entry; 1301 1302 if (rw->free_iovec) 1303 kfree(rw->free_iovec); 1304 kfree(rw); 1305 } 1306