Skip to content

Commit abcbcb8

Browse files
geertuKAGA-KOKO
authored andcommitted
time: Make sure jiffies_to_msecs() preserves non-zero time periods
For the common cases where 1000 is a multiple of HZ, or HZ is a multiple of 1000, jiffies_to_msecs() never returns zero when passed a non-zero time period. However, if HZ > 1000 and not an integer multiple of 1000 (e.g. 1024 or 1200, as used on alpha and DECstation), jiffies_to_msecs() may return zero for small non-zero time periods. This may break code that relies on receiving back a non-zero value. jiffies_to_usecs() does not need such a fix: one jiffy can only be less than one µs if HZ > 1000000, and such large values of HZ are already rejected at build time, twice: - include/linux/jiffies.h does #error if HZ >= 12288, - kernel/time/time.c has BUILD_BUG_ON(HZ > USEC_PER_SEC). Broken since forever. Signed-off-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Arnd Bergmann <[email protected]> Cc: John Stultz <[email protected]> Cc: Stephen Boyd <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 0fe2795 commit abcbcb8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

kernel/time/time.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929

3030
#include <linux/export.h>
31+
#include <linux/kernel.h>
3132
#include <linux/timex.h>
3233
#include <linux/capability.h>
3334
#include <linux/timekeeper_internal.h>
@@ -314,9 +315,10 @@ unsigned int jiffies_to_msecs(const unsigned long j)
314315
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
315316
#else
316317
# if BITS_PER_LONG == 32
317-
return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
318+
return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >>
319+
HZ_TO_MSEC_SHR32;
318320
# else
319-
return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
321+
return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
320322
# endif
321323
#endif
322324
}

0 commit comments

Comments
 (0)