Skip to content

Commit 91ae257

Browse files
committed
timer/linux: prevent 64-bit overflow
The linux timer code was multiplying the result of the x86 time stamp counter by 1000000 before dividing by the cpu frequency. This can cause us to overflow 64 bits if the time stamp counter grows larger than ~ 1.8e13 (about 8400 seconds after boot). To fix the issue the units of opal_timer_linux_freq have been changed to MHz. Signed-off-by: Nathan Hjelm <[email protected]> (cherry picked from commit 45c0588) Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 44f56f2 commit 91ae257

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

opal/mca/timer/linux/timer_linux_component.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* All rights reserved.
1313
* Copyright (c) 2015 Research Organization for Information Science
1414
* and Technology (RIST). All rights reserved.
15-
* Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights
15+
* Copyright (c) 2015-2017 Los Alamos National Security, LLC. All rights
1616
* reserved.
1717
* Copyright (c) 2015-2017 Cisco Systems, Inc. All rights reserved.
1818
* $COPYRIGHT$
@@ -149,6 +149,10 @@ static int opal_timer_linux_find_freq(void)
149149

150150
fclose(fp);
151151

152+
/* convert the timer frequency to MHz to avoid an extra operation when
153+
* converting from cycles to usec */
154+
opal_timer_linux_freq /= 1000000;
155+
152156
return OPAL_SUCCESS;
153157
}
154158

@@ -160,7 +164,7 @@ int opal_timer_linux_open(void)
160164
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
161165
struct timespec res;
162166
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
163-
opal_timer_linux_freq = 1.e9;
167+
opal_timer_linux_freq = 1.e3;
164168
opal_timer_base_get_cycles = opal_timer_base_get_cycles_clock_gettime;
165169
opal_timer_base_get_usec = opal_timer_base_get_usec_clock_gettime;
166170
return ret;
@@ -209,16 +213,16 @@ opal_timer_t opal_timer_base_get_cycles_sys_timer(void)
209213
opal_timer_t opal_timer_base_get_usec_sys_timer(void)
210214
{
211215
#if OPAL_HAVE_SYS_TIMER_GET_CYCLES
212-
/* freq is in Hz, so this gives usec */
213-
return opal_sys_timer_get_cycles() * 1000000 / opal_timer_linux_freq;
216+
/* freq is in MHz, so this gives usec */
217+
return opal_sys_timer_get_cycles() / opal_timer_linux_freq;
214218
#else
215219
return 0;
216220
#endif
217221
}
218222

219223
opal_timer_t opal_timer_base_get_freq(void)
220224
{
221-
return opal_timer_linux_freq;
225+
return opal_timer_linux_freq * 1000000;
222226
}
223227

224228

0 commit comments

Comments
 (0)