linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tile: avoid overflow in ns2cycles
@ 2014-03-04  8:20 Henrik Austad
  2014-03-06 16:53 ` Chris Metcalf
  0 siblings, 1 reply; 2+ messages in thread
From: Henrik Austad @ 2014-03-04  8:20 UTC (permalink / raw)
  To: Chris Metcalf
  Cc: LKML, Henrik Austad, John Stultz, Mike Galbraith, Salman Qazi

In commit 4cecf6d401a ("sched, x86: Avoid unnecessary overflow in
sched_clock") and in recent patch "clocksource: avoid unnecessary
overflow in cyclecounter_cyc2ns()" https://lkml.org/lkml/2014/3/4/17,
the mult-shift approach is replaced by 2 steps to avoid storing a large,
intermediate value that could overflow.

arch/tile/kernel/time.c has a similar pattern in cycles2ns, and this
copies the same pattern in this function

CC: John Stultz <johnstul@us.ibm.com>
CC: Mike Galbraith <bitbucket@online.de>
CC: Salman Qazi <sqazi@google.com>
Signed-off-by: Henrik Austad <henrik@austad.us>
---
 arch/tile/kernel/time.c |   10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/arch/tile/kernel/time.c b/arch/tile/kernel/time.c
index 5d10642..9e74733 100644
--- a/arch/tile/kernel/time.c
+++ b/arch/tile/kernel/time.c
@@ -236,7 +236,15 @@ cycles_t ns2cycles(unsigned long nsecs)
 	 * clock frequency.
 	 */
 	struct clock_event_device *dev = &__raw_get_cpu_var(tile_timer);
-	return ((u64)nsecs * dev->mult) >> dev->shift;
+
+	/*
+	 * as in clocksource.h and x86's timer.h, we split the calculation into  2
+	 * parts to avoid unecessary overflow of the intermediate value. This will
+	 * not lead to any loss of precision.
+	 */
+	u64 quot = (u64)nsecs >> dev->shift;
+	u64 rem  = (u64)nsecs & ((1ULL << dev->shift)-1);
+	return quot * dev->mult + ((rem * dev->mult) >> dev->shift);
 }
 
 void update_vsyscall_tz(void)
-- 
1.7.10.4


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

* Re: [PATCH] tile: avoid overflow in ns2cycles
  2014-03-04  8:20 [PATCH] tile: avoid overflow in ns2cycles Henrik Austad
@ 2014-03-06 16:53 ` Chris Metcalf
  0 siblings, 0 replies; 2+ messages in thread
From: Chris Metcalf @ 2014-03-06 16:53 UTC (permalink / raw)
  To: Henrik Austad; +Cc: LKML, John Stultz, Mike Galbraith, Salman Qazi

On 3/4/2014 3:20 AM, Henrik Austad wrote:
> In commit 4cecf6d401a ("sched, x86: Avoid unnecessary overflow in
> sched_clock") and in recent patch "clocksource: avoid unnecessary
> overflow in cyclecounter_cyc2ns()" https://lkml.org/lkml/2014/3/4/17,
> the mult-shift approach is replaced by 2 steps to avoid storing a large,
> intermediate value that could overflow.
>
> arch/tile/kernel/time.c has a similar pattern in cycles2ns, and this
> copies the same pattern in this function
>
> CC: John Stultz <johnstul@us.ibm.com>
> CC: Mike Galbraith <bitbucket@online.de>
> CC: Salman Qazi <sqazi@google.com>
> Signed-off-by: Henrik Austad <henrik@austad.us>
> ---
>  arch/tile/kernel/time.c |   10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)

Thanks; taken into the tile tree (with some minor whitespace formatting tweaks).

-- 
Chris Metcalf, Tilera Corp.
http://www.tilera.com


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

end of thread, other threads:[~2014-03-06 16:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-04  8:20 [PATCH] tile: avoid overflow in ns2cycles Henrik Austad
2014-03-06 16:53 ` 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).