mirror of
https://github.com/torvalds/linux.git
synced 2024-12-19 17:41:29 +00:00
2e27e793e2
Currently, WATCHDOG_THRESHOLD is set to detect a 62.5-millisecond skew in a 500-millisecond WATCHDOG_INTERVAL. This requires that clocks be skewed by more than 12.5% in order to be marked unstable. Except that a clock that is skewed by that much is probably destroying unsuspecting software right and left. And given that there are now checks for false-positive skews due to delays between reading the two clocks, it should be possible to greatly decrease WATCHDOG_THRESHOLD, at least for fine-grained clocks such as TSC. Therefore, add a new uncertainty_margin field to the clocksource structure that contains the maximum uncertainty in nanoseconds for the corresponding clock. This field may be initialized manually, as it is for clocksource_tsc_early and clocksource_jiffies, which is copied to refined_jiffies. If the field is not initialized manually, it will be computed at clock-registry time as the period of the clock in question based on the scale and freq parameters to __clocksource_update_freq_scale() function. If either of those two parameters are zero, the tens-of-milliseconds WATCHDOG_THRESHOLD is used as a cowardly alternative to dividing by zero. No matter how the uncertainty_margin field is calculated, it is bounded below by twice WATCHDOG_MAX_SKEW, that is, by 100 microseconds. Note that manually initialized uncertainty_margin fields are not adjusted, but there is a WARN_ON_ONCE() that triggers if any such field is less than twice WATCHDOG_MAX_SKEW. This WARN_ON_ONCE() is intended to discourage production use of the one-nanosecond uncertainty_margin values that are used to test the clock-skew code itself. The actual clock-skew check uses the sum of the uncertainty_margin fields of the two clocksource structures being compared. Integer overflow is avoided because the largest computed value of the uncertainty_margin fields is one billion (10^9), and double that value fits into an unsigned int. However, if someone manually specifies (say) UINT_MAX, they will get what they deserve. Note that the refined_jiffies uncertainty_margin field is initialized to TICK_NSEC, which means that skew checks involving this clocksource will be sufficently forgiving. In a similar vein, the clocksource_tsc_early uncertainty_margin field is initialized to 32*NSEC_PER_MSEC, which replicates the current behavior and allows custom setting if needed in order to address the rare skews detected for this clocksource in current mainline. Suggested-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Feng Tang <feng.tang@intel.com> Link: https://lore.kernel.org/r/20210527190124.440372-4-paulmck@kernel.org
124 lines
3.2 KiB
C
124 lines
3.2 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* This file contains the jiffies based clocksource.
|
|
*
|
|
* Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
|
|
*/
|
|
#include <linux/clocksource.h>
|
|
#include <linux/jiffies.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
|
|
#include "timekeeping.h"
|
|
|
|
|
|
/* Since jiffies uses a simple TICK_NSEC multiplier
|
|
* conversion, the .shift value could be zero. However
|
|
* this would make NTP adjustments impossible as they are
|
|
* in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
|
|
* shift both the nominator and denominator the same
|
|
* amount, and give ntp adjustments in units of 1/2^8
|
|
*
|
|
* The value 8 is somewhat carefully chosen, as anything
|
|
* larger can result in overflows. TICK_NSEC grows as HZ
|
|
* shrinks, so values greater than 8 overflow 32bits when
|
|
* HZ=100.
|
|
*/
|
|
#if HZ < 34
|
|
#define JIFFIES_SHIFT 6
|
|
#elif HZ < 67
|
|
#define JIFFIES_SHIFT 7
|
|
#else
|
|
#define JIFFIES_SHIFT 8
|
|
#endif
|
|
|
|
static u64 jiffies_read(struct clocksource *cs)
|
|
{
|
|
return (u64) jiffies;
|
|
}
|
|
|
|
/*
|
|
* The Jiffies based clocksource is the lowest common
|
|
* denominator clock source which should function on
|
|
* all systems. It has the same coarse resolution as
|
|
* the timer interrupt frequency HZ and it suffers
|
|
* inaccuracies caused by missed or lost timer
|
|
* interrupts and the inability for the timer
|
|
* interrupt hardware to accurately tick at the
|
|
* requested HZ value. It is also not recommended
|
|
* for "tick-less" systems.
|
|
*/
|
|
static struct clocksource clocksource_jiffies = {
|
|
.name = "jiffies",
|
|
.rating = 1, /* lowest valid rating*/
|
|
.uncertainty_margin = 32 * NSEC_PER_MSEC,
|
|
.read = jiffies_read,
|
|
.mask = CLOCKSOURCE_MASK(32),
|
|
.mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */
|
|
.shift = JIFFIES_SHIFT,
|
|
.max_cycles = 10,
|
|
};
|
|
|
|
__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
|
|
__cacheline_aligned_in_smp seqcount_raw_spinlock_t jiffies_seq =
|
|
SEQCNT_RAW_SPINLOCK_ZERO(jiffies_seq, &jiffies_lock);
|
|
|
|
#if (BITS_PER_LONG < 64)
|
|
u64 get_jiffies_64(void)
|
|
{
|
|
unsigned int seq;
|
|
u64 ret;
|
|
|
|
do {
|
|
seq = read_seqcount_begin(&jiffies_seq);
|
|
ret = jiffies_64;
|
|
} while (read_seqcount_retry(&jiffies_seq, seq));
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL(get_jiffies_64);
|
|
#endif
|
|
|
|
EXPORT_SYMBOL(jiffies);
|
|
|
|
static int __init init_jiffies_clocksource(void)
|
|
{
|
|
return __clocksource_register(&clocksource_jiffies);
|
|
}
|
|
|
|
core_initcall(init_jiffies_clocksource);
|
|
|
|
struct clocksource * __init __weak clocksource_default_clock(void)
|
|
{
|
|
return &clocksource_jiffies;
|
|
}
|
|
|
|
static struct clocksource refined_jiffies;
|
|
|
|
int register_refined_jiffies(long cycles_per_second)
|
|
{
|
|
u64 nsec_per_tick, shift_hz;
|
|
long cycles_per_tick;
|
|
|
|
|
|
|
|
refined_jiffies = clocksource_jiffies;
|
|
refined_jiffies.name = "refined-jiffies";
|
|
refined_jiffies.rating++;
|
|
|
|
/* Calc cycles per tick */
|
|
cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
|
|
/* shift_hz stores hz<<8 for extra accuracy */
|
|
shift_hz = (u64)cycles_per_second << 8;
|
|
shift_hz += cycles_per_tick/2;
|
|
do_div(shift_hz, cycles_per_tick);
|
|
/* Calculate nsec_per_tick using shift_hz */
|
|
nsec_per_tick = (u64)NSEC_PER_SEC << 8;
|
|
nsec_per_tick += (u32)shift_hz/2;
|
|
do_div(nsec_per_tick, (u32)shift_hz);
|
|
|
|
refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
|
|
|
|
__clocksource_register(&refined_jiffies);
|
|
return 0;
|
|
}
|