Skip to content

Commit 300bb1f

Browse files
jongwuMarc Zyngier
authored andcommitted
ptp: arm/arm64: Enable ptp_kvm for arm/arm64
Currently, there is no mechanism to keep time sync between guest and host in arm/arm64 virtualization environment. 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 in some scenarios,like in cloud environment, we ask for higher time precision. kvm ptp clock, which chooses the host clock source as a reference clock to sync time between guest and host, has been adopted by x86 which takes the time sync order from milliseconds to nanoseconds. This patch enables kvm ptp clock for arm/arm64 and improves clock sync precision significantly. Test result comparisons between with kvm ptp clock and without it in arm/arm64 are as follows. This test derived from the result of command 'chronyc sources'. we should take more care 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 see that the clock difference between host and guest is in order of ns. Cc: Mark Rutland <[email protected]> Acked-by: Richard Cochran <[email protected]> Signed-off-by: Jianyong Wu <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3bf7256 commit 300bb1f

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

drivers/clocksource/arm_arch_timer.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <linux/sched/clock.h>
2626
#include <linux/sched_clock.h>
2727
#include <linux/acpi.h>
28+
#include <linux/arm-smccc.h>
29+
#include <linux/ptp_kvm.h>
2830

2931
#include <asm/arch_timer.h>
3032
#include <asm/virt.h>
@@ -1659,3 +1661,35 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *table)
16591661
}
16601662
TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
16611663
#endif
1664+
1665+
int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *ts,
1666+
struct clocksource **cs)
1667+
{
1668+
struct arm_smccc_res hvc_res;
1669+
u32 ptp_counter;
1670+
ktime_t ktime;
1671+
1672+
if (!IS_ENABLED(CONFIG_HAVE_ARM_SMCCC_DISCOVERY))
1673+
return -EOPNOTSUPP;
1674+
1675+
if (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
1676+
ptp_counter = KVM_PTP_VIRT_COUNTER;
1677+
else
1678+
ptp_counter = KVM_PTP_PHYS_COUNTER;
1679+
1680+
arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID,
1681+
ptp_counter, &hvc_res);
1682+
1683+
if ((int)(hvc_res.a0) < 0)
1684+
return -EOPNOTSUPP;
1685+
1686+
ktime = (u64)hvc_res.a0 << 32 | hvc_res.a1;
1687+
*ts = ktime_to_timespec64(ktime);
1688+
if (cycle)
1689+
*cycle = (u64)hvc_res.a2 << 32 | hvc_res.a3;
1690+
if (cs)
1691+
*cs = &clocksource_counter;
1692+
1693+
return 0;
1694+
}
1695+
EXPORT_SYMBOL_GPL(kvm_arch_ptp_get_crosststamp);

drivers/ptp/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ config PTP_1588_CLOCK_PCH
108108
config PTP_1588_CLOCK_KVM
109109
tristate "KVM virtual PTP clock"
110110
depends on PTP_1588_CLOCK
111-
depends on KVM_GUEST && X86
111+
depends on (KVM_GUEST && X86) || (HAVE_ARM_SMCCC_DISCOVERY && ARM_ARCH_TIMER)
112112
default y
113113
help
114114
This driver adds support for using kvm infrastructure as a PTP

drivers/ptp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
ptp-y := ptp_clock.o ptp_chardev.o ptp_sysfs.o
77
ptp_kvm-$(CONFIG_X86) := ptp_kvm_x86.o ptp_kvm_common.o
8+
ptp_kvm-$(CONFIG_HAVE_ARM_SMCCC) := ptp_kvm_arm.o ptp_kvm_common.o
89
obj-$(CONFIG_PTP_1588_CLOCK) += ptp.o
910
obj-$(CONFIG_PTP_1588_CLOCK_DTE) += ptp_dte.o
1011
obj-$(CONFIG_PTP_1588_CLOCK_INES) += ptp_ines.o

drivers/ptp/ptp_kvm_arm.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Virtual PTP 1588 clock for use with KVM guests
4+
* Copyright (C) 2019 ARM Ltd.
5+
* All Rights Reserved
6+
*/
7+
8+
#include <linux/arm-smccc.h>
9+
#include <linux/ptp_kvm.h>
10+
11+
#include <asm/arch_timer.h>
12+
#include <asm/hypervisor.h>
13+
14+
int kvm_arch_ptp_init(void)
15+
{
16+
int ret;
17+
18+
ret = kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_PTP);
19+
if (ret <= 0)
20+
return -EOPNOTSUPP;
21+
22+
return 0;
23+
}
24+
25+
int kvm_arch_ptp_get_clock(struct timespec64 *ts)
26+
{
27+
return kvm_arch_ptp_get_crosststamp(NULL, ts, NULL);
28+
}

0 commit comments

Comments
 (0)