* [PATCH v2] rcu: Robustify rcu_is_cpu_rrupt_from_idle()
@ 2025-04-29 10:08 Frederic Weisbecker
2025-05-02 15:13 ` Joel Fernandes
0 siblings, 1 reply; 2+ messages in thread
From: Frederic Weisbecker @ 2025-04-29 10:08 UTC (permalink / raw)
To: Joel Fernandes
Cc: LKML, Frederic Weisbecker, Boqun Feng, Neeraj Upadhyay,
Paul E . McKenney, Uladzislau Rezki, Zqiang, rcu
RCU relies on the context tracking nesting counter in order to determine
if it is running in extended quiescent state.
However the context tracking nesting counter is not completely
synchronized with the actual context tracking state:
* The nesting counter is set to 1 or incremented further _after_ the
actual state is set to RCU watching.
* The nesting counter is set to 0 or decremented further _before_ the
actual state is set to RCU not watching.
Therefore it is safe to assume that if ct_nesting() > 0, RCU is
watching. But if ct_nesting() <= 0, RCU is not watching except for tiny
windows.
This hasn't been a problem so far because rcu_is_cpu_rrupt_from_idle()
has only been called from interrupts. However the code is confusing
and abuses the role of the context tracking nesting counter while there
are more accurate indicators available.
Clarify and robustify accordingly.
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
---
kernel/rcu/tree.c | 27 +++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index 90d0214c05c7..cbcd579c5630 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -377,7 +377,7 @@ EXPORT_SYMBOL_GPL(rcu_momentary_eqs);
*/
static int rcu_is_cpu_rrupt_from_idle(void)
{
- long nesting;
+ long nmi_nesting = ct_nmi_nesting();
/*
* Usually called from the tick; but also used from smp_function_call()
@@ -389,21 +389,28 @@ static int rcu_is_cpu_rrupt_from_idle(void)
/* Check for counter underflows */
RCU_LOCKDEP_WARN(ct_nesting() < 0,
"RCU nesting counter underflow!");
- RCU_LOCKDEP_WARN(ct_nmi_nesting() <= 0,
- "RCU nmi_nesting counter underflow/zero!");
- /* Are we at first interrupt nesting level? */
- nesting = ct_nmi_nesting();
- if (nesting > 1)
+ /* Non-idle interrupt or nested idle interrupt */
+ if (nmi_nesting > 1)
return false;
/*
- * If we're not in an interrupt, we must be in the idle task!
+ * Non nested idle interrupt (interrupting section where RCU
+ * wasn't watching).
*/
- WARN_ON_ONCE(!nesting && !is_idle_task(current));
+ if (nmi_nesting == 1)
+ return true;
- /* Does CPU appear to be idle from an RCU standpoint? */
- return ct_nesting() == 0;
+ /* Not in an interrupt */
+ if (!nmi_nesting) {
+ RCU_LOCKDEP_WARN(!in_task() || !is_idle_task(current),
+ "RCU nmi_nesting counter not in idle task!");
+ return !rcu_is_watching_curr_cpu();
+ }
+
+ RCU_LOCKDEP_WARN(1, "RCU nmi_nesting counter underflow/zero!");
+
+ return false;
}
#define DEFAULT_RCU_BLIMIT (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) ? 1000 : 10)
--
2.48.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] rcu: Robustify rcu_is_cpu_rrupt_from_idle()
2025-04-29 10:08 [PATCH v2] rcu: Robustify rcu_is_cpu_rrupt_from_idle() Frederic Weisbecker
@ 2025-05-02 15:13 ` Joel Fernandes
0 siblings, 0 replies; 2+ messages in thread
From: Joel Fernandes @ 2025-05-02 15:13 UTC (permalink / raw)
To: Frederic Weisbecker
Cc: LKML, Boqun Feng, Neeraj Upadhyay, Paul E . McKenney,
Uladzislau Rezki, Zqiang, rcu
On 4/29/2025 6:08 AM, Frederic Weisbecker wrote:
> RCU relies on the context tracking nesting counter in order to determine
> if it is running in extended quiescent state.
>
> However the context tracking nesting counter is not completely
> synchronized with the actual context tracking state:
>
> * The nesting counter is set to 1 or incremented further _after_ the
> actual state is set to RCU watching.
>
> * The nesting counter is set to 0 or decremented further _before_ the
> actual state is set to RCU not watching.
>
> Therefore it is safe to assume that if ct_nesting() > 0, RCU is
> watching. But if ct_nesting() <= 0, RCU is not watching except for tiny
> windows.
>
> This hasn't been a problem so far because rcu_is_cpu_rrupt_from_idle()
> has only been called from interrupts. However the code is confusing
> and abuses the role of the context tracking nesting counter while there
> are more accurate indicators available.
>
> Clarify and robustify accordingly.
>
> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
> ---
This seems like an important change but not urgent or fixing a bug, so I will
put it in my rcu/dev branch as well so it is easy for Neeraj to take.
thanks,
- Joel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-05-02 15:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-29 10:08 [PATCH v2] rcu: Robustify rcu_is_cpu_rrupt_from_idle() Frederic Weisbecker
2025-05-02 15:13 ` Joel Fernandes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).