timekeeping: 2038, optimizations, NMI safe accessors
Subsystems (38)
v2 cover timekeeping: 2038, optimizations, NMI safe accessors
Changes versus V1 [http://lkml.kernel.org/r/[email protected]]
- Addressed review comments and testing fallout
- Simplified update of the ktime_t data in the timekeeper
- Replaced some more interfaces which are availabe as timespec and ktime_t by the ktime_t version to get rid of the duplicated data.
- Restructured the timekeeper again to share code with the NMI safe accessor, which simplifies the update to pure memcpy().
- Removed the monotonic raw fast accessor
I also verified the size impact of the whole endavour:
x86_64:
text data bss dec hex filename
before:
992378 104211 454784 1551373 17ac0d ../build/kernel/built-in.o
2038 changes only:
992312 104211 454784 1551307 17abcb ../build/kernel/built-in.o
full series applied:
991724 104210 454976 1550910 17aa3e ../build/kernel/built-in.o
i386:
text data bss dec hex filename
before:
910278 56201 372436 1338915 146e23 ../build-i386/kernel/built-in.o
2038 changes only:
912381 56201 372468 1341050 14767a ../build-i386/kernel/built-in.o
full series applied:
911607 56200 372596 1340403 1473f3 ../build-i386/kernel/built-in.o
arm:
text data bss dec hex filename
before:
443444 58965 7813144 8315553 7ee2a1 ../build-arm/kernel/built-in.o
2038 changes only:
445205 58965 7813176 8317346 7ee9a2 ../build-arm/kernel/built-in.o
full series applied:
444708 58964 7813368 8317040 7ee870 ../build-arm/kernel/built-in.o
So on x8664 the new functionality comes with smaller size. I whish I could say the same for 32bit, but there we pay the prize for making the core timekeeping code 2038 ready. But also there, while the 2038 stuff adds ~2k, the rest (cleanup and the new monotonic accessor) removes ~0.5k again. So features are not necessarily bundled with bloat :)
The series is against tip/timers/core. It's also available in git at:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git tglx/timers/core
Thanks,
tglx
----
arch/arm/Kconfig | 1 arch/arm/common/bL_switcher.c | 16 arch/arm64/kernel/vdso.c | 10 arch/hexagon/Kconfig | 1 arch/ia64/kernel/time.c | 4 arch/powerpc/kernel/time.c | 4 arch/powerpc/platforms/cell/spu_base.c | 11 arch/powerpc/platforms/cell/spufs/context.c | 4 arch/powerpc/platforms/cell/spufs/file.c | 4 arch/powerpc/platforms/cell/spufs/sched.c | 4 arch/s390/Kconfig | 1 arch/s390/kernel/time.c | 16 arch/tile/kernel/time.c | 13 arch/tile/kernel/vdso/vgettimeofday.c | 7 arch/x86/Kconfig | 2 arch/x86/kernel/tsc.c | 21 arch/x86/kernel/vsyscall_gtod.c | 23 arch/x86/kvm/x86.c | 62 - drivers/char/hangcheck-timer.c | 33 drivers/connector/cn_proc.c | 36 drivers/gpu/drm/drm_irq.c | 16 drivers/gpu/drm/i915/i915_drv.h | 2 drivers/gpu/drm/i915/i915_gem.c | 33 drivers/gpu/drm/i915/intel_pm.c | 12 drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 4 drivers/gpu/drm/vmwgfx/vmwgfx_marker.c | 44 - drivers/hwmon/ibmaem.c | 6 drivers/input/evdev.c | 7 drivers/mfd/cros_ec_spi.c | 8 drivers/misc/ioc4.c | 7 drivers/net/ethernet/mellanox/mlx5/core/cmd.c | 16 drivers/net/wireless/ath/ath9k/hw.c | 7 fs/lockd/mon.c | 4 fs/proc/array.c | 7 fs/timerfd.c | 6 include/linux/clocksource.h | 2 include/linux/hrtimer.h | 14 include/linux/iio/iio.h | 9 include/linux/ktime.h | 228 +----- include/linux/mlx5/driver.h | 4 include/linux/sched.h | 8 include/linux/seqlock.h | 27 include/linux/time.h | 65 - include/linux/time64.h | 191 +++++ include/linux/timekeeper_internal.h | 144 ++- include/linux/timekeeping.h | 209 +++++ kernel/acct.c | 10 kernel/delayacct.c | 52 - kernel/fork.c | 5 kernel/time/Kconfig | 9 kernel/time/clocksource.c | 12 kernel/time/hrtimer.c | 80 -- kernel/time/ntp.c | 15 kernel/time/ntp_internal.h | 2 kernel/time/posix-timers.c | 2 kernel/time/tick-internal.h | 2 kernel/time/time.c | 64 + kernel/time/timekeeping.c | 957 ++++++++++++++------------ kernel/time/timekeeping.h | 20 kernel/time/timekeeping_debug.c | 2 kernel/time/timekeeping_internal.h | 17 kernel/trace/trace.c | 11 kernel/tsacct.c | 19 63 files changed, 1412 insertions(+), 1220 deletions(-)
PATCH v2 1/64 tile: Convert VDSO timekeeping to the precise mechanism
The code was only halfarsed converted to the new VSDO update mechanism and still uses the inaccurate base value which lacks the fractional part of xtime_nsec. Fix it up.
--- arch/tile/kernel/time.c | 9 ++++----- arch/tile/kernel/vdso/vgettimeofday.c | 7 ++++--- 2 files changed, 8 insertions(+), 8 deletions(-)
arch/tile/kernel/time.c +4 -5
@@ -260,7 +260,6 @@ void update_vsyscall_tz(void)void update_vsyscall (struct timekeeper *tk) { -struct timespec wall_time =tk_xtime (tk);struct timespec *wtm = &tk->wall_to_monotonic;struct clocksource *clock = tk->clock;
@@ -271,12 +270,12 @@ void update_vsyscall(struct timekeeper * ++vdso_data->tb_update_count;smp_wmb (); vdso_data->xtime_tod_stamp = clock->cycle_last; - vdso_data->xtime_clock_sec = wall_time.tv_sec; - vdso_data->xtime_clock_nsec = wall_time.tv_nsec; + vdso_data->xtime_clock_sec = tk->xtime_sec; + vdso_data->xtime_clock_nsec = tk->xtime_nsec; vdso_data->wtom_clock_sec = wtm->tv_sec; vdso_data->wtom_clock_nsec = wtm->tv_nsec; - vdso_data->mult = clock->mult; - vdso_data->shift = clock->shift; + vdso_data->mult = tk->mult; + vdso_data->shift = tk->shift;smp_wmb (); ++vdso_data->tb_update_count; }
arch/tile/kernel/vdso/vgettimeofday.c +4 -3
@@ -83,10 +83,11 @@ int __vdso_gettimeofday(struct timeval *if (count &1 )continue ; - cycles = (get_cycles () - vdso_data->xtime_tod_stamp); - ns = (cycles * vdso_data->mult) >> vdso_data->shift; sec = vdso_data->xtime_clock_sec; - ns += vdso_data->xtime_clock_nsec; + cycles =get_cycles () - vdso_data->xtime_tod_stamp; + ns = (cycles * vdso_data->mult) + vdso_data->xtime_clock_nsec; + ns >>= vdso_data->shift; +if (ns >=NSEC_PER_SEC ) { ns -=NSEC_PER_SEC ; sec +=1 ;
PATCH v2 2/64 timekeeping: Simplify arch_gettimeoffset()
In reply to Thomas GleixnerProvide a default stub function instead of having the extra conditional. Cuts binary size on a m68k build by ~100 bytes.
--- kernel/time/timekeeping.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
kernel/time/timekeeping.c +6 -12
@@ -153,16 +153,10 @@ static void tk_setup_internals(struct ti/* Timekeeper helper functions. */ #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET -u32 (*arch_gettimeoffset)(void); - -u32 get_arch_timeoffset (void ) -{ -if (likely (arch_gettimeoffset)) -return arch_gettimeoffset (); -return 0 ; -} +static u32 default_arch_gettimeoffset (void ) {return 0 ; } +u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;#else -static inline u32 get_arch_timeoffset (void ) {return 0 ; } +static inline u32 arch_gettimeoffset (void ) {return 0 ; }#endif static inline s64 timekeeping_get_ns (struct timekeeper *tk)
@@ -182,7 +176,7 @@ static inline s64 timekeeping_get_ns(str nsec >>= tk->shift;/* If arch requires, add in get_arch_timeoffset() */ -return nsec +get_arch_timeoffset (); +return nsec +arch_gettimeoffset (); }static inline s64 timekeeping_get_ns_raw (struct timekeeper *tk)
@@ -202,7 +196,7 @@ static inline s64 timekeeping_get_ns_raw nsec =clocksource_cyc2ns (cycle_delta, clock->mult, clock->shift);/* If arch requires, add in get_arch_timeoffset() */ -return nsec +get_arch_timeoffset (); +return nsec +arch_gettimeoffset (); }static RAW_NOTIFIER_HEAD (pvclock_gtod_chain);
@@ -282,7 +276,7 @@ static void timekeeping_forward_now(stru tk->xtime_nsec += cycle_delta * tk->mult;/* If arch requires, add in get_arch_timeoffset() */ - tk->xtime_nsec += (u64 )get_arch_timeoffset () << tk->shift; + tk->xtime_nsec += (u64 )arch_gettimeoffset () << tk->shift;tk_normalize_xtime (tk);
PATCH v2 3/64 hrtimer: Cleanup hrtimer accessors to the timekepeing state
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
Rather then having two similar but totally different implementations that provide timekeeping state to the hrtimer code, try to unify the two implementations to be more simliar.
Thus this clarifies ktime_get_update_offsets to ktime_get_update_offsets_now and changes get_xtime... to ktime_get_update_offsets_tick.
--- include/linux/hrtimer.h | 9 ++++++--- include/linux/time.h | 2 -- kernel/time/hrtimer.c | 23 ++++++++++------------- kernel/time/timekeeping.c | 36 +++++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 31 deletions(-)
include/linux/hrtimer.h +6 -3
@@ -331,9 +331,12 @@ extern ktime_t ktime_get_real(void);extern ktime_t ktime_get_boottime (void );extern ktime_t ktime_get_monotonic_offset (void );extern ktime_t ktime_get_clocktai (void ); -extern ktime_t ktime_get_update_offsets (ktime_t *offs_real,ktime_t *offs_boot, -ktime_t *offs_tai); - +extern ktime_t ktime_get_update_offsets_tick (ktime_t *offs_real, +ktime_t *offs_boot, +ktime_t *offs_tai); +extern ktime_t ktime_get_update_offsets_now (ktime_t *offs_real, +ktime_t *offs_boot, +ktime_t *offs_tai); DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
include/linux/time.h +0 -2
@@ -133,8 +133,6 @@ unsigned long get_seconds(void);struct timespec current_kernel_time (void );struct timespec __current_kernel_time (void );/* does not take xtime_lock */ struct timespec get_monotonic_coarse (void ); -void get_xtime_and_monotonic_and_sleep_offset (struct timespec *xtim, -struct timespec *wtom,struct timespec *sleep);void timekeeping_inject_sleeptime (struct timespec *delta);#define CURRENT_TIME (current_kernel_time())
kernel/time/hrtimer.c +10 -13
@@ -114,21 +114,18 @@ static inline int hrtimer_clockid_to_bas*/ static void hrtimer_get_softirq_time (struct hrtimer_cpu_base *base) { -ktime_t xtim, mono, boot; -struct timespec xts, tom, slp; -s32 tai_offset; - -get_xtime_and_monotonic_and_sleep_offset (&xts, &tom, &slp); - tai_offset =timekeeping_get_tai_offset (); - - xtim =timespec_to_ktime (xts); - mono =ktime_add (xtim,timespec_to_ktime (tom)); - boot =ktime_add (mono,timespec_to_ktime (slp)); +ktime_t xtim, mono, boot, tai; +ktime_t off_real, off_boot, off_tai; + + mono =ktime_get_update_offsets_tick (&off_real, &off_boot, &off_tai); + boot =ktime_add (mono, off_boot); + xtim =ktime_add (mono, off_real); + tai =ktime_add (xtim, off_tai); + base->clock_base[HRTIMER_BASE_REALTIME ].softirq_time = xtim; base->clock_base[HRTIMER_BASE_MONOTONIC ].softirq_time = mono; base->clock_base[HRTIMER_BASE_BOOTTIME ].softirq_time = boot; - base->clock_base[HRTIMER_BASE_TAI ].softirq_time = -ktime_add (xtim,ktime_set (tai_offset,0 )); + base->clock_base[HRTIMER_BASE_TAI ].softirq_time = tai; }/*
@@ -673,7 +670,7 @@ static inline ktime_t hrtimer_update_basktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME ].offset;ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI ].offset; -return ktime_get_update_offsets (offs_real, offs_boot, offs_tai); +return ktime_get_update_offsets_now (offs_real, offs_boot, offs_tai); }/*
kernel/time/timekeeping.c +23 -13
@@ -1581,29 +1581,39 @@ void do_timer(unsigned long ticks) }/** -* get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic, -* and sleep offsets. -* @xtim: pointer to timespec to be set with xtime -* @wtom: pointer to timespec to be set with wall_to_monotonic -* @sleep: pointer to timespec to be set with time in suspend +* ktime_get_update_offsets_tick - hrtimer helper +* @offs_real: pointer to storage for monotonic -> realtime offset +* @offs_boot: pointer to storage for monotonic -> boottime offset +* @offs_tai: pointer to storage for monotonic -> clock tai offset +* +* Returns monotonic time at last tick and various offsets */ -void get_xtime_and_monotonic_and_sleep_offset (struct timespec *xtim, -struct timespec *wtom,struct timespec *sleep) +ktime_t ktime_get_update_offsets_tick (ktime_t *offs_real,ktime_t *offs_boot, +ktime_t *offs_tai) {struct timekeeper *tk = &timekeeper; -unsigned long seq; +struct timespec ts; +ktime_t now; +unsigned int seq;do { seq =read_seqcount_begin (&timekeeper_seq); - *xtim =tk_xtime (tk); - *wtom = tk->wall_to_monotonic; - *sleep = tk->total_sleep_time; + + ts =tk_xtime (tk); + + *offs_real = tk->offs_real; + *offs_boot = tk->offs_boot; + *offs_tai = tk->offs_tai; }while (read_seqcount_retry (&timekeeper_seq, seq)); + + now =ktime_set (ts.tv_sec, ts.tv_nsec); + now =ktime_sub (now, *offs_real); +return now; }#ifdef CONFIG_HIGH_RES_TIMERS /** -* ktime_get_update_offsets - hrtimer helper +* ktime_get_update_offsets_now - hrtimer helper * @offs_real: pointer to storage for monotonic -> realtime offset * @offs_boot: pointer to storage for monotonic -> boottime offset * @offs_tai: pointer to storage for monotonic -> clock tai offset
@@ -1611,7 +1621,7 @@ void get_xtime_and_monotonic_and_sleep_o* Returns current monotonic time and updates the offsets * Called from hrtimer_interrupt() or retrigger_next_event() */ -ktime_t ktime_get_update_offsets (ktime_t *offs_real,ktime_t *offs_boot, +ktime_t ktime_get_update_offsets_now (ktime_t *offs_real,ktime_t *offs_boot,ktime_t *offs_tai) {struct timekeeper *tk = &timekeeper;
PATCH v2 4/64 ktime: Kill non-scalar ktime_t implementation for 2038
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
The non-scalar ktime_t implementation is basically a timespec which has to be changed to support dates past 2038 on 32bit systems.
This patch removes the non-scalar ktime_t implementation, forcing the scalar s64 nanosecond version on all architectures.
This may have additional performance overhead on some 32bit systems when converting between ktime_t and timespec structures, however the majority of 32bit systems (arm and i386) were already using scalar ktime_t, so no performance regressions will be seen on those platforms.
On affected platforms, I'm open to finding optimizations, including avoiding converting to timespecs where possible.
[ tglx: We can now cleanup the ktime_t.tv64 mess, but thats a
different issue and we can throw a coccinelle script at it ]
--- arch/arm/Kconfig | 1 arch/hexagon/Kconfig | 1 arch/s390/Kconfig | 1 arch/x86/Kconfig | 1 include/linux/ktime.h | 173 ---------------------------------------------- include/linux/time.h | 11 +- kernel/time/Kconfig | 4 - kernel/time/hrtimer.c | 54 -------------- kernel/time/timekeeping.c | 7 - 9 files changed, 7 insertions(+), 246 deletions(-)
arch/arm/Kconfig +0 -1
@@ -64,7 +64,6 @@ config ARM select HAVE_UID16 select HAVE_VIRT_CPU_ACCOUNTING_GEN select IRQ_FORCED_THREADING - select KTIME_SCALAR select MODULES_USE_ELF_REL select NO_BOOTMEM select OLD_SIGACTION
arch/hexagon/Kconfig +0 -1
@@ -23,7 +23,6 @@ config HEXAGON select GENERIC_IOMAP select GENERIC_SMP_IDLE_THREAD select STACKTRACE_SUPPORT - select KTIME_SCALAR select GENERIC_CLOCKEVENTS select GENERIC_CLOCKEVENTS_BROADCAST select MODULES_USE_ELF_RELA
arch/s390/Kconfig +0 -1
@@ -137,7 +137,6 @@ config S390 select HAVE_SYSCALL_TRACEPOINTS select HAVE_UID16 if 32BIT select HAVE_VIRT_CPU_ACCOUNTING - select KTIME_SCALAR if 32BIT select MODULES_USE_ELF_RELA select NO_BOOTMEM select OLD_SIGACTION
arch/x86/Kconfig +0 -1
@@ -111,7 +111,6 @@ config X86 select ARCH_CLOCKSOURCE_DATA select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC) select GENERIC_TIME_VSYSCALL - select KTIME_SCALAR if X86_32 select GENERIC_STRNCPY_FROM_USER select GENERIC_STRNLEN_USER select HAVE_CONTEXT_TRACKING if X86_64
include/linux/ktime.h +1 -172
197 lines not on this page · raw message
include/linux/time.h +4 -7
@@ -19,6 +19,10 @@ extern struct timezone sys_tz;#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) +/* Located here for timespec_valid_strict */ +#define KTIME_MAX ((s64)~((u64)1 << 63)) +#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) +static inline int timespec_equal (const struct timespec *a,const struct timespec *b) {
@@ -84,13 +88,6 @@ static inline struct timespec timespec_sreturn ts_delta; } -#define KTIME_MAX ((s64)~((u64)1 << 63)) -#if (BITS_PER_LONG ==64 ) -# define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) -#else -# define KTIME_SEC_MAX LONG_MAX -#endif -/* * Returns true if the timespec is norm, false if denorm: */
kernel/time/Kconfig +0 -4
@@ -20,10 +20,6 @@ config GENERIC_TIME_VSYSCALL config GENERIC_TIME_VSYSCALL_OLD bool -# ktime_t scalar 64bit nsec representation -config KTIME_SCALAR - bool - # Old style timekeeping config ARCH_USES_GETTIMEOFFSET bool
kernel/time/hrtimer.c +0 -54
60 lines not on this page · raw message
kernel/time/timekeeping.c +2 -5
@@ -344,11 +344,8 @@ ktime_t ktime_get(void) nsecs =timekeeping_get_ns (tk) + tk->wall_to_monotonic.tv_nsec; }while (read_seqcount_retry (&timekeeper_seq, seq)); -/* -* Use ktime_set/ktime_add_ns to create a proper ktime on -* 32-bit architectures without CONFIG_KTIME_SCALAR. -*/ -return ktime_add_ns (ktime_set (secs,0 ), nsecs); + +return ktime_set (secs, nsecs); }EXPORT_SYMBOL_GPL (ktime_get);
PATCH v2 5/64 ktime: Sanitize ktime_to_us/ms conversion
In reply to Thomas GleixnerWith the plain nanoseconds based ktime_t we can simply use ktime_divns() instead of going through loops and hoops of timespec/timeval conversion.
--- include/linux/hrtimer.h | 6 ------ include/linux/ktime.h | 12 ++++++++---- kernel/time/hrtimer.c | 1 + 3 files changed, 9 insertions(+), 10 deletions(-)
include/linux/hrtimer.h +0 -6
@@ -457,12 +457,6 @@ extern void hrtimer_run_pending(void);/* Bootup initialization: */ extern void __init hrtimers_init (void ); -#if BITS_PER_LONG <64 -extern u64 ktime_divns (const ktime_t kt,s64 div); -#else /* BITS_PER_LONG < 64 */ -# define ktime_divns (kt, div) (u64)((kt).tv64 / (div)) -#endif -/* Show pending timers: */ extern void sysrq_timer_list_show (void );
include/linux/ktime.h +8 -4
@@ -157,16 +157,20 @@ static inline bool ktime_before(const ktreturn ktime_compare (cmp1, cmp2) <0 ; } +#if BITS_PER_LONG <64 +extern u64 ktime_divns (const ktime_t kt,s64 div); +#else /* BITS_PER_LONG < 64 */ +# define ktime_divns (kt, div) (u64)((kt).tv64 / (div)) +#endif +static inline s64 ktime_to_us (const ktime_t kt) { -struct timeval tv =ktime_to_timeval (kt); -return (s64 ) tv.tv_sec *USEC_PER_SEC + tv.tv_usec; +return ktime_divns (kt,NSEC_PER_USEC ); }static inline s64 ktime_to_ms (const ktime_t kt) { -struct timeval tv =ktime_to_timeval (kt); -return (s64 ) tv.tv_sec *MSEC_PER_SEC + tv.tv_usec /USEC_PER_MSEC ; +return ktime_divns (kt,NSEC_PER_MSEC ); }static inline s64 ktime_us_delta (const ktime_t later,const ktime_t earlier)
kernel/time/hrtimer.c +1 -0
@@ -280,6 +280,7 @@ u64 ktime_divns(const ktime_t kt, s64 di return dclc; } +EXPORT_SYMBOL_GPL (ktime_divns);#endif /* BITS_PER_LONG >= 64 */ /*
PATCH v2 6/64 ktime: Change ktime_set() to take 64bit seconds value
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
In order to support dates past 2038 on 32bit systems, ktime_set() needs to handle 64bit second values.
[ tglx: Removed the BITS_PER_LONG check ]
--- include/linux/ktime.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
include/linux/ktime.h +3 -4
@@ -47,13 +47,12 @@ typedef union ktime ktime_t; /* Kill th* * Return: The ktime_t representation of the value. */ -static inline ktime_t ktime_set (const long secs,const unsigned long nsecs) +static inline ktime_t ktime_set (const s64 secs,const unsigned long nsecs) { -#if (BITS_PER_LONG ==64 )if (unlikely (secs >=KTIME_SEC_MAX ))return (ktime_t ){ .tv64 =KTIME_MAX }; -#endif -return (ktime_t ) { .tv64 = (s64 )secs *NSEC_PER_SEC + (s64 )nsecs }; + +return (ktime_t ) { .tv64 = secs *NSEC_PER_SEC + (s64 )nsecs }; }/* Subtract two ktime_t variables. rem = lhs -rhs: */
PATCH v2 7/64 time64: Add time64.h header and define struct timespec64
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
Define the timespec64 structure and standard helper functions.
[ tglx: Make it 32bit only. 64bit really can map timespec to timespec64 ]
--- include/linux/time.h | 15 ---- include/linux/time64.h | 163 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 14 deletions(-)
include/linux/time.h +1 -14
@@ -4,25 +4,12 @@# include <linux/cache.h> # include <linux/seqlock.h> # include <linux/math64.h> -#include <uapi/linux/time.h> +# include <linux/time64.h> extern struct timezone sys_tz; -/* Parameters used to convert the timespec values: */ -#define MSEC_PER_SEC 1000L -#define USEC_PER_MSEC 1000L -#define NSEC_PER_USEC 1000L -#define NSEC_PER_MSEC 1000000L -#define USEC_PER_SEC 1000000L -#define NSEC_PER_SEC 1000000000L -#define FSEC_PER_SEC 1000000000000000LL -#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) -/* Located here for timespec_valid_strict */ -#define KTIME_MAX ((s64)~((u64)1 << 63)) -#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) -static inline int timespec_equal (const struct timespec *a,const struct timespec *b) {
include/linux/time64.h new file +163 -0
163 lines not on this page · raw message
PATCH v2 8/64 time: More core infrastructure for timespec64
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
Helper and conversion functions for timespec64.
--- include/linux/ktime.h | 28 ++++++++++++++++++++++ include/linux/time64.h | 28 ++++++++++++++++++++++ kernel/time/time.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+)
include/linux/ktime.h +28 -0
@@ -83,6 +83,12 @@ static inline ktime_t timespec_to_ktime(return ktime_set (ts.tv_sec, ts.tv_nsec); } +/* convert a timespec64 to ktime_t format: */ +static inline ktime_t timespec64_to_ktime (struct timespec64 ts) +{ +return ktime_set (ts.tv_sec, ts.tv_nsec); +} +/* convert a timeval to ktime_t format: */ static inline ktime_t timeval_to_ktime (struct timeval tv) {
@@ -92,6 +98,9 @@ static inline ktime_t timeval_to_ktime(s/* Map the ktime_t to timespec conversion to ns_to_timespec function */ #define ktime_to_timespec (kt) ns_to_timespec((kt).tv64) +/* Map the ktime_t to timespec conversion to ns_to_timespec function */ +#define ktime_to_timespec64 (kt) ns_to_timespec64((kt).tv64) +/* Map the ktime_t to timeval conversion to ns_to_timeval function */ #define ktime_to_timeval (kt) ns_to_timeval((kt).tv64)
@@ -186,6 +195,25 @@ static inline __must_check bool ktime_toreturn true; }else {return false; + } +} + +/** +* ktime_to_timespec64_cond - convert a ktime_t variable to timespec64 +* format only if the variable contains data +* @kt: the ktime_t variable to convert +* @ts: the timespec variable to store the result in +* +* Return: %true if there was a successful conversion, %false if kt was 0. +*/ +static inline __must_check bool ktime_to_timespec64_cond (const ktime_t kt, +struct timespec64 *ts) +{ +if (kt.tv64) { + *ts =ktime_to_timespec64 (kt); +return true; + }else { +return false; } }
include/linux/time64.h +28 -0
@@ -33,6 +33,16 @@ struct timespec64 {#if __BITS_PER_LONG ==64 +static inline struct timespec timespec64_to_timespec (const struct timespec64 ts64) +{ +return ts64; +} + +static inline struct timespec64 timespec_to_timespec64 (const struct timespec ts) +{ +return ts; +} +# define timespec64_equal timespec_equal# define timespec64_compare timespec_compare# define set_normalized_timespec64 set_normalized_timespec
@@ -47,6 +57,24 @@ struct timespec64 {#else +static inline struct timespec timespec64_to_timespec (const struct timespec64 ts64) +{ +struct timespec ret; + + ret.tv_sec = (time_t )ts64.tv_sec; + ret.tv_nsec = ts64.tv_nsec; +return ret; +} + +static inline struct timespec64 timespec_to_timespec64 (const struct timespec ts) +{ +struct timespec64 ret; + + ret.tv_sec = ts.tv_sec; + ret.tv_nsec = ts.tv_nsec; +return ret; +} +static inline int timespec64_equal (const struct timespec64 *a,const struct timespec64 *b) {
kernel/time/time.c +62 -0
68 lines not on this page · raw message
PATCH v2 9/64 timekeeping: Convert timekeeping core to use timespec64s
In reply to Thomas GleixnerFrom: John Stultz <[email protected]>
Convert the core timekeeping logic to use timespec64s. This moves the 2038 issues out of the core logic and into all of the accessor functions.
Future changes will need to push the timespec64s out to all timekeeping users, but that can be done interface by interface.
--- include/linux/timekeeper_internal.h | 10 +- kernel/time/ntp.c | 8 - kernel/time/ntp_internal.h | 2 kernel/time/timekeeping.c | 172 ++++++++++++++++++++---------------- kernel/time/timekeeping_debug.c | 2 kernel/time/timekeeping_internal.h | 2 6 files changed, 109 insertions(+), 87 deletions(-)
include/linux/timekeeper_internal.h +5 -5
@@ -55,15 +55,15 @@ struct timekeeper {* - wall_to_monotonic is no longer the boot time, getboottime must be * used instead. */ -struct timespec wall_to_monotonic; +struct timespec64 wall_to_monotonic;/* Offset clock monotonic -> clock realtime */ ktime_t offs_real;/* time spent in suspend */ -struct timespec total_sleep_time; +struct timespec64 total_sleep_time;/* Offset clock monotonic -> clock boottime */ ktime_t offs_boot;/* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ -struct timespec raw_time; +struct timespec64 raw_time;/* The current UTC to TAI offset in seconds */ s32 tai_offset;/* Offset clock monotonic -> clock tai */
@@ -71,9 +71,9 @@ struct timekeeper { }; -static inline struct timespec tk_xtime (struct timekeeper *tk) +static inline struct timespec64 tk_xtime (struct timekeeper *tk) { -struct timespec ts; +struct timespec64 ts; ts.tv_sec = tk->xtime_sec; ts.tv_nsec = (long )(tk->xtime_nsec >> tk->shift);
kernel/time/ntp.c +4 -4
@@ -531,7 +531,7 @@ void ntp_notify_cmos_timer(void) { }/* * Propagate a new txc->status value into the NTP state: */ -static inline void process_adj_status (struct timex *txc,struct timespec *ts) +static inline void process_adj_status (struct timex *txc,struct timespec64 *ts) {if ((time_status &STA_PLL ) && !(txc->status &STA_PLL )) { time_state =TIME_OK ;
@@ -554,7 +554,7 @@ static inline void process_adj_status(ststatic inline void process_adjtimex_modes (struct timex *txc, -struct timespec *ts, +struct timespec64 *ts,s32 *time_tai) {if (txc->modes &ADJ_STATUS )
@@ -640,7 +640,7 @@ int ntp_validate_timex(struct timex *txc* adjtimex mainly allows reading (and writing, if superuser) of * kernel time-keeping variables. used by xntpd. */ -int __do_adjtimex (struct timex *txc,struct timespec *ts,s32 *time_tai) +int __do_adjtimex (struct timex *txc,struct timespec64 *ts,s32 *time_tai) {int result;
@@ -684,7 +684,7 @@ int __do_adjtimex(struct timex *txc, str/* fill PPS status fields */ pps_fill_timex (txc); - txc->time.tv_sec = ts->tv_sec; + txc->time.tv_sec = (time_t )ts->tv_sec; txc->time.tv_usec = ts->tv_nsec;if (!(time_status &STA_NANO )) txc->time.tv_usec /=NSEC_PER_USEC ;
kernel/time/ntp_internal.h +1 -1
@@ -7,6 +7,6 @@ extern void ntp_clear(void);extern u64 ntp_tick_length (void );extern int second_overflow (unsigned long secs);extern int ntp_validate_timex (struct timex *); -extern int __do_adjtimex (struct timex *,struct timespec *,s32 *); +extern int __do_adjtimex (struct timex *,struct timespec64 *,s32 *);extern void __hardpps (const struct timespec *,const struct timespec *);#endif /* _LINUX_NTP_INTERNAL_H */
kernel/time/timekeeping.c +97 -75
486 lines not on this page · raw message
kernel/time/timekeeping_debug.c +1 -1
@@ -67,7 +67,7 @@ static int __init tk_debug_sleep_time_in }late_initcall (tk_debug_sleep_time_init); -void tk_debug_account_sleep_time (struct timespec *t) +void tk_debug_account_sleep_time (struct timespec64 *t) { sleep_time_bin[fls (t->tv_sec)]++; }
kernel/time/timekeeping_internal.h +1 -1
@@ -6,7 +6,7 @@#include <linux/time.h> #ifdef CONFIG_DEBUG_FS -extern void tk_debug_account_sleep_time (struct timespec *t); +extern void tk_debug_account_sleep_time (struct timespec64 *t);#else #define tk_debug_account_sleep_time (x)#endif
PATCH v2 10/64 time: Consolidate the time accessor prototypes
In reply to Thomas GleixnerRight now we have time related prototypes in 3 different header files. Move it to a single timekeeping header file and move the core internal stuff into a core private header.
--- include/linux/hrtimer.h | 11 ------ include/linux/ktime.h | 8 +--- include/linux/time.h | 45 ++----------------------- include/linux/timekeeping.h | 78 ++++++++++++++++++++++++++++++++++++++++++++ kernel/time/hrtimer.c | 2 + kernel/time/posix-timers.c | 2 + kernel/time/tick-internal.h | 2 + kernel/time/time.c | 1 kernel/time/timekeeping.h | 20 +++++++++++ 9 files changed, 111 insertions(+), 58 deletions(-)
include/linux/hrtimer.h +0 -11
@@ -326,17 +326,6 @@ static inline void timerfd_clock_was_set#endif extern void hrtimers_resume (void ); -extern ktime_t ktime_get (void ); -extern ktime_t ktime_get_real (void ); -extern ktime_t ktime_get_boottime (void ); -extern ktime_t ktime_get_monotonic_offset (void ); -extern ktime_t ktime_get_clocktai (void ); -extern ktime_t ktime_get_update_offsets_tick (ktime_t *offs_real, -ktime_t *offs_boot, -ktime_t *offs_tai); -extern ktime_t ktime_get_update_offsets_now (ktime_t *offs_real, -ktime_t *offs_boot, -ktime_t *offs_tai); DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
include/linux/ktime.h +2 -6
@@ -250,12 +250,6 @@ static inline __must_check bool ktime_to#define LOW_RES_NSEC TICK_NSEC#define KTIME_LOW_RES (ktime_t){ .tv64 = LOW_RES_NSEC } -/* Get the monotonic time in timespec format: */ -extern void ktime_get_ts (struct timespec *ts); - -/* Get the real (wall-) time in timespec format: */ -#define ktime_get_real_ts (ts) getnstimeofday(ts) -static inline ktime_t ns_to_ktime (u64 ns) {static const ktime_t ktime_zero = { .tv64 =0 };
@@ -270,4 +264,6 @@ static inline ktime_t ms_to_ktime(u64 ms return ktime_add_ms(ktime_zero, ms); } +# include <linux/timekeeping.h> +#endif
include/linux/time.h +4 -41
62 lines not on this page · raw message
include/linux/timekeeping.h new file +78 -0
78 lines not on this page · raw message
kernel/time/hrtimer.c +2 -0
@@ -54,6 +54,8 @@#include <trace/events/timer.h> +#include "timekeeping.h" +/* * The timer bases: *
kernel/time/posix-timers.c +2 -0
@@ -49,6 +49,8 @@#include <linux/export.h> #include <linux/hashtable.h> +#include "timekeeping.h" +/* * Management arrays for POSIX timers. Timers are now kept in static hash table * with 512 entries.
kernel/time/tick-internal.h +2 -0
@@ -4,6 +4,8 @@#include <linux/hrtimer.h> #include <linux/tick.h> +#include "timekeeping.h" +extern seqlock_t jiffies_lock;#define CS_NAME_LEN 32
kernel/time/time.c +1 -0
@@ -42,6 +42,7 @@#include <asm/unistd.h> #include "timeconst.h" +#include "timekeeping.h" /* * The timezone where the local system is located. Used as a default by some
kernel/time/timekeeping.h new file +20 -0
@@ -0,0 +1,20 @@ +#ifndef _KERNEL_TIME_TIMEKEEPING_H +#define _KERNEL_TIME_TIMEKEEPING_H +/* +* Internal interfaces for kernel/time/ +*/ +extern ktime_t ktime_get_update_offsets_tick (ktime_t *offs_real, +ktime_t *offs_boot, +ktime_t *offs_tai); +extern ktime_t ktime_get_update_offsets_now (ktime_t *offs_real, +ktime_t *offs_boot, +ktime_t *offs_tai); + +extern int timekeeping_valid_for_hres (void ); +extern u64 timekeeping_max_deferment (void ); +extern int timekeeping_inject_offset (struct timespec *ts); +extern s32 timekeeping_get_tai_offset (void ); +extern void timekeeping_set_tai_offset (s32 tai_offset); +extern void timekeeping_clocktai (struct timespec *ts); + +#endif
PATCH v2 11/64 timekeeping: Provide timespec64 based interfaces
In reply to Thomas GleixnerTo convert callers of the core code to timespec64 we need to provide the proper interfaces.
--- include/linux/timekeeping.h | 66 ++++++++++++++++++++++++++++++++++++++++---- kernel/time/ntp.c | 7 ++-- kernel/time/timekeeping.c | 47 ++++++++++++++----------------- 3 files changed, 87 insertions(+), 33 deletions(-)
include/linux/timekeeping.h +61 -5
@@ -29,15 +29,71 @@ struct timespec get_monotonic_coarse(voiextern void getrawmonotonic (struct timespec *ts);extern void monotonic_to_bootbased (struct timespec *ts);extern void get_monotonic_boottime (struct timespec *ts); -extern void ktime_get_ts (struct timespec *ts); +extern void ktime_get_ts64 (struct timespec64 *ts); + +extern int __getnstimeofday64 (struct timespec64 *tv); +extern void getnstimeofday64 (struct timespec64 *tv); + +#if BITS_PER_LONG ==64 +static inline int __getnstimeofday (struct timespec *ts) +{ +return __getnstimeofday64 (ts); +} + +static inline void getnstimeofday (struct timespec *ts) +{ +getnstimeofday64 (ts); +} + +static inline void ktime_get_ts (struct timespec *ts) +{ +ktime_get_ts64 (ts); +} + +static inline void ktime_get_real_ts (struct timespec *ts) +{ +getnstimeofday64 (ts); +} + +#else +static inline int __getnstimeofday (struct timespec *ts) +{ +struct timespec64 ts64; +int ret =__getnstimeofday64 (&ts64); + + *ts =timespec64_to_timespec (ts64); +return ret; +} + +static inline void getnstimeofday (struct timespec *ts) +{ +struct timespec64 ts64; + +getnstimeofday64 (&ts64); + *ts =timespec64_to_timespec (ts64); +} + +static inline void ktime_get_ts (struct timespec *ts) +{ +struct timespec64 ts64; + +ktime_get_ts64 (&ts64); + *ts =timespec64_to_timespec (ts64); +} + +static inline void ktime_get_real_ts (struct timespec *ts) +{ +struct timespec64 ts64; + +getnstimeofday64 (&ts64); + *ts =timespec64_to_timespec (ts64); +} +#endif -extern int __getnstimeofday (struct timespec *tv); -extern void getnstimeofday (struct timespec *tv);extern void getboottime (struct timespec *ts);#define do_posix_clock_monotonic_gettime (ts) ktime_get_ts(ts) -#define ktime_get_real_ts (ts) getnstimeofday(ts) - +#define ktime_get_real_ts64 (ts) getnstimeofday64(ts)/* * ktime_t based interfaces
kernel/time/ntp.c +4 -3
@@ -466,7 +466,8 @@ static DECLARE_DELAYED_WORK(sync_cmos_wostatic void sync_cmos_clock (struct work_struct *work) { -struct timespec now, next; +struct timespec64 now; +struct timespec next;int fail =1 ;/*
@@ -485,9 +486,9 @@ static void sync_cmos_clock(struct work_return ; } -getnstimeofday (&now); +getnstimeofday64 (&now);if (abs (now.tv_nsec - (NSEC_PER_SEC /2 )) <= tick_nsec *5 ) { -struct timespec adjust = now; +struct timespec adjust =timespec64_to_timespec (now); fail = -ENODEV ;if (persistent_clock_is_local)
kernel/time/timekeeping.c +22 -25
131 lines not on this page · raw message
PATCH v2 12/64 timekeeper: Move tk_xtime to core code
In reply to Thomas GleixnerNo users outside of the core.
--- include/linux/timekeeper_internal.h | 18 --------- kernel/time/timekeeping.c | 70 ++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 45 deletions(-)
include/linux/timekeeper_internal.h +0 -18
@@ -71,16 +71,6 @@ struct timekeeper { }; -static inline struct timespec64 tk_xtime (struct timekeeper *tk) -{ -struct timespec64 ts; - - ts.tv_sec = tk->xtime_sec; - ts.tv_nsec = (long )(tk->xtime_nsec >> tk->shift); -return ts; -} - -#ifdef CONFIG_GENERIC_TIME_VSYSCALL extern void update_vsyscall (struct timekeeper *tk);
@@ -92,14 +82,6 @@ extern void update_vsyscall_old(struct t struct clocksource *c, u32 mult);extern void update_vsyscall_tz (void ); -static inline void update_vsyscall (struct timekeeper *tk) -{ -struct timespec xt; - - xt =tk_xtime (tk); -update_vsyscall_old (&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); -} -#else static inline void update_vsyscall (struct timekeeper *tk)
kernel/time/timekeeping.c +43 -27
88 lines not on this page · raw message
Re: [patch V2 12/64] timekeeper: Move tk_xtime to core code
Quoted 23 lines · Thomas Gleixner
Index:
tip/kernel/time/timekeeping.c
===================================================================kernel/time/timekeeping.c +9
-0@@ -51,6 +51,15 @@ static inline void tk_normalize_xtime(st } } +static inline struct timespec64 tk_xtime (struct timekeeper *tk) +{ +struct timespec64 ts; + + ts.tv_sec = tk->xtime_sec; + ts.tv_nsec = (long )(tk->xtime_nsec >> tk->shift); +return ts; +} +static void tk_set_xtime (struct timekeeper *tk,const struct timespec64 *ts) { tk->xtime_sec = ts->tv_sec;@@ -199,6 +208,40 @@ static inline s64 timekeeping_get_ns_rawreturn nsec +arch_gettimeoffset (); }
kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 219
Quoted 10 lines
kernel/time/timekeeping.c +9
-0+#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD + +static inline void update_vsyscall (struct timekeeper *tk) +{ +struct timespec xt; + + xt =tk_xtime (tk); +update_vsyscall_old (&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); +}
So one gotcha here, and I realized I missed this in my timekeeping timespec64 conversion patch, is that here we're calling tk_xtime() which returns a timespec64, and we stuff it into a timespec.
Now, the reason we didn't see the compiler gripe on this, was that the only VSYSCALL_OLD implementations left are ia64 and ppc64.
I'll add a fixup patch to the queue to address this.
thanks
-john
Re: [patch V2 12/64] timekeeper: Move tk_xtime to core code
Quoted 39 lines · John Stultz
Quoted 23 lines · Thomas Gleixner
Index:
tip/kernel/time/timekeeping.c
===================================================================kernel/time/timekeeping.c +9
-0@@ -51,6 +51,15 @@ static inline void tk_normalize_xtime(st } } +static inline struct timespec64 tk_xtime (struct timekeeper *tk) +{ +struct timespec64 ts; + + ts.tv_sec = tk->xtime_sec; + ts.tv_nsec = (long )(tk->xtime_nsec >> tk->shift); +return ts; +} +static void tk_set_xtime (struct timekeeper *tk,const struct timespec64 *ts) { tk->xtime_sec = ts->tv_sec;@@ -199,6 +208,40 @@ static inline s64 timekeeping_get_ns_rawreturn nsec +arch_gettimeoffset (); }kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 219
Quoted 10 lines
kernel/time/timekeeping.c +9
-0+#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD + +static inline void update_vsyscall (struct timekeeper *tk) +{ +struct timespec xt; + + xt =tk_xtime (tk); +update_vsyscall_old (&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); +}So one gotcha here, and I realized I missed this in my timekeeping timespec64 conversion patch, is that here we're calling
tk_xtime()which returns a timespec64, and we stuff it into a timespec.
Quoted 4 lines
Now, the reason we didn't see the compiler gripe on this, was that the only
VSYSCALL_OLDimplementations left are ia64 and ppc64.I'll add a fixup patch to the queue to address this.
Nice catch!
Thanks,
tglx
PATCH v2 13/64 timekeeping: Cache optimize struct timekeeper
In reply to Thomas Gleixnerstruct timekeeper is quite badly sorted for the hot readout path. Most time access functions need to load two cache lines.
Rearrange it so ktime_get() and getnstimeofday() are happy with a single cache line.
--- include/linux/timekeeper_internal.h | 84 ++++++++-------- kernel/time/timekeeping.c | 185 ++++++++++++++++++------------------ 2 files changed, 143 insertions(+), 126 deletions(-)
include/linux/timekeeper_internal.h +46 -38
@@ -10,7 +10,22 @@#include <linux/jiffies.h> #include <linux/time.h> -/* Structure holding internal timekeeping values. */ +/* +* Structure holding internal timekeeping values. +* +* Note: wall_to_monotonic is what we need to add to xtime (or xtime +* corrected for sub jiffie times) to get to monotonic time. +* Monotonic is pegged at zero at system boot time, so +* wall_to_monotonic will be negative, however, we will ALWAYS keep +* the tv_nsec part positive so we can use the usual normalization. +* +* wall_to_monotonic is moved after resume from suspend for the +* monotonic time not to jump. We need to add total_sleep_time to +* wall_to_monotonic to get the real boot based time offset. +* +* - wall_to_monotonic is no longer the boot time, getboottime must be +* used instead. +*/ struct timekeeper {/* Current clocksource used for timekeeping. */ struct clocksource *clock;
@@ -18,6 +33,29 @@ struct timekeeper {u32 mult;/* The shift value of the current clocksource. */ u32 shift; +/* Clock shifted nano seconds */ +u64 xtime_nsec; + +/* Current CLOCK_REALTIME time in seconds */ +u64 xtime_sec; +/* CLOCK_REALTIME to CLOCK_MONOTONIC offset */ +struct timespec64 wall_to_monotonic; + +/* Offset clock monotonic -> clock realtime */ +ktime_t offs_real; +/* Offset clock monotonic -> clock boottime */ +ktime_t offs_boot; +/* Offset clock monotonic -> clock tai */ +ktime_t offs_tai; + +/* time spent in suspend */ +struct timespec64 total_sleep_time; +/* The current UTC to TAI offset in seconds */ +s32 tai_offset; + +/* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ +struct timespec64 raw_time; +/* Number of clock cycles in one NTP interval. */ cycle_t cycle_interval;/* Last cycle value (also stored in clock->cycle_last) */
@@ -29,46 +67,16 @@ struct timekeeper {/* Raw nano seconds accumulated per NTP interval. */ u32 raw_interval; -/* Current CLOCK_REALTIME time in seconds */ -u64 xtime_sec; -/* Clock shifted nano seconds */ -u64 xtime_nsec; - -/* Difference between accumulated time and NTP time in ntp -* shifted nano seconds. */ +/* +* Difference between accumulated time and NTP time in ntp +* shifted nano seconds. +*/ s64 ntp_error; -/* Shift conversion between clock shifted nano seconds and -* ntp shifted nano seconds. */ -u32 ntp_error_shift; -/* -* wall_to_monotonic is what we need to add to xtime (or xtime corrected -* for sub jiffie times) to get to monotonic time. Monotonic is pegged -* at zero at system boot time, so wall_to_monotonic will be negative, -* however, we will ALWAYS keep the tv_nsec part positive so we can use -* the usual normalization. -* -* wall_to_monotonic is moved after resume from suspend for the -* monotonic time not to jump. We need to add total_sleep_time to -* wall_to_monotonic to get the real boot based time offset. -* -* - wall_to_monotonic is no longer the boot time, getboottime must be -* used instead. +* Shift conversion between clock shifted nano seconds and +* ntp shifted nano seconds. */ -struct timespec64 wall_to_monotonic; -/* Offset clock monotonic -> clock realtime */ -ktime_t offs_real; -/* time spent in suspend */ -struct timespec64 total_sleep_time; -/* Offset clock monotonic -> clock boottime */ -ktime_t offs_boot; -/* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ -struct timespec64 raw_time; -/* The current UTC to TAI offset in seconds */ -s32 tai_offset; -/* Offset clock monotonic -> clock tai */ -ktime_t offs_tai; - +u32 ntp_error_shift; };#ifdef CONFIG_GENERIC_TIME_VSYSCALL
kernel/time/timekeeping.c +97 -88
637 lines not on this page · raw message
PATCH v2 14/64 timekeeping: Use timekeeping_update() instead of memcpy()
In reply to Thomas GleixnerWe already have a function which does the right thing, that also makes sure that the coming ktime_t based cached values are getting updated.
--- kernel/time/timekeeping.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/time/timekeeping.c +1 -2
@@ -883,8 +883,7 @@ void __init timekeeping_init(void) tmp.tv_nsec =0 ;tk_set_sleep_time (tk, tmp); -memcpy (&shadow_timekeeper, &tk_core.timekeeper, -sizeof (tk_core.timekeeper)); +timekeeping_update (tk,TK_MIRROR );write_seqcount_end (&tk_core.seq);raw_spin_unlock_irqrestore (&timekeeper_lock, flags);
PATCH v2 15/64 timekeeping: Provide internal ktime_t based data
In reply to Thomas GleixnerThe ktime_t based interfaces are used a lot in performance critical code pathes. Add ktime_t based data so the interfaces don't have to convert from the xtime/timespec based data.
--- include/linux/timekeeper_internal.h | 3 +++ kernel/time/timekeeping.c | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+)
include/linux/timekeeper_internal.h +3 -0
@@ -36,6 +36,9 @@ struct timekeeper {/* Clock shifted nano seconds */ u64 xtime_nsec; +/* Monotonic base time */ +ktime_t base_mono; +/* Current CLOCK_REALTIME time in seconds */ u64 xtime_sec;/* CLOCK_REALTIME to CLOCK_MONOTONIC offset */
kernel/time/timekeeping.c +22 -0
@@ -291,6 +291,26 @@ int pvclock_gtod_unregister_notifier(str }EXPORT_SYMBOL_GPL (pvclock_gtod_unregister_notifier); +/* +* Update the ktime_t based scalar nsec members of the timekeeper +*/ +static inline void tk_update_ktime_data (struct timekeeper *tk) +{ +s64 nsec; + +/* +* The xtime based monotonic readout is: +* nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now(); +* The ktime based monotonic readout is: +* nsec = base_mono + now(); +* ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec +*/ + nsec = (s64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec); + nsec *=NSEC_PER_SEC ; + nsec += tk->wall_to_monotonic.tv_nsec; + tk->base_mono =ns_to_ktime (nsec); +} +/* must hold timekeeper_lock */ static void timekeeping_update (struct timekeeper *tk,unsigned int action) {
@@ -301,6 +321,8 @@ static void timekeeping_update(struct tiupdate_vsyscall (tk);update_pvclock_gtod (tk, action &TK_CLOCK_WAS_SET ); +tk_update_ktime_data (tk); +if (action &TK_MIRROR )memcpy (&shadow_timekeeper, &tk_core.timekeeper,sizeof (tk_core.timekeeper));
PATCH v2 16/64 timekeeping: Use ktime_t based data for ktime_get()
In reply to Thomas GleixnerSpeed up ktime_get() by using ktime_t based data. Text size shrinks by 64 bytes on x8664.
--- kernel/time/timekeeping.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/time/timekeeping.c +5 -4
@@ -407,18 +407,19 @@ ktime_t ktime_get(void) {struct timekeeper *tk = &tk_core.timekeeper;unsigned int seq; -s64 secs, nsecs; +ktime_t base; +s64 nsecs;WARN_ON (timekeeping_suspended);do { seq =read_seqcount_begin (&tk_core.seq); - secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; - nsecs =timekeeping_get_ns (tk) + tk->wall_to_monotonic.tv_nsec; + base = tk->base_mono; + nsecs =timekeeping_get_ns (tk); }while (read_seqcount_retry (&tk_core.seq, seq)); -return ktime_set (secs, nsecs); +return ktime_add_ns (base, nsecs); }EXPORT_SYMBOL_GPL (ktime_get);
PATCH v2 17/64 timekeeping: Provide ktime_get_with_offset()
In reply to Thomas GleixnerProvide a helper function which lets us implement ktime_t based interfaces for real, boot and tai clocks.
--- include/linux/timekeeping.h | 9 +++++++++ kernel/time/timekeeping.c | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/timekeeping.h +9 -0
@@ -98,7 +98,16 @@ extern void getboottime(struct timespec/* * ktime_t based interfaces */ + +enum tk_offsets { +TK_OFFS_REAL , +TK_OFFS_BOOT , +TK_OFFS_TAI , +TK_OFFS_MAX , +}; +extern ktime_t ktime_get (void ); +extern ktime_t ktime_get_with_offset (enum tk_offsets offs);extern ktime_t ktime_get_real (void );extern ktime_t ktime_get_boottime (void );extern ktime_t ktime_get_monotonic_offset (void );
kernel/time/timekeeping.c +27 -0
@@ -423,6 +423,33 @@ ktime_t ktime_get(void) }EXPORT_SYMBOL_GPL (ktime_get); +static ktime_t *offsets[TK_OFFS_MAX ] = { + [TK_OFFS_REAL ] = &tk_core.timekeeper.offs_real, + [TK_OFFS_BOOT ] = &tk_core.timekeeper.offs_boot, + [TK_OFFS_TAI ] = &tk_core.timekeeper.offs_tai, +}; + +ktime_t ktime_get_with_offset (enum tk_offsets offs) +{ +struct timekeeper *tk = &tk_core.timekeeper; +unsigned int seq; +ktime_t base, *offset = offsets[offs]; +s64 nsecs; + +WARN_ON (timekeeping_suspended); + +do { + seq =read_seqcount_begin (&tk_core.seq); + base =ktime_add (tk->base_mono, *offset); + nsecs =timekeeping_get_ns (tk); + + }while (read_seqcount_retry (&tk_core.seq, seq)); + +return ktime_add_ns (base, nsecs); + +} +EXPORT_SYMBOL_GPL (ktime_get_with_offset); +/** * ktime_get_ts64 - get the monotonic clock in timespec64 format * @ts: pointer to timespec variable
PATCH v2 18/64 timekeeping: Use ktime_t based data for ktime_get_real()
In reply to Thomas GleixnerSpeed up the readout.
--- include/linux/timekeeping.h | 9 ++++++++- kernel/time/timekeeping.c | 15 --------------- 2 files changed, 8 insertions(+), 16 deletions(-)
include/linux/timekeeping.h +8 -1
@@ -108,11 +108,18 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs); -extern ktime_t ktime_get_real (void );extern ktime_t ktime_get_boottime (void );extern ktime_t ktime_get_monotonic_offset (void );extern ktime_t ktime_get_clocktai (void ); +/** +* ktime_get_real - get the real (wall-) time in ktime_t format +*/ +static inline ktime_t ktime_get_real (void ) +{ +return ktime_get_with_offset (TK_OFFS_REAL ); +} +/* * RTC specific */
kernel/time/timekeeping.c +0 -15
@@ -774,21 +774,6 @@ int timekeeping_notify(struct clocksourc }/** -* ktime_get_real - get the real (wall-) time in ktime_t format -* -* returns the time in ktime_t format -*/ -ktime_t ktime_get_real (void ) -{ -struct timespec64 now; - -getnstimeofday64 (&now); - -return timespec64_to_ktime (now); -} -EXPORT_SYMBOL_GPL (ktime_get_real); - -/** * getrawmonotonic - Returns the raw monotonic time in a timespec * @ts: pointer to the timespec to be set *
PATCH v2 19/64 timekeeping; Use ktime_t based data for ktime_get_boottime()
In reply to Thomas Gleixner--- include/linux/timekeeping.h | 12 +++++++++++- kernel/time/timekeeping.c | 17 ----------------- 2 files changed, 11 insertions(+), 18 deletions(-)
include/linux/timekeeping.h +11 -1
@@ -108,7 +108,6 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs); -extern ktime_t ktime_get_boottime (void );extern ktime_t ktime_get_monotonic_offset (void );extern ktime_t ktime_get_clocktai (void );
@@ -120,6 +119,17 @@ static inline ktime_t ktime_get_real(voireturn ktime_get_with_offset (TK_OFFS_REAL ); } +/** +* ktime_get_boottime - Returns monotonic time since boot in ktime_t format +* +* This is similar to CLOCK_MONTONIC/ktime_get, but also includes the +* time spent in suspend. +*/ +static inline ktime_t ktime_get_boottime (void ) +{ +return ktime_get_with_offset (TK_OFFS_BOOT ); +} +/* * RTC specific */
kernel/time/timekeeping.c +0 -17
@@ -1566,23 +1566,6 @@ void get_monotonic_boottime(struct timesEXPORT_SYMBOL_GPL (get_monotonic_boottime);/** -* ktime_get_boottime - Returns monotonic time since boot in a ktime -* -* Returns the monotonic time since boot in a ktime -* -* This is similar to CLOCK_MONTONIC/ktime_get, but also -* includes the time spent in suspend. -*/ -ktime_t ktime_get_boottime (void ) -{ -struct timespec ts; - -get_monotonic_boottime (&ts); -return timespec_to_ktime (ts); -} -EXPORT_SYMBOL_GPL (ktime_get_boottime); - -/** * monotonic_to_bootbased - Convert the monotonic time to boot based. * @ts: pointer to the timespec to be converted */
PATCH v2 20/64 timekeeping: Use ktime_t based data for ktime_get_clocktai()
In reply to Thomas Gleixner--- include/linux/timekeeping.h | 9 ++++++++- kernel/time/timekeeping.c | 15 --------------- 2 files changed, 8 insertions(+), 16 deletions(-)
include/linux/timekeeping.h +8 -1
@@ -109,7 +109,6 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs);extern ktime_t ktime_get_monotonic_offset (void ); -extern ktime_t ktime_get_clocktai (void );/** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -130,6 +129,14 @@ static inline ktime_t ktime_get_boottimereturn ktime_get_with_offset (TK_OFFS_BOOT ); } +/** +* ktime_get_clocktai - Returns the TAI time of day in ktime_t format +*/ +static inline ktime_t ktime_get_clocktai (void ) +{ +return ktime_get_with_offset (TK_OFFS_TAI ); +} +/* * RTC specific */
kernel/time/timekeeping.c +0 -15
@@ -512,21 +512,6 @@ void timekeeping_clocktai(struct timespe }EXPORT_SYMBOL (timekeeping_clocktai); - -/** -* ktime_get_clocktai - Returns the TAI time of day in a ktime -* -* Returns the time of day in a ktime. -*/ -ktime_t ktime_get_clocktai (void ) -{ -struct timespec ts; - -timekeeping_clocktai (&ts); -return timespec_to_ktime (ts); -} -EXPORT_SYMBOL (ktime_get_clocktai); -#ifdef CONFIG_NTP_PPS /**
PATCH v2 21/64 timekeeping: Use ktime_t data for ktime_get_update_offsets_now()
In reply to Thomas GleixnerNo need to juggle with timespecs.
--- kernel/time/timekeeping.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
kernel/time/timekeeping.c +4 -6
@@ -1668,14 +1668,14 @@ ktime_t ktime_get_update_offsets_now(kti ktime_t *offs_tai) {struct timekeeper *tk = &tk_core.timekeeper; -ktime_t now;unsigned int seq; -u64 secs, nsecs; +ktime_t base; +u64 nsecs; do { seq = read_seqcount_begin(&tk_core.seq); - secs = tk->xtime_sec; + base = tk->base_mono; nsecs = timekeeping_get_ns(tk); *offs_real = tk->offs_real;
@@ -1683,9 +1683,7 @@ ktime_t ktime_get_update_offsets_now(kti *offs_tai = tk->offs_tai; }while (read_seqcount_retry (&tk_core.seq, seq)); - now =ktime_add_ns (ktime_set (secs,0 ), nsecs); - now =ktime_sub (now, *offs_real); -return now; +return ktime_add_ns (base, nsecs); }#endif
PATCH v2 22/64 timekeeping; Use ktime based data for ktime_get_update_offsets_tick()
In reply to Thomas GleixnerNo need to juggle with timespecs.
--- kernel/time/timekeeping.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/time/timekeeping.c +6 -6
@@ -1636,22 +1636,22 @@ ktime_t ktime_get_update_offsets_tick(kt ktime_t *offs_tai) {struct timekeeper *tk = &tk_core.timekeeper; -struct timespec64 ts; -ktime_t now;unsigned int seq; +ktime_t base; +u64 nsecs;do { seq =read_seqcount_begin (&tk_core.seq); - ts =tk_xtime (tk); + base = tk->base_mono; + nsecs = tk->xtime_nsec >> tk->shift; + *offs_real = tk->offs_real; *offs_boot = tk->offs_boot; *offs_tai = tk->offs_tai; }while (read_seqcount_retry (&tk_core.seq, seq)); - now =ktime_set (ts.tv_sec, ts.tv_nsec); - now =ktime_sub (now, *offs_real); -return now; +return ktime_add_ns (base, nsecs); }#ifdef CONFIG_HIGH_RES_TIMERS
PATCH v2 23/64 timekeeping: Provide ktime_mono_to_any()
In reply to Thomas Gleixnerktime based conversion function to map a monotonic time stamp to a different CLOCK.
--- include/linux/timekeeping.h | 9 +++++++++ kernel/time/timekeeping.c | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/timekeeping.h +9 -0
@@ -109,6 +109,7 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs);extern ktime_t ktime_get_monotonic_offset (void ); +extern ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs);/** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -137,6 +138,14 @@ static inline ktime_t ktime_get_clocktaireturn ktime_get_with_offset (TK_OFFS_TAI ); } +/** +* ktime_mono_to_real - Convert monotonic time to clock realtime +*/ +static inline ktime_t ktime_mono_to_real (ktime_t mono) +{ +return ktime_mono_to_any (mono,TK_OFFS_REAL ); +} +/* * RTC specific */
kernel/time/timekeeping.c +20 -0
@@ -451,6 +451,26 @@ ktime_t ktime_get_with_offset(enum tk_ofEXPORT_SYMBOL_GPL (ktime_get_with_offset);/** +* ktime_mono_to_any() - convert mononotic time to any other time +* @tmono: time to convert. +* @offs: which offset to use +*/ +ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs) +{ +ktime_t *offset = offsets[offs]; +unsigned long seq; +ktime_t tconv; + +do { + seq =read_seqcount_begin (&tk_core.seq); + tconv =ktime_add (tmono, *offset); + }while (read_seqcount_retry (&tk_core.seq, seq)); + +return tconv; +} +EXPORT_SYMBOL_GPL (ktime_mono_to_any); + +/** * ktime_get_ts64 - get the monotonic clock in timespec64 format * @ts: pointer to timespec variable *
PATCH v2 24/64 timerfd: Use ktime_mono_to_real()
In reply to Thomas GleixnerWe have a few other use cases of ktime_get_monotonic_offset() which can be optimized with ktime_mono_to_real(). The timerfd code uses the offset only for comparison, so we can use ktime_mono_to_real(0) for this as well.
Funny enough text size shrinks with that on ARM and x8664 !?
--- fs/timerfd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/timerfd.c +3 -3
@@ -92,7 +92,7 @@ static enum alarmtimer_restart timerfd_a*/ void timerfd_clock_was_set (void ) { -ktime_t moffs =ktime_get_monotonic_offset (); +ktime_t moffs =ktime_mono_to_real ((ktime_t ){ .tv64 =0 });struct timerfd_ctx *ctx;unsigned long flags;
@@ -125,7 +125,7 @@ static bool timerfd_canceled(struct time {if (!ctx->might_cancel || ctx->moffs.tv64 !=KTIME_MAX )return false; - ctx->moffs =ktime_get_monotonic_offset (); + ctx->moffs =ktime_mono_to_real ((ktime_t ){ .tv64 =0 });return true; }
@@ -336,7 +336,7 @@ SYSCALL_DEFINE2(timerfd_create, int, cloelse hrtimer_init (&ctx->t.tmr, clockid,HRTIMER_MODE_ABS ); - ctx->moffs =ktime_get_monotonic_offset (); + ctx->moffs =ktime_mono_to_real ((ktime_t ){ .tv64 =0 }); ufd =anon_inode_getfd ("[timerfd]" , &timerfd_fops, ctx,O_RDWR | (flags &TFD_SHARED_FCNTL_FLAGS ));
PATCH v2 25/64 input: evdev: Use ktime_mono_to_real()
In reply to Thomas GleixnerConvert the monotonic timestamp with ktime_mono_to_real() in evdev_events().
In evdev_queue_syn_dropped() we can call either ktime_get() or ktime_get_real() depending on the clkid. No point in having two calls for CLOCK_REALTIME.
--- drivers/input/evdev.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/input/evdev.c +3 -4
@@ -108,9 +108,8 @@ static void evdev_queue_syn_dropped(strustruct input_event ev;ktime_t time; - time =ktime_get (); -if (client->clkid !=CLOCK_MONOTONIC ) - time =ktime_sub (time,ktime_get_monotonic_offset ()); + time = (client->clkid ==CLOCK_MONOTONIC ) ? +ktime_get () :ktime_get_real (); ev.time =ktime_to_timeval (time); ev.type =EV_SYN ;
@@ -202,7 +201,7 @@ static void evdev_events(struct input_haktime_t time_mono, time_real; time_mono =ktime_get (); - time_real =ktime_sub (time_mono,ktime_get_monotonic_offset ()); + time_real =ktime_mono_to_real (time_mono);rcu_read_lock ();
PATCH v2 26/64 drm: Use ktime_mono_to_real()
In reply to Thomas GleixnerConvert the monotonic timestamp with ktime_mono_to_real() in drm_calc_vbltimestamp_from_scanoutpos().
In get_drm_timestamp we can call either ktime_get() or ktime_get_real() depending on drm_timestamp_monotonic. No point in having two calls into the core for CLOCK_REALTIME.
--- drivers/gpu/drm/drm_irq.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_irq.c +3 -13
@@ -542,8 +542,8 @@ int drm_calc_vbltimestamp_from_scanoutpoconst struct drm_crtc *refcrtc, const struct drm_display_mode *mode) { - ktime_t stime, etime, mono_time_offset; struct timeval tv_etime; +ktime_t stime, etime;int vbl_status;int vpos, hpos, i;int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
@@ -588,13 +588,6 @@ int drm_calc_vbltimestamp_from_scanoutpo vbl_status = dev->driver->get_scanout_position (dev, crtc, flags, &vpos, &hpos, &stime, &etime); -/* -* Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if -* CLOCK_REALTIME is requested. -*/ -if (!drm_timestamp_monotonic) - mono_time_offset =ktime_get_monotonic_offset (); -/* Return as no-op if scanout query unsupported or failed. */ if (!(vbl_status &DRM_SCANOUTPOS_VALID )) { DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
@@ -633,7 +626,7 @@ int drm_calc_vbltimestamp_from_scanoutpo delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;if (!drm_timestamp_monotonic) - etime =ktime_sub (etime, mono_time_offset); + etime =ktime_mono_to_real (etime);/* save this only for debugging purposes */ tv_etime =ktime_to_timeval (etime);
@@ -664,10 +657,7 @@ static struct timeval get_drm_timestamp( {ktime_t now; - now =ktime_get (); -if (!drm_timestamp_monotonic) - now =ktime_sub (now,ktime_get_monotonic_offset ()); - + now = drm_timestamp_monotonic ?ktime_get () :ktime_get_real ();return ktime_to_timeval (now); }
PATCH v2 27/64 timekeeping: Remove ktime_get_monotonic_offset()
In reply to Thomas GleixnerNo more users.
--- include/linux/timekeeping.h | 1 - kernel/time/timekeeping.c | 18 ------------------ 2 files changed, 19 deletions(-)
include/linux/timekeeping.h +0 -1
@@ -108,7 +108,6 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs); -extern ktime_t ktime_get_monotonic_offset (void );extern ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs);/**
kernel/time/timekeeping.c +0 -18
@@ -1708,24 +1708,6 @@ ktime_t ktime_get_update_offsets_now(kti#endif /** -* ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format -*/ -ktime_t ktime_get_monotonic_offset (void ) -{ -struct timekeeper *tk = &tk_core.timekeeper; -unsigned long seq; -struct timespec64 wtom; - -do { - seq =read_seqcount_begin (&tk_core.seq); - wtom = tk->wall_to_monotonic; - }while (read_seqcount_retry (&tk_core.seq, seq)); - -return timespec64_to_ktime (wtom); -} -EXPORT_SYMBOL_GPL (ktime_get_monotonic_offset); - -/** * do_adjtimex() - Accessor function to NTP __do_adjtimex function */ int do_adjtimex (struct timex *txc)
PATCH v2 28/64 timekeeping: Provide ktime_get[*]_ns() helpers
In reply to Thomas GleixnerA lot of code converts either timespecs or ktime_t to nanoseconds. Provide helper functions.
--- include/linux/timekeeping.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
include/linux/timekeeping.h +15 -0
@@ -145,6 +145,21 @@ static inline ktime_t ktime_mono_to_realreturn ktime_mono_to_any (mono,TK_OFFS_REAL ); } +static inline u64 ktime_get_ns (void ) +{ +return ktime_to_ns (ktime_get ()); +} + +static inline u64 ktime_get_real_ns (void ) +{ +return ktime_to_ns (ktime_get_real ()); +} + +static inline u64 ktime_get_boot_ns (void ) +{ +return ktime_to_ns (ktime_get_boottime ()); +} +/* * RTC specific */
PATCH v2 29/64 time: Export nsecs_to_jiffies()
In reply to Thomas GleixnerRequired for moving drivers to the nanosecond based interfaces.
--- kernel/time/time.c | 1 + 1 file changed, 1 insertion(+)
kernel/time/time.c +1 -0
@@ -757,6 +757,7 @@ unsigned long nsecs_to_jiffies(u64 n) {return (unsigned long )nsecs_to_jiffies64 (n); } +EXPORT_SYMBOL_GPL (nsecs_to_jiffies);/* * Add two timespec values and do a safety check for overflow.
PATCH v2 30/64 sched: Make task->real_start_time nanoseconds based
In reply to Thomas GleixnerSimplify the only user of this data by removing the timespec conversion.
--- fs/proc/array.c | 7 +------ include/linux/sched.h | 2 +- kernel/fork.c | 3 +-- 3 files changed, 3 insertions(+), 9 deletions(-)
fs/proc/array.c +1 -6
@@ -473,13 +473,8 @@ static int do_task_stat(struct seq_file priority =task_prio (task); nice =task_nice (task); -/* Temporary variable needed for gcc-2.96 */ -/* convert timespec -> nsec*/ - start_time = - (unsigned long long )task->real_start_time.tv_sec *NSEC_PER_SEC - + task->real_start_time.tv_nsec;/* convert nsec -> ticks */ - start_time =nsec_to_clock_t (start_time); + start_time =nsec_to_clock_t (task->real_start_time);seq_printf (m,"%d (%s) %c" ,pid_nr_ns (pid, ns), tcomm, state);seq_put_decimal_ll (m,' ' , ppid);
include/linux/sched.h +1 -1
@@ -1368,7 +1368,7 @@ struct task_struct {#endif unsigned long nvcsw, nivcsw;/* context switch counts */ struct timespec start_time;/* monotonic time */ -struct timespec real_start_time;/* boot based time */ +u64 real_start_time;/* boot based time in nsec */ /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ unsigned long min_flt, maj_flt;
kernel/fork.c +1 -2
@@ -1263,8 +1263,7 @@ static struct task_struct *copy_process(posix_cpu_timers_init (p);ktime_get_ts (&p->start_time); - p->real_start_time = p->start_time; -monotonic_to_bootbased (&p->real_start_time); + p->real_start_time =ktime_get_boot_ns (); p->io_context =NULL ; p->audit_context =NULL ;if (clone_flags &CLONE_THREAD )
PATCH v2 31/64 sched: Make task->start_time nanoseconds based
In reply to Thomas GleixnerSimplify the timespec to nsec/usec conversions.
--- include/linux/sched.h | 2 +- kernel/acct.c | 10 +++------- kernel/fork.c | 2 +- kernel/tsacct.c | 19 +++++++++---------- 4 files changed, 14 insertions(+), 19 deletions(-)
include/linux/sched.h +1 -1
@@ -1367,7 +1367,7 @@ struct task_struct { } vtime_snap_whence;#endif unsigned long nvcsw, nivcsw;/* context switch counts */ -struct timespec start_time;/* monotonic time */ +u64 start_time;/* monotonic time in nsec */ u64 real_start_time;/* boot based time in nsec */ /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ unsigned long min_flt, maj_flt;
kernel/acct.c +3 -7
@@ -458,9 +458,7 @@ static void do_acct_process(struct bsd_aacct_t ac;mm_segment_t fs;unsigned long flim; -u64 elapsed; -u64 run_time; -struct timespec uptime; +u64 elapsed, run_time;struct tty_struct *tty;const struct cred *orig_cred;
@@ -484,10 +482,8 @@ static void do_acct_process(struct bsd_astrlcpy (ac.ac_comm, current->comm,sizeof (ac.ac_comm));/* calculate run_time in nsec*/ -ktime_get_ts (&uptime); - run_time = (u64 )uptime.tv_sec*NSEC_PER_SEC + uptime.tv_nsec; - run_time -= (u64 )current->group_leader->start_time.tv_sec *NSEC_PER_SEC - + current->group_leader->start_time.tv_nsec; + run_time =ktime_get_ns (); + run_time -= current->group_leader->start_time;/* convert nsec -> AHZ */ elapsed =nsec_to_AHZ (run_time);#if ACCT_VERSION ==3
kernel/fork.c +1 -1
@@ -1262,7 +1262,7 @@ static struct task_struct *copy_process(posix_cpu_timers_init (p); -ktime_get_ts (&p->start_time); + p->start_time =ktime_get_ns (); p->real_start_time =ktime_get_boot_ns (); p->io_context =NULL ; p->audit_context =NULL ;
kernel/tsacct.c +9 -10
@@ -31,20 +31,19 @@ void bacct_add_tsk(struct user_namespace struct taskstats *stats, struct task_struct *tsk) { const struct cred *tcred; -struct timespec uptime, ts;cputime_t utime, stime, utimescaled, stimescaled; -u64 ac_etime; +u64 delta;BUILD_BUG_ON (TS_COMM_LEN <TASK_COMM_LEN ); -/* calculate task elapsed time in timespec */ -ktime_get_ts (&uptime); - ts =timespec_sub (uptime, tsk->start_time); -/* rebase elapsed time to usec (should never be negative) */ - ac_etime =timespec_to_ns (&ts); -do_div (ac_etime,NSEC_PER_USEC ); - stats->ac_etime = ac_etime; - stats->ac_btime =get_seconds () - ts.tv_sec; +/* calculate task elapsed time in nsec */ + delta =ktime_get_ns () - tsk->start_time; +/* Convert to micro seconds */ +do_div (delta,NSEC_PER_USEC ); + stats->ac_etime = delta; +/* Convert to seconds for btime */ +do_div (delta,USEC_PER_SEC ); + stats->ac_btime =get_seconds () - delta;if (thread_group_leader (tsk)) { stats->ac_exitcode = tsk->exit_code;if (tsk->flags &PF_FORKNOEXEC )
PATCH v2 32/64 delayacct: Make accounting nanosecond based
In reply to Thomas GleixnerKill the timespec juggling and calculate with plain nanoseconds.
--- include/linux/sched.h | 4 ++-- kernel/delayacct.c | 34 ++++++++++++---------------------- 2 files changed, 14 insertions(+), 24 deletions(-)
include/linux/sched.h +2 -2
@@ -813,7 +813,7 @@ struct task_delay_info {* associated with the operation is added to XXX_delay. * XXX_delay contains the accumulated delay time in nanoseconds. */ -struct timespec blkio_start, blkio_end;/* Shared by blkio, swapin */ +u64 blkio_start;/* Shared by blkio, swapin */ u64 blkio_delay;/* wait for sync block io completion */ u64 swapin_delay;/* wait for swapin block io completion */ u32 blkio_count;/* total count of the number of sync block */
@@ -821,7 +821,7 @@ struct task_delay_info {u32 swapin_count;/* total count of the number of swapin block */ /* io operations performed */ -struct timespec freepages_start, freepages_end; +u64 freepages_start;u64 freepages_delay;/* wait for memory reclaim */ u32 freepages_count;/* total count of memory reclaim */ };
kernel/delayacct.c +12 -22
@@ -46,32 +46,25 @@ void __delayacct_tsk_init(struct task_st }/* -* Finish delay accounting for a statistic using -* its timestamps (@start, @end), accumalator (@total) and @count +* Finish delay accounting for a statistic using its timestamps (@start), +* accumalator (@total) and @count */ - -static void delayacct_end (struct timespec *start,struct timespec *end, -u64 *total,u32 *count) +static void delayacct_end (u64 *start,u64 *total,u32 *count) { -struct timespec ts; -s64 ns; +s64 ns =ktime_get_ns () - *start;unsigned long flags; -ktime_get_ts (end); - ts =timespec_sub (*end, *start); - ns =timespec_to_ns (&ts); -if (ns <0 ) -return ; - -spin_lock_irqsave (¤t->delays->lock, flags); - *total += ns; - (*count)++; -spin_unlock_irqrestore (¤t->delays->lock, flags); +if (ns >0 ) { +spin_lock_irqsave (¤t->delays->lock, flags); + *total += ns; + (*count)++; +spin_unlock_irqrestore (¤t->delays->lock, flags); + } }void __delayacct_blkio_start (void ) { -ktime_get_ts (¤t->delays->blkio_start); + current->delays->blkio_start =ktime_get_ns (); }void __delayacct_blkio_end (void )
@@ -79,12 +72,10 @@ void __delayacct_blkio_end(void)if (current->delays->flags &DELAYACCT_PF_SWAPIN )/* Swapin block I/O */ delayacct_end (¤t->delays->blkio_start, - ¤t->delays->blkio_end, ¤t->delays->swapin_delay, ¤t->delays->swapin_count);else /* Other block I/O */ delayacct_end (¤t->delays->blkio_start, - ¤t->delays->blkio_end, ¤t->delays->blkio_delay, ¤t->delays->blkio_count); }
@@ -159,13 +150,12 @@ __u64 __delayacct_blkio_ticks(struct tasvoid __delayacct_freepages_start (void ) { -ktime_get_ts (¤t->delays->freepages_start); + current->delays->freepages_start =ktime_get_ns (); }void __delayacct_freepages_end (void ) {delayacct_end (¤t->delays->freepages_start, - ¤t->delays->freepages_end, ¤t->delays->freepages_delay, ¤t->delays->freepages_count); }
PATCH v2 33/64 delayacct: Remove braindamaged type conversions
In reply to Thomas GleixnerConverting cputime to timespec and timespec to nanoseconds makes no sense. Use cputime_to_ns() and be done with it.
--- kernel/delayacct.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
kernel/delayacct.c +7 -11
@@ -82,23 +82,19 @@ void __delayacct_blkio_end(void)int __delayacct_add_tsk (struct taskstats *d,struct task_struct *tsk) { -s64 tmp; -unsigned long t1; -unsigned long long t2, t3; -unsigned long flags; -struct timespec ts;cputime_t utime, stime, stimescaled, utimescaled; +unsigned long long t2, t3; +unsigned long flags, t1; +s64 tmp; - tmp = (s64 )d->cpu_run_real_total;task_cputime (tsk, &utime, &stime); -cputime_to_timespec (utime + stime, &ts); - tmp +=timespec_to_ns (&ts); + tmp = (s64 )d->cpu_run_real_total; + tmp +=cputime_to_nsecs (utime + stime); d->cpu_run_real_total = (tmp < (s64 )d->cpu_run_real_total) ?0 : tmp; - tmp = (s64 )d->cpu_scaled_run_real_total;task_cputime_scaled (tsk, &utimescaled, &stimescaled); -cputime_to_timespec (utimescaled + stimescaled, &ts); - tmp +=timespec_to_ns (&ts); + tmp = (s64 )d->cpu_scaled_run_real_total; + tmp +=cputime_to_nsecs (utimescaled + stimescaled); d->cpu_scaled_run_real_total = (tmp < (s64 )d->cpu_scaled_run_real_total) ?0 : tmp;
PATCH v2 34/64 powerpc: cell: Use ktime_get_ns()
In reply to Thomas GleixnerReplace the ever recurring:
ts = ktime_get_ts(); ns = timespec_to_ns(&ts); with ns = ktime_get_ns();
--- arch/powerpc/platforms/cell/spu_base.c | 11 +++-------- arch/powerpc/platforms/cell/spufs/context.c | 4 +--- arch/powerpc/platforms/cell/spufs/file.c | 4 +--- arch/powerpc/platforms/cell/spufs/sched.c | 4 +--- 4 files changed, 6 insertions(+), 17 deletions(-)
arch/powerpc/platforms/cell/spu_base.c +3 -8
@@ -611,7 +611,6 @@ static int __init create_spu(void *data)int ret;static int number;unsigned long flags; -struct timespec ts; ret = -ENOMEM ; spu =kzalloc (sizeof (*spu),GFP_KERNEL );
@@ -652,8 +651,7 @@ static int __init create_spu(void *data)mutex_unlock (&spu_full_list_mutex); spu->stats.util_state =SPU_UTIL_IDLE_LOADED ; -ktime_get_ts (&ts); - spu->stats.tstamp =timespec_to_ns (&ts); + spu->stats.tstamp =ktime_get_ns ();INIT_LIST_HEAD (&spu->aff_list);
@@ -676,7 +674,6 @@ static const char *spu_state_names[] = {static unsigned long long spu_acct_time (struct spu *spu,enum spu_utilization_state state) { -struct timespec ts;unsigned long long time = spu->stats.times[state];/*
@@ -684,10 +681,8 @@ static unsigned long long spu_acct_time(* statistics are not updated. Apply the time delta from the * last recorded state of the spu. */ -if (spu->stats.util_state == state) { -ktime_get_ts (&ts); - time +=timespec_to_ns (&ts) - spu->stats.tstamp; - } +if (spu->stats.util_state == state) + time +=ktime_get_ns () - spu->stats.tstamp;return time /NSEC_PER_MSEC ; }
arch/powerpc/platforms/cell/spufs/context.c +1 -3
@@ -36,7 +36,6 @@ atomic_t nr_spu_contexts = ATOMIC_INIT(0struct spu_context *alloc_spu_context (struct spu_gang *gang) {struct spu_context *ctx; -struct timespec ts; ctx =kzalloc (sizeof *ctx,GFP_KERNEL );if (!ctx)
@@ -67,8 +66,7 @@ struct spu_context *alloc_spu_context(st__spu_update_sched_info (ctx);spu_set_timeslice (ctx); ctx->stats.util_state =SPU_UTIL_IDLE_LOADED ; -ktime_get_ts (&ts); - ctx->stats.tstamp =timespec_to_ns (&ts); + ctx->stats.tstamp =ktime_get_ns ();atomic_inc (&nr_spu_contexts); goto out;
arch/powerpc/platforms/cell/spufs/file.c +1 -3
@@ -2338,7 +2338,6 @@ static const char *ctx_state_names[] = {static unsigned long long spufs_acct_time (struct spu_context *ctx,enum spu_utilization_state state) { -struct timespec ts;unsigned long long time = ctx->stats.times[state];/*
@@ -2351,8 +2350,7 @@ static unsigned long long spufs_acct_tim* of the spu context. */ if (ctx->spu && ctx->stats.util_state == state) { -ktime_get_ts (&ts); - time +=timespec_to_ns (&ts) - ctx->stats.tstamp; + time +=ktime_get_ns () - ctx->stats.tstamp; }return time /NSEC_PER_MSEC ;
arch/powerpc/platforms/cell/spufs/sched.c +1 -3
@@ -1039,13 +1039,11 @@ void spuctx_switch_state(struct spu_cont {unsigned long long curtime;signed long long delta; -struct timespec ts;struct spu *spu;enum spu_utilization_state old_state;int node; -ktime_get_ts (&ts); - curtime =timespec_to_ns (&ts); + curtime =ktime_get_ns (); delta = curtime - ctx->stats.tstamp;WARN_ON (!mutex_is_locked (&ctx->state_mutex));
PATCH v2 35/64 connector: Use ktime_get_ns()
In reply to Thomas GleixnerReplace the ever recurring:
ts = ktime_get_ts();
ns = timespec_to_ns(&ts);
with
ns = ktime_get_ns();--- drivers/connector/cn_proc.c | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-)
drivers/connector/cn_proc.c +9 -27
@@ -69,7 +69,6 @@ void proc_fork_connector(struct task_strstruct cn_msg *msg;struct proc_event *ev;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ; -struct timespec ts;struct task_struct *parent;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -79,8 +78,7 @@ void proc_fork_connector(struct task_str ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_FORK ;rcu_read_lock (); parent =rcu_dereference (task->real_parent);
@@ -102,7 +100,6 @@ void proc_exec_connector(struct task_str {struct cn_msg *msg;struct proc_event *ev; -struct timespec ts;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -112,8 +109,7 @@ void proc_exec_connector(struct task_str ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_EXEC ; ev->event_data.exec.process_pid = task->pid; ev->event_data.exec.process_tgid = task->tgid;
@@ -130,7 +126,6 @@ void proc_id_connector(struct task_strucstruct cn_msg *msg;struct proc_event *ev;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ; -struct timespec ts;const struct cred *cred;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -156,8 +151,7 @@ void proc_id_connector(struct task_struc }rcu_read_unlock ();get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns ();memcpy (&msg->id, &cn_proc_event_id,sizeof (msg->id)); msg->ack =0 ;/* not used */
@@ -170,7 +164,6 @@ void proc_sid_connector(struct task_stru {struct cn_msg *msg;struct proc_event *ev; -struct timespec ts;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -180,8 +173,7 @@ void proc_sid_connector(struct task_stru ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_SID ; ev->event_data.sid.process_pid = task->pid; ev->event_data.sid.process_tgid = task->tgid;
@@ -197,7 +189,6 @@ void proc_ptrace_connector(struct task_s {struct cn_msg *msg;struct proc_event *ev; -struct timespec ts;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -207,8 +198,7 @@ void proc_ptrace_connector(struct task_s ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_PTRACE ; ev->event_data.ptrace.process_pid = task->pid; ev->event_data.ptrace.process_tgid = task->tgid;
@@ -232,7 +222,6 @@ void proc_comm_connector(struct task_str {struct cn_msg *msg;struct proc_event *ev; -struct timespec ts;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ;if (atomic_read (&proc_event_num_listeners) <1 )
@@ -242,8 +231,7 @@ void proc_comm_connector(struct task_str ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_COMM ; ev->event_data.comm.process_pid = task->pid; ev->event_data.comm.process_tgid = task->tgid;
@@ -261,7 +249,6 @@ void proc_coredump_connector(struct taskstruct cn_msg *msg;struct proc_event *ev;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ; -struct timespec ts;if (atomic_read (&proc_event_num_listeners) <1 )return ;
@@ -270,8 +257,7 @@ void proc_coredump_connector(struct task ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_COREDUMP ; ev->event_data.coredump.process_pid = task->pid; ev->event_data.coredump.process_tgid = task->tgid;
@@ -288,7 +274,6 @@ void proc_exit_connector(struct task_strstruct cn_msg *msg;struct proc_event *ev;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ; -struct timespec ts;if (atomic_read (&proc_event_num_listeners) <1 )return ;
@@ -297,8 +282,7 @@ void proc_exit_connector(struct task_str ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data));get_seq (&msg->seq, &ev->cpu); -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->what =PROC_EVENT_EXIT ; ev->event_data.exit.process_pid = task->pid; ev->event_data.exit.process_tgid = task->tgid;
@@ -325,7 +309,6 @@ static void cn_proc_ack(int err, int rcvstruct cn_msg *msg;struct proc_event *ev;__u8 buffer[CN_PROC_MSG_SIZE ]__aligned(8) ; -struct timespec ts;if (atomic_read (&proc_event_num_listeners) <1 )return ;
@@ -334,8 +317,7 @@ static void cn_proc_ack(int err, int rcv ev = (struct proc_event *)msg->data;memset (&ev->event_data,0 ,sizeof (ev->event_data)); msg->seq = rcvd_seq; -ktime_get_ts (&ts);/* get high res monotonic timestamp */ - ev->timestamp_ns =timespec_to_ns (&ts); + ev->timestamp_ns =ktime_get_ns (); ev->cpu =-1 ; ev->what =PROC_EVENT_NONE ; ev->event_data.ack.err = err;
Re: [patch V2 35/64] connector: Use ktime_get_ns()
Hi
17.07.2014, 01:04, "Thomas Gleixner" <[email protected]>:
Quoted 8 lines
Replace the ever recurring:
ts =ktime_get_ts();
ns =timespec_to_ns(&ts);
with
ns =ktime_get_ns();Signed-off-by Thomas Gleixner <[email protected]>Cc Evgeniy Polyakov <[email protected]>
Looks good to me, thank you.
PATCH v2 36/64 mfd: cros_ec_spi: Use ktime_get_ns()
In reply to Thomas GleixnerReplace the ever recurring:
ts = ktime_get_ts(); ns = timespec_to_ns(&ts); with ns = ktime_get_ns();
--- drivers/mfd/cros_ec_spi.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/mfd/cros_ec_spi.c +2 -6
@@ -225,7 +225,6 @@ static int cros_ec_command_spi_xfer(struu8 *ptr;int sum;int ret =0 , final_ret; -struct timespec ts;/* * We have the shared ec_dev buffer plus we do lots of separate spi_sync
@@ -239,11 +238,9 @@ static int cros_ec_command_spi_xfer(stru/* If it's too soon to do another transaction, wait */ if (ec_spi->last_transfer_ns) { -struct timespec ts;unsigned long delay;/* The delay completed so far */ -ktime_get_ts (&ts); - delay =timespec_to_ns (&ts) - ec_spi->last_transfer_ns; + delay =ktime_get_ns () - ec_spi->last_transfer_ns;if (delay <EC_SPI_RECOVERY_TIME_NS )ndelay (EC_SPI_RECOVERY_TIME_NS - delay); }
@@ -280,8 +277,7 @@ static int cros_ec_command_spi_xfer(stru } final_ret =spi_sync (ec_spi->spi, &msg); -ktime_get_ts (&ts); - ec_spi->last_transfer_ns =timespec_to_ns (&ts); + ec_spi->last_transfer_ns =ktime_get_ns ();if (!ret) ret = final_ret;if (ret <0 ) {
PATCH v2 37/64 misc: ioc4: Use ktime_get_ns()
In reply to Thomas GleixnerReplace the ever recurring:
ts = ktime_get_ts();
ns = timespec_to_ns(&ts);
with
ns = ktime_get_ns();--- drivers/misc/ioc4.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/misc/ioc4.c +2 -5
@@ -145,7 +145,6 @@ ioc4_clock_calibrate(struct ioc4_driver_union ioc4_int_out int_out;union ioc4_gpcr gpcr;unsigned int state, last_state =1 ; -struct timespec start_ts, end_ts;uint64_t start, end, period;unsigned int count =0 ;
@@ -174,10 +173,10 @@ ioc4_clock_calibrate(struct ioc4_driver_if (!last_state && state) { count++;if (count ==IOC4_CALIBRATE_END ) { -ktime_get_ts (&end_ts); + end =ktime_get_ns ();break ; }else if (count ==IOC4_CALIBRATE_DISCARD ) -ktime_get_ts (&start_ts); + start =ktime_get_ns (); } last_state = state; }while (1 );
@@ -192,8 +191,6 @@ ioc4_clock_calibrate(struct ioc4_driver_* by which the IOC4 generates the square wave, to get the * period of an IOC4 INT_OUT count. */ - end = end_ts.tv_sec *NSEC_PER_SEC + end_ts.tv_nsec; - start = start_ts.tv_sec *NSEC_PER_SEC + start_ts.tv_nsec; period = (end - start) / (IOC4_CALIBRATE_CYCLES *2 * (IOC4_CALIBRATE_COUNT +1 ));
PATCH v2 38/64 net: mlx5: Use ktime_get_ns()
In reply to Thomas GleixnerThis code is beyond silly:
struct timespec ts = ktime_get_ts();
ktime_t ktime = timespec_to_ktime(ts);Further down the code builds the delta of two ktime_t values and converts the result to nanoseconds.
Use ktime_get_ns() and replace all the nonsense.
--- drivers/net/ethernet/mellanox/mlx5/core/cmd.c | 16 ++++------------ include/linux/mlx5/driver.h | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/cmd.c +4 -12
@@ -548,7 +548,7 @@ static void cmd_work_handler(struct work lay->status_own =CMD_OWNER_HW ;set_signature (ent, !cmd->checksum_disabled);dump_command (dev, ent,1 ); -ktime_get_ts (&ent->ts1); + ent->ts1 =ktime_get_ns ();/* ring doorbell after the descriptor is valid */ wmb ();
@@ -637,7 +637,6 @@ static int mlx5_cmd_invoke(struct mlx5_c {struct mlx5_cmd *cmd = &dev->cmd;struct mlx5_cmd_work_ent *ent; -ktime_t t1, t2, delta;struct mlx5_cmd_stats *stats;int err =0 ;s64 ds;
@@ -668,10 +667,7 @@ static int mlx5_cmd_invoke(struct mlx5_cif (err == -ETIMEDOUT ) goto out; - t1 =timespec_to_ktime (ent->ts1); - t2 =timespec_to_ktime (ent->ts2); - delta =ktime_sub (t2, t1); - ds =ktime_to_ns (delta); + ds = ent->ts2 - ent->ts1; op =be16_to_cpu (((struct mlx5_inbox_hdr *)in->first.data)->opcode);if (op <ARRAY_SIZE (cmd->stats)) { stats = &cmd->stats[op];
@@ -1135,7 +1131,6 @@ void mlx5_cmd_comp_handler(struct mlx5_cvoid *context;int err;int i; -ktime_t t1, t2, delta;s64 ds;struct mlx5_cmd_stats *stats;unsigned long flags;
@@ -1149,7 +1144,7 @@ void mlx5_cmd_comp_handler(struct mlx5_c sem = &cmd->pages_sem; else sem = &cmd->sem; -ktime_get_ts (&ent->ts2); + ent->ts2 =ktime_get_ns ();memcpy (ent->out->first.data, ent->lay->out,sizeof (ent->lay->out));dump_command (dev, ent,0 );if (!ent->ret) {
@@ -1163,10 +1158,7 @@ void mlx5_cmd_comp_handler(struct mlx5_c }free_ent (cmd, ent->idx);if (ent->callback) { - t1 =timespec_to_ktime (ent->ts1); - t2 =timespec_to_ktime (ent->ts2); - delta =ktime_sub (t2, t1); - ds =ktime_to_ns (delta); + ds = ent->ts2 - ent->ts1;if (ent->op <ARRAY_SIZE (cmd->stats)) { stats = &cmd->stats[ent->op];spin_lock_irqsave (&stats->lock, flags);
include/linux/mlx5/driver.h +2 -2
@@ -604,8 +604,8 @@ struct mlx5_cmd_work_ent {int page_queue;u8 status;u8 token; -struct timespec ts1; -struct timespec ts2; +u64 ts1; +u64 ts2;u16 op; };
PATCH v2 39/64 fs: lockd: Use ktime_get_ns()
In reply to Thomas GleixnerReplace the ever recurring:
ts = ktime_get_ts();
ns = timespec_to_ns(&ts);
with
ns = ktime_get_ns();--- fs/lockd/mon.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/lockd/mon.c +1 -3
@@ -306,11 +306,9 @@ static struct nsm_handle *nsm_lookup_pristatic void nsm_init_private (struct nsm_handle *nsm) {u64 *p = (u64 *)&nsm->sm_priv.data; -struct timespec ts;s64 ns; -ktime_get_ts (&ts); - ns =timespec_to_ns (&ts); + ns =ktime_get_ns ();put_unaligned (ns, p);put_unaligned ((unsigned long )nsm, p +1 ); }
PATCH v2 40/64 hwmon: ibmaem: Use ktime_get_ns()
In reply to Thomas GleixnerUsing the wall clock time for delta time calculations is wrong to begin with because wall clock time can be set from userspace and NTP. Such data wants to be based on clock monotonic.
The calcuations also are done on a nanosecond basis. Use the nanoseconds based interface right away.
--- drivers/hwmon/ibmaem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/hwmon/ibmaem.c +2 -4
@@ -842,11 +842,10 @@ static ssize_t aem_show_power(struct devstruct aem_data *data =dev_get_drvdata (dev);u64 before, after, delta, time;signed long leftover; -struct timespec b, a;mutex_lock (&data->lock);update_aem_energy_one (data, attr->index); -getnstimeofday (&b); + time =ktime_get_ns (); before = data->energy[attr->index]; leftover = schedule_timeout_interruptible(
@@ -858,11 +857,10 @@ static ssize_t aem_show_power(struct dev }update_aem_energy_one (data, attr->index); -getnstimeofday (&a); + time =ktime_get_ns () - time; after = data->energy[attr->index];mutex_unlock (&data->lock); - time =timespec_to_ns (&a) -timespec_to_ns (&b); delta = (after - before) *UJ_PER_MJ ; return sprintf(buf, "%llu\n",
Re: [patch V2 40/64] hwmon: ibmaem: Use ktime_get_ns()
Hi Thomas,
Quoted 5 lines · Thomas Gleixner
Using the wall clock time for delta time calculations is wrong to begin with because wall clock time can be set from userspace and NTP. Such data wants to be based on clock monotonic.
The calcuations also are done on a nanosecond basis. Use the
Typo: calculations
Quoted 28 lines
nanoseconds based interface right away.
Signed-off-by Thomas Gleixner <[email protected]>Cc Jean Delvare <[email protected]>--- drivers/hwmon/ibmaem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)Index:
tip/drivers/hwmon/ibmaem.c
===================================================================drivers/hwmon/ibmaem.c +1
-2@@ -842,11 +842,10 @@ static ssize_t aem_show_power(struct devstruct aem_data *data =dev_get_drvdata (dev);u64 before, after, delta, time;signed long leftover; -struct timespec b, a;mutex_lock (&data->lock);update_aem_energy_one (data, attr->index); -getnstimeofday (&b); + time =ktime_get_ns (); before = data->energy[attr->index]; leftover = schedule_timeout_interruptible(@@ -858,11 +857,10 @@ static ssize_t aem_show_power(struct dev }
drivers/hwmon/ibmaem.c static ssize_t aem_show_power(struct dev · line 866
Quoted 10 lines
drivers/hwmon/ibmaem.c +1
-2update_aem_energy_one (data, attr->index); -getnstimeofday (&a); + time =ktime_get_ns () - time; after = data->energy[attr->index];mutex_unlock (&data->lock); - time =timespec_to_ns (&a) -timespec_to_ns (&b); delta = (after - before) *UJ_PER_MJ ; return sprintf(buf, "%llu\n",
I'm not familiar with the driver and I can't test it, and I'm not familiar with kernel timekeeping either. All I can say is that the changes look reasonable and good.
Darrick, maybe you want to comment or test?
Signature · 2 lines
Jean Delvare SUSE L3 Support
Re: [patch V2 40/64] hwmon: ibmaem: Use ktime_get_ns()
Quoted 54 lines · Jean Delvare
Hi Thomas,
Quoted 5 lines · Thomas Gleixner
Using the wall clock time for delta time calculations is wrong to begin with because wall clock time can be set from userspace and NTP. Such data wants to be based on clock monotonic.
The calcuations also are done on a nanosecond basis. Use the
Typo: calculations
Quoted 28 lines
nanoseconds based interface right away.
Signed-off-by Thomas Gleixner <[email protected]>Cc Jean Delvare <[email protected]>--- drivers/hwmon/ibmaem.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)Index:
tip/drivers/hwmon/ibmaem.c
===================================================================drivers/hwmon/ibmaem.c +1
-2@@ -842,11 +842,10 @@ static ssize_t aem_show_power(struct devstruct aem_data *data =dev_get_drvdata (dev);u64 before, after, delta, time;signed long leftover; -struct timespec b, a;mutex_lock (&data->lock);update_aem_energy_one (data, attr->index); -getnstimeofday (&b); + time =ktime_get_ns (); before = data->energy[attr->index]; leftover = schedule_timeout_interruptible(@@ -858,11 +857,10 @@ static ssize_t aem_show_power(struct dev }drivers/hwmon/ibmaem.c static ssize_t aem_show_power(struct dev · line 866
Quoted 10 lines
drivers/hwmon/ibmaem.c +1
-2update_aem_energy_one (data, attr->index); -getnstimeofday (&a); + time =ktime_get_ns () - time; after = data->energy[attr->index];mutex_unlock (&data->lock); - time =timespec_to_ns (&a) -timespec_to_ns (&b); delta = (after - before) *UJ_PER_MJ ; return sprintf(buf, "%llu\n",I'm not familiar with the driver and I can't test it, and I'm not familiar with kernel timekeeping either. All I can say is that the changes look reasonable and good.
Quoted 3 lines
Acked-by Jean Delvare <[email protected]>Darrick, maybe you want to comment or test?
Looks ok to me from a theoretical point of view, but since I no longer work at IBM, I don't have hardware for a real test. :(
--D
Quoted 5 lines
Signature · 3 lines
-- Jean Delvare SUSE L3 Support
PATCH v2 41/64 iio: Use ktime_get_real_ns()
In reply to Thomas GleixnerNo idea why iio needs wall clock based time stamps, but we can avoid the timespec conversion dance by using the new interfaces.
--- include/linux/iio/iio.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
include/linux/iio/iio.h +1 -8
@@ -277,14 +277,7 @@ static inline bool iio_channel_has_info(**/ static inline s64 iio_get_time_ns (void ) { -struct timespec ts; -/* -* calls getnstimeofday. -* If hrtimers then up to ns accurate, if not microsecond. -*/ -ktime_get_real_ts (&ts); - -return timespec_to_ns (&ts); +return ktime_get_real_ns (); }/* Device operating modes */
PATCH v2 42/64 arm: bL_switcher:k Use ktime_get_real_ns()
In reply to Thomas GleixnerUse the nanoseconds based interface instead of converting from a timespec.
--- arch/arm/common/bL_switcher.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
arch/arm/common/bL_switcher.c +3 -13
@@ -58,16 +58,6 @@ static int read_mpidr(void) }/* -* Get a global nanosecond time stamp for tracing. -*/ -static s64 get_ns (void ) -{ -struct timespec ts; -getnstimeofday (&ts); -return timespec_to_ns (&ts); -} - -/* * bL switcher core code. */
@@ -224,7 +214,7 @@ static int bL_switch_to(unsigned int new*/ local_irq_disable ();local_fiq_disable (); -trace_cpu_migrate_begin (get_ns (), ob_mpidr); +trace_cpu_migrate_begin (ktime_get_real_ns (), ob_mpidr);/* redirect GIC's SGIs to our counterpart */ gic_migrate_target (bL_gic_id[ib_cpu][ib_cluster]);
@@ -267,7 +257,7 @@ static int bL_switch_to(unsigned int new tdev->evtdev->next_event, 1); } -trace_cpu_migrate_finish (get_ns (), ib_mpidr); +trace_cpu_migrate_finish (ktime_get_real_ns (), ib_mpidr);local_fiq_enable ();local_irq_enable ();
@@ -558,7 +548,7 @@ int bL_switcher_get_logical_index(u32 mpstatic void bL_switcher_trace_trigger_cpu (void *__always_unused info) { -trace_cpu_migrate_current (get_ns (),read_mpidr ()); +trace_cpu_migrate_current (ktime_get_real_ns (),read_mpidr ()); }int bL_switcher_trace_trigger (void )
PATCH v2 43/64 x86: kvm: Use ktime_get_boot_ns()
In reply to Thomas GleixnerUse the new nanoseconds based interface and get rid of the timespec conversion dance.
--- arch/x86/kvm/x86.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
arch/x86/kvm/x86.c +1 -5
@@ -1109,11 +1109,7 @@ static void kvm_get_time_scale(uint32_tstatic inline u64 get_kernel_ns (void ) { -struct timespec ts; - -ktime_get_ts (&ts); -monotonic_to_bootbased (&ts); -return timespec_to_ns (&ts); +return ktime_get_boot_ns (); }#ifdef CONFIG_X86_64
Re: [patch V2 43/64] x86: kvm: Use ktime_get_boot_ns()
Il 16/07/2014 23:04, Thomas Gleixner ha scritto:
Quoted 19 lines
Use the new nanoseconds based interface and get rid of the timespec conversion dance.
Signed-off-by Thomas Gleixner <[email protected]>Cc Gleb Natapov <[email protected]>--- arch/x86/kvm/x86.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)Index:
tip/arch/x86/kvm/x86.c
===================================================================arch/x86/kvm/x86.c +0
-0@@ -1109,11 +1109,7 @@ static void kvm_get_time_scale(uint32_tstatic inline u64 get_kernel_ns (void )
arch/x86/kvm/x86.c static void kvm_get_time_scale(uint32_t · line 1115
Quoted 10 lines
arch/x86/kvm/x86.c +1
-5{ -struct timespec ts; - -ktime_get_ts (&ts); -monotonic_to_bootbased (&ts); -return timespec_to_ns (&ts); +return ktime_get_boot_ns (); }#ifdef CONFIG_X86_64
I will remove get_kernel_ns if you don't do that for me...
Paolo
PATCH v2 44/64 x86: kvm: Make kvm_get_time_and_clockread() nanoseconds based
In reply to Thomas GleixnerConvert the relevant base data right away to nanoseconds instead of doing the conversion on every readout. Reduces text size by 160 bytes.
--- arch/x86/kvm/x86.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-)
arch/x86/kvm/x86.c +14 -30
@@ -984,9 +984,8 @@ struct pvclock_gtod_data {u32 shift; } clock; -/* open coded 'struct timespec' */ -u64 monotonic_time_snsec; -time_t monotonic_time_sec; +u64 boot_ns; +u64 nsec_base; };static struct pvclock_gtod_data pvclock_gtod_data;
@@ -994,6 +993,9 @@ static struct pvclock_gtod_data pvclock_static void update_pvclock_gtod (struct timekeeper *tk) {struct pvclock_gtod_data *vdata = &pvclock_gtod_data; +u64 boot_ns; + + boot_ns =ktime_to_ns (ktime_add (tk->base_mono, tk->offs_boot));write_seqcount_begin (&vdata->seq);
@@ -1004,17 +1006,8 @@ static void update_pvclock_gtod(struct t vdata->clock.mult = tk->mult; vdata->clock.shift = tk->shift; - vdata->monotonic_time_sec = tk->xtime_sec - + tk->wall_to_monotonic.tv_sec; - vdata->monotonic_time_snsec = tk->xtime_nsec - + (tk->wall_to_monotonic.tv_nsec - << tk->shift); -while (vdata->monotonic_time_snsec >= - (((u64 )NSEC_PER_SEC ) << tk->shift)) { - vdata->monotonic_time_snsec -= - ((u64 )NSEC_PER_SEC ) << tk->shift; - vdata->monotonic_time_sec++; - } + vdata->boot_ns = boot_ns; + vdata->nsec_base = tk->xtime_nsec;write_seqcount_end (&vdata->seq); }
@@ -1371,23 +1364,22 @@ static inline u64 vgettsc(cycle_t *cyclereturn v * gtod->clock.mult; } -static int do_monotonic (struct timespec *ts,cycle_t *cycle_now) +static int do_monotonic_boot (s64 *t,cycle_t *cycle_now) { +struct pvclock_gtod_data *gtod = &pvclock_gtod_data;unsigned long seq; -u64 ns;int mode; -struct pvclock_gtod_data *gtod = &pvclock_gtod_data; +u64 ns; - ts->tv_nsec =0 ;do { seq =read_seqcount_begin (>od->seq); mode = gtod->clock.vclock_mode; - ts->tv_sec = gtod->monotonic_time_sec; - ns = gtod->monotonic_time_snsec; + ns = gtod->nsec_base; ns +=vgettsc (cycle_now); ns >>= gtod->clock.shift; + ns += gtod->boot_ns; }while (unlikely (read_seqcount_retry (>od->seq, seq))); -timespec_add_ns (ts, ns); + *t = ns;return mode; }
@@ -1395,19 +1387,11 @@ static int do_monotonic(struct timespec/* returns true if host is using tsc clocksource */ static bool kvm_get_time_and_clockread (s64 *kernel_ns,cycle_t *cycle_now) { -struct timespec ts; -/* checked again under seqlock below */ if (pvclock_gtod_data.clock.vclock_mode !=VCLOCK_TSC )return false; -if (do_monotonic (&ts, cycle_now) !=VCLOCK_TSC ) -return false; - -monotonic_to_bootbased (&ts); - *kernel_ns =timespec_to_ns (&ts); - -return true; +return do_monotonic_boot (kernel_ns, cycle_now) ==VCLOCK_TSC ; }#endif
Re: [patch V2 44/64] x86: kvm: Make kvm_get_time_and_clockread() nanoseconds based
Il 16/07/2014 23:04, Thomas Gleixner ha scritto:
Quoted 99 lines
Convert the relevant base data right away to nanoseconds instead of doing the conversion on every readout. Reduces text size by 160 bytes.
Signed-off-by Thomas Gleixner <[email protected]>Cc Gleb Natapov <[email protected]>--- arch/x86/kvm/x86.c | 44 ++++++++++++++------------------------------ 1 file changed, 14 insertions(+), 30 deletions(-)Index:
tip/arch/x86/kvm/x86.c
===================================================================arch/x86/kvm/x86.c +13
-24@@ -984,9 +984,8 @@ struct pvclock_gtod_data {u32 shift; } clock; -/* open coded 'struct timespec' */ -u64 monotonic_time_snsec; -time_t monotonic_time_sec; +u64 boot_ns; +u64 nsec_base; };static struct pvclock_gtod_data pvclock_gtod_data;@@ -994,6 +993,9 @@ static struct pvclock_gtod_data pvclock_static void update_pvclock_gtod (struct timekeeper *tk) {struct pvclock_gtod_data *vdata = &pvclock_gtod_data; +u64 boot_ns; + + boot_ns =ktime_to_ns (ktime_add (tk->base_mono, tk->offs_boot));write_seqcount_begin (&vdata->seq);@@ -1004,17 +1006,8 @@ static void update_pvclock_gtod(struct t vdata->clock.mult = tk->mult; vdata->clock.shift = tk->shift; - vdata->monotonic_time_sec = tk->xtime_sec - + tk->wall_to_monotonic.tv_sec; - vdata->monotonic_time_snsec = tk->xtime_nsec - + (tk->wall_to_monotonic.tv_nsec - << tk->shift); -while (vdata->monotonic_time_snsec >= - (((u64 )NSEC_PER_SEC ) << tk->shift)) { - vdata->monotonic_time_snsec -= - ((u64 )NSEC_PER_SEC ) << tk->shift; - vdata->monotonic_time_sec++; - } + vdata->boot_ns = boot_ns; + vdata->nsec_base = tk->xtime_nsec;write_seqcount_end (&vdata->seq); }@@ -1371,23 +1364,22 @@ static inline u64 vgettsc(cycle_t *cyclereturn v * gtod->clock.mult; } -static int do_monotonic (struct timespec *ts,cycle_t *cycle_now) +static int do_monotonic_boot (s64 *t,cycle_t *cycle_now) { +struct pvclock_gtod_data *gtod = &pvclock_gtod_data;unsigned long seq; -u64 ns;int mode; -struct pvclock_gtod_data *gtod = &pvclock_gtod_data; +u64 ns; - ts->tv_nsec =0 ;do { seq =read_seqcount_begin (>od->seq); mode = gtod->clock.vclock_mode; - ts->tv_sec = gtod->monotonic_time_sec; - ns = gtod->monotonic_time_snsec; + ns = gtod->nsec_base; ns +=vgettsc (cycle_now); ns >>= gtod->clock.shift; + ns += gtod->boot_ns; }while (unlikely (read_seqcount_retry (>od->seq, seq))); -timespec_add_ns (ts, ns); + *t = ns;return mode; }@@ -1395,19 +1387,11 @@ static int do_monotonic(struct timespec/* returns true if host is using tsc clocksource */ static bool kvm_get_time_and_clockread (s64 *kernel_ns,cycle_t *cycle_now) { -struct timespec ts; -/* checked again under seqlock below */ if (pvclock_gtod_data.clock.vclock_mode !=VCLOCK_TSC )return false; -if (do_monotonic (&ts, cycle_now) !=VCLOCK_TSC )
arch/x86/kvm/x86.c static int do_monotonic(struct timespec · line 1397
Quoted 10 lines
arch/x86/kvm/x86.c +1
-6-return false; - -monotonic_to_bootbased (&ts); - *kernel_ns =timespec_to_ns (&ts); - - return true; + return do_monotonic_boot(kernel_ns, cycle_now) == VCLOCK_TSC; }#endif
PATCH v2 45/64 timekeeping: Remove monotonic_to_bootbased
In reply to Thomas GleixnerNo more users.
--- include/linux/timekeeping.h | 1 - kernel/time/timekeeping.c | 15 --------------- 2 files changed, 16 deletions(-)
include/linux/timekeeping.h +0 -1
@@ -27,7 +27,6 @@ struct timespec __current_kernel_time(vo*/ struct timespec get_monotonic_coarse (void );extern void getrawmonotonic (struct timespec *ts); -extern void monotonic_to_bootbased (struct timespec *ts);extern void get_monotonic_boottime (struct timespec *ts);extern void ktime_get_ts64 (struct timespec64 *ts);
kernel/time/timekeeping.c +0 -15
@@ -1570,21 +1570,6 @@ void get_monotonic_boottime(struct times }EXPORT_SYMBOL_GPL (get_monotonic_boottime); -/** -* monotonic_to_bootbased - Convert the monotonic time to boot based. -* @ts: pointer to the timespec to be converted -*/ -void monotonic_to_bootbased (struct timespec *ts) -{ -struct timekeeper *tk = &tk_core.timekeeper; -struct timespec64 ts64; - - ts64 =timespec_to_timespec64 (*ts); - ts64 =timespec64_add (ts64, tk->total_sleep_time); - *ts =timespec64_to_timespec (ts64); -} -EXPORT_SYMBOL_GPL (monotonic_to_bootbased); -unsigned long get_seconds (void ) {struct timekeeper *tk = &tk_core.timekeeper;
PATCH v2 46/64 timekeeping: Use ktime_get_boottime() for get_monotonic_boottime()
In reply to Thomas Gleixnerget_monotonic_boottime() is not used in fast pathes, so the extra timespec conversion is not problematic.
--- include/linux/timekeeping.h | 9 ++++++++- kernel/time/timekeeping.c | 34 ---------------------------------- 2 files changed, 8 insertions(+), 35 deletions(-)
include/linux/timekeeping.h +8 -1
@@ -27,7 +27,6 @@ struct timespec __current_kernel_time(vo*/ struct timespec get_monotonic_coarse (void );extern void getrawmonotonic (struct timespec *ts); -extern void get_monotonic_boottime (struct timespec *ts);extern void ktime_get_ts64 (struct timespec64 *ts);extern int __getnstimeofday64 (struct timespec64 *tv);
@@ -160,6 +159,14 @@ static inline u64 ktime_get_boot_ns(void }/* +* Timespec interfaces utilizing the ktime based ones +*/ +static inline void get_monotonic_boottime (struct timespec *ts) +{ + *ts =ktime_to_timespec (ktime_get_boottime ()); +} + +/* * RTC specific */ extern void timekeeping_inject_sleeptime (struct timespec *delta);
kernel/time/timekeeping.c +0 -34
@@ -1536,40 +1536,6 @@ void getboottime(struct timespec *ts) }EXPORT_SYMBOL_GPL (getboottime); -/** -* get_monotonic_boottime - Returns monotonic time since boot -* @ts: pointer to the timespec to be set -* -* Returns the monotonic time since boot in a timespec. -* -* This is similar to CLOCK_MONTONIC/ktime_get_ts, but also -* includes the time spent in suspend. -*/ -void get_monotonic_boottime (struct timespec *ts) -{ -struct timekeeper *tk = &tk_core.timekeeper; -struct timespec64 tomono, sleep, ret; -s64 nsec; -unsigned int seq; - -WARN_ON (timekeeping_suspended); - -do { - seq =read_seqcount_begin (&tk_core.seq); - ret.tv_sec = tk->xtime_sec; - nsec =timekeeping_get_ns (tk); - tomono = tk->wall_to_monotonic; - sleep = tk->total_sleep_time; - - }while (read_seqcount_retry (&tk_core.seq, seq)); - - ret.tv_sec += tomono.tv_sec + sleep.tv_sec; - ret.tv_nsec =0 ; -timespec64_add_ns (&ret, nsec + tomono.tv_nsec + sleep.tv_nsec); - *ts =timespec64_to_timespec (ret); -} -EXPORT_SYMBOL_GPL (get_monotonic_boottime); -unsigned long get_seconds (void ) {struct timekeeper *tk = &tk_core.timekeeper;
PATCH v2 47/64 timekeeping: Simplify getboottime()
In reply to Thomas GleixnerSubtracting plain nsec values and converting to timespec is simpler than the whole timespec math. Not really fastpath code, so the division is not an issue.
--- kernel/time/timekeeping.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/time/timekeeping.c +2 -7
@@ -1525,14 +1525,9 @@ out:void getboottime (struct timespec *ts) {struct timekeeper *tk = &tk_core.timekeeper; -struct timespec boottime = { - .tv_sec = tk->wall_to_monotonic.tv_sec + - tk->total_sleep_time.tv_sec, - .tv_nsec = tk->wall_to_monotonic.tv_nsec + - tk->total_sleep_time.tv_nsec - }; +ktime_t t =ktime_sub (tk->offs_real, tk->offs_boot); -set_normalized_timespec (ts, -boottime.tv_sec, -boottime.tv_nsec); + *ts =ktime_to_timespec (t); }EXPORT_SYMBOL_GPL (getboottime);
PATCH v2 48/64 timekeeping: Remove timekeeper.total_sleep_time
In reply to Thomas GleixnerNo more users. Remove it
--- include/linux/timekeeper_internal.h | 6 ++---- kernel/time/timekeeping.c | 14 +++----------- 2 files changed, 5 insertions(+), 15 deletions(-)
include/linux/timekeeper_internal.h +2 -4
@@ -20,8 +20,8 @@* the tv_nsec part positive so we can use the usual normalization. * * wall_to_monotonic is moved after resume from suspend for the -* monotonic time not to jump. We need to add total_sleep_time to -* wall_to_monotonic to get the real boot based time offset. +* monotonic time not to jump. To calculate the real boot time offset +* we need to do offs_real - offs_boot. * * - wall_to_monotonic is no longer the boot time, getboottime must be * used instead.
@@ -51,8 +51,6 @@ struct timekeeper {/* Offset clock monotonic -> clock tai */ ktime_t offs_tai; -/* time spent in suspend */ -struct timespec64 total_sleep_time;/* The current UTC to TAI offset in seconds */ s32 tai_offset;
kernel/time/timekeeping.c +3 -11
@@ -97,13 +97,9 @@ static void tk_set_wall_to_mono(struct t tk->offs_tai =ktime_add (tk->offs_real,ktime_set (tk->tai_offset,0 )); } -static void tk_set_sleep_time (struct timekeeper *tk,struct timespec64 t) +static inline void tk_update_sleep_time (struct timekeeper *tk,ktime_t delta) { -/* Verify consistency before modifying */ -WARN_ON_ONCE (tk->offs_boot.tv64 !=timespec64_to_ktime (tk->total_sleep_time).tv64); - - tk->total_sleep_time = t; - tk->offs_boot =timespec64_to_ktime (t); + tk->offs_boot =ktime_add (tk->offs_boot, delta); }/**
@@ -919,10 +915,6 @@ void __init timekeeping_init(void)set_normalized_timespec64 (&tmp, -boot.tv_sec, -boot.tv_nsec);tk_set_wall_to_mono (tk, tmp); - tmp.tv_sec =0 ; - tmp.tv_nsec =0 ; -tk_set_sleep_time (tk, tmp); -timekeeping_update (tk,TK_MIRROR );write_seqcount_end (&tk_core.seq);
@@ -950,7 +942,7 @@ static void __timekeeping_inject_sleepti }tk_xtime_add (tk, delta);tk_set_wall_to_mono (tk,timespec64_sub (tk->wall_to_monotonic, *delta)); -tk_set_sleep_time (tk,timespec64_add (tk->total_sleep_time, *delta)); +tk_update_sleep_time (tk,timespec64_to_ktime (*delta));tk_debug_account_sleep_time (delta); }
PATCH v2 49/64 timekeeping: Simplify timekeeping_clocktai()
In reply to Thomas Gleixnertimekeeping_clocktai() is not used in fast pathes, so the extra timespec conversion is not problematic.
--- include/linux/timekeeping.h | 5 +++++ kernel/time/timekeeping.c | 31 ------------------------------- 2 files changed, 5 insertions(+), 31 deletions(-)
include/linux/timekeeping.h +5 -0
@@ -166,6 +166,11 @@ static inline void get_monotonic_boottim *ts =ktime_to_timespec (ktime_get_boottime ()); } +static inline void timekeeping_clocktai (struct timespec *ts) +{ + *ts =ktime_to_timespec (ktime_get_clocktai ()); +} +/* * RTC specific */
kernel/time/timekeeping.c +0 -31
@@ -497,37 +497,6 @@ void ktime_get_ts64(struct timespec64 *t }EXPORT_SYMBOL_GPL (ktime_get_ts64); - -/** -* timekeeping_clocktai - Returns the TAI time of day in a timespec -* @ts: pointer to the timespec to be set -* -* Returns the time of day in a timespec. -*/ -void timekeeping_clocktai (struct timespec *ts) -{ -struct timekeeper *tk = &tk_core.timekeeper; -struct timespec64 ts64; -unsigned long seq; -u64 nsecs; - -WARN_ON (timekeeping_suspended); - -do { - seq =read_seqcount_begin (&tk_core.seq); - - ts64.tv_sec = tk->xtime_sec + tk->tai_offset; - nsecs =timekeeping_get_ns (tk); - - }while (read_seqcount_retry (&tk_core.seq, seq)); - - ts64.tv_nsec =0 ; -timespec64_add_ns (&ts64, nsecs); - *ts =timespec64_to_timespec (ts64); - -} -EXPORT_SYMBOL (timekeeping_clocktai); -#ifdef CONFIG_NTP_PPS /**
PATCH v2 50/64 hangcheck-timer: Use ktime_get_ns()
In reply to Thomas GleixnerThere is no point in having a S390 private implementation and there is no point in using the raw monotonic time. The NTP freqeuency adjustment of CLOCK_MONOTONIC is really not doing any harm for the hang check timer.
Use ktime_get_ns() for everything and get rid of the timespec conversions.
V2: Drop the raw monotonic and the S390 special case
--- drivers/char/hangcheck-timer.c | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-)
drivers/char/hangcheck-timer.c +5 -28
@@ -49,7 +49,7 @@#include <asm/uaccess.h> #include <linux/sysrq.h> #include <linux/timer.h> -#include <linux/time.h> +#include <linux/hrtimer.h> #define VERSION_STR "0.9.1"
@@ -117,24 +117,7 @@ __setup("hcheck_reboot", hangcheck_parse__setup ("hcheck_dump_tasks" , hangcheck_parse_dump_tasks);#endif /* not MODULE */ -#if defined(CONFIG_S390 ) -# define HAVE_MONOTONIC -# define TIMER_FREQ 1000000000ULL -#else -# define TIMER_FREQ 1000000000ULL -#endif - -#ifdef HAVE_MONOTONIC -extern unsigned long long monotonic_clock (void ); -#else -static inline unsigned long long monotonic_clock (void ) -{ -struct timespec ts; -getrawmonotonic (&ts); -return timespec_to_ns (&ts); -} -#endif /* HAVE_MONOTONIC */ - +#define TIMER_FREQ 1000000000ULL/* Last time scheduled */ static unsigned long long hangcheck_tsc, hangcheck_tsc_margin;
@@ -143,12 +126,11 @@ static void hangcheck_fire(unsigned long static DEFINE_TIMER(hangcheck_ticktock, hangcheck_fire, 0, 0); -static void hangcheck_fire (unsigned long data) {unsigned long long cur_tsc, tsc_diff; - cur_tsc =monotonic_clock (); + cur_tsc =ktime_get_ns ();if (cur_tsc > hangcheck_tsc) tsc_diff = cur_tsc - hangcheck_tsc;
@@ -177,7 +159,7 @@ static void hangcheck_fire(unsigned long tsc_diff, tsc_diff - hangcheck_tick*TIMER_FREQ);#endif mod_timer (&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ )); - hangcheck_tsc =monotonic_clock (); + hangcheck_tsc =ktime_get_ns (); }
@@ -185,16 +167,11 @@ static int __init hangcheck_init(void) {printk ("Hangcheck: starting hangcheck timer %s (tick is %d seconds, margin is %d seconds).\n" ,VERSION_STR , hangcheck_tick, hangcheck_margin); -#if defined (HAVE_MONOTONIC ) -printk ("Hangcheck: Using monotonic_clock().\n" ); -#else -printk ("Hangcheck: Using getrawmonotonic().\n" ); -#endif /* HAVE_MONOTONIC */ hangcheck_tsc_margin = (unsigned long long )(hangcheck_margin + hangcheck_tick); hangcheck_tsc_margin *= (unsigned long long )TIMER_FREQ ; - hangcheck_tsc =monotonic_clock (); + hangcheck_tsc =ktime_get_ns ();mod_timer (&hangcheck_ticktock, jiffies + (hangcheck_tick*HZ ));return 0 ;
Re: [patch V2 50/64] hangcheck-timer: Use ktime_get_ns()
Quoted 8 lines · Thomas Gleixner
There is no point in having a S390 private implementation and there is no point in using the raw monotonic time. The NTP freqeuency adjustment of
CLOCK_MONOTONICis really not doing any harm for the hang check timer.Use
ktime_get_ns()for everything and get rid of the timespec conversions.
Quoted 6 lines
V2: Drop the raw monotonic and the S390 special case
Signed-off-by Thomas Gleixner <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Heiko Carstens <[email protected]>
PATCH v2 51/64 timekeeping: Provide ktime_get_raw()
In reply to Thomas GleixnerProvide a ktime_t based interface for raw monotonic time.
--- include/linux/timekeeper_internal.h | 3 +++ include/linux/timekeeping.h | 6 ++++++ kernel/time/timekeeping.c | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+)
include/linux/timekeeper_internal.h +3 -0
@@ -54,6 +54,9 @@ struct timekeeper {/* The current UTC to TAI offset in seconds */ s32 tai_offset; +/* Monotonic raw base time */ +ktime_t base_raw; +/* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */ struct timespec64 raw_time;
include/linux/timekeeping.h +6 -0
@@ -107,6 +107,7 @@ enum tk_offsets {extern ktime_t ktime_get (void );extern ktime_t ktime_get_with_offset (enum tk_offsets offs);extern ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs); +extern ktime_t ktime_get_raw (void );/** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -158,6 +159,11 @@ static inline u64 ktime_get_boot_ns(voidreturn ktime_to_ns (ktime_get_boottime ()); } +static inline u64 ktime_get_raw_ns (void ) +{ +return ktime_to_ns (ktime_get_raw ()); +} +/* * Timespec interfaces utilizing the ktime based ones */
kernel/time/timekeeping.c +25 -0
@@ -305,6 +305,9 @@ static inline void tk_update_ktime_data( nsec *=NSEC_PER_SEC ; nsec += tk->wall_to_monotonic.tv_nsec; tk->base_mono =ns_to_ktime (nsec); + +/* Update the monotonic raw base */ + tk->base_raw =timespec64_to_ktime (tk->raw_time); }/* must hold timekeeper_lock */
@@ -467,6 +470,27 @@ ktime_t ktime_mono_to_any(ktime_t tmono,EXPORT_SYMBOL_GPL (ktime_mono_to_any);/** +* ktime_get_raw - Returns the raw monotonic time in ktime_t format +*/ +ktime_t ktime_get_raw (void ) +{ +struct timekeeper *tk = &tk_core.timekeeper; +unsigned int seq; +ktime_t base; +s64 nsecs; + +do { + seq =read_seqcount_begin (&tk_core.seq); + base = tk->base_raw; + nsecs =timekeeping_get_ns_raw (tk); + + }while (read_seqcount_retry (&tk_core.seq, seq)); + +return ktime_add_ns (base, nsecs); +} +EXPORT_SYMBOL_GPL (ktime_get_raw); + +/** * ktime_get_ts64 - get the monotonic clock in timespec64 format * @ts: pointer to timespec variable *
@@ -878,6 +902,7 @@ void __init timekeeping_init(void)tk_set_xtime (tk, &now); tk->raw_time.tv_sec =0 ; tk->raw_time.tv_nsec =0 ; + tk->base_raw.tv64 =0 ;if (boot.tv_sec ==0 && boot.tv_nsec ==0 ) boot =tk_xtime (tk);
PATCH v2 52/64 drm: i915: Use nsec based interfaces
In reply to Thomas GleixnerUse ktime_get_raw_ns() and get rid of the back and forth timespec conversions.
--- drivers/gpu/drm/i915/i915_drv.h | 2 +- drivers/gpu/drm/i915/i915_gem.c | 33 ++++++++++++--------------------- drivers/gpu/drm/i915/intel_pm.c | 12 +++++------- 3 files changed, 18 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +1 -1
@@ -931,7 +931,7 @@ struct intel_ilk_power_mgmt {unsigned long last_time1;unsigned long chipset_power;u64 last_count2; -struct timespec last_time2; +u64 last_time2;unsigned long gfx_power;u8 corr;
drivers/gpu/drm/i915/i915_gem.c +12 -21
@@ -1149,16 +1149,16 @@ static bool can_wait_boost(struct drm_i9static int __wait_seqno (struct intel_engine_cs *ring,u32 seqno,unsigned reset_counter,bool interruptible, -struct timespec *timeout, +s64 *timeout,struct drm_i915_file_private *file_priv) {struct drm_device *dev = ring->dev;struct drm_i915_private *dev_priv = dev->dev_private;const bool irq_test_in_progress =ACCESS_ONCE (dev_priv->gpu_error.test_irq_rings) &intel_ring_flag (ring); -struct timespec before, now;DEFINE_WAIT (wait);unsigned long timeout_expire; +s64 before, now;int ret;WARN (dev_priv->pm.irqs_disabled,"IRQs disabled\n" );
@@ -1166,7 +1166,7 @@ static int __wait_seqno(struct intel_engif (i915_seqno_passed (ring->get_seqno (ring, true), seqno))return 0 ; - timeout_expire = timeout ? jiffies +timespec_to_jiffies_timeout (timeout) :0 ; + timeout_expire = timeout ? jiffies +nsecs_to_jiffies ((u64)*timeout) :0 ;if (INTEL_INFO (dev)->gen >=6 &&can_wait_boost (file_priv)) {gen6_rps_boost (dev_priv);
@@ -1181,7 +1181,7 @@ static int __wait_seqno(struct intel_eng/* Record current time in case interrupted by signal, or wedged */ trace_i915_gem_request_wait_begin (ring, seqno); -getrawmonotonic (&before); + before =ktime_get_raw_ns ();for (;;) {struct timer_list timer;
@@ -1230,7 +1230,7 @@ static int __wait_seqno(struct intel_engdestroy_timer_on_stack (&timer); } } -getrawmonotonic (&now); + now =ktime_get_raw_ns ();trace_i915_gem_request_wait_end (ring, seqno);if (!irq_test_in_progress)
@@ -1239,10 +1239,9 @@ static int __wait_seqno(struct intel_engfinish_wait (&ring->irq_queue, &wait);if (timeout) { -struct timespec sleep_time =timespec_sub (now, before); - *timeout =timespec_sub (*timeout, sleep_time); -if (!timespec_valid (timeout))/* i.e. negative time remains */ -set_normalized_timespec (timeout,0 ,0 ); +s64 tres = *timeout - (now - before); + + *timeout = tres <0 ?0 : tres; }return ret;
@@ -2753,16 +2752,10 @@ i915_gem_wait_ioctl(struct drm_device *dstruct drm_i915_gem_wait *args = data;struct drm_i915_gem_object *obj;struct intel_engine_cs *ring =NULL ; -struct timespec timeout_stack, *timeout =NULL ;unsigned reset_counter;u32 seqno =0 ;int ret =0 ; -if (args->timeout_ns >=0 ) { - timeout_stack =ns_to_timespec (args->timeout_ns); - timeout = &timeout_stack; - } - ret =i915_mutex_lock_interruptible (dev);if (ret)return ret;
@@ -2787,9 +2780,9 @@ i915_gem_wait_ioctl(struct drm_device *d goto out;/* Do this after OLR check to make sure we make forward progress polling -* on this IOCTL with a 0 timeout (like busy ioctl) +* on this IOCTL with a timeout <=0 (like busy ioctl) */ -if (!args->timeout_ns) { +if (args->timeout_ns <=0 ) { ret = -ETIME ; goto out; }
@@ -2798,10 +2791,8 @@ i915_gem_wait_ioctl(struct drm_device *d reset_counter =atomic_read (&dev_priv->gpu_error.reset_counter);mutex_unlock (&dev->struct_mutex); - ret =__wait_seqno (ring, seqno, reset_counter, true, timeout, file->driver_priv); -if (timeout) - args->timeout_ns =timespec_to_ns (timeout); -return ret; +return __wait_seqno (ring, seqno, reset_counter, true, &args->timeout_ns, + file->driver_priv); out:drm_gem_object_unreference (&obj->base);
drivers/gpu/drm/i915/intel_pm.c +5 -7
@@ -2993,7 +2993,7 @@ static void ironlake_enable_drps(structI915_READ (0x112e0 ); dev_priv->ips.last_time1 =jiffies_to_msecs (jiffies); dev_priv->ips.last_count2 =I915_READ (0x112f4 ); -getrawmonotonic (&dev_priv->ips.last_time2); + dev_priv->ips.last_time2 =ktime_get_raw_ns ();spin_unlock_irq (&mchdev_lock); }
@@ -4314,18 +4314,16 @@ static u16 pvid_to_extvid(struct drm_i91static void __i915_update_gfx_val (struct drm_i915_private *dev_priv) { -struct timespec now, diff1; -u64 diff; -unsigned long diffms; +u64 now, diff, diffms;u32 count;assert_spin_locked (&mchdev_lock); -getrawmonotonic (&now); - diff1 =timespec_sub (now, dev_priv->ips.last_time2); + now =ktime_get_raw_ns (); + diffms = now - dev_priv->ips.last_time2; +do_div (diffms,NSEC_PER_MSEC );/* Don't divide by 0 */ - diffms = diff1.tv_sec *1000 + diff1.tv_nsec /1000000 ;if (!diffms)return ;
PATCH v2 53/64 drm: vmwgfx: Use nsec based interfaces
In reply to Thomas GleixnerNo point in converting timespecs back and forth.
--- drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 4 +-- drivers/gpu/drm/vmwgfx/vmwgfx_marker.c | 44 ++++++++++----------------------- 2 files changed, 16 insertions(+), 32 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +2 -2
@@ -159,8 +159,8 @@ struct vmw_surface {struct vmw_marker_queue {struct list_head head; -struct timespec lag; -struct timespec lag_time; +u64 lag; +u64 lag_time;spinlock_t lock; };
drivers/gpu/drm/vmwgfx/vmwgfx_marker.c +14 -30
94 lines not on this page · raw message
PATCH v2 54/64 wireless: ath9k: Get rid of timespec conversions
In reply to Thomas GleixnerWe have interfaces. Remove the open coded cruft. Reduces text size along with the code.
--- drivers/net/wireless/ath/ath9k/hw.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/wireless/ath/ath9k/hw.c +2 -5
@@ -1734,7 +1734,6 @@ int ath9k_hw_reset(struct ath_hw *ah, st struct ath9k_hw_cal_data *caldata, bool fastcc) {struct ath_common *common =ath9k_hw_common (ah); -struct timespec ts;u32 saveLedState;u32 saveDefAntenna;u32 macStaId1;
@@ -1784,8 +1783,7 @@ int ath9k_hw_reset(struct ath_hw *ah, st/* Save TSF before chip reset, a cold reset clears it */ tsf =ath9k_hw_gettsf64 (ah); -getrawmonotonic (&ts); - usec = ts.tv_sec *1000000ULL + ts.tv_nsec /1000 ; + usec =ktime_to_us (ktime_get_raw ()); saveLedState = REG_READ(ah, AR_CFG_LED) & (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
@@ -1818,8 +1816,7 @@ int ath9k_hw_reset(struct ath_hw *ah, st }/* Restore TSF */ -getrawmonotonic (&ts); - usec = ts.tv_sec *1000000ULL + ts.tv_nsec /1000 - usec; + usec =ktime_to_us (ktime_get_raw ()) - usec;ath9k_hw_settsf64 (ah, tsf + usec);if (AR_SREV_9280_20_OR_LATER (ah))
PATCH v2 55/64 clocksource: Make delta calculation a function
In reply to Thomas GleixnerWe want to move the TSC sanity check into core code to make NMI safe accessors to clock monotonic[_raw] possible. For this we need to sanity check the delta calculation. Create a helper function and convert all sites to use it.
[ Build fix from jstultz ]
--- kernel/time/clocksource.c | 12 +++++++----- kernel/time/timekeeping.c | 26 ++++++++++++++------------ kernel/time/timekeeping_internal.h | 6 ++++++ 3 files changed, 27 insertions(+), 17 deletions(-)
kernel/time/clocksource.c +7 -5
@@ -32,6 +32,7 @@#include <linux/kthread.h> #include "tick-internal.h" +#include "timekeeping_internal.h" void timecounter_init(struct timecounter *tc, const struct cyclecounter *cc,
@@ -249,7 +250,7 @@ void clocksource_mark_unstable(struct clstatic void clocksource_watchdog (unsigned long data) {struct clocksource *cs; -cycle_t csnow, wdnow; +cycle_t csnow, wdnow, delta;int64_t wd_nsec, cs_nsec;int next_cpu, reset_pending;
@@ -282,11 +283,12 @@ static void clocksource_watchdog(unsignecontinue ; } - wd_nsec =clocksource_cyc2ns ((wdnow - cs->wd_last) & watchdog->mask, - watchdog->mult, watchdog->shift); + delta =clocksource_delta (wdnow, cs->wd_last, watchdog->mask); + wd_nsec =clocksource_cyc2ns (delta, watchdog->mult, + watchdog->shift); - cs_nsec =clocksource_cyc2ns ((csnow - cs->cs_last) & - cs->mask, cs->mult, cs->shift); + delta =clocksource_delta (csnow, cs->cs_last, cs->mask); + cs_nsec =clocksource_cyc2ns (delta, cs->mult, cs->shift); cs->cs_last = csnow; cs->wd_last = wdnow;
kernel/time/timekeeping.c +14 -12
@@ -173,7 +173,7 @@ static inline u32 arch_gettimeoffset(voistatic inline s64 timekeeping_get_ns (struct timekeeper *tk) { -cycle_t cycle_now, cycle_delta; +cycle_t cycle_now, delta;struct clocksource *clock;s64 nsec;
@@ -182,9 +182,9 @@ static inline s64 timekeeping_get_ns(str cycle_now = clock->read (clock);/* calculate the delta since the last update_wall_time: */ - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask); - nsec = cycle_delta * tk->mult + tk->xtime_nsec; + nsec = delta * tk->mult + tk->xtime_nsec; nsec >>= tk->shift;/* If arch requires, add in get_arch_timeoffset() */
@@ -193,7 +193,7 @@ static inline s64 timekeeping_get_ns(strstatic inline s64 timekeeping_get_ns_raw (struct timekeeper *tk) { -cycle_t cycle_now, cycle_delta; +cycle_t cycle_now, delta;struct clocksource *clock;s64 nsec;
@@ -202,10 +202,10 @@ static inline s64 timekeeping_get_ns_raw cycle_now = clock->read (clock);/* calculate the delta since the last update_wall_time: */ - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask);/* convert delta to nanoseconds. */ - nsec =clocksource_cyc2ns (cycle_delta, clock->mult, clock->shift); + nsec =clocksource_cyc2ns (delta, clock->mult, clock->shift);/* If arch requires, add in get_arch_timeoffset() */ return nsec +arch_gettimeoffset ();
@@ -336,23 +336,23 @@ static void timekeeping_update(struct ti*/ static void timekeeping_forward_now (struct timekeeper *tk) { -cycle_t cycle_now, cycle_delta; +cycle_t cycle_now, delta;struct clocksource *clock;s64 nsec; clock = tk->clock; cycle_now = clock->read (clock); - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask); tk->cycle_last = clock->cycle_last = cycle_now; - tk->xtime_nsec += cycle_delta * tk->mult; + tk->xtime_nsec += delta * tk->mult;/* If arch requires, add in get_arch_timeoffset() */ tk->xtime_nsec += (u64 )arch_gettimeoffset () << tk->shift;tk_normalize_xtime (tk); - nsec =clocksource_cyc2ns (cycle_delta, clock->mult, clock->shift); + nsec =clocksource_cyc2ns (delta, clock->mult, clock->shift);timespec64_add_ns (&tk->raw_time, nsec); }
@@ -1026,7 +1026,8 @@ static void timekeeping_resume(void)u32 shift = clock->shift;s64 nsec =0 ; - cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + cycle_delta =clocksource_delta (cycle_now, clock->cycle_last, + clock->mask);/* * "cycle_delta * mutl" may cause 64 bits overflow, if the
@@ -1432,7 +1433,8 @@ void update_wall_time(void)#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET offset = real_tk->cycle_interval;#else - offset = (clock->read (clock) - clock->cycle_last) & clock->mask; + offset =clocksource_delta (clock->read (clock), clock->cycle_last, + clock->mask);#endif /* Check if there's really nothing to do */
kernel/time/timekeeping_internal.h +6 -0
@@ -3,6 +3,7 @@/* * timekeeping debug functions */ +#include <linux/clocksource.h> #include <linux/time.h> #ifdef CONFIG_DEBUG_FS
@@ -11,4 +12,9 @@ extern void tk_debug_account_sleep_time(#define tk_debug_account_sleep_time (x)#endif +static inline cycle_t clocksource_delta (cycle_t now,cycle_t last,cycle_t mask) +{ +return (now - last) & mask; +} +#endif /* _TIMEKEEPING_INTERNAL_H */
PATCH v2 56/64 clocksource: Move cycle_last validation to core code
In reply to Thomas GleixnerThe only user of the cycle_last validation is the x86 TSC. In order to provide NMI safe accessor functions for clock monotonic and monotonic_raw we need to do that in the core.
We can't do the TSC specific
if (now < cycle_last)
now = cycle_last;
for the other wrapping around clocksources, but TSC has CLOCKSOURCE_MASK(64) which actually does not mask out anything so if now is less than cycle_last the subtraction will give a negative result. So we can check for that in clocksource_delta() and return 0 for that case.
Implement and enable it for x86
--- arch/x86/Kconfig | 1 + arch/x86/kernel/tsc.c | 21 +++++++++------------ kernel/time/Kconfig | 5 +++++ kernel/time/timekeeping_internal.h | 9 +++++++++ 4 files changed, 24 insertions(+), 12 deletions(-)
arch/x86/Kconfig +1 -0
@@ -109,6 +109,7 @@ config X86 select CLOCKSOURCE_WATCHDOG select GENERIC_CLOCKEVENTS select ARCH_CLOCKSOURCE_DATA + select CLOCKSOURCE_VALIDATE_LAST_CYCLE select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC) select GENERIC_TIME_VSYSCALL select GENERIC_STRNCPY_FROM_USER
arch/x86/kernel/tsc.c +9 -12
@@ -951,7 +951,7 @@ core_initcall(cpufreq_tsc);static struct clocksource clocksource_tsc;/* -* We compare the TSC to the cycle_last value in the clocksource +* We used to compare the TSC to the cycle_last value in the clocksource * structure to avoid a nasty time-warp. This can be observed in a * very small window right after one CPU updated cycle_last under * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
@@ -961,26 +961,23 @@ static struct clocksource clocksource_ts* due to the unsigned delta calculation of the time keeping core * code, which is necessary to support wrapping clocksources like pm * timer. +* +* This sanity check is now done in the core timekeeping code. +* checking the result of read_tsc() - cycle_last for being negative. +* That works because CLOCKSOURCE_MASK(64) does not mask out any bit. */ static cycle_t read_tsc (struct clocksource *cs) { -cycle_t ret = (cycle_t )get_cycles (); - -return ret >= clocksource_tsc.cycle_last ? - ret : clocksource_tsc.cycle_last; -} - -static void resume_tsc (struct clocksource *cs) -{ -if (!boot_cpu_has (X86_FEATURE_NONSTOP_TSC_S3 )) - clocksource_tsc.cycle_last =0 ; +return (cycle_t )get_cycles (); } +/* +* .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc() +*/ static struct clocksource clocksource_tsc = { .name ="tsc" , .rating =300 , .read = read_tsc, - .resume = resume_tsc, .mask =CLOCKSOURCE_MASK (64 ), .flags =CLOCK_SOURCE_IS_CONTINUOUS |CLOCK_SOURCE_MUST_VERIFY ,
kernel/time/Kconfig +5 -0
@@ -12,6 +12,11 @@ config CLOCKSOURCE_WATCHDOG config ARCH_CLOCKSOURCE_DATA bool +# Clocksources require validation of the clocksource against the last +# cycle update - x86/TSC misfeature +config CLOCKSOURCE_VALIDATE_LAST_CYCLE + bool + # Timekeeping vsyscall support config GENERIC_TIME_VSYSCALL bool
kernel/time/timekeeping_internal.h +9 -0
@@ -12,9 +12,18 @@ extern void tk_debug_account_sleep_time(#define tk_debug_account_sleep_time (x)#endif +#ifdef CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE +static inline cycle_t clocksource_delta (cycle_t now,cycle_t last,cycle_t mask) +{ +cycle_t ret = (now - last) & mask; + +return (s64 ) ret >0 ? ret :0 ; +} +#else static inline cycle_t clocksource_delta (cycle_t now,cycle_t last,cycle_t mask) {return (now - last) & mask; } +#endif #endif /* _TIMEKEEPING_INTERNAL_H */
PATCH v2 57/64 clocksource: Get rid of cycle_last
In reply to Thomas Gleixnercycle_last was added to the clocksource to support the TSC validation. We moved that to the core code, so we can get rid of the extra copy.
--- arch/arm64/kernel/vdso.c | 2 +- arch/ia64/kernel/time.c | 4 ++-- arch/powerpc/kernel/time.c | 4 ++-- arch/s390/kernel/time.c | 2 +- arch/tile/kernel/time.c | 2 +- arch/x86/kernel/vsyscall_gtod.c | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/clocksource.h | 2 -- include/linux/timekeeper_internal.h | 7 ++++--- kernel/time/timekeeping.c | 23 +++++++++++------------ 10 files changed, 24 insertions(+), 26 deletions(-)
arch/arm64/kernel/vdso.c +1 -1
@@ -224,7 +224,7 @@ void update_vsyscall(struct timekeeper * vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;if (!use_syscall) { - vdso_data->cs_cycle_last = tk->clock->cycle_last; + vdso_data->cs_cycle_last = tk->cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; vdso_data->xtime_clock_nsec = tk->xtime_nsec; vdso_data->cs_mult = tk->mult;
arch/ia64/kernel/time.c +2 -2
@@ -441,7 +441,7 @@ void update_vsyscall_tz(void) }void update_vsyscall_old (struct timespec *wall,struct timespec *wtm, -struct clocksource *c,u32 mult) +struct clocksource *c,u32 mult,cycles_t cycle_last) {write_seqcount_begin (&fsyscall_gtod_data.seq);
@@ -450,7 +450,7 @@ void update_vsyscall_old(struct timespec fsyscall_gtod_data.clk_mult = mult; fsyscall_gtod_data.clk_shift = c->shift; fsyscall_gtod_data.clk_fsys_mmio = c->archdata.fsys_mmio; - fsyscall_gtod_data.clk_cycle_last = c->cycle_last; + fsyscall_gtod_data.clk_cycle_last = cycle_last;/* copy kernel time structures */ fsyscall_gtod_data.wall_time.tv_sec = wall->tv_sec;
arch/powerpc/kernel/time.c +2 -2
@@ -741,7 +741,7 @@ static cycle_t timebase_read(struct cloc }void update_vsyscall_old (struct timespec *wall_time,struct timespec *wtm, -struct clocksource *clock,u32 mult) +struct clocksource *clock,u32 mult,cycle_t cycle_last) {u64 new_tb_to_xs, new_stamp_xsec;u32 frac_sec;
@@ -774,7 +774,7 @@ void update_vsyscall_old(struct timespec* We expect the caller to have done the first increment of * vdso_data->tb_update_count already. */ - vdso_data->tb_orig_stamp = clock->cycle_last; + vdso_data->tb_orig_stamp = cycle_last; vdso_data->stamp_xsec = new_stamp_xsec; vdso_data->tb_to_xs = new_tb_to_xs; vdso_data->wtom_clock_sec = wtm->tv_sec;
arch/s390/kernel/time.c +1 -1
@@ -220,7 +220,7 @@ void update_vsyscall(struct timekeeper */* Make userspace gettimeofday spin until we're done. */ ++vdso_data->tb_update_count;smp_wmb (); - vdso_data->xtime_tod_stamp = tk->clock->cycle_last; + vdso_data->xtime_tod_stamp = tk->cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; vdso_data->xtime_clock_nsec = tk->xtime_nsec; vdso_data->wtom_clock_sec =
arch/tile/kernel/time.c +1 -1
@@ -269,7 +269,7 @@ void update_vsyscall(struct timekeeper */* Userspace gettimeofday will spin while this value is odd. */ ++vdso_data->tb_update_count;smp_wmb (); - vdso_data->xtime_tod_stamp = clock->cycle_last; + vdso_data->xtime_tod_stamp = tk->cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; vdso_data->xtime_clock_nsec = tk->xtime_nsec; vdso_data->wtom_clock_sec = wtm->tv_sec;
arch/x86/kernel/vsyscall_gtod.c +1 -1
@@ -32,7 +32,7 @@ void update_vsyscall(struct timekeeper */* copy vsyscall data */ vdata->vclock_mode = tk->clock->archdata.vclock_mode; - vdata->cycle_last = tk->clock->cycle_last; + vdata->cycle_last = tk->cycle_last; vdata->mask = tk->clock->mask; vdata->mult = tk->mult; vdata->shift = tk->shift;
arch/x86/kvm/x86.c +1 -1
@@ -1001,7 +1001,7 @@ static void update_pvclock_gtod(struct t/* copy pvclock gtod data */ vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode; - vdata->clock.cycle_last = tk->clock->cycle_last; + vdata->clock.cycle_last = tk->cycle_last; vdata->clock.mask = tk->clock->mask; vdata->clock.mult = tk->mult; vdata->clock.shift = tk->shift;
include/linux/clocksource.h +0 -2
@@ -162,7 +162,6 @@ extern u64 timecounter_cyc2time(struct t* @archdata: arch-specific data * @suspend: suspend function for the clocksource, if necessary * @resume: resume function for the clocksource, if necessary -* @cycle_last: most recent cycle counter value seen by ::read() * @owner: module reference, must be set by clocksource in modules */ struct clocksource {
@@ -171,7 +170,6 @@ struct clocksource {* clocksource itself is cacheline aligned. */ cycle_t (*read)(struct clocksource *cs); -cycle_t cycle_last;cycle_t mask;u32 mult;u32 shift;
include/linux/timekeeper_internal.h +4 -3
@@ -29,6 +29,8 @@struct timekeeper {/* Current clocksource used for timekeeping. */ struct clocksource *clock; +/* Last cycle value */ +cycle_t cycle_last;/* NTP adjusted clock multiplier */ u32 mult;/* The shift value of the current clocksource. */
@@ -62,8 +64,6 @@ struct timekeeper {/* Number of clock cycles in one NTP interval. */ cycle_t cycle_interval; -/* Last cycle value (also stored in clock->cycle_last) */ -cycle_t cycle_last;/* Number of clock shifted nano seconds in one NTP interval. */ u64 xtime_interval;/* shifted nano seconds left over when rounding cycle_interval */
@@ -91,7 +91,8 @@ extern void update_vsyscall_tz(void);#elif defined(CONFIG_GENERIC_TIME_VSYSCALL_OLD )extern void update_vsyscall_old (struct timespec *ts,struct timespec *wtm, -struct clocksource *c,u32 mult); +struct clocksource *c,u32 mult, +cycles_t cycle_last);extern void update_vsyscall_tz (void );#else
kernel/time/timekeeping.c +11 -12
@@ -121,7 +121,7 @@ static void tk_setup_internals(struct ti old_clock = tk->clock; tk->clock = clock; - tk->cycle_last = clock->cycle_last = clock->read (clock); + tk->cycle_last = clock->read (clock);/* Do the ns -> cycle conversion first, using original mult */ tmp =NTP_INTERVAL_LENGTH ;
@@ -182,7 +182,7 @@ static inline s64 timekeeping_get_ns(str cycle_now = clock->read (clock);/* calculate the delta since the last update_wall_time: */ - delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask); + delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask); nsec = delta * tk->mult + tk->xtime_nsec; nsec >>= tk->shift;
@@ -202,7 +202,7 @@ static inline s64 timekeeping_get_ns_raw cycle_now = clock->read (clock);/* calculate the delta since the last update_wall_time: */ - delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask); + delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask);/* convert delta to nanoseconds. */ nsec =clocksource_cyc2ns (delta, clock->mult, clock->shift);
@@ -218,7 +218,8 @@ static inline void update_vsyscall(strucstruct timespec xt; xt =tk_xtime (tk); -update_vsyscall_old (&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); +update_vsyscall_old (&xt, &tk->wall_to_monotonic, tk->clock, tk->mult, + tk->cycle_last); }static inline void old_vsyscall_fixup (struct timekeeper *tk)
@@ -342,8 +343,8 @@ static void timekeeping_forward_now(stru clock = tk->clock; cycle_now = clock->read (clock); - delta =clocksource_delta (cycle_now, clock->cycle_last, clock->mask); - tk->cycle_last = clock->cycle_last = cycle_now; + delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask); + tk->cycle_last = cycle_now; tk->xtime_nsec += delta * tk->mult;
@@ -1020,13 +1021,13 @@ static void timekeeping_resume(void)*/ cycle_now = clock->read (clock);if ((clock->flags &CLOCK_SOURCE_SUSPEND_NONSTOP ) && - cycle_now > clock->cycle_last) { + cycle_now > tk->cycle_last) {u64 num, max =ULLONG_MAX ;u32 mult = clock->mult;u32 shift = clock->shift;s64 nsec =0 ; - cycle_delta =clocksource_delta (cycle_now, clock->cycle_last, + cycle_delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask);/*
@@ -1053,7 +1054,7 @@ static void timekeeping_resume(void)__timekeeping_inject_sleeptime (tk, &ts_delta);/* Re-base the last cycle value */ - tk->cycle_last = clock->cycle_last = cycle_now; + tk->cycle_last = cycle_now; tk->ntp_error =0 ; timekeeping_suspended =0 ;timekeeping_update (tk,TK_MIRROR |TK_CLOCK_WAS_SET );
@@ -1433,7 +1434,7 @@ void update_wall_time(void)#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET offset = real_tk->cycle_interval;#else - offset =clocksource_delta (clock->read (clock), clock->cycle_last, + offset =clocksource_delta (clock->read (clock), tk->cycle_last, clock->mask);#endif
@@ -1477,8 +1478,6 @@ void update_wall_time(void) clock_set |=accumulate_nsecs_to_secs (tk);write_seqcount_begin (&tk_core.seq); -/* Update clock->cycle_last with the new value */ - clock->cycle_last = tk->cycle_last;/* * Update the real timekeeper. *
PATCH v2 58/64 timekeeping: Restructure the timekeeper some more
In reply to Thomas GleixnerAccess to time requires to touch two cachelines at minimum
1) The timekeeper data structure
2) The clocksource data structure
The access to the clocksource data structure can be avoided as almost all clocksource implementations ignore the argument to the read callback, which is a pointer to the clocksource.
But the core needs to touch it to access the members @read and @mask.
So we are better off by copying the @read function pointer and the @mask from the clocksource to the core data structure itself.
For the most used ktime_get() access all required data including the @read and @mask copies fits together with the sequence counter into a single 64 byte cacheline.
For the other time access functions we touch in the current code three cache lines in the worst case. But with the clocksource data copies we can reduce that to two adjacent cachelines, which is more efficient than disjunct cache lines.
--- include/linux/timekeeper_internal.h | 4 ++++ kernel/time/timekeeping.c | 35 +++++++++++++++-------------------- 2 files changed, 19 insertions(+), 20 deletions(-)
include/linux/timekeeper_internal.h +4 -0
@@ -29,6 +29,10 @@struct timekeeper {/* Current clocksource used for timekeeping. */ struct clocksource *clock; +/* Read function of @clock */ +cycle_t (*read)(struct clocksource *cs); +/* Bitmask for two's complement subtraction of non 64bit counters */ +cycle_t mask;/* Last cycle value */ cycle_t cycle_last;/* NTP adjusted clock multiplier */
kernel/time/timekeeping.c +15 -20
@@ -121,7 +121,9 @@ static void tk_setup_internals(struct ti old_clock = tk->clock; tk->clock = clock; - tk->cycle_last = clock->read (clock); + tk->read = clock->read; + tk->mask = clock->mask; + tk->cycle_last = tk->read (clock);/* Do the ns -> cycle conversion first, using original mult */ tmp =NTP_INTERVAL_LENGTH ;
@@ -174,15 +176,13 @@ static inline u32 arch_gettimeoffset(voistatic inline s64 timekeeping_get_ns (struct timekeeper *tk) {cycle_t cycle_now, delta; -struct clocksource *clock;s64 nsec;/* read clocksource: */ - clock = tk->clock; - cycle_now = clock->read (clock); + cycle_now = tk->read (tk->clock);/* calculate the delta since the last update_wall_time: */ - delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask); + delta =clocksource_delta (cycle_now, tk->cycle_last, tk->mask); nsec = delta * tk->mult + tk->xtime_nsec; nsec >>= tk->shift;
@@ -193,16 +193,15 @@ static inline s64 timekeeping_get_ns(strstatic inline s64 timekeeping_get_ns_raw (struct timekeeper *tk) { +struct clocksource *clock = tk->clock;cycle_t cycle_now, delta; -struct clocksource *clock;s64 nsec;/* read clocksource: */ - clock = tk->clock; - cycle_now = clock->read (clock); + cycle_now = tk->read (clock);/* calculate the delta since the last update_wall_time: */ - delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask); + delta =clocksource_delta (cycle_now, tk->cycle_last, tk->mask);/* convert delta to nanoseconds. */ nsec =clocksource_cyc2ns (delta, clock->mult, clock->shift);
@@ -337,13 +336,12 @@ static void timekeeping_update(struct ti*/ static void timekeeping_forward_now (struct timekeeper *tk) { +struct clocksource *clock = tk->clock;cycle_t cycle_now, delta; -struct clocksource *clock;s64 nsec; - clock = tk->clock; - cycle_now = clock->read (clock); - delta =clocksource_delta (cycle_now, tk->cycle_last, clock->mask); + cycle_now = tk->read (clock); + delta =clocksource_delta (cycle_now, tk->cycle_last, tk->mask); tk->cycle_last = cycle_now; tk->xtime_nsec += delta * tk->mult;
@@ -1019,7 +1017,7 @@ static void timekeeping_resume(void)* The less preferred source will only be tried if there is no better * usable source. The rtc part is handled separately in rtc core code. */ - cycle_now = clock->read (clock); + cycle_now = tk->read (clock);if ((clock->flags &CLOCK_SOURCE_SUSPEND_NONSTOP ) && cycle_now > tk->cycle_last) {u64 num, max =ULLONG_MAX ;
@@ -1028,7 +1026,7 @@ static void timekeeping_resume(void)s64 nsec =0 ; cycle_delta =clocksource_delta (cycle_now, tk->cycle_last, - clock->mask); + tk->mask);/* * "cycle_delta * mutl" may cause 64 bits overflow, if the
@@ -1415,7 +1413,6 @@ static cycle_t logarithmic_accumulation(*/ void update_wall_time (void ) { -struct clocksource *clock;struct timekeeper *real_tk = &tk_core.timekeeper;struct timekeeper *tk = &shadow_timekeeper;cycle_t offset;
@@ -1429,13 +1426,11 @@ void update_wall_time(void)if (unlikely (timekeeping_suspended)) goto out; - clock = real_tk->clock; -#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET offset = real_tk->cycle_interval;#else - offset =clocksource_delta (clock->read (clock), tk->cycle_last, - clock->mask); + offset =clocksource_delta (tk->read (tk->clock), tk->cycle_last, + tk->mask);#endif /* Check if there's really nothing to do */
PATCH v2 59/64 timekeeping: Create struct tk_read_base and use it in struct timekeeper
In reply to Thomas GleixnerThe members of the new struct are the required ones for the new NMI safe accessor to clcok monotonic. In order to reuse the existing timekeeping code and to make the update of the fast NMI safe timekeepers a simple memcpy use the struct for the timekeeper as well and convert all users.
--- arch/arm64/kernel/vdso.c | 10 +- arch/s390/kernel/time.c | 16 ++-- arch/tile/kernel/time.c | 10 +- arch/x86/kernel/vsyscall_gtod.c | 23 +++--- arch/x86/kvm/x86.c | 14 +-- include/linux/timekeeper_internal.h | 103 +++++++++++++++------------- kernel/time/timekeeping.c | 132 ++++++++++++++++++------------------ 7 files changed, 158 insertions(+), 150 deletions(-)
arch/arm64/kernel/vdso.c +5 -5
@@ -211,7 +211,7 @@ struct vm_area_struct *get_gate_vma(struvoid update_vsyscall (struct timekeeper *tk) {struct timespec xtime_coarse; -u32 use_syscall =strcmp (tk->clock->name,"arch_sys_counter" ); +u32 use_syscall =strcmp (tk->tkr.clock->name,"arch_sys_counter" ); ++vdso_data->tb_seq_count;smp_wmb ();
@@ -224,11 +224,11 @@ void update_vsyscall(struct timekeeper * vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;if (!use_syscall) { - vdso_data->cs_cycle_last = tk->cycle_last; + vdso_data->cs_cycle_last = tk->tkr.cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; - vdso_data->xtime_clock_nsec = tk->xtime_nsec; - vdso_data->cs_mult = tk->mult; - vdso_data->cs_shift = tk->shift; + vdso_data->xtime_clock_nsec = tk->tkr.xtime_nsec; + vdso_data->cs_mult = tk->tkr.mult; + vdso_data->cs_shift = tk->tkr.shift; }smp_wmb ();
arch/s390/kernel/time.c +8 -8
@@ -214,26 +214,26 @@ void update_vsyscall(struct timekeeper * {u64 nsecps; -if (tk->clock != &clocksource_tod) +if (tk->tkr.clock != &clocksource_tod)return ;/* Make userspace gettimeofday spin until we're done. */ ++vdso_data->tb_update_count;smp_wmb (); - vdso_data->xtime_tod_stamp = tk->cycle_last; + vdso_data->xtime_tod_stamp = tk->tkr.cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; - vdso_data->xtime_clock_nsec = tk->xtime_nsec; + vdso_data->xtime_clock_nsec = tk->tkr.xtime_nsec; vdso_data->wtom_clock_sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; - vdso_data->wtom_clock_nsec = tk->xtime_nsec + - + ((u64 ) tk->wall_to_monotonic.tv_nsec << tk->shift); - nsecps = (u64 )NSEC_PER_SEC << tk->shift; + vdso_data->wtom_clock_nsec = tk->tkr.xtime_nsec + + + ((u64 ) tk->wall_to_monotonic.tv_nsec << tk->tkr.shift); + nsecps = (u64 )NSEC_PER_SEC << tk->tkr.shift;while (vdso_data->wtom_clock_nsec >= nsecps) { vdso_data->wtom_clock_nsec -= nsecps; vdso_data->wtom_clock_sec++; } - vdso_data->tk_mult = tk->mult; - vdso_data->tk_shift = tk->shift; + vdso_data->tk_mult = tk->tkr.mult; + vdso_data->tk_shift = tk->tkr.shift;smp_wmb (); ++vdso_data->tb_update_count; }
arch/tile/kernel/time.c +5 -5
@@ -261,7 +261,7 @@ void update_vsyscall_tz(void)void update_vsyscall (struct timekeeper *tk) {struct timespec *wtm = &tk->wall_to_monotonic; -struct clocksource *clock = tk->clock; +struct clocksource *clock = tk->tkr.clock;if (clock != &cycle_counter_cs)return ;
@@ -269,13 +269,13 @@ void update_vsyscall(struct timekeeper */* Userspace gettimeofday will spin while this value is odd. */ ++vdso_data->tb_update_count;smp_wmb (); - vdso_data->xtime_tod_stamp = tk->cycle_last; + vdso_data->xtime_tod_stamp = tk->tkr.cycle_last; vdso_data->xtime_clock_sec = tk->xtime_sec; - vdso_data->xtime_clock_nsec = tk->xtime_nsec; + vdso_data->xtime_clock_nsec = tk->tkr.xtime_nsec; vdso_data->wtom_clock_sec = wtm->tv_sec; vdso_data->wtom_clock_nsec = wtm->tv_nsec; - vdso_data->mult = tk->mult; - vdso_data->shift = tk->shift; + vdso_data->mult = tk->tkr.mult; + vdso_data->shift = tk->tkr.shift;smp_wmb (); ++vdso_data->tb_update_count; }
arch/x86/kernel/vsyscall_gtod.c +12 -11
@@ -31,29 +31,30 @@ void update_vsyscall(struct timekeeper *gtod_write_begin (vdata);/* copy vsyscall data */ - vdata->vclock_mode = tk->clock->archdata.vclock_mode; - vdata->cycle_last = tk->cycle_last; - vdata->mask = tk->clock->mask; - vdata->mult = tk->mult; - vdata->shift = tk->shift; + vdata->vclock_mode = tk->tkr.clock->archdata.vclock_mode; + vdata->cycle_last = tk->tkr.cycle_last; + vdata->mask = tk->tkr.mask; + vdata->mult = tk->tkr.mult; + vdata->shift = tk->tkr.shift; vdata->wall_time_sec = tk->xtime_sec; - vdata->wall_time_snsec = tk->xtime_nsec; + vdata->wall_time_snsec = tk->tkr.xtime_nsec; vdata->monotonic_time_sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; - vdata->monotonic_time_snsec = tk->xtime_nsec + vdata->monotonic_time_snsec = tk->tkr.xtime_nsec + ((u64 )tk->wall_to_monotonic.tv_nsec - << tk->shift); + << tk->tkr.shift);while (vdata->monotonic_time_snsec >= - (((u64 )NSEC_PER_SEC ) << tk->shift)) { + (((u64 )NSEC_PER_SEC ) << tk->tkr.shift)) { vdata->monotonic_time_snsec -= - ((u64 )NSEC_PER_SEC ) << tk->shift; + ((u64 )NSEC_PER_SEC ) << tk->tkr.shift; vdata->monotonic_time_sec++; } vdata->wall_time_coarse_sec = tk->xtime_sec; - vdata->wall_time_coarse_nsec = (long )(tk->xtime_nsec >> tk->shift); + vdata->wall_time_coarse_nsec = (long )(tk->tkr.xtime_nsec >> + tk->tkr.shift); vdata->monotonic_time_coarse_sec = vdata->wall_time_coarse_sec + tk->wall_to_monotonic.tv_sec;
arch/x86/kvm/x86.c +7 -7
@@ -995,19 +995,19 @@ static void update_pvclock_gtod(struct tstruct pvclock_gtod_data *vdata = &pvclock_gtod_data;u64 boot_ns; - boot_ns =ktime_to_ns (ktime_add (tk->base_mono, tk->offs_boot)); + boot_ns =ktime_to_ns (ktime_add (tk->tkr.base_mono, tk->offs_boot));write_seqcount_begin (&vdata->seq);/* copy pvclock gtod data */ - vdata->clock.vclock_mode = tk->clock->archdata.vclock_mode; - vdata->clock.cycle_last = tk->cycle_last; - vdata->clock.mask = tk->clock->mask; - vdata->clock.mult = tk->mult; - vdata->clock.shift = tk->shift; + vdata->clock.vclock_mode = tk->tkr.clock->archdata.vclock_mode; + vdata->clock.cycle_last = tk->tkr.cycle_last; + vdata->clock.mask = tk->tkr.mask; + vdata->clock.mult = tk->tkr.mult; + vdata->clock.shift = tk->tkr.shift; vdata->boot_ns = boot_ns; - vdata->nsec_base = tk->xtime_nsec; + vdata->nsec_base = tk->tkr.xtime_nsec;write_seqcount_end (&vdata->seq); }
include/linux/timekeeper_internal.h +55 -48
135 lines not on this page · raw message
kernel/time/timekeeping.c +66 -66
346 lines not on this page · raw message
PATCH v2 60/64 timekeeping: Use tk_read_base as argument for timekeeping_get_ns()
In reply to Thomas GleixnerAll the function needs is in the tk_read_base struct. No functional change for the current code, just a preparatory patch for the NMI safe accessor to clock monotonic which will use struct tk_read_base as well.
--- kernel/time/timekeeping.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
kernel/time/timekeeping.c +11 -11
@@ -173,19 +173,19 @@ u32 (*arch_gettimeoffset)(void) = defaulstatic inline u32 arch_gettimeoffset (void ) {return 0 ; }#endif -static inline s64 timekeeping_get_ns (struct timekeeper *tk) +static inline s64 timekeeping_get_ns (struct tk_read_base *tkr) {cycle_t cycle_now, delta;s64 nsec;/* read clocksource: */ - cycle_now = tk->tkr.read (tk->tkr.clock); + cycle_now = tkr->read (tkr->clock);/* calculate the delta since the last update_wall_time: */ - delta =clocksource_delta (cycle_now, tk->tkr.cycle_last, tk->tkr.mask); + delta =clocksource_delta (cycle_now, tkr->cycle_last, tkr->mask); - nsec = delta * tk->tkr.mult + tk->tkr.xtime_nsec; - nsec >>= tk->tkr.shift; + nsec = delta * tkr->mult + tkr->xtime_nsec; + nsec >>= tkr->shift;/* If arch requires, add in get_arch_timeoffset() */ return nsec +arch_gettimeoffset ();
@@ -372,7 +372,7 @@ int __getnstimeofday64(struct timespec64 seq =read_seqcount_begin (&tk_core.seq); ts->tv_sec = tk->xtime_sec; - nsecs =timekeeping_get_ns (tk); + nsecs =timekeeping_get_ns (&tk->tkr); }while (read_seqcount_retry (&tk_core.seq, seq));
@@ -413,7 +413,7 @@ ktime_t ktime_get(void)do { seq =read_seqcount_begin (&tk_core.seq); base = tk->tkr.base_mono; - nsecs =timekeeping_get_ns (tk); + nsecs =timekeeping_get_ns (&tk->tkr); }while (read_seqcount_retry (&tk_core.seq, seq));
@@ -439,7 +439,7 @@ ktime_t ktime_get_with_offset(enum tk_ofdo { seq =read_seqcount_begin (&tk_core.seq); base =ktime_add (tk->tkr.base_mono, *offset); - nsecs =timekeeping_get_ns (tk); + nsecs =timekeeping_get_ns (&tk->tkr); }while (read_seqcount_retry (&tk_core.seq, seq));
@@ -509,7 +509,7 @@ void ktime_get_ts64(struct timespec64 *tdo { seq =read_seqcount_begin (&tk_core.seq); ts->tv_sec = tk->xtime_sec; - nsec =timekeeping_get_ns (tk); + nsec =timekeeping_get_ns (&tk->tkr); tomono = tk->wall_to_monotonic; }while (read_seqcount_retry (&tk_core.seq, seq));
@@ -547,7 +547,7 @@ void getnstime_raw_and_real(struct times ts_real->tv_nsec =0 ; nsecs_raw =timekeeping_get_ns_raw (tk); - nsecs_real =timekeeping_get_ns (tk); + nsecs_real =timekeeping_get_ns (&tk->tkr); }while (read_seqcount_retry (&tk_core.seq, seq));
@@ -1624,7 +1624,7 @@ ktime_t ktime_get_update_offsets_now(kti seq =read_seqcount_begin (&tk_core.seq); base = tk->tkr.base_mono; - nsecs =timekeeping_get_ns (tk); + nsecs =timekeeping_get_ns (&tk->tkr); *offs_real = tk->offs_real; *offs_boot = tk->offs_boot;
PATCH v2 61/64 seqcount: Provide raw_read_seqcount()
In reply to Thomas Gleixnerraw_read_seqcount opens a read critical section of the given seqcount without any lockdep checking and without checking or masking the LSB. Calling code is responsible for handling that.
Preparatory patch to provide a NMI safe clock monotonic accessor function.
--- include/linux/seqlock.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/seqlock.h +16 -0
@@ -117,6 +117,22 @@ repeat: }/** +* raw_read_seqcount - Read the raw seqcount +* @s: pointer to seqcount_t +* Returns: count to be passed to read_seqcount_retry +* +* raw_read_seqcount opens a read critical section of the given +* seqcount without any lockdep checking and without checking or +* masking the LSB. Calling code is responsible for handling that. +*/ +static inline unsigned raw_read_seqcount (const seqcount_t *s) +{ +unsigned ret =ACCESS_ONCE (s->sequence); +smp_rmb (); +return ret; +} + +/** * raw_read_seqcount_begin - start seq-read critical section w/o lockdep * @s: pointer to seqcount_t * Returns: count to be passed to read_seqcount_retry
PATCH v2 62/64 seqcount: Add raw_write_seqcount_latch()
In reply to Thomas GleixnerFrom: Mathieu Desnoyers <[email protected]>
For NMI safe access to clock monotonic we use the seqcount LSB as index of a timekeeper array. The update sequence looks like this:
smp_wmb(); <- prior stores to a[1]
seq++;
smp_wmb(); <- seq increment before update of a[0]
update(a[0]);
smp_wmb(); <- update of a[0]
seq++;
smp_wmb(); <- seq increment before update of a[1]
update(a[1]);To avoid open coded barriers, provide a helper function.
[ tglx: Split out of a combo patch against the first implementation of
the NMI safe accessor ]
--- include/linux/seqlock.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/seqlock.h +11 -0
@@ -236,6 +236,17 @@ static inline void raw_write_seqcount_en }/* +* raw_write_seqcount_latch - redirect readers to even/odd copy +* @s: pointer to seqcount_t +*/ +static inline void raw_write_seqcount_latch (seqcount_t *s) +{ +smp_wmb ();/* prior stores before incrementing "sequence" */ + s->sequence++; +smp_wmb ();/* increment "sequence" before following stores */ +} + +/* * Sequence counter only version assumes that callers are using their * own mutexing. */
PATCH v2 63/64 timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC
In reply to Thomas GleixnerTracers want a correlated time between the kernel instrumentation and user space. We really do not want to export sched_clock() to user space, so we need to provide something sensible for this.
Using separate data structures with an non blocking sequence count based update mechanism allows us to do that. The data structure required for the readout has a sequence counter and two copies of the timekeeping data.
On the update side:
smp_wmb(); tkf->seq++; smp_wmb(); update(tkf->base[0], tk); smp_wmb(); tkf->seq++; smp_wmb(); update(tkf->base[1], tk);
On the reader side:
do {
seq = tkf->seq;
smp_rmb();
idx = seq & 0x01;
now = now(tkf->base[idx]);
smp_rmb();
} while (seq != tkf->seq)So if a NMI hits the update of base[0] it will use base[1] which is still consistent, but this timestamp is not guaranteed to be monotonic across an update.
The timestamp is calculated by:
now = base_mono + clock_delta * slope
So if the update lowers the slope, readers who are forced to the not yet updated second array are still using the old steeper slope.
tmono
^
| o n | o n | u | o |o |12345678---> reader order
o = old slope
u = update
n = new slope
So reader 6 will observe time going backwards versus reader 5.
While other CPUs are likely to be able observe that, the only way for a CPU local observation is when an NMI hits in the middle of the update. Timestamps taken from that NMI context might be ahead of the following timestamps. Callers need to be aware of that and deal with it.
V2: Got rid of clock monotonic raw and reorganized the data
structures. Folded in the barrier fix from Mathieu.
--- include/linux/timekeeping.h | 2 kernel/time/timekeeping.c | 124 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+)
include/linux/timekeeping.h +2 -0
@@ -164,6 +164,8 @@ static inline u64 ktime_get_raw_ns(void)return ktime_to_ns (ktime_get_raw ()); } +extern u64 ktime_get_mono_fast_ns (void ); +/* * Timespec interfaces utilizing the ktime based ones */
kernel/time/timekeeping.c +124 -0
142 lines not on this page · raw message
PATCH v2 64/64 ftrace: Provide trace clocks monotonic
In reply to Thomas GleixnerExpose the new NMI safe accessor to clock monotonic to the tracer.
--- kernel/trace/trace.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
kernel/trace/trace.c +6 -5
@@ -806,11 +806,12 @@ static struct {const char *name;int in_ns;/* is this clock in nanoseconds? */ } trace_clocks[] = { - { trace_clock_local, "local", 1 }, - { trace_clock_global, "global", 1 }, - { trace_clock_counter, "counter", 0 }, - { trace_clock_jiffies, "uptime", 1 }, - { trace_clock, "perf", 1 }, + { trace_clock_local, "local", 1 }, + { trace_clock_global, "global", 1 }, + { trace_clock_counter, "counter", 0 }, + { trace_clock_jiffies, "uptime", 1 }, + { trace_clock, "perf", 1 }, + { ktime_get_mono_fast_ns, "mono", 1 }, ARCH_TRACE_CLOCKS };
Steven Rostedt
maintainerRe: [patch V2 64/64] ftrace: Provide trace clocks monotonic
Quoted 19 lines · On Wed, 16 Jul 2014 21:05:25 -0000 Thomas Gleixner <[email protected]> wrote:
Expose the new NMI safe accessor to clock monotonic to the tracer.
Signed-off-by Thomas Gleixner <[email protected]>Cc Steven Rostedt <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Mathieu Desnoyers <[email protected]>--- kernel/trace/trace.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)Index:
tip/kernel/trace/trace.c
===================================================================kernel/trace/trace.c +0
-1@@ -806,11 +806,12 @@ static struct {const char *name;int in_ns;/* is this clock in nanoseconds? */ } trace_clocks[] = { - { trace_clock_local, "local", 1 },
kernel/trace/trace.c static struct { · line 814
Quoted 10 lines
kernel/trace/trace.c +6
-4- { trace_clock_global, "global", 1 }, - { trace_clock_counter, "counter", 0 }, - { trace_clock_jiffies, "uptime", 1 }, - { trace_clock, "perf", 1 }, + { trace_clock_local, "local", 1 }, + { trace_clock_global, "global", 1 }, + { trace_clock_counter, "counter", 0 }, + { trace_clock_jiffies, "uptime", 1 }, + { trace_clock, "perf", 1 }, + { ktime_get_mono_fast_ns, "mono", 1 },
I'm not sure I like the name "mono" because all the clocks listed are considered monolithic (with the view of tracing). What about calling it "user" as I believe this clock is the one that will be used to synchronize userspace times with the kernel. Right?
-- Steve
Quoted 4 lines
kernel/trace/trace.c +0
-0ARCH_TRACE_CLOCKS };
Steven Rostedt
maintainerRe: [patch V2 64/64] ftrace: Provide trace clocks monotonic
Quoted 6 lines · On Thu, 17 Jul 2014 08:55:35 -0400 Steven Rostedt <[email protected]> wrote:
On Wed, 16 Jul 2014 21:05:25 -0000
Thomas Gleixner <[email protected]> wrote:I'm not sure I like the name "mono" because all the clocks listed are considered monolithic (with the view of tracing). What about calling it
Peter nicely informed me on IRC that I just confused monotonic with monolithic. I think this is an improvement over my usual embarrassing moments when I confuse the words generic with genetic.
-- Steve
Quoted 10 lines
"user" as I believe this clock is the one that will be used to synchronize userspace times with the kernel. Right?
-- Steve
Quoted 4 lines
ARCH_TRACE_CLOCKS
};
Quoted 3 lines · Thomas Gleixner
The series is against tip/timers/core. It's also available in git at:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gittglx/timers/core
Anyone, who pulled from there, please do again. I just got aware that the branch was stale.
I could swear that I updated it and according to my bash history I really did a force push on that branch and a verify that origin/tglx/timers/core is the same as tglx/timers/core, that's the same procedure I use before sending out a pull request to Linus - got burned once not doing that :)
The same thing happened about half a year ago, and looking at the mail thread it was also a forced update, i.e. git push -f
I'm not blaming anyone except me as long as I can't be sure that my scripting around git is not to blame.
Sorry for the inconveniance!
Thanks,
tglx