Skip to contentSkip to conversation

clocksource: Scale the max retry number of watchdog read according to CPU numbers

PATCH Superseded Feng Tang 3 participants 6 messages Jan 26, 2024 – Jan 28, 2024 Timekeeping, clocksource core, NTP, alarmtimer

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH 1/1 clocksource: Scale the max retry number of watchdog read according to CPU numbers

There was a bug on one 8-socket server that the TSC is wrongly marked as 'unstable' and disabled during boot time. (reproduce rate is every 120 rounds of reboot tests), with log:

clocksource: timekeeping watchdog on CPU227: wd-tsc-wd excessive read-back delay of 153560ns vs. limit of 125000ns, wd-wd read-back delay only 11440ns, attempt 3, marking tsc unstable
tsc: Marking TSC unstable due to clocksource watchdog
TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
sched_clock: Marking unstable (119294969739, 159204297)<-(125446229205, -5992055152)
clocksource: Checking clocksource tsc synchronization from CPU 319 to CPUs 0,99,136,180,210,542,601,896.
clocksource: Switched to clocksource hpet

The reason is for platform with lots of CPU, there are sporadic big or huge read latency of read watchog/clocksource during boot or when system is under stress work load, and the frequency and maximum value of the latency goes up with the increasing of CPU numbers. Current code already has logic to detect and filter such high latency case by reading 3 times of watchdog, and check the 2 deltas. Due to the randomness of the latency, there is a low possibility situation that the first delta (latency) is big, but the second delta is small and looks valid, which can escape from the check, and there is a 'max_cswd_read_retries' for retrying that check covering this case, whose default value is only 2 and may be not enough for machines with huge number of CPUs.

So scale and enlarge the max retry number according to CPU number to better filter those latency noise on large system, which has been verified fine in 4 days and 670 rounds of reboot test on the 8-socket machine.

---
 kernel/time/clocksource.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/clocksource.c +9 -0
index c108ed8a9804..f15283101906 100644
@@ -226,6 +226,15 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
 	u64 wd_end, wd_end2, wd_delta;
 	int64_t wd_delay, wd_seq_delay;
 
+	/*
+	 * If no user changes the default value, scale the retry threshold
+	 * according to CPU numbers. As per test, the more CPU a platform has,
+	 * the bigger read latency is found during boot time or under stress
+	 * work load. Increase the try nubmer to reduce false alarms.
+ 	 */
+	if (max_cswd_read_retries == 2)
+		max_cswd_read_retries = max(2, ilog2(num_online_cpus()));
+
 	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
 		local_irq_disable();
 		*wdnow = watchdog->read(watchdog);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 39 lines · Feng Tang

There was a bug on one 8-socket server that the TSC is wrongly marked as 'unstable' and disabled during boot time. (reproduce rate is every 120 rounds of reboot tests), with log:

clocksource: timekeeping watchdog on CPU227: wd-tsc-wd excessive read-back delay of 153560ns vs. limit of 125000ns, wd-wd read-back delay only 11440ns, attempt 3, marking tsc unstable
tsc: Marking TSC unstable due to clocksource watchdog
TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
sched_clock: Marking unstable (119294969739, 159204297)<-(125446229205, -5992055152)
clocksource: Checking clocksource tsc synchronization from CPU 319 to CPUs 0,99,136,180,210,542,601,896.
clocksource: Switched to clocksource hpet

The reason is for platform with lots of CPU, there are sporadic big or huge read latency of read watchog/clocksource during boot or when system is under stress work load, and the frequency and maximum value of the latency goes up with the increasing of CPU numbers. Current code already has logic to detect and filter such high latency case by reading 3 times of watchdog, and check the 2 deltas. Due to the randomness of the latency, there is a low possibility situation that the first delta (latency) is big, but the second delta is small and looks valid, which can escape from the check, and there is a 'max_cswd_read_retries' for retrying that check covering this case, whose default value is only 2 and may be not enough for machines with huge number of CPUs.

So scale and enlarge the max retry number according to CPU number to better filter those latency noise on large system, which has been verified fine in 4 days and 670 rounds of reboot test on the 8-socket machine.

---
 kernel/time/clocksource.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/clocksource.c +0 -0
index c108ed8a9804..f15283101906 100644

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 235

