Skip to contentSkip to conversation

sched_ext: Follow-up fixes for exit_cpu accuracy

PATCH Superseded Cheng-Yang Chou 3 participants 8 messages May 18 – May 19

Subsystems (3)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover sched_ext: Follow-up fixes for exit_cpu accuracy

Follow-up of Tejun's suggestion [1], discussed in v2 and the subsequent review thread [2].

Patch 1: Fix exit_cpu accuracy for the hard lockup and softlockup paths, which were recording the detector CPU instead of the stalled one. For the RCU stall path, use -1 as a placeholder until patch 2 threads the cpumask.

Patch 2: Upgrade panic_on_rcu_stall() and scx_rcu_cpu_stall() to accept a cpumask of stalled CPUs. Both the non-expedited and expedited RCU stall paths collect all stalled CPUs and pass the full set to scx_rcu_cpu_stall(), which uses cpumask_first() for exit_cpu and includes the full CPU list in the exit message.

Based on sched_ext/for-next (d83f90561532).

Changes in v3:
- Drop patch 1 of v2 ("Normalize exit dump header to 'on CPU N'"), already applied to sched_ext/for-7.2.
- Replace single stalled_cpu int with const struct cpumask * in panic_on_rcu_stall() and scx_rcu_cpu_stall() (Paul McKenney, Tejun Heo).
- Use cpumask_of(smp_processor_id()) in print_cpu_stall().
- Build cpumask of all stalled CPUs in print_other_cpu_stall().
- Thread cpumask through synchronize_rcu_expedited_stall() and synchronize_rcu_expedited_wait() (Tejun Heo).
- Use cpumask_first() for exit_cpu and include the full CPU list in the exit message (Tejun Heo).
- Pass %*pbl directly to handle_lockup() to avoid intermediate buffer truncation.
- Falls back to cpu_none_mask on allocation failure.
- Link to v2:
https://lore.kernel.org/r/[email protected]/

Changes in v2:
- Use raw_smp_processor_id() in synchronize_rcu_expedited_wait() to avoid CONFIG_DEBUG_PREEMPT splat (preemption is enabled there after nbcon_cpu_emergency_exit()).
- Link to v1:
https://lore.kernel.org/r/[email protected]/

[1]: https://lore.kernel.org/r/[email protected]/
[2]: https://lore.kernel.org/r/[email protected]/

Thanks,
Cheng-Yang

---

Cheng-Yang Chou (2):
sched_ext: Fix exit_cpu accuracy for lockup paths sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled
CPUs

 include/linux/sched/ext.h   |  4 ++--
 kernel/rcu/tree_exp.h       | 14 +++++++++++---
 kernel/rcu/tree_stall.h     | 15 +++++++++++----
 kernel/sched/ext.c          | 17 +++++++++++------
 kernel/sched/ext_internal.h |  2 --
 5 files changed, 35 insertions(+), 17 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/2 sched_ext: Fix exit_cpu accuracy for lockup paths

handle_lockup() uses raw_smp_processor_id() for exit_cpu, which is wrong for two paths:

- scx_hardlockup_irq_workfn() has the hung CPU in a local variable but irq_work may run elsewhere. Pass the local cpu explicitly.
- scx_rcu_cpu_stall() records the detector CPU rather than the stalled one. Pass -1 for now. The next patch fixes this properly.

---
 kernel/sched/ext.c          | 12 +++++++-----
 kernel/sched/ext_internal.h |  2 --
 2 files changed, 7 insertions(+), 7 deletions(-)
kernel/sched/ext.c +7 -5
index 9c458552d14f..22b1356aa4eb 100644
@@ -5141,6 +5141,7 @@ bool scx_allow_ttwu_queue(const struct task_struct *p)
 
 /**
  * handle_lockup - sched_ext common lockup handler
+ * @exit_cpu: CPU to record in exit_info. Pass the stalled/hung CPU, not current.
  * @fmt: format string
  *
  * Called on system stall or lockup condition and initiates abort of sched_ext
@@ -5150,7 +5151,7 @@ bool scx_allow_ttwu_queue(const struct task_struct *p)
  * resolve the lockup. %false if sched_ext is not enabled or abort was already
  * initiated by someone else.
  */
-static __printf(1, 2) bool handle_lockup(const char *fmt, ...)
+static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...)
 {
 	struct scx_sched *sch;
 	va_list args;
@@ -5166,7 +5167,7 @@ static __printf(1, 2) bool handle_lockup(const char *fmt, ...)
 	case SCX_ENABLING:
 	case SCX_ENABLED:
 		va_start(args, fmt);
-		ret = scx_verror(sch, fmt, args);
+		ret = scx_vexit(sch, SCX_EXIT_ERROR, 0, exit_cpu, fmt, args);
 		va_end(args);
 		return ret;
 	default:
@@ -5188,7 +5189,7 @@ static __printf(1, 2) bool handle_lockup(const char *fmt, ...)
  */
 bool scx_rcu_cpu_stall(void)
 {
-	return handle_lockup("RCU CPU stall detected!");
+	return handle_lockup(-1, "RCU CPU stall detected!");
 }
 
 /**
@@ -5203,7 +5204,8 @@ bool scx_rcu_cpu_stall(void)
  */
 void scx_softlockup(u32 dur_s)
 {
-	if (!handle_lockup("soft lockup - CPU %d stuck for %us", smp_processor_id(), dur_s))
+	if (!handle_lockup(smp_processor_id(), "soft lockup - CPU %d stuck for %us",
+			   smp_processor_id(), dur_s))
 		return;
 
 	printk_deferred(KERN_ERR "sched_ext: Soft lockup - CPU %d stuck for %us, disabling BPF scheduler\n",
@@ -5222,7 +5224,7 @@ static void scx_hardlockup_irq_workfn(struct irq_work *work)
 {
 	int cpu = atomic_xchg(&scx_hardlockup_cpu, -1);
 
-	if (cpu >= 0 && handle_lockup("hard lockup - CPU %d", cpu))
+	if (cpu >= 0 && handle_lockup(cpu, "hard lockup - CPU %d", cpu))
 		printk_deferred(KERN_ERR "sched_ext: Hard lockup - CPU %d, disabling BPF scheduler\n",
 				cpu);
 }
kernel/sched/ext_internal.h +0 -2
index 7258aea94b9f..3ccfea06cba0 100644
@@ -1490,8 +1490,6 @@ __printf(5, 6) bool __scx_exit(struct scx_sched *sch, enum scx_exit_kind kind,
 	__scx_exit(sch, kind, exit_code, raw_smp_processor_id(), fmt, ##args)
 #define scx_error(sch, fmt, args...)						\
 	scx_exit((sch), SCX_EXIT_ERROR, 0, fmt, ##args)
-#define scx_verror(sch, fmt, args)						\
-	scx_vexit((sch), SCX_EXIT_ERROR, 0, raw_smp_processor_id(), fmt, args)
 
 /*
  * Return the rq currently locked from an scx callback, or NULL if no rq is
Signature · 1 line
2.48.1
#2 lore raw

PATCH v3 2/2 sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

In reply to Cheng-Yang Chou

scx_rcu_cpu_stall() previously recorded the detector CPU rather than the stalled one, and the expedited grace period path had no stalled CPU to report at all.

Thread a cpumask through panic_on_rcu_stall() and scx_rcu_cpu_stall() to capture all stalled CPUs. Report cpumask_first() as exit_cpu and the full CPU list in the exit message. Task-only stalls yield exit_cpu = -1. Falls back to cpu_none_mask on allocation failure.

---
 include/linux/sched/ext.h |  4 ++--
 kernel/rcu/tree_exp.h     | 14 +++++++++++---
 kernel/rcu/tree_stall.h   | 15 +++++++++++----
 kernel/sched/ext.c        |  7 +++++--
 4 files changed, 29 insertions(+), 11 deletions(-)
include/linux/sched/ext.h +2 -2
index 20b2343aa344..75cb8b119fb7 100644
@@ -263,7 +263,7 @@ void sched_ext_dead(struct task_struct *p);
 void print_scx_info(const char *log_lvl, struct task_struct *p);
 void scx_softlockup(u32 dur_s);
 bool scx_hardlockup(int cpu);
-bool scx_rcu_cpu_stall(void);
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask);
 
 #else	/* !CONFIG_SCHED_CLASS_EXT */
 
@@ -271,7 +271,7 @@ static inline void sched_ext_dead(struct task_struct *p) {}
 static inline void print_scx_info(const char *log_lvl, struct task_struct *p) {}
 static inline void scx_softlockup(u32 dur_s) {}
 static inline bool scx_hardlockup(int cpu) { return false; }
-static inline bool scx_rcu_cpu_stall(void) { return false; }
+static inline bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask) { return false; }
 
 #endif	/* CONFIG_SCHED_CLASS_EXT */
 
kernel/rcu/tree_exp.h +11 -3
index 82cada459e5d..6199ca3cfb7d 100644
@@ -555,7 +555,8 @@ static bool synchronize_rcu_expedited_wait_once(long tlimit)
 /*
  * Print out an expedited RCU CPU stall warning message.
  */
-static void synchronize_rcu_expedited_stall(unsigned long jiffies_start, unsigned long j)
+static void synchronize_rcu_expedited_stall(unsigned long jiffies_start, unsigned long j,
+					    struct cpumask *stalled_mask)
 {
 	int cpu;
 	unsigned long mask;
@@ -578,6 +579,8 @@ static void synchronize_rcu_expedited_stall(unsigned long jiffies_start, unsigne
 			if (!(READ_ONCE(rnp->expmask) & mask))
 				continue;
 			ndetected++;
+			if (stalled_mask)
+				cpumask_set_cpu(cpu, stalled_mask);
 			rdp = per_cpu_ptr(&rcu_data, cpu);
 			pr_cont(" %d-%c%c%c%c", cpu,
 				"O."[!!cpu_online(cpu)],
@@ -659,23 +662,28 @@ static void synchronize_rcu_expedited_wait(void)
 
 	for (;;) {
 		unsigned long j;
+		cpumask_var_t stalled_mask;
+		bool have_mask;
 
 		if (synchronize_rcu_expedited_wait_once(jiffies_stall))
 			return;
 		if (rcu_stall_is_suppressed())
 			continue;
 
+		have_mask = zalloc_cpumask_var(&stalled_mask, GFP_KERNEL);
+
 		nbcon_cpu_emergency_enter();
 
 		j = jiffies;
 		rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_EXP, (void *)(j - jiffies_start));
 		trace_rcu_stall_warning(rcu_state.name, TPS("ExpeditedStall"));
-		synchronize_rcu_expedited_stall(jiffies_start, j);
+		synchronize_rcu_expedited_stall(jiffies_start, j, have_mask ? stalled_mask : NULL);
 		jiffies_stall = 3 * rcu_exp_jiffies_till_stall_check() + 3;
 
 		nbcon_cpu_emergency_exit();
 
-		panic_on_rcu_stall();
+		panic_on_rcu_stall(have_mask ? stalled_mask : cpu_none_mask);
+		free_cpumask_var(stalled_mask);
 	}
 }
 
kernel/rcu/tree_stall.h +11 -4
index b67532cb8770..b25609520418 100644
@@ -159,7 +159,7 @@ static int __init check_cpu_stall_init(void)
 early_initcall(check_cpu_stall_init);
 
 /* If so specified via sysctl, panic, yielding cleaner stall-warning output. */
-static void panic_on_rcu_stall(void)
+static void panic_on_rcu_stall(const struct cpumask *stalled_mask)
 {
 	static int cpu_stall;
 
@@ -167,7 +167,7 @@ static void panic_on_rcu_stall(void)
 	 * Attempt to kick out the BPF scheduler if it's installed and defer
 	 * the panic to give the system a chance to recover.
 	 */
-	if (scx_rcu_cpu_stall())
+	if (scx_rcu_cpu_stall(stalled_mask))
 		return;
 
 	if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
@@ -631,6 +631,8 @@ static void rcu_check_gp_kthread_expired_fqs_timer(void)
 static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
 {
 	int cpu;
+	cpumask_var_t stalled_mask;
+	bool have_mask;
 	unsigned long flags;
 	unsigned long gpa;
 	unsigned long j;
@@ -645,6 +647,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
 	if (rcu_stall_is_suppressed())
 		return;
 
+	have_mask = zalloc_cpumask_var(&stalled_mask, GFP_ATOMIC);
+
 	nbcon_cpu_emergency_enter();
 
 	/*
@@ -660,6 +664,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
 			for_each_leaf_node_possible_cpu(rnp, cpu)
 				if (rnp->qsmask & leaf_node_cpu_bit(rnp, cpu)) {
 					print_cpu_stall_info(cpu);
+					if (have_mask)
+						cpumask_set_cpu(cpu, stalled_mask);
 					ndetected++;
 				}
 		}
@@ -701,7 +707,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
 
 	nbcon_cpu_emergency_exit();
 
-	panic_on_rcu_stall();
+	panic_on_rcu_stall(have_mask ? stalled_mask : cpu_none_mask);
+	free_cpumask_var(stalled_mask);
 
 	rcu_force_quiescent_state();  /* Kick them all. */
 }
@@ -754,7 +761,7 @@ static void print_cpu_stall(unsigned long gp_seq, unsigned long gps)
 
 	nbcon_cpu_emergency_exit();
 
-	panic_on_rcu_stall();
+	panic_on_rcu_stall(cpumask_of(smp_processor_id()));
 
 	/*
 	 * Attempt to revive the RCU machinery by forcing a context switch.
kernel/sched/ext.c +5 -2
index 22b1356aa4eb..a2ef2eae7e91 100644
@@ -5187,9 +5187,12 @@ static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...)
  * resolve the reported RCU stall. %false if sched_ext is not enabled or someone
  * else already initiated abort.
  */
-bool scx_rcu_cpu_stall(void)
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask)
 {
-	return handle_lockup(-1, "RCU CPU stall detected!");
+	int exit_cpu = cpumask_empty(stalled_mask) ? -1 : (int)cpumask_first(stalled_mask);
+
+	return handle_lockup(exit_cpu, "RCU CPU stall on CPUs (%*pbl)",
+			     cpumask_pr_args(stalled_mask));
 }
 
 /**
Signature · 1 line
2.48.1
#3 lore raw

Re: [PATCH v3 2/2] sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Dynamic memory allocation in RCU stall reporting paths introduces deadlocks and degrades…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Dynamic memory allocation in RCU stall reporting paths introduces deadlocks and degrades diagnostic reliability. --

commit 97fc58777bb71d599b2d82e880aa479bdea14d1d
Author: Cheng-Yang Chou <[email protected]>
Subject: sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

This commit updates the RCU stall reporting paths to thread a cpumask through panic_on_rcu_stall() and scx_rcu_cpu_stall() to capture and report all stalled CPUs. It dynamically allocates the mask using zalloc_cpumask_var() with GFP_KERNEL in the expedited path and GFP_ATOMIC in the normal stall path.

Quoted 4 lines
kernel/rcu/tree_exp.h +0 -0
index 82cada459e5d0..6199ca3cfb7d7 100644

[ ... ]

kernel/rcu/tree_exp.h static void synchronize_rcu_expedited_wait(void) · line 673

Quoted 13 lines
kernel/rcu/tree_exp.h +3 -0
@@ -659,23 +662,28 @@ static void synchronize_rcu_expedited_wait(void)
 
 	for (;;) {
 		unsigned long j;
+		cpumask_var_t stalled_mask;
+		bool have_mask;
 
 		if (synchronize_rcu_expedited_wait_once(jiffies_stall))
 			return;
 		if (rcu_stall_is_suppressed())
 			continue;
 
+		have_mask = zalloc_cpumask_var(&stalled_mask, GFP_KERNEL);

Since callers of synchronize_rcu_expedited() might hold arbitrary sleepable locks, can this GFP_KERNEL allocation cause an AB-BA deadlock if the system enters direct memory reclaim and attempts to acquire those same locks?

Additionally, if the allocation fails due to memory exhaustion (which is a common cause of RCU stalls), this falls back to cpu_none_mask. This drops the stalled CPU list exactly when diagnostic information is most needed.

Would a pre-allocated static mask be safer for these critical diagnostic paths?

[ ... ]

Quoted 4 lines
kernel/rcu/tree_stall.h +0 -0
index b67532cb87705..b256095204184 100644

[ ... ]

kernel/rcu/tree_stall.h static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps) · line 650

Quoted 5 lines
kernel/rcu/tree_stall.h +1 -0
@@ -645,6 +647,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)
 	if (rcu_stall_is_suppressed())
 		return;
 
+	have_mask = zalloc_cpumask_var(&stalled_mask, GFP_ATOMIC);

When CONFIG_CPUMASK_OFFSTACK=y is enabled, this dynamically allocates memory.

If the stalled CPU is stuck holding a memory allocator lock like a slab node lock, will the detector CPU spin indefinitely trying to acquire that same lock, entirely suppressing the stall warning and deadlocking the system?

Also, if this GFP_ATOMIC allocation fails, it emits a page allocation failure warning before nbcon_cpu_emergency_enter() is called.

If the stalled CPU happens to hold the console lock, could this standard printk path deadlock the reporting CPU?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
reviewer
#4 lore raw

Re: [PATCH v3 2/2] sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

In reply to Cheng-Yang Chou

kernel/sched/ext.c static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...) · line 5195

Quoted 7 lines · Cheng-Yang Chou
kernel/sched/ext.c +5 -1
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask)
 {
-	return handle_lockup(-1, "RCU CPU stall detected!");
+	int exit_cpu = cpumask_empty(stalled_mask) ? -1 : (int)cpumask_first(stalled_mask);
+
+	return handle_lockup(exit_cpu, "RCU CPU stall on CPUs (%*pbl)",
+			     cpumask_pr_args(stalled_mask));

Wouldn't you want to pipe this down so that you can prioritize all stalled CPUs when dumping?

Thanks.

Signature · 1 line
tejun
#5 lore raw

Re: [PATCH v3 2/2] sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

Hi Tejun,

Quoted 11 lines · Tejun Heo

kernel/sched/ext.c static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...) · line 5195

Quoted 7 lines · Cheng-Yang Chou
kernel/sched/ext.c +5 -1
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask)
 {
-	return handle_lockup(-1, "RCU CPU stall detected!");
+	int exit_cpu = cpumask_empty(stalled_mask) ? -1 : (int)cpumask_first(stalled_mask);
+
+	return handle_lockup(exit_cpu, "RCU CPU stall on CPUs (%*pbl)",
+			     cpumask_pr_args(stalled_mask));

Wouldn't you want to pipe this down so that you can prioritize all stalled CPUs when dumping?

Ahh yes, will add stall_cpus to scx_exit_info, store the full mask there, and update scx_dump_state() to dump all stalled CPUs first.

Thanks.

Signature · 2 lines
Cheers,
Cheng-Yang
reviewer
#6 lore raw

Re: [PATCH v3 2/2] sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

Quoted 15 lines · Cheng-Yang Chou

Hi Tejun,

Quoted 11 lines · Tejun Heo

kernel/sched/ext.c static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...) · line 5195

Quoted 7 lines · Cheng-Yang Chou
kernel/sched/ext.c +5 -1
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask)
 {
-	return handle_lockup(-1, "RCU CPU stall detected!");
+	int exit_cpu = cpumask_empty(stalled_mask) ? -1 : (int)cpumask_first(stalled_mask);
+
+	return handle_lockup(exit_cpu, "RCU CPU stall on CPUs (%*pbl)",
+			     cpumask_pr_args(stalled_mask));

Wouldn't you want to pipe this down so that you can prioritize all stalled CPUs when dumping?

Quoted 2 lines

Ahh yes, will add stall_cpus to scx_exit_info, store the full mask there, and update scx_dump_state() to dump all stalled CPUs first.

I'm not sure I'd add it to scx_exit_info as that's public interface. Just pipe it to dump?

Thanks.

Signature · 1 line
tejun
#7 lore raw

Re: [PATCH v3 2/2] sched_ext, rcu: Upgrade RCU stall paths to report cpumask of stalled CPUs

Hi Tejun,

Quoted 16 lines · Tejun Heo
Quoted 15 lines · Cheng-Yang Chou

Hi Tejun,

Quoted 11 lines · Tejun Heo

kernel/sched/ext.c static __printf(2, 3) bool handle_lockup(int exit_cpu, const char *fmt, ...) · line 5195

Quoted 7 lines · Cheng-Yang Chou
kernel/sched/ext.c +5 -1
+bool scx_rcu_cpu_stall(const struct cpumask *stalled_mask)
 {
-	return handle_lockup(-1, "RCU CPU stall detected!");
+	int exit_cpu = cpumask_empty(stalled_mask) ? -1 : (int)cpumask_first(stalled_mask);
+
+	return handle_lockup(exit_cpu, "RCU CPU stall on CPUs (%*pbl)",
+			     cpumask_pr_args(stalled_mask));

Wouldn't you want to pipe this down so that you can prioritize all stalled CPUs when dumping?

Quoted 6 lines
Quoted 2 lines

Ahh yes, will add stall_cpus to scx_exit_info, store the full mask there, and update scx_dump_state() to dump all stalled CPUs first.

I'm not sure I'd add it to scx_exit_info as that's public interface. Just pipe it to dump?

Ok, just pipe it to dump, will send a v4 patch soon. Thanks.

Signature · 2 lines
Cheers,
Cheng-Yang