gve: add support for PTP gettimex64
Subsystems (3)
Harshitha Ramamurthy
maintainerv4 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:
V4:
- call out change to dev_err_ratelimited() in patch 1 commit message (Jacob Keller)
- Ensure only one log is emitted when command returns GVE_ADMINQ_COMMAND_UNSET (Jacob Keller)
- Link to v3: https://lore.kernel.org/netdev/[email protected]/
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 | 27 +- 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(+), 95 deletions(-)
base-commit: 5446b8691eb8278f10deca92048fad84ffd1e4d5
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Harshitha Ramamurthy
maintainerPATCH v4 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, and move logging to dev_err_ratelimited() to avoid spamming the log.
---
Changes in v4:
- call out change to dev_err_ratelimited() in the commit message (Jacob Keller)
- remove extra print when adminQ status is GVE_ADMINQ_COMMAND_UNSET (Jacob Keller)
--- drivers/net/ethernet/google/gve/gve_adminq.c | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-)
drivers/net/ethernet/google/gve/gve_adminq.c +20 -7
@@ -416,16 +416,10 @@ 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 ;case GVE_ADMINQ_COMMAND_UNSET : -dev_err (&priv->pdev->dev,"parse_aq_err: err and status both unset, this should not be possible.\n" );return -EINVAL ;case GVE_ADMINQ_COMMAND_ERROR_ABORTED :case GVE_ADMINQ_COMMAND_ERROR_CANCELLED :
@@ -455,6 +449,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 +486,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
Harshitha Ramamurthy
maintainerPATCH v4 2/3 gve: make nic clock reads thread safe
In reply to Harshitha RamamurthyFrom: 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
@@ -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
@@ -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
Harshitha Ramamurthy
maintainerPATCH v4 3/3 gve: implement PTP gettimex64
In reply to Harshitha RamamurthyFrom: 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
@@ -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
Jakub Kicinski
maintainerRe: [PATCH net-next v4 3/3] gve: implement PTP gettimex64
Quoted 9 lines · On Mon, 6 Apr 2026 23:40:02 +0000 Harshitha Ramamurthy wrote:
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.
Interesting. I'd like this looked over by David Woodhouse and tglx. Please repost after the merge window or send them an RFC.
drivers/net/ethernet/google/gve/gve_ptp.c line 45
Quoted 11 lines
drivers/net/ethernet/google/gve/gve_ptp.c +11
-0+static int gve_ptp_read_timestamp (struct gve_ptp *ptp,cycles_t *pre_cycles, +cycles_t *post_cycles, +struct system_time_snapshot *snap) +{ +unsigned long delay_us =1000 ; +int retry_count =0 ; +int err; + +lockdep_assert_held (&ptp->nic_ts_read_lock); + + do {
This can't be a for () loop with 5 iterations?
Quoted 58 lines
drivers/net/ethernet/google/gve/gve_ptp.c +44
-4+if (snap) +ktime_get_snapshot (snap); + + *pre_cycles =get_cycles (); + err =gve_adminq_report_nic_ts (ptp->priv, + ptp->nic_ts_report_bus); + +/* Prevent get_cycles() from being speculatively executed +* before the AdminQ command +*/ +rmb (); + *post_cycles =get_cycles (); +if (likely (err != -EAGAIN )) +return err; + +fsleep (delay_us); + +/* Exponential backoff */ + delay_us *=2 ; + retry_count++; + }while (retry_count <5 ); + +return -ETIMEDOUT ; +} +/* Read the nic timestamp from hardware via the admin queue. */ -static int gve_clock_nic_ts_read (struct gve_ptp *ptp,u64 *nic_raw) +static int gve_clock_nic_ts_read (struct gve_ptp *ptp,u64 *nic_raw, +struct gve_sysclock_sample *sysclock) { +cycles_t host_pre_cycles, host_post_cycles; +struct gve_nic_ts_report *ts_report;int err;mutex_lock (&ptp->nic_ts_read_lock); - err =gve_adminq_report_nic_ts (ptp->priv, ptp->nic_ts_report_bus); -if (err) + err =gve_ptp_read_timestamp (ptp, &host_pre_cycles, &host_post_cycles, + sysclock ? &sysclock->snapshot :NULL ); +if (err) { +dev_err_ratelimited (&ptp->priv->pdev->dev, +"AdminQ timestamp read failed: %d\n" , err); goto out; + } - *nic_raw =be64_to_cpu (ptp->nic_ts_report->nic_timestamp); + ts_report = ptp->nic_ts_report; + *nic_raw =be64_to_cpu (ts_report->nic_timestamp); + +if (sysclock) { + sysclock->nic_pre_cycles =be64_to_cpu (ts_report->pre_cycles); + sysclock->nic_post_cycles =be64_to_cpu (ts_report->post_cycles); + sysclock->host_pre_cycles = host_pre_cycles; + sysclock->host_post_cycles = host_post_cycles; + } out:mutex_unlock (&ptp->nic_ts_read_lock);
drivers/net/ethernet/google/gve/gve_ptp.c line 109
Quoted 10 lines
drivers/net/ethernet/google/gve/gve_ptp.c +7
-0return err; } +struct gve_cycles_to_clock_callback_ctx { +u64 cycles; +}; + +static int gve_cycles_to_clock_fn (ktime_t *device_time, +struct system_counterval_t *system_counterval, +void *ctx)
Does this do anything GVE specific??
Quoted 17 lines
drivers/net/ethernet/google/gve/gve_ptp.c +17
-0+{ +struct gve_cycles_to_clock_callback_ctx *context = ctx; + + *device_time =0 ; + + system_counterval->cycles = context->cycles; + system_counterval->use_nsecs = false; + +if (IS_ENABLED (CONFIG_X86 )) + system_counterval->cs_id =CSID_X86_TSC ; +else if (IS_ENABLED (CONFIG_ARM64 )) + system_counterval->cs_id =CSID_ARM_ARCH_COUNTER ; +else +return -EOPNOTSUPP ; + +return 0 ; +}
Signature · 1 line
pw-bot: cr
Re: [PATCH net-next v4 3/3] gve: implement PTP gettimex64
Quoted 6 lines · Jakub Kicinski
Quoted 4 lines · On Mon, 6 Apr 2026 23:40:02 +0000 Harshitha Ramamurthy wrote:
From: Jordan Rhee <[email protected]>
Enable chrony and phc2sys to synchronize system clock to NIC clock.
Quoted 8 lines
Quoted 5 lines
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.
Interesting. I'd like this looked over by David Woodhouse and tglx. Please repost after the merge window or send them an RFC.
Ack, will include comparison against ptp_read_system_prets/postts() implementation.
Quoted 14 lines
drivers/net/ethernet/google/gve/gve_ptp.c line 45
Quoted 12 lines
drivers/net/ethernet/google/gve/gve_ptp.c +11
-0+static int gve_ptp_read_timestamp (struct gve_ptp *ptp,cycles_t *pre_cycles, +cycles_t *post_cycles, +struct system_time_snapshot *snap) +{ +unsigned long delay_us =1000 ; +int retry_count =0 ; +int err; + +lockdep_assert_held (&ptp->nic_ts_read_lock); + + do {This can't be a for () loop with 5 iterations?
I've reformulated it to be in terms of total timeout, and kept it as a do-while to guarantee that at least 1 iteration is run.
Quoted 62 lines
Quoted 62 lines
drivers/net/ethernet/google/gve/gve_ptp.c +44
-4+if (snap) +ktime_get_snapshot (snap); + + *pre_cycles =get_cycles (); + err =gve_adminq_report_nic_ts (ptp->priv, + ptp->nic_ts_report_bus); + +/* Prevent get_cycles() from being speculatively executed +* before the AdminQ command +*/ +rmb (); + *post_cycles =get_cycles (); +if (likely (err != -EAGAIN )) +return err; + +fsleep (delay_us); + +/* Exponential backoff */ + delay_us *=2 ; + retry_count++; + }while (retry_count <5 ); + +return -ETIMEDOUT ; +} +/* Read the nic timestamp from hardware via the admin queue. */ -static int gve_clock_nic_ts_read (struct gve_ptp *ptp,u64 *nic_raw) +static int gve_clock_nic_ts_read (struct gve_ptp *ptp,u64 *nic_raw, +struct gve_sysclock_sample *sysclock) { +cycles_t host_pre_cycles, host_post_cycles; +struct gve_nic_ts_report *ts_report;int err;mutex_lock (&ptp->nic_ts_read_lock); - err =gve_adminq_report_nic_ts (ptp->priv, ptp->nic_ts_report_bus); -if (err) + err =gve_ptp_read_timestamp (ptp, &host_pre_cycles, &host_post_cycles, + sysclock ? &sysclock->snapshot :NULL ); +if (err) { +dev_err_ratelimited (&ptp->priv->pdev->dev, +"AdminQ timestamp read failed: %d\n" , err); goto out; + } - *nic_raw =be64_to_cpu (ptp->nic_ts_report->nic_timestamp); + ts_report = ptp->nic_ts_report; + *nic_raw =be64_to_cpu (ts_report->nic_timestamp); + +if (sysclock) { + sysclock->nic_pre_cycles =be64_to_cpu (ts_report->pre_cycles); + sysclock->nic_post_cycles =be64_to_cpu (ts_report->post_cycles); + sysclock->host_pre_cycles = host_pre_cycles; + sysclock->host_post_cycles = host_post_cycles; + } out:mutex_unlock (&ptp->nic_ts_read_lock);return err; }
Quoted 9 lines
drivers/net/ethernet/google/gve/gve_ptp.c line 109
Quoted 7 lines
drivers/net/ethernet/google/gve/gve_ptp.c +7
-0+struct gve_cycles_to_clock_callback_ctx { +u64 cycles; +}; + +static int gve_cycles_to_clock_fn (ktime_t *device_time, +struct system_counterval_t *system_counterval, +void *ctx)Does this do anything GVE specific??
No, however, I would caution against adding an API to the kernel to convert TSC to ktime since this could invite further abuses of TSC.
Quoted 20 lines
Quoted 8 lines
drivers/net/ethernet/google/gve/gve_ptp.c +7
-0+{ +struct gve_cycles_to_clock_callback_ctx *context = ctx; + + *device_time =0 ; + + system_counterval->cycles = context->cycles; + system_counterval->use_nsecs = false;drivers/net/ethernet/google/gve/gve_ptp.c line 126
Quoted 10 lines
drivers/net/ethernet/google/gve/gve_ptp.c +10
-0+ +if (IS_ENABLED (CONFIG_X86 )) + system_counterval->cs_id =CSID_X86_TSC ; +else if (IS_ENABLED (CONFIG_ARM64 )) + system_counterval->cs_id =CSID_ARM_ARCH_COUNTER ; +else +return -EOPNOTSUPP ; + +return 0 ; +}--
pw-bot: cr
Jakub Kicinski
maintainerRe: [PATCH net-next v4 3/3] gve: implement PTP gettimex64
Quoted 8 lines · On Tue, 28 Apr 2026 08:49:31 -0700 Jordan Rhee wrote:
Quoted 5 lines
drivers/net/ethernet/google/gve/gve_ptp.c line 109
Quoted 3 lines
drivers/net/ethernet/google/gve/gve_ptp.c +3
-0+static int gve_cycles_to_clock_fn (ktime_t *device_time, +struct system_counterval_t *system_counterval, +void *ctx)Does this do anything GVE specific??
No, however, I would caution against adding an API to the kernel to convert TSC to ktime since this could invite further abuses of TSC.
I don't understand. You caution against doing something you're about to do? In case I didn't ask for it already - please make sure you CC time keeping maintainers on v5.