Skip to contentSkip to conversation

timekeeping: 2038, optimizations, NMI safe accessors

PATCH Superseded Thomas Gleixner 13 participants 91 messages Jul 11, 2014 – Jul 18, 2014

Subsystems (37)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover timekeeping: 2038, optimizations, NMI safe accessors

The patch series addresses the following points:

1) Make the core timekeeping code ready for 2038

This is work done by John Stultz with a few clenaups and modifications from me.

This switches ktime_t to the scalar nanoseconds format.

We can ged rid of the union now, but that wants to be done at rc1 time with a coccinelle script.

2) Optimize the timekeeper data structure

Better cache layout

3) Avoid timespec/ktime_t conversions in the core code.

Results in smaller code for the ktime_t based interfaces.

4) Add pure nanaoseconds based interfaces and cleanup places all over the tree which do silly back and forth conversions from timespecs to nanoseconds.

5) Provide NMI safe accessors to clock monotonic and monotonic_raw

Useful for instrumentation, tracing etc. as it allows correlated time information of kernel and user space.

The series is against tip/timers/core + 3.16-rc4 merged in. 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/hexagon/Kconfig                          |    1 
 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/tile/kernel/time.c                       |    9 
 arch/tile/kernel/vdso/vgettimeofday.c         |    7 
 arch/x86/Kconfig                              |    2 
 arch/x86/kernel/tsc.c                         |   14 
 arch/x86/kvm/x86.c                            |   50 -
 drivers/char/hangcheck-timer.c                |   10 
 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                     |    7 
 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/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                       |   16 
 include/linux/time.h                          |   65 -
 include/linux/time64.h                        |  191 +++++
 include/linux/timekeeper_internal.h           |  108 +--
 include/linux/timekeeping.h                   |  204 ++++++
 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                         |   79 --
 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                     |  881 +++++++++++++++-----------
 kernel/time/timekeeping.h                     |   20 
 kernel/time/timekeeping_debug.c               |    2 
 kernel/time/timekeeping_internal.h            |   17 
 kernel/trace/trace.c                          |   12 
 kernel/tsacct.c                               |   19 
 57 files changed, 1339 insertions(+), 1059 deletions(-)
#1 lore raw

PATCH 1/55 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
Index: tip/arch/tile/kernel/time.c
===================================================================
@@ -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
Index: tip/arch/tile/kernel/vdso/vgettimeofday.c
===================================================================
@@ -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;
Acked-by on v1 1/55
#2 lore raw

Re: [patch 01/55] tile: Convert VDSO timekeeping to the precise mechanism

Quoted 9 lines · Thomas Gleixner

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(-)
Signature · 2 lines
Chris Metcalf, Tilera Corp.
http://www.tilera.com
#3 lore raw

PATCH 2/55 timekeeping: Simplify arch_gettimeoffset()

In reply to Thomas Gleixner

Provide 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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -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);
 
Acked-by on v1 2/55
#4 lore raw

Re: [patch 02/55] timekeeping: Simplify arch_gettimeoffset()

Hi Thomas,

Quoted 2 lines · Thomas Gleixner

Provide a default stub function instead of having the extra conditional. Cuts binary size on a m68k build by ~100 bytes.

Thanks about caring about binary size.

Quoted 1 line

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#5 lore raw

PATCH 3/55 hrtimer: Cleanup hrtimer accessors to the timekepeing state

In reply to Thomas Gleixner

From: 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
Index: tip/include/linux/hrtimer.h
===================================================================
@@ -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
Index: tip/include/linux/time.h
===================================================================
@@ -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
Index: tip/kernel/time/hrtimer.c
===================================================================
@@ -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_bas
 	ktime_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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -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;
#6 lore raw

PATCH 4/55 ktime: Kill non-scalar ktime_t implementation for 2038

In reply to Thomas Gleixner

From: 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
Index: tip/arch/arm/Kconfig
===================================================================
@@ -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
Index: tip/arch/hexagon/Kconfig
===================================================================
@@ -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
Index: tip/arch/s390/Kconfig
===================================================================
@@ -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
Index: tip/arch/x86/Kconfig
===================================================================
@@ -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
Index: tip/include/linux/time.h
===================================================================
@@ -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_s
 	return 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
Index: tip/kernel/time/Kconfig
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -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);
 
#7 lore raw

PATCH 5/55 ktime: Sanitize ktime_to_us/ms conversion

In reply to Thomas Gleixner

With 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 ++++++++----
 2 files changed, 8 insertions(+), 10 deletions(-)
include/linux/hrtimer.h +0 -6
Index: tip/include/linux/hrtimer.h
===================================================================
@@ -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
Index: tip/include/linux/ktime.h
===================================================================
@@ -157,16 +157,20 @@ static inline bool ktime_before(const kt
 	return 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)
#8 lore raw

PATCH 6/55 ktime: Change ktime_set() to take 64bit seconds value

In reply to Thomas Gleixner

From: 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
Index: tip/include/linux/ktime.h
===================================================================
@@ -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: */
#9 lore raw

PATCH 7/55 time64: Add time64.h header and define struct timespec64

In reply to Thomas Gleixner

From: 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
Index: tip/include/linux/time.h
===================================================================
@@ -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

#10 lore raw

Re: [patch 07/55] time64: Add time64.h header and define struct timespec64

Quoted 4 lines · Thomas Gleixner

From: John Stultz <[email protected]>
+/*
+ * timespec64_add_safe assumes both values are positive and checks
+ * for overflow. It will return TIME_T_MAX if the reutrn would be

                                                     ^^^^^^

returned value?

include/linux/time64.h line 78

Quoted 4 lines

+ * smaller then either of the arguments.
+ */

include/linux/time64.h new file +2 -0
+extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
+					 const struct timespec64 rhs);

Thanks,
Richard

#11 lore raw

PATCH 8/55 time: More core infrastructure for timespec64

In reply to Thomas Gleixner

From: 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
Index: tip/include/linux/ktime.h
===================================================================
@@ -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_to
 		return 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
Index: tip/include/linux/time64.h
===================================================================
@@ -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

#12 lore raw

PATCH 9/55 timekeeping: Convert timekeeping core to use timespec64s

In reply to Thomas Gleixner

From: 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
Index: tip/include/linux/timekeeper_internal.h
===================================================================
@@ -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
Index: tip/kernel/time/ntp.c
===================================================================
@@ -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(st
 
 
 static 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
Index: tip/kernel/time/ntp_internal.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping_debug.c
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping_internal.h
===================================================================
@@ -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
#13 lore raw

PATCH 10/55 time: Consolidate the time accessor prototypes

In reply to Thomas Gleixner

Right 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
Index: tip/include/linux/hrtimer.h
===================================================================
@@ -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
Index: tip/include/linux/ktime.h
===================================================================
@@ -226,12 +226,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 };
@@ -246,4 +240,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
Index: tip/kernel/time/hrtimer.c
===================================================================
@@ -54,6 +54,8 @@
 
 #include <trace/events/timer.h>
 
+#include "timekeeping.h"
+
 /*
  * The timer bases:
  *
kernel/time/posix-timers.c +2 -0
Index: tip/kernel/time/posix-timers.c
===================================================================
@@ -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
Index: tip/kernel/time/tick-internal.h
===================================================================
@@ -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
Index: tip/kernel/time/time.c
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.h
===================================================================
@@ -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
#14 lore raw

PATCH 11/55 timekeeping: Provide timespec64 based interfaces

In reply to Thomas Gleixner

To 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -29,15 +29,71 @@ struct timespec get_monotonic_coarse(voi
 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_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
Index: tip/kernel/time/ntp.c
===================================================================
@@ -466,7 +466,8 @@ static DECLARE_DELAYED_WORK(sync_cmos_wo
 
 static 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

#15 lore raw

PATCH 12/55 timekeeper: Move tk_xtime to core code

In reply to Thomas Gleixner

No 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
Index: tip/include/linux/timekeeper_internal.h
===================================================================
@@ -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

#16 lore raw

PATCH 13/55 timekeeping: Cache optimize struct timekeeper

In reply to Thomas Gleixner

struct 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
Index: tip/include/linux/timekeeper_internal.h
===================================================================
@@ -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

#17 lore raw

PATCH 14/55 timekeeping: Provide internal ktime_t based data

In reply to Thomas Gleixner

The 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           |   25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
include/linux/timekeeper_internal.h +3 -0
Index: tip/include/linux/timekeeper_internal.h
===================================================================
@@ -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 +25 -0
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -50,12 +50,30 @@ int __read_mostly timekeeping_suspended;
 /* Flag for if there is a persistent clock on this platform */
 bool __read_mostly persistent_clock_exist = false;
 
