time: Make sure jiffies_to_msecs() preserves non-zero time periods
Subsystems (3)
PATCH v2 1/1 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, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.
---
v2:
- Add examples of affected systems,
- Use DIV_ROUND_UP().
--- kernel/time/time.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/time.c +4 -2
@@ -28,6 +28,7 @@*/ #include <linux/export.h> +#include <linux/kernel.h> #include <linux/timex.h> #include <linux/capability.h> #include <linux/timekeeper_internal.h>
@@ -314,9 +315,10 @@ unsigned int jiffies_to_msecs(const unsigned long j)return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else # if BITS_PER_LONG ==32 -return (HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +return (HZ_TO_MSEC_MUL32 * j + (1ULL <<HZ_TO_MSEC_SHR32 ) -1 ) >> +HZ_TO_MSEC_SHR32 ;# else -return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +return DIV_ROUND_UP (j *HZ_TO_MSEC_NUM ,HZ_TO_MSEC_DEN );# endif #endif }
Signature · 1 line
2.17.1
CC alpha, mips
Quoted 49 lines · Geert Uytterhoeven
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, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.Signed-off-by Geert Uytterhoeven <[email protected]>---
v2:
- Add examples of affected systems,
- UseDIV_ROUND_UP().--- kernel/time/time.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/time/time.c +4
-3@@ -28,6 +28,7 @@*/ #include <linux/export.h> +#include <linux/kernel.h> #include <linux/timex.h> #include <linux/capability.h> #include <linux/timekeeper_internal.h> @@ -314,9 +315,10 @@ unsigned int jiffies_to_msecs(const unsigned long j)return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else # if BITS_PER_LONG ==32 -return (HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +return (HZ_TO_MSEC_MUL32 * j + (1ULL <<HZ_TO_MSEC_SHR32 ) -1 ) >> +HZ_TO_MSEC_SHR32 ;# else -return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +return DIV_ROUND_UP (j *HZ_TO_MSEC_NUM ,HZ_TO_MSEC_DEN );# endif #endif } --2.17.1
Quoted 9 lines · Geert Uytterhoeven
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.
Quoted 6 lines
jiffies_to_usecs()does not need such a fix, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.Signed-off-by Geert Uytterhoeven <[email protected]>---
Looks good to me,
Quoted 4 lines · Geert Uytterhoeven
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.
Quoted 8 lines
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, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.
Sorry, I'm not understanding that sentence at all.
Quoted 1 line
Signed-off-by Geert Uytterhoeven <[email protected]>
This lacks a stable tag, right?
Quoted 37 lines
---
v2:
- Add examples of affected systems,
- UseDIV_ROUND_UP().--- kernel/time/time.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)kernel/time/time.c +4
-2@@ -28,6 +28,7 @@*/ #include <linux/export.h> +#include <linux/kernel.h> #include <linux/timex.h> #include <linux/capability.h> #include <linux/timekeeper_internal.h> @@ -314,9 +315,10 @@ unsigned int jiffies_to_msecs(const unsigned long j)return (j + (HZ /MSEC_PER_SEC ) -1 )/(HZ /MSEC_PER_SEC );#else # if BITS_PER_LONG ==32 -return (HZ_TO_MSEC_MUL32 * j) >>HZ_TO_MSEC_SHR32 ; +return (HZ_TO_MSEC_MUL32 * j + (1ULL <<HZ_TO_MSEC_SHR32 ) -1 ) >> +HZ_TO_MSEC_SHR32 ;# else -return (j *HZ_TO_MSEC_NUM ) /HZ_TO_MSEC_DEN ; +return DIV_ROUND_UP (j *HZ_TO_MSEC_NUM ,HZ_TO_MSEC_DEN );# endif #endif }Signature · 1 line
2.17.1
Hi Thomas,
Quoted 10 lines · Thomas Gleixner
Quoted 9 lines · Geert Uytterhoeven
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.
Quoted 5 lines
Quoted 3 lines
jiffies_to_usecs()does not need such a fix, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.Sorry, I'm not understanding that sentence at all.
Sorry for being unclear.
1 jiffy can only be less than 1µs if HZ > 1000000.
But include/linux/jiffies.h checks if HZ >= 12288, and does #error otherwise. In addition, there's a "BUILD_BUG_ON(HZ > USEC_PER_SEC)" in time.c
Quoted 3 lines
Quoted 1 line
Signed-off-by Geert Uytterhoeven <[email protected]>This lacks a stable tag, right?
Up to the maintainer to add, isn't it?
Thanks!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 15 lines · Geert Uytterhoeven
Quoted 14 lines · Thomas Gleixner
Quoted 12 lines · Geert Uytterhoeven
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, as <linux/jiffies.h> does not support values of HZ larger than 12287, thus rejecting any problematic huge values of HZ.
Quoted 7 lines
Quoted 1 line
Sorry, I'm not understanding that sentence at all.
Sorry for being unclear.
1 jiffy can only be less than 1µs if HZ > 1000000.
Butinclude/linux/jiffies.hchecks if HZ >= 12288, and does #error otherwise. In addition, there's a "BUILD_BUG_ON(HZ > USEC_PER_SEC)" intime.c
Hmm, ok. Care to reword?
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Geert Uytterhoeven <[email protected]>This lacks a stable tag, right?
Up to the maintainer to add, isn't it?
Yes and no. At least a hint that this has been broken by commit X or has been broken forever would be appreciated.
Thanks,
tglx