Skip to contentSkip to conversation

xen/x86: various XPTI speedups

PATCH Superseded Juergen Gross 4 participants 33 messages Apr 18, 2018 – Apr 24, 2018 5 messages not in the archive

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v8 cover xen/x86: various XPTI speedups

This patch series aims at reducing the overhead of the XPTI Meltdown mitigation.

Patch 1 had been posted before, the main changes in this patch are due to addressing Jan's comments on my first version. The main objective of that patch is to avoid copying the L4 page table each time the guest is being activated, as often the contents didn't change while the hypervisor was active.

Patch 2 adds a new helper for writing cr3 instead of open coding the inline assembly in multiple places.

Patch 3 sets the stage for being able to activate XPTI per domain. As a first step it is now possible to switch XPTI off for dom0 via the xpti boot parameter.

Patch 4 adds support for using the INVPCID instruction for flushing the TLB.

Patch 5 reduces the costs of TLB flushes even further: as we don't make any use of global TLB entries with XPTI being active we can avoid removing all global TLB entries on TLB flushes by simply deactivating the global pages in CR4.

Patch 6 prepares using PCIDs in patch 6.
For that purpose it was necessary to allow CR3 values with bit 63 set in order to avoid flushing TLB entries when writing CR3. This requires a modification of Jan's rather clever state machine with positive and negative CR3 values for the hypervisor by using a dedicated flag byte instead.

Patch 7 converts pv_guest_cr4_to_real_cr4() from a macro to a function as it was becoming more and more complex.

Patch 8 adds some PCID helper functions for accessing the different parts of cr3 (address and pcid part).

Patch 9 is the main performance contributor: by making use of the PCID feature (if available) TLB entries can survive CR3 switches. The TLB needs to be flushed on context switches only and not when switching between guest and hypervisor or guest kernel and user mode.

On my machine (Intel i7-4600M) using the PCID feature in the non-XPTI case showed a slightly worse performance than using global pages instead (using PCID and global pages is a bad idea as invalidating global pages in this case would need a complete TLB flush). For this reason I've decided to use PCID for XPTI only as the default. That can easily be changed by using the command line parameter "pcid=true".

The complete series has been verified to still mitigate against Meltdown attacks. A simple performance test (make -j 4 in the Xen hypervisor directory) showed significant improvements compared to the state without this series.
Numbers are seconds, stddev in braces.

xpti=false  elapsed         system         user
unpatched:  88.42 ( 2.01)   94.49 ( 1.38)  180.40 ( 1.41)
patched  :  89.45 ( 3.10)   96.47 ( 3.22)  181.34 ( 1.98)
xpti=true   elapsed         system         user
unpatched: 113.43 ( 3.68)  165.44 ( 4.41)  183.30 ( 1.72)
patched  :  92.76 ( 2.11)  103.39 ( 1.13)  184.86 ( 0.12)

Changes since last version:
- patch 1: set root_pgt_changed flag on other cpus, too, when changing a shadow L4 entry
- patch 3: shadow code needs to check xpti flag now due to change in patch 1

Juergen Gross (9):
x86/xpti: avoid copying L4 page table contents when possible xen/x86: add a function for modifying cr3
xen/x86: support per-domain flag for xpti
xen/x86: use invpcid for flushing the TLB
xen/x86: disable global pages for domains with XPTI active xen/x86: use flag byte for decision whether xen_cr3 is valid xen/x86: convert pv_guest_cr4_to_real_cr4() to a function xen/x86: add some cr3 helpers
xen/x86: use PCID feature

 docs/misc/xen-command-line.markdown |  37 +++++++++++-
 xen/arch/x86/cpu/mtrr/generic.c     |  37 ++++++++----
 xen/arch/x86/debug.c                |   2 +-
 xen/arch/x86/domain.c               |   6 +-
 xen/arch/x86/domain_page.c          |   2 +-
 xen/arch/x86/flushtlb.c             | 111 ++++++++++++++++++++++++++++++------
 xen/arch/x86/mm.c                   |  86 +++++++++++++++++++++++-----
 xen/arch/x86/mm/shadow/multi.c      |  10 ++++
 xen/arch/x86/pv/dom0_build.c        |   8 ++-
 xen/arch/x86/pv/domain.c            |  89 ++++++++++++++++++++++++++++-
 xen/arch/x86/setup.c                |  27 +++------
 xen/arch/x86/smp.c                  |   2 +-
 xen/arch/x86/smpboot.c              |   6 +-
 xen/arch/x86/spec_ctrl.c            |  70 +++++++++++++++++++++++
 xen/arch/x86/x86_64/asm-offsets.c   |   2 +
 xen/arch/x86/x86_64/compat/entry.S  |   5 +-
 xen/arch/x86/x86_64/entry.S         |  78 +++++++++++--------------
 xen/common/efi/runtime.c            |   4 +-
 xen/include/asm-x86/current.h       |  23 ++++++--
 xen/include/asm-x86/domain.h        |  17 +++---
 xen/include/asm-x86/flushtlb.h      |   7 ++-
 xen/include/asm-x86/invpcid.h       |   2 +
 xen/include/asm-x86/processor.h     |  18 ++++++
 xen/include/asm-x86/pv/domain.h     |  31 ++++++++++
 xen/include/asm-x86/spec_ctrl.h     |   4 ++
 xen/include/asm-x86/x86-defns.h     |   4 +-
 26 files changed, 543 insertions(+), 145 deletions(-)
Signature · 7 lines
2.13.6


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

PATCH v8 1/9 x86/xpti: avoid copying L4 page table contents when possible

For mitigation of Meltdown the current L4 page table is copied to the cpu local root page table each time a 64 bit pv guest is entered.

Copying can be avoided in cases where the guest L4 page table hasn't been modified while running the hypervisor, e.g. when handling interrupts or any hypercall not modifying the L4 page table or %cr3.

So add a per-cpu flag indicating whether the copying should be performed and set that flag only when loading a new %cr3 or modifying the L4 page table. This includes synchronization of the cpu local root page table with other cpus, so add a special synchronization flag for that case.

A simple performance check (compiling the hypervisor via "make -j 4") in dom0 with 4 vcpus shows a significant improvement:

- real time drops from 112 seconds to 103 seconds
- system time drops from 142 seconds to 131 seconds

---
V8:
- more shadow code: set flag on all affected cpus (Tim Deegan)

V7:
- add missing flag setting in shadow code

V6:
- correct an error from rebasing to staging in assembly part

V4:
- move setting of root_pgt_changed flag in flush_area_local() out of irq disabled section (Jan Beulich)
- move setting of root_pgt_changed in make_cr3() to _toggle_guest_pt() (Jan Beulich)
- remove most conditionals in write_ptbase() (Jan Beulich)
- don't set root_pgt_changed in do_mmu_update() for modification of the user page table (Jan Beulich)

V3:
- set flag locally only if affected L4 is active (Jan Beulich)
- add setting flag to flush_area_mask() (Jan Beulich)
- set flag in make_cr3() only if called for current active vcpu

To be applied on top of Jan's "Meltdown band-aid overhead reduction" series

---
 xen/arch/x86/flushtlb.c           | 16 ++++++++++++++++
 xen/arch/x86/mm.c                 | 36 +++++++++++++++++++++++-------------
 xen/arch/x86/mm/shadow/multi.c    | 10 ++++++++++
 xen/arch/x86/pv/domain.c          |  2 ++
 xen/arch/x86/smp.c                |  2 +-
 xen/arch/x86/x86_64/asm-offsets.c |  1 +
 xen/arch/x86/x86_64/entry.S       |  9 +++++++--
 xen/include/asm-x86/current.h     |  8 ++++++++
 xen/include/asm-x86/flushtlb.h    |  5 +++++
 9 files changed, 73 insertions(+), 16 deletions(-)
xen/arch/x86/flushtlb.c +16 -0
index 8a7a76b8ff..ad19ba0f5d 100644
@@ -8,6 +8,7 @@
  */
 
 #include <xen/sched.h>
+#include <xen/smp.h>
 #include <xen/softirq.h>
 #include <asm/flushtlb.h>
 #include <asm/page.h>
@@ -160,5 +161,20 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
 
     local_irq_restore(irqfl);
 
+    if ( flags & FLUSH_ROOT_PGTBL )
+        get_cpu_info()->root_pgt_changed = true;
+
     return flags;
 }
+
+void flush_root_pgt_mask(cpumask_t *mask)
+{
+    int cpu;
+    struct cpu_info *cpu_info;
+
+    for_each_cpu(cpu, mask)
+    {
+        cpu_info = (struct cpu_info *)(stack_base[cpu] + STACK_SIZE) - 1;
+        cpu_info->root_pgt_changed = true;
+    }
+}
xen/arch/x86/mm.c +23 -13
index 9fe5583fc3..7d960c742e 100644
@@ -502,6 +502,7 @@ void make_cr3(struct vcpu *v, mfn_t mfn)
 
 void write_ptbase(struct vcpu *v)
 {
+    get_cpu_info()->root_pgt_changed = true;
     write_cr3(v->arch.cr3);
 }
 
@@ -3699,18 +3700,27 @@ long do_mmu_update(
                         break;
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
                                       cmd == MMU_PT_UPDATE_PRESERVE_AD, v);
-                    /*
-                     * No need to sync if all uses of the page can be accounted
-                     * to the page lock we hold, its pinned status, and uses on
-                     * this (v)CPU.
-                     */
-                    if ( !rc && !cpu_has_no_xpti &&
-                         ((page->u.inuse.type_info & PGT_count_mask) >
-                          (1 + !!(page->u.inuse.type_info & PGT_pinned) +
-                           (pagetable_get_pfn(curr->arch.guest_table) == mfn) +
-                           (pagetable_get_pfn(curr->arch.guest_table_user) ==
-                            mfn))) )
-                        sync_guest = true;
+                    if ( !rc && !cpu_has_no_xpti )
+                    {
+                        bool local_in_use = false;
+
+                        if ( pagetable_get_pfn(curr->arch.guest_table) == mfn )
+                        {
+                            local_in_use = true;
+                            get_cpu_info()->root_pgt_changed = true;
+                        }
+
+                        /*
+                         * No need to sync if all uses of the page can be
+                         * accounted to the page lock we hold, its pinned
+                         * status, and uses on this (v)CPU.
+                         */
+                        if ( (page->u.inuse.type_info & PGT_count_mask) >
+                             (1 + !!(page->u.inuse.type_info & PGT_pinned) +
+                              (pagetable_get_pfn(curr->arch.guest_table_user) ==
+                               mfn) + local_in_use) )
+                            sync_guest = true;
+                    }
                     break;
 
                 case PGT_writable_page:
@@ -3825,7 +3835,7 @@ long do_mmu_update(
 
         cpumask_andnot(mask, pt_owner->dirty_cpumask, cpumask_of(cpu));
         if ( !cpumask_empty(mask) )
-            flush_mask(mask, FLUSH_TLB_GLOBAL);
+            flush_mask(mask, FLUSH_TLB_GLOBAL | FLUSH_ROOT_PGTBL);
     }
 
     perfc_add(num_page_updates, i);
xen/arch/x86/mm/shadow/multi.c +10 -0
index 9c3af330ec..5e779baa53 100644
@@ -966,6 +966,16 @@ static int shadow_set_l4e(struct domain *d,
         }
         sh_put_ref(d, osl3mfn, paddr);
     }
+
+    if ( !cpu_has_no_xpti )
+        /*
+         * Lazy flushing is enough: either we do a TLB flush right afterwards
+         * which will pick up the new root page table on all affected cpus
+         * or a TLB flush won't be needed and the same reasons for that being
+         * not necessary apply to the root page table, too.
+         */
+        flush_root_pgt_mask(d->dirty_cpumask);
+
     return flags;
 }
 
xen/arch/x86/pv/domain.c +2 -0
index ac65ba4609..b1c40373fa 100644
@@ -217,6 +217,8 @@ static void _toggle_guest_pt(struct vcpu *v)
 {
     v->arch.flags ^= TF_kernel_mode;
     update_cr3(v);
+    get_cpu_info()->root_pgt_changed = true;
+
     /* Don't flush user global mappings from the TLB. Don't tick TLB clock. */
     asm volatile ( "mov %0, %%cr3" : : "r" (v->arch.cr3) : "memory" );
 
xen/arch/x86/smp.c +1 -1
index 033dd05958..63e819ca38 100644
@@ -208,7 +208,7 @@ void invalidate_interrupt(struct cpu_user_regs *regs)
     ack_APIC_irq();
     perfc_incr(ipis);
     if ( (flags & FLUSH_VCPU_STATE) && __sync_local_execstate() )
-        flags &= ~(FLUSH_TLB | FLUSH_TLB_GLOBAL);
+        flags &= ~(FLUSH_TLB | FLUSH_TLB_GLOBAL | FLUSH_ROOT_PGTBL);
     if ( flags & ~(FLUSH_VCPU_STATE | FLUSH_ORDER_MASK) )
         flush_area_local(flush_va, flags);
     cpumask_clear_cpu(smp_processor_id(), &flush_cpumask);
xen/arch/x86/x86_64/asm-offsets.c +1 -0
index a2fea94f4c..9e2aefb00f 100644
@@ -143,6 +143,7 @@ void __dummy__(void)
     OFFSET(CPUINFO_shadow_spec_ctrl, struct cpu_info, shadow_spec_ctrl);
     OFFSET(CPUINFO_use_shadow_spec_ctrl, struct cpu_info, use_shadow_spec_ctrl);
     OFFSET(CPUINFO_bti_ist_info, struct cpu_info, bti_ist_info);
+    OFFSET(CPUINFO_root_pgt_changed, struct cpu_info, root_pgt_changed);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
     BLANK();
 
xen/arch/x86/x86_64/entry.S +7 -2
index 45d9842d09..dd42223b20 100644
@@ -129,11 +129,15 @@ restore_all_guest:
         mov   VCPU_cr3(%rbx), %r9
         GET_STACK_END(dx)
         mov   STACK_CPUINFO_FIELD(pv_cr3)(%rdx), %rdi
+        test  %rdi, %rdi
+        jz    .Lrag_keep_cr3
+        mov   %rdi, %rax
+        cmpb  $0, STACK_CPUINFO_FIELD(root_pgt_changed)(%rdx)
+        je    .Lrag_copy_done
+        movb  $0, STACK_CPUINFO_FIELD(root_pgt_changed)(%rdx)
         movabs $PADDR_MASK & PAGE_MASK, %rsi
         movabs $DIRECTMAP_VIRT_START, %rcx
-        mov   %rdi, %rax
         and   %rsi, %rdi
-        jz    .Lrag_keep_cr3
         and   %r9, %rsi
         add   %rcx, %rdi
         add   %rcx, %rsi
@@ -148,6 +152,7 @@ restore_all_guest:
         sub   $(ROOT_PAGETABLE_FIRST_XEN_SLOT - \
                 ROOT_PAGETABLE_LAST_XEN_SLOT - 1) * 8, %rdi
         rep movsq
+.Lrag_copy_done:
         mov   STACK_CPUINFO_FIELD(cr4)(%rdx), %rdi
         mov   %r9, STACK_CPUINFO_FIELD(xen_cr3)(%rdx)
         mov   %rdi, %rsi
xen/include/asm-x86/current.h +8 -0
index 3a0e1eef36..f2491b4423 100644
@@ -59,6 +59,14 @@ struct cpu_info {
     bool         use_shadow_spec_ctrl;
     uint8_t      bti_ist_info;
 
+    /*
+     * The following field controls copying of the L4 page table of 64-bit
+     * PV guests to the per-cpu root page table on entering the guest context.
+     * If set the L4 page table is being copied to the root page table and
+     * the field will be reset.
+     */
+    bool         root_pgt_changed;
+
     unsigned long __pad;
     /* get_stack_bottom() must be 16-byte aligned */
 };
xen/include/asm-x86/flushtlb.h +5 -0
index 2cade9cbfb..6bf33a6f5e 100644
@@ -103,6 +103,8 @@ void write_cr3(unsigned long cr3);
 #define FLUSH_VA_VALID   0x800
  /* Flush CPU state */
 #define FLUSH_VCPU_STATE 0x1000
+ /* Flush the per-cpu root page table */
+#define FLUSH_ROOT_PGTBL 0x2000
 
 /* Flush local TLBs/caches. */
 unsigned int flush_area_local(const void *va, unsigned int flags);
@@ -134,6 +136,9 @@ void flush_area_mask(const cpumask_t *, const void *va, unsigned int flags);
 #define flush_tlb_one_all(v)                    \
     flush_tlb_one_mask(&cpu_online_map, v)
 
+/* Lazy flush of the per-cpu root page table. */
+void flush_root_pgt_mask(cpumask_t *mask);
+
 static inline void flush_page_to_ram(unsigned long mfn, bool sync_icache) {}
 static inline int invalidate_dcache_va_range(const void *p,
                                              unsigned long size)
Signature · 7 lines
2.13.6


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

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 170

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +5 -0
@@ -160,5 +161,20 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
 
     local_irq_restore(irqfl);
 
+    if ( flags & FLUSH_ROOT_PGTBL )
+        get_cpu_info()->root_pgt_changed = true;
+
     return flags;
 }
+
+void flush_root_pgt_mask(cpumask_t *mask)

const

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 172

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+{
+    int cpu;

unsigned int

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 173

Quoted 1 line
xen/arch/x86/flushtlb.c +1 -0
+    struct cpu_info *cpu_info;

Declaration in most narrow possible scope please.

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 180

Quoted 6 lines
xen/arch/x86/flushtlb.c +6 -0
+    for_each_cpu(cpu, mask)
+    {
+        cpu_info = (struct cpu_info *)(stack_base[cpu] + STACK_SIZE) - 1;
+        cpu_info->root_pgt_changed = true;
+    }
+}

So why is this not sending an IPI to trigger the FLUSH_ROOT_PGTBL processing visible above? Especially the cast you need here looks quite undesirable. Plus I think this is racy (even if quite unlikely to trigger) with a CPU being brought down:
What if stack_base[cpu] becomes NULL after you've found the respective bit in mask set. Especially when Xen runs itself virtualized, (almost) arbitrarily long periods of time may pass between any two instructions.

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 31 lines · Jan Beulich

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 170

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +5 -0
@@ -160,5 +161,20 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
 
     local_irq_restore(irqfl);
 
+    if ( flags & FLUSH_ROOT_PGTBL )
+        get_cpu_info()->root_pgt_changed = true;
+
     return flags;
 }
+
+void flush_root_pgt_mask(cpumask_t *mask)

const

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 172

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+{
+    int cpu;

unsigned int

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 173

Quoted 1 line
xen/arch/x86/flushtlb.c +1 -0
+    struct cpu_info *cpu_info;

Declaration in most narrow possible scope please.

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 180

Quoted 6 lines
xen/arch/x86/flushtlb.c +6 -0
+    for_each_cpu(cpu, mask)
+    {
+        cpu_info = (struct cpu_info *)(stack_base[cpu] + STACK_SIZE) - 1;
+        cpu_info->root_pgt_changed = true;
+    }
+}
Quoted 2 lines

So why is this not sending an IPI to trigger the FLUSH_ROOT_PGTBL processing visible above?

It is not needed for the case this function was introduced. Maybe I should rename it to flush_root_pgt_mask_lazy() ?

Quoted 1 line

Especially the cast you need here looks quite undesirable.

In get_cpu_info() it is done the same way. I can add a common helper function to current.h

Quoted 5 lines

Plus I
think this is racy (even if quite unlikely to trigger) with a CPU being brought down:
What if stack_base[cpu] becomes NULL after you've found the respective bit in mask set. Especially when Xen runs itself virtualized, (almost) arbitrarily long periods of time may pass between any two instructions.

Right.

So either I'm adding some kind of locking/rcu, or I'm switching to use IPIs and access root_pgt_changed only locally.

Do you have any preference?

Juergen

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

#5 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 08:19, <[email protected]> wrote:
Quoted 31 lines · Jan Beulich

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 170

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +5 -0
@@ -160,5 +161,20 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
 
     local_irq_restore(irqfl);
 
+    if ( flags & FLUSH_ROOT_PGTBL )
+        get_cpu_info()->root_pgt_changed = true;
+
     return flags;
 }
+
+void flush_root_pgt_mask(cpumask_t *mask)

const

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 172

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+{
+    int cpu;

unsigned int

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 173

Quoted 1 line
xen/arch/x86/flushtlb.c +1 -0
+    struct cpu_info *cpu_info;

Declaration in most narrow possible scope please.

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 180

Quoted 6 lines
xen/arch/x86/flushtlb.c +6 -0
+    for_each_cpu(cpu, mask)
+    {
+        cpu_info = (struct cpu_info *)(stack_base[cpu] + STACK_SIZE) - 1;
+        cpu_info->root_pgt_changed = true;
+    }
+}
Quoted 2 lines

So why is this not sending an IPI to trigger the FLUSH_ROOT_PGTBL processing visible above?

Quoted 7 lines

It is not needed for the case this function was introduced. Maybe I should rename it to flush_root_pgt_mask_lazy() ?

Quoted 1 line

Especially the cast you need here looks quite undesirable.

In get_cpu_info() it is done the same way. I can add a common helper function to current.h

While aiui moot with the below, a common helper would indeed be the way to go if we really needed such a cross-CPU access.

Quoted 6 lines
Quoted 5 lines

Plus I
think this is racy (even if quite unlikely to trigger) with a CPU being brought down:
What if stack_base[cpu] becomes NULL after you've found the respective bit in mask set. Especially when Xen runs itself virtualized, (almost) arbitrarily long periods of time may pass between any two instructions.

Quoted 6 lines

Right.

So either I'm adding some kind of locking/rcu, or I'm switching to use IPIs and access root_pgt_changed only locally.

Do you have any preference?

Since issuing an IPI is just a single call, I'd prefer not to have new (locking, rcu, or whatever else) logic added here. Unless of course someone, in particular Tim, thinks sending an IPI here is a rather bad idea.

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 55 lines · Jan Beulich
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 19.04.18 at 08:19, <[email protected]> wrote:
Quoted 31 lines · Jan Beulich

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 170

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +5 -0
@@ -160,5 +161,20 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
 
     local_irq_restore(irqfl);
 
+    if ( flags & FLUSH_ROOT_PGTBL )
+        get_cpu_info()->root_pgt_changed = true;
+
     return flags;
 }
+
+void flush_root_pgt_mask(cpumask_t *mask)

const

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 172

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+{
+    int cpu;

unsigned int

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 173

Quoted 1 line
xen/arch/x86/flushtlb.c +1 -0
+    struct cpu_info *cpu_info;

Declaration in most narrow possible scope please.

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 180

Quoted 6 lines
xen/arch/x86/flushtlb.c +6 -0
+    for_each_cpu(cpu, mask)
+    {
+        cpu_info = (struct cpu_info *)(stack_base[cpu] + STACK_SIZE) - 1;
+        cpu_info->root_pgt_changed = true;
+    }
+}
Quoted 2 lines

So why is this not sending an IPI to trigger the FLUSH_ROOT_PGTBL processing visible above?

Quoted 7 lines

It is not needed for the case this function was introduced. Maybe I should rename it to flush_root_pgt_mask_lazy() ?

Quoted 1 line

Especially the cast you need here looks quite undesirable.

In get_cpu_info() it is done the same way. I can add a common helper function to current.h

While aiui moot with the below, a common helper would indeed be the way to go if we really needed such a cross-CPU access.

Quoted 8 lines
Quoted 5 lines

Plus I
think this is racy (even if quite unlikely to trigger) with a CPU being brought down:
What if stack_base[cpu] becomes NULL after you've found the respective bit in mask set. Especially when Xen runs itself virtualized, (almost) arbitrarily long periods of time may pass between any two instructions.

Right.

Quoted 8 lines
Quoted 4 lines

So either I'm adding some kind of locking/rcu, or I'm switching to use IPIs and access root_pgt_changed only locally.

Do you have any preference?

Since issuing an IPI is just a single call, I'd prefer not to have new (locking, rcu, or whatever else) logic added here. Unless of course someone, in particular Tim, thinks sending an IPI here is a rather bad idea.

Okay.

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

Juergen

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

#8 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Hi,

Quoted 8 lines · Juergen Gross
Quoted 8 lines
Quoted 4 lines

So either I'm adding some kind of locking/rcu, or I'm switching to use IPIs and access root_pgt_changed only locally.

Do you have any preference?

Since issuing an IPI is just a single call, I'd prefer not to have new (locking, rcu, or whatever else) logic added here. Unless of course someone, in particular Tim, thinks sending an IPI here is a rather bad idea.

AFAICS you're calling this from shadow code whenever it changes an L4e, so I'd rather not have an IPI here if we don't need it.

Quoted 4 lines

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

Do you think it needs more (in particular to avoid the L4 copy on TLB flushes?) Would a per-domain flag be good enough if per-vcpu is difficult?

Tim.

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

#9 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 20 lines · Tim Deegan

Hi,

Quoted 8 lines · Juergen Gross
Quoted 8 lines
Quoted 4 lines

So either I'm adding some kind of locking/rcu, or I'm switching to use IPIs and access root_pgt_changed only locally.

Do you have any preference?

Since issuing an IPI is just a single call, I'd prefer not to have new (locking, rcu, or whatever else) logic added here. Unless of course someone, in particular Tim, thinks sending an IPI here is a rather bad idea.

AFAICS you're calling this from shadow code whenever it changes an L4e, so I'd rather not have an IPI here if we don't need it.

Quoted 4 lines

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

Quoted 9 lines

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

It is not so easy. The problem is that e.g. a page fault will flush the TLB entry for the page in question, but it won't lead to the L4 to be copied. Additionally a new introduced page resulting in a new L4 entry would possibly never result in picking up the new L4 shadow entry as a TLB flush wouldn't be necessary and this would result in an endless stream of page faults.

I tried that approach be just doing a L4 copy in case of a TLB flush IPI on all affected cpus but this wouldn't be enough.

So what I'd need to do is to set a new flag when writing a L4 entry and carry it up to the point where the TLB flush is done to add a L4 copy. All the places which won't do a TLB flush today will have to add one for the L4 copy.

Quoted 3 lines

Do you think it needs more (in particular to avoid the L4 copy on TLB flushes?) Would a per-domain flag be good enough if per-vcpu is difficult?

Juergen

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

#10 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 17 lines · Juergen Gross
Quoted 6 lines · Tim Deegan
Quoted 4 lines · Juergen Gross

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

Quoted 9 lines

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

Quoted 3 lines

It is not so easy. The problem is that e.g. a page fault will flush the TLB entry for the page in question, but it won't lead to the L4 to be copied.

Oh yes, I see; thanks for the explanation. It might be worth copying what the hardware does here, and checking/propagating the relevant l4e in the PV pagefault handler.

Tim.

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

#11 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 18 lines · Tim Deegan
Quoted 17 lines · Juergen Gross
Quoted 15 lines · Tim Deegan
Quoted 4 lines · Juergen Gross

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

Quoted 7 lines
Quoted 3 lines

It is not so easy. The problem is that e.g. a page fault will flush the TLB entry for the page in question, but it won't lead to the L4 to be copied.

Oh yes, I see; thanks for the explanation. It might be worth copying what the hardware does here, and checking/propagating the relevant l4e in the PV pagefault handler.

While in the long run being an interesting option I'm not sure I want to go this route for 4.11. There might be nasty corner cases and I think such a lazy approach is much more error prone than doing explicit updates of the L4 table on the affected cpus in case of a modified entry. After all this is the way we are doing it for the non-shadow case, too.

I think we should either do the explicit call of flush_mask() in shadow_set_l4e() or propagate the need for the flush up to the caller.

In case you want to discuss this problem in more detail I think we should do that on IRC for speeding things up a little bit.

Juergen

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

#12 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 27 lines · Juergen Gross
Quoted 18 lines · Tim Deegan
Quoted 17 lines · Juergen Gross
Quoted 15 lines · Tim Deegan
Quoted 4 lines · Juergen Gross

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

Quoted 7 lines
Quoted 3 lines

It is not so easy. The problem is that e.g. a page fault will flush the TLB entry for the page in question, but it won't lead to the L4 to be copied.

Oh yes, I see; thanks for the explanation. It might be worth copying what the hardware does here, and checking/propagating the relevant l4e in the PV pagefault handler.

Quoted 6 lines

While in the long run being an interesting option I'm not sure I want to go this route for 4.11. There might be nasty corner cases and I think such a lazy approach is much more error prone than doing explicit updates of the L4 table on the affected cpus in case of a modified entry. I think we should either do the explicit call of flush_mask() in shadow_set_l4e() or propagate the need for the flush up to the caller.

FWIW, I disagree -- I think that having the fault handler DTRT and relying on the existing, tested, TLB-flush logic is more likely to work than introducing a new mechanism that _also_ has to catch every possible l4e update. It should touch less code and be less likely to break with later changes. And I think it would be better to do it 'properly' now than to hope there's time to revisit it later. That said, if Jan agrees that this way is OK, I'll quit grumbling and review the shadow parts. :)

I think that setting the bits in shadow_set_l4e() is better than having this leak out into all the callers. I'm happy to see that the hunk in l4e_propagate_from_guest() has gone away too.

Please move the shadow_set_l4e() hunk up so it's just after the write, and before the general TLB flush logic.

Please move the logic into your code: the new function should take a domain pointer and do all the filtering itself rather than have shadow code be aware of what xpti is or why the domain's dirty-cpumask is relevant.

It doesn't look like there's any check limiting this to PV guests, and I think there should be, right?

Cheers,

Tim.

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

#13 lore raw

Re: [PATCH v8 1/9] x86/xpti: avoid copying L4 page table contents when possible

Quoted 35 lines · Tim Deegan
Quoted 27 lines · Juergen Gross
Quoted 18 lines · Tim Deegan
Quoted 17 lines · Juergen Gross
Quoted 15 lines · Tim Deegan
Quoted 4 lines · Juergen Gross

Another alternative would be to pass another flag to the callers to signal the need for a flush. This would require quite some modifications to shadow code I'd like to avoid, though. OTOH this way we could combine flushing the tlb and the root page tables. Tim, any preferences?

This sounds a promising direction but it should be doabl without major surgery to the shadow code. The shadow code already leaves old sl4es visible (in TLBs) when it's safe to do so, so I think the right place to hook this is on the receiving side of the TLB flush IPI. IOW as long as:
- you copy the L4 on context switch; and
- you copy it on the TLB flush IPI is received
then you can rely on the existing TLB flush mechanisms to do what you need. And shadow doesn't have to behave differently from 'normal' PV MM.

Quoted 7 lines
Quoted 3 lines

It is not so easy. The problem is that e.g. a page fault will flush the TLB entry for the page in question, but it won't lead to the L4 to be copied.

Oh yes, I see; thanks for the explanation. It might be worth copying what the hardware does here, and checking/propagating the relevant l4e in the PV pagefault handler.

Quoted 6 lines

While in the long run being an interesting option I'm not sure I want to go this route for 4.11. There might be nasty corner cases and I think such a lazy approach is much more error prone than doing explicit updates of the L4 table on the affected cpus in case of a modified entry. I think we should either do the explicit call of flush_mask() in shadow_set_l4e() or propagate the need for the flush up to the caller.

Quoted 8 lines

FWIW, I disagree -- I think that having the fault handler DTRT and relying on the existing, tested, TLB-flush logic is more likely to work than introducing a new mechanism that _also_ has to catch every possible l4e update. It should touch less code and be less likely to break with later changes. And I think it would be better to do it 'properly' now than to hope there's time to revisit it later. That said, if Jan agrees that this way is OK, I'll quit grumbling and review the shadow parts. :)

Okay, thanks.

Quoted 6 lines

I think that setting the bits in shadow_set_l4e() is better than having this leak out into all the callers. I'm happy to see that the hunk in l4e_propagate_from_guest() has gone away too.

Please move the shadow_set_l4e() hunk up so it's just after the write, and before the general TLB flush logic.

Okay.

Quoted 4 lines

Please move the logic into your code: the new function should take a domain pointer and do all the filtering itself rather than have shadow code be aware of what xpti is or why the domain's dirty-cpumask is relevant.

Okay.

Quoted 2 lines

It doesn't look like there's any check limiting this to PV guests, and I think there should be, right?

In my newest version it already is testing that.

Juergen

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

#14 lore raw

PATCH v8 2/9 xen/x86: add a function for modifying cr3

In reply to Juergen Gross

Instead of having multiple places with more or less identical asm statements just have one function doing a write to cr3.

As this function should be named write_cr3() rename the current write_cr3() function to switch_cr3().

---
V6:
- new patch

---
 xen/arch/x86/flushtlb.c         | 4 ++--
 xen/arch/x86/mm.c               | 2 +-
 xen/arch/x86/pv/domain.c        | 2 +-
 xen/common/efi/runtime.c        | 4 ++--
 xen/include/asm-x86/flushtlb.h  | 2 +-
 xen/include/asm-x86/processor.h | 5 +++++
 6 files changed, 12 insertions(+), 7 deletions(-)
xen/arch/x86/flushtlb.c +2 -2
index ad19ba0f5d..e36bd6f383 100644
@@ -72,7 +72,7 @@ static void post_flush(u32 t)
     this_cpu(tlbflush_time) = t;
 }
 
-void write_cr3(unsigned long cr3)
+void switch_cr3(unsigned long cr3)
 {
     unsigned long flags, cr4;
     u32 t;
@@ -84,7 +84,7 @@ void write_cr3(unsigned long cr3)
     cr4 = read_cr4();
 
     write_cr4(cr4 & ~X86_CR4_PGE);
-    asm volatile ( "mov %0, %%cr3" : : "r" (cr3) : "memory" );
+    write_cr3(cr3);
     write_cr4(cr4);
 
     post_flush(t);
xen/arch/x86/mm.c +1 -1
index 7d960c742e..e245d96a97 100644
@@ -503,7 +503,7 @@ void make_cr3(struct vcpu *v, mfn_t mfn)
 void write_ptbase(struct vcpu *v)
 {
     get_cpu_info()->root_pgt_changed = true;
-    write_cr3(v->arch.cr3);
+    switch_cr3(v->arch.cr3);
 }
 
 /*
xen/arch/x86/pv/domain.c +1 -1
index b1c40373fa..be40843b05 100644
@@ -220,7 +220,7 @@ static void _toggle_guest_pt(struct vcpu *v)
     get_cpu_info()->root_pgt_changed = true;
 
     /* Don't flush user global mappings from the TLB. Don't tick TLB clock. */
-    asm volatile ( "mov %0, %%cr3" : : "r" (v->arch.cr3) : "memory" );
+    write_cr3(v->arch.cr3);
 
     if ( !(v->arch.flags & TF_kernel_mode) )
         return;
xen/common/efi/runtime.c +2 -2
index 3dbc2e8ee5..4e5ddfef4f 100644
@@ -111,7 +111,7 @@ struct efi_rs_state efi_rs_enter(void)
         lgdt(&gdt_desc);
     }
 
-    write_cr3(virt_to_maddr(efi_l4_pgtable));
+    switch_cr3(virt_to_maddr(efi_l4_pgtable));
 
     return state;
 }
@@ -120,7 +120,7 @@ void efi_rs_leave(struct efi_rs_state *state)
 {
     if ( !state->cr3 )
         return;
-    write_cr3(state->cr3);
+    switch_cr3(state->cr3);
     if ( is_pv_vcpu(current) && !is_idle_vcpu(current) )
     {
         struct desc_ptr gdt_desc = {
xen/include/asm-x86/flushtlb.h +1 -1
index 6bf33a6f5e..c150f82ca2 100644
@@ -84,7 +84,7 @@ static inline unsigned long read_cr3(void)
 }
 
 /* Write pagetable base and implicitly tick the tlbflush clock. */
-void write_cr3(unsigned long cr3);
+void switch_cr3(unsigned long cr3);
 
 /* flush_* flag fields: */
  /*
xen/include/asm-x86/processor.h +5 -0
index db9988ab33..71d32c0333 100644
@@ -283,6 +283,11 @@ static inline unsigned long read_cr2(void)
     return cr2;
 }
 
+static inline void write_cr3(unsigned long val)
+{
+    asm volatile ( "mov %0, %%cr3" : : "r" (val) : "memory" );
+}
+
 static inline unsigned long read_cr4(void)
 {
     return get_cpu_info()->cr4;
Signature · 7 lines
2.13.6


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

PATCH v8 3/9 xen/x86: support per-domain flag for xpti

In reply to Juergen Gross

Instead of switching XPTI globally on or off add a per-domain flag for that purpose. This allows to modify the xpti boot parameter to support running dom0 without Meltdown mitigations. Using "xpti=nodom0" as boot parameter will achieve that.

Move the xpti boot parameter handling to xen/arch/x86/pv/domain.c as it is pv-domain specific.

---
V6.1:
- address some minor comments (Jan Beulich)

V6:
- modify xpti boot parameter options (Andrew Cooper)
- move xpti_init() code to spec_ctrl.c (Andrew Cooper)
- irework init of per-domain xpti flag (Andrew Cooper)

V3:
- latch get_cpu_info() return value in variable (Jan Beulich)
- call always xpti_domain_init() for pv dom0 (Jan Beulich)
- add __init annotations (Jan Beulich)
- drop per domain XPTI message (Jan Beulich)
- document xpti=default support (Jan Beulich)
- move domain xpti flag into a padding hole (Jan Beulich)

---
 docs/misc/xen-command-line.markdown | 14 ++++++--
 xen/arch/x86/mm.c                   | 17 +++++++--
 xen/arch/x86/mm/shadow/multi.c      |  2 +-
 xen/arch/x86/pv/dom0_build.c        |  1 +
 xen/arch/x86/pv/domain.c            |  6 ++++
 xen/arch/x86/setup.c                | 19 ----------
 xen/arch/x86/smpboot.c              |  4 +--
 xen/arch/x86/spec_ctrl.c            | 70 +++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/current.h       |  3 +-
 xen/include/asm-x86/domain.h        |  3 ++
 xen/include/asm-x86/spec_ctrl.h     |  4 +++
 11 files changed, 116 insertions(+), 27 deletions(-)
docs/misc/xen-command-line.markdown +12 -2
index b353352adf..d4f758487a 100644
@@ -1955,14 +1955,24 @@ clustered mode.  The default, given no hint from the **FADT**, is cluster
 mode.
 
 ### xpti
-> `= <boolean>`
+> `= List of [ default | <boolean> | dom0=<bool> | domu=<bool> ]`
 
-> Default: `false` on AMD hardware
+> Default: `false` on hardware not vulnerable to Meltdown (e.g. AMD)
 > Default: `true` everywhere else
 
 Override default selection of whether to isolate 64-bit PV guest page
 tables.
 
+`true` activates page table isolation even on hardware not vulnerable by
+Meltdown for all domains.
+
+`false` deactivates page table isolation on all systems for all domains.
+
+`default` sets the default behaviour.
+
+With `dom0` and `domu` it is possible to control page table isolation
+for dom0 or guest domains only.
+
 ### xsave
 > `= <boolean>`
 
xen/arch/x86/mm.c +15 -2
index e245d96a97..9c36614099 100644
@@ -502,8 +502,21 @@ void make_cr3(struct vcpu *v, mfn_t mfn)
 
 void write_ptbase(struct vcpu *v)
 {
-    get_cpu_info()->root_pgt_changed = true;
-    switch_cr3(v->arch.cr3);
+    struct cpu_info *cpu_info = get_cpu_info();
+
+    if ( is_pv_vcpu(v) && v->domain->arch.pv_domain.xpti )
+    {
+        cpu_info->root_pgt_changed = true;
+        cpu_info->pv_cr3 = __pa(this_cpu(root_pgt));
+        switch_cr3(v->arch.cr3);
+    }
+    else
+    {
+        /* Make sure to clear xen_cr3 before pv_cr3; switch_cr3() serializes. */
+        cpu_info->xen_cr3 = 0;
+        switch_cr3(v->arch.cr3);
+        cpu_info->pv_cr3 = 0;
+    }
 }
 
 /*
xen/arch/x86/mm/shadow/multi.c +1 -1
index 5e779baa53..5a9242de71 100644
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus
xen/arch/x86/pv/dom0_build.c +1 -0
index 5b4325b87f..d148395919 100644
@@ -387,6 +387,7 @@ int __init dom0_construct_pv(struct domain *d,
     if ( compat32 )
     {
         d->arch.is_32bit_pv = d->arch.has_32bit_shinfo = 1;
+        d->arch.pv_domain.xpti = false;
         v->vcpu_info = (void *)&d->shared_info->compat.vcpu_info[0];
         if ( setup_compat_arg_xlat(v) != 0 )
             BUG();
xen/arch/x86/pv/domain.c +6 -0
index be40843b05..ce1a1a9d35 100644
@@ -9,6 +9,7 @@
 #include <xen/lib.h>
 #include <xen/sched.h>
 
+#include <asm/spec_ctrl.h>
 #include <asm/pv/domain.h>
 
 static void noreturn continue_nonidle_domain(struct vcpu *v)
@@ -75,6 +76,8 @@ int switch_compat(struct domain *d)
 
     d->arch.x87_fip_width = 4;
 
+    d->arch.pv_domain.xpti = false;
+
     return 0;
 
  undo_and_fail:
@@ -205,6 +208,9 @@ int pv_domain_initialise(struct domain *d)
     /* 64-bit PV guest by default. */
     d->arch.is_32bit_pv = d->arch.has_32bit_shinfo = 0;
 
+    d->arch.pv_domain.xpti = opt_xpti & (is_hardware_domain(d)
+                                         ? OPT_XPTI_DOM0 : OPT_XPTI_DOMU);
+
     return 0;
 
   fail:
xen/arch/x86/setup.c +0 -19
index b2baee3d2c..f803980b97 100644
@@ -169,9 +169,6 @@ static int __init parse_smap_param(const char *s)
 }
 custom_param("smap", parse_smap_param);
 
-static int8_t __initdata opt_xpti = -1;
-boolean_param("xpti", opt_xpti);
-
 bool __read_mostly acpi_disabled;
 bool __initdata acpi_force;
 static char __initdata acpi_param[10] = "";
@@ -1546,22 +1543,6 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     cr4_pv32_mask = mmu_cr4_features & XEN_CR4_PV32_BITS;
 
-    if ( opt_xpti < 0 )
-    {
-        uint64_t caps = 0;
-
-        if ( boot_cpu_data.x86_vendor == X86_VENDOR_AMD )
-            caps = ARCH_CAPABILITIES_RDCL_NO;
-        else if ( boot_cpu_has(X86_FEATURE_ARCH_CAPS) )
-            rdmsrl(MSR_ARCH_CAPABILITIES, caps);
-
-        opt_xpti = !(caps & ARCH_CAPABILITIES_RDCL_NO);
-    }
-    if ( opt_xpti )
-        setup_clear_cpu_cap(X86_FEATURE_NO_XPTI);
-    else
-        setup_force_cpu_cap(X86_FEATURE_NO_XPTI);
-
     if ( cpu_has_fsgsbase )
         set_in_cr4(X86_CR4_FSGSBASE);
 
xen/arch/x86/smpboot.c +2 -2
index 103d8f7142..980192e71f 100644
@@ -325,7 +325,7 @@ void start_secondary(void *unused)
     spin_debug_disable();
 
     get_cpu_info()->xen_cr3 = 0;
-    get_cpu_info()->pv_cr3 = this_cpu(root_pgt) ? __pa(this_cpu(root_pgt)) : 0;
+    get_cpu_info()->pv_cr3 = 0;
 
     load_system_tables();
 
@@ -1044,7 +1044,7 @@ void __init smp_prepare_cpus(void)
         panic("Error %d setting up PV root page table\n", rc);
     if ( per_cpu(root_pgt, 0) )
     {
-        get_cpu_info()->pv_cr3 = __pa(per_cpu(root_pgt, 0));
+        get_cpu_info()->pv_cr3 = 0;
 
         /*
          * All entry points which may need to switch page tables have to start
xen/arch/x86/spec_ctrl.c +70 -0

82 lines not on this page · raw message

xen/include/asm-x86/current.h +2 -1
index f2491b4423..b2475783f8 100644
@@ -44,7 +44,8 @@ struct cpu_info {
     /*
      * Of the two following fields the latter is being set to the CR3 value
      * to be used on the given pCPU for loading whenever 64-bit PV guest
-     * context is being entered. The value never changes once set.
+     * context is being entered. A value of zero indicates no setting of CR3
+     * is to be performed.
      * The former is the value to restore when re-entering Xen, if any. IOW
      * its value being zero means there's nothing to restore. However, its
      * value can also be negative, indicating to the exit-to-Xen code that
xen/include/asm-x86/domain.h +3 -0
index a12ae47f1b..ed4199931a 100644
@@ -253,6 +253,9 @@ struct pv_domain
 
     atomic_t nr_l4_pages;
 
+    /* XPTI active? */
+    bool xpti;
+
     /* map_domain_page() mapping cache. */
     struct mapcache_domain mapcache;
 
xen/include/asm-x86/spec_ctrl.h +4 -0
index 5ab4ff3f68..b4fa43269e 100644
@@ -29,6 +29,10 @@ void init_speculation_mitigations(void);
 extern bool opt_ibpb;
 extern uint8_t default_bti_ist_info;
 
+extern uint8_t opt_xpti;
+#define OPT_XPTI_DOM0  0x01
+#define OPT_XPTI_DOMU  0x02
+
 static inline void init_shadow_spec_ctrl_state(void)
 {
     struct cpu_info *info = get_cpu_info();
Signature · 7 lines
2.13.6


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

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Hi Juergen,

2 small requests from me below.

Quoted 17 lines · Juergen Gross

Instead of switching XPTI globally on or off add a per-domain flag for that purpose. This allows to modify the xpti boot parameter to support running dom0 without Meltdown mitigations. Using "xpti=nodom0" as boot parameter will achieve that.

Move the xpti boot parameter handling to xen/arch/x86/pv/domain.c as it is pv-domain specific.

docs/misc/xen-command-line.markdown +0 -0
index b353352adf..d4f758487a 100644

docs/misc/xen-command-line.markdown clustered mode. The default, given no hint from the **FADT**, is cluster · line 1960

Quoted 8 lines
docs/misc/xen-command-line.markdown +2 -2
@@ -1955,14 +1955,24 @@ clustered mode.  The default, given no hint from the **FADT**, is cluster
 mode.
 
 ### xpti
-> `= <boolean>`
+> `= List of [ default | <boolean> | dom0=<bool> | domu=<bool> ]`
 
-> Default: `false` on AMD hardware
+> Default: `false` on hardware not vulnerable to Meltdown (e.g. AMD)

Could this line please be changed to:

false on hardware known not to be vulnerable to Meltdown (e.g. AMD)

Quoted 24 lines
docs/misc/xen-command-line.markdown +10 -0
 > Default: `true` everywhere else
 
 Override default selection of whether to isolate 64-bit PV guest page
 tables.
 
+`true` activates page table isolation even on hardware not vulnerable by
+Meltdown for all domains.
+
+`false` deactivates page table isolation on all systems for all domains.
+
+`default` sets the default behaviour.
+
+With `dom0` and `domu` it is possible to control page table isolation
+for dom0 or guest domains only.
+
 ### xsave
 > `= <boolean>`
 
xen/include/asm-x86/spec_ctrl.h +0 -0
index 5ab4ff3f68..b4fa43269e 100644

xen/include/asm-x86/spec_ctrl.h void init_speculation_mitigations(void); · line 38

Quoted 10 lines
xen/include/asm-x86/spec_ctrl.h +4 -0
@@ -29,6 +29,10 @@ void init_speculation_mitigations(void);
 extern bool opt_ibpb;
 extern uint8_t default_bti_ist_info;
 
+extern uint8_t opt_xpti;
+#define OPT_XPTI_DOM0  0x01
+#define OPT_XPTI_DOMU  0x02
+
 static inline void init_shadow_spec_ctrl_state(void)
 {
     struct cpu_info *info = get_cpu_info();

Could you please also include something like the following:

@@ -119,8 +122,9 @@ static void __init print_details(enum ind_thunk thunk)

            boot_cpu_has(X86_FEATURE_RSB_NATIVE)      ? " RSB_NATIVE" : "",
            boot_cpu_has(X86_FEATURE_RSB_VMEXIT)      ? " RSB_VMEXIT" : "");

- printk("XPTI: %s\n",
- boot_cpu_has(X86_FEATURE_NO_XPTI) ? "disabled" : "enabled");
+ printk("XPTI: Dom0 %s, DomU (64-bit PV only) %s\n",
+ opt_xpti & OPT_XPTI_DOM0 ? "enabled" : "disabled",
+ opt_xpti & OPT_XPTI_DOMU ? "enabled" : "disabled");
}

(just noticed that commit message also needs update regarding param name)

Signature · 6 lines
Thanks,
Sergey
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#17 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Review static void __init print_details(enum ind_thunk thunk) · line 128

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 11:42, <[email protected]> wrote:

On Wed, 2018-04-18 at 10:30 +0200, Juergen Gross wrote:

(quoted diff) +3 -2
@@ -119,8 +122,9 @@ static void __init print_details(enum ind_thunk thunk)
            boot_cpu_has(X86_FEATURE_RSB_NATIVE)      ? " RSB_NATIVE" : "",
            boot_cpu_has(X86_FEATURE_RSB_VMEXIT)      ? " RSB_VMEXIT" : "");
 
-    printk("XPTI: %s\n",
-           boot_cpu_has(X86_FEATURE_NO_XPTI) ? "disabled" : "enabled");
+    printk("XPTI: Dom0 %s, DomU (64-bit PV only) %s\n",
+           opt_xpti & OPT_XPTI_DOM0 ? "enabled" : "disabled",
+           opt_xpti & OPT_XPTI_DOMU ? "enabled" : "disabled");
 }

Ah, yes, making this log message more precise is certainly worthwhile. The placement of "(64-bit PV only)" is a little confusing though: I'd put it either before the colon or at the end of the line, as it affects Dom0 as much as DomU.

Jan

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

#18 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 11:42, <[email protected]> wrote:

On Wed, 2018-04-18 at 10:30 +0200, Juergen Gross wrote:

(quoted diff) +0 -0
@@ -119,8 +122,9 @@ static void __init print_details(enum ind_thunk thunk)
            boot_cpu_has(X86_FEATURE_RSB_NATIVE)      ? " RSB_NATIVE" : "",
            boot_cpu_has(X86_FEATURE_RSB_VMEXIT)      ? " RSB_VMEXIT" : "");
 
Quoted 11 lines

Review static void __init print_details(enum ind_thunk thunk) · line 128

Quoted 6 lines
(quoted diff) +3 -2
-    printk("XPTI: %s\n",
-           boot_cpu_has(X86_FEATURE_NO_XPTI) ? "disabled" : "enabled");
+    printk("XPTI: Dom0 %s, DomU (64-bit PV only) %s\n",
+           opt_xpti & OPT_XPTI_DOM0 ? "enabled" : "disabled",
+           opt_xpti & OPT_XPTI_DOMU ? "enabled" : "disabled");
 }

Ah, yes, making this log message more precise is certainly worthwhile. The placement of "(64-bit PV only)" is a little confusing though: I'd put it either before the colon or at the end of the line, as it affects Dom0 as much as DomU.

I'd prefer the former.

Juergen

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

#19 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

In reply to Sergey Dyasli
Quoted 28 lines · Sergey Dyasli

Hi Juergen,

2 small requests from me below.

Quoted 23 lines · Juergen Gross

Instead of switching XPTI globally on or off add a per-domain flag for that purpose. This allows to modify the xpti boot parameter to support running dom0 without Meltdown mitigations. Using "xpti=nodom0" as boot parameter will achieve that.

Move the xpti boot parameter handling to xen/arch/x86/pv/domain.c as it is pv-domain specific.

docs/misc/xen-command-line.markdown +1 -1
index b353352adf..d4f758487a 100644
@@ -1955,14 +1955,24 @@ clustered mode.  The default, given no hint from the **FADT**, is cluster
 mode.
 
 ### xpti
-> `= <boolean>`
+> `= List of [ default | <boolean> | dom0=<bool> | domu=<bool> ]`
 
Quoted 6 lines

docs/misc/xen-command-line.markdown clustered mode. The default, given no hint from the **FADT**, is cluster · line 1960

Quoted 2 lines
docs/misc/xen-command-line.markdown +1 -1
-> Default: `false` on AMD hardware
+> Default: `false` on hardware not vulnerable to Meltdown (e.g. AMD)

Could this line please be changed to:

false on hardware known not to be vulnerable to Meltdown (e.g. AMD)

Sure.

Quoted 39 lines
Quoted 25 lines
docs/misc/xen-command-line.markdown +10 -0
 > Default: `true` everywhere else
 
 Override default selection of whether to isolate 64-bit PV guest page
 tables.
 
+`true` activates page table isolation even on hardware not vulnerable by
+Meltdown for all domains.
+
+`false` deactivates page table isolation on all systems for all domains.
+
+`default` sets the default behaviour.
+
+With `dom0` and `domu` it is possible to control page table isolation
+for dom0 or guest domains only.
+
 ### xsave
 > `= <boolean>`
 
xen/include/asm-x86/spec_ctrl.h +0 -0
index 5ab4ff3f68..b4fa43269e 100644

xen/include/asm-x86/spec_ctrl.h void init_speculation_mitigations(void); · line 38

Quoted 10 lines
xen/include/asm-x86/spec_ctrl.h +4 -0
@@ -29,6 +29,10 @@ void init_speculation_mitigations(void);
 extern bool opt_ibpb;
 extern uint8_t default_bti_ist_info;
 
+extern uint8_t opt_xpti;
+#define OPT_XPTI_DOM0  0x01
+#define OPT_XPTI_DOMU  0x02
+
 static inline void init_shadow_spec_ctrl_state(void)
 {
     struct cpu_info *info = get_cpu_info();

Could you please also include something like the following:

Review static void __init print_details(enum ind_thunk thunk) · line 128

Quoted 9 lines
(quoted diff) +3 -2
@@ -119,8 +122,9 @@ static void __init print_details(enum ind_thunk thunk)
            boot_cpu_has(X86_FEATURE_RSB_NATIVE)      ? " RSB_NATIVE" : "",
            boot_cpu_has(X86_FEATURE_RSB_VMEXIT)      ? " RSB_VMEXIT" : "");
 
-    printk("XPTI: %s\n",
-           boot_cpu_has(X86_FEATURE_NO_XPTI) ? "disabled" : "enabled");
+    printk("XPTI: Dom0 %s, DomU (64-bit PV only) %s\n",
+           opt_xpti & OPT_XPTI_DOM0 ? "enabled" : "disabled",
+           opt_xpti & OPT_XPTI_DOMU ? "enabled" : "disabled");
 }

As stated already in the reply to Jan: I'll use the text:

"XPTI (64-bit PV only): Dom0 ...".

Review static void __init print_details(enum ind_thunk thunk) · line 129

Quoted 1 line
(quoted diff) +0 -0
 (just noticed that commit message also needs update regarding param name)

Aah, yes.

Thanks,

Juergen

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

#20 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

In reply to Juergen Gross

xen/arch/x86/mm/shadow/multi.c static int shadow_set_l4e(struct domain *d, · line 973

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus

How come the is_pv_domain() is appearing only here?

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/multi.c +0 -0
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
Quoted 7 lines

xen/arch/x86/mm/shadow/multi.c static int shadow_set_l4e(struct domain *d, · line 973

Quoted 5 lines
xen/arch/x86/mm/shadow/multi.c +1 -1
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus

How come the is_pv_domain() is appearing only here?

It is mandatory for testing the per-domain xpti flag. I could add it in patch 1 already if you like that better.

Juergen

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

#23 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 17:33, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/multi.c +0 -0
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
Quoted 7 lines

xen/arch/x86/mm/shadow/multi.c static int shadow_set_l4e(struct domain *d, · line 973

Quoted 5 lines
xen/arch/x86/mm/shadow/multi.c +1 -1
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus

How come the is_pv_domain() is appearing only here?

Quoted 2 lines

It is mandatory for testing the per-domain xpti flag. I could add it in patch 1 already if you like that better.

Well, if you added it earlier, some unnecessary IPIs would be suppressed right away.

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#25 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 17:33, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich

xen/arch/x86/mm/shadow/multi.c static int shadow_set_l4e(struct domain *d, · line 973

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus

How come the is_pv_domain() is appearing only here?

Quoted 5 lines
Quoted 2 lines

It is mandatory for testing the per-domain xpti flag. I could add it in patch 1 already if you like that better.

Well, if you added it earlier, some unnecessary IPIs would be suppressed right away.

Which IPIs? There is no IPI involved here. We are just setting the flags from the current cpu for all cpus which need to picḱ it up. That's the reason for the comment regarding "lazy flushing".

Juergen

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

#26 lore raw

Re: [PATCH v8 3/9] xen/x86: support per-domain flag for xpti

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 17:54, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 17:33, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:
xen/arch/x86/mm/shadow/multi.c +0 -0
@@ -967,7 +967,7 @@ static int shadow_set_l4e(struct domain *d,
         sh_put_ref(d, osl3mfn, paddr);
     }
 
Quoted 7 lines

xen/arch/x86/mm/shadow/multi.c static int shadow_set_l4e(struct domain *d, · line 973

Quoted 5 lines
xen/arch/x86/mm/shadow/multi.c +1 -1
-    if ( !cpu_has_no_xpti )
+    if ( is_pv_domain(d) && d->arch.pv_domain.xpti )
         /*
          * Lazy flushing is enough: either we do a TLB flush right afterwards
          * which will pick up the new root page table on all affected cpus

How come the is_pv_domain() is appearing only here?

It is mandatory for testing the per-domain xpti flag. I could add it in patch 1 already if you like that better.

Quoted 6 lines
Quoted 2 lines

Well, if you added it earlier, some unnecessary IPIs would be suppressed right away.

Which IPIs? There is no IPI involved here. We are just setting the flags from the current cpu for all cpus which need to picḱ it up. That's the reason for the comment regarding "lazy flushing".

Oh, I did assume flush_root_pgt_mask() would be a function that I had seen in prior versions, invoking the processing of FLUSH_ROOT_PGTBL on remote CPUs.

Clearly with the introduction of a new function you should have dropped my R-b; I'll comment there.

Jan

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

#27 lore raw

PATCH v8 4/9 xen/x86: use invpcid for flushing the TLB

In reply to Juergen Gross

If possible use the INVPCID instruction for flushing the TLB instead of toggling cr4.pge for that purpose.

While at it remove the dependency on cr4.pge being required for mtrr loading, as this will be required later anyway.

Add a command line option "invpcid" for controlling the use of INVPCID (default to true).

---
V6:
- reword invpcid parameter description (Andrew Cooper)
- add __read_mostly to use_invpcid definition (Andrew Cooper)

V5:
- use pre_flush() as an initializer in do_tlb_flush() (Jan Beulich)
- introduce boolean use_invpcid instead of clearing X86_FEATURE_INVPCID (Jan Beulich)

V4:
- option "invpcid" instead of "noinvpcid" (Jan Beulich)

V3:
- new patch

---
 docs/misc/xen-command-line.markdown |  9 +++++++++
 xen/arch/x86/cpu/mtrr/generic.c     | 37 ++++++++++++++++++++++++++-----------
 xen/arch/x86/flushtlb.c             | 29 +++++++++++++++++++----------
 xen/arch/x86/setup.c                |  8 ++++++++
 xen/include/asm-x86/invpcid.h       |  2 ++
 5 files changed, 64 insertions(+), 21 deletions(-)
docs/misc/xen-command-line.markdown +9 -0
index d4f758487a..451a4fa566 100644
@@ -1380,6 +1380,15 @@ Because responsibility for APIC setup is shared between Xen and the
 domain 0 kernel this option is automatically propagated to the domain
 0 command line.
 
+### invpcid (x86)
+> `= <boolean>`
+
+> Default: `true`
+
+By default, Xen will use the INVPCID instruction for TLB management if
+it is available.  This option can be used to cause Xen to fall back to
+older mechanisms, which are generally slower.
+
 ### noirqbalance
 > `= <boolean>`
 
xen/arch/x86/cpu/mtrr/generic.c +26 -11
index e9c0e5e059..7ba0c3f0fe 100644
@@ -5,6 +5,7 @@
 #include <xen/mm.h>
 #include <xen/stdbool.h>
 #include <asm/flushtlb.h>
+#include <asm/invpcid.h>
 #include <asm/io.h>
 #include <asm/mtrr.h>
 #include <asm/msr.h>
@@ -400,8 +401,10 @@ static DEFINE_SPINLOCK(set_atomicity_lock);
  * has been called.
  */
 
-static void prepare_set(void)
+static bool prepare_set(void)
 {
+	unsigned long cr4;
+
 	/*  Note that this is not ideal, since the cache is only flushed/disabled
 	   for this CPU while the MTRRs are changed, but changing this requires
 	   more invasive changes to the way the kernel boots  */
@@ -412,18 +415,24 @@ static void prepare_set(void)
 	write_cr0(read_cr0() | X86_CR0_CD);
 	wbinvd();
 
-	/*  TLB flushing here relies on Xen always using CR4.PGE. */
-	BUILD_BUG_ON(!(XEN_MINIMAL_CR4 & X86_CR4_PGE));
-	write_cr4(read_cr4() & ~X86_CR4_PGE);
+	cr4 = read_cr4();
+	if (cr4 & X86_CR4_PGE)
+		write_cr4(cr4 & ~X86_CR4_PGE);
+	else if (use_invpcid)
+		invpcid_flush_all();
+	else
+		write_cr3(read_cr3());
 
 	/*  Save MTRR state */
 	rdmsrl(MSR_MTRRdefType, deftype);
 
 	/*  Disable MTRRs, and set the default type to uncached  */
 	mtrr_wrmsr(MSR_MTRRdefType, deftype & ~0xcff);
+
+	return cr4 & X86_CR4_PGE;
 }
 
-static void post_set(void)
+static void post_set(bool pge)
 {
 	/* Intel (P6) standard MTRRs */
 	mtrr_wrmsr(MSR_MTRRdefType, deftype);
@@ -432,7 +441,12 @@ static void post_set(void)
 	write_cr0(read_cr0() & ~X86_CR0_CD);
 
 	/*  Reenable CR4.PGE (also flushes the TLB) */
-	write_cr4(read_cr4() | X86_CR4_PGE);
+	if (pge)
+		write_cr4(read_cr4() | X86_CR4_PGE);
+	else if (use_invpcid)
+		invpcid_flush_all();
+	else
+		write_cr3(read_cr3());
 
 	spin_unlock(&set_atomicity_lock);
 }
@@ -441,14 +455,15 @@ static void generic_set_all(void)
 {
 	unsigned long mask, count;
 	unsigned long flags;
+	bool pge;
 
 	local_irq_save(flags);
-	prepare_set();
+	pge = prepare_set();
 
 	/* Actually set the state */
 	mask = set_mtrr_state();
 
-	post_set();
+	post_set(pge);
 	local_irq_restore(flags);
 
 	/*  Use the atomic bitops to update the global mask  */
@@ -457,7 +472,6 @@ static void generic_set_all(void)
 			set_bit(count, &smp_changes_mask);
 		mask >>= 1;
 	}
-	
 }
 
 static void generic_set_mtrr(unsigned int reg, unsigned long base,
@@ -474,11 +488,12 @@ static void generic_set_mtrr(unsigned int reg, unsigned long base,
 {
 	unsigned long flags;
 	struct mtrr_var_range *vr;
+	bool pge;
 
 	vr = &mtrr_state.var_ranges[reg];
 
 	local_irq_save(flags);
-	prepare_set();
+	pge = prepare_set();
 
 	if (size == 0) {
 		/* The invalid bit is kept in the mask, so we simply clear the
@@ -499,7 +514,7 @@ static void generic_set_mtrr(unsigned int reg, unsigned long base,
 		mtrr_wrmsr(MSR_IA32_MTRR_PHYSMASK(reg), vr->mask);
 	}
 
-	post_set();
+	post_set(pge);
 	local_irq_restore(flags);
 }
 
xen/arch/x86/flushtlb.c +19 -10
index e36bd6f383..717f3bd19b 100644
@@ -11,6 +11,7 @@
 #include <xen/smp.h>
 #include <xen/softirq.h>
 #include <asm/flushtlb.h>
+#include <asm/invpcid.h>
 #include <asm/page.h>
 
 /* Debug builds: Wrap frequently to stress-test the wrap logic. */
@@ -72,6 +73,23 @@ static void post_flush(u32 t)
     this_cpu(tlbflush_time) = t;
 }
 
+static void do_tlb_flush(void)
+{
+    u32 t = pre_flush();
+
+    if ( use_invpcid )
+        invpcid_flush_all();
+    else
+    {
+        unsigned long cr4 = read_cr4();
+
+        write_cr4(cr4 ^ X86_CR4_PGE);
+        write_cr4(cr4);
+    }
+
+    post_flush(t);
+}
+
 void switch_cr3(unsigned long cr3)
 {
     unsigned long flags, cr4;
@@ -119,16 +137,7 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
                            : : "m" (*(const char *)(va)) : "memory" );
         }
         else
-        {
-            u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
-
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
-
-            post_flush(t);
-        }
+            do_tlb_flush();
     }
 
     if ( flags & FLUSH_CACHE )
xen/arch/x86/setup.c +8 -0
index f803980b97..164c42cbf1 100644
@@ -62,6 +62,11 @@ boolean_param("nosmp", opt_nosmp);
 static unsigned int __initdata max_cpus;
 integer_param("maxcpus", max_cpus);
 
+/* opt_invpcid: If false, don't use INVPCID instruction even if available. */
+static bool __initdata opt_invpcid = true;
+boolean_param("invpcid", opt_invpcid);
+bool __read_mostly use_invpcid;
+
 unsigned long __read_mostly cr4_pv32_mask;
 
 /* **** Linux config option: propagated to domain0. */
@@ -1546,6 +1551,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)
     if ( cpu_has_fsgsbase )
         set_in_cr4(X86_CR4_FSGSBASE);
 
+    if ( opt_invpcid && cpu_has_invpcid )
+        use_invpcid = true;
+
     init_speculation_mitigations();
 
     init_idle_domain();
xen/include/asm-x86/invpcid.h +2 -0
index b46624a865..edd8b68706 100644
@@ -3,6 +3,8 @@
 
 #include <xen/types.h>
 
+extern bool use_invpcid;
+
 #define INVPCID_TYPE_INDIV_ADDR      0
 #define INVPCID_TYPE_SINGLE_CTXT     1
 #define INVPCID_TYPE_ALL_INCL_GLOBAL 2
Signature · 7 lines
2.13.6


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

PATCH v8 5/9 xen/x86: disable global pages for domains with XPTI active

In reply to Juergen Gross

Instead of flushing the TLB from global pages when switching address spaces with XPTI being active just disable global pages via %cr4 completely when a domain subject to XPTI is active. This avoids the need for extra TLB flushes as loading %cr3 will remove all TLB entries.

In order to avoid states with cr3/cr4 having inconsistent values (e.g. global pages being activated while cr3 already specifies a XPTI address space) move loading of the new cr4 value to write_ptbase() (actually to switch_cr3_cr4() called by write_ptbase()).

This requires to use switch_cr3_cr4() instead of write_ptbase() when building dom0 in order to avoid setting cr4 with cr4.smap set.

---
V7:
- use switch_cr3_cr4() in dom0_build.c

V6:
- don't call read_cr4() multiple times in switch_cr3_cr4() (Andrew Cooper)

V4:
- don't use mmu_cr4_features for setting new cr4 value (Jan Beulich)
- use simpler scheme for setting X86_CR4_PGE in pv_guest_cr4_to_real_cr4() (Jan Beulich)

V3:
- move cr4 loading for all domains from *_ctxt_switch_to() to write_cr3_cr4() called by write_ptbase() (Jan Beulich)
- rebase

---
 xen/arch/x86/domain.c          |  5 -----
 xen/arch/x86/flushtlb.c        | 17 ++++++++++++-----
 xen/arch/x86/mm.c              | 14 +++++++++++---
 xen/arch/x86/pv/dom0_build.c   |  6 +++---
 xen/arch/x86/x86_64/entry.S    | 10 ----------
 xen/common/efi/runtime.c       |  4 ++--
 xen/include/asm-x86/domain.h   |  3 ++-
 xen/include/asm-x86/flushtlb.h |  2 +-
 8 files changed, 31 insertions(+), 30 deletions(-)
xen/arch/x86/domain.c +0 -5
index 3d9c19d055..9b001a03ec 100644
@@ -1523,17 +1523,12 @@ void paravirt_ctxt_switch_from(struct vcpu *v)
 void paravirt_ctxt_switch_to(struct vcpu *v)
 {
     root_pgentry_t *root_pgt = this_cpu(root_pgt);
-    unsigned long cr4;
 
     if ( root_pgt )
         root_pgt[root_table_offset(PERDOMAIN_VIRT_START)] =
             l4e_from_page(v->domain->arch.perdomain_l3_pg,
                           __PAGE_HYPERVISOR_RW);
 
-    cr4 = pv_guest_cr4_to_real_cr4(v);
-    if ( unlikely(cr4 != read_cr4()) )
-        write_cr4(cr4);
-
     if ( unlikely(v->arch.debugreg[7] & DR7_ACTIVE_MASK) )
         activate_debugregs(v);
 
xen/arch/x86/flushtlb.c +12 -5
index 717f3bd19b..d48b900100 100644
@@ -90,20 +90,27 @@ static void do_tlb_flush(void)
     post_flush(t);
 }
 
-void switch_cr3(unsigned long cr3)
+void switch_cr3_cr4(unsigned long cr3, unsigned long cr4)
 {
-    unsigned long flags, cr4;
+    unsigned long flags, old_cr4;
     u32 t;
 
     /* This non-reentrant function is sometimes called in interrupt context. */
     local_irq_save(flags);
 
     t = pre_flush();
-    cr4 = read_cr4();
 
-    write_cr4(cr4 & ~X86_CR4_PGE);
+    old_cr4 = read_cr4();
+    if ( old_cr4 & X86_CR4_PGE )
+    {
+        old_cr4 = cr4 & ~X86_CR4_PGE;
+        write_cr4(old_cr4);
+    }
+
     write_cr3(cr3);
-    write_cr4(cr4);
+
+    if ( old_cr4 != cr4 )
+        write_cr4(cr4);
 
     post_flush(t);
 
xen/arch/x86/mm.c +11 -3
index 9c36614099..73a38e8715 100644
@@ -503,20 +503,28 @@ void make_cr3(struct vcpu *v, mfn_t mfn)
 void write_ptbase(struct vcpu *v)
 {
     struct cpu_info *cpu_info = get_cpu_info();
+    unsigned long new_cr4;
+
+    new_cr4 = (is_pv_vcpu(v) && !is_idle_vcpu(v))
+              ? pv_guest_cr4_to_real_cr4(v)
+              : ((read_cr4() & ~X86_CR4_TSD) | X86_CR4_PGE);
 
     if ( is_pv_vcpu(v) && v->domain->arch.pv_domain.xpti )
     {
         cpu_info->root_pgt_changed = true;
         cpu_info->pv_cr3 = __pa(this_cpu(root_pgt));
-        switch_cr3(v->arch.cr3);
+        switch_cr3_cr4(v->arch.cr3, new_cr4);
     }
     else
     {
-        /* Make sure to clear xen_cr3 before pv_cr3; switch_cr3() serializes. */
+        /* Make sure to clear xen_cr3 before pv_cr3. */
         cpu_info->xen_cr3 = 0;
-        switch_cr3(v->arch.cr3);
+        /* switch_cr3_cr4() serializes. */
+        switch_cr3_cr4(v->arch.cr3, new_cr4);
         cpu_info->pv_cr3 = 0;
     }
+
+    ASSERT(is_pv_vcpu(v) || read_cr4() == mmu_cr4_features);
 }
 
 /*
xen/arch/x86/pv/dom0_build.c +3 -3
index d148395919..4465a059a8 100644
@@ -717,7 +717,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build process. */
-    write_ptbase(v);
+    switch_cr3_cr4(v->arch.cr3, read_cr4());
     mapcache_override_current(v);
 
     /* Copy the OS image and free temporary buffer. */
@@ -738,7 +738,7 @@ int __init dom0_construct_pv(struct domain *d,
              (parms.virt_hypercall >= v_end) )
         {
             mapcache_override_current(NULL);
-            write_ptbase(current);
+            switch_cr3_cr4(current->arch.cr3, read_cr4());
             printk("Invalid HYPERCALL_PAGE field in ELF notes.\n");
             rc = -1;
             goto out;
@@ -871,7 +871,7 @@ int __init dom0_construct_pv(struct domain *d,
 
     /* Return to idle domain's page tables. */
     mapcache_override_current(NULL);
-    write_ptbase(current);
+    switch_cr3_cr4(current->arch.cr3, read_cr4());
 
     update_domain_wallclock_time(d);
 
xen/arch/x86/x86_64/entry.S +0 -10
index dd42223b20..5f0758d64f 100644
@@ -153,13 +153,8 @@ restore_all_guest:
                 ROOT_PAGETABLE_LAST_XEN_SLOT - 1) * 8, %rdi
         rep movsq
 .Lrag_copy_done:
-        mov   STACK_CPUINFO_FIELD(cr4)(%rdx), %rdi
         mov   %r9, STACK_CPUINFO_FIELD(xen_cr3)(%rdx)
-        mov   %rdi, %rsi
-        and   $~X86_CR4_PGE, %rdi
-        mov   %rdi, %cr4
         mov   %rax, %cr3
-        mov   %rsi, %cr4
 .Lrag_keep_cr3:
 
         /* Restore stashed SPEC_CTRL value. */
@@ -215,12 +210,7 @@ restore_all_xen:
          * so "g" will have to do.
          */
 UNLIKELY_START(g, exit_cr3)
-        mov   %cr4, %rdi
-        mov   %rdi, %rsi
-        and   $~X86_CR4_PGE, %rdi
-        mov   %rdi, %cr4
         mov   %rax, %cr3
-        mov   %rsi, %cr4
 UNLIKELY_END(exit_cr3)
 
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
xen/common/efi/runtime.c +2 -2
index 4e5ddfef4f..070a70d784 100644
@@ -111,7 +111,7 @@ struct efi_rs_state efi_rs_enter(void)
         lgdt(&gdt_desc);
     }
 
-    switch_cr3(virt_to_maddr(efi_l4_pgtable));
+    switch_cr3_cr4(virt_to_maddr(efi_l4_pgtable), read_cr4());
 
     return state;
 }
@@ -120,7 +120,7 @@ void efi_rs_leave(struct efi_rs_state *state)
 {
     if ( !state->cr3 )
         return;
-    switch_cr3(state->cr3);
+    switch_cr3_cr4(state->cr3, read_cr4());
     if ( is_pv_vcpu(current) && !is_idle_vcpu(current) )
     {
         struct desc_ptr gdt_desc = {
xen/include/asm-x86/domain.h +2 -1
index ed4199931a..b7894dc8c8 100644
@@ -618,9 +618,10 @@ unsigned long pv_guest_cr4_fixup(const struct vcpu *, unsigned long guest_cr4);
 #define pv_guest_cr4_to_real_cr4(v)                         \
     (((v)->arch.pv_vcpu.ctrlreg[4]                          \
       | (mmu_cr4_features                                   \
-         & (X86_CR4_PGE | X86_CR4_PSE | X86_CR4_SMEP |      \
+         & (X86_CR4_PSE | X86_CR4_SMEP |                    \
             X86_CR4_SMAP | X86_CR4_OSXSAVE |                \
             X86_CR4_FSGSBASE))                              \
+      | ((v)->domain->arch.pv_domain.xpti ? 0 : X86_CR4_PGE) \
       | ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0))         \
      & ~X86_CR4_DE)
 #define real_cr4_to_pv_guest_cr4(c)                         \
xen/include/asm-x86/flushtlb.h +1 -1
index c150f82ca2..6356e85101 100644
@@ -84,7 +84,7 @@ static inline unsigned long read_cr3(void)
 }
 
 /* Write pagetable base and implicitly tick the tlbflush clock. */
-void switch_cr3(unsigned long cr3);
+void switch_cr3_cr4(unsigned long cr3, unsigned long cr4);
 
 /* flush_* flag fields: */
  /*
Signature · 7 lines
2.13.6


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

PATCH v8 6/9 xen/x86: use flag byte for decision whether xen_cr3 is valid

In reply to Juergen Gross

Today cpu_info->xen_cr3 is either 0 to indicate %cr3 doesn't need to be switched on entry to Xen, or negative for keeping the value while indicating not to restore %cr3, or positive in case %cr3 is to be restored.

Switch to use a flag byte instead of a negative xen_cr3 value in order to allow %cr3 values with the high bit set in case we want to keep TLB entries when using the PCID feature.

This reduces the number of branches in interrupt handling and results in better performance (e.g. parallel make of the Xen hypervisor on my system was using about 3% less system time).

---
V3:
- renamed use_xen_cr3 to better fitting use_pv_cr3
- corrected comment regarding semantics of use_pv_cr3 (Jan Beulich)
- prefer 32-bit operations over 8- or 16-bit ones (Jan Beulich)

---
 xen/arch/x86/domain.c              |  1 +
 xen/arch/x86/mm.c                  |  3 +-
 xen/arch/x86/smpboot.c             |  2 ++
 xen/arch/x86/x86_64/asm-offsets.c  |  1 +
 xen/arch/x86/x86_64/compat/entry.S |  5 ++--
 xen/arch/x86/x86_64/entry.S        | 59 ++++++++++++++++----------------------
 xen/include/asm-x86/current.h      | 12 +++++---
 7 files changed, 41 insertions(+), 42 deletions(-)
xen/arch/x86/domain.c +1 -0
index 9b001a03ec..801ac33810 100644
@@ -1696,6 +1696,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
 
     ASSERT(local_irq_is_enabled());
 
+    get_cpu_info()->use_pv_cr3 = false;
     get_cpu_info()->xen_cr3 = 0;
 
     if ( unlikely(dirty_cpu != cpu) && dirty_cpu != VCPU_CPU_CLEAN )
xen/arch/x86/mm.c +2 -1
index 73a38e8715..4997047edf 100644
@@ -517,7 +517,8 @@ void write_ptbase(struct vcpu *v)
     }
     else
     {
-        /* Make sure to clear xen_cr3 before pv_cr3. */
+        /* Make sure to clear use_pv_cr3 and xen_cr3 before pv_cr3. */
+        cpu_info->use_pv_cr3 = false;
         cpu_info->xen_cr3 = 0;
         /* switch_cr3_cr4() serializes. */
         switch_cr3_cr4(v->arch.cr3, new_cr4);
xen/arch/x86/smpboot.c +2 -0
index 980192e71f..d21020c510 100644
@@ -324,6 +324,7 @@ void start_secondary(void *unused)
      */
     spin_debug_disable();
 
+    get_cpu_info()->use_pv_cr3 = false;
     get_cpu_info()->xen_cr3 = 0;
     get_cpu_info()->pv_cr3 = 0;
 
@@ -1123,6 +1124,7 @@ void __init smp_prepare_boot_cpu(void)
     per_cpu(scratch_cpumask, cpu) = &scratch_cpu0mask;
 #endif
 
+    get_cpu_info()->use_pv_cr3 = false;
     get_cpu_info()->xen_cr3 = 0;
     get_cpu_info()->pv_cr3 = 0;
 }
xen/arch/x86/x86_64/asm-offsets.c +1 -0
index 9e2aefb00f..7ad024cf37 100644
@@ -144,6 +144,7 @@ void __dummy__(void)
     OFFSET(CPUINFO_use_shadow_spec_ctrl, struct cpu_info, use_shadow_spec_ctrl);
     OFFSET(CPUINFO_bti_ist_info, struct cpu_info, bti_ist_info);
     OFFSET(CPUINFO_root_pgt_changed, struct cpu_info, root_pgt_changed);
+    OFFSET(CPUINFO_use_pv_cr3, struct cpu_info, use_pv_cr3);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
     BLANK();
 
xen/arch/x86/x86_64/compat/entry.S +2 -3
index ae2bb4bf1e..b909977e33 100644
@@ -210,10 +210,9 @@ ENTRY(cstar_enter)
 
         GET_STACK_END(bx)
         mov   STACK_CPUINFO_FIELD(xen_cr3)(%rbx), %rcx
-        neg   %rcx
+        test  %rcx, %rcx
         jz    .Lcstar_cr3_okay
-        mov   %rcx, STACK_CPUINFO_FIELD(xen_cr3)(%rbx)
-        neg   %rcx
+        movb  $0, STACK_CPUINFO_FIELD(use_pv_cr3)(%rbx)
         mov   %rcx, %cr3
         movq  $0, STACK_CPUINFO_FIELD(xen_cr3)(%rbx)
 .Lcstar_cr3_okay:
xen/arch/x86/x86_64/entry.S +25 -34

163 lines not on this page · raw message

xen/include/asm-x86/current.h +8 -4
index b2475783f8..43bdec1f49 100644
@@ -47,10 +47,7 @@ struct cpu_info {
      * context is being entered. A value of zero indicates no setting of CR3
      * is to be performed.
      * The former is the value to restore when re-entering Xen, if any. IOW
-     * its value being zero means there's nothing to restore. However, its
-     * value can also be negative, indicating to the exit-to-Xen code that
-     * restoring is not necessary, but allowing any nested entry code paths
-     * to still know the value to put back into CR3.
+     * its value being zero means there's nothing to restore.
      */
     unsigned long xen_cr3;
     unsigned long pv_cr3;
@@ -68,6 +65,13 @@ struct cpu_info {
      */
     bool         root_pgt_changed;
 
+    /*
+     * use_pv_cr3 is set in case the value of pv_cr3 is to be written into
+     * CR3 when returning from an interrupt. The main use is when returning
+     * from a NMI or MCE to hypervisor code where pv_cr3 was active.
+     */
+    bool         use_pv_cr3;
+
     unsigned long __pad;
     /* get_stack_bottom() must be 16-byte aligned */
 };
Signature · 7 lines
2.13.6


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

PATCH v8 7/9 xen/x86: convert pv_guest_cr4_to_real_cr4() to a function

In reply to Juergen Gross

pv_guest_cr4_to_real_cr4() is becoming more and more complex. Convert it from a macro to an ordinary function.

---
V6:
- new patch, split off from (old) patch 7 (Andrew Cooper)

---
 xen/arch/x86/mm.c            | 14 ++++++++++++++
 xen/include/asm-x86/domain.h | 11 ++---------
 2 files changed, 16 insertions(+), 9 deletions(-)
xen/arch/x86/mm.c +14 -0
index 4997047edf..6aa0c34bae 100644
@@ -500,6 +500,20 @@ void make_cr3(struct vcpu *v, mfn_t mfn)
     v->arch.cr3 = mfn_x(mfn) << PAGE_SHIFT;
 }
 
+unsigned long pv_guest_cr4_to_real_cr4(const struct vcpu *v)
+{
+    const struct domain *d = v->domain;
+    unsigned long cr4;
+
+    cr4 = v->arch.pv_vcpu.ctrlreg[4] & ~X86_CR4_DE;
+    cr4 |= mmu_cr4_features & (X86_CR4_PSE | X86_CR4_SMEP | X86_CR4_SMAP |
+                               X86_CR4_OSXSAVE | X86_CR4_FSGSBASE);
+    cr4 |= d->arch.pv_domain.xpti  ? 0 : X86_CR4_PGE;
+    cr4 |= d->arch.vtsc ? X86_CR4_TSD : 0;
+
+    return cr4;
+}
+
 void write_ptbase(struct vcpu *v)
 {
     struct cpu_info *cpu_info = get_cpu_info();
xen/include/asm-x86/domain.h +2 -9
index b7894dc8c8..9627058cd0 100644
@@ -615,15 +615,8 @@ void vcpu_show_registers(const struct vcpu *);
 unsigned long pv_guest_cr4_fixup(const struct vcpu *, unsigned long guest_cr4);
 
 /* Convert between guest-visible and real CR4 values. */
-#define pv_guest_cr4_to_real_cr4(v)                         \
-    (((v)->arch.pv_vcpu.ctrlreg[4]                          \
-      | (mmu_cr4_features                                   \
-         & (X86_CR4_PSE | X86_CR4_SMEP |                    \
-            X86_CR4_SMAP | X86_CR4_OSXSAVE |                \
-            X86_CR4_FSGSBASE))                              \
-      | ((v)->domain->arch.pv_domain.xpti ? 0 : X86_CR4_PGE) \
-      | ((v)->domain->arch.vtsc ? X86_CR4_TSD : 0))         \
-     & ~X86_CR4_DE)
+unsigned long pv_guest_cr4_to_real_cr4(const struct vcpu *v);
+
 #define real_cr4_to_pv_guest_cr4(c)                         \
     ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD |      \
              X86_CR4_OSXSAVE | X86_CR4_SMEP |               \
Signature · 7 lines
2.13.6


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

PATCH v8 8/9 xen/x86: add some cr3 helpers

In reply to Juergen Gross

Add some helper macros to access the address and pcid parts of cr3.

Use those helpers where appropriate.

---
V6:
- new patch (Andrew Cooper)

---
 xen/arch/x86/debug.c            |  2 +-
 xen/arch/x86/domain_page.c      |  2 +-
 xen/include/asm-x86/processor.h | 10 ++++++++++
 xen/include/asm-x86/x86-defns.h |  4 +++-
 4 files changed, 15 insertions(+), 3 deletions(-)
xen/arch/x86/debug.c +1 -1
index 9159f32db4..a500df01ac 100644
@@ -98,7 +98,7 @@ dbg_pv_va2mfn(dbgva_t vaddr, struct domain *dp, uint64_t pgd3val)
     l2_pgentry_t l2e, *l2t;
     l1_pgentry_t l1e, *l1t;
     unsigned long cr3 = (pgd3val ? pgd3val : dp->vcpu[0]->arch.cr3);
-    mfn_t mfn = maddr_to_mfn(cr3);
+    mfn_t mfn = maddr_to_mfn(cr3_pa(cr3));
 
     DBGP2("vaddr:%lx domid:%d cr3:%lx pgd3:%lx\n", vaddr, dp->domain_id, 
           cr3, pgd3val);
xen/arch/x86/domain_page.c +1 -1
index 11b6a5421a..0c24530ed9 100644
@@ -51,7 +51,7 @@ static inline struct vcpu *mapcache_current_vcpu(void)
         if ( (v = idle_vcpu[smp_processor_id()]) == current )
             sync_local_execstate();
         /* We must now be running on the idle page table. */
-        ASSERT(read_cr3() == __pa(idle_pg_table));
+        ASSERT(cr3_pa(read_cr3()) == __pa(idle_pg_table));
     }
 
     return v;
xen/include/asm-x86/processor.h +10 -0
index 71d32c0333..36628459dc 100644
@@ -288,6 +288,16 @@ static inline void write_cr3(unsigned long val)
     asm volatile ( "mov %0, %%cr3" : : "r" (val) : "memory" );
 }
 
+static inline unsigned long cr3_pa(unsigned long cr3)
+{
+    return cr3 & X86_CR3_ADDR_MASK;
+}
+
+static inline unsigned long cr3_pcid(unsigned long cr3)
+{
+    return cr3 & X86_CR3_PCID_MASK;
+}
+
 static inline unsigned long read_cr4(void)
 {
     return get_cpu_info()->cr4;
xen/include/asm-x86/x86-defns.h +3 -1
index ff8d66be3c..904041e1ab 100644
@@ -45,7 +45,9 @@
 /*
  * Intel CPU flags in CR3
  */
-#define X86_CR3_NOFLUSH (_AC(1, ULL) << 63)
+#define X86_CR3_NOFLUSH    (_AC(1, ULL) << 63)
+#define X86_CR3_ADDR_MASK  (PAGE_MASK & PADDR_MASK)
+#define X86_CR3_PCID_MASK  _AC(0x0fff, ULL) /* Mask for PCID */
 
 /*
  * Intel CPU features in CR4
Signature · 7 lines
2.13.6


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

PATCH v8 9/9 xen/x86: use PCID feature

In reply to Juergen Gross

Avoid flushing the complete TLB when switching %cr3 for mitigation of Meltdown by using the PCID feature if available.

We are using 4 PCID values for a 64 bit pv domain subject to XPTI and 2 values for the non-XPTI case:

- guest active and in kernel mode
- guest active and in user mode
- hypervisor active and guest in user mode (XPTI only)
- hypervisor active and guest in kernel mode (XPTI only)

We use PCID only if PCID _and_ INVPCID are supported. With PCID in use we disable global pages in cr4. A command line parameter controls in which cases PCID is being used.

As the non-XPTI case has shown not to perform better with PCID at least on some machines the default is to use PCID only for domains subject to XPTI.

With PCID enabled we always disable global pages. This avoids having to either flush the complete TLB or do a cycle through all PCID values when invalidating a single global page.

---
V6.1:
- address some minor comments (Jan Beulich)

V6:
- split off pv_guest_cr4_to_real_cr4() conversion to function into new patch (Andrew Cooper)
- changed some comments (Jan Beulich, Andrew Cooper)

V5:
- use X86_CR3_ADDR_MASK instead of ~X86_CR3_PCID_MASK (Jan Beulich)
- add some const qualifiers (Jan Beulich)
- mask X86_CR3_ADDR_MASK with PADDR_MASK (Jan Beulich)
- add flushing the TLB from old PCID related entries in write_cr3_cr4() (Jan Beulich)

V4:
- add cr3 mask for page table address and use that in dbg_pv_va2mfn() (Jan Beulich)
- use invpcid_flush_all_nonglobals() instead of invpcid_flush_all() (Jan Beulich)
- use PCIDs 0/1 when running in Xen or without XPTI, 2/3 with XPTI in guest (Jan Beulich)
- ASSERT cr4.pge and cr4.pcide are never active at the same time (Jan Beulich)
- make pv_guest_cr4_to_real_cr4() a real function

V3:
- support PCID for non-XPTI case, too
- add command line parameter for controlling usage of PCID
- check PCID active by using cr4.pcide (Jan Beulich)

---
 docs/misc/xen-command-line.markdown | 14 +++++++
 xen/arch/x86/flushtlb.c             | 47 ++++++++++++++++++++-
 xen/arch/x86/mm.c                   | 16 +++++++-
 xen/arch/x86/pv/dom0_build.c        |  3 +-
 xen/arch/x86/pv/domain.c            | 81 ++++++++++++++++++++++++++++++++++++-
 xen/include/asm-x86/domain.h        |  4 +-
 xen/include/asm-x86/processor.h     |  3 ++
 xen/include/asm-x86/pv/domain.h     | 31 ++++++++++++++
 8 files changed, 192 insertions(+), 7 deletions(-)
docs/misc/xen-command-line.markdown +14 -0
index 451a4fa566..f8950a3bb2 100644
@@ -1451,6 +1451,20 @@ All numbers specified must be hexadecimal ones.
 
 This option can be specified more than once (up to 8 times at present).
 
+### pcid (x86)
+> `= <boolean> | xpti=<bool>`
+
+> Default: `xpti`
+
+> Can be modified at runtime (change takes effect only for domains created
+  afterwards)
+
+If available, control usage of the PCID feature of the processor for
+64-bit pv-domains. PCID can be used either for no domain at all (`false`),
+for all of them (`true`), only for those subject to XPTI (`xpti`) or for
+those not subject to XPTI (`no-xpti`). The feature is used only in case
+INVPCID is supported and not disabled via `invpcid=false`.
+
 ### ple\_gap
 > `= <integer>`
 
xen/arch/x86/flushtlb.c +45 -2

79 lines not on this page · raw message

xen/arch/x86/mm.c +14 -2
index 6aa0c34bae..d981ce0b19 100644
@@ -126,6 +126,7 @@
 #include <asm/hvm/ioreq.h>
 
 #include <asm/hvm/grant_table.h>
+#include <asm/pv/domain.h>
 #include <asm/pv/grant_table.h>
 #include <asm/pv/mm.h>
 
@@ -497,7 +498,11 @@ void free_shared_domheap_page(struct page_info *page)
 
 void make_cr3(struct vcpu *v, mfn_t mfn)
 {
+    struct domain *d = v->domain;
+
     v->arch.cr3 = mfn_x(mfn) << PAGE_SHIFT;
+    if ( is_pv_domain(d) && d->arch.pv_domain.pcid )
+        v->arch.cr3 |= get_pcid_bits(v, false);
 }
 
 unsigned long pv_guest_cr4_to_real_cr4(const struct vcpu *v)
@@ -508,7 +513,12 @@ unsigned long pv_guest_cr4_to_real_cr4(const struct vcpu *v)
     cr4 = v->arch.pv_vcpu.ctrlreg[4] & ~X86_CR4_DE;
     cr4 |= mmu_cr4_features & (X86_CR4_PSE | X86_CR4_SMEP | X86_CR4_SMAP |
                                X86_CR4_OSXSAVE | X86_CR4_FSGSBASE);
-    cr4 |= d->arch.pv_domain.xpti  ? 0 : X86_CR4_PGE;
+
+    if ( d->arch.pv_domain.pcid )
+        cr4 |= X86_CR4_PCIDE;
+    else if ( !d->arch.pv_domain.xpti )
+        cr4 |= X86_CR4_PGE;
+
     cr4 |= d->arch.vtsc ? X86_CR4_TSD : 0;
 
     return cr4;
@@ -521,12 +531,14 @@ void write_ptbase(struct vcpu *v)
 
     new_cr4 = (is_pv_vcpu(v) && !is_idle_vcpu(v))
               ? pv_guest_cr4_to_real_cr4(v)
-              : ((read_cr4() & ~X86_CR4_TSD) | X86_CR4_PGE);
+              : ((read_cr4() & ~(X86_CR4_PCIDE | X86_CR4_TSD)) | X86_CR4_PGE);
 
     if ( is_pv_vcpu(v) && v->domain->arch.pv_domain.xpti )
     {
         cpu_info->root_pgt_changed = true;
         cpu_info->pv_cr3 = __pa(this_cpu(root_pgt));
+        if ( new_cr4 & X86_CR4_PCIDE )
+            cpu_info->pv_cr3 |= get_pcid_bits(v, true);
         switch_cr3_cr4(v->arch.cr3, new_cr4);
     }
     else
xen/arch/x86/pv/dom0_build.c +2 -1
index 4465a059a8..34c77bcbe4 100644
@@ -388,6 +388,7 @@ int __init dom0_construct_pv(struct domain *d,
     {
         d->arch.is_32bit_pv = d->arch.has_32bit_shinfo = 1;
         d->arch.pv_domain.xpti = false;
+        d->arch.pv_domain.pcid = false;
         v->vcpu_info = (void *)&d->shared_info->compat.vcpu_info[0];
         if ( setup_compat_arg_xlat(v) != 0 )
             BUG();
@@ -717,7 +718,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build process. */
-    switch_cr3_cr4(v->arch.cr3, read_cr4());
+    switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());
     mapcache_override_current(v);
 
     /* Copy the OS image and free temporary buffer. */
xen/arch/x86/pv/domain.c +80 -1

110 lines not on this page · raw message

xen/include/asm-x86/domain.h +3 -1
index 9627058cd0..8b66096e7f 100644
@@ -255,6 +255,8 @@ struct pv_domain
 
     /* XPTI active? */
     bool xpti;
+    /* Use PCID feature? */
+    bool pcid;
 
     /* map_domain_page() mapping cache. */
     struct mapcache_domain mapcache;
@@ -620,7 +622,7 @@ unsigned long pv_guest_cr4_to_real_cr4(const struct vcpu *v);
 #define real_cr4_to_pv_guest_cr4(c)                         \
     ((c) & ~(X86_CR4_PGE | X86_CR4_PSE | X86_CR4_TSD |      \
              X86_CR4_OSXSAVE | X86_CR4_SMEP |               \
-             X86_CR4_FSGSBASE | X86_CR4_SMAP))
+             X86_CR4_FSGSBASE | X86_CR4_SMAP | X86_CR4_PCIDE))
 
 #define domain_max_vcpus(d) (is_hvm_domain(d) ? HVM_MAX_VCPUS : MAX_VIRT_CPUS)
 
xen/include/asm-x86/processor.h +3 -0
index 36628459dc..c4aa385a6f 100644
@@ -305,6 +305,9 @@ static inline unsigned long read_cr4(void)
 
 static inline void write_cr4(unsigned long val)
 {
+    /* No global pages in case of PCIDs enabled! */
+    ASSERT(!(val & X86_CR4_PGE) || !(val & X86_CR4_PCIDE));
+
     get_cpu_info()->cr4 = val;
     asm volatile ( "mov %0,%%cr4" : : "r" (val) );
 }
xen/include/asm-x86/pv/domain.h +31 -0
index 5e34176939..4fea76444a 100644
@@ -21,6 +21,37 @@
 #ifndef __X86_PV_DOMAIN_H__
 #define __X86_PV_DOMAIN_H__
 
+/*
+ * PCID values for the address spaces of 64-bit pv domains:
+ *
+ * We are using 4 PCID values for a 64 bit pv domain subject to XPTI:
+ * - hypervisor active and guest in kernel mode   PCID 0
+ * - hypervisor active and guest in user mode     PCID 1
+ * - guest active and in kernel mode              PCID 2
+ * - guest active and in user mode                PCID 3
+ *
+ * Without XPTI only 2 values are used:
+ * - guest in kernel mode                         PCID 0
+ * - guest in user mode                           PCID 1
+ */
+
+#define PCID_PV_PRIV      0x0000    /* Used for other domains, too. */
+#define PCID_PV_USER      0x0001
+#define PCID_PV_XPTI      0x0002    /* To be ORed to above values. */
+
+/*
+ * Return additional PCID specific cr3 bits.
+ *
+ * Note that X86_CR3_NOFLUSH will not be readable in cr3. Anyone consuming
+ * v->arch.cr3 should mask away X86_CR3_NOFLUSH and X86_CR3_PCIDMASK in case
+ * the value is used to address the root page table.
+ */
+static inline unsigned long get_pcid_bits(const struct vcpu *v, bool is_xpti)
+{
+    return X86_CR3_NOFLUSH | (is_xpti ? PCID_PV_XPTI : 0) |
+           ((v->arch.flags & TF_kernel_mode) ? PCID_PV_PRIV : PCID_PV_USER);
+}
+
 #ifdef CONFIG_PV
 
 void pv_vcpu_destroy(struct vcpu *v);
Signature · 7 lines
2.13.6


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

Re: [PATCH v8 9/9] xen/x86: use PCID feature

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:

Avoid flushing the complete TLB when switching %cr3 for mitigation of Meltdown by using the PCID feature if available.

We are using 4 PCID values for a 64 bit pv domain subject to XPTI and 2 values for the non-XPTI case:

- guest active and in kernel mode
- guest active and in user mode
- hypervisor active and guest in user mode (XPTI only)
- hypervisor active and guest in kernel mode (XPTI only)

We use PCID only if PCID _and_ INVPCID are supported. With PCID in use we disable global pages in cr4. A command line parameter controls in which cases PCID is being used.

As the non-XPTI case has shown not to perform better with PCID at least on some machines the default is to use PCID only for domains subject to XPTI.

With PCID enabled we always disable global pages. This avoids having to either flush the complete TLB or do a cycle through all PCID values when invalidating a single global page.

Quoted 5 lines

---
V6.1:
- address some minor comments (Jan Beulich)

No v7 changes? Afaict ...

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 721

Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1 -1
@@ -717,7 +718,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build process. */
-    switch_cr3_cr4(v->arch.cr3, read_cr4());
+    switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());

... at least this was added. And to be honest I'm not convinced cr3_pa() is the right construct to use here: It's neither clear why the other bits don't matter at this point, nor why any of the bits that you mask out this way need masking out in the first place (e.g. why, in the crash case that Andrew had observed, the noflush bit was wrongly set).

I hope there aren't any other such hidden changes in this version of the series.

Jan

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

#34 lore raw

Re: [PATCH v8 9/9] xen/x86: use PCID feature

Quoted 35 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:

Avoid flushing the complete TLB when switching %cr3 for mitigation of Meltdown by using the PCID feature if available.

We are using 4 PCID values for a 64 bit pv domain subject to XPTI and 2 values for the non-XPTI case:

- guest active and in kernel mode
- guest active and in user mode
- hypervisor active and guest in user mode (XPTI only)
- hypervisor active and guest in kernel mode (XPTI only)

We use PCID only if PCID _and_ INVPCID are supported. With PCID in use we disable global pages in cr4. A command line parameter controls in which cases PCID is being used.

As the non-XPTI case has shown not to perform better with PCID at least on some machines the default is to use PCID only for domains subject to XPTI.

With PCID enabled we always disable global pages. This avoids having to either flush the complete TLB or do a cycle through all PCID values when invalidating a single global page.

Quoted 5 lines

---
V6.1:
- address some minor comments (Jan Beulich)

No v7 changes? Afaict ...

Quoted 3 lines
(quoted diff) +0 -0
@@ -717,7 +718,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
Quoted 9 lines

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 721

Quoted 3 lines
xen/arch/x86/pv/dom0_build.c +1 -1
     /* We run on dom0's page tables for the final part of the build process. */
-    switch_cr3_cr4(v->arch.cr3, read_cr4());
+    switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());

... at least this was added. And to be honest I'm not convinced cr3_pa() is the right construct to use here: It's neither clear why the other bits don't matter at this point, nor why any of the bits that you mask out this way need masking out in the first place (e.g. why, in the crash case that Andrew had observed, the noflush bit was wrongly set).

At this point in time we only want to use another cr3 value. So PCIDs should _not_ be used as this would require adapting cr4, resulting in writing the cr3_pa() bits only. It would have been possible to use write_cr3() here, but only together with open coding a TLB flush in order to avoid any global pages remaining in the TLB. So I decided to use switch_cr3_cr4().

The noflush bit was set as dom0 is subject to XPTI and PCID usage and update_cr3() is updating v->arch.cr3 accordingly.

I know you are not fond of setting noflush in v->arch.cr3. The only sensible alternative to that would be adding something like v->arch.cr3_pcid_bits being or-ed to the cr3 value in restore_all_guest. This would let us get rid of having to use cr3_pa() in some places while adding a (very little) performance penalty.

Quoted 2 lines

I hope there aren't any other such hidden changes in this version of the series.

Oh sorry, I missed to update the history.

I'm not aware of other changes in v8 (apart from patch 1 and the resulting needed change in patch 3).

Juergen

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

#35 lore raw

Re: [PATCH v8 9/9] xen/x86: use PCID feature

Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 11:37, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:

Avoid flushing the complete TLB when switching %cr3 for mitigation of Meltdown by using the PCID feature if available.

We are using 4 PCID values for a 64 bit pv domain subject to XPTI and 2 values for the non-XPTI case:

- guest active and in kernel mode
- guest active and in user mode
- hypervisor active and guest in user mode (XPTI only)
- hypervisor active and guest in kernel mode (XPTI only)

We use PCID only if PCID _and_ INVPCID are supported. With PCID in use we disable global pages in cr4. A command line parameter controls in which cases PCID is being used.

As the non-XPTI case has shown not to perform better with PCID at least on some machines the default is to use PCID only for domains subject to XPTI.

With PCID enabled we always disable global pages. This avoids having to either flush the complete TLB or do a cycle through all PCID values when invalidating a single global page.

Quoted 5 lines

---
V6.1:
- address some minor comments (Jan Beulich)

Quoted 6 lines

No v7 changes? Afaict ...

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 720

Quoted 4 lines
xen/arch/x86/pv/dom0_build.c +0 -0
@@ -717,7 +718,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build 

process. */

Quoted 8 lines

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 721

Quoted 2 lines
xen/arch/x86/pv/dom0_build.c +1 -1
-    switch_cr3_cr4(v->arch.cr3, read_cr4());
+    switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());

... at least this was added. And to be honest I'm not convinced cr3_pa() is the right construct to use here: It's neither clear why the other bits don't matter at this point, nor why any of the bits that you mask out this way need masking out in the first place (e.g. why, in the crash case that Andrew had observed, the noflush bit was wrongly set).

At this point in time we only want to use another cr3 value. So PCIDs should _not_ be used as this would require adapting cr4, resulting in writing the cr3_pa() bits only. It would have been possible to use write_cr3() here, but only together with open coding a TLB flush in order to avoid any global pages remaining in the TLB. So I decided to use switch_cr3_cr4().

Quoted 8 lines

The noflush bit was set as dom0 is subject to XPTI and PCID usage and update_cr3() is updating v->arch.cr3 accordingly.

I know you are not fond of setting noflush in v->arch.cr3. The only sensible alternative to that would be adding something like v->arch.cr3_pcid_bits being or-ed to the cr3 value in restore_all_guest. This would let us get rid of having to use cr3_pa() in some places while adding a (very little) performance penalty.

Well, I had accepted the current approach as the (performance wise) better alternative, but with Andrew sharing that original concern of mine, and with there having been a first real problem resulting from that approach, more seriously considering the alternative certainly seems necessary. Andrew?

Quoted 7 lines
Quoted 2 lines

I hope there aren't any other such hidden changes in this version of the series.

Oh sorry, I missed to update the history.

I'm not aware of other changes in v8 (apart from patch 1 and the resulting needed change in patch 3).

Ah - I hadn't spotted that follow-on change, and I've just sent a small comment in its regard.

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#37 lore raw

Re: [PATCH v8 9/9] xen/x86: use PCID feature

Quoted 64 lines · Jan Beulich
Quoted 55 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 11:37, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 18.04.18 at 10:30, <[email protected]> wrote:

Avoid flushing the complete TLB when switching %cr3 for mitigation of Meltdown by using the PCID feature if available.

We are using 4 PCID values for a 64 bit pv domain subject to XPTI and 2 values for the non-XPTI case:

- guest active and in kernel mode
- guest active and in user mode
- hypervisor active and guest in user mode (XPTI only)
- hypervisor active and guest in kernel mode (XPTI only)

We use PCID only if PCID _and_ INVPCID are supported. With PCID in use we disable global pages in cr4. A command line parameter controls in which cases PCID is being used.

As the non-XPTI case has shown not to perform better with PCID at least on some machines the default is to use PCID only for domains subject to XPTI.

With PCID enabled we always disable global pages. This avoids having to either flush the complete TLB or do a cycle through all PCID values when invalidating a single global page.

Quoted 5 lines

---
V6.1:
- address some minor comments (Jan Beulich)

Quoted 6 lines

No v7 changes? Afaict ...

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 720

Quoted 4 lines
xen/arch/x86/pv/dom0_build.c +0 -0
@@ -717,7 +718,7 @@ int __init dom0_construct_pv(struct domain *d,
         update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build 

process. */

Quoted 8 lines

xen/arch/x86/pv/dom0_build.c int __init dom0_construct_pv(struct domain *d, · line 721

Quoted 2 lines
xen/arch/x86/pv/dom0_build.c +1 -1
-    switch_cr3_cr4(v->arch.cr3, read_cr4());
+    switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());

... at least this was added. And to be honest I'm not convinced cr3_pa() is the right construct to use here: It's neither clear why the other bits don't matter at this point, nor why any of the bits that you mask out this way need masking out in the first place (e.g. why, in the crash case that Andrew had observed, the noflush bit was wrongly set).

At this point in time we only want to use another cr3 value. So PCIDs should _not_ be used as this would require adapting cr4, resulting in writing the cr3_pa() bits only. It would have been possible to use write_cr3() here, but only together with open coding a TLB flush in order to avoid any global pages remaining in the TLB. So I decided to use switch_cr3_cr4().

Quoted 8 lines

The noflush bit was set as dom0 is subject to XPTI and PCID usage and update_cr3() is updating v->arch.cr3 accordingly.

I know you are not fond of setting noflush in v->arch.cr3. The only sensible alternative to that would be adding something like v->arch.cr3_pcid_bits being or-ed to the cr3 value in restore_all_guest. This would let us get rid of having to use cr3_pa() in some places while adding a (very little) performance penalty.

Quoted 5 lines

Well, I had accepted the current approach as the (performance wise) better alternative, but with Andrew sharing that original concern of mine, and with there having been a first real problem resulting from that approach, more seriously considering the alternative certainly seems necessary. Andrew?

I'm fine with both variants. I just need to know which one will be accepted. :-)

Juergen

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