From patchwork Sun Dec 6 22:22:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 339598 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.7 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 1D48AC433FE for ; Sun, 6 Dec 2020 22:27:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D29B92310B for ; Sun, 6 Dec 2020 22:27:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727874AbgLFW0z (ORCPT ); Sun, 6 Dec 2020 17:26:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727474AbgLFW0z (ORCPT ); Sun, 6 Dec 2020 17:26:55 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90484C0613D2; Sun, 6 Dec 2020 14:26:14 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id v14so9958579wml.1; Sun, 06 Dec 2020 14:26: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=7HHLLOjkccgnUtL12PevMC3GpBirCPeoDGjbB/DnT+Y=; b=NeM8aHM9j9LZGo3lw/4Bpv+VLP4pTNkPEW+zFH27MizLN2o/U0trzEWclyyMxCNnp+ Ta1AMl0GMlUCiD+Z6VKCKeYi6vQ+2fHgCGTWMzb7TaCiV7FB6cI7wpGMf/gtD6FUdWN4 GXt3xTxyZTPOtXa9/pyfHzIo9pgVk5RfY4Mqc8h7mTZMhg/jymJbGojSChm86EzZae5g lv4Scj4AW9SJmApzDsHcVi7yqPJajWxCoh8oLHuyqe2dIFREjHr0jT148s8/7+bYmUm6 75IRcVhR2qXhnqe5KfU3i7ibwG/c/NYMfDMxdV1CuJZQXxqEEz00WtAL1vAIPEe6tDP7 QATQ== 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=7HHLLOjkccgnUtL12PevMC3GpBirCPeoDGjbB/DnT+Y=; b=Jqo2VX0vyfilYmL9qUNWtWVEflAB1dqGv5P8aT2QmKAwid3bUOGyWlgsrB5EpTa545 bLRXNLa1h4VQRn99PAicaI2St1V0/HP5S7pQZn/unHQVVL8zlbGWuoWRbyV7+zYCJdpn +v74E278P8iD/Ct/6FtAvCQcRFJoR9vJDf2QiWK1ICgL8qMncAcUZThWC8vwcCLj8HnG Dmnt9cnVS0F/ZR2Rr2V5I0z0o+dgraOkFzRadnpdEiqILfDfgfy0AtZq5lthg8nlvoBZ +JjSJvjCW3u7kCCv+hUztCIS20SByTeCsBNt8O+6QaF6tihzmcfVbeGMZQbr7otJJdaD 6I4Q== X-Gm-Message-State: AOAM533BISZtO890c5KhczoLd9Mj07+tOkwbIa7GqRyCnW/3biZG2P8S yYIZQ3l+/HOCryKe7aQa/w3VZMAT06J/UQ== X-Google-Smtp-Source: ABdhPJxvblVV9KczXNy1bATM6LPc7rsC/mmb+Ug8kijIWdP8JbO4P87zMQb+edotAN1X/FUPnP4/xw== X-Received: by 2002:a1c:25c3:: with SMTP id l186mr14993663wml.113.1607293573246; Sun, 06 Dec 2020 14:26:13 -0800 (PST) Received: from localhost.localdomain ([185.69.145.92]) by smtp.gmail.com with ESMTPSA id h20sm11284917wmb.29.2020.12.06.14.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2020 14:26:12 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org Cc: Xiaoguang Wang , stable@vger.kernel.org, Abaci Fuzz , Joseph Qi Subject: [PATCH 5.10 1/5] io_uring: always let io_iopoll_complete() complete polled io. Date: Sun, 6 Dec 2020 22:22:42 +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 From: Xiaoguang Wang Abaci Fuzz reported a double-free or invalid-free BUG in io_commit_cqring(): [ 95.504842] BUG: KASAN: double-free or invalid-free in io_commit_cqring+0x3ec/0x8e0 [ 95.505921] [ 95.506225] CPU: 0 PID: 4037 Comm: io_wqe_worker-0 Tainted: G B W 5.10.0-rc5+ #1 [ 95.507434] Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 [ 95.508248] Call Trace: [ 95.508683] dump_stack+0x107/0x163 [ 95.509323] ? io_commit_cqring+0x3ec/0x8e0 [ 95.509982] print_address_description.constprop.0+0x3e/0x60 [ 95.510814] ? vprintk_func+0x98/0x140 [ 95.511399] ? io_commit_cqring+0x3ec/0x8e0 [ 95.512036] ? io_commit_cqring+0x3ec/0x8e0 [ 95.512733] kasan_report_invalid_free+0x51/0x80 [ 95.513431] ? io_commit_cqring+0x3ec/0x8e0 [ 95.514047] __kasan_slab_free+0x141/0x160 [ 95.514699] kfree+0xd1/0x390 [ 95.515182] io_commit_cqring+0x3ec/0x8e0 [ 95.515799] __io_req_complete.part.0+0x64/0x90 [ 95.516483] io_wq_submit_work+0x1fa/0x260 [ 95.517117] io_worker_handle_work+0xeac/0x1c00 [ 95.517828] io_wqe_worker+0xc94/0x11a0 [ 95.518438] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.519151] ? __kthread_parkme+0x11d/0x1d0 [ 95.519806] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.520512] ? io_worker_handle_work+0x1c00/0x1c00 [ 95.521211] kthread+0x396/0x470 [ 95.521727] ? _raw_spin_unlock_irq+0x24/0x30 [ 95.522380] ? kthread_mod_delayed_work+0x180/0x180 [ 95.523108] ret_from_fork+0x22/0x30 [ 95.523684] [ 95.523985] Allocated by task 4035: [ 95.524543] kasan_save_stack+0x1b/0x40 [ 95.525136] __kasan_kmalloc.constprop.0+0xc2/0xd0 [ 95.525882] kmem_cache_alloc_trace+0x17b/0x310 [ 95.533930] io_queue_sqe+0x225/0xcb0 [ 95.534505] io_submit_sqes+0x1768/0x25f0 [ 95.535164] __x64_sys_io_uring_enter+0x89e/0xd10 [ 95.535900] do_syscall_64+0x33/0x40 [ 95.536465] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 95.537199] [ 95.537505] Freed by task 4035: [ 95.538003] kasan_save_stack+0x1b/0x40 [ 95.538599] kasan_set_track+0x1c/0x30 [ 95.539177] kasan_set_free_info+0x1b/0x30 [ 95.539798] __kasan_slab_free+0x112/0x160 [ 95.540427] kfree+0xd1/0x390 [ 95.540910] io_commit_cqring+0x3ec/0x8e0 [ 95.541516] io_iopoll_complete+0x914/0x1390 [ 95.542150] io_do_iopoll+0x580/0x700 [ 95.542724] io_iopoll_try_reap_events.part.0+0x108/0x200 [ 95.543512] io_ring_ctx_wait_and_kill+0x118/0x340 [ 95.544206] io_uring_release+0x43/0x50 [ 95.544791] __fput+0x28d/0x940 [ 95.545291] task_work_run+0xea/0x1b0 [ 95.545873] do_exit+0xb6a/0x2c60 [ 95.546400] do_group_exit+0x12a/0x320 [ 95.546967] __x64_sys_exit_group+0x3f/0x50 [ 95.547605] do_syscall_64+0x33/0x40 [ 95.548155] entry_SYSCALL_64_after_hwframe+0x44/0xa9 The reason is that once we got a non EAGAIN error in io_wq_submit_work(), we'll complete req by calling io_req_complete(), which will hold completion_lock to call io_commit_cqring(), but for polled io, io_iopoll_complete() won't hold completion_lock to call io_commit_cqring(), then there maybe concurrent access to ctx->defer_list, double free may happen. To fix this bug, we always let io_iopoll_complete() complete polled io. Cc: # 5.5+ Reported-by: Abaci Fuzz Signed-off-by: Xiaoguang Wang Reviewed-by: Pavel Begunkov Reviewed-by: Joseph Qi Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index a2a7c65a77aa..c895a306f919 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -6074,8 +6074,19 @@ static struct io_wq_work *io_wq_submit_work(struct io_wq_work *work) } if (ret) { - req_set_fail_links(req); - io_req_complete(req, ret); + /* + * io_iopoll_complete() does not hold completion_lock to complete + * polled io, so here for polled io, just mark it done and still let + * io_iopoll_complete() complete it. + */ + if (req->ctx->flags & IORING_SETUP_IOPOLL) { + struct kiocb *kiocb = &req->rw.kiocb; + + kiocb_done(kiocb, ret, NULL); + } else { + req_set_fail_links(req); + io_req_complete(req, ret); + } } return io_steal_work(req); From patchwork Sun Dec 6 22:22:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 339053 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.7 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 98447C4167B for ; Sun, 6 Dec 2020 22:27:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5F74523107 for ; Sun, 6 Dec 2020 22:27:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727985AbgLFW1P (ORCPT ); Sun, 6 Dec 2020 17:27:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727974AbgLFW1B (ORCPT ); Sun, 6 Dec 2020 17:27:01 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 822B1C0613D1; Sun, 6 Dec 2020 14:26:15 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id v14so9958594wml.1; Sun, 06 Dec 2020 14:26:15 -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=L2/MEMTfd2VanoPXuAV+bQvvdS0lO+7tGI7L0mWtxrI=; b=SyyF38+THU/ACXGrxjRHqvmA5dAZn4CPYIY8z/Obk5xKGAFX3lHNItssXuEAStd/lu UoFXZgLpf9u9UA5/7OCEOqTI1fGyufclCKxsA67ozTe7SCIDWPZTJlvdFn9kgNpbHbgW djqlOatKX0KoiaB543lm53USsZ98yqQQsWacA/3WHnyYbViG4MiP2zp6plnlTLTkFrou 09mvFsp3ZMjdNd3oAEPGjE3hUvxFBxvB5ClihB95yBpx4ISUUZqDNOboJvNLIFQhi53z ygOZwmgtsiOTMz6/forSdVphTGpCyU3SMK/6HQ3Vo4z0gzAvqTNGFDD7X8U+EsOlV45p pNSg== 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=L2/MEMTfd2VanoPXuAV+bQvvdS0lO+7tGI7L0mWtxrI=; b=Hc4t7M/s62y04N9mzQ39I1TGcEW3hzZ4UAmGG3+Tag+GFTmvXrQNMiNgh1zqL7A3Z0 iwIxDHyQA8ew8R/dFmKzyOVpOEcn1duALQ/l73mcecFAeQ75n/1xN1QfIJFDECeybiO0 abtJHFDxfKNYggB8obt2Ut+rLLQKUwZqLuknyR3DMOu67iR2lW+CMpwiXVQbJQ4d0wlX DRCaJEtk5rv5Xa9RxxGSzRcQftJHBAvpnEr3c2qirVahHNyqoeZqpe4baWyV5K8aU6Yf NBsZVMJY2BYzKNh0mMlAAfK0NlsTBLbZeyn2tP3DItMZycwNlga1xVQNfL5smbxcl4PR nXEQ== X-Gm-Message-State: AOAM530BYpbLO32rqelhUyYsQ+BAyG/EvwHbKU0QLU6y78Xev3EuKIE4 7igI5VLT9H+6NK4zdZ5S8CNVitHOUQx98w== X-Google-Smtp-Source: ABdhPJzk7VFHpl0dG4xkvmfqfD0oDeEqi5IcROsSOvu73aYLjGwtHm9tK/W20vlUnKOfqXQ113YxVQ== X-Received: by 2002:a1c:80cb:: with SMTP id b194mr15427639wmd.91.1607293574348; Sun, 06 Dec 2020 14:26:14 -0800 (PST) Received: from localhost.localdomain ([185.69.145.92]) by smtp.gmail.com with ESMTPSA id h20sm11284917wmb.29.2020.12.06.14.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2020 14:26:13 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org Cc: stable@vger.kernel.org Subject: [PATCH 5.10 2/5] io_uring: fix racy IOPOLL completions Date: Sun, 6 Dec 2020 22:22:43 +0000 Message-Id: <3a8d4b11f6cbb28c5067fd77ba35c2995f4658be.1607293068.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 IOPOLL allows buffer remove/provide requests, but they doesn't synchronise by rules of IOPOLL, namely it have to hold uring_lock. Cc: # 5.7+ Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index c895a306f919..4fac02ea5f4c 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -3948,11 +3948,17 @@ static int io_remove_buffers(struct io_kiocb *req, bool force_nonblock, head = idr_find(&ctx->io_buffer_idr, p->bgid); if (head) ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); - - io_ring_submit_lock(ctx, !force_nonblock); if (ret < 0) req_set_fail_links(req); - __io_req_complete(req, ret, 0, cs); + + /* need to hold the lock to complete IOPOLL requests */ + if (ctx->flags & IORING_SETUP_IOPOLL) { + __io_req_complete(req, ret, 0, cs); + io_ring_submit_unlock(ctx, !force_nonblock); + } else { + io_ring_submit_unlock(ctx, !force_nonblock); + __io_req_complete(req, ret, 0, cs); + } return 0; } @@ -4037,10 +4043,17 @@ static int io_provide_buffers(struct io_kiocb *req, bool force_nonblock, } } out: - io_ring_submit_unlock(ctx, !force_nonblock); if (ret < 0) req_set_fail_links(req); - __io_req_complete(req, ret, 0, cs); + + /* need to hold the lock to complete IOPOLL requests */ + if (ctx->flags & IORING_SETUP_IOPOLL) { + __io_req_complete(req, ret, 0, cs); + io_ring_submit_unlock(ctx, !force_nonblock); + } else { + io_ring_submit_unlock(ctx, !force_nonblock); + __io_req_complete(req, ret, 0, cs); + } return 0; } From patchwork Sun Dec 6 22:22:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 339597 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.7 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=unavailable 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 226A2C1B0D8 for ; Sun, 6 Dec 2020 22:27:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D853E2310B for ; Sun, 6 Dec 2020 22:27:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727930AbgLFW1P (ORCPT ); Sun, 6 Dec 2020 17:27:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727474AbgLFW05 (ORCPT ); Sun, 6 Dec 2020 17:26:57 -0500 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9954AC0613D3; Sun, 6 Dec 2020 14:26:16 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id r3so10943531wrt.2; Sun, 06 Dec 2020 14:26:16 -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=R1puTOoeitudJFFNKlVi61XHZNTGXHFCWqgDCd/4paY=; b=XwuW2sClPIiwbB8nEwoPcK5n76AS/nJhmQfhHETGyk84PoIAHAvcSF5Vv1lcqRz/EX 3v8la0XTwOZJHyRF0C7p4fPZf5zDAI4OTvYuwOfVS6fXyROcbI1daNU1VojDdk/l+YkH kgPmiybYb1suR5TYQ85GkJIW+3135oaAi5CwIHE9Uea31VXiWtt84bqZHg/edljScCvC 1UgYyEyNDzSxDgktFRokpB+Z0s3zOy9Wozy/qRUM2bXcBy9wF6brFD6RMQEHlsz1CgVj 65+YzYJJfXeGCn/uENek8IWWQCOFMyz1l6ZtSF4Niw+ijksoreQH/gWVTyhYJtwC0ob0 G1aA== 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=R1puTOoeitudJFFNKlVi61XHZNTGXHFCWqgDCd/4paY=; b=pltAjHFkyMKEKVsa35xMLKiI0PnSCvNl4KJ4uNfu8SdhGpKS50GhkrruWA2ujIjiqX IHdAd2eDm+bek/ZMHn0qWfxwyb8PWn111v6ibaY6pYLYdZ9KRoD0DrtWYEucVi6cxHrW DcHeeJZT1hf7VAjkblXPN5RWCcmNW2Mcdhv5tFxZv0/ORXOPEquRsEKCmBVHiUpYe45P 1z7KuUwk3GwjL7yr9ZGcs3KXsVlMvifUEpG2FRlJwFmVG+SVxPNlppSMDDauAKuOTENK gVKk1MDaHPobDxiPzkymtuHfQbMnTeghoPrXD6s46eMsvxRnecI7TnAgz1ZxRW3tHTtE QHpw== X-Gm-Message-State: AOAM533HkFi2wyeTZjDFyW5Q81tNj8sUzbIICwLs7MOV/mnycq9DAfWY aUmbGX7x7gJ/YF2qttxWwSs= X-Google-Smtp-Source: ABdhPJyzi59Eoo/wBBzVt79pgjkgGds44qyzBsu9woxRNZw/L/iphnE12UOIxaoeYjKCnH3EhWKH1w== X-Received: by 2002:adf:b647:: with SMTP id i7mr16544414wre.241.1607293575369; Sun, 06 Dec 2020 14:26:15 -0800 (PST) Received: from localhost.localdomain ([185.69.145.92]) by smtp.gmail.com with ESMTPSA id h20sm11284917wmb.29.2020.12.06.14.26.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2020 14:26:14 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org Cc: stable@vger.kernel.org Subject: [PATCH 5.10 3/5] io_uring: fix racy IOPOLL flush overflow Date: Sun, 6 Dec 2020 22:22:44 +0000 Message-Id: <54043d0489ded8e883a9800a82cf66fc9c0cded5.1607293068.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 It's not safe to call io_cqring_overflow_flush() for IOPOLL mode without hodling uring_lock, because it does synchronisation differently. Make sure we have it. As for io_ring_exit_work(), we don't even need it there because io_ring_ctx_wait_and_kill() already set force flag making all overflowed requests to be dropped. Cc: # 5.5+ Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 4fac02ea5f4c..b1ba9a738315 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8393,8 +8393,6 @@ static void io_ring_exit_work(struct work_struct *work) * as nobody else will be looking for them. */ do { - if (ctx->rings) - 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); @@ -8404,6 +8402,8 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) { mutex_lock(&ctx->uring_lock); percpu_ref_kill(&ctx->refs); + if (ctx->rings) + io_cqring_overflow_flush(ctx, true, NULL, NULL); mutex_unlock(&ctx->uring_lock); io_kill_timeouts(ctx, NULL); @@ -8413,8 +8413,6 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) 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, NULL, NULL); io_iopoll_try_reap_events(ctx); idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); @@ -8691,7 +8689,9 @@ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx, else io_cancel_defer_files(ctx, task, NULL); + 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(); @@ -8993,8 +8993,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, */ ret = 0; if (ctx->flags & IORING_SETUP_SQPOLL) { + io_ring_submit_lock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); if (!list_empty_careful(&ctx->cq_overflow_list)) io_cqring_overflow_flush(ctx, false, NULL, NULL); + io_ring_submit_unlock(ctx, (ctx->flags & IORING_SETUP_IOPOLL)); if (flags & IORING_ENTER_SQ_WAKEUP) wake_up(&ctx->sq_data->wait); if (flags & IORING_ENTER_SQ_WAIT) From patchwork Sun Dec 6 22:22:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Begunkov X-Patchwork-Id: 339052 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.7 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=unavailable 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 69E8BC1B0D9 for ; Sun, 6 Dec 2020 22:27:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33BC02310B for ; Sun, 6 Dec 2020 22:27:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727974AbgLFW1P (ORCPT ); Sun, 6 Dec 2020 17:27:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727930AbgLFW06 (ORCPT ); Sun, 6 Dec 2020 17:26:58 -0500 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A258CC0613D4; Sun, 6 Dec 2020 14:26:17 -0800 (PST) Received: by mail-wm1-x342.google.com with SMTP id 3so11935669wmg.4; Sun, 06 Dec 2020 14:26:17 -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=sz41q27dEN+NhvetrgJqhS/NYwKThir04BgllJLnIN4=; b=VJkIe7flzNp7E0qyfQgADSbE5ZVMFnXOjWDTh56IZgT1lM5iUGRAySCB6FWHaDD2u0 DoynFHVFs9z4Gk+IjvOL6hgdTMoMsMVYZPltWL75D9aG7g4x3P4/jqDahSEkz9a9zHn7 v6u8Tl5kg6neZjfSfd9VArENyXAK+WKCstq8QuDFLixCyWcnj/pN4BYilBg/uR2jKTVS Gt7fcr2mAOetomlWnMH53XLAyK6bDUWb/qxJ9Cb44ksTBlMNLJPGsRbn8Bls39YLVonC Ss0RRGqpwNA8AWUIhuAiC+7e1UFGNtatpk5mmUCZ7Dwhpebq0bvPLb70Xt860FlSTc1p pckw== 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=sz41q27dEN+NhvetrgJqhS/NYwKThir04BgllJLnIN4=; b=OpLBCIxWt0aKwPU/9FKk7i08WUGXrxHs6vtFg+Jt2H74rdks2FywXm5ZLW01x/lslf 5nYneL6qIK0cU5pujtmpTCQERC35PeiPBpQhPPBUvEh+uzxeaokY8pdSXd9MAmkMDazM kAtIzGkx4TL1EqnVRIWFzfU+C/YQZwsASPv0H3QuoMYAEF8spFI0Flk73dCm1qDNOT6S lBqrImTTNeI55MlYHhAW5dV9mfA/gcM9/+jVFfDvHeB9FFfS1rBYDG7StILU5YPlNCV9 LPhTY/btA1lb+5plWRrv6Eqs2ToRsixQuHibkzZGDYFzrhN6etYu/kBLUiRzgqQdmPti A3Pw== X-Gm-Message-State: AOAM5308+sktgwOsUUgLwaTlppPdF5IG5QWUo/nu1M57HB7IyB+2qPmL sjISJiHXlQVZ5SdtzHIwNuQ= X-Google-Smtp-Source: ABdhPJxRvQUCVR593/iyRbpKVC/EWnFmCIBgELzyoU8fB8tjHLDba1UO/iGx1NSGKuceMhmeUULfAQ== X-Received: by 2002:a1c:bc88:: with SMTP id m130mr15687469wmf.82.1607293576420; Sun, 06 Dec 2020 14:26:16 -0800 (PST) Received: from localhost.localdomain ([185.69.145.92]) by smtp.gmail.com with ESMTPSA id h20sm11284917wmb.29.2020.12.06.14.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2020 14:26:15 -0800 (PST) From: Pavel Begunkov To: Jens Axboe , io-uring@vger.kernel.org Cc: stable@vger.kernel.org Subject: [PATCH 5.10 4/5] io_uring: fix io_cqring_events()'s noflush Date: Sun, 6 Dec 2020 22:22:45 +0000 Message-Id: <018bb0de66981fa798da015a983e5bb6c41bae5b.1607293068.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 Checking !list_empty(&ctx->cq_overflow_list) around noflush in io_cqring_events() is racy, because if it fails but a request overflowed just after that, io_cqring_overflow_flush() still will be called. Remove the second check, it shouldn't be a problem for performance, because there is cq_check_overflow bit check just above. Cc: # 5.5+ Signed-off-by: Pavel Begunkov --- fs/io_uring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index b1ba9a738315..f707caed9f79 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -2246,7 +2246,7 @@ static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) * we wake up the task, and the next invocation will flush the * entries. We cannot safely to it from here. */ - if (noflush && !list_empty(&ctx->cq_overflow_list)) + if (noflush) return -1U; io_cqring_overflow_flush(ctx, false, NULL, NULL);