Skip to content

Commit 35af99e

Browse files
Peter Zijlstraingomolnar
authored andcommitted
sched/clock, x86: Use a static_key for sched_clock_stable
In order to avoid the runtime condition and variable load turn sched_clock_stable into a static_key. Also provide a shorter implementation of local_clock() and cpu_clock(int) when sched_clock_stable==1. MAINLINE PRE POST sched_clock_stable: 1 1 1 (cold) sched_clock: 329841 221876 215295 (cold) local_clock: 301773 234692 220773 (warm) sched_clock: 38375 25602 25659 (warm) local_clock: 100371 33265 27242 (warm) rdtsc: 27340 24214 24208 sched_clock_stable: 0 0 0 (cold) sched_clock: 382634 235941 237019 (cold) local_clock: 396890 297017 294819 (warm) sched_clock: 38194 25233 25609 (warm) local_clock: 143452 71234 71232 (warm) rdtsc: 27345 24245 24243 Signed-off-by: Peter Zijlstra <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Link: http://lkml.kernel.org/n/tip-eummbdechzz37mwmpags1gjr@git.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ef08f0f commit 35af99e

9 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎arch/x86/kernel/cpu/amd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static void early_init_amd(struct cpuinfo_x86 *c)
487487
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
488488
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
489489
if (!check_tsc_unstable())
490-
sched_clock_stable = 1;
490+
set_sched_clock_stable();
491491
}
492492

493493
#ifdef CONFIG_X86_64

‎arch/x86/kernel/cpu/intel.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void early_init_intel(struct cpuinfo_x86 *c)
9393
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
9494
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
9595
if (!check_tsc_unstable())
96-
sched_clock_stable = 1;
96+
set_sched_clock_stable();
9797
}
9898

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

‎arch/x86/kernel/cpu/perf_event.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ void arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
18901890
userpg->cap_user_rdpmc = x86_pmu.attr_rdpmc;
18911891
userpg->pmc_width = x86_pmu.cntval_bits;
18921892

1893-
if (!sched_clock_stable)
1893+
if (!sched_clock_stable())
18941894
return;
18951895

18961896
data = cyc2ns_read_begin();

‎arch/x86/kernel/tsc.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ static unsigned long long cyc2ns_suspend;
822822

