Skip to content

Commit b421b22

Browse files
Peter Zijlstraingomolnar
authored andcommitted
x86/tsc, sched/clock, clocksource: Use clocksource watchdog to provide stable sync points
Currently we keep sched_clock_tick() active for stable TSC in order to keep the per-CPU state semi up-to-date. The (obvious) problem is that by the time we detect TSC is borked, our per-CPU state is also borked. So hook into the clocksource watchdog and call a method after we've found it to still be stable. There's the obvious race where the TSC goes wonky between finding it stable and us running the callback, but closing that is too much work and not really worth it, since we're already detecting TSC wobbles after the fact, so we cannot, per definition, fully avoid funny clock values. And since the watchdog runs less often than the tick, this is also an optimization. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent cf15ca8 commit b421b22

5 files changed

Lines changed: 42 additions & 10 deletions

File tree

‎arch/x86/kernel/tsc.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,15 @@ static void tsc_cs_mark_unstable(struct clocksource *cs)
10331033
pr_info("Marking TSC unstable due to clocksource watchdog\n");
10341034
}
10351035

1036+
static void tsc_cs_tick_stable(struct clocksource *cs)
1037+
{
1038+
if (tsc_unstable)
1039+
return;
1040+
1041+
if (using_native_sched_clock())
1042+
sched_clock_tick_stable();
1043+
}
1044+
10361045
/*
10371046
* .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc()
10381047
*/
@@ -1046,6 +1055,7 @@ static struct clocksource clocksource_tsc = {
10461055
.archdata = { .vclock_mode = VCLOCK_TSC },
10471056
.resume = tsc_resume,
10481057
.mark_unstable = tsc_cs_mark_unstable,
1058+
.tick_stable = tsc_cs_tick_stable,
10491059
};
10501060

10511061
void mark_tsc_unstable(char *reason)

‎include/linux/clocksource.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct clocksource {
9696
void (*suspend)(struct clocksource *cs);
9797
void (*resume)(struct clocksource *cs);
9898
void (*mark_unstable)(struct clocksource *cs);
99+
void (*tick_stable)(struct clocksource *cs);
99100

100101
/* private: */
101102
#ifdef CONFIG_CLOCKSOURCE_WATCHDOG

‎include/linux/sched/clock.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ extern void clear_sched_clock_stable(void);
6363
*/
6464
extern u64 __sched_clock_offset;
6565

66-
6766
extern void sched_clock_tick(void);
67+
extern void sched_clock_tick_stable(void);
6868
extern void sched_clock_idle_sleep_event(void);
6969
extern void sched_clock_idle_wakeup_event(u64 delta_ns);
7070

‎kernel/sched/clock.c‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,38 @@ void sched_clock_tick(void)
366366
{
367367
struct sched_clock_data *scd;
368368

369+
if (sched_clock_stable())
370+
return;
371+
372+
if (unlikely(!sched_clock_running))
373+
return;
374+
369375
WARN_ON_ONCE(!irqs_disabled());
370376

371-
/*
372-
* Update these values even if sched_clock_stable(), because it can
373-
* become unstable at any point in time at which point we need some
374-
* values to fall back on.
375-
*
376-
* XXX arguably we can skip this if we expose tsc_clocksource_reliable
377-
*/
378377
scd = this_scd();
379378
__scd_stamp(scd);
379+
sched_clock_local(scd);
380+
}
381+
382+
void sched_clock_tick_stable(void)
383+
{
384+
u64 gtod, clock;
380385

381-
if (!sched_clock_stable() && likely(sched_clock_running))
382-
sched_clock_local(scd);
386+
if (!sched_clock_stable())
387+
return;
388+
389+
/*
390+
* Called under watchdog_lock.
391+
*
392+
* The watchdog just found this TSC to (still) be stable, so now is a
393+
* good moment to update our __gtod_offset. Because once we find the
394+
* TSC to be unstable, any computation will be computing crap.
395+
*/
396+
local_irq_disable();
397+
gtod = ktime_get_ns();
398+
clock = sched_clock();
399+
__gtod_offset = (clock + __sched_clock_offset) - gtod;
400+
local_irq_enable();
383401
}
384402

385403
/*

‎kernel/time/clocksource.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ static void clocksource_watchdog(unsigned long data)
233233
continue;
234234
}
235235

236+
if (cs == curr_clocksource && cs->tick_stable)
237+
cs->tick_stable(cs);
238+
236239
if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
237240
(cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
238241
(watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {

0 commit comments

Comments
 (0)