+/*
+ * 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
+ */
+static inline void tk_update_ktime_base(struct timekeeper *tk)
+{
+	s64 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);
+}
+
 static inline void tk_normalize_xtime(struct timekeeper *tk)
 {
 	while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
 		tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
 		tk->xtime_sec++;
 	}
+	tk_update_ktime_base(tk);
 }
 
 static inline struct timespec64 tk_xtime(struct timekeeper *tk)
@@ -71,6 +89,7 @@ static void tk_set_xtime(struct timekeep
 {
 	tk->xtime_sec = ts->tv_sec;
 	tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
+	tk_update_ktime_base(tk);
 }
 
 static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
@@ -93,6 +112,7 @@ static void tk_set_wall_to_mono(struct t
 	WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
 	tk->wall_to_monotonic = wtm;
 	set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
+	tk_update_ktime_base(tk);
 	tk->offs_real = timespec64_to_ktime(tmp);
 	tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
 }
@@ -329,6 +349,7 @@ static void timekeeping_forward_now(stru
 	/* If arch requires, add in get_arch_timeoffset() */
 	tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
 
+	/* Updates tk->base_mono as well */
 	tk_normalize_xtime(tk);
 
 	nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
@@ -1448,6 +1469,10 @@ void update_wall_time(void)
 	 * xtime_nsec isn't larger than NSEC_PER_SEC
 	 */
 	clock_set |= accumulate_nsecs_to_secs(tk);
+	/*
+	 * Update the monotonic base
+	 */
+	tk_update_ktime_base(tk);
 
 	write_seqcount_begin(&tk_core.seq);
 	/* Update clock->cycle_last with the new value */
#18 lore raw

Re: [patch 14/55] timekeeping: Provide internal ktime_t based data

Quoted 38 lines · Thomas Gleixner

The 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           |   25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

Index: tip/include/linux/timekeeper_internal.h
===================================================================

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 */

Index: tip/kernel/time/timekeeping.c
===================================================================

kernel/time/timekeeping.c +6 -0
@@ -50,12 +50,30 @@ int __read_mostly timekeeping_suspended;
 /* Flag for if there is a persistent clock on this platform */
 bool __read_mostly persistent_clock_exist = false;
 
+/*
+ * 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

kernel/time/timekeeping.c int __read_mostly timekeeping_suspended; · line 68

Quoted 10 lines
kernel/time/timekeeping.c +10 -0
+ */
+static inline void tk_update_ktime_base(struct timekeeper *tk)
+{
+       s64 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);
+}

Hrmm.. So I do understand why this is useful performance wise. However, I'm really starting to feel that keeping all this duplicate data is a real maintenance burden, as remembering to keep the values in sync always is prone to error.

So I may have to just put up with it, but I'd like to start thinking about how to reduce the duplicated data in the future. Arnd had an interesting idea for something like storing fixed point seconds, which could be cheaply converted to either ktime_t or timespec values. However, I suspect that would be even more complex for folks to understand, which I'd rather not do.

Overall, it might be best if we just kill the timespec wall_to_monotonic/total_sleep_time/tai_offset values and keep the timekeeper values almost all in timespecs. Then we can leave the conversion process to basically cache the timespec values to the vsyscall_update logic?

Or if we do want to maintain performance for non-vdso implementations, maybe we can split the timespec functions out of the timekeeping core and do a similar single shot update each tick to set the update the tiemspec cached values. But that can be layered over the timekeeping core and we can avoid the crazy updating 2 values any time we change one logic we currently have.

thanks
-joh

#19 lore raw

Re: [patch 14/55] timekeeping: Provide internal ktime_t based data

Quoted 12 lines · John Stultz

Hrmm.. So I do understand why this is useful performance wise. However, I'm really starting to feel that keeping all this duplicate data is a real maintenance burden, as remembering to keep the values in sync always is prone to error.

So I may have to just put up with it, but I'd like to start thinking about how to reduce the duplicated data in the future. Arnd had an interesting idea for something like storing fixed point seconds, which could be cheaply converted to either ktime_t or timespec values. However, I suspect that would be even more complex for folks to understand, which I'd rather not do.

Quoted 3 lines

Overall, it might be best if we just kill the timespec wall_to_monotonic/total_sleep_time/tai_offset values and keep the timekeeper values almost all in timespecs. Then we can leave the

So we kill the time specs and store everything in timespecs :)

Quoted 9 lines

conversion process to basically cache the timespec values to the vsyscall_update logic?

Or if we do want to maintain performance for non-vdso implementations, maybe we can split the timespec functions out of the timekeeping core and do a similar single shot update each tick to set the update the tiemspec cached values. But that can be layered over the timekeeping core and we can avoid the crazy updating 2 values any time we change one logic we currently have.

Makes sense. Lemme have a stab on it.

Thanks,

tglx

#20 lore raw

Re: [patch 14/55] timekeeping: Provide internal ktime_t based data

Quoted 13 lines · Thomas Gleixner
Quoted 12 lines · John Stultz

Hrmm.. So I do understand why this is useful performance wise. However, I'm really starting to feel that keeping all this duplicate data is a real maintenance burden, as remembering to keep the values in sync always is prone to error.

So I may have to just put up with it, but I'd like to start thinking about how to reduce the duplicated data in the future. Arnd had an interesting idea for something like storing fixed point seconds, which could be cheaply converted to either ktime_t or timespec values. However, I suspect that would be even more complex for folks to understand, which I'd rather not do.

Quoted 8 lines
Quoted 3 lines

Overall, it might be best if we just kill the timespec wall_to_monotonic/total_sleep_time/tai_offset values and keep the timekeeper values almost all in timespecs. Then we can leave the

So we kill the time specs and store everything in timespecs :)

Quoted 2 lines

conversion process to basically cache the timespec values to the vsyscall_update logic?

Looking into it I think for now it's the least risky approach to keep the core logic based on the timespec stuff unmodified and update the ktime_t members in timekeeping_update(). Converting the whole thing to a pure nsec based mechanism and update the timespec stuff in timekeeping_update() needs a lot more thought and we should do that later on. It wont change any of the interfaces.

Thanks,

tglx

maintainer
#21 lore raw

Re: [patch 14/55] timekeeping: Provide internal ktime_t based data

Quoted 6 lines · Thomas Gleixner

Looking into it I think for now it's the least risky approach to keep the core logic based on the timespec stuff unmodified and update the ktime_t members in timekeeping_update(). Converting the whole thing to a pure nsec based mechanism and update the timespec stuff in timekeeping_update() needs a lot more thought and we should do that later on. It wont change any of the interfaces.

So I don't think you can only do nsec, seeing how the conversion from nsec to timespec is expensive. So if we want to also avoid the timespec -> nsec conversion we need to keep both, no two ways around it.

That said the timespec -> nsec conversion is heaps cheaper, although still not what you call really sheep ;-)

#22 lore raw

PATCH 15/55 timekeeping: Use ktime_t based data for ktime_get()

In reply to Thomas Gleixner

Speed 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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -406,18 +406,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);
 
#23 lore raw

PATCH 16/55 timekeeping: Provide ktime_get_with_offset()

In reply to Thomas Gleixner

Provide 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -422,6 +422,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
#24 lore raw

PATCH 17/55 timekeeping: Use ktime_t based data for ktime_get_real()

In reply to Thomas Gleixner

Speed 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -773,21 +773,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
  *
#25 lore raw

PATCH 18/55 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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(voi
 	return 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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1570,23 +1570,6 @@ void get_monotonic_boottime(struct times
 EXPORT_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
  */
#26 lore raw

PATCH 19/55 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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_boottime
 	return 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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -511,21 +511,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
 
 /**
#27 lore raw

PATCH 20/55 timekeeping: Use ktime_t data for ktime_get_update_offsets_now()

In reply to Thomas Gleixner

No need to juggle with timespecs.

---
 kernel/time/timekeeping.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
kernel/time/timekeeping.c +4 -6
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1672,14 +1672,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;
@@ -1687,9 +1687,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
 
#28 lore raw

PATCH 21/55 timekeeping; Use ktime based data for ktime_get_update_offsets_tick()

In reply to Thomas Gleixner

No need to juggle with timespecs.

---
 kernel/time/timekeeping.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/time/timekeeping.c +6 -6
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1640,22 +1640,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
#29 lore raw

PATCH 22/55 timekeeping: Provide ktime_mono_to_any()

In reply to Thomas Gleixner

ktime 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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_clocktai
 	return 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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -450,6 +450,26 @@ ktime_t ktime_get_with_offset(enum tk_of
 EXPORT_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
  *
#30 lore raw

PATCH 23/55 timerfd: Use ktime_mono_to_real()

In reply to Thomas Gleixner

We 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
Index: tip/fs/timerfd.c
===================================================================
@@ -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, clo
 	else
 		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));
#31 lore raw

PATCH 24/55 input: evdev: Use ktime_mono_to_real()

In reply to Thomas Gleixner

Convert 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
Index: tip/drivers/input/evdev.c
===================================================================
@@ -108,9 +108,8 @@ static void evdev_queue_syn_dropped(stru
 	struct 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_ha
 	ktime_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();
 
#32 lore raw

PATCH 25/55 drm: Use ktime_mono_to_real()

In reply to Thomas Gleixner

Convert 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
Index: tip/drivers/gpu/drm/drm_irq.c
===================================================================
@@ -542,8 +542,8 @@ int drm_calc_vbltimestamp_from_scanoutpo
 					  const 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);
 }
 
#33 lore raw

PATCH 26/55 timekeeping: Remove ktime_get_monotonic_offset()

In reply to Thomas Gleixner

No more users.

---
 include/linux/timekeeping.h |    1 -
 kernel/time/timekeeping.c   |   18 ------------------
 2 files changed, 19 deletions(-)
include/linux/timekeeping.h +0 -1
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1712,24 +1712,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)
#34 lore raw

PATCH 27/55 timekeeping: Provide ktime_get[*]_ns() helpers

In reply to Thomas Gleixner

A 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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -145,6 +145,21 @@ static inline ktime_t ktime_mono_to_real
 	return 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
  */
#35 lore raw

PATCH 28/55 time-export-nsecs-to-jiffies.patch

In reply to Thomas Gleixner
---
 kernel/time/time.c |    1 +
 1 file changed, 1 insertion(+)
kernel/time/time.c +1 -0
Index: tip/kernel/time/time.c
===================================================================
@@ -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.
#36 lore raw

PATCH 29/55 sched: Make task->real_start_time nanoseconds based

In reply to Thomas Gleixner

Simplify 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
Index: tip/fs/proc/array.c
===================================================================
@@ -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
Index: tip/include/linux/sched.h
===================================================================
@@ -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
Index: tip/kernel/fork.c
===================================================================
@@ -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)
#37 lore raw

PATCH 30/55 sched: Make task->start_time nanoseconds based

In reply to Thomas Gleixner

Simplify 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
Index: tip/include/linux/sched.h
===================================================================
@@ -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
Index: tip/kernel/acct.c
===================================================================
@@ -458,9 +458,7 @@ static void do_acct_process(struct bsd_a
 	acct_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_a
 	strlcpy(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
Index: tip/kernel/fork.c
===================================================================
@@ -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
Index: tip/kernel/tsacct.c
===================================================================
@@ -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)
#38 lore raw

PATCH 31/55 delayacct: Make accounting nanosecond based

In reply to Thomas Gleixner

Kill 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
Index: tip/include/linux/sched.h
===================================================================
@@ -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
Index: tip/kernel/delayacct.c
===================================================================
@@ -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(&current->delays->lock, flags);
-	*total += ns;
-	(*count)++;
-	spin_unlock_irqrestore(&current->delays->lock, flags);
+	if (ns > 0) {
+		spin_lock_irqsave(&current->delays->lock, flags);
+		*total += ns;
+		(*count)++;
+		spin_unlock_irqrestore(&current->delays->lock, flags);
+	}
 }
 
 void __delayacct_blkio_start(void)
 {
-	ktime_get_ts(&current->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(&current->delays->blkio_start,
-			&current->delays->blkio_end,
 			&current->delays->swapin_delay,
 			&current->delays->swapin_count);
 	else	/* Other block I/O */
 		delayacct_end(&current->delays->blkio_start,
-			&current->delays->blkio_end,
 			&current->delays->blkio_delay,
 			&current->delays->blkio_count);
 }
@@ -159,13 +150,12 @@ __u64 __delayacct_blkio_ticks(struct tas
 
 void __delayacct_freepages_start(void)
 {
-	ktime_get_ts(&current->delays->freepages_start);
+	current->delays->freepages_start = ktime_get_ns();
 }
 
 void __delayacct_freepages_end(void)
 {
 	delayacct_end(&current->delays->freepages_start,
-			&current->delays->freepages_end,
 			&current->delays->freepages_delay,
 			&current->delays->freepages_count);
 }
#39 lore raw

PATCH 32/55 delayacct: Remove braindamaged type conversions

In reply to Thomas Gleixner

Converting 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
Index: tip/kernel/delayacct.c
===================================================================
@@ -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;
 
#40 lore raw

PATCH 33/55 powerpc: cell: Use ktime_get_ns()

In reply to Thomas Gleixner

Replace 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
Index: tip/arch/powerpc/platforms/cell/spu_base.c
===================================================================
@@ -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
Index: tip/arch/powerpc/platforms/cell/spufs/context.c
===================================================================
@@ -36,7 +36,6 @@ atomic_t nr_spu_contexts = ATOMIC_INIT(0
 struct 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
Index: tip/arch/powerpc/platforms/cell/spufs/file.c
===================================================================
@@ -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
Index: tip/arch/powerpc/platforms/cell/spufs/sched.c
===================================================================
@@ -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));
#42 lore raw

PATCH 34/55 connector: Use ktime_get_ns()

In reply to Thomas Gleixner

Replace the ever recurring:

        ts = ktime_get_ts();
        ns = timespec_to_ns(&ts);
with
        ns = ktime_get_ns();
Cc Evgeniy Polyakov <[email protected]>
---
 drivers/connector/cn_proc.c |   36 +++++++++---------------------------
 1 file changed, 9 insertions(+), 27 deletions(-)
drivers/connector/cn_proc.c +9 -27
Index: tip/drivers/connector/cn_proc.c
===================================================================
@@ -69,7 +69,6 @@ void proc_fork_connector(struct task_str
 	struct 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_struc
 	struct 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 task
 	struct 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_str
 	struct 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 rcv
 	struct 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;
#43 lore raw

PATCH 35/55 mfd: cros_ec_spi: Use ktime_get_ns()

In reply to Thomas Gleixner

Replace the ever recurring:

	ts = ktime_get_ts();
	ns = timespec_to_ns(&ts);
with
	ns = ktime_get_ns();
---
 drivers/mfd/cros_ec_spi.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/mfd/cros_ec_spi.c +2 -5
Index: tip/drivers/mfd/cros_ec_spi.c
===================================================================
@@ -225,7 +225,6 @@ static int cros_ec_command_spi_xfer(stru
 	u8 *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
@@ -242,8 +241,7 @@ static int cros_ec_command_spi_xfer(stru
 		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 +278,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) {
#44 lore raw

Re: [patch 35/55] mfd: cros_ec_spi: Use ktime_get_ns()

Quoted 8 lines

Replace the ever recurring:

	ts = ktime_get_ts();
	ns = timespec_to_ns(&ts);
with
	ns = ktime_get_ns();

Only Sam? How old is your version of MAINTAINERS?

Sam hasn't actively maintained MFD for over a year now.

Patch applied though, thanks.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#45 lore raw

Re: [patch 35/55] mfd: cros_ec_spi: Use ktime_get_ns()

Quoted 10 lines · Lee Jones
Quoted 8 lines

Replace the ever recurring:

	ts = ktime_get_ts();
	ns = timespec_to_ns(&ts);
with
	ns = ktime_get_ns();

Only Sam? How old is your version of MAINTAINERS?

Hmm. I use get_maintainers.pl and manually edit it afterwards. Seems that I killed one extra line :)

Quoted 3 lines

Sam hasn't actively maintained MFD for over a year now.

Patch applied though, thanks.

ktime_get_ns() is not upstream yet, it's part of the series I posted :)

If you ack it, i'll carry it and also remove the now unused variable ts before applying it.

Thanks,

tglx

Acked-by
#46 lore raw

Re: [patch 35/55] mfd: cros_ec_spi: Use ktime_get_ns()

Quoted 16 lines · Thomas Gleixner
Quoted 10 lines · Lee Jones
Quoted 8 lines

Replace the ever recurring:

	ts = ktime_get_ts();
	ns = timespec_to_ns(&ts);
with
	ns = ktime_get_ns();

Only Sam? How old is your version of MAINTAINERS?

Hmm. I use get_maintainers.pl and manually edit it afterwards. Seems that I killed one extra line :)

Quoted 8 lines
Quoted 3 lines

Sam hasn't actively maintained MFD for over a year now.

Patch applied though, thanks.

ktime_get_ns() is not upstream yet, it's part of the series I posted :)

If you ack it, i'll carry it and also remove the now unused variable ts before applying it.

Very well. Patch looks good to me:

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#47 lore raw

PATCH 36/55 misc: ioc4: Use ktime_get_ns()

In reply to Thomas Gleixner

Replace the ever recurring:

        ts = ktime_get_ts();
        ns = timespec_to_ns(&ts);
with
        ns = ktime_get_ns();
Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/misc/ioc4.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/misc/ioc4.c +2 -5
Index: tip/drivers/misc/ioc4.c
===================================================================
@@ -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));
 
#50 lore raw

PATCH 37/55 net: mlx5: Use ktime_get_ns()

In reply to Thomas Gleixner

This 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
Index: tip/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
===================================================================
@@ -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_c
 		if (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_c
 	void *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
Index: tip/include/linux/mlx5/driver.h
===================================================================
@@ -605,8 +605,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;
 };
 
#51 lore raw

PATCH 38/55 fs: lockd: Use ktime_get_ns()

In reply to Thomas Gleixner

Replace the ever recurring:

        ts = ktime_get_ts();
        ns = timespec_to_ns(&ts);
with
        ns = ktime_get_ns();
Cc "J. Bruce Fields" <[email protected]>
Cc Trond Myklebust <[email protected]>
---
 fs/lockd/mon.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/lockd/mon.c +1 -3
Index: tip/fs/lockd/mon.c
===================================================================
@@ -306,11 +306,9 @@ static struct nsm_handle *nsm_lookup_pri
 static 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);
 }
Acked-by on v1 38/55
#52 lore raw

Re: [patch 38/55] fs: lockd: Use ktime_get_ns()

Quoted 22 lines · Thomas Gleixner

Replace the ever recurring:

        ts = ktime_get_ts();
        ns = timespec_to_ns(&ts);
with
        ns = ktime_get_ns();
Cc "J. Bruce Fields" <[email protected]>
Cc Trond Myklebust <[email protected]>
---
 fs/lockd/mon.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Index: tip/fs/lockd/mon.c
===================================================================

fs/lockd/mon.c +0 -0
@@ -306,11 +306,9 @@ static struct nsm_handle *nsm_lookup_pri
 static void nsm_init_private(struct nsm_handle *nsm)
 {

fs/lockd/mon.c static struct nsm_handle *nsm_lookup_pri · line 314

Quoted 10 lines
fs/lockd/mon.c +1 -3
        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);
 }

Looks fine to me.

Signature · 5 lines
Trond Myklebust

Linux NFS client maintainer, PrimaryData

[email protected]
#53 lore raw

PATCH 39/55 hwmon: ibmaem: Use ktime_get_ns()

In reply to 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 nanoseconds based interface right away.

---
 drivers/hwmon/ibmaem.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/hwmon/ibmaem.c +2 -4
Index: tip/drivers/hwmon/ibmaem.c
===================================================================
@@ -842,11 +842,10 @@ static ssize_t aem_show_power(struct dev
 	struct 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",
#54 lore raw

PATCH 40/55 iio: Use ktime_get_real_ns()

In reply to Thomas Gleixner

No idea why iio needs wall clock based time stamps, but we can avoid the timespec conversion dance by using the new interfaces.

Cc Jonathan Cameron <[email protected]>
---
 include/linux/iio/iio.h |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
include/linux/iio/iio.h +1 -8
Index: tip/include/linux/iio/iio.h
===================================================================
@@ -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 */
#56 lore raw

PATCH 41/55 arm: bL_switcher:k Use ktime_get_real_ns()

In reply to Thomas Gleixner

Use the nanoseconds based interface instead of converting from a timespec.

Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 arch/arm/common/bL_switcher.c |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
arch/arm/common/bL_switcher.c +3 -13
Index: tip/arch/arm/common/bL_switcher.c
===================================================================
@@ -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 mp
 
 static 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)
#57 lore raw

PATCH 42/55 x86: kvm: Use ktime_get_boot_ns()

In reply to Thomas Gleixner

