]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: x86: extract KVM_GET_CLOCK/KVM_SET_CLOCK to separate functions
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 16 Sep 2021 18:15:33 +0000 (18:15 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 1 Oct 2021 07:44:47 +0000 (03:44 -0400)
No functional change intended.

Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c

index 759ba849e10b5e3f809d8dcafbccff825ae72867..ed86e437d70793c608f0a742effed2725904db8b 100644 (file)
@@ -5817,6 +5817,54 @@ int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state)
 }
 #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */
 
+static int kvm_vm_ioctl_get_clock(struct kvm *kvm, void __user *argp)
+{
+       struct kvm_clock_data data;
+       u64 now_ns;
+
+       now_ns = get_kvmclock_ns(kvm);
+       user_ns.clock = now_ns;
+       user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
+       memset(&user_ns.pad, 0, sizeof(user_ns.pad));
+
+       if (copy_to_user(argp, &data, sizeof(data)))
+               return -EFAULT;
+
+       return 0;
+}
+
+static int kvm_vm_ioctl_set_clock(struct kvm *kvm, void __user *argp)
+{
+       struct kvm_arch *ka = &kvm->arch;
+       struct kvm_clock_data data;
+       u64 now_ns;
+
+       if (copy_from_user(&data, argp, sizeof(data)))
+               return -EFAULT;
+
+       if (data.flags)
+               return -EINVAL;
+
+       kvm_hv_invalidate_tsc_page(kvm);
+       kvm_start_pvclock_update(kvm);
+       pvclock_update_vm_gtod_copy(kvm);
+
+       /*
+        * This pairs with kvm_guest_time_update(): when masterclock is
+        * 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.
+        */
+       if (kvm->arch.use_master_clock)
+               now_ns = ka->master_kernel_ns;
+       else
+               now_ns = get_kvmclock_base_ns();
+       ka->kvmclock_offset = data.clock - now_ns;
+       kvm_end_pvclock_update(kvm);
+       return 0;
+}
+
 long kvm_arch_vm_ioctl(struct file *filp,
                       unsigned int ioctl, unsigned long arg)
 {
@@ -6060,55 +6108,12 @@ set_pit2_out:
                break;
        }
 #endif
-       case KVM_SET_CLOCK: {
-               struct kvm_arch *ka = &kvm->arch;
-               struct kvm_clock_data user_ns;
-               u64 now_ns;
-
-               r = -EFAULT;
-               if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
-                       goto out;
-
-               r = -EINVAL;
-               if (user_ns.flags)
-                       goto out;
-
-               r = 0;
-
-               kvm_hv_invalidate_tsc_page(kvm);
-               kvm_start_pvclock_update(kvm);
-               pvclock_update_vm_gtod_copy(kvm);
-
-               /*
-                * This pairs with kvm_guest_time_update(): when masterclock is
-                * 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 'user_ns.clock' is very small.
-                */
-               if (kvm->arch.use_master_clock)
-                       now_ns = ka->master_kernel_ns;
-               else
-                       now_ns = get_kvmclock_base_ns();
-               ka->kvmclock_offset = user_ns.clock - now_ns;
-               kvm_end_pvclock_update(kvm);
+       case KVM_SET_CLOCK:
+               r = kvm_vm_ioctl_set_clock(kvm, argp);
                break;
-       }
-       case KVM_GET_CLOCK: {
-               struct kvm_clock_data user_ns;
-               u64 now_ns;
-
-               now_ns = get_kvmclock_ns(kvm);
-               user_ns.clock = now_ns;
-               user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
-               memset(&user_ns.pad, 0, sizeof(user_ns.pad));
-
-               r = -EFAULT;
-               if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
-                       goto out;
-               r = 0;
+       case KVM_GET_CLOCK:
+               r = kvm_vm_ioctl_get_clock(kvm, argp);
                break;
-       }
        case KVM_MEMORY_ENCRYPT_OP: {
                r = -ENOTTY;
                if (kvm_x86_ops.mem_enc_op)