Skip to contentSkip to conversation

gve: add support for PTP gettimex64

PATCH Superseded Harshitha Ramamurthy 3 participants 10 messages Apr 3 – Apr 9

Subsystems (3)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Harshitha Ramamurthy

author maintainer
#0 lore raw

v3 cover gve: add support for PTP gettimex64

From: Jordan Rhee <[email protected]>

This patch series adds support to obtain near-simultaneous NIC and system timestamps with gettimex64. This enables daemons like chrony and phc2sys to synchronize the system clock to the NIC clock.

GVE does not have direct register access to the NIC hardware clock, so it must issue an AdminQ command to read the NIC clock. Due to relatively high latency of AQ commands (~100us), it is necessary for the hypervisor to sample the system time sandwich inside the AQ command.

The first two patches pave the way for the PTP implementation by quieting excessive logging and refactoring an existing routine for thread safety.

When tested inside a GCE C3 VM, this enabled chrony to synchronize the NIC clock and system clock with sub-microsecond precision.

chronyc tracking
Reference ID : 50484330 (PHC0)
Stratum : 1
Ref time (UTC) : Sat Feb 14 01:53:42 2026
System time : 0.000000001 seconds fast of NTP time
Last offset : -0.000000000 seconds
RMS offset : 0.000000001 seconds
Frequency : 0.686 ppm fast
Residual freq : -0.000 ppm
Skew : 0.001 ppm
Root delay : 0.000000001 seconds
Root dispersion : 0.000000812 seconds
Update interval : 0.5 seconds
Leap status : Normal

---
Changelog:
V3:
- Take system time snapshot inside the mutex
- Return -EOPNOTSUPP if cross-timestamp is requested on an arch other
than x86 or arm64
- Fix initialization to only register PTP clock once all data is
initialized
- Link to v2: https://lore.kernel.org/netdev/[email protected]/

V2:
- Fixed compilation warning on ARM by casting to u64
- Link to v1: https://lore.kernel.org/netdev/[email protected]/
---

Ankit Garg (1):
gve: make nic clock reads thread safe

Jordan Rhee (2):
gve: skip error logging for retryable AdminQ commands
gve: implement PTP gettimex64

 drivers/net/ethernet/google/gve/gve.h         |  12 +-
 drivers/net/ethernet/google/gve/gve_adminq.c  |  26 +-
 drivers/net/ethernet/google/gve/gve_adminq.h  |   4 +-
 drivers/net/ethernet/google/gve/gve_ethtool.c |   3 +-
 drivers/net/ethernet/google/gve/gve_ptp.c     | 317 +++++++++++++-----
 5 files changed, 268 insertions(+), 94 deletions(-)

base-commit: 5446b8691eb8278f10deca92048fad84ffd1e4d5

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
author maintainer
#1 lore raw

PATCH v3 1/3 gve: skip error logging for retryable AdminQ commands

From: Jordan Rhee <[email protected]>

AdminQ commands may return -EAGAIN under certain transient conditions. These commands are intended to be retried by the driver, so logging a formal error to the system log is misleading and creates unnecessary noise.

Modify the logging logic to skip the error message when the result is -EAGAIN.

---
 drivers/net/ethernet/google/gve/gve_adminq.c | 26 +++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/net/ethernet/google/gve/gve_adminq.c +20 -6
index 08587bf40ed4..c7834614c5f0 100644
@@ -416,11 +416,6 @@ static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt)
 
 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
 {
-	if (status != GVE_ADMINQ_COMMAND_PASSED &&
-	    status != GVE_ADMINQ_COMMAND_UNSET) {
-		dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status);
-		priv->adminq_cmd_fail++;
-	}
 	switch (status) {
 	case GVE_ADMINQ_COMMAND_PASSED:
 		return 0;
@@ -455,6 +450,16 @@ static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
 	}
 }
 
