All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kvmclock: fix wall clock msr write error
@ 2017-01-10 10:25 Huaitong Han
  2017-01-10 10:49 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Huaitong Han @ 2017-01-10 10:25 UTC (permalink / raw)
  To: pbonzini; +Cc: kvm, Huaitong Han

kvmclock_offset is negative, it should be timespec64_add intead of
timespec64_sub.

kvm->arch.kvmclock_offset = -ktime_get_boot_ns()

Signed-off-by: Huaitong Han <huaitong.han@intel.com>
---
 arch/x86/kvm/x86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2f22810..7668a0e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1208,7 +1208,7 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 
 	if (kvm->arch.kvmclock_offset) {
 		struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
-		boot = timespec64_sub(boot, ts);
+		boot = timespec64_add(boot, ts);
 	}
 	wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
 	wc.nsec = boot.tv_nsec;
-- 
1.8.3.1


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

* Re: [PATCH] kvmclock: fix wall clock msr write error
  2017-01-10 10:25 [PATCH] kvmclock: fix wall clock msr write error Huaitong Han
@ 2017-01-10 10:49 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2017-01-10 10:49 UTC (permalink / raw)
  To: Huaitong Han; +Cc: kvm



On 10/01/2017 11:25, Huaitong Han wrote:
> kvmclock_offset is negative, it should be timespec64_add intead of
> timespec64_sub.
> 
> kvm->arch.kvmclock_offset = -ktime_get_boot_ns()

No, timespec64_sub is correct (and kvmclock.flat in kvm-unit-tests tests
it).  The guest computes CLOCK_REALTIME as pvclock_wall_clock + kvmclock
(see pvclock_read_wallclock in arch/x86/kvm/pvclock.c).

So:

	host_boot_time = getboottime64()
	pvclock_wall_clock = guest_boot_time
	kvmclock_offset = host_boot_time - guest_boot_time

The latter is important: kvmclock_offset is negative because
guest_boot_time > host_boot_time.  Then:

	pvclock_wall_clock =
		= guest_boot_time
		= host_boot_time - (host_boot_time - guest_boot_time)
		= getboottime64() - kvmclock_offset

Thanks,

Paolo


> Signed-off-by: Huaitong Han <huaitong.han@intel.com>
> ---
>  arch/x86/kvm/x86.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 2f22810..7668a0e 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1208,7 +1208,7 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
>  
>  	if (kvm->arch.kvmclock_offset) {
>  		struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
> -		boot = timespec64_sub(boot, ts);
> +		boot = timespec64_add(boot, ts);
>  	}
>  	wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
>  	wc.nsec = boot.tv_nsec;
> 

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

end of thread, other threads:[~2017-01-10 10:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-10 10:25 [PATCH] kvmclock: fix wall clock msr write error Huaitong Han
2017-01-10 10:49 ` Paolo Bonzini

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.