All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kvm: x86: fix reversed timespec values in PV wall clock
@ 2020-07-24 17:35 Oliver Upton
  2020-07-24 18:45 ` Oliver Upton
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Upton @ 2020-07-24 17:35 UTC (permalink / raw)
  To: kvm
  Cc: Paolo Bonzini, Sean Christopherson, linux-kernel, Oliver Upton,
	stable, Jim Mattson, Peter Shier

commit 8171cd68806b ("KVM: x86: use raw clock values consistently")
causes KVM to accidentally write seconds to the nanoseconds field (and
vice versa) in the KVM wall clock. Fix it by reversing this accidental
switch. Modulo the written nanoseconds value by NSEC_PER_SEC to correct
for the amount of time represented as seconds.

Fixes: 8171cd68806b ("KVM: x86: use raw clock values consistently")
Cc: stable@vger.kernel.org
Reviewed-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Peter Shier <pshier@google.com>
Signed-off-by: Oliver Upton <oupton@google.com>
---
 Parent commit: c34b26b98cac ("KVM: MIPS: clean up redundant 'kvm_run' parameters")

 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e27d3db7e43f..86228cc6b29e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1809,8 +1809,9 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 	 */
 	wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
 
-	wc.nsec = do_div(wall_nsec, 1000000000);
-	wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
+	/* overflow in 2106 guest time */
+	wc.sec = (u32)do_div(wall_nsec, NSEC_PER_SEC);
+	wc.nsec = wall_nsec % NSEC_PER_SEC;
 	wc.version = version;
 
 	kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
-- 
2.28.0.rc0.142.g3c755180ce-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] kvm: x86: fix reversed timespec values in PV wall clock
  2020-07-24 17:35 [PATCH] kvm: x86: fix reversed timespec values in PV wall clock Oliver Upton
@ 2020-07-24 18:45 ` Oliver Upton
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Upton @ 2020-07-24 18:45 UTC (permalink / raw)
  To: kvm list
  Cc: Paolo Bonzini, Sean Christopherson, Linux Kernel Mailing List,
	stable, Jim Mattson, Peter Shier

On Fri, Jul 24, 2020 at 10:35 AM Oliver Upton <oupton@google.com> wrote:
>
> commit 8171cd68806b ("KVM: x86: use raw clock values consistently")
> causes KVM to accidentally write seconds to the nanoseconds field (and
> vice versa) in the KVM wall clock. Fix it by reversing this accidental
> switch. Modulo the written nanoseconds value by NSEC_PER_SEC to correct
> for the amount of time represented as seconds.
>
> Fixes: 8171cd68806b ("KVM: x86: use raw clock values consistently")
> Cc: stable@vger.kernel.org
> Reviewed-by: Jim Mattson <jmattson@google.com>
> Reviewed-by: Peter Shier <pshier@google.com>
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---
>  Parent commit: c34b26b98cac ("KVM: MIPS: clean up redundant 'kvm_run' parameters")
>
>  arch/x86/kvm/x86.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index e27d3db7e43f..86228cc6b29e 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1809,8 +1809,9 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
>          */
>         wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
>
> -       wc.nsec = do_div(wall_nsec, 1000000000);
> -       wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */
> +       /* overflow in 2106 guest time */
> +       wc.sec = (u32)do_div(wall_nsec, NSEC_PER_SEC);
> +       wc.nsec = wall_nsec % NSEC_PER_SEC;
>         wc.version = version;
>
>         kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
> --
> 2.28.0.rc0.142.g3c755180ce-goog
>

Disregard, I crossed wires in my reading of do_div().

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-07-24 18:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-24 17:35 [PATCH] kvm: x86: fix reversed timespec values in PV wall clock Oliver Upton
2020-07-24 18:45 ` Oliver Upton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.