Skip to contentSkip to conversation

x86/hvm/viridian: flush remote tlbs by hypercall

PATCH Superseded Paul Durrant 2 participants 3 messages Nov 19, 2015

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm/viridian: flush remote tlbs by hypercall

The Microsoft Hypervisor Top Level Functional Spec. (section 3.4) defines two bits in CPUID leaf 0x40000004:EAX for the hypervisor to recommend whether or not to issue a hypercall for local or remote TLB flush.

Whilst it's doubtful whether using a hypercall for local TLB flush would be any more efficient than a specific INVLPG VMEXIT, a remote TLB flush may well be more efficiently done. This is because the alternative mechanism is to IPI all the vCPUs in question which (in the absence of APIC virtualisation) will require emulation and scheduling of the vCPUs only to have them immediately VMEXIT for local TLB flush.

This patch therefore adds a viridian option which, if selected, enables the hypercall for remote TLB flush and implements it using ASID invalidation for targetted vCPUs followed by an IPI only to the set of CPUs that happened to be running a targetted vCPU (which may be the empty set). The flush may be more severe than requested since the hypercall can request flush only for a specific address space (CR3) but Xen neither keeps a mapping of ASID to guest CR3 nor allows invalidation of a specific ASID, but on a host with contended CPUs performance is still likely to be better than a more specific flush using IPIs.

The implementation of the patch introduces per-vCPU viridian_init() and viridian_deinit() functions to allow a scratch cpumask to be allocated. This avoids needing to put this potentially large data structure on stack during hypercall processing. It also modifies the hypercall input and output bit-fields to allow a check for the 'fast' calling convention, and a white-space fix in the definition of HVMPV_feature_mask (to remove hard tabs).

Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>
Cc Wei Liu <[email protected]>
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 docs/man/xl.cfg.pod.5              |   6 ++
 tools/libxl/libxl_dom.c            |   3 +
 tools/libxl/libxl_types.idl        |   1 +
 xen/arch/x86/hvm/hvm.c             |  12 ++++
 xen/arch/x86/hvm/viridian.c        | 136 +++++++++++++++++++++++++++++++++----
 xen/include/asm-x86/hvm/viridian.h |   4 ++
 xen/include/asm-x86/perfc_defn.h   |   1 +
 xen/include/public/hvm/params.h    |  14 ++--
 8 files changed, 160 insertions(+), 17 deletions(-)
docs/man/xl.cfg.pod.5 +6 -0
index b63846a..1a88e36 100644
@@ -1466,6 +1466,12 @@ This set incorporates the Partition Reference TSC MSR. This
 enlightenment can improve performance of Windows 7 and Windows
 Server 2008 R2 onwards.
 
+=item B<hcall_remote_tlb_flush>
+
+This set incorporates use of hypercalls for remote TLB flushing.
+This enlightenment may improve performance of Windows guests running
+on hosts with higher levels of (physical) CPU contention.
+
 =item B<defaults>
 
 This is a special value that enables the default set of groups, which
tools/libxl/libxl_dom.c +3 -0
index 44d481b..009ca9c 100644
@@ -251,6 +251,9 @@ static int hvm_set_viridian_features(libxl__gc *gc, uint32_t domid,
     if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_REFERENCE_TSC))
         mask |= HVMPV_reference_tsc;
 
+    if (libxl_bitmap_test(&enlightenments, LIBXL_VIRIDIAN_ENLIGHTENMENT_HCALL_REMOTE_TLB_FLUSH))
+        mask |= HVMPV_hcall_remote_tlb_flush;
+
     if (mask != 0 &&
         xc_hvm_param_set(CTX->xch,
                          domid,
tools/libxl/libxl_types.idl +1 -0
index 4d78f86..0aa5b9d 100644
@@ -219,6 +219,7 @@ libxl_viridian_enlightenment = Enumeration("viridian_enlightenment", [
     (1, "freq"),
     (2, "time_ref_count"),
     (3, "reference_tsc"),
+    (4, "hcall_remote_tlb_flush"),
     ])
 
 libxl_hdtype = Enumeration("hdtype", [
xen/arch/x86/hvm/hvm.c +12 -0
index 21f42a7..5aa90b3 100644
@@ -2452,6 +2452,13 @@ int hvm_vcpu_initialise(struct vcpu *v)
     if ( rc != 0 )
         goto fail6;
 
+    if ( is_viridian_domain(d) )
+    {
+        rc = viridian_init(v);
+        if ( rc != 0 )
+            goto fail7;
+    }
+
     if ( v->vcpu_id == 0 )
     {
         /* NB. All these really belong in hvm_domain_initialise(). */
@@ -2468,6 +2475,8 @@ int hvm_vcpu_initialise(struct vcpu *v)
 
     return 0;
 
+ fail7:
+    hvm_all_ioreq_servers_remove_vcpu(v->domain, v);
  fail6:
     nestedhvm_vcpu_destroy(v);
  fail5:
@@ -2484,6 +2493,9 @@ int hvm_vcpu_initialise(struct vcpu *v)
 
 void hvm_vcpu_destroy(struct vcpu *v)
 {
+    if ( is_viridian_domain(v->domain) )
+        viridian_deinit(v);
+
     hvm_all_ioreq_servers_remove_vcpu(v->domain, v);
 
     if ( hvm_altp2m_supported() )
xen/arch/x86/hvm/viridian.c +123 -13

188 lines not on this page · raw message

xen/include/asm-x86/hvm/viridian.h +4 -0
index c4319d7..28f190e 100644
@@ -22,6 +22,7 @@ union viridian_apic_assist
 struct viridian_vcpu
 {
     union viridian_apic_assist apic_assist;
+    cpumask_var_t flush_cpumask;
 };
 
 union viridian_guest_os_id
@@ -117,6 +118,9 @@ 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);
 
+int viridian_init(struct vcpu *v);
+void viridian_deinit(struct vcpu *v);
+
 #endif /* __ASM_X86_HVM_VIRIDIAN_H__ */
 
 /*
xen/include/asm-x86/perfc_defn.h +1 -0
index 9ef092e..aac9331 100644
@@ -115,6 +115,7 @@ PERFCOUNTER(mshv_call_sw_addr_space,    "MS Hv Switch Address Space")
 PERFCOUNTER(mshv_call_flush_tlb_list,   "MS Hv Flush TLB list")
 PERFCOUNTER(mshv_call_flush_tlb_all,    "MS Hv Flush TLB all")
 PERFCOUNTER(mshv_call_long_wait,        "MS Hv Notify long wait")
+PERFCOUNTER(mshv_call_flush,            "MS Hv Flush TLB")
 PERFCOUNTER(mshv_rdmsr_osid,            "MS Hv rdmsr Guest OS ID")
 PERFCOUNTER(mshv_rdmsr_hc_page,         "MS Hv rdmsr hypercall page")
 PERFCOUNTER(mshv_rdmsr_vp_index,        "MS Hv rdmsr vp index")
xen/include/public/hvm/params.h +10 -4
index 356dfd3..5e54a84 100644
@@ -1,3 +1,4 @@
+
 /*
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -98,11 +99,16 @@
 #define _HVMPV_reference_tsc 3
 #define HVMPV_reference_tsc  (1 << _HVMPV_reference_tsc)
 
+/* Use Hypercall for remote TLB flush */
+#define _HVMPV_hcall_remote_tlb_flush 4
+#define HVMPV_hcall_remote_tlb_flush (1 << _HVMPV_hcall_remote_tlb_flush)
+
 #define HVMPV_feature_mask \
-	(HVMPV_base_freq | \
-	 HVMPV_no_freq | \
-	 HVMPV_time_ref_count | \
-	 HVMPV_reference_tsc)
+        (HVMPV_base_freq | \
+         HVMPV_no_freq | \
+         HVMPV_time_ref_count | \
+         HVMPV_reference_tsc | \
+         HVMPV_hcall_remote_tlb_flush)
 
 #endif
 
Signature · 1 line
2.1.4
#1 lore raw

xen/arch/x86/hvm/hvm.c int hvm_vcpu_initialise(struct vcpu *v) · line 2457

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.11.15 at 10:43, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +3 -0
@@ -2452,6 +2452,13 @@ int hvm_vcpu_initialise(struct vcpu *v)
     if ( rc != 0 )
         goto fail6;
 
+    if ( is_viridian_domain(d) )
+    {
+        rc = viridian_init(v);

I think these would better be viridian_vcpu_{,de}init().

xen/arch/x86/hvm/hvm.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 526

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -512,9 +521,25 @@ int rdmsr_viridian_regs(uint32_t idx, uint64_t *val)
     return 1;
 }
 
+int viridian_init(struct vcpu *v)
+{
+    v->arch.hvm_vcpu.viridian.flush_cpumask = xmalloc(cpumask_t);

Why not alloc_cpumask_var()?

xen/arch/x86/hvm/hvm.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 533

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    case HvFlushVirtualAddressSpace:
+    case HvFlushVirtualAddressList:
+    {
+        struct page_info *page;
+        unsigned int page_off;
+        uint8_t *input_params;
+        uint64_t vcpu_mask;

Is there a built in limit to 64 CPUs in the Hyper-V spec?

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +8 -0
+        uint64_t flags;
+        cpumask_t *pcpu_mask = curr->arch.hvm_vcpu.viridian.flush_cpumask;
+        struct vcpu *v;
+
+        /*
+         * See Microsoft Hypervisor Top Level Spec. sections 12.4.2
+         * and 12.4.3.
+         */

xen/arch/x86/hvm/hvm.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val)

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+        perfc_incr(mshv_flush);
+
+        /* These hypercalls should never use the fast-call convention. */
+        status = HV_STATUS_INVALID_PARAMETER;
+        if ( input.fast )
+            break;
+
+        /* Get input parameters. */
+        page = get_page_from_gfn(currd, paddr_to_pfn(input_params_gpa),
+                                 NULL, P2M_ALLOC);

Does this really need to be P2M_ALLOC?

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5 -0
+        if ( !page )
+            break;
+
+        page_off = input_params_gpa & (PAGE_SIZE - 1);
+

xen/arch/x86/hvm/hvm.c

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+        input_params = __map_domain_page(page);
+        input_params += page_off;
+
+        /*
+         * Address space is ignored since we cannot restrict the flush
+         * to a particular guest CR3 value.
+         */
+
+        flags = *(uint64_t *)(input_params + 8);
+        vcpu_mask = *(uint64_t *)(input_params + 16);

What if one of these crosses the page boundary?

Quoted 26 lines
xen/arch/x86/hvm/hvm.c +26 -0
+        input_params -= page_off;
+        unmap_domain_page(input_params);
+
+        put_page(page);
+
+        /*
+         * It is not clear from the spec. if we are supposed to
+         * include current virtual CPU in the set or not in this case,
+         * so err on the safe side.
+         */
+        if ( flags & HV_FLUSH_ALL_PROCESSORS )
+            vcpu_mask = ~0ul;
+
+        cpumask_clear(pcpu_mask);
+
+        /*
+         * For each specified virtual CPU flush all ASIDs to invalidate
+         * TLB entries the next time it is scheduled and then, if it
+         * is currently running, add its physical CPU to a mask of
+         * those which need to be interrupted to force a flush. (Since
+         * ASIDs have already been flushed they actually only need
+         * forcing out of non-root mode, but this is as good a way as
+         * any).
+         */
+        for_each_vcpu ( currd, v )
+        {

xen/arch/x86/hvm/hvm.c

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+            if ( !(vcpu_mask & (1ul << v->vcpu_id)) )
+                continue;
+
+            hvm_asid_flush_vcpu(v);
+            if ( v->is_running )
+                cpumask_set_cpu(v->processor, pcpu_mask);
+        }
+
+        if ( !cpumask_empty(pcpu_mask) )
+            flush_tlb_mask(pcpu_mask);

Is this correct when scheduling happens between calculating pcpu_mask and actually using it?

xen/include/asm-x86/hvm/viridian.h union viridian_apic_assist · line 25

Quoted 7 lines
xen/include/asm-x86/hvm/viridian.h +1 -0
@@ -22,6 +22,7 @@ union viridian_apic_assist
 struct viridian_vcpu
 {
     union viridian_apic_assist apic_assist;
+    cpumask_var_t flush_cpumask;

This suggests you even introduce a build problem without using alloc_cpumask_var() above.

Jan

#2 lore raw
Quoted 15 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 19 November 2015 10:18
To: Paul Durrant
Cc: Andrew Cooper; Ian Campbell; Wei Liu; Ian Jackson; Stefano Stabellini; [email protected]; Keir (Xen.org)
Subject: Re: [PATCH v1] x86/hvm/viridian: flush remote tlbs by hypercall

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.11.15 at 10:43, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2452,6 +2452,13 @@ int hvm_vcpu_initialise(struct vcpu *v)
     if ( rc != 0 )
         goto fail6;
 
Quoted 6 lines

xen/arch/x86/hvm/hvm.c int hvm_vcpu_initialise(struct vcpu *v) · line 2457

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +3 -0
+    if ( is_viridian_domain(d) )
+    {
+        rc = viridian_init(v);

I think these would better be viridian_vcpu_{,de}init().

Sure.

Quoted 10 lines

xen/arch/x86/hvm/hvm.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 526

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +3 -0
@@ -512,9 +521,25 @@ int rdmsr_viridian_regs(uint32_t idx, uint64_t *val)
     return 1;
 }
 
+int viridian_init(struct vcpu *v)
+{
+    v->arch.hvm_vcpu.viridian.flush_cpumask = xmalloc(cpumask_t);

Why not alloc_cpumask_var()?

Because I didn't know about it. Now I do :-)

Quoted 10 lines

xen/arch/x86/hvm/hvm.c int rdmsr_viridian_regs(uint32_t idx, uint64_t *val) · line 533

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7 -0
+    case HvFlushVirtualAddressSpace:
+    case HvFlushVirtualAddressList:
+    {
+        struct page_info *page;
+        unsigned int page_off;
+        uint8_t *input_params;
+        uint64_t vcpu_mask;

Is there a built in limit to 64 CPUs in the Hyper-V spec?

Yes. The spec. includes the following (in section 12.4.2):

"Future versions of the hypervisor may support more than 64 virtual processors per partition. In that case, a new field will be added to the flags value that allows the caller to define the "processor bank" to which the processor mask applies."

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/hvm/hvm.c +12 -0
+        uint64_t flags;
+        cpumask_t *pcpu_mask = curr-
 arch.hvm_vcpu.viridian.flush_cpumask;
+        struct vcpu *v;
+
+        /*
+         * See Microsoft Hypervisor Top Level Spec. sections 12.4.2
+         * and 12.4.3.
+         */
+        perfc_incr(mshv_flush);
+
+        /* These hypercalls should never use the fast-call convention. */
+        status = HV_STATUS_INVALID_PARAMETER;
Quoted 8 lines

xen/arch/x86/hvm/hvm.c

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +6 -0
+        if ( input.fast )
+            break;
+
+        /* Get input parameters. */
+        page = get_page_from_gfn(currd, paddr_to_pfn(input_params_gpa),
+                                 NULL, P2M_ALLOC);

Does this really need to be P2M_ALLOC?

I guess not. It won't be zero-filled so there's no chance of it being swept from under us.

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +9 -0
+        if ( !page )
+            break;
+
+        page_off = input_params_gpa & (PAGE_SIZE - 1);
+
+        input_params = __map_domain_page(page);
+        input_params += page_off;
+
+        /*
Quoted 9 lines

xen/arch/x86/hvm/hvm.c

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +6 -0
+         * Address space is ignored since we cannot restrict the flush
+         * to a particular guest CR3 value.
+         */
+
+        flags = *(uint64_t *)(input_params + 8);
+        vcpu_mask = *(uint64_t *)(input_params + 16);

What if one of these crosses the page boundary?

Good point. I'd assumed from the spec. that they would not cross a boundary but reading again I can't see why I assumed that. I'll look at using copy_to/from_guest() instead.

Quoted 40 lines
Quoted 26 lines
xen/arch/x86/hvm/hvm.c +26 -0
+        input_params -= page_off;
+        unmap_domain_page(input_params);
+
+        put_page(page);
+
+        /*
+         * It is not clear from the spec. if we are supposed to
+         * include current virtual CPU in the set or not in this case,
+         * so err on the safe side.
+         */
+        if ( flags & HV_FLUSH_ALL_PROCESSORS )
+            vcpu_mask = ~0ul;
+
+        cpumask_clear(pcpu_mask);
+
+        /*
+         * For each specified virtual CPU flush all ASIDs to invalidate
+         * TLB entries the next time it is scheduled and then, if it
+         * is currently running, add its physical CPU to a mask of
+         * those which need to be interrupted to force a flush. (Since
+         * ASIDs have already been flushed they actually only need
+         * forcing out of non-root mode, but this is as good a way as
+         * any).
+         */
+        for_each_vcpu ( currd, v )
+        {

xen/arch/x86/hvm/hvm.c

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+            if ( !(vcpu_mask & (1ul << v->vcpu_id)) )
+                continue;
+
+            hvm_asid_flush_vcpu(v);
+            if ( v->is_running )
+                cpumask_set_cpu(v->processor, pcpu_mask);
+        }
+
+        if ( !cpumask_empty(pcpu_mask) )
+            flush_tlb_mask(pcpu_mask);

Is this correct when scheduling happens between calculating pcpu_mask and actually using it?

Quoted 11 lines

xen/include/asm-x86/hvm/viridian.h union viridian_apic_assist · line 25

Quoted 7 lines
xen/include/asm-x86/hvm/viridian.h +1 -0
@@ -22,6 +22,7 @@ union viridian_apic_assist
 struct viridian_vcpu
 {
     union viridian_apic_assist apic_assist;
+    cpumask_var_t flush_cpumask;

This suggests you even introduce a build problem without using alloc_cpumask_var() above.

Ok.

Paul

Quoted 1 line

Jan