linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clocksource_cyc2ns: avoid overflowing 64 bits
@ 2016-11-16 16:57 Chris Metcalf
  2016-11-16 18:04 ` John Stultz
  0 siblings, 1 reply; 18+ messages in thread
From: Chris Metcalf @ 2016-11-16 16:57 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Salman Qazi, Paul Turner,
	Tony Lindgren, Steven Miao, linux-kernel
  Cc: Chris Metcalf

For large values of "mult" and long uptimes, the intermediate
result of "cycles * mult" can overflow 64 bits.  For example,
the tile platform uses this helper function; for a 1.2 GHz clock,
we have mult = 853, and after 208.5 days, we overflow 64 bits.

The fix is basically the same as the fix for arch/x86 __cycles_2_ns()
in commit 4cecf6d401a0 ("sched, x86: Avoid unnecessary overflow in
sched_clock"), using the new mult_frac() helper.

In addition to tile, arm/plat-omap and blackfin also use this helper
function, so will presumably hit similar issues.

Signed-off-by: Chris Metcalf <cmetcalf@mellanox.com>
---
By the way, this is the bug that I was looking for when I tripped over
the missing bugfix for timekeeping_delta_to_ns() a couple of days ago :-)

 include/linux/clocksource.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 08398182f56e..b2a022acf232 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -175,7 +175,7 @@ static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
  */
 static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
 {
-	return ((u64) cycles * mult) >> shift;
+	return mult_frac(cycles, mult, 1ULL << shift);
 }
 
 
-- 
2.7.2

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

end of thread, other threads:[~2016-11-18 16:59 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-16 16:57 [PATCH] clocksource_cyc2ns: avoid overflowing 64 bits Chris Metcalf
2016-11-16 18:04 ` John Stultz
2016-11-16 19:30   ` Chris Metcalf
2016-11-16 19:35     ` [PATCH v2] tile: avoid using clocksource_cyc2ns with absolute cycle count Chris Metcalf
2016-11-16 19:59       ` John Stultz
2016-11-16 20:16         ` Chris Metcalf
2016-11-16 20:29           ` John Stultz
2016-11-16 20:31             ` John Stultz
2016-11-17  9:53           ` Peter Zijlstra
2016-11-17 20:00             ` Chris Metcalf
2016-11-18 10:34               ` Peter Zijlstra
2016-11-18 14:24                 ` Chris Metcalf
2016-11-18 14:50                   ` Peter Zijlstra
2016-11-16 19:40     ` [PATCH] clocksource_cyc2ns: avoid overflowing 64 bits John Stultz
2016-11-16 19:45     ` John Stultz
2016-11-16 19:56       ` Chris Metcalf
2016-11-16 20:00         ` John Stultz
2016-11-16 20:30           ` Chris Metcalf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).