Skip to content

Commit

Permalink
tile: avoid overflow in ns2cycles
Browse files Browse the repository at this point in the history
In commit 4cecf6d ("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>
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
  • Loading branch information
henrikau authored and cmetcalf-tilera committed Mar 6, 2014
1 parent 0414855 commit 767f302
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion arch/tile/kernel/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 767f302

Please sign in to comment.