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