[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <1393921253-23396-1-git-send-email-henrik@austad.us>
Date: Tue, 4 Mar 2014 09:20:53 +0100
From: Henrik Austad <henrik@...tad.us>
To: Chris Metcalf <cmetcalf@...era.com>
Cc: LKML <linux-kernel@...r.kernel.org>,
Henrik Austad <henrik@...tad.us>,
John Stultz <johnstul@...ibm.com>,
Mike Galbraith <bitbucket@...ine.de>,
Salman Qazi <sqazi@...gle.com>
Subject: [PATCH] tile: avoid overflow in ns2cycles
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@...ibm.com>
CC: Mike Galbraith <bitbucket@...ine.de>
CC: Salman Qazi <sqazi@...gle.com>
Signed-off-by: Henrik Austad <henrik@...tad.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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists