Skip to contentSkip to conversation

x86/viridian: Freeze time reference counter when domain is paused

PATCH In review Paul Durrant 4 participants 6 messages Oct 16, 2014 – Oct 21, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86/viridian: Freeze time reference counter when domain is paused

In XenServer system test it has become apparent that versions of Windows that make use of the time reference counter enlightenment cannot cope with large jumps forward in the value read from the MSR. Specifically, suspending a very large domain took approx. 45 minutes to complete and when the domain was resumed it was discovered that the WMI (Windows Management Instrumentation) service had hung.

The reason a large jump forward is seen by the guest is that, when a guest is suspended, the guest stops running when the SCHEDOP_suspend hypercall is made, however the MSR value essentially keeps incrementing until the tool-stack issues DOMCTL_gethvmcontext.

This patch adds code to freeze the value of the time reference counter on domain pause and 'thaw' it on domain unpause, but only thaw it if the domain is not shutting down. The absolute value of the counter is then saved in the viridian domain context record. This prevents the guest OS from experiencing large jumps in the value of the MSR and has been shown to reliably fix the problem with WMI.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>

---
v3:
- Moved viridian calls into new arch_domain functions as suggested by Jan

v2:
- Added extra check-in comment to explain timing discrepency
- Fixed viridian_feature_mask() macro
- Pass vcpu sleep function to do_domain_pause() rather than a bool

 xen/arch/arm/domain.c                  |   12 +++++++
 xen/arch/x86/domain.c                  |   19 ++++++++++
 xen/arch/x86/hvm/viridian.c            |   62 +++++++++++++++++++++++++-------
 xen/common/domain.c                    |   26 ++++++++------
 xen/include/asm-x86/hvm/hvm.h          |    9 ++++-
 xen/include/asm-x86/hvm/viridian.h     |   27 ++++++++++++++
 xen/include/public/arch-x86/hvm/save.h |    1 +
 xen/include/xen/domain.h               |    4 +++
 8 files changed, 136 insertions(+), 24 deletions(-)
xen/arch/arm/domain.c +12 -0
index 2b53931..5043837 100644
@@ -580,6 +580,18 @@ void arch_domain_destroy(struct domain *d)
     free_xenheap_page(d->shared_info);
 }
 
+void arch_domain_shutdown(struct domain *d)
+{
+}
+
+void arch_domain_pause(struct domain *d)
+{
+}
+
+void arch_domain_unpause(struct domain *d)
+{
+}
+
 static int is_guest_pv32_psr(uint32_t psr)
 {
     switch (psr & PSR_MODE_MASK)
xen/arch/x86/domain.c +19 -0
index 558d8d5..ae0a344 100644
@@ -51,6 +51,7 @@
 #include <asm/fixmap.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
+#include <asm/hvm/viridian.h>
 #include <asm/debugreg.h>
 #include <asm/msr.h>
 #include <asm/traps.h>
@@ -666,6 +667,24 @@ void arch_domain_destroy(struct domain *d)
     psr_free_rmid(d);
 }
 
+void arch_domain_shutdown(struct domain *d)
+{
+    if ( has_viridian_time_ref_count(d) )
+        viridian_time_ref_count_freeze(d);
+}
+
+void arch_domain_pause(struct domain *d)
+{
+    if ( has_viridian_time_ref_count(d) )
+        viridian_time_ref_count_freeze(d);
+}
+
+void arch_domain_unpause(struct domain *d)
+{
+    if ( has_viridian_time_ref_count(d) )
+        viridian_time_ref_count_thaw(d);
+}
+
 unsigned long pv_guest_cr4_fixup(const struct vcpu *v, unsigned long guest_cr4)
 {
     unsigned long hv_cr4_mask, hv_cr4 = real_cr4_to_pv_guest_cr4(read_cr4());
xen/arch/x86/hvm/viridian.c +49 -13

93 lines not on this page · raw message

xen/common/domain.c +16 -10
index 190998c..7c8e13a 100644
@@ -706,6 +706,8 @@ void domain_shutdown(struct domain *d, u8 reason)
         v->paused_for_shutdown = 1;
     }
 
+    arch_domain_shutdown(d);
+
     __domain_finalise_shutdown(d);
 
     spin_unlock(&d->shutdown_lock);
@@ -925,32 +927,36 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
     return 0;
 }
 
-void domain_pause(struct domain *d)
+void do_domain_pause(struct domain *d,
+                     void (*sleep_fn)(struct vcpu *v))
 {
     struct vcpu *v;
 
-    ASSERT(d != current->domain);
-
     atomic_inc(&d->pause_count);
 
     for_each_vcpu( d, v )
-        vcpu_sleep_sync(v);
+        sleep_fn(v);
+
+    arch_domain_pause(d);
 }
 
-void domain_pause_nosync(struct domain *d)
+void domain_pause(struct domain *d)
 {
-    struct vcpu *v;
-
-    atomic_inc(&d->pause_count);
+    ASSERT(d != current->domain);
+    do_domain_pause(d, vcpu_sleep_sync);
+}
 
-    for_each_vcpu( d, v )
-        vcpu_sleep_nosync(v);
+void domain_pause_nosync(struct domain *d)
+{
+    do_domain_pause(d, vcpu_sleep_nosync);
 }
 
 void domain_unpause(struct domain *d)
 {
     struct vcpu *v;
 
+    arch_domain_unpause(d);
+
     if ( atomic_dec_and_test(&d->pause_count) )
         for_each_vcpu( d, v )
             vcpu_wake(v);
xen/include/asm-x86/hvm/hvm.h +8 -1
index 0d94c48..e3d2d9a 100644
@@ -340,12 +340,19 @@ static inline unsigned long hvm_get_shadow_gs_base(struct vcpu *v)
     return hvm_funcs.get_shadow_gs_base(v);
 }
 
+
+#define has_hvm_params(d) \
+    ((d)->arch.hvm_domain.params != NULL)
+
 #define viridian_feature_mask(d) \
-    ((d)->arch.hvm_domain.params[HVM_PARAM_VIRIDIAN])
+    (has_hvm_params(d) ? (d)->arch.hvm_domain.params[HVM_PARAM_VIRIDIAN] : 0)
 
 #define is_viridian_domain(d) \
     (is_hvm_domain(d) && (viridian_feature_mask(d) & HVMPV_base_freq))
 
+#define has_viridian_time_ref_count(d) \
+    (is_viridian_domain(d) && (viridian_feature_mask(d) & HVMPV_time_ref_count))
+
 void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
                                uint32_t *eax, uint32_t *ebx,
                                uint32_t *ecx, uint32_t *edx);
xen/include/asm-x86/hvm/viridian.h +27 -0
index 496da33..4cab2e8 100644
@@ -48,10 +48,24 @@ union viridian_hypercall_gpa
     } fields;
 };
 
+struct viridian_time_ref_count
+{
+    unsigned long flags;
+
+#define _TRC_accessed 0
+#define TRC_accessed (1 << _TRC_accessed)
+#define _TRC_running 1
+#define TRC_running (1 << _TRC_running)
+
+    uint64_t val;
+    int64_t off;
+};
+
 struct viridian_domain
 {
     union viridian_guest_os_id guest_os_id;
     union viridian_hypercall_gpa hypercall_gpa;
+    struct viridian_time_ref_count time_ref_count;
 };
 
 int