+static bool gve_adminq_is_retryable(enum gve_adminq_opcodes opcode)
+{
+	switch (opcode) {
+	case GVE_ADMINQ_REPORT_NIC_TIMESTAMP:
+		return true;
+	default:
+		return false;
+	}
+}
+
 /* Flushes all AQ commands currently queued and waits for them to complete.
  * If there are failures, it will return the first error.
  */
@@ -482,9 +487,18 @@ static int gve_adminq_kick_and_wait(struct gve_priv *priv)
 		cmd = &priv->adminq[i & priv->adminq_mask];
 		status = be32_to_cpu(READ_ONCE(cmd->status));
 		err = gve_adminq_parse_err(priv, status);
-		if (err)
+		if (err) {
+			enum gve_adminq_opcodes opcode =
+				be32_to_cpu(READ_ONCE(cmd->opcode));
+			priv->adminq_cmd_fail++;
+			if (!gve_adminq_is_retryable(opcode) || err != -EAGAIN)
+				dev_err_ratelimited(&priv->pdev->dev,
+						    "AQ command %d failed with status %d\n",
+						    opcode, status);
+
 			// Return the first error if we failed.
 			return err;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#2 lore raw

Re: [PATCH net-next v3 1/3] gve: skip error logging for retryable AdminQ commands

Quoted 10 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

AdminQ commands may return -EAGAIN under certain transient conditions. These commands are intended to be retried by the driver, so logging a formal error to the system log is misleading and creates unnecessary noise.

Modify the logging logic to skip the error message when the result is -EAGAIN.

The implementation changes from using dev_err() to using dev_err_ratelimited(). IMO that is a good change but it would be nice if this was mentioned in the commit message.

Thanks,
Jake

Quoted 12 lines
---
 drivers/net/ethernet/google/gve/gve_adminq.c | 26 +++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/net/ethernet/google/gve/gve_adminq.c +0 -0
index 08587bf40ed4..c7834614c5f0 100644

drivers/net/ethernet/google/gve/gve_adminq.c static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt) · line 423

Quoted 8 lines
drivers/net/ethernet/google/gve/gve_adminq.c +0 -5
@@ -416,11 +416,6 @@ static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt)
 
 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
 {
-	if (status != GVE_ADMINQ_COMMAND_PASSED &&
-	    status != GVE_ADMINQ_COMMAND_UNSET) {
-		dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status);
-		priv->adminq_cmd_fail++;
-	}

You also now log every error regardless of what the status is since this is no longer checked?

Quoted 40 lines
drivers/net/ethernet/google/gve/gve_adminq.c +20 -1
 	switch (status) {
 	case GVE_ADMINQ_COMMAND_PASSED:
 		return 0;
@@ -455,6 +450,16 @@ static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
 	}
 }
 
+static bool gve_adminq_is_retryable(enum gve_adminq_opcodes opcode)
+{
+	switch (opcode) {
+	case GVE_ADMINQ_REPORT_NIC_TIMESTAMP:
+		return true;
+	default:
+		return false;
+	}
+}
+
 /* Flushes all AQ commands currently queued and waits for them to complete.
  * If there are failures, it will return the first error.
  */
@@ -482,9 +487,18 @@ static int gve_adminq_kick_and_wait(struct gve_priv *priv)
 		cmd = &priv->adminq[i & priv->adminq_mask];
 		status = be32_to_cpu(READ_ONCE(cmd->status));
 		err = gve_adminq_parse_err(priv, status);
-		if (err)
+		if (err) {
+			enum gve_adminq_opcodes opcode =
+				be32_to_cpu(READ_ONCE(cmd->opcode));
+			priv->adminq_cmd_fail++;
+			if (!gve_adminq_is_retryable(opcode) || err != -EAGAIN)
+				dev_err_ratelimited(&priv->pdev->dev,
+						    "AQ command %d failed with status %d\n",
+						    opcode, status);
+
 			// Return the first error if we failed.
 			return err;
+		}
 	}
 
 	return 0;
#3 lore raw

Re: [PATCH net-next v3 1/3] gve: skip error logging for retryable AdminQ commands

Quoted 9 lines · Jacob Keller
Quoted 7 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

AdminQ commands may return -EAGAIN under certain transient conditions. These commands are intended to be retried by the driver, so logging a formal error to the system log is misleading and creates unnecessary noise.

Quoted 7 lines
Quoted 3 lines

Modify the logging logic to skip the error message when the result is -EAGAIN.

The implementation changes from using dev_err() to using dev_err_ratelimited(). IMO that is a good change but it would be nice if this was mentioned in the commit message.

Ack, will do.

Quoted 17 lines

Thanks,
Jake

Quoted 13 lines
---
 drivers/net/ethernet/google/gve/gve_adminq.c | 26 +++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)
drivers/net/ethernet/google/gve/gve_adminq.c +0 -0
index 08587bf40ed4..c7834614c5f0 100644
@@ -416,11 +416,6 @@ static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt)
 
Quoted 10 lines

drivers/net/ethernet/google/gve/gve_adminq.c static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt) · line 423

Quoted 7 lines
drivers/net/ethernet/google/gve/gve_adminq.c +0 -5
 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
 {
-     if (status != GVE_ADMINQ_COMMAND_PASSED &&
-         status != GVE_ADMINQ_COMMAND_UNSET) {
-             dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status);
-             priv->adminq_cmd_fail++;
-     }

You also now log every error regardless of what the status is since this is no longer checked?

PASSED gets translated to err=0, so it will not be printed.

You bring up a good point that GVE_ADMINQ_COMMAND_UNSET will now result in two messages being printed:

     dev_err(&priv->pdev->dev, "parse_aq_err: err and status both
unset, this should not be possible.\n");
     dev_err_ratelimited(&priv->pdev->dev, "AQ command %d failed with

status %d\n", opcode, status);

The first message doesn't make sense anymore, so I'll remove it. Thanks for catching this.

Quoted 42 lines
Quoted 41 lines
drivers/net/ethernet/google/gve/gve_adminq.c +20 -1
      switch (status) {
      case GVE_ADMINQ_COMMAND_PASSED:
              return 0;
@@ -455,6 +450,16 @@ static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
      }
 }
 
+static bool gve_adminq_is_retryable(enum gve_adminq_opcodes opcode)
+{
+     switch (opcode) {
+     case GVE_ADMINQ_REPORT_NIC_TIMESTAMP:
+             return true;
+     default:
+             return false;
+     }
+}
+
 /* Flushes all AQ commands currently queued and waits for them to complete.
  * If there are failures, it will return the first error.
  */
@@ -482,9 +487,18 @@ static int gve_adminq_kick_and_wait(struct gve_priv *priv)
              cmd = &priv->adminq[i & priv->adminq_mask];
              status = be32_to_cpu(READ_ONCE(cmd->status));
              err = gve_adminq_parse_err(priv, status);
-             if (err)
+             if (err) {
+                     enum gve_adminq_opcodes opcode =
+                             be32_to_cpu(READ_ONCE(cmd->opcode));
+                     priv->adminq_cmd_fail++;
+                     if (!gve_adminq_is_retryable(opcode) || err != -EAGAIN)
+                             dev_err_ratelimited(&priv->pdev->dev,
+                                                 "AQ command %d failed with status %d\n",
+                                                 opcode, status);
+
                      // Return the first error if we failed.
                      return err;
+             }
      }
 
      return 0;
author maintainer
#4 lore raw

PATCH v3 2/3 gve: make nic clock reads thread safe

In reply to Harshitha Ramamurthy

From: Ankit Garg <[email protected]>

Add a mutex to protect the shared DMA buffer that receives NIC timestamp reports. The NIC timestamp will be read from two different
threads: the periodic worker and upcoming gettimex64.

Move clock registration to the last step of initialization to ensure that all data needed by the clock module is initialized before the clock is exposed to usermode.

---
Changes in v3:
- Reorder init/teardown to register PTP clock last, and simplify code
- Move ptp-related members from gve_priv to gve_ptp
- Only assign priv->ptp after ptp module is successfully initialized

---
 drivers/net/ethernet/google/gve/gve.h         |  12 +-
 drivers/net/ethernet/google/gve/gve_ethtool.c |   3 +-
 drivers/net/ethernet/google/gve/gve_ptp.c     | 134 ++++++++----------
 3 files changed, 63 insertions(+), 86 deletions(-)
drivers/net/ethernet/google/gve/gve.h +4 -8
index 1d66d3834f7e..7b69d0cfc0d5 100644
@@ -792,6 +792,9 @@ struct gve_ptp {
 	struct ptp_clock_info info;
 	struct ptp_clock *clock;
 	struct gve_priv *priv;
+	struct mutex nic_ts_read_lock; /* Protects nic_ts_report */
+	struct gve_nic_ts_report *nic_ts_report;
+	dma_addr_t nic_ts_report_bus;
 };
 
 struct gve_priv {
@@ -923,8 +926,6 @@ struct gve_priv {
 	bool nic_timestamp_supported;
 	struct gve_ptp *ptp;
 	struct kernel_hwtstamp_config ts_config;
-	struct gve_nic_ts_report *nic_ts_report;
-	dma_addr_t nic_ts_report_bus;
 	u64 last_sync_nic_counter; /* Clock counter from last NIC TS report */
 };
 
@@ -1201,7 +1202,7 @@ static inline bool gve_supports_xdp_xmit(struct gve_priv *priv)
 
 static inline bool gve_is_clock_enabled(struct gve_priv *priv)
 {
-	return priv->nic_ts_report;
+	return priv->ptp;
 }
 
 /* gqi napi handler defined in gve_main.c */
@@ -1321,14 +1322,9 @@ int gve_flow_rules_reset(struct gve_priv *priv);
 int gve_init_rss_config(struct gve_priv *priv, u16 num_queues);
 /* PTP and timestamping */
 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
-int gve_clock_nic_ts_read(struct gve_priv *priv);
 int gve_init_clock(struct gve_priv *priv);
 void gve_teardown_clock(struct gve_priv *priv);
 #else /* CONFIG_PTP_1588_CLOCK */
-static inline int gve_clock_nic_ts_read(struct gve_priv *priv)
-{
-	return -EOPNOTSUPP;
-}
 
 static inline int gve_init_clock(struct gve_priv *priv)
 {
drivers/net/ethernet/google/gve/gve_ethtool.c +1 -2
index dc2213b5ce24..4fd7e8a442c5 100644
@@ -972,8 +972,7 @@ static int gve_get_ts_info(struct net_device *netdev,
 		info->rx_filters |= BIT(HWTSTAMP_FILTER_NONE) |
 				    BIT(HWTSTAMP_FILTER_ALL);
 
-		if (priv->ptp)
-			info->phc_index = ptp_clock_index(priv->ptp->clock);
+		info->phc_index = ptp_clock_index(priv->ptp->clock);
 	}
 
 	return 0;
drivers/net/ethernet/google/gve/gve_ptp.c +58 -76

188 lines not on this page · raw message

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
author maintainer
#5 lore raw

PATCH v3 3/3 gve: implement PTP gettimex64

In reply to Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

Enable chrony and phc2sys to synchronize system clock to NIC clock.

The system cycle counters are sampled by the device to minimize the uncertainty window. If the system times are sampled in the host, the delta between pre and post readings is 100us or more due to AQ command latency. The system times returned by the device have a delta of ~1us, which enables significantly more accurate clock synchronization.

---
Changes in v3:
- Take system time snapshot inside the mutex
- Return -EOPNOTSUPP if cross-timestamp is requested on an arch other than x86 or arm64

Changes in v2:
- fix compilation warning on ARM by casting cycles_t to u64

---
 drivers/net/ethernet/google/gve/gve_adminq.h |   4 +-
 drivers/net/ethernet/google/gve/gve_ptp.c    | 197 ++++++++++++++++++-
 2 files changed, 192 insertions(+), 9 deletions(-)
drivers/net/ethernet/google/gve/gve_adminq.h +2 -2
index 22a74b6aa17e..e6dcf6da9091 100644
@@ -411,8 +411,8 @@ static_assert(sizeof(struct gve_adminq_report_nic_ts) == 16);
 
 struct gve_nic_ts_report {
 	__be64 nic_timestamp; /* NIC clock in nanoseconds */
-	__be64 reserved1;
-	__be64 reserved2;
+	__be64 pre_cycles; /* System cycle counter before NIC clock read */
+	__be64 post_cycles; /* System cycle counter after NIC clock read */
 	__be64 reserved3;
 	__be64 reserved4;
 };
drivers/net/ethernet/google/gve/gve_ptp.c +190 -7

232 lines not on this page · raw message

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#6 lore raw

Re: [PATCH net-next v3 3/3] gve: implement PTP gettimex64

Quoted 31 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

Enable chrony and phc2sys to synchronize system clock to NIC clock.

The system cycle counters are sampled by the device to minimize the uncertainty window. If the system times are sampled in the host, the delta between pre and post readings is 100us or more due to AQ command latency. The system times returned by the device have a delta of ~1us, which enables significantly more accurate clock synchronization.

---

drivers/net/ethernet/google/gve/gve_ptp.c +14 -0
+/*
+ * Convert a raw cycle count (e.g. from get_cycles()) to the system clock
+ * type specified by clockid. The system_time_snapshot must be taken before
+ * the cycle counter is sampled.
+ */
+static int gve_cycles_to_timespec64(struct gve_priv *priv, clockid_t clockid,
+				    struct system_time_snapshot *snap,
+				    u64 cycles, struct timespec64 *ts)
+{
+	struct gve_cycles_to_clock_callback_ctx ctx = {0};
+	struct system_device_crosststamp xtstamp;
+	int err;
+
+	ctx.cycles = cycles;

drivers/net/ethernet/google/gve/gve_ptp.c line 151

Quoted 10 lines
drivers/net/ethernet/google/gve/gve_ptp.c +10 -0
+	err = get_device_system_crosststamp(gve_cycles_to_clock_fn, &ctx, snap,
+					    &xtstamp);
+	if (err) {
+		dev_err_ratelimited(&priv->pdev->dev,
+				    "get_device_system_crosststamp() failed to convert %lld cycles to system time: %d\n",
+				    cycles,
+				    err);
+		return err;
+	}
+ 

This looks a lot like a cross timestamp (i.e. something like PCIe PTM) Why not just implement the .crosstimestamp and PTP_SYS_OFF_PRECISE? Does that not work properly? Or is this not really a cross timestamp despite use of the get_device_system_crosststamp handler? :D

Thanks,
Jake

Quoted 17 lines
drivers/net/ethernet/google/gve/gve_ptp.c +17 -0
+	switch (clockid) {
+	case CLOCK_REALTIME:
+		*ts = ktime_to_timespec64(xtstamp.sys_realtime);
+		break;
+	case CLOCK_MONOTONIC_RAW:
+		*ts = ktime_to_timespec64(xtstamp.sys_monoraw);
+		break;
+	default:
+		dev_err_ratelimited(&priv->pdev->dev,
+				    "Cycle count conversion to clockid %d not supported\n",
+				    clockid);
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
#7 lore raw

Re: [PATCH net-next v3 3/3] gve: implement PTP gettimex64

Quoted 40 lines · Jacob Keller
Quoted 38 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

Enable chrony and phc2sys to synchronize system clock to NIC clock.

The system cycle counters are sampled by the device to minimize the uncertainty window. If the system times are sampled in the host, the delta between pre and post readings is 100us or more due to AQ command latency. The system times returned by the device have a delta of ~1us, which enables significantly more accurate clock synchronization.

---

drivers/net/ethernet/google/gve/gve_ptp.c +21 -0
+/*
+ * Convert a raw cycle count (e.g. from get_cycles()) to the system clock
+ * type specified by clockid. The system_time_snapshot must be taken before
+ * the cycle counter is sampled.
+ */
+static int gve_cycles_to_timespec64(struct gve_priv *priv, clockid_t clockid,
+                                 struct system_time_snapshot *snap,
+                                 u64 cycles, struct timespec64 *ts)
+{
+     struct gve_cycles_to_clock_callback_ctx ctx = {0};
+     struct system_device_crosststamp xtstamp;
+     int err;
+
+     ctx.cycles = cycles;
+     err = get_device_system_crosststamp(gve_cycles_to_clock_fn, &ctx, snap,
+                                         &xtstamp);
+     if (err) {
+             dev_err_ratelimited(&priv->pdev->dev,
+                                 "get_device_system_crosststamp() failed to convert %lld cycles to system time: %d\n",
+                                 cycles,
+                                 err);
Quoted 8 lines

drivers/net/ethernet/google/gve/gve_ptp.c line 70

Quoted 3 lines
drivers/net/ethernet/google/gve/gve_ptp.c +3 -0
+             return err;
+     }
+

This looks a lot like a cross timestamp (i.e. something like PCIe PTM) Why not just implement the .crosstimestamp and PTP_SYS_OFF_PRECISE? Does that not work properly? Or is this not really a cross timestamp despite use of the get_device_system_crosststamp handler? :D

.crosstimestamp is for devices that support simultaneous NIC and system timestamps. Devices that don't support simultaneous timestamps have to take a system time sandwich by calling ptp_read_system_prets()/ptp_read_system_postts() on either side of the NIC timestamp. Upper layers (e.g. chrony) use the sandwich delta in nontrivial ways when estimating the system clock / NIC clock offset. This is information that must be preserved, and it would be incorrect to implement .crosstimestamp by returning the midpoint of the sandwich, as tempting as that implementation might be.

Gvnic does not support simultaneous NIC and system timestamps, so it must use the sandwich technique. Since the NIC timestamp is obtained using a firmware (hypervisor) call, the uncertainty window would be too large if it were taken inside the VM. Gvnic takes the sandwich in the hypervisor and returns the raw TSC values to the VM. get_device_system_crosststamp() is used to convert the TSCs to system times, which I believe is the only correct way to do this conversion. Jordan

Quoted 21 lines

Thanks,
Jake

Quoted 17 lines
drivers/net/ethernet/google/gve/gve_ptp.c +17 -0
+     switch (clockid) {
+     case CLOCK_REALTIME:
+             *ts = ktime_to_timespec64(xtstamp.sys_realtime);
+             break;
+     case CLOCK_MONOTONIC_RAW:
+             *ts = ktime_to_timespec64(xtstamp.sys_monoraw);
+             break;
+     default:
+             dev_err_ratelimited(&priv->pdev->dev,
+                                 "Cycle count conversion to clockid %d not supported\n",
+                                 clockid);
+             return -EOPNOTSUPP;
+     }
+
+     return 0;
+}
+
#8 lore raw

Re: [PATCH net-next v3 3/3] gve: implement PTP gettimex64

Quoted 50 lines · Jordan Rhee
Quoted 40 lines · Jacob Keller
Quoted 38 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

Enable chrony and phc2sys to synchronize system clock to NIC clock.

The system cycle counters are sampled by the device to minimize the uncertainty window. If the system times are sampled in the host, the delta between pre and post readings is 100us or more due to AQ command latency. The system times returned by the device have a delta of ~1us, which enables significantly more accurate clock synchronization.

---

drivers/net/ethernet/google/gve/gve_ptp.c +21 -0
+/*
+ * Convert a raw cycle count (e.g. from get_cycles()) to the system clock
+ * type specified by clockid. The system_time_snapshot must be taken before
+ * the cycle counter is sampled.
+ */
+static int gve_cycles_to_timespec64(struct gve_priv *priv, clockid_t clockid,
+                                 struct system_time_snapshot *snap,
+                                 u64 cycles, struct timespec64 *ts)
+{
+     struct gve_cycles_to_clock_callback_ctx ctx = {0};
+     struct system_device_crosststamp xtstamp;
+     int err;
+
+     ctx.cycles = cycles;
+     err = get_device_system_crosststamp(gve_cycles_to_clock_fn, &ctx, snap,
+                                         &xtstamp);
+     if (err) {
+             dev_err_ratelimited(&priv->pdev->dev,
+                                 "get_device_system_crosststamp() failed to convert %lld cycles to system time: %d\n",
+                                 cycles,
+                                 err);
Quoted 8 lines

drivers/net/ethernet/google/gve/gve_ptp.c line 70

Quoted 3 lines
drivers/net/ethernet/google/gve/gve_ptp.c +3 -0
+             return err;
+     }
+

This looks a lot like a cross timestamp (i.e. something like PCIe PTM) Why not just implement the .crosstimestamp and PTP_SYS_OFF_PRECISE? Does that not work properly? Or is this not really a cross timestamp despite use of the get_device_system_crosststamp handler? :D

Quoted 10 lines

.crosstimestamp is for devices that support simultaneous NIC and system timestamps. Devices that don't support simultaneous timestamps have to take a system time sandwich by calling ptp_read_system_prets()/ptp_read_system_postts() on either side of the NIC timestamp. Upper layers (e.g. chrony) use the sandwich delta in nontrivial ways when estimating the system clock / NIC clock offset. This is information that must be preserved, and it would be incorrect to implement .crosstimestamp by returning the midpoint of the sandwich, as tempting as that implementation might be.

True.

Quoted 9 lines

Gvnic does not support simultaneous NIC and system timestamps, so it must use the sandwich technique. Since the NIC timestamp is obtained using a firmware (hypervisor) call, the uncertainty window would be too large if it were taken inside the VM. Gvnic takes the sandwich in the hypervisor and returns the raw TSC values to the VM. get_device_system_crosststamp() is used to convert the TSCs to system times, which I believe is the only correct way to do this conversion. Jordan

Hmm. The function says:

"Synchronously capture system/device timestamp". That is what confuses me. Your implementation uses gve_cycles_to_clock_fn() which just sets some values in the system_counterval struct and exits. It doesn't "capture a system/device timestamp" tuple.

This does feel a bit weird. No other caller appears to exist outside of the cross timestamp implementations.

It sounds like what you want is a function that takes a cycles count value and does the conversion from TSC to the appropriate clock, along with all of the interopolation etc. What you've done is sort of a cludge around get_device_system_crosststamp() to force it to do that for you without actually using it as intended.

I'd argue it would be better to have a cycles_to_ktime() or something which takes the TSC cycles value and the appropriate clock and does the exact same flow as get_device_system_crosststamp() for converting the cycles into proper ktime values without the mess of the callback function etc.

I guess in principle what you've implemented is "correct" and functional, but it definitely feels a bit weird to use the API in this way. It smells like a neat hack instead of a proper interface for this purpose.

That said, I won't object strongly if the maintainers are fine with using it for this purpose.

Thanks,
Jake

#9 lore raw

Re: [PATCH net-next v3 3/3] gve: implement PTP gettimex64

Quoted 91 lines · Jacob Keller
Quoted 50 lines · Jordan Rhee
Quoted 40 lines · Jacob Keller
Quoted 38 lines · Harshitha Ramamurthy

From: Jordan Rhee <[email protected]>

Enable chrony and phc2sys to synchronize system clock to NIC clock.

The system cycle counters are sampled by the device to minimize the uncertainty window. If the system times are sampled in the host, the delta between pre and post readings is 100us or more due to AQ command latency. The system times returned by the device have a delta of ~1us, which enables significantly more accurate clock synchronization.

---

drivers/net/ethernet/google/gve/gve_ptp.c +21 -0
+/*
+ * Convert a raw cycle count (e.g. from get_cycles()) to the system clock
+ * type specified by clockid. The system_time_snapshot must be taken before
+ * the cycle counter is sampled.
+ */
+static int gve_cycles_to_timespec64(struct gve_priv *priv, clockid_t clockid,
+                                 struct system_time_snapshot *snap,
+                                 u64 cycles, struct timespec64 *ts)
+{
+     struct gve_cycles_to_clock_callback_ctx ctx = {0};
+     struct system_device_crosststamp xtstamp;
+     int err;
+
+     ctx.cycles = cycles;
+     err = get_device_system_crosststamp(gve_cycles_to_clock_fn, &ctx, snap,
+                                         &xtstamp);
+     if (err) {
+             dev_err_ratelimited(&priv->pdev->dev,
+                                 "get_device_system_crosststamp() failed to convert %lld cycles to system time: %d\n",
+                                 cycles,
+                                 err);
Quoted 8 lines

drivers/net/ethernet/google/gve/gve_ptp.c line 70

Quoted 3 lines
drivers/net/ethernet/google/gve/gve_ptp.c +3 -0
+             return err;
+     }
+

This looks a lot like a cross timestamp (i.e. something like PCIe PTM) Why not just implement the .crosstimestamp and PTP_SYS_OFF_PRECISE? Does that not work properly? Or is this not really a cross timestamp despite use of the get_device_system_crosststamp handler? :D

Quoted 10 lines

.crosstimestamp is for devices that support simultaneous NIC and system timestamps. Devices that don't support simultaneous timestamps have to take a system time sandwich by calling ptp_read_system_prets()/ptp_read_system_postts() on either side of the NIC timestamp. Upper layers (e.g. chrony) use the sandwich delta in nontrivial ways when estimating the system clock / NIC clock offset. This is information that must be preserved, and it would be incorrect to implement .crosstimestamp by returning the midpoint of the sandwich, as tempting as that implementation might be.

True.

Quoted 9 lines

Gvnic does not support simultaneous NIC and system timestamps, so it must use the sandwich technique. Since the NIC timestamp is obtained using a firmware (hypervisor) call, the uncertainty window would be too large if it were taken inside the VM. Gvnic takes the sandwich in the hypervisor and returns the raw TSC values to the VM. get_device_system_crosststamp() is used to convert the TSCs to system times, which I believe is the only correct way to do this conversion. Jordan

Hmm. The function says:

"Synchronously capture system/device timestamp". That is what confuses me. Your implementation uses gve_cycles_to_clock_fn() which just sets some values in the system_counterval struct and exits. It doesn't "capture a system/device timestamp" tuple.

This does feel a bit weird. No other caller appears to exist outside of the cross timestamp implementations.

It sounds like what you want is a function that takes a cycles count value and does the conversion from TSC to the appropriate clock, along with all of the interopolation etc. What you've done is sort of a cludge around get_device_system_crosststamp() to force it to do that for you without actually using it as intended.

Quoted 5 lines

I'd argue it would be better to have a cycles_to_ktime() or something which takes the TSC cycles value and the appropriate clock and does the exact same flow as get_device_system_crosststamp() for converting the cycles into proper ktime values without the mess of the callback function etc.

Yes, that's exactly how I'm using get_device_system_crosststamp(). There is no cycles_to_ktime() function, and adding one would make this patch more difficult to backport to older kernels.

There is precedent for using get_device_system_crosststamp() this way in the virtio_rtc driver. In viortc_ptp_getcrosststamp(), the timestamp is sampled before calling get_device_system_crosststamp(), and the callback simply populates the return values from the context. I believe the snapshot parameter was added to support this use case.

Quoted 11 lines

I guess in principle what you've implemented is "correct" and functional, but it definitely feels a bit weird to use the API in this way. It smells like a neat hack instead of a proper interface for this purpose.

That said, I won't object strongly if the maintainers are fine with using it for this purpose.

Thanks,
Jake