823823
void tsc_save_sched_clock_state(void)
824824
{
825-
if (!sched_clock_stable)
825+
if (!sched_clock_stable())
826826
return;
827827

828828
cyc2ns_suspend = sched_clock();
@@ -842,7 +842,7 @@ void tsc_restore_sched_clock_state(void)
842842
unsigned long flags;
843843
int cpu;
844844

845-
if (!sched_clock_stable)
845+
if (!sched_clock_stable())
846846
return;
847847

848848
local_irq_save(flags);
@@ -984,7 +984,7 @@ void mark_tsc_unstable(char *reason)
984984
{
985985
if (!tsc_unstable) {
986986
tsc_unstable = 1;
987-
sched_clock_stable = 0;
987+
clear_sched_clock_stable();
988988
disable_sched_clock_irqtime();
989989
pr_info("Marking TSC unstable due to %s\n", reason);
990990
/* Change only the rating, when not registered */

‎include/linux/sched.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,9 @@ static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
19941994
* but then during bootup it turns out that sched_clock()
19951995
* is reliable after all:
19961996
*/
1997-
extern int sched_clock_stable;
1997+
extern int sched_clock_stable(void);
1998+
extern void set_sched_clock_stable(void);
1999+
extern void clear_sched_clock_stable(void);
19982000

19992001
extern void sched_clock_tick(void);
20002002
extern void sched_clock_idle_sleep_event(void);

‎kernel/sched/clock.c‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/percpu.h>
5959
#include <linux/ktime.h>
6060
#include <linux/sched.h>
61+
#include <linux/static_key.h>
6162

6263
/*
6364
* Scheduler clock - returns current time in nanosec units.
@@ -74,7 +75,27 @@ EXPORT_SYMBOL_GPL(sched_clock);
7475
__read_mostly int sched_clock_running;
7576

7677
#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
77-
__read_mostly int sched_clock_stable;
78+
static struct static_key __sched_clock_stable = STATIC_KEY_INIT;
79+
80+
int sched_clock_stable(void)
81+
{
82+
if (static_key_false(&__sched_clock_stable))
83+
return false;
84+
return true;
85+
}
86+
87+
void set_sched_clock_stable(void)
88+
{
89+
if (!sched_clock_stable())
90+
static_key_slow_dec(&__sched_clock_stable);
91+
}
92+
93+
void clear_sched_clock_stable(void)
94+
{
95+
/* XXX worry about clock continuity */
96+
if (sched_clock_stable())
97+
static_key_slow_inc(&__sched_clock_stable);
98+
}
7899

79100
struct sched_clock_data {
80101
u64 tick_raw;
@@ -234,7 +255,7 @@ u64 sched_clock_cpu(int cpu)
234255
struct sched_clock_data *scd;
235256
u64 clock;
236257

237-
if (sched_clock_stable)
258+
if (sched_clock_stable())
238259
return sched_clock();
239260

240261
if (unlikely(!sched_clock_running))
@@ -257,7 +278,7 @@ void sched_clock_tick(void)
257278
struct sched_clock_data *scd;
258279
u64 now, now_gtod;
259280

260-
if (sched_clock_stable)
281+
if (sched_clock_stable())
261282
return;
262283

263284
if (unlikely(!sched_clock_running))
@@ -308,7 +329,10 @@ EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
308329
*/
309330
u64 cpu_clock(int cpu)
310331
{
311-
return sched_clock_cpu(cpu);
332+
if (static_key_false(&__sched_clock_stable))
333+
return sched_clock_cpu(cpu);
334+
335+
return sched_clock();
312336
}
313337

314338
/*
@@ -320,7 +344,10 @@ u64 cpu_clock(int cpu)
320344
*/
321345
u64 local_clock(void)
322346
{
323-
return sched_clock_cpu(raw_smp_processor_id());
347+
if (static_key_false(&__sched_clock_stable))
348+
return sched_clock_cpu(raw_smp_processor_id());
349+
350+
return sched_clock();
324351
}
325352

326353
#else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
@@ -340,12 +367,12 @@ u64 sched_clock_cpu(int cpu)
340367

341368
u64 cpu_clock(int cpu)
342369
{
343-
return sched_clock_cpu(cpu);
370+
return sched_clock();
344371
}
345372

346373
u64 local_clock(void)
347374
{
348-
return sched_clock_cpu(0);
375+
return sched_clock();
349376
}
350377

351378
#endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */

‎kernel/sched/debug.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static void sched_debug_header(struct seq_file *m)
371371
PN(cpu_clk);
372372
P(jiffies);
373373
#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
374-
P(sched_clock_stable);
374+
P(sched_clock_stable());
375375
#endif
376376
#undef PN
377377
#undef P

‎kernel/time/tick-sched.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static bool can_stop_full_tick(void)
177177
* TODO: kick full dynticks CPUs when
178178
* sched_clock_stable is set.
179179
*/
180-
if (!sched_clock_stable) {
180+
if (!sched_clock_stable()) {
181181
trace_tick_stop(0, "unstable sched clock\n");
182182
/*
183183
* Don't allow the user to think they can get

‎kernel/trace/ring_buffer.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ rb_reserve_next_event(struct ring_buffer *buffer,
25582558
if (unlikely(test_time_stamp(delta))) {
25592559
int local_clock_stable = 1;
25602560
#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2561-
local_clock_stable = sched_clock_stable;
2561+
local_clock_stable = sched_clock_stable();
25622562
#endif
25632563
WARN_ONCE(delta > (1ULL << 59),
25642564
KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",

0 commit comments

Comments
 (0)