Skip to contentSkip to conversation

Enable ptp_kvm for arm64

RFC PATCH Superseded Jianyong Wu 3 participants 13 messages Sep 26, 2019 – Sep 27, 2019

Subsystems (10)

v4 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v4 cover Enable ptp_kvm for arm64

kvm ptp targets to provide high precision time sync between guest and host in virtualization environment. This patch enable kvm ptp for arm64.
This patch set base on [1][2][3]

change log:
from v3 to v4:
(1) fix clocksource of ptp_kvm to arch_sys_counter.
(2) move kvm_arch_ptp_get_clock_fn into arm_arch_timer.c
(3) subtract cntvoff before return cycles from host.
(4) use ktime_get_snapshot instead of getnstimeofday and
get_current_counterval to return time and counter value.
(5) split ktime and counter into two 32-bit block respectively
to avoid Y2038-safe issue.
(6) set time compensation to device time as half of the delay of hvc call.
(7) add ARM_ARCH_TIMER as dependency of ptp_kvm for
arm64.
from v2 to v3:
(1) fix some issues in commit log.
(2) add some receivers in send list.
from v1 to v2:
(1) move arch-specific code from arch/ to driver/ptp/
(2) offer mechanism to inform userspace if ptp_kvm service is
available.
(3) separate ptp_kvm code for arm64 into hypervisor part and
guest part.
(4) add API to expose monotonic clock and counter value.
(5) refine code: remove no necessary part and reconsitution.

[1]https://git.kernel.org/pub/scm/linux/kernel/git/will/linux.git/ commit/?h=kvm/hvc&id=125ea89e4a21e2fc5235410f966a996a1a7148bf [2]https://git.kernel.org/pub/scm/linux/kernel/git/will/linux.git/ commit/?h=kvm/hvc&id=464f5a1741e5959c3e4d2be1966ae0093b4dce06 [3]https://git.kernel.org/pub/scm/linux/kernel/git/will/linux.git/ commit/?h=kvm/hvc&id=6597490e005d0eeca8ed8c1c1d7b4318ee014681

Jianyong Wu (5):
psci: Export psci_ops.conduit symbol as modules will use it.
ptp: Reorganize ptp_kvm modules to make it arch-independent.
psci: Add hvc call service for ptp_kvm.
ptp: arm64: Enable ptp_kvm for arm64
kvm: arm64: Add capability check extension for ptp_kvm

 drivers/clocksource/arm_arch_timer.c | 28 +++++++++
 drivers/firmware/psci/psci.c         |  6 ++
 drivers/ptp/Kconfig                  |  2 +-
 drivers/ptp/Makefile                 |  1 +
 drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 79 +++++++------------------
 drivers/ptp/ptp_kvm_arm64.c          | 73 +++++++++++++++++++++++
 drivers/ptp/ptp_kvm_x86.c            | 87 ++++++++++++++++++++++++++++
 include/asm-generic/ptp_kvm.h        | 12 ++++
 include/linux/arm-smccc.h            | 14 ++++-
 include/linux/psci.h                 |  1 +
 include/uapi/linux/kvm.h             |  1 +
 virt/kvm/arm/arm.c                   |  1 +
 virt/kvm/arm/psci.c                  | 18 ++++++
 13 files changed, 262 insertions(+), 61 deletions(-)
 rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)
 create mode 100644 drivers/ptp/ptp_kvm_arm64.c
 create mode 100644 drivers/ptp/ptp_kvm_x86.c
 create mode 100644 include/asm-generic/ptp_kvm.h
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH v4 1/5 psci: Export psci_ops.conduit symbol as modules will use it.

If arm_smccc_1_1_invoke used in modules, psci_ops.conduit should be export.

---
 drivers/firmware/psci/psci.c | 6 ++++++
 include/linux/arm-smccc.h    | 2 +-
 include/linux/psci.h         | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/firmware/psci/psci.c +6 -0
index f82ccd39a913..35c4eaab1451 100644
@@ -212,6 +212,12 @@ static unsigned long psci_migrate_info_up_cpu(void)
 			      0, 0, 0);
 }
 
+enum psci_conduit psci_get_conduit(void)
+{
+	return psci_ops.conduit;
+}
+EXPORT_SYMBOL(psci_get_conduit);
+
 static void set_conduit(enum psci_conduit conduit)
 {
 	switch (conduit) {
include/linux/arm-smccc.h +1 -1
index 552cbd49abe8..a6e4d3e3d10a 100644
@@ -357,7 +357,7 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,
  * The return value also provides the conduit that was used.
  */
 #define arm_smccc_1_1_invoke(...) ({					\
-		int method = psci_ops.conduit;				\
+		int method = psci_get_conduit();			\
 		switch (method) {					\
 		case PSCI_CONDUIT_HVC:					\
 			arm_smccc_1_1_hvc(__VA_ARGS__);			\
include/linux/psci.h +1 -0
index a8a15613c157..e5cedc986049 100644
@@ -42,6 +42,7 @@ struct psci_operations {
 	enum smccc_version smccc_version;
 };
 
+extern enum psci_conduit psci_get_conduit(void);
 extern struct psci_operations psci_ops;
 
 #if defined(CONFIG_ARM_PSCI_FW)
Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH v4 2/5 ptp: Reorganize ptp_kvm modules to make it arch-independent.

In reply to Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

---
 drivers/ptp/Makefile                 |  1 +
 drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 77 ++++++------------------
 drivers/ptp/ptp_kvm_x86.c            | 87 ++++++++++++++++++++++++++++
 include/asm-generic/ptp_kvm.h        | 12 ++++
 4 files changed, 118 insertions(+), 59 deletions(-)
 rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)
 create mode 100644 drivers/ptp/ptp_kvm_x86.c
 create mode 100644 include/asm-generic/ptp_kvm.h
drivers/ptp/Makefile +1 -0
index 677d1d178a3e..8f27ba302e31 100644
@@ -4,6 +4,7 @@
 #
 
 ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
+ptp_kvm-y				:= ptp_kvm_$(ARCH).o kvm_ptp.o
 obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
 obj-$(CONFIG_PTP_1588_CLOCK_DTE)	+= ptp_dte.o
 obj-$(CONFIG_PTP_1588_CLOCK_IXP46X)	+= ptp_ixp46x.o
drivers/ptp/kvm_ptp.c renamed from drivers/ptp/ptp_kvm.c +18 -59

124 lines not on this page · raw message

drivers/ptp/ptp_kvm_x86.c new file +87 -0

87 lines not on this page · raw message

include/asm-generic/ptp_kvm.h new file +12 -0
new file mode 100644
index 000000000000..208e842bfa64
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ *  Virtual PTP 1588 clock for use with KVM guests
+ *
+ *  Copyright (C) 2019 ARM Ltd.
+ *  All Rights Reserved
+ */
+
+int kvm_arch_ptp_init(void);
+int kvm_arch_ptp_get_clock(struct timespec64 *ts);
+int kvm_arch_ptp_get_clock_fn(long *cycle,
+		struct timespec64 *tspec, void *cs);
Signature · 1 line
2.17.1
#3 lore raw

Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Jianyong,

Quoted 4 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

Quoted 8 lines
---
  drivers/ptp/Makefile                 |  1 +
  drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 77 ++++++------------------
  drivers/ptp/ptp_kvm_x86.c            | 87 ++++++++++++++++++++++++++++
  include/asm-generic/ptp_kvm.h        | 12 ++++
  4 files changed, 118 insertions(+), 59 deletions(-)

rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)

minor nit: Could we not skip renaming the file ? Given you are following the ptp_kvm_* for the arch specific files and the header files, wouldn't it be good to keep ptp_kvm.c ?

Rest looks fine.

Cheers
Suzuki

#4 lore raw

RE: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Suzuki,

Quoted 28 lines

-----Original Message-----
From: Suzuki K Poulose <[email protected]>
Sent: Thursday, September 26, 2019 9:06 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; Will Deacon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Steve Capper <[email protected]>; Kaly Xin (Arm Technology China) <[email protected]>; Justin He (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Jianyong,

Quoted 9 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

---
  drivers/ptp/Makefile                 |  1 +
  drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 77 ++++++------------------
  drivers/ptp/ptp_kvm_x86.c            | 87
Quoted 9 lines

++++++++++++++++++++++++++++

Quoted 3 lines
  include/asm-generic/ptp_kvm.h        | 12 ++++
  4 files changed, 118 insertions(+), 59 deletions(-)

rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)

minor nit: Could we not skip renaming the file ? Given you are following the ptp_kvm_* for the arch specific files and the header files, wouldn't it be good to keep ptp_kvm.c ?

If the module name ptp_kvm.ko is the same with its dependent object file ptp_kvm.o, warning will be given by compiler, So I change the ptp_kvm.c to kvm_ptp.c to avoid that conflict.

Thanks
Jianyong Wu

Quoted 4 lines

Rest looks fine.

Cheers
Suzuki

#5 lore raw

Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Quoted 35 lines · Jianyong Wu (Arm Technology China)

Hi Suzuki,

Quoted 33 lines

-----Original Message-----
From: Suzuki K Poulose <[email protected]>
Sent: Thursday, September 26, 2019 9:06 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; Will Deacon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Steve Capper <[email protected]>; Kaly Xin (Arm Technology China) <[email protected]>; Justin He (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Jianyong,

Quoted 9 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

---
   drivers/ptp/Makefile                 |  1 +
   drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 77 ++++++------------------
   drivers/ptp/ptp_kvm_x86.c            | 87

++++++++++++++++++++++++++++

Quoted 3 lines
   include/asm-generic/ptp_kvm.h        | 12 ++++
   4 files changed, 118 insertions(+), 59 deletions(-)

rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)

Quoted 6 lines
Quoted 4 lines

minor nit: Could we not skip renaming the file ? Given you are following the ptp_kvm_* for the arch specific files and the header files, wouldn't it be good to keep ptp_kvm.c ?

If the module name ptp_kvm.ko is the same with its dependent object file ptp_kvm.o, warning will be given by compiler, So I change the ptp_kvm.c to kvm_ptp.c to avoid that conflict.

Hmm, ok. How about ptp_kvm_common.c instead of kvm_ptp.c ?

Suzuki

#6 lore raw

RE: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Suzuki,

Quoted 62 lines

-----Original Message-----
From: Suzuki K Poulose <[email protected]>
Sent: Friday, September 27, 2019 6:12 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; Will Deacon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Steve Capper <[email protected]>; Kaly Xin (Arm Technology China) <[email protected]>; Justin He (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Quoted 10 lines · Jianyong Wu (Arm Technology China)

Hi Suzuki,

Quoted 8 lines

-----Original Message-----
From: Suzuki K Poulose <[email protected]>
Sent: Thursday, September 26, 2019 9:06 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>;

Will

Quoted 32 lines
Quoted 32 lines

Deacon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Steve Capper <[email protected]>; Kaly Xin (Arm Technology China) <[email protected]>; Justin He (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Jianyong,

Quoted 9 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

---
   drivers/ptp/Makefile                 |  1 +
   drivers/ptp/{ptp_kvm.c => kvm_ptp.c} | 77 ++++++------------------
   drivers/ptp/ptp_kvm_x86.c            | 87

++++++++++++++++++++++++++++

Quoted 3 lines
   include/asm-generic/ptp_kvm.h        | 12 ++++
   4 files changed, 118 insertions(+), 59 deletions(-)

rename drivers/ptp/{ptp_kvm.c => kvm_ptp.c} (63%)

minor nit: Could we not skip renaming the file ? Given you are following the
ptp_kvm_* for the arch specific files and the header files, wouldn't it be good to keep ptp_kvm.c ?

Quoted 5 lines
Quoted 2 lines

If the module name ptp_kvm.ko is the same with its dependent object file ptp_kvm.o, warning will be given by compiler, So I change the

ptp_kvm.c to kvm_ptp.c to avoid that conflict.

Hmm, ok. How about ptp_kvm_common.c instead of kvm_ptp.c ?

Yeah, it's a better name, I will change it next version.

Thanks
Jianyong Wu

Quoted 2 lines

Suzuki

#7 lore raw

Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

In reply to Jianyong Wu
Quoted 5 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

...

include/asm-generic/ptp_kvm.h line 12

Quoted 13 lines
drivers/ptp/ptp_kvm_x86.c new file +2 -0
+int kvm_arch_ptp_get_clock_fn(unsigned long *cycle, struct timespec64 *tspec,
+			      struct clocksource **cs)
include/asm-generic/ptp_kvm.h new file +2 -0
new file mode 100644
index 000000000000..208e842bfa64
 
+int kvm_arch_ptp_get_clock_fn(long *cycle,
+		struct timespec64 *tspec, void *cs);

Conflicting types for kvm_arch_ptp_get_clock_fn() ?

Suzuki

#8 lore raw

RE: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Hi Suzuki,

Quoted 38 lines

-----Original Message-----
From: Suzuki K Poulose <[email protected]>
Sent: Friday, September 27, 2019 6:23 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; Will Deacon <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Steve Capper <[email protected]>; Kaly Xin (Arm Technology China) <[email protected]>; Justin He (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [RFC PATCH v4 2/5] ptp: Reorganize ptp_kvm modules to make it arch-independent.

Quoted 5 lines · Jianyong Wu

Currently, ptp_kvm modules implementation is only for x86 which includs large part of arch-specific code. This patch move all of those code into new arch related file in the same directory.

...

Quoted 1 line

+int kvm_arch_ptp_get_clock_fn(unsigned long *cycle, struct timespec64

*tspec,

Quoted 8 lines
drivers/ptp/ptp_kvm_x86.c new file +1 -0
+			      struct clocksource **cs)
(quoted diff) +0 -0

b/include/asm-generic/ptp_kvm.h new file mode 100644 index 000000000000..208e842bfa64

include/asm-generic/ptp_kvm.h new file +0 -0
Quoted 6 lines

include/asm-generic/ptp_kvm.h line 12

Quoted 3 lines
include/asm-generic/ptp_kvm.h new file +2 -0
+int kvm_arch_ptp_get_clock_fn(long *cycle,
+		struct timespec64 *tspec, void *cs);

Conflicting types for kvm_arch_ptp_get_clock_fn() ?

Yeah, need fix.

Thanks
Jianyong Wu

Quoted 1 line

Suzuki

#9 lore raw

RFC PATCH v4 3/5 psci: Add hvc call service for ptp_kvm.

In reply to Jianyong Wu

This patch is the base of ptp_kvm for arm64.
ptp_kvm modules will call hvc to get this service.
The service offers real time and counter cycle of host for guest.

---
 include/linux/arm-smccc.h | 12 ++++++++++++
 virt/kvm/arm/psci.c       | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+)
include/linux/arm-smccc.h +12 -0
index a6e4d3e3d10a..bc0cdad10f35 100644
@@ -94,6 +94,7 @@
 
 /* KVM "vendor specific" services */
 #define ARM_SMCCC_KVM_FUNC_FEATURES		0
+#define ARM_SMCCC_KVM_PTP			1
 #define ARM_SMCCC_KVM_FUNC_FEATURES_2		127
 #define ARM_SMCCC_KVM_NUM_FUNCS			128
 
@@ -103,6 +104,17 @@
 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
 			   ARM_SMCCC_KVM_FUNC_FEATURES)
 
+/*
+ * This ID used for virtual ptp kvm clock and it will pass second value
+ * and nanosecond value of host real time and system counter by vcpu
+ * register to guest.
+ */
+#define ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID				\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
+			   ARM_SMCCC_SMC_32,				\
+			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
+			   ARM_SMCCC_KVM_PTP)
+
 #ifndef __ASSEMBLY__
 
 #include <linux/linkage.h>
virt/kvm/arm/psci.c +18 -0
index 0debf49bf259..3f30fc42a5ca 100644
@@ -392,6 +392,8 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
 	u32 func_id = smccc_get_function(vcpu);
 	u32 val[4] = {};
 	u32 option;
+	u64 cycles;
+	struct system_time_snapshot systime_snapshot;
 
 	val[0] = SMCCC_RET_NOT_SUPPORTED;
 
@@ -431,6 +433,22 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
 	case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID:
 		val[0] = BIT(ARM_SMCCC_KVM_FUNC_FEATURES);
 		break;
+	/*
+	 * This will used for virtual ptp kvm clock. three
+	 * values will be passed back.
+	 * reg0 stores high 32-bit host ktime;
+	 * reg1 stores low 32-bit host ktime;
+	 * reg2 stores high 32-bit difference of host cycles and cntvoff;
+	 * reg3 stores low 32-bit difference of host cycles and cntvoff.
+	 */
+	case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID:
+		ktime_get_snapshot(&systime_snapshot);
+		val[0] = systime_snapshot.real >> 32;
+		val[1] = systime_snapshot.real << 32 >> 32;
+		cycles = systime_snapshot.cycles - vcpu_vtimer(vcpu)->cntvoff;
+		val[2] = cycles >> 32;
+		val[3] = cycles << 32 >> 32;
+		break;
 	default:
 		return kvm_psci_call(vcpu);
 	}
Signature · 1 line
2.17.1
#10 lore raw

Re: [RFC PATCH v4 3/5] psci: Add hvc call service for ptp_kvm.

Hello!

Quoted 9 lines · Jianyong Wu

This patch is the base of ptp_kvm for arm64.
ptp_kvm modules will call hvc to get this service.
The service offers real time and counter cycle of host for guest.

---
  include/linux/arm-smccc.h | 12 ++++++++++++
  virt/kvm/arm/psci.c       | 18 ++++++++++++++++++
  2 files changed, 30 insertions(+)

[...]

Quoted 4 lines
virt/kvm/arm/psci.c +0 -0
index 0debf49bf259..3f30fc42a5ca 100644

[...]

virt/kvm/arm/psci.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 437

Quoted 6 lines
virt/kvm/arm/psci.c +2 -0
@@ -431,6 +433,22 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
  	case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID:
  		val[0] = BIT(ARM_SMCCC_KVM_FUNC_FEATURES);
  		break;
+	/*
+	 * This will used for virtual ptp kvm clock. three

^ be? ^ T

[...]

MBR, Sergei

#11 lore raw

RFC PATCH v4 4/5 ptp: arm64: Enable ptp_kvm for arm64

In reply to Jianyong Wu

Currently in arm64 virtualization environment, there is no mechanism to keep time sync between guest and host. Time in guest will drift compared with host after boot up as they may both use third party time sources to correct their time respectively. The time deviation will be in order of milliseconds but some scenarios ask for higher time precision, like in cloud envirenment, we want all the VMs running in the host aquire the same level accuracy from host clock.

Use of kvm ptp clock, which choose the host clock source clock as a reference clock to sync time clock between guest and host has been adopted by x86 which makes the time sync order from milliseconds to nanoseconds.

This patch enable kvm ptp on arm64 and we get the similar clock drift as found with x86 with kvm ptp.

Test result comparison between with kvm ptp and without it in arm64 are as follows. This test derived from the result of command 'chronyc sources'. we should take more cure of the last sample column which shows the offset between the local clock and the source at the last measurement.

no kvm ptp in guest:
MS Name/IP address Stratum Poll Reach LastRx Last sample
========================================================================

^* dns1.synet.edu.cn      2   6   377    13  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    21  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    29  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    37  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    45  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    53  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    61  +1040us[+1581us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377     4   -130us[ +796us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    12   -130us[ +796us] +/-   21ms
^* dns1.synet.edu.cn      2   6   377    20   -130us[ +796us] +/-   21ms

in host:
MS Name/IP address Stratum Poll Reach LastRx Last sample
========================================================================

^* 120.25.115.20          2   7   377    72   -470us[ -603us] +/-   18ms
^* 120.25.115.20          2   7   377    92   -470us[ -603us] +/-   18ms
^* 120.25.115.20          2   7   377   112   -470us[ -603us] +/-   18ms
^* 120.25.115.20          2   7   377     2   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377    22   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377    43   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377    63   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377    83   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377   103   +872ns[-6808ns] +/-   17ms
^* 120.25.115.20          2   7   377   123   +872ns[-6808ns] +/-   17ms

The dns1.synet.edu.cn is the network reference clock for guest and 120.25.115.20 is the network reference clock for host. we can't get the clock error between guest and host directly, but a roughly estimated value will be in order of hundreds of us to ms.

with kvm ptp in guest:
chrony has been disabled in host to remove the disturb by network clock.

MS Name/IP address Stratum Poll Reach LastRx Last sample
========================================================================

* PHC0                    0   3   377     8     -7ns[   +1ns] +/-    3ns
* PHC0                    0   3   377     8     +1ns[  +16ns] +/-    3ns
* PHC0                    0   3   377     6     -4ns[   -0ns] +/-    6ns
* PHC0                    0   3   377     6     -8ns[  -12ns] +/-    5ns
* PHC0                    0   3   377     5     +2ns[   +4ns] +/-    4ns
* PHC0                    0   3   377    13     +2ns[   +4ns] +/-    4ns
* PHC0                    0   3   377    12     -4ns[   -6ns] +/-    4ns
* PHC0                    0   3   377    11     -8ns[  -11ns] +/-    6ns
* PHC0                    0   3   377    10    -14ns[  -20ns] +/-    4ns
* PHC0                    0   3   377     8     +4ns[   +5ns] +/-    4ns

The PHC0 is the ptp clock which choose the host clock as its source clock. So we can be sure to say that the clock error between host and guest is in order of ns.

---
 drivers/clocksource/arm_arch_timer.c | 28 +++++++++++
 drivers/ptp/Kconfig                  |  2 +-
 drivers/ptp/kvm_ptp.c                |  4 +-
 drivers/ptp/ptp_kvm_arm64.c          | 73 ++++++++++++++++++++++++++++
 include/asm-generic/ptp_kvm.h        |  2 +-
 5 files changed, 106 insertions(+), 3 deletions(-)
 create mode 100644 drivers/ptp/ptp_kvm_arm64.c
drivers/clocksource/arm_arch_timer.c +28 -0
index 07e57a49d1e8..7a99673e4149 100644
@@ -1633,4 +1633,32 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *table)
 	return arch_timer_common_init();
 }
 TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
+
+#if IS_ENABLED(CONFIG_PTP_1588_CLOCK_KVM)
+#include <linux/arm-smccc.h>
+int kvm_arch_ptp_get_clock_fn(long *cycle, struct timespec64 *ts,
+			      struct clocksource **cs)
+{
+	u64 t1, t2;
+	struct arm_smccc_res hvc_res;
+	ktime_t ktime_overall;
+
+	t1 = sched_clock();
+	arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID, &hvc_res);
+	t2 = sched_clock();
+	t2 -= t1;
+	t2 /= 2;
+	if ((long)(hvc_res.a0) < 0)
+		return -EOPNOTSUPP;
+
+	ktime_overall = hvc_res.a0 << 32 | hvc_res.a1;
+	*ts = ktime_to_timespec64(ktime_overall);
+	timespec64_add_ns(ts, t2);
+	*cycle = hvc_res.a2 << 32 | hvc_res.a3;
+	*cs = &clocksource_counter;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_clock_fn);
+#endif
 #endif
drivers/ptp/Kconfig +1 -1
index 9b8fee5178e8..3c31ff8eb05f 100644
@@ -110,7 +110,7 @@ config PTP_1588_CLOCK_PCH
 config PTP_1588_CLOCK_KVM
 	tristate "KVM virtual PTP clock"
 	depends on PTP_1588_CLOCK
-	depends on KVM_GUEST && X86
+	depends on KVM_GUEST && X86 || ARM64 && ARM_ARCH_TIMER
 	default y
 	help
 	  This driver adds support for using kvm infrastructure as a PTP
drivers/ptp/kvm_ptp.c +3 -1
index d8f215186904..b467e1674a71 100644
@@ -22,6 +22,8 @@ struct kvm_ptp_clock {
 	struct ptp_clock_info caps;
 };
 
+//extern int kvm_arch_ptp_get_clock_fn(long *cycle, struct timespec64 *ts,
+//			      struct clocksource **cs)
 DEFINE_SPINLOCK(kvm_ptp_lock);
 
 static int ptp_kvm_get_time_fn(ktime_t *device_time,
@@ -138,7 +140,7 @@ static int __init ptp_kvm_init(void)
 	int ret;
 
 	ret = kvm_arch_ptp_init();
-	if (!ret)
+	if (ret)
 		return -EOPNOTSUPP;
 
 	kvm_ptp_clock.caps = ptp_kvm_caps;
drivers/ptp/ptp_kvm_arm64.c new file +73 -0

73 lines not on this page · raw message

include/asm-generic/ptp_kvm.h +1 -1
index 208e842bfa64..829d76bdb905 100644
@@ -8,5 +8,5 @@
 
 int kvm_arch_ptp_init(void);
 int kvm_arch_ptp_get_clock(struct timespec64 *ts);
-int kvm_arch_ptp_get_clock_fn(long *cycle,
+int kvm_arch_ptp_get_clock_fn(unsigned long *cycle,
 		struct timespec64 *tspec, void *cs);
Signature · 1 line
2.17.1
#12 lore raw

RFC PATCH v4 5/5 kvm: arm64: Add capability check extension for ptp_kvm

In reply to Jianyong Wu

Let userspace check if there is kvm ptp service in host. before VMs migrate to a another host, VMM may check if this cap is available to determine the migration behaviour.

---
 include/uapi/linux/kvm.h | 1 +
 virt/kvm/arm/arm.c       | 1 +
 2 files changed, 2 insertions(+)
include/uapi/linux/kvm.h +1 -0
index 2fe12b40d503..a0bff6002bd9 100644
@@ -993,6 +993,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ARM_SVE 170
 #define KVM_CAP_ARM_PTRAUTH_ADDRESS 171
 #define KVM_CAP_ARM_PTRAUTH_GENERIC 172
+#define KVM_CAP_ARM_KVM_PTP 173
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/arm/arm.c +1 -0
index bd5c55916d0d..80999985160b 100644
@@ -201,6 +201,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_MP_STATE:
 	case KVM_CAP_IMMEDIATE_EXIT:
 	case KVM_CAP_VCPU_EVENTS:
+	case KVM_CAP_ARM_KVM_PTP:
 		r = 1;
 		break;
 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
Signature · 1 line
2.17.1