Skip to content

Commit 45c0588

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]>
1 parent 269e5e3 commit 45c0588

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 Cisco Systems, Inc. All rights reserved.
1818
* Copyright (c) 2016 Broadcom Limited. All rights reserved.
@@ -155,6 +155,10 @@ static int opal_timer_linux_find_freq(void)
155155

156156
fclose(fp);
157157

158+
/* convert the timer frequency to MHz to avoid an extra operation when
159+
* converting from cycles to usec */
160+
opal_timer_linux_freq /= 1000000;
161+
158162
return OPAL_SUCCESS;
159163
}
160164

@@ -166,7 +170,7 @@ int opal_timer_linux_open(void)
166170
#if OPAL_HAVE_CLOCK_GETTIME && (0 == OPAL_TIMER_MONOTONIC)
167171
struct timespec res;
168172
if( 0 == clock_getres(CLOCK_MONOTONIC, &res)) {
169-
opal_timer_linux_freq = 1.e9;
173+
opal_timer_linux_freq = 1.e3;
170174
opal_timer_base_get_cycles = opal_timer_base_get_cycles_clock_gettime;
171175
opal_timer_base_get_usec = opal_timer_base_get_usec_clock_gettime;
172176
return ret;
@@ -215,16 +219,16 @@ opal_timer_t opal_timer_base_get_cycles_sys_timer(void)
215219
opal_timer_t opal_timer_base_get_usec_sys_timer(void)
216220
{
217221
#if OPAL_HAVE_SYS_TIMER_GET_CYCLES
218-
/* freq is in Hz, so this gives usec */
219-
return opal_sys_timer_get_cycles() * 1000000 / opal_timer_linux_freq;
222+
/* freq is in MHz, so this gives usec */
223+
return opal_sys_timer_get_cycles() / opal_timer_linux_freq;
220224
#else
221225
return 0;
222226
#endif
223227
}
224228

225229
opal_timer_t opal_timer_base_get_freq(void)
226230
{
227-
return opal_timer_linux_freq;
231+
return opal_timer_linux_freq * 1000000;
228232
}
229233

230234

0 commit comments

Comments
 (0)