Skip to contentSkip to conversation

sched_ext: Follow-up fixes for exit_cpu accuracy

PATCH Superseded Cheng-Yang Chou 2 participants 4 messages May 1

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover sched_ext: Follow-up fixes for exit_cpu accuracy

Follow-up of Tejun's suggestion [1]:

Patch 1: Normalize "on cpu N" -> "on CPU N" in the exit dump header to match UEI output style.

Patch 2: Fix exit_cpu accuracy for the hard lockup and RCU stall paths which were recording the detector CPU instead of the actual hung/stalled CPU. Touches kernel/rcu/tree_stall.h and kernel/rcu/tree_exp.h to thread the stalled CPU through panic_on_rcu_stall().

Based on sched_ext/for-7.2 (88588854c283).

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

Thanks,
Cheng-Yang

---

Cheng-Yang Chou (2):
sched_ext: Normalize exit dump header to "on CPU N"
sched_ext: Fix exit_cpu accuracy for lockup paths

 include/linux/sched/ext.h   |  4 ++--
 kernel/rcu/tree_exp.h       |  2 +-
 kernel/rcu/tree_stall.h     | 11 +++++++----
 kernel/sched/ext.c          | 15 ++++++++-------
 kernel/sched/ext_internal.h |  2 --
 5 files changed, 18 insertions(+), 16 deletions(-)

--
2.48.1

#1 lore raw

PATCH 1/2 sched_ext: Normalize exit dump header to "on CPU N"

Unify to uppercase to match the UEI output.

---
 kernel/sched/ext.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c +1 -1
index 45a2668284bc..48c65ac8e230 100644
@@ -6486,7 +6486,7 @@ static void scx_dump_state(struct scx_sched *sch, struct scx_exit_info *ei,
 		dump_line(&s, "Debug dump triggered by %s", ei->reason);
 	} else {
 		if (ei->exit_cpu >= 0)
-			dump_line(&s, "%s[%d] triggered exit kind %d on cpu %d:",
+			dump_line(&s, "%s[%d] triggered exit kind %d on CPU %d:",
 				  current->comm, current->pid, ei->kind,
 				  ei->exit_cpu);
 		else
Signature · 1 line
2.48.1
#2 lore raw

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

In reply to Cheng-Yang Chou

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() takes no CPU, recording the detector rather than the stalled one; add stalled_cpu to its signature and panic_on_rcu_stall(), threading it from print_cpu_stall() (current CPU) and print_other_cpu_stall() (first detected stalled CPU).

Add an exit_cpu parameter to handle_lockup() so callers pass the correct CPU directly. Remove the now-unused scx_verror() macro.

---
 include/linux/sched/ext.h   |  4 ++--
 kernel/rcu/tree_exp.h       |  2 +-
 kernel/rcu/tree_stall.h     | 11 +++++++----
 kernel/sched/ext.c          | 14 ++++++++------
 kernel/sched/ext_internal.h |  2 --
 5 files changed, 18 insertions(+), 15 deletions(-)
include/linux/sched/ext.h +2 -2
index d05efcac794d..16bbf24089ca 100644
@@ -259,7 +259,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(int stalled_cpu);
 
 #else	/* !CONFIG_SCHED_CLASS_EXT */
 
@@ -267,7 +267,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(int stalled_cpu) { return false; }
 
 #endif	/* CONFIG_SCHED_CLASS_EXT */
 
kernel/rcu/tree_exp.h +1 -1
index 82cada459e5d..2c9a99a9b68e 100644
@@ -675,7 +675,7 @@ static void synchronize_rcu_expedited_wait(void)
 
 		nbcon_cpu_emergency_exit();
 
-		panic_on_rcu_stall();
+		panic_on_rcu_stall(smp_processor_id());
 	}
 }
 
kernel/rcu/tree_stall.h +7 -4
index b67532cb8770..172ac08e673d 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(int stalled_cpu)
 {
 	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_cpu))
 		return;
 
 	if (++cpu_stall < sysctl_max_rcu_stall_to_panic)
@@ -631,6 +631,7 @@ 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;
+	int first_stalled_cpu = -1;
 	unsigned long flags;
 	unsigned long gpa;
 	unsigned long j;
@@ -660,6 +661,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 (first_stalled_cpu < 0)
+						first_stalled_cpu = cpu;
 					ndetected++;
 				}
 		}
@@ -701,7 +704,7 @@ 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(first_stalled_cpu);
 
 	rcu_force_quiescent_state();  /* Kick them all. */
 }
@@ -754,7 +757,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(smp_processor_id());
 
 	/*
 	 * Attempt to revive the RCU machinery by forcing a context switch.
kernel/sched/ext.c +8 -6
index 48c65ac8e230..8a0b1662a75a 100644
@@ -5069,6 +5069,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
@@ -5078,7 +5079,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;
@@ -5094,7 +5095,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:
@@ -5114,9 +5115,9 @@ static __printf(1, 2) bool handle_lockup(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(int stalled_cpu)
 {
-	return handle_lockup("RCU CPU stall detected!");
+	return handle_lockup(stalled_cpu, "RCU CPU stall detected!");
 }
 
 /**
@@ -5131,7 +5132,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",
@@ -5150,7 +5152,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 b4f5dd28855e..2d04f27cf7c5 100644
@@ -1486,8 +1486,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
#3 lore raw

Re: [PATCH 2/2] sched_ext: Fix exit_cpu accuracy for lockup paths

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Incorrect CPU attribution and preemption warning in synchronize_rcu_expedited_wait() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incorrect CPU attribution and preemption warning in synchronize_rcu_expedited_wait() --

commit 90ea438e747b72bc99a16a5708470577b00e6a1a
Author: Cheng-Yang Chou <[email protected]>

sched_ext: Fix exit_cpu accuracy for lockup paths

This patch adds an exit_cpu parameter to handle_lockup() and updates the lockup paths, including RCU stall detectors, to pass the actually stalled CPU rather than the detector CPU.

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

Quoted 9 lines
kernel/rcu/tree_exp.h +1 -1
@@ -675,7 +675,7 @@ static void synchronize_rcu_expedited_wait(void)
 
 		nbcon_cpu_emergency_exit();
 
-		panic_on_rcu_stall();
+		panic_on_rcu_stall(smp_processor_id());

Does this accurately record the stalled CPU for expedited grace periods?

The commit message notes the goal is to avoid recording the detector CPU rather than the stalled one. However, passing smp_processor_id() here appears to still record the CPU of the task waiting for the expedited grace period (the detector), rather than the actual stalled CPU.

Additionally, is it safe to call smp_processor_id() at this point?

Looking at synchronize_rcu_expedited_wait(), it sleeps via synchronize_rcu_expedited_wait_once(). The nbcon_cpu_emergency_exit() call immediately preceding this balances a prior nbcon_cpu_emergency_enter(), leaving preemption enabled:

    nbcon_cpu_emergency_exit();
    panic_on_rcu_stall(smp_processor_id());

Could this trigger a "BUG: using smp_processor_id() in preemptible" warning when CONFIG_DEBUG_PREEMPT is enabled? Would it be better to pass -1 or extract the actual stalled CPU instead?

[ ... ]

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