Skip to contentSkip to conversation

x86: assorted shadow mode adjustments

PATCH In review Jan Beulich 3 participants 27 messages May 16, 2023 – Feb 23

lore
Share & tools
lore
#0 lore raw

v3 cover x86: assorted shadow mode adjustments

This is kind of fallout from XSA-427 investigations, partly related to there having been a more intrusive first approach.

Most patches aren't really dependent upon one another, so can probably go in independently (as they get acked).

A few patches from v2 went in, but there are also two new OOS patches in v3. See individual patches for what has changed (in response to review comments).

01: reduce explicit log-dirty recording for HVM
02: call sh_update_cr3() directly from sh_page_fault()
03: don't generate bogus "domain dying" trace entry from sh_page_fault() 04: use lighter weight mode checks
05: move OOS functions to their own file
06: restrict OOS allocation to when it's really needed
07: OOS doesn't track VAs anymore
08: sh_rm_write_access_from_sl1p() is HVM-only
09: drop is_hvm_...() where easily possible
10: make monitor table create/destroy more consistent
11: vCPU-s never have "no mode"
12: adjust monitor table prealloc amount

Jan

#1 lore raw

PATCH v3 1/12 x86/shadow: reduce explicit log-dirty recording for HVM

validate_guest_pt_write(), by calling sh_validate_guest_entry(), already guarantees the needed update of log-dirty information. Move the operation into the sole code path needing it (when SHOPT_SKIP_VERIFY is enabled), making clear that only one such call is needed.

