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