]> Git Repo - linux.git/commitdiff
Merge tag 'io_uring-5.10-2020-10-12' of git://git.kernel.dk/linux-block
authorLinus Torvalds <[email protected]>
Tue, 13 Oct 2020 19:36:21 +0000 (12:36 -0700)
committerLinus Torvalds <[email protected]>
Tue, 13 Oct 2020 19:36:21 +0000 (12:36 -0700)
Pull io_uring updates from Jens Axboe:

 - Add blkcg accounting for io-wq offload (Dennis)

 - A use-after-free fix for io-wq (Hillf)

 - Cancelation fixes and improvements

 - Use proper files_struct references for offload

 - Cleanup of io_uring_get_socket() since that can now go into our own
   header

 - SQPOLL fixes and cleanups, and support for sharing the thread

 - Improvement to how page accounting is done for registered buffers and
   huge pages, accounting the real pinned state

 - Series cleaning up the xarray code (Willy)

 - Various cleanups, refactoring, and improvements (Pavel)

 - Use raw spinlock for io-wq (Sebastian)

 - Add support for ring restrictions (Stefano)

* tag 'io_uring-5.10-2020-10-12' of git://git.kernel.dk/linux-block: (62 commits)
  io_uring: keep a pointer ref_node in file_data
  io_uring: refactor *files_register()'s error paths
  io_uring: clean file_data access in files_register
  io_uring: don't delay io_init_req() error check
  io_uring: clean leftovers after splitting issue
  io_uring: remove timeout.list after hrtimer cancel
  io_uring: use a separate struct for timeout_remove
  io_uring: improve submit_state.ios_left accounting
  io_uring: simplify io_file_get()
  io_uring: kill extra check in fixed io_file_get()
  io_uring: clean up ->files grabbing
  io_uring: don't io_prep_async_work() linked reqs
  io_uring: Convert advanced XArray uses to the normal API
  io_uring: Fix XArray usage in io_uring_add_task_file
  io_uring: Fix use of XArray in __io_uring_files_cancel
  io_uring: fix break condition for __io_uring_register() waiting
  io_uring: no need to call xa_destroy() on empty xarray
  io_uring: batch account ->req_issue and task struct references
  io_uring: kill callback_head argument for io_req_task_work_add()
  io_uring: move req preps out of io_issue_sqe()
  ...

1  2 
fs/io_uring.c
include/linux/fs.h
include/linux/sched.h

diff --combined fs/io_uring.c
index f58b3d6bba8ad1fb19928e200afdecb7978f95cd,c729ee8033f8c08aea704191794623ba0edec444..fc6de6b4784e67d472c7ff27ae911c389f6e9292
@@@ -79,6 -79,8 +79,8 @@@
  #include <linux/splice.h>
  #include <linux/task_work.h>
  #include <linux/pagemap.h>
+ #include <linux/io_uring.h>
+ #include <linux/blk-cgroup.h>
  
  #define CREATE_TRACE_POINTS
  #include <trace/events/io_uring.h>
  #define IORING_MAX_FILES_TABLE        (1U << IORING_FILE_TABLE_SHIFT)
  #define IORING_FILE_TABLE_MASK        (IORING_MAX_FILES_TABLE - 1)
  #define IORING_MAX_FIXED_FILES        (64 * IORING_MAX_FILES_TABLE)
+ #define IORING_MAX_RESTRICTIONS       (IORING_RESTRICTION_LAST + \
+                                IORING_REGISTER_LAST + IORING_OP_LAST)
  
  struct io_uring {
        u32 head ____cacheline_aligned_in_smp;
@@@ -187,6 -191,7 +191,7 @@@ struct io_mapped_ubuf 
        size_t          len;
        struct          bio_vec *bvec;
        unsigned int    nr_bvecs;
+       unsigned long   acct_pages;
  };
  
  struct fixed_file_table {
@@@ -205,7 -210,7 +210,7 @@@ struct fixed_file_data 
        struct fixed_file_table         *table;
        struct io_ring_ctx              *ctx;
  
-       struct percpu_ref               *cur_refs;
+       struct fixed_file_ref_node      *node;
        struct percpu_ref               refs;
        struct completion               done;
        struct list_head                ref_list;
@@@ -219,6 -224,27 +224,27 @@@ struct io_buffer 
        __u16 bid;
  };
  
+ struct io_restriction {
+       DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
+       DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
+       u8 sqe_flags_allowed;
+       u8 sqe_flags_required;
+       bool registered;
+ };
+ struct io_sq_data {
+       refcount_t              refs;
+       struct mutex            lock;
+       /* ctx's that are using this sqd */
+       struct list_head        ctx_list;
+       struct list_head        ctx_new_list;
+       struct mutex            ctx_lock;
+       struct task_struct      *thread;
+       struct wait_queue_head  wait;
+ };
  struct io_ring_ctx {
        struct {
                struct percpu_ref       refs;
                unsigned int            cq_overflow_flushed: 1;
                unsigned int            drain_next: 1;
                unsigned int            eventfd_async: 1;
+               unsigned int            restricted: 1;
  
                /*
                 * Ring buffer of indices into array of io_uring_sqe, which is
  
        /* IO offload */
        struct io_wq            *io_wq;
-       struct task_struct      *sqo_thread;    /* if using sq thread polling */
-       struct mm_struct        *sqo_mm;
-       wait_queue_head_t       sqo_wait;
+       /*
+        * For SQPOLL usage - we hold a reference to the parent task, so we
+        * have access to the ->files
+        */
+       struct task_struct      *sqo_task;
+       /* Only used for accounting purposes */
+       struct mm_struct        *mm_account;
+ #ifdef CONFIG_BLK_CGROUP
+       struct cgroup_subsys_state      *sqo_blkcg_css;
+ #endif
+       struct io_sq_data       *sq_data;       /* if using sq thread polling */
+       struct wait_queue_head  sqo_sq_wait;
+       struct wait_queue_entry sqo_wait_entry;
+       struct list_head        sqd_list;
  
        /*
         * If used, fixed file set. Writers must ensure that ->refs is dead,
         */
        struct fixed_file_data  *file_data;
        unsigned                nr_user_files;
-       int                     ring_fd;
-       struct file             *ring_file;
  
        /* if used, fixed mapped user buffers */
        unsigned                nr_user_bufs;
        struct llist_head               file_put_llist;
  
        struct work_struct              exit_work;
+       struct io_restriction           restrictions;
  };
  
  /*
@@@ -392,13 -434,16 +434,16 @@@ struct io_cancel 
  
  struct io_timeout {
        struct file                     *file;
-       u64                             addr;
-       int                             flags;
        u32                             off;
        u32                             target_seq;
        struct list_head                list;
  };
  
+ struct io_timeout_rem {
+       struct file                     *file;
+       u64                             addr;
+ };
  struct io_rw {
        /* NOTE: kiocb has the file as the first member, so don't do it here */
        struct kiocb                    kiocb;
@@@ -514,15 -559,6 +559,6 @@@ struct io_async_rw 
        struct wait_page_queue          wpq;
  };
  
- struct io_async_ctx {
-       union {
-               struct io_async_rw      rw;
-               struct io_async_msghdr  msg;
-               struct io_async_connect connect;
-               struct io_timeout_data  timeout;
-       };
- };
  enum {
        REQ_F_FIXED_FILE_BIT    = IOSQE_FIXED_FILE_BIT,
        REQ_F_IO_DRAIN_BIT      = IOSQE_IO_DRAIN_BIT,
        REQ_F_BUFFER_SELECTED_BIT,
        REQ_F_NO_FILE_TABLE_BIT,
        REQ_F_WORK_INITIALIZED_BIT,
-       REQ_F_TASK_PINNED_BIT,
  
        /* not a real bit, just to check we're not overflowing the space */
        __REQ_F_LAST_BIT,
@@@ -590,8 -625,6 +625,6 @@@ enum 
        REQ_F_NO_FILE_TABLE     = BIT(REQ_F_NO_FILE_TABLE_BIT),
        /* io_wq_work is initialized */
        REQ_F_WORK_INITIALIZED  = BIT(REQ_F_WORK_INITIALIZED_BIT),
-       /* req->task is refcounted */
-       REQ_F_TASK_PINNED       = BIT(REQ_F_TASK_PINNED_BIT),
  };
  
  struct async_poll {
@@@ -614,6 -647,7 +647,7 @@@ struct io_kiocb 
                struct io_sync          sync;
                struct io_cancel        cancel;
                struct io_timeout       timeout;
+               struct io_timeout_rem   timeout_rem;
                struct io_connect       connect;
                struct io_sr_msg        sr_msg;
                struct io_open          open;
                struct io_completion    compl;
        };
  
-       struct io_async_ctx             *io;
+       /* opcode allocated if it needs to store data for async defer */
+       void                            *async_data;
        u8                              opcode;
        /* polled IO has completed */
        u8                              iopoll_completed;
@@@ -697,8 -732,6 +732,6 @@@ struct io_submit_state 
  };
  
  struct io_op_def {
-       /* needs req->io allocated for deferral/async */
-       unsigned                async_ctx : 1;
        /* needs current->mm setup, does mm access */
        unsigned                needs_mm : 1;
        /* needs req->file assigned */
        unsigned                pollout : 1;
        /* op supports buffer selection */
        unsigned                buffer_select : 1;
+       /* needs rlimit(RLIMIT_FSIZE) assigned */
        unsigned                needs_fsize : 1;
+       /* must always have async data allocated */
+       unsigned                needs_async_data : 1;
+       /* needs blkcg context, issues async io potentially */
+       unsigned                needs_blkcg : 1;
+       /* size of async data needed, if any */
+       unsigned short          async_size;
  };
  
- static const struct io_op_def io_op_defs[] = {
+ static const struct io_op_def io_op_defs[] __read_mostly = {
        [IORING_OP_NOP] = {},
        [IORING_OP_READV] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .pollin                 = 1,
                .buffer_select          = 1,
+               .needs_async_data       = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_WRITEV] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
                .needs_file             = 1,
                .hash_reg_file          = 1,
                .unbound_nonreg_file    = 1,
                .pollout                = 1,
                .needs_fsize            = 1,
+               .needs_async_data       = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_FSYNC] = {
                .needs_file             = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_READ_FIXED] = {
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .pollin                 = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_WRITE_FIXED] = {
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .pollout                = 1,
                .needs_fsize            = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_POLL_ADD] = {
                .needs_file             = 1,
        [IORING_OP_POLL_REMOVE] = {},
        [IORING_OP_SYNC_FILE_RANGE] = {
                .needs_file             = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_SENDMSG] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .needs_fs               = 1,
                .pollout                = 1,
+               .needs_async_data       = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_msghdr),
        },
        [IORING_OP_RECVMSG] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .needs_fs               = 1,
                .pollin                 = 1,
                .buffer_select          = 1,
+               .needs_async_data       = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_msghdr),
        },
        [IORING_OP_TIMEOUT] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
+               .needs_async_data       = 1,
+               .async_size             = sizeof(struct io_timeout_data),
        },
        [IORING_OP_TIMEOUT_REMOVE] = {},
        [IORING_OP_ACCEPT] = {
        },
        [IORING_OP_ASYNC_CANCEL] = {},
        [IORING_OP_LINK_TIMEOUT] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
+               .needs_async_data       = 1,
+               .async_size             = sizeof(struct io_timeout_data),
        },
        [IORING_OP_CONNECT] = {
-               .async_ctx              = 1,
                .needs_mm               = 1,
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .pollout                = 1,
+               .needs_async_data       = 1,
+               .async_size             = sizeof(struct io_async_connect),
        },
        [IORING_OP_FALLOCATE] = {
                .needs_file             = 1,
                .needs_fsize            = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_OPENAT] = {
                .file_table             = 1,
                .needs_fs               = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_CLOSE] = {
                .needs_file             = 1,
                .needs_file_no_error    = 1,
                .file_table             = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_FILES_UPDATE] = {
                .needs_mm               = 1,
                .needs_mm               = 1,
                .needs_fs               = 1,
                .file_table             = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_READ] = {
                .needs_mm               = 1,
                .unbound_nonreg_file    = 1,
                .pollin                 = 1,
                .buffer_select          = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_WRITE] = {
                .needs_mm               = 1,
                .unbound_nonreg_file    = 1,
                .pollout                = 1,
                .needs_fsize            = 1,
+               .needs_blkcg            = 1,
+               .async_size             = sizeof(struct io_async_rw),
        },
        [IORING_OP_FADVISE] = {
                .needs_file             = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_MADVISE] = {
                .needs_mm               = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_SEND] = {
                .needs_mm               = 1,
                .needs_file             = 1,
                .unbound_nonreg_file    = 1,
                .pollout                = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_RECV] = {
                .needs_mm               = 1,
                .unbound_nonreg_file    = 1,
                .pollin                 = 1,
                .buffer_select          = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_OPENAT2] = {
                .file_table             = 1,
                .needs_fs               = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_EPOLL_CTL] = {
                .unbound_nonreg_file    = 1,
                .needs_file             = 1,
                .hash_reg_file          = 1,
                .unbound_nonreg_file    = 1,
+               .needs_blkcg            = 1,
        },
        [IORING_OP_PROVIDE_BUFFERS] = {},
        [IORING_OP_REMOVE_BUFFERS] = {},
@@@ -900,13 -971,10 +971,10 @@@ static void io_queue_linked_timeout(str
  static int __io_sqe_files_update(struct io_ring_ctx *ctx,
                                 struct io_uring_files_update *ip,
                                 unsigned nr_args);
- static int io_prep_work_files(struct io_kiocb *req);
  static void __io_clean_op(struct io_kiocb *req);
- static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
-                      int fd, struct file **out_file, bool fixed);
- static void __io_queue_sqe(struct io_kiocb *req,
-                          const struct io_uring_sqe *sqe,
-                          struct io_comp_state *cs);
+ static struct file *io_file_get(struct io_submit_state *state,
+                               struct io_kiocb *req, int fd, bool fixed);
+ static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs);
  static void io_file_put_work(struct work_struct *work);
  
  static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
@@@ -918,7 -986,7 +986,7 @@@ static int io_setup_async_rw(struct io_
  
  static struct kmem_cache *req_cachep;
  
- static const struct file_operations io_uring_fops;
+ static const struct file_operations io_uring_fops __read_mostly;
  
  struct sock *io_uring_get_socket(struct file *file)
  {
  }
  EXPORT_SYMBOL(io_uring_get_socket);
  
- static void io_get_req_task(struct io_kiocb *req)
- {
-       if (req->flags & REQ_F_TASK_PINNED)
-               return;
-       get_task_struct(req->task);
-       req->flags |= REQ_F_TASK_PINNED;
- }
  static inline void io_clean_op(struct io_kiocb *req)
  {
        if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED |
                __io_clean_op(req);
  }
  
- /* not idempotent -- it doesn't clear REQ_F_TASK_PINNED */
- static void __io_put_req_task(struct io_kiocb *req)
- {
-       if (req->flags & REQ_F_TASK_PINNED)
-               put_task_struct(req->task);
- }
  static void io_sq_thread_drop_mm(void)
  {
        struct mm_struct *mm = current->mm;
@@@ -969,9 -1022,10 +1022,10 @@@ static int __io_sq_thread_acquire_mm(st
  {
        if (!current->mm) {
                if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL) ||
-                            !mmget_not_zero(ctx->sqo_mm)))
+                            !ctx->sqo_task->mm ||
+                            !mmget_not_zero(ctx->sqo_task->mm)))
                        return -EFAULT;
-               kthread_use_mm(ctx->sqo_mm);
+               kthread_use_mm(ctx->sqo_task->mm);
        }
  
        return 0;
@@@ -985,6 -1039,26 +1039,26 @@@ static int io_sq_thread_acquire_mm(stru
        return __io_sq_thread_acquire_mm(ctx);
  }
  