Quoted 10 lines
kernel/time/clocksource.c +7 -0
@@ -226,6 +226,15 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
 	u64 wd_end, wd_end2, wd_delta;
 	int64_t wd_delay, wd_seq_delay;
 
+	/*
+	 * If no user changes the default value, scale the retry threshold
+	 * according to CPU numbers. As per test, the more CPU a platform has,
+	 * the bigger read latency is found during boot time or under stress
+	 * work load. Increase the try nubmer to reduce false alarms.
+ 	 */
+	if (max_cswd_read_retries == 2)

I like the self-adjusting behavior!

But why not make max_cswd_read_retries be a long (instead of a ulong) defaulting to -1. Then when someone sets it to a specific positive value, they get exactly that value, while leaving it -1 gets the default CPU-scaling behavior. Zero and other negative values should get a warning in order to reserve the for possible future use.

I also suggest doing the adjustment at boot time, for example, using an early_initcall(). That way the test code also sees the scaled value.

Thanx, Paul

Quoted 8 lines
kernel/time/clocksource.c +2 -0
+		max_cswd_read_retries = max(2, ilog2(num_online_cpus()));
+
 	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
 		local_irq_disable();
 		*wdnow = watchdog->read(watchdog);
Signature · 1 line
2.34.1
author
#2 lore raw

Hi Paul,

Quoted 43 lines · Paul E. McKenney
Quoted 42 lines · Feng Tang

There was a bug on one 8-socket server that the TSC is wrongly marked as 'unstable' and disabled during boot time. (reproduce rate is every 120 rounds of reboot tests), with log:

clocksource: timekeeping watchdog on CPU227: wd-tsc-wd excessive read-back delay of 153560ns vs. limit of 125000ns, wd-wd read-back delay only 11440ns, attempt 3, marking tsc unstable
tsc: Marking TSC unstable due to clocksource watchdog
TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
sched_clock: Marking unstable (119294969739, 159204297)<-(125446229205, -5992055152)
clocksource: Checking clocksource tsc synchronization from CPU 319 to CPUs 0,99,136,180,210,542,601,896.
clocksource: Switched to clocksource hpet

The reason is for platform with lots of CPU, there are sporadic big or huge read latency of read watchog/clocksource during boot or when system is under stress work load, and the frequency and maximum value of the latency goes up with the increasing of CPU numbers. Current code already has logic to detect and filter such high latency case by reading 3 times of watchdog, and check the 2 deltas. Due to the randomness of the latency, there is a low possibility situation that the first delta (latency) is big, but the second delta is small and looks valid, which can escape from the check, and there is a 'max_cswd_read_retries' for retrying that check covering this case, whose default value is only 2 and may be not enough for machines with huge number of CPUs.

So scale and enlarge the max retry number according to CPU number to better filter those latency noise on large system, which has been verified fine in 4 days and 670 rounds of reboot test on the 8-socket machine.

---
 kernel/time/clocksource.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/clocksource.c +0 -0
index c108ed8a9804..f15283101906 100644
@@ -226,6 +226,15 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
 	u64 wd_end, wd_end2, wd_delta;
 	int64_t wd_delay, wd_seq_delay;
 
Quoted 9 lines

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 235

Quoted 7 lines
kernel/time/clocksource.c +7 -0
+	/*
+	 * If no user changes the default value, scale the retry threshold
+	 * according to CPU numbers. As per test, the more CPU a platform has,
+	 * the bigger read latency is found during boot time or under stress
+	 * work load. Increase the try nubmer to reduce false alarms.
+ 	 */
+	if (max_cswd_read_retries == 2)

I like the self-adjusting behavior!

Thanks!

Quoted 5 lines

But why not make max_cswd_read_retries be a long (instead of a ulong) defaulting to -1. Then when someone sets it to a specific positive value, they get exactly that value, while leaving it -1 gets the default CPU-scaling behavior. Zero and other negative values should get a warning in order to reserve the for possible future use.

Good suggestions!

Quoted 2 lines

I also suggest doing the adjustment at boot time, for example, using an early_initcall(). That way the test code also sees the scaled value.

I also thought about doing the adjustment once in early boot phase using num_possible_cpus(), but gave up as that parameters could be changed runtime using sysfs's module parameter interface, and cpu runtime hotplugging.

Since the watchdog timer only happens (if not skipped) every 500 ms, how about doing the ilog2 math everytime, like below:

include/linux/clocksource.h +1 -1
index 1d42d4b17327..9104bdecf34e 100644
@@ -291,7 +291,7 @@ static inline void timer_probe(void) {}
 #define TIMER_ACPI_DECLARE(name, table_id, fn)		\
 	ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
 
-extern ulong max_cswd_read_retries;
+extern long max_cswd_read_retries;
 void clocksource_verify_percpu(struct clocksource *cs);
 
 #endif /* _LINUX_CLOCKSOURCE_H */
kernel/time/clocksource.c +13 -4
index c108ed8a9804..867bb36e6dad 100644
@@ -208,8 +208,8 @@ void clocksource_mark_unstable(struct clocksource *cs)
 	spin_unlock_irqrestore(&watchdog_lock, flags);
 }
 
-ulong max_cswd_read_retries = 2;
-module_param(max_cswd_read_retries, ulong, 0644);
+long max_cswd_read_retries = -1;
+module_param(max_cswd_read_retries, long, 0644);
 EXPORT_SYMBOL_GPL(max_cswd_read_retries);
 static int verify_n_cpus = 8;
 module_param(verify_n_cpus, int, 0644);
@@ -225,8 +225,17 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
 	unsigned int nretries;
 	u64 wd_end, wd_end2, wd_delta;
 	int64_t wd_delay, wd_seq_delay;
+	long max_retries = max_cswd_read_retries;
+	
+	if (max_cswd_read_retries <= 0) {
+		if (max_cswd_read_retries != -1)
+			pr_warn_once("max_cswd_read_retries has been set a invalid number: %d\n",
+				max_cswd_read_retries);
 
-	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
+		max_retries = ilog2(num_online_cpus()) + 1;
+	} 		
+
+	for (nretries = 0; nretries <= max_retries; nretries++) {
 		local_irq_disable();
 		*wdnow = watchdog->read(watchdog);
 		*csnow = cs->read(cs);
@@ -238,7 +247,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
 		wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
 					      watchdog->shift);
 		if (wd_delay <= WATCHDOG_MAX_SKEW) {
-			if (nretries > 1 || nretries >= max_cswd_read_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);
 			}

Thanks,
Feng

#3 lore raw
Quoted 121 lines · Feng Tang

Hi Paul,

Quoted 43 lines · Paul E. McKenney
Quoted 42 lines · Feng Tang

There was a bug on one 8-socket server that the TSC is wrongly marked as 'unstable' and disabled during boot time. (reproduce rate is every 120 rounds of reboot tests), with log:

clocksource: timekeeping watchdog on CPU227: wd-tsc-wd excessive read-back delay of 153560ns vs. limit of 125000ns, wd-wd read-back delay only 11440ns, attempt 3, marking tsc unstable
tsc: Marking TSC unstable due to clocksource watchdog
TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
sched_clock: Marking unstable (119294969739, 159204297)<-(125446229205, -5992055152)
clocksource: Checking clocksource tsc synchronization from CPU 319 to CPUs 0,99,136,180,210,542,601,896.
clocksource: Switched to clocksource hpet

The reason is for platform with lots of CPU, there are sporadic big or huge read latency of read watchog/clocksource during boot or when system is under stress work load, and the frequency and maximum value of the latency goes up with the increasing of CPU numbers. Current code already has logic to detect and filter such high latency case by reading 3 times of watchdog, and check the 2 deltas. Due to the randomness of the latency, there is a low possibility situation that the first delta (latency) is big, but the second delta is small and looks valid, which can escape from the check, and there is a 'max_cswd_read_retries' for retrying that check covering this case, whose default value is only 2 and may be not enough for machines with huge number of CPUs.

So scale and enlarge the max retry number according to CPU number to better filter those latency noise on large system, which has been verified fine in 4 days and 670 rounds of reboot test on the 8-socket machine.

---
  kernel/time/clocksource.c | 9 +++++++++
  1 file changed, 9 insertions(+)
kernel/time/clocksource.c +0 -0
index c108ed8a9804..f15283101906 100644
@@ -226,6 +226,15 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  	u64 wd_end, wd_end2, wd_delta;
  	int64_t wd_delay, wd_seq_delay;
 
