Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp986030ybb; Sat, 28 Mar 2020 15:19:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvjh2SC70gyuPGypn6imFV/ac5YKk7Gm5D2LRswrUbiSSorT+7heeeBH7xtg1tCgUG3Dbla X-Received: by 2002:a05:6830:1e96:: with SMTP id n22mr3966901otr.189.1585433991137; Sat, 28 Mar 2020 15:19:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585433991; cv=none; d=google.com; s=arc-20160816; b=PdPSArKatNEw/6pd0xuyJ66PyPCwOe9iEKBrRksGoPerRIrmpkOMC1FYkVLLYzn7ae /xqFfkJjXrLhw95/2VtozvjgymhfSZLVUeSQI9Wmye7HrhhTtlBrV0xTUKT4mPc0bWjn GT8EuOUXpWapEqhLVP5lpMJXnCFro7GoH+AfGAFur8vMisYxl1YWVigCUKl0AYfHoTMP 1EJHMrUkWWnvT55Ot31C3bHXBA8Etne5X1L/6HPp2MndceJSM3jLwCjQT2cNPNv0ciH9 8MiCOBDIAnZVYePTy6bNwioNWc0JwmAgX6SQXuq0sy8LONbRaNIKCKyS5fphg6atgMYl SJGg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xfNPr5WFIiLX+x0UIy9/MN3FHS6KSSVTYOGNWa2ADGI=; b=rNNETWyWrJvf4k86g2H2ZATOk4XKd6Tvtkm+0/X4pI6L6lB9MA5DvGlakvL3bhl2kn b7qr/jLEiNteNVLgEqlmtiwCNtNvbg1ZjZ4+LpR9L4vuIF2o4dGSxd7YhQcRQqYo9rkN qCb32blCMWrmjPZLRu8lKsvGQJKXubcl/Eq1tQr4r+rhvp85zEGNX+lqz3/b5idTKq4I S2HWXxfG/c6T2q8mMaillrszshvZA5yQWMK+MRJfhCWR3P3ADR/Whoh/fSJ3VEcs4Qjj kQg7bXSd3EWW4voRLnHrYzAdUu6QrNzcpkeGHMdm20+P7RmQnjXaf3LwK5neoD/Jurkl BdKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=FrNItR+p; 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 b68si4153060oii.117.2020.03.28.15.19.39; Sat, 28 Mar 2020 15:19:51 -0700 (PDT) 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=@joelfernandes.org header.s=google header.b=FrNItR+p; 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 S1727885AbgC1WRQ (ORCPT + 99 others); Sat, 28 Mar 2020 18:17:16 -0400 Received: from mail-qv1-f41.google.com ([209.85.219.41]:40631 "EHLO mail-qv1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727850AbgC1WRP (ORCPT ); Sat, 28 Mar 2020 18:17:15 -0400 Received: by mail-qv1-f41.google.com with SMTP id bp12so2931798qvb.7 for ; Sat, 28 Mar 2020 15:17:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xfNPr5WFIiLX+x0UIy9/MN3FHS6KSSVTYOGNWa2ADGI=; b=FrNItR+paeaDGrTRVq24QQFHwSo3w1KzWqrRw5NR5R7iDfWHB1S5nIszaI0Elbi/2g qjqqX79Y4R6YqbYDelsP5z3Uzh3eJ8xTyxA72wQM6AQCAzvthqVjWGXQKZ4GQwdz2mZW rb2mi+SQWTKZaSPz6rsAl4Dp6JeZrdrXPNCaU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xfNPr5WFIiLX+x0UIy9/MN3FHS6KSSVTYOGNWa2ADGI=; b=lb4zteU74z1+HnbD52LndtG9YHctgSXK8sofjFV+ANzTUTT579C8p53NAG3Zd8QWC5 BUz3CY0kHGme0pnoT/r/+fhIvmQnQrsa6hFODPy+JkzAFfS0dThhOURVIgaDtCqSKOMO Uiv9MfgCF96NcuCQHOxvbQEKKjAbFvAsgGszbonaEC7tFdLKr4Gb+JmWslRBj8o68xxc XF615jUFwnGGbOtnNA0+/AVAnuGEv7FT2V/eZxk8UfnGGoZ5HNg18/Olm5pqpWqkL0ac LvPgdnwSW0xhAGaVZH9jW6pgD+Ot5iqjy2lgxgCKBYteILqgSn7I0j920Yruos3YptoS Ddvg== X-Gm-Message-State: ANhLgQ0QbkZilOCPJRs6qURQw2ZqE5X1HjLRk0FZU9eNCXEvoBOJzx9e jXnfZ+Yylk3PRJcfJo+qhDD+VKvspm4= X-Received: by 2002:a05:6214:11e1:: with SMTP id e1mr5623128qvu.176.1585433833709; Sat, 28 Mar 2020 15:17:13 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id k66sm6742950qke.10.2020.03.28.15.17.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Mar 2020 15:17:13 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Andy Lutomirski , Frederic Weisbecker , frextrite@gmail.com, Ingo Molnar , Josh Triplett , kernel-team@android.com, Lai Jiangshan , madhuparnabhowmik04@gmail.com, Mathieu Desnoyers , "Paul E. McKenney" , peterz@infradead.org, Petr Mladek , rcu@vger.kernel.org, rostedt@goodmis.org, tglx@linutronix.de, vpillai@digitalocean.com Subject: [PATCH v2 3/4] rcu/tree: Clean up dynticks counter usage Date: Sat, 28 Mar 2020 18:17:02 -0400 Message-Id: <20200328221703.48171-4-joel@joelfernandes.org> X-Mailer: git-send-email 2.26.0.rc2.310.g2932bb562d-goog In-Reply-To: <20200328221703.48171-1-joel@joelfernandes.org> References: <20200328221703.48171-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The dynticks counter are confusing due to crowbar writes of DYNTICK_IRQ_NONIDLE whose purpose is to detect half-interrupts (i.e. we see rcu_irq_enter() but not rcu_irq_exit() due to a usermode upcall) and if so then do a reset of the dyntick_nmi_nesting counters. This patch tries to get rid of DYNTICK_IRQ_NONIDLE while still keeping the code working, fully functional, and less confusing. The confusion recently has even led to patches forgetting that DYNTICK_IRQ_NONIDLE was written to which wasted lots of time. The patch has the following changes: (1) Use dynticks_nesting instead of dynticks_nmi_nesting for determining outer most "EQS exit". This is needed to detect in rcu_nmi_enter_common() if we have already EQS-exited, such as because of a syscall. Currently we rely on a forced write of DYNTICK_IRQ_NONIDLE from rcu_eqs_exit() for this purpose. This is one purpose of the DYNTICK_IRQ_NONIDLE write (other than detecting half-interrupts). However, we do not need to do that. dyntick_nesting already tells us that we have EQS-exited so just use that thus removing the dependence of dynticks_nmi_nesting for this purpose. (2) Keep dynticks_nmi_nesting around because: (a) rcu_is_cpu_rrupt_from_idle() needs to be able to detect first interrupt nesting level. (b) We need to detect half-interrupts till we are sure they're not an issue. However, change the comparison to DYNTICK_IRQ_NONIDLE with 0. (3) Since we got rid of DYNTICK_IRQ_NONIDLE, we also do cheaper comparisons with zero instead for the code that keeps the tick on in rcu_nmi_enter_common(). In the next patch, both of the concerns of (2) will be addressed and then we can get rid of dynticks_nmi_nesting, however one step at a time. Signed-off-by: Joel Fernandes (Google) --- kernel/rcu/rcu.h | 4 ---- kernel/rcu/tree.c | 60 +++++++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/kernel/rcu/rcu.h b/kernel/rcu/rcu.h index 05f936ed167a7..429065a75e05f 100644 --- a/kernel/rcu/rcu.h +++ b/kernel/rcu/rcu.h @@ -12,10 +12,6 @@ #include -/* Offset to allow distinguishing irq vs. task-based idle entry/exit. */ -#define DYNTICK_IRQ_NONIDLE ((LONG_MAX / 2) + 1) - - /* * Grace-period counter management. */ diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 0e5304bad705a..f1c7cbad97d09 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -71,7 +71,6 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = { .dynticks_nesting = 1, - .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE, .dynticks = ATOMIC_INIT(1), }; static struct rcu_state rcu_state = { @@ -504,17 +503,19 @@ EXPORT_SYMBOL_GPL(rcutorture_get_gp_data); /* * Enter an RCU extended quiescent state, which can be either the * idle loop or adaptive-tickless usermode execution. - * - * We crowbar the ->dynticks_nmi_nesting field to zero to allow for - * the possibility of usermode upcalls having messed up our count - * of interrupt nesting level during the prior busy period. */ static void rcu_eqs_enter(bool user) { struct rcu_data *rdp = this_cpu_ptr(&rcu_data); - WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE); - WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); + /* + * Entering usermode/idle from interrupt is not handled. These would + * mean usermode upcalls or idle exit happened from interrupts. Remove + * the warning by 2020. + */ + if (WARN_ON_ONCE(rdp->dynticks_nmi_nesting != 0)) + WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); + WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && rdp->dynticks_nesting == 0); if (rdp->dynticks_nesting != 1) { @@ -589,26 +590,29 @@ static __always_inline void rcu_nmi_exit_common(bool irq) * (We are exiting an NMI handler, so RCU better be paying attention * to us!) */ + WARN_ON_ONCE(rdp->dynticks_nesting <= 0); WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0); WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs()); + WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */ + rdp->dynticks_nmi_nesting - 1); /* * If the nesting level is not 1, the CPU wasn't RCU-idle, so * leave it in non-RCU-idle state. */ - if (rdp->dynticks_nmi_nesting != 1) { + if (rdp->dynticks_nesting != 1) { trace_rcu_dyntick(TPS("StillNonIdle"), TPS("IRQ"), - rdp->dynticks_nmi_nesting, - rdp->dynticks_nmi_nesting - 2, atomic_read(&rdp->dynticks)); - WRITE_ONCE(rdp->dynticks_nmi_nesting, /* No store tearing. */ - rdp->dynticks_nmi_nesting - 2); + rdp->dynticks_nesting, + rdp->dynticks_nesting - 2, atomic_read(&rdp->dynticks)); + WRITE_ONCE(rdp->dynticks_nesting, /* No store tearing. */ + rdp->dynticks_nesting - 2); return; } /* This NMI interrupted an RCU-idle CPU, restore RCU-idleness. */ - trace_rcu_dyntick(TPS("Start"), TPS("IRQ"), rdp->dynticks_nmi_nesting, + trace_rcu_dyntick(TPS("Start"), TPS("IRQ"), rdp->dynticks_nesting, 0, atomic_read(&rdp->dynticks)); - WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); /* Avoid store tearing. */ + WRITE_ONCE(rdp->dynticks_nesting, 0); /* Avoid store tearing. */ if (irq) rcu_prepare_for_idle(); @@ -673,10 +677,6 @@ void rcu_irq_exit_irqson(void) /* * Exit an RCU extended quiescent state, which can be either the * idle loop or adaptive-tickless usermode execution. - * - * We crowbar the ->dynticks_nmi_nesting field to DYNTICK_IRQ_NONIDLE to - * allow for the possibility of usermode upcalls messing up our count of - * interrupt nesting level during the busy period that is just now starting. */ static void rcu_eqs_exit(bool user) { @@ -698,8 +698,14 @@ static void rcu_eqs_exit(bool user) rdp->dynticks_nesting, 1, atomic_read(&rdp->dynticks)); WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current)); WRITE_ONCE(rdp->dynticks_nesting, 1); - WARN_ON_ONCE(rdp->dynticks_nmi_nesting); - WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE); + + /* + * Exiting usermode/idle from interrupt is not handled. These would + * mean usermode upcalls or idle exit happened from interrupts. Remove + * the warning by 2020. + */ + if (WARN_ON_ONCE(rdp->dynticks_nmi_nesting != 0)) + WRITE_ONCE(rdp->dynticks_nmi_nesting, 0); } /** @@ -755,6 +761,7 @@ static __always_inline void rcu_nmi_enter_common(bool irq) struct rcu_data *rdp = this_cpu_ptr(&rcu_data); /* Complain about underflow. */ + WARN_ON_ONCE(rdp->dynticks_nesting < 0); WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0); /* @@ -777,8 +784,8 @@ static __always_inline void rcu_nmi_enter_common(bool irq) incby = 1; } else if (tick_nohz_full_cpu(rdp->cpu) && - rdp->dynticks_nmi_nesting == DYNTICK_IRQ_NONIDLE && - READ_ONCE(rdp->rcu_urgent_qs) && !rdp->rcu_forced_tick) { + !rdp->dynticks_nmi_nesting && READ_ONCE(rdp->rcu_urgent_qs) + && !rdp->rcu_forced_tick) { raw_spin_lock_rcu_node(rdp->mynode); // Recheck under lock. if (rdp->rcu_urgent_qs && !rdp->rcu_forced_tick) { @@ -789,11 +796,14 @@ static __always_inline void rcu_nmi_enter_common(bool irq) } trace_rcu_dyntick(incby == 1 ? TPS("End") : TPS("StillNonIdle"), - TPS("IRQ"), rdp->dynticks_nmi_nesting, - rdp->dynticks_nmi_nesting + incby, atomic_read(&rdp->dynticks)); + TPS("IRQ"), rdp->dynticks_nesting, + rdp->dynticks_nesting + incby, atomic_read(&rdp->dynticks)); + + WRITE_ONCE(rdp->dynticks_nesting, /* Prevent store tearing. */ + rdp->dynticks_nesting + incby); WRITE_ONCE(rdp->dynticks_nmi_nesting, /* Prevent store tearing. */ - rdp->dynticks_nmi_nesting + incby); + rdp->dynticks_nmi_nesting + 1); barrier(); } -- 2.26.0.rc2.310.g2932bb562d-goog