Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2268653pxb; Thu, 11 Feb 2021 08:18:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMIBWMYD2MSPv11/z206KhVWSoZNYmurIOLqetZNLEF3Yt+cQqQxV3bHF+q+WlAhJubCtS X-Received: by 2002:a05:6402:c91:: with SMTP id cm17mr9239816edb.219.1613060331451; Thu, 11 Feb 2021 08:18:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613060331; cv=none; d=google.com; s=arc-20160816; b=q3Eij6UlHmHrAL/EPEod2ea5COickwGT8jkZk4d3c+ii1huxJpNuPEV3lDBXnY0zGR 0uJlENqmZnvZbmJDERHc31daWEnFNTKJHmhJ7IhhCpPKXuq0IQIlFkzOqG2RmoPykWFK BGuM9NT8KTmIQKf3nuXL7vBqCB40ax9sVXafaos1qX81vA70jNUhG8Xjs2EvPdbNqGv7 AfNTmxlntxrd6uT3R9NktG3NIEIhn/2mJGVyX8LzJMHL16ZK1RDs0F1wFGSEVnX8L33Y Z6GENP84EfvoNZBWTReMdfITeDCMCl8S2c9WC132ot78uoHhfL3+cMKnTrVH4VGqQnr+ 7yAA== 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=kbagNz6zCqH5TciAt9Y0wHe3Y71dgDojZbJUeQtya54=; b=0AIUR+leQDCkkhhvCgoalvC0zQp8N742TOVe3DDm8bFptBm/j8rKWsT6/5OR/aIpIf U8zOwlGYT+2ZiltrgLoukIstxEp9p/OUys9JWh+jECKmzodH0SCDxRpaLXPnwqvrAaHB yOmSo9kgs1Gan6pbdm/BTV+VxGa8gbZDGALatUObeR8bAaWDlqC+AYCWKhlXQnq65e8w /BRE46atok6RT2wbWxvmM9ArrgX6CUDlpVxNmOGOTJEymQ1VRDFxJBTtlpitKdQj0OeJ 5TveyrjQDWYZ3/nIIjVl6SuOuNoCgwkCEupKi2/OuoZUJ5dfETgro0l9mWeoSQspPbke cCmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2GZ3uNTz; 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 jg13si4938828ejc.669.2021.02.11.08.18.26; Thu, 11 Feb 2021 08:18:51 -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=2GZ3uNTz; 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 S230438AbhBKQRQ (ORCPT + 99 others); Thu, 11 Feb 2021 11:17:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:52622 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230499AbhBKPWw (ORCPT ); Thu, 11 Feb 2021 10:22:52 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6864164E95; Thu, 11 Feb 2021 15:03:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1613055796; bh=Y/D9TlconG/v/RfHRvPj2cE1WCzKMekYt0QQa4iN0xY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2GZ3uNTzPDOyWkvTRN83FHJrjvtB24aUES6kQSASCQHUAXrZekkxPbWCIp7xnmlWw ILSTcHf0qjzBJeB0S4KFJ/QWbMF8A6FD7YDr7HprfASft/GBSyavMB8UYRo3T6p8qL 5rIFv6VLNBL3MfryfWtlYLIRAWbLN3PgoY6Yuxwo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , syzbot+f655445043a26a7cfab8@syzkaller.appspotmail.com, Pavel Begunkov , Jens Axboe Subject: [PATCH 5.10 11/54] io_uring: fix cancellation taking mutex while TASK_UNINTERRUPTIBLE Date: Thu, 11 Feb 2021 16:01:55 +0100 Message-Id: <20210211150153.372524326@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210211150152.885701259@linuxfoundation.org> References: <20210211150152.885701259@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 ca70f00bed6cb255b7a9b91aa18a2717c9217f70 ] do not call blocking ops when !TASK_RUNNING; state=2 set at [<00000000ced9dbfc>] prepare_to_wait+0x1f4/0x3b0 kernel/sched/wait.c:262 WARNING: CPU: 1 PID: 19888 at kernel/sched/core.c:7853 __might_sleep+0xed/0x100 kernel/sched/core.c:7848 RIP: 0010:__might_sleep+0xed/0x100 kernel/sched/core.c:7848 Call Trace: __mutex_lock_common+0xc4/0x2ef0 kernel/locking/mutex.c:935 __mutex_lock kernel/locking/mutex.c:1103 [inline] mutex_lock_nested+0x1a/0x20 kernel/locking/mutex.c:1118 io_wq_submit_work+0x39a/0x720 fs/io_uring.c:6411 io_run_cancel fs/io-wq.c:856 [inline] io_wqe_cancel_pending_work fs/io-wq.c:990 [inline] io_wq_cancel_cb+0x614/0xcb0 fs/io-wq.c:1027 io_uring_cancel_files fs/io_uring.c:8874 [inline] io_uring_cancel_task_requests fs/io_uring.c:8952 [inline] __io_uring_files_cancel+0x115d/0x19e0 fs/io_uring.c:9038 io_uring_files_cancel include/linux/io_uring.h:51 [inline] do_exit+0x2e6/0x2490 kernel/exit.c:780 do_group_exit+0x168/0x2d0 kernel/exit.c:922 get_signal+0x16b5/0x2030 kernel/signal.c:2770 arch_do_signal_or_restart+0x8e/0x6a0 arch/x86/kernel/signal.c:811 handle_signal_work kernel/entry/common.c:147 [inline] exit_to_user_mode_loop kernel/entry/common.c:171 [inline] exit_to_user_mode_prepare+0xac/0x1e0 kernel/entry/common.c:201 __syscall_exit_to_user_mode_work kernel/entry/common.c:291 [inline] syscall_exit_to_user_mode+0x48/0x190 kernel/entry/common.c:302 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Rewrite io_uring_cancel_files() to mimic __io_uring_task_cancel()'s counting scheme, so it does all the heavy work before setting TASK_UNINTERRUPTIBLE. Cc: stable@vger.kernel.org # 5.9+ Reported-by: syzbot+f655445043a26a7cfab8@syzkaller.appspotmail.com Signed-off-by: Pavel Begunkov [axboe: fix inverted task check] Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8586,30 +8586,31 @@ static void io_cancel_defer_files(struct } } +static int io_uring_count_inflight(struct io_ring_ctx *ctx, + struct task_struct *task, + struct files_struct *files) +{ + struct io_kiocb *req; + int cnt = 0; + + spin_lock_irq(&ctx->inflight_lock); + list_for_each_entry(req, &ctx->inflight_list, inflight_entry) + cnt += io_match_task(req, task, files); + spin_unlock_irq(&ctx->inflight_lock); + return cnt; +} + static void io_uring_cancel_files(struct io_ring_ctx *ctx, struct task_struct *task, struct files_struct *files) { while (!list_empty_careful(&ctx->inflight_list)) { struct io_task_cancel cancel = { .task = task, .files = files }; - struct io_kiocb *req; DEFINE_WAIT(wait); - bool found = false; + int inflight; - spin_lock_irq(&ctx->inflight_lock); - list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { - if (!io_match_task(req, task, files)) - continue; - found = true; - break; - } - if (found) - prepare_to_wait(&task->io_uring->wait, &wait, - TASK_UNINTERRUPTIBLE); - spin_unlock_irq(&ctx->inflight_lock); - - /* We need to keep going until we don't find a matching req */ - if (!found) + inflight = io_uring_count_inflight(ctx, task, files); + if (!inflight) break; io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb, &cancel, true); @@ -8617,7 +8618,11 @@ static void io_uring_cancel_files(struct io_kill_timeouts(ctx, task, files); /* cancellations _may_ trigger task work */ io_run_task_work(); - schedule(); + + prepare_to_wait(&task->io_uring->wait, &wait, + TASK_UNINTERRUPTIBLE); + if (inflight == io_uring_count_inflight(ctx, task, files)) + schedule(); finish_wait(&task->io_uring->wait, &wait); } }