1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
6 #include <linux/blk-mq.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>
16 #include <uapi/linux/io_uring.h>
21 #include "alloc_cache.h"
26 static void io_complete_rw(struct kiocb *kiocb, long res);
27 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res);
30 /* NOTE: kiocb has the file as the first member, so don't do it here */
37 static bool io_file_supports_nowait(struct io_kiocb *req, __poll_t mask)
39 /* If FMODE_NOWAIT is set for a file, we're golden */
40 if (req->flags & REQ_F_SUPPORT_NOWAIT)
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 };
46 return vfs_poll(req->file, &pt) & mask;
48 /* No FMODE_NOWAIT support, and file isn't pollable. Tough luck. */
52 static int io_iov_compat_buffer_select_prep(struct io_rw *rw)
54 struct compat_iovec __user *uiov = u64_to_user_ptr(rw->addr);
55 struct compat_iovec iov;
57 if (copy_from_user(&iov, uiov, sizeof(iov)))
59 rw->len = iov.iov_len;
63 static int io_iov_buffer_select_prep(struct io_kiocb *req)
65 struct iovec __user *uiov;
67 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
72 if (io_is_compat(req->ctx))
73 return io_iov_compat_buffer_select_prep(rw);
75 uiov = u64_to_user_ptr(rw->addr);
76 if (copy_from_user(&iov, uiov, sizeof(*uiov)))
78 rw->len = iov.iov_len;
82 static int io_import_vec(int ddir, struct io_kiocb *req,
83 struct io_async_rw *io,
84 const struct iovec __user *uvec,
98 ret = __import_iovec(ddir, uvec, uvec_segs, nr_segs, &iov, &io->iter,
99 io_is_compat(req->ctx));
100 if (unlikely(ret < 0))
103 req->flags |= REQ_F_NEED_CLEANUP;
104 io_vec_reset_iovec(&io->vec, iov, io->iter.nr_segs);
109 static int __io_import_rw_buffer(int ddir, struct io_kiocb *req,
110 struct io_async_rw *io,
111 unsigned int issue_flags)
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;
118 if (def->vectored && !(req->flags & REQ_F_BUFFER_SELECT))
119 return io_import_vec(ddir, req, io, buf, sqe_len);
121 if (io_do_buffer_select(req)) {
122 buf = io_buffer_select(req, &sqe_len, io->buf_group, issue_flags);
125 rw->addr = (unsigned long) buf;
128 return import_ubuf(ddir, buf, sqe_len, &io->iter);
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)
137 ret = __io_import_rw_buffer(rw, req, io, issue_flags);
138 if (unlikely(ret < 0))
141 iov_iter_save_state(&io->iter, &io->iter_state);
145 static void io_rw_recycle(struct io_kiocb *req, unsigned int issue_flags)
147 struct io_async_rw *rw = req->async_data;
149 if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
152 io_alloc_cache_vec_kasan(&rw->vec);
153 if (rw->vec.nr > IO_VEC_CACHE_SOFT_CAP)
154 io_vec_free(&rw->vec);
156 if (io_alloc_cache_put(&req->ctx->rw_cache, rw)) {
157 req->async_data = NULL;
158 req->flags &= ~REQ_F_ASYNC_DATA;
162 static void io_req_rw_cleanup(struct io_kiocb *req, unsigned int issue_flags)
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:
174 * blkdev_write_iter()
180 * iov_iter_count() <- look at iov_iter again
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.
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.
191 if (!(req->flags & (REQ_F_REISSUE | REQ_F_REFCOUNT))) {
192 req->flags &= ~REQ_F_NEED_CLEANUP;
193 io_rw_recycle(req, issue_flags);
197 static int io_rw_alloc_async(struct io_kiocb *req)
199 struct io_ring_ctx *ctx = req->ctx;
200 struct io_async_rw *rw;
202 rw = io_uring_alloc_async_data(&ctx->rw_cache, req);
206 req->flags |= REQ_F_NEED_CLEANUP;
211 static inline void io_meta_save_state(struct io_async_rw *io)
213 io->meta_state.seed = io->meta.seed;
214 iov_iter_save_state(&io->meta.iter, &io->meta_state.iter_meta);
217 static inline void io_meta_restore(struct io_async_rw *io, struct kiocb *kiocb)
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);
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)
228 struct io_uring_attr_pi pi_attr;
229 struct io_async_rw *io;
232 if (copy_from_user(&pi_attr, u64_to_user_ptr(attr_ptr),
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))
247 req->flags |= REQ_F_HAS_METADATA;
248 io_meta_save_state(io);
252 static int __io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
255 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
256 struct io_async_rw *io;
261 if (io_rw_alloc_async(req))
263 io = req->async_data;
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;
270 ioprio = READ_ONCE(sqe->ioprio);
272 ret = ioprio_check_cap(ioprio);
276 rw->kiocb.ki_ioprio = ioprio;
278 rw->kiocb.ki_ioprio = get_current_ioprio();
280 rw->kiocb.dio_complete = NULL;
281 rw->kiocb.ki_flags = 0;
282 rw->kiocb.ki_write_stream = READ_ONCE(sqe->write_stream);
284 if (req->ctx->flags & IORING_SETUP_IOPOLL)
285 rw->kiocb.ki_complete = io_complete_rw_iopoll;
287 rw->kiocb.ki_complete = io_complete_rw;
289 rw->addr = READ_ONCE(sqe->addr);
290 rw->len = READ_ONCE(sqe->len);
291 rw->flags = READ_ONCE(sqe->rw_flags);
293 attr_type_mask = READ_ONCE(sqe->attr_type_mask);
294 if (attr_type_mask) {
297 /* only PI attribute is supported currently */
298 if (attr_type_mask != IORING_RW_ATTR_FLAG_PI)
301 attr_ptr = READ_ONCE(sqe->attr_ptr);
302 return io_prep_rw_pi(req, rw, ddir, attr_ptr, attr_type_mask);
307 static int io_rw_do_import(struct io_kiocb *req, int ddir)
309 if (io_do_buffer_select(req))
312 return io_import_rw_buffer(ddir, req, req->async_data, 0);
315 static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
320 ret = __io_prep_rw(req, sqe, ddir);
324 return io_rw_do_import(req, ddir);
327 int io_prep_read(struct io_kiocb *req, const struct io_uring_sqe *sqe)
329 return io_prep_rw(req, sqe, ITER_DEST);
332 int io_prep_write(struct io_kiocb *req, const struct io_uring_sqe *sqe)
334 return io_prep_rw(req, sqe, ITER_SOURCE);
337 static int io_prep_rwv(struct io_kiocb *req, const struct io_uring_sqe *sqe,
342 ret = io_prep_rw(req, sqe, ddir);
345 if (!(req->flags & REQ_F_BUFFER_SELECT))
349 * Have to do this validation here, as this is in io_read() rw->len
350 * might have chanaged due to buffer selection
352 return io_iov_buffer_select_prep(req);
355 int io_prep_readv(struct io_kiocb *req, const struct io_uring_sqe *sqe)
357 return io_prep_rwv(req, sqe, ITER_DEST);
360 int io_prep_writev(struct io_kiocb *req, const struct io_uring_sqe *sqe)
362 return io_prep_rwv(req, sqe, ITER_SOURCE);
365 static int io_init_rw_fixed(struct io_kiocb *req, unsigned int issue_flags,
368 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
369 struct io_async_rw *io = req->async_data;
375 ret = io_import_reg_buf(req, &io->iter, rw->addr, rw->len, ddir,
377 iov_iter_save_state(&io->iter, &io->iter_state);
381 int io_prep_read_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
383 return __io_prep_rw(req, sqe, ITER_DEST);
386 int io_prep_write_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
388 return __io_prep_rw(req, sqe, ITER_SOURCE);
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)
395 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
396 unsigned uvec_segs = rw->len;
399 ret = io_import_reg_vec(ddir, &io->iter, req, &io->vec,
400 uvec_segs, issue_flags);
403 iov_iter_save_state(&io->iter, &io->iter_state);
404 req->flags &= ~REQ_F_IMPORT_BUFFER;
408 static int io_rw_prep_reg_vec(struct io_kiocb *req)
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;
414 uvec = u64_to_user_ptr(rw->addr);
415 return io_prep_reg_iovec(req, &io->vec, uvec, rw->len);
418 int io_prep_readv_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
422 ret = __io_prep_rw(req, sqe, ITER_DEST);
425 return io_rw_prep_reg_vec(req);
428 int io_prep_writev_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
432 ret = __io_prep_rw(req, sqe, ITER_SOURCE);
435 return io_rw_prep_reg_vec(req);
439 * Multishot read is prepared just like a normal read/write request, only
440 * difference is that we set the MULTISHOT flag.
442 int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
444 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
447 /* must be used with provided buffers */
448 if (!(req->flags & REQ_F_BUFFER_SELECT))
451 ret = __io_prep_rw(req, sqe, ITER_DEST);
455 if (rw->addr || rw->len)
458 req->flags |= REQ_F_APOLL_MULTISHOT;
462 void io_readv_writev_cleanup(struct io_kiocb *req)
464 lockdep_assert_held(&req->ctx->uring_lock);
465 io_rw_recycle(req, 0);
468 static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req)
470 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
472 if (rw->kiocb.ki_pos != -1)
473 return &rw->kiocb.ki_pos;
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;
481 rw->kiocb.ki_pos = 0;
485 static bool io_rw_should_reissue(struct io_kiocb *req)
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;
493 if (!S_ISBLK(mode) && !S_ISREG(mode))
495 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
496 !(ctx->flags & IORING_SETUP_IOPOLL)))
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
503 if (percpu_ref_is_dying(&ctx->refs))
506 io_meta_restore(io, &rw->kiocb);
507 iov_iter_restore(&io->iter, &io->iter_state);
514 static void io_req_end_write(struct io_kiocb *req)
516 if (req->flags & REQ_F_ISREG) {
517 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
519 kiocb_end_write(&rw->kiocb);
524 * Trigger the notifications after having done some IO, and finish the write
525 * accounting, if any.
527 static void io_req_io_end(struct io_kiocb *req)
529 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
531 if (rw->kiocb.ki_flags & IOCB_WRITE) {
532 io_req_end_write(req);
533 fsnotify_modify(req->file);
535 fsnotify_access(req->file);
539 static void __io_complete_rw_common(struct io_kiocb *req, long res)
541 if (res == req->cqe.res)
543 if (res == -EAGAIN && io_rw_should_reissue(req)) {
544 req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE;
551 static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
553 struct io_async_rw *io = req->async_data;
555 /* add previously done IO, if any */
556 if (req_has_async_data(req) && io->bytes_done > 0) {
558 res = io->bytes_done;
560 res += io->bytes_done;
565 void io_req_rw_complete(struct io_kiocb *req, io_tw_token_t tw)
567 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
568 struct kiocb *kiocb = &rw->kiocb;
570 if ((kiocb->ki_flags & IOCB_DIO_CALLER_COMP) && kiocb->dio_complete) {
571 long res = kiocb->dio_complete(rw->kiocb.private);
573 io_req_set_res(req, io_fixup_rw_res(req, res), 0);
578 if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING))
579 req->cqe.flags |= io_put_kbuf(req, req->cqe.res, 0);
581 io_req_rw_cleanup(req, 0);
582 io_req_task_complete(req, tw);
585 static void io_complete_rw(struct kiocb *kiocb, long res)
587 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
588 struct io_kiocb *req = cmd_to_io_kiocb(rw);
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);
594 req->io_task_work.func = io_req_rw_complete;
595 __io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE);
598 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
600 struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
601 struct io_kiocb *req = cmd_to_io_kiocb(rw);
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;
612 /* order with io_iopoll_complete() checking ->iopoll_completed */
613 smp_store_release(&req->iopoll_completed, 1);
616 static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)
618 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
620 /* IO was queued async, completion will happen later */
621 if (ret == -EIOCBQUEUED)
624 /* transform internal restart error codes */
625 if (unlikely(ret < 0)) {
628 case -ERESTARTNOINTR:
629 case -ERESTARTNOHAND:
630 case -ERESTART_RESTARTBLOCK:
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.
641 if (req->ctx->flags & IORING_SETUP_IOPOLL)
642 io_complete_rw_iopoll(&rw->kiocb, ret);
644 io_complete_rw(&rw->kiocb, ret);
647 static int kiocb_done(struct io_kiocb *req, ssize_t ret,
648 unsigned int issue_flags)
650 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
651 unsigned final_ret = io_fixup_rw_res(req, ret);
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);
658 * Safe to call io_end from here as we're inline
659 * from the submission path.
662 io_req_set_res(req, final_ret, io_put_kbuf(req, ret, issue_flags));
663 io_req_rw_cleanup(req, issue_flags);
666 io_rw_done(req, ret);
669 return IOU_ISSUE_SKIP_COMPLETE;
672 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
674 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
678 * For files that don't have ->read_iter() and ->write_iter(), handle them
679 * by looping over ->read() or ->write() manually.
681 static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
683 struct io_kiocb *req = cmd_to_io_kiocb(rw);
684 struct kiocb *kiocb = &rw->kiocb;
685 struct file *file = kiocb->ki_filp;
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.
694 if (kiocb->ki_flags & IOCB_HIPRI)
696 if ((kiocb->ki_flags & IOCB_NOWAIT) &&
697 !(kiocb->ki_filp->f_flags & O_NONBLOCK))
699 if ((req->flags & REQ_F_BUF_NODE) && req->buf_node->buf->is_kbuf)
702 ppos = io_kiocb_ppos(kiocb);
704 while (iov_iter_count(iter)) {
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);
716 addr = u64_to_user_ptr(rw->addr);
721 nr = file->f_op->read(file, addr, len, ppos);
723 nr = file->f_op->write(file, addr, len, ppos);
731 if (!iov_iter_is_bvec(iter)) {
732 iov_iter_advance(iter, nr);
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
756 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
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;
764 wpq = container_of(wait, struct wait_page_queue, wait);
766 if (!wake_page_match(wpq, key))
769 rw->kiocb.ki_flags &= ~IOCB_WAITQ;
770 list_del_init(&wait->entry);
771 io_req_task_queue(req);
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.
787 static bool io_rw_should_retry(struct io_kiocb *req)
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;
795 * Never retry for NOWAIT or a request with metadata, we just complete
798 if (req->flags & (REQ_F_NOWAIT | REQ_F_HAS_METADATA))
801 /* Only for buffered IO */
802 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
806 * just use poll if we can, and don't attempt if the fs doesn't
807 * support callback based unlocks
809 if (io_file_can_poll(req) ||
810 !(req->file->f_op->fop_flags & FOP_BUFFER_RASYNC))
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;
823 static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter)
825 struct file *file = rw->kiocb.ki_filp;
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);
835 static bool need_complete_io(struct io_kiocb *req)
837 return req->flags & REQ_F_ISREG ||
838 S_ISBLK(file_inode(req->file)->i_mode);
841 static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)
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;
849 if (unlikely(!(file->f_mode & mode)))
852 if (!(req->flags & REQ_F_FIXED_FILE))
853 req->flags |= io_file_get_flags(file);
855 kiocb->ki_flags = file->f_iocb_flags;
856 ret = kiocb_set_rw_flags(kiocb, rw->flags, rw_type);
859 kiocb->ki_flags |= IOCB_ALLOC_CACHE;
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.
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;
870 if (ctx->flags & IORING_SETUP_IOPOLL) {
871 if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
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();
882 if (kiocb->ki_flags & IOCB_HIPRI)
886 if (req->flags & REQ_F_HAS_METADATA) {
887 struct io_async_rw *io = req->async_data;
890 * We have a union of meta fields with wpq used for buffered-io
891 * in io_async_rw, so fail it here.
893 if (!(req->file->f_flags & O_DIRECT))
895 kiocb->ki_flags |= IOCB_HAS_METADATA;
896 kiocb->private = &io->meta;
902 static int __io_read(struct io_kiocb *req, unsigned int issue_flags)
904 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
905 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
906 struct io_async_rw *io = req->async_data;
907 struct kiocb *kiocb = &rw->kiocb;
911 if (req->flags & REQ_F_IMPORT_BUFFER) {
912 ret = io_rw_import_reg_vec(req, io, ITER_DEST, issue_flags);
915 } else if (io_do_buffer_select(req)) {
916 ret = io_import_rw_buffer(ITER_DEST, req, io, issue_flags);
917 if (unlikely(ret < 0))
920 ret = io_rw_init_file(req, FMODE_READ, READ);
923 req->cqe.res = iov_iter_count(&io->iter);
925 if (force_nonblock) {
926 /* If the file doesn't support async, just async punt */
927 if (unlikely(!io_file_supports_nowait(req, EPOLLIN)))
929 kiocb->ki_flags |= IOCB_NOWAIT;
931 /* Ensure we clear previously set non-block flag */
932 kiocb->ki_flags &= ~IOCB_NOWAIT;
935 ppos = io_kiocb_update_pos(req);
937 ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
941 ret = io_iter_do_read(rw, &io->iter);
944 * Some file systems like to return -EOPNOTSUPP for an IOCB_NOWAIT
945 * issue, even though they should be returning -EAGAIN. To be safe,
946 * retry from blocking context for either.
948 if (ret == -EOPNOTSUPP && force_nonblock)
951 if (ret == -EAGAIN) {
952 /* If we can poll, just do that. */
953 if (io_file_can_poll(req))
955 /* IOPOLL retry should happen for io-wq threads */
956 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
958 /* no retry on NONBLOCK nor RWF_NOWAIT */
959 if (req->flags & REQ_F_NOWAIT)
962 } else if (ret == -EIOCBQUEUED) {
963 return IOU_ISSUE_SKIP_COMPLETE;
964 } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
965 (req->flags & REQ_F_NOWAIT) || !need_complete_io(req) ||
966 (issue_flags & IO_URING_F_MULTISHOT)) {
967 /* read all, failed, already did sync or don't want to retry */
972 * Don't depend on the iter state matching what was consumed, or being
973 * untouched in case of error. Restore it and we'll advance it
974 * manually if we need to.
976 iov_iter_restore(&io->iter, &io->iter_state);
977 io_meta_restore(io, kiocb);
981 * We end up here because of a partial read, either from
982 * above or inside this loop. Advance the iter by the bytes
983 * that were consumed.
985 iov_iter_advance(&io->iter, ret);
986 if (!iov_iter_count(&io->iter))
988 io->bytes_done += ret;
989 iov_iter_save_state(&io->iter, &io->iter_state);
991 /* if we can retry, do so with the callbacks armed */
992 if (!io_rw_should_retry(req)) {
993 kiocb->ki_flags &= ~IOCB_WAITQ;
997 req->cqe.res = iov_iter_count(&io->iter);
999 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
1000 * we get -EIOCBQUEUED, then we'll get a notification when the
1001 * desired page gets unlocked. We can also get a partial read
1002 * here, and if we do, then just retry at the new offset.
1004 ret = io_iter_do_read(rw, &io->iter);
1005 if (ret == -EIOCBQUEUED)
1006 return IOU_ISSUE_SKIP_COMPLETE;
1007 /* we got some bytes, but not all. retry. */
1008 kiocb->ki_flags &= ~IOCB_WAITQ;
1009 iov_iter_restore(&io->iter, &io->iter_state);
1012 /* it's faster to check here then delegate to kfree */
1016 int io_read(struct io_kiocb *req, unsigned int issue_flags)
1020 ret = __io_read(req, issue_flags);
1022 return kiocb_done(req, ret, issue_flags);
1027 int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
1029 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1030 unsigned int cflags = 0;
1034 * Multishot MUST be used on a pollable file
1036 if (!io_file_can_poll(req))
1039 /* make it sync, multishot doesn't support async execution */
1040 rw->kiocb.ki_complete = NULL;
1041 ret = __io_read(req, issue_flags);
1044 * If we get -EAGAIN, recycle our buffer and just let normal poll
1047 if (ret == -EAGAIN) {
1049 * Reset rw->len to 0 again to avoid clamping future mshot
1050 * reads, in case the buffer size varies.
1052 if (io_kbuf_recycle(req, issue_flags))
1055 } else if (ret <= 0) {
1056 io_kbuf_recycle(req, issue_flags);
1059 } else if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
1060 cflags = io_put_kbuf(req, ret, issue_flags);
1063 * Any successful return value will keep the multishot read
1064 * armed, if it's still set. Put our buffer and post a CQE. If
1065 * we fail to post a CQE, or multishot is no longer set, then
1066 * jump to the termination path. This request is then done.
1068 cflags = io_put_kbuf(req, ret, issue_flags);
1069 rw->len = 0; /* similarly to above, reset len to 0 */
1071 if (io_req_post_cqe(req, ret, cflags | IORING_CQE_F_MORE)) {
1072 if (issue_flags & IO_URING_F_MULTISHOT)
1074 * Force retry, as we might have more data to
1075 * be read and otherwise it won't get retried
1076 * until (if ever) another poll is triggered.
1078 io_poll_multishot_retry(req);
1085 * Either an error, or we've hit overflow posting the CQE. For any
1086 * multishot request, hitting overflow will terminate it.
1088 io_req_set_res(req, ret, cflags);
1089 io_req_rw_cleanup(req, issue_flags);
1090 return IOU_COMPLETE;
1093 static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb)
1095 struct inode *inode;
1098 if (!(req->flags & REQ_F_ISREG))
1100 if (!(kiocb->ki_flags & IOCB_NOWAIT)) {
1101 kiocb_start_write(kiocb);
1105 inode = file_inode(kiocb->ki_filp);
1106 ret = sb_start_write_trylock(inode->i_sb);
1108 __sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
1112 int io_write(struct io_kiocb *req, unsigned int issue_flags)
1114 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1115 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1116 struct io_async_rw *io = req->async_data;
1117 struct kiocb *kiocb = &rw->kiocb;
1121 if (req->flags & REQ_F_IMPORT_BUFFER) {
1122 ret = io_rw_import_reg_vec(req, io, ITER_SOURCE, issue_flags);
1127 ret = io_rw_init_file(req, FMODE_WRITE, WRITE);
1130 req->cqe.res = iov_iter_count(&io->iter);
1132 if (force_nonblock) {
1133 /* If the file doesn't support async, just async punt */
1134 if (unlikely(!io_file_supports_nowait(req, EPOLLOUT)))
1137 /* Check if we can support NOWAIT. */
1138 if (!(kiocb->ki_flags & IOCB_DIRECT) &&
1139 !(req->file->f_op->fop_flags & FOP_BUFFER_WASYNC) &&
1140 (req->flags & REQ_F_ISREG))
1143 kiocb->ki_flags |= IOCB_NOWAIT;
1145 /* Ensure we clear previously set non-block flag */
1146 kiocb->ki_flags &= ~IOCB_NOWAIT;
1149 ppos = io_kiocb_update_pos(req);
1151 ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
1155 if (unlikely(!io_kiocb_start_write(req, kiocb)))
1157 kiocb->ki_flags |= IOCB_WRITE;
1159 if (likely(req->file->f_op->write_iter))
1160 ret2 = req->file->f_op->write_iter(kiocb, &io->iter);
1161 else if (req->file->f_op->write)
1162 ret2 = loop_rw_iter(WRITE, rw, &io->iter);
1167 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
1168 * retry them without IOCB_NOWAIT.
1170 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
1172 /* no retry on NONBLOCK nor RWF_NOWAIT */
1173 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
1175 if (!force_nonblock || ret2 != -EAGAIN) {
1176 /* IOPOLL retry should happen for io-wq threads */
1177 if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
1180 if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
1181 trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
1182 req->cqe.res, ret2);
1184 /* This is a partial write. The file pos has already been
1185 * updated, setup the async struct to complete the request
1186 * in the worker. Also update bytes_done to account for
1187 * the bytes already written.
1189 iov_iter_save_state(&io->iter, &io->iter_state);
1190 io->bytes_done += ret2;
1192 if (kiocb->ki_flags & IOCB_WRITE)
1193 io_req_end_write(req);
1197 return kiocb_done(req, ret2, issue_flags);
1200 iov_iter_restore(&io->iter, &io->iter_state);
1201 io_meta_restore(io, kiocb);
1202 if (kiocb->ki_flags & IOCB_WRITE)
1203 io_req_end_write(req);
1208 int io_read_fixed(struct io_kiocb *req, unsigned int issue_flags)
1212 ret = io_init_rw_fixed(req, issue_flags, ITER_DEST);
1216 return io_read(req, issue_flags);
1219 int io_write_fixed(struct io_kiocb *req, unsigned int issue_flags)
1223 ret = io_init_rw_fixed(req, issue_flags, ITER_SOURCE);
1227 return io_write(req, issue_flags);
1230 void io_rw_fail(struct io_kiocb *req)
1234 res = io_fixup_rw_res(req, req->cqe.res);
1235 io_req_set_res(req, res, req->cqe.flags);
1238 static int io_uring_classic_poll(struct io_kiocb *req, struct io_comp_batch *iob,
1239 unsigned int poll_flags)
1241 struct file *file = req->file;
1243 if (req->opcode == IORING_OP_URING_CMD) {
1244 struct io_uring_cmd *ioucmd;
1246 ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
1247 return file->f_op->uring_cmd_iopoll(ioucmd, iob, poll_flags);
1249 struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1251 return file->f_op->iopoll(&rw->kiocb, iob, poll_flags);
1255 static u64 io_hybrid_iopoll_delay(struct io_ring_ctx *ctx, struct io_kiocb *req)
1257 struct hrtimer_sleeper timer;
1258 enum hrtimer_mode mode;
1262 if (req->flags & REQ_F_IOPOLL_STATE)
1265 if (ctx->hybrid_poll_time == LLONG_MAX)
1268 /* Using half the running time to do schedule */
1269 sleep_time = ctx->hybrid_poll_time / 2;
1271 kt = ktime_set(0, sleep_time);
1272 req->flags |= REQ_F_IOPOLL_STATE;
1274 mode = HRTIMER_MODE_REL;
1275 hrtimer_setup_sleeper_on_stack(&timer, CLOCK_MONOTONIC, mode);
1276 hrtimer_set_expires(&timer.timer, kt);
1277 set_current_state(TASK_INTERRUPTIBLE);
1278 hrtimer_sleeper_start_expires(&timer, mode);
1283 hrtimer_cancel(&timer.timer);
1284 __set_current_state(TASK_RUNNING);
1285 destroy_hrtimer_on_stack(&timer.timer);
1289 static int io_uring_hybrid_poll(struct io_kiocb *req,
1290 struct io_comp_batch *iob, unsigned int poll_flags)
1292 struct io_ring_ctx *ctx = req->ctx;
1293 u64 runtime, sleep_time;
1296 sleep_time = io_hybrid_iopoll_delay(ctx, req);
1297 ret = io_uring_classic_poll(req, iob, poll_flags);
1298 runtime = ktime_get_ns() - req->iopoll_start - sleep_time;
1301 * Use minimum sleep time if we're polling devices with different
1302 * latencies. We could get more completions from the faster ones.
1304 if (ctx->hybrid_poll_time > runtime)
1305 ctx->hybrid_poll_time = runtime;
1310 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
1312 struct io_wq_work_node *pos, *start, *prev;
1313 unsigned int poll_flags = 0;
1314 DEFINE_IO_COMP_BATCH(iob);
1318 * Only spin for completions if we don't have multiple devices hanging
1319 * off our complete list.
1321 if (ctx->poll_multi_queue || force_nonspin)
1322 poll_flags |= BLK_POLL_ONESHOT;
1324 wq_list_for_each(pos, start, &ctx->iopoll_list) {
1325 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1329 * Move completed and retryable entries to our local lists.
1330 * If we find a request that requires polling, break out
1331 * and complete those lists first, if we have entries there.
1333 if (READ_ONCE(req->iopoll_completed))
1336 if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL)
1337 ret = io_uring_hybrid_poll(req, &iob, poll_flags);
1339 ret = io_uring_classic_poll(req, &iob, poll_flags);
1341 if (unlikely(ret < 0))
1344 poll_flags |= BLK_POLL_ONESHOT;
1346 /* iopoll may have completed current req */
1347 if (!rq_list_empty(&iob.req_list) ||
1348 READ_ONCE(req->iopoll_completed))
1352 if (!rq_list_empty(&iob.req_list))
1358 wq_list_for_each_resume(pos, prev) {
1359 struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1361 /* order with io_complete_rw_iopoll(), e.g. ->result updates */
1362 if (!smp_load_acquire(&req->iopoll_completed))
1365 req->cqe.flags = io_put_kbuf(req, req->cqe.res, 0);
1366 if (req->opcode != IORING_OP_URING_CMD)
1367 io_req_rw_cleanup(req, 0);
1369 if (unlikely(!nr_events))
1372 pos = start ? start->next : ctx->iopoll_list.first;
1373 wq_list_cut(&ctx->iopoll_list, prev, start);
1375 if (WARN_ON_ONCE(!wq_list_empty(&ctx->submit_state.compl_reqs)))
1377 ctx->submit_state.compl_reqs.first = pos;
1378 __io_submit_flush_completions(ctx);
1382 void io_rw_cache_free(const void *entry)
1384 struct io_async_rw *rw = (struct io_async_rw *) entry;
1386 io_vec_free(&rw->vec);