[PATCH net] ptp: kvm: report paired cycles for the active clocksource
From: Wojtek Wasko
Date: Thu Nov 05 2026 - 09:49:51 EST
The KVM clock-pairing hypercall returns host wall time paired with a
guest TSC value. Cross-timestamping needs that counter expressed in the
guest's active clocksource domain.
ptp_kvm always converts the paired TSC value to kvm-clock units and
reports it as CSID_X86_KVM_CLK, so precise cross-timestamping
(PTP_SYS_OFFSET_PRECISE) fails on guests with TSC clocksource.
Return the paired TSC value directly when TSC cycles are requested and
convert it to kvm-clock units otherwise. If get_device_system_crosststamp()
rejects the counter with -ENODEV and the active clocksource differs from
the requested one, retry once using the active clocksource.
Fixes: a0e136d436de ("PTP: add kvm PTP driver")
Signed-off-by: Wojtek Wasko <wwasko@xxxxxxxxxx>
Reviewed-by: Carolina Jubran <cjubran@xxxxxxxxxx>
---
drivers/ptp/ptp_kvm_common.c | 28 +++++++++++++++++++++++++---
drivers/ptp/ptp_kvm_x86.c | 13 +++++++++++++
2 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/drivers/ptp/ptp_kvm_common.c b/drivers/ptp/ptp_kvm_common.c
index 15ccb7dd2ed0..e11fcf961758 100644
--- a/drivers/ptp/ptp_kvm_common.c
+++ b/drivers/ptp/ptp_kvm_common.c
@@ -23,12 +23,13 @@ struct kvm_ptp_clock {
};
static DEFINE_SPINLOCK(kvm_ptp_lock);
+static enum clocksource_ids kvm_ptp_cs_id = CSID_GENERIC;
static int ptp_kvm_get_time_fn(ktime_t *device_time,
struct system_counterval_t *system_counter,
void *ctx)
{
- enum clocksource_ids cs_id;
+ enum clocksource_ids cs_id = *(enum clocksource_ids *)ctx;
struct timespec64 tspec;
u64 cycle;
int ret;
@@ -58,8 +59,29 @@ static int ptp_kvm_get_time_fn(ktime_t *device_time,
static int ptp_kvm_getcrosststamp(struct ptp_clock_info *ptp,
struct system_device_crosststamp *xtstamp)
{
- return get_device_system_crosststamp(ptp_kvm_get_time_fn, NULL,
- NULL, xtstamp);
+ enum clocksource_ids cs_id = READ_ONCE(kvm_ptp_cs_id);
+ struct system_time_snapshot snapshot;
+ int ret;
+
+ ret = get_device_system_crosststamp(ptp_kvm_get_time_fn, &cs_id, NULL,
+ xtstamp);
+ /*
+ * The paired counter value is not in the units of the current
+ * clocksource. If the clocksource differs from the one this request
+ * asked for, ask for the counter in the new units and retry once.
+ */
+ if (ret == -ENODEV) {
+ ktime_get_snapshot_id(CLOCK_REALTIME, &snapshot);
+ if (snapshot.valid && snapshot.cs_id != cs_id) {
+ cs_id = snapshot.cs_id;
+ WRITE_ONCE(kvm_ptp_cs_id, cs_id);
+ ret = get_device_system_crosststamp(ptp_kvm_get_time_fn,
+ &cs_id, NULL,
+ xtstamp);
+ }
+ }
+
+ return ret;
}
/*
diff --git a/drivers/ptp/ptp_kvm_x86.c b/drivers/ptp/ptp_kvm_x86.c
index 6cea4fe39bcf..4dd55c60d2ce 100644
--- a/drivers/ptp/ptp_kvm_x86.c
+++ b/drivers/ptp/ptp_kvm_x86.c
@@ -92,6 +92,11 @@ int kvm_arch_ptp_get_clock(struct timespec64 *ts)
return 0;
}
+/*
+ * *cs_id selects the clocksource @cycle is reported in: CSID_X86_TSC returns
+ * the paired TSC value as is, anything else converts it to kvm-clock units.
+ * On return, *cs_id holds the clocksource actually used.
+ */
int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
enum clocksource_ids *cs_id)
{
@@ -99,6 +104,14 @@ int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
unsigned int version;
long ret;
+ if (*cs_id == CSID_X86_TSC) {
+ ret = kvm_arch_ptp_get_clock(tspec);
+ if (ret)
+ return ret;
+ *cycle = clock_pair->tsc;
+ return 0;
+ }
+
src = this_cpu_pvti();
do {
base-commit: 57889a8f6d49fc153aa0017d13086ab736567f8a
--
2.43.0