Skip to contentSkip to conversation

sched/clock: fix early boot splat on clock transition to unstable

PATCH In review Paul Gortmaker 3 participants 6 messages Oct 3, 2017 – Oct 17, 2017 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/clock: fix early boot splat on clock transition to unstable

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

  [  2.487904] BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1
  [  2.487909] caller is debug_smp_processor_id+0x17/0x20
  [  2.487911] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.11.12-rt14-00451
  [  2.487911] Hardware name: Dell Inc. OptiPlex 760                 /0M858N, BIOS A16 08/06/2013
  [  2.487912] Call Trace:
  [  2.487918]  dump_stack+0x4f/0x6a
  [  2.487919]  check_preemption_disabled+0xda/0xe0
  [  2.487921]  debug_smp_processor_id+0x17/0x20
  [  2.487924]  clear_sched_clock_stable+0x28/0x80
  [  2.487927]  mark_tsc_unstable+0x22/0x70
  [  2.487930]  acpi_processor_get_power_info+0x3e3/0x6a0
  [  2.487932]  acpi_processor_power_init+0x3a/0x1d0
  [  2.487933]  __acpi_processor_start+0x162/0x1b0
               ....
  [  2.487950]  acpi_processor_driver_init+0x20/0x96
  [  2.487951]  do_one_initcall+0x3f/0x170
  [  2.487954]  kernel_init_freeable+0x18e/0x216
  [  2.487955]  ? rest_init+0xd0/0xd0
  [  2.487956]  kernel_init+0x9/0x100
  [  2.487958]  ret_from_fork+0x22/0x30
  [  2.487960] sched_clock: Marking unstable (2488005383, -223143)<-(2590866395, -103084155)
  [  2.488004] tsc: Marking TSC unstable due to TSC halts in idle

(gdb) list *clear_sched_clock_stable+0x28
0xffffffff8108bbb8 is in clear_sched_clock_stable (kernel/sched/clock.c:114).

[...]

112 static inline struct sched_clock_data *this_scd(void)
113 {
114 return this_cpu_ptr(&sched_clock_data);
115 }

We now get this_scd with preemption disabled. I also decided to pass in the scd to __clear_sched_clock_stable in the hope it made it more clear that the caller (currently only one) needs to get this_scd with preemption disabled, even though that wasn't strictly required.

kernel/sched/clock.c +9 -4
index 11ad4bd995e2..32dcda23c616 100644
@@ -155,9 +155,8 @@ static void __sched_clock_work(struct work_struct *work)
 
 static DECLARE_WORK(sched_clock_work, __sched_clock_work);
 
-static void __clear_sched_clock_stable(void)
+static void __clear_sched_clock_stable(struct sched_clock_data *scd)
 {
-	struct sched_clock_data *scd = this_scd();
 
 	/*
 	 * Attempt to make the stable->unstable transition continuous.
@@ -186,8 +185,14 @@ void clear_sched_clock_stable(void)
 
 	smp_mb(); /* matches sched_clock_init_late() */
 
-	if (sched_clock_running == 2)
-		__clear_sched_clock_stable();
+	if (sched_clock_running == 2) {
+		struct sched_clock_data *scd;
+
+		preempt_disable();
+		scd = this_scd();
+		preempt_enable();
+		__clear_sched_clock_stable(scd);
+	}
 }
 
 void sched_clock_init_late(void)
Signature · 1 line
2.1.4
#1 lore raw
Quoted 61 lines · Paul Gortmaker

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

  [  2.487904] BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1
  [  2.487909] caller is debug_smp_processor_id+0x17/0x20
  [  2.487911] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.11.12-rt14-00451
  [  2.487911] Hardware name: Dell Inc. OptiPlex 760                 /0M858N, BIOS A16 08/06/2013
  [  2.487912] Call Trace:
  [  2.487918]  dump_stack+0x4f/0x6a
  [  2.487919]  check_preemption_disabled+0xda/0xe0
  [  2.487921]  debug_smp_processor_id+0x17/0x20
  [  2.487924]  clear_sched_clock_stable+0x28/0x80
  [  2.487927]  mark_tsc_unstable+0x22/0x70
  [  2.487930]  acpi_processor_get_power_info+0x3e3/0x6a0
  [  2.487932]  acpi_processor_power_init+0x3a/0x1d0
  [  2.487933]  __acpi_processor_start+0x162/0x1b0
               ....
  [  2.487950]  acpi_processor_driver_init+0x20/0x96
  [  2.487951]  do_one_initcall+0x3f/0x170
  [  2.487954]  kernel_init_freeable+0x18e/0x216
  [  2.487955]  ? rest_init+0xd0/0xd0
  [  2.487956]  kernel_init+0x9/0x100
  [  2.487958]  ret_from_fork+0x22/0x30
  [  2.487960] sched_clock: Marking unstable (2488005383, -223143)<-(2590866395, -103084155)
  [  2.488004] tsc: Marking TSC unstable due to TSC halts in idle

(gdb) list *clear_sched_clock_stable+0x28
0xffffffff8108bbb8 is in clear_sched_clock_stable (kernel/sched/clock.c:114).

[...]

112 static inline struct sched_clock_data *this_scd(void)
113 {
114 return this_cpu_ptr(&sched_clock_data);
115 }

We now get this_scd with preemption disabled. I also decided to pass in the scd to __clear_sched_clock_stable in the hope it made it more clear that the caller (currently only one) needs to get this_scd with preemption disabled, even though that wasn't strictly required.

kernel/sched/clock.c +1 -2
index 11ad4bd995e2..32dcda23c616 100644
@@ -155,9 +155,8 @@ static void __sched_clock_work(struct work_struct *work)
 
 static DECLARE_WORK(sched_clock_work, __sched_clock_work);
 
-static void __clear_sched_clock_stable(void)
+static void __clear_sched_clock_stable(struct sched_clock_data *scd)
 {
-	struct sched_clock_data *scd = this_scd();
 
 	/*
 	 * Attempt to make the stable->unstable transition continuous.
@@ -186,8 +185,14 @@ void clear_sched_clock_stable(void)
 

kernel/sched/clock.c void clear_sched_clock_stable(void) · line 193

Quoted 10 lines
kernel/sched/clock.c +6 -2
 	smp_mb(); /* matches sched_clock_init_late() */
 
-	if (sched_clock_running == 2)
-		__clear_sched_clock_stable();
+	if (sched_clock_running == 2) {
+		struct sched_clock_data *scd;
+
+		preempt_disable();
+		scd = this_scd();
+		preempt_enable();

It would seem to me that this doesn't move it out far enough.

Should it not be the callers responsibility to ensure that we're in a non-migratable context? Otherwise it's possible that we end up adjusting the percpu sched_clock_data for a CPU other than what was intended (because we could be migrated between clear_sched_clock_stable() entry and this preempt_disable()). Maybe this is not a problem, I don't know.

It does make we wonder whether or not we should reduce the this_cpu_* access preemption checks to migration-disabled checks. Or, alternatively, go the route of get_cpu()/get_cpu_light() and add new accessors with the reduced check.

Julia

Acked-by Thomas Gleixner on v1 1/1
#2 lore raw
In reply to Paul Gortmaker
Quoted 3 lines · Paul Gortmaker

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

Would that work for you?

Quoted 1 line

From 45aea321678856687927c53972321ebfab77759a Mon Sep 17 00:00:00 2001

From: Peter Zijlstra <[email protected]>
Date: Wed, 24 May 2017 08:52:02 +0200
Subject: [PATCH] sched/clock: Fix early boot preempt assumption in
__set_sched_clock_stable()

The more strict early boot preemption warnings found that __set_sched_clock_stable() was incorrectly assuming we'd still be running on a single CPU:

  BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1
  caller is debug_smp_processor_id+0x1c/0x1e
  CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.12.0-rc2-00108-g1c3c5ea #1
  Call Trace:
   dump_stack+0x110/0x192
   check_preemption_disabled+0x10c/0x128
   ? set_debug_rodata+0x25/0x25
   debug_smp_processor_id+0x1c/0x1e
   sched_clock_init_late+0x27/0x87
  [...]

Fix it by disabling IRQs.

---
 kernel/sched/clock.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/sched/clock.c +8 -1
index 1a0d389d2f2b..ca0f8fc945c6 100644
@@ -133,12 +133,19 @@ static void __scd_stamp(struct sched_clock_data *scd)
 
 static void __set_sched_clock_stable(void)
 {
-	struct sched_clock_data *scd = this_scd();
+	struct sched_clock_data *scd;
 
+	/*
+	 * Since we're still unstable and the tick is already running, we have
+	 * to disable IRQs in order to get a consistent scd->tick* reading.
+	 */
+	local_irq_disable();
+	scd = this_scd();
 	/*
 	 * Attempt to make the (initial) unstable->stable transition continuous.
 	 */
 	__sched_clock_offset = (scd->tick_gtod + __gtod_offset) - (scd->tick_raw);
+	local_irq_enable();
 
 	printk(KERN_INFO "sched_clock: Marking stable (%lld, %lld)->(%lld, %lld)\n",
 			scd->tick_gtod, __gtod_offset,
Signature · 1 line
2.14.2
#3 lore raw
Quoted 6 lines · To Paul Gortmaker
Quoted 3 lines · Paul Gortmaker

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

Would that work for you?

please excuse the nonsense. Now:

From: Peter Zijlstra <[email protected]>
Date: Fri, 21 Apr 2017 12:11:53 +0200
Subject: [PATCH] sched/clock: Initialize all per-CPU state before switching
(back) to unstable

In preparation for not keeping the sched_clock_tick() active for stable TSC, we need to explicitly initialize all per-CPU state before switching back to unstable.

Note: this patch looses the __gtod_offset calculation; it will be restored in the next one.

Cc Linus Torvalds <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/sched/clock.c |   60 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 21 deletions(-)
kernel/sched/clock.c +39 -21
@@ -124,6 +124,12 @@ int sched_clock_stable(void)
 	return static_branch_likely(&__sched_clock_stable);
 }
 
+static void __scd_stamp(struct sched_clock_data *scd)
+{
+	scd->tick_gtod = ktime_get_ns();
+	scd->tick_raw = sched_clock();
+}
+
 static void __set_sched_clock_stable(void)
 {
 	struct sched_clock_data *scd = this_scd();
@@ -141,8 +147,37 @@ static void __set_sched_clock_stable(voi
 	tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
 }
 
+/*
+ * If we ever get here, we're screwed, because we found out -- typically after
+ * the fact -- that TSC wasn't good. This means all our clocksources (including
+ * ktime) could have reported wrong values.
+ *
+ * What we do here is an attempt to fix up and continue sort of where we left
+ * off in a coherent manner.
+ *
+ * The only way to fully avoid random clock jumps is to boot with:
+ * "tsc=unstable".
+ */
 static void __sched_clock_work(struct work_struct *work)
 {
+	struct sched_clock_data *scd;
+	int cpu;
+
+	/* take a current timestamp and set 'now' */
+	preempt_disable();
+	scd = this_scd();
+	__scd_stamp(scd);
+	scd->clock = scd->tick_gtod + __gtod_offset;
+	preempt_enable();
+
+	/* clone to all CPUs */
+	for_each_possible_cpu(cpu)
+		per_cpu(sched_clock_data, cpu) = *scd;
+
+	printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
+			scd->tick_gtod, __gtod_offset,
+			scd->tick_raw,  __sched_clock_offset);
+
 	static_branch_disable(&__sched_clock_stable);
 }
 
@@ -150,27 +185,11 @@ static DECLARE_WORK(sched_clock_work, __
 
 static void __clear_sched_clock_stable(void)
 {
-	struct sched_clock_data *scd = this_scd();
-
-	/*
-	 * Attempt to make the stable->unstable transition continuous.
-	 *
-	 * Trouble is, this is typically called from the TSC watchdog
-	 * timer, which is late per definition. This means the tick
-	 * values can already be screwy.
-	 *
-	 * Still do what we can.
-	 */
-	__gtod_offset = (scd->tick_raw + __sched_clock_offset) - (scd->tick_gtod);
-
-	printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
-			scd->tick_gtod, __gtod_offset,
-			scd->tick_raw,  __sched_clock_offset);
+	if (!sched_clock_stable())
+		return;
 
 	tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
-
-	if (sched_clock_stable())
-		schedule_work(&sched_clock_work);
+	schedule_work(&sched_clock_work);
 }
 
 void clear_sched_clock_stable(void)
@@ -357,8 +376,7 @@ void sched_clock_tick(void)
 	 * XXX arguably we can skip this if we expose tsc_clocksource_reliable
 	 */
 	scd = this_scd();
-	scd->tick_raw  = sched_clock();
-	scd->tick_gtod = ktime_get_ns();
+	__scd_stamp(scd);
 
 	if (!sched_clock_stable() && likely(sched_clock_running))
 		sched_clock_local(scd);

Sebastian

#4 lore raw
Quoted 8 lines · [Re: [PATCH rt-v4.11] sched/clock: fix early boot splat on clock transition to…
Quoted 6 lines · To Paul Gortmaker
Quoted 3 lines · Paul Gortmaker

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

Would that work for you?

please excuse the nonsense. Now:

No problem. I had to look to see we already had that patch in order to realize why you called it nonsense. :)

Quoted 6 lines

From: Peter Zijlstra <[email protected]>
Date: Fri, 21 Apr 2017 12:11:53 +0200
Subject: [PATCH] sched/clock: Initialize all per-CPU state before switching
(back) to unstable

Quoted 6 lines

In preparation for not keeping the sched_clock_tick() active for stable TSC, we need to explicitly initialize all per-CPU state before switching back to unstable.

Note: this patch looses the __gtod_offset calculation; it will be restored in the next one.

Since the commit log mentions taking the next one, I used this (mainline cf15ca8deda8) and "x86/tsc, sched/clock, clocksource: Use clocksource watchdog to provide stable sync points" (which is mainline b421b22b00b00)

The tree patched up from scratch without any need to refresh the two newly exported commits and the old trash box with the broken tsc doesn't have the boot splat. Note: I've not [yet] tested on something with a working tsc, but I doubt that is a concern...

Patch to linux-4.11.y-rt-patches branch follows. Note that the two newly added commits are in the series file right above:

0001-sched-clock-Fix-early-boot-preempt-assumption-in-__s.patch

since that is where they belong chronologically.

Paul.
--

Quoted 1 line

From e2e84968995428c6472046793c763884971c58ec Mon Sep 17 00:00:00 2001

From: Paul Gortmaker <[email protected]>
Date: Wed, 11 Oct 2017 08:47:36 -0400
Subject: [PATCH 4.11-rt] sched/tsc: add two mainline commits to fix boot splat

On an older machine with a Pentium(R) Dual-Core E5300 I see the the following early (see time stamps) boot splat on clock transition due to TSC unstable (indicated in the last line):

  [  2.487904] BUG: using smp_processor_id() in preemptible [00000000] code: swapper/0/1
  [  2.487909] caller is debug_smp_processor_id+0x17/0x20
  [  2.487911] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.11.12-rt14-00451
  [  2.487911] Hardware name: Dell Inc. OptiPlex 760                 /0M858N, BIOS A16 08/06/2013
  [  2.487912] Call Trace:
  [  2.487918]  dump_stack+0x4f/0x6a
  [  2.487919]  check_preemption_disabled+0xda/0xe0
  [  2.487921]  debug_smp_processor_id+0x17/0x20
  [  2.487924]  clear_sched_clock_stable+0x28/0x80
  [  2.487927]  mark_tsc_unstable+0x22/0x70
  [  2.487930]  acpi_processor_get_power_info+0x3e3/0x6a0
  [  2.487932]  acpi_processor_power_init+0x3a/0x1d0
  [  2.487933]  __acpi_processor_start+0x162/0x1b0
               ....
  [  2.487950]  acpi_processor_driver_init+0x20/0x96
  [  2.487951]  do_one_initcall+0x3f/0x170
  [  2.487954]  kernel_init_freeable+0x18e/0x216
  [  2.487955]  ? rest_init+0xd0/0xd0
  [  2.487956]  kernel_init+0x9/0x100
  [  2.487958]  ret_from_fork+0x22/0x30
  [  2.487960] sched_clock: Marking unstable (2488005383, -223143)<-(2590866395, -103084155)
  [  2.488004] tsc: Marking TSC unstable due to TSC halts in idle

(gdb) list *clear_sched_clock_stable+0x28
0xffffffff8108bbb8 is in clear_sched_clock_stable (kernel/sched/clock.c:114).

[...]

112 static inline struct sched_clock_data *this_scd(void)
113 {
114 return this_cpu_ptr(&sched_clock_data);
115 }

Make use of two mainline commits from post v4.11 to fix this.

patches/sched-clock-Initialize-all-per-CPU-state-before-swit.patch new file +122 -0
new file mode 100644
index 000000000000..95d17edcf2fb
@@ -0,0 +1,122 @@
+From cf15ca8deda86b27b66e27848b4b0fe58098fc0b Mon Sep 17 00:00:00 2001
+From: Peter Zijlstra <[email protected]>
+Date: Fri, 21 Apr 2017 12:11:53 +0200
+Subject: [PATCH] sched/clock: Initialize all per-CPU state before switching
+ (back) to unstable
+
+commit cf15ca8deda86b27b66e27848b4b0fe58098fc0b upstream.
+
+In preparation for not keeping the sched_clock_tick() active for
+stable TSC, we need to explicitly initialize all per-CPU state
+before switching back to unstable.
+
+Note: this patch looses the __gtod_offset calculation; it will be
+restored in the next one.
+
+Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
+Cc: Linus Torvalds <[email protected]>
+Cc: Mike Galbraith <[email protected]>
+Cc: Peter Zijlstra <[email protected]>
+Cc: Thomas Gleixner <[email protected]>
+Cc: [email protected]
+Signed-off-by: Ingo Molnar <[email protected]>
+
+diff --git a/kernel/sched/clock.c b/kernel/sched/clock.c
+index 00a45c45beca..dc650851935f 100644
+--- a/kernel/sched/clock.c
++++ b/kernel/sched/clock.c
+@@ -124,6 +124,12 @@ int sched_clock_stable(void)
+ 	return static_branch_likely(&__sched_clock_stable);
+ }
+ 
++static void __scd_stamp(struct sched_clock_data *scd)
++{
++	scd->tick_gtod = ktime_get_ns();
++	scd->tick_raw = sched_clock();
++}
++
+ static void __set_sched_clock_stable(void)
+ {
+ 	struct sched_clock_data *scd = this_scd();
+@@ -141,8 +147,37 @@ static void __set_sched_clock_stable(void)
+ 	tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
+ }
+ 
++/*
++ * If we ever get here, we're screwed, because we found out -- typically after
++ * the fact -- that TSC wasn't good. This means all our clocksources (including
++ * ktime) could have reported wrong values.
++ *
++ * What we do here is an attempt to fix up and continue sort of where we left
++ * off in a coherent manner.
++ *
++ * The only way to fully avoid random clock jumps is to boot with:
++ * "tsc=unstable".
++ */
+ static void __sched_clock_work(struct work_struct *work)
+ {
++	struct sched_clock_data *scd;
++	int cpu;
++
++	/* take a current timestamp and set 'now' */
++	preempt_disable();
++	scd = this_scd();
++	__scd_stamp(scd);
++	scd->clock = scd->tick_gtod + __gtod_offset;
++	preempt_enable();
++
++	/* clone to all CPUs */
++	for_each_possible_cpu(cpu)
++		per_cpu(sched_clock_data, cpu) = *scd;
++
++	printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
++			scd->tick_gtod, __gtod_offset,
++			scd->tick_raw,  __sched_clock_offset);
++
+ 	static_branch_disable(&__sched_clock_stable);
+ }
+ 
+@@ -150,27 +185,11 @@ static DECLARE_WORK(sched_clock_work, __sched_clock_work);
+ 
+ static void __clear_sched_clock_stable(void)
+ {
+-	struct sched_clock_data *scd = this_scd();
+-
+-	/*
+-	 * Attempt to make the stable->unstable transition continuous.
+-	 *
+-	 * Trouble is, this is typically called from the TSC watchdog
+-	 * timer, which is late per definition. This means the tick
+-	 * values can already be screwy.
+-	 *
+-	 * Still do what we can.
+-	 */
+-	__gtod_offset = (scd->tick_raw + __sched_clock_offset) - (scd->tick_gtod);
+-
+-	printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
+-			scd->tick_gtod, __gtod_offset,
+-			scd->tick_raw,  __sched_clock_offset);
++	if (!sched_clock_stable())
++		return;
+ 
+ 	tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
+-
+-	if (sched_clock_stable())
+-		schedule_work(&sched_clock_work);
++	schedule_work(&sched_clock_work);
+ }
+ 
+ void clear_sched_clock_stable(void)
+@@ -357,8 +376,7 @@ void sched_clock_tick(void)
+ 	 * XXX arguably we can skip this if we expose tsc_clocksource_reliable
+ 	 */
+ 	scd = this_scd();
+-	scd->tick_raw  = sched_clock();
+-	scd->tick_gtod = ktime_get_ns();
++	__scd_stamp(scd);
+ 
+ 	if (!sched_clock_stable() && likely(sched_clock_running))
+ 		sched_clock_local(scd);
+-- 
+2.1.4
+
patches/series +2 -0
index 32bfb07ec7d9..5f7de7b66ad9 100644
@@ -67,6 +67,8 @@ smp-hotplug-Move-unparking-of-percpu-threads-to-the-.patch
 0013-crypto-N2-Replace-racy-task-affinity-logic.patch
 
 # a few patches from tip's sched/core
+sched-clock-Initialize-all-per-CPU-state-before-swit.patch
+x86-tsc-sched-clock-clocksource-Use-clocksource-watc.patch
 0001-sched-clock-Fix-early-boot-preempt-assumption-in-__s.patch
 0001-init-Pin-init-task-to-the-boot-CPU-initially.patch
 0002-arm-Adjust-system_state-check.patch
patches/x86-tsc-sched-clock-clocksource-Use-clocksource-watc.patch new file +155 -0

155 lines not on this page · raw message

Signature · 1 line
2.1.4
#5 lore raw
Quoted 4 lines · Paul Gortmaker

The tree patched up from scratch without any need to refresh the two newly exported commits and the old trash box with the broken tsc doesn't have the boot splat. Note: I've not [yet] tested on something with a working tsc, but I doubt that is a concern...

I forgot to respond but I released a v4.11-RT with this included.

Quoted 1 line

Paul.

Sebastian