xen/arch/x86/mm/shadow/hvm.c +1 -2
@@ -644,6 +644,7 @@ static void sh_emulate_unmap_dest(struct
     {
         /* Writes with this alignment constraint can't possibly cross pages. */
         ASSERT(!mfn_valid(sh_ctxt->mfn[1]));
+        paging_mark_dirty(v->domain, sh_ctxt->mfn[0]);
     }
     else
 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY */
@@ -661,12 +662,10 @@ static void sh_emulate_unmap_dest(struct
             validate_guest_pt_write(v, sh_ctxt->mfn[1], addr + b1, b2);
     }
 
-    paging_mark_dirty(v->domain, sh_ctxt->mfn[0]);
     put_page(mfn_to_page(sh_ctxt->mfn[0]));
 
     if ( unlikely(mfn_valid(sh_ctxt->mfn[1])) )
     {
-        paging_mark_dirty(v->domain, sh_ctxt->mfn[1]);
         put_page(mfn_to_page(sh_ctxt->mfn[1]));
         vunmap((void *)((unsigned long)addr & PAGE_MASK));
     }
Acked-by on v3 1/12
#2 lore raw

Re: [PATCH v3 01/12] x86/shadow: reduce explicit log-dirty recording for HVM

Quoted 6 lines · Jan Beulich

validate_guest_pt_write(), by calling sh_validate_guest_entry(), already guarantees the needed update of log-dirty information. Move the operation into the sole code path needing it (when SHOPT_SKIP_VERIFY is enabled), making clear that only one such call is needed.

#3 lore raw

PATCH v3 2/12 x86/shadow: call sh_update_cr3() directly from sh_page_fault()

In reply to Jan Beulich

There's no need for an indirect call here, as the mode is invariant throughout the entire paging-locked region. All it takes to avoid it is to have a forward declaration of sh_update_cr3() in place.

---
I find this and the respective Win7 related comment suspicious: If we really need to "fix up" L3 entries "on demand", wouldn't we better retry the shadow_get_and_create_l1e() rather than exit? The spurious page fault that the guest observes can, after all, not be known to be non- fatal inside the guest. That's purely an OS policy.

Furthermore the sh_update_cr3() will also invalidate L3 entries which were loaded successfully before, but invalidated by the guest afterwards. I strongly suspect that the described hardware behavior is _only_ to load previously not-present entries from the PDPT, but not purge ones already marked present. IOW I think sh_update_cr3() would need calling in an "incremental" mode here. (The alternative of doing this in shadow_get_and_create_l3e() instead would likely be more cumbersome.)

Beyond the "on demand" L3 entry creation I also can't see what guest actions could lead to the ASSERT() being inapplicable in the PAE case. The 3-level code in shadow_get_and_create_l2e() doesn't consult guest PDPTEs, and all other logic is similar to that for other modes.

(See 89329d832aed ["x86 shadow: Update cr3 in PAE mode when guest walk succeed but shadow walk fails"].)

xen/arch/x86/mm/shadow/multi.c +3 -1
@@ -79,6 +79,8 @@ const char *const fetch_type_names[] = {
 # define for_each_shadow_table(v, i) for ( (i) = 0; (i) < 1; ++(i) )
 #endif
 
+static void cf_check sh_update_cr3(struct vcpu *v, int do_locking, bool noflush);
+
 /* Helper to perform a local TLB flush. */
 static void sh_flush_local(const struct domain *d)
 {
@@ -2475,7 +2477,7 @@ static int cf_check sh_page_fault(
          * In any case, in the PAE case, the ASSERT is not true; it can
          * happen because of actions the guest is taking. */
 #if GUEST_PAGING_LEVELS == 3
-        v->arch.paging.mode->update_cr3(v, 0, false);
+        sh_update_cr3(v, 0, false);
 #else
         ASSERT(d->is_shutting_down);
 #endif
Acked-by on v3 2/12
#4 lore raw

Re: [PATCH v3 02/12] x86/shadow: call sh_update_cr3() directly from sh_page_fault()

Quoted 5 lines · Jan Beulich

There's no need for an indirect call here, as the mode is invariant throughout the entire paging-locked region. All it takes to avoid it is to have a forward declaration of sh_update_cr3() in place.

Quoted 16 lines

---
I find this and the respective Win7 related comment suspicious: If we really need to "fix up" L3 entries "on demand", wouldn't we better retry the shadow_get_and_create_l1e() rather than exit? The spurious page fault that the guest observes can, after all, not be known to be non- fatal inside the guest. That's purely an OS policy.

Furthermore the sh_update_cr3() will also invalidate L3 entries which were loaded successfully before, but invalidated by the guest afterwards. I strongly suspect that the described hardware behavior is _only_ to load previously not-present entries from the PDPT, but not purge ones already marked present. IOW I think sh_update_cr3() would need calling in an "incremental" mode here. (The alternative of doing this in shadow_get_and_create_l3e() instead would likely be more cumbersome.)

Quoted 7 lines

Beyond the "on demand" L3 entry creation I also can't see what guest actions could lead to the ASSERT() being inapplicable in the PAE case. The 3-level code in shadow_get_and_create_l2e() doesn't consult guest PDPTEs, and all other logic is similar to that for other modes.

(See 89329d832aed ["x86 shadow: Update cr3 in PAE mode when guest walk succeed but shadow walk fails"].)

I recall this being an issue in flight when I joined Citrix, and relates to PDPTRs.

Architecturally, PDPTRs are are loaded on MOV CR3, the TLB flushing subset of MOV CR0/4, and Task Switch, and are otherwise non-coherent with RAM.  PDPTRs are not shot down by INVLPG/#PF/etc (they're not part of the TLB state, they're "roots" of the paging structures like CR3 is).

(If you recall, this was one of my major concerns with your PTE caching series, and I still need to figure out how to make this case work.)

Intel maintains the behaviour with 4x PDPTR fields in the VMCS, but AMD punted on the problem by declaring that 32bit PAE paging under NPT would behave as "normal" PTEs and get loaded on demand.

For a while, there was a threat in the APM saying that this behaviour might start applying to native scenarios too, but I recall AMD saying that this hadn't happened and was unlikely to.

Either way.  All OSes need to cope with both behaviour.  It's probably easiest for Shadow to stick to strictly architectural behaviour, rather than trying to account for a behaviour that only manifests in another context.

I vaguely recall discussion of Windows 7 expecting one behaviour and shadow implementing the other, but it was 15 years ago and probably at least partial speculation over an unfixed bug.

~Andrew

#5 lore raw

PATCH v3 3/12 x86/shadow: don't generate bogus "domain dying" trace entry from sh_page_fault()

In reply to Jan Beulich

When in 3-level guest mode we help a guest to stay alive, we also shouldn't emit a trace entry to the contrary. Move the invocation up into the respective #ifdef, noting that while this moves it into the locked region, emitting trace records with the paging lock held is okay (as done elsewhere as well), just needlessly increasing lock holding time a little.

---
v2: New.

xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -2480,10 +2480,10 @@ static int cf_check sh_page_fault(
         sh_update_cr3(v, 0, false);
 #else
         ASSERT(d->is_shutting_down);
+        trace_shadow_gen(TRC_SHADOW_DOMF_DYING, va);
 #endif
         paging_unlock(d);
         put_gfn(d, gfn_x(gfn));
-        trace_shadow_gen(TRC_SHADOW_DOMF_DYING, va);
         return 0;
     }
 
Acked-by on v3 3/12
#6 lore raw

Re: [PATCH v3 03/12] x86/shadow: don't generate bogus "domain dying" trace entry from sh_page_fault()

Quoted 8 lines · Jan Beulich

When in 3-level guest mode we help a guest to stay alive, we also shouldn't emit a trace entry to the contrary. Move the invocation up into the respective #ifdef, noting that while this moves it into the locked region, emitting trace records with the paging lock held is okay (as done elsewhere as well), just needlessly increasing lock holding time a little.

#7 lore raw

PATCH v3 4/12 x86/shadow: use lighter weight mode checks

In reply to Jan Beulich

shadow_mode_...(), with the exception of shadow_mode_enabled(), are shorthands for shadow_mode_enabled() && paging_mode_...(). While potentially useful outside of shadow-internal functions, when we already know that we're dealing with a domain in shadow mode, the "paging" checks are sufficient and cheaper. While the "shadow" ones commonly translate to a MOV/AND/CMP/Jcc sequence, the "paging" ones typically resolve to just TEST+Jcc.

---
v2: Re-base over new earlier patch.

xen/arch/x86/mm/shadow/common.c +5 -5
@@ -1843,7 +1843,7 @@ int sh_remove_write_access(struct domain
      * In guest refcounting, we trust Xen to already be restricting
      * all the writes to the guest page tables, so we do not need to
      * do more. */
-    if ( !shadow_mode_refcounts(d) )
+    if ( !paging_mode_refcounts(d) )
         return 0;
 
     /* Early exit if it's already a pagetable, or otherwise not writeable */
@@ -2075,7 +2075,7 @@ int sh_remove_all_mappings(struct domain
          *   guest pages with an extra reference taken by
          *   prepare_ring_for_helper().
          */
-        if ( !(shadow_mode_external(d)
+        if ( !(paging_mode_external(d)
                && (page->count_info & PGC_count_mask) <= 3
                && ((page->u.inuse.type_info & PGT_count_mask)
                    == (is_special_page(page) ||
@@ -2372,8 +2372,8 @@ static void sh_update_paging_modes(struc
     {
         const struct paging_mode *old_mode = v->arch.paging.mode;
 
-        ASSERT(shadow_mode_translate(d));
-        ASSERT(shadow_mode_external(d));
+        ASSERT(paging_mode_translate(d));
+        ASSERT(paging_mode_external(d));
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
         /* Need to resync all our pages now, because if a page goes out
@@ -2760,7 +2760,7 @@ void shadow_vcpu_teardown(struct vcpu *v
 
     sh_detach_old_tables(v);
 #ifdef CONFIG_HVM
-    if ( shadow_mode_external(d) )
+    if ( paging_mode_external(d) )
     {
         mfn_t mfn = pagetable_get_mfn(v->arch.hvm.monitor_table);
 
xen/arch/x86/mm/shadow/multi.c +18 -18
@@ -514,7 +514,7 @@ _sh_propagate(struct vcpu *v,
                || (level == 1
                    && page_get_owner(mfn_to_page(target_mfn)) == dom_io);
     if ( mmio_mfn
-         && !(level == 1 && (!shadow_mode_refcounts(d)
+         && !(level == 1 && (!paging_mode_refcounts(d)
                              || p2mt == p2m_mmio_direct)) )
     {
         ASSERT((ft == ft_prefetch));
@@ -531,7 +531,7 @@ _sh_propagate(struct vcpu *v,
                        _PAGE_RW | _PAGE_PRESENT);
     if ( guest_nx_enabled(v) )
         pass_thru_flags |= _PAGE_NX_BIT;
-    if ( level == 1 && !shadow_mode_refcounts(d) && mmio_mfn )
+    if ( level == 1 && !paging_mode_refcounts(d) && mmio_mfn )
         pass_thru_flags |= PAGE_CACHE_ATTRS;
     sflags = gflags & pass_thru_flags;
 
@@ -651,7 +651,7 @@ _sh_propagate(struct vcpu *v,
      * (We handle log-dirty entirely inside the shadow code, without using the
      * p2m_ram_logdirty p2m type: only HAP uses that.)
      */
-    if ( level == 1 && unlikely(shadow_mode_log_dirty(d)) && !mmio_mfn )
+    if ( level == 1 && unlikely(paging_mode_log_dirty(d)) && !mmio_mfn )
     {
         if ( ft & FETCH_TYPE_WRITE )
             paging_mark_dirty(d, target_mfn);
@@ -807,7 +807,7 @@ do {
 #define FOREACH_PRESENT_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code)    \
 do {                                                                      \
     int _i, _j;                                                           \
-    ASSERT(shadow_mode_external(_dom));                                   \
+    ASSERT(paging_mode_external(_dom));                                   \
     ASSERT(mfn_to_page(_sl2mfn)->u.sh.type == SH_type_l2_32_shadow);      \
     for ( _j = 0; _j < 4; _j++ )                                          \
     {                                                                     \
@@ -833,7 +833,7 @@ do {
 do {                                                                       \
     int _i;                                                                \
     shadow_l2e_t *_sp = map_domain_page((_sl2mfn));                        \
-    ASSERT(shadow_mode_external(_dom));                                    \
+    ASSERT(paging_mode_external(_dom));                                    \
     ASSERT(mfn_to_page(_sl2mfn)->u.sh.type == SH_type_l2_pae_shadow);      \
     for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ )                 \
     {                                                                      \
@@ -854,7 +854,7 @@ do {
     unsigned int _i, _end = SHADOW_L2_PAGETABLE_ENTRIES;                    \
     shadow_l2e_t *_sp = map_domain_page((_sl2mfn));                         \
     ASSERT_VALID_L2(mfn_to_page(_sl2mfn)->u.sh.type);                       \
-    if ( is_pv_32bit_domain(_dom) /* implies !shadow_mode_external */ &&    \
+    if ( is_pv_32bit_domain(_dom) /* implies !paging_mode_external */ &&    \
          mfn_to_page(_sl2mfn)->u.sh.type != SH_type_l2_64_shadow )          \
         _end = COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom);                    \
     for ( _i = 0; _i < _end; ++_i )                                         \
@@ -896,7 +896,7 @@ do {
 #define FOREACH_PRESENT_L4E(_sl4mfn, _sl4e, _gl4p, _done, _dom, _code)  \
 do {                                                                    \
     shadow_l4e_t *_sp = map_domain_page((_sl4mfn));                     \
-    int _xen = !shadow_mode_external(_dom);                             \
+    int _xen = !paging_mode_external(_dom);                             \
     int _i;                                                             \
     ASSERT(mfn_to_page(_sl4mfn)->u.sh.type == SH_type_l4_64_shadow);\
     for ( _i = 0; _i < SHADOW_L4_PAGETABLE_ENTRIES; _i++ )              \
@@ -965,7 +965,7 @@ sh_make_shadow(struct vcpu *v, mfn_t gmf
 #endif
 
     // Create the Xen mappings...
-    if ( !shadow_mode_external(d) )
+    if ( !paging_mode_external(d) )
     {
         switch (shadow_type)
         {
@@ -1367,7 +1367,7 @@ void sh_destroy_l1_shadow(struct domain
         shadow_demote(d, gmfn, t);
     }
 
-    if ( shadow_mode_refcounts(d) )
+    if ( paging_mode_refcounts(d) )
     {
         /* Decrement refcounts of all the old entries */
         mfn_t sl1mfn = smfn;
@@ -1464,7 +1464,7 @@ static int cf_check validate_gl4e(
     l4e_propagate_from_guest(v, new_gl4e, sl3mfn, &new_sl4e, ft_prefetch);
 
     // check for updates to xen reserved slots
-    if ( !shadow_mode_external(d) )
+    if ( !paging_mode_external(d) )
     {
         int shadow_index = (((unsigned long)sl4p & ~PAGE_MASK) /
                             sizeof(shadow_l4e_t));
@@ -2387,7 +2387,7 @@ static int cf_check sh_page_fault(
     gfn = guest_walk_to_gfn(&gw);
     gmfn = get_gfn(d, gfn, &p2mt);
 
-    if ( shadow_mode_refcounts(d) &&
+    if ( paging_mode_refcounts(d) &&
          ((!p2m_is_valid(p2mt) && !p2m_is_grant(p2mt)) ||
           (!p2m_is_mmio(p2mt) && !mfn_valid(gmfn))) )
     {
@@ -2611,7 +2611,7 @@ static int cf_check sh_page_fault(
     return EXCRET_fault_fixed;
 
  emulate:
-    if ( !shadow_mode_refcounts(d) )
+    if ( !paging_mode_refcounts(d) )
         goto not_a_shadow_fault;
 
 #ifdef CONFIG_HVM
@@ -3055,7 +3055,7 @@ sh_update_linear_entries(struct vcpu *v)
      */
 
     /* Don't try to update the monitor table if it doesn't exist */
-    if ( !shadow_mode_external(d) ||
+    if ( !paging_mode_external(d) ||
          pagetable_get_pfn(v->arch.hvm.monitor_table) == 0 )
         return;
 
@@ -3204,7 +3204,7 @@ static void cf_check sh_update_cr3(struc
     /* Double-check that the HVM code has sent us a sane guest_table */
     if ( is_hvm_domain(d) )
     {
-        ASSERT(shadow_mode_external(d));
+        ASSERT(paging_mode_external(d));
         if ( hvm_paging_enabled(v) )
             ASSERT(pagetable_get_pfn(v->arch.guest_table));
         else
@@ -3229,7 +3229,7 @@ static void cf_check sh_update_cr3(struc
      * table.  We cache the current state of that table and shadow that,
      * until the next CR3 write makes us refresh our cache.
      */
-    ASSERT(shadow_mode_external(d));
+    ASSERT(paging_mode_external(d));
 
     /*
      * Find where in the page the l3 table is, but ignore the low 2 bits of
@@ -3260,7 +3260,7 @@ static void cf_check sh_update_cr3(struc
         ASSERT(d->is_dying || d->is_shutting_down);
         return;
     }
-    if ( !shadow_mode_external(d) && !is_pv_32bit_domain(d) )
+    if ( !paging_mode_external(d) && !is_pv_32bit_domain(d) )
     {
         mfn_t smfn = pagetable_get_mfn(v->arch.paging.shadow.shadow_table[0]);
 
@@ -3354,7 +3354,7 @@ static void cf_check sh_update_cr3(struc
     ///
     /// v->arch.cr3
     ///
-    if ( shadow_mode_external(d) )
+    if ( paging_mode_external(d) )
     {
         make_cr3(v, pagetable_get_mfn(v->arch.hvm.monitor_table));
     }
@@ -3371,7 +3371,7 @@ static void cf_check sh_update_cr3(struc
     ///
     /// v->arch.hvm.hw_cr[3]
     ///
-    if ( shadow_mode_external(d) )
+    if ( paging_mode_external(d) )
     {
         ASSERT(is_hvm_domain(d));
 #if SHADOW_PAGING_LEVELS == 3
xen/arch/x86/mm/shadow/private.h +3 -3
@@ -407,7 +407,7 @@ static inline int sh_remove_write_access
                                          unsigned int level,
                                          unsigned long fault_addr)
 {
-    ASSERT(!shadow_mode_refcounts(d));
+    ASSERT(!paging_mode_refcounts(d));
     return 0;
 }
 #endif
@@ -520,8 +520,8 @@ sh_mfn_is_a_page_table(mfn_t gmfn)
         return 0;
 
     owner = page_get_owner(page);
-    if ( owner && shadow_mode_refcounts(owner)
-         && (page->count_info & PGC_shadowed_pt) )
+    if ( owner && paging_mode_refcounts(owner) &&
+         (page->count_info & PGC_shadowed_pt) )
         return 1;
 
     type_info = page->u.inuse.type_info & PGT_type_mask;
xen/arch/x86/mm/shadow/set.c +3 -3
@@ -81,7 +81,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl
     struct domain *owner = NULL;
 
     ASSERT(!sh_l1e_is_magic(sl1e));
-    ASSERT(shadow_mode_refcounts(d));
+    ASSERT(paging_mode_refcounts(d));
 
     if ( mfn_valid(mfn) )
     {
@@ -342,7 +342,7 @@ int shadow_set_l1e(struct domain *d, sha
          !sh_l1e_is_magic(new_sl1e) )
     {
         /* About to install a new reference */
-        if ( shadow_mode_refcounts(d) )
+        if ( paging_mode_refcounts(d) )
         {
 #define PAGE_FLIPPABLE (_PAGE_RW | _PAGE_PWT | _PAGE_PCD | _PAGE_PAT)
             int rc;
@@ -375,7 +375,7 @@ int shadow_set_l1e(struct domain *d, sha
 
     old_sl1f = shadow_l1e_get_flags(old_sl1e);
     if ( (old_sl1f & _PAGE_PRESENT) && !sh_l1e_is_magic(old_sl1e) &&
-         shadow_mode_refcounts(d) )
+         paging_mode_refcounts(d) )
     {
         /*
          * We lost a reference to an old mfn.
xen/arch/x86/mm/shadow/types.h +1 -1
@@ -262,7 +262,7 @@ int shadow_set_l4e(struct domain *d, sha
 static void inline
 shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
 {
-    if ( !shadow_mode_refcounts(d) )
+    if ( !paging_mode_refcounts(d) )
         return;
 
     put_page_from_l1e(sl1e, d);
Acked-by on v3 4/12
#8 lore raw

Re: [PATCH v3 04/12] x86/shadow: use lighter weight mode checks

Quoted 7 lines · Jan Beulich

shadow_mode_...(), with the exception of shadow_mode_enabled(), are shorthands for shadow_mode_enabled() && paging_mode_...(). While potentially useful outside of shadow-internal functions, when we already know that we're dealing with a domain in shadow mode, the "paging" checks are sufficient and cheaper. While the "shadow" ones commonly translate to a MOV/AND/CMP/Jcc sequence, the "paging" ones typically resolve to just TEST+Jcc.

That's because shadow_*() are checking a minimum of 2 bits, while most of the paging_*() ones are only checking a single bit.

Quoted 1 line
#9 lore raw

PATCH v3 5/12 x86/shadow: move OOS functions to their own file

In reply to Jan Beulich

The code has been identified as HVM-only, and its main functions are pretty well isolated. Move them to their own file. While moving, besides making two functions non-static, do a few style adjustments, mainly comment formatting, but leave the code otherwise untouched.

---
v2: Adjust SPDX to GPL-2.0-only. A few more style adjustments.

xen/arch/x86/mm/shadow/Makefile +1 -1
@@ -1,6 +1,6 @@
 ifeq ($(CONFIG_SHADOW_PAGING),y)
 obj-y += common.o set.o
-obj-$(CONFIG_HVM) += hvm.o guest_2.o guest_3.o guest_4.o
+obj-$(CONFIG_HVM) += hvm.o guest_2.o guest_3.o guest_4.o oos.o
 obj-$(CONFIG_PV) += pv.o guest_4.o
 else
 obj-y += none.o
xen/arch/x86/mm/shadow/common.c +0 -570

576 lines not on this page · raw message

xen/arch/x86/mm/shadow/oos.c new file +606 -0

606 lines not on this page · raw message

xen/arch/x86/mm/shadow/private.h +2 -0
@@ -439,6 +439,7 @@ int sh_unsync(struct vcpu *v, mfn_t gmfn
 /* Pull an out-of-sync page back into sync. */
 void sh_resync(struct domain *d, mfn_t gmfn);
 
+void oos_hash_remove(struct domain *d, mfn_t gmfn);
 void oos_fixup_add(struct domain *d, mfn_t gmfn, mfn_t smfn, unsigned long off);
 
 /* Pull all out-of-sync shadows back into sync.  If skip != 0, we try
@@ -464,6 +465,7 @@ shadow_sync_other_vcpus(struct vcpu *v)
     sh_resync_all(v, 1 /* skip */, 0 /* this */, 1 /* others */);
 }
 
+void sh_oos_audit(struct domain *d);
 void oos_audit_hash_is_present(struct domain *d, mfn_t gmfn);
 mfn_t oos_snapshot_lookup(struct domain *d, mfn_t gmfn);
 
#10 lore raw

PATCH v3 6/12 x86/shadow: restrict OOS allocation to when it's really needed

In reply to Jan Beulich

PV domains won't use it, and even HVM ones won't when OOS is turned off for them. There's therefore no point in putting extra pressure on the (limited) pool of memory.

While there also zap the sh_type_to_size[] entry when OOS is disabled altogether.

---
v3: New.

xen/arch/x86/mm/shadow/common.c +4 -1
@@ -61,7 +61,9 @@ const uint8_t sh_type_to_size[] = {
     [SH_type_l4_64_shadow]   = 1,
     [SH_type_p2m_table]      = 1,
     [SH_type_monitor_table]  = 1,
+#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
     [SH_type_oos_snapshot]   = 1,
+#endif
 };
 #endif /* CONFIG_HVM */
 
@@ -1771,7 +1773,8 @@ static void sh_update_paging_modes(struc
 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
-    if ( mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
+    if ( !(d->options & XEN_DOMCTL_CDF_oos_off) &&
+         mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
     {
         int i;
 
Acked-by on v3 6/12
#11 lore raw

Re: [PATCH v3 06/12] x86/shadow: restrict OOS allocation to when it's really needed

Quoted 8 lines · Jan Beulich

PV domains won't use it, and even HVM ones won't when OOS is turned off for them. There's therefore no point in putting extra pressure on the (limited) pool of memory.

While there also zap the sh_type_to_size[] entry when OOS is disabled altogether.

Thanks, Roger.

#12 lore raw

Re: [PATCH v3 06/12] x86/shadow: restrict OOS allocation to when it's really needed

In reply to Jan Beulich
Quoted 25 lines · Jan Beulich

PV domains won't use it, and even HVM ones won't when OOS is turned off for them. There's therefore no point in putting extra pressure on the (limited) pool of memory.

While there also zap the sh_type_to_size[] entry when OOS is disabled altogether.

---
v3: New.

xen/arch/x86/mm/shadow/common.c +2 -0
@@ -61,7 +61,9 @@ const uint8_t sh_type_to_size[] = {
     [SH_type_l4_64_shadow]   = 1,
     [SH_type_p2m_table]      = 1,
     [SH_type_monitor_table]  = 1,
+#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
     [SH_type_oos_snapshot]   = 1,
+#endif
 };
 #endif /* CONFIG_HVM */
 

xen/arch/x86/mm/shadow/common.c static void sh_update_paging_modes(struc · line 1780

Quoted 9 lines
xen/arch/x86/mm/shadow/common.c +2 -1
@@ -1771,7 +1773,8 @@ static void sh_update_paging_modes(struc
 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
-    if ( mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
+    if ( !(d->options & XEN_DOMCTL_CDF_oos_off) &&
+         mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
     {
         int i;
 

I've never seen XEN_DOMCTL_CDF_oos_off used.  Xapi has no plumbing for this, and xl only inherited it from xend.

At this point, OOS is the tested path and OOS_OFF is the untested path. I think we should remove the flag and let OOS be unconditional like all the other shadow optimisations.

~Andrew

#13 lore raw

Re: [PATCH v3 06/12] x86/shadow: restrict OOS allocation to when it's really needed

Quoted 37 lines · Andrew Cooper
Quoted 24 lines · Jan Beulich

PV domains won't use it, and even HVM ones won't when OOS is turned off for them. There's therefore no point in putting extra pressure on the (limited) pool of memory.

While there also zap the sh_type_to_size[] entry when OOS is disabled altogether.

---
v3: New.

xen/arch/x86/mm/shadow/common.c +2 -0
@@ -61,7 +61,9 @@ const uint8_t sh_type_to_size[] = {
     [SH_type_l4_64_shadow]   = 1,
     [SH_type_p2m_table]      = 1,
     [SH_type_monitor_table]  = 1,
+#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
     [SH_type_oos_snapshot]   = 1,
+#endif
 };
 #endif /* CONFIG_HVM */
 

xen/arch/x86/mm/shadow/common.c static void sh_update_paging_modes(struc · line 1779

Quoted 8 lines
xen/arch/x86/mm/shadow/common.c +2 -1
@@ -1771,7 +1773,8 @@ static void sh_update_paging_modes(struc
 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
-    if ( mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
+    if ( !(d->options & XEN_DOMCTL_CDF_oos_off) &&
+         mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
     {
         int i;

I've never seen XEN_DOMCTL_CDF_oos_off used.  Xapi has no plumbing for this, and xl only inherited it from xend.

Quoted 3 lines

At this point, OOS is the tested path and OOS_OFF is the untested path. I think we should remove the flag and let OOS be unconditional like all the other shadow optimisations.

It being a guest config setting, I'm hesitant. Maybe we should actually make sure it's tested, so that in the case it's needed it could be named as a mitigation for an XSA?

Jan

#14 lore raw

Re: [PATCH v3 06/12] x86/shadow: restrict OOS allocation to when it's really needed

Quoted 27 lines · Jan Beulich
Quoted 26 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich
xen/arch/x86/mm/shadow/common.c +2 -0
@@ -61,7 +61,9 @@ const uint8_t sh_type_to_size[] = {
     [SH_type_l4_64_shadow]   = 1,
     [SH_type_p2m_table]      = 1,
     [SH_type_monitor_table]  = 1,
+#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
     [SH_type_oos_snapshot]   = 1,
+#endif
 };
 #endif /* CONFIG_HVM */
 

xen/arch/x86/mm/shadow/common.c static void sh_update_paging_modes(struc · line 1779

Quoted 8 lines
xen/arch/x86/mm/shadow/common.c +2 -1
@@ -1771,7 +1773,8 @@ static void sh_update_paging_modes(struc
 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
 
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
-    if ( mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
+    if ( !(d->options & XEN_DOMCTL_CDF_oos_off) &&
+         mfn_eq(v->arch.paging.shadow.oos_snapshot[0], INVALID_MFN) )
     {
         int i;

I've never seen XEN_DOMCTL_CDF_oos_off used.  Xapi has no plumbing for this, and xl only inherited it from xend.

Quoted 7 lines
Quoted 3 lines

At this point, OOS is the tested path and OOS_OFF is the untested path. I think we should remove the flag and let OOS be unconditional like all the other shadow optimisations.

It being a guest config setting, I'm hesitant. Maybe we should actually make sure it's tested, so that in the case it's needed it could be named as a mitigation for an XSA?

Also, ftaod, this change had gone in already long ago, with Roger's ack.

Jan

#15 lore raw

PATCH v3 7/12 x86/shadow: OOS doesn't track VAs anymore

In reply to Jan Beulich

The tracking lasted only for about two weeks, but the related comment parts were never purged.

Fixes 50b74f55e0c0 ("OOS cleanup: Fixup arrays instead of fixup tables")

---
I'm heavily inclined to fold this into "x86/shadow: move OOS functions to their own file".

This largely removes the justification for the per-vCPU hash. Is there any other reason to name there in exchange?
---
v3: New.

xen/arch/x86/mm/shadow/oos.c +2 -5
@@ -51,13 +51,10 @@
  *
  * Currently out-of-sync pages are listed in a simple open-addressed
  * hash table with a second chance (must resist temptation to radically
- * over-engineer hash tables...)  The virtual address of the access
- * which caused us to unsync the page is also kept in the hash table, as
- * a hint for finding the writable mappings later.
+ * over-engineer hash tables...).
  *
  * We keep a hash per vcpu, because we want as much as possible to do
- * the re-sync on the save vcpu we did the unsync on, so the VA hint
- * will be valid.
+ * the re-sync on the same vcpu we did the unsync on.
  */
 
 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_FULL
#17 lore raw

PATCH v3 8/12 x86/shadow: sh_rm_write_access_from_sl1p() is HVM-only

In reply to Jan Beulich

The function is used from (HVM-only) OOS code only - replace the respective #ifdef inside the function to make this more obvious. (Note that SHOPT_OUT_OF_SYNC won't be set when !HVM, so the #ifdef surrounding the function is already sufficient.)

---
v2: New.

xen/arch/x86/mm/shadow/multi.c +0 -4
@@ -3417,9 +3417,7 @@ static void cf_check sh_update_cr3(struc
 int sh_rm_write_access_from_sl1p(struct domain *d, mfn_t gmfn,
                                  mfn_t smfn, unsigned long off)
 {
-#ifdef CONFIG_HVM
     struct vcpu *curr = current;
-#endif
     int r;
     shadow_l1e_t *sl1p, sl1e;
     struct page_info *sp;
@@ -3427,12 +3425,10 @@ int sh_rm_write_access_from_sl1p(struct
     ASSERT(mfn_valid(gmfn));
     ASSERT(mfn_valid(smfn));
 
-#ifdef CONFIG_HVM
     /* Remember if we've been told that this process is being torn down */
     if ( curr->domain == d && is_hvm_domain(d) )
         curr->arch.paging.shadow.pagetable_dying
             = mfn_to_page(gmfn)->pagetable_dying;
-#endif
 
     sp = mfn_to_page(smfn);
 
Acked-by on v3 8/12
#18 lore raw

Re: [PATCH v3 08/12] x86/shadow: sh_rm_write_access_from_sl1p() is HVM-only

Quoted 7 lines · Jan Beulich

The function is used from (HVM-only) OOS code only - replace the respective #ifdef inside the function to make this more obvious. (Note that SHOPT_OUT_OF_SYNC won't be set when !HVM, so the #ifdef surrounding the function is already sufficient.)

#19 lore raw

PATCH v3 9/12 x86/shadow: drop is_hvm_...() where easily possible

In reply to Jan Beulich

Emulation related functions are involved in HVM handling only, and in some cases they even invoke such checks after having already done things which are valid for HVM domains only. OOS active also implies HVM. In sh_remove_all_mappings() one of the two checks is redundant with an earlier paging_mode_external() one (the other, however, needs to stay).

---
v2: Re-base over changes/additions earlier in the series.

xen/arch/x86/mm/shadow/common.c +1 -1
@@ -1511,7 +1511,7 @@ int sh_remove_all_mappings(struct domain
                && (page->count_info & PGC_count_mask) <= 3
                && ((page->u.inuse.type_info & PGT_count_mask)
                    == (is_special_page(page) ||
-                       (is_hvm_domain(d) && is_ioreq_server_page(d, page))))) )
+                       is_ioreq_server_page(d, page)))) )
             printk(XENLOG_G_ERR "can't find all mappings of mfn %"PRI_mfn
                    " (gfn %"PRI_gfn"): c=%lx t=%lx s=%d i=%d\n",
                    mfn_x(gmfn), gfn_x(gfn),
xen/arch/x86/mm/shadow/hvm.c +1 -19
@@ -192,10 +192,6 @@ hvm_emulate_write(enum x86_segment seg,
     if ( rc || !bytes )
         return rc;
 
-    /* Unaligned writes are only acceptable on HVM */
-    if ( (addr & (bytes - 1)) && !is_hvm_vcpu(v)  )
-        return X86EMUL_UNHANDLEABLE;
-
     ptr = sh_emulate_map_dest(v, addr, bytes, sh_ctxt);
     if ( IS_ERR(ptr) )
         return ~PTR_ERR(ptr);
@@ -246,10 +242,6 @@ hvm_emulate_cmpxchg(enum x86_segment seg
     if ( rc )
         return rc;
 
-    /* Unaligned writes are only acceptable on HVM */
-    if ( (addr & (bytes - 1)) && !is_hvm_vcpu(v)  )
-        return X86EMUL_UNHANDLEABLE;
-
     ptr = sh_emulate_map_dest(v, addr, bytes, sh_ctxt);
     if ( IS_ERR(ptr) )
         return ~PTR_ERR(ptr);
@@ -445,8 +437,7 @@ static void *sh_emulate_map_dest(struct
 
 #ifndef NDEBUG
     /* We don't emulate user-mode writes to page tables. */
-    if ( is_hvm_domain(d) ? hvm_get_cpl(v) == 3
-                          : !guest_kernel_mode(v, guest_cpu_user_regs()) )
+    if ( hvm_get_cpl(v) == 3 )
     {
         gdprintk(XENLOG_DEBUG, "User-mode write to pagetable reached "
                  "emulate_map_dest(). This should never happen!\n");
@@ -475,15 +466,6 @@ static void *sh_emulate_map_dest(struct
         sh_ctxt->mfn[1] = INVALID_MFN;
         map = map_domain_page(sh_ctxt->mfn[0]) + (vaddr & ~PAGE_MASK);
     }
-    else if ( !is_hvm_domain(d) )
-    {
-        /*
-         * Cross-page emulated writes are only supported for HVM guests;
-         * PV guests ought to know better.
-         */
-        put_page(mfn_to_page(sh_ctxt->mfn[0]));
-        return MAPPING_UNHANDLEABLE;
-    }
     else
     {
         /* This write crosses a page boundary. Translate the second page. */
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -3426,7 +3426,7 @@ int sh_rm_write_access_from_sl1p(struct
     ASSERT(mfn_valid(smfn));
 
     /* Remember if we've been told that this process is being torn down */
-    if ( curr->domain == d && is_hvm_domain(d) )
+    if ( curr->domain == d )
         curr->arch.paging.shadow.pagetable_dying
             = mfn_to_page(gmfn)->pagetable_dying;
 
xen/arch/x86/mm/shadow/oos.c +0 -1
@@ -577,7 +577,6 @@ int sh_unsync(struct vcpu *v, mfn_t gmfn
     if ( (pg->shadow_flags &
           ((SHF_page_type_mask & ~SHF_L1_ANY) | SHF_out_of_sync)) ||
          sh_page_has_multiple_shadows(pg) ||
-         !is_hvm_vcpu(v) ||
          !v->domain->arch.paging.shadow.oos_active )
         return 0;
 
#20 lore raw

Re: [PATCH v3 09/12] x86/shadow: drop is_hvm_...() where easily possible

Quoted 1 line · Jan Beulich

Emulation related functions are involved in HVM handling only

I think this wants clarifying a little.  *These* emulation functions are only used by HVM.

pv/ro-page-fault.c does have emulation for PV guests, but uses paging_{write,cmpxchg}_guest_entry() to enter into the shadow code.

Quoted 7 lines

, and in
some cases they even invoke such checks after having already done things which are valid for HVM domains only. OOS active also implies HVM. In sh_remove_all_mappings() one of the two checks is redundant with an earlier paging_mode_external() one (the other, however, needs to stay).

Preferably with the commit message tweaked, Acked-by: Andrew Cooper <[email protected]>

#21 lore raw

Re: [PATCH v3 09/12] x86/shadow: drop is_hvm_...() where easily possible

Quoted 8 lines · Andrew Cooper
Quoted 1 line · Jan Beulich

Emulation related functions are involved in HVM handling only

I think this wants clarifying a little.  *These* emulation functions are only used by HVM.

pv/ro-page-fault.c does have emulation for PV guests, but uses paging_{write,cmpxchg}_guest_entry() to enter into the shadow code.

I don't mind; I would have thought that x86/shadow: already narrows scope enough.

Quoted 10 lines
Quoted 7 lines

, and in
some cases they even invoke such checks after having already done things which are valid for HVM domains only. OOS active also implies HVM. In sh_remove_all_mappings() one of the two checks is redundant with an earlier paging_mode_external() one (the other, however, needs to stay).

Preferably with the commit message tweaked, Acked-by: Andrew Cooper <[email protected]>

Thanks.

Jan

#22 lore raw

PATCH v3 10/12 x86/shadow: make monitor table create/destroy more consistent

In reply to Jan Beulich

While benign at present, it is still a little fragile to operate on a wrong "old_mode" value in sh_update_paging_modes(). This can happen when no monitor table was present initially - we'd create one for the new mode without updating old_mode. Correct this in two ways, each of which would be sufficient on its own: Once by adding "else" to the second of the involved if()s in the function, and then by setting the correct initial mode for HVM domains in shadow_vcpu_init().

Further use the same predicate (paging_mode_external()) consistently when dealing with shadow mode init/update/cleanup, rather than a mix of is_hvm_vcpu() (init), is_hvm_domain() (update), and paging_mode_external() (cleanup).

Finally drop a redundant is_hvm_domain() from inside the bigger if() (which is being converted to paging_mode_external()) in sh_update_paging_modes().

---
v2: Style adjustment.

xen/arch/x86/mm/shadow/common.c +6 -7
@@ -119,9 +119,9 @@ void shadow_vcpu_init(struct vcpu *v)
     }
 #endif
 
-    v->arch.paging.mode = is_hvm_vcpu(v) ?
-                          &SHADOW_INTERNAL_NAME(sh_paging_mode, 3) :
-                          &SHADOW_INTERNAL_NAME(sh_paging_mode, 4);
+    v->arch.paging.mode = paging_mode_external(v->domain)
+                          ? &SHADOW_INTERNAL_NAME(sh_paging_mode, 2)
+                          : &SHADOW_INTERNAL_NAME(sh_paging_mode, 4);
 }
 
 #if SHADOW_AUDIT
@@ -1801,7 +1801,7 @@ static void sh_update_paging_modes(struc
         sh_detach_old_tables(v);
 
 #ifdef CONFIG_HVM
-    if ( is_hvm_domain(d) )
+    if ( paging_mode_external(d) )
     {
         const struct paging_mode *old_mode = v->arch.paging.mode;
 
@@ -1854,13 +1854,12 @@ static void sh_update_paging_modes(struc
             make_cr3(v, mmfn);
             hvm_update_host_cr3(v);
         }
-
-        if ( v->arch.paging.mode != old_mode )
+        else if ( v->arch.paging.mode != old_mode )
         {
             SHADOW_PRINTK("new paging mode: %pv pe=%d gl=%u "
                           "sl=%u (was g=%u s=%u)\n",
                           v,
-                          is_hvm_domain(d) ? hvm_paging_enabled(v) : 1,
+                          hvm_paging_enabled(v),
                           v->arch.paging.mode->guest_levels,
                           v->arch.paging.mode->shadow.shadow_levels,
                           old_mode ? old_mode->guest_levels : 0,
Acked-by on v3 10/12
#23 lore raw

Re: [PATCH v3 10/12] x86/shadow: make monitor table create/destroy more consistent

Quoted 13 lines · Jan Beulich

While benign at present, it is still a little fragile to operate on a wrong "old_mode" value in sh_update_paging_modes(). This can happen when no monitor table was present initially - we'd create one for the new mode without updating old_mode. Correct this in two ways, each of which would be sufficient on its own: Once by adding "else" to the second of the involved if()s in the function, and then by setting the correct initial mode for HVM domains in shadow_vcpu_init().

Further use the same predicate (paging_mode_external()) consistently when dealing with shadow mode init/update/cleanup, rather than a mix of is_hvm_vcpu() (init), is_hvm_domain() (update), and paging_mode_external() (cleanup).

Quoted 5 lines

Finally drop a redundant is_hvm_domain() from inside the bigger if() (which is being converted to paging_mode_external()) in sh_update_paging_modes().

#24 lore raw

PATCH v3 11/12 x86/shadow: vCPU-s never have "no mode"

In reply to Jan Beulich

With an initial mode installed by shadow_vcpu_init(), there's no need for sh_update_paging_modes() to deal with the "mode is still unset" case. Leave an assertion, though.

xen/arch/x86/mm/shadow/common.c +6 -5
@@ -1854,6 +1854,8 @@ static void sh_update_paging_modes(struc
             make_cr3(v, mmfn);
             hvm_update_host_cr3(v);
         }
+        else if ( !old_mode )
+            ASSERT_UNREACHABLE();
         else if ( v->arch.paging.mode != old_mode )
         {
             SHADOW_PRINTK("new paging mode: %pv pe=%d gl=%u "
@@ -1862,11 +1864,10 @@ static void sh_update_paging_modes(struc
                           hvm_paging_enabled(v),
                           v->arch.paging.mode->guest_levels,
                           v->arch.paging.mode->shadow.shadow_levels,
-                          old_mode ? old_mode->guest_levels : 0,
-                          old_mode ? old_mode->shadow.shadow_levels : 0);
-            if ( old_mode &&
-                 (v->arch.paging.mode->shadow.shadow_levels !=
-                  old_mode->shadow.shadow_levels) )
+                          old_mode->guest_levels,
+                          old_mode->shadow.shadow_levels);
+            if ( v->arch.paging.mode->shadow.shadow_levels !=
+                 old_mode->shadow.shadow_levels )
             {
                 /* Need to make a new monitor table for the new mode */
                 mfn_t new_mfn, old_mfn;
#25 lore raw

PATCH v3 12/12 x86/shadow: adjust monitor table prealloc amount

In reply to Jan Beulich

While 670d6b908ff2 ('x86 shadow: Move the shadow linear mapping for n-on-3-on-4 shadows so') bumped the amount by one too little for the 32-on-64 case (which luckily was dead code, and hence a bump wasn't necessary in the first place), 0b841314dace ('x86/shadow:
sh_{make,destroy}_monitor_table() are "even more" HVM-only'), dropping the dead code, then didn't adjust the amount back. Yet even the original amount was too high in certain cases. Switch to pre-allocating just as much as is going to be needed.

xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -726,7 +726,7 @@ mfn_t sh_make_monitor_table(const struct
     ASSERT(!pagetable_get_pfn(v->arch.hvm.monitor_table));
 
     /* Guarantee we can get the memory we need */
-    if ( !shadow_prealloc(d, SH_type_monitor_table, CONFIG_PAGING_LEVELS) )
+    if ( !shadow_prealloc(d, SH_type_monitor_table, shadow_levels < 4 ? 3 : 1) )
         return INVALID_MFN;
 
     m4mfn = shadow_alloc(d, SH_type_monitor_table, 0);
Acked-by on v3 12/12
#26 lore raw

Re: [PATCH v3 12/12] x86/shadow: adjust monitor table prealloc amount

Quoted 10 lines · Jan Beulich

While 670d6b908ff2 ('x86 shadow: Move the shadow linear mapping for n-on-3-on-4 shadows so') bumped the amount by one too little for the 32-on-64 case (which luckily was dead code, and hence a bump wasn't necessary in the first place), 0b841314dace ('x86/shadow:
sh_{make,destroy}_monitor_table() are "even more" HVM-only'), dropping the dead code, then didn't adjust the amount back. Yet even the original amount was too high in certain cases. Switch to pre-allocating just as much as is going to be needed.