Skip to content

Commit d25a025

Browse files
paulmckrcuKAGA-KOKO
authored andcommitted
clocksource: Make clocksource watchdog test safe for slow-HZ systems
The clocksource watchdog test sets a local JIFFIES_SHIFT macro and assumes that HZ is >= 100. For smaller HZ values this shift value is too large and causes undefined behaviour. Move the HZ-based definitions of JIFFIES_SHIFT from kernel/time/jiffies.c to kernel/time/tick-internal.h so the clocksource watchdog test can utilize them, which makes it work correctly with all HZ values. [ tglx: Resolved conflicts and massaged changelog ] Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/lkml/20210812000133.GA402890@paulmck-ThinkPad-P17-Gen-1/
1 parent 127c92f commit d25a025

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

kernel/time/clocksource-wdtest.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <linux/prandom.h>
2020
#include <linux/cpu.h>
2121

22+
#include "tick-internal.h"
23+
2224
MODULE_LICENSE("GPL");
2325
MODULE_AUTHOR("Paul E. McKenney <paulmck@kernel.org>");
2426

@@ -34,9 +36,6 @@ static u64 wdtest_jiffies_read(struct clocksource *cs)
3436
return (u64)jiffies;
3537
}
3638

37-
/* Assume HZ > 100. */
38-
#define JIFFIES_SHIFT 8
39-
4039
static struct clocksource clocksource_wdtest_jiffies = {
4140
.name = "wdtest-jiffies",
4241
.rating = 1, /* lowest valid rating*/

kernel/time/jiffies.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,9 @@
1010
#include <linux/init.h>
1111

1212
#include "timekeeping.h"
13+
#include "tick-internal.h"
1314

1415

15-
/* Since jiffies uses a simple TICK_NSEC multiplier
16-
* conversion, the .shift value could be zero. However
17-
* this would make NTP adjustments impossible as they are
18-
* in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
19-
* shift both the nominator and denominator the same
20-
* amount, and give ntp adjustments in units of 1/2^8
21-
*
22-
* The value 8 is somewhat carefully chosen, as anything
23-
* larger can result in overflows. TICK_NSEC grows as HZ
24-
* shrinks, so values greater than 8 overflow 32bits when
25-
* HZ=100.
26-
*/
27-
#if HZ < 34
28-
#define JIFFIES_SHIFT 6
29-
#elif HZ < 67
30-
#define JIFFIES_SHIFT 7
31-
#else
32-
#define JIFFIES_SHIFT 8
33-
#endif
34-
3516
static u64 jiffies_read(struct clocksource *cs)
3617
{
3718
return (u64) jiffies;

kernel/time/tick-internal.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,23 @@ void clock_was_set(unsigned int bases);
177177
void clock_was_set_delayed(void);
178178

179179
void hrtimers_resume_local(void);
180+
181+
/* Since jiffies uses a simple TICK_NSEC multiplier
182+
* conversion, the .shift value could be zero. However
183+
* this would make NTP adjustments impossible as they are
184+
* in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
185+
* shift both the nominator and denominator the same
186+
* amount, and give ntp adjustments in units of 1/2^8
187+
*
188+
* The value 8 is somewhat carefully chosen, as anything
189+
* larger can result in overflows. TICK_NSEC grows as HZ
190+
* shrinks, so values greater than 8 overflow 32bits when
191+
* HZ=100.
192+
*/
193+
#if HZ < 34
194+
#define JIFFIES_SHIFT 6
195+
#elif HZ < 67
196+
#define JIFFIES_SHIFT 7
197+
#else
198+
#define JIFFIES_SHIFT 8
199+
#endif

0 commit comments

Comments
 (0)