Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1548307pxb; Thu, 16 Sep 2021 09:41:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwesURYZ6+LOL6MPC3yGrcWiqm9eKqnVAK1gGEXZyWp+3YYmND4GrZLY4GUHHIwETohz3fX X-Received: by 2002:a50:954c:: with SMTP id v12mr7430356eda.313.1631810465664; Thu, 16 Sep 2021 09:41:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631810465; cv=none; d=google.com; s=arc-20160816; b=zjBWMrhwIq45bvOh+lRQBTTRsczKuxGcK4zDIvITsWXGchBNCIQ4gD6ECNHT146va1 w8MLXzp3WITkVnp/BSguHnwUFgnB9yR25dng7wDDFOinEGBdh1Ox8BjN2qMnD7U9NabP RobIYwDK60gmBl97zLcUs9ih/zlEfAtA2iTZtiVv0D1Il3jT8+2FcuP079ZV+GC8T0F1 OSVFvZzYudTyYagqry2r8zJeKXGxRvMwnnXpcZb6Fpfo9s11mEYqBeI9BTyW/hTkCXZx YPbckCWl+lHtsv+cKDb9p4rWL/SEPMizt4yN7yGm20Yg+/ji3j6O+7JRH/lyZI38NQ3G TGfQ== 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=vJBBS0/pAq79eYX9L4Vp3ONwLnLb9+iM0Hjk8SETD/E=; b=KkoWjpSLkNCgl4i35YKlzEzgp4iXBhAgv0rRCTrwt7W9rGQDMzzyorDVAMtEZpJMKA x8okHI2Y5xJLJK97tkqzKR9s0J7if+frkO+B71ulhi8UFegoB959MuWJG8RKPWH4UYta l6ZIaY+CF41QtOMUcfi9ZX9u6/f3PAzCrsVHDtNjQw9iLMqCWdKrUDnDCX7S6uio/n8D 9qYXGlpu5NLdIwNjrseR144CH362Mzp/c4BkYTVaqRDw1X3PjrUC6ol/Uub6eX+HCnBI ubUys3+XVmLLPZWdYnc2gWpJFHk3ygVVeUCIspaXAFcoh8xux6XRjEQJNoNEptHN1m8f QRag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aKlhDTt1; 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 g6si3503881edf.632.2021.09.16.09.40.40; Thu, 16 Sep 2021 09:41:05 -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=aKlhDTt1; 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 S243724AbhIPQjF (ORCPT + 99 others); Thu, 16 Sep 2021 12:39:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:44660 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243386AbhIPQbY (ORCPT ); Thu, 16 Sep 2021 12:31:24 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 118C16138E; Thu, 16 Sep 2021 16:19:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631809162; bh=t36PD241Vt04inE54Cxt7bIoDCPhYYCSm2Egul+soOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aKlhDTt1O3/23o2YJ0GxrVXMZgxpsSdu5jGcCJvjxnWoCH/VaPJ/mQFVBTtjBjnYG Ugce8qoypS6rFoLFyVxLMdYhgvi3i2+INSgCjng4kubgSN23k7kNoTTe+Wfi1WSXWx W4Y5lnDUU1/fMyLmV3NFk9JIGMAyCZO7dFXcNMbU= 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.13 054/380] io-wq: fix wakeup race when adding new work Date: Thu, 16 Sep 2021 17:56:51 +0200 Message-Id: <20210916155805.820023413@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155803.966362085@linuxfoundation.org> References: <20210916155803.966362085@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 @@ -798,7 +798,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; /* @@ -811,14 +811,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); }