Skip to contentSkip to conversation

RCU changes for PREEMPT_LAZY

PATCH Superseded Ankur Arora 3 participants 36 messages Nov 6, 2024 – Nov 29, 2024

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover RCU changes for PREEMPT_LAZY

This series adds RCU and some leftover scheduler bits for lazy preemption.

The main problem addressed in the RCU related patches is that before PREEMPT_LAZY, PREEMPTION=y implied PREEMPT_RCU=y. With PREEMPT_LAZY, that's no longer true.

That's because PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, and configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, with standalone PREEMPT_LAZY (much like PREEMPT_NONE, PREEMPT_VOLUNTARY) we want to use PREEMPT_RCU=n. And, when used in conjunction with PREEMPT_DYNAMIC, we continue to use PREEMPT_RCU=y.

Patches 1 and 2 are cleanup patches:
"rcu: fix header guard for rcu_all_qs()"
"rcu: rename PREEMPT_AUTO to PREEMPT_LAZY"

Patch 3, "rcu: limit PREEMPT_RCU configurations", explicitly limits PREEMPT_RCU=y to the PREEMPT_DYNAMIC or the latency oriented models.

Patches 4 and 5,
"rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y" "osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y"

handle quiescent states for the (PREEMPT_LAZY=y, PREEMPT_RCU=n) configuration.

And, finally patch-6
"sched: warn for high latency with TIF_NEED_RESCHED_LAZY"
adds high latency warning for TIF_NEED_RESCHED_LAZY.

Goes on top of PeterZ's tree:

git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git sched/core

Changelog:
- fixup incorrect usage of tif_need_resched_lazy() (comment from from Sebastian Andrzej Siewior)
- massaged the commit messages a bit
- drops the powerpc support for PREEMPT_LAZY as that was orthogonal to this series (Shrikanth will send that out separately.)

Please review.

Ankur Arora (6):
rcu: fix header guard for rcu_all_qs()
rcu: rename PREEMPT_AUTO to PREEMPT_LAZY
rcu: limit PREEMPT_RCU configurations
rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y
osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y
sched: warn for high latency with TIF_NEED_RESCHED_LAZY

 include/linux/rcutree.h      |  2 +-
 include/linux/srcutiny.h     |  2 +-
 kernel/rcu/Kconfig           |  4 ++--
 kernel/rcu/srcutiny.c        | 14 +++++++-------
 kernel/rcu/tree_plugin.h     | 11 +++++++----
 kernel/sched/core.c          |  3 ++-
 kernel/sched/debug.c         |  7 +++++--
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 8 files changed, 37 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH v2 1/6 rcu: fix header guard for rcu_all_qs()

rcu_all_qs() is defined for !CONFIG_PREEMPT_RCU but the declaration is conditioned on CONFIG_PREEMPTION.

With CONFIG_PREEMPT_LAZY, CONFIG_PREEMPTION=y does not imply CONFIG_PREEMPT_RCU=y.

Decouple the two.

Cc Paul E. McKenney <[email protected]>
---
 include/linux/rcutree.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/rcutree.h +1 -1
index 90a684f94776..ae8b5cb475a3 100644
@@ -104,7 +104,7 @@ extern int rcu_scheduler_active;
 void rcu_end_inkernel_boot(void);
 bool rcu_inkernel_boot_has_ended(void);
 bool rcu_is_watching(void);
-#ifndef CONFIG_PREEMPTION
+#ifndef CONFIG_PREEMPT_RCU
 void rcu_all_qs(void);
 #endif
 
Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] rcu: fix header guard for rcu_all_qs()

Quoted 10 lines · Ankur Arora

rcu_all_qs() is defined for !CONFIG_PREEMPT_RCU but the declaration is conditioned on CONFIG_PREEMPTION.

With CONFIG_PREEMPT_LAZY, CONFIG_PREEMPTION=y does not imply CONFIG_PREEMPT_RCU=y.

Decouple the two.

Cc Paul E. McKenney <[email protected]>
Reviewed-by on v2 1/6
#3 lore raw

Re: [PATCH v2 1/6] rcu: fix header guard for rcu_all_qs()

In reply to Ankur Arora
Quoted 10 lines · Ankur Arora

rcu_all_qs() is defined for !CONFIG_PREEMPT_RCU but the declaration is conditioned on CONFIG_PREEMPTION.

With CONFIG_PREEMPT_LAZY, CONFIG_PREEMPTION=y does not imply CONFIG_PREEMPT_RCU=y.

Decouple the two.

Cc Paul E. McKenney <[email protected]>

Sebastian

#4 lore raw

Re: [PATCH v2 1/6] rcu: fix header guard for rcu_all_qs()

Quoted 7 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Ankur Arora

rcu_all_qs() is defined for !CONFIG_PREEMPT_RCU but the declaration is conditioned on CONFIG_PREEMPTION.

With CONFIG_PREEMPT_LAZY, CONFIG_PREEMPTION=y does not imply CONFIG_PREEMPT_RCU=y.

Quoted 6 lines
Quoted 4 lines

Decouple the two.

Cc Paul E. McKenney <[email protected]>

Thanks!

--
ankur

#5 lore raw

PATCH v2 2/6 rcu: rename PREEMPT_AUTO to PREEMPT_LAZY

In reply to Ankur Arora

Replace mentions of PREEMPT_AUTO with PREEMPT_LAZY.

Also, since PREMPT_LAZY implies PREEMPTION, we can just reduce the TASKS_RCU selection criteria from:

NEED_TASKS_RCU && (PREEMPTION || PREEMPT_AUTO)
to:
NEED_TASKS_RCU && PREEMPTION

CC Paul E. McKenney <[email protected]>
---
 include/linux/srcutiny.h |  2 +-
 kernel/rcu/Kconfig       |  2 +-
 kernel/rcu/srcutiny.c    | 14 +++++++-------
 3 files changed, 9 insertions(+), 9 deletions(-)
include/linux/srcutiny.h +1 -1
index 4d96bbdb45f0..1635c5e2662f 100644
@@ -64,7 +64,7 @@ static inline int __srcu_read_lock(struct srcu_struct *ssp)
 {
 	int idx;
 
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	idx = ((READ_ONCE(ssp->srcu_idx) + 1) & 0x2) >> 1;
 	WRITE_ONCE(ssp->srcu_lock_nesting[idx], READ_ONCE(ssp->srcu_lock_nesting[idx]) + 1);
 	preempt_enable();
kernel/rcu/Kconfig +1 -1
index 3e079de0f5b4..5a7ff5e1cdcb 100644
@@ -91,7 +91,7 @@ config NEED_TASKS_RCU
 
 config TASKS_RCU
 	bool
-	default NEED_TASKS_RCU && (PREEMPTION || PREEMPT_AUTO)
+	default NEED_TASKS_RCU && PREEMPTION
 	select IRQ_WORK
 
 config FORCE_TASKS_RUDE_RCU
kernel/rcu/srcutiny.c +7 -7
index 549c03336ee9..8a662d911abd 100644
@@ -98,7 +98,7 @@ void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
 {
 	int newval;
 
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1;
 	WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
 	preempt_enable();
@@ -120,7 +120,7 @@ void srcu_drive_gp(struct work_struct *wp)
 	struct srcu_struct *ssp;
 
 	ssp = container_of(wp, struct srcu_struct, srcu_work);
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	if (ssp->srcu_gp_running || ULONG_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) {
 		return; /* Already running or nothing to do. */
 		preempt_enable();
@@ -138,7 +138,7 @@ void srcu_drive_gp(struct work_struct *wp)
 	WRITE_ONCE(ssp->srcu_gp_waiting, true);  /* srcu_read_unlock() wakes! */
 	preempt_enable();
 	swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
 	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
 	preempt_enable();
@@ -159,7 +159,7 @@ void srcu_drive_gp(struct work_struct *wp)
 	 * at interrupt level, but the ->srcu_gp_running checks will
 	 * straighten that out.
 	 */
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	WRITE_ONCE(ssp->srcu_gp_running, false);
 	idx = ULONG_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max));
 	preempt_enable();
@@ -172,7 +172,7 @@ static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
 {
 	unsigned long cookie;
 
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	cookie = get_state_synchronize_srcu(ssp);
 	if (ULONG_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie)) {
 		preempt_enable();
@@ -199,7 +199,7 @@ void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
 
 	rhp->func = func;
 	rhp->next = NULL;
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	local_irq_save(flags);
 	*ssp->srcu_cb_tail = rhp;
 	ssp->srcu_cb_tail = &rhp->next;
@@ -261,7 +261,7 @@ unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
 {
 	unsigned long ret;
 
-	preempt_disable();  // Needed for PREEMPT_AUTO
+	preempt_disable();  // Needed for PREEMPT_LAZY
 	ret = get_state_synchronize_srcu(ssp);
 	srcu_gp_start_if_needed(ssp);
 	preempt_enable();
Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 2/6
#6 lore raw

Re: [PATCH v2 2/6] rcu: rename PREEMPT_AUTO to PREEMPT_LAZY

Quoted 11 lines · Ankur Arora

Replace mentions of PREEMPT_AUTO with PREEMPT_LAZY.

Also, since PREMPT_LAZY implies PREEMPTION, we can just reduce the TASKS_RCU selection criteria from:

NEED_TASKS_RCU && (PREEMPTION || PREEMPT_AUTO)
to:
NEED_TASKS_RCU && PREEMPTION

CC Paul E. McKenney <[email protected]>
#7 lore raw

Re: [PATCH v2 2/6] rcu: rename PREEMPT_AUTO to PREEMPT_LAZY

Quoted 6 lines · Frederic Weisbecker
Quoted 5 lines · Ankur Arora

Replace mentions of PREEMPT_AUTO with PREEMPT_LAZY.

Also, since PREMPT_LAZY implies PREEMPTION, we can just reduce the TASKS_RCU selection criteria from:

Quoted 8 lines
Quoted 6 lines

NEED_TASKS_RCU && (PREEMPTION || PREEMPT_AUTO)
to:
NEED_TASKS_RCU && PREEMPTION

CC Paul E. McKenney <[email protected]>

Thanks!

--
ankur

Reviewed-by on v2 2/6
#8 lore raw

Re: [PATCH v2 2/6] rcu: rename PREEMPT_AUTO to PREEMPT_LAZY

In reply to Ankur Arora
Quoted 11 lines · Ankur Arora

Replace mentions of PREEMPT_AUTO with PREEMPT_LAZY.

Also, since PREMPT_LAZY implies PREEMPTION, we can just reduce the TASKS_RCU selection criteria from:

NEED_TASKS_RCU && (PREEMPTION || PREEMPT_AUTO)
to:
NEED_TASKS_RCU && PREEMPTION

CC Paul E. McKenney <[email protected]>

Sebastian

#9 lore raw

PATCH v2 3/6 rcu: limit PREEMPT_RCU configurations

In reply to Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +1 -1
index 5a7ff5e1cdcb..9d52f87fac27 100644
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is
Signature · 1 line
2.43.5
maintainer Reviewed-by on v2 3/6
#10 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +1 -1
index 58d84c59f3dd..368f00267d4e 100644
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Let me audit further if we missed something else...

Thanks.

Quoted 3 lines
Signature · 1 line
2.43.5
#11 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 30 lines · Frederic Weisbecker
Quoted 29 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644
@@ -18,7 +18,7 @@ config TREE_RCU
 
Quoted 9 lines

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 7 lines
kernel/rcu/Kconfig +1 -1
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

Thanks!

Quoted 8 lines

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy

I think I see your point about rdp being racy, but given that rcu_read_unlock_strict() would always be called with a non-zero preemption count (with CONFIG_PREEMPTION), wouldn't the preempt_count() check defeat any calls to rcu_read_unlock_strict()?

    void rcu_read_unlock_strict(void)
    {
            struct rcu_data *rdp;

            if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
                    return;

Or am I missing something?

Ankur

Quoted 27 lines

(though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +1 -1
index 58d84c59f3dd..368f00267d4e 100644
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Let me audit further if we missed something else...

Thanks.

Quoted 3 lines

--
2.43.5

#12 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 14 lines · Ankur Arora
Quoted 8 lines

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy

I think I see your point about rdp being racy, but given that rcu_read_unlock_strict() would always be called with a non-zero preemption count (with CONFIG_PREEMPTION), wouldn't the preempt_count() check defeat any calls to rcu_read_unlock_strict()?

Quoted 8 lines
    void rcu_read_unlock_strict(void)
    {
            struct rcu_data *rdp;

            if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
                    return;

Or am I missing something?

This is indeed broken. By moving preempt_disable() as Frederic suggested then rcu_read_unlock_strict() becomes a NOP. Keeping this as-is results in spats due to this_cpu_ptr() in preemptible regions. Looking further we have "rdp->cpu != smp_processor_id()" as the next candidate.

That preempt_disable() should go to rcu_read_unlock_strict() after the check.

Quoted 1 line

Ankur

Sebastian

#13 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 15 lines · Sebastian Andrzej Siewior
Quoted 14 lines · Ankur Arora
Quoted 8 lines

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy

I think I see your point about rdp being racy, but given that rcu_read_unlock_strict() would always be called with a non-zero preemption count (with CONFIG_PREEMPTION), wouldn't the preempt_count() check defeat any calls to rcu_read_unlock_strict()?

Quoted 8 lines
Quoted 8 lines
    void rcu_read_unlock_strict(void)
    {
            struct rcu_data *rdp;

            if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
                    return;

Or am I missing something?

Haha, right!

Quoted 8 lines

This is indeed broken. By moving preempt_disable() as Frederic suggested then rcu_read_unlock_strict() becomes a NOP. Keeping this as-is results in spats due to this_cpu_ptr() in preemptible regions. Looking further we have "rdp->cpu != smp_processor_id()" as the next candidate.

That preempt_disable() should go to rcu_read_unlock_strict() after the check.

Yeah that looks better ;-)

Quoted 4 lines
Quoted 2 lines

Ankur

Sebastian

#14 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

In reply to Frederic Weisbecker
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

kernel/rcu/tree_plugin.h +8 -1
index 1c7cbd145d5e..8fc67639d3a7 100644
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);
 
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       /*
+        * rcu_report_qs_rdp() can only be invoked with a stable rdp and
+        * and from the local CPU.
+        * With CONFIG_PREEMPTION=y, do this while holding the last
+        * preempt_count which gets dropped after __rcu_read_unlock().
+        */
+       if (irqs_disabled() || pc > 1 || !rcu_state.gp_kthread)
                return;
        rdp = this_cpu_ptr(&rcu_data);
        rdp->cpu_no_qs.b.norm = false;

Thanks

--
ankur

#15 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 77 lines · Ankur Arora
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

kernel/rcu/tree_plugin.h +0 -0
index 1c7cbd145d5e..8fc67639d3a7 100644

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 834

Quoted 4 lines
kernel/rcu/tree_plugin.h +1 -0
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);

This should be in_atomic_preempt_off(), otherwise softirqs and IRQs are spuriously accounted as quiescent states.

Thanks.

Quoted 18 lines
kernel/rcu/tree_plugin.h +7 -1
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       /*
+        * rcu_report_qs_rdp() can only be invoked with a stable rdp and
+        * and from the local CPU.
+        * With CONFIG_PREEMPTION=y, do this while holding the last
+        * preempt_count which gets dropped after __rcu_read_unlock().
+        */
+       if (irqs_disabled() || pc > 1 || !rcu_state.gp_kthread)
                return;
        rdp = this_cpu_ptr(&rcu_data);
        rdp->cpu_no_qs.b.norm = false;
 

Thanks

--
ankur

#16 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 73 lines · Frederic Weisbecker
Quoted 72 lines · Ankur Arora
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 834

Quoted 9 lines
kernel/rcu/tree_plugin.h +1 -0
index 1c7cbd145d5e..8fc67639d3a7 100644
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);

This should be in_atomic_preempt_off(), otherwise softirqs and IRQs are spuriously accounted as quiescent states.

Not sure I got that. Won't ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT) give us task only preempt count?

And, given that the preempt_count is at least 1, the (pc > 1) check below would ensure we have a stable rdp and call rcu_report_qs_rdp() before dropping the last preempt-count.

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 845

Quoted 12 lines
kernel/rcu/tree_plugin.h +7 -1
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       /*
+        * rcu_report_qs_rdp() can only be invoked with a stable rdp and
+        * and from the local CPU.
+        * With CONFIG_PREEMPTION=y, do this while holding the last
+        * preempt_count which gets dropped after __rcu_read_unlock().
+        */
+       if (irqs_disabled() || pc > 1 || !rcu_state.gp_kthread)
                return;
        rdp = this_cpu_ptr(&rcu_data);
        rdp->cpu_no_qs.b.norm = false;

Thanks

--
ankur

#17 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 88 lines · Ankur Arora
Quoted 73 lines · Frederic Weisbecker
Quoted 72 lines · Ankur Arora
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 834

Quoted 9 lines
kernel/rcu/tree_plugin.h +1 -0
index 1c7cbd145d5e..8fc67639d3a7 100644
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);

This should be in_atomic_preempt_off(), otherwise softirqs and IRQs are spuriously accounted as quiescent states.

Quoted 2 lines

Not sure I got that. Won't ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT) give us task only preempt count?

Oh wait. I see your point now. My check is too narrow.

Great. This'll work:

-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       if (irqs_disabled() || in_atomic_preempt_off()|| !rcu_state.gp_kthread)

Thanks

Ankur

Quoted 17 lines

And, given that the preempt_count is at least 1, the (pc > 1) check below would ensure we have a stable rdp and call rcu_report_qs_rdp() before dropping the last preempt-count.

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 845

Quoted 12 lines
kernel/rcu/tree_plugin.h +7 -1
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       /*
+        * rcu_report_qs_rdp() can only be invoked with a stable rdp and
+        * and from the local CPU.
+        * With CONFIG_PREEMPTION=y, do this while holding the last
+        * preempt_count which gets dropped after __rcu_read_unlock().
+        */
+       if (irqs_disabled() || pc > 1 || !rcu_state.gp_kthread)
                return;
        rdp = this_cpu_ptr(&rcu_data);
        rdp->cpu_no_qs.b.norm = false;
Quoted 1 line

Thanks

--
ankur

#18 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 101 lines · Ankur Arora
Quoted 88 lines · Ankur Arora
Quoted 73 lines · Frederic Weisbecker
Quoted 72 lines · Ankur Arora
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 834

Quoted 9 lines
kernel/rcu/tree_plugin.h +1 -0
index 1c7cbd145d5e..8fc67639d3a7 100644
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);

This should be in_atomic_preempt_off(), otherwise softirqs and IRQs are spuriously accounted as quiescent states.

Quoted 2 lines

Not sure I got that. Won't ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT) give us task only preempt count?

Oh wait. I see your point now. My check is too narrow.

Great. This'll work:

(quoted diff) +1 -1
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       if (irqs_disabled() || in_atomic_preempt_off()|| !rcu_state.gp_kthread)
Quoted 1 line

Thanks

Do you plan to integrate this in a further version of your set? Or should I send a patch?

Thanks.

#19 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 99 lines · Frederic Weisbecker
Quoted 98 lines · Ankur Arora
Quoted 88 lines · Ankur Arora
Quoted 73 lines · Frederic Weisbecker
Quoted 72 lines · Ankur Arora
Quoted 55 lines · Frederic Weisbecker
Quoted 28 lines · Ankur Arora

PREEMPT_LAZY can be enabled stand-alone or alongside PREEMPT_DYNAMIC which allows for dynamic switching of preemption models.

The choice of PREEMPT_RCU or not, however, is fixed at compile time.

Given that PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, explicitly limit PREEMPT_RCU=y to the latency oriented preemption models: PREEMPT, PREEMPT_RT, and the runtime configurable model PREEMPT_DYNAMIC.

This means the throughput oriented models, PREEMPT_NONE, PREEMPT_VOLUNTARY and PREEMPT_LAZY will run with PREEMPT_RCU=n.

Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/rcu/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/Kconfig +0 -0
index 5a7ff5e1cdcb..9d52f87fac27 100644

kernel/rcu/Kconfig config TREE_RCU · line 24

Quoted 8 lines
kernel/rcu/Kconfig +1 -1
@@ -18,7 +18,7 @@ config TREE_RCU
 
 config PREEMPT_RCU
 	bool
-	default y if PREEMPTION
+	default y if (PREEMPT || PREEMPT_RT || PREEMPT_DYNAMIC)
 	select TREE_RCU
 	help
 	  This option selects the RCU implementation that is

But looking at !CONFIG_PREEMPT_RCU code on tree_plugin.h, I see some issues now that the code can be preemptible. Well I think it has always been preemptible but PREEMPTION && !PREEMPT_RCU has seldom been exerciced (or was it even possible?).

For example rcu_read_unlock_strict() can be called with preemption enabled so we need the following otherwise the rdp is unstable, the norm value becomes racy (though automagically fixed in rcu_report_qs_rdp()) and rcu_report_qs_rdp() might warn.

include/linux/rcupdate.h +0 -0
index 58d84c59f3dd..368f00267d4e 100644

include/linux/rcupdate.h static inline void __rcu_read_lock(void) · line 103

Quoted 10 lines
include/linux/rcupdate.h +1 -1
@@ -95,9 +95,9 @@ static inline void __rcu_read_lock(void)
 
 static inline void __rcu_read_unlock(void)
 {
-	preempt_enable();
 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
 		rcu_read_unlock_strict();
+	preempt_enable();
 }
 
 static inline int rcu_preempt_depth(void)

Based on the discussion on the thread, how about keeping this and changing the preempt_count check in rcu_read_unlock_strict() instead?

Quoted 12 lines

kernel/rcu/tree_plugin.h dump_blkd_tasks(struct rcu_node *rnp, int ncheck) · line 834

Quoted 9 lines
kernel/rcu/tree_plugin.h +1 -0
index 1c7cbd145d5e..8fc67639d3a7 100644
@@ -831,8 +831,15 @@ dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
 void rcu_read_unlock_strict(void)
 {
        struct rcu_data *rdp;
+       int pc = ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT);

This should be in_atomic_preempt_off(), otherwise softirqs and IRQs are spuriously accounted as quiescent states.

Quoted 2 lines

Not sure I got that. Won't ((preempt_count() & PREEMPT_MASK) >> PREEMPT_SHIFT) give us task only preempt count?

Oh wait. I see your point now. My check is too narrow.

Great. This'll work:

Quoted 7 lines
Quoted 4 lines
(quoted diff) +1 -1
-       if (irqs_disabled() || preempt_count() || !rcu_state.gp_kthread)
+       if (irqs_disabled() || in_atomic_preempt_off()|| !rcu_state.gp_kthread)

Thanks

Do you plan to integrate this in a further version of your set? Or should I send a patch?

Sure. I can add it to v3. Okay, if I add your suggested-by?

--
ankur

#20 lore raw

Re: [PATCH v2 3/6] rcu: limit PREEMPT_RCU configurations

Quoted 4 lines · Ankur Arora
Quoted 2 lines

Do you plan to integrate this in a further version of your set? Or should I send a patch?

Sure. I can add it to v3. Okay, if I add your suggested-by?

Sure! Thanks.

#21 lore raw

PATCH v2 4/6 rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y

In reply to Ankur Arora

With PREEMPT_RCU=n, cond_resched() provides urgently needed quiescent states for read-side critical sections via rcu_all_qs(). One reason why this was needed, was lacking preempt-count, the tick handler has no way of knowing whether it is executing in a read-side critical section or not.

With PREEMPT_LAZY=y, there can be configurations with PREEMPT_COUNT=y, PREEMPT_RCU=n, where cond_resched() is a stub that does not provide quiescent states via rcu_all_qs().

So, use the availability of preempt_count() to report quiescent states in rcu_flavor_sched_clock_irq().

---
 kernel/rcu/tree_plugin.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/rcu/tree_plugin.h +7 -4
index 1c7cbd145d5e..da324d66034b 100644
@@ -974,13 +974,16 @@ static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  */
 static void rcu_flavor_sched_clock_irq(int user)
 {
-	if (user || rcu_is_cpu_rrupt_from_idle()) {
+	if (user || rcu_is_cpu_rrupt_from_idle() ||
+	     (IS_ENABLED(CONFIG_PREEMPT_COUNT) &&
+	      !(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)))) {
 
 		/*
 		 * Get here if this CPU took its interrupt from user
-		 * mode or from the idle loop, and if this is not a
-		 * nested interrupt.  In this case, the CPU is in
-		 * a quiescent state, so note it.
+		 * mode, from the idle loop without this being a nested
+		 * interrupt, or while not holding a preempt count (but
+		 * with PREEMPT_COUNT=y. In this case, the CPU is in a
+		 * quiescent state, so note it.
 		 *
 		 * No memory barrier is required here because rcu_qs()
 		 * references only CPU-local variables that other CPUs
Signature · 1 line
2.43.5
Reviewed-by on v2 4/6
#22 lore raw

Re: [PATCH v2 4/6] rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y

Quoted 9 lines · Ankur Arora

With PREEMPT_RCU=n, cond_resched() provides urgently needed quiescent states for read-side critical sections via rcu_all_qs(). One reason why this was needed, was lacking preempt-count, the tick handler has no way of knowing whether it is executing in a read-side critical section or not.

With PREEMPT_LAZY=y, there can be configurations with PREEMPT_COUNT=y, PREEMPT_RCU=n, where cond_resched() is a stub that does not provide quiescent states via rcu_all_qs().

With PREEMPT_LAZY=y && PREEMPT_DYNAMIC=n we get PREEMPT_COUNT=y and PREEMPT_RCU=n. In this configuration cond_resched() is an empty stub and does not provide quiescent states via rcu_all_qs(). PREEMPT_RCU=y provides this information via rcu_read_unlock() and its nesting counter.

Quoted 2 lines

So, use the availability of preempt_count() to report quiescent states in rcu_flavor_sched_clock_irq().

Okay. You might also want to update the cond_resched() comment,

	s@In preemptible kernels, ->rcu_read_lock_nesting@
	  In PREEMPT_RCU kernels, ->rcu_read_lock_nesting@
Quoted 2 lines

Sebastian

#23 lore raw

Re: [PATCH v2 4/6] rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y

Quoted 16 lines · Sebastian Andrzej Siewior
Quoted 9 lines · Ankur Arora

With PREEMPT_RCU=n, cond_resched() provides urgently needed quiescent states for read-side critical sections via rcu_all_qs(). One reason why this was needed, was lacking preempt-count, the tick handler has no way of knowing whether it is executing in a read-side critical section or not.

With PREEMPT_LAZY=y, there can be configurations with PREEMPT_COUNT=y, PREEMPT_RCU=n, where cond_resched() is a stub that does not provide quiescent states via rcu_all_qs().

With PREEMPT_LAZY=y && PREEMPT_DYNAMIC=n we get PREEMPT_COUNT=y and PREEMPT_RCU=n. In this configuration cond_resched() is an empty stub and does not provide quiescent states via rcu_all_qs(). PREEMPT_RCU=y provides this information via rcu_read_unlock() and its nesting counter.

Quoted 6 lines
Quoted 2 lines

So, use the availability of preempt_count() to report quiescent states in rcu_flavor_sched_clock_irq().

Okay. You might also want to update the cond_resched() comment,

	s@In preemptible kernels, ->rcu_read_lock_nesting@
	  In PREEMPT_RCU kernels, ->rcu_read_lock_nesting@

Good point. Will add.

Quoted 1 line

Thanks!

Ankur

Quoted 4 lines
Quoted 2 lines

Sebastian

--
ankur

#24 lore raw

Re: [PATCH v2 4/6] rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y

In reply to Ankur Arora
Quoted 24 lines · Ankur Arora

With PREEMPT_RCU=n, cond_resched() provides urgently needed quiescent states for read-side critical sections via rcu_all_qs(). One reason why this was needed, was lacking preempt-count, the tick handler has no way of knowing whether it is executing in a read-side critical section or not.

With PREEMPT_LAZY=y, there can be configurations with PREEMPT_COUNT=y, PREEMPT_RCU=n, where cond_resched() is a stub that does not provide quiescent states via rcu_all_qs().

So, use the availability of preempt_count() to report quiescent states in rcu_flavor_sched_clock_irq().

---
 kernel/rcu/tree_plugin.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/rcu/tree_plugin.h +0 -0
index 1c7cbd145d5e..da324d66034b 100644

kernel/rcu/tree_plugin.h static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp) · line 979

Quoted 7 lines
kernel/rcu/tree_plugin.h +3 -1
@@ -974,13 +974,16 @@ static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  */
 static void rcu_flavor_sched_clock_irq(int user)
 {
-	if (user || rcu_is_cpu_rrupt_from_idle()) {
+	if (user || rcu_is_cpu_rrupt_from_idle() ||
+	     (IS_ENABLED(CONFIG_PREEMPT_COUNT) &&
+	      !(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)))) {

I'm never sure if nested hardirqs are still possible but just in case,

    preempt_count() == HARDIRQ_OFFSET

might be a more robust check. And that also applies to the PREEMPT_RCU implementation.

Thanks.

Quoted 16 lines
kernel/rcu/tree_plugin.h +4 -3
 		/*
 		 * Get here if this CPU took its interrupt from user
-		 * mode or from the idle loop, and if this is not a
-		 * nested interrupt.  In this case, the CPU is in
-		 * a quiescent state, so note it.
+		 * mode, from the idle loop without this being a nested
+		 * interrupt, or while not holding a preempt count (but
+		 * with PREEMPT_COUNT=y. In this case, the CPU is in a
+		 * quiescent state, so note it.
 		 *
 		 * No memory barrier is required here because rcu_qs()
 		 * references only CPU-local variables that other CPUs
Signature · 1 line
2.43.5
#25 lore raw

PATCH v2 5/6 osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

In reply to Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

---
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_osnoise.c +12 -10
index a50ed23bee77..15e9600d231d 100644
@@ -1538,18 +1538,20 @@ static int run_osnoise(void)
 		/*
 		 * In some cases, notably when running on a nohz_full CPU with
 		 * a stopped tick PREEMPT_RCU has no way to account for QSs.
-		 * This will eventually cause unwarranted noise as PREEMPT_RCU
-		 * will force preemption as the means of ending the current
-		 * grace period. We avoid this problem by calling
-		 * rcu_momentary_eqs(), which performs a zero duration
-		 * EQS allowing PREEMPT_RCU to end the current grace period.
-		 * This call shouldn't be wrapped inside an RCU critical
-		 * section.
+		 * This will eventually cause unwarranted noise as RCU forces
+		 * preemption as the means of ending the current grace period.
+		 * We avoid this by calling rcu_momentary_eqs(), which performs
+		 * a zero duration EQS allowing RCU to end the current grace
+		 * period. This call shouldn't be wrapped inside an RCU
+		 * critical section.
 		 *
-		 * Note that in non PREEMPT_RCU kernels QSs are handled through
-		 * cond_resched()
+		 * For non-PREEMPT_RCU kernels with cond_resched() (non-
+		 * PREEMPT_LAZY configurations), QSs are handled through
+		 * cond_resched(). For PREEMPT_LAZY kernels, we fallback to
+		 * the zero duration QS via rcu_momentary_eqs().
 		 */
-		if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+		if (IS_ENABLED(CONFIG_PREEMPT_RCU) ||
+		    (!IS_ENABLED(CONFIG_PREEMPT_RCU) && IS_ENABLED(CONFIG_PREEMPTION))) {
 			if (!disable_irq)
 				local_irq_disable();
 
Signature · 1 line
2.43.5
#26 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

Quoted 8 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

The problem is as you say CONFIG_PREEMPT_RCU=n + CONFIG_PREEMPTION=y. You can't select any of those two directly but get here via
PREEMPT_LAZY=y + PREEMPT_DYNAMIC=n.

Please spell it out to make it obvious. It is not a large group of configurations, it is exactly this combo.

With PREEMPT_LAZY=y + PREEMPT_DYNAMIC=n however we get PREEMPT_RCU=n which means no direct rcu_momentary_eqs() invocations and cond_resched() is an empty stub.

Quoted 9 lines

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

Sebastian

#27 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

Quoted 10 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Quoted 6 lines

The problem is as you say CONFIG_PREEMPT_RCU=n + CONFIG_PREEMPTION=y. You can't select any of those two directly but get here via
PREEMPT_LAZY=y + PREEMPT_DYNAMIC=n.

Please spell it out to make it obvious. It is not a large group of configurations, it is exactly this combo.

Makes sense.

Will do. Thanks.

Ankur

Quoted 7 lines

With PREEMPT_LAZY=y + PREEMPT_DYNAMIC=n however we get PREEMPT_RCU=n which means no direct rcu_momentary_eqs() invocations and cond_resched() is an empty stub.

Quoted 2 lines

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

Quoted 8 lines
Quoted 6 lines

Sebastian

--
ankur

#28 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

In reply to Ankur Arora
Quoted 46 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

---
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_osnoise.c +6 -9
index a50ed23bee77..15e9600d231d 100644
@@ -1538,18 +1538,20 @@ static int run_osnoise(void)
 		/*
 		 * In some cases, notably when running on a nohz_full CPU with
 		 * a stopped tick PREEMPT_RCU has no way to account for QSs.
-		 * This will eventually cause unwarranted noise as PREEMPT_RCU
-		 * will force preemption as the means of ending the current
-		 * grace period. We avoid this problem by calling
-		 * rcu_momentary_eqs(), which performs a zero duration
-		 * EQS allowing PREEMPT_RCU to end the current grace period.
-		 * This call shouldn't be wrapped inside an RCU critical
-		 * section.
+		 * This will eventually cause unwarranted noise as RCU forces
+		 * preemption as the means of ending the current grace period.
+		 * We avoid this by calling rcu_momentary_eqs(), which performs
+		 * a zero duration EQS allowing RCU to end the current grace
+		 * period. This call shouldn't be wrapped inside an RCU
+		 * critical section.
 		 *
-		 * Note that in non PREEMPT_RCU kernels QSs are handled through
-		 * cond_resched()

kernel/trace/trace_osnoise.c static int run_osnoise(void) · line 1556

Quoted 10 lines
kernel/trace/trace_osnoise.c +6 -1
+		 * For non-PREEMPT_RCU kernels with cond_resched() (non-
+		 * PREEMPT_LAZY configurations), QSs are handled through
+		 * cond_resched(). For PREEMPT_LAZY kernels, we fallback to
+		 * the zero duration QS via rcu_momentary_eqs().
 		 */
-		if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+		if (IS_ENABLED(CONFIG_PREEMPT_RCU) ||
+		    (!IS_ENABLED(CONFIG_PREEMPT_RCU) && IS_ENABLED(CONFIG_PREEMPTION))) {
 			if (!disable_irq)
 				local_irq_disable();

How about making this unconditional so it works everywhere and doesn't rely on cond_resched() Kconfig/preempt-dynamic mood?

Thanks.

Quoted 4 lines
Signature · 2 lines
-- 
2.43.5
#29 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

Quoted 52 lines · Frederic Weisbecker
Quoted 51 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

---
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_osnoise.c +10 -9
index a50ed23bee77..15e9600d231d 100644
@@ -1538,18 +1538,20 @@ static int run_osnoise(void)
 		/*
 		 * In some cases, notably when running on a nohz_full CPU with
 		 * a stopped tick PREEMPT_RCU has no way to account for QSs.
-		 * This will eventually cause unwarranted noise as PREEMPT_RCU
-		 * will force preemption as the means of ending the current
-		 * grace period. We avoid this problem by calling
-		 * rcu_momentary_eqs(), which performs a zero duration
-		 * EQS allowing PREEMPT_RCU to end the current grace period.
-		 * This call shouldn't be wrapped inside an RCU critical
-		 * section.
+		 * This will eventually cause unwarranted noise as RCU forces
+		 * preemption as the means of ending the current grace period.
+		 * We avoid this by calling rcu_momentary_eqs(), which performs
+		 * a zero duration EQS allowing RCU to end the current grace
+		 * period. This call shouldn't be wrapped inside an RCU
+		 * critical section.
 		 *
-		 * Note that in non PREEMPT_RCU kernels QSs are handled through
-		 * cond_resched()
+		 * For non-PREEMPT_RCU kernels with cond_resched() (non-
+		 * PREEMPT_LAZY configurations), QSs are handled through
+		 * cond_resched(). For PREEMPT_LAZY kernels, we fallback to
+		 * the zero duration QS via rcu_momentary_eqs().
 		 */
Quoted 8 lines

kernel/trace/trace_osnoise.c static int run_osnoise(void) · line 1556

Quoted 5 lines
kernel/trace/trace_osnoise.c +2 -1
-		if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+		if (IS_ENABLED(CONFIG_PREEMPT_RCU) ||
+		    (!IS_ENABLED(CONFIG_PREEMPT_RCU) && IS_ENABLED(CONFIG_PREEMPTION))) {
 			if (!disable_irq)
 				local_irq_disable();

How about making this unconditional so it works everywhere and doesn't rely on cond_resched() Kconfig/preempt-dynamic mood?

I think it's a minor matter given that this isn't a hot path, but we don't really need it for the !PREEMPT_RCU configuration.

Still, given that both of those clauses imply CONFIG_PREEMPTION, we can just simplify this to (with an appropriately adjusted comment):

kernel/trace/trace_osnoise.c +1 -1
@@ -1543,7 +1543,7 @@ static int run_osnoise(void)
                 * Note that in non PREEMPT_RCU kernels QSs are handled through
                 * cond_resched()
                 */
-               if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+               if (IS_ENABLED(CONFIG_PREEMPTION)) {
                        if (!disable_irq)
                                local_irq_disable();
 

--
ankur

#30 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

Quoted 64 lines · Ankur Arora
Quoted 52 lines · Frederic Weisbecker
Quoted 51 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

---
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_osnoise.c +10 -9
index a50ed23bee77..15e9600d231d 100644
@@ -1538,18 +1538,20 @@ static int run_osnoise(void)
 		/*
 		 * In some cases, notably when running on a nohz_full CPU with
 		 * a stopped tick PREEMPT_RCU has no way to account for QSs.
-		 * This will eventually cause unwarranted noise as PREEMPT_RCU
-		 * will force preemption as the means of ending the current
-		 * grace period. We avoid this problem by calling
-		 * rcu_momentary_eqs(), which performs a zero duration
-		 * EQS allowing PREEMPT_RCU to end the current grace period.
-		 * This call shouldn't be wrapped inside an RCU critical
-		 * section.
+		 * This will eventually cause unwarranted noise as RCU forces
+		 * preemption as the means of ending the current grace period.
+		 * We avoid this by calling rcu_momentary_eqs(), which performs
+		 * a zero duration EQS allowing RCU to end the current grace
+		 * period. This call shouldn't be wrapped inside an RCU
+		 * critical section.
 		 *
-		 * Note that in non PREEMPT_RCU kernels QSs are handled through
-		 * cond_resched()
+		 * For non-PREEMPT_RCU kernels with cond_resched() (non-
+		 * PREEMPT_LAZY configurations), QSs are handled through
+		 * cond_resched(). For PREEMPT_LAZY kernels, we fallback to
+		 * the zero duration QS via rcu_momentary_eqs().
 		 */
Quoted 8 lines

kernel/trace/trace_osnoise.c static int run_osnoise(void) · line 1548

Quoted 5 lines
kernel/trace/trace_osnoise.c +2 -1
-		if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+		if (IS_ENABLED(CONFIG_PREEMPT_RCU) ||
+		    (!IS_ENABLED(CONFIG_PREEMPT_RCU) && IS_ENABLED(CONFIG_PREEMPTION))) {
 			if (!disable_irq)
 				local_irq_disable();

How about making this unconditional so it works everywhere and doesn't rely on cond_resched() Kconfig/preempt-dynamic mood?

Quoted 2 lines

I think it's a minor matter given that this isn't a hot path, but we don't really need it for the !PREEMPT_RCU configuration.

Well if you make it unconditional, cond_resched() / rcu_all_qs() won't do its own rcu_momentary_qs(), because rcu_data.rcu_urgent_qs should
be false. So that essentially unify the behaviours for all configurations.

Thanks.

Quoted 17 lines

Still, given that both of those clauses imply CONFIG_PREEMPTION, we can just simplify this to (with an appropriately adjusted comment):

kernel/trace/trace_osnoise.c +1 -2
@@ -1543,7 +1543,7 @@ static int run_osnoise(void)
                 * Note that in non PREEMPT_RCU kernels QSs are handled through
                 * cond_resched()
                 */
-               if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+               if (IS_ENABLED(CONFIG_PREEMPTION)) {
                        if (!disable_irq)
                                local_irq_disable();
 
--

ankur

#31 lore raw

Re: [PATCH v2 5/6] osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y

Quoted 65 lines · Frederic Weisbecker
Quoted 64 lines · Ankur Arora
Quoted 60 lines · Frederic Weisbecker
Quoted 46 lines · Ankur Arora

To reduce RCU noise for nohz_full configurations, osnoise depends on cond_resched() providing quiescent states for PREEMPT_RCU=n configurations. And, for PREEMPT_RCU=y configurations does this by directly calling rcu_momentary_eqs().

With PREEMPT_LAZY=y, however, we can have configurations with (PREEMPTION=y, PREEMPT_RCU=n), which means neither of the above can help.

Handle that by fallback to the explicit quiescent states via rcu_momentary_eqs().

---
 kernel/trace/trace_osnoise.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
kernel/trace/trace_osnoise.c +6 -9
index a50ed23bee77..15e9600d231d 100644
@@ -1538,18 +1538,20 @@ static int run_osnoise(void)
 		/*
 		 * In some cases, notably when running on a nohz_full CPU with
 		 * a stopped tick PREEMPT_RCU has no way to account for QSs.
-		 * This will eventually cause unwarranted noise as PREEMPT_RCU
-		 * will force preemption as the means of ending the current
-		 * grace period. We avoid this problem by calling
-		 * rcu_momentary_eqs(), which performs a zero duration
-		 * EQS allowing PREEMPT_RCU to end the current grace period.
-		 * This call shouldn't be wrapped inside an RCU critical
-		 * section.
+		 * This will eventually cause unwarranted noise as RCU forces
+		 * preemption as the means of ending the current grace period.
+		 * We avoid this by calling rcu_momentary_eqs(), which performs
+		 * a zero duration EQS allowing RCU to end the current grace
+		 * period. This call shouldn't be wrapped inside an RCU
+		 * critical section.
 		 *
-		 * Note that in non PREEMPT_RCU kernels QSs are handled through
-		 * cond_resched()

kernel/trace/trace_osnoise.c static int run_osnoise(void) · line 1556

Quoted 10 lines
kernel/trace/trace_osnoise.c +6 -1
+		 * For non-PREEMPT_RCU kernels with cond_resched() (non-
+		 * PREEMPT_LAZY configurations), QSs are handled through
+		 * cond_resched(). For PREEMPT_LAZY kernels, we fallback to
+		 * the zero duration QS via rcu_momentary_eqs().
 		 */
-		if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+		if (IS_ENABLED(CONFIG_PREEMPT_RCU) ||
+		    (!IS_ENABLED(CONFIG_PREEMPT_RCU) && IS_ENABLED(CONFIG_PREEMPTION))) {
 			if (!disable_irq)
 				local_irq_disable();

How about making this unconditional so it works everywhere and doesn't rely on cond_resched() Kconfig/preempt-dynamic mood?

Quoted 6 lines
Quoted 2 lines

I think it's a minor matter given that this isn't a hot path, but we don't really need it for the !PREEMPT_RCU configuration.

Well if you make it unconditional, cond_resched() / rcu_all_qs() won't do its own rcu_momentary_qs(), because rcu_data.rcu_urgent_qs should
be false. So that essentially unify the behaviours for all configurations.

Ah, yes. That makes sense.

Ankur

Quoted 15 lines
Quoted 15 lines

Still, given that both of those clauses imply CONFIG_PREEMPTION, we can just simplify this to (with an appropriately adjusted comment):

kernel/trace/trace_osnoise.c +1 -1
@@ -1543,7 +1543,7 @@ static int run_osnoise(void)
                 * Note that in non PREEMPT_RCU kernels QSs are handled through
                 * cond_resched()
                 */
-               if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
+               if (IS_ENABLED(CONFIG_PREEMPTION)) {
                        if (!disable_irq)
                                local_irq_disable();
 
Quoted 2 lines
Quoted 2 lines

--
ankur

--
ankur

#32 lore raw

PATCH v2 6/6 sched: warn for high latency with TIF_NEED_RESCHED_LAZY

In reply to Ankur Arora

Add support for warning if the TIF_NEED_RESCHED_LAZY bit is set without rescheduling for more than the latency_warn_ms period.

Cc Ingo Molnar <[email protected]>
Cc Peter Ziljstra <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Vincent Guittot <[email protected]>
---
 kernel/sched/core.c  | 3 ++-
 kernel/sched/debug.c | 7 +++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
kernel/sched/core.c +2 -1
index 5c47d70f4204..077ea42a17f1 100644
@@ -5579,7 +5579,8 @@ static u64 cpu_resched_latency(struct rq *rq)
 	if (sysctl_resched_latency_warn_once && warned_once)
 		return 0;
 
-	if (!need_resched() || !latency_warn_ms)
+	if ((!need_resched() && !tif_test_bit(TIF_NEED_RESCHED_LAZY)) ||
+	    !latency_warn_ms)
 		return 0;
 
 	if (system_state == SYSTEM_BOOTING)
kernel/sched/debug.c +5 -2
index a48b2a701ec2..6c1a5305a1b3 100644
@@ -1293,9 +1293,12 @@ void proc_sched_set_task(struct task_struct *p)
 void resched_latency_warn(int cpu, u64 latency)
 {
 	static DEFINE_RATELIMIT_STATE(latency_check_ratelimit, 60 * 60 * HZ, 1);
+	char *nr;
+
+	nr = tif_need_resched() ? "need_resched" : "need_resched_lazy";
 
 	WARN(__ratelimit(&latency_check_ratelimit),
-	     "sched: CPU %d need_resched set for > %llu ns (%d ticks) "
+	     "sched: CPU %d %s set for > %llu ns (%d ticks) "
 	     "without schedule\n",
-	     cpu, latency, cpu_rq(cpu)->ticks_without_resched);
+	     cpu, nr, latency, cpu_rq(cpu)->ticks_without_resched);
 }
Signature · 1 line
2.43.5
#33 lore raw

Re: [PATCH v2 6/6] sched: warn for high latency with TIF_NEED_RESCHED_LAZY

Quoted 2 lines · Ankur Arora

Add support for warning if the TIF_NEED_RESCHED_LAZY bit is set without rescheduling for more than the latency_warn_ms period.

You fail to explain _why_ it is required to also check TIF_NEED_RESCHED_LAZY to not be set.

The problem with NEED_RESCHED set but no scheduling in 100ms is a long preempt-off or IRQ-off region .
The problem with NEED_RESCHED_LAZY set but no scheduling in 100ms is a missing timer tick in that time. Also the previous mentioned things might have happen.
And the code acting on NEED_RESCHED_LAZY is invoked before this check is.

Sebastian

#34 lore raw
In reply to Ankur Arora
Quoted 2 lines · Ankur Arora

This series adds RCU and some leftover scheduler bits for lazy preemption.

This is not critical for the current implementation. The way I understand is that you make a change in 3/6 and then all other patches in this series are required to deal with this.

For bisect reasons it would make sense to have 3/6 last in the series and to the "fixes" first before the code is enabled. I mean if you apply 3/6 first then you get build failures without 1/6. But with 3/6 before 5/6 you should get runtime errors, right?

Quoted 3 lines

The main problem addressed in the RCU related patches is that before PREEMPT_LAZY, PREEMPTION=y implied PREEMPT_RCU=y. With PREEMPT_LAZY, that's no longer true.

No, you want to make PREEMPTION=y + PREEMPT_RCU=n + PREEMPT_LAZY=y possible. This is different. Your wording makes it sound like there _is_ an actual problem.

Quoted 16 lines

That's because PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, and configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, with standalone PREEMPT_LAZY (much like PREEMPT_NONE, PREEMPT_VOLUNTARY) we want to use PREEMPT_RCU=n. And, when used in conjunction with PREEMPT_DYNAMIC, we continue to use PREEMPT_RCU=y.

Patches 1 and 2 are cleanup patches:
"rcu: fix header guard for rcu_all_qs()"
"rcu: rename PREEMPT_AUTO to PREEMPT_LAZY"

Patch 3, "rcu: limit PREEMPT_RCU configurations", explicitly limits PREEMPT_RCU=y to the PREEMPT_DYNAMIC or the latency oriented models.

Quoted 6 lines

Patches 4 and 5,
"rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y" "osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y"

handle quiescent states for the (PREEMPT_LAZY=y, PREEMPT_RCU=n) configuration.

I was briefly thinking about

kernel/sched/core.c +4 -1
@@ -5646,8 +5646,11 @@ void sched_tick(void)
 	hw_pressure = arch_scale_hw_pressure(cpu_of(rq));
 	update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure);
 
-	if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY))
+	if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) {
 		resched_curr(rq);
+		if (!IS_ENABLED(CONFIG_PREEMPT_RCU))
+			rcu_all_qs();
+	}
 
 	donor->sched_class->task_tick(rq, donor, 0);
 	if (sched_feat(LATENCY_WARN))

which should make #4+ #5 obsolete. But I think it is nicer to have the change in #4 since it extends the check to cover all cases. And then we would do it twice just for osnoise.

Sebastian

#35 lore raw
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Ankur Arora

This series adds RCU and some leftover scheduler bits for lazy preemption.

Quoted 8 lines

This is not critical for the current implementation. The way I understand is that you make a change in 3/6 and then all other patches in this series are required to deal with this.

For bisect reasons it would make sense to have 3/6 last in the series and to the "fixes" first before the code is enabled. I mean if you apply 3/6 first then you get build failures without 1/6. But with 3/6 before 5/6 you should get runtime errors, right?

That's a good point. Will reorder.

Quoted 7 lines
Quoted 3 lines

The main problem addressed in the RCU related patches is that before PREEMPT_LAZY, PREEMPTION=y implied PREEMPT_RCU=y. With PREEMPT_LAZY, that's no longer true.

No, you want to make PREEMPTION=y + PREEMPT_RCU=n + PREEMPT_LAZY=y possible. This is different. Your wording makes it sound like there _is_ an actual problem.

That's too literal a reading. It's just the problem ("matter or situation that is unwelcome" to quote from a dictionary) addressed in the patches.

Quoted 41 lines
Quoted 16 lines

That's because PREEMPT_RCU makes some trade-offs to optimize for latency as opposed to throughput, and configurations with limited preemption might prefer the stronger forward-progress guarantees of PREEMPT_RCU=n.

Accordingly, with standalone PREEMPT_LAZY (much like PREEMPT_NONE, PREEMPT_VOLUNTARY) we want to use PREEMPT_RCU=n. And, when used in conjunction with PREEMPT_DYNAMIC, we continue to use PREEMPT_RCU=y.

Patches 1 and 2 are cleanup patches:
"rcu: fix header guard for rcu_all_qs()"
"rcu: rename PREEMPT_AUTO to PREEMPT_LAZY"

Patch 3, "rcu: limit PREEMPT_RCU configurations", explicitly limits PREEMPT_RCU=y to the PREEMPT_DYNAMIC or the latency oriented models.

Quoted 6 lines

Patches 4 and 5,
"rcu: handle quiescent states for PREEMPT_RCU=n, PREEMPT_COUNT=y" "osnoise: handle quiescent states for PREEMPT_RCU=n, PREEMPTION=y"

handle quiescent states for the (PREEMPT_LAZY=y, PREEMPT_RCU=n) configuration.

I was briefly thinking about

kernel/sched/core.c +4 -1
@@ -5646,8 +5646,11 @@ void sched_tick(void)
 	hw_pressure = arch_scale_hw_pressure(cpu_of(rq));
 	update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure);
 
-	if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY))
+	if (dynamic_preempt_lazy() && tif_test_bit(TIF_NEED_RESCHED_LAZY)) {
 		resched_curr(rq);
+		if (!IS_ENABLED(CONFIG_PREEMPT_RCU))
+			rcu_all_qs();
+	}
 
 	donor->sched_class->task_tick(rq, donor, 0);
 	if (sched_feat(LATENCY_WARN))
Quoted 3 lines

which should make #4+ #5 obsolete. But I think it is nicer to have the change in #4 since it extends the check to cover all cases. And then we would do it twice just for osnoise.

Yeah, exactly. The check here only deals with this specific case while the one in rcu_flavor_sched_clock_irq() can handle that more generally.

Thanks.

--
ankur