Commit d0304569 authored by Adrian Hunter's avatar Adrian Hunter Committed by Thomas Gleixner

clocksource: Make watchdog and suspend-timing multiplication overflow safe

Kernel timekeeping is designed to keep the change in cycles (since the last
timer interrupt) below max_cycles, which prevents multiplication overflow
when converting cycles to nanoseconds. However, if timer interrupts stop,
the clocksource_cyc2ns() calculation will eventually overflow.

Add protection against that. Simplify by folding together
clocksource_delta() and clocksource_cyc2ns() into cycles_to_nsec_safe().
Check against max_cycles, falling back to a slower higher precision
calculation.
Suggested-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20240325064023.2997-20-adrian.hunter@intel.com
parent 135225a3
...@@ -20,6 +20,16 @@ ...@@ -20,6 +20,16 @@
#include "tick-internal.h" #include "tick-internal.h"
#include "timekeeping_internal.h" #include "timekeeping_internal.h"
static noinline u64 cycles_to_nsec_safe(struct clocksource *cs, u64 start, u64 end)
{
u64 delta = clocksource_delta(end, start, cs->mask);
if (likely(delta < cs->max_cycles))
return clocksource_cyc2ns(delta, cs->mult, cs->shift);
return mul_u64_u32_shr(delta, cs->mult, cs->shift);
}
/** /**
* clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
* @mult: pointer to mult variable * @mult: pointer to mult variable
...@@ -222,8 +232,8 @@ enum wd_read_status { ...@@ -222,8 +232,8 @@ enum wd_read_status {
static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow) static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
{ {
unsigned int nretries, max_retries; unsigned int nretries, max_retries;
u64 wd_end, wd_end2, wd_delta;
int64_t wd_delay, wd_seq_delay; int64_t wd_delay, wd_seq_delay;
u64 wd_end, wd_end2;
max_retries = clocksource_get_max_watchdog_retry(); max_retries = clocksource_get_max_watchdog_retry();
for (nretries = 0; nretries <= max_retries; nretries++) { for (nretries = 0; nretries <= max_retries; nretries++) {
...@@ -234,9 +244,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, ...@@ -234,9 +244,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
wd_end2 = watchdog->read(watchdog); wd_end2 = watchdog->read(watchdog);
local_irq_enable(); local_irq_enable();
wd_delta = clocksource_delta(wd_end, *wdnow, watchdog->mask); wd_delay = cycles_to_nsec_safe(watchdog, *wdnow, wd_end);
wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
watchdog->shift);
if (wd_delay <= WATCHDOG_MAX_SKEW) { if (wd_delay <= WATCHDOG_MAX_SKEW) {
if (nretries > 1 || nretries >= max_retries) { if (nretries > 1 || nretries >= max_retries) {
pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n", pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
...@@ -254,8 +262,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, ...@@ -254,8 +262,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
* report system busy, reinit the watchdog and skip the current * report system busy, reinit the watchdog and skip the current
* watchdog test. * watchdog test.
*/ */
wd_delta = clocksource_delta(wd_end2, wd_end, watchdog->mask); wd_seq_delay = cycles_to_nsec_safe(watchdog, wd_end, wd_end2);
wd_seq_delay = clocksource_cyc2ns(wd_delta, watchdog->mult, watchdog->shift);
if (wd_seq_delay > WATCHDOG_MAX_SKEW/2) if (wd_seq_delay > WATCHDOG_MAX_SKEW/2)
goto skip_test; goto skip_test;
} }
...@@ -366,8 +373,7 @@ void clocksource_verify_percpu(struct clocksource *cs) ...@@ -366,8 +373,7 @@ void clocksource_verify_percpu(struct clocksource *cs)
delta = (csnow_end - csnow_mid) & cs->mask; delta = (csnow_end - csnow_mid) & cs->mask;
if (delta < 0) if (delta < 0)
cpumask_set_cpu(cpu, &cpus_ahead); cpumask_set_cpu(cpu, &cpus_ahead);
delta = clocksource_delta(csnow_end, csnow_begin, cs->mask); cs_nsec = cycles_to_nsec_safe(cs, csnow_begin, csnow_end);
cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
if (cs_nsec > cs_nsec_max) if (cs_nsec > cs_nsec_max)
cs_nsec_max = cs_nsec; cs_nsec_max = cs_nsec;
if (cs_nsec < cs_nsec_min) if (cs_nsec < cs_nsec_min)
...@@ -398,8 +404,8 @@ static inline void clocksource_reset_watchdog(void) ...@@ -398,8 +404,8 @@ static inline void clocksource_reset_watchdog(void)
static void clocksource_watchdog(struct timer_list *unused) static void clocksource_watchdog(struct timer_list *unused)
{ {
u64 csnow, wdnow, cslast, wdlast, delta;
int64_t wd_nsec, cs_nsec, interval; int64_t wd_nsec, cs_nsec, interval;
u64 csnow, wdnow, cslast, wdlast;
int next_cpu, reset_pending; int next_cpu, reset_pending;
struct clocksource *cs; struct clocksource *cs;
enum wd_read_status read_ret; enum wd_read_status read_ret;
...@@ -456,12 +462,8 @@ static void clocksource_watchdog(struct timer_list *unused) ...@@ -456,12 +462,8 @@ static void clocksource_watchdog(struct timer_list *unused)
continue; continue;
} }
delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask); wd_nsec = cycles_to_nsec_safe(watchdog, cs->wd_last, wdnow);
wd_nsec = clocksource_cyc2ns(delta, watchdog->mult, cs_nsec = cycles_to_nsec_safe(cs, cs->cs_last, csnow);
watchdog->shift);
delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
wdlast = cs->wd_last; /* save these in case we print them */ wdlast = cs->wd_last; /* save these in case we print them */
cslast = cs->cs_last; cslast = cs->cs_last;
cs->cs_last = csnow; cs->cs_last = csnow;
...@@ -832,7 +834,7 @@ void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles) ...@@ -832,7 +834,7 @@ void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
*/ */
u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now) u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
{ {
u64 now, delta, nsec = 0; u64 now, nsec = 0;
if (!suspend_clocksource) if (!suspend_clocksource)
return 0; return 0;
...@@ -847,12 +849,8 @@ u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now) ...@@ -847,12 +849,8 @@ u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
else else
now = suspend_clocksource->read(suspend_clocksource); now = suspend_clocksource->read(suspend_clocksource);
if (now > suspend_start) { if (now > suspend_start)
delta = clocksource_delta(now, suspend_start, nsec = cycles_to_nsec_safe(suspend_clocksource, suspend_start, now);
suspend_clocksource->mask);
nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
suspend_clocksource->shift);
}
/* /*
* Disable the suspend timer to save power if current clocksource is * Disable the suspend timer to save power if current clocksource is
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment