Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1668598pxb; Thu, 16 Sep 2021 12:30:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyag+JeOyTmwdJofq0Pb9D4TC2/LTQmWiR2tInkAxzgZJjVtwmrZpT9hKekXL12HKvM/6Hw X-Received: by 2002:a17:906:7792:: with SMTP id s18mr7703653ejm.492.1631820607985; Thu, 16 Sep 2021 12:30:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631820607; cv=none; d=google.com; s=arc-20160816; b=g1XfG/3b1hWY696xJ2Q6A48t4Dc23wf3+Ugxn/sgTOZz4720yoMxmvNQsSvUL8vlXI CG9sqN64wSeH8peKt3jtuydZZRNRN7avafbXXsjGkEVAnDQWcKo9Jw7dd7aQKTX48mGP UmEE8hB+Pvr/U8hINbdfStZ4QZrA+4qjXX0hlvGGkP9+Ep9S1LNqyi8i54G/nYiI0Fjz 4/I/KLfs3uwNMwA7Q+CkwU3a8PNa551GIOpEtaL2G35woJTSxaNpbEX6xs75ORuIPiZt XpC9FQYdALuCmicwZfblLXM8yLjSCerZGYGyiP814Uwl+KeNXL7CNY2qPxxox29rmCEZ Cm3g== 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=ebrsDzrhN8a7KrHzrGor8ojfIseVZFcigouslQ/Ijo4=; b=xjHyLSg7nUStvLhZpl6E4Jr3yHrO6IqhSjoIABi65bIO0w0uwX0zBouFKaQdkMkhh1 a+7nKDnGJ8BW1rfACZbuoRjgjIkmiTAz+XQGthP0hoViXZxoTyQ9jjjrmbmLa8a/CVEg QMqoPvHOf02i9YI0qv8pr4Ounpc0V7aO8z2IOaP70arZ+x9XnfQv2v4JkZxX18qh6Uhh 3EpbWk6/SNbLb+DGKarQcfhR9Np5sxEyZfqELPnK/79kKZyBdo9EiFaTqIiG8pcCFDEb MYPAGdiYDqpE6t9D9NfucZ3alFmsm+IqO7Yi8HBmBiVcZdja3ZSTmnhm4cNxIILFbLVO KB+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AMVtuq6L; 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 i14si4422401eje.121.2021.09.16.12.29.44; Thu, 16 Sep 2021 12:30:07 -0700 (PDT) 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=AMVtuq6L; 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 S233584AbhIPQIG (ORCPT + 99 others); Thu, 16 Sep 2021 12:08:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:45442 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232350AbhIPQGR (ORCPT ); Thu, 16 Sep 2021 12:06:17 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8BCFD61260; Thu, 16 Sep 2021 16:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631808297; bh=+WlHnPP/OYK4t7YBDA66m+OpClmkD9CppTefsEBPggY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AMVtuq6LOP3JMJNbeZDipYpFNfgR69THz0cHRAxZH9/TwFKuZoqcQXQj2XzwD/BzE y2RJKPeEAxYvm+IRRwaM6WCpqMcOlsPFsiAC5bv6MD5Z2sWGhXSDifICfZhmcNtbnP +L5eiUJmCnLnAo2Enkkfvhp/zenMcwAg1swf4hqY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andres Freund , Jens Axboe Subject: [PATCH 5.10 006/306] io-wq: fix wakeup race when adding new work Date: Thu, 16 Sep 2021 17:55:51 +0200 Message-Id: <20210916155754.130352649@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155753.903069397@linuxfoundation.org> References: <20210916155753.903069397@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: Jens Axboe commit 87df7fb922d18e96992aa5e824aa34b2065fef59 upstream. When new work is added, io_wqe_enqueue() checks if we need to wake or create a new worker. But that check is done outside the lock that otherwise synchronizes us with a worker going to sleep, so we can end up in the following situation: CPU0 CPU1 lock insert work unlock atomic_read(nr_running) != 0 lock atomic_dec(nr_running) no wakeup needed Hold the wqe lock around the "need to wakeup" check. Then we can also get rid of the temporary work_flags variable, as we know the work will remain valid as long as we hold the lock. Cc: stable@vger.kernel.org Reported-by: Andres Freund Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io-wq.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/fs/io-wq.c +++ b/fs/io-wq.c @@ -895,7 +895,7 @@ append: static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) { struct io_wqe_acct *acct = io_work_get_acct(wqe, work); - int work_flags; + bool do_wake; unsigned long flags; /* @@ -909,14 +909,14 @@ static void io_wqe_enqueue(struct io_wqe return; } - work_flags = work->flags; raw_spin_lock_irqsave(&wqe->lock, flags); io_wqe_insert_work(wqe, work); wqe->flags &= ~IO_WQE_FLAG_STALLED; + do_wake = (work->flags & IO_WQ_WORK_CONCURRENT) || + !atomic_read(&acct->nr_running); raw_spin_unlock_irqrestore(&wqe->lock, flags); - if ((work_flags & IO_WQ_WORK_CONCURRENT) || - !atomic_read(&acct->nr_running)) + if (do_wake) io_wqe_wake_worker(wqe, acct); }