// SPDX-License-Identifier: GPL-2.0-only /* * sched_clock() for unstable CPU clocks * * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra * * Updates and enhancements: * Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <[email protected]> * * Based on code by: * Ingo Molnar <[email protected]> * Guillaume Chazarain <[email protected]> * * * What this file implements: * * cpu_clock(i) provides a fast (execution time) high resolution * clock with bounded drift between CPUs. The value of cpu_clock(i) * is monotonic for constant i. The timestamp returned is in nanoseconds. * * ######################### BIG FAT WARNING ########################## * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can # * # go backwards !! # * #################################################################### * * There is no strict promise about the base, although it tends to start * at 0 on boot (but people really shouldn't rely on that). * * cpu_clock(i) -- can be used from any context, including NMI. * local_clock() -- is cpu_clock() on the current CPU. * * sched_clock_cpu(i) * * How it is implemented: * * The implementation either uses sched_clock() when * !CONFIG_HAVE_UNSTABLE_SCHED_CLOCK, which means in that case the * sched_clock() is assumed to provide these properties (mostly it means * the architecture provides a globally synchronized highres time source). * * Otherwise it tries to create a semi stable clock from a mixture of other * clocks, including: * * - GTOD (clock monotonic) * - sched_clock() * - explicit idle events * * We use GTOD as base and use sched_clock() deltas to improve resolution. The * deltas are filtered to provide monotonicity and keeping it within an * expected window. * * Furthermore, explicit sleep and wakeup hooks allow us to account for time * that is otherwise invisible (TSC gets stopped). * */ /* * Scheduler clock - returns current time in nanosec units. * This is default implementation. * Architectures and sub-architectures can override this. */ notrace unsigned long long __weak sched_clock(void) { … } EXPORT_SYMBOL_GPL(…); static DEFINE_STATIC_KEY_FALSE(sched_clock_running); #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 = …; /* * We want: ktime_get_ns() + __gtod_offset == sched_clock() + __sched_clock_offset */ __read_mostly u64 __sched_clock_offset; static __read_mostly u64 __gtod_offset; struct sched_clock_data { … }; static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data); static __always_inline struct sched_clock_data *this_scd(void) { … } notrace static inline struct sched_clock_data *cpu_sdc(int cpu) { … } notrace int sched_clock_stable(void) { … } notrace static void __scd_stamp(struct sched_clock_data *scd) { … } notrace static void __set_sched_clock_stable(void) { … } /* * If we ever get here, we're screwed, because we found out -- typically after * the fact -- that TSC wasn't good. This means all our clocksources (including * ktime) could have reported wrong values. * * What we do here is an attempt to fix up and continue sort of where we left * off in a coherent manner. * * The only way to fully avoid random clock jumps is to boot with: * "tsc=unstable". */ notrace static void __sched_clock_work(struct work_struct *work) { … } static DECLARE_WORK(sched_clock_work, __sched_clock_work); notrace static void __clear_sched_clock_stable(void) { … } notrace void clear_sched_clock_stable(void) { … } notrace static void __sched_clock_gtod_offset(void) { … } void __init sched_clock_init(void) { … } /* * We run this as late_initcall() such that it runs after all built-in drivers, * notably: acpi_processor and intel_idle, which can mark the TSC as unstable. */ static int __init sched_clock_init_late(void) { … } late_initcall(sched_clock_init_late); /* * min, max except they take wrapping into account */ static __always_inline u64 wrap_min(u64 x, u64 y) { … } static __always_inline u64 wrap_max(u64 x, u64 y) { … } /* * update the percpu scd from the raw @now value * * - filter out backward motion * - use the GTOD tick value to create a window to filter crazy TSC values */ static __always_inline u64 sched_clock_local(struct sched_clock_data *scd) { … } noinstr u64 local_clock_noinstr(void) { … } u64 local_clock(void) { … } EXPORT_SYMBOL_GPL(…); static notrace u64 sched_clock_remote(struct sched_clock_data *scd) { … } /* * Similar to cpu_clock(), but requires local IRQs to be disabled. * * See cpu_clock(). */ notrace u64 sched_clock_cpu(int cpu) { … } EXPORT_SYMBOL_GPL(…); notrace void sched_clock_tick(void) { … } notrace void sched_clock_tick_stable(void) { … } /* * We are going deep-idle (IRQs are disabled): */ notrace void sched_clock_idle_sleep_event(void) { … } EXPORT_SYMBOL_GPL(…); /* * We just idled; resync with ktime. */ notrace void sched_clock_idle_wakeup_event(void) { … } EXPORT_SYMBOL_GPL(…); #else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */ void __init sched_clock_init(void) { static_branch_inc(&sched_clock_running); local_irq_disable(); generic_sched_clock_init(); local_irq_enable(); } notrace u64 sched_clock_cpu(int cpu) { if (!static_branch_likely(&sched_clock_running)) return 0; return sched_clock(); } #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */ /* * Running clock - returns the time that has elapsed while a guest has been * running. * On a guest this value should be local_clock minus the time the guest was * suspended by the hypervisor (for any reason). * On bare metal this function should return the same as local_clock. * Architectures and sub-architectures can override this. */ notrace u64 __weak running_clock(void) { … }