6c691d
commit 513c7457865dba262a43e03fbe9178f4b08ba319
6c691d
Author: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
Date:   Mon Oct 24 15:25:30 2022 +0200
6c691d
6c691d
    Drop support for old kernels returning zero frequency.
6c691d
    
6c691d
    Kernels before 3.10 had a bug in reading of the system clock frequency,
6c691d
    which was worked around by commit da347d7a36f2 ("ptp4l: Set clock
6c691d
    frequency on start").
6c691d
    
6c691d
    Drop this workaround and support for the old kernels to make
6c691d
    clockadj_get_freq() useful.
6c691d
    
6c691d
    (Rebased to 3.1.1)
6c691d
    
6c691d
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
6c691d
diff --git a/clock.c b/clock.c
6c691d
index f3df220..9079428 100644
6c691d
--- a/clock.c
6c691d
+++ b/clock.c
6c691d
@@ -1147,11 +1147,6 @@ struct clock *clock_create(enum clock_type type, struct config *config,
6c691d
 
6c691d
 	if (c->clkid != CLOCK_INVALID) {
6c691d
 		fadj = (int) clockadj_get_freq(c->clkid);
6c691d
-		/* Due to a bug in older kernels, the reading may silently fail
6c691d
-		   and return 0. Set the frequency back to make sure fadj is
6c691d
-		   the actual frequency of the clock. */
6c691d
-		clockadj_set_freq(c->clkid, fadj);
6c691d
-
6c691d
 		/* Disable write phase mode if not implemented by driver */
6c691d
 		if (c->write_phase_mode && !phc_has_writephase(c->clkid)) {
6c691d
 			pr_err("clock does not support write phase mode");
6c691d
@@ -1746,7 +1741,6 @@ int clock_switch_phc(struct clock *c, int phc_index)
6c691d
 		return -1;
6c691d
 	}
6c691d
 	fadj = (int) clockadj_get_freq(clkid);
6c691d
-	clockadj_set_freq(clkid, fadj);
6c691d
 	servo = servo_create(c->config, c->servo_type, -fadj, max_adj, 0);
6c691d
 	if (!servo) {
6c691d
 		pr_err("Switching PHC, failed to create clock servo");
6c691d
diff --git a/phc2sys.c b/phc2sys.c
6c691d
index 5f6fbaa..af948eb 100644
6c691d
--- a/phc2sys.c
6c691d
+++ b/phc2sys.c
6c691d
@@ -147,9 +147,6 @@ static struct servo *servo_add(struct phc2sys_private *priv,
6c691d
 
6c691d
 	clockadj_init(clock->clkid);
6c691d
 	ppb = clockadj_get_freq(clock->clkid);
6c691d
-	/* The reading may silently fail and return 0, reset the frequency to
6c691d
-	   make sure ppb is the actual frequency of the clock. */
6c691d
-	clockadj_set_freq(clock->clkid, ppb);
6c691d
 	if (clock->clkid == CLOCK_REALTIME) {
6c691d
 		sysclk_set_leap(0);
6c691d
 		max_ppb = sysclk_max_freq();
6c691d
diff --git a/ts2phc_slave.c b/ts2phc_slave.c
6c691d
index 749efe5..5541d91 100644
6c691d
--- a/ts2phc_slave.c
6c691d
+++ b/ts2phc_slave.c
6c691d
@@ -183,10 +183,6 @@ static struct ts2phc_slave *ts2phc_slave_create(struct config *cfg, const char *
6c691d
 	pr_debug("PHC slave %s has ptp index %d", device, junk);
6c691d
 
6c691d
 	fadj = (int) clockadj_get_freq(slave->clk);
6c691d
-	/* Due to a bug in older kernels, the reading may silently fail
6c691d
-	   and return 0. Set the frequency back to make sure fadj is
6c691d
-	   the actual frequency of the clock. */
6c691d
-	clockadj_set_freq(slave->clk, fadj);
6c691d
 
6c691d
 	max_adj = phc_max_adj(slave->clk);
6c691d
 
6c691d
6c691d
commit 80d6875b0072c7ea636a5631e590fa72d169c351
6c691d
Author: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
Date:   Mon Oct 24 15:25:31 2022 +0200
6c691d
6c691d
    Don't accept errors in clockadj_get_freq().
6c691d
    
6c691d
    Exit if an error is returned from the clock_adjtime() call in
6c691d
    clockadj_get_freq(). No recoverable errors are expected.
6c691d
    
6c691d
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
6c691d
diff --git a/clockadj.c b/clockadj.c
6c691d
index 957dc57..4c920b9 100644
6c691d
--- a/clockadj.c
6c691d
+++ b/clockadj.c
6c691d
@@ -19,6 +19,7 @@
6c691d
 
6c691d
 #include <errno.h>
6c691d
 #include <math.h>
6c691d
+#include <stdlib.h>
6c691d
 #include <string.h>
6c691d
 #include <unistd.h>
6c691d
 
6c691d
@@ -72,6 +73,7 @@ double clockadj_get_freq(clockid_t clkid)
6c691d
 	memset(&tx, 0, sizeof(tx));
6c691d
 	if (clock_adjtime(clkid, &tx) < 0) {
6c691d
 		pr_err("failed to read out the clock frequency adjustment: %m");
6c691d
+		exit(1);
6c691d
 	} else {
6c691d
 		f = tx.freq / 65.536;
6c691d
 		if (clkid == CLOCK_REALTIME && realtime_nominal_tick && tx.tick)
6c691d
6c691d
commit 211cbfe810ef4082d1af8e9b50f65d4f6fd7246a
6c691d
Author: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
Date:   Mon Oct 24 15:25:32 2022 +0200
6c691d
6c691d
    Extend clockcheck to check for changes in frequency.
6c691d
    
6c691d
    Before setting the new frequency offset on a clock update, compare the
6c691d
    current frequency returned by the kernel with the value saved from the
6c691d
    previous update. Print a warning message if the difference is larger
6c691d
    than 1 ppb, allowing for rounding errors in conversion to and from
6c691d
    double. The kernel caches the value set by clock_adjtime() in shifted
6c691d
    ppm, it doesn't request it from the driver, which can have a lower
6c691d
    resulution.
6c691d
    
6c691d
    This should detect misconfigurations where multiple processes are trying
6c691d
    to control the clock (e.g. another ptp4l/phc2sys instance or an NTP
6c691d
    client), even when they don't step the clock.
6c691d
    
6c691d
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
6c691d
6c691d
diff --git a/clock.c b/clock.c
6c691d
index 9079428..eea7983 100644
6c691d
--- a/clock.c
6c691d
+++ b/clock.c
6c691d
@@ -1760,6 +1760,9 @@ int clock_switch_phc(struct clock *c, int phc_index)
6c691d
 
6c691d
 static void clock_synchronize_locked(struct clock *c, double adj)
6c691d
 {
6c691d
+	if (c->sanity_check) {
6c691d
+		clockcheck_freq(c->sanity_check, clockadj_get_freq(c->clkid));
6c691d
+	}
6c691d
 	clockadj_set_freq(c->clkid, -adj);
6c691d
 	if (c->clkid == CLOCK_REALTIME) {
6c691d
 		sysclk_set_sync();
6c691d
diff --git a/clockcheck.c b/clockcheck.c
6c691d
index f0141be..b5a69cc 100644
6c691d
--- a/clockcheck.c
6c691d
+++ b/clockcheck.c
6c691d
@@ -123,6 +123,16 @@ void clockcheck_set_freq(struct clockcheck *cc, int freq)
6c691d
 	cc->freq_known = 1;
6c691d
 }
6c691d
 
6c691d
+int clockcheck_freq(struct clockcheck *cc, int freq)
6c691d
+{
6c691d
+	/* Allow difference of 1 ppb due to conversion to/from double */
6c691d
+	if (cc->freq_known && abs(cc->current_freq - freq) > 1) {
6c691d
+		pr_warning("clockcheck: clock frequency changed unexpectedly!");
6c691d
+		return 1;
6c691d
+	}
6c691d
+	return 0;
6c691d
+}
6c691d
+
6c691d
 void clockcheck_step(struct clockcheck *cc, int64_t step)
6c691d
 {
6c691d
 	if (cc->last_ts)
6c691d
diff --git a/clockcheck.h b/clockcheck.h
6c691d
index 1ff86eb..4b09b98 100644
6c691d
--- a/clockcheck.h
6c691d
+++ b/clockcheck.h
6c691d
@@ -54,6 +54,14 @@ int clockcheck_sample(struct clockcheck *cc, uint64_t ts);
6c691d
  */
6c691d
 void clockcheck_set_freq(struct clockcheck *cc, int freq);
6c691d
 
6c691d
+/**
6c691d
+ * Check whether the frequency correction did not change unexpectedly.
6c691d
+ * @param cc   Pointer to a clock check obtained via @ref clockcheck_create().
6c691d
+ * @param freq Current reading of the frequency correction in ppb.
6c691d
+ * @return Zero if the frequency did not change, non-zero otherwise.
6c691d
+ */
6c691d
+int clockcheck_freq(struct clockcheck *cc, int freq);
6c691d
+
6c691d
 /**
6c691d
  * Inform clock check that the clock was stepped.
6c691d
  * @param cc   Pointer to a clock check obtained via @ref clockcheck_create().
6c691d
diff --git a/phc2sys.c b/phc2sys.c
6c691d
index af948eb..4daa1bf 100644
6c691d
--- a/phc2sys.c
6c691d
+++ b/phc2sys.c
6c691d
@@ -566,6 +566,9 @@ static void update_clock(struct phc2sys_private *priv, struct clock *clock,
6c691d
 		/* Fall through. */
6c691d
 	case SERVO_LOCKED:
6c691d
 	case SERVO_LOCKED_STABLE:
6c691d
+		if (clock->sanity_check)
6c691d
+			clockcheck_freq(clock->sanity_check,
6c691d
+					clockadj_get_freq(clock->clkid));
6c691d
 		clockadj_set_freq(clock->clkid, -ppb);
6c691d
 		if (clock->clkid == CLOCK_REALTIME)
6c691d
 			sysclk_set_sync();
6c691d
diff --git a/ptp4l.8 b/ptp4l.8
6c691d
index 4917240..f760e2b 100644
6c691d
--- a/ptp4l.8
6c691d
+++ b/ptp4l.8
6c691d
@@ -605,8 +605,10 @@ This option used to be called
6c691d
 The maximum allowed frequency offset between uncorrected clock and the system
6c691d
 monotonic clock in parts per billion (ppb). This is used as a sanity check of
6c691d
 the synchronized clock. When a larger offset is measured, a warning message
6c691d
-will be printed and the servo will be reset. When set to 0, the sanity check is
6c691d
-disabled. The default is 200000000 (20%).
6c691d
+will be printed and the servo will be reset. If the frequency correction set by
6c691d
+ptp4l changes unexpectedly between updates of the clock (e.g. due to another
6c691d
+process trying to control the clock), a warning message will be printed. When
6c691d
+set to 0, the sanity check is disabled. The default is 200000000 (20%).
6c691d
 .TP
6c691d
 .B initial_delay
6c691d
 The initial path delay of the clock in nanoseconds used for synchronization of