Skip to content

Commit cf15ca8

Browse files
Peter Zijlstraingomolnar
authored andcommitted
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. 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 aa7b630 commit cf15ca8

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

‎kernel/sched/clock.c‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ int sched_clock_stable(void)
124124
return static_branch_likely(&__sched_clock_stable);
125125
}
126126

127+
static void __scd_stamp(struct sched_clock_data *scd)
128+
{
129+
scd->tick_gtod = ktime_get_ns();
130+
scd->tick_raw = sched_clock();
131+
}
132+
127133
static void __set_sched_clock_stable(void)
128134
{
129135
struct sched_clock_data *scd = this_scd();
@@ -141,36 +147,49 @@ static void __set_sched_clock_stable(void)
141147
tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
142148
}
143149

150+
/*
151+
* If we ever get here, we're screwed, because we found out -- typically after
152+
* the fact -- that TSC wasn't good. This means all our clocksources (including
153+
* ktime) could have reported wrong values.
154+
*
155+
* What we do here is an attempt to fix up and continue sort of where we left
156+
* off in a coherent manner.
157+
*
158+
* The only way to fully avoid random clock jumps is to boot with:
159+
* "tsc=unstable".
160+
*/
144161
static void __sched_clock_work(struct work_struct *work)
145162
{
163+
struct sched_clock_data *scd;
164+
int cpu;
165+
166+
/* take a current timestamp and set 'now' */
167+
preempt_disable();
168+
scd = this_scd();
169+
__scd_stamp(scd);
170+
scd->clock = scd->tick_gtod + __gtod_offset;
171+
preempt_enable();
172+
173+
/* clone to all CPUs */
174+
for_each_possible_cpu(cpu)
175+
per_cpu(sched_clock_data, cpu) = *scd;
176+
177+
printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
178+
scd->tick_gtod, __gtod_offset,
179+
scd->tick_raw, __sched_clock_offset);
180+
146181
static_branch_disable(&__sched_clock_stable);
147182
}
148183

149184
static DECLARE_WORK(sched_clock_work, __sched_clock_work);
150185

151186
static void __clear_sched_clock_stable(void)
152187
{
153-
struct sched_clock_data *scd = this_scd();
154-
155-
/*
156-
* Attempt to make the stable->unstable transition continuous.
157-
*
158-
* Trouble is, this is typically called from the TSC watchdog
159-
* timer, which is late per definition. This means the tick
160-
* values can already be screwy.
161-
*
162-
* Still do what we can.
163-
*/
164-
__gtod_offset = (scd->tick_raw + __sched_clock_offset) - (scd->tick_gtod);
165-
166-
printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
167-
scd->tick_gtod, __gtod_offset,
168-
scd->tick_raw, __sched_clock_offset);
188+
if (!sched_clock_stable())
189+
return;
169190

170191
tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
171-
172-
if (sched_clock_stable())
173-
schedule_work(&sched_clock_work);
192+
schedule_work(&sched_clock_work);
174193
}
175194

176195
void clear_sched_clock_stable(void)
@@ -357,8 +376,7 @@ void sched_clock_tick(void)
357376
* XXX arguably we can skip this if we expose tsc_clocksource_reliable
358377
*/
359378
scd = this_scd();
360-
scd->tick_raw = sched_clock();
361-
scd->tick_gtod = ktime_get_ns();
379+
__scd_stamp(scd);
362380

363381
if (!sched_clock_stable() && likely(sched_clock_running))
364382
sched_clock_local(scd);

0 commit comments

Comments
 (0)