ptp: vmclock: Add VM generation counter and ACPI notification
cover ptp: vmclock: Add VM generation counter and ACPI notification
Similarly to live migration, starting a VM from some serialized state (aka snapshot) is an event which calls for adjusting guest clocks, hence a hypervisor should increase the disruption_marker before resuming the VM vCPUs, letting the guest know.
However, loading a snapshot, is slightly different than live migration, especially since we can start multiple VMs from the same serialized state. Apart from adjusting clocks, the guest needs to take additional action during such events, e.g. recreate UUIDs, reset network adapters/connections, reseed entropy pools, etc. These actions are not necessary during live migration. This calls for a differentiation between the two triggering events.
We differentiate between the two events via an extra field in the vmclock_abi, called vm_generation_counter. Whereas hypervisors should increase the disruption marker in both cases, they should only increase vm_generation_counter when a snapshot is loaded in a VM (not during live migration).
Additionally, we attach an ACPI notification to VMClock. Hypervisors should execute an ACPI notify operation every time seq_count changes to an even number. We implement the poll() interface for VMClock device. listeners of poll() will be notified about changes in the ABI every time the hypervisor sends us a notification.
Babis Chalios (2):
ptp: vmclock: add vm generation counter
ptp: vmclock: support device notifications
drivers/ptp/ptp_vmclock.c | 85 ++++++++++++++++++++++++++++++-- include/uapi/linux/vmclock-abi.h | 19 +++++++ 2 files changed, 99 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH 1/2 ptp: vmclock: add vm generation counter
Similar to live migration, loading a VM from some saved state (aka snapshot) is also an event that calls for clock adjustments in the guest. However, guests might want to take more actions as a response to such events, e.g. as discarding UUIDs, resetting network connections, reseeding entropy pools, etc. These are actions that guests don't typically take during live migration, so add a new field in the vmclock_abi called vm_generation_counter which informs the guest about such events.
--- include/uapi/linux/vmclock-abi.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
include/uapi/linux/vmclock-abi.h +19 -0
@@ -115,6 +115,12 @@ struct vmclock_abi {* bit again after the update, using the about-to-be-valid fields. */ #define VMCLOCK_FLAG_TIME_MONOTONIC (1 << 7) +/* +* If the VM_GEN_COUNTER_PRESENT flag is set, the hypervisor will +* bump the vm_generation_counter field every time the guest is +* loaded from some save state (restored from a snapshot). +*/ +#define VMCLOCK_FLAG_VM_GEN_COUNTER_PRESENT (1 << 8)__u8 pad[2 ];__u8 clock_status;
@@ -177,6 +183,19 @@ struct vmclock_abi {__le64 time_frac_sec;/* Units of 1/2^64 of a second */ __le64 time_esterror_nanosec;__le64 time_maxerror_nanosec; + +/* +* This field changes to another non-repeating value when the VM +* is loaded from a snapshot. This event, typically, represents a +* "jump" forward in time. As a result, in this case as well, the +* guest needs to discard any calibrarion against external sources. +* Loading a snapshot in a VM has different semantics than other VM +* events such as live migration, i.e. apart from re-adjusting guest +* clocks a guest user space might want to discard UUIDs, reset +* network connections or reseed entropy, etc. As a result, we +* use a dedicated marker for such events. +*/ + __le64 vm_generation_counter; };#endif /* __VMCLOCK_ABI_H__ */
Signature · 1 line
2.34.1
David Woodhouse
maintainerRe: [RFC PATCH 1/2] ptp: vmclock: add vm generation counter
Quoted 48 lines · Babis
Similar to live migration, loading a VM from some saved state (aka snapshot) is also an event that calls for clock adjustments in the guest. However, guests might want to take more actions as a response to such events, e.g. as discarding UUIDs, resetting network connections, reseeding entropy pools, etc. These are actions that guests don't typically take during live migration, so add a new field in the
vmclock_abicalledvm_generation_counterwhich informs the guest about such events.Signed-off-by Babis Chalios <[email protected]>---
include/uapi/linux/vmclock-abi.h | 19 +++++++++++++++++++1 file changed, 19 insertions(+)include/uapi/linux/vmclock-abi.h +6
-0@@ -115,6 +115,12 @@ struct vmclock_abi {* bit again after the update, using the about-to-be-valid fields. */ #define VMCLOCK_FLAG_TIME_MONOTONIC (1 << 7) +/* +* If the VM_GEN_COUNTER_PRESENT flag is set, the hypervisor will +* bump the vm_generation_counter field every time the guest is +* loaded from some save state (restored from a snapshot). +*/ +#define VMCLOCK_FLAG_VM_GEN_COUNTER_PRESENT (1 << 8)
__u8pad[2];
__u8clock_status;include/uapi/linux/vmclock-abi.h +0
-0@@ -177,6 +183,19 @@ struct vmclock_abi {
__le64time_frac_sec; /* Units of 1/2^64 of a second */
__le64time_esterror_nanosec;
__le64time_maxerror_nanosec;include/uapi/linux/vmclock-abi.h +13
-0+ +/* +* This field changes to another non-repeating value when the VM +* is loaded from a snapshot. This event, typically, represents a +* "jump" forward in time. As a result, in this case as well, the +* guest needs to discard any calibrarion against external sources. +* Loading a snapshot in a VM has different semantics than other VM +* events such as live migration, i.e. apart from re-adjusting guest +* clocks a guest user space might want to discard UUIDs, reset +* network connections or reseed entropy, etc. As a result, we +* use a dedicated marker for such events. +*/ +__le64 vm_generation_counter;
Quoted 1 line
};
Looks good, thank you. Just a bit of nitpicking about the comment.
I really don't like talking about a "jump forward in time". That's what clocks *do*. VMClock tells the guest a relationship between its CPU counter (TSC, arch timer, etc.) and real time. As long as that relationship remains within the error bounds which were previously promised, there is no disruption. It certainly isn't about time jumping *forward*.
Let's also make it 100% clear that an implementation must *also* bump the disruption_marker field in this case, not only the vm_generation_counter. How about...
"A change in this field indicates that the guest has been loaded from a snapshot. In addition to handling a disruption in time (which will also be signalled through the disruption_marker field), a guest may wish to discard UUIDs, reset network connections or reseed entropy, etc."
RFC PATCH 2/2 ptp: vmclock: support device notifications
In reply to Chalios, BabisVMClock now expects the hypervisor to send a device notification every time the seqcount lock changes to a new (even) value.
Moreover, add support for poll() in VMClock as a means to propagate this notification to user space. poll() will notify listeners every time seq_count has changed to a new (even) value since the last time read() (or open()) was called on the device. This means that when poll() returns a (POLLIN) event, listeners need to use read() to observe what has changed and update the reader's view of seq_count. In other words, after a poll() returned all subsequent calls to poll() will immediately return with a POLLIN event until the listener calls read().
--- drivers/ptp/ptp_vmclock.c | 85 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 5 deletions(-)
drivers/ptp/ptp_vmclock.c +80 -5
@@ -5,6 +5,9 @@* Copyright © 2024 Amazon.com, Inc. or its affiliates. */ +#include "linux/poll.h" +#include "linux/types.h" +#include "linux/wait.h" #include <linux/acpi.h> #include <linux/device.h> #include <linux/err.h>
@@ -39,6 +42,7 @@ struct vmclock_state {struct resource res;struct vmclock_abi *clk;struct miscdevice miscdev; +wait_queue_head_t disrupt_wait;struct ptp_clock_info ptp_clock_info;struct ptp_clock *ptp_clock;enum clocksource_ids cs_id, sys_cs_id;
@@ -357,10 +361,15 @@ static struct ptp_clock *vmclock_ptp_register(struct device *dev,return ptp_clock_register (&st->ptp_clock_info, dev); } +struct vmclock_file_state { +struct vmclock_state *st; +uint32_t seq; +}; +static int vmclock_miscdev_mmap (struct file *fp,struct vm_area_struct *vma) { -struct vmclock_state *st =container_of (fp->private_data, - struct vmclock_state, miscdev); +struct vmclock_file_state *fst = fp->private_data; +struct vmclock_state *st = fst->st;if ((vma->vm_flags & (VM_READ |VM_WRITE )) !=VM_READ )return -EROFS ;
@@ -379,8 +388,9 @@ static int vmclock_miscdev_mmap(struct file *fp, struct vm_area_struct *vma)static ssize_t vmclock_miscdev_read (struct file *fp,char __user *buf,size_t count,loff_t *ppos) { -struct vmclock_state *st =container_of (fp->private_data, - struct vmclock_state, miscdev); +struct vmclock_file_state *fst = fp->private_data; +struct vmclock_state *st = fst->st; +ktime_t deadline =ktime_add (ktime_get (),VMCLOCK_MAX_WAIT );size_t max_count;uint32_t seq;
@@ -402,8 +412,10 @@ static ssize_t vmclock_miscdev_read(struct file *fp, char __user *buf,/* Pairs with hypervisor wmb */ virt_rmb (); -if (seq ==le32_to_cpu (st->clk->seq_count)) +if (seq ==le32_to_cpu (st->clk->seq_count)) { + fst->seq = seq;break ; + }if (ktime_after (ktime_get (), deadline))return -ETIMEDOUT ;
@@ -413,10 +425,51 @@ static ssize_t vmclock_miscdev_read(struct file *fp, char __user *buf,return count; } +static __poll_t vmclock_miscdev_poll (struct file *fp,poll_table *wait) +{ +struct vmclock_file_state *fst = fp->private_data; +struct vmclock_state *st = fst->st; +uint32_t seq; + +poll_wait (fp, &st->disrupt_wait, wait); + + seq =le32_to_cpu (st->clk->seq_count); +if (fst->seq != seq) +return POLLIN |POLLRDNORM ; + +return 0 ; +} + +static int vmclock_miscdev_open (struct inode *inode,struct file *fp) +{ +struct vmclock_state *st =container_of (fp->private_data, + struct vmclock_state, miscdev); +struct vmclock_file_state *fst =kzalloc (sizeof (*fst),GFP_KERNEL ); + +if (!fst) +return -ENOMEM ; + + fst->st = st; + fst->seq =le32_to_cpu (st->clk->seq_count); + + fp->private_data = fst; + +return 0 ; +} + +static int vmclock_miscdev_release (struct inode *inode,struct file *fp) +{ +kfree (fp->private_data); +return 0 ; +} +static const struct file_operations vmclock_miscdev_fops = { .owner =THIS_MODULE , + .open = vmclock_miscdev_open, + .release = vmclock_miscdev_release, .mmap = vmclock_miscdev_mmap, .read = vmclock_miscdev_read, + .poll = vmclock_miscdev_poll, };/* module operations */
@@ -459,6 +512,16 @@ static acpi_status vmclock_acpi_resources(struct acpi_resource *ares, void *datareturn AE_ERROR ; } +static void +vmclock_acpi_notification_handler (acpi_handle __always_unused handle, +u32 __always_unused event,void *dev) +{ +struct device *device = dev; +struct vmclock_state *st = device->driver_data; + +wake_up_interruptible (&st->disrupt_wait); +} +static int vmclock_probe_acpi (struct device *dev,struct vmclock_state *st) {struct acpi_device *adev =ACPI_COMPANION (dev);
@@ -479,6 +542,14 @@ static int vmclock_probe_acpi(struct device *dev, struct vmclock_state *st)return -ENODEV ; } + status =acpi_install_notify_handler (adev->handle,ACPI_DEVICE_NOTIFY , + vmclock_acpi_notification_handler, + dev); +if (ACPI_FAILURE (status)) { +dev_err (dev,"failed to install notification handler" ); +return -ENODEV ; + } +return 0 ; }
@@ -549,6 +620,8 @@ static int vmclock_probe(struct platform_device *pdev)if (ret)return ret; +init_waitqueue_head (&st->disrupt_wait); +/* * If the structure is big enough, it can be mapped to userspace. * Theoretically a guest OS even using larger pages could still
@@ -581,6 +654,8 @@ static int vmclock_probe(struct platform_device *pdev)return -ENODEV ; } + dev->driver_data = st; + dev_info(dev, "%s: registered %s%s%s\n", st->name, st->miscdev.minor ? "miscdev" : "", (st->miscdev.minor && st->ptp_clock) ? ", " : "",
Signature · 1 line
2.34.1