All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] kvm: kvmclock: apply kvmclock offset to guest wall clock time
@ 2012-07-19 15:21 Bruce Rogers
  2012-07-19 15:21 ` [PATCH 2/2] kvm: kvmclock: eliminate kvmclock offset when time page count goes to zero Bruce Rogers
  0 siblings, 1 reply; 4+ messages in thread
From: Bruce Rogers @ 2012-07-19 15:21 UTC (permalink / raw)
  To: kvm; +Cc: Bruce Rogers, Glauber Costa, Zachary Amsden

When a guest migrates to a new host, the system time difference from the
previous host is used in the updates to the kvmclock system time visible
to the guest, resulting in a continuation of correct kvmclock based guest
timekeeping.

The wall clock component of the kvmclock provided time is currently not
updated with this same time offset. Since the Linux guest caches the
wall clock based time, this discrepency is not noticed until the guest is
rebooted. After reboot the guest's time calculations are off.

This patch adjusts the wall clock by the kvmclock_offset, resulting in
correct guest time after a reboot.

Cc: Glauber Costa <glommer@redhat.com>
Cc: Zachary Amsden <zamsden@redhat.com>
Signed-off-by: Bruce Rogers <brogers@suse.com>
---
 arch/x86/kvm/x86.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index be6d549..14c290d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -907,6 +907,10 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 	 */
 	getboottime(&boot);
 
+	if (kvm->arch.kvmclock_offset) {
+		struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset);
+		boot = timespec_sub(boot, ts);
+	}
 	wc.sec = boot.tv_sec;
 	wc.nsec = boot.tv_nsec;
 	wc.version = version;
-- 
1.7.7


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

* [PATCH 2/2] kvm: kvmclock: eliminate kvmclock offset when time page count goes to zero
  2012-07-19 15:21 [PATCH 1/2] kvm: kvmclock: apply kvmclock offset to guest wall clock time Bruce Rogers
@ 2012-07-19 15:21 ` Bruce Rogers
  0 siblings, 0 replies; 4+ messages in thread
From: Bruce Rogers @ 2012-07-19 15:21 UTC (permalink / raw)
  To: kvm; +Cc: Bruce Rogers, Glauber Costa, Zachary Amsden

When a guest is migrated, a time offset is generated in order to
maintain the correct kvmclock based time for the guest. Detect when
all kvmclock time pages are deleted so that the kvmclock offset can
be safely reset to zero.

Cc: Glauber Costa <glommer@redhat.com>
Cc: Zachary Amsden <zamsden@redhat.com>
Signed-off-by: Bruce Rogers <brogers@suse.com>
---
 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/kvm/x86.c              |    5 ++++-
 2 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index db7c1f2..112415c 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -524,6 +524,7 @@ struct kvm_arch {
 
 	unsigned long irq_sources_bitmap;
 	s64 kvmclock_offset;
+	unsigned int n_time_pages;
 	raw_spinlock_t tsc_write_lock;
 	u64 last_tsc_nsec;
 	u64 last_tsc_write;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 14c290d..350c51b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1511,6 +1511,8 @@ static void kvmclock_reset(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.time_page) {
 		kvm_release_page_dirty(vcpu->arch.time_page);
 		vcpu->arch.time_page = NULL;
+		if (--vcpu->kvm->arch.n_time_pages == 0)
+			vcpu->kvm->arch.kvmclock_offset = 0;
 	}
 }
 
@@ -1624,7 +1626,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		if (is_error_page(vcpu->arch.time_page)) {
 			kvm_release_page_clean(vcpu->arch.time_page);
 			vcpu->arch.time_page = NULL;
-		}
+		} else
+			vcpu->kvm->arch.n_time_pages++;
 		break;
 	}
 	case MSR_KVM_ASYNC_PF_EN:
-- 
1.7.7


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

* Re: [PATCH 2/2] kvm: kvmclock: eliminate kvmclock offset when time page count goes to zero
  2012-07-20 16:50 Bruce Rogers
@ 2012-07-24  0:47 ` Marcelo Tosatti
  0 siblings, 0 replies; 4+ messages in thread
From: Marcelo Tosatti @ 2012-07-24  0:47 UTC (permalink / raw)
  To: Bruce Rogers; +Cc: kvm, Zamsden, glommer

On Fri, Jul 20, 2012 at 10:50:11AM -0600, Bruce Rogers wrote:
> When a guest is migrated, a time offset is generated in order to
> maintain the correct kvmclock based time for the guest. Detect when
> all kvmclock time pages are deleted so that the kvmclock offset can
> be safely reset to zero.
> 
> Cc: Glauber Costa <glommer@redhat.com>
> Cc: Zachary Amsden <zamsden@gmail.com>
> Signed-off-by: Bruce Rogers <brogers@suse.com>
> ---
>  arch/x86/include/asm/kvm_host.h |    1 +
>  arch/x86/kvm/x86.c              |    5 ++++-
>  2 files changed, 5 insertions(+), 1 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index db7c1f2..112415c 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -524,6 +524,7 @@ struct kvm_arch {
>  
>  	unsigned long irq_sources_bitmap;
>  	s64 kvmclock_offset;
> +	unsigned int n_time_pages;
>  	raw_spinlock_t tsc_write_lock;
>  	u64 last_tsc_nsec;
>  	u64 last_tsc_write;
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 14c290d..350c51b 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1511,6 +1511,8 @@ static void kvmclock_reset(struct kvm_vcpu *vcpu)
>  	if (vcpu->arch.time_page) {
>  		kvm_release_page_dirty(vcpu->arch.time_page);
>  		vcpu->arch.time_page = NULL;
> +		if (--vcpu->kvm->arch.n_time_pages == 0)
> +			vcpu->kvm->arch.kvmclock_offset = 0;
>  	}
>  }
>  
> @@ -1624,7 +1626,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
>  		if (is_error_page(vcpu->arch.time_page)) {
>  			kvm_release_page_clean(vcpu->arch.time_page);
>  			vcpu->arch.time_page = NULL;
> -		}
> +		} else
> +			vcpu->kvm->arch.n_time_pages++;
>  		break;
>  	}
>  	case MSR_KVM_ASYNC_PF_EN:

Userspace is responsible for configuring a correct kvmclock_offset
AFAICS?


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

* [PATCH 2/2] kvm: kvmclock: eliminate kvmclock offset when time page count goes to zero
@ 2012-07-20 16:50 Bruce Rogers
  2012-07-24  0:47 ` Marcelo Tosatti
  0 siblings, 1 reply; 4+ messages in thread
From: Bruce Rogers @ 2012-07-20 16:50 UTC (permalink / raw)
  To: kvm; +Cc: Zamsden, glommer

When a guest is migrated, a time offset is generated in order to
maintain the correct kvmclock based time for the guest. Detect when
all kvmclock time pages are deleted so that the kvmclock offset can
be safely reset to zero.

Cc: Glauber Costa <glommer@redhat.com>
Cc: Zachary Amsden <zamsden@gmail.com>
Signed-off-by: Bruce Rogers <brogers@suse.com>
---
 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/kvm/x86.c              |    5 ++++-
 2 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index db7c1f2..112415c 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -524,6 +524,7 @@ struct kvm_arch {
 
 	unsigned long irq_sources_bitmap;
 	s64 kvmclock_offset;
+	unsigned int n_time_pages;
 	raw_spinlock_t tsc_write_lock;
 	u64 last_tsc_nsec;
 	u64 last_tsc_write;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 14c290d..350c51b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1511,6 +1511,8 @@ static void kvmclock_reset(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.time_page) {
 		kvm_release_page_dirty(vcpu->arch.time_page);
 		vcpu->arch.time_page = NULL;
+		if (--vcpu->kvm->arch.n_time_pages == 0)
+			vcpu->kvm->arch.kvmclock_offset = 0;
 	}
 }
 
@@ -1624,7 +1626,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		if (is_error_page(vcpu->arch.time_page)) {
 			kvm_release_page_clean(vcpu->arch.time_page);
 			vcpu->arch.time_page = NULL;
-		}
+		} else
+			vcpu->kvm->arch.n_time_pages++;
 		break;
 	}
 	case MSR_KVM_ASYNC_PF_EN:
-- 
1.7.7


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

end of thread, other threads:[~2012-07-24  0:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-19 15:21 [PATCH 1/2] kvm: kvmclock: apply kvmclock offset to guest wall clock time Bruce Rogers
2012-07-19 15:21 ` [PATCH 2/2] kvm: kvmclock: eliminate kvmclock offset when time page count goes to zero Bruce Rogers
2012-07-20 16:50 Bruce Rogers
2012-07-24  0:47 ` Marcelo Tosatti

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.