History

Merge tag 'for-5.9/io_uring-20200802' of git://git.kernel.dk/linux-block

Pull io_uring updates from Jens Axboe: "Lots of cleanups in here, hardening the code and/or making it easier to read and fixing bugs, but a core feature/change too adding support for real async buffered reads. With the latter in place, we just need buffered write async support and we're done relying on kthreads for the fast path. In detail: - Cleanup how memory accounting is done on ring setup/free (Bijan) - sq array offset calculation fixup (Dmitry) - Consistently handle blocking off O_DIRECT submission path (me) - Support proper async buffered reads, instead of relying on kthread offload for that. This uses the page waitqueue to drive retries from task_work, like we handle poll based retry. (me) - IO completion optimizations (me) - Fix race with accounting and ring fd install (me) - Support EPOLLEXCLUSIVE (Jiufei) - Get rid of the io_kiocb unionizing, made possible by shrinking other bits (Pavel) - Completion side cleanups (Pavel) - Cleanup REQ_F_ flags handling, and kill off many of them (Pavel) - Request environment grabbing cleanups (Pavel) - File and socket read/write cleanups (Pavel) - Improve kiocb_set_rw_flags() (Pavel) - Tons of fixes and cleanups (Pavel) - IORING_SQ_NEED_WAKEUP clear fix (Xiaoguang)" * tag 'for-5.9/io_uring-20200802' of git://git.kernel.dk/linux-block: (127 commits) io_uring: flip if handling after io_setup_async_rw fs: optimise kiocb_set_rw_flags() io_uring: don't touch 'ctx' after installing file descriptor io_uring: get rid of atomic FAA for cq_timeouts io_uring: consolidate *_check_overflow accounting io_uring: fix stalled deferred requests io_uring: fix racy overflow count reporting io_uring: deduplicate __io_complete_rw() io_uring: de-unionise io_kiocb io-wq: update hash bits io_uring: fix missing io_queue_linked_timeout() io_uring: mark ->work uninitialised after cleanup io_uring: deduplicate io_grab_files() calls io_uring: don't do opcode prep twice io_uring: clear IORING_SQ_NEED_WAKEUP after executing task works io_uring: batch put_task_struct() tasks: add put_task_struct_many() io_uring: return locked and pinned page accounting io_uring: don't miscount pinned memory io_uring: don't open-code recv kbuf managment ...

Linus Torvalds committed 6 years ago

Commit cdc8fcb2 parents 382625d fa15baf14 files · +1644 −1181Browse files

Unified · Split

14 files
  1. block/blk-core.c +6 −0
  2. fs/block_dev.c +1 −1
  3. fs/btrfs/file.c +1 −1
  4. fs/io-wq.c +5 −9
  5. fs/io-wq.h +6 −5
  6. fs/io_uring.c +1482 −1129
  7. fs/xfs/xfs_file.c +1 −1
  8. include/linux/blkdev.h +1 −0
  9. include/linux/fs.h +20 −6
  10. include/linux/pagemap.h +43 −0
  11. include/linux/sched/task.h +6 −0
  12. include/uapi/linux/io_uring.h +3 −1
  13. mm/filemap.c +64 −27
  14. tools/io_uring/liburing.h +5 −1
