Skip to contentSkip to conversation

ptp: Add support for the AMZNC10C 'vmclock' device

PATCH Applied David Woodhouse 4 participants 9 messages Oct 6, 2024 – Oct 28, 2024

Subsystems (6)
lore
Share & tools
lore

David Woodhouse

author maintainer
#0 lore raw

PATCH v7 1/1 ptp: Add support for the AMZNC10C 'vmclock' device

From: David Woodhouse <[email protected]>

The vmclock device addresses the problem of live migration with precision clocks. The tolerances of a hardware counter (e.g. TSC) are typically around ±50PPM. A guest will use NTP/PTP/PPS to discipline that counter against an external source of 'real' time, and track the precise frequency of the counter as it changes with environmental conditions.

When a guest is live migrated, anything it knows about the frequency of the underlying counter becomes invalid. It may move from a host where the counter running at -50PPM of its nominal frequency, to a host where it runs at +50PPM. There will also be a step change in the value of the counter, as the correctness of its absolute value at migration is limited by the accuracy of the source and destination host's time synchronization.

In its simplest form, the device merely advertises a 'disruption_marker' which indicates that the guest should throw away any NTP synchronization it thinks it has, and start again.

Because the shared memory region can be exposed all the way to userspace through the /dev/vmclock0 node, applications can still use time from a fast vDSO 'system call', and check the disruption marker to be sure that their timestamp is indeed truthful.

The structure also allows for the precise time, as known by the host, to be exposed directly to guests so that they don't have to wait for NTP to resync from scratch. The PTP driver consumes this information if present. Like the KVM PTP clock, this PTP driver can convert TSC-based cross timestamps into KVM clock values. Unlike the KVM PTP clock, it does so only when such is actually helpful.

The values and fields are based on the nascent virtio-rtc specification, and the intent is that a version (hopefully precisely this version) of this structure will be included as an optional part of that spec. In the meantime, this driver supports the simple ACPI form of the device which is being shipped in certain commercial hypervisors (and submitted for inclusion in QEMU).

---

QEMU implementation at
https://git.infradead.org/users/dwmw2/qemu.git/shortlog/refs/heads/vmclock

v7:
• Add MAINTAINERS entry, one final stray whitespace change.

v6:
• Checkpatch trivia.

v5:
• Rewrite commit message based on the more informative QEMU one.
• Fix missing le32_to_cpu() in VMCLOCK_FIELD_PRESENT() macro.
• Remove obsolete comment about "if __int128 isn't available'.

v4:
• Make it all explicitly little-endian.
• Fix duplicate 'the the' in comment.

v3:
• Fix stray backtick from space→tab conversion.
• Switch to assigned AMZNC10C HID.

v2:
• Match "AMZNVCLK" HID instead of CID (QEMU patch updated accordingly)
• Be more flexible about struct size to allow expansion
• Remove 'inline'
• Comment read barriers, other cosmetics.

v1:
• Change absolute error fields to nanoseconds
• Update leap second definition to match virtio-rtc intentions in

https://lore.kernel.org/all/[email protected]

RFC v4:
• Add esterror fields, MONOTONIC flag.
• Reduce seq_count to 32 bits
• Expand size to permit 64KiB pages
• Align with virtio-rtc fields, values and leap handling
• Drop gettime() method (since we have gettimex())
• Add leap second smearing hint
• Use a real _CRS on the ACPI device

RFC v3: (wrong patch sent)

RFC v2:
• Add gettimex64() support
• Convert TSC values to KVM clock when appropriate
• Require int128 support
• Add counter_period_shift
• Add timeout when seq_count is invalid
• Add flags field
• Better comments in vmclock ABI structure
• Explicitly forbid smearing (as clock rates would need to change)

 MAINTAINERS                      |   7 +
 drivers/ptp/Kconfig              |  13 +
 drivers/ptp/Makefile             |   1 +
 drivers/ptp/ptp_vmclock.c        | 615 +++++++++++++++++++++++++++++++
 include/uapi/linux/vmclock-abi.h | 182 +++++++++
 5 files changed, 818 insertions(+)
 create mode 100644 drivers/ptp/ptp_vmclock.c
 create mode 100644 include/uapi/linux/vmclock-abi.h
MAINTAINERS +7 -0
index 8a76c256229c..ff1dc856e8f1 100644
@@ -18551,6 +18551,13 @@ S:	Maintained
 F:	drivers/ptp/ptp_vclock.c
 F:	net/ethtool/phc_vclocks.c
 
+PTP VMCLOCK SUPPORT
+M:	David Woodhouse <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/ptp/ptp_vmclock.c
+F:	include/uapi/linux/vmclock-abi.h
+
 PTRACE SUPPORT
 M:	Oleg Nesterov <[email protected]>
 S:	Maintained
drivers/ptp/Kconfig +13 -0
index 604541dcb320..e98c9767e0ef 100644
@@ -131,6 +131,19 @@ config PTP_1588_CLOCK_KVM
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp_kvm.
 
+config PTP_1588_CLOCK_VMCLOCK
+	tristate "Virtual machine PTP clock"
+	depends on X86_TSC || ARM_ARCH_TIMER
+	depends on PTP_1588_CLOCK && ACPI && ARCH_SUPPORTS_INT128
+	default y
+	help
+	  This driver adds support for using a virtual precision clock
+	  advertised by the hypervisor. This clock is only useful in virtual
+	  machines where such a device is present.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp_vmclock.
+
 config PTP_1588_CLOCK_IDT82P33
 	tristate "IDT 82P33xxx PTP clock"
 	depends on PTP_1588_CLOCK && I2C
drivers/ptp/Makefile +1 -0
index 68bf02078053..01b5cd91eb61 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_PTP_1588_CLOCK_DTE)	+= ptp_dte.o
 obj-$(CONFIG_PTP_1588_CLOCK_INES)	+= ptp_ines.o
 obj-$(CONFIG_PTP_1588_CLOCK_PCH)	+= ptp_pch.o
 obj-$(CONFIG_PTP_1588_CLOCK_KVM)	+= ptp_kvm.o
+obj-$(CONFIG_PTP_1588_CLOCK_VMCLOCK)	+= ptp_vmclock.o
 obj-$(CONFIG_PTP_1588_CLOCK_QORIQ)	+= ptp-qoriq.o
 ptp-qoriq-y				+= ptp_qoriq.o
 ptp-qoriq-$(CONFIG_DEBUG_FS)		+= ptp_qoriq_debugfs.o
drivers/ptp/ptp_vmclock.c new file +615 -0

615 lines not on this page · raw message

include/uapi/linux/vmclock-abi.h new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.44.0
maintainer Acked-by on v7 1/1
#1 lore raw
Quoted 32 lines · David Woodhouse

From: David Woodhouse <[email protected]>

The vmclock device addresses the problem of live migration with precision clocks. The tolerances of a hardware counter (e.g. TSC) are typically around ±50PPM. A guest will use NTP/PTP/PPS to discipline that counter against an external source of 'real' time, and track the precise frequency of the counter as it changes with environmental conditions.

When a guest is live migrated, anything it knows about the frequency of the underlying counter becomes invalid. It may move from a host where the counter running at -50PPM of its nominal frequency, to a host where it runs at +50PPM. There will also be a step change in the value of the counter, as the correctness of its absolute value at migration is limited by the accuracy of the source and destination host's time synchronization.

In its simplest form, the device merely advertises a 'disruption_marker' which indicates that the guest should throw away any NTP synchronization it thinks it has, and start again.

Because the shared memory region can be exposed all the way to userspace through the /dev/vmclock0 node, applications can still use time from a fast vDSO 'system call', and check the disruption marker to be sure that their timestamp is indeed truthful.

The structure also allows for the precise time, as known by the host, to be exposed directly to guests so that they don't have to wait for NTP to resync from scratch. The PTP driver consumes this information if present. Like the KVM PTP clock, this PTP driver can convert TSC-based cross timestamps into KVM clock values. Unlike the KVM PTP clock, it does so only when such is actually helpful.

Quoted 8 lines

The values and fields are based on the nascent virtio-rtc specification, and the intent is that a version (hopefully precisely this version) of this structure will be included as an optional part of that spec. In the meantime, this driver supports the simple ACPI form of the device which is being shipped in certain commercial hypervisors (and submitted for inclusion in QEMU).

#2 lore raw
In reply to David Woodhouse
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 9 lines · On Sun, 06 Oct 2024 08:17:58 +0100 you wrote:

From: David Woodhouse <[email protected]>

The vmclock device addresses the problem of live migration with precision clocks. The tolerances of a hardware counter (e.g. TSC) are typically around ±50PPM. A guest will use NTP/PTP/PPS to discipline that counter against an external source of 'real' time, and track the precise frequency of the counter as it changes with environmental conditions.

[...]

Here is the summary with links:
- [net-next,v7] ptp: Add support for the AMZNC10C 'vmclock' device

    https://git.kernel.org/netdev/net-next/c/205032724226

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#3 lore raw
In reply to David Woodhouse

drivers/ptp/Kconfig config PTP_1588_CLOCK_KVM · line 138

Quoted 5 lines · On Sun, 06 Oct 2024 08:17:58 +0100 David Woodhouse wrote:
drivers/ptp/Kconfig +5 -0
+config PTP_1588_CLOCK_VMCLOCK
+	tristate "Virtual machine PTP clock"
+	depends on X86_TSC || ARM_ARCH_TIMER
+	depends on PTP_1588_CLOCK && ACPI && ARCH_SUPPORTS_INT128
+	default y

Why default to enabled? Linus will not be happy..

author maintainer
#4 lore raw
Quoted 8 lines · Jakub Kicinski
Quoted 5 lines · On Sun, 06 Oct 2024 08:17:58 +0100 David Woodhouse wrote:

+config PTP_1588_CLOCK_VMCLOCK
+       tristate "Virtual machine PTP clock"
+       depends on X86_TSC || ARM_ARCH_TIMER
+       depends on PTP_1588_CLOCK && ACPI && ARCH_SUPPORTS_INT128 +       default y

Why default to enabled? Linus will not be happy..

Want an incremental patch to change that?

maintainer
#5 lore raw
Quoted 11 lines · On Mon, 14 Oct 2024 08:25:35 +0100 David Woodhouse wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 5 lines · On Sun, 06 Oct 2024 08:17:58 +0100 David Woodhouse wrote:

+config PTP_1588_CLOCK_VMCLOCK
+       tristate "Virtual machine PTP clock"
+       depends on X86_TSC || ARM_ARCH_TIMER
+       depends on PTP_1588_CLOCK && ACPI && ARCH_SUPPORTS_INT128 +       default y

Why default to enabled? Linus will not be happy..

Want an incremental patch to change that?

Yes please and thank you! We gotta straighten it out before the merge window.

author maintainer
#6 lore raw
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Mon, 14 Oct 2024 08:25:35 +0100 David Woodhouse wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 5 lines · On Sun, 06 Oct 2024 08:17:58 +0100 David Woodhouse wrote:

+config PTP_1588_CLOCK_VMCLOCK
+       tristate "Virtual machine PTP clock"
+       depends on X86_TSC || ARM_ARCH_TIMER
+       depends on PTP_1588_CLOCK && ACPI && ARCH_SUPPORTS_INT128 +       default y

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Why default to enabled? Linus will not be happy..

Want an incremental patch to change that?

Yes please and thank you! We gotta straighten it out before the merge window.

Hm, as I (finally) come to do that, I realise that many of the others defined in drivers/ptp/Kconfig are also 'default y'. Which is only really 'default PTP_1588_CLOCK' in practice since they all depend on that.

Most importantly, PTP_1588_CLOCK_KVM is 'default y'. And that one is fundamentally broken (at least in the presence of live migration if guests care about their clock suddenly being wrong) which is why it's being superseded by the new VMCLOCK thing. We absolutely don't want to leave the _KVM one enabled by default and not its _VMCLOCK replacement.

Please advise... I suspect the best answer is to leave it as it is?

maintainer
#7 lore raw
Quoted 7 lines · On Sat, 19 Oct 2024 18:49:24 +0100 David Woodhouse wrote:
Quoted 2 lines

Yes please and thank you! We gotta straighten it out before the merge window.

Hm, as I (finally) come to do that, I realise that many of the others defined in drivers/ptp/Kconfig are also 'default y'. Which is only really 'default PTP_1588_CLOCK' in practice since they all depend on that.

AFAICT nothing defaulted to enabled since 2017, so I'd chalk it up to us getting better at catching mistakes over time.

Quoted 5 lines

Most importantly, PTP_1588_CLOCK_KVM is 'default y'. And that one is fundamentally broken (at least in the presence of live migration if guests care about their clock suddenly being wrong) which is why it's being superseded by the new VMCLOCK thing. We absolutely don't want to leave the _KVM one enabled by default and not its _VMCLOCK replacement.

You can default to .._CLOCK_KVM, and provide the explanation in the commit message and Kconfig help.
Or if you feel strongly even make CLOCK_KVM depend on the new one?

Quoted 1 line

Please advise... I suspect the best answer is to leave it as it is?

I'd really rather not. Linus has complained to us about Kconfig symbols appearing / getting enabled by default multiple times in the past.

Sorry for the delay, vacation time.

author maintainer
#8 lore raw
Quoted 12 lines · Jakub Kicinski
Quoted 7 lines · On Sat, 19 Oct 2024 18:49:24 +0100 David Woodhouse wrote:
Quoted 2 lines

Yes please and thank you! We gotta straighten it out before the merge window.

Hm, as I (finally) come to do that, I realise that many of the others defined in drivers/ptp/Kconfig are also 'default y'. Which is only really 'default PTP_1588_CLOCK' in practice since they all depend on that.

AFAICT nothing defaulted to enabled since 2017, so I'd chalk it up to us getting better at catching mistakes over time.

Quoted 8 lines
Quoted 5 lines

Most importantly, PTP_1588_CLOCK_KVM is 'default y'. And that one is fundamentally broken (at least in the presence of live migration if guests care about their clock suddenly being wrong) which is why it's being superseded by the new VMCLOCK thing. We absolutely don't want to leave the _KVM one enabled by default and not its _VMCLOCK replacement.

You can default to .._CLOCK_KVM, and provide the explanation in the commit message and Kconfig help.

That works for me. But now it's my vacation time so it'll be at least Thursday before I can do that. I'll offer a preemptive Acked-by: David Woodhouse <[email protected]> to anyone who beats me to it :)