Received: by 2002:a17:90a:9307:0:0:0:0 with SMTP id p7csp3958713pjo; Tue, 3 Mar 2020 10:06:09 -0800 (PST) X-Google-Smtp-Source: ADFU+vumGGWU4FUaoI7Wj7w38iFdJqvTQBi2LMMl7nlJ1/5p+mJQVYtALqg6eMRbfUI6iBILnWRo X-Received: by 2002:aca:b60a:: with SMTP id g10mr3204779oif.102.1583258769782; Tue, 03 Mar 2020 10:06:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583258769; cv=none; d=google.com; s=arc-20160816; b=VkDefBXHEujABJKr6m13hA+q1bPkS/lAdn4SXiButTH95bRLOAmvouv9FKT/6nEej6 lY0O8f1FfaimyPMa9n/SKaCyTpHjyCcuq7XFUcjPdrLsKhhu3npG0a0WbakxANozbHv6 KMwUfg+Xcklo2QFnHulBheH4rpgK4nFVwOr7KqVjUlqrL3zrelXfGyV7rhdhRi62vMWH 3l8xnMiiA6ko1GpZeXmrReHngJa/NesjEYrB5fYDZX/A2qGAv++sTD/yXTPvfJ0PhoXR 1okVrAL6535EyFsYqoBXFwfMRXAl5T803cadDLU6VzYv5t5SsXlQIbF09Ezby7v0Jvps xN1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MT4bCbbdpARj2eVKjRLhNTswHWUHzBbxrN2PYTfqwkg=; b=Eeuk/cmgSYfc4LEsHpU35LIEhxybnZEsBR45kCuWW+nv5gn4HMqeBuzGveWuZiJi+6 jrE6u8Ir9mENbFBpV1K++E6pJhK7m661ZNagcmPc0T5bOIUsmwENezmeoad9OcppJuIv iv6bbr/JOiohVbRi4SFiAekoVDha0iQoaL1oqFNvozhB7nBu7rKOF2mmJmAkrbRRo8vU 91wiELI2FjQJJhcdq1nfDXo/IwHfLqrB4fl2kvtIkQ8Sfr41PYxGL2Ye30fuDNkA4TkT dVYgHCdXCL9/+9yQm6KSrpFxrRlthGi16zRBU/WBR3PGqDMRD6+LpXAne/1x9vUbaCFU E+1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XVz+K3OG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z6si2265128oto.204.2020.03.03.10.05.57; Tue, 03 Mar 2020 10:06:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=XVz+K3OG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387461AbgCCR6r (ORCPT + 99 others); Tue, 3 Mar 2020 12:58:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:41656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732914AbgCCR6p (ORCPT ); Tue, 3 Mar 2020 12:58:45 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 06860206D5; Tue, 3 Mar 2020 17:58:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583258324; bh=q9ncxq94EGXOCTEZx6k1DLyeoq/IGMKBwAr6s2YL1N4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XVz+K3OGeVXijX7TebxEVpmtMy6iNBkAzjTSYljPKQWJWvpyadj6RE+EcrNKXUJa3 fCKn5VHkWA1IBu8LgzfGQsLmX3Oz+OiMVqnDm0q35VbiMdJu7yhns2jqRTDGyw98+D Qc+unNRVCWkwnKLANLjaJ+2NnOqAAjhb/gsS/+DU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Neeraj Upadhyay , "Paul E. McKenney" Subject: [PATCH 5.4 137/152] rcu: Allow only one expedited GP to run concurrently with wakeups Date: Tue, 3 Mar 2020 18:43:55 +0100 Message-Id: <20200303174318.383328773@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200303174302.523080016@linuxfoundation.org> References: <20200303174302.523080016@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Neeraj Upadhyay commit 4bc6b745e5cbefed92c48071e28a5f41246d0470 upstream. The current expedited RCU grace-period code expects that a task requesting an expedited grace period cannot awaken until that grace period has reached the wakeup phase. However, it is possible for a long preemption to result in the waiting task never sleeping. For example, consider the following sequence of events: 1. Task A starts an expedited grace period by invoking synchronize_rcu_expedited(). It proceeds normally up to the wait_event() near the end of that function, and is then preempted (or interrupted or whatever). 2. The expedited grace period completes, and a kworker task starts the awaken phase, having incremented the counter and acquired the rcu_state structure's .exp_wake_mutex. This kworker task is then preempted or interrupted or whatever. 3. Task A resumes and enters wait_event(), which notes that the expedited grace period has completed, and thus doesn't sleep. 4. Task B starts an expedited grace period exactly as did Task A, complete with the preemption (or whatever delay) just before the call to wait_event(). 5. The expedited grace period completes, and another kworker task starts the awaken phase, having incremented the counter. However, it blocks when attempting to acquire the rcu_state structure's .exp_wake_mutex because step 2's kworker task has not yet released it. 6. Steps 4 and 5 repeat, resulting in overflow of the rcu_node structure's ->exp_wq[] array. In theory, this is harmless. Tasks waiting on the various ->exp_wq[] array will just be spuriously awakened, but they will just sleep again on noting that the rcu_state structure's ->expedited_sequence value has not advanced far enough. In practice, this wastes CPU time and is an accident waiting to happen. This commit therefore moves the rcu_exp_gp_seq_end() call that officially ends the expedited grace period (along with associate tracing) until after the ->exp_wake_mutex has been acquired. This prevents Task A from awakening prematurely, thus preventing more than one expedited grace period from being in flight during a previous expedited grace period's wakeup phase. Fixes: 3b5f668e715b ("rcu: Overlap wakeups with next expedited grace period") Signed-off-by: Neeraj Upadhyay [ paulmck: Added updated comment. ] Signed-off-by: Paul E. McKenney Signed-off-by: Greg Kroah-Hartman --- kernel/rcu/tree_exp.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) --- a/kernel/rcu/tree_exp.h +++ b/kernel/rcu/tree_exp.h @@ -540,14 +540,13 @@ static void rcu_exp_wait_wake(unsigned l struct rcu_node *rnp; synchronize_sched_expedited_wait(); - rcu_exp_gp_seq_end(); - trace_rcu_exp_grace_period(rcu_state.name, s, TPS("end")); - /* - * Switch over to wakeup mode, allowing the next GP, but -only- the - * next GP, to proceed. - */ + // Switch over to wakeup mode, allowing the next GP to proceed. + // End the previous grace period only after acquiring the mutex + // to ensure that only one GP runs concurrently with wakeups. mutex_lock(&rcu_state.exp_wake_mutex); + rcu_exp_gp_seq_end(); + trace_rcu_exp_grace_period(rcu_state.name, s, TPS("end")); rcu_for_each_node_breadth_first(rnp) { if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s)) {