]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - fs/io_uring.c
io_uring: clean cqe filling functions
[mirror_ubuntu-jammy-kernel.git] / fs / io_uring.c
index 5fc3a62eae7228df0a69adb0027bbec8588acaa0..39d39dfaa55ab6fe4a72e41e2f6973cc9c47460b 100644 (file)
@@ -623,10 +623,10 @@ struct io_epoll {
 
 struct io_splice {
        struct file                     *file_out;
-       struct file                     *file_in;
        loff_t                          off_out;
        loff_t                          off_in;
        u64                             len;
+       int                             splice_fd_in;
        unsigned int                    flags;
 };
 
@@ -1079,8 +1079,8 @@ static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
                                         bool cancel_all);
 static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
 
-static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
-                                long res, unsigned int cflags);
+static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags);
+
 static void io_put_req(struct io_kiocb *req);
 static void io_put_req_deferred(struct io_kiocb *req);
 static void io_dismantle_req(struct io_kiocb *req);
@@ -1452,14 +1452,6 @@ static void io_prep_async_work(struct io_kiocb *req)
                if (def->unbound_nonreg_file)
                        req->work.flags |= IO_WQ_WORK_UNBOUND;
        }
-
-       switch (req->opcode) {
-       case IORING_OP_SPLICE:
-       case IORING_OP_TEE:
-               if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
-                       req->work.flags |= IO_WQ_WORK_UNBOUND;
-               break;
-       }
 }
 
 static void io_prep_async_link(struct io_kiocb *req)
@@ -1523,7 +1515,7 @@ static void io_kill_timeout(struct io_kiocb *req, int status)
                atomic_set(&req->ctx->cq_timeouts,
                        atomic_read(&req->ctx->cq_timeouts) + 1);
                list_del_init(&req->timeout.list);
-               io_cqring_fill_event(req->ctx, req->user_data, status, 0);
+               io_fill_cqe_req(req, status, 0);
                io_put_req_deferred(req);
        }
 }
@@ -1546,12 +1538,11 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx)
        __must_hold(&ctx->completion_lock)
 {
        u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
+       struct io_kiocb *req, *tmp;
 
        spin_lock_irq(&ctx->timeout_lock);
-       while (!list_empty(&ctx->timeout_list)) {
+       list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
                u32 events_needed, events_got;
-               struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
-                                               struct io_kiocb, timeout.list);
 
                if (io_is_timeout_noseq(req))
                        break;
@@ -1568,7 +1559,6 @@ static void io_flush_timeouts(struct io_ring_ctx *ctx)
                if (events_got < events_needed)
                        break;
 
-               list_del_init(&req->timeout.list);
                io_kill_timeout(req, 0);
        }
        ctx->cq_last_tm_flush = seq;
@@ -1773,7 +1763,7 @@ static __cold void io_uring_drop_tctx_refs(struct task_struct *task)
 }
 
 static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
-                                    long res, unsigned int cflags)
+                                    s32 res, u32 cflags)
 {
        struct io_overflow_cqe *ocqe;
 
@@ -1800,8 +1790,8 @@ static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
        return true;
 }
 
-static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
-                                         long res, unsigned int cflags)
+static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data,
+                                s32 res, u32 cflags)
 {
        struct io_uring_cqe *cqe;
 
@@ -1822,20 +1812,25 @@ static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data
        return io_cqring_event_overflow(ctx, user_data, res, cflags);
 }
 
-/* not as hot to bloat with inlining */
-static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
-                                         long res, unsigned int cflags)
+static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags)
+{
+       __io_fill_cqe(req->ctx, req->user_data, res, cflags);
+}
+
+static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data,
+                                    s32 res, u32 cflags)
 {
-       return __io_cqring_fill_event(ctx, user_data, res, cflags);
+       ctx->cq_extra++;
+       return __io_fill_cqe(ctx, user_data, res, cflags);
 }
 
-static void io_req_complete_post(struct io_kiocb *req, long res,
-                                unsigned int cflags)
+static void io_req_complete_post(struct io_kiocb *req, s32 res,
+                                u32 cflags)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
        spin_lock(&ctx->completion_lock);
-       __io_cqring_fill_event(ctx, req->user_data, res, cflags);
+       __io_fill_cqe(ctx, req->user_data, res, cflags);
        /*
         * If we're the last reference to this request, add to our locked
         * free_list cache.
@@ -1871,8 +1866,8 @@ static inline bool io_req_needs_clean(struct io_kiocb *req)
        return req->flags & IO_REQ_CLEAN_FLAGS;
 }
 
-static void io_req_complete_state(struct io_kiocb *req, long res,
-                                 unsigned int cflags)
+static inline void io_req_complete_state(struct io_kiocb *req, s32 res,
+                                        u32 cflags)
 {
        if (io_req_needs_clean(req))
                io_clean_op(req);
@@ -1882,7 +1877,7 @@ static void io_req_complete_state(struct io_kiocb *req, long res,
 }
 
 static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
-                                    long res, unsigned cflags)
+                                    s32 res, u32 cflags)
 {
        if (issue_flags & IO_URING_F_COMPLETE_DEFER)
                io_req_complete_state(req, res, cflags);
@@ -1890,12 +1885,12 @@ static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
                io_req_complete_post(req, res, cflags);
 }
 
-static inline void io_req_complete(struct io_kiocb *req, long res)
+static inline void io_req_complete(struct io_kiocb *req, s32 res)
 {
        __io_req_complete(req, 0, res, 0);
 }
 
-static void io_req_complete_failed(struct io_kiocb *req, long res)
+static void io_req_complete_failed(struct io_kiocb *req, s32 res)
 {
        req_set_fail(req);
        io_req_complete_post(req, res, 0);
@@ -2061,8 +2056,7 @@ static bool io_kill_linked_timeout(struct io_kiocb *req)
                link->timeout.head = NULL;
                if (hrtimer_try_to_cancel(&io->timer) != -1) {
                        list_del(&link->timeout.list);
-                       io_cqring_fill_event(link->ctx, link->user_data,
-                                            -ECANCELED, 0);
+                       io_fill_cqe_req(link, -ECANCELED, 0);
                        io_put_req_deferred(link);
                        return true;
                }
@@ -2086,7 +2080,7 @@ static void io_fail_links(struct io_kiocb *req)
                link->link = NULL;
 
                trace_io_uring_fail_link(req, link);
-               io_cqring_fill_event(link->ctx, link->user_data, res, 0);
+               io_fill_cqe_req(link, res, 0);
                io_put_req_deferred(link);
                link = nxt;
        }
@@ -2103,8 +2097,7 @@ static bool io_disarm_next(struct io_kiocb *req)
                req->flags &= ~REQ_F_ARM_LTIMEOUT;
                if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
                        io_remove_next_linked(req);
-                       io_cqring_fill_event(link->ctx, link->user_data,
-                                            -ECANCELED, 0);
+                       io_fill_cqe_req(link, -ECANCELED, 0);
                        io_put_req_deferred(link);
                        posted = true;
                }
@@ -2380,8 +2373,8 @@ static void io_submit_flush_completions(struct io_ring_ctx *ctx)
        for (i = 0; i < nr; i++) {
                struct io_kiocb *req = state->compl_reqs[i];
 
-               __io_cqring_fill_event(ctx, req->user_data, req->result,
-                                       req->compl.cflags);
+               __io_fill_cqe(ctx, req->user_data, req->result,
+                             req->compl.cflags);
        }
        io_commit_cqring(ctx);
        spin_unlock(&ctx->completion_lock);
@@ -2492,8 +2485,7 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
                req = list_first_entry(done, struct io_kiocb, inflight_entry);
                list_del(&req->inflight_entry);
 
-               __io_cqring_fill_event(ctx, req->user_data, req->result,
-                                       io_put_rw_kbuf(req));
+               io_fill_cqe_req(req, req->result, io_put_rw_kbuf(req));
                (*nr_events)++;
 
                if (req_ref_put_and_test(req))
@@ -2696,8 +2688,12 @@ static bool io_rw_should_reissue(struct io_kiocb *req)
 
 static bool __io_complete_rw_common(struct io_kiocb *req, long res)
 {
-       if (req->rw.kiocb.ki_flags & IOCB_WRITE)
+       if (req->rw.kiocb.ki_flags & IOCB_WRITE) {
                kiocb_end_write(req);
+               fsnotify_modify(req->file);
+       } else {
+               fsnotify_access(req->file);
+       }
        if (res != req->result) {
                if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
                    io_rw_should_reissue(req)) {
@@ -2713,7 +2709,7 @@ static bool __io_complete_rw_common(struct io_kiocb *req, long res)
 static void io_req_task_complete(struct io_kiocb *req, bool *locked)
 {
        unsigned int cflags = io_put_rw_kbuf(req);
-       long res = req->result;
+       int res = req->result;
 
        if (*locked) {
                struct io_ring_ctx *ctx = req->ctx;
@@ -2942,15 +2938,24 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                kiocb->ki_complete = io_complete_rw;
        }
 
+       /* used for fixed read/write too - just read unconditionally */
+       req->buf_index = READ_ONCE(sqe->buf_index);
+       req->imu = NULL;
+
        if (req->opcode == IORING_OP_READ_FIXED ||
            req->opcode == IORING_OP_WRITE_FIXED) {
-               req->imu = NULL;
+               struct io_ring_ctx *ctx = req->ctx;
+               u16 index;
+
+               if (unlikely(req->buf_index >= ctx->nr_user_bufs))
+                       return -EFAULT;
+               index = array_index_nospec(req->buf_index, ctx->nr_user_bufs);
+               req->imu = ctx->user_bufs[index];
                io_req_set_rsrc_node(req);
        }
 
        req->rw.addr = READ_ONCE(sqe->addr);
        req->rw.len = READ_ONCE(sqe->len);
-       req->buf_index = READ_ONCE(sqe->buf_index);
        return 0;
 }
 
@@ -3076,18 +3081,9 @@ static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter
 
 static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
 {
-       struct io_ring_ctx *ctx = req->ctx;
-       struct io_mapped_ubuf *imu = req->imu;
-       u16 index, buf_index = req->buf_index;
-
-       if (likely(!imu)) {
-               if (unlikely(buf_index >= ctx->nr_user_bufs))
-                       return -EFAULT;
-               index = array_index_nospec(buf_index, ctx->nr_user_bufs);
-               imu = READ_ONCE(ctx->user_bufs[index]);
-               req->imu = imu;
-       }
-       return __io_import_fixed(req, rw, iter, imu);
+       if (WARN_ON_ONCE(!req->imu))
+               return -EFAULT;
+       return __io_import_fixed(req, rw, iter, req->imu);
 }
 
 static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
@@ -4027,18 +4023,11 @@ static int __io_splice_prep(struct io_kiocb *req,
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
 
-       sp->file_in = NULL;
        sp->len = READ_ONCE(sqe->len);
        sp->flags = READ_ONCE(sqe->splice_flags);
-
        if (unlikely(sp->flags & ~valid_flags))
                return -EINVAL;
-
-       sp->file_in = io_file_get(req->ctx, 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;
+       sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
        return 0;
 }
 
@@ -4053,20 +4042,27 @@ static int io_tee_prep(struct io_kiocb *req,
 static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
 {
        struct io_splice *sp = &req->splice;
-       struct file *in = sp->file_in;
        struct file *out = sp->file_out;
        unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
+       struct file *in;
        long ret = 0;
 
        if (issue_flags & IO_URING_F_NONBLOCK)
                return -EAGAIN;
+
+       in = io_file_get(req->ctx, req, sp->splice_fd_in,
+                                 (sp->flags & SPLICE_F_FD_IN_FIXED));
+       if (!in) {
+               ret = -EBADF;
+               goto done;
+       }
+
        if (sp->len)
                ret = do_tee(in, out, sp->len, flags);
 
        if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
                io_put_file(in);
-       req->flags &= ~REQ_F_NEED_CLEANUP;
-
+done:
        if (ret != sp->len)
                req_set_fail(req);
        io_req_complete(req, ret);
@@ -4085,15 +4081,22 @@ static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
 {
        struct io_splice *sp = &req->splice;
-       struct file *in = sp->file_in;
        struct file *out = sp->file_out;
        unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
        loff_t *poff_in, *poff_out;
+       struct file *in;
        long ret = 0;
 
        if (issue_flags & IO_URING_F_NONBLOCK)
                return -EAGAIN;
 
+       in = io_file_get(req->ctx, req, sp->splice_fd_in,
+                                 (sp->flags & SPLICE_F_FD_IN_FIXED));
+       if (!in) {
+               ret = -EBADF;
+               goto done;
+       }
+
        poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
        poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
 
@@ -4102,8 +4105,7 @@ static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
 
        if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
                io_put_file(in);
-       req->flags &= ~REQ_F_NEED_CLEANUP;
-
+done:
        if (ret != sp->len)
                req_set_fail(req);
        io_req_complete(req, ret);
@@ -4128,9 +4130,6 @@ static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        struct io_ring_ctx *ctx = req->ctx;
 
-       if (!req->file)
-               return -EBADF;
-
        if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
        if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
@@ -4190,6 +4189,8 @@ static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
                                req->sync.len);
        if (ret < 0)
                req_set_fail(req);
+       else
+               fsnotify_modify(req->file);
        io_req_complete(req, ret);
        return 0;
 }
@@ -4793,6 +4794,10 @@ static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
+       if (unlikely(sqe->addr2 || sqe->file_index))
+               return -EINVAL;
+       if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio))
+               return -EINVAL;
 
        sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
        sr->len = READ_ONCE(sqe->len);
@@ -5014,6 +5019,10 @@ static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
        if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
                return -EINVAL;
+       if (unlikely(sqe->addr2 || sqe->file_index))
+               return -EINVAL;
+       if (unlikely(sqe->addr2 || sqe->file_index || sqe->ioprio))
+               return -EINVAL;
 
        sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
        sr->len = READ_ONCE(sqe->len);
@@ -5303,6 +5312,43 @@ struct io_poll_table {
        int error;
 };
 
+static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
+{
+       /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
+       if (req->opcode == IORING_OP_POLL_ADD)
+               return req->async_data;
+       return req->apoll->double_poll;
+}
+
+static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
+{
+       if (req->opcode == IORING_OP_POLL_ADD)
+               return &req->poll;
+       return &req->apoll->poll;
+}
+
+static void io_poll_req_insert(struct io_kiocb *req)
+{
+       struct io_ring_ctx *ctx = req->ctx;
+       struct hlist_head *list;
+
+       list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
+       hlist_add_head(&req->hash_node, list);
+}
+
+static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
+                             wait_queue_func_t wake_func)
+{
+       poll->head = NULL;
+       poll->done = false;
+       poll->canceled = false;
+#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
+       /* mask in events that we always want/need */
+       poll->events = events | IO_POLL_UNMASK;
+       INIT_LIST_HEAD(&poll->wait.entry);
+       init_waitqueue_func_entry(&poll->wait, wake_func);
+}
+
 static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
                           __poll_t mask, io_req_tw_func_t func)
 {
@@ -5351,21 +5397,6 @@ static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
        return false;
 }
 
-static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
-{
-       /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
-       if (req->opcode == IORING_OP_POLL_ADD)
-               return req->async_data;
-       return req->apoll->double_poll;
-}
-
-static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
-{
-       if (req->opcode == IORING_OP_POLL_ADD)
-               return &req->poll;
-       return &req->apoll->poll;
-}
-
 static void io_poll_remove_double(struct io_kiocb *req)
        __must_hold(&req->ctx->completion_lock)
 {
@@ -5400,30 +5431,19 @@ static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
        }
        if (req->poll.events & EPOLLONESHOT)
                flags = 0;
-       if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
+
+       if (!(flags & IORING_CQE_F_MORE)) {
+               io_fill_cqe_req(req, error, flags);
+       } else if (!io_fill_cqe_aux(ctx, req->user_data, error, flags)) {
                req->poll.events |= EPOLLONESHOT;
                flags = 0;
        }
-       if (flags & IORING_CQE_F_MORE)
-               ctx->cq_extra++;
-
        return !(flags & IORING_CQE_F_MORE);
 }
 
-static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
-       __must_hold(&req->ctx->completion_lock)
-{
-       bool done;
-
-       done = __io_poll_complete(req, mask);
-       io_commit_cqring(req->ctx);
-       return done;
-}
-
 static void io_poll_task_func(struct io_kiocb *req, bool *locked)
 {
        struct io_ring_ctx *ctx = req->ctx;
-       struct io_kiocb *nxt;
 
        if (io_poll_rewait(req, &req->poll)) {
                spin_unlock(&ctx->completion_lock);
@@ -5447,11 +5467,8 @@ static void io_poll_task_func(struct io_kiocb *req, bool *locked)
                spin_unlock(&ctx->completion_lock);
                io_cqring_ev_posted(ctx);
 
-               if (done) {
-                       nxt = io_put_req_find_next(req);
-                       if (nxt)
-                               io_req_task_submit(nxt, locked);
-               }
+               if (done)
+                       io_put_req(req);
        }
 }
 
@@ -5490,19 +5507,6 @@ static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
        return 1;
 }
 
-static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
-                             wait_queue_func_t wake_func)
-{
-       poll->head = NULL;
-       poll->done = false;
-       poll->canceled = false;
-#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
-       /* mask in events that we always want/need */
-       poll->events = events | IO_POLL_UNMASK;
-       INIT_LIST_HEAD(&poll->wait.entry);
-       init_waitqueue_func_entry(&poll->wait, wake_func);
-}
-
 static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
                            struct wait_queue_head *head,
                            struct io_poll_iocb **poll_ptr)
@@ -5597,15 +5601,6 @@ static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
        return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
 }
 
-static void io_poll_req_insert(struct io_kiocb *req)
-{
-       struct io_ring_ctx *ctx = req->ctx;
-       struct hlist_head *list;
-
-       list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
-       hlist_add_head(&req->hash_node, list);
-}
-
 static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
                                      struct io_poll_iocb *poll,
                                      struct io_poll_table *ipt, __poll_t mask,
@@ -5665,7 +5660,6 @@ static int io_arm_poll_handler(struct io_kiocb *req)
        struct async_poll *apoll;
        struct io_poll_table ipt;
        __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
-       int rw;
 
        if (!req->file || !file_can_poll(req->file))
                return IO_APOLL_ABORTED;
@@ -5675,7 +5669,6 @@ static int io_arm_poll_handler(struct io_kiocb *req)
                return IO_APOLL_ABORTED;
 
        if (def->pollin) {
-               rw = READ;
                mask |= POLLIN | POLLRDNORM;
 
                /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
@@ -5683,14 +5676,9 @@ static int io_arm_poll_handler(struct io_kiocb *req)
                    (req->sr_msg.msg_flags & MSG_ERRQUEUE))
                        mask &= ~POLLIN;
        } else {
-               rw = WRITE;
                mask |= POLLOUT | POLLWRNORM;
        }
 
-       /* if we can't nonblock try, then no point in arming a poll handler */
-       if (!io_file_supports_nowait(req, rw))
-               return IO_APOLL_ABORTED;
-
        apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
        if (unlikely(!apoll))
                return IO_APOLL_ABORTED;
@@ -5740,9 +5728,9 @@ static bool io_poll_remove_one(struct io_kiocb *req)
        do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
 
        if (do_complete) {
-               io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
-               io_commit_cqring(req->ctx);
                req_set_fail(req);
+               io_fill_cqe_req(req, -ECANCELED, 0);
+               io_commit_cqring(req->ctx);
                io_put_req_deferred(req);
        }
        return do_complete;
@@ -5906,7 +5894,8 @@ static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
 
        if (mask) { /* no async, we'd stolen it */
                ipt.error = 0;
-               done = io_poll_complete(req, mask);
+               done = __io_poll_complete(req, mask);
+               io_commit_cqring(req->ctx);
        }
        spin_unlock(&ctx->completion_lock);
 
@@ -5923,61 +5912,51 @@ static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
        struct io_ring_ctx *ctx = req->ctx;
        struct io_kiocb *preq;
        bool completing;
-       int ret;
+       int ret2, ret = 0;
 
        spin_lock(&ctx->completion_lock);
        preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
        if (!preq) {
                ret = -ENOENT;
-               goto err;
-       }
-
-       if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
-               completing = true;
-               ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
-               goto err;
+fail:
+               spin_unlock(&ctx->completion_lock);
+               goto out;
        }
-
+       io_poll_remove_double(preq);
        /*
         * Don't allow racy completion with singleshot, as we cannot safely
         * update those. For multishot, if we're racing with completion, just
         * let completion re-add it.
         */
-       io_poll_remove_double(preq);
        completing = !__io_poll_remove_one(preq, &preq->poll, false);
        if (completing && (preq->poll.events & EPOLLONESHOT)) {
                ret = -EALREADY;
-               goto err;
-       }
-       /* we now have a detached poll request. reissue. */
-       ret = 0;
-err:
-       if (ret < 0) {
-               spin_unlock(&ctx->completion_lock);
-               req_set_fail(req);
-               io_req_complete(req, ret);
-               return 0;
-       }
-       /* only mask one event flags, keep behavior flags */
-       if (req->poll_update.update_events) {
-               preq->poll.events &= ~0xffff;
-               preq->poll.events |= req->poll_update.events & 0xffff;
-               preq->poll.events |= IO_POLL_UNMASK;
+               goto fail;
        }
-       if (req->poll_update.update_user_data)
-               preq->user_data = req->poll_update.new_user_data;
        spin_unlock(&ctx->completion_lock);
 
-       /* complete update request, we're done with it */
-       io_req_complete(req, ret);
-
-       if (!completing) {
-               ret = io_poll_add(preq, issue_flags);
-               if (ret < 0) {
-                       req_set_fail(preq);
-                       io_req_complete(preq, ret);
+       if (req->poll_update.update_events || req->poll_update.update_user_data) {
+               /* only mask one event flags, keep behavior flags */
+               if (req->poll_update.update_events) {
+                       preq->poll.events &= ~0xffff;
+                       preq->poll.events |= req->poll_update.events & 0xffff;
+                       preq->poll.events |= IO_POLL_UNMASK;
                }
+               if (req->poll_update.update_user_data)
+                       preq->user_data = req->poll_update.new_user_data;
+
+               ret2 = io_poll_add(preq, issue_flags);
+               /* successfully updated, don't complete poll request */
+               if (!ret2)
+                       goto out;
        }
+       req_set_fail(preq);
+       io_req_complete(preq, -ECANCELED);
+out:
+       if (ret < 0)
+               req_set_fail(req);
+       /* complete update request, we're done with it */
+       io_req_complete(req, ret);
        return 0;
 }
 
@@ -6039,7 +6018,7 @@ static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
                return PTR_ERR(req);
 
        req_set_fail(req);
-       io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
+       io_fill_cqe_req(req, -ECANCELED, 0);
        io_put_req_deferred(req);
        return 0;
 }
