kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Upton <oupton@google.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <sean.j.christopherson@intel.com>,
	linux-kernel@vger.kernel.org, Oliver Upton <oupton@google.com>,
	stable@vger.kernel.org, Jim Mattson <jmattson@google.com>,
	Peter Shier <pshier@google.com>
Subject: [PATCH] kvm: x86: fix reversed timespec values in PV wall clock
Date: Fri, 24 Jul 2020 17:35:36 +0000	[thread overview]
Message-ID: <20200724173536.789982-1-oupton@google.com> (raw)

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


             reply	other threads:[~2020-07-24 17:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-24 17:35 Oliver Upton [this message]
2020-07-24 18:45 ` [PATCH] kvm: x86: fix reversed timespec values in PV wall clock Oliver Upton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200724173536.789982-1-oupton@google.com \
    --to=oupton@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pshier@google.com \
    --cc=sean.j.christopherson@intel.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).