From: Wojtek Wasko <wwasko@nvidia.com>
To: netdev@vger.kernel.org
Cc: richardcochran@gmail.com, andrew+netdev@lunn.ch,
davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, mtosatti@redhat.com, pbonzini@redhat.com,
cjubran@nvidia.com, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, Wojtek Wasko <wwasko@nvidia.com>
Subject: [PATCH net] ptp: kvm: report paired cycles for the active clocksource
Date: Thu, 5 Nov 2026 15:48:21 +0100 [thread overview]
Message-ID: <20261105144821.5299-1-wwasko@nvidia.com> (raw)
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@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
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
next reply other threads:[~2026-11-05 14:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 14:48 Wojtek Wasko [this message]
2026-11-05 14:54 ` [PATCH net] ptp: kvm: report paired cycles for the active clocksource netdev-bot+sinfo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261105144821.5299-1-wwasko@nvidia.com \
--to=wwasko@nvidia.com \
--cc=andrew+netdev@lunn.ch \
--cc=cjubran@nvidia.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mtosatti@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pbonzini@redhat.com \
--cc=richardcochran@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox