Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9108182pxu; Mon, 28 Dec 2020 06:51:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJy1sXgvALMUqCidhv9frlgEy1Odg7m+mfy13wvr5GxnZYuyIc9J9sahYnrFHKXLC/vWoHe1 X-Received: by 2002:a17:906:a951:: with SMTP id hh17mr29819565ejb.388.1609167074919; Mon, 28 Dec 2020 06:51:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609167074; cv=none; d=google.com; s=arc-20160816; b=GRQhSUFn9AoktzVTg4sQNqxZrmG8Q0YooFhMxOVH1vt3hAPGOeMLCQnS5jrCJwNSg6 lrqSLhXlIYgGuBWtcEgjaKBQ2e4E/2+qtYpwKYpkeileQZb3C+qFQT/q6cyY6IsOfIfn X8LUcwsmFX80P91YMmc/UCL6VKeCUqDSof1ZNcm0aJk1ai3XOW5ujzSrPo5u7fACp9AP wTQvXBHQ1lBafzyoJQ9PYJkfnadaKwx5OPVlAxjPTMChDwWAR9/JYoipES2WK4qV6SUs wy2DljmqfJOX4bP5r8LZ2SzVQsNxJQsv7gMCGCT1GOfgQHp0or9F53kxbI0IbyKeHsfZ BlJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=D6g5PsYM/3coVldxYVdMRlV2oEST+S0G1YBkNS8EEjM=; b=0v0CzxkwK/Lug29/DWWzWu4rYd/t3JxAgQ7VVcGGpAoYW8S8PevcTNO0IjONfY6A7K G3hHr/1X3rhfCVJQCYo0MY/GG+jnFMrccsTbuBv9YyiShjoyDuPdTO8rNRuszmkssqG9 auix34SlOkQFxVvo29Y+vP0x12+RvQZVCM3Bwc8vN5F6HToy7f+Fspf4Y247z2ZzqOBc gqz7kkrad5n90N9a9+v4aB+sxve7Nw+jnDy3bRDZruOO4H7S5zoWBSAwNB1T8jXlnVk9 EDJt9nFHSwrNIajK0Dpk3FzfGOvec6mh8e6CsLWmvqppnq8EMj97+RdZr0fLMsb3+tmk 8OgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="hzzbt/ER"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f19si18482440ejq.225.2020.12.28.06.50.52; Mon, 28 Dec 2020 06:51:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="hzzbt/ER"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502911AbgL1OXs (ORCPT + 99 others); Mon, 28 Dec 2020 09:23:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:58226 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502766AbgL1OXH (ORCPT ); Mon, 28 Dec 2020 09:23:07 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4E43022D03; Mon, 28 Dec 2020 14:22:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165371; bh=pEuBK8ujUagU0dlJy7WscqftfQKL9i6gUUDLsClTPjo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hzzbt/ER4m1RCeP0j+nxJz0VLCOgYXluLU0USOQyGa+33fskuy4NFUh5u/ONRI8j+ CegSio90tr27jVOzGvQZGmUa05CYikE7qKyKO5Ftejy8PQH+OFjRSpcFdhCQqfcEdL U3yw0npbd5kSaw3Vs5L0TuoNxDWjhRL/FivBDvsM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Begunkov , Jens Axboe , Sasha Levin Subject: [PATCH 5.10 506/717] io_uring: fix racy IOPOLL flush overflow Date: Mon, 28 Dec 2020 13:48:24 +0100 Message-Id: <20201228125045.202569307@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Begunkov [ Upstream commit 634578f800652035debba3098d8ab0d21af7c7a5 ] 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 Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- 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 0621f581943cd..b9d3209a5f9de 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8369,8 +8369,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); @@ -8380,6 +8378,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); @@ -8389,8 +8389,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); @@ -8654,7 +8652,9 @@ 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(); @@ -8956,8 +8956,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) -- 2.27.0