Quoted 8 lines

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 235

Quoted 7 lines
kernel/time/clocksource.c +7 -0
+	/*
+	 * If no user changes the default value, scale the retry threshold
+	 * according to CPU numbers. As per test, the more CPU a platform has,
+	 * the bigger read latency is found during boot time or under stress
+	 * work load. Increase the try nubmer to reduce false alarms.
+ 	 */
+	if (max_cswd_read_retries == 2)

I like the self-adjusting behavior!

Thanks!

Quoted 5 lines

But why not make max_cswd_read_retries be a long (instead of a ulong) defaulting to -1. Then when someone sets it to a specific positive value, they get exactly that value, while leaving it -1 gets the default CPU-scaling behavior. Zero and other negative values should get a warning in order to reserve the for possible future use.

Good suggestions!

Quoted 2 lines

I also suggest doing the adjustment at boot time, for example, using an early_initcall(). That way the test code also sees the scaled value.

I also thought about doing the adjustment once in early boot phase using num_possible_cpus(), but gave up as that parameters could be changed runtime using sysfs's module parameter interface, and cpu runtime hotplugging.

Since the watchdog timer only happens (if not skipped) every 500 ms, how about doing the ilog2 math everytime, like below:

include/linux/clocksource.h +1 -1
index 1d42d4b17327..9104bdecf34e 100644
@@ -291,7 +291,7 @@ static inline void timer_probe(void) {}
  #define TIMER_ACPI_DECLARE(name, table_id, fn)		\
  	ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
 
-extern ulong max_cswd_read_retries;
+extern long max_cswd_read_retries;
  void clocksource_verify_percpu(struct clocksource *cs);
 
  #endif /* _LINUX_CLOCKSOURCE_H */
kernel/time/clocksource.c +12 -3
index c108ed8a9804..867bb36e6dad 100644
@@ -208,8 +208,8 @@ void clocksource_mark_unstable(struct clocksource *cs)
  	spin_unlock_irqrestore(&watchdog_lock, flags);
  }
 
-ulong max_cswd_read_retries = 2;
-module_param(max_cswd_read_retries, ulong, 0644);
+long max_cswd_read_retries = -1;
+module_param(max_cswd_read_retries, long, 0644);
  EXPORT_SYMBOL_GPL(max_cswd_read_retries);
  static int verify_n_cpus = 8;
  module_param(verify_n_cpus, int, 0644);
@@ -225,8 +225,17 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  	unsigned int nretries;
  	u64 wd_end, wd_end2, wd_delta;
  	int64_t wd_delay, wd_seq_delay;
+	long max_retries = max_cswd_read_retries;
+	
+	if (max_cswd_read_retries <= 0) {
+		if (max_cswd_read_retries != -1)
+			pr_warn_once("max_cswd_read_retries has been set a invalid number: %d\n",
+				max_cswd_read_retries);
 
-	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
+		max_retries = ilog2(num_online_cpus()) + 1;
+	} 		
+
+	for (nretries = 0; nretries <= max_retries; nretries++) {
  		local_irq_disable();
  		*wdnow = watchdog->read(watchdog);
  		*csnow = cs->read(cs);

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 253

Quoted 8 lines
kernel/time/clocksource.c +1 -1
@@ -238,7 +247,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  		wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
  					      watchdog->shift);
  		if (wd_delay <= WATCHDOG_MAX_SKEW) {
-			if (nretries > 1 || nretries >= max_cswd_read_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);
  			}

The max_cswd_read_retries value is also used in kernel/time/clocksource-wdtest.c. You will have to apply similar logic to clocksource-wdtest.c if it is not done once in early_init.

Cheers,
Longman

#4 lore raw
Quoted 120 lines · Waiman Long
Quoted 119 lines · Feng Tang

Hi Paul,

Quoted 35 lines · Paul E. McKenney
Quoted 34 lines · Feng Tang

There was a bug on one 8-socket server that the TSC is wrongly marked as 'unstable' and disabled during boot time. (reproduce rate is every 120 rounds of reboot tests), with log:

clocksource: timekeeping watchdog on CPU227: wd-tsc-wd excessive read-back delay of 153560ns vs. limit of 125000ns, wd-wd read-back delay only 11440ns, attempt 3, marking tsc unstable
tsc: Marking TSC unstable due to clocksource watchdog
TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.
sched_clock: Marking unstable (119294969739, 159204297)<-(125446229205, -5992055152)
clocksource: Checking clocksource tsc synchronization from CPU 319 to CPUs 0,99,136,180,210,542,601,896.
clocksource: Switched to clocksource hpet

The reason is for platform with lots of CPU, there are sporadic big or huge read latency of read watchog/clocksource during boot or when system is under stress work load, and the frequency and maximum value of the latency goes up with the increasing of CPU numbers. Current code already has logic to detect and filter such high latency case by reading 3 times of watchdog, and check the 2 deltas. Due to the randomness of the latency, there is a low possibility situation that the first delta (latency) is big, but the second delta is small and looks valid, which can escape from the check, and there is a 'max_cswd_read_retries' for retrying that check covering this case, whose default value is only 2 and may be not enough for machines with huge number of CPUs.

So scale and enlarge the max retry number according to CPU number to better filter those latency noise on large system, which has been verified fine in 4 days and 670 rounds of reboot test on the 8-socket machine.

---
  kernel/time/clocksource.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 15 lines
Quoted 5 lines
kernel/time/clocksource.c +0 -0
index c108ed8a9804..f15283101906 100644

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 234

Quoted 9 lines
kernel/time/clocksource.c +7 -0
@@ -226,6 +226,15 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  	u64 wd_end, wd_end2, wd_delta;
  	int64_t wd_delay, wd_seq_delay;
+	/*
+	 * If no user changes the default value, scale the retry threshold
+	 * according to CPU numbers. As per test, the more CPU a platform has,
+	 * the bigger read latency is found during boot time or under stress
+	 * work load. Increase the try nubmer to reduce false alarms.
+ 	 */
+	if (max_cswd_read_retries == 2)

I like the self-adjusting behavior!

Thanks!

Quoted 5 lines

But why not make max_cswd_read_retries be a long (instead of a ulong) defaulting to -1. Then when someone sets it to a specific positive value, they get exactly that value, while leaving it -1 gets the default CPU-scaling behavior. Zero and other negative values should get a warning in order to reserve the for possible future use.

Good suggestions!

Quoted 2 lines

I also suggest doing the adjustment at boot time, for example, using an early_initcall(). That way the test code also sees the scaled value.

I also thought about doing the adjustment once in early boot phase using num_possible_cpus(), but gave up as that parameters could be changed runtime using sysfs's module parameter interface, and cpu runtime hotplugging.

Since the watchdog timer only happens (if not skipped) every 500 ms, how about doing the ilog2 math everytime, like below:

include/linux/clocksource.h +1 -1
index 1d42d4b17327..9104bdecf34e 100644
@@ -291,7 +291,7 @@ static inline void timer_probe(void) {}
  #define TIMER_ACPI_DECLARE(name, table_id, fn)		\
  	ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
-extern ulong max_cswd_read_retries;
+extern long max_cswd_read_retries;
  void clocksource_verify_percpu(struct clocksource *cs);
  #endif /* _LINUX_CLOCKSOURCE_H */
kernel/time/clocksource.c +12 -4
index c108ed8a9804..867bb36e6dad 100644
@@ -208,8 +208,8 @@ void clocksource_mark_unstable(struct clocksource *cs)
  	spin_unlock_irqrestore(&watchdog_lock, flags);
  }
-ulong max_cswd_read_retries = 2;
-module_param(max_cswd_read_retries, ulong, 0644);
+long max_cswd_read_retries = -1;
+module_param(max_cswd_read_retries, long, 0644);
  EXPORT_SYMBOL_GPL(max_cswd_read_retries);
  static int verify_n_cpus = 8;
  module_param(verify_n_cpus, int, 0644);
@@ -225,8 +225,17 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  	unsigned int nretries;
  	u64 wd_end, wd_end2, wd_delta;
  	int64_t wd_delay, wd_seq_delay;
+	long max_retries = max_cswd_read_retries;
+	
+	if (max_cswd_read_retries <= 0) {
+		if (max_cswd_read_retries != -1)
+			pr_warn_once("max_cswd_read_retries has been set a invalid number: %d\n",
+				max_cswd_read_retries);
-	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
+		max_retries = ilog2(num_online_cpus()) + 1;
+	} 		
+
+	for (nretries = 0; nretries <= max_retries; nretries++) {
  		local_irq_disable();
  		*wdnow = watchdog->read(watchdog);
  		*csnow = cs->read(cs);
@@ -238,7 +247,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  		wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
  					      watchdog->shift);
  		if (wd_delay <= WATCHDOG_MAX_SKEW) {
-			if (nretries > 1 || nretries >= max_cswd_read_retries) {
Quoted 8 lines

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 253

Quoted 4 lines
kernel/time/clocksource.c +1 -0
+			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);
  			}

The max_cswd_read_retries value is also used in kernel/time/clocksource-wdtest.c. You will have to apply similar logic to clocksource-wdtest.c if it is not done once in early_init.

Good point! If it is not done in an early_init(), could we please have a function for the common code?

Thanx, Paul

author
#5 lore raw
Quoted 1 line · Paul E. McKenney

On Fri, Jan 26, 2024 at 11:19:50AM -0500, Waiman Long wrote:

[...]

Quoted 67 lines
Quoted 58 lines
Quoted 58 lines
Quoted 2 lines

I also suggest doing the adjustment at boot time, for example, using an early_initcall(). That way the test code also sees the scaled value.

I also thought about doing the adjustment once in early boot phase using num_possible_cpus(), but gave up as that parameters could be changed runtime using sysfs's module parameter interface, and cpu runtime hotplugging.

Since the watchdog timer only happens (if not skipped) every 500 ms, how about doing the ilog2 math everytime, like below:

include/linux/clocksource.h +1 -1
index 1d42d4b17327..9104bdecf34e 100644
@@ -291,7 +291,7 @@ static inline void timer_probe(void) {}
  #define TIMER_ACPI_DECLARE(name, table_id, fn)		\
  	ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
-extern ulong max_cswd_read_retries;
+extern long max_cswd_read_retries;
  void clocksource_verify_percpu(struct clocksource *cs);
  #endif /* _LINUX_CLOCKSOURCE_H */
kernel/time/clocksource.c +12 -4
index c108ed8a9804..867bb36e6dad 100644
@@ -208,8 +208,8 @@ void clocksource_mark_unstable(struct clocksource *cs)
  	spin_unlock_irqrestore(&watchdog_lock, flags);
  }
-ulong max_cswd_read_retries = 2;
-module_param(max_cswd_read_retries, ulong, 0644);
+long max_cswd_read_retries = -1;
+module_param(max_cswd_read_retries, long, 0644);
  EXPORT_SYMBOL_GPL(max_cswd_read_retries);
  static int verify_n_cpus = 8;
  module_param(verify_n_cpus, int, 0644);
@@ -225,8 +225,17 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  	unsigned int nretries;
  	u64 wd_end, wd_end2, wd_delta;
  	int64_t wd_delay, wd_seq_delay;
+	long max_retries = max_cswd_read_retries;
+	
+	if (max_cswd_read_retries <= 0) {
+		if (max_cswd_read_retries != -1)
+			pr_warn_once("max_cswd_read_retries has been set a invalid number: %d\n",
+				max_cswd_read_retries);
-	for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
+		max_retries = ilog2(num_online_cpus()) + 1;
+	} 		
+
+	for (nretries = 0; nretries <= max_retries; nretries++) {
  		local_irq_disable();
  		*wdnow = watchdog->read(watchdog);
  		*csnow = cs->read(cs);
@@ -238,7 +247,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
  		wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
  					      watchdog->shift);
  		if (wd_delay <= WATCHDOG_MAX_SKEW) {
-			if (nretries > 1 || nretries >= max_cswd_read_retries) {
Quoted 8 lines

kernel/time/clocksource.c static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, · line 253

Quoted 4 lines
kernel/time/clocksource.c +1 -0
+			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);
  			}

The max_cswd_read_retries value is also used in kernel/time/clocksource-wdtest.c. You will have to apply similar logic to clocksource-wdtest.c if it is not done once in early_init.

Quoted 2 lines

Good point! If it is not done in an early_init(), could we please have a function for the common code?

Thanks Waiman for the catch! And sure, will add a new helper function for that.

Thanks,
Feng

Quoted 2 lines

Thanx, Paul