TSC and clocksource-watchdog updates for v6.12
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): 463c0174-356b-4918-8759-c36b1b30ba55@paulmck-laptop
v2 cover TSC and clocksource-watchdog updates for v6.12
Hello!
This series contains TSC and clocksource-watchdog updates:
1. clocksource: Improve comments for watchdog skew bounds, courtesy
of Borislav Petkov.
2. clocksource: Fix brown-bag boolean thinko in cs_watchdog_read().
3. clocksource: Fix comments on WATCHDOG_THRESHOLD &
WATCHDOG_MAX_SKEW.
4. clocksource: Set cs_watchdog_read() checks based on
.uncertainty_margin.
5. Check for sockets instead of CPUs to make code match comment.
Thanx, Paul
Changes since v1:
o Remember to CC LKML. :-/
------------------------------------------------------------------------
b/arch/x86/kernel/tsc.c | 2 +- b/kernel/time/clocksource.c | 28 ++++++++++++++++++++-------- kernel/time/clocksource.c | 19 +++++++++++++------ 3 files changed, 34 insertions(+), 15 deletions(-)
PATCH v2 1/5 clocksource: Improve comments for watchdog skew bounds
From: Borislav Petkov <[email protected]>
Add more detail on the rationale for bounding the clocksource ->uncertainty_margin below at about 500ppm.
--- kernel/time/clocksource.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
kernel/time/clocksource.c +20 -8
@@ -125,6 +125,13 @@ static u64 suspend_start;* * The default of 500 parts per million is based on NTP's limits. * If a clocksource is good enough for NTP, it is good enough for us! +* +* In other words, by default, even if a clocksource is extremely +* precise (for example, with a sub-nanosecond period), the maximum +* permissible skew between the clocksource watchdog and the clocksource +* under test is not permitted to go below the 500ppm minimum defined +* by MAX_SKEW_USEC. This 500ppm minimum may be overridden using the +* CLOCKSOURCE_WATCHDOG_MAX_SKEW_US Kconfig option. */ #ifdef CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US #define MAX_SKEW_USEC CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
@@ -1146,14 +1153,19 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq }/* -* If the uncertainty margin is not specified, calculate it. -* If both scale and freq are non-zero, calculate the clock -* period, but bound below at 2*WATCHDOG_MAX_SKEW. However, -* if either of scale or freq is zero, be very conservative and -* take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the -* uncertainty margin. Allow stupidly small uncertainty margins -* to be specified by the caller for testing purposes, but warn -* to discourage production use of this capability. +* If the uncertainty margin is not specified, calculate it. If +* both scale and freq are non-zero, calculate the clock period, but +* bound below at 2*WATCHDOG_MAX_SKEW, that is, 500ppm by default. +* However, if either of scale or freq is zero, be very conservative +* and take the tens-of-milliseconds WATCHDOG_THRESHOLD value +* for the uncertainty margin. Allow stupidly small uncertainty +* margins to be specified by the caller for testing purposes, +* but warn to discourage production use of this capability. +* +* Bottom line: The sum of the uncertainty margins of the +* watchdog clocksource and the clocksource under test will be at +* least 500ppm by default. For more information, please see the +* comment preceding CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US above. */ if (scale && freq && !cs->uncertainty_margin) { cs->uncertainty_margin =NSEC_PER_SEC / (scale * freq);
Signature · 1 line
2.40.1
[tip: timers/clocksource] clocksource: Improve comments for watchdog skew bounds
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: 17915131ae4660658aa779f89e9f444319861561 Gitweb: https://git.kernel.org/tip/17915131ae4660658aa779f89e9f444319861561 Author: Borislav Petkov <[email protected]> AuthorDate: Fri, 02 Aug 2024 08:46:14 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 02 Aug 2024 18:37:13 +02:00
clocksource: Improve comments for watchdog skew bounds
Add more detail on the rationale for bounding the clocksource ->uncertainty_margin below at about 500ppm.
--- kernel/time/clocksource.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
kernel/time/clocksource.c +20 -8
@@ -125,6 +125,13 @@ static u64 suspend_start;* * The default of 500 parts per million is based on NTP's limits. * If a clocksource is good enough for NTP, it is good enough for us! +* +* In other words, by default, even if a clocksource is extremely +* precise (for example, with a sub-nanosecond period), the maximum +* permissible skew between the clocksource watchdog and the clocksource +* under test is not permitted to go below the 500ppm minimum defined +* by MAX_SKEW_USEC. This 500ppm minimum may be overridden using the +* CLOCKSOURCE_WATCHDOG_MAX_SKEW_US Kconfig option. */ #ifdef CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US #define MAX_SKEW_USEC CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
@@ -1146,14 +1153,19 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq }/* -* If the uncertainty margin is not specified, calculate it. -* If both scale and freq are non-zero, calculate the clock -* period, but bound below at 2*WATCHDOG_MAX_SKEW. However, -* if either of scale or freq is zero, be very conservative and -* take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the -* uncertainty margin. Allow stupidly small uncertainty margins -* to be specified by the caller for testing purposes, but warn -* to discourage production use of this capability. +* If the uncertainty margin is not specified, calculate it. If +* both scale and freq are non-zero, calculate the clock period, but +* bound below at 2*WATCHDOG_MAX_SKEW, that is, 500ppm by default. +* However, if either of scale or freq is zero, be very conservative +* and take the tens-of-milliseconds WATCHDOG_THRESHOLD value +* for the uncertainty margin. Allow stupidly small uncertainty +* margins to be specified by the caller for testing purposes, +* but warn to discourage production use of this capability. +* +* Bottom line: The sum of the uncertainty margins of the +* watchdog clocksource and the clocksource under test will be at +* least 500ppm by default. For more information, please see the +* comment preceding CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US above. */ if (scale && freq && !cs->uncertainty_margin) { cs->uncertainty_margin =NSEC_PER_SEC / (scale * freq);
PATCH v2 2/5 clocksource: Fix brown-bag boolean thinko in cs_watchdog_read()
In reply to Paul E. McKenneyThe current "nretries > 1 || nretries >= max_retries" check in cs_watchdog_read() will always evaluate to true, and thus pr_warn(), if nretries is greater than 1. The intent is instead to never warn on the first try, but otherwise warn if the successful retry was the last retry.
Therefore, change that "||" to "&&".
--- kernel/time/clocksource.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/clocksource.c +1 -1
@@ -253,7 +253,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, wd_delay =cycles_to_nsec_safe (watchdog, *wdnow, wd_end);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" ,smp_processor_id (), watchdog->name, nretries); }
Signature · 1 line
2.40.1
[tip: timers/urgent] clocksource: Fix brown-bag boolean thinko in cs_watchdog_read()
The following commit has been merged into the timers/urgent branch of tip:
The following commit has been merged into the timers/urgent branch of tip:
Commit-ID: f2655ac2c06a15558e51ed6529de280e1553c86e Gitweb: https://git.kernel.org/tip/f2655ac2c06a15558e51ed6529de280e1553c86e Author: Paul E. McKenney <[email protected]> AuthorDate: Fri, 02 Aug 2024 08:46:15 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 02 Aug 2024 18:29:28 +02:00
clocksource: Fix brown-bag boolean thinko in cs_watchdog_read()
The current "nretries > 1 || nretries >= max_retries" check in cs_watchdog_read() will always evaluate to true, and thus pr_warn(), if nretries is greater than 1. The intent is instead to never warn on the first try, but otherwise warn if the successful retry was the last retry.
Therefore, change that "||" to "&&".
--- kernel/time/clocksource.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/clocksource.c +1 -1
@@ -246,7 +246,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, wd_delay =cycles_to_nsec_safe (watchdog, *wdnow, wd_end);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" ,smp_processor_id (), watchdog->name, nretries); }
PATCH v2 3/5 clocksource: Fix comments on WATCHDOG_THRESHOLD & WATCHDOG_MAX_SKEW
In reply to Paul E. McKenneyThe WATCHDOG_THRESHOLD macro is no longer used to supply a default value for ->uncertainty_margin, but WATCHDOG_MAX_SKEW now is. Therefore, update the comments to reflect this change.
--- kernel/time/clocksource.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/time/clocksource.c +7 -1
@@ -113,7 +113,6 @@ static u64 suspend_start;/* * Threshold: 0.0312s, when doubled: 0.0625s. -* Also a default for cs->uncertainty_margin when registering clocks. */ #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
@@ -139,6 +138,13 @@ static u64 suspend_start;#define MAX_SKEW_USEC (125 * WATCHDOG_INTERVAL / HZ)#endif +/* +* Default for maximum permissible skew when cs->uncertainty_margin is +* not specified, and the lower bound even when cs->uncertainty_margin +* is specified. This is also the default that is used when registering +* clocks with unspecifed cs->uncertainty_margin, so this macro is used +* even in CONFIG_CLOCKSOURCE_WATCHDOG=n kernels. +*/ #define WATCHDOG_MAX_SKEW (MAX_SKEW_USEC * NSEC_PER_USEC)#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Signature · 1 line
2.40.1
[tip: timers/clocksource] clocksource: Fix comments on WATCHDOG_THRESHOLD & WATCHDOG_MAX_SKEW
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: f33a5d4bd9c2e545857b2cf7481eb721bcab867c Gitweb: https://git.kernel.org/tip/f33a5d4bd9c2e545857b2cf7481eb721bcab867c Author: Paul E. McKenney <[email protected]> AuthorDate: Fri, 02 Aug 2024 08:46:16 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 02 Aug 2024 18:37:13 +02:00
clocksource: Fix comments on WATCHDOG_THRESHOLD & WATCHDOG_MAX_SKEW
The WATCHDOG_THRESHOLD macro is no longer used to supply a default value for ->uncertainty_margin, but WATCHDOG_MAX_SKEW now is.
Therefore, update the comments to reflect this change.
--- kernel/time/clocksource.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/time/clocksource.c +7 -1
@@ -113,7 +113,6 @@ static u64 suspend_start;/* * Threshold: 0.0312s, when doubled: 0.0625s. -* Also a default for cs->uncertainty_margin when registering clocks. */ #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
@@ -139,6 +138,13 @@ static u64 suspend_start;#define MAX_SKEW_USEC (125 * WATCHDOG_INTERVAL / HZ)#endif +/* +* Default for maximum permissible skew when cs->uncertainty_margin is +* not specified, and the lower bound even when cs->uncertainty_margin +* is specified. This is also the default that is used when registering +* clocks with unspecifed cs->uncertainty_margin, so this macro is used +* even in CONFIG_CLOCKSOURCE_WATCHDOG=n kernels. +*/ #define WATCHDOG_MAX_SKEW (MAX_SKEW_USEC * NSEC_PER_USEC)#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
PATCH v2 4/5 clocksource: Set cs_watchdog_read() checks based on .uncertainty_margin
In reply to Paul E. McKenneyRight now, cs_watchdog_read() does clocksource sanity checks based on WATCHDOG_MAX_SKEW, which sets a floor on any clocksource's .uncertainty_margin. These sanity checks can therefore act inappropriately for clocksources with large uncertainty margins.
One reason for a clocksource to have a large .uncertainty_margin is when that clocksource has long read-out latency, given that it does not make sense for the .uncertainty_margin to be smaller than the read-out latency. With the current checks, cs_watchdog_read() could reject all normal reads from a clocksource with long read-out latencies, such as those from legacy clocksources that are no longer implemented in hardware.
Therefore, recast the cs_watchdog_read() checks in terms of the .uncertainty_margin values of the clocksources involved in the timespan in question. The first covers two watchdog reads and one cs read, so use twice the watchdog .uncertainty_margin plus that of the cs. The second covers only a pair of watchdog reads, so use twice the watchdog .uncertainty_margin.
--- kernel/time/clocksource.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/time/clocksource.c +5 -4
@@ -244,6 +244,7 @@ enum wd_read_status {static enum wd_read_status cs_watchdog_read (struct clocksource *cs,u64 *csnow,u64 *wdnow) { +int64_t md =2 * watchdog->uncertainty_margin;unsigned int nretries, max_retries;int64_t wd_delay, wd_seq_delay;u64 wd_end, wd_end2;
@@ -258,7 +259,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,local_irq_enable (); wd_delay =cycles_to_nsec_safe (watchdog, *wdnow, wd_end); -if (wd_delay <=WATCHDOG_MAX_SKEW ) { +if (wd_delay <= md + cs->uncertainty_margin) {if (nretries >1 && nretries >= max_retries) {pr_warn ("timekeeping watchdog on CPU%d: %s retried %d times before success\n" ,smp_processor_id (), watchdog->name, nretries);
@@ -271,12 +272,12 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,* there is too much external interferences that cause * significant delay in reading both clocksource and watchdog. * -* If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2, -* report system busy, reinit the watchdog and skip the current +* If consecutive WD read-back delay > md, report +* system busy, reinit the watchdog and skip the current * watchdog test. */ wd_seq_delay =cycles_to_nsec_safe (watchdog, wd_end, wd_end2); -if (wd_seq_delay >WATCHDOG_MAX_SKEW /2 ) +if (wd_seq_delay > md) goto skip_test; }
Signature · 1 line
2.40.1
[tip: timers/clocksource] clocksource: Set cs_watchdog_read() checks based on .uncertainty_margin
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: 4ac1dd3245b9067f929ab30141bb0475e9e32fc5 Gitweb: https://git.kernel.org/tip/4ac1dd3245b9067f929ab30141bb0475e9e32fc5 Author: Paul E. McKenney <[email protected]> AuthorDate: Fri, 02 Aug 2024 08:46:17 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 02 Aug 2024 18:37:13 +02:00
clocksource: Set cs_watchdog_read() checks based on .uncertainty_margin
Right now, cs_watchdog_read() does clocksource sanity checks based on WATCHDOG_MAX_SKEW, which sets a floor on any clocksource's .uncertainty_margin. These sanity checks can therefore act inappropriately for clocksources with large uncertainty margins.
One reason for a clocksource to have a large .uncertainty_margin is when that clocksource has long read-out latency, given that it does not make sense for the .uncertainty_margin to be smaller than the read-out latency. With the current checks, cs_watchdog_read() could reject all normal reads from a clocksource with long read-out latencies, such as those from legacy clocksources that are no longer implemented in hardware.
Therefore, recast the cs_watchdog_read() checks in terms of the .uncertainty_margin values of the clocksources involved in the timespan in question. The first covers two watchdog reads and one cs read, so use twice the watchdog .uncertainty_margin plus that of the cs. The second covers only a pair of watchdog reads, so use twice the watchdog .uncertainty_margin.
--- kernel/time/clocksource.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/time/clocksource.c +5 -4
@@ -244,6 +244,7 @@ enum wd_read_status {static enum wd_read_status cs_watchdog_read (struct clocksource *cs,u64 *csnow,u64 *wdnow) { +int64_t md =2 * watchdog->uncertainty_margin;unsigned int nretries, max_retries;int64_t wd_delay, wd_seq_delay;u64 wd_end, wd_end2;
@@ -258,7 +259,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,local_irq_enable (); wd_delay =cycles_to_nsec_safe (watchdog, *wdnow, wd_end); -if (wd_delay <=WATCHDOG_MAX_SKEW ) { +if (wd_delay <= md + cs->uncertainty_margin) {if (nretries >1 && nretries >= max_retries) {pr_warn ("timekeeping watchdog on CPU%d: %s retried %d times before success\n" ,smp_processor_id (), watchdog->name, nretries);
@@ -271,12 +272,12 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,* there is too much external interferences that cause * significant delay in reading both clocksource and watchdog. * -* If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2, -* report system busy, reinit the watchdog and skip the current +* If consecutive WD read-back delay > md, report +* system busy, reinit the watchdog and skip the current * watchdog test. */ wd_seq_delay =cycles_to_nsec_safe (watchdog, wd_end, wd_end2); -if (wd_seq_delay >WATCHDOG_MAX_SKEW /2 ) +if (wd_seq_delay > md) goto skip_test; }
PATCH v2 5/5 tsc: Check for sockets instead of CPUs to make code match comment
In reply to Paul E. McKenneyThe unsynchronized_tsc() eventually checks num_possible_cpus(), and if the system is non-Intel and the number of possible CPUs is greater than one, assumes that TSCs are unsynchronized. This despite the comment saying "assume multi socket systems are not synchronized", that is, socket rather than CPU. This behavior was preserved by commit 8fbbc4b45ce3 ("x86: merge tsc_init and clocksource code") and by the previous relevant commit 7e69f2b1ead2 ("clocksource: Remove the update callback").
The clocksource drivers were added by commit 5d0cf410e94b ("Time: i386 Clocksource Drivers") back in 2006, and the comment still said "socket" rather than "CPU".
Therefore, bravely (and perhaps foolishly) make the code match the comment.
Note that it is possible to bypass both code and comment by booting with tsc=reliable, but this also disables the clocksource watchdog, which is undesirable when trust in the TSC is strictly limited.
[ paulmck: Switch from nr_online_nodes to topology_max_packages() per Feng Tang feedback. ]
--- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/tsc.c +1 -1
@@ -1290,7 +1290,7 @@ int unsynchronized_tsc(void)*/ if (boot_cpu_data.x86_vendor !=X86_VENDOR_INTEL ) {/* assume multi socket systems are not synchronized: */ -if (num_possible_cpus () >1 ) +if (topology_max_packages () >1 )return 1 ; }
Signature · 1 line
2.40.1
[tip: x86/timers] x86/tsc: Check for sockets instead of CPUs to make code match comment
The following commit has been merged into the x86/timers branch of tip:
The following commit has been merged into the x86/timers branch of tip:
Commit-ID: e7ff4ebffe3bedf55560ef861d80f6500ff0d76f Gitweb: https://git.kernel.org/tip/e7ff4ebffe3bedf55560ef861d80f6500ff0d76f Author: Paul E. McKenney <[email protected]> AuthorDate: Fri, 02 Aug 2024 08:46:18 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 02 Aug 2024 18:38:07 +02:00
x86/tsc: Check for sockets instead of CPUs to make code match comment
The unsynchronized_tsc() eventually checks num_possible_cpus(), and if the system is non-Intel and the number of possible CPUs is greater than one, assumes that TSCs are unsynchronized. This despite the comment saying "assume multi socket systems are not synchronized", that is, socket rather than CPU. This behavior was preserved by commit 8fbbc4b45ce3 ("x86: merge tsc_init and clocksource code") and by the previous relevant commit 7e69f2b1ead2 ("clocksource: Remove the update callback").
The clocksource drivers were added by commit 5d0cf410e94b ("Time: i386 Clocksource Drivers") back in 2006, and the comment still said "socket" rather than "CPU".
Therefore, bravely (and perhaps foolishly) make the code match the comment.
Note that it is possible to bypass both code and comment by booting with tsc=reliable, but this also disables the clocksource watchdog, which is undesirable when trust in the TSC is strictly limited.
--- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/tsc.c +1 -1
@@ -1288,7 +1288,7 @@ int unsynchronized_tsc(void)*/ if (boot_cpu_data.x86_vendor !=X86_VENDOR_INTEL ) {/* assume multi socket systems are not synchronized: */ -if (num_possible_cpus () >1 ) +if (topology_max_packages () >1 )return 1 ; }