Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1717763pxb; Fri, 24 Sep 2021 10:18:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjNcN0iKuCnZvnN8Bfr774Q6zXfQl6cxoTsV0f64WvNF3Iia7liVDwbMi/6/+7JlKZ+x3x X-Received: by 2002:a6b:b785:: with SMTP id h127mr9996530iof.149.1632503927260; Fri, 24 Sep 2021 10:18:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632503927; cv=none; d=google.com; s=arc-20160816; b=ZvsNOCybJ6rQ4LAPYIZxcjmzLGJTOIbRBmY+0mUxyRrxAZOPjEuWfyJfqzyH1Fh8gX 19IbB7LNXTcFP02Lav/+Xs36TGOMBj1Ic5KAU2ym3W7WcnhoyPROp8apXeK8hWCB7TqJ 4yvtGdcXF14oAQrjTu9h7CMmE0gJpxz9fIh/XSJybjfMLXODPMCjlxmE6BdYlktrWJsZ M3HwaLN+g4p+lIXDEDo6wuq5x179Vi0cSNhIGe9/M+H8VdMmblEbyLSVWiZTx0RX89MR RS+A3bSmhRwRsVO5mgwMxpzYIUyci1TLLy44lGfgRiMGR6XF/RtRd/HXFcTHNKMj7QTN ZJPA== 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=tLdvjl+Ds1oy4J+lYy4c00/GX4e23hQtU6IFycWX1p8=; b=QPO71zircljxBSkzr+4bpQXq1AZgkOn4c6zqPgs5RmXaQqsOlxBD4wTDX9kJGS/p/r 6K18rPZFvmFU3yOGP5xOGvL8q4DBtdiNQIuSSWry66zmkMJAxZ2zekOJ3zgIEcxTdWmz exeSI9vs49uUrwuHUp7+YgLm/9VsRS05H32ud/F0djuT2yC09yTw3BpoOYUXAOMs9IS8 HceII3dAcVtZdSIsqDT384y61TFa1+iUya4D5wS1SAyNEkUFyQ9VvODANtfMlJz7WqWw 4t2F02CaFoRQvfo991VLsFZche6xw/tSOpoCPTz9icAT8fwdHy2tCzvdLkRwabZt2vtC B07w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=w1FQXDXY; 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 s18si10376526ilj.28.2021.09.24.10.18.34; Fri, 24 Sep 2021 10:18:47 -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=w1FQXDXY; 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 S1344625AbhIXMwt (ORCPT + 99 others); Fri, 24 Sep 2021 08:52:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:45988 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344610AbhIXMvB (ORCPT ); Fri, 24 Sep 2021 08:51:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 15C8361268; Fri, 24 Sep 2021 12:48:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632487723; bh=JY6hxvivT5MrWbBnVusMY3Nk4zVUu3MPkPJBaDZVmTA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w1FQXDXY/hOWF9nW6kqi1zSVPC2o5IFF6YkV59oaXLkcCUYgLSHi2frYGB2K+lqk+ EZkXouvjhBrGM+9ZbASGLw9pkKCHbe9eJZ377tTfTSiSqjSeYsqSFPMKJXv7Tjzcow iLr+lbe975Msaip+p77wwi3gA+KrU1GfMFdZN89w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Neeraj Upadhyay , "Paul E. McKenney" , David Chen Subject: [PATCH 4.19 03/34] rcu: Fix missed wakeup of exp_wq waiters Date: Fri, 24 Sep 2021 14:43:57 +0200 Message-Id: <20210924124330.079218750@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210924124329.965218583@linuxfoundation.org> References: <20210924124329.965218583@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: Neeraj Upadhyay commit fd6bc19d7676a060a171d1cf3dcbf6fd797eb05f upstream. Tasks waiting within exp_funnel_lock() for an expedited grace period to elapse can be starved due to the following sequence of events: 1. Tasks A and B both attempt to start an expedited grace period at about the same time. This grace period will have completed when the lower four bits of the rcu_state structure's ->expedited_sequence field are 0b'0100', for example, when the initial value of this counter is zero. Task A wins, and thus does the actual work of starting the grace period, including acquiring the rcu_state structure's .exp_mutex and sets the counter to 0b'0001'. 2. Because task B lost the race to start the grace period, it waits on ->expedited_sequence to reach 0b'0100' inside of exp_funnel_lock(). This task therefore blocks on the rcu_node structure's ->exp_wq[1] field, keeping in mind that the end-of-grace-period value of ->expedited_sequence (0b'0100') is shifted down two bits before indexing the ->exp_wq[] field. 3. Task C attempts to start another expedited grace period, but blocks on ->exp_mutex, which is still held by Task A. 4. The aforementioned expedited grace period completes, so that ->expedited_sequence now has the value 0b'0100'. A kworker task therefore acquires the rcu_state structure's ->exp_wake_mutex and starts awakening any tasks waiting for this grace period. 5. One of the first tasks awakened happens to be Task A. Task A therefore releases the rcu_state structure's ->exp_mutex, which allows Task C to start the next expedited grace period, which causes the lower four bits of the rcu_state structure's ->expedited_sequence field to become 0b'0101'. 6. Task C's expedited grace period completes, so that the lower four bits of the rcu_state structure's ->expedited_sequence field now become 0b'1000'. 7. The kworker task from step 4 above continues its wakeups. Unfortunately, the wake_up_all() refetches the rcu_state structure's .expedited_sequence field: wake_up_all(&rnp->exp_wq[rcu_seq_ctr(rcu_state.expedited_sequence) & 0x3]); This results in the wakeup being applied to the rcu_node structure's ->exp_wq[2] field, which is unfortunate given that Task B is instead waiting on ->exp_wq[1]. On a busy system, no harm is done (or at least no permanent harm is done). Some later expedited grace period will redo the wakeup. But on a quiet system, such as many embedded systems, it might be a good long time before there was another expedited grace period. On such embedded systems, this situation could therefore result in a system hang. This issue manifested as DPM device timeout during suspend (which usually qualifies as a quiet time) due to a SCSI device being stuck in _synchronize_rcu_expedited(), with the following stack trace: schedule() synchronize_rcu_expedited() synchronize_rcu() scsi_device_quiesce() scsi_bus_suspend() dpm_run_callback() __device_suspend() This commit therefore prevents such delays, timeouts, and hangs by making rcu_exp_wait_wake() use its "s" argument consistently instead of refetching from rcu_state.expedited_sequence. Fixes: 3b5f668e715b ("rcu: Overlap wakeups with next expedited grace period") Signed-off-by: Neeraj Upadhyay Signed-off-by: Paul E. McKenney Signed-off-by: David Chen Acked-by: Neeraj Upadhyay Signed-off-by: Greg Kroah-Hartman --- kernel/rcu/tree_exp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/kernel/rcu/tree_exp.h +++ b/kernel/rcu/tree_exp.h @@ -613,7 +613,7 @@ static void rcu_exp_wait_wake(struct rcu spin_unlock(&rnp->exp_lock); } smp_mb(); /* All above changes before wakeup. */ - wake_up_all(&rnp->exp_wq[rcu_seq_ctr(rsp->expedited_sequence) & 0x3]); + wake_up_all(&rnp->exp_wq[rcu_seq_ctr(s) & 0x3]); } trace_rcu_exp_grace_period(rsp->name, s, TPS("endwake")); mutex_unlock(&rsp->exp_wake_mutex);