Skip to contentSkip to conversation

x86: various EPT adjustments

PATCH Superseded Jan Beulich 4 participants 23 messages Mar 28, 2014 – Apr 4, 2014

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86: various EPT adjustments

1: EPT: force re-evaluation of memory type as necessary
2: EPT: split super pages upon mismatching memory types
3: EPT: IOMMU snoop capability should not affect memory type selection 4: fix pinned cache attribute handling

v2: Patches 1...5 of the previous 8 patch series were already applied.
Patch 1 here has Tim's review comments incorporated. Patches 2 and 3 here are unchanged.
Patch 4 here is a new one.

#1 lore raw

PATCH v2 1/4 x86/EPT: force re-evaluation of memory type as necessary

The main goal here is to drop the bogus dependency of epte_get_entry_emt() on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].

Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the EPT_MISCONFIG VM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected).

This is being done in a hierarchical manner to keep execution time
down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing).

This restoring can result in spurious EPT_MISCONFIG VM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits.

Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the EPT_MISCONFIG VM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility.

xen/arch/x86/domctl.c +6 -0
@@ -84,6 +84,8 @@ long arch_do_domctl(
             ret = ioports_permit_access(d, fp, fp + np - 1);
         else
             ret = ioports_deny_access(d, fp, fp + np - 1);
+        if ( !ret )
+            memory_type_changed(d);
     }
     break;
 
@@ -705,6 +707,8 @@ long arch_do_domctl(
                        ret, add ? "removing" : "denying", d->domain_id,
                        mfn, mfn + nr_mfns - 1);
         }
+        /* Do this unconditionally to cover errors on above failure paths. */
+        memory_type_changed(d);
     }
     break;
 
@@ -791,6 +795,8 @@ long arch_do_domctl(
                        "ioport_map: error %ld denying dom%d access to [%x,%x]\n",
                        ret, d->domain_id, fmp, fmp + np - 1);
         }
+        if ( !ret )
+            memory_type_changed(d);
     }
     break;
 
xen/arch/x86/hvm/hvm.c +3 -0
@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6
 
     if ( !hvm_funcs.set_guest_pat(v, guest_pat) )
         v->arch.hvm_vcpu.pat_cr = guest_pat;
+
+    memory_type_changed(v->domain);
+
     return 1;
 }
 
xen/arch/x86/hvm/mtrr.c +16 -4
@@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct doma
          return 0;
     }
 
-    m->enabled = enabled;
-    m->def_type = def_type;
+    if ( m->enabled != enabled || m->def_type != def_type )
+    {
+        m->enabled = enabled;
+        m->def_type = def_type;
+        memory_type_changed(d);
+    }
 
     return 1;
 }
@@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct dom
                 return 0;
 
         fixed_range_base[row] = msr_content;
+        memory_type_changed(d);
     }
 
     return 1;
@@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set(
 
     m->overlapped = is_var_mtrr_overlapped(m);
 
+    memory_type_changed(d);
+
     return 1;
 }
 
@@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct doma
 HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,
                           1, HVMSR_PER_VCPU);
 
+void memory_type_changed(struct domain *d)
+{
+    if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0] )
+        p2m_memory_type_changed(d);
+}
+
 uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
                            uint8_t *ipat, bool_t direct_mmio)
 {
@@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domain
         return MTRR_TYPE_WRBACK;
     }
 
-    gmtrr_mtype = is_hvm_domain(d) && v &&
-                  d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] ?
+    gmtrr_mtype = is_hvm_domain(d) && v ?
                   get_mtrr_type(&v->arch.hvm_vcpu.mtrr, (gfn << PAGE_SHIFT)) :
                   MTRR_TYPE_WRBACK;
 
xen/arch/x86/hvm/vmx/vmx.c +10 -0
@@ -3016,6 +3016,16 @@ void vmx_vmexit_handler(struct cpu_user_
         break;
     }
 
+    case EXIT_REASON_EPT_MISCONFIG:
+    {
+        paddr_t gpa;
+
+        __vmread(GUEST_PHYSICAL_ADDRESS, &gpa);
+        if ( !ept_handle_misconfig(gpa) )
+            goto exit_and_crash;
+        break;
+    }
+
     case EXIT_REASON_MONITOR_TRAP_FLAG:
         v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG;
         vmx_update_cpu_exec_control(v);
xen/arch/x86/mm/p2m.c +12 -0
@@ -200,6 +200,18 @@ void p2m_change_entry_type_global(struct
     p2m_unlock(p2m);
 }
 
+void p2m_memory_type_changed(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( p2m->memory_type_changed )
+    {
+        p2m_lock(p2m);
+        p2m->memory_type_changed(p2m);
+        p2m_unlock(p2m);
+    }
+}
+
 mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn,
                     p2m_type_t *t, p2m_access_t *a, p2m_query_t q,
                     unsigned int *page_order, bool_t locked)
xen/arch/x86/mm/p2m-ept.c +137 -1

173 lines not on this page · raw message

xen/common/domctl.c +4 -0
@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
             ret = iomem_permit_access(d, mfn, mfn + nr_mfns - 1);
         else
             ret = iomem_deny_access(d, mfn, mfn + nr_mfns - 1);
+#ifdef CONFIG_X86
+        if ( !ret )
+            memory_type_changed(d);
+#endif
     }
     break;
 
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
@@ -124,6 +124,9 @@ struct arch_vmx_struct {
 
     unsigned long        host_cr0;
 
+    /* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */
+    bool_t               ept_spurious_misconfig;
+
     /* Is the guest in real mode? */
     uint8_t              vmx_realmode;
     /* Are we emulating rather than VMENTERing? */
xen/include/asm-x86/hvm/vmx/vmx.h +1 -0
@@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)
 void ept_p2m_uninit(struct p2m_domain *p2m);
 
 void ept_walk_table(struct domain *d, unsigned long gfn);
+bool_t ept_handle_misconfig(uint64_t gpa);
 void setup_ept_dump(void);
 
 void update_guest_eip(void);
xen/include/asm-x86/mtrr.h +1 -0
@@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(str
                                      uint32_t row, uint64_t msr_content);
 extern bool_t mtrr_def_type_msr_set(struct domain *, struct mtrr_state *,
                                     uint64_t msr_content);
+extern void memory_type_changed(struct domain *);
 extern bool_t pat_msr_set(uint64_t *pat, uint64_t msr);
 
 bool_t is_var_mtrr_overlapped(struct mtrr_state *m);
xen/include/asm-x86/p2m.h +4 -0
@@ -233,6 +233,7 @@ struct p2m_domain {
     void               (*change_entry_type_global)(struct p2m_domain *p2m,
                                                    p2m_type_t ot,
                                                    p2m_type_t nt);
+    void               (*memory_type_changed)(struct p2m_domain *p2m);
     
     void               (*write_p2m_entry)(struct p2m_domain *p2m,
                                           unsigned long gfn, l1_pgentry_t *p,
@@ -506,6 +507,9 @@ void p2m_change_type_range(struct domain
 p2m_type_t p2m_change_type(struct domain *d, unsigned long gfn,
                            p2m_type_t ot, p2m_type_t nt);
 
+/* Report a change affecting memory types. */
+void p2m_memory_type_changed(struct domain *d);
+
 /* Set mmio addresses in the p2m table (for pass-through) */
 int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn);
 int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn);

x86/EPT: force re-evaluation of memory type as necessary

The main goal here is to drop the bogus dependency of epte_get_entry_emt() on d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT].

Any change to state influencing epte_get_entry_emt()'s decision needs to result in re-calculation. Do this by using the EPT_MISCONFIG VM exit, storing an invalid memory type into EPT's emt field (leaving the IOMMU, which doesn't care about memory types, unaffected).

This is being done in a hierarchical manner to keep execution time
down: Initially only the top level directory gets invalidated this way. Upon access, the involved intermediate page table levels get cleared back to zero, and the leaf entry gets its field properly set. For 4k leaves all other entries in the same directory also get processed to amortize the cost of the extra VM exit (which halved the number of these VM exits in my testing).

This restoring can result in spurious EPT_MISCONFIG VM exits (since two vCPU-s may access addresses involving identical page table structures). Rather than simply returning in such cases (and risking that such a VM exit results from a real mis-configuration, which would then result in an endless loop rather than killing the VM), a per-vCPU flag is being introduced indicating when such a spurious VM exit might validly happen - if another one occurs right after VM re- entry, the flag would generally end up being clear, causing the VM to be killed as before on such VM exits.

Note that putting a reserved memory type value in the EPT structures isn't formally sanctioned by the specification. Intel isn't willing to adjust the specification to make this or a similar use of the EPT_MISCONFIG VM exit formally possible, but they have indicated that us using this is low risk wrt forward compatibility.

xen/arch/x86/domctl.c +6 -0
@@ -84,6 +84,8 @@ long arch_do_domctl(
             ret = ioports_permit_access(d, fp, fp + np - 1);
         else
             ret = ioports_deny_access(d, fp, fp + np - 1);
+        if ( !ret )
+            memory_type_changed(d);
     }
     break;
 
@@ -705,6 +707,8 @@ long arch_do_domctl(
                        ret, add ? "removing" : "denying", d->domain_id,
                        mfn, mfn + nr_mfns - 1);
         }
+        /* Do this unconditionally to cover errors on above failure paths. */
+        memory_type_changed(d);
     }
     break;
 
@@ -791,6 +795,8 @@ long arch_do_domctl(
                        "ioport_map: error %ld denying dom%d access to [%x,%x]\n",
                        ret, d->domain_id, fmp, fmp + np - 1);
         }
+        if ( !ret )
+            memory_type_changed(d);
     }
     break;
 
xen/arch/x86/hvm/hvm.c +3 -0
@@ -252,6 +252,9 @@ int hvm_set_guest_pat(struct vcpu *v, u6
 
     if ( !hvm_funcs.set_guest_pat(v, guest_pat) )
         v->arch.hvm_vcpu.pat_cr = guest_pat;
+
+    memory_type_changed(v->domain);
+
     return 1;
 }
 
xen/arch/x86/hvm/mtrr.c +16 -4
@@ -431,8 +431,12 @@ bool_t mtrr_def_type_msr_set(struct doma
          return 0;
     }
 
-    m->enabled = enabled;
-    m->def_type = def_type;
+    if ( m->enabled != enabled || m->def_type != def_type )
+    {
+        m->enabled = enabled;
+        m->def_type = def_type;
+        memory_type_changed(d);
+    }
 
     return 1;
 }
@@ -452,6 +456,7 @@ bool_t mtrr_fix_range_msr_set(struct dom
                 return 0;
 
         fixed_range_base[row] = msr_content;
+        memory_type_changed(d);
     }
 
     return 1;
@@ -496,6 +501,8 @@ bool_t mtrr_var_range_msr_set(
 
     m->overlapped = is_var_mtrr_overlapped(m);
 
+    memory_type_changed(d);
+
     return 1;
 }
 
@@ -690,6 +697,12 @@ static int hvm_load_mtrr_msr(struct doma
 HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,
                           1, HVMSR_PER_VCPU);
 
+void memory_type_changed(struct domain *d)
+{
+    if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0] )
+        p2m_memory_type_changed(d);
+}
+
 uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
                            uint8_t *ipat, bool_t direct_mmio)
 {
@@ -733,8 +746,7 @@ uint8_t epte_get_entry_emt(struct domain
         return MTRR_TYPE_WRBACK;
     }
 
-    gmtrr_mtype = is_hvm_domain(d) && v &&
-                  d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] ?
+    gmtrr_mtype = is_hvm_domain(d) && v ?
                   get_mtrr_type(&v->arch.hvm_vcpu.mtrr, (gfn << PAGE_SHIFT)) :
                   MTRR_TYPE_WRBACK;
 
xen/arch/x86/hvm/vmx/vmx.c +10 -0
@@ -3016,6 +3016,16 @@ void vmx_vmexit_handler(struct cpu_user_
         break;
     }
 
+    case EXIT_REASON_EPT_MISCONFIG:
+    {
+        paddr_t gpa;
+
+        __vmread(GUEST_PHYSICAL_ADDRESS, &gpa);
+        if ( !ept_handle_misconfig(gpa) )
+            goto exit_and_crash;
+        break;
+    }
+
     case EXIT_REASON_MONITOR_TRAP_FLAG:
         v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG;
         vmx_update_cpu_exec_control(v);
xen/arch/x86/mm/p2m.c +12 -0
@@ -200,6 +200,18 @@ void p2m_change_entry_type_global(struct
     p2m_unlock(p2m);
 }
 
+void p2m_memory_type_changed(struct domain *d)
+{
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( p2m->memory_type_changed )
+    {
+        p2m_lock(p2m);
+        p2m->memory_type_changed(p2m);
+        p2m_unlock(p2m);
+    }
+}
+
 mfn_t __get_gfn_type_access(struct p2m_domain *p2m, unsigned long gfn,
                     p2m_type_t *t, p2m_access_t *a, p2m_query_t q,
                     unsigned int *page_order, bool_t locked)
xen/arch/x86/mm/p2m-ept.c +137 -1

173 lines not on this page · raw message

xen/common/domctl.c +4 -0
@@ -815,6 +815,10 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
             ret = iomem_permit_access(d, mfn, mfn + nr_mfns - 1);
         else
             ret = iomem_deny_access(d, mfn, mfn + nr_mfns - 1);
+#ifdef CONFIG_X86
+        if ( !ret )
+            memory_type_changed(d);
+#endif
     }
     break;
 
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
@@ -124,6 +124,9 @@ struct arch_vmx_struct {
 
     unsigned long        host_cr0;
 
+    /* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */
+    bool_t               ept_spurious_misconfig;
+
     /* Is the guest in real mode? */
     uint8_t              vmx_realmode;
     /* Are we emulating rather than VMENTERing? */
xen/include/asm-x86/hvm/vmx/vmx.h +1 -0
@@ -520,6 +520,7 @@ int ept_p2m_init(struct p2m_domain *p2m)
 void ept_p2m_uninit(struct p2m_domain *p2m);
 
 void ept_walk_table(struct domain *d, unsigned long gfn);
+bool_t ept_handle_misconfig(uint64_t gpa);
 void setup_ept_dump(void);
 
 void update_guest_eip(void);
xen/include/asm-x86/mtrr.h +1 -0
@@ -88,6 +88,7 @@ extern bool_t mtrr_fix_range_msr_set(str
                                      uint32_t row, uint64_t msr_content);
 extern bool_t mtrr_def_type_msr_set(struct domain *, struct mtrr_state *,
                                     uint64_t msr_content);
+extern void memory_type_changed(struct domain *);
 extern bool_t pat_msr_set(uint64_t *pat, uint64_t msr);
 
 bool_t is_var_mtrr_overlapped(struct mtrr_state *m);
xen/include/asm-x86/p2m.h +4 -0
@@ -233,6 +233,7 @@ struct p2m_domain {
     void               (*change_entry_type_global)(struct p2m_domain *p2m,
                                                    p2m_type_t ot,
                                                    p2m_type_t nt);
+    void               (*memory_type_changed)(struct p2m_domain *p2m);
     
     void               (*write_p2m_entry)(struct p2m_domain *p2m,
                                           unsigned long gfn, l1_pgentry_t *p,
@@ -506,6 +507,9 @@ void p2m_change_type_range(struct domain
 p2m_type_t p2m_change_type(struct domain *d, unsigned long gfn,
                            p2m_type_t ot, p2m_type_t nt);
 
+/* Report a change affecting memory types. */
+void p2m_memory_type_changed(struct domain *d);
+
 /* Set mmio addresses in the p2m table (for pass-through) */
 int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn);
 int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);

    return okay;

instead? With that change,

Cheers,

Tim.

#3 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

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

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);

    return okay;
Quoted 3 lines

instead? With that change,

Will do, even though another patch (not posted yet, removing the recursion in ept_change_entry_type_global()) converts this to a more conventional "rc" anyway, as I'm re-using the bulk of this function then to flush out pending changes first thing in ept_set_entry().

Jan

#4 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

In reply to Tim Deegan
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 12:18, <[email protected]> wrote:

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);

    return okay;
Quoted 1 line

instead?

Actually, trying this I find that this conflicts with the added use in patch 2, so I'd prefer to keep it the way it is.

Jan

#5 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

Quoted 40 lines · Jan Beulich
Quoted 40 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 12:18, <[email protected]> wrote:

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);
Quoted 6 lines
Quoted 3 lines
    return okay;

instead?

Actually, trying this I find that this conflicts with the added use in patch 2, so I'd prefer to keep it the way it is.

Oh. On closer inspection, in patch 2, when you split a superpage you set okay = 0 and break, which should cause this whole function to return 0, killing the guest. Shouldn't that be a 'continue'?

And I'm not sure that setting okay = 0 is the right thing to do anyway. I guess what you're looking for is to avoid a false negative if you split the superpage and then don't clear an invalid EMT in the new leaf; but you might have cleared an invalid EMT at a higher level, in which case okay should not be touched; 'continue' will DTRT.

Or maybe I'm confused. Time for lunch and I'll stare at it again. :)

Tim.

#6 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

Quoted 44 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 14:30, <[email protected]> wrote:
Quoted 42 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 12:18, <[email protected]> wrote:

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);

    return okay;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

instead?

Actually, trying this I find that this conflicts with the added use in patch 2, so I'd prefer to keep it the way it is.

Oh. On closer inspection, in patch 2, when you split a superpage you set okay = 0 and break, which should cause this whole function to return 0, killing the guest. Shouldn't that be a 'continue'?

But that's only when the splitting fails, and in that case I have no choice but killing the guest.

Jan

Quoted 9 lines

And I'm not sure that setting okay = 0 is the right thing to do anyway. I guess what you're looking for is to avoid a false negative if you split the superpage and then don't clear an invalid EMT in the new leaf; but you might have cleared an invalid EMT at a higher level, in which case okay should not be touched; 'continue' will DTRT.

Or maybe I'm confused. Time for lunch and I'll stare at it again. :)

Tim.

Reviewed-by
#7 lore raw

Re: [PATCH v2 1/4] x86/EPT: force re-evaluation of memory type as necessary

Quoted 49 lines · Jan Beulich
Quoted 49 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 14:30, <[email protected]> wrote:
Quoted 46 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 03.04.14 at 12:18, <[email protected]> wrote:

Hi,

This version looks correct to me, but this tristate:

Quoted 3 lines · Jan Beulich
xen/arch/x86/mm/p2m-ept.c +3 -0
+    p2m_lock(p2m);
+
+    okay = -curr->arch.hvm_vmx.ept_spurious_misconfig;

[...]

xen/arch/x86/mm/p2m-ept.c static void ept_change_entry_type_global · line 789

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +12 -0
+    if ( okay > 0 )
+    {
+        struct vcpu *v;
+
+        for_each_vcpu ( curr->domain, v )
+            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
+    }
+    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
+    ept_sync_domain(p2m);
+    p2m_unlock(p2m);
+
+    return !!okay;

seems a bit baroque. Can we go for 'bool_t okay = 0' at the top and then

    if ( okay )
    {
        struct vcpu *v;

        for_each_vcpu ( curr->domain, v )
            v->arch.hvm_vmx.ept_spurious_misconfig = 1;
    }
    else
        okay = curr->arch.hvm_vmx.ept_spurious_misconfig; 
    curr->arch.hvm_vmx.ept_spurious_misconfig = 0;
    ept_sync_domain(p2m);
    p2m_unlock(p2m);

    return okay;
Quoted 1 line

instead?

Actually, trying this I find that this conflicts with the added use in patch 2, so I'd prefer to keep it the way it is.

Quoted 6 lines
Quoted 3 lines

Oh. On closer inspection, in patch 2, when you split a superpage you set okay = 0 and break, which should cause this whole function to return 0, killing the guest. Shouldn't that be a 'continue'?

But that's only when the splitting fails, and in that case I have no choice but killing the guest.

Right, yes, I was misreading patch 2; sorry for the noise. And on the grounds that the tristate is going away anyway,

for this patch as it stands.

Cheers,

Tim.

#8 lore raw

PATCH v2 2/4 x86/EPT: split super pages upon mismatching memory types

In reply to Jan Beulich

... between constituent pages. To indicate such, the page order is being passed down to the vMTRR routines, with a negative return value (possible only on order-non-zero pages) indicating such collisions.

Some code redundancy reduction is being done to ept_set_entry() along the way, allowing the new handling to be centralized to a single place there.

In order to keep ept_set_entry() fast and simple, the actual splitting is being deferred to the EPT_MISCONFIG VM exit handler.

xen/arch/x86/hvm/mtrr.c +46 -30
@@ -222,30 +222,40 @@ void hvm_vcpu_cacheattr_destroy(struct v
 
 /*
  * Get MTRR memory type for physical address pa.
+ *
+ * May return a negative value when order > 0, indicating to the caller
+ * that the respective mapping needs splitting.
  */
-static uint8_t get_mtrr_type(struct mtrr_state *m, paddr_t pa)
+static int get_mtrr_type(const struct mtrr_state *m,
+                         paddr_t pa, unsigned int order)
 {
-   int32_t     addr, seg, index;
    uint8_t     overlap_mtrr = 0;
    uint8_t     overlap_mtrr_pos = 0;
-   uint64_t    phys_base;
-   uint64_t    phys_mask;
-   uint8_t     num_var_ranges = m->mtrr_cap & 0xff;
+   uint64_t    mask = -(uint64_t)PAGE_SIZE << order;
+   unsigned int seg, num_var_ranges = m->mtrr_cap & 0xff;
 
    if ( unlikely(!(m->enabled & 0x2)) )
        return MTRR_TYPE_UNCACHABLE;
 
+   pa &= mask;
    if ( (pa < 0x100000) && (m->enabled & 1) )
    {
-       /* Fixed range MTRR takes effective */
-       addr = (uint32_t) pa;
+       /* Fixed range MTRR takes effect. */
+       uint32_t addr = (uint32_t)pa, index;
+
        if ( addr < 0x80000 )
        {
+           /* 0x00000 ... 0x7FFFF in 64k steps */
+           if ( order > 4 )
+               return -1;
            seg = (addr >> 16);
            return m->fixed_ranges[seg];
        }
        else if ( addr < 0xc0000 )
        {
+           /* 0x80000 ... 0xBFFFF in 16k steps */
+           if ( order > 2 )
+               return -1;
            seg = (addr - 0x80000) >> 14;
            index = (seg >> 3) + 1;
            seg &= 7;            /* select 0-7 segments */
@@ -253,7 +263,9 @@ static uint8_t get_mtrr_type(struct mtrr
        }
        else
        {
-           /* 0xC0000 --- 0x100000 */
+           /* 0xC0000 ... 0xFFFFF in 4k steps */
+           if ( order )
+               return -1;
            seg = (addr - 0xc0000) >> 12;
            index = (seg >> 3) + 3;
            seg &= 7;            /* select 0-7 segments */
@@ -264,14 +276,15 @@ static uint8_t get_mtrr_type(struct mtrr
    /* Match with variable MTRRs. */
    for ( seg = 0; seg < num_var_ranges; seg++ )
    {
-       phys_base = ((uint64_t*)m->var_ranges)[seg*2];
-       phys_mask = ((uint64_t*)m->var_ranges)[seg*2 + 1];
+       uint64_t phys_base = m->var_ranges[seg].base;
+       uint64_t phys_mask = m->var_ranges[seg].mask;
+
        if ( phys_mask & MTRR_PHYSMASK_VALID )
        {
-           if ( ((uint64_t) pa & phys_mask) >> MTRR_PHYSMASK_SHIFT ==
-                (phys_base & phys_mask) >> MTRR_PHYSMASK_SHIFT )
+           phys_mask &= mask;
+           if ( (pa & phys_mask) == (phys_base & phys_mask) )
            {
-               if ( unlikely(m->overlapped) )
+               if ( unlikely(m->overlapped) || order )
                {
                     overlap_mtrr |= 1 << (phys_base & MTRR_PHYSBASE_TYPE_MASK);
                     overlap_mtrr_pos = phys_base & MTRR_PHYSBASE_TYPE_MASK;
@@ -285,23 +298,24 @@ static uint8_t get_mtrr_type(struct mtrr
        }
    }
 
-   /* Overlapped or not found. */
+   /* Not found? */
    if ( unlikely(overlap_mtrr == 0) )
        return m->def_type;
 
-   if ( likely(!(overlap_mtrr & ~( ((uint8_t)1) << overlap_mtrr_pos ))) )
-       /* Covers both one variable memory range matches and
-        * two or more identical match.
-        */
+   /* One match, or multiple identical ones? */
+   if ( likely(overlap_mtrr == (1 << overlap_mtrr_pos)) )
        return overlap_mtrr_pos;
 
+   if ( order )
+       return -1;
+
+   /* Two or more matches, one being UC? */
    if ( overlap_mtrr & (1 << MTRR_TYPE_UNCACHABLE) )
-       /* Two or more match, one is UC. */
        return MTRR_TYPE_UNCACHABLE;
 
-   if ( !(overlap_mtrr &
-          ~((1 << MTRR_TYPE_WRTHROUGH) | (1 << MTRR_TYPE_WRBACK))) )
-       /* Two or more match, WT and WB. */
+   /* Two or more matches, all of them WT and WB? */
+   if ( overlap_mtrr ==
+        ((1 << MTRR_TYPE_WRTHROUGH) | (1 << MTRR_TYPE_WRBACK)) )
        return MTRR_TYPE_WRTHROUGH;
 
    /* Behaviour is undefined, but return the last overlapped type. */
@@ -341,7 +355,7 @@ static uint8_t effective_mm_type(struct 
      * just use it
      */ 
     if ( gmtrr_mtype == NO_HARDCODE_MEM_TYPE )
-        mtrr_mtype = get_mtrr_type(m, gpa);
+        mtrr_mtype = get_mtrr_type(m, gpa, 0);
     else
         mtrr_mtype = gmtrr_mtype;
 
@@ -370,7 +384,7 @@ uint32_t get_pat_flags(struct vcpu *v,
     guest_eff_mm_type = effective_mm_type(g, pat, gpaddr, 
                                           gl1e_flags, gmtrr_mtype);
     /* 2. Get the memory type of host physical address, with MTRR */
-    shadow_mtrr_type = get_mtrr_type(&mtrr_state, spaddr);
+    shadow_mtrr_type = get_mtrr_type(&mtrr_state, spaddr, 0);
 
     /* 3. Find the memory type in PAT, with host MTRR memory type
      * and guest effective memory type.
@@ -703,10 +717,10 @@ void memory_type_changed(struct domain *
         p2m_memory_type_changed(d);
 }
 
-uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
-                           uint8_t *ipat, bool_t direct_mmio)
+int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
+                       unsigned int order, uint8_t *ipat, bool_t direct_mmio)
 {
-    uint8_t gmtrr_mtype, hmtrr_mtype;
+    int gmtrr_mtype, hmtrr_mtype;
     uint32_t type;
     struct vcpu *v = current;
 
@@ -747,10 +761,12 @@ uint8_t epte_get_entry_emt(struct domain
     }
 
     gmtrr_mtype = is_hvm_domain(d) && v ?
-                  get_mtrr_type(&v->arch.hvm_vcpu.mtrr, (gfn << PAGE_SHIFT)) :
+                  get_mtrr_type(&v->arch.hvm_vcpu.mtrr,
+                                gfn << PAGE_SHIFT, order) :
                   MTRR_TYPE_WRBACK;
-
-    hmtrr_mtype = get_mtrr_type(&mtrr_state, (mfn_x(mfn) << PAGE_SHIFT));
+    hmtrr_mtype = get_mtrr_type(&mtrr_state, mfn_x(mfn) << PAGE_SHIFT, order);
+    if ( gmtrr_mtype < 0 || hmtrr_mtype < 0 )
+        return -1;
 
     /* If both types match we're fine. */
     if ( likely(gmtrr_mtype == hmtrr_mtype) )
xen/arch/x86/mm/p2m-ept.c +32 -32

114 lines not on this page · raw message

xen/include/asm-x86/mtrr.h +3 -2
@@ -72,8 +72,9 @@ extern int mtrr_del_page(int reg, unsign
 extern void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi);
 extern u32 get_pat_flags(struct vcpu *v, u32 gl1e_flags, paddr_t gpaddr,
                   paddr_t spaddr, uint8_t gmtrr_mtype);
-extern uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn,
-                                  mfn_t mfn, uint8_t *ipat, bool_t direct_mmio);
+extern int epte_get_entry_emt(struct domain *, unsigned long gfn, mfn_t mfn,
+                              unsigned int order, uint8_t *ipat,
+                              bool_t direct_mmio);
 extern void ept_change_entry_emt_with_range(
     struct domain *d, unsigned long start_gfn, unsigned long end_gfn);
 extern unsigned char pat_type_2_pte_flags(unsigned char pat_type);

x86/EPT: split super pages upon mismatching memory types

... between constituent pages. To indicate such, the page order is being passed down to the vMTRR routines, with a negative return value (possible only on order-non-zero pages) indicating such collisions.

Some code redundancy reduction is being done to ept_set_entry() along the way, allowing the new handling to be centralized to a single place there.

In order to keep ept_set_entry() fast and simple, the actual splitting is being deferred to the EPT_MISCONFIG VM exit handler.

xen/arch/x86/hvm/mtrr.c +46 -30

154 lines not on this page · raw message

xen/arch/x86/mm/p2m-ept.c +32 -32

114 lines not on this page · raw message

xen/include/asm-x86/mtrr.h +3 -2
@@ -72,8 +72,9 @@ extern int mtrr_del_page(int reg, unsign
 extern void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi);
 extern u32 get_pat_flags(struct vcpu *v, u32 gl1e_flags, paddr_t gpaddr,
                   paddr_t spaddr, uint8_t gmtrr_mtype);
-extern uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn,
-                                  mfn_t mfn, uint8_t *ipat, bool_t direct_mmio);
+extern int epte_get_entry_emt(struct domain *, unsigned long gfn, mfn_t mfn,
+                              unsigned int order, uint8_t *ipat,
+                              bool_t direct_mmio);
 extern void ept_change_entry_emt_with_range(
     struct domain *d, unsigned long start_gfn, unsigned long end_gfn);
 extern unsigned char pat_type_2_pte_flags(unsigned char pat_type);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

PATCH v2 3/4 x86/EPT: IOMMU snoop capability should not affect memory type selection

In reply to Jan Beulich

This capability solely makes a statement on cache coherency guarantees by the IOMMU. It does specifically not imply any further guarantees implied by certain memory types (cachability, ordering).

---
This already drops a few bits of the changes done for XSA-60. I think that if the earlier patches (and particularly the use of the EPT_MISCONFIG VM exit) are being accepted, we should switch that hole mechanism, avoiding the need to play games with the guest's PAT MSR.

xen/arch/x86/hvm/hvm.c +11 -16
@@ -1950,6 +1950,7 @@ static void hvm_update_cr(struct vcpu *v
 int hvm_set_cr0(unsigned long value)
 {
     struct vcpu *v = current;
+    struct domain *d = v->domain;
     unsigned long gfn, old_value = v->arch.hvm_vcpu.guest_cr[0];
     struct page_info *page;
 
@@ -1973,8 +1974,8 @@ int hvm_set_cr0(unsigned long value)
         goto gpf;
 
     /* A pvh is not expected to change to real mode. */
-    if ( is_pvh_vcpu(v)
-         && (value & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PG | X86_CR0_PE) )
+    if ( is_pvh_domain(d) &&
+         (value & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PG | X86_CR0_PE) )
     {
         printk(XENLOG_G_WARNING
                "PVH attempting to turn off PE/PG. CR0:%lx\n", value);
@@ -1996,16 +1997,16 @@ int hvm_set_cr0(unsigned long value)
             hvm_update_guest_efer(v);
         }
 
-        if ( !paging_mode_hap(v->domain) )
+        if ( !paging_mode_hap(d) )
         {
             /* The guest CR3 must be pointing to the guest physical. */
             gfn = v->arch.hvm_vcpu.guest_cr[3]>>PAGE_SHIFT;
-            page = get_page_from_gfn(v->domain, gfn, NULL, P2M_ALLOC);
+            page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
             if ( !page )
             {
                 gdprintk(XENLOG_ERR, "Invalid CR3 value = %lx\n",
                          v->arch.hvm_vcpu.guest_cr[3]);
-                domain_crash(v->domain);
+                domain_crash(d);
                 return X86EMUL_UNHANDLEABLE;
             }
 
@@ -2032,24 +2033,18 @@ int hvm_set_cr0(unsigned long value)
             hvm_update_guest_efer(v);
         }
 
-        if ( !paging_mode_hap(v->domain) )
+        if ( !paging_mode_hap(d) )
         {
             put_page(pagetable_get_page(v->arch.guest_table));
             v->arch.guest_table = pagetable_null();
         }
     }
 
-    /*
-     * When cr0.cd setting
-     * 1. For guest w/o VT-d, and for guest with VT-d but snooped, Xen need not
-     * do anything, since hardware snoop mechanism has ensured cache coherency;
-     * 2. For guest with VT-d but non-snooped, cache coherency cannot be
-     * guaranteed by h/w so need emulate UC memory type to guest.
-     */
     if ( ((value ^ old_value) & X86_CR0_CD) &&
-           has_arch_pdevs(v->domain) &&
-           iommu_enabled && !iommu_snoop &&
-           hvm_funcs.handle_cd )
+         iommu_enabled && hvm_funcs.handle_cd &&
+         (!rangeset_is_empty(d->iomem_caps) ||
+          !rangeset_is_empty(d->arch.ioport_caps) ||
+          has_arch_pdevs(d)) )
         hvm_funcs.handle_cd(v, value);
 
     hvm_update_cr(v, 0, value);
xen/arch/x86/hvm/mtrr.c +1 -7
@@ -713,7 +713,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save
 
 void memory_type_changed(struct domain *d)
 {
-    if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0] )
+    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
         p2m_memory_type_changed(d);
 }
 
@@ -754,12 +754,6 @@ int epte_get_entry_emt(struct domain *d,
         return MTRR_TYPE_WRBACK;
     }
 
-    if ( iommu_snoop )
-    {
-        *ipat = 1;
-        return MTRR_TYPE_WRBACK;
-    }
-
     gmtrr_mtype = is_hvm_domain(d) && v ?
                   get_mtrr_type(&v->arch.hvm_vcpu.mtrr,
                                 gfn << PAGE_SHIFT, order) :

x86/EPT: IOMMU snoop capability should not affect memory type selection

This capability solely makes a statement on cache coherency guarantees by the IOMMU. It does specifically not imply any further guarantees implied by certain memory types (cachability, ordering).

---
This already drops a few bits of the changes done for XSA-60. I think that if the earlier patches (and particularly the use of the EPT_MISCONFIG VM exit) are being accepted, we should switch that hole mechanism, avoiding the need to play games with the guest's PAT MSR.

xen/arch/x86/hvm/hvm.c +11 -16
@@ -1950,6 +1950,7 @@ static void hvm_update_cr(struct vcpu *v
 int hvm_set_cr0(unsigned long value)
 {
     struct vcpu *v = current;
+    struct domain *d = v->domain;
     unsigned long gfn, old_value = v->arch.hvm_vcpu.guest_cr[0];
     struct page_info *page;
 
@@ -1973,8 +1974,8 @@ int hvm_set_cr0(unsigned long value)
         goto gpf;
 
     /* A pvh is not expected to change to real mode. */
-    if ( is_pvh_vcpu(v)
-         && (value & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PG | X86_CR0_PE) )
+    if ( is_pvh_domain(d) &&
+         (value & (X86_CR0_PE | X86_CR0_PG)) != (X86_CR0_PG | X86_CR0_PE) )
     {
         printk(XENLOG_G_WARNING
                "PVH attempting to turn off PE/PG. CR0:%lx\n", value);
@@ -1996,16 +1997,16 @@ int hvm_set_cr0(unsigned long value)
             hvm_update_guest_efer(v);
         }
 
-        if ( !paging_mode_hap(v->domain) )
+        if ( !paging_mode_hap(d) )
         {
             /* The guest CR3 must be pointing to the guest physical. */
             gfn = v->arch.hvm_vcpu.guest_cr[3]>>PAGE_SHIFT;
-            page = get_page_from_gfn(v->domain, gfn, NULL, P2M_ALLOC);
+            page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
             if ( !page )
             {
                 gdprintk(XENLOG_ERR, "Invalid CR3 value = %lx\n",
                          v->arch.hvm_vcpu.guest_cr[3]);
-                domain_crash(v->domain);
+                domain_crash(d);
                 return X86EMUL_UNHANDLEABLE;
             }
 
@@ -2032,24 +2033,18 @@ int hvm_set_cr0(unsigned long value)
             hvm_update_guest_efer(v);
         }
 
-        if ( !paging_mode_hap(v->domain) )
+        if ( !paging_mode_hap(d) )
         {
             put_page(pagetable_get_page(v->arch.guest_table));
             v->arch.guest_table = pagetable_null();
         }
     }
 
-    /*
-     * When cr0.cd setting
-     * 1. For guest w/o VT-d, and for guest with VT-d but snooped, Xen need not
-     * do anything, since hardware snoop mechanism has ensured cache coherency;
-     * 2. For guest with VT-d but non-snooped, cache coherency cannot be
-     * guaranteed by h/w so need emulate UC memory type to guest.
-     */
     if ( ((value ^ old_value) & X86_CR0_CD) &&
-           has_arch_pdevs(v->domain) &&
-           iommu_enabled && !iommu_snoop &&
-           hvm_funcs.handle_cd )
+         iommu_enabled && hvm_funcs.handle_cd &&
+         (!rangeset_is_empty(d->iomem_caps) ||
+          !rangeset_is_empty(d->arch.ioport_caps) ||
+          has_arch_pdevs(d)) )
         hvm_funcs.handle_cd(v, value);
 
     hvm_update_cr(v, 0, value);
xen/arch/x86/hvm/mtrr.c +1 -7
@@ -713,7 +713,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save
 
 void memory_type_changed(struct domain *d)
 {
-    if ( iommu_enabled && !iommu_snoop && d->vcpu && d->vcpu[0] )
+    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
         p2m_memory_type_changed(d);
 }
 
@@ -754,12 +754,6 @@ int epte_get_entry_emt(struct domain *d,
         return MTRR_TYPE_WRBACK;
     }
 
-    if ( iommu_snoop )
-    {
-        *ipat = 1;
-        return MTRR_TYPE_WRBACK;
-    }
-
     gmtrr_mtype = is_hvm_domain(d) && v ?
                   get_mtrr_type(&v->arch.hvm_vcpu.mtrr,
                                 gfn << PAGE_SHIFT, order) :

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

PATCH v2 4/4 x86: fix pinned cache attribute handling

In reply to Jan Beulich

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

xen/arch/x86/hvm/mtrr.c +82 -7
@@ -551,6 +551,15 @@ bool_t mtrr_pat_not_equal(struct vcpu *v
     return 0;
 }
 
+struct hvm_mem_pinned_cacheattr_range {
+    struct list_head list;
+    uint64_t start, end;
+    uint32_t type;
+    struct rcu_head rcu;
+};
+
+static DEFINE_RCU_READ_LOCK(pinned_cacheattr_rcu_lock);
+
 void hvm_init_cacheattr_region_list(
     struct domain *d)
 {
@@ -573,30 +582,47 @@ void hvm_destroy_cacheattr_region_list(
     }
 }
 
-int32_t hvm_get_mem_pinned_cacheattr(
+int hvm_get_mem_pinned_cacheattr(
     struct domain *d,
     uint64_t guest_fn,
+    unsigned int order,
     uint32_t *type)
 {
     struct hvm_mem_pinned_cacheattr_range *range;
+    int rc = 0;
 
-    *type = 0;
+    *type = ~0;
 
     if ( !is_hvm_domain(d) )
         return 0;
 
+    rcu_read_lock(&pinned_cacheattr_rcu_lock);
     list_for_each_entry_rcu ( range,
                               &d->arch.hvm_domain.pinned_cacheattr_ranges,
                               list )
     {
-        if ( (guest_fn >= range->start) && (guest_fn <= range->end) )
+        if ( (guest_fn >= range->start) &&
+             (guest_fn + (1UL << order) - 1 <= range->end) )
         {
             *type = range->type;
-            return 1;
+            rc = 1;
+            break;
+        }
+        if ( (guest_fn <= range->end) &&
+             (range->start <= guest_fn + (1UL << order) - 1) )
+        {
+            rc = -1;
+            break;
         }
     }
+    rcu_read_unlock(&pinned_cacheattr_rcu_lock);
 
-    return 0;
+    return rc;
+}
+
+static void free_pinned_cacheattr_entry(struct rcu_head *rcu)
+{
+    xfree(container_of(rcu, struct hvm_mem_pinned_cacheattr_range, rcu));
 }
 
 int32_t hvm_set_mem_pinned_cacheattr(
@@ -606,6 +632,28 @@ int32_t hvm_set_mem_pinned_cacheattr(
     uint32_t  type)
 {
     struct hvm_mem_pinned_cacheattr_range *range;
+    int rc = 1;
+
+    if ( !is_hvm_domain(d) || gfn_end < gfn_start )
+        return 0;
+
+    if ( type == XEN_DOMCTL_DELETE_MEM_CACHEATTR )
+    {
+        /* Remove the requested range. */
+        rcu_read_lock(&pinned_cacheattr_rcu_lock);
+        list_for_each_entry_rcu ( range,
+                                  &d->arch.hvm_domain.pinned_cacheattr_ranges,
+                                  list )
+            if ( range->start == gfn_start && range->end == gfn_end )
+            {
+                rcu_read_unlock(&pinned_cacheattr_rcu_lock);
+                list_del_rcu(&range->list);
+                call_rcu(&range->rcu, free_pinned_cacheattr_entry);
+                return 0;
+            }
+        rcu_read_unlock(&pinned_cacheattr_rcu_lock);
+        return -ENOENT;
+    }
 
     if ( !((type == PAT_TYPE_UNCACHABLE) ||
            (type == PAT_TYPE_WRCOMB) ||
@@ -616,6 +664,27 @@ int32_t hvm_set_mem_pinned_cacheattr(
          !is_hvm_domain(d) )
         return -EINVAL;
 
+    rcu_read_lock(&pinned_cacheattr_rcu_lock);
+    list_for_each_entry_rcu ( range,
+                              &d->arch.hvm_domain.pinned_cacheattr_ranges,
+                              list )
+    {
+        if ( range->start == gfn_start && range->end == gfn_end )
+        {
+            range->type = type;
+            rc = 0;
+            break;
+        }
+        if ( range->start <= gfn_end && gfn_start <= range->end )
+        {
+            rc = -EBUSY;
+            break;
+        }
+    }
+    rcu_read_unlock(&pinned_cacheattr_rcu_lock);
+    if ( rc <= 0 )
+        return rc;
+
     range = xzalloc(struct hvm_mem_pinned_cacheattr_range);
     if ( range == NULL )
         return -ENOMEM;
@@ -732,8 +801,14 @@ int epte_get_entry_emt(struct domain *d,
     if ( !mfn_valid(mfn_x(mfn)) )
         return MTRR_TYPE_UNCACHABLE;
 
-    if ( hvm_get_mem_pinned_cacheattr(d, gfn, &type) )
-        return type;
+    switch ( hvm_get_mem_pinned_cacheattr(d, gfn, order, &type) )
+    {
+    case 1:
+        *ipat = 1;
+        return type != PAT_TYPE_UC_MINUS ? type : PAT_TYPE_UNCACHABLE;
+    case -1:
+        return -1;
+    }
 
     if ( !iommu_enabled ||
          (rangeset_is_empty(d->iomem_caps) &&
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -601,7 +601,7 @@ _sh_propagate(struct vcpu *v, 
          * 3) if disables snoop control, compute the PAT index with
          *    gMTRR and gPAT.
          */
-        if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(target_gfn), &type) )
+        if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(target_gfn), 0, &type) )
             sflags |= pat_type_2_pte_flags(type);
         else if ( d->arch.hvm_domain.is_in_uc_mode )
             sflags |= pat_type_2_pte_flags(PAT_TYPE_UNCACHABLE);
xen/include/asm-x86/hvm/cacheattr.h +4 -8
@@ -1,12 +1,6 @@
 #ifndef __HVM_CACHEATTR_H__
 #define __HVM_CACHEATTR_H__
 
-struct hvm_mem_pinned_cacheattr_range {
-    struct list_head list;
-    uint64_t start, end;
-    uint32_t type;
-};
-
 void hvm_init_cacheattr_region_list(
     struct domain *d);
 void hvm_destroy_cacheattr_region_list(
@@ -15,11 +9,13 @@ void hvm_destroy_cacheattr_region_list(
 /*
  * To see guest_fn is in the pinned range or not,
  * if yes, return 1, and set type to value in this range
- * if no,  return 0, and set type to 0
+ * if no,  return 0, setting type to ~0
+ * if ambiguous, return -1, setting type to ~0 (possible only for order > 0)
  */
-int32_t hvm_get_mem_pinned_cacheattr(
+int hvm_get_mem_pinned_cacheattr(
     struct domain *d,
     uint64_t guest_fn,
+    unsigned int order,
     uint32_t *type);
 
 
xen/include/public/domctl.h +1 -0
@@ -555,6 +555,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_iopor
 #define XEN_DOMCTL_MEM_CACHEATTR_WP  5
 #define XEN_DOMCTL_MEM_CACHEATTR_WB  6
 #define XEN_DOMCTL_MEM_CACHEATTR_UCM 7
+#define XEN_DOMCTL_DELETE_MEM_CACHEATTR (~(uint32_t)0)
 struct xen_domctl_pin_mem_cacheattr {
     uint64_aligned_t start, end;
     uint32_t type; /* XEN_DOMCTL_MEM_CACHEATTR_* */

x86: fix pinned cache attribute handling

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

xen/arch/x86/hvm/mtrr.c +82 -7

138 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -601,7 +601,7 @@ _sh_propagate(struct vcpu *v, 
          * 3) if disables snoop control, compute the PAT index with
          *    gMTRR and gPAT.
          */
-        if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(target_gfn), &type) )
+        if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(target_gfn), 0, &type) )
             sflags |= pat_type_2_pte_flags(type);
         else if ( d->arch.hvm_domain.is_in_uc_mode )
             sflags |= pat_type_2_pte_flags(PAT_TYPE_UNCACHABLE);
xen/include/asm-x86/hvm/cacheattr.h +4 -8
@@ -1,12 +1,6 @@
 #ifndef __HVM_CACHEATTR_H__
 #define __HVM_CACHEATTR_H__
 
-struct hvm_mem_pinned_cacheattr_range {
-    struct list_head list;
-    uint64_t start, end;
-    uint32_t type;
-};
-
 void hvm_init_cacheattr_region_list(
     struct domain *d);
 void hvm_destroy_cacheattr_region_list(
@@ -15,11 +9,13 @@ void hvm_destroy_cacheattr_region_list(
 /*
  * To see guest_fn is in the pinned range or not,
  * if yes, return 1, and set type to value in this range
- * if no,  return 0, and set type to 0
+ * if no,  return 0, setting type to ~0
+ * if ambiguous, return -1, setting type to ~0 (possible only for order > 0)
  */
-int32_t hvm_get_mem_pinned_cacheattr(
+int hvm_get_mem_pinned_cacheattr(
     struct domain *d,
     uint64_t guest_fn,
+    unsigned int order,
     uint32_t *type);
 
 
xen/include/public/domctl.h +1 -0
@@ -555,6 +555,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_iopor
 #define XEN_DOMCTL_MEM_CACHEATTR_WP  5
 #define XEN_DOMCTL_MEM_CACHEATTR_WB  6
 #define XEN_DOMCTL_MEM_CACHEATTR_UCM 7
+#define XEN_DOMCTL_DELETE_MEM_CACHEATTR (~(uint32_t)0)
 struct xen_domctl_pin_mem_cacheattr {
     uint64_aligned_t start, end;
     uint32_t type; /* XEN_DOMCTL_MEM_CACHEATTR_* */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:19, <[email protected]> wrote:

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Quoted 4 lines

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

Actually I think I should point you guys more explicitly at the above (forgot to Cc you when sending the patch).

Jan

#12 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:19, <[email protected]> wrote:

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Quoted 7 lines
Quoted 4 lines

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

Actually I think I should point you guys more explicitly at the above (forgot to Cc you when sending the patch).

This is about XEN_DOMCTL_pin_mem_cacheattr?

Is it the case that we are failing to set the cacheattrs of the last page in the range?

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

What is considered more normal for our hypercall interfaces, in- or exclusive?

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

Ian.

#13 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:19, <[email protected]> wrote:

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

Quoted 7 lines
Quoted 2 lines

Actually I think I should point you guys more explicitly at the above (forgot to Cc you when sending the patch).

This is about XEN_DOMCTL_pin_mem_cacheattr?

Is it the case that we are failing to set the cacheattrs of the last page in the range?

No, the attributes get set for one page too many.

Quoted 6 lines

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Jan

#14 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 24 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:19, <[email protected]> wrote:

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Quoted 4 lines

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.

Actually I think I should point you guys more explicitly at the above (forgot to Cc you when sending the patch).

Quoted 6 lines
Quoted 4 lines

This is about XEN_DOMCTL_pin_mem_cacheattr?

Is it the case that we are failing to set the cacheattrs of the last page in the range?

No, the attributes get set for one page too many.

Just as bad I guess ;-)

Quoted 9 lines
Quoted 6 lines

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

This is a strong argument IMHO and would seem to suggest that qemu is at fault and is the one which should be fixed.

Quoted 7 lines
Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Right that is a real downside.

In the domctl struct itself you would naturally change the field name, so that would catch that.

For libxc, which is how all the real callers end up calling it, I guess you would have to change the function name, which would also allow the existing one to remain for compatibility/transitional purposes (if we wanted that).

xc_domain_pin_memory_cacheattr_range isn't a very good name, but it is not terrible I suppose.

(I'm not necessarily advocating this approach, just thinking it through)

Ian.

#15 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 15:52, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

This is a strong argument IMHO and would seem to suggest that qemu is at fault and is the one which should be fixed.

With this ...

Quoted 13 lines
Quoted 7 lines
Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Right that is a real downside.

In the domctl struct itself you would naturally change the field name, so that would catch that.

Quoted 7 lines

For libxc, which is how all the real callers end up calling it, I guess you would have to change the function name, which would also allow the existing one to remain for compatibility/transitional purposes (if we wanted that).

xc_domain_pin_memory_cacheattr_range isn't a very good name, but it is not terrible I suppose.

... and the basic rule of not changing interfaces without reason, I think we should leave the interface alone and just fix all qemu variants. May I hope that the qemu maintainers could take care of this as well as the ROM issue pointed out earlier today?

Jan

#16 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 36 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 15:52, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

This is a strong argument IMHO and would seem to suggest that qemu is at fault and is the one which should be fixed.

With this ...

Quoted 18 lines
Quoted 7 lines
Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Right that is a real downside.

In the domctl struct itself you would naturally change the field name, so that would catch that.

For libxc, which is how all the real callers end up calling it, I guess you would have to change the function name, which would also allow the existing one to remain for compatibility/transitional purposes (if we wanted that).

Quoted 7 lines
Quoted 2 lines

xc_domain_pin_memory_cacheattr_range isn't a very good name, but it is not terrible I suppose.

... and the basic rule of not changing interfaces without reason, I think we should leave the interface alone and just fix all qemu variants. May I hope that the qemu maintainers could take care of this as well as the ROM issue pointed out earlier today?

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

#17 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 40 lines · Stefano Stabellini
Quoted 39 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 15:52, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

This is a strong argument IMHO and would seem to suggest that qemu is at fault and is the one which should be fixed.

With this ...

Quoted 20 lines
Quoted 7 lines
Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Right that is a real downside.

In the domctl struct itself you would naturally change the field name, so that would catch that.

For libxc, which is how all the real callers end up calling it, I guess you would have to change the function name, which would also allow the existing one to remain for compatibility/transitional purposes (if we wanted that).

xc_domain_pin_memory_cacheattr_range isn't a very good name, but it is not terrible I suppose.

Quoted 8 lines
Quoted 4 lines

... and the basic rule of not changing interfaces without reason, I think we should leave the interface alone and just fix all qemu variants. May I hope that the qemu maintainers could take care of this as well as the ROM issue pointed out earlier today?

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

Qemu is passing the wrong thing to xc_domain_pin_memoyr_cacheattr, it's off by one and fiddles with one page too many.

There is no xc_domain_pin_memory_cacheattr_range function, that was a halfassed proposal of mine to allow us to change the xc_domain_pin_memory_cacheattr interface to one which would be harder to get wrong in this way.

As Jan says we try not to change interfaces grauitously, so the answer is for qemu to be fixed to pass the correct arguments to xc_domain_pin_memory_cacheattr.

Ian.

#18 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

In reply to Stefano Stabellini
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 19:00, <[email protected]> wrote:
Quoted 39 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 15:52, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 14:36, <[email protected]> wrote:

I think it is generally better if libxc remains a pretty thin layer over the hypercall, so adjustments to the inclusiveness of the arguments don't really belong there IMHO.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is considered more normal for our hypercall interfaces, in- or exclusive?

I think ranges specified via two GFNs should generally be inclusive, in order to make sure they can extend to the end of address space.

This is a strong argument IMHO and would seem to suggest that qemu is at fault and is the one which should be fixed.

With this ...

Quoted 20 lines
Quoted 7 lines
Quoted 3 lines

For the cacheflush thing I added recently you suggested to use a nrpages instead of end which nicely sidestepped the issue. Is that an option here while you are changing things anyway?

I considered that, but didn't want to propose it because such a change to the interface would go un-noticeable to the caller (at build time). But of course it is an option.

Right that is a real downside.

In the domctl struct itself you would naturally change the field name, so that would catch that.

For libxc, which is how all the real callers end up calling it, I guess you would have to change the function name, which would also allow the existing one to remain for compatibility/transitional purposes (if we wanted that).

xc_domain_pin_memory_cacheattr_range isn't a very good name, but it is not terrible I suppose.

Quoted 8 lines
Quoted 4 lines

... and the basic rule of not changing interfaces without reason, I think we should leave the interface alone and just fix all qemu variants. May I hope that the qemu maintainers could take care of this as well as the ROM issue pointed out earlier today?

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

The issue is that the hypervisor (and hence libxc) interface expect the passed range to be inclusive, yet the ending page number all the qemus pass is one past the intended range.

Jan

#19 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 19:00, <[email protected]> wrote:

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

The issue is that the hypervisor (and hence libxc) interface expect the passed range to be inclusive, yet the ending page number all the qemus pass is one past the intended range.

Thanks for the clear explanation.
Is this patch what you are looking for?

---

xen-all.c +1 -1
index ba34739..027e7a8 100644
@@ -323,7 +323,7 @@ go_physmap:
 
     xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
                                    start_addr >> TARGET_PAGE_BITS,
-                                   (start_addr + size) >> TARGET_PAGE_BITS,
+                                   (start_addr + size - 1) >> TARGET_PAGE_BITS,
                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
 
     snprintf(path, sizeof(path),
#20 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 04.04.14 at 16:30, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 19:00, <[email protected]> wrote:

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

The issue is that the hypervisor (and hence libxc) interface expect the passed range to be inclusive, yet the ending page number all the qemus pass is one past the intended range.

Thanks for the clear explanation.
Is this patch what you are looking for?

---

xen-all.c +0 -0
index ba34739..027e7a8 100644

xen-all.c go_physmap: · line 329

Quoted 8 lines
xen-all.c +1 -1
@@ -323,7 +323,7 @@ go_physmap:
 
     xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
                                    start_addr >> TARGET_PAGE_BITS,
-                                   (start_addr + size) >> TARGET_PAGE_BITS,
+                                   (start_addr + size - 1) >> TARGET_PAGE_BITS,
                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
 
     snprintf(path, sizeof(path),

Something along these lines, for all maintained trees. The main thing I'm not sure about is whether size can ever be zero, or not page aligned - in both cases further care would need to be taken.

Jan

#21 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 04.04.14 at 16:30, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 28.03.14 at 19:00, <[email protected]> wrote:

I miss some context here.
What is the issue with xc_domain_pin_memory_cacheattr_range and how does it affect QEMU (that uses the xc_domain_pin_memory_cacheattr variety)?

The issue is that the hypervisor (and hence libxc) interface expect the passed range to be inclusive, yet the ending page number all the qemus pass is one past the intended range.

Thanks for the clear explanation.
Is this patch what you are looking for?

---

xen-all.c +1 -1
index ba34739..027e7a8 100644
@@ -323,7 +323,7 @@ go_physmap:
 
     xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
                                    start_addr >> TARGET_PAGE_BITS,
-                                   (start_addr + size) >> TARGET_PAGE_BITS,
+                                   (start_addr + size - 1) >> TARGET_PAGE_BITS,
                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
 
Quoted 5 lines

xen-all.c go_physmap: · line 329

Quoted 1 line
xen-all.c +0 -0
     snprintf(path, sizeof(path),

Something along these lines, for all maintained trees. The main thing I'm not sure about is whether size can ever be zero, or not page aligned - in both cases further care would need to be taken.

size cannot be zero, see:

    if (size <= 0) {
        return -1;
    }

at the beginning of the function.
Currently I don't think that size can be page misaligned. Even if it could I think that the above would do the right thing.

Reviewed-by on v2 4/4
#22 lore raw

Re: [PATCH v2 4/4] x86: fix pinned cache attribute handling

In reply to Jan Beulich
Quoted 13 lines · Jan Beulich

- make sure UC- is only used for PAT purposes (MTRRs and hence EPT don't have this type)
- add order input to "get", and properly handle conflict case (forcing an EPT page split)
- properly detect (and refuse) overlaps during "set"
- properly use RCU constructs
- support deleting ranges through a special type input to "set"
- set ignore-PAT flag in epte_get_entry_emt() when "get" succeeds
- set "get" output to ~0 (invalid) rather than 0 (UC) on error (the caller shouldn't be looking at it anyway)
- move struct hvm_mem_pinned_cacheattr_range from header to C file (used only there)

Quoted 6 lines

Note that the code (before and after this change) implies the GFN ranges passed to the hypercall to be inclusive, which is in contrast to the sole current user in qemu (all variants). It is not clear to me at which layer (qemu, libxc, hypervisor) this would best be fixed.