@@ -75,4 +89,17 @@ rdmsr_viridian_regs(
 int
 viridian_hypercall(struct cpu_user_regs *regs);
 
+void viridian_time_ref_count_freeze(struct domain *d);
+void viridian_time_ref_count_thaw(struct domain *d);
+
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/public/arch-x86/hvm/save.h +1 -0
index 16d85a3..88aab7e 100644
@@ -568,6 +568,7 @@ struct hvm_hw_cpu_xsave {
 struct hvm_viridian_domain_context {
     uint64_t hypercall_gpa;
     uint64_t guest_os_id;
+    uint64_t time_ref_count;
 };
 
 DECLARE_HVM_SAVE_TYPE(VIRIDIAN_DOMAIN, 15, struct hvm_viridian_domain_context);
xen/include/xen/domain.h +4 -0
index c5664c2..9215b0e 100644
@@ -59,6 +59,10 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags);
 
 void arch_domain_destroy(struct domain *d);
 
+void arch_domain_shutdown(struct domain *d);
+void arch_domain_pause(struct domain *d);
+void arch_domain_unpause(struct domain *d);
+
 int arch_set_info_guest(struct vcpu *, vcpu_guest_context_u);
 void arch_get_info_guest(struct vcpu *, vcpu_guest_context_u);
 
Signature · 1 line
1.7.10.4
Reviewed-by on v3 1/1
#1 lore raw
Quoted 19 lines · Paul Durrant

In XenServer system test it has become apparent that versions of Windows that make use of the time reference counter enlightenment cannot cope with large jumps forward in the value read from the MSR. Specifically, suspending a very large domain took approx. 45 minutes to complete and when the domain was resumed it was discovered that the WMI (Windows Management Instrumentation) service had hung.

The reason a large jump forward is seen by the guest is that, when a guest is suspended, the guest stops running when the SCHEDOP_suspend hypercall is made, however the MSR value essentially keeps incrementing until the tool-stack issues DOMCTL_gethvmcontext.

This patch adds code to freeze the value of the time reference counter on domain pause and 'thaw' it on domain unpause, but only thaw it if the domain is not shutting down. The absolute value of the counter is then saved in the viridian domain context record. This prevents the guest OS from experiencing large jumps in the value of the MSR and has been shown to reliably fix the problem with WMI.

Quoted 4 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>

One nit, which could be fixed on commit given --whitespace=fix

<snip>

Quoted 5 lines
xen/arch/x86/hvm/viridian.c +0 -0
index 6726168..7d55363 100644

xen/arch/x86/hvm/viridian.c static int viridian_save_domain_ctxt(struct domain *d, hvm_domain_context_t *h) · line 490

Quoted 10 lines
xen/arch/x86/hvm/viridian.c +4 -3
@@ -450,9 +484,10 @@ static int viridian_save_domain_ctxt(struct domain *d, hvm_domain_context_t *h)
     if ( !is_viridian_domain(d) )
         return 0;
 
-    ctxt.hypercall_gpa = d->arch.hvm_domain.viridian.hypercall_gpa.raw;
-    ctxt.guest_os_id   = d->arch.hvm_domain.viridian.guest_os_id.raw;
-
+    ctxt.time_ref_count = d->arch.hvm_domain.viridian.time_ref_count.val;
+    ctxt.hypercall_gpa  = d->arch.hvm_domain.viridian.hypercall_gpa.raw;
+    ctxt.guest_os_id    = d->arch.hvm_domain.viridian.guest_os_id.raw;
+  

Introduction of trailing whitespace into the blank line.

~Andrew

#2 lore raw
In reply to Paul Durrant
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 16.10.14 at 11:21, <[email protected]> wrote:

v3:
- Moved viridian calls into new arch_domain functions as suggested by Jan

Much better this way imo, but sadly ...

Quoted 8 lines
xen/arch/arm/domain.c +2 -0
@@ -580,6 +580,18 @@ void arch_domain_destroy(struct domain *d)
     free_xenheap_page(d->shared_info);
 }
 
+void arch_domain_shutdown(struct domain *d)
+{

xen/arch/arm/domain.c void arch_domain_destroy(struct domain *d) · line 594

Quoted 10 lines
xen/arch/arm/domain.c +10 -0
+}
+
+void arch_domain_pause(struct domain *d)
+{
+}
+
+void arch_domain_unpause(struct domain *d)
+{
+}
+

... you forgot to Cc the ARM maintainers (doing so now) - one of them will need to ack this addition.

xen/arch/arm/domain.c int vcpu_unpause_by_systemcontroller(struct vcpu *v) · line 931

Quoted 7 lines
xen/arch/arm/domain.c +2 -1
@@ -925,32 +927,36 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)
     return 0;
 }
 
-void domain_pause(struct domain *d)
+void do_domain_pause(struct domain *d,
+                     void (*sleep_fn)(struct vcpu *v))

Already for v3 I pointed out that this could (and should) be static. I've taken note of this and the whitespace issue Andrew pointed out in case this can be committed without another full round.

Jan

#3 lore raw
Quoted 39 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 16 October 2014 13:18
To: Paul Durrant
Cc: Andrew Cooper; Stefano Stabellini; Ian Campbell; xen- [email protected]; Keir (Xen.org); Tim (Xen.org)
Subject: Re: [PATCH v3 for 4.5] x86/viridian: Freeze time reference counter when domain is paused

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 16.10.14 at 11:21, <[email protected]> wrote:

v3:
- Moved viridian calls into new arch_domain functions as suggested by Jan

Much better this way imo, but sadly ...

Quoted 8 lines
xen/arch/arm/domain.c +2 -0
@@ -580,6 +580,18 @@ void arch_domain_destroy(struct domain *d)
     free_xenheap_page(d->shared_info);
 }
 
