Skip to contentSkip to conversation

x86: use VMLOAD for PV context switch

PATCH Superseded Jan Beulich 3 participants 4 messages Sep 26, 2018 – Oct 4, 2018

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86: use VMLOAD for PV context switch

Having noticed that VMLOAD alone is about as fast as a single of the involved WRMSRs, I thought it might be a reasonable idea to also use it for PV. Measurements, however, have shown that an actual improvement can be achieved only with an early prefetch of the VMCB (thanks to Andrew for suggesting to try this), which I have to admit I can't really explain. This way on my Fam15 box context switch takes over 100 clocks less on average (the measured values are heavily varying in all cases, though).

This is intentionally not using a new hvm_funcs hook: For one, this is all about PV, and something similar can hardly be done for VMX. Furthermore the indirect to direct call patching that is meant to be applied to most hvm_funcs hooks would be ugly to make work with functions having more than 6 parameters.

---
v4: Cosmetics as per Andrew (none of which I've considered invalidating
the already present tags).
v3: Add/extend comments.
v2: Re-base.
---
Besides the mentioned oddity with measured performance, I've also noticed a significant difference (of at least 150 clocks) between measuring immediately around the calls to svm_load_segs() and measuring immediately inside the function.

xen/arch/x86/domain.c +34 -6
@@ -52,6 +52,7 @@
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/support.h>
+#include <asm/hvm/svm/svm.h>
 #include <asm/hvm/viridian.h>
 #include <asm/debugreg.h>
 #include <asm/msr.h>
