]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - fs/io_uring.c
fuse: unexport fuse_put_request
[linux.git] / fs / io_uring.c
index 4ef62a45045d3da7b5f4a255242584ed9cfb9655..cfb48bd088e12a0bbd8847e2b78e7ba14cb26fd8 100644 (file)
@@ -202,7 +202,7 @@ struct async_list {
 
        struct file             *file;
        off_t                   io_end;
-       size_t                  io_pages;
+       size_t                  io_len;
 };
 
 struct io_ring_ctx {
@@ -231,6 +231,7 @@ struct io_ring_ctx {
        struct task_struct      *sqo_thread;    /* if using sq thread polling */
        struct mm_struct        *sqo_mm;
        wait_queue_head_t       sqo_wait;
+       struct completion       sqo_thread_started;
 
        struct {
                /* CQ ring */
@@ -322,6 +323,7 @@ struct io_kiocb {
 
        struct io_ring_ctx      *ctx;
        struct list_head        list;
+       struct list_head        link_list;
        unsigned int            flags;
        refcount_t              refs;
 #define REQ_F_NOWAIT           1       /* must not punt to workers */
@@ -330,8 +332,11 @@ struct io_kiocb {
 #define REQ_F_SEQ_PREV         8       /* sequential with previous */
 #define REQ_F_IO_DRAIN         16      /* drain existing IO first */
 #define REQ_F_IO_DRAINED       32      /* drain done */
+#define REQ_F_LINK             64      /* linked sqes */
+#define REQ_F_LINK_DONE                128     /* linked sqes done */
+#define REQ_F_FAIL_LINK                256     /* fail rest of links */
        u64                     user_data;
-       u32                     error;  /* iopoll result from callback */
+       u32                     result;
        u32                     sequence;
 
        struct work_struct      work;
@@ -395,7 +400,8 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
        if (!ctx)
                return NULL;
 
-       if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
+       if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
+                           PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
                kfree(ctx);
                return NULL;
        }
@@ -403,6 +409,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
        ctx->flags = p->flags;
        init_waitqueue_head(&ctx->cq_wait);
        init_completion(&ctx->ctx_done);
+       init_completion(&ctx->sqo_thread_started);
        mutex_init(&ctx->uring_lock);
        init_waitqueue_head(&ctx->wait);
        for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
@@ -423,7 +430,7 @@ static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
        if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
                return false;
 
-       return req->sequence > ctx->cached_cq_tail + ctx->sq_ring->dropped;
+       return req->sequence != ctx->cached_cq_tail + ctx->sq_ring->dropped;
 }
 
 static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
@@ -584,6 +591,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
        req->flags = 0;
        /* one is dropped after submission, the other at completion */
        refcount_set(&req->refs, 2);
+       req->result = 0;
        return req;
 out:
        io_ring_drop_ctx_refs(ctx, 1);
@@ -599,7 +607,7 @@ static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
        }
 }
 
-static void io_free_req(struct io_kiocb *req)
+static void __io_free_req(struct io_kiocb *req)
 {
        if (req->file && !(req->flags & REQ_F_FIXED_FILE))
                fput(req->file);
@@ -607,12 +615,77 @@ static void io_free_req(struct io_kiocb *req)
        kmem_cache_free(req_cachep, req);
 }
 
+static void io_req_link_next(struct io_kiocb *req)
+{
+       struct io_kiocb *nxt;
+
+       /*
+        * The list should never be empty when we are called here. But could
+        * potentially happen if the chain is messed up, check to be on the
+        * safe side.
+        */
+       nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
+       if (nxt) {
+               list_del(&nxt->list);
+               if (!list_empty(&req->link_list)) {
+                       INIT_LIST_HEAD(&nxt->link_list);
+                       list_splice(&req->link_list, &nxt->link_list);
+                       nxt->flags |= REQ_F_LINK;
+               }
+
+               nxt->flags |= REQ_F_LINK_DONE;
+               INIT_WORK(&nxt->work, io_sq_wq_submit_work);
+               queue_work(req->ctx->sqo_wq, &nxt->work);
+       }
+}
+
+/*
+ * Called if REQ_F_LINK is set, and we fail the head request
+ */
+static void io_fail_links(struct io_kiocb *req)
+{
+       struct io_kiocb *link;
+
+       while (!list_empty(&req->link_list)) {
+               link = list_first_entry(&req->link_list, struct io_kiocb, list);
+               list_del(&link->list);
+
+               io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
+               __io_free_req(link);
+       }
+}
+
+static void io_free_req(struct io_kiocb *req)
+{
+       /*
+        * If LINK is set, we have dependent requests in this chain. If we
+        * didn't fail this request, queue the first one up, moving any other
+        * dependencies to the next request. In case of failure, fail the rest
+        * of the chain.
+        */
+       if (req->flags & REQ_F_LINK) {
+               if (req->flags & REQ_F_FAIL_LINK)
+                       io_fail_links(req);
+               else
+                       io_req_link_next(req);
+       }
+
+       __io_free_req(req);
+}
+
 static void io_put_req(struct io_kiocb *req)
 {
        if (refcount_dec_and_test(&req->refs))
                io_free_req(req);
 }
 
+static unsigned io_cqring_events(struct io_cq_ring *ring)
+{
+       /* See comment at the top of this file */
+       smp_rmb();
+       return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
+}
+
 /*
  * Find and free completed poll iocbs
  */
@@ -628,16 +701,17 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
                req = list_first_entry(done, struct io_kiocb, list);
                list_del(&req->list);
 
-               io_cqring_fill_event(ctx, req->user_data, req->error);
+               io_cqring_fill_event(ctx, req->user_data, req->result);
                (*nr_events)++;
 
                if (refcount_dec_and_test(&req->refs)) {
                        /* If we're not using fixed files, we have to pair the
                         * completion part with the file put. Use regular
                         * completions for those, only batch free for fixed
-                        * file.
+                        * file and non-linked commands.
                         */
-                       if (req->flags & REQ_F_FIXED_FILE) {
+                       if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
+                           REQ_F_FIXED_FILE) {
                                reqs[to_free++] = req;
                                if (to_free == ARRAY_SIZE(reqs))
                                        io_free_req_many(ctx, reqs, &to_free);
@@ -704,7 +778,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
 static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
                                long min)
 {
-       while (!list_empty(&ctx->poll_list)) {
+       while (!list_empty(&ctx->poll_list) && !need_resched()) {
                int ret;
 
                ret = io_do_iopoll(ctx, nr_events, min);
@@ -731,6 +805,12 @@ static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
                unsigned int nr_events = 0;
 
                io_iopoll_getevents(ctx, &nr_events, 1);
+
+               /*
+                * Ensure we allow local-to-the-cpu processing to take place,
+                * in this case we need to ensure that we reap all events.
+                */
+               cond_resched();
        }
        mutex_unlock(&ctx->uring_lock);
 }
@@ -738,11 +818,42 @@ static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
 static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
                           long min)
 {
-       int ret = 0;
+       int iters, ret = 0;
+
+       /*
+        * We disallow the app entering submit/complete with polling, but we
+        * still need to lock the ring to prevent racing with polled issue
+        * that got punted to a workqueue.
+        */
+       mutex_lock(&ctx->uring_lock);
 
+       iters = 0;
        do {
                int tmin = 0;
 
+               /*
+                * Don't enter poll loop if we already have events pending.
+                * If we do, we can potentially be spinning for commands that
+                * already triggered a CQE (eg in error).
+                */
+               if (io_cqring_events(ctx->cq_ring))
+                       break;
+
+               /*
+                * If a submit got punted to a workqueue, we can have the
+                * application entering polling for a command before it gets
+                * issued. That app will hold the uring_lock for the duration
+                * of the poll right here, so we need to take a breather every
+                * now and then to ensure that the issue has a chance to add
+                * the poll to the issued list. Otherwise we can spin here
+                * forever, while the workqueue is stuck trying to acquire the
+                * very same mutex.
+                */
+               if (!(++iters & 7)) {
+                       mutex_unlock(&ctx->uring_lock);
+                       mutex_lock(&ctx->uring_lock);
+               }
+
                if (*nr_events < min)
                        tmin = min - *nr_events;
 
@@ -752,6 +863,7 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
                ret = 0;
        } while (min && !*nr_events && !need_resched());
 
+       mutex_unlock(&ctx->uring_lock);
        return ret;
 }
 
@@ -776,6 +888,8 @@ static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
 
        kiocb_end_write(kiocb);
 
+       if ((req->flags & REQ_F_LINK) && res != req->result)
+               req->flags |= REQ_F_FAIL_LINK;
        io_cqring_add_event(req->ctx, req->user_data, res);
        io_put_req(req);
 }
@@ -786,7 +900,9 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
 
        kiocb_end_write(kiocb);
 
-       req->error = res;
+       if ((req->flags & REQ_F_LINK) && res != req->result)
+               req->flags |= REQ_F_FAIL_LINK;
+       req->result = res;
        if (res != -EAGAIN)
                req->flags |= REQ_F_IOPOLL_COMPLETED;
 }
@@ -929,7 +1045,6 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
                    !kiocb->ki_filp->f_op->iopoll)
                        return -EOPNOTSUPP;
 
-               req->error = 0;
                kiocb->ki_flags |= IOCB_HIPRI;
                kiocb->ki_complete = io_complete_rw_iopoll;
        } else {
@@ -996,17 +1111,48 @@ static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
         */
        offset = buf_addr - imu->ubuf;
        iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
-       if (offset)
-               iov_iter_advance(iter, offset);
 
-       /* don't drop a reference to these pages */
-       iter->type |= ITER_BVEC_FLAG_NO_REF;
+       if (offset) {
+               /*
+                * Don't use iov_iter_advance() here, as it's really slow for
+                * using the latter parts of a big fixed buffer - it iterates
+                * over each segment manually. We can cheat a bit here, because
+                * we know that:
+                *
+                * 1) it's a BVEC iter, we set it up
+                * 2) all bvecs are PAGE_SIZE in size, except potentially the
+                *    first and last bvec
+                *
+                * So just find our index, and adjust the iterator afterwards.
+                * If the offset is within the first bvec (or the whole first
+                * bvec, just use iov_iter_advance(). This makes it easier
+                * since we can just skip the first segment, which may not
+                * be PAGE_SIZE aligned.
+                */
+               const struct bio_vec *bvec = imu->bvec;
+
+               if (offset <= bvec->bv_len) {
+                       iov_iter_advance(iter, offset);
+               } else {
+                       unsigned long seg_skip;
+
+                       /* skip first vec */
+                       offset -= bvec->bv_len;
+                       seg_skip = 1 + (offset >> PAGE_SHIFT);
+
+                       iter->bvec = bvec + seg_skip;
+                       iter->nr_segs -= seg_skip;
+                       iter->count -= bvec->bv_len + offset;
+                       iter->iov_offset = offset & ~PAGE_MASK;
+               }
+       }
+
        return 0;
 }
 
-static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
-                          const struct sqe_submit *s, struct iovec **iovec,
-                          struct iov_iter *iter)
+static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
+                              const struct sqe_submit *s, struct iovec **iovec,
+                              struct iov_iter *iter)
 {
        const struct io_uring_sqe *sqe = s->sqe;
        void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -1024,7 +1170,7 @@ static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
        opcode = READ_ONCE(sqe->opcode);
        if (opcode == IORING_OP_READ_FIXED ||
            opcode == IORING_OP_WRITE_FIXED) {
-               int ret = io_import_fixed(ctx, rw, sqe, iter);
+               ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
                *iovec = NULL;
                return ret;
        }
@@ -1055,28 +1201,26 @@ static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
        off_t io_end = kiocb->ki_pos + len;
 
        if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
-               unsigned long max_pages;
+               unsigned long max_bytes;
 
                /* Use 8x RA size as a decent limiter for both reads/writes */
-               max_pages = filp->f_ra.ra_pages;
-               if (!max_pages)
-                       max_pages = VM_READAHEAD_PAGES;
-               max_pages *= 8;
-
-               /* If max pages are exceeded, reset the state */
-               len >>= PAGE_SHIFT;
-               if (async_list->io_pages + len <= max_pages) {
+               max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
+               if (!max_bytes)
+                       max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
+
+               /* If max len are exceeded, reset the state */
+               if (async_list->io_len + len <= max_bytes) {
                        req->flags |= REQ_F_SEQ_PREV;
-                       async_list->io_pages += len;
+                       async_list->io_len += len;
                } else {
                        io_end = 0;
-                       async_list->io_pages = 0;
+                       async_list->io_len = 0;
                }
        }
 
        /* New file? Reset state. */
        if (async_list->file != filp) {
-               async_list->io_pages = 0;
+               async_list->io_len = 0;
                async_list->file = filp;
        }
        async_list->io_end = io_end;
@@ -1090,7 +1234,7 @@ static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
        struct iov_iter iter;
        struct file *file;
        size_t iov_count;
-       int ret;
+       ssize_t read_size, ret;
 
        ret = io_prep_rw(req, s, force_nonblock);
        if (ret)
@@ -1103,16 +1247,30 @@ static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
                return -EINVAL;
 
        ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
-       if (ret)
+       if (ret < 0)
                return ret;
 
+       read_size = ret;
+       if (req->flags & REQ_F_LINK)
+               req->result = read_size;
+
        iov_count = iov_iter_count(&iter);
        ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
        if (!ret) {
                ssize_t ret2;
 
-               /* Catch -EAGAIN return for forced non-blocking submission */
                ret2 = call_read_iter(file, kiocb, &iter);
+               /*
+                * In case of a short read, punt to async. This can happen
+                * if we have data partially cached. Alternatively we can
+                * return the short read, in which case the application will
+                * need to issue another SQE and wait for it. That SQE will
+                * need async punt anyway, so it's more efficient to do it
+                * here.
+                */
+               if (force_nonblock && ret2 > 0 && ret2 < read_size)
+                       ret2 = -EAGAIN;
+               /* Catch -EAGAIN return for forced non-blocking submission */
                if (!force_nonblock || ret2 != -EAGAIN) {
                        io_rw_done(kiocb, ret2);
                } else {
@@ -1137,7 +1295,7 @@ static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
        struct iov_iter iter;
        struct file *file;
        size_t iov_count;
-       int ret;
+       ssize_t ret;
 
        ret = io_prep_rw(req, s, force_nonblock);
        if (ret)
@@ -1150,9 +1308,12 @@ static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
                return -EINVAL;
 
        ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
-       if (ret)
+       if (ret < 0)
                return ret;
 
+       if (req->flags & REQ_F_LINK)
+               req->result = ret;
+
        iov_count = iov_iter_count(&iter);
 
        ret = -EAGAIN;
@@ -1256,6 +1417,8 @@ static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
                                end > 0 ? end : LLONG_MAX,
                                fsync_flags & IORING_FSYNC_DATASYNC);
 
+       if (ret < 0 && (req->flags & REQ_F_LINK))
+               req->flags |= REQ_F_FAIL_LINK;
        io_cqring_add_event(req->ctx, sqe->user_data, ret);
        io_put_req(req);
        return 0;
@@ -1300,10 +1463,69 @@ static int io_sync_file_range(struct io_kiocb *req,
 
        ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
 
+       if (ret < 0 && (req->flags & REQ_F_LINK))
+               req->flags |= REQ_F_FAIL_LINK;
+       io_cqring_add_event(req->ctx, sqe->user_data, ret);
+       io_put_req(req);
+       return 0;
+}
+
+#if defined(CONFIG_NET)
+static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
+                          bool force_nonblock,
+                  long (*fn)(struct socket *, struct user_msghdr __user *,
+                               unsigned int))
+{
+       struct socket *sock;
+       int ret;
+
+       if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
+               return -EINVAL;
+
+       sock = sock_from_file(req->file, &ret);
+       if (sock) {
+               struct user_msghdr __user *msg;
+               unsigned flags;
+
+               flags = READ_ONCE(sqe->msg_flags);
+               if (flags & MSG_DONTWAIT)
+                       req->flags |= REQ_F_NOWAIT;
+               else if (force_nonblock)
+                       flags |= MSG_DONTWAIT;
+
+               msg = (struct user_msghdr __user *) (unsigned long)
+                       READ_ONCE(sqe->addr);
+
+               ret = fn(sock, msg, flags);
+               if (force_nonblock && ret == -EAGAIN)
+                       return ret;
+       }
+
        io_cqring_add_event(req->ctx, sqe->user_data, ret);
        io_put_req(req);
        return 0;
 }
+#endif
+
+static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
+                     bool force_nonblock)
+{
+#if defined(CONFIG_NET)
+       return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
+#else
+       return -EOPNOTSUPP;
+#endif
+}
+
+static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
+                     bool force_nonblock)
+{
+#if defined(CONFIG_NET)
+       return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
+#else
+       return -EOPNOTSUPP;
+#endif
+}
 
 static void io_poll_remove_one(struct io_kiocb *req)
 {
@@ -1487,6 +1709,8 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
        INIT_LIST_HEAD(&poll->wait.entry);
        init_waitqueue_func_entry(&poll->wait, io_poll_wake);
 
+       INIT_LIST_HEAD(&req->list);
+
        mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
 
        spin_lock_irq(&ctx->completion_lock);
@@ -1552,9 +1776,10 @@ static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
 {
        int ret, opcode;
 
+       req->user_data = READ_ONCE(s->sqe->user_data);
+
        if (unlikely(s->index >= ctx->sq_entries))
                return -EINVAL;
-       req->user_data = READ_ONCE(s->sqe->user_data);
 
        opcode = READ_ONCE(s->sqe->opcode);
        switch (opcode) {
@@ -1589,6 +1814,12 @@ static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
        case IORING_OP_SYNC_FILE_RANGE:
                ret = io_sync_file_range(req, s->sqe, force_nonblock);
                break;
+       case IORING_OP_SENDMSG:
+               ret = io_sendmsg(req, s->sqe, force_nonblock);
+               break;
+       case IORING_OP_RECVMSG:
+               ret = io_recvmsg(req, s->sqe, force_nonblock);
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -1598,7 +1829,7 @@ static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
                return ret;
 
        if (ctx->flags & IORING_SETUP_IOPOLL) {
-               if (req->error == -EAGAIN)
+               if (req->result == -EAGAIN)
                        return -EAGAIN;
 
                /* workqueue context doesn't hold uring_lock, grab it now */
@@ -1650,6 +1881,7 @@ static void io_sq_wq_submit_work(struct work_struct *work)
        do {
                struct sqe_submit *s = &req->submit;
                const struct io_uring_sqe *sqe = s->sqe;
+               unsigned int flags = req->flags;
 
                /* Ensure we clear previously set non-block flag */
                req->rw.ki_flags &= ~IOCB_NOWAIT;
@@ -1694,6 +1926,10 @@ static void io_sq_wq_submit_work(struct work_struct *work)
                /* async context always use a copy of the sqe */
                kfree(sqe);
 
+               /* req from defer and link list needn't decrease async cnt */
+               if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
+                       goto out;
+
                if (!async_list)
                        break;
                if (!list_empty(&req_list)) {
@@ -1741,6 +1977,7 @@ static void io_sq_wq_submit_work(struct work_struct *work)
                }
        }
 
+out:
        if (cur_mm) {
                set_fs(old_fs);
                unuse_mm(cur_mm);
@@ -1767,6 +2004,10 @@ static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
        ret = true;
        spin_lock(&list->lock);
        list_add_tail(&req->list, &list->list);
+       /*
+        * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
+        */
+       smp_mb();
        if (!atomic_read(&list->cnt)) {
                list_del_init(&req->list);
                ret = false;
@@ -1822,29 +2063,18 @@ static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
        return 0;
 }
 
-static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
-                        struct io_submit_state *state)
+static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
+                       struct sqe_submit *s)
 {
-       struct io_kiocb *req;
        int ret;
 
-       /* enforce forwards compatibility on users */
-       if (unlikely(s->sqe->flags & ~(IOSQE_FIXED_FILE | IOSQE_IO_DRAIN)))
-               return -EINVAL;
-
-       req = io_get_req(ctx, state);
-       if (unlikely(!req))
-               return -EAGAIN;
-
-       ret = io_req_set_file(ctx, s, state, req);
-       if (unlikely(ret))
-               goto out;
-
        ret = io_req_defer(ctx, req, s->sqe);
        if (ret) {
-               if (ret == -EIOCBQUEUED)
-                       ret = 0;
-               return ret;
+               if (ret != -EIOCBQUEUED) {
+                       io_free_req(req);
+                       io_cqring_add_event(ctx, s->sqe->user_data, ret);
+               }
+               return 0;
        }
 
        ret = __io_submit_sqe(ctx, req, s, true);
@@ -1869,24 +2099,86 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
 
                        /*
                         * Queued up for async execution, worker will release
-                        * submit reference when the iocb is actually
-                        * submitted.
+                        * submit reference when the iocb is actually submitted.
                         */
                        return 0;
                }
        }
 
-out:
        /* drop submission reference */
        io_put_req(req);
 
        /* and drop final reference, if we failed */
-       if (ret)
+       if (ret) {
+               io_cqring_add_event(ctx, req->user_data, ret);
+               if (req->flags & REQ_F_LINK)
+                       req->flags |= REQ_F_FAIL_LINK;
                io_put_req(req);
+       }
 
        return ret;
 }
 
+#define SQE_VALID_FLAGS        (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
+
+static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
+                         struct io_submit_state *state, struct io_kiocb **link)
+{
+       struct io_uring_sqe *sqe_copy;
+       struct io_kiocb *req;
+       int ret;
+
+       /* enforce forwards compatibility on users */
+       if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       req = io_get_req(ctx, state);
+       if (unlikely(!req)) {
+               ret = -EAGAIN;
+               goto err;
+       }
+
+       ret = io_req_set_file(ctx, s, state, req);
+       if (unlikely(ret)) {
+err_req:
+               io_free_req(req);
+err:
+               io_cqring_add_event(ctx, s->sqe->user_data, ret);
+               return;
+       }
+
+       /*
+        * If we already have a head request, queue this one for async
+        * submittal once the head completes. If we don't have a head but
+        * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
+        * submitted sync once the chain is complete. If none of those
+        * conditions are true (normal request), then just queue it.
+        */
+       if (*link) {
+               struct io_kiocb *prev = *link;
+
+               sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
+               if (!sqe_copy) {
+                       ret = -EAGAIN;
+                       goto err_req;
+               }
+
+               s->sqe = sqe_copy;
+               memcpy(&req->submit, s, sizeof(*s));
+               list_add_tail(&req->list, &prev->link_list);
+       } else if (s->sqe->flags & IOSQE_IO_LINK) {
+               req->flags |= REQ_F_LINK;
+
+               memcpy(&req->submit, s, sizeof(*s));
+               INIT_LIST_HEAD(&req->link_list);
+               *link = req;
+       } else {
+               io_queue_sqe(ctx, req, s);
+       }
+}
+
 /*
  * Batched submission is done, ensure local IO is flushed out.
  */
@@ -1969,7 +2261,9 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
                          unsigned int nr, bool has_user, bool mm_fault)
 {
        struct io_submit_state state, *statep = NULL;
-       int ret, i, submitted = 0;
+       struct io_kiocb *link = NULL;
+       bool prev_was_link = false;
+       int i, submitted = 0;
 
        if (nr > IO_PLUG_THRESHOLD) {
                io_submit_state_start(&state, ctx, nr);
@@ -1977,22 +2271,30 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
        }
 
        for (i = 0; i < nr; i++) {
+               /*
+                * If previous wasn't linked and we have a linked command,
+                * that's the end of the chain. Submit the previous link.
+                */
+               if (!prev_was_link && link) {
+                       io_queue_sqe(ctx, link, &link->submit);
+                       link = NULL;
+               }
+               prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0;
+
                if (unlikely(mm_fault)) {
-                       ret = -EFAULT;
+                       io_cqring_add_event(ctx, sqes[i].sqe->user_data,
+                                               -EFAULT);
                } else {
                        sqes[i].has_user = has_user;
                        sqes[i].needs_lock = true;
                        sqes[i].needs_fixed_file = true;
-                       ret = io_submit_sqe(ctx, &sqes[i], statep);
-               }
-               if (!ret) {
+                       io_submit_sqe(ctx, &sqes[i], statep, &link);
                        submitted++;
-                       continue;
                }
-
-               io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret);
        }
 
+       if (link)
+               io_queue_sqe(ctx, link, &link->submit);
        if (statep)
                io_submit_state_end(&state);
 
@@ -2009,6 +2311,8 @@ static int io_sq_thread(void *data)
        unsigned inflight;
        unsigned long timeout;
 
+       complete(&ctx->sqo_thread_started);
+
        old_fs = get_fs();
        set_fs(USER_DS);
 
@@ -2021,15 +2325,7 @@ static int io_sq_thread(void *data)
                        unsigned nr_events = 0;
 
                        if (ctx->flags & IORING_SETUP_IOPOLL) {
-                               /*
-                                * We disallow the app entering submit/complete
-                                * with polling, but we still need to lock the
-                                * ring to prevent racing with polled issue
-                                * that got punted to a workqueue.
-                                */
-                               mutex_lock(&ctx->uring_lock);
                                io_iopoll_check(ctx, &nr_events, 0);
-                               mutex_unlock(&ctx->uring_lock);
                        } else {
                                /*
                                 * Normal IO, just pretend everything completed.
@@ -2133,6 +2429,8 @@ static int io_sq_thread(void *data)
 static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
 {
        struct io_submit_state state, *statep = NULL;
+       struct io_kiocb *link = NULL;
+       bool prev_was_link = false;
        int i, submit = 0;
 
        if (to_submit > IO_PLUG_THRESHOLD) {
@@ -2142,35 +2440,36 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
 
        for (i = 0; i < to_submit; i++) {
                struct sqe_submit s;
-               int ret;
 
                if (!io_get_sqring(ctx, &s))
                        break;
 
+               /*
+                * If previous wasn't linked and we have a linked command,
+                * that's the end of the chain. Submit the previous link.
+                */
+               if (!prev_was_link && link) {
+                       io_queue_sqe(ctx, link, &link->submit);
+                       link = NULL;
+               }
+               prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
+
                s.has_user = true;
                s.needs_lock = false;
                s.needs_fixed_file = false;
                submit++;
-
-               ret = io_submit_sqe(ctx, &s, statep);
-               if (ret)
-                       io_cqring_add_event(ctx, s.sqe->user_data, ret);
+               io_submit_sqe(ctx, &s, statep, &link);
        }
        io_commit_sqring(ctx);
 
