ac0718
commit f32a8469a236728fb158ce997385b53f92b821cc
ac0718
Author: Jacob Keller <jacob.e.keller@intel.com>
ac0718
Date:   Tue Nov 23 14:43:26 2021 -0800
ac0718
ac0718
    phc2sys: move read_phc into clock_adj.c
ac0718
    
ac0718
    The read_phc function implemented in phc2sys.c is used to perform clock
ac0718
    comparison between two arbitrary clocks using clock_gettime.
ac0718
    
ac0718
    This support is used to allow phc2sys to work on any pair of clocks and
ac0718
    is implemented in a very similar manner as the kernel PTP_SYS_OFFSET
ac0718
    ioctls.
ac0718
    
ac0718
    Make this function easier to re-use by moving it out of phc2sys.c and
ac0718
    into a more accessible location. clockadj.c seems like a reasonable
ac0718
    location as this file has many functions which deal with clockid_t
ac0718
    values, and this functionality is tangentially related to adjusting
ac0718
    clocks.
ac0718
    
ac0718
    Moving this function will allow using it in the phc_ctl program in a
ac0718
    future change.
ac0718
    
ac0718
    Notice that read_phc returned 0 on failure and 1 on success. This is
ac0718
    fairly non-standard, so lets update clockadj_compare to return 0 on
ac0718
    success and -1 on failure. Fix the call sites to check correctly and
ac0718
    report an error.
ac0718
    
ac0718
    Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
ac0718
ac0718
diff --git a/clockadj.c b/clockadj.c
ac0718
index b5c78cd..e8c5789 100644
ac0718
--- a/clockadj.c
ac0718
+++ b/clockadj.c
ac0718
@@ -139,6 +139,37 @@ int clockadj_max_freq(clockid_t clkid)
ac0718
 	return f;
ac0718
 }
ac0718
 
ac0718
+int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
ac0718
+		     int64_t *offset, uint64_t *ts, int64_t *delay)
ac0718
+{
ac0718
+	struct timespec tdst1, tdst2, tsrc;
ac0718
+	int i;
ac0718
+	int64_t interval, best_interval = INT64_MAX;
ac0718
+
ac0718
+	/* Pick the quickest clkid reading. */
ac0718
+	for (i = 0; i < readings; i++) {
ac0718
+		if (clock_gettime(sysclk, &tdst1) ||
ac0718
+				clock_gettime(clkid, &tsrc) ||
ac0718
+				clock_gettime(sysclk, &tdst2)) {
ac0718
+			pr_err("failed to read clock: %m");
ac0718
+			return -1;
ac0718
+		}
ac0718
+
ac0718
+		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
ac0718
+			tdst2.tv_nsec - tdst1.tv_nsec;
ac0718
+
ac0718
+		if (best_interval > interval) {
ac0718
+			best_interval = interval;
ac0718
+			*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
ac0718
+				tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
ac0718
+			*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
ac0718
+		}
ac0718
+	}
ac0718
+	*delay = best_interval;
ac0718
+
ac0718
+	return 0;
ac0718
+}
ac0718
+
ac0718
 void sysclk_set_leap(int leap)
ac0718
 {
ac0718
 	clockid_t clkid = CLOCK_REALTIME;
ac0718
diff --git a/clockadj.h b/clockadj.h
ac0718
index 43325c8..b63ae38 100644
ac0718
--- a/clockadj.h
ac0718
+++ b/clockadj.h
ac0718
@@ -63,6 +63,24 @@ void clockadj_step(clockid_t clkid, int64_t step);
ac0718
  */
ac0718
 int clockadj_max_freq(clockid_t clkid);
ac0718
 
ac0718
+/**
ac0718
+ * Compare offset between two clocks
ac0718
+ * @param clkid     A clock ID obtained using phc_open() or CLOCK_REALTIME
ac0718
+ * @param sysclk    A clock ID obtained using phc_open() or CLOCK_REALTIME
ac0718
+ * @param readings  Number of readings to try
ac0718
+ * @param offset    On return, the nanoseconds offset between the clocks
ac0718
+ * @param ts        On return, the time of sysclk in nanoseconds that was used
ac0718
+ * @param delay     On return, the interval between two reads of sysclk
ac0718
+ * @return Zero on success and non-zero on failure.
ac0718
+ *
ac0718
+ * Compare the offset between two clocks in a similar manner as the
ac0718
+ * PTP_SYS_OFFSET ioctls. Performs multiple reads of sysclk with a read of
ac0718
+ * clkid between in order to calculate the time difference of sysclk minus
ac0718
+ * clkid.
ac0718
+ */
ac0718
+int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
ac0718
+		     int64_t *offset, uint64_t *ts, int64_t *delay);
ac0718
+
ac0718
 /**
ac0718
  * Set the system clock to insert/delete leap second at midnight.
ac0718
  * @param leap  +1 to insert leap second, -1 to delete leap second,
ac0718
diff --git a/phc2sys.c b/phc2sys.c
ac0718
index a36cbe0..7a547fa 100644
ac0718
--- a/phc2sys.c
ac0718
+++ b/phc2sys.c
ac0718
@@ -486,37 +486,6 @@ static void reconfigure(struct phc2sys_private *priv)
ac0718
 	pr_info("selecting %s as the master clock", src->device);
ac0718
 }
ac0718
 
ac0718
-static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
ac0718
-		    int64_t *offset, uint64_t *ts, int64_t *delay)
ac0718
-{
ac0718
-	struct timespec tdst1, tdst2, tsrc;
ac0718
-	int i;
ac0718
-	int64_t interval, best_interval = INT64_MAX;
ac0718
-
ac0718
-	/* Pick the quickest clkid reading. */
ac0718
-	for (i = 0; i < readings; i++) {
ac0718
-		if (clock_gettime(sysclk, &tdst1) ||
ac0718
-				clock_gettime(clkid, &tsrc) ||
ac0718
-				clock_gettime(sysclk, &tdst2)) {
ac0718
-			pr_err("failed to read clock: %m");
ac0718
-			return 0;
ac0718
-		}
ac0718
-
ac0718
-		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
ac0718
-			tdst2.tv_nsec - tdst1.tv_nsec;
ac0718
-
ac0718
-		if (best_interval > interval) {
ac0718
-			best_interval = interval;
ac0718
-			*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
ac0718
-				tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
ac0718
-			*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
ac0718
-		}
ac0718
-	}
ac0718
-	*delay = best_interval;
ac0718
-
ac0718
-	return 1;
ac0718
-}
ac0718
-
ac0718
 static int64_t get_sync_offset(struct phc2sys_private *priv, struct clock *dst)
ac0718
 {
ac0718
 	int direction = priv->forced_sync_offset;
ac0718
@@ -672,8 +641,10 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
ac0718
 		/* If a PHC is available, use it to get the whole number
ac0718
 		   of seconds in the offset and PPS for the rest. */
ac0718
 		if (src != CLOCK_INVALID) {
ac0718
-			if (!read_phc(src, clock->clkid, priv->phc_readings,
ac0718
-				      &phc_offset, &phc_ts, &phc_delay))
ac0718
+			if (clockadj_compare(src, clock->clkid,
ac0718
+					     priv->phc_readings,
ac0718
+					     &phc_offset, &phc_ts,
ac0718
+					     &phc_delay))
ac0718
 				return -1;
ac0718
 
ac0718
 			/* Convert the time stamp to the PHC time. */
ac0718
@@ -781,10 +752,11 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
ac0718
 				ts += offset;
ac0718
 			} else {
ac0718
 				/* use phc */
ac0718
-				if (!read_phc(priv->master->clkid, clock->clkid,
ac0718
-					      priv->phc_readings,
ac0718
-					      &offset, &ts, &delay))
ac0718
-					continue;
ac0718
+				if (clockadj_compare(priv->master->clkid,
ac0718
+						     clock->clkid,
ac0718
+						     priv->phc_readings,
ac0718
+						     &offset, &ts, &delay))
ac0718
+					return -1;
ac0718
 			}
ac0718
 			update_clock(priv, clock, offset, ts, delay);
ac0718
 		}
ac0718
ac0718
commit 96486bda9ac1613fb36feb84d76ababd8972bba6
ac0718
Author: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
Date:   Tue May 17 12:31:45 2022 +0200
ac0718
ac0718
    clockadj: Change clockadj_compare() to return errno.
ac0718
    
ac0718
    Return -errno from the failed clock_gettime() to allow the callers to
ac0718
    check for specific errors.
ac0718
    
ac0718
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
ac0718
diff --git a/clockadj.c b/clockadj.c
ac0718
index e8c5789..957dc57 100644
ac0718
--- a/clockadj.c
ac0718
+++ b/clockadj.c
ac0718
@@ -17,6 +17,7 @@
ac0718
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ac0718
  */
ac0718
 
ac0718
+#include <errno.h>
ac0718
 #include <math.h>
ac0718
 #include <string.h>
ac0718
 #include <unistd.h>
ac0718
@@ -152,7 +153,7 @@ int clockadj_compare(clockid_t clkid, clockid_t sysclk, int readings,
ac0718
 				clock_gettime(clkid, &tsrc) ||
ac0718
 				clock_gettime(sysclk, &tdst2)) {
ac0718
 			pr_err("failed to read clock: %m");
ac0718
-			return -1;
ac0718
+			return -errno;
ac0718
 		}
ac0718
 
ac0718
 		interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
ac0718
diff --git a/clockadj.h b/clockadj.h
ac0718
index b63ae38..6db1d79 100644
ac0718
--- a/clockadj.h
ac0718
+++ b/clockadj.h
ac0718
@@ -71,7 +71,7 @@ int clockadj_max_freq(clockid_t clkid);
ac0718
  * @param offset    On return, the nanoseconds offset between the clocks
ac0718
  * @param ts        On return, the time of sysclk in nanoseconds that was used
ac0718
  * @param delay     On return, the interval between two reads of sysclk
ac0718
- * @return Zero on success and non-zero on failure.
ac0718
+ * @return Zero on success, or negative error code on failure.
ac0718
  *
ac0718
  * Compare the offset between two clocks in a similar manner as the
ac0718
  * PTP_SYS_OFFSET ioctls. Performs multiple reads of sysclk with a read of
ac0718
ac0718
commit a523e893a15001025379e3c2dedb231e99cc886f
ac0718
Author: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
Date:   Thu Mar 24 11:55:35 2022 +0100
ac0718
ac0718
    sysoff: Change sysoff_measure() to return errno.
ac0718
    
ac0718
    Return -errno from failed ioctl instead of the SYSOFF_* enum from the
ac0718
    measurement functions to allow the callers to check for specific errors.
ac0718
    
ac0718
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
ac0718
diff --git a/sysoff.c b/sysoff.c
ac0718
index 2743859..5d3b907 100644
ac0718
--- a/sysoff.c
ac0718
+++ b/sysoff.c
ac0718
@@ -17,6 +17,7 @@
ac0718
  * with this program; if not, write to the Free Software Foundation, Inc.,
ac0718
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ac0718
  */
ac0718
+#include <errno.h>
ac0718
 #include <stdio.h>
ac0718
 #include <string.h>
ac0718
 #include <sys/ioctl.h>
ac0718
@@ -38,11 +39,11 @@ static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
ac0718
 	memset(&pso, 0, sizeof(pso));
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
ac0718
 		pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
ac0718
-		return SYSOFF_RUN_TIME_MISSING;
ac0718
+		return -errno;
ac0718
 	}
ac0718
 	*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
ac0718
 	*ts = pctns(&pso.sys_realtime);
ac0718
-	return SYSOFF_PRECISE;
ac0718
+	return 0;
ac0718
 }
ac0718
 
ac0718
 static int64_t sysoff_estimate(struct ptp_clock_time *pct, int extended,
ac0718
@@ -98,10 +99,10 @@ static int sysoff_extended(int fd, int n_samples,
ac0718
 	pso.n_samples = n_samples;
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
ac0718
 		pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
ac0718
-		return SYSOFF_RUN_TIME_MISSING;
ac0718
+		return -errno;
ac0718
 	}
ac0718
 	*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
ac0718
-	return SYSOFF_EXTENDED;
ac0718
+	return 0;
ac0718
 }
ac0718
 
ac0718
 static int sysoff_basic(int fd, int n_samples,
ac0718
@@ -112,10 +113,10 @@ static int sysoff_basic(int fd, int n_samples,
ac0718
 	pso.n_samples = n_samples;
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
ac0718
 		perror("ioctl PTP_SYS_OFFSET");
ac0718
-		return SYSOFF_RUN_TIME_MISSING;
ac0718
+		return -errno;
ac0718
 	}
ac0718
 	*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
ac0718
-	return SYSOFF_BASIC;
ac0718
+	return 0;
ac0718
 }
ac0718
 
ac0718
 int sysoff_measure(int fd, int method, int n_samples,
ac0718
@@ -130,7 +131,7 @@ int sysoff_measure(int fd, int method, int n_samples,
ac0718
 	case SYSOFF_BASIC:
ac0718
 		return sysoff_basic(fd, n_samples, result, ts, delay);
ac0718
 	}
ac0718
-	return SYSOFF_RUN_TIME_MISSING;
ac0718
+	return -EOPNOTSUPP;
ac0718
 }
ac0718
 
ac0718
 int sysoff_probe(int fd, int n_samples)
ac0718
diff --git a/sysoff.h b/sysoff.h
ac0718
index e4de919..5480f8f 100644
ac0718
--- a/sysoff.h
ac0718
+++ b/sysoff.h
ac0718
@@ -44,7 +44,7 @@ int sysoff_probe(int fd, int n_samples);
ac0718
  * @param result     The estimated offset in nanoseconds.
ac0718
  * @param ts         The system time corresponding to the 'result'.
ac0718
  * @param delay      The delay in reading of the clock in nanoseconds.
ac0718
- * @return  One of the SYSOFF_ enumeration values.
ac0718
+ * @return  Zero on success, negative error code otherwise.
ac0718
  */
ac0718
 int sysoff_measure(int fd, int method, int n_samples,
ac0718
 		   int64_t *result, uint64_t *ts, int64_t *delay);
ac0718
ac0718
commit 25b340eb1daad807d9485728f0917ec25a376e0c
ac0718
Author: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
Date:   Wed May 18 10:21:29 2022 +0200
ac0718
ac0718
    sysoff: Change log level of ioctl error messages.
ac0718
    
ac0718
    Change the log level of ioctl error messages to the error level to make
ac0718
    them visible in default configuration, with the exception of EOPNOTSUPP
ac0718
    which is expected in probing and should stay at the debug level to avoid
ac0718
    confusing users.
ac0718
    
ac0718
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
ac0718
diff --git a/sysoff.c b/sysoff.c
ac0718
index 5d3b907..a425275 100644
ac0718
--- a/sysoff.c
ac0718
+++ b/sysoff.c
ac0718
@@ -28,6 +28,14 @@
ac0718
 
ac0718
 #define NS_PER_SEC 1000000000LL
ac0718
 
ac0718
+static void print_ioctl_error(const char *name)
ac0718
+{
ac0718
+	if (errno == EOPNOTSUPP)
ac0718
+		pr_debug("ioctl %s: %s", name, strerror(errno));
ac0718
+	else
ac0718
+		pr_err("ioctl %s: %s", name, strerror(errno));
ac0718
+}
ac0718
+
ac0718
 static int64_t pctns(struct ptp_clock_time *t)
ac0718
 {
ac0718
 	return t->sec * NS_PER_SEC + t->nsec;
ac0718
@@ -38,7 +46,7 @@ static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
ac0718
 	struct ptp_sys_offset_precise pso;
ac0718
 	memset(&pso, 0, sizeof(pso));
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
ac0718
-		pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
ac0718
+		print_ioctl_error("PTP_SYS_OFFSET_PRECISE");
ac0718
 		return -errno;
ac0718
 	}
ac0718
 	*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
ac0718
@@ -98,7 +106,7 @@ static int sysoff_extended(int fd, int n_samples,
ac0718
 	memset(&pso, 0, sizeof(pso));
ac0718
 	pso.n_samples = n_samples;
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
ac0718
-		pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
ac0718
+		print_ioctl_error("PTP_SYS_OFFSET_EXTENDED");
ac0718
 		return -errno;
ac0718
 	}
ac0718
 	*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
ac0718
@@ -112,7 +120,7 @@ static int sysoff_basic(int fd, int n_samples,
ac0718
 	memset(&pso, 0, sizeof(pso));
ac0718
 	pso.n_samples = n_samples;
ac0718
 	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
ac0718
-		perror("ioctl PTP_SYS_OFFSET");
ac0718
+		print_ioctl_error("PTP_SYS_OFFSET");
ac0718
 		return -errno;
ac0718
 	}
ac0718
 	*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
ac0718
ac0718
commit 755cf11ad6e5d02e11519b6e2644ee0f71da91ea
ac0718
Author: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
Date:   Thu Mar 24 12:41:49 2022 +0100
ac0718
ac0718
    sysoff: Retry on EBUSY when probing supported ioctls.
ac0718
    
ac0718
    Handle EBUSY when probing support for a PTP_SYS_OFFSET ioctl. Try each
ac0718
    ioctl up to three times before giving up on it to make the detection
ac0718
    more reliable.
ac0718
    
ac0718
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
ac0718
diff --git a/sysoff.c b/sysoff.c
ac0718
index a425275..fc1f7ca 100644
ac0718
--- a/sysoff.c
ac0718
+++ b/sysoff.c
ac0718
@@ -145,8 +145,8 @@ int sysoff_measure(int fd, int method, int n_samples,
ac0718
 int sysoff_probe(int fd, int n_samples)
ac0718
 {
ac0718
 	int64_t junk, delay;
ac0718
+	int i, j, err;
ac0718
 	uint64_t ts;
ac0718
-	int i;
ac0718
 
ac0718
 	if (n_samples > PTP_MAX_SAMPLES) {
ac0718
 		fprintf(stderr, "warning: %d exceeds kernel max readings %d\n",
ac0718
@@ -156,9 +156,15 @@ int sysoff_probe(int fd, int n_samples)
ac0718
 	}
ac0718
 
ac0718
 	for (i = 0; i < SYSOFF_LAST; i++) {
ac0718
-		if (sysoff_measure(fd, i, n_samples, &junk, &ts, &delay) < 0)
ac0718
-			continue;
ac0718
-		return i;
ac0718
+		for (j = 0; j < 3; j++) {
ac0718
+			err = sysoff_measure(fd, i, n_samples, &junk, &ts,
ac0718
+					     &delay);
ac0718
+			if (err == -EBUSY)
ac0718
+				continue;
ac0718
+			if (err)
ac0718
+				break;
ac0718
+			return i;
ac0718
+		}
ac0718
 	}
ac0718
 
ac0718
 	return SYSOFF_RUN_TIME_MISSING;
ac0718
ac0718
commit d1e8ea2405a42b42bcaf2166717fe0da6e9871ae
ac0718
Author: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
Date:   Tue Mar 8 12:18:31 2022 +0100
ac0718
ac0718
    phc2sys: Don't exit when reading of PHC fails with EBUSY.
ac0718
    
ac0718
    Reading of the PHC can occasionally fail with some drivers, e.g. the ice
ac0718
    driver returns EBUSY when it fails to get a lock. Continue in the loop
ac0718
    instead of exiting on the error.
ac0718
    
ac0718
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
ac0718
ac0718
diff --git a/phc2sys.c b/phc2sys.c
ac0718
index 7a547fa..b4e2e87 100644
ac0718
--- a/phc2sys.c
ac0718
+++ b/phc2sys.c
ac0718
@@ -623,6 +623,7 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
ac0718
 	int64_t pps_offset, phc_offset, phc_delay;
ac0718
 	uint64_t pps_ts, phc_ts;
ac0718
 	clockid_t src = priv->master->clkid;
ac0718
+	int err;
ac0718
 
ac0718
 	priv->master->source_label = "pps";
ac0718
 
ac0718
@@ -641,10 +642,13 @@ static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
ac0718
 		/* If a PHC is available, use it to get the whole number
ac0718
 		   of seconds in the offset and PPS for the rest. */
ac0718
 		if (src != CLOCK_INVALID) {
ac0718
-			if (clockadj_compare(src, clock->clkid,
ac0718
-					     priv->phc_readings,
ac0718
-					     &phc_offset, &phc_ts,
ac0718
-					     &phc_delay))
ac0718
+			err = clockadj_compare(src, clock->clkid,
ac0718
+					       priv->phc_readings,
ac0718
+					       &phc_offset, &phc_ts,
ac0718
+					       &phc_delay);
ac0718
+			if (err == -EBUSY)
ac0718
+				continue;
ac0718
+			if (err)
ac0718
 				return -1;
ac0718
 
ac0718
 			/* Convert the time stamp to the PHC time. */
ac0718
@@ -692,6 +696,7 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
ac0718
 	struct clock *clock;
ac0718
 	uint64_t ts;
ac0718
 	int64_t offset, delay;
ac0718
+	int err;
ac0718
 
ac0718
 	interval.tv_sec = priv->phc_interval;
ac0718
 	interval.tv_nsec = (priv->phc_interval - interval.tv_sec) * 1e9;
ac0718
@@ -735,29 +740,32 @@ static int do_loop(struct phc2sys_private *priv, int subscriptions)
ac0718
 			if (clock->clkid == CLOCK_REALTIME &&
ac0718
 			    priv->master->sysoff_method >= 0) {
ac0718
 				/* use sysoff */
ac0718
-				if (sysoff_measure(CLOCKID_TO_FD(priv->master->clkid),
ac0718
-						   priv->master->sysoff_method,
ac0718
-						   priv->phc_readings,
ac0718
-						   &offset, &ts, &delay) < 0)
ac0718
-					return -1;
ac0718
+				err = sysoff_measure(CLOCKID_TO_FD(priv->master->clkid),
ac0718
+						     priv->master->sysoff_method,
ac0718
+						     priv->phc_readings,
ac0718
+						     &offset, &ts, &delay);
ac0718
 			} else if (priv->master->clkid == CLOCK_REALTIME &&
ac0718
 				   clock->sysoff_method >= 0) {
ac0718
 				/* use reversed sysoff */
ac0718
-				if (sysoff_measure(CLOCKID_TO_FD(clock->clkid),
ac0718
-						   clock->sysoff_method,
ac0718
-						   priv->phc_readings,
ac0718
-						   &offset, &ts, &delay) < 0)
ac0718
-					return -1;
ac0718
-				offset = -offset;
ac0718
-				ts += offset;
ac0718
+				err = sysoff_measure(CLOCKID_TO_FD(clock->clkid),
ac0718
+						     clock->sysoff_method,
ac0718
+						     priv->phc_readings,
ac0718
+						     &offset, &ts, &delay);
ac0718
+				if (!err) {
ac0718
+					offset = -offset;
ac0718
+					ts += offset;
ac0718
+				}
ac0718
 			} else {
ac0718
 				/* use phc */
ac0718
-				if (clockadj_compare(priv->master->clkid,
ac0718
-						     clock->clkid,
ac0718
-						     priv->phc_readings,
ac0718
-						     &offset, &ts, &delay))
ac0718
-					return -1;
ac0718
+				err = clockadj_compare(priv->master->clkid,
ac0718
+						       clock->clkid,
ac0718
+						       priv->phc_readings,
ac0718
+						       &offset, &ts, &delay);
ac0718
 			}
ac0718
+			if (err == -EBUSY)
ac0718
+				continue;
ac0718
+			if (err)
ac0718
+				return -1;
ac0718
 			update_clock(priv, clock, offset, ts, delay);
ac0718
 		}
ac0718
 	}