@@ -1281,11 +1282,34 @@ static void load_segments(struct vcpu *n
     struct cpu_user_regs *uregs = &n->arch.user_regs;
     int all_segs_okay = 1;
     unsigned int dirty_segment_mask, cpu = smp_processor_id();
+    bool fs_gs_done = false;
 
     /* Load and clear the dirty segment mask. */
     dirty_segment_mask = per_cpu(dirty_segment_mask, cpu);
     per_cpu(dirty_segment_mask, cpu) = 0;
 
+#ifdef CONFIG_HVM
+    if ( !is_pv_32bit_vcpu(n) && !cpu_has_fsgsbase && cpu_has_svm &&
+         !((uregs->fs | uregs->gs) & ~3) &&
+         /*
+          * The remaining part is just for optimization: If only shadow GS
+          * needs loading, there's nothing to be gained here.
+          */
+         (n->arch.pv.fs_base | n->arch.pv.gs_base_user) )
+    {
+        unsigned long gsb = n->arch.flags & TF_kernel_mode
+            ? n->arch.pv.gs_base_kernel : n->arch.pv.gs_base_user;
+        unsigned long gss = n->arch.flags & TF_kernel_mode
+            ? n->arch.pv.gs_base_user : n->arch.pv.gs_base_kernel;
+
+        fs_gs_done = svm_load_segs(n->arch.pv.ldt_ents, LDT_VIRT_START(n),
+                                   uregs->fs, n->arch.pv.fs_base,
+                                   uregs->gs, gsb, gss);
+    }
+#endif
+    if ( !fs_gs_done )
+        load_LDT(n);
+
     /* Either selector != 0 ==> reload. */
     if ( unlikely((dirty_segment_mask & DIRTY_DS) | uregs->ds) )
     {
@@ -1301,7 +1325,7 @@ static void load_segments(struct vcpu *n
     }
 
     /* Either selector != 0 ==> reload. */
-    if ( unlikely((dirty_segment_mask & DIRTY_FS) | uregs->fs) )
+    if ( unlikely((dirty_segment_mask & DIRTY_FS) | uregs->fs) && !fs_gs_done )
     {
         all_segs_okay &= loadsegment(fs, uregs->fs);
         /* non-nul selector updates fs_base */
@@ -1310,7 +1334,7 @@ static void load_segments(struct vcpu *n
     }
 
     /* Either selector != 0 ==> reload. */
-    if ( unlikely((dirty_segment_mask & DIRTY_GS) | uregs->gs) )
+    if ( unlikely((dirty_segment_mask & DIRTY_GS) | uregs->gs) && !fs_gs_done )
     {
         all_segs_okay &= loadsegment(gs, uregs->gs);
         /* non-nul selector updates gs_base_user */
@@ -1318,7 +1342,7 @@ static void load_segments(struct vcpu *n
             dirty_segment_mask &= ~DIRTY_GS_BASE;
     }
 
-    if ( !is_pv_32bit_vcpu(n) )
+    if ( !fs_gs_done && !is_pv_32bit_vcpu(n) )
     {
         /* This can only be non-zero if selector is NULL. */
         if ( n->arch.pv.fs_base | (dirty_segment_mask & DIRTY_FS_BASE) )
@@ -1653,6 +1677,13 @@ static void __context_switch(void)
 
     write_ptbase(n);
 
+#if defined(CONFIG_PV) && defined(CONFIG_HVM)
+    /* Prefetch the VMCB if we expect to use it later in the context switch */
+    if ( is_pv_domain(nd) && !is_pv_32bit_domain(nd) && !is_idle_domain(nd) &&
+         !cpu_has_fsgsbase && cpu_has_svm )
+        svm_load_segs(0, 0, 0, 0, 0, 0, 0);
+#endif
+
     if ( need_full_gdt(nd) &&
          ((p->vcpu_id != n->vcpu_id) || !need_full_gdt(pd)) )
     {
@@ -1714,10 +1745,7 @@ void context_switch(struct vcpu *prev, s
         local_irq_enable();
 
         if ( is_pv_domain(nextd) )
-        {
-            load_LDT(next);
             load_segments(next);
-        }
 
         ctxt_switch_levelling(next);
 
xen/arch/x86/hvm/svm/svm.c +78 -0

108 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/svm.h +9 -0
@@ -53,6 +53,15 @@ unsigned long *svm_msrbit(unsigned long
 void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len);
 void svm_update_guest_cr(struct vcpu *, unsigned int cr, unsigned int flags);
 
+/*
+ * PV context switch helper. Calls with zero ldt_base request a prefetch of
+ * the VMCB area to be loaded from, instead of an actual load of state.
+ */
+bool svm_load_segs(unsigned int ldt_ents, unsigned long ldt_base,
+                   unsigned int fs_sel, unsigned long fs_base,
+                   unsigned int gs_sel, unsigned long gs_base,
+                   unsigned long gs_shadow);
+
 extern u32 svm_feature_flags;
 
 #define SVM_FEATURE_NPT            0 /* Nested page table support */

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v4 1/1
#1 lore raw
Quoted 15 lines · Jan Beulich

Having noticed that VMLOAD alone is about as fast as a single of the involved WRMSRs, I thought it might be a reasonable idea to also use it for PV. Measurements, however, have shown that an actual improvement can be achieved only with an early prefetch of the VMCB (thanks to Andrew for suggesting to try this), which I have to admit I can't really explain. This way on my Fam15 box context switch takes over 100 clocks less on average (the measured values are heavily varying in all cases, though).

This is intentionally not using a new hvm_funcs hook: For one, this is all about PV, and something similar can hardly be done for VMX. Furthermore the indirect to direct call patching that is meant to be applied to most hvm_funcs hooks would be ugly to make work with functions having more than 6 parameters.

Quoted 3 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Jan Beulich
Quoted 15 lines · Jan Beulich
xen/arch/x86/hvm/svm/svm.c +11 -0
@@ -1630,6 +1646,66 @@ static void svm_init_erratum_383(const s
     }
 }
 
+#ifdef CONFIG_PV
+bool svm_load_segs(unsigned int ldt_ents, unsigned long ldt_base,
+                   unsigned int fs_sel, unsigned long fs_base,
+                   unsigned int gs_sel, unsigned long gs_base,
+                   unsigned long gs_shadow)
+{
+    unsigned int cpu = smp_processor_id();
+    struct vmcb_struct *vmcb = per_cpu(host_vmcb_va, cpu);
+
+    if ( unlikely(!vmcb) )
+        return false;

xen/arch/x86/hvm/svm/svm.c static void svm_init_erratum_383(const s · line 1669

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +10 -0
+
+    if ( !ldt_base )
+    {
+        /*
+         * The actual structure field used here was arbitrarily chosen.
+         * Empirically it doesn't seem to matter much which element is used,
+         * and a clear explanation of the otherwise poor performance has not
+         * been found/provided so far.
+         */
+        asm volatile ( "prefetch %0" :: "m" (vmcb->ldtr) );

prefetchw().

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 04.10.18 at 13:58, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
xen/arch/x86/hvm/svm/svm.c +15 -0
@@ -1630,6 +1646,66 @@ static void svm_init_erratum_383(const s
     }
 }
 
+#ifdef CONFIG_PV
+bool svm_load_segs(unsigned int ldt_ents, unsigned long ldt_base,
+                   unsigned int fs_sel, unsigned long fs_base,
+                   unsigned int gs_sel, unsigned long gs_base,
+                   unsigned long gs_shadow)
+{
+    unsigned int cpu = smp_processor_id();
+    struct vmcb_struct *vmcb = per_cpu(host_vmcb_va, cpu);
+
+    if ( unlikely(!vmcb) )
+        return false;
+
+    if ( !ldt_base )
+    {
+        /*
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static void svm_init_erratum_383(const s · line 1669

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+         * The actual structure field used here was arbitrarily chosen.
+         * Empirically it doesn't seem to matter much which element is used,
+         * and a clear explanation of the otherwise poor performance has not
+         * been found/provided so far.
+         */
+        asm volatile ( "prefetch %0" :: "m" (vmcb->ldtr) );

prefetchw().

Yeah, I have changed this already in what will (would) become v5, along with the optimization condition you had pointed out is not in line with the comment next to it.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks. I guess I'll post v5 and give it a day before committing, just in case.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel