public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()
@ 2014-03-04  5:20 Mike Galbraith
  2014-03-04  7:20 ` Henrik Austad
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Galbraith @ 2014-03-04  5:20 UTC (permalink / raw)
  To: John Stultz; +Cc: Salman Qazi, LKML

Greetings,

While rummaging around looking for HTH a gaggle of weird a$$ machines
can manage to timewarp back and forth by exactly 208 days, I stumbled
across $subject which looks like it may want to borrow Salman's fix.

clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()

As per 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock",
cycles * mult >> shift is overflow prone. so give it the same treatment.

Cc: Salman Qazi <sqazi@google.com>
Cc: John Stultz <johnstul@us.ibm.com>
Signed-off-by: Mike Galbraith <bitbucket@online.de>
---
 include/linux/clocksource.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -77,13 +77,18 @@ struct timecounter {
  *
  * XXX - This could use some mult_lxl_ll() asm optimization. Same code
  * as in cyc2ns, but with unsigned result.
+ *
+ * Because it is the same as x86 __cycles_2_ns, give it the same treatment as
+ * commit 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock"
+ * to avoid a potential cycles * mult overflow.
  */
 static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
 				      cycle_t cycles)
 {
-	u64 ret = (u64)cycles;
-	ret = (ret * cc->mult) >> cc->shift;
-	return ret;
+	u64 quot = (u64)cycles >> cc->shift;
+	u64 rem = (u64)cycles & ((1ULL << cc->shift) - 1);
+
+	return  quot * cc->mult + ((rem * cc->mult) >> cc->shift);
 }
 
 /**



^ permalink raw reply	[flat|nested] 11+ messages in thread
* [RFC][PATCH] clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()
@ 2014-03-04  5:38 Mike Galbraith
  2014-03-04  6:40 ` John Stultz
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Galbraith @ 2014-03-04  5:38 UTC (permalink / raw)
  To: John Stultz; +Cc: LKML, Cc: Salman Qazi

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()

As per 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock",
cycles * mult >> shift is overflow prone. so give it the same treatment.

Cc: Salman Qazi <sqazi@google.com>
Cc: John Stultz <john.stultz@linaro.org>,
Signed-off-by: Mike Galbraith <bitbucket@online.de>
---
 include/linux/clocksource.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -77,13 +77,18 @@ struct timecounter {
  *
  * XXX - This could use some mult_lxl_ll() asm optimization. Same code
  * as in cyc2ns, but with unsigned result.
+ *
+ * Because it is the same as x86 __cycles_2_ns, give it the same treatment as
+ * commit 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock"
+ * to avoid a potential cycles * mult overflow.
  */
 static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
 				      cycle_t cycles)
 {
-	u64 ret = (u64)cycles;
-	ret = (ret * cc->mult) >> cc->shift;
-	return ret;
+	u64 quot = (u64)cycles >> cc->shift;
+	u64 rem = (u64)cycles & ((1ULL << cc->shift) - 1);
+
+	return  quot * cc->mult + ((rem * cc->mult) >> cc->shift);
 }
 
 /**





^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-03-05  2:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-04  5:20 [RFC][PATCH] clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns() Mike Galbraith
2014-03-04  7:20 ` Henrik Austad
2014-03-04  7:31   ` Mike Galbraith
2014-03-04  7:36     ` Mike Galbraith
2014-03-04  8:02       ` Henrik Austad
2014-03-04  8:24         ` Mike Galbraith
  -- strict thread matches above, loose matches on Subject: below --
2014-03-04  5:38 Mike Galbraith
2014-03-04  6:40 ` John Stultz
2014-03-04  7:10   ` Mike Galbraith
2014-03-05  0:58     ` John Stultz
2014-03-05  2:56       ` Mike Galbraith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox