[PATCH 2/3] KVM: x86: Extract __kvm_set_clock() from kvm_vm_ioctl_set_clock()
From: Mushahid Hussain
Date: Mon Oct 05 2026 - 15:26:04 EST
Move the body of KVM_SET_CLOCK into __kvm_set_clock() and leave the
copy from userspace and the flags check in the ioctl handler. A
following patch steps kvmclock from inside KVM and needs the worker.
No functional change intended.
Assisted-by: Claude:claude-fable-5.1
Signed-off-by: Mushahid Hussain <hmushi@xxxxxxxxxxxx>
---
arch/x86/kvm/x86.c | 40 +++++++++++++++++++++++-----------------
1 file changed, 23 insertions(+), 17 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 32563a91fba99..19666a80240a5 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4354,22 +4354,11 @@ static int kvm_vm_ioctl_get_clock(struct kvm *kvm, void __user *argp)
return 0;
}
-static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
+static void __kvm_set_clock(struct kvm *kvm, struct kvm_clock_data *data)
{
struct kvm_arch *ka = &kvm->arch;
- struct kvm_clock_data data;
u64 now_raw_ns;
- if (copy_from_user(&data, argp, sizeof(data)))
- return -EFAULT;
-
- /*
- * Only KVM_CLOCK_REALTIME is used, but allow passing the
- * result of KVM_GET_CLOCK back to KVM_SET_CLOCK.
- */
- if (data.flags & ~KVM_CLOCK_VALID_FLAGS)
- return -EINVAL;
-
kvm_hv_request_tsc_page_update(kvm);
kvm_start_pvclock_update(kvm);
pvclock_update_vm_gtod_copy(kvm);
@@ -4379,24 +4368,41 @@ static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
* in use, we use master_kernel_ns + kvmclock_offset to set
* unsigned 'system_time' so if we use get_kvmclock_ns() (which
* is slightly ahead) here we risk going negative on unsigned
- * 'system_time' when 'data.clock' is very small.
+ * 'system_time' when 'data->clock' is very small.
*/
- if (data.flags & KVM_CLOCK_REALTIME) {
+ if (data->flags & KVM_CLOCK_REALTIME) {
u64 now_real_ns = ktime_get_real_ns();
/*
* Avoid stepping the kvmclock backwards.
*/
- if (now_real_ns > data.realtime)
- data.clock += now_real_ns - data.realtime;
+ if (now_real_ns > data->realtime)
+ data->clock += now_real_ns - data->realtime;
}
if (ka->use_master_clock)
now_raw_ns = ka->master_kernel_ns;
else
now_raw_ns = get_kvmclock_base_ns();
- ka->kvmclock_offset = data.clock - now_raw_ns;
+ ka->kvmclock_offset = data->clock - now_raw_ns;
kvm_end_pvclock_update(kvm);
+}
+
+static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
+{
+ struct kvm_clock_data data;
+
+ if (copy_from_user(&data, argp, sizeof(data)))
+ return -EFAULT;
+
+ /*
+ * Only KVM_CLOCK_REALTIME is used, but allow passing the
+ * result of KVM_GET_CLOCK back to KVM_SET_CLOCK.
+ */
+ if (data.flags & ~KVM_CLOCK_VALID_FLAGS)
+ return -EINVAL;
+
+ __kvm_set_clock(kvm, &data);
return 0;
}
--
2.47.3