+ static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
+                                        struct cgroup_subsys_state **cur_css)
+ {
+ #ifdef CONFIG_BLK_CGROUP
+       /* puts the old one when swapping */
+       if (*cur_css != ctx->sqo_blkcg_css) {
+               kthread_associate_blkcg(ctx->sqo_blkcg_css);
+               *cur_css = ctx->sqo_blkcg_css;
+       }
+ #endif
+ }
+ static void io_sq_thread_unassociate_blkcg(void)
+ {
+ #ifdef CONFIG_BLK_CGROUP
+       kthread_associate_blkcg(NULL);
+ #endif
+ }
  static inline void req_set_fail_links(struct io_kiocb *req)
  {
        if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
@@@ -1054,7 -1128,8 +1128,8 @@@ static struct io_ring_ctx *io_ring_ctx_
                goto err;
  
        ctx->flags = p->flags;
-       init_waitqueue_head(&ctx->sqo_wait);
+       init_waitqueue_head(&ctx->sqo_sq_wait);
+       INIT_LIST_HEAD(&ctx->sqd_list);
        init_waitqueue_head(&ctx->cq_wait);
        INIT_LIST_HEAD(&ctx->cq_overflow_list);
        init_completion(&ctx->ref_comp);
@@@ -1121,6 -1196,10 +1196,10 @@@ static bool io_req_clean_work(struct io
                mmdrop(req->work.mm);
                req->work.mm = NULL;
        }
+ #ifdef CONFIG_BLK_CGROUP
+       if (req->work.blkcg_css)
+               css_put(req->work.blkcg_css);
+ #endif
        if (req->work.creds) {
                put_cred(req->work.creds);
                req->work.creds = NULL;
  static void io_prep_async_work(struct io_kiocb *req)
  {
        const struct io_op_def *def = &io_op_defs[req->opcode];
+       struct io_ring_ctx *ctx = req->ctx;
  
        io_req_init_async(req);
  
        if (req->flags & REQ_F_ISREG) {
-               if (def->hash_reg_file || (req->ctx->flags & IORING_SETUP_IOPOLL))
+               if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
                        io_wq_hash_work(&req->work, file_inode(req->file));
        } else {
                if (def->unbound_nonreg_file)
                        req->work.flags |= IO_WQ_WORK_UNBOUND;
        }
+       if (!req->work.files && io_op_defs[req->opcode].file_table &&
+           !(req->flags & REQ_F_NO_FILE_TABLE)) {
+               req->work.files = get_files_struct(current);
+               get_nsproxy(current->nsproxy);
+               req->work.nsproxy = current->nsproxy;
+               req->flags |= REQ_F_INFLIGHT;
+               spin_lock_irq(&ctx->inflight_lock);
+               list_add(&req->inflight_entry, &ctx->inflight_list);
+               spin_unlock_irq(&ctx->inflight_lock);
+       }
        if (!req->work.mm && def->needs_mm) {
                mmgrab(current->mm);
                req->work.mm = current->mm;
        }
+ #ifdef CONFIG_BLK_CGROUP
+       if (!req->work.blkcg_css && def->needs_blkcg) {
+               rcu_read_lock();
+               req->work.blkcg_css = blkcg_css();
+               /*
+                * This should be rare, either the cgroup is dying or the task
+                * is moving cgroups. Just punt to root for the handful of ios.
+                */
+               if (!css_tryget_online(req->work.blkcg_css))
+                       req->work.blkcg_css = NULL;
+               rcu_read_unlock();
+       }
+ #endif
        if (!req->work.creds)
                req->work.creds = get_current_cred();
        if (!req->work.fs && def->needs_fs) {
@@@ -1213,9 -1317,10 +1317,10 @@@ static void io_queue_async_work(struct 
  
  static void io_kill_timeout(struct io_kiocb *req)
  {
+       struct io_timeout_data *io = req->async_data;
        int ret;
  
-       ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
+       ret = hrtimer_try_to_cancel(&io->timer);
        if (ret != -1) {
                atomic_set(&req->ctx->cq_timeouts,
                        atomic_read(&req->ctx->cq_timeouts) + 1);
        }
  }
  
- static void io_kill_timeouts(struct io_ring_ctx *ctx)
+ static bool io_task_match(struct io_kiocb *req, struct task_struct *tsk)
+ {
+       struct io_ring_ctx *ctx = req->ctx;
+       if (!tsk || req->task == tsk)
+               return true;
+       if (ctx->flags & IORING_SETUP_SQPOLL) {
+               if (ctx->sq_data && req->task == ctx->sq_data->thread)
+                       return true;
+       }
+       return false;
+ }
+ /*
+  * Returns true if we found and killed one or more timeouts
+  */
+ static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk)
  {
        struct io_kiocb *req, *tmp;
+       int canceled = 0;
  
        spin_lock_irq(&ctx->completion_lock);
-       list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list)
-               io_kill_timeout(req);
+       list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
+               if (io_task_match(req, tsk)) {
+                       io_kill_timeout(req);
+                       canceled++;
+               }
+       }
        spin_unlock_irq(&ctx->completion_lock);
+       return canceled != 0;
  }
  
  static void __io_queue_deferred(struct io_ring_ctx *ctx)
@@@ -1284,6 -1411,13 +1411,13 @@@ static void io_commit_cqring(struct io_
                __io_queue_deferred(ctx);
  }
  
+ static inline bool io_sqring_full(struct io_ring_ctx *ctx)
+ {
+       struct io_rings *r = ctx->rings;
+       return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
+ }
  static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
  {
        struct io_rings *rings = ctx->rings;
@@@ -1317,8 -1451,8 +1451,8 @@@ static void io_cqring_ev_posted(struct 
  {
        if (waitqueue_active(&ctx->wait))
                wake_up(&ctx->wait);
-       if (waitqueue_active(&ctx->sqo_wait))
-               wake_up(&ctx->sqo_wait);
+       if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
+               wake_up(&ctx->sq_data->wait);
        if (io_should_trigger_evfd(ctx))
                eventfd_signal(ctx->cq_ev_fd, 1);
  }
@@@ -1332,12 -1466,24 +1466,24 @@@ static void io_cqring_mark_overflow(str
        }
  }
  
+ static inline bool io_match_files(struct io_kiocb *req,
+                                      struct files_struct *files)
+ {
+       if (!files)
+               return true;
+       if (req->flags & REQ_F_WORK_INITIALIZED)
+               return req->work.files == files;
+       return false;
+ }
  /* Returns true if there are no backlogged entries after the flush */
- static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
+ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
+                                    struct task_struct *tsk,
+                                    struct files_struct *files)
  {
        struct io_rings *rings = ctx->rings;
+       struct io_kiocb *req, *tmp;
        struct io_uring_cqe *cqe;
-       struct io_kiocb *req;
        unsigned long flags;
        LIST_HEAD(list);
  
                ctx->cq_overflow_flushed = 1;
  
        cqe = NULL;
-       while (!list_empty(&ctx->cq_overflow_list)) {
+       list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
+               if (tsk && req->task != tsk)
+                       continue;
+               if (!io_match_files(req, files))
+                       continue;
                cqe = io_get_cqring(ctx);
                if (!cqe && !force)
                        break;
  
-               req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
-                                               compl.list);
                list_move(&req->compl.list, &list);
                if (cqe) {
                        WRITE_ONCE(cqe->user_data, req->user_data);
@@@ -1406,7 -1555,12 +1555,12 @@@ static void __io_cqring_fill_event(stru
                WRITE_ONCE(cqe->user_data, req->user_data);
                WRITE_ONCE(cqe->res, res);
                WRITE_ONCE(cqe->flags, cflags);
-       } else if (ctx->cq_overflow_flushed) {
+       } else if (ctx->cq_overflow_flushed || req->task->io_uring->in_idle) {
+               /*
+                * If we're in ring overflow flush mode, or in task cancel mode,
+                * then we cannot store the request for later flushing, we need
+                * to drop it on the floor.
+                */
                WRITE_ONCE(ctx->rings->cq_overflow,
                                atomic_inc_return(&ctx->cached_cq_overflow));
        } else {
@@@ -1509,10 -1663,8 +1663,8 @@@ static struct io_kiocb *io_get_fallback
  static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx,
                                     struct io_submit_state *state)
  {
-       gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
-       struct io_kiocb *req;
        if (!state->free_reqs) {
+               gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
                size_t sz;
                int ret;
  
                                goto fallback;
                        ret = 1;
                }
-               state->free_reqs = ret - 1;
-               req = state->reqs[ret - 1];
-       } else {
-               state->free_reqs--;
-               req = state->reqs[state->free_reqs];
+               state->free_reqs = ret;
        }
  
-       return req;
+       state->free_reqs--;
+       return state->reqs[state->free_reqs];
  fallback:
        return io_get_fallback_req(ctx);
  }
@@@ -1554,8 -1703,8 +1703,8 @@@ static bool io_dismantle_req(struct io_
  {
        io_clean_op(req);
  
-       if (req->io)
-               kfree(req->io);
+       if (req->async_data)
+               kfree(req->async_data);
        if (req->file)
                io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
  
  
  static void __io_free_req_finish(struct io_kiocb *req)
  {
+       struct io_uring_task *tctx = req->task->io_uring;
        struct io_ring_ctx *ctx = req->ctx;
  
-       __io_put_req_task(req);
+       atomic_long_inc(&tctx->req_complete);
+       if (tctx->in_idle)
+               wake_up(&tctx->wait);
+       put_task_struct(req->task);
        if (likely(!io_is_fallback_req(req)))
                kmem_cache_free(req_cachep, req);
        else
@@@ -1609,10 -1763,11 +1763,11 @@@ static void __io_free_req(struct io_kio
  
  static bool io_link_cancel_timeout(struct io_kiocb *req)
  {
+       struct io_timeout_data *io = req->async_data;
        struct io_ring_ctx *ctx = req->ctx;
        int ret;
  
-       ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
+       ret = hrtimer_try_to_cancel(&io->timer);
        if (ret != -1) {
                io_cqring_fill_event(req, -ECANCELED);
                io_commit_cqring(ctx);
@@@ -1746,8 -1901,7 +1901,7 @@@ static struct io_kiocb *io_req_find_nex
        return __io_req_find_next(req);
  }
  
- static int io_req_task_work_add(struct io_kiocb *req, struct callback_head *cb,
-                               bool twa_signal_ok)
+ static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok)
  {
        struct task_struct *tsk = req->task;
        struct io_ring_ctx *ctx = req->ctx;
        if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok)
                notify = TWA_SIGNAL;
  
-       ret = task_work_add(tsk, cb, notify);
+       ret = task_work_add(tsk, &req->task_work, notify);
        if (!ret)
                wake_up_process(tsk);
  
@@@ -1802,7 -1956,7 +1956,7 @@@ static void __io_req_task_submit(struc
  
        if (!__io_sq_thread_acquire_mm(ctx)) {
                mutex_lock(&ctx->uring_lock);
-               __io_queue_sqe(req, NULL, NULL);
+               __io_queue_sqe(req, NULL);
                mutex_unlock(&ctx->uring_lock);
        } else {
                __io_req_task_cancel(req, -EFAULT);
@@@ -1825,7 -1979,7 +1979,7 @@@ static void io_req_task_queue(struct io
        init_task_work(&req->task_work, io_req_task_submit);
        percpu_ref_get(&req->ctx->refs);
  
-       ret = io_req_task_work_add(req, &req->task_work, true);
+       ret = io_req_task_work_add(req, true);
        if (unlikely(ret)) {
                struct task_struct *tsk;
  
@@@ -1879,6 -2033,7 +2033,7 @@@ static void io_req_free_batch_finish(st
        if (rb->to_free)
                __io_req_free_batch_flush(ctx, rb);
        if (rb->task) {
+               atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
                put_task_struct_many(rb->task, rb->task_refs);
                rb->task = NULL;
        }
@@@ -1893,16 -2048,15 +2048,15 @@@ static void io_req_free_batch(struct re
        if (req->flags & REQ_F_LINK_HEAD)
                io_queue_next(req);
  
-       if (req->flags & REQ_F_TASK_PINNED) {
-               if (req->task != rb->task) {
-                       if (rb->task)
-                               put_task_struct_many(rb->task, rb->task_refs);
-                       rb->task = req->task;
-                       rb->task_refs = 0;
+       if (req->task != rb->task) {
+               if (rb->task) {
+                       atomic_long_add(rb->task_refs, &rb->task->io_uring->req_complete);
+                       put_task_struct_many(rb->task, rb->task_refs);
                }
-               rb->task_refs++;
-               req->flags &= ~REQ_F_TASK_PINNED;
+               rb->task = req->task;
+               rb->task_refs = 0;
        }
+       rb->task_refs++;
  
        WARN_ON_ONCE(io_dismantle_req(req));
        rb->reqs[rb->to_free++] = req;
@@@ -1978,7 -2132,7 +2132,7 @@@ static unsigned io_cqring_events(struc
                if (noflush && !list_empty(&ctx->cq_overflow_list))
                        return -1U;
  
-               io_cqring_overflow_flush(ctx, false);
+               io_cqring_overflow_flush(ctx, false, NULL, NULL);
        }
  
        /* See comment at the top of this file */
@@@ -2294,7 -2448,7 +2448,7 @@@ static bool io_resubmit_prep(struct io_
                goto end_req;
        }
  
-       if (!req->io) {
+       if (!req->async_data) {
                ret = io_import_iovec(rw, req, &iovec, &iter, false);
                if (ret < 0)
                        goto end_req;
@@@ -2401,8 -2555,8 +2555,8 @@@ static void io_iopoll_req_issued(struc
                list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
  
        if ((ctx->flags & IORING_SETUP_SQPOLL) &&
-           wq_has_sleeper(&ctx->sqo_wait))
-               wake_up(&ctx->sqo_wait);
+           wq_has_sleeper(&ctx->sq_data->wait))
+               wake_up(&ctx->sq_data->wait);
  }
  
  static void __io_state_file_put(struct io_submit_state *state)
@@@ -2431,7 -2585,6 +2585,6 @@@ static struct file *__io_file_get(struc
        if (state->file) {
                if (state->fd == fd) {
                        state->has_refs--;
-                       state->ios_left--;
                        return state->file;
                }
                __io_state_file_put(state);
                return NULL;
  
        state->fd = fd;
-       state->ios_left--;
-       state->has_refs = state->ios_left;
+       state->has_refs = state->ios_left - 1;
        return state->file;
  }
  
@@@ -2491,8 -2643,7 +2643,7 @@@ static bool io_file_supports_async(stru
        return file->f_op->write_iter != NULL;
  }
  
- static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                     bool force_nonblock)
+ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
        struct io_ring_ctx *ctx = req->ctx;
        struct kiocb *kiocb = &req->rw.kiocb;
        if (kiocb->ki_flags & IOCB_NOWAIT)
                req->flags |= REQ_F_NOWAIT;
  
-       if (kiocb->ki_flags & IOCB_DIRECT)
-               io_get_req_task(req);
-       if (force_nonblock)
-               kiocb->ki_flags |= IOCB_NOWAIT;
        if (ctx->flags & IORING_SETUP_IOPOLL) {
                if (!(kiocb->ki_flags & IOCB_DIRECT) ||
                    !kiocb->ki_filp->f_op->iopoll)
                kiocb->ki_flags |= IOCB_HIPRI;
                kiocb->ki_complete = io_complete_rw_iopoll;
                req->iopoll_completed = 0;
-               io_get_req_task(req);
        } else {
                if (kiocb->ki_flags & IOCB_HIPRI)
                        return -EINVAL;
@@@ -2579,13 -2723,14 +2723,14 @@@ static void kiocb_done(struct kiocb *ki
                       struct io_comp_state *cs)
  {
        struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
+       struct io_async_rw *io = req->async_data;
  
        /* add previously done IO, if any */
-       if (req->io && req->io->rw.bytes_done > 0) {
+       if (io && io->bytes_done > 0) {
                if (ret < 0)
-                       ret = req->io->rw.bytes_done;
+                       ret = io->bytes_done;
                else
-                       ret += req->io->rw.bytes_done;
+                       ret += io->bytes_done;
        }
  
        if (req->flags & REQ_F_CUR_POS)
@@@ -2602,18 -2747,12 +2747,12 @@@ static ssize_t io_import_fixed(struct i
        struct io_ring_ctx *ctx = req->ctx;
        size_t len = req->rw.len;
        struct io_mapped_ubuf *imu;
-       u16 index, buf_index;
+       u16 index, buf_index = req->buf_index;
        size_t offset;
        u64 buf_addr;
  
-       /* attempt to use fixed buffers without having provided iovecs */
-       if (unlikely(!ctx->user_bufs))
-               return -EFAULT;
-       buf_index = req->buf_index;
        if (unlikely(buf_index >= ctx->nr_user_bufs))
                return -EFAULT;
        index = array_index_nospec(buf_index, ctx->nr_user_bufs);
        imu = &ctx->user_bufs[index];
        buf_addr = req->rw.addr;
@@@ -2852,23 -2991,30 +2991,25 @@@ static ssize_t __io_import_iovec(int rw
                return ret;
        }
  
 -#ifdef CONFIG_COMPAT
 -      if (req->ctx->compat)
 -              return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
 -                                              iovec, iter);
 -#endif
 -
 -      return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
 +      return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
 +                            req->ctx->compat);
  }
  
  static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
                               struct iovec **iovec, struct iov_iter *iter,
                               bool needs_lock)
  {
-       if (!req->io)
+       struct io_async_rw *iorw = req->async_data;
+       if (!iorw)
                return __io_import_iovec(rw, req, iovec, iter, needs_lock);
        *iovec = NULL;
-       return iov_iter_count(&req->io->rw.iter);
+       return iov_iter_count(&iorw->iter);
  }
  
  static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
  {
-       return kiocb->ki_filp->f_mode & FMODE_STREAM ? NULL : &kiocb->ki_pos;
+       return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
  }
  
  /*
@@@ -2932,10 -3078,10 +3073,10 @@@ static ssize_t loop_rw_iter(int rw, str
  static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
                          const struct iovec *fast_iov, struct iov_iter *iter)
  {
-       struct io_async_rw *rw = &req->io->rw;
+       struct io_async_rw *rw = req->async_data;
  
        memcpy(&rw->iter, iter, sizeof(*iter));
-       rw->free_iovec = NULL;
+       rw->free_iovec = iovec;
        rw->bytes_done = 0;
        /* can only be fixed buffers, no need to do anything */
        if (iter->type == ITER_BVEC)
                        memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
                               sizeof(struct iovec) * iter->nr_segs);
        } else {
-               rw->free_iovec = iovec;
                req->flags |= REQ_F_NEED_CLEANUP;
        }
  }
  
- static inline int __io_alloc_async_ctx(struct io_kiocb *req)
+ static inline int __io_alloc_async_data(struct io_kiocb *req)
  {
-       req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
-       return req->io == NULL;
+       WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
+       req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
+       return req->async_data == NULL;
  }
  
- static int io_alloc_async_ctx(struct io_kiocb *req)
+ static int io_alloc_async_data(struct io_kiocb *req)
  {
-       if (!io_op_defs[req->opcode].async_ctx)
+       if (!io_op_defs[req->opcode].needs_async_data)
                return 0;
  
-       return  __io_alloc_async_ctx(req);
+       return  __io_alloc_async_data(req);
  }
  
  static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
                             const struct iovec *fast_iov,
                             struct iov_iter *iter, bool force)
  {
-       if (!force && !io_op_defs[req->opcode].async_ctx)
+       if (!force && !io_op_defs[req->opcode].needs_async_data)
                return 0;
-       if (!req->io) {
-               if (__io_alloc_async_ctx(req))
+       if (!req->async_data) {
+               if (__io_alloc_async_data(req))
                        return -ENOMEM;
  
                io_req_map_rw(req, iovec, fast_iov, iter);
        return 0;
  }
  
- static inline int io_rw_prep_async(struct io_kiocb *req, int rw,
-                                  bool force_nonblock)
+ static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
  {
-       struct io_async_rw *iorw = &req->io->rw;
-       struct iovec *iov;
+       struct io_async_rw *iorw = req->async_data;
+       struct iovec *iov = iorw->fast_iov;
        ssize_t ret;
  
-       iorw->iter.iov = iov = iorw->fast_iov;
-       ret = __io_import_iovec(rw, req, &iov, &iorw->iter, !force_nonblock);
+       ret = __io_import_iovec(rw, req, &iov, &iorw->iter, false);
        if (unlikely(ret < 0))
                return ret;
  
-       iorw->iter.iov = iov;
-       io_req_map_rw(req, iorw->iter.iov, iorw->fast_iov, &iorw->iter);
+       iorw->bytes_done = 0;
+       iorw->free_iovec = iov;
+       if (iov)
+               req->flags |= REQ_F_NEED_CLEANUP;
        return 0;
  }
  
- static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                       bool force_nonblock)
+ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
        ssize_t ret;
  
-       ret = io_prep_rw(req, sqe, force_nonblock);
+       ret = io_prep_rw(req, sqe);
        if (ret)
                return ret;
  
                return -EBADF;
  
        /* either don't need iovec imported or already have it */
-       if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
+       if (!req->async_data)
                return 0;
-       return io_rw_prep_async(req, READ, force_nonblock);
+       return io_rw_prep_async(req, READ);
  }
  
  /*
@@@ -3052,7 -3197,7 +3192,7 @@@ static int io_async_buf_func(struct wai
  
        /* submit ref gets dropped, acquire a new one */
        refcount_inc(&req->refs);
-       ret = io_req_task_work_add(req, &req->task_work, true);
+       ret = io_req_task_work_add(req, true);
        if (unlikely(ret)) {
                struct task_struct *tsk;
  
   */
  static bool io_rw_should_retry(struct io_kiocb *req)
  {
-       struct wait_page_queue *wait = &req->io->rw.wpq;
+       struct io_async_rw *rw = req->async_data;
+       struct wait_page_queue *wait = &rw->wpq;
        struct kiocb *kiocb = &req->rw.kiocb;
  
        /* never retry for NOWAIT, we just complete with -EAGAIN */
        kiocb->ki_flags |= IOCB_WAITQ;
        kiocb->ki_flags &= ~IOCB_NOWAIT;
        kiocb->ki_waitq = wait;
-       io_get_req_task(req);
        return true;
  }
  
@@@ -3125,12 -3269,13 +3264,13 @@@ static int io_read(struct io_kiocb *req
        struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
        struct kiocb *kiocb = &req->rw.kiocb;
        struct iov_iter __iter, *iter = &__iter;
+       struct io_async_rw *rw = req->async_data;
        ssize_t io_size, ret, ret2;
        size_t iov_count;
        bool no_async;
  
-       if (req->io)
-               iter = &req->io->rw.iter;
+       if (rw)
+               iter = &rw->iter;
  
        ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
        if (ret < 0)
        /* Ensure we clear previously set non-block flag */
        if (!force_nonblock)
                kiocb->ki_flags &= ~IOCB_NOWAIT;
+       else
+               kiocb->ki_flags |= IOCB_NOWAIT;
  
        /* If the file doesn't support async, just async punt */
        no_async = force_nonblock && !io_file_supports_async(req->file, READ);
@@@ -3190,12 -3338,13 +3333,13 @@@ copy_iov
        }
        if (no_async)
                return -EAGAIN;
+       rw = req->async_data;
        /* it's copied and will be cleaned with ->io */
        iovec = NULL;
        /* now use our persistent iterator, if we aren't already */
-       iter = &req->io->rw.iter;
+       iter = &rw->iter;
  retry:
-       req->io->rw.bytes_done += ret;
+       rw->bytes_done += ret;
        /* if we can retry, do so with the callbacks armed */
        if (!io_rw_should_retry(req)) {
                kiocb->ki_flags &= ~IOCB_WAITQ;
@@@ -3226,12 -3375,11 +3370,11 @@@ out_free
        return ret;
  }
  
- static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                        bool force_nonblock)
+ static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
        ssize_t ret;
  
-       ret = io_prep_rw(req, sqe, force_nonblock);
+       ret = io_prep_rw(req, sqe);
        if (ret)
                return ret;
  
                return -EBADF;
  
        /* either don't need iovec imported or already have it */
-       if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
+       if (!req->async_data)
                return 0;
-       return io_rw_prep_async(req, WRITE, force_nonblock);
+       return io_rw_prep_async(req, WRITE);
  }
  
  static int io_write(struct io_kiocb *req, bool force_nonblock,
        struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
        struct kiocb *kiocb = &req->rw.kiocb;
        struct iov_iter __iter, *iter = &__iter;
+       struct io_async_rw *rw = req->async_data;
        size_t iov_count;
        ssize_t ret, ret2, io_size;
  
-       if (req->io)
-               iter = &req->io->rw.iter;
+       if (rw)
+               iter = &rw->iter;
  
        ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
        if (ret < 0)
  
        /* Ensure we clear previously set non-block flag */
        if (!force_nonblock)
-               req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
+               kiocb->ki_flags &= ~IOCB_NOWAIT;
+       else
+               kiocb->ki_flags |= IOCB_NOWAIT;
  
        /* If the file doesn't support async, just async punt */
        if (force_nonblock && !io_file_supports_async(req->file, WRITE))
@@@ -3337,10 -3488,7 +3483,7 @@@ static int __io_splice_prep(struct io_k
  {
        struct io_splice* sp = &req->splice;
        unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
-       int ret;
  
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
  
        if (unlikely(sp->flags & ~valid_flags))
                return -EINVAL;
  
-       ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
-                         (sp->flags & SPLICE_F_FD_IN_FIXED));
-       if (ret)
-               return ret;
+       sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
+                                 (sp->flags & SPLICE_F_FD_IN_FIXED));
+       if (!sp->file_in)
+               return -EBADF;
        req->flags |= REQ_F_NEED_CLEANUP;
  
        if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
@@@ -3550,8 -3698,6 +3693,6 @@@ static int io_openat_prep(struct io_kio
  
        if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
                return -EINVAL;
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        mode = READ_ONCE(sqe->len);
        flags = READ_ONCE(sqe->open_flags);
        req->open.how = build_open_how(flags, mode);
@@@ -3566,8 -3712,6 +3707,6 @@@ static int io_openat2_prep(struct io_ki
  
        if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
                return -EINVAL;
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
        how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
        len = READ_ONCE(sqe->len);
        if (len < OPEN_HOW_SIZE_VER0)
@@@ -3954,8 -4098,7 +4093,7 @@@ static int io_close_prep(struct io_kioc
                return -EBADF;
  
        req->close.fd = READ_ONCE(sqe->fd);
-       if ((req->file && req->file->f_op == &io_uring_fops) ||
-           req->close.fd == req->ctx->ring_fd)
+       if ((req->file && req->file->f_op == &io_uring_fops))
                return -EBADF;
  
        req->close.put_file = NULL;
@@@ -4032,15 -4175,18 +4170,18 @@@ static int io_sync_file_range(struct io
  static int io_setup_async_msg(struct io_kiocb *req,
                              struct io_async_msghdr *kmsg)
  {
-       if (req->io)
+       struct io_async_msghdr *async_msg = req->async_data;
+       if (async_msg)
                return -EAGAIN;
-       if (io_alloc_async_ctx(req)) {
+       if (io_alloc_async_data(req)) {
                if (kmsg->iov != kmsg->fast_iov)
                        kfree(kmsg->iov);
                return -ENOMEM;
        }
+       async_msg = req->async_data;
        req->flags |= REQ_F_NEED_CLEANUP;
-       memcpy(&req->io->msg, kmsg, sizeof(*kmsg));
+       memcpy(async_msg, kmsg, sizeof(*kmsg));
        return -EAGAIN;
  }
  
@@@ -4055,8 -4201,8 +4196,8 @@@ static int io_sendmsg_copy_hdr(struct i
  
  static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
+       struct io_async_msghdr *async_msg = req->async_data;
        struct io_sr_msg *sr = &req->sr_msg;
-       struct io_async_ctx *io = req->io;
        int ret;
  
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                sr->msg_flags |= MSG_CMSG_COMPAT;
  #endif
  
-       if (!io || req->opcode == IORING_OP_SEND)
+       if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
                return 0;
-       /* iovec is already imported */
-       if (req->flags & REQ_F_NEED_CLEANUP)
-               return 0;
-       ret = io_sendmsg_copy_hdr(req, &io->msg);
+       ret = io_sendmsg_copy_hdr(req, async_msg);
        if (!ret)
                req->flags |= REQ_F_NEED_CLEANUP;
        return ret;
@@@ -4095,9 -4237,9 +4232,9 @@@ static int io_sendmsg(struct io_kiocb *
        if (unlikely(!sock))
                return ret;
  
-       if (req->io) {
-               kmsg = &req->io->msg;
-               kmsg->msg.msg_name = &req->io->msg.addr;
+       if (req->async_data) {
+               kmsg = req->async_data;
+               kmsg->msg.msg_name = &kmsg->addr;
                /* if iov is set, it's allocated already */
                if (!kmsg->iov)
                        kmsg->iov = kmsg->fast_iov;
@@@ -4146,7 -4288,7 +4283,7 @@@ static int io_send(struct io_kiocb *req
  
        ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
        if (unlikely(ret))
-               return ret;;
+               return ret;
  
        msg.msg_name = NULL;
        msg.msg_control = NULL;
@@@ -4195,9 -4337,8 +4332,9 @@@ static int __io_recvmsg_copy_hdr(struc
                                sr->len);
                iomsg->iov = NULL;
        } else {
 -              ret = import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
 -                                      &iomsg->iov, &iomsg->msg.msg_iter);
 +              ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
 +                                   &iomsg->iov, &iomsg->msg.msg_iter,
 +                                   false);
                if (ret > 0)
                        ret = 0;
        }
@@@ -4237,9 -4378,9 +4374,9 @@@ static int __io_compat_recvmsg_copy_hdr
                sr->len = iomsg->iov[0].iov_len;
                iomsg->iov = NULL;
        } else {
 -              ret = compat_import_iovec(READ, uiov, len, UIO_FASTIOV,
 -                                              &iomsg->iov,
 -                                              &iomsg->msg.msg_iter);
 +              ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
 +                                 UIO_FASTIOV, &iomsg->iov,
 +                                 &iomsg->msg.msg_iter, true);
                if (ret < 0)
                        return ret;
        }
@@@ -4285,8 -4426,8 +4422,8 @@@ static inline unsigned int io_put_recv_
  static int io_recvmsg_prep(struct io_kiocb *req,
                           const struct io_uring_sqe *sqe)
  {
+       struct io_async_msghdr *async_msg = req->async_data;
        struct io_sr_msg *sr = &req->sr_msg;
-       struct io_async_ctx *io = req->io;
        int ret;
  
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                sr->msg_flags |= MSG_CMSG_COMPAT;
  #endif
  
-       if (!io || req->opcode == IORING_OP_RECV)
-               return 0;
-       /* iovec is already imported */
-       if (req->flags & REQ_F_NEED_CLEANUP)
+       if (!async_msg || !io_op_defs[req->opcode].needs_async_data)
                return 0;
-       ret = io_recvmsg_copy_hdr(req, &io->msg);
+       ret = io_recvmsg_copy_hdr(req, async_msg);
        if (!ret)
                req->flags |= REQ_F_NEED_CLEANUP;
        return ret;
@@@ -4327,9 -4464,9 +4460,9 @@@ static int io_recvmsg(struct io_kiocb *
        if (unlikely(!sock))
                return ret;
  
-       if (req->io) {
-               kmsg = &req->io->msg;
-               kmsg->msg.msg_name = &req->io->msg.addr;
+       if (req->async_data) {
+               kmsg = req->async_data;
+               kmsg->msg.msg_name = &kmsg->addr;
                /* if iov is set, it's allocated already */
                if (!kmsg->iov)
                        kmsg->iov = kmsg->fast_iov;
@@@ -4471,7 -4608,7 +4604,7 @@@ static int io_accept(struct io_kiocb *r
  static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
        struct io_connect *conn = &req->connect;
-       struct io_async_ctx *io = req->io;
+       struct io_async_connect *io = req->async_data;
  
        if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
                return -EINVAL;
                return 0;
  
        return move_addr_to_kernel(conn->addr, conn->addr_len,
-                                       &io->connect.address);
+                                       &io->address);
  }
  
  static int io_connect(struct io_kiocb *req, bool force_nonblock,
                      struct io_comp_state *cs)
  {
-       struct io_async_ctx __io, *io;
+       struct io_async_connect __io, *io;
        unsigned file_flags;
        int ret;
  
-       if (req->io) {
-               io = req->io;
+       if (req->async_data) {
+               io = req->async_data;
        } else {
                ret = move_addr_to_kernel(req->connect.addr,
                                                req->connect.addr_len,
-                                               &__io.connect.address);
+                                               &__io.address);
                if (ret)
                        goto out;
                io = &__io;
  
        file_flags = force_nonblock ? O_NONBLOCK : 0;
  
-       ret = __sys_connect_file(req->file, &io->connect.address,
+       ret = __sys_connect_file(req->file, &io->address,
                                        req->connect.addr_len, file_flags);
        if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
-               if (req->io)
+               if (req->async_data)
                        return -EAGAIN;
-               if (io_alloc_async_ctx(req)) {
+               if (io_alloc_async_data(req)) {
                        ret = -ENOMEM;
                        goto out;
                }
-               memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
+               io = req->async_data;
+               memcpy(req->async_data, &__io, sizeof(__io));
                return -EAGAIN;
        }
        if (ret == -ERESTARTSYS)
@@@ -4625,7 -4763,7 +4759,7 @@@ static int __io_async_wake(struct io_ki
         * of executing it. We can't safely execute it anyway, as we may not
         * have the needed state needed for it anyway.
         */
-       ret = io_req_task_work_add(req, &req->task_work, twa_signal_ok);
+       ret = io_req_task_work_add(req, twa_signal_ok);
        if (unlikely(ret)) {
                struct task_struct *tsk;
  
@@@ -4659,9 -4797,9 +4793,9 @@@ static bool io_poll_rewait(struct io_ki
  
  static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
  {
-       /* pure poll stashes this in ->io, poll driven retry elsewhere */
+       /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
        if (req->opcode == IORING_OP_POLL_ADD)
-               return (struct io_poll_iocb *) req->io;
+               return req->async_data;
        return req->apoll->double_poll;
  }
  
@@@ -4938,7 -5076,6 +5072,6 @@@ static bool io_arm_poll_handler(struct 
        apoll->double_poll = NULL;
  
        req->flags |= REQ_F_POLLED;
-       io_get_req_task(req);
        req->apoll = apoll;
        INIT_HLIST_NODE(&req->hash_node);
  
@@@ -5013,7 -5150,10 +5146,10 @@@ static bool io_poll_remove_one(struct i
        return do_complete;
  }
  
- static void io_poll_remove_all(struct io_ring_ctx *ctx)
+ /*
+  * Returns true if we found and killed one or more poll requests
+  */
+ static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk)
  {
        struct hlist_node *tmp;
        struct io_kiocb *req;
                struct hlist_head *list;
  
                list = &ctx->cancel_hash[i];
-               hlist_for_each_entry_safe(req, tmp, list, hash_node)
-                       posted += io_poll_remove_one(req);
+               hlist_for_each_entry_safe(req, tmp, list, hash_node) {
+                       if (io_task_match(req, tsk))
+                               posted += io_poll_remove_one(req);
+               }
        }
        spin_unlock_irq(&ctx->completion_lock);
  
        if (posted)
                io_cqring_ev_posted(ctx);
+       return posted != 0;
  }
  
  static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
@@@ -5098,7 -5242,7 +5238,7 @@@ static void io_poll_queue_proc(struct f
  {
        struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
  
-       __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->io);
+       __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
  }
  
  static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  #endif
        poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
                       (events & EPOLLEXCLUSIVE);
-       io_get_req_task(req);
        return 0;
  }
  
@@@ -5159,16 -5301,10 +5297,10 @@@ static enum hrtimer_restart io_timeout_
        unsigned long flags;
  
        spin_lock_irqsave(&ctx->completion_lock, flags);
+       list_del_init(&req->timeout.list);
        atomic_set(&req->ctx->cq_timeouts,
                atomic_read(&req->ctx->cq_timeouts) + 1);
  
-       /*
-        * We could be racing with timeout deletion. If the list is empty,
-        * then timeout lookup already found it and will be handling it.
-        */
-       if (!list_empty(&req->timeout.list))
-               list_del_init(&req->timeout.list);
        io_cqring_fill_event(req, -ETIME);
        io_commit_cqring(ctx);
        spin_unlock_irqrestore(&ctx->completion_lock, flags);
  
  static int __io_timeout_cancel(struct io_kiocb *req)
  {
+       struct io_timeout_data *io = req->async_data;
        int ret;
  
-       list_del_init(&req->timeout.list);
-       ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
+       ret = hrtimer_try_to_cancel(&io->timer);
        if (ret == -1)
                return -EALREADY;
+       list_del_init(&req->timeout.list);
  
        req_set_fail_links(req);
        req->flags |= REQ_F_COMP_LOCKED;
@@@ -5221,14 -5357,10 +5353,10 @@@ static int io_timeout_remove_prep(struc
                return -EINVAL;
        if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
                return -EINVAL;
-       if (sqe->ioprio || sqe->buf_index || sqe->len)
-               return -EINVAL;
-       req->timeout.addr = READ_ONCE(sqe->addr);
-       req->timeout.flags = READ_ONCE(sqe->timeout_flags);
-       if (req->timeout.flags)
+       if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->timeout_flags)
                return -EINVAL;
  
+       req->timeout_rem.addr = READ_ONCE(sqe->addr);
        return 0;
  }
  
@@@ -5241,7 -5373,7 +5369,7 @@@ static int io_timeout_remove(struct io_
        int ret;
  
        spin_lock_irq(&ctx->completion_lock);
-       ret = io_timeout_cancel(ctx, req->timeout.addr);
+       ret = io_timeout_cancel(ctx, req->timeout_rem.addr);
  
        io_cqring_fill_event(req, ret);
        io_commit_cqring(ctx);
@@@ -5272,10 -5404,10 +5400,10 @@@ static int io_timeout_prep(struct io_ki
  
        req->timeout.off = off;
  
-       if (!req->io && io_alloc_async_ctx(req))
+       if (!req->async_data && io_alloc_async_data(req))
                return -ENOMEM;
  
-       data = &req->io->timeout;
+       data = req->async_data;
        data->req = req;
  
        if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
  static int io_timeout(struct io_kiocb *req)
  {
        struct io_ring_ctx *ctx = req->ctx;
-       struct io_timeout_data *data = &req->io->timeout;
+       struct io_timeout_data *data = req->async_data;
        struct list_head *entry;
        u32 tail, off = req->timeout.off;
  
@@@ -5456,120 -5588,86 +5584,86 @@@ static int io_files_update(struct io_ki
        return 0;
  }
  
- static int io_req_defer_prep(struct io_kiocb *req,
-                            const struct io_uring_sqe *sqe)
+ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  {
-       ssize_t ret = 0;
-       if (!sqe)
-               return 0;
-       if (io_alloc_async_ctx(req))
-               return -EAGAIN;
-       ret = io_prep_work_files(req);
-       if (unlikely(ret))
-               return ret;
-       io_prep_async_work(req);
        switch (req->opcode) {
        case IORING_OP_NOP:
-               break;
+               return 0;
        case IORING_OP_READV:
        case IORING_OP_READ_FIXED:
        case IORING_OP_READ:
-               ret = io_read_prep(req, sqe, true);
-               break;
+               return io_read_prep(req, sqe);
        case IORING_OP_WRITEV:
        case IORING_OP_WRITE_FIXED:
        case IORING_OP_WRITE:
-               ret = io_write_prep(req, sqe, true);
-               break;
+               return io_write_prep(req, sqe);
        case IORING_OP_POLL_ADD:
-               ret = io_poll_add_prep(req, sqe);
-               break;
+               return io_poll_add_prep(req, sqe);
        case IORING_OP_POLL_REMOVE:
-               ret = io_poll_remove_prep(req, sqe);
-               break;
+               return io_poll_remove_prep(req, sqe);
        case IORING_OP_FSYNC:
-               ret = io_prep_fsync(req, sqe);
-               break;
+               return io_prep_fsync(req, sqe);
        case IORING_OP_SYNC_FILE_RANGE:
-               ret = io_prep_sfr(req, sqe);
-               break;
+               return io_prep_sfr(req, sqe);
        case IORING_OP_SENDMSG:
        case IORING_OP_SEND:
-               ret = io_sendmsg_prep(req, sqe);
-               break;
+               return io_sendmsg_prep(req, sqe);
        case IORING_OP_RECVMSG:
        case IORING_OP_RECV:
-               ret = io_recvmsg_prep(req, sqe);
-               break;
+               return io_recvmsg_prep(req, sqe);
        case IORING_OP_CONNECT:
-               ret = io_connect_prep(req, sqe);
-               break;
+               return io_connect_prep(req, sqe);
        case IORING_OP_TIMEOUT:
-               ret = io_timeout_prep(req, sqe, false);
-               break;
+               return io_timeout_prep(req, sqe, false);
        case IORING_OP_TIMEOUT_REMOVE:
-               ret = io_timeout_remove_prep(req, sqe);
-               break;
+               return io_timeout_remove_prep(req, sqe);
        case IORING_OP_ASYNC_CANCEL:
-               ret = io_async_cancel_prep(req, sqe);
-               break;
+               return io_async_cancel_prep(req, sqe);
        case IORING_OP_LINK_TIMEOUT:
-               ret = io_timeout_prep(req, sqe, true);
-               break;
+               return io_timeout_prep(req, sqe, true);
        case IORING_OP_ACCEPT:
-               ret = io_accept_prep(req, sqe);
-               break;
+               return io_accept_prep(req, sqe);
        case IORING_OP_FALLOCATE:
-               ret = io_fallocate_prep(req, sqe);
-               break;
+               return io_fallocate_prep(req, sqe);
        case IORING_OP_OPENAT:
-               ret = io_openat_prep(req, sqe);
-               break;
+               return io_openat_prep(req, sqe);
        case IORING_OP_CLOSE:
-               ret = io_close_prep(req, sqe);
-               break;
+               return io_close_prep(req, sqe);
        case IORING_OP_FILES_UPDATE:
-               ret = io_files_update_prep(req, sqe);
-               break;
+               return io_files_update_prep(req, sqe);
        case IORING_OP_STATX:
-               ret = io_statx_prep(req, sqe);
-               break;
+               return io_statx_prep(req, sqe);
        case IORING_OP_FADVISE:
-               ret = io_fadvise_prep(req, sqe);
-               break;
+               return io_fadvise_prep(req, sqe);
        case IORING_OP_MADVISE:
-               ret = io_madvise_prep(req, sqe);
-               break;
+               return io_madvise_prep(req, sqe);
        case IORING_OP_OPENAT2:
-               ret = io_openat2_prep(req, sqe);
-               break;
+               return io_openat2_prep(req, sqe);
        case IORING_OP_EPOLL_CTL:
-               ret = io_epoll_ctl_prep(req, sqe);
-               break;
+               return io_epoll_ctl_prep(req, sqe);
        case IORING_OP_SPLICE:
-               ret = io_splice_prep(req, sqe);
-               break;
+               return io_splice_prep(req, sqe);
        case IORING_OP_PROVIDE_BUFFERS:
-               ret = io_provide_buffers_prep(req, sqe);
-               break;
+               return io_provide_buffers_prep(req, sqe);
        case IORING_OP_REMOVE_BUFFERS:
-               ret = io_remove_buffers_prep(req, sqe);
-               break;
+               return io_remove_buffers_prep(req, sqe);
        case IORING_OP_TEE:
-               ret = io_tee_prep(req, sqe);
-               break;
-       default:
-               printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
-                               req->opcode);
-               ret = -EINVAL;
-               break;
+               return io_tee_prep(req, sqe);
        }
  
-       return ret;
+       printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
+                       req->opcode);
+       return-EINVAL;
+ }
+ static int io_req_defer_prep(struct io_kiocb *req,
+                            const struct io_uring_sqe *sqe)
+ {
+       if (!sqe)
+               return 0;
+       if (io_alloc_async_data(req))
+               return -EAGAIN;
+       return io_req_prep(req, sqe);
  }
  
  static u32 io_get_sequence(struct io_kiocb *req)
@@@ -5603,7 -5701,7 +5697,7 @@@ static int io_req_defer(struct io_kioc
        if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
                return 0;
  
-       if (!req->io) {
+       if (!req->async_data) {
                ret = io_req_defer_prep(req, sqe);
                if (ret)
                        return ret;
        return -EIOCBQUEUED;
  }
  
- static void __io_clean_op(struct io_kiocb *req)
+ static void io_req_drop_files(struct io_kiocb *req)
  {
-       struct io_async_ctx *io = req->io;
+       struct io_ring_ctx *ctx = req->ctx;
+       unsigned long flags;
+       spin_lock_irqsave(&ctx->inflight_lock, flags);
+       list_del(&req->inflight_entry);
+       if (waitqueue_active(&ctx->inflight_wait))
+               wake_up(&ctx->inflight_wait);
+       spin_unlock_irqrestore(&ctx->inflight_lock, flags);
+       req->flags &= ~REQ_F_INFLIGHT;
+       put_files_struct(req->work.files);
+       put_nsproxy(req->work.nsproxy);
+       req->work.files = NULL;
+ }
  
+ static void __io_clean_op(struct io_kiocb *req)
+ {
        if (req->flags & REQ_F_BUFFER_SELECTED) {
                switch (req->opcode) {
                case IORING_OP_READV:
                case IORING_OP_READ:
                case IORING_OP_WRITEV:
                case IORING_OP_WRITE_FIXED:
-               case IORING_OP_WRITE:
-                       if (io->rw.free_iovec)
-                               kfree(io->rw.free_iovec);
+               case IORING_OP_WRITE: {
+                       struct io_async_rw *io = req->async_data;
+                       if (io->free_iovec)
+                               kfree(io->free_iovec);
                        break;
+                       }
                case IORING_OP_RECVMSG:
-               case IORING_OP_SENDMSG:
-                       if (io->msg.iov != io->msg.fast_iov)
-                               kfree(io->msg.iov);
+               case IORING_OP_SENDMSG: {
+                       struct io_async_msghdr *io = req->async_data;
+                       if (io->iov != io->fast_iov)
+                               kfree(io->iov);
                        break;
+                       }
                case IORING_OP_SPLICE:
                case IORING_OP_TEE:
                        io_put_file(req, req->splice.file_in,
                req->flags &= ~REQ_F_NEED_CLEANUP;
        }
  
-       if (req->flags & REQ_F_INFLIGHT) {
-               struct io_ring_ctx *ctx = req->ctx;
-               unsigned long flags;
-               spin_lock_irqsave(&ctx->inflight_lock, flags);
-               list_del(&req->inflight_entry);
-               if (waitqueue_active(&ctx->inflight_wait))
-                       wake_up(&ctx->inflight_wait);
-               spin_unlock_irqrestore(&ctx->inflight_lock, flags);
-               req->flags &= ~REQ_F_INFLIGHT;
-       }
+       if (req->flags & REQ_F_INFLIGHT)
+               io_req_drop_files(req);
  }
  
- static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                       bool force_nonblock, struct io_comp_state *cs)
+ static int io_issue_sqe(struct io_kiocb *req, bool force_nonblock,
+                       struct io_comp_state *cs)
  {
        struct io_ring_ctx *ctx = req->ctx;
        int ret;
        case IORING_OP_READV:
        case IORING_OP_READ_FIXED:
        case IORING_OP_READ:
-               if (sqe) {
-                       ret = io_read_prep(req, sqe, force_nonblock);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_read(req, force_nonblock, cs);
                break;
        case IORING_OP_WRITEV:
        case IORING_OP_WRITE_FIXED:
        case IORING_OP_WRITE:
-               if (sqe) {
-                       ret = io_write_prep(req, sqe, force_nonblock);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_write(req, force_nonblock, cs);
                break;
        case IORING_OP_FSYNC:
-               if (sqe) {
-                       ret = io_prep_fsync(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_fsync(req, force_nonblock);
                break;
        case IORING_OP_POLL_ADD:
-               if (sqe) {
-                       ret = io_poll_add_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_poll_add(req);
                break;
        case IORING_OP_POLL_REMOVE:
-               if (sqe) {
-                       ret = io_poll_remove_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_poll_remove(req);
                break;
        case IORING_OP_SYNC_FILE_RANGE:
-               if (sqe) {
-                       ret = io_prep_sfr(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_sync_file_range(req, force_nonblock);
                break;
        case IORING_OP_SENDMSG:
+               ret = io_sendmsg(req, force_nonblock, cs);
+               break;
        case IORING_OP_SEND:
-               if (sqe) {
-                       ret = io_sendmsg_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
-               if (req->opcode == IORING_OP_SENDMSG)
-                       ret = io_sendmsg(req, force_nonblock, cs);
-               else
-                       ret = io_send(req, force_nonblock, cs);
+               ret = io_send(req, force_nonblock, cs);
                break;
        case IORING_OP_RECVMSG:
+               ret = io_recvmsg(req, force_nonblock, cs);
+               break;
        case IORING_OP_RECV:
-               if (sqe) {
-                       ret = io_recvmsg_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
-               if (req->opcode == IORING_OP_RECVMSG)
-                       ret = io_recvmsg(req, force_nonblock, cs);
-               else
-                       ret = io_recv(req, force_nonblock, cs);
+               ret = io_recv(req, force_nonblock, cs);
                break;
        case IORING_OP_TIMEOUT:
-               if (sqe) {
-                       ret = io_timeout_prep(req, sqe, false);
-                       if (ret)
-                               break;
-               }
                ret = io_timeout(req);
                break;
        case IORING_OP_TIMEOUT_REMOVE:
-               if (sqe) {
-                       ret = io_timeout_remove_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_timeout_remove(req);
                break;
        case IORING_OP_ACCEPT:
-               if (sqe) {
-                       ret = io_accept_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_accept(req, force_nonblock, cs);
                break;
        case IORING_OP_CONNECT:
-               if (sqe) {
-                       ret = io_connect_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_connect(req, force_nonblock, cs);
                break;
        case IORING_OP_ASYNC_CANCEL:
-               if (sqe) {
-                       ret = io_async_cancel_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_async_cancel(req);
                break;
        case IORING_OP_FALLOCATE:
-               if (sqe) {
-                       ret = io_fallocate_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_fallocate(req, force_nonblock);
                break;
        case IORING_OP_OPENAT:
-               if (sqe) {
-                       ret = io_openat_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_openat(req, force_nonblock);
                break;
        case IORING_OP_CLOSE:
-               if (sqe) {
-                       ret = io_close_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_close(req, force_nonblock, cs);
                break;
        case IORING_OP_FILES_UPDATE:
-               if (sqe) {
-                       ret = io_files_update_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_files_update(req, force_nonblock, cs);
                break;
        case IORING_OP_STATX:
-               if (sqe) {
-                       ret = io_statx_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_statx(req, force_nonblock);
                break;
        case IORING_OP_FADVISE:
-               if (sqe) {
-                       ret = io_fadvise_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_fadvise(req, force_nonblock);
                break;
        case IORING_OP_MADVISE:
-               if (sqe) {
-                       ret = io_madvise_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_madvise(req, force_nonblock);
                break;
        case IORING_OP_OPENAT2:
-               if (sqe) {
-                       ret = io_openat2_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_openat2(req, force_nonblock);
                break;
        case IORING_OP_EPOLL_CTL:
-               if (sqe) {
-                       ret = io_epoll_ctl_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_epoll_ctl(req, force_nonblock, cs);
                break;
        case IORING_OP_SPLICE:
-               if (sqe) {
-                       ret = io_splice_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_splice(req, force_nonblock);
                break;
        case IORING_OP_PROVIDE_BUFFERS:
-               if (sqe) {
-                       ret = io_provide_buffers_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_provide_buffers(req, force_nonblock, cs);
                break;
        case IORING_OP_REMOVE_BUFFERS:
-               if (sqe) {
-                       ret = io_remove_buffers_prep(req, sqe);
-                       if (ret)
-                               break;
-               }
                ret = io_remove_buffers(req, force_nonblock, cs);
                break;
        case IORING_OP_TEE:
-               if (sqe) {
-                       ret = io_tee_prep(req, sqe);
-                       if (ret < 0)
-                               break;
-               }
                ret = io_tee(req, force_nonblock);
                break;
        default:
@@@ -5964,7 -5939,7 +5935,7 @@@ static struct io_wq_work *io_wq_submit_
  
        if (!ret) {
                do {
-                       ret = io_issue_sqe(req, NULL, false, NULL);
+                       ret = io_issue_sqe(req, false, NULL);
                        /*
                         * We can get EAGAIN for polled IO even though we're
                         * forcing a sync submission from here, since we can't
@@@ -5993,20 -5968,19 +5964,19 @@@ static inline struct file *io_file_from
        return table->files[index & IORING_FILE_TABLE_MASK];
  }
  
- static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
-                       int fd, struct file **out_file, bool fixed)
+ static struct file *io_file_get(struct io_submit_state *state,
+                               struct io_kiocb *req, int fd, bool fixed)
  {
        struct io_ring_ctx *ctx = req->ctx;
        struct file *file;
  
        if (fixed) {
-               if (unlikely(!ctx->file_data ||
-                   (unsigned) fd >= ctx->nr_user_files))
-                       return -EBADF;
+               if (unlikely((unsigned int)fd >= ctx->nr_user_files))
+                       return NULL;
                fd = array_index_nospec(fd, ctx->nr_user_files);
                file = io_file_from_index(ctx, fd);
                if (file) {
-                       req->fixed_file_refs = ctx->file_data->cur_refs;
+                       req->fixed_file_refs = &ctx->file_data->node->refs;
                        percpu_ref_get(req->fixed_file_refs);
                }
        } else {
                file = __io_file_get(state, fd);
        }
  
-       if (file || io_op_defs[req->opcode].needs_file_no_error) {
-               *out_file = file;
-               return 0;
-       }
-       return -EBADF;
+       return file;
  }
  
  static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
        if (unlikely(!fixed && io_async_submit(req->ctx)))
                return -EBADF;
  
-       return io_file_get(state, req, fd, &req->file, fixed);
- }
- static int io_grab_files(struct io_kiocb *req)
- {
-       int ret = -EBADF;
-       struct io_ring_ctx *ctx = req->ctx;
-       io_req_init_async(req);
-       if (req->work.files || (req->flags & REQ_F_NO_FILE_TABLE))
+       req->file = io_file_get(state, req, fd, fixed);
+       if (req->file || io_op_defs[req->opcode].needs_file_no_error)
                return 0;
-       if (!ctx->ring_file)
-               return -EBADF;
-       rcu_read_lock();
-       spin_lock_irq(&ctx->inflight_lock);
-       /*
-        * We use the f_ops->flush() handler to ensure that we can flush
-        * out work accessing these files if the fd is closed. Check if
-        * the fd has changed since we started down this path, and disallow
-        * this operation if it has.
-        */
-       if (fcheck(ctx->ring_fd) == ctx->ring_file) {
-               list_add(&req->inflight_entry, &ctx->inflight_list);
-               req->flags |= REQ_F_INFLIGHT;
-               req->work.files = current->files;
-               ret = 0;
-       }
-       spin_unlock_irq(&ctx->inflight_lock);
-       rcu_read_unlock();
-       return ret;
- }
- static inline int io_prep_work_files(struct io_kiocb *req)
- {
-       if (!io_op_defs[req->opcode].file_table)
-               return 0;
-       return io_grab_files(req);
+       return -EBADF;
  }
  
  static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
@@@ -6116,7 -6050,7 +6046,7 @@@ static void __io_queue_linked_timeout(s
         * we got a chance to setup the timer
         */
        if (!list_empty(&req->link_list)) {
-               struct io_timeout_data *data = &req->io->timeout;
+               struct io_timeout_data *data = req->async_data;
  
                data->timer.function = io_link_timeout_fn;
                hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
@@@ -6154,8 -6088,7 +6084,7 @@@ static struct io_kiocb *io_prep_linked_
        return nxt;
  }
  
- static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
-                          struct io_comp_state *cs)
+ static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs)
  {
        struct io_kiocb *linked_timeout;
        struct io_kiocb *nxt;
@@@ -6175,7 -6108,7 +6104,7 @@@ again
                        old_creds = override_creds(req->work.creds);
        }
  
-       ret = io_issue_sqe(req, sqe, true, cs);
+       ret = io_issue_sqe(req, true, cs);
  
        /*
         * We async punt it if the file wasn't marked NOWAIT, or if the file
        if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
                if (!io_arm_poll_handler(req)) {
  punt:
-                       ret = io_prep_work_files(req);
-                       if (unlikely(ret))
-                               goto err;
                        /*
                         * Queued up for async execution, worker will release
                         * submit reference when the iocb is actually submitted.
        }
  
        if (unlikely(ret)) {
- err:
                /* un-prep timeout, so it'll be killed as any other linked */
                req->flags &= ~REQ_F_LINK_TIMEOUT;
                req_set_fail_links(req);
@@@ -6240,7 -6169,7 +6165,7 @@@ fail_req
                        io_req_complete(req, ret);
                }
        } else if (req->flags & REQ_F_FORCE_ASYNC) {
-               if (!req->io) {
+               if (!req->async_data) {
                        ret = io_req_defer_prep(req, sqe);
                        if (unlikely(ret))
                                goto fail_req;
                req->work.flags |= IO_WQ_WORK_CONCURRENT;
                io_queue_async_work(req);
        } else {
-               __io_queue_sqe(req, sqe, cs);
+               if (sqe) {
+                       ret = io_req_prep(req, sqe);
+                       if (unlikely(ret))
+                               goto fail_req;
+               }
+               __io_queue_sqe(req, cs);
        }
  }
  
@@@ -6302,7 -6236,6 +6232,6 @@@ static int io_submit_sqe(struct io_kioc
                        return ret;
                }
                trace_io_uring_link(ctx, req, head);
-               io_get_req_task(req);
                list_add_tail(&req->link_list, &head->link_list);
  
                /* last request of a link, enqueue the link */
@@@ -6407,6 -6340,32 +6336,32 @@@ static inline void io_consume_sqe(struc
        ctx->cached_sq_head++;
  }
  
+ /*
+  * Check SQE restrictions (opcode and flags).
+  *
+  * Returns 'true' if SQE is allowed, 'false' otherwise.
+  */
+ static inline bool io_check_restriction(struct io_ring_ctx *ctx,
+                                       struct io_kiocb *req,
+                                       unsigned int sqe_flags)
+ {
+       if (!ctx->restricted)
+               return true;
+       if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
+               return false;
+       if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
+           ctx->restrictions.sqe_flags_required)
+               return false;
+       if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
+                         ctx->restrictions.sqe_flags_required))
+               return false;
+       return true;
+ }
  #define SQE_VALID_FLAGS       (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
                                IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
                                IOSQE_BUFFER_SELECT)
@@@ -6416,11 -6375,11 +6371,11 @@@ static int io_init_req(struct io_ring_c
                       struct io_submit_state *state)
  {
        unsigned int sqe_flags;
-       int id;
+       int id, ret;
  
        req->opcode = READ_ONCE(sqe->opcode);
        req->user_data = READ_ONCE(sqe->user_data);
-       req->io = NULL;
+       req->async_data = NULL;
        req->file = NULL;
        req->ctx = ctx;
        req->flags = 0;
        if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
                return -EINVAL;
  
+       if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
+               return -EACCES;
        if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
            !io_op_defs[req->opcode].buffer_select)
                return -EOPNOTSUPP;
        if (!io_op_defs[req->opcode].needs_file)
                return 0;
  
-       return io_req_set_file(state, req, READ_ONCE(sqe->fd));
+       ret = io_req_set_file(state, req, READ_ONCE(sqe->fd));
+       state->ios_left--;
+       return ret;
  }
  
- static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
-                         struct file *ring_file, int ring_fd)
+ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
  {
        struct io_submit_state state;
        struct io_kiocb *link = NULL;
        /* if we have a backlog and couldn't flush it all, return BUSY */
        if (test_bit(0, &ctx->sq_check_overflow)) {
                if (!list_empty(&ctx->cq_overflow_list) &&
-                   !io_cqring_overflow_flush(ctx, false))
+                   !io_cqring_overflow_flush(ctx, false, NULL, NULL))
                        return -EBUSY;
        }
  
        if (!percpu_ref_tryget_many(&ctx->refs, nr))
                return -EAGAIN;
  
-       io_submit_state_start(&state, ctx, nr);
+       atomic_long_add(nr, &current->io_uring->req_issue);
+       refcount_add(nr, &current->usage);
  
-       ctx->ring_fd = ring_fd;
-       ctx->ring_file = ring_file;
+       io_submit_state_start(&state, ctx, nr);
  
        for (i = 0; i < nr; i++) {
                const struct io_uring_sqe *sqe;
                                submitted = -EAGAIN;
                        break;
                }
-               err = io_init_req(ctx, req, sqe, &state);
                io_consume_sqe(ctx);
                /* will complete beyond this point, count as submitted */
                submitted++;
  
+               err = io_init_req(ctx, req, sqe, &state);
                if (unlikely(err)) {
  fail_req:
                        io_put_req(req);
                int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
  
                percpu_ref_put_many(&ctx->refs, nr - ref_used);
+               atomic_long_sub(nr - ref_used, &current->io_uring->req_issue);
+               put_task_struct_many(current, nr - ref_used);
        }
        if (link)
                io_queue_link_head(link, &state.comp);
@@@ -6553,117 -6517,186 +6513,186 @@@ static inline void io_ring_clear_wakeup
        spin_unlock_irq(&ctx->completion_lock);
  }
  
- static int io_sq_thread(void *data)
+ static int io_sq_wake_function(struct wait_queue_entry *wqe, unsigned mode,
+                              int sync, void *key)
  {
-       struct io_ring_ctx *ctx = data;
-       const struct cred *old_cred;
-       DEFINE_WAIT(wait);
-       unsigned long timeout;
+       struct io_ring_ctx *ctx = container_of(wqe, struct io_ring_ctx, sqo_wait_entry);
+       int ret;
+       ret = autoremove_wake_function(wqe, mode, sync, key);
+       if (ret) {
+               unsigned long flags;
+               spin_lock_irqsave(&ctx->completion_lock, flags);
+               ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
+               spin_unlock_irqrestore(&ctx->completion_lock, flags);
+       }
+       return ret;
+ }
+ enum sq_ret {
+       SQT_IDLE        = 1,
+       SQT_SPIN        = 2,
+       SQT_DID_WORK    = 4,
+ };
+ static enum sq_ret __io_sq_thread(struct io_ring_ctx *ctx,
+                                 unsigned long start_jiffies, bool cap_entries)
+ {
+       unsigned long timeout = start_jiffies + ctx->sq_thread_idle;
+       struct io_sq_data *sqd = ctx->sq_data;
+       unsigned int to_submit;
        int ret = 0;
  
-       complete(&ctx->sq_thread_comp);
+ again:
+       if (!list_empty(&ctx->iopoll_list)) {
+               unsigned nr_events = 0;
  
-       old_cred = override_creds(ctx->creds);
+               mutex_lock(&ctx->uring_lock);
+               if (!list_empty(&ctx->iopoll_list) && !need_resched())
+                       io_do_iopoll(ctx, &nr_events, 0);
+               mutex_unlock(&ctx->uring_lock);
+       }
  
-       timeout = jiffies + ctx->sq_thread_idle;
-       while (!kthread_should_park()) {
-               unsigned int to_submit;
+       to_submit = io_sqring_entries(ctx);
  
-               if (!list_empty(&ctx->iopoll_list)) {
-                       unsigned nr_events = 0;
+       /*
+        * If submit got -EBUSY, flag us as needing the application
+        * to enter the kernel to reap and flush events.
+        */
+       if (!to_submit || ret == -EBUSY || need_resched()) {
+               /*
+                * Drop cur_mm before scheduling, we can't hold it for
+                * long periods (or over schedule()). Do this before
+                * adding ourselves to the waitqueue, as the unuse/drop
+                * may sleep.
+                */
+               io_sq_thread_drop_mm();
  
-                       mutex_lock(&ctx->uring_lock);
-                       if (!list_empty(&ctx->iopoll_list) && !need_resched())
-                               io_do_iopoll(ctx, &nr_events, 0);
-                       else
-                               timeout = jiffies + ctx->sq_thread_idle;
-                       mutex_unlock(&ctx->uring_lock);
+               /*
+                * We're polling. If we're within the defined idle
+                * period, then let us spin without work before going
+                * to sleep. The exception is if we got EBUSY doing
+                * more IO, we should wait for the application to
+                * reap events and wake us up.
+                */
+               if (!list_empty(&ctx->iopoll_list) || need_resched() ||
+                   (!time_after(jiffies, timeout) && ret != -EBUSY &&
+                   !percpu_ref_is_dying(&ctx->refs)))
+                       return SQT_SPIN;
+               prepare_to_wait(&sqd->wait, &ctx->sqo_wait_entry,
+                                       TASK_INTERRUPTIBLE);
+               /*
+                * While doing polled IO, before going to sleep, we need
+                * to check if there are new reqs added to iopoll_list,
+                * it is because reqs may have been punted to io worker
+                * and will be added to iopoll_list later, hence check
+                * the iopoll_list again.
+                */
+               if ((ctx->flags & IORING_SETUP_IOPOLL) &&
+                   !list_empty_careful(&ctx->iopoll_list)) {
+                       finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
+                       goto again;
                }
  
                to_submit = io_sqring_entries(ctx);
+               if (!to_submit || ret == -EBUSY)
+                       return SQT_IDLE;
+       }
+       finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
+       io_ring_clear_wakeup_flag(ctx);
+       /* if we're handling multiple rings, cap submit size for fairness */
+       if (cap_entries && to_submit > 8)
+               to_submit = 8;
+       mutex_lock(&ctx->uring_lock);
+       if (likely(!percpu_ref_is_dying(&ctx->refs)))
+               ret = io_submit_sqes(ctx, to_submit);
+       mutex_unlock(&ctx->uring_lock);
+       if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
+               wake_up(&ctx->sqo_sq_wait);
+       return SQT_DID_WORK;
+ }
+ static void io_sqd_init_new(struct io_sq_data *sqd)
+ {
+       struct io_ring_ctx *ctx;
+       while (!list_empty(&sqd->ctx_new_list)) {
+               ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
+               init_wait(&ctx->sqo_wait_entry);
+               ctx->sqo_wait_entry.func = io_sq_wake_function;
+               list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
+               complete(&ctx->sq_thread_comp);
+       }
+ }
+ static int io_sq_thread(void *data)
+ {
+       struct cgroup_subsys_state *cur_css = NULL;
+       const struct cred *old_cred = NULL;
+       struct io_sq_data *sqd = data;
+       struct io_ring_ctx *ctx;
+       unsigned long start_jiffies;
+       start_jiffies = jiffies;
+       while (!kthread_should_stop()) {
+               enum sq_ret ret = 0;
+               bool cap_entries;
  
                /*
-                * If submit got -EBUSY, flag us as needing the application
-                * to enter the kernel to reap and flush events.
+                * Any changes to the sqd lists are synchronized through the
+                * kthread parking. This synchronizes the thread vs users,
+                * the users are synchronized on the sqd->ctx_lock.
                 */
-               if (!to_submit || ret == -EBUSY || need_resched()) {
-                       /*
-                        * Drop cur_mm before scheduling, we can't hold it for
-                        * long periods (or over schedule()). Do this before
-                        * adding ourselves to the waitqueue, as the unuse/drop
-                        * may sleep.
-                        */
-                       io_sq_thread_drop_mm();
+               if (kthread_should_park())
+                       kthread_parkme();
  
-                       /*
-                        * We're polling. If we're within the defined idle
-                        * period, then let us spin without work before going
-                        * to sleep. The exception is if we got EBUSY doing
-                        * more IO, we should wait for the application to
-                        * reap events and wake us up.
-                        */
-                       if (!list_empty(&ctx->iopoll_list) || need_resched() ||
-                           (!time_after(jiffies, timeout) && ret != -EBUSY &&
-                           !percpu_ref_is_dying(&ctx->refs))) {
-                               io_run_task_work();
-                               cond_resched();
-                               continue;
-                       }
+               if (unlikely(!list_empty(&sqd->ctx_new_list)))
+                       io_sqd_init_new(sqd);
  
-                       prepare_to_wait(&ctx->sqo_wait, &wait,
-                                               TASK_INTERRUPTIBLE);
+               cap_entries = !list_is_singular(&sqd->ctx_list);
  
-                       /*
-                        * While doing polled IO, before going to sleep, we need
-                        * to check if there are new reqs added to iopoll_list,
-                        * it is because reqs may have been punted to io worker
-                        * and will be added to iopoll_list later, hence check
-                        * the iopoll_list again.
-                        */
-                       if ((ctx->flags & IORING_SETUP_IOPOLL) &&
-                           !list_empty_careful(&ctx->iopoll_list)) {
-                               finish_wait(&ctx->sqo_wait, &wait);
-                               continue;
+               list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
+                       if (current->cred != ctx->creds) {
+                               if (old_cred)
+                                       revert_creds(old_cred);
+                               old_cred = override_creds(ctx->creds);
                        }
+                       io_sq_thread_associate_blkcg(ctx, &cur_css);
  
-                       io_ring_set_wakeup_flag(ctx);
+                       ret |= __io_sq_thread(ctx, start_jiffies, cap_entries);
  
-                       to_submit = io_sqring_entries(ctx);
-                       if (!to_submit || ret == -EBUSY) {
-                               if (kthread_should_park()) {
-                                       finish_wait(&ctx->sqo_wait, &wait);
-                                       break;
-                               }
-                               if (io_run_task_work()) {
-                                       finish_wait(&ctx->sqo_wait, &wait);
-                                       io_ring_clear_wakeup_flag(ctx);
-                                       continue;
-                               }
-                               if (signal_pending(current))
-                                       flush_signals(current);
-                               schedule();
-                               finish_wait(&ctx->sqo_wait, &wait);
+                       io_sq_thread_drop_mm();
+               }
  
-                               io_ring_clear_wakeup_flag(ctx);
-                               ret = 0;
+               if (ret & SQT_SPIN) {
+                       io_run_task_work();
+                       cond_resched();
+               } else if (ret == SQT_IDLE) {
+                       if (kthread_should_park())
                                continue;
-                       }
-                       finish_wait(&ctx->sqo_wait, &wait);
-                       io_ring_clear_wakeup_flag(ctx);
+                       list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
+                               io_ring_set_wakeup_flag(ctx);
+                       schedule();
+                       start_jiffies = jiffies;
+                       list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
+                               io_ring_clear_wakeup_flag(ctx);
                }
-               mutex_lock(&ctx->uring_lock);
-               if (likely(!percpu_ref_is_dying(&ctx->refs)))
-                       ret = io_submit_sqes(ctx, to_submit, NULL, -1);
-               mutex_unlock(&ctx->uring_lock);
-               timeout = jiffies + ctx->sq_thread_idle;
        }
  
        io_run_task_work();
  
-       io_sq_thread_drop_mm();
-       revert_creds(old_cred);
+       if (cur_css)
+               io_sq_thread_unassociate_blkcg();
+       if (old_cred)
+               revert_creds(old_cred);
  
        kthread_parkme();
  
@@@ -6703,6 -6736,22 +6732,22 @@@ static int io_wake_function(struct wait
        return autoremove_wake_function(curr, mode, wake_flags, key);
  }
  
+ static int io_run_task_work_sig(void)
+ {
+       if (io_run_task_work())
+               return 1;
+       if (!signal_pending(current))
+               return 0;
+       if (current->jobctl & JOBCTL_TASK_WORK) {
+               spin_lock_irq(&current->sighand->siglock);
+               current->jobctl &= ~JOBCTL_TASK_WORK;
+               recalc_sigpending();
+               spin_unlock_irq(&current->sighand->siglock);
+               return 1;
+       }
+       return -EINTR;
+ }
  /*
   * Wait until events become available, if we don't already have some. The
   * application must reap them itself, as they reside on the shared cq ring.
@@@ -6748,19 -6797,11 +6793,11 @@@ static int io_cqring_wait(struct io_rin
                prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
                                                TASK_INTERRUPTIBLE);
                /* make sure we run task_work before checking for signals */
-               if (io_run_task_work())
+               ret = io_run_task_work_sig();
+               if (ret > 0)
                        continue;
-               if (signal_pending(current)) {
-                       if (current->jobctl & JOBCTL_TASK_WORK) {
-                               spin_lock_irq(&current->sighand->siglock);
-                               current->jobctl &= ~JOBCTL_TASK_WORK;
-                               recalc_sigpending();
-                               spin_unlock_irq(&current->sighand->siglock);
-                               continue;
-                       }
-                       ret = -EINTR;
+               else if (ret < 0)
                        break;
-               }
                if (io_should_wake(&iowq, false))
                        break;
                schedule();
@@@ -6838,18 -6879,116 +6875,116 @@@ static int io_sqe_files_unregister(stru
        return 0;
  }
  
- static void io_sq_thread_stop(struct io_ring_ctx *ctx)
+ static void io_put_sq_data(struct io_sq_data *sqd)
  {
-       if (ctx->sqo_thread) {
-               wait_for_completion(&ctx->sq_thread_comp);
+       if (refcount_dec_and_test(&sqd->refs)) {
                /*
                 * The park is a bit of a work-around, without it we get
                 * warning spews on shutdown with SQPOLL set and affinity
                 * set to a single CPU.
                 */
-               kthread_park(ctx->sqo_thread);
-               kthread_stop(ctx->sqo_thread);
-               ctx->sqo_thread = NULL;
+               if (sqd->thread) {
+                       kthread_park(sqd->thread);
+                       kthread_stop(sqd->thread);
+               }
+               kfree(sqd);
+       }
+ }
+ static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
+ {
+       struct io_ring_ctx *ctx_attach;
+       struct io_sq_data *sqd;
+       struct fd f;
+       f = fdget(p->wq_fd);
+       if (!f.file)
+               return ERR_PTR(-ENXIO);
+       if (f.file->f_op != &io_uring_fops) {
+               fdput(f);
+               return ERR_PTR(-EINVAL);
+       }
+       ctx_attach = f.file->private_data;
+       sqd = ctx_attach->sq_data;
+       if (!sqd) {
+               fdput(f);
+               return ERR_PTR(-EINVAL);
+       }
+       refcount_inc(&sqd->refs);
+       fdput(f);
+       return sqd;
+ }
+ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
+ {
+       struct io_sq_data *sqd;
+       if (p->flags & IORING_SETUP_ATTACH_WQ)
+               return io_attach_sq_data(p);
+       sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
+       if (!sqd)
+               return ERR_PTR(-ENOMEM);
+       refcount_set(&sqd->refs, 1);
+       INIT_LIST_HEAD(&sqd->ctx_list);
+       INIT_LIST_HEAD(&sqd->ctx_new_list);
+       mutex_init(&sqd->ctx_lock);
+       mutex_init(&sqd->lock);
+       init_waitqueue_head(&sqd->wait);
+       return sqd;
+ }
+ static void io_sq_thread_unpark(struct io_sq_data *sqd)
+       __releases(&sqd->lock)
+ {
+       if (!sqd->thread)
+               return;
+       kthread_unpark(sqd->thread);
+       mutex_unlock(&sqd->lock);
+ }
+ static void io_sq_thread_park(struct io_sq_data *sqd)
+       __acquires(&sqd->lock)
+ {
+       if (!sqd->thread)
+               return;
+       mutex_lock(&sqd->lock);
+       kthread_park(sqd->thread);
+ }
+ static void io_sq_thread_stop(struct io_ring_ctx *ctx)
+ {
+       struct io_sq_data *sqd = ctx->sq_data;
+       if (sqd) {
+               if (sqd->thread) {
+                       /*
+                        * We may arrive here from the error branch in
+                        * io_sq_offload_create() where the kthread is created
+                        * without being waked up, thus wake it up now to make
+                        * sure the wait will complete.
+                        */
+                       wake_up_process(sqd->thread);
+                       wait_for_completion(&ctx->sq_thread_comp);
+                       io_sq_thread_park(sqd);
+               }
+               mutex_lock(&sqd->ctx_lock);
+               list_del(&ctx->sqd_list);
+               mutex_unlock(&sqd->ctx_lock);
+               if (sqd->thread) {
+                       finish_wait(&sqd->wait, &ctx->sqo_wait_entry);
+                       io_sq_thread_unpark(sqd);
+               }
+               io_put_sq_data(sqd);
+               ctx->sq_data = NULL;
        }
  }
  
@@@ -6960,13 -7099,13 +7095,13 @@@ static int io_sqe_files_scm(struct io_r
  }
  #endif
  
- static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
-                                   unsigned nr_files)
+ static int io_sqe_alloc_file_tables(struct fixed_file_data *file_data,
+                                   unsigned nr_tables, unsigned nr_files)
  {
        int i;
  
        for (i = 0; i < nr_tables; i++) {
-               struct fixed_file_table *table = &ctx->file_data->table[i];
+               struct fixed_file_table *table = &file_data->table[i];
                unsigned this_files;
  
                this_files = min(nr_files, IORING_MAX_FILES_TABLE);
                return 0;
  
        for (i = 0; i < nr_tables; i++) {
-               struct fixed_file_table *table = &ctx->file_data->table[i];
+               struct fixed_file_table *table = &file_data->table[i];
                kfree(table->files);
        }
        return 1;
@@@ -7143,11 -7282,11 +7278,11 @@@ static int io_sqe_files_register(struc
                                 unsigned nr_args)
  {
        __s32 __user *fds = (__s32 __user *) arg;
-       unsigned nr_tables;
+       unsigned nr_tables, i;
        struct file *file;
-       int fd, ret = 0;
-       unsigned i;
+       int fd, ret = -ENOMEM;
        struct fixed_file_ref_node *ref_node;
+       struct fixed_file_data *file_data;
  
        if (ctx->file_data)
                return -EBUSY;
        if (nr_args > IORING_MAX_FIXED_FILES)
                return -EMFILE;
  
-       ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
-       if (!ctx->file_data)
+       file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
+       if (!file_data)
                return -ENOMEM;
-       ctx->file_data->ctx = ctx;
-       init_completion(&ctx->file_data->done);
-       INIT_LIST_HEAD(&ctx->file_data->ref_list);
-       spin_lock_init(&ctx->file_data->lock);
+       file_data->ctx = ctx;
+       init_completion(&file_data->done);
+       INIT_LIST_HEAD(&file_data->ref_list);
+       spin_lock_init(&file_data->lock);
  
        nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
-       ctx->file_data->table = kcalloc(nr_tables,
-                                       sizeof(struct fixed_file_table),
-                                       GFP_KERNEL);
-       if (!ctx->file_data->table) {
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
-               return -ENOMEM;
-       }
+       file_data->table = kcalloc(nr_tables, sizeof(file_data->table),
+                                  GFP_KERNEL);
+       if (!file_data->table)
+               goto out_free;
  
-       if (percpu_ref_init(&ctx->file_data->refs, io_file_ref_kill,
-                               PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
-               return -ENOMEM;
-       }
+       if (percpu_ref_init(&file_data->refs, io_file_ref_kill,
+                               PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
+               goto out_free;
  
-       if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
-               percpu_ref_exit(&ctx->file_data->refs);
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
-               return -ENOMEM;
-       }
+       if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
+               goto out_ref;
  
        for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
                struct fixed_file_table *table;
                unsigned index;
  
-               ret = -EFAULT;
-               if (copy_from_user(&fd, &fds[i], sizeof(fd)))
-                       break;
+               if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
+                       ret = -EFAULT;
+                       goto out_fput;
+               }
                /* allow sparse sets */
-               if (fd == -1) {
-                       ret = 0;
+               if (fd == -1)
                        continue;
-               }
  
-               table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
-               index = i & IORING_FILE_TABLE_MASK;
                file = fget(fd);
                ret = -EBADF;
                if (!file)
-                       break;
+                       goto out_fput;
  
                /*
                 * Don't allow io_uring instances to be registered. If UNIX
                 */
                if (file->f_op == &io_uring_fops) {
                        fput(file);
-                       break;
+                       goto out_fput;
                }
-               ret = 0;
+               table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
+               index = i & IORING_FILE_TABLE_MASK;
                table->files[index] = file;
        }
  
-       if (ret) {
-               for (i = 0; i < ctx->nr_user_files; i++) {
-                       file = io_file_from_index(ctx, i);
-                       if (file)
-                               fput(file);
-               }
-               for (i = 0; i < nr_tables; i++)
-                       kfree(ctx->file_data->table[i].files);
-               percpu_ref_exit(&ctx->file_data->refs);
-               kfree(ctx->file_data->table);
-               kfree(ctx->file_data);
-               ctx->file_data = NULL;
-               ctx->nr_user_files = 0;
-               return ret;
-       }
+       ctx->file_data = file_data;
        ret = io_sqe_files_scm(ctx);
        if (ret) {
                io_sqe_files_unregister(ctx);
                return PTR_ERR(ref_node);
        }
  
-       ctx->file_data->cur_refs = &ref_node->refs;
-       spin_lock(&ctx->file_data->lock);
-       list_add(&ref_node->node, &ctx->file_data->ref_list);
-       spin_unlock(&ctx->file_data->lock);
-       percpu_ref_get(&ctx->file_data->refs);
+       file_data->node = ref_node;
+       spin_lock(&file_data->lock);
+       list_add(&ref_node->node, &file_data->ref_list);
+       spin_unlock(&file_data->lock);
+       percpu_ref_get(&file_data->refs);
+       return ret;
+ out_fput:
+       for (i = 0; i < ctx->nr_user_files; i++) {
+               file = io_file_from_index(ctx, i);
+               if (file)
+                       fput(file);
+       }
+       for (i = 0; i < nr_tables; i++)
+               kfree(file_data->table[i].files);
+       ctx->nr_user_files = 0;
+ out_ref:
+       percpu_ref_exit(&file_data->refs);
+ out_free:
+       kfree(file_data->table);
+       kfree(file_data);
        return ret;
  }
  
@@@ -7310,14 -7432,12 +7428,12 @@@ static int io_queue_file_removal(struc
                                 struct file *file)
  {
        struct io_file_put *pfile;
-       struct percpu_ref *refs = data->cur_refs;
-       struct fixed_file_ref_node *ref_node;
+       struct fixed_file_ref_node *ref_node = data->node;
  
        pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
        if (!pfile)
                return -ENOMEM;
  
-       ref_node = container_of(refs, struct fixed_file_ref_node, refs);
        pfile->file = file;
        list_add(&pfile->list, &ref_node->file_list);
  
@@@ -7400,10 -7520,10 +7516,10 @@@ static int __io_sqe_files_update(struc
        }
  
        if (needs_switch) {
-               percpu_ref_kill(data->cur_refs);
+               percpu_ref_kill(&data->node->refs);
                spin_lock(&data->lock);
                list_add(&ref_node->node, &data->ref_list);
-               data->cur_refs = &ref_node->refs;
+               data->node = ref_node;
                spin_unlock(&data->lock);
                percpu_ref_get(&ctx->file_data->refs);
        } else
@@@ -7484,20 -7604,65 +7600,65 @@@ out_fput
        return ret;
  }
  
- static int io_sq_offload_start(struct io_ring_ctx *ctx,
-                              struct io_uring_params *p)
+ static int io_uring_alloc_task_context(struct task_struct *task)
+ {
+       struct io_uring_task *tctx;
+       tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
+       if (unlikely(!tctx))
+               return -ENOMEM;
+       xa_init(&tctx->xa);
+       init_waitqueue_head(&tctx->wait);
+       tctx->last = NULL;
+       tctx->in_idle = 0;
+       atomic_long_set(&tctx->req_issue, 0);
+       atomic_long_set(&tctx->req_complete, 0);
+       task->io_uring = tctx;
+       return 0;
+ }
+ void __io_uring_free(struct task_struct *tsk)
+ {
+       struct io_uring_task *tctx = tsk->io_uring;
+       WARN_ON_ONCE(!xa_empty(&tctx->xa));
+       kfree(tctx);
+       tsk->io_uring = NULL;
+ }
+ static int io_sq_offload_create(struct io_ring_ctx *ctx,
+                               struct io_uring_params *p)
  {
        int ret;
  
        if (ctx->flags & IORING_SETUP_SQPOLL) {
+               struct io_sq_data *sqd;
                ret = -EPERM;
                if (!capable(CAP_SYS_ADMIN))
                        goto err;
  
+               sqd = io_get_sq_data(p);
+               if (IS_ERR(sqd)) {
+                       ret = PTR_ERR(sqd);
+                       goto err;
+               }
+               ctx->sq_data = sqd;
+               io_sq_thread_park(sqd);
+               mutex_lock(&sqd->ctx_lock);
+               list_add(&ctx->sqd_list, &sqd->ctx_new_list);
+               mutex_unlock(&sqd->ctx_lock);
+               io_sq_thread_unpark(sqd);
                ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
                if (!ctx->sq_thread_idle)
                        ctx->sq_thread_idle = HZ;
  
+               if (sqd->thread)
+                       goto done;
                if (p->flags & IORING_SETUP_SQ_AFF) {
                        int cpu = p->sq_thread_cpu;
  
                        if (!cpu_online(cpu))
                                goto err;
  
-                       ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
-                                                       ctx, cpu,
-                                                       "io_uring-sq");
+                       sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
+                                                       cpu, "io_uring-sq");
                } else {
-                       ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
+                       sqd->thread = kthread_create(io_sq_thread, sqd,
                                                        "io_uring-sq");
                }
-               if (IS_ERR(ctx->sqo_thread)) {
-                       ret = PTR_ERR(ctx->sqo_thread);
-                       ctx->sqo_thread = NULL;
+               if (IS_ERR(sqd->thread)) {
+                       ret = PTR_ERR(sqd->thread);
+                       sqd->thread = NULL;
                        goto err;
                }
-               wake_up_process(ctx->sqo_thread);
+               ret = io_uring_alloc_task_context(sqd->thread);
+               if (ret)
+                       goto err;
        } else if (p->flags & IORING_SETUP_SQ_AFF) {
                /* Can't have SQ_AFF without SQPOLL */
                ret = -EINVAL;
                goto err;
        }
  
+ done:
        ret = io_init_wq_offload(ctx, p);
        if (ret)
                goto err;
        return ret;
  }
  
+ static void io_sq_offload_start(struct io_ring_ctx *ctx)
+ {
+       struct io_sq_data *sqd = ctx->sq_data;
+       if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
+               wake_up_process(sqd->thread);
+ }
  static inline void __io_unaccount_mem(struct user_struct *user,
                                      unsigned long nr_pages)
  {
@@@ -7567,11 -7742,11 +7738,11 @@@ static void io_unaccount_mem(struct io_
        if (ctx->limit_mem)
                __io_unaccount_mem(ctx->user, nr_pages);
  
-       if (ctx->sqo_mm) {
+       if (ctx->mm_account) {
                if (acct == ACCT_LOCKED)
-                       ctx->sqo_mm->locked_vm -= nr_pages;
+                       ctx->mm_account->locked_vm -= nr_pages;
                else if (acct == ACCT_PINNED)
-                       atomic64_sub(nr_pages, &ctx->sqo_mm->pinned_vm);
+                       atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
        }
  }
  
@@@ -7586,11 -7761,11 +7757,11 @@@ static int io_account_mem(struct io_rin
                        return ret;
        }
  
-       if (ctx->sqo_mm) {
+       if (ctx->mm_account) {
                if (acct == ACCT_LOCKED)
-                       ctx->sqo_mm->locked_vm += nr_pages;
+                       ctx->mm_account->locked_vm += nr_pages;
                else if (acct == ACCT_PINNED)
-                       atomic64_add(nr_pages, &ctx->sqo_mm->pinned_vm);
+                       atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
        }
  
        return 0;
@@@ -7670,7 -7845,8 +7841,8 @@@ static int io_sqe_buffer_unregister(str
                for (j = 0; j < imu->nr_bvecs; j++)
                        unpin_user_page(imu->bvec[j].bv_page);
  
-               io_unaccount_mem(ctx, imu->nr_bvecs, ACCT_PINNED);
+               if (imu->acct_pages)
+                       io_unaccount_mem(ctx, imu->acct_pages, ACCT_PINNED);
                kvfree(imu->bvec);
                imu->nr_bvecs = 0;
        }
@@@ -7706,11 -7882,80 +7878,80 @@@ static int io_copy_iov(struct io_ring_c
        return 0;
  }
  
+ /*
+  * Not super efficient, but this is just a registration time. And we do cache
+  * the last compound head, so generally we'll only do a full search if we don't
+  * match that one.
+  *
+  * We check if the given compound head page has already been accounted, to
+  * avoid double accounting it. This allows us to account the full size of the
+  * page, not just the constituent pages of a huge page.
+  */
+ static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
+                                 int nr_pages, struct page *hpage)
+ {
+       int i, j;
+       /* check current page array */
+       for (i = 0; i < nr_pages; i++) {
+               if (!PageCompound(pages[i]))
+                       continue;
+               if (compound_head(pages[i]) == hpage)
+                       return true;
+       }
+       /* check previously registered pages */
+       for (i = 0; i < ctx->nr_user_bufs; i++) {
+               struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
+               for (j = 0; j < imu->nr_bvecs; j++) {
+                       if (!PageCompound(imu->bvec[j].bv_page))
+                               continue;
+                       if (compound_head(imu->bvec[j].bv_page) == hpage)
+                               return true;
+               }
+       }
+       return false;
+ }
+ static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
+                                int nr_pages, struct io_mapped_ubuf *imu,
+                                struct page **last_hpage)
+ {
+       int i, ret;
+       for (i = 0; i < nr_pages; i++) {
+               if (!PageCompound(pages[i])) {
+                       imu->acct_pages++;
+               } else {
+                       struct page *hpage;
+                       hpage = compound_head(pages[i]);
+                       if (hpage == *last_hpage)
+                               continue;
+                       *last_hpage = hpage;
+                       if (headpage_already_acct(ctx, pages, i, hpage))
+                               continue;
+                       imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
+               }
+       }
+       if (!imu->acct_pages)
+               return 0;
+       ret = io_account_mem(ctx, imu->acct_pages, ACCT_PINNED);
+       if (ret)
+               imu->acct_pages = 0;
+       return ret;
+ }
  static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
                                  unsigned nr_args)
  {
        struct vm_area_struct **vmas = NULL;
        struct page **pages = NULL;
+       struct page *last_hpage = NULL;
        int i, j, got_pages = 0;
        int ret = -EINVAL;
  
                start = ubuf >> PAGE_SHIFT;
                nr_pages = end - start;
  
-               ret = io_account_mem(ctx, nr_pages, ACCT_PINNED);
-               if (ret)
-                       goto err;
                ret = 0;
                if (!pages || nr_pages > got_pages) {
                        kvfree(vmas);
                                        GFP_KERNEL);
                        if (!pages || !vmas) {
                                ret = -ENOMEM;
-                               io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
                                goto err;
                        }
                        got_pages = nr_pages;
                imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
                                                GFP_KERNEL);
                ret = -ENOMEM;
-               if (!imu->bvec) {
-                       io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
+               if (!imu->bvec)
                        goto err;
-               }
  
                ret = 0;
                mmap_read_lock(current->mm);
                         */
                        if (pret > 0)
                                unpin_user_pages(pages, pret);
-                       io_unaccount_mem(ctx, nr_pages, ACCT_PINNED);
+                       kvfree(imu->bvec);
+                       goto err;
+               }
+               ret = io_buffer_account_pin(ctx, pages, pret, imu, &last_hpage);
+               if (ret) {
+                       unpin_user_pages(pages, pret);
                        kvfree(imu->bvec);
                        goto err;
                }
@@@ -7894,11 -8138,19 +8134,19 @@@ static void io_ring_ctx_free(struct io_
  {
        io_finish_async(ctx);
        io_sqe_buffer_unregister(ctx);
-       if (ctx->sqo_mm) {
-               mmdrop(ctx->sqo_mm);
-               ctx->sqo_mm = NULL;
+       if (ctx->sqo_task) {
+               put_task_struct(ctx->sqo_task);
+               ctx->sqo_task = NULL;
+               mmdrop(ctx->mm_account);
+               ctx->mm_account = NULL;
        }
  
+ #ifdef CONFIG_BLK_CGROUP
+       if (ctx->sqo_blkcg_css)
+               css_put(ctx->sqo_blkcg_css);
+ #endif
        io_sqe_files_unregister(ctx);
        io_eventfd_unregister(ctx);
        io_destroy_buffers(ctx);
@@@ -7933,8 -8185,7 +8181,7 @@@ static __poll_t io_uring_poll(struct fi
         * io_commit_cqring
         */
        smp_rmb();
-       if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
-           ctx->rings->sq_ring_entries)
+       if (!io_sqring_full(ctx))
                mask |= EPOLLOUT | EPOLLWRNORM;
        if (io_cqring_events(ctx, false))
                mask |= EPOLLIN | EPOLLRDNORM;
@@@ -7973,7 -8224,7 +8220,7 @@@ static void io_ring_exit_work(struct wo
         */
        do {
                if (ctx->rings)
-                       io_cqring_overflow_flush(ctx, true);
+                       io_cqring_overflow_flush(ctx, true, NULL, NULL);
                io_iopoll_try_reap_events(ctx);
        } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
        io_ring_ctx_free(ctx);
@@@ -7985,15 -8236,15 +8232,15 @@@ static void io_ring_ctx_wait_and_kill(s
        percpu_ref_kill(&ctx->refs);
        mutex_unlock(&ctx->uring_lock);
  
-       io_kill_timeouts(ctx);
-       io_poll_remove_all(ctx);
+       io_kill_timeouts(ctx, NULL);
+       io_poll_remove_all(ctx, NULL);
  
        if (ctx->io_wq)
                io_wq_cancel_all(ctx->io_wq);
  
        /* if we failed setting up the ctx, we might not have any rings */
        if (ctx->rings)
-               io_cqring_overflow_flush(ctx, true);
+               io_cqring_overflow_flush(ctx, true, NULL, NULL);
        io_iopoll_try_reap_events(ctx);
        idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
  
@@@ -8028,7 -8279,7 +8275,7 @@@ static bool io_wq_files_match(struct io
  {
        struct files_struct *files = data;
  
-       return work->files == files;
+       return !files || work->files == files;
  }
  
  /*
@@@ -8049,12 -8300,6 +8296,6 @@@ static bool io_match_link(struct io_kio
        return false;
  }
  
- static inline bool io_match_files(struct io_kiocb *req,
-                                      struct files_struct *files)
- {
-       return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
- }
  static bool io_match_link_files(struct io_kiocb *req,
                                struct files_struct *files)
  {
@@@ -8170,11 -8415,14 +8411,14 @@@ static void io_cancel_defer_files(struc
        }
  }
  
- static void io_uring_cancel_files(struct io_ring_ctx *ctx,
+ /*
+  * Returns true if we found and killed one or more files pinning requests
+  */
+ static bool io_uring_cancel_files(struct io_ring_ctx *ctx,
                                  struct files_struct *files)
  {
        if (list_empty_careful(&ctx->inflight_list))
-               return;
+               return false;
  
        io_cancel_defer_files(ctx, files);
        /* cancel all at once, should be faster than doing it one by one*/
  
                spin_lock_irq(&ctx->inflight_lock);
                list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
-                       if (req->work.files != files)
+                       if (files && req->work.files != files)
                                continue;
                        /* req is being completed, ignore */
                        if (!refcount_inc_not_zero(&req->refs))
                schedule();
                finish_wait(&ctx->inflight_wait, &wait);
        }
+       return true;
  }
  
  static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
        struct io_kiocb *req = container_of(work, struct io_kiocb, work);
        struct task_struct *task = data;
  
-       return req->task == task;
+       return io_task_match(req, task);
+ }
+ static bool __io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
+                                           struct task_struct *task,
+                                           struct files_struct *files)
+ {
+       bool ret;
+       ret = io_uring_cancel_files(ctx, files);
+       if (!files) {
+               enum io_wq_cancel cret;
+               cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, task, true);
+               if (cret != IO_WQ_CANCEL_NOTFOUND)
+                       ret = true;
+               /* SQPOLL thread does its own polling */
+               if (!(ctx->flags & IORING_SETUP_SQPOLL)) {
+                       while (!list_empty_careful(&ctx->iopoll_list)) {
+                               io_iopoll_try_reap_events(ctx);
+                               ret = true;
+                       }
+               }
+               ret |= io_poll_remove_all(ctx, task);
+               ret |= io_kill_timeouts(ctx, task);
+       }
+       return ret;
+ }
+ /*
+  * We need to iteratively cancel requests, in case a request has dependent
+  * hard links. These persist even for failure of cancelations, hence keep
+  * looping until none are found.
+  */
+ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
+                                         struct files_struct *files)
+ {
+       struct task_struct *task = current;
+       if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data)
+               task = ctx->sq_data->thread;
+       io_cqring_overflow_flush(ctx, true, task, files);
+       while (__io_uring_cancel_task_requests(ctx, task, files)) {
+               io_run_task_work();
+               cond_resched();
+       }
+ }
+ /*
+  * Note that this task has used io_uring. We use it for cancelation purposes.
+  */
+ static int io_uring_add_task_file(struct file *file)
+ {
+       struct io_uring_task *tctx = current->io_uring;
+       if (unlikely(!tctx)) {
+               int ret;
+               ret = io_uring_alloc_task_context(current);
+               if (unlikely(ret))
+                       return ret;
+               tctx = current->io_uring;
+       }
+       if (tctx->last != file) {
+               void *old = xa_load(&tctx->xa, (unsigned long)file);
+               if (!old) {
+                       get_file(file);
+                       xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
+               }
+               tctx->last = file;
+       }
+       return 0;
+ }
+ /*
+  * Remove this io_uring_file -> task mapping.
+  */
+ static void io_uring_del_task_file(struct file *file)
+ {
+       struct io_uring_task *tctx = current->io_uring;
+       if (tctx->last == file)
+               tctx->last = NULL;
+       file = xa_erase(&tctx->xa, (unsigned long)file);
+       if (file)
+               fput(file);
+ }
+ static void __io_uring_attempt_task_drop(struct file *file)
+ {
+       struct file *old = xa_load(&current->io_uring->xa, (unsigned long)file);
+       if (old == file)
+               io_uring_del_task_file(file);
+ }
+ /*
+  * Drop task note for this file if we're the only ones that hold it after
+  * pending fput()
+  */
+ static void io_uring_attempt_task_drop(struct file *file, bool exiting)
+ {
+       if (!current->io_uring)
+               return;
+       /*
+        * fput() is pending, will be 2 if the only other ref is our potential
+        * task file note. If the task is exiting, drop regardless of count.
+        */
+       if (!exiting && atomic_long_read(&file->f_count) != 2)
+               return;
+       __io_uring_attempt_task_drop(file);
+ }
+ void __io_uring_files_cancel(struct files_struct *files)
+ {
+       struct io_uring_task *tctx = current->io_uring;
+       struct file *file;
+       unsigned long index;
+       /* make sure overflow events are dropped */
+       tctx->in_idle = true;
+       xa_for_each(&tctx->xa, index, file) {
+               struct io_ring_ctx *ctx = file->private_data;
+               io_uring_cancel_task_requests(ctx, files);
+               if (files)
+                       io_uring_del_task_file(file);
+       }
+ }
+ static inline bool io_uring_task_idle(struct io_uring_task *tctx)
+ {
+       return atomic_long_read(&tctx->req_issue) ==
+               atomic_long_read(&tctx->req_complete);
+ }
+ /*
+  * Find any io_uring fd that this task has registered or done IO on, and cancel
+  * requests.
+  */
+ void __io_uring_task_cancel(void)
+ {
+       struct io_uring_task *tctx = current->io_uring;
+       DEFINE_WAIT(wait);
+       long completions;
+       /* make sure overflow events are dropped */
+       tctx->in_idle = true;
+       while (!io_uring_task_idle(tctx)) {
+               /* read completions before cancelations */
+               completions = atomic_long_read(&tctx->req_complete);
+               __io_uring_files_cancel(NULL);
+               prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
+               /*
+                * If we've seen completions, retry. This avoids a race where
+                * a completion comes in before we did prepare_to_wait().
+                */
+               if (completions != atomic_long_read(&tctx->req_complete))
+                       continue;
+               if (io_uring_task_idle(tctx))
+                       break;
+               schedule();
+       }
+       finish_wait(&tctx->wait, &wait);
+       tctx->in_idle = false;
  }
  
  static int io_uring_flush(struct file *file, void *data)
  {
        struct io_ring_ctx *ctx = file->private_data;
  
-       io_uring_cancel_files(ctx, data);
        /*
         * If the task is going away, cancel work it may have pending
         */
        if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
-               io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, current, true);
+               data = NULL;
  
+       io_uring_cancel_task_requests(ctx, data);
+       io_uring_attempt_task_drop(file, !data);
        return 0;
  }
  
@@@ -8305,6 -8732,25 +8728,25 @@@ static unsigned long io_uring_nommu_get
  
  #endif /* !CONFIG_MMU */
  
+ static void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
+ {
+       DEFINE_WAIT(wait);
+       do {
+               if (!io_sqring_full(ctx))
+                       break;
+               prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
+               if (!io_sqring_full(ctx))
+                       break;
+               schedule();
+       } while (!signal_pending(current));
+       finish_wait(&ctx->sqo_sq_wait, &wait);
+ }
  SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
                u32, min_complete, u32, flags, const sigset_t __user *, sig,
                size_t, sigsz)
  
        io_run_task_work();
  
-       if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
+       if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
+                       IORING_ENTER_SQ_WAIT))
                return -EINVAL;
  
        f = fdget(fd);
        if (!percpu_ref_tryget(&ctx->refs))
                goto out_fput;
  
+       ret = -EBADFD;
+       if (ctx->flags & IORING_SETUP_R_DISABLED)
+               goto out;
        /*
         * For SQ polling, the thread will do all submissions and completions.
         * Just return the requested submit count, and wake the thread if
        ret = 0;
        if (ctx->flags & IORING_SETUP_SQPOLL) {
                if (!list_empty_careful(&ctx->cq_overflow_list))
-                       io_cqring_overflow_flush(ctx, false);
+                       io_cqring_overflow_flush(ctx, false, NULL, NULL);
                if (flags & IORING_ENTER_SQ_WAKEUP)
-                       wake_up(&ctx->sqo_wait);
+                       wake_up(&ctx->sq_data->wait);
+               if (flags & IORING_ENTER_SQ_WAIT)
+                       io_sqpoll_wait_sq(ctx);
                submitted = to_submit;
        } else if (to_submit) {
+               ret = io_uring_add_task_file(f.file);
+               if (unlikely(ret))
+                       goto out;
                mutex_lock(&ctx->uring_lock);
-               submitted = io_submit_sqes(ctx, to_submit, f.file, fd);
+               submitted = io_submit_sqes(ctx, to_submit);
                mutex_unlock(&ctx->uring_lock);
  
                if (submitted != to_submit)
@@@ -8412,6 -8868,7 +8864,7 @@@ static int io_uring_show_cred(int id, v
  
  static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
  {
+       struct io_sq_data *sq = NULL;
        bool has_lock;
        int i;
  
         */
        has_lock = mutex_trylock(&ctx->uring_lock);
  
+       if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
+               sq = ctx->sq_data;
+       seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
+       seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
        seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
        for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
                struct fixed_file_table *table;
@@@ -8556,6 -9018,7 +9014,7 @@@ static int io_uring_get_fd(struct io_ri
        file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
                                        O_RDWR | O_CLOEXEC);
        if (IS_ERR(file)) {
+ err_fd:
                put_unused_fd(ret);
                ret = PTR_ERR(file);
                goto err;
  #if defined(CONFIG_UNIX)
        ctx->ring_sock->file = file;
  #endif
+       if (unlikely(io_uring_add_task_file(file))) {
+               file = ERR_PTR(-ENOMEM);
+               goto err_fd;
+       }
        fd_install(ret, file);
        return ret;
  err:
@@@ -8641,8 -9108,35 +9104,35 @@@ static int io_uring_create(unsigned ent
        ctx->user = user;
        ctx->creds = get_current_cred();
  
+       ctx->sqo_task = get_task_struct(current);
+       /*
+        * This is just grabbed for accounting purposes. When a process exits,
+        * the mm is exited and dropped before the files, hence we need to hang
+        * on to this mm purely for the purposes of being able to unaccount
+        * memory (locked/pinned vm). It's not used for anything else.
+        */
        mmgrab(current->mm);
-       ctx->sqo_mm = current->mm;
+       ctx->mm_account = current->mm;
+ #ifdef CONFIG_BLK_CGROUP
+       /*
+        * The sq thread will belong to the original cgroup it was inited in.
+        * If the cgroup goes offline (e.g. disabling the io controller), then
+        * issued bios will be associated with the closest cgroup later in the
+        * block layer.
+        */
+       rcu_read_lock();
+       ctx->sqo_blkcg_css = blkcg_css();
+       ret = css_tryget_online(ctx->sqo_blkcg_css);
+       rcu_read_unlock();
+       if (!ret) {
+               /* don't init against a dying cgroup, have the user try again */
+               ctx->sqo_blkcg_css = NULL;
+               ret = -ENODEV;
+               goto err;
+       }
+ #endif
  
        /*
         * Account memory _before_ installing the file descriptor. Once
        if (ret)
                goto err;
  
-       ret = io_sq_offload_start(ctx, p);
+       ret = io_sq_offload_create(ctx, p);
        if (ret)
                goto err;
  
+       if (!(p->flags & IORING_SETUP_R_DISABLED))
+               io_sq_offload_start(ctx);
        memset(&p->sq_off, 0, sizeof(p->sq_off));
        p->sq_off.head = offsetof(struct io_rings, sq.head);
        p->sq_off.tail = offsetof(struct io_rings, sq.tail);
@@@ -8724,7 -9221,8 +9217,8 @@@ static long io_uring_setup(u32 entries
  
        if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
                        IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
-                       IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
+                       IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
+                       IORING_SETUP_R_DISABLED))
                return -EINVAL;
  
        return  io_uring_create(entries, &p, params);
@@@ -8800,6 -9298,91 +9294,91 @@@ static int io_unregister_personality(st
        return -EINVAL;
  }
  
+ static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
+                                   unsigned int nr_args)
+ {
+       struct io_uring_restriction *res;
+       size_t size;
+       int i, ret;
+       /* Restrictions allowed only if rings started disabled */
+       if (!(ctx->flags & IORING_SETUP_R_DISABLED))
+               return -EBADFD;
+       /* We allow only a single restrictions registration */
+       if (ctx->restrictions.registered)
+               return -EBUSY;
+       if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
+               return -EINVAL;
+       size = array_size(nr_args, sizeof(*res));
+       if (size == SIZE_MAX)
+               return -EOVERFLOW;
+       res = memdup_user(arg, size);
+       if (IS_ERR(res))
+               return PTR_ERR(res);
+       ret = 0;
+       for (i = 0; i < nr_args; i++) {
+               switch (res[i].opcode) {
+               case IORING_RESTRICTION_REGISTER_OP:
+                       if (res[i].register_op >= IORING_REGISTER_LAST) {
+                               ret = -EINVAL;
+                               goto out;
+                       }
+                       __set_bit(res[i].register_op,
+                                 ctx->restrictions.register_op);
+                       break;
+               case IORING_RESTRICTION_SQE_OP:
+                       if (res[i].sqe_op >= IORING_OP_LAST) {
+                               ret = -EINVAL;
+                               goto out;
+                       }
+                       __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
+                       break;
+               case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
+                       ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
+                       break;
+               case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
+                       ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
+                       break;
+               default:
+                       ret = -EINVAL;
+                       goto out;
+               }
+       }
+ out:
+       /* Reset all restrictions if an error happened */
+       if (ret != 0)
+               memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
+       else
+               ctx->restrictions.registered = true;
+       kfree(res);
+       return ret;
+ }
+ static int io_register_enable_rings(struct io_ring_ctx *ctx)
+ {
+       if (!(ctx->flags & IORING_SETUP_R_DISABLED))
+               return -EBADFD;
+       if (ctx->restrictions.registered)
+               ctx->restricted = 1;
+       ctx->flags &= ~IORING_SETUP_R_DISABLED;
+       io_sq_offload_start(ctx);
+       return 0;
+ }
  static bool io_register_op_must_quiesce(int op)
  {
        switch (op) {
@@@ -8841,11 -9424,31 +9420,31 @@@ static int __io_uring_register(struct i
                 * after we've killed the percpu ref.
                 */
                mutex_unlock(&ctx->uring_lock);
-               ret = wait_for_completion_interruptible(&ctx->ref_comp);
+               do {
+                       ret = wait_for_completion_interruptible(&ctx->ref_comp);
+                       if (!ret)
+                               break;
+                       ret = io_run_task_work_sig();
+                       if (ret < 0)
+                               break;
+               } while (1);
                mutex_lock(&ctx->uring_lock);
                if (ret) {
                        percpu_ref_resurrect(&ctx->refs);
-                       ret = -EINTR;
+                       goto out_quiesce;
+               }
+       }
+       if (ctx->restricted) {
+               if (opcode >= IORING_REGISTER_LAST) {
+                       ret = -EINVAL;
+                       goto out;
+               }
+               if (!test_bit(opcode, ctx->restrictions.register_op)) {
+                       ret = -EACCES;
                        goto out;
                }
        }
                        break;
                ret = io_unregister_personality(ctx, nr_args);
                break;
+       case IORING_REGISTER_ENABLE_RINGS:
+               ret = -EINVAL;
+               if (arg || nr_args)
+                       break;
+               ret = io_register_enable_rings(ctx);
+               break;
+       case IORING_REGISTER_RESTRICTIONS:
+               ret = io_register_restrictions(ctx, arg, nr_args);
+               break;
        default:
                ret = -EINVAL;
                break;
        }
  
+ out:
        if (io_register_op_must_quiesce(opcode)) {
                /* bring the ctx back to life */
                percpu_ref_reinit(&ctx->refs);
- out:
+ out_quiesce:
                reinit_completion(&ctx->ref_comp);
        }
        return ret;
diff --combined include/linux/fs.h
index 0b1e2f1f388b0030b5a19e82b116d64f34815793,4ebc14dee4218f8a065189230829d4b3c6fd00c3..2e621d28cd65952c119a49677969186d83634f2c
@@@ -178,6 -178,14 +178,6 @@@ typedef int (dio_iodone_t)(struct kioc
  /* File supports async buffered reads */
  #define FMODE_BUF_RASYNC      ((__force fmode_t)0x40000000)
  
 -/*
 - * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector
 - * that indicates that they should check the contents of the iovec are
 - * valid, but not check the memory that the iovec elements
 - * points too.
 - */
 -#define CHECK_IOVEC_ONLY -1
 -
  /*
   * Attribute flags.  These should be or-ed together to figure out what
   * has been changed!
@@@ -302,17 -310,20 +302,20 @@@ enum rw_hint 
        WRITE_LIFE_EXTREME      = RWH_WRITE_LIFE_EXTREME,
  };
  
- #define IOCB_EVENTFD          (1 << 0)
- #define IOCB_APPEND           (1 << 1)
- #define IOCB_DIRECT           (1 << 2)
- #define IOCB_HIPRI            (1 << 3)
- #define IOCB_DSYNC            (1 << 4)
- #define IOCB_SYNC             (1 << 5)
- #define IOCB_WRITE            (1 << 6)
- #define IOCB_NOWAIT           (1 << 7)
+ /* Match RWF_* bits to IOCB bits */
+ #define IOCB_HIPRI            (__force int) RWF_HIPRI
+ #define IOCB_DSYNC            (__force int) RWF_DSYNC
+ #define IOCB_SYNC             (__force int) RWF_SYNC
+ #define IOCB_NOWAIT           (__force int) RWF_NOWAIT
+ #define IOCB_APPEND           (__force int) RWF_APPEND
+ /* non-RWF related bits - start at 16 */
+ #define IOCB_EVENTFD          (1 << 16)
+ #define IOCB_DIRECT           (1 << 17)
+ #define IOCB_WRITE            (1 << 18)
  /* iocb->ki_waitq is valid */
- #define IOCB_WAITQ            (1 << 8)
- #define IOCB_NOIO             (1 << 9)
+ #define IOCB_WAITQ            (1 << 19)
+ #define IOCB_NOIO             (1 << 20)
  
  struct kiocb {
        struct file             *ki_filp;
@@@ -1377,7 -1388,7 +1380,7 @@@ extern int send_sigurg(struct fown_stru
  #define SB_I_CGROUPWB 0x00000001      /* cgroup-aware writeback enabled */
  #define SB_I_NOEXEC   0x00000002      /* Ignore executables on this fs */
  #define SB_I_NODEV    0x00000004      /* Ignore devices on this fs */
 -#define SB_I_MULTIROOT        0x00000008      /* Multiple roots to the dentry tree */
 +#define SB_I_STABLE_WRITES 0x00000008 /* don't modify blks until WB is done */
  
  /* sb->s_iflags to limit user namespace mounts */
  #define SB_I_USERNS_VISIBLE           0x00000010 /* fstype already mounted */
@@@ -1879,6 -1890,11 +1882,6 @@@ static inline int call_mmap(struct fil
        return file->f_op->mmap(file, vma);
  }
  
 -ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
 -                            unsigned long nr_segs, unsigned long fast_segs,
 -                            struct iovec *fast_pointer,
 -                            struct iovec **ret_pointer);
 -
  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
  extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
  extern ssize_t vfs_readv(struct file *, const struct iovec __user *,
@@@ -3066,6 -3082,8 +3069,6 @@@ enum 
        DIO_SKIP_HOLES  = 0x02,
  };
  
 -void dio_end_io(struct bio *bio);
 -
  ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
                             struct block_device *bdev, struct iov_iter *iter,
                             get_block_t get_block,
@@@ -3302,6 -3320,9 +3305,9 @@@ static inline int kiocb_set_rw_flags(st
  {
        int kiocb_flags = 0;
  
+       /* make sure there's no overlap between RWF and private IOCB flags */
+       BUILD_BUG_ON((__force int) RWF_SUPPORTED & IOCB_EVENTFD);
        if (!flags)
                return 0;
        if (unlikely(flags & ~RWF_SUPPORTED))
        if (flags & RWF_NOWAIT) {
                if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
                        return -EOPNOTSUPP;
-               kiocb_flags |= IOCB_NOWAIT | IOCB_NOIO;
+               kiocb_flags |= IOCB_NOIO;
        }
-       if (flags & RWF_HIPRI)
-               kiocb_flags |= IOCB_HIPRI;
-       if (flags & RWF_DSYNC)
-               kiocb_flags |= IOCB_DSYNC;
+       kiocb_flags |= (__force int) (flags & RWF_SUPPORTED);
        if (flags & RWF_SYNC)
-               kiocb_flags |= (IOCB_DSYNC | IOCB_SYNC);
-       if (flags & RWF_APPEND)
-               kiocb_flags |= IOCB_APPEND;
+               kiocb_flags |= IOCB_DSYNC;
  
        ki->ki_flags |= kiocb_flags;
        return 0;
@@@ -3499,15 -3515,6 +3500,6 @@@ extern int vfs_fadvise(struct file *fil
  extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
                           int advice);
  
- #if defined(CONFIG_IO_URING)
- extern struct sock *io_uring_get_socket(struct file *file);
- #else
- static inline struct sock *io_uring_get_socket(struct file *file)
- {
-       return NULL;
- }
- #endif
  int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
                             unsigned int flags);
  
diff --combined include/linux/sched.h
index d383cf09e78f5b12947b3377510a64c72462029b,8bf2295ebee48f95a2afc33cd9703a7c0a3837af..829b0697d19cca5ad59103aa1cb98894eb7c73fb
@@@ -63,6 -63,7 +63,7 @@@ struct sighand_struct
  struct signal_struct;
  struct task_delay_info;
  struct task_group;
+ struct io_uring_task;
  
  /*
   * Task state bitmask. NOTE! These bits are also
@@@ -935,6 -936,10 +936,10 @@@ struct task_struct 
        /* Open file information: */
        struct files_struct             *files;
  
+ #ifdef CONFIG_IO_URING
+       struct io_uring_task            *io_uring;
+ #endif
        /* Namespaces: */
        struct nsproxy                  *nsproxy;
  
  #endif
  
  #ifdef CONFIG_X86_MCE
 +      void __user                     *mce_vaddr;
 +      __u64                           mce_kflags;
        u64                             mce_addr;
        __u64                           mce_ripv : 1,
                                        mce_whole_page : 1,
@@@ -1491,10 -1494,9 +1496,10 @@@ extern struct pid *cad_pid
  /*
   * Per process flags
   */
 +#define PF_VCPU                       0x00000001      /* I'm a virtual CPU */
  #define PF_IDLE                       0x00000002      /* I am an IDLE thread */
  #define PF_EXITING            0x00000004      /* Getting shut down */
 -#define PF_VCPU                       0x00000010      /* I'm a virtual CPU */
 +#define PF_IO_WORKER          0x00000010      /* Task is an IO worker */
  #define PF_WQ_WORKER          0x00000020      /* I'm a workqueue worker */
  #define PF_FORKNOEXEC         0x00000040      /* Forked but didn't exec */
  #define PF_MCE_PROCESS                0x00000080      /* Process policy on mce errors */
  #define PF_NO_SETAFFINITY     0x04000000      /* Userland is not allowed to meddle with cpus_mask */
  #define PF_MCE_EARLY          0x08000000      /* Early kill for mce process policy */
  #define PF_MEMALLOC_NOCMA     0x10000000      /* All allocation request will have _GFP_MOVABLE cleared */
 -#define PF_IO_WORKER          0x20000000      /* Task is an IO worker */
  #define PF_FREEZER_SKIP               0x40000000      /* Freezer should not count it as freezable */
  #define PF_SUSPEND_TASK               0x80000000      /* This thread called freeze_processes() and should not be frozen */
  
@@@ -2046,7 -2049,6 +2051,7 @@@ const struct sched_avg *sched_trace_rq_
  const struct sched_avg *sched_trace_rq_avg_irq(struct rq *rq);
  
  int sched_trace_rq_cpu(struct rq *rq);
 +int sched_trace_rq_cpu_capacity(struct rq *rq);
  int sched_trace_rq_nr_running(struct rq *rq);
  
  const struct cpumask *sched_trace_rd_span(struct root_domain *rd);
This page took 0.265577 seconds and 4 git commands to generate.