+void arch_domain_shutdown(struct domain *d)
+{

xen/arch/arm/domain.c void arch_domain_destroy(struct domain *d) · line 594

Quoted 10 lines
xen/arch/arm/domain.c +10 -0
+}
+
+void arch_domain_pause(struct domain *d)
+{
+}
+
+void arch_domain_unpause(struct domain *d)
+{
+}
+

... you forgot to Cc the ARM maintainers (doing so now) - one of them will need to ack this addition.

xen/arch/arm/domain.c int vcpu_unpause_by_systemcontroller(struct

Quoted 1 line
xen/arch/arm/domain.c +0 -0
@@ -925,32 +927,36 @@ int vcpu_unpause_by_systemcontroller(struct

vcpu *v)

Quoted 8 lines

xen/arch/arm/domain.c int vcpu_unpause_by_systemcontroller(struct · line 931

Quoted 6 lines
xen/arch/arm/domain.c +2 -1
     return 0;
 }
 
-void domain_pause(struct domain *d)
+void do_domain_pause(struct domain *d,
+                     void (*sleep_fn)(struct vcpu *v))

Already for v3 I pointed out that this could (and should) be static.

Oh, sorry about that.

Quoted 3 lines

I've taken note of this and the whitespace issue Andrew pointed out in case this can be committed without another full round.

Thanks,

Paul

Quoted 1 line

Jan

#4 lore raw
In reply to Paul Durrant
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.10.14 at 11:21, <[email protected]> wrote:

In XenServer system test it has become apparent that versions of Windows that make use of the time reference counter enlightenment cannot cope with large jumps forward in the value read from the MSR. Specifically, suspending a very large domain took approx. 45 minutes to complete and when the domain was resumed it was discovered that the WMI (Windows Management Instrumentation) service had hung.

The reason a large jump forward is seen by the guest is that, when a guest is suspended, the guest stops running when the SCHEDOP_suspend hypercall is made, however the MSR value essentially keeps incrementing until the tool-stack issues DOMCTL_gethvmcontext.

Quoted 8 lines

This patch adds code to freeze the value of the time reference counter on domain pause and 'thaw' it on domain unpause, but only thaw it if the domain is not shutting down. The absolute value of the counter is then saved in the viridian domain context record. This prevents the guest OS from experiencing large jumps in the value of the MSR and has been shown to reliably fix the problem with WMI.

ARM maintainers: I was about to commit this patch when I noticed that there still was no ack from any of you for this (trivial) addition:

Quoted 11 lines
xen/arch/arm/domain.c +5 -0
@@ -580,6 +580,18 @@ void arch_domain_destroy(struct domain *d)
     free_xenheap_page(d->shared_info);
 }
 
+void arch_domain_shutdown(struct domain *d)
+{
+}
+
+void arch_domain_pause(struct domain *d)

xen/arch/arm/domain.c void arch_domain_destroy(struct domain *d) · line 597

Quoted 10 lines
xen/arch/arm/domain.c +7 -0
+{
+}
+
+void arch_domain_unpause(struct domain *d)
+{
+}
+
 static int is_guest_pv32_psr(uint32_t psr)
 {
     switch (psr & PSR_MODE_MASK)

Jan

Acked-by
#5 lore raw
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 16.10.14 at 11:21, <[email protected]> wrote:

In XenServer system test it has become apparent that versions of Windows that make use of the time reference counter enlightenment cannot cope with large jumps forward in the value read from the MSR. Specifically, suspending a very large domain took approx. 45 minutes to complete and when the domain was resumed it was discovered that the WMI (Windows Management Instrumentation) service had hung.

The reason a large jump forward is seen by the guest is that, when a guest is suspended, the guest stops running when the SCHEDOP_suspend hypercall is made, however the MSR value essentially keeps incrementing until the tool-stack issues DOMCTL_gethvmcontext.

This patch adds code to freeze the value of the time reference counter on domain pause and 'thaw' it on domain unpause, but only thaw it if the domain is not shutting down. The absolute value of the counter is then saved in the viridian domain context record. This prevents the guest OS from experiencing large jumps in the value of the MSR and has been shown to reliably fix the problem with WMI.

Quoted 4 lines
Quoted 1 line

ARM maintainers: I was about to commit this patch when I noticed that there still was no ack from any of you for this (trivial) addition: