Skip to contentSkip to conversation

Enable ptp_kvm for arm64

RFC PATCH Superseded Jianyong Wu 2 participants 13 messages Oct 24, 2019 – Nov 8, 2019

Subsystems (11)

v6 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v6 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 v5 to v6:
(1) apply Mark's patch[4] to get SMCCC conduit.
(2) add mechanism to recognize current clocksource by add
clocksouce_id value into struct clocksource instead of method in patch-v5.
(3) rename kvm_arch_ptp_get_clock_fn into
kvm_arch_ptp_get_crosststamp.

from v4 to v5:
(1) remove hvc delay compensasion as it should leave to userspace.
(2) check current clocksource in hvc call service.
(3) expose current clocksource by adding it to
system_time_snapshot.
(4) add helper to check if clocksource is arm_arch_counter.
(5) rename kvm_ptp.c to ptp_kvm_common.c

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 [4]https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/ commit/?h=for-next/smccc-conduit-cleanup&id=6b7fe77c334ae59fed9500140e08f4f896b36871

Jianyong Wu (6):
psci: let arm_smccc_1_1_invoke available by modules
ptp: Reorganize ptp_kvm modules to make it arch-independent.
time: Add mechanism to recognize clocksource in time_get_snapshot
psci: Add hvc call service for ptp_kvm.
ptp: arm64: Enable ptp_kvm for arm64
kvm: arm64: Add capability check extension for ptp_kvm

Mark Rutland (1):
arm/arm64: smccc/psci: add arm_smccc_1_1_get_conduit()

 drivers/clocksource/arm_arch_timer.c        | 24 ++++++
 drivers/firmware/psci/psci.c                | 16 ++++
 drivers/ptp/Kconfig                         |  2 +-
 drivers/ptp/Makefile                        |  1 +
 drivers/ptp/ptp_kvm_arm64.c                 | 53 +++++++++++++
 drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} | 77 +++++-------------
 drivers/ptp/ptp_kvm_x86.c                   | 87 +++++++++++++++++++++
 include/asm-generic/ptp_kvm.h               | 12 +++
 include/clocksource/arm_arch_timer.h        |  4 +
 include/linux/arm-smccc.h                   | 30 ++++++-
 include/linux/clocksource.h                 |  6 ++
 include/linux/timekeeping.h                 | 12 +--
 include/uapi/linux/kvm.h                    |  1 +
 kernel/time/clocksource.c                   |  3 +
 kernel/time/timekeeping.c                   |  1 +
 virt/kvm/arm/arm.c                          |  1 +
 virt/kvm/arm/psci.c                         | 22 ++++++
 17 files changed, 286 insertions(+), 66 deletions(-)
 create mode 100644 drivers/ptp/ptp_kvm_arm64.c
 rename drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} (63%)
 create mode 100644 drivers/ptp/ptp_kvm_x86.c
 create mode 100644 include/asm-generic/ptp_kvm.h
Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

RFC PATCH v6 1/7 arm/arm64: smccc/psci: add arm_smccc_1_1_get_conduit()

From: Mark Rutland <[email protected]>

SMCCC callers are currently amassing a collection of enums for the SMCCC conduit, and are having to dig into the PSCI driver's internals in order to figure out what to do.

Let's clean this up, with common SMCCC_CONDUIT_* definitions, and an arm_smccc_1_1_get_conduit() helper that abstracts the PSCI driver's internal state.

We can kill off the PSCI_CONDUIT_* definitions once we've migrated users over to the new interface.

---
 drivers/firmware/psci/psci.c | 15 +++++++++++++++
 include/linux/arm-smccc.h    | 16 ++++++++++++++++
 2 files changed, 31 insertions(+)
drivers/firmware/psci/psci.c +15 -0
index f82ccd39a913..5f31f1bea1af 100644
@@ -57,6 +57,21 @@ struct psci_operations psci_ops = {
 	.smccc_version = SMCCC_VERSION_1_0,
 };
 
+enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void)
+{
+	if (psci_ops.smccc_version < SMCCC_VERSION_1_1)
+		return SMCCC_CONDUIT_NONE;
+
+	switch (psci_ops.conduit) {
+	case PSCI_CONDUIT_SMC:
+		return SMCCC_CONDUIT_SMC;
+	case PSCI_CONDUIT_HVC:
+		return SMCCC_CONDUIT_HVC;
+	default:
+		return SMCCC_CONDUIT_NONE;
+	}
+}
+
 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
 				unsigned long, unsigned long);
 static psci_fn *invoke_psci_fn;
include/linux/arm-smccc.h +16 -0
index 552cbd49abe8..701fa1da6b4a 100644
@@ -107,6 +107,22 @@
 
 #include <linux/linkage.h>
 #include <linux/types.h>
+
+enum arm_smccc_conduit {
+	SMCCC_CONDUIT_NONE,
+	SMCCC_CONDUIT_SMC,
+	SMCCC_CONDUIT_HVC,
+};
+
+/**
+ * arm_smccc_1_1_get_conduit()
+ *
+ * Returns the conduit to be used for SMCCCv1.1 or later.
+ *
+ * When SMCCCv1.1 is not present, returns SMCCC_CONDUIT_NONE.
+ */
+enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void);
+
 /**
  * struct arm_smccc_res - Result from SMC/HVC call
  * @a0-a3 result values from registers 0 to 3
Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#2 lore raw

RFC PATCH v6 2/7 psci: Let arm_smccc_1_1_invoke available by modules

In reply to Jianyong Wu

Export arm_smccc_1_1_get_conduit and apply it to get conduit in arm_smccc_1_1_invoke, then modules can use this arm_smccc_1_1_invoke.

---
 drivers/firmware/psci/psci.c | 1 +
 include/linux/arm-smccc.h    | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/psci/psci.c +1 -0
index 5f31f1bea1af..cf509171422c 100644
@@ -71,6 +71,7 @@ enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void)
 		return SMCCC_CONDUIT_NONE;
 	}
 }
+EXPORT_SYMBOL(arm_smccc_1_1_get_conduit);
 
 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
 				unsigned long, unsigned long);
include/linux/arm-smccc.h +1 -1
index 701fa1da6b4a..6f82c87308ed 100644
@@ -373,7 +373,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 = arm_smccc_1_1_get_conduit();		\
 		switch (method) {					\
 		case PSCI_CONDUIT_HVC:					\
 			arm_smccc_1_1_hvc(__VA_ARGS__);			\
Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#3 lore raw

RFC PATCH v6 3/7 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 => ptp_kvm_common.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 => ptp_kvm_common.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..3b7554f56ad9 100644
@@ -4,6 +4,7 @@
 #
 
 ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
+ptp_kvm-y				:= ptp_kvm_$(ARCH).o ptp_kvm_common.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/ptp_kvm_common.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..e5dd386f6664
@@ -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_crosststamp(unsigned long *cycle,
+		struct timespec64 *tspec, void *cs);
Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#4 lore raw

RFC PATCH v6 4/7 time: Add mechanism to recognize clocksource in time_get_snapshot

In reply to Jianyong Wu

From: Thomas Gleixner <[email protected]>
In some scenario like return device time to ptp_kvm guest, we need identify the current clocksource outside core time code. This patch add a mechanism to recognize the current clocksource by export clocksource id in time_get_snapshot.

---
 include/linux/clocksource.h |  6 ++++++
 include/linux/timekeeping.h | 12 +++++++-----
 kernel/time/clocksource.c   |  3 +++
 kernel/time/timekeeping.c   |  1 +
 4 files changed, 17 insertions(+), 5 deletions(-)
include/linux/clocksource.h +6 -0
index b21db536fd52..ac8016b22734 100644
@@ -19,6 +19,7 @@
 #include <linux/of.h>
 #include <asm/div64.h>
 #include <asm/io.h>
+#include <linux/clocksource_ids.h>
 
 struct clocksource;
 struct module;
@@ -49,6 +50,10 @@ struct module;
  *			400-499: Perfect
  *				The ideal clocksource. A must-use where
  *				available.
+ * @id:			Defaults to CSID_GENERIC. The id value is captured
+ *			in certain snapshot functions to allow callers to
+ *			validate the clocksource from which the snapshot was
+ *			taken.
  * @read:		returns a cycle value, passes clocksource as argument
  * @enable:		optional function to enable the clocksource
  * @disable:		optional function to disable the clocksource
@@ -91,6 +96,7 @@ struct clocksource {
 	const char *name;
 	struct list_head list;
 	int rating;
+	enum clocksource_ids id;
 	int (*enable)(struct clocksource *cs);
 	void (*disable)(struct clocksource *cs);
 	unsigned long flags;
include/linux/timekeeping.h +7 -5
index a8ab0f143ac4..ecce56269a7e 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_TIMEKEEPING_H
 #define _LINUX_TIMEKEEPING_H
 
+#include <linux/clocksource_ids.h>
 #include <linux/errno.h>
 
 /* Included from linux/ktime.h */
@@ -204,11 +205,12 @@ extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
  * @cs_was_changed_seq:	The sequence number of clocksource change events
  */
 struct system_time_snapshot {
-	u64		cycles;
-	ktime_t		real;
-	ktime_t		raw;
-	unsigned int	clock_was_set_seq;
-	u8		cs_was_changed_seq;
+	u64			cycles;
+	ktime_t			real;
+	ktime_t			raw;
+	enum clocksource_ids	cs_id;
+	unsigned int		clock_was_set_seq;
+	u8			cs_was_changed_seq;
 };
 
 /*
kernel/time/clocksource.c +3 -0
index 3bcc19ceb073..26a3add61771 100644
@@ -921,6 +921,9 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
 
 	clocksource_arch_init(cs);
 
+	if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX))
+		cs->id = CSID_GENERIC;
+
 	/* Initialize mult/shift and max_idle_ns */
 	__clocksource_update_freq_scale(cs, scale, freq);
 
kernel/time/timekeeping.c +1 -0
index 44b726bab4bd..88078cff7fe2 100644
@@ -974,6 +974,7 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
 		now = tk_clock_read(&tk->tkr_mono);
+		systime_snapshot->cs_id = tk->tkr_mono.clock->id;
 		systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
 		systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
 		base_real = ktime_add(tk->tkr_mono.base,
Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#5 lore raw

Re: [RFC PATCH v6 4/7] time: Add mechanism to recognize clocksource in time_get_snapshot

Quoted 7 lines · Jianyong Wu

From: Thomas Gleixner <[email protected]>

In some scenario like return device time to ptp_kvm guest, we need identify the current clocksource outside core time code.

This patch add a mechanism to recognize the current clocksource by export clocksource id in time_get_snapshot.

Please check Documentation/process/submitting-patches.rst and search for 'This patch'.

include/linux/clocksource.h line 22

Quoted 9 lines
include/linux/clocksource.h +1 -0
index b21db536fd52..ac8016b22734 100644
@@ -19,6 +19,7 @@
 #include <linux/of.h>
 #include <asm/div64.h>
 #include <asm/io.h>
+#include <linux/clocksource_ids.h>

Please place that include to the other linux includes. You might notice that there is ordering here.

But where is that include? It's not part of that series, so how is this supposed to compile?

Thanks,

tglx

#6 lore raw

RE: [RFC PATCH v6 4/7] time: Add mechanism to recognize clocksource in time_get_snapshot

Hi tglx,

Quoted 19 lines

-----Original Message-----
From: Thomas Gleixner <[email protected]>
Sent: Thursday, November 7, 2019 3:56 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; [email protected]; Suzuki Poulose <[email protected]>; linux- [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 v6 4/7] time: Add mechanism to recognize clocksource in time_get_snapshot

Quoted 2 lines · Jianyong Wu

From: Thomas Gleixner <[email protected]>

Quoted 9 lines
Quoted 5 lines

In some scenario like return device time to ptp_kvm guest, we need identify the current clocksource outside core time code.

This patch add a mechanism to recognize the current clocksource by export clocksource id in time_get_snapshot.

Please check Documentation/process/submitting-patches.rst and search for 'This patch'.

Yeah, the expression should be more impersonal.

Quoted 4 lines
Quoted 4 lines
include/linux/clocksource.h +0 -0
index b21db536fd52..ac8016b22734 100644
Quoted 9 lines

include/linux/clocksource.h line 22

Quoted 5 lines
include/linux/clocksource.h +1 -0
@@ -19,6 +19,7 @@
 #include <linux/of.h>
 #include <asm/div64.h>
 #include <asm/io.h>
+#include <linux/clocksource_ids.h>

Please place that include to the other linux includes. You might notice that there is ordering here.

Ok,

Quoted 2 lines

But where is that include? It's not part of that series, so how is this supposed to compile?

OMG! Sorry, I added this file when I prepared my patches but missed to include it into this patch! This must be avoid latter.

Thanks
Jianyong

Quoted 4 lines

Thanks,

tglx

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#7 lore raw

RFC PATCH v6 5/7 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.

---
 drivers/clocksource/arm_arch_timer.c |  2 ++
 include/clocksource/arm_arch_timer.h |  4 ++++
 include/linux/arm-smccc.h            | 12 ++++++++++++
 virt/kvm/arm/psci.c                  | 22 ++++++++++++++++++++++
 4 files changed, 40 insertions(+)
drivers/clocksource/arm_arch_timer.c +2 -0
index 07e57a49d1e8..e4ad38042ef6 100644
@@ -29,6 +29,7 @@
 #include <asm/virt.h>
 
 #include <clocksource/arm_arch_timer.h>
+#include <linux/clocksource_ids.h>
 
 #define CNTTIDR		0x08
 #define CNTTIDR_VIRT(n)	(BIT(1) << ((n) * 4))
@@ -188,6 +189,7 @@ static u64 arch_counter_read_cc(const struct cyclecounter *cc)
 static struct clocksource clocksource_counter = {
 	.name	= "arch_sys_counter",
 	.rating	= 400,
+	.id	= CSID_ARM_ARCH_COUNTER,
 	.read	= arch_counter_read,
 	.mask	= CLOCKSOURCE_MASK(56),
 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
include/clocksource/arm_arch_timer.h +4 -0
index 1d68d5613dae..426d749e8cf8 100644
@@ -104,6 +104,10 @@ static inline bool arch_timer_evtstrm_available(void)
 	return false;
 }
 
+bool is_arm_arch_counter(void *unuse)
+{
+	return false;
+}
 #endif
 
 #endif
include/linux/arm-smccc.h +12 -0
index 6f82c87308ed..aafb6bac167d 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 +22 -0
index 0debf49bf259..339bcbafac7b 100644
@@ -15,6 +15,7 @@
 #include <asm/kvm_host.h>
 
 #include <kvm/arm_psci.h>
+#include <linux/clocksource_ids.h>
 
 /*
  * This is an implementation of the Power State Coordination Interface
@@ -392,6 +393,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 +434,25 @@ 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);
+		if (systime_snapshot.cs_id != CSID_ARM_ARCH_COUNTER)
+			return kvm_psci_call(vcpu);
+		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 · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#8 lore raw

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

Quoted 1 line · Jianyong Wu

This patch is the base of ptp_kvm for arm64.

This patch ...

Quoted 16 lines

ptp_kvm modules will call hvc to get this service.
The service offers real time and counter cycle of host for guest.

---
 drivers/clocksource/arm_arch_timer.c |  2 ++
 include/clocksource/arm_arch_timer.h |  4 ++++
 include/linux/arm-smccc.h            | 12 ++++++++++++
 virt/kvm/arm/psci.c                  | 22 ++++++++++++++++++++++
 4 files changed, 40 insertions(+)
drivers/clocksource/arm_arch_timer.c +0 -0
index 07e57a49d1e8..e4ad38042ef6 100644

drivers/clocksource/arm_arch_timer.c line 32

Quoted 4 lines
drivers/clocksource/arm_arch_timer.c +1 -0
@@ -29,6 +29,7 @@
 #include <asm/virt.h>
 
 #include <clocksource/arm_arch_timer.h>
+#include <linux/clocksource_ids.h>

Same ordering issue and lack of file.

include/clocksource/arm_arch_timer.h static inline bool arch_timer_evtstrm_available(void) · line 107

Quoted 9 lines
include/clocksource/arm_arch_timer.h +1 -0
index 1d68d5613dae..426d749e8cf8 100644
@@ -104,6 +104,10 @@ static inline bool arch_timer_evtstrm_available(void)
 	return false;
 }
 
+bool is_arm_arch_counter(void *unuse)

A global function in a header file? You might want to make this static inline. And while at it please s/unuse/unused/

Quoted 10 lines
include/clocksource/arm_arch_timer.h +3 -0
+{
+	return false;
+}
 #endif
 #include <linux/linkage.h>
virt/kvm/arm/psci.c +0 -0
index 0debf49bf259..339bcbafac7b 100644

virt/kvm/arm/psci.c line 18

Quoted 4 lines
virt/kvm/arm/psci.c +1 -0
@@ -15,6 +15,7 @@
 #include <asm/kvm_host.h>
 
 #include <kvm/arm_psci.h>
+#include <linux/clocksource_ids.h>

Sigh.

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

Quoted 8 lines
virt/kvm/arm/psci.c +2 -0
 /*
  * This is an implementation of the Power State Coordination Interface
@@ -392,6 +393,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;

Also here you might notice that the variables are not randomly ordered.

Thanks,

tglx

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#9 lore raw

RE: [RFC PATCH v6 5/7] psci: Add hvc call service for ptp_kvm.

Hi tglx,

Quoted 15 lines

-----Original Message-----
From: Thomas Gleixner <[email protected]>
Sent: Thursday, November 7, 2019 4:01 PM
To: Jianyong Wu (Arm Technology China) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; [email protected]; Suzuki Poulose <[email protected]>; linux- [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 v6 5/7] psci: Add hvc call service for ptp_kvm.

Quoted 6 lines
Quoted 1 line · Jianyong Wu

This patch is the base of ptp_kvm for arm64.

This patch ...

Yeah, avoid subjective expression.

Quoted 19 lines
Quoted 19 lines

ptp_kvm modules will call hvc to get this service.
The service offers real time and counter cycle of host for guest.

---
 drivers/clocksource/arm_arch_timer.c |  2 ++
include/clocksource/arm_arch_timer.h |  4 ++++
 include/linux/arm-smccc.h            | 12 ++++++++++++
 virt/kvm/arm/psci.c                  | 22 ++++++++++++++++++++++
 4 files changed, 40 insertions(+)
drivers/clocksource/arm_arch_timer.c +0 -0
index 07e57a49d1e8..e4ad38042ef6 100644
@@ -29,6 +29,7 @@
 #include <asm/virt.h>
 
Quoted 5 lines

drivers/clocksource/arm_arch_timer.c line 32

Quoted 2 lines
drivers/clocksource/arm_arch_timer.c +1 -0
 #include <clocksource/arm_arch_timer.h>
+#include <linux/clocksource_ids.h>

Same ordering issue and lack of file.

OK,

Quoted 7 lines

include/clocksource/arm_arch_timer.h static inline bool

Quoted 6 lines
include/clocksource/arm_arch_timer.h +0 -0
index 1d68d5613dae..426d749e8cf8 100644
@@ -104,6 +104,10 @@ static inline bool

arch_timer_evtstrm_available(void)

Quoted 8 lines

include/clocksource/arm_arch_timer.h static inline bool arch_timer_evtstrm_available(void) · line 107

Quoted 4 lines
include/clocksource/arm_arch_timer.h +1 -0
 	return false;
 }
 
+bool is_arm_arch_counter(void *unuse)

A global function in a header file? You might want to make this static inline. And while at it please s/unuse/unused/

Should remove this residue line from v5 in v6.

Quoted 12 lines
Quoted 12 lines
include/clocksource/arm_arch_timer.h +3 -0
+{
+	return false;
+}
 #endif
 #include <linux/linkage.h>
virt/kvm/arm/psci.c index renamed from virt/kvm/arm/psci.c +0 -0

0debf49bf259..339bcbafac7b 100644

virt/kvm/arm/psci.c +0 -0
@@ -15,6 +15,7 @@
 #include <asm/kvm_host.h>
 
Quoted 5 lines

virt/kvm/arm/psci.c line 18

Quoted 2 lines
virt/kvm/arm/psci.c +1 -0
 #include <kvm/arm_psci.h>
+#include <linux/clocksource_ids.h>

Sigh.

Yeah,

Quoted 12 lines
Quoted 3 lines
virt/kvm/arm/psci.c +0 -0
 /*
  * This is an implementation of the Power State Coordination

Interface @@ -392,6 +393,8 @@ int kvm_hvc_call_handler(struct

kvm_vcpu *vcpu)

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

Quoted 5 lines
virt/kvm/arm/psci.c +2 -0
 	u32 func_id = smccc_get_function(vcpu);
 	u32 val[4] = {};
 	u32 option;
+	u64 cycles;
+	struct system_time_snapshot systime_snapshot;

Also here you might notice that the variables are not randomly ordered.

Do you mean considering the alignment then put "struct system_time_snapshot systime_snapshot" as the top one and u64 cycles as the second?

Thanks
Jianyong

Quoted 3 lines

Thanks,

tglx

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm

#10 lore raw

RFC PATCH v6 6/7 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 | 22 ++++++++++++
 drivers/ptp/Kconfig                  |  2 +-
 drivers/ptp/ptp_kvm_arm64.c          | 54 ++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 drivers/ptp/ptp_kvm_arm64.c
drivers/clocksource/arm_arch_timer.c +22 -0
index e4ad38042ef6..fe0ed8acfa33 100644
@@ -1636,3 +1636,25 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *table)
 }
 TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
 #endif
+
+#if IS_ENABLED(CONFIG_PTP_1588_CLOCK_KVM)
+#include <linux/arm-smccc.h>
+int kvm_arch_ptp_get_clock_fn(unsigned long *cycle, struct timespec64 *ts,
+			      struct clocksource **cs)
+{
+	struct arm_smccc_res hvc_res;
+	ktime_t ktime_overall;
+
+	arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID, &hvc_res);
+	if ((long)(hvc_res.a0) < 0)
+		return -EOPNOTSUPP;
+
+	ktime_overall = hvc_res.a0 << 32 | hvc_res.a1;
+	*ts = ktime_to_timespec64(ktime_overall);
+	*cycle = hvc_res.a2 << 32 | hvc_res.a3;
+	*cs = &clocksource_counter;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_clock_fn);
+#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/ptp_kvm_arm64.c new file +54 -0

54 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#11 lore raw

RFC PATCH v6 7/7 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.

---
 drivers/clocksource/arm_arch_timer.c | 4 ++--
 drivers/ptp/ptp_kvm_arm64.c          | 1 -
 include/uapi/linux/kvm.h             | 1 +
 virt/kvm/arm/arm.c                   | 1 +
 4 files changed, 4 insertions(+), 3 deletions(-)
drivers/clocksource/arm_arch_timer.c +2 -2
index fe0ed8acfa33..0ae673193bee 100644
@@ -1639,7 +1639,7 @@ 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(unsigned long *cycle, struct timespec64 *ts,
+int kvm_arch_ptp_get_crosststamp(unsigned long *cycle, struct timespec64 *ts,
 			      struct clocksource **cs)
 {
 	struct arm_smccc_res hvc_res;
@@ -1656,5 +1656,5 @@ int kvm_arch_ptp_get_clock_fn(unsigned long *cycle, struct timespec64 *ts,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_clock_fn);
+EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_crosststamp);
 #endif
drivers/ptp/ptp_kvm_arm64.c +0 -1
index 7c697f20904f..f3f957117865 100644
@@ -30,7 +30,6 @@ int kvm_arch_ptp_init(void)
 int kvm_arch_ptp_get_clock_generic(struct timespec64 *ts,
 				   struct arm_smccc_res *hvc_res)
 {
-	u64 ns;
 	ktime_t ktime_overall;
 
 	arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID,
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 · 6 lines
2.17.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#12 lore raw
In reply to Jianyong Wu

Hi all,

Any comments?

Thanks
Jianyong

Quoted 103 lines

-----Original Message-----
From: Jianyong Wu <[email protected]>
Sent: Thursday, October 24, 2019 7:02 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Mark Rutland <[email protected]>; [email protected]; Suzuki Poulose <[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]>; Jianyong Wu (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: [RFC PATCH v6 0/7] 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 v5 to v6:
(1) apply Mark's patch[4] to get SMCCC conduit.
(2) add mechanism to recognize current clocksource by add
clocksouce_id value into struct clocksource instead of method in patch-v5.
(3) rename kvm_arch_ptp_get_clock_fn into
kvm_arch_ptp_get_crosststamp.

from v4 to v5:
(1) remove hvc delay compensasion as it should leave to userspace.
(2) check current clocksource in hvc call service.
(3) expose current clocksource by adding it to system_time_snapshot.
(4) add helper to check if clocksource is arm_arch_counter.
(5) rename kvm_ptp.c to ptp_kvm_common.c

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 [4]https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/ commit/?h=for-next/smccc-conduit- cleanup&id=6b7fe77c334ae59fed9500140e08f4f896b36871

Jianyong Wu (6):
psci: let arm_smccc_1_1_invoke available by modules
ptp: Reorganize ptp_kvm modules to make it arch-independent.
time: Add mechanism to recognize clocksource in time_get_snapshot
psci: Add hvc call service for ptp_kvm.
ptp: arm64: Enable ptp_kvm for arm64
kvm: arm64: Add capability check extension for ptp_kvm

Mark Rutland (1):
arm/arm64: smccc/psci: add arm_smccc_1_1_get_conduit()

 drivers/clocksource/arm_arch_timer.c        | 24 ++++++
 drivers/firmware/psci/psci.c                | 16 ++++
 drivers/ptp/Kconfig                         |  2 +-
 drivers/ptp/Makefile                        |  1 +
 drivers/ptp/ptp_kvm_arm64.c                 | 53 +++++++++++++
 drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} | 77 +++++-------------
 drivers/ptp/ptp_kvm_x86.c                   | 87 +++++++++++++++++++++
 include/asm-generic/ptp_kvm.h               | 12 +++
 include/clocksource/arm_arch_timer.h        |  4 +
 include/linux/arm-smccc.h                   | 30 ++++++-
 include/linux/clocksource.h                 |  6 ++
 include/linux/timekeeping.h                 | 12 +--
 include/uapi/linux/kvm.h                    |  1 +
 kernel/time/clocksource.c                   |  3 +
 kernel/time/timekeeping.c                   |  1 +
 virt/kvm/arm/arm.c                          |  1 +
 virt/kvm/arm/psci.c                         | 22 ++++++
 17 files changed, 286 insertions(+), 66 deletions(-)  create mode 100644

drivers/ptp/ptp_kvm_arm64.c rename drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} (63%) create mode 100644 drivers/ptp/ptp_kvm_x86.c create mode 100644 include/asm- generic/ptp_kvm.h

--
2.17.1