mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: John Stultz <john.stultz@linaro.org>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [patch 51/55] clocksource: Make delta calculation a function
Date: Fri, 11 Jul 2014 13:45:15 -0000	[thread overview]
Message-ID: <20140711133709.584242982@linutronix.de> (raw)
In-Reply-To: <20140711133623.530368377@linutronix.de>

[-- Attachment #1: clocksource-make-delta-calculation-a-function.patch --]
[-- Type: text/plain, Size: 5797 bytes --]

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 ]

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/clocksource.c          |   12 +++++++-----
 kernel/time/timekeeping.c          |   27 ++++++++++++++-------------
 kernel/time/timekeeping_internal.h |    6 ++++++
 3 files changed, 27 insertions(+), 18 deletions(-)

Index: tip/kernel/time/clocksource.c
===================================================================
--- tip.orig/kernel/time/clocksource.c
+++ 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;
 
Index: tip/kernel/time/timekeeping.c
===================================================================
--- tip.orig/kernel/time/timekeeping.c
+++ 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 */
Index: tip/kernel/time/timekeeping_internal.h
===================================================================
--- tip.orig/kernel/time/timekeeping_internal.h
+++ 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 */



  parent reply	other threads:[~2014-07-11 13:47 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-11 13:43 [patch 00/55] timekeeping: 2038, optimizations, NMI safe accessors Thomas Gleixner
2014-07-11 13:43 ` [patch 01/55] tile: Convert VDSO timekeeping to the precise mechanism Thomas Gleixner
2014-07-18  2:32   ` Chris Metcalf
2014-07-11 13:44 ` [patch 02/55] timekeeping: Simplify arch_gettimeoffset() Thomas Gleixner
2014-07-11 14:10   ` Geert Uytterhoeven
2014-07-11 13:44 ` [patch 03/55] hrtimer: Cleanup hrtimer accessors to the timekepeing state Thomas Gleixner
2014-07-11 13:44 ` [patch 04/55] ktime: Kill non-scalar ktime_t implementation for 2038 Thomas Gleixner
2014-07-11 13:44 ` [patch 05/55] ktime: Sanitize ktime_to_us/ms conversion Thomas Gleixner
2014-07-11 13:44 ` [patch 06/55] ktime: Change ktime_set() to take 64bit seconds value Thomas Gleixner
2014-07-11 13:44 ` [patch 07/55] time64: Add time64.h header and define struct timespec64 Thomas Gleixner
2014-07-11 18:41   ` Richard Cochran
2014-07-11 13:44 ` [patch 08/55] time: More core infrastructure for timespec64 Thomas Gleixner
2014-07-11 13:44 ` [patch 09/55] timekeeping: Convert timekeeping core to use timespec64s Thomas Gleixner
2014-07-11 13:44 ` [patch 10/55] time: Consolidate the time accessor prototypes Thomas Gleixner
2014-07-11 13:44 ` [patch 11/55] timekeeping: Provide timespec64 based interfaces Thomas Gleixner
2014-07-11 13:44 ` [patch 12/55] timekeeper: Move tk_xtime to core code Thomas Gleixner
2014-07-11 13:44 ` [patch 13/55] timekeeping: Cache optimize struct timekeeper Thomas Gleixner
2014-07-11 13:44 ` [patch 14/55] timekeeping: Provide internal ktime_t based data Thomas Gleixner
2014-07-16  3:29   ` John Stultz
2014-07-16  7:00     ` Thomas Gleixner
2014-07-16  7:12       ` Thomas Gleixner
2014-07-16  7:20         ` Peter Zijlstra
2014-07-11 13:44 ` [patch 15/55] timekeeping: Use ktime_t based data for ktime_get() Thomas Gleixner
2014-07-11 13:44 ` [patch 16/55] timekeeping: Provide ktime_get_with_offset() Thomas Gleixner
2014-07-11 13:44 ` [patch 17/55] timekeeping: Use ktime_t based data for ktime_get_real() Thomas Gleixner
2014-07-11 13:44 ` [patch 18/55] timekeeping; Use ktime_t based data for ktime_get_boottime() Thomas Gleixner
2014-07-11 13:44 ` [patch 19/55] timekeeping: Use ktime_t based data for ktime_get_clocktai() Thomas Gleixner
2014-07-11 13:44 ` [patch 20/55] timekeeping: Use ktime_t data for ktime_get_update_offsets_now() Thomas Gleixner
2014-07-11 13:44 ` [patch 21/55] timekeeping; Use ktime based data for ktime_get_update_offsets_tick() Thomas Gleixner
2014-07-11 13:44 ` [patch 22/55] timekeeping: Provide ktime_mono_to_any() Thomas Gleixner
2014-07-11 13:44 ` [patch 23/55] timerfd: Use ktime_mono_to_real() Thomas Gleixner
2014-07-11 13:44 ` [patch 24/55] input: evdev: " Thomas Gleixner
2014-07-11 13:44 ` [patch 25/55] drm: " Thomas Gleixner
2014-07-11 13:44 ` [patch 26/55] timekeeping: Remove ktime_get_monotonic_offset() Thomas Gleixner
2014-07-11 13:44 ` [patch 27/55] timekeeping: Provide ktime_get[*]_ns() helpers Thomas Gleixner
2014-07-11 13:44 ` [patch 28/55] time-export-nsecs-to-jiffies.patch Thomas Gleixner
2014-07-11 13:44 ` [patch 29/55] sched: Make task->real_start_time nanoseconds based Thomas Gleixner
2014-07-11 13:44 ` [patch 30/55] sched: Make task->start_time " Thomas Gleixner
2014-07-11 13:44 ` [patch 31/55] delayacct: Make accounting nanosecond based Thomas Gleixner
2014-07-11 13:44 ` [patch 32/55] delayacct: Remove braindamaged type conversions Thomas Gleixner
2014-07-11 13:44 ` [patch 33/55] powerpc: cell: Use ktime_get_ns() Thomas Gleixner
2014-07-11 17:02   ` Arnd Bergmann
2014-07-11 13:44 ` [patch 34/55] connector: " Thomas Gleixner
2014-07-11 13:44 ` [patch 35/55] mfd: cros_ec_spi: " Thomas Gleixner
2014-07-11 14:52   ` Lee Jones
2014-07-12 19:40     ` Thomas Gleixner
2014-07-14  7:07       ` Lee Jones
2014-07-11 13:44 ` [patch 36/55] misc: ioc4: " Thomas Gleixner
2014-07-11 17:03   ` Arnd Bergmann
2014-07-12  0:26   ` Greg Kroah-Hartman
2014-07-11 13:44 ` [patch 37/55] net: mlx5: " Thomas Gleixner
2014-07-11 13:44 ` [patch 38/55] fs: lockd: " Thomas Gleixner
2014-07-11 14:04   ` Trond Myklebust
2014-07-11 13:44 ` [patch 39/55] hwmon: ibmaem: " Thomas Gleixner
2014-07-11 13:44 ` [patch 40/55] iio: Use ktime_get_real_ns() Thomas Gleixner
2014-07-13  9:21   ` Jonathan Cameron
2014-07-11 13:44 ` [patch 41/55] arm: bL_switcher:k " Thomas Gleixner
2014-07-11 13:45 ` [patch 42/55] x86: kvm: Use ktime_get_boot_ns() Thomas Gleixner
2014-07-11 13:45 ` [patch 43/55] x86: kvm: Make kvm_get_time_and_clockread() nanoseconds based Thomas Gleixner
2014-07-11 13:45 ` [patch 44/55] timekeeping: Remove monotonic_to_bootbased Thomas Gleixner
2014-07-11 13:45 ` [patch 45/55] timekeeping: Use ktime_get_boottime() for get_monotonic_boottime() Thomas Gleixner
2014-07-11 13:45 ` [patch 46/55] timekeeping: Provide ktime_get_raw() Thomas Gleixner
2014-07-11 13:45 ` [patch 47/55] hangcheck-timer: Use ktime_get_raw_ns() Thomas Gleixner
2014-07-11 17:03   ` Arnd Bergmann
2014-07-12  0:27   ` Greg Kroah-Hartman
2014-07-11 13:45 ` [patch 48/55] drm: i915: Use nsec based interfaces Thomas Gleixner
2014-07-14  9:21   ` Daniel Vetter
2014-07-11 13:45 ` [patch 49/55] drm: vmwgfx: " Thomas Gleixner
2014-07-11 13:45 ` [patch 50/55] wireless: ath9k: Get rid of timespec conversions Thomas Gleixner
2014-07-11 13:45 ` Thomas Gleixner [this message]
2014-07-14  8:31   ` [patch 51/55] clocksource: Make delta calculation a function Peter Zijlstra
2014-07-16  3:50   ` John Stultz
2014-07-16  6:58     ` Thomas Gleixner
2014-07-11 13:45 ` [patch 52/55] clocksource: Move cycle_last validation to core code Thomas Gleixner
2014-07-11 13:45 ` [patch 53/55] seqcount: Provide raw_read_seqcount() Thomas Gleixner
2014-07-11 13:45 ` [patch 54/55] timekeeping: Provide fast and NMI safe access to CLOCK_MONOTONIC[_RAW] Thomas Gleixner
2014-07-11 20:04   ` Mathieu Desnoyers
2014-07-12  8:11     ` Thomas Gleixner
2014-07-12 13:59       ` Mathieu Desnoyers
2014-07-12 19:31         ` Thomas Gleixner
2014-07-12 14:53   ` Mathieu Desnoyers
2014-07-12 19:28     ` Thomas Gleixner
2014-07-12 20:04       ` Thomas Gleixner
2014-07-12 20:33         ` Mathieu Desnoyers
2014-07-14  8:37   ` Peter Zijlstra
2014-07-14  9:04     ` Thomas Gleixner
2014-07-14  9:45       ` Peter Zijlstra
2014-07-14  9:47   ` Peter Zijlstra
2014-07-14 12:39     ` Mathieu Desnoyers
2014-07-14 14:15       ` Thomas Gleixner
2014-07-11 13:45 ` [patch 55/55] ftrace: Provide trace clocks mono and mono_raw Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140711133709.584242982@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome