Skip to content

Commit f94c8d1

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched/clock, x86/tsc: Rework the x86 'unstable' sched_clock() interface
Wanpeng Li reported that since the following commit: acb0405 ("sched/clock: Fix hotplug crash") ... KVM always runs with unstable sched-clock even though KVM's kvm_clock _is_ stable. The problem is that we've tied clear_sched_clock_stable() to the TSC state, and overlooked that sched_clock() is a paravirt function. Solve this by doing two things: - tie the sched_clock() stable state more clearly to the TSC stable state for the normal (!paravirt) case. - only call clear_sched_clock_stable() when we mark TSC unstable when we use native_sched_clock(). The first means we can actually run with stable sched_clock in more situations then before, which is good. And since commit: 12907fb ("sched/clock, clocksource: Add optional cs::mark_unstable() method") ... this should be reliable. Since any detection of TSC fail now results in marking the TSC unstable. Reported-by: Wanpeng Li <kernellwp@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Fixes: acb0405 ("sched/clock: Fix hotplug crash") Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 0ba87bb commit f94c8d1

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

arch/x86/kernel/cpu/amd.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,6 @@ static void early_init_amd(struct cpuinfo_x86 *c)
555555
if (c->x86_power & (1 << 8)) {
556556
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
557557
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
558-
if (check_tsc_unstable())
559-
clear_sched_clock_stable();
560-
} else {
561-
clear_sched_clock_stable();
562558
}
563559

564560
/* Bit 12 of 8000_0007 edx is accumulated power mechanism. */

arch/x86/kernel/cpu/centaur.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ static void early_init_centaur(struct cpuinfo_x86 *c)
104104
#ifdef CONFIG_X86_64
105105
set_cpu_cap(c, X86_FEATURE_SYSENTER32);
106106
#endif
107-
108-
clear_sched_clock_stable();
109107
}
110108

111109
static void init_centaur(struct cpuinfo_x86 *c)

arch/x86/kernel/cpu/common.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ static void default_init(struct cpuinfo_x86 *c)
8686
strcpy(c->x86_model_id, "386");
8787
}
8888
#endif
89-
clear_sched_clock_stable();
9089
}
9190

9291
static const struct cpu_dev default_cpu = {
@@ -1075,8 +1074,6 @@ static void identify_cpu(struct cpuinfo_x86 *c)
10751074
*/
10761075
if (this_cpu->c_init)
10771076
this_cpu->c_init(c);
1078-
else
1079-
clear_sched_clock_stable();
10801077

10811078
/* Disable the PN if appropriate */
10821079
squash_the_stupid_serial_number(c);

arch/x86/kernel/cpu/cyrix.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ static void early_init_cyrix(struct cpuinfo_x86 *c)
184184
set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
185185
break;
186186
}
187-
clear_sched_clock_stable();
188187
}
189188

190189
static void init_cyrix(struct cpuinfo_x86 *c)

arch/x86/kernel/cpu/intel.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ static void early_init_intel(struct cpuinfo_x86 *c)
161161
if (c->x86_power & (1 << 8)) {
162162
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
163163
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
164-
if (check_tsc_unstable())
165-
clear_sched_clock_stable();
166-
} else {
167-
clear_sched_clock_stable();
168164
}
169165

170166
/* Penwell and Cloverview have the TSC which doesn't sleep on S3 */

arch/x86/kernel/cpu/transmeta.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ static void early_init_transmeta(struct cpuinfo_x86 *c)
1515
if (xlvl >= 0x80860001)
1616
c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001);
1717
}
18-
19-
clear_sched_clock_stable();
2018
}
2119

2220
static void init_transmeta(struct cpuinfo_x86 *c)

arch/x86/kernel/tsc.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,16 @@ unsigned long long sched_clock(void)
326326
{
327327
return paravirt_sched_clock();
328328
}
329+
330+
static inline bool using_native_sched_clock(void)
331+
{
332+
return pv_time_ops.sched_clock == native_sched_clock;
333+
}
329334
#else
330335
unsigned long long
331336
sched_clock(void) __attribute__((alias("native_sched_clock")));
337+
338+
static inline bool using_native_sched_clock(void) { return true; }
332339
#endif
333340

334341
int check_tsc_unstable(void)
@@ -1111,8 +1118,10 @@ static void tsc_cs_mark_unstable(struct clocksource *cs)
11111118
{
11121119
if (tsc_unstable)
11131120
return;
1121+
11141122
tsc_unstable = 1;
1115-
clear_sched_clock_stable();
1123+
if (using_native_sched_clock())
1124+
clear_sched_clock_stable();
11161125
disable_sched_clock_irqtime();
11171126
pr_info("Marking TSC unstable due to clocksource watchdog\n");
11181127
}
@@ -1134,18 +1143,20 @@ static struct clocksource clocksource_tsc = {
11341143

11351144
void mark_tsc_unstable(char *reason)
11361145
{
1137-
if (!tsc_unstable) {
1138-
tsc_unstable = 1;
1146+
if (tsc_unstable)
1147+
return;
1148+
1149+
tsc_unstable = 1;
1150+
if (using_native_sched_clock())
11391151
clear_sched_clock_stable();
1140-
disable_sched_clock_irqtime();
1141-
pr_info("Marking TSC unstable due to %s\n", reason);
1142-
/* Change only the rating, when not registered */
1143-
if (clocksource_tsc.mult)
1144-
clocksource_mark_unstable(&clocksource_tsc);
1145-
else {
1146-
clocksource_tsc.flags |= CLOCK_SOURCE_UNSTABLE;
1147-
clocksource_tsc.rating = 0;
1148-
}
1152+
disable_sched_clock_irqtime();
1153+
pr_info("Marking TSC unstable due to %s\n", reason);
1154+
/* Change only the rating, when not registered */
1155+
if (clocksource_tsc.mult) {
1156+
clocksource_mark_unstable(&clocksource_tsc);
1157+
} else {
1158+
clocksource_tsc.flags |= CLOCK_SOURCE_UNSTABLE;
1159+
clocksource_tsc.rating = 0;
11491160
}
11501161
}
11511162

0 commit comments

Comments
 (0)