aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/Kconfig1
-rw-r--r--arch/x86/kernel/tsc.c21
-rw-r--r--kernel/time/Kconfig5
-rw-r--r--kernel/time/timekeeping_internal.h9
4 files changed, 24 insertions, 12 deletions
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 7fa17b5ce668..d08e061c187a 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -109,6 +109,7 @@ config X86
109 select CLOCKSOURCE_WATCHDOG 109 select CLOCKSOURCE_WATCHDOG
110 select GENERIC_CLOCKEVENTS 110 select GENERIC_CLOCKEVENTS
111 select ARCH_CLOCKSOURCE_DATA 111 select ARCH_CLOCKSOURCE_DATA
112 select CLOCKSOURCE_VALIDATE_LAST_CYCLE
112 select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC) 113 select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
113 select GENERIC_TIME_VSYSCALL 114 select GENERIC_TIME_VSYSCALL
114 select GENERIC_STRNCPY_FROM_USER 115 select GENERIC_STRNCPY_FROM_USER
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 57e5ce126d5a..456c0e660c43 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -951,7 +951,7 @@ core_initcall(cpufreq_tsc);
951static struct clocksource clocksource_tsc; 951static struct clocksource clocksource_tsc;
952 952
953/* 953/*
954 * We compare the TSC to the cycle_last value in the clocksource 954 * We used to compare the TSC to the cycle_last value in the clocksource
955 * structure to avoid a nasty time-warp. This can be observed in a 955 * structure to avoid a nasty time-warp. This can be observed in a
956 * very small window right after one CPU updated cycle_last under 956 * very small window right after one CPU updated cycle_last under
957 * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which 957 * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
@@ -961,26 +961,23 @@ static struct clocksource clocksource_tsc;
961 * due to the unsigned delta calculation of the time keeping core 961 * due to the unsigned delta calculation of the time keeping core
962 * code, which is necessary to support wrapping clocksources like pm 962 * code, which is necessary to support wrapping clocksources like pm
963 * timer. 963 * timer.
964 *
965 * This sanity check is now done in the core timekeeping code.
966 * checking the result of read_tsc() - cycle_last for being negative.
967 * That works because CLOCKSOURCE_MASK(64) does not mask out any bit.
964 */ 968 */
965static cycle_t read_tsc(struct clocksource *cs) 969static cycle_t read_tsc(struct clocksource *cs)
966{ 970{
967 cycle_t ret = (cycle_t)get_cycles(); 971 return (cycle_t)get_cycles();
968
969 return ret >= clocksource_tsc.cycle_last ?
970 ret : clocksource_tsc.cycle_last;
971}
972
973static void resume_tsc(struct clocksource *cs)
974{
975 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC_S3))
976 clocksource_tsc.cycle_last = 0;
977} 972}
978 973
974/*
975 * .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc()
976 */
979static struct clocksource clocksource_tsc = { 977static struct clocksource clocksource_tsc = {
980 .name = "tsc", 978 .name = "tsc",
981 .rating = 300, 979 .rating = 300,
982 .read = read_tsc, 980 .read = read_tsc,
983 .resume = resume_tsc,
984 .mask = CLOCKSOURCE_MASK(64), 981 .mask = CLOCKSOURCE_MASK(64),
985 .flags = CLOCK_SOURCE_IS_CONTINUOUS | 982 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
986 CLOCK_SOURCE_MUST_VERIFY, 983 CLOCK_SOURCE_MUST_VERIFY,
diff --git a/kernel/time/Kconfig b/kernel/time/Kconfig
index feccfd888732..d626dc98e8df 100644
--- a/kernel/time/Kconfig
+++ b/kernel/time/Kconfig
@@ -12,6 +12,11 @@ config CLOCKSOURCE_WATCHDOG
12config ARCH_CLOCKSOURCE_DATA 12config ARCH_CLOCKSOURCE_DATA
13 bool 13 bool
14 14
15# Clocksources require validation of the clocksource against the last
16# cycle update - x86/TSC misfeature
17config CLOCKSOURCE_VALIDATE_LAST_CYCLE
18 bool
19
15# Timekeeping vsyscall support 20# Timekeeping vsyscall support
16config GENERIC_TIME_VSYSCALL 21config GENERIC_TIME_VSYSCALL
17 bool 22 bool
diff --git a/kernel/time/timekeeping_internal.h b/kernel/time/timekeeping_internal.h
index 05dfa6b25dc4..4ea005a7f9da 100644
--- a/kernel/time/timekeeping_internal.h
+++ b/kernel/time/timekeeping_internal.h
@@ -12,9 +12,18 @@ extern void tk_debug_account_sleep_time(struct timespec64 *t);
12#define tk_debug_account_sleep_time(x) 12#define tk_debug_account_sleep_time(x)
13#endif 13#endif
14 14
15#ifdef CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE
16static inline cycle_t clocksource_delta(cycle_t now, cycle_t last, cycle_t mask)
17{
18 cycle_t ret = (now - last) & mask;
19
20 return (s64) ret > 0 ? ret : 0;
21}
22#else
15static inline cycle_t clocksource_delta(cycle_t now, cycle_t last, cycle_t mask) 23static inline cycle_t clocksource_delta(cycle_t now, cycle_t last, cycle_t mask)
16{ 24{
17 return (now - last) & mask; 25 return (now - last) & mask;
18} 26}
27#endif
19 28
20#endif /* _TIMEKEEPING_INTERNAL_H */ 29#endif /* _TIMEKEEPING_INTERNAL_H */