From patchwork Tue Jan 12 21:17:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 361789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17AFEC433E0 for ; Tue, 12 Jan 2021 21:47:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D226423122 for ; Tue, 12 Jan 2021 21:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2436845AbhALVhE (ORCPT ); Tue, 12 Jan 2021 16:37:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2437148AbhALVVx (ORCPT ); Tue, 12 Jan 2021 16:21:53 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC320C061786 for ; Tue, 12 Jan 2021 13:21:12 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id 190so3169544wmz.0 for ; Tue, 12 Jan 2021 13:21:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CDAz73ZJWZh8wN5scIZgGUH6eRmdeJv4r+mh1oZtxf8=; b=ACAKXD6DT0YHGelge9/GSr0NoT/tKJYwWZasy+qWXYP2RSEV5oX6zot4kLUNXSHfNZ PboLSJNcF4D36V3jN2rXAUMdmBFYi3OrmOaU760aRBh9+W2cl5hfTX7l35jT857HAKPq 8qU6Zxw83hebfZERceCvaO/T2WjK3609hjejwDKZmtoag8VOFAhhzGVygrCy+Bk5Bxbf qo0+aWzd0Vf3iLV6NSWwIoAKbUE91RbO/WmdjcGqvWOWtsVbRxoVmU9CsSu9jfL4LgvM IWWcAT4ULYVz55mxL97TVsJlgyGuTJio2Ugj6pcnZS9He7/2WWR4eMIm3J2gf4k3n4UK 8dKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CDAz73ZJWZh8wN5scIZgGUH6eRmdeJv4r+mh1oZtxf8=; b=MnUuEf4IuSyPhZFUxBsJIBYSO8lkkBzNepkxQQLu6O4jGb5abHudeK4FqtNzDSlYUO CgseX9H/3oBLgcxSemTDXD6iLuLpQkgx6d90jRNI+Rh0T/ZBOMCEnCHIVzaWFhZpHCFg D7g6vBJ+Ue0xPGEuwzW2fHXqadD5raAVzNeD4a542LtAvuJHXKLBjYpDNu37TrPz8edo KolfjyuvDQM38x9NpUiBx2ZPEf1LKxglJOFPjCLjk0SOrqKzfn8wpcFRGs4asIkRxrKH SJpsFUdjxaMjB/iY5zuasDZ5qV+viwxHlkKoSlbW6FTr8RZFYNVGB1QA2wsEgvyBDe7w KubA== X-Gm-Message-State: AOAM533OgaRBa0lq/mlzKqt1M+9eVj/lHK2m+v+nRGVskpG913/PRS4l pz8uIr9jainLS9rea+9rGGo= X-Google-Smtp-Source: ABdhPJzyNyJ231+ugZrmQm+I0uJpmV/RNU0pzPJUfi8PTmV4zSHgMsQBj9haw6drVh9ia4bQeuZ/Gg== X-Received: by 2002:a05:600c:4101:: with SMTP id j1mr1028753wmi.55.1610486471542; Tue, 12 Jan 2021 13:21:11 -0800 (PST) Received: from localhost.localdomain ([85.255.235.134]) by smtp.gmail.com with ESMTPSA id y13sm7166093wrl.63.2021.01.12.13.21.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jan 2021 13:21:11 -0800 (PST) From: Pavel Begunkov To: gregkh@linuxfoundation.org Cc: stable@vger.kernel.org, Jens Axboe Subject: [PATCH 5.10-stable 1/3] io_uring: synchronise IOPOLL on task_submit fail Date: Tue, 12 Jan 2021 21:17:24 +0000 Message-Id: X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org commit 81b6d05ccad4f3d8a9dfb091fb46ad6978ee40e4 upstream io_req_task_submit() might be called for IOPOLL, do the fail path under uring_lock to comply with IOPOLL synchronisation based solely on it. Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe --- fs/io_uring.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 1f798c5c4213..3974b4f124b6 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2047,14 +2047,15 @@ static void io_req_task_cancel(struct callback_head *cb) static void __io_req_task_submit(struct io_kiocb *req) { struct io_ring_ctx *ctx = req->ctx; + bool fail; - if (!__io_sq_thread_acquire_mm(ctx)) { - mutex_lock(&ctx->uring_lock); + fail = __io_sq_thread_acquire_mm(ctx); + mutex_lock(&ctx->uring_lock); + if (!fail) __io_queue_sqe(req, NULL); - mutex_unlock(&ctx->uring_lock); - } else { + else __io_req_task_cancel(req, -EFAULT); - } + mutex_unlock(&ctx->uring_lock); } static void io_req_task_submit(struct callback_head *cb) From patchwork Tue Jan 12 21:17:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 361791 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 234E9C0755A for ; Tue, 12 Jan 2021 21:46:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D976723122 for ; Tue, 12 Jan 2021 21:46:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438014AbhALVhE (ORCPT ); Tue, 12 Jan 2021 16:37:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2437150AbhALVVz (ORCPT ); Tue, 12 Jan 2021 16:21:55 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA68DC061795 for ; Tue, 12 Jan 2021 13:21:14 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id c124so3149511wma.5 for ; Tue, 12 Jan 2021 13:21:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VKdW4WkmrvRNVM/Vb5+GBiSNUwdBp53cxZDvRw7T1J8=; b=j0ol5F01puV6aJKdp3QZdw9IlKnKpA3ZMWXbyJe6/LUTijPyedgIk0XMPziDKzP8Cl qcQXiNPj2B88itO+FaPq4y7BhVCHMgOGqDzAi8/kxAvETxKyBFgBq5EIaINHW+tTxkvm iLcAsH7ob0NqHTpZlPC0ANh2tiOQIQB/W2WDz3kFLICzVDAt7dsGiNALVyVGY3gg/KQb MkOLNU5AOeO5CTeN23RzOTgN5QSghw6GevbeK4w7jCCQ7YwBsgAAu9rieRdzrRMcGDre sDP1Imz7xGQd0g+yp8bRq44isv94I88dpFAgC0QYeXUcoJnho/h5Dzi+8eJ5iPPCs5/x 8XMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VKdW4WkmrvRNVM/Vb5+GBiSNUwdBp53cxZDvRw7T1J8=; b=OM8y2kK0TvCTopDbxDPj4yz5hDfb8aGqGDjHrDWdjqI9x2nY0AzNT1Nr7Flv80J9Vp L6cWtUESxXCaJqWDS3e79E8zH9T5t7fFHOj3ld2gCZtskWl8ifZGJLAtyk66U6GaNY/Q 6vM1axzKyp+fr0BafGsOgvDYtI60Kg2O1rSd6fQQYvF2YVb/qmRCJlyRmrN1uXDKXxL5 7IBfn+detGis4H/XdH0xGbZKZyOYMVo05xfl7sXaj6GLduvQQLn1Nfc4thSvj8Eu3t7g +GL0NqakiVW3z2HOd84qe6AKz2CW01sYOG4q1dGaJ11zRagXNmVYibQBWCGf/7BIj3fS EBAQ== X-Gm-Message-State: AOAM530huBItMnXyuWXP2GpyaAdUYXvOshBwakXguXtwG5GWQxFdMlKx JuL7mFblgj3Fptk4ZfN2JssL3KxcL+rPVw== X-Google-Smtp-Source: ABdhPJxqobPcujkf8LJL8IzuocOy5n6J/PkoUrZnIY3uA3pZuuqvnruxLAgGBkrXVQ1JEybrjTzCIw== X-Received: by 2002:a7b:ce17:: with SMTP id m23mr1027654wmc.117.1610486473558; Tue, 12 Jan 2021 13:21:13 -0800 (PST) Received: from localhost.localdomain ([85.255.235.134]) by smtp.gmail.com with ESMTPSA id y13sm7166093wrl.63.2021.01.12.13.21.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jan 2021 13:21:13 -0800 (PST) From: Pavel Begunkov To: gregkh@linuxfoundation.org Cc: stable@vger.kernel.org, Jens Axboe Subject: [PATCH 5.10-stable 3/3] io_uring: patch up IOPOLL overflow_flush sync Date: Tue, 12 Jan 2021 21:17:26 +0000 Message-Id: <033b9defb5fbbf412675489fd1df42e7cbc1cd9a.1610485688.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org commit 6c503150ae33ee19036255cfda0998463613352c upstream IOPOLL skips completion locking but keeps it under uring_lock, thus io_cqring_overflow_flush() and so io_cqring_events() need additional locking with uring_lock in some cases for IOPOLL. Remove __io_cqring_overflow_flush() from io_cqring_events(), introduce a wrapper around flush doing needed synchronisation and call it by hand. Signed-off-by: Pavel Begunkov Signed-off-by: Jens Axboe --- fs/io_uring.c | 78 +++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5ba312ab9978..492492a010a2 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -1625,9 +1625,9 @@ static bool io_match_files(struct io_kiocb *req, } /* Returns true if there are no backlogged entries after the flush */ -static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, - struct task_struct *tsk, - struct files_struct *files) +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; @@ -1681,6 +1681,20 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, return cqe != NULL; } +static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, + struct task_struct *tsk, + struct files_struct *files) +{ + if (test_bit(0, &ctx->cq_check_overflow)) { + /* iopoll syncs against uring_lock, not completion_lock */ + if (ctx->flags & IORING_SETUP_IOPOLL) + mutex_lock(&ctx->uring_lock); + __io_cqring_overflow_flush(ctx, force, tsk, files); + if (ctx->flags & IORING_SETUP_IOPOLL) + mutex_unlock(&ctx->uring_lock); + } +} + static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags) { struct io_ring_ctx *ctx = req->ctx; @@ -2235,22 +2249,10 @@ static void io_double_put_req(struct io_kiocb *req) io_free_req(req); } -static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) +static unsigned io_cqring_events(struct io_ring_ctx *ctx) { struct io_rings *rings = ctx->rings; - if (test_bit(0, &ctx->cq_check_overflow)) { - /* - * noflush == true is from the waitqueue handler, just ensure - * we wake up the task, and the next invocation will flush the - * entries. We cannot safely to it from here. - */ - if (noflush) - return -1U; - - io_cqring_overflow_flush(ctx, false, NULL, NULL); - } - /* See comment at the top of this file */ smp_rmb(); return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); @@ -2475,7 +2477,9 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, long min) * If we do, we can potentially be spinning for commands that * already triggered a CQE (eg in error). */ - if (io_cqring_events(ctx, false)) + if (test_bit(0, &ctx->cq_check_overflow)) + __io_cqring_overflow_flush(ctx, false, NULL, NULL); + if (io_cqring_events(ctx)) break; /* @@ -6578,7 +6582,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) /* if we have a backlog and couldn't flush it all, return BUSY */ if (test_bit(0, &ctx->sq_check_overflow)) { - if (!io_cqring_overflow_flush(ctx, false, NULL, NULL)) + if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL)) return -EBUSY; } @@ -6867,7 +6871,7 @@ struct io_wait_queue { unsigned nr_timeouts; }; -static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) +static inline bool io_should_wake(struct io_wait_queue *iowq) { struct io_ring_ctx *ctx = iowq->ctx; @@ -6876,7 +6880,7 @@ static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) * started waiting. For timeouts, we always want to return to userspace, * regardless of event count. */ - return io_cqring_events(ctx, noflush) >= iowq->to_wait || + return io_cqring_events(ctx) >= iowq->to_wait || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; } @@ -6886,11 +6890,13 @@ static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, wq); - /* use noflush == true, as we can't safely rely on locking context */ - if (!io_should_wake(iowq, true)) - return -1; - - return autoremove_wake_function(curr, mode, wake_flags, key); + /* + * Cannot safely flush overflowed CQEs from here, ensure we wake up + * the task, and the next invocation will do it. + */ + if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow)) + return autoremove_wake_function(curr, mode, wake_flags, key); + return -1; } static int io_run_task_work_sig(void) @@ -6929,7 +6935,8 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, int ret = 0; do { - if (io_cqring_events(ctx, false) >= min_events) + io_cqring_overflow_flush(ctx, false, NULL, NULL); + if (io_cqring_events(ctx) >= min_events) return 0; if (!io_run_task_work()) break; @@ -6951,6 +6958,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); trace_io_uring_cqring_wait(ctx, min_events); do { + io_cqring_overflow_flush(ctx, false, NULL, NULL); prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, TASK_INTERRUPTIBLE); /* make sure we run task_work before checking for signals */ @@ -6959,8 +6967,10 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, continue; else if (ret < 0) break; - if (io_should_wake(&iowq, false)) + if (io_should_wake(&iowq)) break; + if (test_bit(0, &ctx->cq_check_overflow)) + continue; schedule(); } while (1); finish_wait(&ctx->wait, &iowq.wq); @@ -8385,7 +8395,8 @@ static __poll_t io_uring_poll(struct file *file, poll_table *wait) smp_rmb(); if (!io_sqring_full(ctx)) mask |= EPOLLOUT | EPOLLWRNORM; - if (io_cqring_events(ctx, false)) + io_cqring_overflow_flush(ctx, false, NULL, NULL); + if (io_cqring_events(ctx)) mask |= EPOLLIN | EPOLLRDNORM; return mask; @@ -8443,7 +8454,7 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) /* if force is set, the ring is going away. always drop after that */ ctx->cq_overflow_flushed = 1; if (ctx->rings) - io_cqring_overflow_flush(ctx, true, NULL, NULL); + __io_cqring_overflow_flush(ctx, true, NULL, NULL); mutex_unlock(&ctx->uring_lock); io_kill_timeouts(ctx, NULL); @@ -8716,9 +8727,7 @@ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, } io_cancel_defer_files(ctx, task, files); - io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); io_cqring_overflow_flush(ctx, true, task, files); - io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); while (__io_uring_cancel_task_requests(ctx, task, files)) { io_run_task_work(); @@ -9024,13 +9033,8 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, */ ret = 0; if (ctx->flags & IORING_SETUP_SQPOLL) { - if (!list_empty_careful(&ctx->cq_overflow_list)) { - bool needs_lock = ctx->flags & IORING_SETUP_IOPOLL; + io_cqring_overflow_flush(ctx, false, NULL, NULL); - io_ring_submit_lock(ctx, needs_lock); - io_cqring_overflow_flush(ctx, false, NULL, NULL); - io_ring_submit_unlock(ctx, needs_lock); - } if (flags & IORING_ENTER_SQ_WAKEUP) wake_up(&ctx->sq_data->wait); if (flags & IORING_ENTER_SQ_WAIT)