History

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.

Geert Uytterhoeven committed 8 years ago

Commit abcbcb81 parent 0fe27951 file · +4 −2Browse files

Unified · Split

kernel/time/time.c+4−2
@@ -28,6 +28,7 @@ · 27 unmodified lines
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) · 280 unmodified lines
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 }