Use 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
Index: tip/arch/x86/kvm/x86.c
===================================================================
@@ -1109,11 +1109,7 @@ static void kvm_get_time_scale(uint32_t
 
 static 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
#58 lore raw

PATCH 43/55 x86: kvm: Make kvm_get_time_and_clockread() nanoseconds based

In reply to Thomas Gleixner

Convert 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
Index: tip/arch/x86/kvm/x86.c
===================================================================
@@ -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 *cycle
 	return 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(&gtod->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(&gtod->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
 
#59 lore raw

PATCH 44/55 timekeeping: Remove monotonic_to_bootbased

In reply to Thomas Gleixner

No more users.

---
 include/linux/timekeeping.h |    1 -
 kernel/time/timekeeping.c   |   15 ---------------
 2 files changed, 16 deletions(-)
include/linux/timekeeping.h +0 -1
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1574,21 +1574,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;
#60 lore raw

PATCH 45/55 timekeeping: Use ktime_get_boottime() for get_monotonic_boottime()

In reply to Thomas Gleixner

get_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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -1540,40 +1540,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;
#61 lore raw

PATCH 46/55 timekeeping: Provide ktime_get_raw()

In reply to Thomas Gleixner

Provide a ktime_t based interface for raw monotonic time.

---
 include/linux/timekeeper_internal.h |    3 +++
 include/linux/timekeeping.h         |    6 ++++++
 kernel/time/timekeeping.c           |   26 ++++++++++++++++++++++++++
 3 files changed, 35 insertions(+)
include/linux/timekeeper_internal.h +3 -0
Index: tip/include/linux/timekeeper_internal.h
===================================================================
@@ -56,6 +56,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
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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(void
 	return 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 +26 -0
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -354,6 +354,7 @@ static void timekeeping_forward_now(stru
 
 	nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
 	timespec64_add_ns(&tk->raw_time, nsec);
+	tk->base_raw = ktime_add_ns(tk->base_raw, nsec);
 }
 
 /**
@@ -470,6 +471,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
  *
@@ -912,6 +934,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);
 
@@ -1405,6 +1428,9 @@ static cycle_t logarithmic_accumulation(
 
 	/* Accumulate raw time */
 	raw_nsecs = (u64)tk->raw_interval << shift;
+	/* Update ktime_t base */
+	tk->base_raw = ktime_add_ns(tk->base_raw, raw_nsecs);
+	/* Update the timespec base */
 	raw_nsecs += tk->raw_time.tv_nsec;
 	if (raw_nsecs >= NSEC_PER_SEC) {
 		u64 raw_secs = raw_nsecs;
#62 lore raw

PATCH 47/55 hangcheck-timer: Use ktime_get_raw_ns()

In reply to Thomas Gleixner

Use the nanoseconds based interface instead of converting timespecs.

Cc Arnd Bergmann <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/char/hangcheck-timer.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/char/hangcheck-timer.c +4 -6
Index: tip/drivers/char/hangcheck-timer.c
===================================================================
@@ -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"
 
@@ -129,9 +129,7 @@ extern unsigned long long monotonic_cloc
 #else
 static inline unsigned long long monotonic_clock(void)
 {
-	struct timespec ts;
-	getrawmonotonic(&ts);
-	return timespec_to_ns(&ts);
+	return ktime_get_raw_ns();
 }
 #endif  /* HAVE_MONOTONIC */
 
@@ -186,9 +184,9 @@ 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");
+	printk("Hangcheck: Using monotonic clock\n");
 #else
-	printk("Hangcheck: Using getrawmonotonic().\n");
+	printk("Hangcheck: Using raw monotonic clock.\n");
 #endif  /* HAVE_MONOTONIC */
 	hangcheck_tsc_margin =
 		(unsigned long long)(hangcheck_margin + hangcheck_tick);
#65 lore raw

PATCH 48/55 drm: i915: Use nsec based interfaces

In reply to Thomas Gleixner

No point in converting timespecs back and forth.

---
 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
Index: tip/drivers/gpu/drm/i915/i915_drv.h
===================================================================
@@ -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
Index: tip/drivers/gpu/drm/i915/i915_gem.c
===================================================================
@@ -1149,16 +1149,16 @@ static bool can_wait_boost(struct drm_i9
 static 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_eng
 	if (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_eng
 			destroy_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_eng
 	finish_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 *d
 	struct 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
Index: tip/drivers/gpu/drm/i915/intel_pm.c
===================================================================
@@ -2993,7 +2993,7 @@ static void ironlake_enable_drps(struct
 		I915_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_i91
 
 static 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;
 
#66 lore raw

Re: [patch 48/55] drm: i915: Use nsec based interfaces

Quoted 4 lines · Thomas Gleixner

No point in converting timespecs back and forth.

I guess simplest to merge together with the new ktime_get_raw_ns.
Acked-by: me for that.
-Daniel

Quoted 163 lines
---
 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(-)

Index: tip/drivers/gpu/drm/i915/i915_drv.h
===================================================================

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;
 

Index: tip/drivers/gpu/drm/i915/i915_gem.c
===================================================================

drivers/gpu/drm/i915/i915_gem.c +12 -21
@@ -1149,16 +1149,16 @@ static bool can_wait_boost(struct drm_i9
 static 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_eng
        if (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_eng
                        destroy_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_eng
        finish_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 *d
        struct 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);

Index: tip/drivers/gpu/drm/i915/intel_pm.c
===================================================================

drivers/gpu/drm/i915/intel_pm.c +5 -7
@@ -2993,7 +2993,7 @@ static void ironlake_enable_drps(struct
                I915_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_i91
 
 static 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;
 
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#67 lore raw

PATCH 49/55 drm: vmwgfx: Use nsec based interfaces

In reply to Thomas Gleixner

No point in converting timespecs back and forth.

Cc Thomas Hellstrom <[email protected]>
---
 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
Index: tip/drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
===================================================================
@@ -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

#68 lore raw

PATCH 50/55 wireless: ath9k: Get rid of timespec conversions

In reply to Thomas Gleixner

We have interfaces. Remove the open coded cruft. Reduces text size along with the code.

Cc QCA ath9k Development <[email protected]>
Cc "John W. Linville" <[email protected]>
---
 drivers/net/wireless/ath/ath9k/hw.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/wireless/ath/ath9k/hw.c +2 -5
Index: tip/drivers/net/wireless/ath/ath9k/hw.c
===================================================================
@@ -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))
#69 lore raw

PATCH 51/55 clocksource: Make delta calculation a function

In reply to Thomas Gleixner

We 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          |   27 ++++++++++++++-------------
 kernel/time/timekeeping_internal.h |    6 ++++++
 3 files changed, 27 insertions(+), 18 deletions(-)
kernel/time/clocksource.c +7 -5
Index: tip/kernel/time/clocksource.c
===================================================================
@@ -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 cl
 static 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(unsigne
 			continue;
 		}
 
-		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 -13
Index: tip/kernel/time/timekeeping.c
===================================================================
@@ -197,7 +197,7 @@ static inline u32 arch_gettimeoffset(voi
 
 static inline s64 timekeeping_get_ns(struct timekeeper *tk)
 {
-	cycle_t cycle_now, cycle_delta;
+	cycle_t cycle_now, delta;
 	struct clocksource *clock;
 	s64 nsec;
 
@@ -206,9 +206,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() */
@@ -217,7 +217,7 @@ static inline s64 timekeeping_get_ns(str
 
 static 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;
 
@@ -226,10 +226,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();
@@ -335,16 +335,15 @@ 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;
-	tk->cycle_last = clock->cycle_last = cycle_now;
+	delta = clocksource_delta(cycle_now, clock->cycle_last, clock->mask);
 
-	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;
@@ -352,7 +351,7 @@ static void timekeeping_forward_now(stru
 	/* Updates tk->base_mono as well */
 	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);
 	tk->base_raw = ktime_add_ns(tk->base_raw, nsec);
 }
@@ -1063,7 +1062,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
@@ -1472,7 +1472,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
Index: tip/kernel/time/timekeeping_internal.h
===================================================================
@@ -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 */
maintainer
#70 lore raw

Re: [patch 51/55] clocksource: Make delta calculation a function

kernel/time/timekeeping_internal.h extern void tk_debug_account_sleep_time( · line 18

Quoted 4 lines · Thomas Gleixner
kernel/time/timekeeping_internal.h +4 -0
+static inline cycle_t clocksource_delta(cycle_t now, cycle_t last, cycle_t mask)
+{
+	return (now -last) & mask;
+}

Inconsistent whitespace there.

#71 lore raw

Re: [patch 51/55] clocksource: Make delta calculation a function

In reply to Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
kernel/time/timekeeping.c +0 -0
@@ -335,16 +335,15 @@ static void timekeeping_update(struct ti
  */
 static void timekeeping_forward_now(struct timekeeper *tk)
 {

kernel/time/timekeeping.c static void timekeeping_update(struct ti · line 344

Quoted 10 lines
kernel/time/timekeeping.c +2 -3
-       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;
-       tk->cycle_last = clock->cycle_last = cycle_now;
+       delta = clocksource_delta(cycle_now, clock->cycle_last, clock->mask);

Errr.. Was dropping the update to tk->cycle_last and clock->cycle_last intended? That seems broken.

thanks
-john

#72 lore raw

Re: [patch 51/55] clocksource: Make delta calculation a function

Quoted 10 lines · John Stultz
Quoted 9 lines · Thomas Gleixner
kernel/time/timekeeping.c +1 -1
@@ -335,16 +335,15 @@ 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;
 
Quoted 8 lines

kernel/time/timekeeping.c static void timekeeping_update(struct ti · line 344

Quoted 5 lines
kernel/time/timekeeping.c +1 -2
        clock = tk->clock;
        cycle_now = clock->read(clock);
-       cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
-       tk->cycle_last = clock->cycle_last = cycle_now;
+       delta = clocksource_delta(cycle_now, clock->cycle_last, clock->mask);

Errr.. Was dropping the update to tk->cycle_last and clock->cycle_last intended? That seems broken.

Grr, was definitely not intended.

#73 lore raw

PATCH 52/55 clocksource: Move cycle_last validation to core code

In reply to Thomas Gleixner

The 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              |   14 +++++++++-----
 kernel/time/Kconfig                |    5 +++++
 kernel/time/timekeeping_internal.h |    9 +++++++++
 4 files changed, 24 insertions(+), 5 deletions(-)
arch/x86/Kconfig +1 -0
Index: tip/arch/x86/Kconfig
===================================================================
@@ -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 -5
Index: tip/arch/x86/kernel/tsc.c
===================================================================
@@ -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,13 +961,14 @@ 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;
+	return (cycle_t)get_cycles();
 }
 
 static void resume_tsc(struct clocksource *cs)
@@ -976,6 +977,9 @@ static void resume_tsc(struct clocksourc
 		clocksource_tsc.cycle_last = 0;
 }
 
+/*
+ * .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc()
+ */
 static struct clocksource clocksource_tsc = {
 	.name                   = "tsc",
 	.rating                 = 300,
kernel/time/Kconfig +5 -0
Index: tip/kernel/time/Kconfig
===================================================================
@@ -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
Index: tip/kernel/time/timekeeping_internal.h
===================================================================
@@ -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 */
#74 lore raw

PATCH 53/55 seqcount: Provide raw_read_seqcount()

In reply to Thomas Gleixner

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.

Preparatory patch to provide NMI safe clock monotonic[_raw] accessor functions.

Cc John Stultz <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/seqlock.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/seqlock.h +16 -0
Index: tip/include/linux/seqlock.h
===================================================================
@@ -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
#75 lore raw

PATCH 54/55 timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

In reply to Thomas Gleixner

Tracers 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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;
  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 NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a big issue for the intended use case.

Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
---
 include/linux/timekeeping.h |    2 
 kernel/time/timekeeping.c   |  208 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 183 insertions(+), 27 deletions(-)
include/linux/timekeeping.h +2 -0
Index: tip/include/linux/timekeeping.h
===================================================================
@@ -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 +181 -27

266 lines not on this page · raw message

#76 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

----- Original Message -----

Quoted 7 lines

From: "Thomas Gleixner" <[email protected]>
To: "LKML" <[email protected]>
Cc: "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Friday, July 11, 2014 9:45:19 AM
Subject: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Hi Thomas,

Thanks for submitting this patch. It will be very useful for tracing! A few comments,

Quoted 4 lines

Tracers 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

"an non blocking" -> "a non-blocking"

Quoted 9 lines

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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;

missing ")"

Quoted 3 lines
  tkf->seq++;
  smp_wmb();
  update(tkf->base[1], tk;

missing ")"

Any reason why the updater wouldn't do:

tkf->seq++;
smp_wmb();
update(tkf->base[1 - (tkf->seq & 0x01)], tk);

instead of updating both array entries each time ?

Quoted 11 lines

On the reader side:

  do {
     seq = tkf->seq;
     smp_rmb();
     idx = seq & 0x01;
     now = now(tkf->base[idx]);
     smp_rmb();
  } while (seq != tkf->seq)
Quoted 3 lines

So if NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a

"accross" -> "across"

Quoted 73 lines

big issue for the intended use case.

Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
---
 include/linux/timekeeping.h |    2
 kernel/time/timekeeping.c   |  208
 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 183 insertions(+), 27 deletions(-)

Index: tip/include/linux/timekeeping.h
===================================================================

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
  */

Index: tip/kernel/time/timekeeping.c
===================================================================

kernel/time/timekeeping.c +36 -0
@@ -50,6 +50,42 @@ int __read_mostly timekeeping_suspended;
 /* Flag for if there is a persistent clock on this platform */
 bool __read_mostly persistent_clock_exist = false;
 
+/**
+ * struct tk_fast_base - timekeeper data for NMI safe fast access
+ * @clock:	Pointer to the clocksource
+ * @cycle_last:	The reference cycles for delta calculation
+ * @base:	The base value for the readout
+ * @shift:	Shift factor for scaled math
+ * @mult:	Mult factor for scaled math
+ *
+ * Note: We store cycle_last independent from clock->cycle_last so the
+ * update of the real timekeeper does not disturb the fast ones.
+ */
+struct tk_fast_base {
+	struct clocksource	*clock;
+	cycle_t			cycle_last;
+	u64			base;
+	u32			shift;
+	u32			mult;
+};
+
+/**
+ * struct tk_fast - NMI safe timekeeper
+ * @seq:	Sequence counter for protecting updates. The lowest bit
+ *		is the index for the tk_fast_base array
+ * @base:	tk_fast_base array. Access is indexed by the lowest bit of
+ *		@seq.
+ *
+ * See @update_fast_timekeeper() below.
+ */
+struct tk_fast {
+	seqcount_t		seq;
+	struct tk_fast_base	base[2];
+};
+
+static struct tk_fast tk_fast_raw  ____cacheline_aligned;
+static struct tk_fast tk_fast_mono ____cacheline_aligned;
+
 /*
  * The xtime based monotonic readout is:
  *	nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns(str · line 254

Quoted 5 lines
kernel/time/timekeeping.c +1 -1
@@ -215,7 +251,7 @@ static inline s64 timekeeping_get_ns(str
 	return nsec + arch_gettimeoffset();
 }
 
-static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
+static inline s64 notrace timekeeping_get_ns_raw(struct tk_fast_base *tk)

So here, am I correct in saying that CLOCK_MONOTONIC_RAW would now use this implementation ? Why can we assume that the tk_fast_base will ensure that time never goes even slightly backwards from the point of view of a thread ?

Quoted 25 lines
kernel/time/timekeeping.c +3 -1
 {
 	cycle_t cycle_now, delta;
 	struct clocksource *clock;
@@ -226,7 +262,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);
@@ -235,6 +271,136 @@ static inline s64 timekeeping_get_ns_raw
 	return nsec + arch_gettimeoffset();
 }
 
+/**
+ * update_fast_timekeeper - Update the fast and NMI safe monotonic

timekeeper.
+ * @tk: The timekeeper from which we take the update
+ * @tkf: The fast timekeeper to update
+ * @tbase: The time base for the fast timekeeper (mono/raw)
+ *
+ * We want to use this from any context including NMI and tracing /
+ * instrumenting the timekeeping code itself.

Quoted 8 lines

+ *
+ * So we handle this differently than the other timekeeping accessor
+ * functions which retry when the sequence count has changed. The
+ * update side does:
+ *
+ * tkf->seq++;
+ * smp_wmb();
+ * update(tkf->base[0], tk;

missing ")";

Quoted 3 lines

+ * tkf->seq++;
+ * smp_wmb();
+ * update(tkf->base[1], tk;

missing ")".

Quoted 8 lines

+ *
+ * The reader side does:
+ *
+ * do {
+ * seq = tkf->seq;
+ * smp_rmb();
+ * idx = seq & 0x01;
+ * now = now(tkf->base[idx]);

Quoted 8 lines

+ * smp_rmb();
+ * } while (seq != tkf->seq)
+ *
+ * As long as we update base[0] readers are forced off to
+ * base[1]. Once base[0] is updated readers are redirected to base[0]
+ * and the base[1] update takes place.
+ *
+ * Soif NMI hits the update of base[0] then it will use base[1] which

"Soif" -> "So if"

Quoted 4 lines

+ * is still consistent. In the worst case this can result is a
+ * slightly wrong timestamp (a few nanoseconds) for CLOCK_MONOTONIC
+ * only. Tracing and instrumentation is blury anyway, so this is not
+ * really an issue.

A time source can be "slightly wrong" without ever going backwards from the POV of a thread. We might want to explicitly spell out that time can go slightly backward from the POV of a single thread, and that the caller should expect this.

Quoted 71 lines

+ */
+static void update_fast_timekeeper(struct clocksource *clk, struct tk_fast *tkf,

kernel/time/timekeeping.c +68 -0
+				   s64 tbase, u32 mult, u32 shift)
+{
+	struct tk_fast_base *base = tkf->base;
+
+	/* Force readers off to base[1] */
+	raw_write_seqcount_begin(&tkf->seq);
+
+	/* Update base[0] */
+	base->clock = clk;
+	base->cycle_last = clk->cycle_last;
+	base->base = tbase;
+	base->shift = shift;
+	base->mult = mult;
+
+	/* Force readers back to base[0] */
+	raw_write_seqcount_end(&tkf->seq);
+
+	/* Update base[1] */
+	base++;
+	base->clock = clk;
+	base->cycle_last = clk->cycle_last;
+	base->base = tbase;
+	base->shift = shift;
+	base->mult = mult;
+}
+
+static void update_fast_timekeepers(struct timekeeper *tk)
+{
+	struct clocksource *clk = tk->clock;
+	s64 base;
+
+	/*
+	 * Calulate the monotonic base in nano seconds. That's less
+	 * accurate than the real monotonic time as we drop the
+	 * fractial nsecs of xtime_nsec with the shift. But good
+	 * enough for the fast stuff we want.
+	 */
+	base = ktime_to_ns(tk->base_mono) + (tk->xtime_nsec >> tk->shift);
+	update_fast_timekeeper(clk, &tk_fast_mono, base, tk->mult, tk->shift);
+	/* Update the raw timekeeper */
+	base = ktime_to_ns(tk->base_raw);
+	update_fast_timekeeper(clk, &tk_fast_raw, base, clk->mult, clk->shift);
+}
+
+/*
+ * The reader function for the fast NMI safe timekeepers.
+ */
+static u64 notrace ktime_get_fast_ns(struct tk_fast *tkf)
+{
+	struct tk_fast_base *b;
+	unsigned int seq;
+	u64 now;
+
+	do {
+		seq = raw_read_seqcount(&tkf->seq);
+		b = tkf->base + (seq & 0x01);
+		now = b->base + timekeeping_get_ns_raw(b);
+
+	} while (read_seqcount_retry(&tkf->seq, seq));
+	return now;
+}
+
+/**
+ * ktime_get_raw - Returns the raw monotonic time in ktime_t format
+ *
+ * Can be called from any context including NMI
+ */
+ktime_t notrace ktime_get_raw(void)

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 392

Quoted 10 lines
kernel/time/timekeeping.c +10 -0
+{
+	return ns_to_ktime(ktime_get_fast_ns(&tk_fast_raw));
+}
+EXPORT_SYMBOL_GPL(ktime_get_raw);
+
+/**
+ * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
+ *
+ * This timestamp is not guaranteed to be monotonic because the
+ * nanoseconds reminder of the base time is not accounted. So accross

"accross" -> "across"
"reminder" -> "remainder"

Quoted 4 lines

+ * an update time can go slighty backwards in the single digit
+ * nanoseconds range, if the mult/shift factors are adjusted by the
+ * update. So don't use this for code which might be sensitive about
+ * that. For the intended use case of tracing and instrumentation its

"its" -> "it's"

Thanks,

Mathieu

Quoted 90 lines

+ * a non issue.
+ */

kernel/time/timekeeping.c +18 -25
+u64 notrace ktime_get_mono_fast_ns(void)
+{
+	return ktime_get_fast_ns(&tk_fast_mono);
+}
+
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
 
 static inline void update_vsyscall(struct timekeeper *tk)
@@ -324,6 +490,8 @@ static void timekeeping_update(struct ti
 	if (action & TK_MIRROR)
 		memcpy(&shadow_timekeeper, &tk_core.timekeeper,
 		       sizeof(tk_core.timekeeper));
+
+	update_fast_timekeepers(tk);
 }
 
 /**
@@ -470,27 +638,6 @@ 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
  *
@@ -574,13 +721,19 @@ void getnstime_raw_and_real(struct times
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
 
-		*ts_raw = timespec64_to_timespec(tk->raw_time);
 		ts_real->tv_sec = tk->xtime_sec;
 		ts_real->tv_nsec = 0;
-
-		nsecs_raw = timekeeping_get_ns_raw(tk);
 		nsecs_real = timekeeping_get_ns(tk);
 
+		/*
+		 * base[0] of tk_fast_raw is valid here as we are
+		 * protected by the tk_core.seq counter. The raw_base
+		 * has it's own sequence counter, but that is updated
+		 * under tk_core.seq.
+		 */
+		*ts_raw = timespec64_to_timespec(tk->raw_time);
+		nsecs_raw = timekeeping_get_ns_raw(tk_fast_raw.base);
+
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
 	timespec_add_ns(ts_raw, nsecs_raw);
@@ -813,7 +966,7 @@ void getrawmonotonic(struct timespec *ts
 
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
-		nsecs = timekeeping_get_ns_raw(tk);
+		nsecs = timekeeping_get_ns_raw(tk_fast_raw.base);
 		ts64 = tk->raw_time;
 
 	} while (read_seqcount_retry(&tk_core.seq, seq));
@@ -946,6 +1099,7 @@ void __init timekeeping_init(void)
 
 	memcpy(&shadow_timekeeper, &tk_core.timekeeper,
 	       sizeof(tk_core.timekeeper));
+	update_fast_timekeepers(tk);
 
 	write_seqcount_end(&tk_core.seq);
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#77 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 9 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 8 lines

From: "Thomas Gleixner" <[email protected]>
To: "LKML" <[email protected]>
Cc: "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Friday, July 11, 2014 9:45:19 AM
Subject: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW] On the update side:

Quoted 5 lines
Quoted 3 lines
  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;

missing ")"

:)

Quoted 7 lines

Any reason why the updater wouldn't do:

tkf->seq++;
smp_wmb();
update(tkf->base[1 - (tkf->seq & 0x01)], tk);

instead of updating both array entries each time ?

base[0]; <- Current active
seq++; -> Makes base[1] the active one for readers
update(base[0]);

So readers are always one update cycle behind. Probably not an issue most of the time, but think about fast wrapping clocksources and NOHZ....

Thanks,

tglx

#78 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

----- Original Message -----

Quoted 37 lines

From: "Thomas Gleixner" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: "LKML" <[email protected]>, "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>
Sent: Saturday, July 12, 2014 4:11:17 AM
Subject: Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 12 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 11 lines

From: "Thomas Gleixner" <[email protected]>
To: "LKML" <[email protected]>
Cc: "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Friday, July 11, 2014 9:45:19 AM
Subject: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]
On the update side:

Quoted 5 lines
Quoted 3 lines
  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;

missing ")"

:)

Quoted 7 lines

Any reason why the updater wouldn't do:

tkf->seq++;
smp_wmb();
update(tkf->base[1 - (tkf->seq & 0x01)], tk);

instead of updating both array entries each time ?

Quoted 7 lines

base[0]; <- Current active
seq++; -> Makes base[1] the active one for readers
update(base[0]);

So readers are always one update cycle behind. Probably not an issue most of the time, but think about fast wrapping clocksources and NOHZ....

Ah, yep, got it.

So instead of calling update() twice per update, could we do the following instead ?

tkf->seq++;
smp_wmb();
update(tkf->base[0], tk);
tkf->seq++;
smp_wmb();
memcpy(tkf->base[1], tkf->base[0], sizeof(tkf->base[1]);

just in case "update" would happen to be expensive for some clock implementation.

Thanks,

Mathieu

Quoted 7 lines

Thanks,

tglx

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#79 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 31 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 10 lines

From: "Thomas Gleixner" <[email protected]>

Quoted 7 lines · Mathieu Desnoyers

Any reason why the updater wouldn't do:

tkf->seq++;
smp_wmb();
update(tkf->base[1 - (tkf->seq & 0x01)], tk);

instead of updating both array entries each time ?

Quoted 7 lines

base[0]; <- Current active
seq++; -> Makes base[1] the active one for readers
update(base[0]);

So readers are always one update cycle behind. Probably not an issue most of the time, but think about fast wrapping clocksources and NOHZ....

Ah, yep, got it.

So instead of calling update() twice per update, could we do the following instead ?

tkf->seq++;
smp_wmb();
update(tkf->base[0], tk);
tkf->seq++;
smp_wmb();
memcpy(tkf->base[1], tkf->base[0], sizeof(tkf->base[1]);

Quoted 2 lines

just in case "update" would happen to be expensive for some clock implementation.

Sure, same end result ...

Thanks,

tglx

#80 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

In reply to Thomas Gleixner

----- Original Message -----

Quoted 35 lines

From: "Thomas Gleixner" <[email protected]>
To: "LKML" <[email protected]>
Cc: "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Friday, July 11, 2014 9:45:19 AM
Subject: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Tracers 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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;
  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)
Quoted 4 lines

So if NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a big issue for the intended use case.

Hi Thomas,

I'm perhaps missing something here, but what happens with the following scenario ?

Initial conditions:

tkf->seq = 0
tkf->base[0] and tkf->base[1] are initialized.

CPU 0 CPU 1
------------ ----------------
update:
tkf->seq++
smb_wmb()
tkf->seq++ (reordered before update)

                                           reader:
                                           seq = tkf->seq (reads 2)
                                           smp_rmb()
                                           idx = seq & 0x01
                                           now = now(tkf->base[idx]   (reads base[0])
  update(tkf->base[0], tk) (racy concurrent update)
                                           smp_rmb()
                                           while (seq != tkf->seq) (they are equal)

So AFAIU, we end up returning a corrupted value. Adding a smp_wmb() between update of base[0] and increment of seq, as well as between update of base[1] and the _following_ increment of seq (next update call) would fix this.

Thoughts ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#81 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 23 lines · Mathieu Desnoyers

I'm perhaps missing something here, but what happens with the following scenario ?

Initial conditions:

tkf->seq = 0
tkf->base[0] and tkf->base[1] are initialized.

CPU 0 CPU 1
------------ ----------------
update:
tkf->seq++
smb_wmb()
tkf->seq++ (reordered before update)

                                           reader:
                                           seq = tkf->seq (reads 2)
                                           smp_rmb()
                                           idx = seq & 0x01
                                           now = now(tkf->base[idx]   (reads base[0])
  update(tkf->base[0], tk) (racy concurrent update)
                                           smp_rmb()
                                           while (seq != tkf->seq) (they are equal)
Quoted 6 lines

So AFAIU, we end up returning a corrupted value. Adding a smp_wmb() between update of base[0] and increment of seq, as well as between update of base[1] and the _following_ increment of seq (next update call) would fix this.

Thoughts ?

Well, the actual implementation does:

+ /* Force readers off to base[1] */
+ raw_write_seqcount_begin(&tkf->seq);
+

+       /* Update base[0] */
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;

+
+ /* Force readers back to base[0] */
+ raw_write_seqcount_end(&tkf->seq);
+

+       /* Update base[1] */
+       base++;
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;

Where raw_write_seqcount_begin/raw_write_seqcount_end provides the required memory barriers.

Sure I should update the documentaion ....

Thanks,

tglx

#82 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 24 lines · Thomas Gleixner
Quoted 23 lines · Mathieu Desnoyers

I'm perhaps missing something here, but what happens with the following scenario ?

Initial conditions:

tkf->seq = 0
tkf->base[0] and tkf->base[1] are initialized.

CPU 0 CPU 1
------------ ----------------
update:
tkf->seq++
smb_wmb()
tkf->seq++ (reordered before update)

                                           reader:
                                           seq = tkf->seq (reads 2)
                                           smp_rmb()
                                           idx = seq & 0x01
                                           now = now(tkf->base[idx]   (reads base[0])
  update(tkf->base[0], tk) (racy concurrent update)
                                           smp_rmb()
                                           while (seq != tkf->seq) (they are equal)
Quoted 6 lines
Quoted 6 lines

So AFAIU, we end up returning a corrupted value. Adding a smp_wmb() between update of base[0] and increment of seq, as well as between update of base[1] and the _following_ increment of seq (next update call) would fix this.

Thoughts ?

Second thoughts :)

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 320

Quoted 4 lines

Well, the actual implementation does:

kernel/time/timekeeping.c +2 -0
+       /* Force readers off to base[1] */
+       raw_write_seqcount_begin(&tkf->seq);

i.e:

	seq++;
	smp_wmb();

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 330

Quoted 10 lines
kernel/time/timekeeping.c +10 -0
+
+       /* Update base[0] */
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;
+
+       /* Force readers back to base[0] */
+       raw_write_seqcount_end(&tkf->seq);

i.e:

	smp_wmb();
	seq++;

So while this orders against the update of base0, it does not prevent reordering against the update of base1. So you're right, we need a

  	smp_wmb();

before we start updating base1.

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 338

Quoted 7 lines
kernel/time/timekeeping.c +7 -0
+       /* Update base[1] */
+       base++;
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;

So as a consequence we need another one here:

      	smp_wmb();

to protect against the unlikely, but possible seq++ at the begin of the update. Debatable whether this can happen without another wmb() between the two calls, but yes for sanity reasons we should add it until we can prove that the actual call chains prevent this.

Nice catch!

tglx

#83 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

----- Original Message -----

Quoted 84 lines

From: "Thomas Gleixner" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: "LKML" <[email protected]>, "John Stultz" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Steven Rostedt" <[email protected]>
Sent: Saturday, July 12, 2014 4:04:59 PM
Subject: Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 26 lines · Thomas Gleixner
Quoted 25 lines · Mathieu Desnoyers

I'm perhaps missing something here, but what happens with the following scenario ?

Initial conditions:

tkf->seq = 0
tkf->base[0] and tkf->base[1] are initialized.

CPU 0 CPU 1
------------ ----------------
update:
tkf->seq++
smb_wmb()
tkf->seq++ (reordered before update)

                                           reader:
                                           seq = tkf->seq (reads 2)
                                           smp_rmb()
                                           idx = seq & 0x01
                                           now = now(tkf->base[idx]
                                           (reads base[0])
  update(tkf->base[0], tk) (racy concurrent update)
                                           smp_rmb()
                                           while (seq != tkf->seq) (they
                                           are equal)
Quoted 6 lines
Quoted 6 lines

So AFAIU, we end up returning a corrupted value. Adding a smp_wmb() between update of base[0] and increment of seq, as well as between update of base[1] and the _following_ increment of seq (next update call) would fix this.

Thoughts ?

Second thoughts :)

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 320

Quoted 4 lines

Well, the actual implementation does:

kernel/time/timekeeping.c +2 -0
+       /* Force readers off to base[1] */
+       raw_write_seqcount_begin(&tkf->seq);

i.e:

	seq++;
	smp_wmb();

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 330

Quoted 10 lines
kernel/time/timekeeping.c +10 -0
+
+       /* Update base[0] */
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;
+
+       /* Force readers back to base[0] */
+       raw_write_seqcount_end(&tkf->seq);

i.e:

	smp_wmb();
	seq++;

So while this orders against the update of base0, it does not prevent reordering against the update of base1. So you're right, we need a

  	smp_wmb();

before we start updating base1.

kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw · line 338

Quoted 7 lines
kernel/time/timekeeping.c +7 -0
+       /* Update base[1] */
+       base++;
+       base->clock = clk;
+       base->cycle_last = clk->cycle_last;
+       base->base = tbase;
+       base->shift = shift;
+       base->mult = mult;

So as a consequence we need another one here:

Quoted 8 lines
      	smp_wmb();

to protect against the unlikely, but possible seq++ at the begin of the update. Debatable whether this can happen without another wmb() between the two calls, but yes for sanity reasons we should add it until we can prove that the actual call chains prevent this.

Nice catch!

Thanks! Yep, the barriers you propose are what appears to be missing,

Mathieu

Quoted 3 lines

tglx

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
maintainer
#84 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

In reply to Thomas Gleixner
Quoted 28 lines · Thomas Gleixner

Tracers 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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;
  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)
Quoted 4 lines

So if NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a big issue for the intended use case.

But it breaks monotonicity, right? ;-)

Also, what happens when TSC is not available as a clocksource? There's still a metric ton of hardware (including the latest generation HSW) that has fucked firmware/TSC.

#85 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Thomas Gleixner

Tracers 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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;
  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)
Quoted 6 lines
Quoted 4 lines

So if NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a big issue for the intended use case.

But it breaks monotonicity, right? ;-)

It can in theory, but does it really matter for tracing?

Quoted 3 lines

Also, what happens when TSC is not available as a clocksource? There's still a metric ton of hardware (including the latest generation HSW) that has fucked firmware/TSC.

Well, bad luck then. You end up using hpet or worse, but it's still your decision whether to base your instrumentation on that or not. For sane clock sources (i.e. almost anything except TSC) it works perfectly fine.

Thanks,

tglx

maintainer
#86 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 30 lines · Thomas Gleixner
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Thomas Gleixner

Tracers 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:

  tkf->seq++;
  smp_wmb();
  update(tkf->base[0], tk;
  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)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So if NMI hits the update of base[0] it will use base[1] which is still consistent. In case of CLOCK_MONOTONIC this can result in slightly wrong timestamps (a few nanoseconds) accross an update. Not a big issue for the intended use case.

But it breaks monotonicity, right? ;-)

It can in theory, but does it really matter for tracing?

Not so much; but I think the interfaces aren't quite as explicit as they maybe should be about this.

Quoted 8 lines
Quoted 3 lines

Also, what happens when TSC is not available as a clocksource? There's still a metric ton of hardware (including the latest generation HSW) that has fucked firmware/TSC.

Well, bad luck then. You end up using hpet or worse, but it's still your decision whether to base your instrumentation on that or not. For sane clock sources (i.e. almost anything except TSC) it works perfectly fine.

So I can imagine clock sources themselves not being NMI safe on a hardware level. But also, I'm fairly sure you do not want HPET reads in a function tracer :-)

And I absolutely detest this configurability muck. I want a single 'clock'.

But yes, we can do what you suggested, we can do something like:

if (tsc-stable)
return nmi-mono
else
return sched_clock

Seeing how with tsc-unstable the sched_clock muck is synched to clock monotonic.

maintainer
#87 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

In reply to Thomas Gleixner

kernel/time/timekeeping.c int __read_mostly timekeeping_suspended; · line 87

Quoted 2 lines · Thomas Gleixner
kernel/time/timekeeping.c +2 -0
+static struct tk_fast tk_fast_raw  ____cacheline_aligned;
+static struct tk_fast tk_fast_mono ____cacheline_aligned;

Question to the 'trace maniacs'; why do you lot need access to mono_raw?

I would much prefer to limit choice here.

#88 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

----- Original Message -----

Quoted 7 lines

From: "Peter Zijlstra" <[email protected]>
To: "Thomas Gleixner" <[email protected]>
Cc: "LKML" <[email protected]>, "John Stultz" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Monday, July 14, 2014 5:47:50 AM
Subject: Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 7 lines

kernel/time/timekeeping.c int __read_mostly timekeeping_suspended; · line 87

Quoted 2 lines · Thomas Gleixner
kernel/time/timekeeping.c +2 -0
+static struct tk_fast tk_fast_raw  ____cacheline_aligned;
+static struct tk_fast tk_fast_mono ____cacheline_aligned;

Question to the 'trace maniacs'; why do you lot need access to mono_raw?

I would much prefer to limit choice here.

For LTTng, which I guess qualifies as bring part of the aforementioned trace maniacs group, we only need the "tk_fast_mono" clock. We don't need mono_raw.

We can then correlate with user-space traces by using tk_fast_mono in kernel-space and using CLOCK_MONOTONIC in userspace.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#89 lore raw

Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

Quoted 14 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 13 lines

From: "Peter Zijlstra" <[email protected]>
To: "Thomas Gleixner" <[email protected]>
Cc: "LKML" <[email protected]>, "John Stultz" <[email protected]>, "Steven Rostedt" <[email protected]>, "Mathieu Desnoyers" <[email protected]>
Sent: Monday, July 14, 2014 5:47:50 AM
Subject: Re: [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW]

kernel/time/timekeeping.c int __read_mostly timekeeping_suspended; · line 87

Quoted 2 lines · Thomas Gleixner
kernel/time/timekeeping.c +2 -0
+static struct tk_fast tk_fast_raw  ____cacheline_aligned;
+static struct tk_fast tk_fast_mono ____cacheline_aligned;

Question to the 'trace maniacs'; why do you lot need access to mono_raw?

Quoted 8 lines
Quoted 1 line

I would much prefer to limit choice here.

For LTTng, which I guess qualifies as bring part of the aforementioned trace maniacs group, we only need the "tk_fast_mono" clock. We don't need mono_raw.

We can then correlate with user-space traces by using tk_fast_mono in kernel-space and using CLOCK_MONOTONIC in userspace.

I'm happy to ditch the raw one ...

#90 lore raw

PATCH 55/55 ftrace: Provide trace clocks mono and mono_raw

In reply to Thomas Gleixner

Expose the new NMI safe accessors to clock monotonic and monotonic raw to the tracer.

Cc Steven Rostedt <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
---
 kernel/trace/trace.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/trace/trace.c +7 -5
Index: tip/kernel/trace/trace.c
===================================================================
@@ -806,11 +806,13 @@ 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 },
+	{ ktime_get_raw_ns,		"mono_raw",	1 },
 	ARCH_TRACE_CLOCKS
 };