@@ -6210,6 +6189,7 @@ static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
        if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
                return -EFAULT;
 
+       INIT_LIST_HEAD(&req->timeout.list);
        data->mode = io_translate_timeout_mode(flags);
        hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
 
@@ -6409,6 +6389,7 @@ static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
        up.nr = 0;
        up.tags = 0;
        up.resv = 0;
+       up.resv2 = 0;
 
        io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
        ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
@@ -6652,11 +6633,6 @@ static void io_clean_op(struct io_kiocb *req)
                        kfree(io->free_iov);
                        break;
                        }
-               case IORING_OP_SPLICE:
-               case IORING_OP_TEE:
-                       if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
-                               io_put_file(req->splice.file_in);
-                       break;
                case IORING_OP_OPENAT:
                case IORING_OP_OPENAT2:
                        if (req->open.filename)
@@ -6869,7 +6845,7 @@ static void io_wq_submit_work(struct io_wq_work *work)
                         * forcing a sync submission from here, since we can't
                         * wait for request slots on the block side.
                         */
-                       if (ret != -EAGAIN)
+                       if (ret != -EAGAIN || !(req->ctx->flags & IORING_SETUP_IOPOLL))
                                break;
                        cond_resched();
                } while (1);
@@ -7946,11 +7922,19 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
 
 static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
 {
+       unsigned nr = ctx->nr_user_files;
        int ret;
 
        if (!ctx->file_data)
                return -ENXIO;
+
+       /*
+        * Quiesce may unlock ->uring_lock, and while it's not held
+        * prevent new requests using the table.
+        */
+       ctx->nr_user_files = 0;
        ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
+       ctx->nr_user_files = nr;
        if (!ret)
                __io_sqe_files_unregister(ctx);
        return ret;
@@ -8126,8 +8110,12 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
                refcount_add(skb->truesize, &sk->sk_wmem_alloc);
                skb_queue_head(&sk->sk_receive_queue, skb);
 
-               for (i = 0; i < nr_files; i++)
-                       fput(fpl->fp[i]);
+               for (i = 0; i < nr; i++) {
+                       struct file *file = io_file_from_index(ctx, i + offset);
+
+                       if (file)
+                               fput(file);
+               }
        } else {
                kfree_skb(skb);
                free_uid(fpl->user);
@@ -8256,8 +8244,7 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
 
                        io_ring_submit_lock(ctx, lock_ring);
                        spin_lock(&ctx->completion_lock);
-                       io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
-                       ctx->cq_extra++;
+                       io_fill_cqe_aux(ctx, prsrc->tag, 0, 0);
                        io_commit_cqring(ctx);
                        spin_unlock(&ctx->completion_lock);
                        io_cqring_ev_posted(ctx);
@@ -8419,13 +8406,15 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
 static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
                                 struct io_rsrc_node *node, void *rsrc)
 {
+       u64 *tag_slot = io_get_tag_slot(data, idx);
        struct io_rsrc_put *prsrc;
 
        prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
        if (!prsrc)
                return -ENOMEM;
 
-       prsrc->tag = *io_get_tag_slot(data, idx);
+       prsrc->tag = *tag_slot;
+       *tag_slot = 0;
        prsrc->rsrc = rsrc;
        list_add(&prsrc->list, &node->rsrc_list);
        return 0;
@@ -8493,7 +8482,7 @@ static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
        struct io_ring_ctx *ctx = req->ctx;
        struct io_fixed_file *file_slot;
        struct file *file;
-       int ret, i;
+       int ret;
 
        io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
        ret = -ENXIO;
@@ -8506,8 +8495,8 @@ static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
        if (ret)
                goto out;
 
-       i = array_index_nospec(offset, ctx->nr_user_files);
-       file_slot = io_fixed_file_slot(&ctx->file_table, i);
+       offset = array_index_nospec(offset, ctx->nr_user_files);
+       file_slot = io_fixed_file_slot(&ctx->file_table, offset);
        ret = -EBADF;
        if (!file_slot->file_ptr)
                goto out;
@@ -8563,8 +8552,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 
                if (file_slot->file_ptr) {
                        file = (struct file *)(file_slot->file_ptr & FFS_MASK);
-                       err = io_queue_rsrc_removal(data, up->offset + done,
-                                                   ctx->rsrc_node, file);
+                       err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
                        if (err)
                                break;
                        file_slot->file_ptr = 0;
@@ -8589,7 +8577,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
                                err = -EBADF;
                                break;
                        }
-                       *io_get_tag_slot(data, up->offset + done) = tag;
+                       *io_get_tag_slot(data, i) = tag;
                        io_fixed_file_set(file_slot, file);
                        err = io_sqe_file_register(ctx, file, i);
                        if (err) {
@@ -8905,12 +8893,19 @@ static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
 
 static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
 {
+       unsigned nr = ctx->nr_user_bufs;
        int ret;
 
        if (!ctx->buf_data)
                return -ENXIO;
 
+       /*
+        * Quiesce may unlock ->uring_lock, and while it's not held
+        * prevent new requests using the table.
+        */
+       ctx->nr_user_bufs = 0;
        ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
+       ctx->nr_user_bufs = nr;
        if (!ret)
                __io_sqe_buffers_unregister(ctx);
        return ret;
@@ -9233,7 +9228,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
 
                i = array_index_nospec(offset, ctx->nr_user_bufs);
                if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
-                       err = io_queue_rsrc_removal(ctx->buf_data, offset,
+                       err = io_queue_rsrc_removal(ctx->buf_data, i,
                                                    ctx->rsrc_node, ctx->user_bufs[i]);
                        if (unlikely(err)) {
                                io_buffer_unmap(ctx, &imu);
@@ -9987,6 +9982,8 @@ static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz
                return -EINVAL;
        if (copy_from_user(&arg, argp, sizeof(arg)))
                return -EFAULT;
+       if (arg.pad)
+               return -EINVAL;
        *sig = u64_to_user_ptr(arg.sigmask);
        *argsz = arg.sigmask_sz;
        *ts = u64_to_user_ptr(arg.ts);
@@ -10602,8 +10599,6 @@ static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
        __u32 tmp;
        int err;
 
-       if (up->resv)
-               return -EINVAL;
        if (check_add_overflow(up->offset, nr_args, &tmp))
                return -EOVERFLOW;
        err = io_rsrc_node_switch_start(ctx);
@@ -10629,6 +10624,8 @@ static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
        memset(&up, 0, sizeof(up));
        if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
                return -EFAULT;
+       if (up.resv || up.resv2)
+               return -EINVAL;
        return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
 }
 
@@ -10641,7 +10638,7 @@ static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
                return -EINVAL;
        if (copy_from_user(&up, arg, sizeof(up)))
                return -EFAULT;
-       if (!up.nr || up.resv)
+       if (!up.nr || up.resv || up.resv2)
                return -EINVAL;
        return __io_register_rsrc_update(ctx, type, &up, up.nr);
 }
@@ -10689,7 +10686,15 @@ static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
        if (len > cpumask_size())
                len = cpumask_size();
 
-       if (copy_from_user(new_mask, arg, len)) {
+       if (in_compat_syscall()) {
+               ret = compat_get_bitmap(cpumask_bits(new_mask),
+                                       (const compat_ulong_t __user *)arg,
+                                       len * 8 /* CHAR_BIT */);
+       } else {
+               ret = copy_from_user(new_mask, arg, len);
+       }
+
+       if (ret) {
                free_cpumask_var(new_mask);
                return -EFAULT;
        }