+       if (link)
+               io_queue_sqe(ctx, link, &link->submit);
        if (statep)
                io_submit_state_end(statep);
 
        return submit;
 }
 
-static unsigned io_cqring_events(struct io_cq_ring *ring)
-{
-       /* See comment at the top of this file */
-       smp_rmb();
-       return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
-}
-
 /*
  * 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.
@@ -2179,7 +2478,6 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
                          const sigset_t __user *sig, size_t sigsz)
 {
        struct io_cq_ring *ring = ctx->cq_ring;
-       sigset_t ksigmask, sigsaved;
        int ret;
 
        if (io_cqring_events(ring) >= min_events)
@@ -2189,21 +2487,17 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
 #ifdef CONFIG_COMPAT
                if (in_compat_syscall())
                        ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
-                                                     &ksigmask, &sigsaved, sigsz);
+                                                     sigsz);
                else
 #endif
-                       ret = set_user_sigmask(sig, &ksigmask,
-                                              &sigsaved, sigsz);
+                       ret = set_user_sigmask(sig, sigsz);
 
                if (ret)
                        return ret;
        }
 
        ret = wait_event_interruptible(ctx->wait, io_cqring_events(ring) >= min_events);
-
-       if (sig)
-               restore_user_sigmask(sig, &sigsaved, ret == -ERESTARTSYS);
-
+       restore_saved_sigmask_unless(ret == -ERESTARTSYS);
        if (ret == -ERESTARTSYS)
                ret = -EINTR;
 
@@ -2243,6 +2537,7 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
 static void io_sq_thread_stop(struct io_ring_ctx *ctx)
 {
        if (ctx->sqo_thread) {
+               wait_for_completion(&ctx->sqo_thread_started);
                /*
                 * The park is a bit of a work-around, without it we get
                 * warning spews on shutdown with SQPOLL set and affinity
@@ -2925,9 +3220,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
                min_complete = min(min_complete, ctx->cq_entries);
 
                if (ctx->flags & IORING_SETUP_IOPOLL) {
-                       mutex_lock(&ctx->uring_lock);
                        ret = io_iopoll_check(ctx, &nr_events, min_complete);
-                       mutex_unlock(&ctx->uring_lock);
                } else {
                        ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
                }