Skip to content

Commit

Permalink
sched/clock: Fix hotplug crash
Browse files Browse the repository at this point in the history
Mike reported that he could trigger the WARN_ON_ONCE() in
set_sched_clock_stable() using hotplug.

This exposed a fundamental problem with the interface, we should never
mark the TSC stable if we ever find it to be unstable. Therefore
set_sched_clock_stable() is a broken interface.

The reason it existed is that not having it is a pain, it means all
relevant architecture code needs to call clear_sched_clock_stable()
where appropriate.

Of the three architectures that select HAVE_UNSTABLE_SCHED_CLOCK ia64
and parisc are trivial in that they never called
set_sched_clock_stable(), so add an unconditional call to
clear_sched_clock_stable() to them.

For x86 the story is a lot more involved, and what this patch tries to
do is ensure we preserve the status quo. So even is Cyrix or Transmeta
have usable TSC they never called set_sched_clock_stable() so they now
get an explicit mark unstable.

Reported-by: Mike Galbraith <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Fixes: 9881b02 ("sched/clock: Delay switching sched_clock to stable")
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Jan 20, 2017
1 parent 02cfdc9 commit acb0405
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 29 deletions.
2 changes: 2 additions & 0 deletions arch/ia64/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ setup_arch (char **cmdline_p)
check_sal_cache_flush();
#endif
paging_init();

clear_sched_clock_stable();
}

/*
Expand Down
2 changes: 2 additions & 0 deletions arch/parisc/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#undef PCI_DEBUG
#include <linux/proc_fs.h>
#include <linux/export.h>
#include <linux/sched.h>

#include <asm/processor.h>
#include <asm/sections.h>
Expand Down Expand Up @@ -176,6 +177,7 @@ void __init setup_arch(char **cmdline_p)
conswitchp = &dummy_con; /* we use do_take_over_console() later ! */
#endif

clear_sched_clock_stable();
}

/*
Expand Down
6 changes: 4 additions & 2 deletions arch/x86/kernel/cpu/amd.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,10 @@ static void early_init_amd(struct cpuinfo_x86 *c)
if (c->x86_power & (1 << 8)) {
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
if (!check_tsc_unstable())
set_sched_clock_stable();
if (check_tsc_unstable())
clear_sched_clock_stable();
} else {
clear_sched_clock_stable();
}

/* Bit 12 of 8000_0007 edx is accumulated power mechanism. */
Expand Down
6 changes: 4 additions & 2 deletions arch/x86/kernel/cpu/centaur.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <linux/bitops.h>
#include <linux/kernel.h>

#include <linux/sched.h>

#include <asm/cpufeature.h>
#include <asm/e820.h>
Expand Down Expand Up @@ -104,6 +104,8 @@ static void early_init_centaur(struct cpuinfo_x86 *c)
#ifdef CONFIG_X86_64
set_cpu_cap(c, X86_FEATURE_SYSENTER32);
#endif

clear_sched_clock_stable();
}

static void init_centaur(struct cpuinfo_x86 *c)
Expand Down
3 changes: 3 additions & 0 deletions arch/x86/kernel/cpu/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static void default_init(struct cpuinfo_x86 *c)
strcpy(c->x86_model_id, "386");
}
#endif
clear_sched_clock_stable();
}

static const struct cpu_dev default_cpu = {
Expand Down Expand Up @@ -1055,6 +1056,8 @@ static void identify_cpu(struct cpuinfo_x86 *c)
*/
if (this_cpu->c_init)
this_cpu->c_init(c);
else
clear_sched_clock_stable();

/* Disable the PN if appropriate */
squash_the_stupid_serial_number(c);
Expand Down
2 changes: 2 additions & 0 deletions arch/x86/kernel/cpu/cyrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <asm/pci-direct.h>
#include <asm/tsc.h>
#include <asm/cpufeature.h>
#include <linux/sched.h>

#include "cpu.h"

Expand Down Expand Up @@ -183,6 +184,7 @@ static void early_init_cyrix(struct cpuinfo_x86 *c)
set_cpu_cap(c, X86_FEATURE_CYRIX_ARR);
break;
}
clear_sched_clock_stable();
}

static void init_cyrix(struct cpuinfo_x86 *c)
Expand Down
6 changes: 4 additions & 2 deletions arch/x86/kernel/cpu/intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ static void early_init_intel(struct cpuinfo_x86 *c)
if (c->x86_power & (1 << 8)) {
set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
if (!check_tsc_unstable())
set_sched_clock_stable();
if (check_tsc_unstable())
clear_sched_clock_stable();
} else {
clear_sched_clock_stable();
}

/* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
Expand Down
3 changes: 3 additions & 0 deletions arch/x86/kernel/cpu/transmeta.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <asm/cpufeature.h>
#include <asm/msr.h>
Expand All @@ -14,6 +15,8 @@ static void early_init_transmeta(struct cpuinfo_x86 *c)
if (xlvl >= 0x80860001)
c->x86_capability[CPUID_8086_0001_EDX] = cpuid_edx(0x80860001);
}

clear_sched_clock_stable();
}

static void init_transmeta(struct cpuinfo_x86 *c)
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/kvmclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ static inline void kvm_sched_clock_init(bool stable)
{
if (!stable) {
pv_time_ops.sched_clock = kvm_clock_read;
clear_sched_clock_stable();
return;
}

kvm_sched_clock_offset = kvm_clock_read();
pv_time_ops.sched_clock = kvm_sched_clock_read;
set_sched_clock_stable();

printk(KERN_INFO "kvm-clock: using sched offset of %llu cycles\n",
kvm_sched_clock_offset);
Expand Down
1 change: 0 additions & 1 deletion include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -2547,7 +2547,6 @@ extern void sched_clock_init_late(void);
* is reliable after all:
*/
extern int sched_clock_stable(void);
extern void set_sched_clock_stable(void);
extern void clear_sched_clock_stable(void);

extern void sched_clock_tick(void);
Expand Down
29 changes: 8 additions & 21 deletions kernel/sched/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ void sched_clock_init(void)
}

#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
/*
* We must start with !__sched_clock_stable because the unstable -> stable
* transition is accurate, while the stable -> unstable transition is not.
*
* Similarly we start with __sched_clock_stable_early, thereby assuming we
* will become stable, such that there's only a single 1 -> 0 transition.
*/
static DEFINE_STATIC_KEY_FALSE(__sched_clock_stable);
static int __sched_clock_stable_early;
static int __sched_clock_stable_early = 1;

/*
* We want: ktime_get_ns() + gtod_offset == sched_clock() + raw_offset
Expand Down Expand Up @@ -132,24 +139,6 @@ static void __set_sched_clock_stable(void)
tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
}

void set_sched_clock_stable(void)
{
__sched_clock_stable_early = 1;

smp_mb(); /* matches sched_clock_init_late() */

/*
* This really should only be called early (before
* sched_clock_init_late()) when guestimating our sched_clock() is
* solid.
*
* After that we test stability and we can negate our guess using
* clear_sched_clock_stable, possibly from a watchdog.
*/
if (WARN_ON_ONCE(sched_clock_running == 2))
__set_sched_clock_stable();
}

static void __clear_sched_clock_stable(struct work_struct *work)
{
struct sched_clock_data *scd = this_scd();
Expand Down Expand Up @@ -199,8 +188,6 @@ void sched_clock_init_late(void)

if (__sched_clock_stable_early)
__set_sched_clock_stable();
else
__clear_sched_clock_stable(NULL);
}

/*
Expand Down

0 comments on commit acb0405

Please sign in to comment.