block/blk-core.c+6−0
@@ -960,9 +960,14 @@ static noinline_for_stack bool submit_bio_checks(struct bio *bio) · 959 unmodified lines
960960 {
961961 struct request_queue *q = bio->bi_disk->queue;
962962 blk_status_t status = BLK_STS_IOERR;
963+ struct blk_plug *plug;
963964
964965 might_sleep();
965966
967+ plug = blk_mq_plug(q, bio);
968+ if (plug && plug->nowait)
969+ bio->bi_opf |= REQ_NOWAIT;
970+
966971 /*
967972 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
968973 * if queue is not a request based queue.
@@ -1802,6 +1807,7 @@ void blk_start_plug(struct blk_plug *plug) · 833 unmodified lines
18021807 INIT_LIST_HEAD(&plug->cb_list);
18031808 plug->rq_count = 0;
18041809 plug->multiple_queues = false;
1810+ plug->nowait = false;
18051811
18061812 /*
18071813 * Store ordering should not be needed here, since a potential
fs/block_dev.c+1−1
@@ -1734,7 +1734,7 @@ static int blkdev_open(struct inode * inode, struct file * filp) · 1733 unmodified lines
17341734 */
17351735 filp->f_flags |= O_LARGEFILE;
17361736
1737− filp->f_mode |= FMODE_NOWAIT;
1737+ filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
17381738
17391739 if (filp->f_flags & O_NDELAY)
17401740 filp->f_mode |= FMODE_NDELAY;
fs/btrfs/file.c+1−1
@@ -3537,7 +3537,7 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence) · 3536 unmodified lines
35373537
35383538 static int btrfs_file_open(struct inode *inode, struct file *filp)
35393539 {
3540− filp->f_mode |= FMODE_NOWAIT;
3540+ filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
35413541 return generic_file_open(inode, filp);
35423542 }
35433543
fs/io-wq.c+5−9
@@ -462,6 +462,7 @@ static void io_impersonate_work(struct io_worker *worker, · 461 unmodified lines
462462 io_wq_switch_mm(worker, work);
463463 if (worker->cur_creds != work->creds)
464464 io_wq_switch_creds(worker, work);
465+ current->signal->rlim[RLIMIT_FSIZE].rlim_cur = work->fsize;
465466 }
466467
467468 static void io_assign_current_work(struct io_worker *worker,
@@ -489,7 +490,6 @@ static void io_worker_handle_work(struct io_worker *worker) · 21 unmodified lines
489490
490491 do {
491492 struct io_wq_work *work;
492− unsigned int hash;
493493 get_next:
494494 /*
495495 * If we got some work, mark us as busy. If we didn't, but
@@ -512,6 +512,7 @@ get_next: · 16 unmodified lines
512512 /* handle a whole dependent link */
513513 do {
514514 struct io_wq_work *old_work, *next_hashed, *linked;
515+ unsigned int hash = io_get_work_hash(work);
515516
516517 next_hashed = wq_next_work(work);
517518 io_impersonate_work(worker, work);
@@ -522,10 +523,8 @@ get_next: · 4 unmodified lines
522523 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
523524 work->flags |= IO_WQ_WORK_CANCEL;
524525
525− hash = io_get_work_hash(work);
526− linked = old_work = work;
527− wq->do_work(&linked);
528− linked = (old_work == linked) ? NULL : linked;
526+ old_work = work;
527+ linked = wq->do_work(work);
529528
530529 work = next_hashed;
531530 if (!work && linked && !io_wq_is_hashed(linked)) {
@@ -542,8 +541,6 @@ get_next: · 10 unmodified lines
542541 spin_lock_irq(&wqe->lock);
543542 wqe->hash_map &= ~BIT_ULL(hash);
544543 wqe->flags &= ~IO_WQE_FLAG_STALLED;
545− /* dependent work is not hashed */
546− hash = -1U;
547544 /* skip unnecessary unlock-lock wqe->lock */
548545 if (!work)
549546 goto get_next;
@@ -781,8 +778,7 @@ static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) · 231 unmodified lines
781778 struct io_wq_work *old_work = work;
782779
783780 work->flags |= IO_WQ_WORK_CANCEL;
784− wq->do_work(&work);
785− work = (work == old_work) ? NULL : work;
781+ work = wq->do_work(work);
786782 wq->free_work(old_work);
787783 } while (work);
788784 }
fs/io-wq.h+6−5
@@ -5,10 +5,10 @@ struct io_wq; · 4 unmodified lines
55
66 enum {
77 IO_WQ_WORK_CANCEL = 1,
8− IO_WQ_WORK_HASHED = 4,
9− IO_WQ_WORK_UNBOUND = 32,
10− IO_WQ_WORK_NO_CANCEL = 256,
11− IO_WQ_WORK_CONCURRENT = 512,
8+ IO_WQ_WORK_HASHED = 2,
9+ IO_WQ_WORK_UNBOUND = 4,
10+ IO_WQ_WORK_NO_CANCEL = 8,
11+ IO_WQ_WORK_CONCURRENT = 16,
1212
1313 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
1414 };
@@ -89,6 +89,7 @@ struct io_wq_work { · 74 unmodified lines
8989 struct mm_struct *mm;
9090 const struct cred *creds;
9191 struct fs_struct *fs;
92+ unsigned long fsize;
9293 unsigned flags;
9394 };
9495
@@ -101,7 +102,7 @@ static inline struct io_wq_work *wq_next_work(struct io_wq_work *work) · 6 unmodified lines
101102 }
102103
103104 typedef void (free_work_fn)(struct io_wq_work *);
104−typedef void (io_wq_work_fn)(struct io_wq_work **);
105+typedef struct io_wq_work *(io_wq_work_fn)(struct io_wq_work *);
105106
106107 struct io_wq_data {
107108 struct user_struct *user;
fs/io_uring.c+1482−1129
@@ -78,6 +78,7 @@ · 77 unmodified lines
7878 #include <linux/fs_struct.h>
7979 #include <linux/splice.h>
8080 #include <linux/task_work.h>
81+#include <linux/pagemap.h>
8182
8283 #define CREATE_TRACE_POINTS
8384 #include <trace/events/io_uring.h>
@@ -226,7 +227,7 @@ struct io_ring_ctx { · 142 unmodified lines
226227 struct {
227228 unsigned int flags;
228229 unsigned int compat: 1;
229− unsigned int account_mem: 1;
230+ unsigned int limit_mem: 1;
230231 unsigned int cq_overflow_flushed: 1;
231232 unsigned int drain_next: 1;
232233 unsigned int eventfd_async: 1;
@@ -319,12 +320,12 @@ struct io_ring_ctx { · 86 unmodified lines
319320 spinlock_t completion_lock;
320321
321322 /*
322− * ->poll_list is protected by the ctx->uring_lock for
323+ * ->iopoll_list is protected by the ctx->uring_lock for
323324 * io_uring instances that don't use IORING_SETUP_SQPOLL.
324325 * For SQPOLL, only the single threaded io_sq_thread() will
325326 * manipulate the list, hence no extra locking is needed there.
326327 */
327− struct list_head poll_list;
328+ struct list_head iopoll_list;
328329 struct hlist_head *cancel_hash;
329330 unsigned cancel_hash_bits;
330331 bool poll_multi_file;
@@ -395,6 +396,7 @@ struct io_timeout { · 64 unmodified lines
395396 int flags;
396397 u32 off;
397398 u32 target_seq;
399+ struct list_head list;
398400 };
399401
400402 struct io_rw {
@@ -413,7 +415,7 @@ struct io_connect { · 12 unmodified lines
413415 struct io_sr_msg {
414416 struct file *file;
415417 union {
416− struct user_msghdr __user *msg;
418+ struct user_msghdr __user *umsg;
417419 void __user *buf;
418420 };
419421 int msg_flags;
@@ -486,6 +488,12 @@ struct io_statx { · 66 unmodified lines
486488 struct statx __user *buffer;
487489 };
488490
491+struct io_completion {
492+ struct file *file;
493+ struct list_head list;
494+ int cflags;
495+};
496+
489497 struct io_async_connect {
490498 struct sockaddr_storage address;
491499 };
@@ -503,6 +511,7 @@ struct io_async_rw { · 11 unmodified lines
503511 struct iovec *iov;
504512 ssize_t nr_segs;
505513 ssize_t size;
514+ struct wait_page_queue wpq;
506515 };
507516
508517 struct io_async_ctx {
@@ -523,23 +532,18 @@ enum { · 14 unmodified lines
523532 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
524533
525534 REQ_F_LINK_HEAD_BIT,
526− REQ_F_LINK_NEXT_BIT,
527535 REQ_F_FAIL_LINK_BIT,
528536 REQ_F_INFLIGHT_BIT,
529537 REQ_F_CUR_POS_BIT,
530538 REQ_F_NOWAIT_BIT,
531539 REQ_F_LINK_TIMEOUT_BIT,
532− REQ_F_TIMEOUT_BIT,
533540 REQ_F_ISREG_BIT,
534− REQ_F_MUST_PUNT_BIT,
535− REQ_F_TIMEOUT_NOSEQ_BIT,
536541 REQ_F_COMP_LOCKED_BIT,
537542 REQ_F_NEED_CLEANUP_BIT,
538543 REQ_F_OVERFLOW_BIT,
539544 REQ_F_POLLED_BIT,
540545 REQ_F_BUFFER_SELECTED_BIT,
541546 REQ_F_NO_FILE_TABLE_BIT,
542− REQ_F_QUEUE_TIMEOUT_BIT,
543547 REQ_F_WORK_INITIALIZED_BIT,
544548 REQ_F_TASK_PINNED_BIT,
545549
@@ -563,8 +567,6 @@ enum { · 17 unmodified lines
563567
564568 /* head of a link */
565569 REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT),
566− /* already grabbed next link */
567− REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
568570 /* fail rest of links */
569571 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
570572 /* on inflight list */
@@ -575,14 +577,8 @@ enum { · 4 unmodified lines
575577 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
576578 /* has linked timeout */
577579 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
578− /* timeout request */
579− REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
580580 /* regular file */
581581 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
582− /* must be punted even for NONBLOCK */
583− REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
584− /* no timeout sequence */
585− REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
586582 /* completion under lock */
587583 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
588584 /* needs cleanup */
@@ -595,8 +591,6 @@ enum { · 6 unmodified lines
595591 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
596592 /* doesn't need file table for this request */
597593 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
598− /* needs to queue linked timeout */
599− REQ_F_QUEUE_TIMEOUT = BIT(REQ_F_QUEUE_TIMEOUT_BIT),
600594 /* io_wq_work is initialized */
601595 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
602596 /* req->task is refcounted */
@@ -606,7 +600,6 @@ enum { · 3 unmodified lines
606600 struct async_poll {
607601 struct io_poll_iocb poll;
608602 struct io_poll_iocb *double_poll;
609− struct io_wq_work work;
610603 };
611604
612605 /*
@@ -635,51 +628,54 @@ struct io_kiocb { · 22 unmodified lines
635628 struct io_splice splice;
636629 struct io_provide_buf pbuf;
637630 struct io_statx statx;
631+ /* use only after cleaning per-op data, see io_clean_op() */
632+ struct io_completion compl;
638633 };
639634
640635 struct io_async_ctx *io;
641− int cflags;
642636 u8 opcode;
643637 /* polled IO has completed */
644638 u8 iopoll_completed;
645639
646640 u16 buf_index;
641+ u32 result;
647642
648− struct io_ring_ctx *ctx;
649− struct list_head list;
650− unsigned int flags;
651− refcount_t refs;
652− struct task_struct *task;
653− unsigned long fsize;
654− u64 user_data;
655− u32 result;
656− u32 sequence;
657−
658− struct list_head link_list;
643+ struct io_ring_ctx *ctx;
644+ unsigned int flags;
645+ refcount_t refs;
646+ struct task_struct *task;
647+ u64 user_data;
659648
660− struct list_head inflight_entry;
649+ struct list_head link_list;
661650
662− struct percpu_ref *fixed_file_refs;
651+ /*
652+ * 1. used with ctx->iopoll_list with reads/writes
653+ * 2. to track reqs with ->files (see io_op_def::file_table)
654+ */
655+ struct list_head inflight_entry;
656+
657+ struct percpu_ref *fixed_file_refs;
658+ struct callback_head task_work;
659+ /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
660+ struct hlist_node hash_node;
661+ struct async_poll *apoll;
662+ struct io_wq_work work;
663+};
663664
664− union {
665− /*
666− * Only commands that never go async can use the below fields,
667− * obviously. Right now only IORING_OP_POLL_ADD uses them, and
668− * async armed poll handlers for regular commands. The latter
669− * restore the work, if needed.
670− */
671− struct {
672− struct callback_head task_work;
673− struct hlist_node hash_node;
674− struct async_poll *apoll;
675− };
676− struct io_wq_work work;
677− };
665+struct io_defer_entry {
666+ struct list_head list;
667+ struct io_kiocb *req;
668+ u32 seq;
678669 };
679670
680−#define IO_PLUG_THRESHOLD 2
681671 #define IO_IOPOLL_BATCH 8
682672
673+struct io_comp_state {
674+ unsigned int nr;
675+ struct list_head list;
676+ struct io_ring_ctx *ctx;
677+};
678+
683679 struct io_submit_state {
684680 struct blk_plug plug;
685681
@@ -689,13 +685,17 @@ struct io_submit_state { · 3 unmodified lines
689685 void *reqs[IO_IOPOLL_BATCH];
690686 unsigned int free_reqs;
691687
688+ /*
689+ * Batch completion logic
690+ */
691+ struct io_comp_state comp;
692+
692693 /*
693694 * File reference cache
694695 */
695696 struct file *file;
696697 unsigned int fd;
697698 unsigned int has_refs;
698− unsigned int used_refs;
699699 unsigned int ios_left;
700700 };
701701
@@ -723,6 +723,7 @@ struct io_op_def { · 21 unmodified lines
723723 unsigned pollout : 1;
724724 /* op supports buffer selection */
725725 unsigned buffer_select : 1;
726+ unsigned needs_fsize : 1;
726727 };
727728
728729 static const struct io_op_def io_op_defs[] = {
@@ -742,6 +743,7 @@ static const struct io_op_def io_op_defs[] = { · 13 unmodified lines
742743 .hash_reg_file = 1,
743744 .unbound_nonreg_file = 1,
744745 .pollout = 1,
746+ .needs_fsize = 1,
745747 },
746748 [IORING_OP_FSYNC] = {
747749 .needs_file = 1,
@@ -756,6 +758,7 @@ static const struct io_op_def io_op_defs[] = { · 8 unmodified lines
756758 .hash_reg_file = 1,
757759 .unbound_nonreg_file = 1,
758760 .pollout = 1,
761+ .needs_fsize = 1,
759762 },
760763 [IORING_OP_POLL_ADD] = {
761764 .needs_file = 1,
@@ -808,6 +811,7 @@ static const struct io_op_def io_op_defs[] = { · 46 unmodified lines
808811 },
809812 [IORING_OP_FALLOCATE] = {
810813 .needs_file = 1,
814+ .needs_fsize = 1,
811815 },
812816 [IORING_OP_OPENAT] = {
813817 .file_table = 1,
@@ -839,6 +843,7 @@ static const struct io_op_def io_op_defs[] = { · 25 unmodified lines
839843 .needs_file = 1,
840844 .unbound_nonreg_file = 1,
841845 .pollout = 1,
846+ .needs_fsize = 1,
842847 },
843848 [IORING_OP_FADVISE] = {
844849 .needs_file = 1,
@@ -881,22 +886,37 @@ static const struct io_op_def io_op_defs[] = { · 36 unmodified lines
881886 },
882887 };
883888
884−static void io_wq_submit_work(struct io_wq_work **workptr);
889+enum io_mem_account {
890+ ACCT_LOCKED,
891+ ACCT_PINNED,
892+};
893+
894+static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
895+ struct io_comp_state *cs);
885896 static void io_cqring_fill_event(struct io_kiocb *req, long res);
886897 static void io_put_req(struct io_kiocb *req);
898+static void io_double_put_req(struct io_kiocb *req);
887899 static void __io_double_put_req(struct io_kiocb *req);
888900 static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
889901 static void io_queue_linked_timeout(struct io_kiocb *req);
890902 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
891903 struct io_uring_files_update *ip,
892904 unsigned nr_args);
893−static int io_grab_files(struct io_kiocb *req);
894−static void io_complete_rw_common(struct kiocb *kiocb, long res);
895−static void io_cleanup_req(struct io_kiocb *req);
905+static int io_prep_work_files(struct io_kiocb *req);
906+static void __io_clean_op(struct io_kiocb *req);
896907 static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
897908 int fd, struct file **out_file, bool fixed);
898909 static void __io_queue_sqe(struct io_kiocb *req,
899− const struct io_uring_sqe *sqe);
910+ const struct io_uring_sqe *sqe,
911+ struct io_comp_state *cs);
912+static void io_file_put_work(struct work_struct *work);
913+
914+static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
915+ struct iovec **iovec, struct iov_iter *iter,
916+ bool needs_lock);
917+static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
918+ struct iovec *iovec, struct iovec *fast_iov,
919+ struct iov_iter *iter);
900920
901921 static struct kmem_cache *req_cachep;
902922
@@ -923,6 +943,12 @@ static void io_get_req_task(struct io_kiocb *req) · 20 unmodified lines
923943 req->flags |= REQ_F_TASK_PINNED;
924944 }
925945
946+static inline void io_clean_op(struct io_kiocb *req)
947+{
948+ if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
949+ __io_clean_op(req);
950+}
951+
926952 /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
927953 static void __io_put_req_task(struct io_kiocb *req)
928954 {
@@ -930,7 +956,41 @@ static void __io_put_req_task(struct io_kiocb *req) · 1 unmodified line
930956 put_task_struct(req->task);
931957 }
932958
933−static void io_file_put_work(struct work_struct *work);
959+static void io_sq_thread_drop_mm(void)
960+{
961+ struct mm_struct *mm = current->mm;
962+
963+ if (mm) {
964+ kthread_unuse_mm(mm);
965+ mmput(mm);
966+ }
967+}
968+
969+static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
970+{
971+ if (!current->mm) {
972+ if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
973+ !mmget_not_zero(ctx->sqo_mm)))
974+ return -EFAULT;
975+ kthread_use_mm(ctx->sqo_mm);
976+ }
977+
978+ return 0;
979+}
980+
981+static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
982+ struct io_kiocb *req)
983+{
984+ if (!io_op_defs[req->opcode].needs_mm)
985+ return 0;
986+ return __io_sq_thread_acquire_mm(ctx);
987+}
988+
989+static inline void req_set_fail_links(struct io_kiocb *req)
990+{
991+ if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
992+ req->flags |= REQ_F_FAIL_LINK;
993+}
934994
935995 /*
936996 * Note: must call io_req_init_async() for the first time you
@@ -957,6 +1017,11 @@ static void io_ring_ctx_ref_free(struct percpu_ref *ref) · 20 unmodified lines
9571017 complete(&ctx->ref_comp);
9581018 }
9591019
1020+static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1021+{
1022+ return !req->timeout.off;
1023+}
1024+
9601025 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
9611026 {
9621027 struct io_ring_ctx *ctx;
@@ -1000,7 +1065,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) · 37 unmodified lines
10001065 mutex_init(&ctx->uring_lock);
10011066 init_waitqueue_head(&ctx->wait);
10021067 spin_lock_init(&ctx->completion_lock);
1003− INIT_LIST_HEAD(&ctx->poll_list);
1068+ INIT_LIST_HEAD(&ctx->iopoll_list);
10041069 INIT_LIST_HEAD(&ctx->defer_list);
10051070 INIT_LIST_HEAD(&ctx->timeout_list);
10061071 init_waitqueue_head(&ctx->inflight_wait);
@@ -1017,18 +1082,14 @@ err: · 10 unmodified lines
10171082 return NULL;
10181083 }
10191084
1020−static inline bool __req_need_defer(struct io_kiocb *req)
1085+static bool req_need_defer(struct io_kiocb *req, u32 seq)
10211086 {
1022− struct io_ring_ctx *ctx = req->ctx;
1087+ if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1088+ struct io_ring_ctx *ctx = req->ctx;
10231089
1024− return req->sequence != ctx->cached_cq_tail
1090+ return seq != ctx->cached_cq_tail
10251091 + atomic_read(&ctx->cached_cq_overflow);
1026−}
1027−
1028−static inline bool req_need_defer(struct io_kiocb *req)
1029−{
1030− if (unlikely(req->flags & REQ_F_IO_DRAIN))
1031− return __req_need_defer(req);
1092+ }
10321093
10331094 return false;
10341095 }
@@ -1046,28 +1107,7 @@ static void __io_commit_cqring(struct io_ring_ctx *ctx) · 11 unmodified lines
10461107 }
10471108 }
10481109
1049−static inline void io_req_work_grab_env(struct io_kiocb *req,
1050− const struct io_op_def *def)
1051−{
1052− if (!req->work.mm && def->needs_mm) {
1053− mmgrab(current->mm);
1054− req->work.mm = current->mm;
1055− }
1056− if (!req->work.creds)
1057− req->work.creds = get_current_cred();
1058− if (!req->work.fs && def->needs_fs) {
1059− spin_lock(&current->fs->lock);
1060− if (!current->fs->in_exec) {
1061− req->work.fs = current->fs;
1062− req->work.fs->users++;
1063− } else {
1064− req->work.flags |= IO_WQ_WORK_CANCEL;
1065− }
1066− spin_unlock(&current->fs->lock);
1067− }
1068−}
1069−
1070−static inline void io_req_work_drop_env(struct io_kiocb *req)
1110+static void io_req_clean_work(struct io_kiocb *req)
10711111 {
10721112 if (!(req->flags & REQ_F_WORK_INITIALIZED))
10731113 return;
@@ -1089,11 +1129,12 @@ static inline void io_req_work_drop_env(struct io_kiocb *req) · 15 unmodified lines
10891129 spin_unlock(&req->work.fs->lock);
10901130 if (fs)
10911131 free_fs_struct(fs);
1132+ req->work.fs = NULL;
10921133 }
1134+ req->flags &= ~REQ_F_WORK_INITIALIZED;
10931135 }
10941136
1095−static inline void io_prep_async_work(struct io_kiocb *req,
1096− struct io_kiocb **link)
1137+static void io_prep_async_work(struct io_kiocb *req)
10971138 {
10981139 const struct io_op_def *def = &io_op_defs[req->opcode];
10991140
@@ -1106,18 +1147,42 @@ static inline void io_prep_async_work(struct io_kiocb *req, · 6 unmodified lines
11061147 if (def->unbound_nonreg_file)
11071148 req->work.flags |= IO_WQ_WORK_UNBOUND;
11081149 }
1150+ if (!req->work.mm && def->needs_mm) {
1151+ mmgrab(current->mm);
1152+ req->work.mm = current->mm;
1153+ }
1154+ if (!req->work.creds)
1155+ req->work.creds = get_current_cred();
1156+ if (!req->work.fs && def->needs_fs) {
1157+ spin_lock(&current->fs->lock);
1158+ if (!current->fs->in_exec) {
1159+ req->work.fs = current->fs;
1160+ req->work.fs->users++;
1161+ } else {
1162+ req->work.flags |= IO_WQ_WORK_CANCEL;
1163+ }
1164+ spin_unlock(&current->fs->lock);
1165+ }
1166+ if (def->needs_fsize)
1167+ req->work.fsize = rlimit(RLIMIT_FSIZE);
1168+ else
1169+ req->work.fsize = RLIM_INFINITY;
1170+}
11091171
1110− io_req_work_grab_env(req, def);
1172+static void io_prep_async_link(struct io_kiocb *req)
1173+{
1174+ struct io_kiocb *cur;
11111175
1112− *link = io_prep_linked_timeout(req);
1176+ io_prep_async_work(req);
1177+ if (req->flags & REQ_F_LINK_HEAD)
1178+ list_for_each_entry(cur, &req->link_list, link_list)
1179+ io_prep_async_work(cur);
11131180 }
11141181
1115−static inline void io_queue_async_work(struct io_kiocb *req)
1182+static void __io_queue_async_work(struct io_kiocb *req)
11161183 {
11171184 struct io_ring_ctx *ctx = req->ctx;
1118− struct io_kiocb *link;
1119−
1120− io_prep_async_work(req, &link);
1185+ struct io_kiocb *link = io_prep_linked_timeout(req);
11211186
11221187 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
11231188 &req->work, req->flags);
@@ -1127,14 +1192,22 @@ static inline void io_queue_async_work(struct io_kiocb *req) · 3 unmodified lines
11271192 io_queue_linked_timeout(link);
11281193 }
11291194
1195+static void io_queue_async_work(struct io_kiocb *req)
1196+{
1197+ /* init ->work of the whole link before punting */
1198+ io_prep_async_link(req);
1199+ __io_queue_async_work(req);
1200+}
1201+
11301202 static void io_kill_timeout(struct io_kiocb *req)
11311203 {
11321204 int ret;
11331205
11341206 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
11351207 if (ret != -1) {
1136− atomic_inc(&req->ctx->cq_timeouts);
1137− list_del_init(&req->list);
1208+ atomic_set(&req->ctx->cq_timeouts,
1209+ atomic_read(&req->ctx->cq_timeouts) + 1);
1210+ list_del_init(&req->timeout.list);
11381211 req->flags |= REQ_F_COMP_LOCKED;
11391212 io_cqring_fill_event(req, 0);
11401213 io_put_req(req);
@@ -1146,7 +1219,7 @@ static void io_kill_timeouts(struct io_ring_ctx *ctx) · 5 unmodified lines
11461219 struct io_kiocb *req, *tmp;
11471220
11481221 spin_lock_irq(&ctx->completion_lock);
1149− list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1222+ list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list)
11501223 io_kill_timeout(req);
11511224 spin_unlock_irq(&ctx->completion_lock);
11521225 }
@@ -1154,13 +1227,15 @@ static void io_kill_timeouts(struct io_ring_ctx *ctx) · 1 unmodified line
11541227 static void __io_queue_deferred(struct io_ring_ctx *ctx)
11551228 {
11561229 do {
1157− struct io_kiocb *req = list_first_entry(&ctx->defer_list,
1158− struct io_kiocb, list);
1230+ struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1231+ struct io_defer_entry, list);
11591232
1160− if (req_need_defer(req))
1233+ if (req_need_defer(de->req, de->seq))
11611234 break;
1162− list_del_init(&req->list);
1163− io_queue_async_work(req);
1235+ list_del_init(&de->list);
1236+ /* punt-init is done before queueing for defer */
1237+ __io_queue_async_work(de->req);
1238+ kfree(de);
11641239 } while (!list_empty(&ctx->defer_list));
11651240 }
11661241
@@ -1168,15 +1243,15 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx) · 1 unmodified line
11681243 {
11691244 while (!list_empty(&ctx->timeout_list)) {
11701245 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
1171− struct io_kiocb, list);
1246+ struct io_kiocb, timeout.list);
11721247
1173− if (req->flags & REQ_F_TIMEOUT_NOSEQ)
1248+ if (io_is_timeout_noseq(req))
11741249 break;
11751250 if (req->timeout.target_seq != ctx->cached_cq_tail
11761251 - atomic_read(&ctx->cq_timeouts))
11771252 break;
11781253
1179− list_del_init(&req->list);
1254+ list_del_init(&req->timeout.list);
11801255 io_kill_timeout(req);
11811256 }
11821257 }
@@ -1229,6 +1304,15 @@ static void io_cqring_ev_posted(struct io_ring_ctx *ctx) · 46 unmodified lines
12291304 eventfd_signal(ctx->cq_ev_fd, 1);
12301305 }
12311306
1307+static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
1308+{
1309+ if (list_empty(&ctx->cq_overflow_list)) {
1310+ clear_bit(0, &ctx->sq_check_overflow);
1311+ clear_bit(0, &ctx->cq_check_overflow);
1312+ ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1313+ }
1314+}
1315+
12321316 /* Returns true if there are no backlogged entries after the flush */
12331317 static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
12341318 {
@@ -1259,13 +1343,13 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) · 24 unmodified lines
12591343 break;
12601344
12611345 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1262− list);
1263− list_move(&req->list, &list);
1346+ compl.list);
1347+ list_move(&req->compl.list, &list);
12641348 req->flags &= ~REQ_F_OVERFLOW;
12651349 if (cqe) {
12661350 WRITE_ONCE(cqe->user_data, req->user_data);
12671351 WRITE_ONCE(cqe->res, req->result);
1268− WRITE_ONCE(cqe->flags, req->cflags);
1352+ WRITE_ONCE(cqe->flags, req->compl.cflags);
12691353 } else {
12701354 WRITE_ONCE(ctx->rings->cq_overflow,
12711355 atomic_inc_return(&ctx->cached_cq_overflow));
@@ -1273,17 +1357,14 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) · 1 unmodified line
12731357 }
12741358
12751359 io_commit_cqring(ctx);
1276− if (cqe) {
1277− clear_bit(0, &ctx->sq_check_overflow);
1278− clear_bit(0, &ctx->cq_check_overflow);
1279− ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1280− }
1360+ io_cqring_mark_overflow(ctx);
1361+
12811362 spin_unlock_irqrestore(&ctx->completion_lock, flags);
12821363 io_cqring_ev_posted(ctx);
12831364
12841365 while (!list_empty(&list)) {
1285− req = list_first_entry(&list, struct io_kiocb, list);
1286− list_del(&req->list);
1366+ req = list_first_entry(&list, struct io_kiocb, compl.list);
1367+ list_del(&req->compl.list);
12871368 io_put_req(req);
12881369 }
12891370
@@ -1316,11 +1397,12 @@ static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) · 26 unmodified lines
13161397 set_bit(0, &ctx->cq_check_overflow);
13171398 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
13181399 }
1400+ io_clean_op(req);
13191401 req->flags |= REQ_F_OVERFLOW;
1320− refcount_inc(&req->refs);
13211402 req->result = res;
1322− req->cflags = cflags;
1323− list_add_tail(&req->list, &ctx->cq_overflow_list);
1403+ req->compl.cflags = cflags;
1404+ refcount_inc(&req->refs);
1405+ list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
13241406 }
13251407 }
13261408
@@ -1329,7 +1411,7 @@ static void io_cqring_fill_event(struct io_kiocb *req, long res) · 2 unmodified lines
13291411 __io_cqring_fill_event(req, res, 0);
13301412 }
13311413
1332−static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
1414+static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags)
13331415 {
13341416 struct io_ring_ctx *ctx = req->ctx;
13351417 unsigned long flags;
@@ -1342,9 +1424,52 @@ static void __io_cqring_add_event(struct io_kiocb *req, long res, long cflags) · 6 unmodified lines
13421424 io_cqring_ev_posted(ctx);
13431425 }
13441426
1345−static void io_cqring_add_event(struct io_kiocb *req, long res)
1427+static void io_submit_flush_completions(struct io_comp_state *cs)
1428+{
1429+ struct io_ring_ctx *ctx = cs->ctx;
1430+
1431+ spin_lock_irq(&ctx->completion_lock);
1432+ while (!list_empty(&cs->list)) {
1433+ struct io_kiocb *req;
1434+
1435+ req = list_first_entry(&cs->list, struct io_kiocb, compl.list);
1436+ list_del(&req->compl.list);
1437+ __io_cqring_fill_event(req, req->result, req->compl.cflags);
1438+ if (!(req->flags & REQ_F_LINK_HEAD)) {
1439+ req->flags |= REQ_F_COMP_LOCKED;
1440+ io_put_req(req);
1441+ } else {
1442+ spin_unlock_irq(&ctx->completion_lock);
1443+ io_put_req(req);
1444+ spin_lock_irq(&ctx->completion_lock);
1445+ }
1446+ }
1447+ io_commit_cqring(ctx);
1448+ spin_unlock_irq(&ctx->completion_lock);
1449+
1450+ io_cqring_ev_posted(ctx);
1451+ cs->nr = 0;
1452+}
1453+
1454+static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags,
1455+ struct io_comp_state *cs)
1456+{
1457+ if (!cs) {
1458+ io_cqring_add_event(req, res, cflags);
1459+ io_put_req(req);
1460+ } else {
1461+ io_clean_op(req);
1462+ req->result = res;
1463+ req->compl.cflags = cflags;
1464+ list_add_tail(&req->compl.list, &cs->list);
1465+ if (++cs->nr >= 32)
1466+ io_submit_flush_completions(cs);
1467+ }
1468+}
1469+
1470+static void io_req_complete(struct io_kiocb *req, long res)
13461471 {
1347− __io_cqring_add_event(req, res, 0);
1472+ __io_req_complete(req, res, 0, NULL);
13481473 }
13491474
13501475 static inline bool io_is_fallback_req(struct io_kiocb *req)
@@ -1370,11 +1495,7 @@ static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, · 19 unmodified lines
13701495 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
13711496 struct io_kiocb *req;
13721497
1373− if (!state) {
1374− req = kmem_cache_alloc(req_cachep, gfp);
1375− if (unlikely(!req))
1376− goto fallback;
1377− } else if (!state->free_reqs) {
1498+ if (!state->free_reqs) {
13781499 size_t sz;
13791500 int ret;
13801501
@@ -1412,21 +1533,15 @@ static inline void io_put_file(struct io_kiocb *req, struct file *file, · 31 unmodified lines
14121533 fput(file);
14131534 }
14141535
1415−static void __io_req_aux_free(struct io_kiocb *req)
1536+static void io_dismantle_req(struct io_kiocb *req)
14161537 {
1417− if (req->flags & REQ_F_NEED_CLEANUP)
1418− io_cleanup_req(req);
1538+ io_clean_op(req);
14191539
1420− kfree(req->io);
1540+ if (req->io)
1541+ kfree(req->io);
14211542 if (req->file)
14221543 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
1423− __io_put_req_task(req);
1424− io_req_work_drop_env(req);
1425−}
1426−
1427−static void __io_free_req(struct io_kiocb *req)
1428−{
1429− __io_req_aux_free(req);
1544+ io_req_clean_work(req);
14301545
14311546 if (req->flags & REQ_F_INFLIGHT) {
14321547 struct io_ring_ctx *ctx = req->ctx;
@@ -1438,57 +1553,20 @@ static void __io_free_req(struct io_kiocb *req) · 5 unmodified lines
14381553 wake_up(&ctx->inflight_wait);
14391554 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
14401555 }
1441−
1442− percpu_ref_put(&req->ctx->refs);
1443− if (likely(!io_is_fallback_req(req)))
1444− kmem_cache_free(req_cachep, req);
1445− else
1446− clear_bit_unlock(0, (unsigned long *) &req->ctx->fallback_req);
14471556 }
14481557
1449−struct req_batch {
1450− void *reqs[IO_IOPOLL_BATCH];
1451− int to_free;
1452− int need_iter;
1453−};
1454−
1455−static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1558+static void __io_free_req(struct io_kiocb *req)
14561559 {
1457− if (!rb->to_free)
1458− return;
1459− if (rb->need_iter) {
1460− int i, inflight = 0;
1461− unsigned long flags;
1462−
1463− for (i = 0; i < rb->to_free; i++) {
1464− struct io_kiocb *req = rb->reqs[i];
1465−
1466− if (req->flags & REQ_F_INFLIGHT)
1467− inflight++;
1468− __io_req_aux_free(req);
1469− }
1470− if (!inflight)
1471− goto do_free;
1472−
1473− spin_lock_irqsave(&ctx->inflight_lock, flags);
1474− for (i = 0; i < rb->to_free; i++) {
1475− struct io_kiocb *req = rb->reqs[i];
1476−
1477− if (req->flags & REQ_F_INFLIGHT) {
1478− list_del(&req->inflight_entry);
1479− if (!--inflight)
1480− break;
1481− }
1482− }
1483− spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1560+ struct io_ring_ctx *ctx;
14841561
1485− if (waitqueue_active(&ctx->inflight_wait))
1486− wake_up(&ctx->inflight_wait);
1487− }
1488−do_free:
1489− kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1490− percpu_ref_put_many(&ctx->refs, rb->to_free);
1491− rb->to_free = rb->need_iter = 0;
1562+ io_dismantle_req(req);
1563+ __io_put_req_task(req);
1564+ ctx = req->ctx;
1565+ if (likely(!io_is_fallback_req(req)))
1566+ kmem_cache_free(req_cachep, req);
1567+ else
1568+ clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req);
1569+ percpu_ref_put(&ctx->refs);
14921570 }
14931571
14941572 static bool io_link_cancel_timeout(struct io_kiocb *req)
@@ -1508,53 +1586,67 @@ static bool io_link_cancel_timeout(struct io_kiocb *req) · 13 unmodified lines
15081586 return false;
15091587 }
15101588
1511−static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1589+static bool __io_kill_linked_timeout(struct io_kiocb *req)
1590+{
1591+ struct io_kiocb *link;
1592+ bool wake_ev;
1593+
1594+ if (list_empty(&req->link_list))
1595+ return false;
1596+ link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1597+ if (link->opcode != IORING_OP_LINK_TIMEOUT)
1598+ return false;
1599+
1600+ list_del_init(&link->link_list);
1601+ wake_ev = io_link_cancel_timeout(link);
1602+ req->flags &= ~REQ_F_LINK_TIMEOUT;
1603+ return wake_ev;
1604+}
1605+
1606+static void io_kill_linked_timeout(struct io_kiocb *req)
15121607 {
15131608 struct io_ring_ctx *ctx = req->ctx;
1514− bool wake_ev = false;
1609+ bool wake_ev;
15151610
1516− /* Already got next link */
1517− if (req->flags & REQ_F_LINK_NEXT)
1518− return;
1611+ if (!(req->flags & REQ_F_COMP_LOCKED)) {
1612+ unsigned long flags;
1613+
1614+ spin_lock_irqsave(&ctx->completion_lock, flags);
1615+ wake_ev = __io_kill_linked_timeout(req);
1616+ spin_unlock_irqrestore(&ctx->completion_lock, flags);
1617+ } else {
1618+ wake_ev = __io_kill_linked_timeout(req);
1619+ }
1620+
1621+ if (wake_ev)
1622+ io_cqring_ev_posted(ctx);
1623+}
1624+
1625+static struct io_kiocb *io_req_link_next(struct io_kiocb *req)
1626+{
1627+ struct io_kiocb *nxt;
15191628
15201629 /*
15211630 * The list should never be empty when we are called here. But could
15221631 * potentially happen if the chain is messed up, check to be on the
15231632 * safe side.
15241633 */
1525− while (!list_empty(&req->link_list)) {
1526− struct io_kiocb *nxt = list_first_entry(&req->link_list,
1527− struct io_kiocb, link_list);
1528−
1529− if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1530− (nxt->flags & REQ_F_TIMEOUT))) {
1531− list_del_init(&nxt->link_list);
1532− wake_ev |= io_link_cancel_timeout(nxt);
1533− req->flags &= ~REQ_F_LINK_TIMEOUT;
1534− continue;
1535− }
1536−
1537− list_del_init(&req->link_list);
1538− if (!list_empty(&nxt->link_list))
1539− nxt->flags |= REQ_F_LINK_HEAD;
1540− *nxtptr = nxt;
1541− break;
1542− }
1634+ if (unlikely(list_empty(&req->link_list)))
1635+ return NULL;
15431636
1544− req->flags |= REQ_F_LINK_NEXT;
1545− if (wake_ev)
1546− io_cqring_ev_posted(ctx);
1637+ nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list);
1638+ list_del_init(&req->link_list);
1639+ if (!list_empty(&nxt->link_list))
1640+ nxt->flags |= REQ_F_LINK_HEAD;
1641+ return nxt;
15471642 }
15481643
15491644 /*
15501645 * Called if REQ_F_LINK_HEAD is set, and we fail the head request
15511646 */
1552−static void io_fail_links(struct io_kiocb *req)
1647+static void __io_fail_links(struct io_kiocb *req)
15531648 {
15541649 struct io_ring_ctx *ctx = req->ctx;
1555− unsigned long flags;
1556−
1557− spin_lock_irqsave(&ctx->completion_lock, flags);
15581650
15591651 while (!list_empty(&req->link_list)) {
15601652 struct io_kiocb *link = list_first_entry(&req->link_list,
@@ -1563,88 +1655,225 @@ static void io_fail_links(struct io_kiocb *req) · 2 unmodified lines
15631655 list_del_init(&link->link_list);
15641656 trace_io_uring_fail_link(req, link);
15651657
1566− if ((req->flags & REQ_F_LINK_TIMEOUT) &&
1567− link->opcode == IORING_OP_LINK_TIMEOUT) {
1568− io_link_cancel_timeout(link);
1569− } else {
1570− io_cqring_fill_event(link, -ECANCELED);
1571− __io_double_put_req(link);
1572− }
1658+ io_cqring_fill_event(link, -ECANCELED);
1659+ __io_double_put_req(link);
15731660 req->flags &= ~REQ_F_LINK_TIMEOUT;
15741661 }
15751662
15761663 io_commit_cqring(ctx);
1577− spin_unlock_irqrestore(&ctx->completion_lock, flags);
15781664 io_cqring_ev_posted(ctx);
15791665 }
15801666
1581−static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
1667+static void io_fail_links(struct io_kiocb *req)
15821668 {
1583− if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1584− return;
1669+ struct io_ring_ctx *ctx = req->ctx;
15851670
1586− /*
1587− * If LINK is set, we have dependent requests in this chain. If we
1588− * didn't fail this request, queue the first one up, moving any other
1589− * dependencies to the next request. In case of failure, fail the rest
1590− * of the chain.
1591− */
1592− if (req->flags & REQ_F_FAIL_LINK) {
1593− io_fail_links(req);
1594− } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1595− REQ_F_LINK_TIMEOUT) {
1596− struct io_ring_ctx *ctx = req->ctx;
1671+ if (!(req->flags & REQ_F_COMP_LOCKED)) {
15971672 unsigned long flags;
15981673
1599− /*
1600− * If this is a timeout link, we could be racing with the
1601− * timeout timer. Grab the completion lock for this case to
1602− * protect against that.
1603− */
16041674 spin_lock_irqsave(&ctx->completion_lock, flags);
1605− io_req_link_next(req, nxt);
1675+ __io_fail_links(req);
16061676 spin_unlock_irqrestore(&ctx->completion_lock, flags);
16071677 } else {
1608− io_req_link_next(req, nxt);
1678+ __io_fail_links(req);
16091679 }
1610−}
16111680
1612−static void io_free_req(struct io_kiocb *req)
1681+ io_cqring_ev_posted(ctx);
1682+}
1683+
1684+static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
16131685 {
1614− struct io_kiocb *nxt = NULL;
1686+ req->flags &= ~REQ_F_LINK_HEAD;
1687+ if (req->flags & REQ_F_LINK_TIMEOUT)
1688+ io_kill_linked_timeout(req);
16151689
1616− io_req_find_next(req, &nxt);
1617− __io_free_req(req);
1690+ /*
1691+ * If LINK is set, we have dependent requests in this chain. If we
1692+ * didn't fail this request, queue the first one up, moving any other
1693+ * dependencies to the next request. In case of failure, fail the rest
1694+ * of the chain.
1695+ */
1696+ if (likely(!(req->flags & REQ_F_FAIL_LINK)))
1697+ return io_req_link_next(req);
1698+ io_fail_links(req);
1699+ return NULL;
1700+}
1701+
1702+static struct io_kiocb *io_req_find_next(struct io_kiocb *req)
1703+{
1704+ if (likely(!(req->flags & REQ_F_LINK_HEAD)))
1705+ return NULL;
1706+ return __io_req_find_next(req);
1707+}
1708+
1709+static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb)
1710+{
1711+ struct task_struct *tsk = req->task;
1712+ struct io_ring_ctx *ctx = req->ctx;
1713+ int ret, notify = TWA_RESUME;
1714+
1715+ /*
1716+ * SQPOLL kernel thread doesn't need notification, just a wakeup.
1717+ * If we're not using an eventfd, then TWA_RESUME is always fine,
1718+ * as we won't have dependencies between request completions for
1719+ * other kernel wait conditions.
1720+ */
1721+ if (ctx->flags & IORING_SETUP_SQPOLL)
1722+ notify = 0;
1723+ else if (ctx->cq_ev_fd)
1724+ notify = TWA_SIGNAL;
1725+
1726+ ret = task_work_add(tsk, cb, notify);
1727+ if (!ret)
1728+ wake_up_process(tsk);
1729+ return ret;
1730+}
1731+
1732+static void __io_req_task_cancel(struct io_kiocb *req, int error)
1733+{
1734+ struct io_ring_ctx *ctx = req->ctx;
1735+
1736+ spin_lock_irq(&ctx->completion_lock);
1737+ io_cqring_fill_event(req, error);
1738+ io_commit_cqring(ctx);
1739+ spin_unlock_irq(&ctx->completion_lock);
1740+
1741+ io_cqring_ev_posted(ctx);
1742+ req_set_fail_links(req);
1743+ io_double_put_req(req);
1744+}
1745+
1746+static void io_req_task_cancel(struct callback_head *cb)
1747+{
1748+ struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1749+
1750+ __io_req_task_cancel(req, -ECANCELED);
1751+}
1752+
1753+static void __io_req_task_submit(struct io_kiocb *req)
1754+{
1755+ struct io_ring_ctx *ctx = req->ctx;
1756+
1757+ if (!__io_sq_thread_acquire_mm(ctx)) {
1758+ mutex_lock(&ctx->uring_lock);
1759+ __io_queue_sqe(req, NULL, NULL);
1760+ mutex_unlock(&ctx->uring_lock);
1761+ } else {
1762+ __io_req_task_cancel(req, -EFAULT);
1763+ }
1764+}
1765+
1766+static void io_req_task_submit(struct callback_head *cb)
1767+{
1768+ struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
1769+
1770+ __io_req_task_submit(req);
1771+}
1772+
1773+static void io_req_task_queue(struct io_kiocb *req)
1774+{
1775+ int ret;
1776+
1777+ init_task_work(&req->task_work, io_req_task_submit);
1778+
1779+ ret = io_req_task_work_add(req, &req->task_work);
1780+ if (unlikely(ret)) {
1781+ struct task_struct *tsk;
1782+
1783+ init_task_work(&req->task_work, io_req_task_cancel);
1784+ tsk = io_wq_get_task(req->ctx->io_wq);
1785+ task_work_add(tsk, &req->task_work, 0);
1786+ wake_up_process(tsk);
1787+ }
1788+}
1789+
1790+static void io_queue_next(struct io_kiocb *req)
1791+{
1792+ struct io_kiocb *nxt = io_req_find_next(req);
16181793
16191794 if (nxt)
1620− io_queue_async_work(nxt);
1795+ io_req_task_queue(nxt);
16211796 }
16221797
1623−static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
1798+static void io_free_req(struct io_kiocb *req)
16241799 {
1625− struct io_kiocb *link;
1626− const struct io_op_def *def = &io_op_defs[nxt->opcode];
1800+ io_queue_next(req);
1801+ __io_free_req(req);
1802+}
16271803
1628− if ((nxt->flags & REQ_F_ISREG) && def->hash_reg_file)
1629− io_wq_hash_work(&nxt->work, file_inode(nxt->file));
1804+struct req_batch {
1805+ void *reqs[IO_IOPOLL_BATCH];
1806+ int to_free;
16301807
1631− *workptr = &nxt->work;
1632− link = io_prep_linked_timeout(nxt);
1633− if (link)
1634− nxt->flags |= REQ_F_QUEUE_TIMEOUT;
1808+ struct task_struct *task;
1809+ int task_refs;
1810+};
1811+
1812+static inline void io_init_req_batch(struct req_batch *rb)
1813+{
1814+ rb->to_free = 0;
1815+ rb->task_refs = 0;
1816+ rb->task = NULL;
1817+}
1818+
1819+static void __io_req_free_batch_flush(struct io_ring_ctx *ctx,
1820+ struct req_batch *rb)
1821+{
1822+ kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1823+ percpu_ref_put_many(&ctx->refs, rb->to_free);
1824+ rb->to_free = 0;
1825+}
1826+
1827+static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
1828+ struct req_batch *rb)
1829+{
1830+ if (rb->to_free)
1831+ __io_req_free_batch_flush(ctx, rb);
1832+ if (rb->task) {
1833+ put_task_struct_many(rb->task, rb->task_refs);
1834+ rb->task = NULL;
1835+ }
1836+}
1837+
1838+static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req)
1839+{
1840+ if (unlikely(io_is_fallback_req(req))) {
1841+ io_free_req(req);
1842+ return;
1843+ }
1844+ if (req->flags & REQ_F_LINK_HEAD)
1845+ io_queue_next(req);
1846+
1847+ if (req->flags & REQ_F_TASK_PINNED) {
1848+ if (req->task != rb->task) {
1849+ if (rb->task)
1850+ put_task_struct_many(rb->task, rb->task_refs);
1851+ rb->task = req->task;
1852+ rb->task_refs = 0;
1853+ }
1854+ rb->task_refs++;
1855+ req->flags &= ~REQ_F_TASK_PINNED;
1856+ }
1857+
1858+ io_dismantle_req(req);
1859+ rb->reqs[rb->to_free++] = req;
1860+ if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1861+ __io_req_free_batch_flush(req->ctx, rb);
16351862 }
16361863
16371864 /*
16381865 * Drop reference to request, return next in chain (if there is one) if this
16391866 * was the last reference to this request.
16401867 */
1641−__attribute__((nonnull))
1642−static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
1868+static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
16431869 {
1870+ struct io_kiocb *nxt = NULL;
1871+
16441872 if (refcount_dec_and_test(&req->refs)) {
1645− io_req_find_next(req, nxtptr);
1873+ nxt = io_req_find_next(req);
16461874 __io_free_req(req);
16471875 }
1876+ return nxt;
16481877 }
16491878
16501879 static void io_put_req(struct io_kiocb *req)
@@ -1653,24 +1882,20 @@ static void io_put_req(struct io_kiocb *req) · 2 unmodified lines
16531882 io_free_req(req);
16541883 }
16551884
1656−static void io_steal_work(struct io_kiocb *req,
1657− struct io_wq_work **workptr)
1885+static struct io_wq_work *io_steal_work(struct io_kiocb *req)
16581886 {
1887+ struct io_kiocb *nxt;
1888+
16591889 /*
1660− * It's in an io-wq worker, so there always should be at least
1661− * one reference, which will be dropped in io_put_work() just
1662− * after the current handler returns.
1663− *
1664− * It also means, that if the counter dropped to 1, then there is
1665− * no asynchronous users left, so it's safe to steal the next work.
1890+ * A ref is owned by io-wq in which context we're. So, if that's the
1891+ * last one, it's safe to steal next work. False negatives are Ok,
1892+ * it just will be re-punted async in io_put_work()
16661893 */
1667− if (refcount_read(&req->refs) == 1) {
1668− struct io_kiocb *nxt = NULL;
1894+ if (refcount_read(&req->refs) != 1)
1895+ return NULL;
16691896
1670− io_req_find_next(req, &nxt);
1671− if (nxt)
1672− io_wq_assign_next(workptr, nxt);
1673− }
1897+ nxt = io_req_find_next(req);
1898+ return nxt ? &nxt->work : NULL;
16741899 }
16751900
16761901 /*
@@ -1720,31 +1945,34 @@ static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) · 43 unmodified lines
17201945 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
17211946 }
17221947
1723−static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
1948+static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
17241949 {
1725− if ((req->flags & REQ_F_LINK_HEAD) || io_is_fallback_req(req))
1726− return false;
1950+ unsigned int cflags;
17271951
1728− if (req->file || req->io)
1729− rb->need_iter++;
1730−
1731− rb->reqs[rb->to_free++] = req;
1732− if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1733− io_free_req_many(req->ctx, rb);
1734− return true;
1952+ cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1953+ cflags |= IORING_CQE_F_BUFFER;
1954+ req->flags &= ~REQ_F_BUFFER_SELECTED;
1955+ kfree(kbuf);
1956+ return cflags;
17351957 }
17361958
1737−static int io_put_kbuf(struct io_kiocb *req)
1959+static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
17381960 {
17391961 struct io_buffer *kbuf;
1740− int cflags;
17411962
17421963 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
1743− cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
1744− cflags |= IORING_CQE_F_BUFFER;
1745− req->rw.addr = 0;
1746− kfree(kbuf);
1747− return cflags;
1964+ return io_put_kbuf(req, kbuf);
1965+}
1966+
1967+static inline bool io_run_task_work(void)
1968+{
1969+ if (current->task_works) {
1970+ __set_current_state(TASK_RUNNING);
1971+ task_work_run();
1972+ return true;
1973+ }
1974+
1975+ return false;
17481976 }
17491977
17501978 static void io_iopoll_queue(struct list_head *again)
@@ -1752,18 +1980,9 @@ static void io_iopoll_queue(struct list_head *again) · 1 unmodified line
17521980 struct io_kiocb *req;
17531981
17541982 do {
1755− req = list_first_entry(again, struct io_kiocb, list);
1756− list_del(&req->list);
1757−
1758− /* shouldn't happen unless io_uring is dying, cancel reqs */
1759− if (unlikely(!current->mm)) {
1760− io_complete_rw_common(&req->rw.kiocb, -EAGAIN);
1761− io_put_req(req);
1762− continue;
1763− }
1764−
1765− refcount_inc(&req->refs);
1766− io_queue_async_work(req);
1983+ req = list_first_entry(again, struct io_kiocb, inflight_entry);
1984+ list_del(&req->inflight_entry);
1985+ __io_complete_rw(req, -EAGAIN, 0, NULL);
17671986 } while (!list_empty(again));
17681987 }
17691988
@@ -1780,33 +1999,32 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, · 10 unmodified lines
17801999 /* order with ->result store in io_complete_rw_iopoll() */
17812000 smp_rmb();
17822001
1783− rb.to_free = rb.need_iter = 0;
2002+ io_init_req_batch(&rb);
17842003 while (!list_empty(done)) {
17852004 int cflags = 0;
17862005
1787− req = list_first_entry(done, struct io_kiocb, list);
2006+ req = list_first_entry(done, struct io_kiocb, inflight_entry);
17882007 if (READ_ONCE(req->result) == -EAGAIN) {
17892008 req->iopoll_completed = 0;
1790− list_move_tail(&req->list, &again);
2009+ list_move_tail(&req->inflight_entry, &again);
17912010 continue;
17922011 }
1793− list_del(&req->list);
2012+ list_del(&req->inflight_entry);
17942013
17952014 if (req->flags & REQ_F_BUFFER_SELECTED)
1796− cflags = io_put_kbuf(req);
2015+ cflags = io_put_rw_kbuf(req);
17972016
17982017 __io_cqring_fill_event(req, req->result, cflags);
17992018 (*nr_events)++;
18002019
1801− if (refcount_dec_and_test(&req->refs) &&
1802− !io_req_multi_free(&rb, req))
1803− io_free_req(req);
2020+ if (refcount_dec_and_test(&req->refs))
2021+ io_req_free_batch(&rb, req);
18042022 }
18052023
18062024 io_commit_cqring(ctx);
18072025 if (ctx->flags & IORING_SETUP_SQPOLL)
18082026 io_cqring_ev_posted(ctx);
1809− io_free_req_many(ctx, &rb);
2027+ io_req_free_batch_finish(ctx, &rb);
18102028
18112029 if (!list_empty(&again))
18122030 io_iopoll_queue(&again);
@@ -1827,7 +2045,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, · 14 unmodified lines
18272045 spin = !ctx->poll_multi_file && *nr_events < min;
18282046
18292047 ret = 0;
1830− list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
2048+ list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
18312049 struct kiocb *kiocb = &req->rw.kiocb;
18322050
18332051 /*
@@ -1836,7 +2054,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, · 2 unmodified lines
18362054 * and complete those lists first, if we have entries there.
18372055 */
18382056 if (READ_ONCE(req->iopoll_completed)) {
1839− list_move_tail(&req->list, &done);
2057+ list_move_tail(&req->inflight_entry, &done);
18402058 continue;
18412059 }
18422060 if (!list_empty(&done))
@@ -1846,6 +2064,10 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, · 3 unmodified lines
18462064 if (ret < 0)
18472065 break;
18482066
2067+ /* iopoll may have completed current req */
2068+ if (READ_ONCE(req->iopoll_completed))
2069+ list_move_tail(&req->inflight_entry, &done);
2070+
18492071 if (ret && spin)
18502072 spin = false;
18512073 ret = 0;
@@ -1865,13 +2087,13 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, · 13 unmodified lines
18652087 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
18662088 long min)
18672089 {
1868− while (!list_empty(&ctx->poll_list) && !need_resched()) {
2090+ while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
18692091 int ret;
18702092
18712093 ret = io_do_iopoll(ctx, nr_events, min);
18722094 if (ret < 0)
18732095 return ret;
1874− if (!min || *nr_events >= min)
2096+ if (*nr_events >= min)
18752097 return 0;
18762098 }
18772099
@@ -1882,29 +2104,37 @@ static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, · 4 unmodified lines
18822104 * We can't just wait for polled events to come to us, we have to actively
18832105 * find and complete them.
18842106 */
1885−static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
2107+static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
18862108 {
18872109 if (!(ctx->flags & IORING_SETUP_IOPOLL))
18882110 return;
18892111
18902112 mutex_lock(&ctx->uring_lock);
1891− while (!list_empty(&ctx->poll_list)) {
2113+ while (!list_empty(&ctx->iopoll_list)) {
18922114 unsigned int nr_events = 0;
18932115
1894− io_iopoll_getevents(ctx, &nr_events, 1);
2116+ io_do_iopoll(ctx, &nr_events, 0);
18952117
2118+ /* let it sleep and repeat later if can't complete a request */
2119+ if (nr_events == 0)
2120+ break;
18962121 /*
18972122 * Ensure we allow local-to-the-cpu processing to take place,
18982123 * in this case we need to ensure that we reap all events.
2124+ * Also let task_work, etc. to progress by releasing the mutex
18992125 */
1900− cond_resched();
2126+ if (need_resched()) {
2127+ mutex_unlock(&ctx->uring_lock);
2128+ cond_resched();
2129+ mutex_lock(&ctx->uring_lock);
2130+ }
19012131 }
19022132 mutex_unlock(&ctx->uring_lock);
19032133 }
19042134
1905−static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1906− long min)
2135+static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
19072136 {
2137+ unsigned int nr_events = 0;
19082138 int iters = 0, ret = 0;
19092139
19102140 /*
@@ -1914,8 +2144,6 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, · 3 unmodified lines
19142144 */
19152145 mutex_lock(&ctx->uring_lock);
19162146 do {
1917− int tmin = 0;
1918−
19192147 /*
19202148 * Don't enter poll loop if we already have events pending.
19212149 * If we do, we can potentially be spinning for commands that
@@ -1936,17 +2164,15 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, · 14 unmodified lines
19362164 */
19372165 if (!(++iters & 7)) {
19382166 mutex_unlock(&ctx->uring_lock);
2167+ io_run_task_work();
19392168 mutex_lock(&ctx->uring_lock);
19402169 }
19412170
1942− if (*nr_events < min)
1943− tmin = min - *nr_events;
1944−
1945− ret = io_iopoll_getevents(ctx, nr_events, tmin);
2171+ ret = io_iopoll_getevents(ctx, &nr_events, min);
19462172 if (ret <= 0)
19472173 break;
19482174 ret = 0;
1949− } while (min && !*nr_events && !need_resched());
2175+ } while (min && !nr_events && !need_resched());
19502176
19512177 mutex_unlock(&ctx->uring_lock);
19522178 return ret;
@@ -1966,13 +2192,8 @@ static void kiocb_end_write(struct io_kiocb *req) · 13 unmodified lines
19662192 file_end_write(req->file);
19672193 }
19682194
1969−static inline void req_set_fail_links(struct io_kiocb *req)
1970−{
1971− if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1972− req->flags |= REQ_F_FAIL_LINK;
1973−}
1974−
1975−static void io_complete_rw_common(struct kiocb *kiocb, long res)
2195+static void io_complete_rw_common(struct kiocb *kiocb, long res,
2196+ struct io_comp_state *cs)
19762197 {
19772198 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
19782199 int cflags = 0;
@@ -1983,16 +2204,96 @@ static void io_complete_rw_common(struct kiocb *kiocb, long res) · 4 unmodified lines
19832204 if (res != req->result)
19842205 req_set_fail_links(req);
19852206 if (req->flags & REQ_F_BUFFER_SELECTED)
1986− cflags = io_put_kbuf(req);
1987− __io_cqring_add_event(req, res, cflags);
2207+ cflags = io_put_rw_kbuf(req);
2208+ __io_req_complete(req, res, cflags, cs);
2209+}
2210+
2211+#ifdef CONFIG_BLOCK
2212+static bool io_resubmit_prep(struct io_kiocb *req, int error)
2213+{
2214+ struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2215+ ssize_t ret = -ECANCELED;
2216+ struct iov_iter iter;
2217+ int rw;
2218+
2219+ if (error) {
2220+ ret = error;
2221+ goto end_req;
2222+ }
2223+
2224+ switch (req->opcode) {
2225+ case IORING_OP_READV:
2226+ case IORING_OP_READ_FIXED:
2227+ case IORING_OP_READ:
2228+ rw = READ;
2229+ break;
2230+ case IORING_OP_WRITEV:
2231+ case IORING_OP_WRITE_FIXED:
2232+ case IORING_OP_WRITE:
2233+ rw = WRITE;
2234+ break;
2235+ default:
2236+ printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2237+ req->opcode);
2238+ goto end_req;
2239+ }
2240+
2241+ ret = io_import_iovec(rw, req, &iovec, &iter, false);
2242+ if (ret < 0)
2243+ goto end_req;
2244+ ret = io_setup_async_rw(req, ret, iovec, inline_vecs, &iter);
2245+ if (!ret)
2246+ return true;
2247+ kfree(iovec);
2248+end_req:
2249+ req_set_fail_links(req);
2250+ io_req_complete(req, ret);
2251+ return false;
2252+}
2253+
2254+static void io_rw_resubmit(struct callback_head *cb)
2255+{
2256+ struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2257+ struct io_ring_ctx *ctx = req->ctx;
2258+ int err;
2259+
2260+ err = io_sq_thread_acquire_mm(ctx, req);
2261+
2262+ if (io_resubmit_prep(req, err)) {
2263+ refcount_inc(&req->refs);
2264+ io_queue_async_work(req);
2265+ }
2266+}
2267+#endif
2268+
2269+static bool io_rw_reissue(struct io_kiocb *req, long res)
2270+{
2271+#ifdef CONFIG_BLOCK
2272+ int ret;
2273+
2274+ if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker())
2275+ return false;
2276+
2277+ init_task_work(&req->task_work, io_rw_resubmit);
2278+ ret = io_req_task_work_add(req, &req->task_work);
2279+ if (!ret)
2280+ return true;
2281+#endif
2282+ return false;
2283+}
2284+
2285+static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2286+ struct io_comp_state *cs)
2287+{
2288+ if (!io_rw_reissue(req, res))
2289+ io_complete_rw_common(&req->rw.kiocb, res, cs);
19882290 }
19892291
19902292 static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
19912293 {
19922294 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
19932295
1994− io_complete_rw_common(kiocb, res);
1995− io_put_req(req);
2296+ __io_complete_rw(req, res, res2, NULL);
19962297 }
19972298
19982299 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
@@ -2026,13 +2327,13 @@ static void io_iopoll_req_issued(struct io_kiocb *req) · 27 unmodified lines
20262327 * how we do polling eventually, not spinning if we're on potentially
20272328 * different devices.
20282329 */
2029− if (list_empty(&ctx->poll_list)) {
2330+ if (list_empty(&ctx->iopoll_list)) {
20302331 ctx->poll_multi_file = false;
20312332 } else if (!ctx->poll_multi_file) {
20322333 struct io_kiocb *list_req;
20332334
2034− list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
2035− list);
2335+ list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
2336+ inflight_entry);
20362337 if (list_req->file != req->file)
20372338 ctx->poll_multi_file = true;
20382339 }
@@ -2042,9 +2343,9 @@ static void io_iopoll_req_issued(struct io_kiocb *req) · 3 unmodified lines
20422343 * it to the front so we find it first.
20432344 */
20442345 if (READ_ONCE(req->iopoll_completed))
2045− list_add(&req->list, &ctx->poll_list);
2346+ list_add(&req->inflight_entry, &ctx->iopoll_list);
20462347 else
2047− list_add_tail(&req->list, &ctx->poll_list);
2348+ list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
20482349
20492350 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
20502351 wq_has_sleeper(&ctx->sqo_wait))
@@ -2053,10 +2354,8 @@ static void io_iopoll_req_issued(struct io_kiocb *req) · 2 unmodified lines
20532354
20542355 static void __io_state_file_put(struct io_submit_state *state)
20552356 {
2056− int diff = state->has_refs - state->used_refs;
2057−
2058− if (diff)
2059− fput_many(state->file, diff);
2357+ if (state->has_refs)
2358+ fput_many(state->file, state->has_refs);
20602359 state->file = NULL;
20612360 }
20622361
@@ -2078,7 +2377,7 @@ static struct file *__io_file_get(struct io_submit_state *state, int fd) · 15 unmodified lines
20782377
20792378 if (state->file) {
20802379 if (state->fd == fd) {
2081− state->used_refs++;
2380+ state->has_refs--;
20822381 state->ios_left--;
20832382 return state->file;
20842383 }
@@ -2089,12 +2388,20 @@ static struct file *__io_file_get(struct io_submit_state *state, int fd) · 4 unmodified lines
20892388 return NULL;
20902389
20912390 state->fd = fd;
2092− state->has_refs = state->ios_left;
2093− state->used_refs = 1;
20942391 state->ios_left--;
2392+ state->has_refs = state->ios_left;
20952393 return state->file;
20962394 }
20972395
2396+static bool io_bdev_nowait(struct block_device *bdev)
2397+{
2398+#ifdef CONFIG_BLOCK
2399+ return !bdev || queue_is_mq(bdev_get_queue(bdev));
2400+#else
2401+ return true;
2402+#endif
2403+}
2404+
20982405 /*
20992406 * If we tracked the file through the SCM inflight mechanism, we could support
21002407 * any file. For now, just ensure that anything potentially problematic is done
@@ -2104,10 +2411,19 @@ static bool io_file_supports_async(struct file *file, int rw) · 3 unmodified lines
21042411 {
21052412 umode_t mode = file_inode(file)->i_mode;
21062413
2107− if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
2108− return true;
2109− if (S_ISREG(mode) && file->f_op != &io_uring_fops)
2414+ if (S_ISBLK(mode)) {
2415+ if (io_bdev_nowait(file->f_inode->i_bdev))
2416+ return true;
2417+ return false;
2418+ }
2419+ if (S_ISCHR(mode) || S_ISSOCK(mode))
21102420 return true;
2421+ if (S_ISREG(mode)) {
2422+ if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
2423+ file->f_op != &io_uring_fops)
2424+ return true;
2425+ return false;
2426+ }
21112427
21122428 /* any ->read/write should understand O_NONBLOCK */
21132429 if (file->f_flags & O_NONBLOCK)
@@ -2158,6 +2474,9 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 44 unmodified lines
21582474 if (kiocb->ki_flags & IOCB_NOWAIT)
21592475 req->flags |= REQ_F_NOWAIT;
21602476
2477+ if (kiocb->ki_flags & IOCB_DIRECT)
2478+ io_get_req_task(req);
2479+
21612480 if (force_nonblock)
21622481 kiocb->ki_flags |= IOCB_NOWAIT;
21632482
@@ -2168,8 +2487,8 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 4 unmodified lines
21682487
21692488 kiocb->ki_flags |= IOCB_HIPRI;
21702489 kiocb->ki_complete = io_complete_rw_iopoll;
2171− req->result = 0;
21722490 req->iopoll_completed = 0;
2491+ io_get_req_task(req);
21732492 } else {
21742493 if (kiocb->ki_flags & IOCB_HIPRI)
21752494 return -EINVAL;
@@ -2203,14 +2522,15 @@ static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) · 27 unmodified lines
22032522 }
22042523 }
22052524
2206−static void kiocb_done(struct kiocb *kiocb, ssize_t ret)
2525+static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
2526+ struct io_comp_state *cs)
22072527 {
22082528 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
22092529
22102530 if (req->flags & REQ_F_CUR_POS)
22112531 req->file->f_pos = kiocb->ki_pos;
22122532 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
2213− io_complete_rw(kiocb, ret, 0);
2533+ __io_complete_rw(req, ret, 0, cs);
22142534 else
22152535 io_rw_done(kiocb, ret);
22162536 }
@@ -2466,10 +2786,8 @@ static ssize_t io_import_iovec(int rw, struct io_kiocb *req, · 249 unmodified lines
24662786 if (req->io) {
24672787 struct io_async_rw *iorw = &req->io->rw;
24682788
2469− *iovec = iorw->iov;
2470− iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2471− if (iorw->iov == iorw->fast_iov)
2472− *iovec = NULL;
2789+ iov_iter_init(iter, rw, iorw->iov, iorw->nr_segs, iorw->size);
2790+ *iovec = NULL;
24732791 return iorw->size;
24742792 }
24752793
@@ -2554,15 +2872,17 @@ static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size, · 78 unmodified lines
25542872 struct iovec *iovec, struct iovec *fast_iov,
25552873 struct iov_iter *iter)
25562874 {
2557− req->io->rw.nr_segs = iter->nr_segs;
2558− req->io->rw.size = io_size;
2559− req->io->rw.iov = iovec;
2560− if (!req->io->rw.iov) {
2561− req->io->rw.iov = req->io->rw.fast_iov;
2562− if (req->io->rw.iov != fast_iov)
2563− memcpy(req->io->rw.iov, fast_iov,
2875+ struct io_async_rw *rw = &req->io->rw;
2876+
2877+ rw->nr_segs = iter->nr_segs;
2878+ rw->size = io_size;
2879+ if (!iovec) {
2880+ rw->iov = rw->fast_iov;
2881+ if (rw->iov != fast_iov)
2882+ memcpy(rw->iov, fast_iov,
25642883 sizeof(struct iovec) * iter->nr_segs);
25652884 } else {
2885+ rw->iov = iovec;
25662886 req->flags |= REQ_F_NEED_CLEANUP;
25672887 }
25682888 }
@@ -2593,95 +2913,205 @@ static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, · 24 unmodified lines
25932913
25942914 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
25952915 }
2596− return 0;
2916+ return 0;
2917+}
2918+
2919+static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
2920+ bool force_nonblock)
2921+{
2922+ struct io_async_ctx *io = req->io;
2923+ struct iov_iter iter;
2924+ ssize_t ret;
2925+
2926+ io->rw.iov = io->rw.fast_iov;
2927+ req->io = NULL;
2928+ ret = io_import_iovec(rw, req, &io->rw.iov, &iter, !force_nonblock);
2929+ req->io = io;
2930+ if (unlikely(ret < 0))
2931+ return ret;
2932+
2933+ io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2934+ return 0;
2935+}
2936+
2937+static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2938+ bool force_nonblock)
2939+{
2940+ ssize_t ret;
2941+
2942+ ret = io_prep_rw(req, sqe, force_nonblock);
2943+ if (ret)
2944+ return ret;
2945+
2946+ if (unlikely(!(req->file->f_mode & FMODE_READ)))
2947+ return -EBADF;
2948+
2949+ /* either don't need iovec imported or already have it */
2950+ if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
2951+ return 0;
2952+ return io_rw_prep_async(req, READ, force_nonblock);
2953+}
2954+
2955+static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
2956+ int sync, void *arg)
2957+{
2958+ struct wait_page_queue *wpq;
2959+ struct io_kiocb *req = wait->private;
2960+ struct wait_page_key *key = arg;
2961+ int ret;
2962+
2963+ wpq = container_of(wait, struct wait_page_queue, wait);
2964+
2965+ if (!wake_page_match(wpq, key))
2966+ return 0;
2967+
2968+ /* Stop waking things up if the page is locked again */
2969+ if (test_bit(key->bit_nr, &key->page->flags))
2970+ return -1;
2971+
2972+ list_del_init(&wait->entry);
2973+
2974+ init_task_work(&req->task_work, io_req_task_submit);
2975+ /* submit ref gets dropped, acquire a new one */
2976+ refcount_inc(&req->refs);
2977+ ret = io_req_task_work_add(req, &req->task_work);
2978+ if (unlikely(ret)) {
2979+ struct task_struct *tsk;
2980+
2981+ /* queue just for cancelation */
2982+ init_task_work(&req->task_work, io_req_task_cancel);
2983+ tsk = io_wq_get_task(req->ctx->io_wq);
2984+ task_work_add(tsk, &req->task_work, 0);
2985+ wake_up_process(tsk);
2986+ }
2987+ return 1;
2988+}
2989+
2990+static inline int kiocb_wait_page_queue_init(struct kiocb *kiocb,
2991+ struct wait_page_queue *wait,
2992+ wait_queue_func_t func,
2993+ void *data)
2994+{
2995+ /* Can't support async wakeup with polled IO */
2996+ if (kiocb->ki_flags & IOCB_HIPRI)
2997+ return -EINVAL;
2998+ if (kiocb->ki_filp->f_mode & FMODE_BUF_RASYNC) {
2999+ wait->wait.func = func;
3000+ wait->wait.private = data;
3001+ wait->wait.flags = 0;
3002+ INIT_LIST_HEAD(&wait->wait.entry);
3003+ kiocb->ki_flags |= IOCB_WAITQ;
3004+ kiocb->ki_waitq = wait;
3005+ return 0;
3006+ }
3007+
3008+ return -EOPNOTSUPP;
25973009 }
25983010
2599−static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2600− bool force_nonblock)
3011+
3012+static bool io_rw_should_retry(struct io_kiocb *req)
26013013 {
2602− struct io_async_ctx *io;
2603− struct iov_iter iter;
2604− ssize_t ret;
3014+ struct kiocb *kiocb = &req->rw.kiocb;
3015+ int ret;
26053016
2606− ret = io_prep_rw(req, sqe, force_nonblock);
2607− if (ret)
2608− return ret;
3017+ /* never retry for NOWAIT, we just complete with -EAGAIN */
3018+ if (req->flags & REQ_F_NOWAIT)
3019+ return false;
26093020
2610− if (unlikely(!(req->file->f_mode & FMODE_READ)))
2611− return -EBADF;
3021+ /* already tried, or we're doing O_DIRECT */
3022+ if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_WAITQ))
3023+ return false;
3024+ /*
3025+ * just use poll if we can, and don't attempt if the fs doesn't
3026+ * support callback based unlocks
3027+ */
3028+ if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3029+ return false;
26123030
2613− /* either don't need iovec imported or already have it */
2614− if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
2615− return 0;
3031+ /*
3032+ * If request type doesn't require req->io to defer in general,
3033+ * we need to allocate it here
3034+ */
3035+ if (!req->io && __io_alloc_async_ctx(req))
3036+ return false;
26163037
2617− io = req->io;
2618− io->rw.iov = io->rw.fast_iov;
2619− req->io = NULL;
2620− ret = io_import_iovec(READ, req, &io->rw.iov, &iter, !force_nonblock);
2621− req->io = io;
2622− if (ret < 0)
2623− return ret;
3038+ ret = kiocb_wait_page_queue_init(kiocb, &req->io->rw.wpq,
3039+ io_async_buf_func, req);
3040+ if (!ret) {
3041+ io_get_req_task(req);
3042+ return true;
3043+ }
26243044
2625− io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2626− return 0;
3045+ return false;
3046+}
3047+
3048+static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3049+{
3050+ if (req->file->f_op->read_iter)
3051+ return call_read_iter(req->file, &req->rw.kiocb, iter);
3052+ return loop_rw_iter(READ, req->file, &req->rw.kiocb, iter);
26273053 }
26283054
2629−static int io_read(struct io_kiocb *req, bool force_nonblock)
3055+static int io_read(struct io_kiocb *req, bool force_nonblock,
3056+ struct io_comp_state *cs)
26303057 {
26313058 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
26323059 struct kiocb *kiocb = &req->rw.kiocb;
26333060 struct iov_iter iter;
26343061 size_t iov_count;
2635− ssize_t io_size, ret;
3062+ ssize_t io_size, ret, ret2;
3063+ unsigned long nr_segs;
26363064
26373065 ret = io_import_iovec(READ, req, &iovec, &iter, !force_nonblock);
26383066 if (ret < 0)
26393067 return ret;
3068+ io_size = ret;
3069+ req->result = io_size;
26403070
26413071 /* Ensure we clear previously set non-block flag */
26423072 if (!force_nonblock)
26433073 kiocb->ki_flags &= ~IOCB_NOWAIT;
26443074
2645− req->result = 0;
2646− io_size = ret;
2647− if (req->flags & REQ_F_LINK_HEAD)
2648− req->result = io_size;
2649−
2650− /*
2651− * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2652− * we know to async punt it even if it was opened O_NONBLOCK
2653− */
3075+ /* If the file doesn't support async, just async punt */
26543076 if (force_nonblock && !io_file_supports_async(req->file, READ))
26553077 goto copy_iov;
26563078
26573079 iov_count = iov_iter_count(&iter);
3080+ nr_segs = iter.nr_segs;
26583081 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
2659− if (!ret) {
2660− ssize_t ret2;
3082+ if (unlikely(ret))
3083+ goto out_free;
26613084
2662− if (req->file->f_op->read_iter)
2663− ret2 = call_read_iter(req->file, kiocb, &iter);
2664− else
2665− ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
3085+ ret2 = io_iter_do_read(req, &iter);
26663086
2667− /* Catch -EAGAIN return for forced non-blocking submission */
2668− if (!force_nonblock || ret2 != -EAGAIN) {
2669− kiocb_done(kiocb, ret2);
2670− } else {
3087+ /* Catch -EAGAIN return for forced non-blocking submission */
3088+ if (!force_nonblock || (ret2 != -EAGAIN && ret2 != -EIO)) {
3089+ kiocb_done(kiocb, ret2, cs);
3090+ } else {
3091+ iter.count = iov_count;
3092+ iter.nr_segs = nr_segs;
26713093 copy_iov:
2672− ret = io_setup_async_rw(req, io_size, iovec,
2673− inline_vecs, &iter);
2674− if (ret)
3094+ ret = io_setup_async_rw(req, io_size, iovec, inline_vecs,
3095+ &iter);
3096+ if (ret)
3097+ goto out_free;
3098+ /* it's copied and will be cleaned with ->io */
3099+ iovec = NULL;
3100+ /* if we can retry, do so with the callbacks armed */
3101+ if (io_rw_should_retry(req)) {
3102+ ret2 = io_iter_do_read(req, &iter);
3103+ if (ret2 == -EIOCBQUEUED) {
26753104 goto out_free;
2676− /* any defer here is final, must blocking retry */
2677− if (!(req->flags & REQ_F_NOWAIT) &&
2678− !file_can_poll(req->file))
2679− req->flags |= REQ_F_MUST_PUNT;
2680− return -EAGAIN;
3105+ } else if (ret2 != -EAGAIN) {
3106+ kiocb_done(kiocb, ret2, cs);
3107+ goto out_free;
3108+ }
26813109 }
3110+ kiocb->ki_flags &= ~IOCB_WAITQ;
3111+ return -EAGAIN;
26823112 }
26833113 out_free:
2684− if (!(req->flags & REQ_F_NEED_CLEANUP))
3114+ if (iovec)
26853115 kfree(iovec);
26863116 return ret;
26873117 }
@@ -2689,8 +3119,6 @@ out_free: · 1 unmodified line
26893119 static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
26903120 bool force_nonblock)
26913121 {
2692− struct io_async_ctx *io;
2693− struct iov_iter iter;
26943122 ssize_t ret;
26953123
26963124 ret = io_prep_rw(req, sqe, force_nonblock);
@@ -2700,49 +3128,33 @@ static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 3 unmodified lines
27003128 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
27013129 return -EBADF;
27023130
2703− req->fsize = rlimit(RLIMIT_FSIZE);
2704−
27053131 /* either don't need iovec imported or already have it */
27063132 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
27073133 return 0;
2708−
2709− io = req->io;
2710− io->rw.iov = io->rw.fast_iov;
2711− req->io = NULL;
2712− ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter, !force_nonblock);
2713− req->io = io;
2714− if (ret < 0)
2715− return ret;
2716−
2717− io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2718− return 0;
3134+ return io_rw_prep_async(req, WRITE, force_nonblock);
27193135 }
27203136
2721−static int io_write(struct io_kiocb *req, bool force_nonblock)
3137+static int io_write(struct io_kiocb *req, bool force_nonblock,
3138+ struct io_comp_state *cs)
27223139 {
27233140 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
27243141 struct kiocb *kiocb = &req->rw.kiocb;
27253142 struct iov_iter iter;
27263143 size_t iov_count;
2727− ssize_t ret, io_size;
3144+ ssize_t ret, ret2, io_size;
3145+ unsigned long nr_segs;
27283146
27293147 ret = io_import_iovec(WRITE, req, &iovec, &iter, !force_nonblock);
27303148 if (ret < 0)
27313149 return ret;
3150+ io_size = ret;
3151+ req->result = io_size;
27323152
27333153 /* Ensure we clear previously set non-block flag */
27343154 if (!force_nonblock)
27353155 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
27363156
2737− req->result = 0;
2738− io_size = ret;
2739− if (req->flags & REQ_F_LINK_HEAD)
2740− req->result = io_size;
2741−
2742− /*
2743− * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2744− * we know to async punt it even if it was opened O_NONBLOCK
2745− */
3157+ /* If the file doesn't support async, just async punt */
27463158 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
27473159 goto copy_iov;
27483160
@@ -2752,59 +3164,53 @@ static int io_write(struct io_kiocb *req, bool force_nonblock) · 3 unmodified lines
27523164 goto copy_iov;
27533165
27543166 iov_count = iov_iter_count(&iter);
3167+ nr_segs = iter.nr_segs;
27553168 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
2756− if (!ret) {
2757− ssize_t ret2;
2758−
2759− /*
2760− * Open-code file_start_write here to grab freeze protection,
2761− * which will be released by another thread in
2762− * io_complete_rw(). Fool lockdep by telling it the lock got
2763− * released so that it doesn't complain about the held lock when
2764− * we return to userspace.
2765− */
2766− if (req->flags & REQ_F_ISREG) {
2767− __sb_start_write(file_inode(req->file)->i_sb,
2768− SB_FREEZE_WRITE, true);
2769− __sb_writers_release(file_inode(req->file)->i_sb,
2770− SB_FREEZE_WRITE);
2771− }
2772− kiocb->ki_flags |= IOCB_WRITE;
2773−
2774− if (!force_nonblock)
2775− current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
3169+ if (unlikely(ret))
3170+ goto out_free;
27763171
2777− if (req->file->f_op->write_iter)
2778− ret2 = call_write_iter(req->file, kiocb, &iter);
2779− else
2780− ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
3172+ /*
3173+ * Open-code file_start_write here to grab freeze protection,
3174+ * which will be released by another thread in
3175+ * io_complete_rw(). Fool lockdep by telling it the lock got
3176+ * released so that it doesn't complain about the held lock when
3177+ * we return to userspace.
3178+ */
3179+ if (req->flags & REQ_F_ISREG) {
3180+ __sb_start_write(file_inode(req->file)->i_sb,
3181+ SB_FREEZE_WRITE, true);
3182+ __sb_writers_release(file_inode(req->file)->i_sb,
3183+ SB_FREEZE_WRITE);
3184+ }
3185+ kiocb->ki_flags |= IOCB_WRITE;
27813186
2782− if (!force_nonblock)
2783− current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
3187+ if (req->file->f_op->write_iter)
3188+ ret2 = call_write_iter(req->file, kiocb, &iter);
3189+ else
3190+ ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
27843191
2785− /*
2786− * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
2787− * retry them without IOCB_NOWAIT.
2788− */
2789− if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2790− ret2 = -EAGAIN;
2791− if (!force_nonblock || ret2 != -EAGAIN) {
2792− kiocb_done(kiocb, ret2);
2793− } else {
3192+ /*
3193+ * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3194+ * retry them without IOCB_NOWAIT.
3195+ */
3196+ if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3197+ ret2 = -EAGAIN;
3198+ if (!force_nonblock || ret2 != -EAGAIN) {
3199+ kiocb_done(kiocb, ret2, cs);
3200+ } else {
3201+ iter.count = iov_count;
3202+ iter.nr_segs = nr_segs;
27943203 copy_iov:
2795− ret = io_setup_async_rw(req, io_size, iovec,
2796− inline_vecs, &iter);
2797− if (ret)
2798− goto out_free;
2799− /* any defer here is final, must blocking retry */
2800− if (!(req->flags & REQ_F_NOWAIT) &&
2801− !file_can_poll(req->file))
2802− req->flags |= REQ_F_MUST_PUNT;
2803− return -EAGAIN;
2804− }
3204+ ret = io_setup_async_rw(req, io_size, iovec, inline_vecs,
3205+ &iter);
3206+ if (ret)
3207+ goto out_free;
3208+ /* it's copied and will be cleaned with ->io */
3209+ iovec = NULL;
3210+ return -EAGAIN;
28053211 }
28063212 out_free:
2807− if (!(req->flags & REQ_F_NEED_CLEANUP))
3213+ if (iovec)
28083214 kfree(iovec);
28093215 return ret;
28103216 }
@@ -2870,10 +3276,9 @@ static int io_tee(struct io_kiocb *req, bool force_nonblock) · 59 unmodified lines
28703276 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
28713277 req->flags &= ~REQ_F_NEED_CLEANUP;
28723278
2873− io_cqring_add_event(req, ret);
28743279 if (ret != sp->len)
28753280 req_set_fail_links(req);
2876− io_put_req(req);
3281+ io_req_complete(req, ret);
28773282 return 0;
28783283 }
28793284
@@ -2907,25 +3312,23 @@ static int io_splice(struct io_kiocb *req, bool force_nonblock) · 27 unmodified lines
29073312 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
29083313 req->flags &= ~REQ_F_NEED_CLEANUP;
29093314
2910− io_cqring_add_event(req, ret);
29113315 if (ret != sp->len)
29123316 req_set_fail_links(req);
2913− io_put_req(req);
3317+ io_req_complete(req, ret);
29143318 return 0;
29153319 }
29163320
29173321 /*
29183322 * IORING_OP_NOP just posts a completion event, nothing else.
29193323 */
2920−static int io_nop(struct io_kiocb *req)
3324+static int io_nop(struct io_kiocb *req, struct io_comp_state *cs)
29213325 {
29223326 struct io_ring_ctx *ctx = req->ctx;
29233327
29243328 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
29253329 return -EINVAL;
29263330
2927− io_cqring_add_event(req, 0);
2928− io_put_req(req);
3331+ __io_req_complete(req, 0, 0, cs);
29293332 return 0;
29303333 }
29313334
@@ -2964,8 +3367,7 @@ static int io_fsync(struct io_kiocb *req, bool force_nonblock) · 32 unmodified lines
29643367 req->sync.flags & IORING_FSYNC_DATASYNC);
29653368 if (ret < 0)
29663369 req_set_fail_links(req);
2967− io_cqring_add_event(req, ret);
2968− io_put_req(req);
3370+ io_req_complete(req, ret);
29693371 return 0;
29703372 }
29713373
@@ -2980,7 +3382,6 @@ static int io_fallocate_prep(struct io_kiocb *req, · 8 unmodified lines
29803382 req->sync.off = READ_ONCE(sqe->off);
29813383 req->sync.len = READ_ONCE(sqe->addr);
29823384 req->sync.mode = READ_ONCE(sqe->len);
2983− req->fsize = rlimit(RLIMIT_FSIZE);
29843385 return 0;
29853386 }
29863387
@@ -2991,15 +3392,11 @@ static int io_fallocate(struct io_kiocb *req, bool force_nonblock) · 4 unmodified lines
29913392 /* fallocate always requiring blocking context */
29923393 if (force_nonblock)
29933394 return -EAGAIN;
2994−
2995− current->signal->rlim[RLIMIT_FSIZE].rlim_cur = req->fsize;
29963395 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
29973396 req->sync.len);
2998− current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
29993397 if (ret < 0)
30003398 req_set_fail_links(req);
3001− io_cqring_add_event(req, ret);
3002− io_put_req(req);
3399+ io_req_complete(req, ret);
30033400 return 0;
30043401 }
30053402
@@ -3095,8 +3492,7 @@ err: · 89 unmodified lines
30953492 req->flags &= ~REQ_F_NEED_CLEANUP;
30963493 if (ret < 0)
30973494 req_set_fail_links(req);
3098− io_cqring_add_event(req, ret);
3099− io_put_req(req);
3495+ io_req_complete(req, ret);
31003496 return 0;
31013497 }
31023498
@@ -3150,7 +3546,8 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, · 47 unmodified lines
31503546 return i;
31513547 }
31523548
3153−static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock)
3549+static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock,
3550+ struct io_comp_state *cs)
31543551 {
31553552 struct io_provide_buf *p = &req->pbuf;
31563553 struct io_ring_ctx *ctx = req->ctx;
@@ -3169,8 +3566,7 @@ static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock) · 12 unmodified lines
31693566 io_ring_submit_lock(ctx, !force_nonblock);
31703567 if (ret < 0)
31713568 req_set_fail_links(req);
3172− io_cqring_add_event(req, ret);
3173− io_put_req(req);
3569+ __io_req_complete(req, ret, 0, cs);
31743570 return 0;
31753571 }
31763572
@@ -3228,7 +3624,8 @@ static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) · 51 unmodified lines
32283624 return i ? i : -ENOMEM;
32293625 }
32303626
3231−static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock)
3627+static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock,
3628+ struct io_comp_state *cs)
32323629 {
32333630 struct io_provide_buf *p = &req->pbuf;
32343631 struct io_ring_ctx *ctx = req->ctx;
@@ -3257,8 +3654,7 @@ out: · 22 unmodified lines
32573654 io_ring_submit_unlock(ctx, !force_nonblock);
32583655 if (ret < 0)
32593656 req_set_fail_links(req);
3260− io_cqring_add_event(req, ret);
3261− io_put_req(req);
3657+ __io_req_complete(req, ret, 0, cs);
32623658 return 0;
32633659 }
32643660
@@ -3289,7 +3685,8 @@ static int io_epoll_ctl_prep(struct io_kiocb *req, · 24 unmodified lines
32893685 #endif
32903686 }
32913687
3292−static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock)
3688+static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock,
3689+ struct io_comp_state *cs)
32933690 {
32943691 #if defined(CONFIG_EPOLL)
32953692 struct io_epoll *ie = &req->epoll;
@@ -3301,8 +3698,7 @@ static int io_epoll_ctl(struct io_kiocb *req, bool force_nonblock) · 5 unmodified lines
33013698
33023699 if (ret < 0)
33033700 req_set_fail_links(req);
3304− io_cqring_add_event(req, ret);
3305− io_put_req(req);
3701+ __io_req_complete(req, ret, 0, cs);
33063702 return 0;
33073703 #else
33083704 return -EOPNOTSUPP;
@@ -3338,8 +3734,7 @@ static int io_madvise(struct io_kiocb *req, bool force_nonblock) · 29 unmodified lines
33383734 ret = do_madvise(ma->addr, ma->len, ma->advice);
33393735 if (ret < 0)
33403736 req_set_fail_links(req);
3341− io_cqring_add_event(req, ret);
3342− io_put_req(req);
3737+ io_req_complete(req, ret);
33433738 return 0;
33443739 #else
33453740 return -EOPNOTSUPP;
@@ -3378,8 +3773,7 @@ static int io_fadvise(struct io_kiocb *req, bool force_nonblock) · 32 unmodified lines
33783773 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
33793774 if (ret < 0)
33803775 req_set_fail_links(req);
3381− io_cqring_add_event(req, ret);
3382− io_put_req(req);
3776+ io_req_complete(req, ret);
33833777 return 0;
33843778 }
33853779
@@ -3418,8 +3812,7 @@ static int io_statx(struct io_kiocb *req, bool force_nonblock) · 32 unmodified lines
34183812
34193813 if (ret < 0)
34203814 req_set_fail_links(req);
3421− io_cqring_add_event(req, ret);
3422− io_put_req(req);
3815+ io_req_complete(req, ret);
34233816 return 0;
34243817 }
34253818
@@ -3450,7 +3843,8 @@ static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 24 unmodified lines
34503843 return 0;
34513844 }
34523845
3453−static int io_close(struct io_kiocb *req, bool force_nonblock)
3846+static int io_close(struct io_kiocb *req, bool force_nonblock,
3847+ struct io_comp_state *cs)
34543848 {
34553849 struct io_close *close = &req->close;
34563850 int ret;
@@ -3464,8 +3858,10 @@ static int io_close(struct io_kiocb *req, bool force_nonblock) · 7 unmodified lines
34643858
34653859 /* if the file has a flush method, be safe and punt to async */
34663860 if (close->put_file->f_op->flush && force_nonblock) {
3861+ /* was never set, but play safe */
3862+ req->flags &= ~REQ_F_NOWAIT;
34673863 /* avoid grabbing files - we don't need the files */
3468− req->flags |= REQ_F_NO_FILE_TABLE | REQ_F_MUST_PUNT;
3864+ req->flags |= REQ_F_NO_FILE_TABLE;
34693865 return -EAGAIN;
34703866 }
34713867
@@ -3473,10 +3869,9 @@ static int io_close(struct io_kiocb *req, bool force_nonblock) · 1 unmodified line
34733869 ret = filp_close(close->put_file, req->work.files);
34743870 if (ret < 0)
34753871 req_set_fail_links(req);
3476− io_cqring_add_event(req, ret);
34773872 fput(close->put_file);
34783873 close->put_file = NULL;
3479− io_put_req(req);
3874+ __io_req_complete(req, ret, 0, cs);
34803875 return 0;
34813876 }
34823877
@@ -3510,8 +3905,7 @@ static int io_sync_file_range(struct io_kiocb *req, bool force_nonblock) · 27 unmodified lines
35103905 req->sync.flags);
35113906 if (ret < 0)
35123907 req_set_fail_links(req);
3513− io_cqring_add_event(req, ret);
3514− io_put_req(req);
3908+ io_req_complete(req, ret);
35153909 return 0;
35163910 }
35173911
@@ -3531,6 +3925,15 @@ static int io_setup_async_msg(struct io_kiocb *req, · 13 unmodified lines
35313925 return -EAGAIN;
35323926 }
35333927
3928+static int io_sendmsg_copy_hdr(struct io_kiocb *req,
3929+ struct io_async_msghdr *iomsg)
3930+{
3931+ iomsg->iov = iomsg->fast_iov;
3932+ iomsg->msg.msg_name = &iomsg->addr;
3933+ return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
3934+ req->sr_msg.msg_flags, &iomsg->iov);
3935+}
3936+
35343937 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
35353938 {
35363939 struct io_sr_msg *sr = &req->sr_msg;
@@ -3541,7 +3944,7 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 4 unmodified lines
35413944 return -EINVAL;
35423945
35433946 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3544− sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3947+ sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
35453948 sr->len = READ_ONCE(sqe->len);
35463949
35473950 #ifdef CONFIG_COMPAT
@@ -3555,136 +3958,126 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 7 unmodified lines
35553958 if (req->flags & REQ_F_NEED_CLEANUP)
35563959 return 0;
35573960
3558− io->msg.msg.msg_name = &io->msg.addr;
3559− io->msg.iov = io->msg.fast_iov;
3560− ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
3561− &io->msg.iov);
3961+ ret = io_sendmsg_copy_hdr(req, &io->msg);
35623962 if (!ret)
35633963 req->flags |= REQ_F_NEED_CLEANUP;
35643964 return ret;
35653965 }
35663966
3567−static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
3967+static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
3968+ struct io_comp_state *cs)
35683969 {
3569− struct io_async_msghdr *kmsg = NULL;
3970+ struct io_async_msghdr iomsg, *kmsg;
35703971 struct socket *sock;
3972+ unsigned flags;
35713973 int ret;
35723974
35733975 sock = sock_from_file(req->file, &ret);
3574− if (sock) {
3575− struct io_async_ctx io;
3576− unsigned flags;
3577−
3578− if (req->io) {
3579− kmsg = &req->io->msg;
3580− kmsg->msg.msg_name = &req->io->msg.addr;
3581− /* if iov is set, it's allocated already */
3582− if (!kmsg->iov)
3583− kmsg->iov = kmsg->fast_iov;
3584− kmsg->msg.msg_iter.iov = kmsg->iov;
3585− } else {
3586− struct io_sr_msg *sr = &req->sr_msg;
3587−
3588− kmsg = &io.msg;
3589− kmsg->msg.msg_name = &io.msg.addr;
3976+ if (unlikely(!sock))
3977+ return ret;
35903978
3591− io.msg.iov = io.msg.fast_iov;
3592− ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3593− sr->msg_flags, &io.msg.iov);
3594− if (ret)
3595− return ret;
3596− }
3979+ if (req->io) {
3980+ kmsg = &req->io->msg;
3981+ kmsg->msg.msg_name = &req->io->msg.addr;
3982+ /* if iov is set, it's allocated already */
3983+ if (!kmsg->iov)
3984+ kmsg->iov = kmsg->fast_iov;
3985+ kmsg->msg.msg_iter.iov = kmsg->iov;
3986+ } else {
3987+ ret = io_sendmsg_copy_hdr(req, &iomsg);
3988+ if (ret)
3989+ return ret;
3990+ kmsg = &iomsg;
3991+ }
35973992
3598− flags = req->sr_msg.msg_flags;
3599− if (flags & MSG_DONTWAIT)
3600− req->flags |= REQ_F_NOWAIT;
3601− else if (force_nonblock)
3602− flags |= MSG_DONTWAIT;
3993+ flags = req->sr_msg.msg_flags;
3994+ if (flags & MSG_DONTWAIT)
3995+ req->flags |= REQ_F_NOWAIT;
3996+ else if (force_nonblock)
3997+ flags |= MSG_DONTWAIT;
36033998
3604− ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
3605− if (force_nonblock && ret == -EAGAIN)
3606− return io_setup_async_msg(req, kmsg);
3607− if (ret == -ERESTARTSYS)
3608− ret = -EINTR;
3609− }
3999+ ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
4000+ if (force_nonblock && ret == -EAGAIN)
4001+ return io_setup_async_msg(req, kmsg);
4002+ if (ret == -ERESTARTSYS)
4003+ ret = -EINTR;
36104004
3611− if (kmsg && kmsg->iov != kmsg->fast_iov)
4005+ if (kmsg->iov != kmsg->fast_iov)
36124006 kfree(kmsg->iov);
36134007 req->flags &= ~REQ_F_NEED_CLEANUP;
3614− io_cqring_add_event(req, ret);
36154008 if (ret < 0)
36164009 req_set_fail_links(req);
3617− io_put_req(req);
4010+ __io_req_complete(req, ret, 0, cs);
36184011 return 0;
36194012 }
36204013
3621−static int io_send(struct io_kiocb *req, bool force_nonblock)
4014+static int io_send(struct io_kiocb *req, bool force_nonblock,
4015+ struct io_comp_state *cs)
36224016 {
4017+ struct io_sr_msg *sr = &req->sr_msg;
4018+ struct msghdr msg;
4019+ struct iovec iov;
36234020 struct socket *sock;
4021+ unsigned flags;
36244022 int ret;
36254023
36264024 sock = sock_from_file(req->file, &ret);
3627− if (sock) {
3628− struct io_sr_msg *sr = &req->sr_msg;
3629− struct msghdr msg;
3630− struct iovec iov;
3631− unsigned flags;
4025+ if (unlikely(!sock))
4026+ return ret;
36324027
3633− ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3634− &msg.msg_iter);
3635− if (ret)
3636− return ret;
4028+ ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4029+ if (unlikely(ret))
4030+ return ret;;
36374031
3638− msg.msg_name = NULL;
3639− msg.msg_control = NULL;
3640− msg.msg_controllen = 0;
3641− msg.msg_namelen = 0;
4032+ msg.msg_name = NULL;
4033+ msg.msg_control = NULL;
4034+ msg.msg_controllen = 0;
4035+ msg.msg_namelen = 0;
36424036
3643− flags = req->sr_msg.msg_flags;
3644− if (flags & MSG_DONTWAIT)
3645− req->flags |= REQ_F_NOWAIT;
3646− else if (force_nonblock)
3647− flags |= MSG_DONTWAIT;
4037+ flags = req->sr_msg.msg_flags;
4038+ if (flags & MSG_DONTWAIT)
4039+ req->flags |= REQ_F_NOWAIT;
4040+ else if (force_nonblock)
4041+ flags |= MSG_DONTWAIT;
36484042
3649− msg.msg_flags = flags;
3650− ret = sock_sendmsg(sock, &msg);
3651− if (force_nonblock && ret == -EAGAIN)
3652− return -EAGAIN;
3653− if (ret == -ERESTARTSYS)
3654− ret = -EINTR;
3655− }
4043+ msg.msg_flags = flags;
4044+ ret = sock_sendmsg(sock, &msg);
4045+ if (force_nonblock && ret == -EAGAIN)
4046+ return -EAGAIN;
4047+ if (ret == -ERESTARTSYS)
4048+ ret = -EINTR;
36564049
3657− io_cqring_add_event(req, ret);
36584050 if (ret < 0)
36594051 req_set_fail_links(req);
3660− io_put_req(req);
4052+ __io_req_complete(req, ret, 0, cs);
36614053 return 0;
36624054 }
36634055
3664−static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4056+static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4057+ struct io_async_msghdr *iomsg)
36654058 {
36664059 struct io_sr_msg *sr = &req->sr_msg;
36674060 struct iovec __user *uiov;
36684061 size_t iov_len;
36694062 int ret;
36704063
3671− ret = __copy_msghdr_from_user(&io->msg.msg, sr->msg, &io->msg.uaddr,
3672− &uiov, &iov_len);
4064+ ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4065+ &iomsg->uaddr, &uiov, &iov_len);
36734066 if (ret)
36744067 return ret;
36754068
36764069 if (req->flags & REQ_F_BUFFER_SELECT) {
36774070 if (iov_len > 1)
36784071 return -EINVAL;
3679− if (copy_from_user(io->msg.iov, uiov, sizeof(*uiov)))
4072+ if (copy_from_user(iomsg->iov, uiov, sizeof(*uiov)))
36804073 return -EFAULT;
3681− sr->len = io->msg.iov[0].iov_len;
3682− iov_iter_init(&io->msg.msg.msg_iter, READ, io->msg.iov, 1,
4074+ sr->len = iomsg->iov[0].iov_len;
4075+ iov_iter_init(&iomsg->msg.msg_iter, READ, iomsg->iov, 1,
36834076 sr->len);
3684− io->msg.iov = NULL;
4077+ iomsg->iov = NULL;
36854078 } else {
36864079 ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
3687− &io->msg.iov, &io->msg.msg.msg_iter);
4080+ &iomsg->iov, &iomsg->msg.msg_iter);
36884081 if (ret > 0)
36894082 ret = 0;
36904083 }
@@ -3694,7 +4087,7 @@ static int __io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io) · 3 unmodified lines
36944087
36954088 #ifdef CONFIG_COMPAT
36964089 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
3697− struct io_async_ctx *io)
4090+ struct io_async_msghdr *iomsg)
36984091 {
36994092 struct compat_msghdr __user *msg_compat;
37004093 struct io_sr_msg *sr = &req->sr_msg;
@@ -3703,8 +4096,8 @@ static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, · 2 unmodified lines
37034096 compat_size_t len;
37044097 int ret;
37054098
3706− msg_compat = (struct compat_msghdr __user *) sr->msg;
3707− ret = __get_compat_msghdr(&io->msg.msg, msg_compat, &io->msg.uaddr,
4099+ msg_compat = (struct compat_msghdr __user *) sr->umsg;
4100+ ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
37084101 &ptr, &len);
37094102 if (ret)
37104103 return ret;
@@ -3721,12 +4114,12 @@ static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, · 10 unmodified lines
37214114 return -EFAULT;
37224115 if (clen < 0)
37234116 return -EINVAL;
3724− sr->len = io->msg.iov[0].iov_len;
3725− io->msg.iov = NULL;
4117+ sr->len = iomsg->iov[0].iov_len;
4118+ iomsg->iov = NULL;
37264119 } else {
37274120 ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
3728− &io->msg.iov,
3729− &io->msg.msg.msg_iter);
4121+ &iomsg->iov,
4122+ &iomsg->msg.msg_iter);
37304123 if (ret < 0)
37314124 return ret;
37324125 }
@@ -3735,40 +4128,40 @@ static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, · 2 unmodified lines
37354128 }
37364129 #endif
37374130
3738−static int io_recvmsg_copy_hdr(struct io_kiocb *req, struct io_async_ctx *io)
4131+static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4132+ struct io_async_msghdr *iomsg)
37394133 {
3740− io->msg.msg.msg_name = &io->msg.addr;
3741− io->msg.iov = io->msg.fast_iov;
4134+ iomsg->msg.msg_name = &iomsg->addr;
4135+ iomsg->iov = iomsg->fast_iov;
37424136
37434137 #ifdef CONFIG_COMPAT
37444138 if (req->ctx->compat)
3745− return __io_compat_recvmsg_copy_hdr(req, io);
4139+ return __io_compat_recvmsg_copy_hdr(req, iomsg);
37464140 #endif
37474141
3748− return __io_recvmsg_copy_hdr(req, io);
4142+ return __io_recvmsg_copy_hdr(req, iomsg);
37494143 }
37504144
37514145 static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
3752− int *cflags, bool needs_lock)
4146+ bool needs_lock)
37534147 {
37544148 struct io_sr_msg *sr = &req->sr_msg;
37554149 struct io_buffer *kbuf;
37564150
3757− if (!(req->flags & REQ_F_BUFFER_SELECT))
3758− return NULL;
3759−
37604151 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
37614152 if (IS_ERR(kbuf))
37624153 return kbuf;
37634154
37644155 sr->kbuf = kbuf;
37654156 req->flags |= REQ_F_BUFFER_SELECTED;
3766−
3767− *cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
3768− *cflags |= IORING_CQE_F_BUFFER;
37694157 return kbuf;
37704158 }
37714159
4160+static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4161+{
4162+ return io_put_kbuf(req, req->sr_msg.kbuf);
4163+}
4164+
37724165 static int io_recvmsg_prep(struct io_kiocb *req,
37734166 const struct io_uring_sqe *sqe)
37744167 {
@@ -3780,7 +4173,7 @@ static int io_recvmsg_prep(struct io_kiocb *req, · 5 unmodified lines
37804173 return -EINVAL;
37814174
37824175 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3783− sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
4176+ sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
37844177 sr->len = READ_ONCE(sqe->len);
37854178 sr->bgid = READ_ONCE(sqe->buf_group);
37864179
@@ -3795,133 +4188,123 @@ static int io_recvmsg_prep(struct io_kiocb *req, · 8 unmodified lines
37954188 if (req->flags & REQ_F_NEED_CLEANUP)
37964189 return 0;
37974190
3798− ret = io_recvmsg_copy_hdr(req, io);
4191+ ret = io_recvmsg_copy_hdr(req, &io->msg);
37994192 if (!ret)
38004193 req->flags |= REQ_F_NEED_CLEANUP;
38014194 return ret;
38024195 }
38034196
3804−static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4197+static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4198+ struct io_comp_state *cs)
38054199 {
3806− struct io_async_msghdr *kmsg = NULL;
4200+ struct io_async_msghdr iomsg, *kmsg;
38074201 struct socket *sock;
4202+ struct io_buffer *kbuf;
4203+ unsigned flags;
38084204 int ret, cflags = 0;
38094205
38104206 sock = sock_from_file(req->file, &ret);
3811− if (sock) {
3812− struct io_buffer *kbuf;
3813− struct io_async_ctx io;
3814− unsigned flags;
3815−
3816− if (req->io) {
3817− kmsg = &req->io->msg;
3818− kmsg->msg.msg_name = &req->io->msg.addr;
3819− /* if iov is set, it's allocated already */
3820− if (!kmsg->iov)
3821− kmsg->iov = kmsg->fast_iov;
3822− kmsg->msg.msg_iter.iov = kmsg->iov;
3823− } else {
3824− kmsg = &io.msg;
3825− kmsg->msg.msg_name = &io.msg.addr;
4207+ if (unlikely(!sock))
4208+ return ret;
38264209
3827− ret = io_recvmsg_copy_hdr(req, &io);
3828− if (ret)
3829− return ret;
3830− }
4210+ if (req->io) {
4211+ kmsg = &req->io->msg;
4212+ kmsg->msg.msg_name = &req->io->msg.addr;
4213+ /* if iov is set, it's allocated already */
4214+ if (!kmsg->iov)
4215+ kmsg->iov = kmsg->fast_iov;
4216+ kmsg->msg.msg_iter.iov = kmsg->iov;
4217+ } else {
4218+ ret = io_recvmsg_copy_hdr(req, &iomsg);
4219+ if (ret)
4220+ return ret;
4221+ kmsg = &iomsg;
4222+ }
38314223
3832− kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
3833− if (IS_ERR(kbuf)) {
4224+ if (req->flags & REQ_F_BUFFER_SELECT) {
4225+ kbuf = io_recv_buffer_select(req, !force_nonblock);
4226+ if (IS_ERR(kbuf))
38344227 return PTR_ERR(kbuf);
3835− } else if (kbuf) {
3836− kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3837− iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
3838− 1, req->sr_msg.len);
3839− }
4228+ kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
4229+ iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->iov,
4230+ 1, req->sr_msg.len);
4231+ }
38404232
3841− flags = req->sr_msg.msg_flags;
3842− if (flags & MSG_DONTWAIT)
3843− req->flags |= REQ_F_NOWAIT;
3844− else if (force_nonblock)
3845− flags |= MSG_DONTWAIT;
4233+ flags = req->sr_msg.msg_flags;
4234+ if (flags & MSG_DONTWAIT)
4235+ req->flags |= REQ_F_NOWAIT;
4236+ else if (force_nonblock)
4237+ flags |= MSG_DONTWAIT;
38464238
3847− ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3848− kmsg->uaddr, flags);
3849− if (force_nonblock && ret == -EAGAIN) {
3850− ret = io_setup_async_msg(req, kmsg);
3851− if (ret != -EAGAIN)
3852− kfree(kbuf);
3853− return ret;
3854− }
3855− if (ret == -ERESTARTSYS)
3856− ret = -EINTR;
3857− if (kbuf)
3858− kfree(kbuf);
3859− }
4239+ ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4240+ kmsg->uaddr, flags);
4241+ if (force_nonblock && ret == -EAGAIN)
4242+ return io_setup_async_msg(req, kmsg);
4243+ if (ret == -ERESTARTSYS)
4244+ ret = -EINTR;
38604245
3861− if (kmsg && kmsg->iov != kmsg->fast_iov)
4246+ if (req->flags & REQ_F_BUFFER_SELECTED)
4247+ cflags = io_put_recv_kbuf(req);
4248+ if (kmsg->iov != kmsg->fast_iov)
38624249 kfree(kmsg->iov);
38634250 req->flags &= ~REQ_F_NEED_CLEANUP;
3864− __io_cqring_add_event(req, ret, cflags);
38654251 if (ret < 0)
38664252 req_set_fail_links(req);
3867− io_put_req(req);
4253+ __io_req_complete(req, ret, cflags, cs);
38684254 return 0;
38694255 }
38704256
3871−static int io_recv(struct io_kiocb *req, bool force_nonblock)
4257+static int io_recv(struct io_kiocb *req, bool force_nonblock,
4258+ struct io_comp_state *cs)
38724259 {
3873− struct io_buffer *kbuf = NULL;
4260+ struct io_buffer *kbuf;
4261+ struct io_sr_msg *sr = &req->sr_msg;
4262+ struct msghdr msg;
4263+ void __user *buf = sr->buf;
38744264 struct socket *sock;
4265+ struct iovec iov;
4266+ unsigned flags;
38754267 int ret, cflags = 0;
38764268
38774269 sock = sock_from_file(req->file, &ret);
3878− if (sock) {
3879− struct io_sr_msg *sr = &req->sr_msg;
3880− void __user *buf = sr->buf;
3881− struct msghdr msg;
3882− struct iovec iov;
3883− unsigned flags;
4270+ if (unlikely(!sock))
4271+ return ret;
38844272
3885− kbuf = io_recv_buffer_select(req, &cflags, !force_nonblock);
4273+ if (req->flags & REQ_F_BUFFER_SELECT) {
4274+ kbuf = io_recv_buffer_select(req, !force_nonblock);
38864275 if (IS_ERR(kbuf))
38874276 return PTR_ERR(kbuf);
3888− else if (kbuf)
3889− buf = u64_to_user_ptr(kbuf->addr);
4277+ buf = u64_to_user_ptr(kbuf->addr);
4278+ }
38904279
3891− ret = import_single_range(READ, buf, sr->len, &iov,
3892− &msg.msg_iter);
3893− if (ret) {
3894− kfree(kbuf);
3895− return ret;
3896− }
4280+ ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
4281+ if (unlikely(ret))
4282+ goto out_free;
38974283
3898− req->flags |= REQ_F_NEED_CLEANUP;
3899− msg.msg_name = NULL;
3900− msg.msg_control = NULL;
3901− msg.msg_controllen = 0;
3902− msg.msg_namelen = 0;
3903− msg.msg_iocb = NULL;
3904− msg.msg_flags = 0;
3905−
3906− flags = req->sr_msg.msg_flags;
3907− if (flags & MSG_DONTWAIT)
3908− req->flags |= REQ_F_NOWAIT;
3909− else if (force_nonblock)
3910− flags |= MSG_DONTWAIT;
3911−
3912− ret = sock_recvmsg(sock, &msg, flags);
3913− if (force_nonblock && ret == -EAGAIN)
3914− return -EAGAIN;
3915− if (ret == -ERESTARTSYS)
3916− ret = -EINTR;
3917− }
4284+ msg.msg_name = NULL;
4285+ msg.msg_control = NULL;
4286+ msg.msg_controllen = 0;
4287+ msg.msg_namelen = 0;
4288+ msg.msg_iocb = NULL;
4289+ msg.msg_flags = 0;
39184290
3919− kfree(kbuf);
3920− req->flags &= ~REQ_F_NEED_CLEANUP;
3921− __io_cqring_add_event(req, ret, cflags);
4291+ flags = req->sr_msg.msg_flags;
4292+ if (flags & MSG_DONTWAIT)
4293+ req->flags |= REQ_F_NOWAIT;
4294+ else if (force_nonblock)
4295+ flags |= MSG_DONTWAIT;
4296+
4297+ ret = sock_recvmsg(sock, &msg, flags);
4298+ if (force_nonblock && ret == -EAGAIN)
4299+ return -EAGAIN;
4300+ if (ret == -ERESTARTSYS)
4301+ ret = -EINTR;
4302+out_free:
4303+ if (req->flags & REQ_F_BUFFER_SELECTED)
4304+ cflags = io_put_recv_kbuf(req);
39224305 if (ret < 0)
39234306 req_set_fail_links(req);
3924− io_put_req(req);
4307+ __io_req_complete(req, ret, cflags, cs);
39254308 return 0;
39264309 }
39274310
@@ -3941,7 +4324,8 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 13 unmodified lines
39414324 return 0;
39424325 }
39434326
3944−static int io_accept(struct io_kiocb *req, bool force_nonblock)
4327+static int io_accept(struct io_kiocb *req, bool force_nonblock,
4328+ struct io_comp_state *cs)
39454329 {
39464330 struct io_accept *accept = &req->accept;
39474331 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
@@ -3960,8 +4344,7 @@ static int io_accept(struct io_kiocb *req, bool force_nonblock) · 12 unmodified lines
39604344 ret = -EINTR;
39614345 req_set_fail_links(req);
39624346 }
3963− io_cqring_add_event(req, ret);
3964− io_put_req(req);
4347+ __io_req_complete(req, ret, 0, cs);
39654348 return 0;
39664349 }
39674350
@@ -3985,7 +4368,8 @@ static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 17 unmodified lines
39854368 &io->connect.address);
39864369 }
39874370
3988−static int io_connect(struct io_kiocb *req, bool force_nonblock)
4371+static int io_connect(struct io_kiocb *req, bool force_nonblock,
4372+ struct io_comp_state *cs)
39894373 {
39904374 struct io_async_ctx __io, *io;
39914375 unsigned file_flags;
@@ -4021,8 +4405,7 @@ static int io_connect(struct io_kiocb *req, bool force_nonblock) · 29 unmodified lines
40214405 out:
40224406 if (ret < 0)
40234407 req_set_fail_links(req);
4024− io_cqring_add_event(req, ret);
4025− io_put_req(req);
4408+ __io_req_complete(req, ret, 0, cs);
40264409 return 0;
40274410 }
40284411 #else /* !CONFIG_NET */
@@ -4031,12 +4414,14 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 2 unmodified lines
40314414 return -EOPNOTSUPP;
40324415 }
40334416
4034−static int io_sendmsg(struct io_kiocb *req, bool force_nonblock)
4417+static int io_sendmsg(struct io_kiocb *req, bool force_nonblock,
4418+ struct io_comp_state *cs)
40354419 {
40364420 return -EOPNOTSUPP;
40374421 }
40384422
4039−static int io_send(struct io_kiocb *req, bool force_nonblock)
4423+static int io_send(struct io_kiocb *req, bool force_nonblock,
4424+ struct io_comp_state *cs)
40404425 {
40414426 return -EOPNOTSUPP;
40424427 }
@@ -4047,12 +4432,14 @@ static int io_recvmsg_prep(struct io_kiocb *req, · 4 unmodified lines
40474432 return -EOPNOTSUPP;
40484433 }
40494434
4050−static int io_recvmsg(struct io_kiocb *req, bool force_nonblock)
4435+static int io_recvmsg(struct io_kiocb *req, bool force_nonblock,
4436+ struct io_comp_state *cs)
40514437 {
40524438 return -EOPNOTSUPP;
40534439 }
40544440
4055−static int io_recv(struct io_kiocb *req, bool force_nonblock)
4441+static int io_recv(struct io_kiocb *req, bool force_nonblock,
4442+ struct io_comp_state *cs)
40564443 {
40574444 return -EOPNOTSUPP;
40584445 }
@@ -4062,55 +4449,33 @@ static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 3 unmodified lines
40624449 return -EOPNOTSUPP;
40634450 }
40644451
4065−static int io_accept(struct io_kiocb *req, bool force_nonblock)
4066−{
4067− return -EOPNOTSUPP;
4068−}
4069−
4070−static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4071−{
4072− return -EOPNOTSUPP;
4073−}
4074−
4075−static int io_connect(struct io_kiocb *req, bool force_nonblock)
4076−{
4077− return -EOPNOTSUPP;
4078−}
4079−#endif /* CONFIG_NET */
4080−
4081−struct io_poll_table {
4082− struct poll_table_struct pt;
4083− struct io_kiocb *req;
4084− int error;
4085−};
4086−
4087−static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb)
4088−{
4089− struct task_struct *tsk = req->task;
4090− struct io_ring_ctx *ctx = req->ctx;
4091− int ret, notify = TWA_RESUME;
4092−
4093− /*
4094− * SQPOLL kernel thread doesn't need notification, just a wakeup.
4095− * If we're not using an eventfd, then TWA_RESUME is always fine,
4096− * as we won't have dependencies between request completions for
4097− * other kernel wait conditions.
4098− */
4099− if (ctx->flags & IORING_SETUP_SQPOLL)
4100− notify = 0;
4101− else if (ctx->cq_ev_fd)
4102− notify = TWA_SIGNAL;
4452+static int io_accept(struct io_kiocb *req, bool force_nonblock,
4453+ struct io_comp_state *cs)
4454+{
4455+ return -EOPNOTSUPP;
4456+}
41034457
4104− ret = task_work_add(tsk, cb, notify);
4105− if (!ret)
4106− wake_up_process(tsk);
4107− return ret;
4458+static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4459+{
4460+ return -EOPNOTSUPP;
4461+}
4462+
4463+static int io_connect(struct io_kiocb *req, bool force_nonblock,
4464+ struct io_comp_state *cs)
4465+{
4466+ return -EOPNOTSUPP;
41084467 }
4468+#endif /* CONFIG_NET */
4469+
4470+struct io_poll_table {
4471+ struct poll_table_struct pt;
4472+ struct io_kiocb *req;
4473+ int error;
4474+};
41094475
41104476 static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
41114477 __poll_t mask, task_work_func_t func)
41124478 {
4113− struct task_struct *tsk;
41144479 int ret;
41154480
41164481 /* for instances that support it check for an event match first: */
@@ -4121,7 +4486,6 @@ static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, · 4 unmodified lines
41214486
41224487 list_del_init(&poll->wait.entry);
41234488
4124− tsk = req->task;
41254489 req->result = mask;
41264490 init_task_work(&req->task_work, func);
41274491 /*
@@ -4132,6 +4496,8 @@ static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, · 4 unmodified lines
41324496 */
41334497 ret = io_req_task_work_add(req, &req->task_work);
41344498 if (unlikely(ret)) {
4499+ struct task_struct *tsk;
4500+
41354501 WRITE_ONCE(poll->canceled, true);
41364502 tsk = io_wq_get_task(req->ctx->io_wq);
41374503 task_work_add(tsk, &req->task_work, 0);
@@ -4199,9 +4565,10 @@ static void io_poll_task_handler(struct io_kiocb *req, struct io_kiocb **nxt) · 61 unmodified lines
41994565
42004566 hash_del(&req->hash_node);
42014567 io_poll_complete(req, req->result, 0);
4568+ req->flags |= REQ_F_COMP_LOCKED;
4569+ *nxt = io_put_req_find_next(req);
42024570 spin_unlock_irq(&ctx->completion_lock);
42034571
4204− io_put_req_find_next(req, nxt);
42054572 io_cqring_ev_posted(ctx);
42064573 }
42074574
@@ -4211,13 +4578,8 @@ static void io_poll_task_func(struct callback_head *cb) · 3 unmodified lines
42114578 struct io_kiocb *nxt = NULL;
42124579
42134580 io_poll_task_handler(req, &nxt);
4214− if (nxt) {
4215− struct io_ring_ctx *ctx = nxt->ctx;
4216−
4217− mutex_lock(&ctx->uring_lock);
4218− __io_queue_sqe(nxt, NULL);
4219− mutex_unlock(&ctx->uring_lock);
4220− }
4581+ if (nxt)
4582+ __io_req_task_submit(nxt);
42214583 }
42224584
42234585 static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
@@ -4287,7 +4649,11 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, · 63 unmodified lines
42874649
42884650 pt->error = 0;
42894651 poll->head = head;
4290− add_wait_queue(head, &poll->wait);
4652+
4653+ if (poll->events & EPOLLEXCLUSIVE)
4654+ add_wait_queue_exclusive(head, &poll->wait);
4655+ else
4656+ add_wait_queue(head, &poll->wait);
42914657 }
42924658
42934659 static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
@@ -4299,34 +4665,11 @@ static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, · 5 unmodified lines
42994665 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
43004666 }
43014667
4302−static void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
4303−{
4304− struct mm_struct *mm = current->mm;
4305−
4306− if (mm) {
4307− kthread_unuse_mm(mm);
4308− mmput(mm);
4309− }
4310−}
4311−
4312−static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx,
4313− struct io_kiocb *req)
4314−{
4315− if (io_op_defs[req->opcode].needs_mm && !current->mm) {
4316− if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
4317− return -EFAULT;
4318− kthread_use_mm(ctx->sqo_mm);
4319− }
4320−
4321− return 0;
4322−}
4323−
43244668 static void io_async_task_func(struct callback_head *cb)
43254669 {
43264670 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
43274671 struct async_poll *apoll = req->apoll;
43284672 struct io_ring_ctx *ctx = req->ctx;
4329− bool canceled = false;
43304673
43314674 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
43324675
@@ -4336,40 +4679,19 @@ static void io_async_task_func(struct callback_head *cb) · 3 unmodified lines
43364679 }
43374680
43384681 /* If req is still hashed, it cannot have been canceled. Don't check. */
4339− if (hash_hashed(&req->hash_node)) {
4682+ if (hash_hashed(&req->hash_node))
43404683 hash_del(&req->hash_node);
4341− } else {
4342− canceled = READ_ONCE(apoll->poll.canceled);
4343− if (canceled) {
4344− io_cqring_fill_event(req, -ECANCELED);
4345− io_commit_cqring(ctx);
4346− }
4347− }
43484684
43494685 io_poll_remove_double(req, apoll->double_poll);
43504686 spin_unlock_irq(&ctx->completion_lock);
43514687
4352− /* restore ->work in case we need to retry again */
4353− if (req->flags & REQ_F_WORK_INITIALIZED)
4354− memcpy(&req->work, &apoll->work, sizeof(req->work));
4688+ if (!READ_ONCE(apoll->poll.canceled))
4689+ __io_req_task_submit(req);
4690+ else
4691+ __io_req_task_cancel(req, -ECANCELED);
4692+
43554693 kfree(apoll->double_poll);
43564694 kfree(apoll);
4357−
4358− if (!canceled) {
4359− __set_current_state(TASK_RUNNING);
4360− if (io_sq_thread_acquire_mm(ctx, req)) {
4361− io_cqring_add_event(req, -EFAULT);
4362− goto end_req;
4363− }
4364− mutex_lock(&ctx->uring_lock);
4365− __io_queue_sqe(req, NULL);
4366− mutex_unlock(&ctx->uring_lock);
4367− } else {
4368− io_cqring_ev_posted(ctx);
4369−end_req:
4370− req_set_fail_links(req);
4371− io_double_put_req(req);
4372− }
43734695 }
43744696
43754697 static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
@@ -4402,8 +4724,8 @@ static __poll_t __io_arm_poll_handler(struct io_kiocb *req, · 26 unmodified lines
44024724 struct io_ring_ctx *ctx = req->ctx;
44034725 bool cancel = false;
44044726
4405− poll->file = req->file;
44064727 io_init_poll_iocb(poll, mask, wake_func);
4728+ poll->file = req->file;
44074729 poll->wait.private = req;
44084730
44094731 ipt->pt._key = mask;
@@ -4443,7 +4765,7 @@ static bool io_arm_poll_handler(struct io_kiocb *req) · 33 unmodified lines
44434765
44444766 if (!req->file || !file_can_poll(req->file))
44454767 return false;
4446− if (req->flags & (REQ_F_MUST_PUNT | REQ_F_POLLED))
4768+ if (req->flags & REQ_F_POLLED)
44474769 return false;
44484770 if (!def->pollin && !def->pollout)
44494771 return false;
@@ -4454,9 +4776,6 @@ static bool io_arm_poll_handler(struct io_kiocb *req) · 4 unmodified lines
44544776 apoll->double_poll = NULL;
44554777
44564778 req->flags |= REQ_F_POLLED;
4457− if (req->flags & REQ_F_WORK_INITIALIZED)
4458− memcpy(&apoll->work, &req->work, sizeof(req->work));
4459−
44604779 io_get_req_task(req);
44614780 req->apoll = apoll;
44624781 INIT_HLIST_NODE(&req->hash_node);
@@ -4475,8 +4794,6 @@ static bool io_arm_poll_handler(struct io_kiocb *req) · 12 unmodified lines
44754794 if (ret) {
44764795 io_poll_remove_double(req, apoll->double_poll);
44774796 spin_unlock_irq(&ctx->completion_lock);
4478− if (req->flags & REQ_F_WORK_INITIALIZED)
4479− memcpy(&req->work, &apoll->work, sizeof(req->work));
44804797 kfree(apoll->double_poll);
44814798 kfree(apoll);
44824799 return false;
@@ -4519,14 +4836,6 @@ static bool io_poll_remove_one(struct io_kiocb *req) · 36 unmodified lines
45194836 do_complete = __io_poll_remove_one(req, &apoll->poll);
45204837 if (do_complete) {
45214838 io_put_req(req);
4522− /*
4523− * restore ->work because we will call
4524− * io_req_work_drop_env below when dropping the
4525− * final reference.
4526− */
4527− if (req->flags & REQ_F_WORK_INITIALIZED)
4528− memcpy(&req->work, &apoll->work,
4529− sizeof(req->work));
45304839 kfree(apoll->double_poll);
45314840 kfree(apoll);
45324841 }
@@ -4607,10 +4916,9 @@ static int io_poll_remove(struct io_kiocb *req) · 74 unmodified lines
46074916 ret = io_poll_cancel(ctx, addr);
46084917 spin_unlock_irq(&ctx->completion_lock);
46094918
4610− io_cqring_add_event(req, ret);
46114919 if (ret < 0)
46124920 req_set_fail_links(req);
4613− io_put_req(req);
4921+ io_req_complete(req, ret);
46144922 return 0;
46154923 }
46164924
@@ -4634,7 +4942,7 @@ static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, · 17 unmodified lines
46344942 static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
46354943 {
46364944 struct io_poll_iocb *poll = &req->poll;
4637− u16 events;
4945+ u32 events;
46384946
46394947 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
46404948 return -EINVAL;
@@ -4643,8 +4951,12 @@ static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe · 2 unmodified lines
46434951 if (!poll->file)
46444952 return -EBADF;
46454953
4646− events = READ_ONCE(sqe->poll_events);
4647− poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
4954+ events = READ_ONCE(sqe->poll32_events);
4955+#ifdef __BIG_ENDIAN
4956+ events = swahw32(events);
4957+#endif
4958+ poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
4959+ (events & EPOLLEXCLUSIVE);
46484960
46494961 io_get_req_task(req);
46504962 return 0;
@@ -4657,12 +4969,7 @@ static int io_poll_add(struct io_kiocb *req) · 6 unmodified lines
46574969 struct io_poll_table ipt;
46584970 __poll_t mask;
46594971
4660− /* ->work is in union with hash_node and others */
4661− io_req_work_drop_env(req);
4662− req->flags &= ~REQ_F_WORK_INITIALIZED;
4663−
46644972 INIT_HLIST_NODE(&req->hash_node);
4665− INIT_LIST_HEAD(&req->list);
46664973 ipt.pt._qproc = io_poll_queue_proc;
46674974
46684975 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
@@ -4689,15 +4996,16 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) · 20 unmodified lines
46894996 struct io_ring_ctx *ctx = req->ctx;
46904997 unsigned long flags;
46914998
4692− atomic_inc(&ctx->cq_timeouts);
4693−
46944999 spin_lock_irqsave(&ctx->completion_lock, flags);
5000+ atomic_set(&req->ctx->cq_timeouts,
5001+ atomic_read(&req->ctx->cq_timeouts) + 1);
5002+
46955003 /*
46965004 * We could be racing with timeout deletion. If the list is empty,
46975005 * then timeout lookup already found it and will be handling it.
46985006 */
4699− if (!list_empty(&req->list))
4700− list_del_init(&req->list);
5007+ if (!list_empty(&req->timeout.list))
5008+ list_del_init(&req->timeout.list);
47015009
47025010 io_cqring_fill_event(req, -ETIME);
47035011 io_commit_cqring(ctx);
@@ -4714,9 +5022,9 @@ static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) · 10 unmodified lines
47145022 struct io_kiocb *req;
47155023 int ret = -ENOENT;
47165024
4717− list_for_each_entry(req, &ctx->timeout_list, list) {
5025+ list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
47185026 if (user_data == req->user_data) {
4719− list_del_init(&req->list);
5027+ list_del_init(&req->timeout.list);
47205028 ret = 0;
47215029 break;
47225030 }
@@ -4798,7 +5106,6 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 75 unmodified lines
47985106
47995107 data = &req->io->timeout;
48005108 data->req = req;
4801− req->flags |= REQ_F_TIMEOUT;
48025109
48035110 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
48045111 return -EFAULT;
@@ -4826,8 +5133,7 @@ static int io_timeout(struct io_kiocb *req) · 21 unmodified lines
48265133 * timeout event to be satisfied. If it isn't set, then this is
48275134 * a pure timeout request, sequence isn't used.
48285135 */
4829− if (!off) {
4830− req->flags |= REQ_F_TIMEOUT_NOSEQ;
5136+ if (io_is_timeout_noseq(req)) {
48315137 entry = ctx->timeout_list.prev;
48325138 goto add;
48335139 }
@@ -4840,16 +5146,17 @@ static int io_timeout(struct io_kiocb *req) · 6 unmodified lines
48405146 * the one we need first.
48415147 */
48425148 list_for_each_prev(entry, &ctx->timeout_list) {
4843− struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
5149+ struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5150+ timeout.list);
48445151
4845− if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
5152+ if (io_is_timeout_noseq(nxt))
48465153 continue;
48475154 /* nxt.seq is behind @tail, otherwise would've been completed */
48485155 if (off >= nxt->timeout.target_seq - tail)
48495156 break;
48505157 }
48515158 add:
4852− list_add(&req->list, entry);
5159+ list_add(&req->timeout.list, entry);
48535160 data->timer.function = io_timeout_fn;
48545161 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
48555162 spin_unlock_irq(&ctx->completion_lock);
@@ -4953,7 +5260,8 @@ static int io_files_update_prep(struct io_kiocb *req, · 97 unmodified lines
49535260 return 0;
49545261 }
49555262
4956−static int io_files_update(struct io_kiocb *req, bool force_nonblock)
5263+static int io_files_update(struct io_kiocb *req, bool force_nonblock,
5264+ struct io_comp_state *cs)
49575265 {
49585266 struct io_ring_ctx *ctx = req->ctx;
49595267 struct io_uring_files_update up;
@@ -4971,8 +5279,7 @@ static int io_files_update(struct io_kiocb *req, bool force_nonblock) · 11 unmodified lines
49715279
49725280 if (ret < 0)
49735281 req_set_fail_links(req);
4974− io_cqring_add_event(req, ret);
4975− io_put_req(req);
5282+ __io_req_complete(req, ret, 0, cs);
49765283 return 0;
49775284 }
49785285
@@ -4984,15 +5291,11 @@ static int io_req_defer_prep(struct io_kiocb *req, · 5 unmodified lines
49845291 if (!sqe)
49855292 return 0;
49865293
4987− io_req_init_async(req);
4988−
4989− if (io_op_defs[req->opcode].file_table) {
4990− ret = io_grab_files(req);
4991− if (unlikely(ret))
4992− return ret;
4993− }
4994−
4995− io_req_work_grab_env(req, &io_op_defs[req->opcode]);
5294+ if (io_alloc_async_ctx(req))
5295+ return -EAGAIN;
5296+ ret = io_prep_work_files(req);
5297+ if (unlikely(ret))
5298+ return ret;
49965299
49975300 switch (req->opcode) {
49985301 case IORING_OP_NOP:
@@ -5094,86 +5397,117 @@ static int io_req_defer_prep(struct io_kiocb *req, · 95 unmodified lines
50945397 return ret;
50955398 }
50965399
5400+static u32 io_get_sequence(struct io_kiocb *req)
5401+{
5402+ struct io_kiocb *pos;
5403+ struct io_ring_ctx *ctx = req->ctx;
5404+ u32 total_submitted, nr_reqs = 1;
5405+
5406+ if (req->flags & REQ_F_LINK_HEAD)
5407+ list_for_each_entry(pos, &req->link_list, link_list)
5408+ nr_reqs++;
5409+
5410+ total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5411+ return total_submitted - nr_reqs;
5412+}
5413+
50975414 static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
50985415 {
50995416 struct io_ring_ctx *ctx = req->ctx;
5417+ struct io_defer_entry *de;
51005418 int ret;
5419+ u32 seq;
51015420
51025421 /* Still need defer if there is pending req in defer list. */
5103− if (!req_need_defer(req) && list_empty_careful(&ctx->defer_list))
5422+ if (likely(list_empty_careful(&ctx->defer_list) &&
5423+ !(req->flags & REQ_F_IO_DRAIN)))
5424+ return 0;
5425+
5426+ seq = io_get_sequence(req);
5427+ /* Still a chance to pass the sequence check */
5428+ if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
51045429 return 0;
51055430
51065431 if (!req->io) {
5107− if (io_alloc_async_ctx(req))
5108− return -EAGAIN;
51095432 ret = io_req_defer_prep(req, sqe);
5110− if (ret < 0)
5433+ if (ret)
51115434 return ret;
51125435 }
5436+ io_prep_async_link(req);
5437+ de = kmalloc(sizeof(*de), GFP_KERNEL);
5438+ if (!de)
5439+ return -ENOMEM;
51135440
51145441 spin_lock_irq(&ctx->completion_lock);
5115− if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
5442+ if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
51165443 spin_unlock_irq(&ctx->completion_lock);
5117− return 0;
5444+ kfree(de);
5445+ io_queue_async_work(req);
5446+ return -EIOCBQUEUED;
51185447 }
51195448
51205449 trace_io_uring_defer(ctx, req, req->user_data);
5121− list_add_tail(&req->list, &ctx->defer_list);
5450+ de->req = req;
5451+ de->seq = seq;
5452+ list_add_tail(&de->list, &ctx->defer_list);
51225453 spin_unlock_irq(&ctx->completion_lock);
51235454 return -EIOCBQUEUED;
51245455 }
51255456
5126−static void io_cleanup_req(struct io_kiocb *req)
5457+static void __io_clean_op(struct io_kiocb *req)
51275458 {
51285459 struct io_async_ctx *io = req->io;
51295460
5130− switch (req->opcode) {
5131− case IORING_OP_READV:
5132− case IORING_OP_READ_FIXED:
5133− case IORING_OP_READ:
5134− if (req->flags & REQ_F_BUFFER_SELECTED)
5461+ if (req->flags & REQ_F_BUFFER_SELECTED) {
5462+ switch (req->opcode) {
5463+ case IORING_OP_READV:
5464+ case IORING_OP_READ_FIXED:
5465+ case IORING_OP_READ:
51355466 kfree((void *)(unsigned long)req->rw.addr);
5136− /* fallthrough */
5137− case IORING_OP_WRITEV:
5138− case IORING_OP_WRITE_FIXED:
5139− case IORING_OP_WRITE:
5140− if (io->rw.iov != io->rw.fast_iov)
5141− kfree(io->rw.iov);
5142− break;
5143− case IORING_OP_RECVMSG:
5144− if (req->flags & REQ_F_BUFFER_SELECTED)
5145− kfree(req->sr_msg.kbuf);
5146− /* fallthrough */
5147− case IORING_OP_SENDMSG:
5148− if (io->msg.iov != io->msg.fast_iov)
5149− kfree(io->msg.iov);
5150− break;
5151− case IORING_OP_RECV:
5152− if (req->flags & REQ_F_BUFFER_SELECTED)
5467+ break;
5468+ case IORING_OP_RECVMSG:
5469+ case IORING_OP_RECV:
51535470 kfree(req->sr_msg.kbuf);
5154− break;
5155− case IORING_OP_OPENAT:
5156− case IORING_OP_OPENAT2:
5157− break;
5158− case IORING_OP_SPLICE:
5159− case IORING_OP_TEE:
5160− io_put_file(req, req->splice.file_in,
5161− (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5162− break;
5471+ break;
5472+ }
5473+ req->flags &= ~REQ_F_BUFFER_SELECTED;
5474+ }
5475+
5476+ if (req->flags & REQ_F_NEED_CLEANUP) {
5477+ switch (req->opcode) {
5478+ case IORING_OP_READV:
5479+ case IORING_OP_READ_FIXED:
5480+ case IORING_OP_READ:
5481+ case IORING_OP_WRITEV:
5482+ case IORING_OP_WRITE_FIXED:
5483+ case IORING_OP_WRITE:
5484+ if (io->rw.iov != io->rw.fast_iov)
5485+ kfree(io->rw.iov);
5486+ break;
5487+ case IORING_OP_RECVMSG:
5488+ case IORING_OP_SENDMSG:
5489+ if (io->msg.iov != io->msg.fast_iov)
5490+ kfree(io->msg.iov);
5491+ break;
5492+ case IORING_OP_SPLICE:
5493+ case IORING_OP_TEE:
5494+ io_put_file(req, req->splice.file_in,
5495+ (req->splice.flags & SPLICE_F_FD_IN_FIXED));
5496+ break;
5497+ }
5498+ req->flags &= ~REQ_F_NEED_CLEANUP;
51635499 }
5164−
5165− req->flags &= ~REQ_F_NEED_CLEANUP;
51665500 }
51675501
51685502 static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5169− bool force_nonblock)
5503+ bool force_nonblock, struct io_comp_state *cs)
51705504 {
51715505 struct io_ring_ctx *ctx = req->ctx;
51725506 int ret;
51735507
51745508 switch (req->opcode) {
51755509 case IORING_OP_NOP:
5176− ret = io_nop(req);
5510+ ret = io_nop(req, cs);
51775511 break;
51785512 case IORING_OP_READV:
51795513 case IORING_OP_READ_FIXED:
@@ -5183,7 +5517,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 3 unmodified lines
51835517 if (ret < 0)
51845518 break;
51855519 }
5186− ret = io_read(req, force_nonblock);
5520+ ret = io_read(req, force_nonblock, cs);
51875521 break;
51885522 case IORING_OP_WRITEV:
51895523 case IORING_OP_WRITE_FIXED:
@@ -5193,7 +5527,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 3 unmodified lines
51935527 if (ret < 0)
51945528 break;
51955529 }
5196− ret = io_write(req, force_nonblock);
5530+ ret = io_write(req, force_nonblock, cs);
51975531 break;
51985532 case IORING_OP_FSYNC:
51995533 if (sqe) {
@@ -5235,9 +5569,9 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 35 unmodified lines
52355569 break;
52365570 }
52375571 if (req->opcode == IORING_OP_SENDMSG)
5238− ret = io_sendmsg(req, force_nonblock);
5572+ ret = io_sendmsg(req, force_nonblock, cs);
52395573 else
5240− ret = io_send(req, force_nonblock);
5574+ ret = io_send(req, force_nonblock, cs);
52415575 break;
52425576 case IORING_OP_RECVMSG:
52435577 case IORING_OP_RECV:
@@ -5247,9 +5581,9 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 3 unmodified lines
52475581 break;
52485582 }
52495583 if (req->opcode == IORING_OP_RECVMSG)
5250− ret = io_recvmsg(req, force_nonblock);
5584+ ret = io_recvmsg(req, force_nonblock, cs);
52515585 else
5252− ret = io_recv(req, force_nonblock);
5586+ ret = io_recv(req, force_nonblock, cs);
52535587 break;
52545588 case IORING_OP_TIMEOUT:
52555589 if (sqe) {
@@ -5273,7 +5607,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 17 unmodified lines
52735607 if (ret)
52745608 break;
52755609 }
5276− ret = io_accept(req, force_nonblock);
5610+ ret = io_accept(req, force_nonblock, cs);
52775611 break;
52785612 case IORING_OP_CONNECT:
52795613 if (sqe) {
@@ -5281,7 +5615,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 1 unmodified line
52815615 if (ret)
52825616 break;
52835617 }
5284− ret = io_connect(req, force_nonblock);
5618+ ret = io_connect(req, force_nonblock, cs);
52855619 break;
52865620 case IORING_OP_ASYNC_CANCEL:
52875621 if (sqe) {
@@ -5313,7 +5647,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 25 unmodified lines
53135647 if (ret)
53145648 break;
53155649 }
5316− ret = io_close(req, force_nonblock);
5650+ ret = io_close(req, force_nonblock, cs);
53175651 break;
53185652 case IORING_OP_FILES_UPDATE:
53195653 if (sqe) {
@@ -5321,7 +5655,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 1 unmodified line
53215655 if (ret)
53225656 break;
53235657 }
5324− ret = io_files_update(req, force_nonblock);
5658+ ret = io_files_update(req, force_nonblock, cs);
53255659 break;
53265660 case IORING_OP_STATX:
53275661 if (sqe) {
@@ -5361,7 +5695,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 33 unmodified lines
53615695 if (ret)
53625696 break;
53635697 }
5364− ret = io_epoll_ctl(req, force_nonblock);
5698+ ret = io_epoll_ctl(req, force_nonblock, cs);
53655699 break;
53665700 case IORING_OP_SPLICE:
53675701 if (sqe) {
@@ -5377,7 +5711,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 9 unmodified lines
53775711 if (ret)
53785712 break;
53795713 }
5380− ret = io_provide_buffers(req, force_nonblock);
5714+ ret = io_provide_buffers(req, force_nonblock, cs);
53815715 break;
53825716 case IORING_OP_REMOVE_BUFFERS:
53835717 if (sqe) {
@@ -5385,7 +5719,7 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 1 unmodified line
53855719 if (ret)
53865720 break;
53875721 }
5388− ret = io_remove_buffers(req, force_nonblock);
5722+ ret = io_remove_buffers(req, force_nonblock, cs);
53895723 break;
53905724 case IORING_OP_TEE:
53915725 if (sqe) {
@@ -5420,25 +5754,15 @@ static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 28 unmodified lines
54205754 return 0;
54215755 }
54225756
5423−static void io_arm_async_linked_timeout(struct io_kiocb *req)
5424−{
5425− struct io_kiocb *link;
5426−
5427− /* link head's timeout is queued in io_queue_async_work() */
5428− if (!(req->flags & REQ_F_QUEUE_TIMEOUT))
5429− return;
5430−
5431− link = list_first_entry(&req->link_list, struct io_kiocb, link_list);
5432− io_queue_linked_timeout(link);
5433−}
5434−
5435−static void io_wq_submit_work(struct io_wq_work **workptr)
5757+static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work)
54365758 {
5437− struct io_wq_work *work = *workptr;
54385759 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5760+ struct io_kiocb *timeout;
54395761 int ret = 0;
54405762
5441− io_arm_async_linked_timeout(req);
5763+ timeout = io_prep_linked_timeout(req);
5764+ if (timeout)
5765+ io_queue_linked_timeout(timeout);
54425766
54435767 /* if NO_CANCEL is set, we must still run the work */
54445768 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
@@ -5448,7 +5772,7 @@ static void io_wq_submit_work(struct io_wq_work **workptr) · 3 unmodified lines
54485772
54495773 if (!ret) {
54505774 do {
5451− ret = io_issue_sqe(req, NULL, false);
5775+ ret = io_issue_sqe(req, NULL, false, NULL);
54525776 /*
54535777 * We can get EAGAIN for polled IO even though we're
54545778 * forcing a sync submission from here, since we can't
@@ -5462,11 +5786,10 @@ static void io_wq_submit_work(struct io_wq_work **workptr) · 7 unmodified lines
54625786
54635787 if (ret) {
54645788 req_set_fail_links(req);
5465− io_cqring_add_event(req, ret);
5466− io_put_req(req);
5789+ io_req_complete(req, ret);
54675790 }
54685791
5469− io_steal_work(req, workptr);
5792+ return io_steal_work(req);
54705793 }
54715794
54725795 static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
@@ -5523,6 +5846,8 @@ static int io_grab_files(struct io_kiocb *req) · 50 unmodified lines
55235846 int ret = -EBADF;
55245847 struct io_ring_ctx *ctx = req->ctx;
55255848
5849+ io_req_init_async(req);
5850+
55265851 if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
55275852 return 0;
55285853 if (!ctx->ring_file)
@@ -5548,6 +5873,13 @@ static int io_grab_files(struct io_kiocb *req) · 19 unmodified lines
55485873 return ret;
55495874 }
55505875
5876+static inline int io_prep_work_files(struct io_kiocb *req)
5877+{
5878+ if (!io_op_defs[req->opcode].file_table)
5879+ return 0;
5880+ return io_grab_files(req);
5881+}
5882+
55515883 static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
55525884 {
55535885 struct io_timeout_data *data = container_of(timer,
@@ -5580,8 +5912,7 @@ static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) · 26 unmodified lines
55805912 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
55815913 io_put_req(prev);
55825914 } else {
5583− io_cqring_add_event(req, -ETIME);
5584− io_put_req(req);
5915+ io_req_complete(req, -ETIME);
55855916 }
55865917 return HRTIMER_NORESTART;
55875918 }
@@ -5614,8 +5945,7 @@ static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) · 26 unmodified lines
56145945
56155946 if (!(req->flags & REQ_F_LINK_HEAD))
56165947 return NULL;
5617− /* for polled retry, if flag is set, we already went through here */
5618− if (req->flags & REQ_F_POLLED)
5948+ if (req->flags & REQ_F_LINK_TIMEOUT)
56195949 return NULL;
56205950
56215951 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
@@ -5627,7 +5957,8 @@ static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) · 5 unmodified lines
56275957 return nxt;
56285958 }
56295959
5630−static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
5960+static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5961+ struct io_comp_state *cs)
56315962 {
56325963 struct io_kiocb *linked_timeout;
56335964 struct io_kiocb *nxt;
@@ -5647,54 +5978,45 @@ again: · 13 unmodified lines
56475978 old_creds = override_creds(req->work.creds);
56485979 }
56495980
5650− ret = io_issue_sqe(req, sqe, true);
5981+ ret = io_issue_sqe(req, sqe, true, cs);
56515982
56525983 /*
56535984 * We async punt it if the file wasn't marked NOWAIT, or if the file
56545985 * doesn't support non-blocking read/write attempts
56555986 */
5656− if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
5657− (req->flags & REQ_F_MUST_PUNT))) {
5658− if (io_arm_poll_handler(req)) {
5659− if (linked_timeout)
5660− io_queue_linked_timeout(linked_timeout);
5661− goto exit;
5662− }
5987+ if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
5988+ if (!io_arm_poll_handler(req)) {
56635989 punt:
5664− io_req_init_async(req);
5665−
5666− if (io_op_defs[req->opcode].file_table) {
5667− ret = io_grab_files(req);
5668− if (ret)
5990+ ret = io_prep_work_files(req);
5991+ if (unlikely(ret))
56695992 goto err;
5993+ /*
5994+ * Queued up for async execution, worker will release
5995+ * submit reference when the iocb is actually submitted.
5996+ */
5997+ io_queue_async_work(req);
56705998 }
56715999
5672− /*
5673− * Queued up for async execution, worker will release
5674− * submit reference when the iocb is actually submitted.
5675− */
5676− io_queue_async_work(req);
6000+ if (linked_timeout)
6001+ io_queue_linked_timeout(linked_timeout);
56776002 goto exit;
56786003 }
56796004
6005+ if (unlikely(ret)) {
56806006 err:
5681− nxt = NULL;
5682− /* drop submission reference */
5683− io_put_req_find_next(req, &nxt);
5684−
5685− if (linked_timeout) {
5686− if (!ret)
5687− io_queue_linked_timeout(linked_timeout);
5688− else
5689− io_put_req(linked_timeout);
5690− }
5691−
5692− /* and drop final reference, if we failed */
5693− if (ret) {
5694− io_cqring_add_event(req, ret);
6007+ /* un-prep timeout, so it'll be killed as any other linked */
6008+ req->flags &= ~REQ_F_LINK_TIMEOUT;
56956009 req_set_fail_links(req);
56966010 io_put_req(req);
6011+ io_req_complete(req, ret);
6012+ goto exit;
56976013 }
6014+
6015+ /* drop submission reference */
6016+ nxt = io_put_req_find_next(req);
6017+ if (linked_timeout)
6018+ io_queue_linked_timeout(linked_timeout);
6019+
56986020 if (nxt) {
56996021 req = nxt;
57006022
@@ -5707,7 +6029,8 @@ exit: · 6 unmodified lines
57076029 revert_creds(old_creds);
57086030 }
57096031
5710−static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
6032+static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
6033+ struct io_comp_state *cs)
57116034 {
57126035 int ret;
57136036
@@ -5715,17 +6038,14 @@ static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) · 1 unmodified line
57156038 if (ret) {
57166039 if (ret != -EIOCBQUEUED) {
57176040 fail_req:
5718− io_cqring_add_event(req, ret);
57196041 req_set_fail_links(req);
5720− io_double_put_req(req);
6042+ io_put_req(req);
6043+ io_req_complete(req, ret);
57216044 }
57226045 } else if (req->flags & REQ_F_FORCE_ASYNC) {
57236046 if (!req->io) {
5724− ret = -EAGAIN;
5725− if (io_alloc_async_ctx(req))
5726− goto fail_req;
57276047 ret = io_req_defer_prep(req, sqe);
5728− if (unlikely(ret < 0))
6048+ if (unlikely(ret))
57296049 goto fail_req;
57306050 }
57316051
@@ -5737,21 +6057,22 @@ fail_req: · 5 unmodified lines
57376057 req->work.flags |= IO_WQ_WORK_CONCURRENT;
57386058 io_queue_async_work(req);
57396059 } else {
5740− __io_queue_sqe(req, sqe);
6060+ __io_queue_sqe(req, sqe, cs);
57416061 }
57426062 }
57436063
5744−static inline void io_queue_link_head(struct io_kiocb *req)
6064+static inline void io_queue_link_head(struct io_kiocb *req,
6065+ struct io_comp_state *cs)
57456066 {
57466067 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
5747− io_cqring_add_event(req, -ECANCELED);
5748− io_double_put_req(req);
6068+ io_put_req(req);
6069+ io_req_complete(req, -ECANCELED);
57496070 } else
5750− io_queue_sqe(req, NULL);
6071+ io_queue_sqe(req, NULL, cs);
57516072 }
57526073
57536074 static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
5754− struct io_kiocb **link)
6075+ struct io_kiocb **link, struct io_comp_state *cs)
57556076 {
57566077 struct io_ring_ctx *ctx = req->ctx;
57576078 int ret;
@@ -5777,21 +6098,19 @@ static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 19 unmodified lines
57776098 head->flags |= REQ_F_IO_DRAIN;
57786099 ctx->drain_next = 1;
57796100 }
5780− if (io_alloc_async_ctx(req))
5781− return -EAGAIN;
5782−
57836101 ret = io_req_defer_prep(req, sqe);
5784− if (ret) {
6102+ if (unlikely(ret)) {
57856103 /* fail even hard links since we don't submit */
57866104 head->flags |= REQ_F_FAIL_LINK;
57876105 return ret;
57886106 }
57896107 trace_io_uring_link(ctx, req, head);
6108+ io_get_req_task(req);
57906109 list_add_tail(&req->link_list, &head->link_list);
57916110
57926111 /* last request of a link, enqueue the link */
57936112 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
5794− io_queue_link_head(head);
6113+ io_queue_link_head(head, cs);
57956114 *link = NULL;
57966115 }
57976116 } else {
@@ -5803,15 +6122,12 @@ static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 5 unmodified lines
58036122 req->flags |= REQ_F_LINK_HEAD;
58046123 INIT_LIST_HEAD(&req->link_list);
58056124
5806− if (io_alloc_async_ctx(req))
5807− return -EAGAIN;
5808−
58096125 ret = io_req_defer_prep(req, sqe);
5810− if (ret)
6126+ if (unlikely(ret))
58116127 req->flags |= REQ_F_FAIL_LINK;
58126128 *link = req;
58136129 } else {
5814− io_queue_sqe(req, sqe);
6130+ io_queue_sqe(req, sqe, cs);
58156131 }
58166132 }
58176133
@@ -5823,6 +6139,8 @@ static int io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, · 5 unmodified lines
58236139 */
58246140 static void io_submit_state_end(struct io_submit_state *state)
58256141 {
6142+ if (!list_empty(&state->comp.list))
6143+ io_submit_flush_completions(&state->comp);
58266144 blk_finish_plug(&state->plug);
58276145 io_state_file_put(state);
58286146 if (state->free_reqs)
@@ -5833,9 +6151,15 @@ static void io_submit_state_end(struct io_submit_state *state) · 4 unmodified lines
58336151 * Start submission side cache.
58346152 */
58356153 static void io_submit_state_start(struct io_submit_state *state,
5836− unsigned int max_ios)
6154+ struct io_ring_ctx *ctx, unsigned int max_ios)
58376155 {
58386156 blk_start_plug(&state->plug);
6157+#ifdef CONFIG_BLOCK
6158+ state->plug.nowait = true;
6159+#endif
6160+ state->comp.nr = 0;
6161+ INIT_LIST_HEAD(&state->comp.list);
6162+ state->comp.ctx = ctx;
58396163 state->free_reqs = 0;
58406164 state->file = NULL;
58416165 state->ios_left = max_ios;
@@ -5900,12 +6224,6 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 58 unmodified lines
59006224 unsigned int sqe_flags;
59016225 int id;
59026226
5903− /*
5904− * All io need record the previous position, if LINK vs DARIN,
5905− * it can be used to mark the position of the first IO in the
5906− * link list.
5907− */
5908− req->sequence = ctx->cached_sq_head - ctx->cached_sq_dropped;
59096227 req->opcode = READ_ONCE(sqe->opcode);
59106228 req->user_data = READ_ONCE(sqe->user_data);
59116229 req->io = NULL;
@@ -5953,7 +6271,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, · 41 unmodified lines
59536271 static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
59546272 struct file *ring_file, int ring_fd)
59556273 {
5956− struct io_submit_state state, *statep = NULL;
6274+ struct io_submit_state state;
59576275 struct io_kiocb *link = NULL;
59586276 int i, submitted = 0;
59596277
@@ -5970,10 +6288,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, · 10 unmodified lines
59706288 if (!percpu_ref_tryget_many(&ctx->refs, nr))
59716289 return -EAGAIN;
59726290
5973− if (nr > IO_PLUG_THRESHOLD) {
5974− io_submit_state_start(&state, nr);
5975− statep = &state;
5976− }
6291+ io_submit_state_start(&state, ctx, nr);
59776292
59786293 ctx->ring_fd = ring_fd;
59796294 ctx->ring_file = ring_file;
@@ -5988,28 +6303,28 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, · 8 unmodified lines
59886303 io_consume_sqe(ctx);
59896304 break;
59906305 }
5991− req = io_alloc_req(ctx, statep);
6306+ req = io_alloc_req(ctx, &state);
59926307 if (unlikely(!req)) {
59936308 if (!submitted)
59946309 submitted = -EAGAIN;
59956310 break;
59966311 }
59976312
5998− err = io_init_req(ctx, req, sqe, statep);
6313+ err = io_init_req(ctx, req, sqe, &state);
59996314 io_consume_sqe(ctx);
60006315 /* will complete beyond this point, count as submitted */
60016316 submitted++;
60026317
60036318 if (unlikely(err)) {
60046319 fail_req:
6005− io_cqring_add_event(req, err);
6006− io_double_put_req(req);
6320+ io_put_req(req);
6321+ io_req_complete(req, err);
60076322 break;
60086323 }
60096324
60106325 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
60116326 true, io_async_submit(ctx));
6012− err = io_submit_sqe(req, sqe, &link);
6327+ err = io_submit_sqe(req, sqe, &link, &state.comp);
60136328 if (err)
60146329 goto fail_req;
60156330 }
@@ -6020,9 +6335,8 @@ fail_req: · 4 unmodified lines
60206335 percpu_ref_put_many(&ctx->refs, nr - ref_used);
60216336 }
60226337 if (link)
6023− io_queue_link_head(link);
6024− if (statep)
6025− io_submit_state_end(&state);
6338+ io_queue_link_head(link, &state.comp);
6339+ io_submit_state_end(&state);
60266340
60276341 /* Commit SQ ring head once we've consumed and submitted all SQEs */
60286342 io_commit_sqring(ctx);
@@ -6030,6 +6344,21 @@ fail_req: · 1 unmodified line
60306344 return submitted;
60316345 }
60326346
6347+static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6348+{
6349+ /* Tell userspace we may need a wakeup call */
6350+ spin_lock_irq(&ctx->completion_lock);
6351+ ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6352+ spin_unlock_irq(&ctx->completion_lock);
6353+}
6354+
6355+static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6356+{
6357+ spin_lock_irq(&ctx->completion_lock);
6358+ ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6359+ spin_unlock_irq(&ctx->completion_lock);
6360+}
6361+
60336362 static int io_sq_thread(void *data)
60346363 {
60356364 struct io_ring_ctx *ctx = data;
@@ -6046,12 +6375,12 @@ static int io_sq_thread(void *data) · 10 unmodified lines
60466375 while (!kthread_should_park()) {
60476376 unsigned int to_submit;
60486377
6049− if (!list_empty(&ctx->poll_list)) {
6378+ if (!list_empty(&ctx->iopoll_list)) {
60506379 unsigned nr_events = 0;
60516380
60526381 mutex_lock(&ctx->uring_lock);
6053− if (!list_empty(&ctx->poll_list))
6054− io_iopoll_getevents(ctx, &nr_events, 0);
6382+ if (!list_empty(&ctx->iopoll_list) && !need_resched())
6383+ io_do_iopoll(ctx, &nr_events, 0);
60556384 else
60566385 timeout = jiffies + ctx->sq_thread_idle;
60576386 mutex_unlock(&ctx->uring_lock);
@@ -6070,7 +6399,7 @@ static int io_sq_thread(void *data) · 12 unmodified lines
60706399 * adding ourselves to the waitqueue, as the unuse/drop
60716400 * may sleep.
60726401 */
6073− io_sq_thread_drop_mm(ctx);
6402+ io_sq_thread_drop_mm();
60746403
60756404 /*
60766405 * We're polling. If we're within the defined idle
@@ -6079,11 +6408,10 @@ static int io_sq_thread(void *data) · 2 unmodified lines
60796408 * more IO, we should wait for the application to
60806409 * reap events and wake us up.
60816410 */
6082− if (!list_empty(&ctx->poll_list) || need_resched() ||
6411+ if (!list_empty(&ctx->iopoll_list) || need_resched() ||
60836412 (!time_after(jiffies, timeout) && ret != -EBUSY &&
60846413 !percpu_ref_is_dying(&ctx->refs))) {
6085− if (current->task_works)
6086− task_work_run();
6414+ io_run_task_work();
60876415 cond_resched();
60886416 continue;
60896417 }
@@ -6093,21 +6421,18 @@ static int io_sq_thread(void *data) · 3 unmodified lines
60936421
60946422 /*
60956423 * While doing polled IO, before going to sleep, we need
6096− * to check if there are new reqs added to poll_list, it
6097− * is because reqs may have been punted to io worker and
6098− * will be added to poll_list later, hence check the
6099− * poll_list again.
6424+ * to check if there are new reqs added to iopoll_list,
6425+ * it is because reqs may have been punted to io worker
6426+ * and will be added to iopoll_list later, hence check
6427+ * the iopoll_list again.
61006428 */
61016429 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6102− !list_empty_careful(&ctx->poll_list)) {
6430+ !list_empty_careful(&ctx->iopoll_list)) {
61036431 finish_wait(&ctx->sqo_wait, &wait);
61046432 continue;
61056433 }
61066434
6107− /* Tell userspace we may need a wakeup call */
6108− spin_lock_irq(&ctx->completion_lock);
6109− ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6110− spin_unlock_irq(&ctx->completion_lock);
6435+ io_ring_set_wakeup_flag(ctx);
61116436
61126437 to_submit = io_sqring_entries(ctx);
61136438 if (!to_submit || ret == -EBUSY) {
@@ -6115,9 +6440,9 @@ static int io_sq_thread(void *data) · 1 unmodified line
61156440 finish_wait(&ctx->sqo_wait, &wait);
61166441 break;
61176442 }
6118− if (current->task_works) {
6119− task_work_run();
6443+ if (io_run_task_work()) {
61206444 finish_wait(&ctx->sqo_wait, &wait);
6445+ io_ring_clear_wakeup_flag(ctx);
61216446 continue;
61226447 }
61236448 if (signal_pending(current))
@@ -6125,17 +6450,13 @@ static int io_sq_thread(void *data) · 1 unmodified line
61256450 schedule();
61266451 finish_wait(&ctx->sqo_wait, &wait);
61276452
6128− spin_lock_irq(&ctx->completion_lock);
6129− ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6130− spin_unlock_irq(&ctx->completion_lock);
6453+ io_ring_clear_wakeup_flag(ctx);
61316454 ret = 0;
61326455 continue;
61336456 }
61346457 finish_wait(&ctx->sqo_wait, &wait);
61356458
6136− spin_lock_irq(&ctx->completion_lock);
6137− ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6138− spin_unlock_irq(&ctx->completion_lock);
6459+ io_ring_clear_wakeup_flag(ctx);
61396460 }
61406461
61416462 mutex_lock(&ctx->uring_lock);
@@ -6145,10 +6466,9 @@ static int io_sq_thread(void *data) · 3 unmodified lines
61456466 timeout = jiffies + ctx->sq_thread_idle;
61466467 }
61476468
6148− if (current->task_works)
6149− task_work_run();
6469+ io_run_task_work();
61506470
6151− io_sq_thread_drop_mm(ctx);
6471+ io_sq_thread_drop_mm();
61526472 revert_creds(old_cred);
61536473
61546474 kthread_parkme();
@@ -6211,9 +6531,8 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 56 unmodified lines
62116531 do {
62126532 if (io_cqring_events(ctx, false) >= min_events)
62136533 return 0;
6214− if (!current->task_works)
6534+ if (!io_run_task_work())
62156535 break;
6216− task_work_run();
62176536 } while (1);
62186537
62196538 if (sig) {
@@ -6235,8 +6554,8 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, · 15 unmodified lines
62356554 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
62366555 TASK_INTERRUPTIBLE);
62376556 /* make sure we run task_work before checking for signals */
6238− if (current->task_works)
6239− task_work_run();
6557+ if (io_run_task_work())
6558+ continue;
62406559 if (signal_pending(current)) {
62416560 if (current->jobctl & JOBCTL_TASK_WORK) {
62426561 spin_lock_irq(&current->sighand->siglock);
@@ -7022,17 +7341,21 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, · 779 unmodified lines
70227341 return 0;
70237342 err:
70247343 io_finish_async(ctx);
7025− mmdrop(ctx->sqo_mm);
7026− ctx->sqo_mm = NULL;
7344+ if (ctx->sqo_mm) {
7345+ mmdrop(ctx->sqo_mm);
7346+ ctx->sqo_mm = NULL;
7347+ }
70277348 return ret;
70287349 }
70297350
7030−static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
7351+static inline void __io_unaccount_mem(struct user_struct *user,
7352+ unsigned long nr_pages)
70317353 {
70327354 atomic_long_sub(nr_pages, &user->locked_vm);
70337355 }
70347356
7035−static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
7357+static inline int __io_account_mem(struct user_struct *user,
7358+ unsigned long nr_pages)
70367359 {
70377360 unsigned long page_limit, cur_pages, new_pages;
70387361
@@ -7050,6 +7373,41 @@ static int io_account_mem(struct user_struct *user, unsigned long nr_pages) · 11 unmodified lines
70507373 return 0;
70517374 }
70527375
7376+static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7377+ enum io_mem_account acct)
7378+{
7379+ if (ctx->limit_mem)
7380+ __io_unaccount_mem(ctx->user, nr_pages);
7381+
7382+ if (ctx->sqo_mm) {
7383+ if (acct == ACCT_LOCKED)
7384+ ctx->sqo_mm->locked_vm -= nr_pages;
7385+ else if (acct == ACCT_PINNED)
7386+ atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
7387+ }
7388+}
7389+
7390+static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages,
7391+ enum io_mem_account acct)
7392+{
7393+ int ret;
7394+
7395+ if (ctx->limit_mem) {
7396+ ret = __io_account_mem(ctx->user, nr_pages);
7397+ if (ret)
7398+ return ret;
7399+ }
7400+
7401+ if (ctx->sqo_mm) {
7402+ if (acct == ACCT_LOCKED)
7403+ ctx->sqo_mm->locked_vm += nr_pages;
7404+ else if (acct == ACCT_PINNED)
7405+ atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
7406+ }
7407+
7408+ return 0;
7409+}
7410+
70537411 static void io_mem_free(void *ptr)
70547412 {
70557413 struct page *page;
@@ -7086,6 +7444,9 @@ static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, · 30 unmodified lines
70867444 return SIZE_MAX;
70877445 #endif
70887446
7447+ if (sq_offset)
7448+ *sq_offset = off;
7449+
70897450 sq_array_size = array_size(sizeof(u32), sq_entries);
70907451 if (sq_array_size == SIZE_MAX)
70917452 return SIZE_MAX;
@@ -7093,9 +7454,6 @@ static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, · 1 unmodified line
70937454 if (check_add_overflow(off, sq_array_size, &off))
70947455 return SIZE_MAX;
70957456
7096− if (sq_offset)
7097− *sq_offset = off;
7098−
70997457 return off;
71007458 }
71017459
@@ -7124,8 +7482,7 @@ static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) · 22 unmodified lines
71247482 for (j = 0; j < imu->nr_bvecs; j++)
71257483 unpin_user_page(imu->bvec[j].bv_page);
71267484
7127− if (ctx->account_mem)
7128− io_unaccount_mem(ctx->user, imu->nr_bvecs);
7485+ io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
71297486 kvfree(imu->bvec);
71307487 imu->nr_bvecs = 0;
71317488 }
@@ -7208,11 +7565,9 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, · 76 unmodified lines
72087565 start = ubuf >> PAGE_SHIFT;
72097566 nr_pages = end - start;
72107567
7211− if (ctx->account_mem) {
7212− ret = io_account_mem(ctx->user, nr_pages);
7213− if (ret)
7214− goto err;
7215− }
7568+ ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
7569+ if (ret)
7570+ goto err;
72167571
72177572 ret = 0;
72187573 if (!pages || nr_pages > got_pages) {
@@ -7225,8 +7580,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, · 6 unmodified lines
72257580 GFP_KERNEL);
72267581 if (!pages || !vmas) {
72277582 ret = -ENOMEM;
7228− if (ctx->account_mem)
7229− io_unaccount_mem(ctx->user, nr_pages);
7583+ io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
72307584 goto err;
72317585 }
72327586 got_pages = nr_pages;
@@ -7236,8 +7590,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, · 3 unmodified lines
72367590 GFP_KERNEL);
72377591 ret = -ENOMEM;
72387592 if (!imu->bvec) {
7239− if (ctx->account_mem)
7240− io_unaccount_mem(ctx->user, nr_pages);
7593+ io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
72417594 goto err;
72427595 }
72437596
@@ -7268,8 +7621,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, · 24 unmodified lines
72687621 */
72697622 if (pret > 0)
72707623 unpin_user_pages(pages, pret);
7271− if (ctx->account_mem)
7272− io_unaccount_mem(ctx->user, nr_pages);
7624+ io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
72737625 kvfree(imu->bvec);
72747626 goto err;
72757627 }
@@ -7353,11 +7705,12 @@ static void io_destroy_buffers(struct io_ring_ctx *ctx) · 77 unmodified lines
73537705 static void io_ring_ctx_free(struct io_ring_ctx *ctx)
73547706 {
73557707 io_finish_async(ctx);
7356− if (ctx->sqo_mm)
7708+ io_sqe_buffer_unregister(ctx);
7709+ if (ctx->sqo_mm) {
73577710 mmdrop(ctx->sqo_mm);
7711+ ctx->sqo_mm = NULL;
7712+ }
73587713
7359− io_iopoll_reap_events(ctx);
7360− io_sqe_buffer_unregister(ctx);
73617714 io_sqe_files_unregister(ctx);
73627715 io_eventfd_unregister(ctx);
73637716 io_destroy_buffers(ctx);
@@ -7421,11 +7774,8 @@ static int io_remove_personalities(int id, void *p, void *data) · 57 unmodified lines
74217774
74227775 static void io_ring_exit_work(struct work_struct *work)
74237776 {
7424− struct io_ring_ctx *ctx;
7425−
7426− ctx = container_of(work, struct io_ring_ctx, exit_work);
7427− if (ctx->rings)
7428− io_cqring_overflow_flush(ctx, true);
7777+ struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
7778+ exit_work);
74297779
74307780 /*
74317781 * If we're doing polled IO and end up having requests being
@@ -7433,11 +7783,11 @@ static void io_ring_exit_work(struct work_struct *work) · 1 unmodified line
74337783 * we're waiting for refs to drop. We need to reap these manually,
74347784 * as nobody else will be looking for them.
74357785 */
7436− while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20)) {
7437− io_iopoll_reap_events(ctx);
7786+ do {
74387787 if (ctx->rings)
74397788 io_cqring_overflow_flush(ctx, true);
7440− }
7789+ io_iopoll_try_reap_events(ctx);
7790+ } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
74417791 io_ring_ctx_free(ctx);
74427792 }
74437793
@@ -7453,10 +7803,10 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) · 9 unmodified lines
74537803 if (ctx->io_wq)
74547804 io_wq_cancel_all(ctx->io_wq);
74557805
7456− io_iopoll_reap_events(ctx);
74577806 /* if we failed setting up the ctx, we might not have any rings */
74587807 if (ctx->rings)
74597808 io_cqring_overflow_flush(ctx, true);
7809+ io_iopoll_try_reap_events(ctx);
74607810 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
74617811
74627812 /*
@@ -7464,9 +7814,8 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) · 1 unmodified line
74647814 * is closed but resources aren't reaped yet. This can cause
74657815 * spurious failure in setting up a new ring.
74667816 */
7467− if (ctx->account_mem)
7468− io_unaccount_mem(ctx->user,
7469− ring_pages(ctx->sq_entries, ctx->cq_entries));
7817+ io_unaccount_mem(ctx, ring_pages(ctx->sq_entries, ctx->cq_entries),
7818+ ACCT_LOCKED);
74707819
74717820 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
74727821 queue_work(system_wq, &ctx->exit_work);
@@ -7522,17 +7871,14 @@ static void io_uring_cancel_files(struct io_ring_ctx *ctx, · 49 unmodified lines
75227871
75237872 if (cancel_req->flags & REQ_F_OVERFLOW) {
75247873 spin_lock_irq(&ctx->completion_lock);
7525− list_del(&cancel_req->list);
7874+ list_del(&cancel_req->compl.list);
75267875 cancel_req->flags &= ~REQ_F_OVERFLOW;
7527− if (list_empty(&ctx->cq_overflow_list)) {
7528− clear_bit(0, &ctx->sq_check_overflow);
7529− clear_bit(0, &ctx->cq_check_overflow);
7530− ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
7531− }
7532− spin_unlock_irq(&ctx->completion_lock);
75337876
7877+ io_cqring_mark_overflow(ctx);
75347878 WRITE_ONCE(ctx->rings->cq_overflow,
75357879 atomic_inc_return(&ctx->cached_cq_overflow));
7880+ io_commit_cqring(ctx);
7881+ spin_unlock_irq(&ctx->completion_lock);
75367882
75377883 /*
75387884 * Put inflight ref and overflow ref. If that's
@@ -7655,8 +8001,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 116 unmodified lines
76558001 int submitted = 0;
76568002 struct fd f;
76578003
7658− if (current->task_works)
7659− task_work_run();
8004+ io_run_task_work();
76608005
76618006 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
76628007 return -EINVAL;
@@ -7695,8 +8040,6 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 32 unmodified lines
76958040 goto out;
76968041 }
76978042 if (flags & IORING_ENTER_GETEVENTS) {
7698− unsigned nr_events = 0;
7699−
77008043 min_complete = min(min_complete, ctx->cq_entries);
77018044
77028045 /*
@@ -7707,7 +8050,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, · 4 unmodified lines
77078050 */
77088051 if (ctx->flags & IORING_SETUP_IOPOLL &&
77098052 !(ctx->flags & IORING_SETUP_SQPOLL)) {
7710− ret = io_iopoll_check(ctx, &nr_events, min_complete);
8053+ ret = io_iopoll_check(ctx, min_complete);
77118054 } else {
77128055 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
77138056 }
@@ -7912,7 +8255,7 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, · 198 unmodified lines
79128255 {
79138256 struct user_struct *user = NULL;
79148257 struct io_ring_ctx *ctx;
7915− bool account_mem;
8258+ bool limit_mem;
79168259 int ret;
79178260
79188261 if (!entries)
@@ -7951,10 +8294,10 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, · 32 unmodified lines
79518294 }
79528295
79538296 user = get_uid(current_user());
7954− account_mem = !capable(CAP_IPC_LOCK);
8297+ limit_mem = !capable(CAP_IPC_LOCK);
79558298
7956− if (account_mem) {
7957− ret = io_account_mem(user,
8299+ if (limit_mem) {
8300+ ret = __io_account_mem(user,
79588301 ring_pages(p->sq_entries, p->cq_entries));
79598302 if (ret) {
79608303 free_uid(user);
@@ -7964,14 +8307,13 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, · 3 unmodified lines
79648307
79658308 ctx = io_ring_ctx_alloc(p);
79668309 if (!ctx) {
7967− if (account_mem)
7968− io_unaccount_mem(user, ring_pages(p->sq_entries,
8310+ if (limit_mem)
8311+ __io_unaccount_mem(user, ring_pages(p->sq_entries,
79698312 p->cq_entries));
79708313 free_uid(user);
79718314 return -ENOMEM;
79728315 }
79738316 ctx->compat = in_compat_syscall();
7974− ctx->account_mem = account_mem;
79758317 ctx->user = user;
79768318 ctx->creds = get_current_cred();
79778319
@@ -8003,12 +8345,22 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, · 25 unmodified lines
80038345
80048346 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
80058347 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
8006− IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL;
8348+ IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
8349+ IORING_FEAT_POLL_32BITS;
80078350
80088351 if (copy_to_user(params, p, sizeof(*p))) {
80098352 ret = -EFAULT;
80108353 goto err;
80118354 }
8355+
8356+ /*
8357+ * Account memory _before_ installing the file descriptor. Once
8358+ * the descriptor is installed, it can get closed at any time.
8359+ */
8360+ io_account_mem(ctx, ring_pages(p->sq_entries, p->cq_entries),
8361+ ACCT_LOCKED);
8362+ ctx->limit_mem = limit_mem;
8363+
80128364 /*
80138365 * Install ring fd as the very last thing, so we don't risk someone
80148366 * having closed it before we finish setup
@@ -8292,7 +8644,8 @@ static int __init io_uring_init(void) · 277 unmodified lines
82928644 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
82938645 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
82948646 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
8295− BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
8647+ BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
8648+ BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
82968649 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
82978650 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
82988651 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
fs/xfs/xfs_file.c+1−1
@@ -1080,7 +1080,7 @@ xfs_file_open( · 1079 unmodified lines
10801080 return -EFBIG;
10811081 if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
10821082 return -EIO;
1083− file->f_mode |= FMODE_NOWAIT;
1083+ file->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
10841084 return 0;
10851085 }
10861086
include/linux/blkdev.h+1−0
@@ -1180,6 +1180,7 @@ struct blk_plug { · 1179 unmodified lines
11801180 struct list_head cb_list; /* md requires an unplug callback */
11811181 unsigned short rq_count;
11821182 bool multiple_queues;
1183+ bool nowait;
11831184 };
11841185 #define BLK_MAX_REQUEST_COUNT 16
11851186 #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
include/linux/fs.h+20−6
@@ -175,6 +175,9 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, · 174 unmodified lines
175175 /* File does not contribute to nr_files count */
176176 #define FMODE_NOACCOUNT ((__force fmode_t)0x20000000)
177177
178+/* File supports async buffered reads */
179+#define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000)
180+
178181 /*
179182 * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
180183 * that indicates that they should check the contents of the iovec are
@@ -315,6 +318,8 @@ enum rw_hint { · 134 unmodified lines
315318 #define IOCB_SYNC (1 << 5)
316319 #define IOCB_WRITE (1 << 6)
317320 #define IOCB_NOWAIT (1 << 7)
321+/* iocb->ki_waitq is valid */
322+#define IOCB_WAITQ (1 << 8)
318323 #define IOCB_NOIO (1 << 9)
319324
320325 struct kiocb {
@@ -329,7 +334,10 @@ struct kiocb { · 8 unmodified lines
329334 int ki_flags;
330335 u16 ki_hint;
331336 u16 ki_ioprio; /* See linux/ioprio.h */
332− unsigned int ki_cookie; /* for ->iopoll */
337+ union {
338+ unsigned int ki_cookie; /* for ->iopoll */
339+ struct wait_page_queue *ki_waitq; /* for async buffered IO */
340+ };
333341
334342 randomized_struct_fields_end
335343 };
@@ -3275,22 +3283,28 @@ static inline int iocb_flags(struct file *file) · 2939 unmodified lines
32753283
32763284 static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
32773285 {
3286+ int kiocb_flags = 0;
3287+
3288+ if (!flags)
3289+ return 0;
32783290 if (unlikely(flags & ~RWF_SUPPORTED))
32793291 return -EOPNOTSUPP;
32803292
32813293 if (flags & RWF_NOWAIT) {
32823294 if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
32833295 return -EOPNOTSUPP;
3284− ki->ki_flags |= IOCB_NOWAIT;
3296+ kiocb_flags |= IOCB_NOWAIT;
32853297 }
32863298 if (flags & RWF_HIPRI)
3287− ki->ki_flags |= IOCB_HIPRI;
3299+ kiocb_flags |= IOCB_HIPRI;
32883300 if (flags & RWF_DSYNC)
3289− ki->ki_flags |= IOCB_DSYNC;
3301+ kiocb_flags |= IOCB_DSYNC;
32903302 if (flags & RWF_SYNC)
3291− ki->ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
3303+ kiocb_flags |= (IOCB_DSYNC | IOCB_SYNC);
32923304 if (flags & RWF_APPEND)
3293− ki->ki_flags |= IOCB_APPEND;
3305+ kiocb_flags |= IOCB_APPEND;
3306+
3307+ ki->ki_flags |= kiocb_flags;
32943308 return 0;
32953309 }
32963310
include/linux/pagemap.h+43−0
@@ -496,8 +496,35 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma, · 495 unmodified lines
496496 return pgoff;
497497 }
498498
499+/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
500+struct wait_page_key {
501+ struct page *page;
502+ int bit_nr;
503+ int page_match;
504+};
505+
506+struct wait_page_queue {
507+ struct page *page;
508+ int bit_nr;
509+ wait_queue_entry_t wait;
510+};
511+
512+static inline bool wake_page_match(struct wait_page_queue *wait_page,
513+ struct wait_page_key *key)
514+{
515+ if (wait_page->page != key->page)
516+ return false;
517+ key->page_match = 1;
518+
519+ if (wait_page->bit_nr != key->bit_nr)
520+ return false;
521+
522+ return true;
523+}
524+
499525 extern void __lock_page(struct page *page);
500526 extern int __lock_page_killable(struct page *page);
527+extern int __lock_page_async(struct page *page, struct wait_page_queue *wait);
501528 extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
502529 unsigned int flags);
503530 extern void unlock_page(struct page *page);
@@ -534,6 +561,22 @@ static inline int lock_page_killable(struct page *page) · 30 unmodified lines
534561 return 0;
535562 }
536563
564+/*
565+ * lock_page_async - Lock the page, unless this would block. If the page
566+ * is already locked, then queue a callback when the page becomes unlocked.
567+ * This callback can then retry the operation.
568+ *
569+ * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page
570+ * was already locked and the callback defined in 'wait' was queued.
571+ */
572+static inline int lock_page_async(struct page *page,
573+ struct wait_page_queue *wait)
574+{
575+ if (!trylock_page(page))
576+ return __lock_page_async(page, wait);
577+ return 0;
578+}
579+
537580 /*
538581 * lock_page_or_retry - Lock the page, unless this would block and the
539582 * caller indicated that it can handle a retry.
include/linux/sched/task.h+6−0
@@ -126,6 +126,12 @@ static inline void put_task_struct(struct task_struct *t) · 125 unmodified lines
126126 __put_task_struct(t);
127127 }
128128
129+static inline void put_task_struct_many(struct task_struct *t, int nr)
130+{
131+ if (refcount_sub_and_test(nr, &t->usage))
132+ __put_task_struct(t);
133+}
134+
129135 void put_task_struct_rcu_user(struct task_struct *task);
130136
131137 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT
include/uapi/linux/io_uring.h+3−1
@@ -31,7 +31,8 @@ struct io_uring_sqe { · 30 unmodified lines
3131 union {
3232 __kernel_rwf_t rw_flags;
3333 __u32 fsync_flags;
34− __u16 poll_events;
34+ __u16 poll_events; /* compatibility */
35+ __u32 poll32_events; /* word-reversed for BE */
3536 __u32 sync_range_flags;
3637 __u32 msg_flags;
3738 __u32 timeout_flags;
@@ -249,6 +250,7 @@ struct io_uring_params { · 211 unmodified lines
249250 #define IORING_FEAT_RW_CUR_POS (1U << 3)
250251 #define IORING_FEAT_CUR_PERSONALITY (1U << 4)
251252 #define IORING_FEAT_FAST_POLL (1U << 5)
253+#define IORING_FEAT_POLL_32BITS (1U << 6)
252254
253255 /*
254256 * io_uring_register(2) opcodes and arguments
mm/filemap.c+64−27
@@ -987,19 +987,6 @@ void __init pagecache_init(void) · 986 unmodified lines
987987 page_writeback_init();
988988 }
989989
990−/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
991−struct wait_page_key {
992− struct page *page;
993− int bit_nr;
994− int page_match;
995−};
996−
997−struct wait_page_queue {
998− struct page *page;
999− int bit_nr;
1000− wait_queue_entry_t wait;
1001−};
1002−
1003990 static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
1004991 {
1005992 int ret;
@@ -1007,11 +994,7 @@ static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, · 1 unmodified line
1007994 struct wait_page_queue *wait_page
1008995 = container_of(wait, struct wait_page_queue, wait);
1009996
1010− if (wait_page->page != key->page)
1011− return 0;
1012− key->page_match = 1;
1013−
1014− if (wait_page->bit_nr != key->bit_nr)
997+ if (!wake_page_match(wait_page, key))
1015998 return 0;
1016999
10171000 /*
@@ -1240,6 +1223,44 @@ int wait_on_page_bit_killable(struct page *page, int bit_nr) · 222 unmodified lines
12401223 }
12411224 EXPORT_SYMBOL(wait_on_page_bit_killable);
12421225
1226+static int __wait_on_page_locked_async(struct page *page,
1227+ struct wait_page_queue *wait, bool set)
1228+{
1229+ struct wait_queue_head *q = page_waitqueue(page);
1230+ int ret = 0;
1231+
1232+ wait->page = page;
1233+ wait->bit_nr = PG_locked;
1234+
1235+ spin_lock_irq(&q->lock);
1236+ __add_wait_queue_entry_tail(q, &wait->wait);
1237+ SetPageWaiters(page);
1238+ if (set)
1239+ ret = !trylock_page(page);
1240+ else
1241+ ret = PageLocked(page);
1242+ /*
1243+ * If we were succesful now, we know we're still on the
1244+ * waitqueue as we're still under the lock. This means it's
1245+ * safe to remove and return success, we know the callback
1246+ * isn't going to trigger.
1247+ */
1248+ if (!ret)
1249+ __remove_wait_queue(q, &wait->wait);
1250+ else
1251+ ret = -EIOCBQUEUED;
1252+ spin_unlock_irq(&q->lock);
1253+ return ret;
1254+}
1255+
1256+static int wait_on_page_locked_async(struct page *page,
1257+ struct wait_page_queue *wait)
1258+{
1259+ if (!PageLocked(page))
1260+ return 0;
1261+ return __wait_on_page_locked_async(compound_head(page), wait, false);
1262+}
1263+
12431264 /**
12441265 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
12451266 * @page: The page to wait for.
@@ -1402,6 +1423,11 @@ int __lock_page_killable(struct page *__page) · 156 unmodified lines
14021423 }
14031424 EXPORT_SYMBOL_GPL(__lock_page_killable);
14041425
1426+int __lock_page_async(struct page *page, struct wait_page_queue *wait)
1427+{
1428+ return __wait_on_page_locked_async(page, wait, true);
1429+}
1430+
14051431 /*
14061432 * Return values:
14071433 * 1 - page is locked; mmap_lock is still held.
@@ -2061,7 +2087,7 @@ find_page: · 653 unmodified lines
20612087
20622088 page = find_get_page(mapping, index);
20632089 if (!page) {
2064− if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2090+ if (iocb->ki_flags & IOCB_NOIO)
20652091 goto would_block;
20662092 page_cache_sync_readahead(mapping,
20672093 ra, filp,
@@ -2080,17 +2106,25 @@ find_page: · 12 unmodified lines
20802106 index, last_index - index);
20812107 }
20822108 if (!PageUptodate(page)) {
2083− if (iocb->ki_flags & IOCB_NOWAIT) {
2084− put_page(page);
2085− goto would_block;
2086− }
2087−
20882109 /*
20892110 * See comment in do_read_cache_page on why
20902111 * wait_on_page_locked is used to avoid unnecessarily
20912112 * serialisations and why it's safe.
20922113 */
2093− error = wait_on_page_locked_killable(page);
2114+ if (iocb->ki_flags & IOCB_WAITQ) {
2115+ if (written) {
2116+ put_page(page);
2117+ goto out;
2118+ }
2119+ error = wait_on_page_locked_async(page,
2120+ iocb->ki_waitq);
2121+ } else {
2122+ if (iocb->ki_flags & IOCB_NOWAIT) {
2123+ put_page(page);
2124+ goto would_block;
2125+ }
2126+ error = wait_on_page_locked_killable(page);
2127+ }
20942128 if (unlikely(error))
20952129 goto readpage_error;
20962130 if (PageUptodate(page))
@@ -2178,7 +2212,10 @@ page_ok: · 81 unmodified lines
21782212
21792213 page_not_up_to_date:
21802214 /* Get exclusive access to the page ... */
2181− error = lock_page_killable(page);
2215+ if (iocb->ki_flags & IOCB_WAITQ)
2216+ error = lock_page_async(page, iocb->ki_waitq);
2217+ else
2218+ error = lock_page_killable(page);
21822219 if (unlikely(error))
21832220 goto readpage_error;
21842221
@@ -2197,7 +2234,7 @@ page_not_up_to_date_locked: · 12 unmodified lines
21972234 }
21982235
21992236 readpage:
2200− if (iocb->ki_flags & IOCB_NOIO) {
2237+ if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT)) {
22012238 unlock_page(page);
22022239 put_page(page);
22032240 goto would_block;
tools/io_uring/liburing.h+5−1
@@ -10,6 +10,7 @@ extern "C" { · 9 unmodified lines
1010 #include <string.h>
1111 #include "../../include/uapi/linux/io_uring.h"
1212 #include <inttypes.h>
13+#include <linux/swab.h>
1314 #include "barrier.h"
1415
1516 /*
@@ -145,11 +146,14 @@ static inline void io_uring_prep_write_fixed(struct io_uring_sqe *sqe, int fd, · 129 unmodified lines
145146 }
146147
147148 static inline void io_uring_prep_poll_add(struct io_uring_sqe *sqe, int fd,
148− short poll_mask)
149+ unsigned poll_mask)
149150 {
150151 memset(sqe, 0, sizeof(*sqe));
151152 sqe->opcode = IORING_OP_POLL_ADD;
152153 sqe->fd = fd;
154+#if __BYTE_ORDER == __BIG_ENDIAN
155+ poll_mask = __swahw32(poll_mask);
156+#endif
153157 sqe->poll_events = poll_mask;
154158 }
155159