Skip to contentSkip to conversation

x86: shim building adjustments (plus shadow follow-on)

PATCH Superseded Jan Beulich 3 participants 12 messages Oct 19, 2020 – Apr 27, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover x86: shim building adjustments (plus shadow follow-on)

The change addressing the shadow related build issue noticed by Andrew went in already. The build breakage goes beyond this specific combination though - PV_SHIM_EXCLUSIVE plus HVM is similarly an issue. This is what the 1st patch tries to take care of, in a shape already on irc noticed to be controversial. I'm submitting the change nevertheless because for the moment there looks to be a majority in favor of going this route. One argument not voiced there yet: What good does it do to allow a user to enable HVM when then on the resulting hypervisor they still can't run HVM guests (for the hypervisor still being a dedicated PV shim one). On top of this, the alternative approach is likely going to get ugly.

The shadow related adjustments are here merely because the want to make them was noticed in the context of the patch which has already gone in.

1: don't permit HVM and PV_SHIM_EXCLUSIVE at the same time 2: refactor shadow_vram_{get,put}_l1e()
3: sh_{make,destroy}_monitor_table() are "even more" HVM-only

Jan

#1 lore raw

PATCH v3 1/3 x86/shim: don't permit HVM and PV_SHIM_EXCLUSIVE at the same time

This combination doesn't really make sense (and there likely are more); in particular even if the code built with both options set, HVM guests wouldn't work (and I think one wouldn't be able to create one in the first place). The alternative here would be some presumably intrusive #ifdef-ary to get this combination to actually build (but still not work) again.

Fixes 8b5b49ceb3d9 ("x86: don't include domctl and alike in shim-exclusive builds")

---
v2: Restore lost default setting.

xen/arch/x86/Kconfig +4 -3
@@ -23,7 +23,7 @@ config X86
 	select HAS_PDX
 	select HAS_SCHED_GRANULARITY
 	select HAS_UBSAN
-	select HAS_VPCI if !PV_SHIM_EXCLUSIVE && HVM
+	select HAS_VPCI if HVM
 	select NEEDS_LIBELF
 	select NUMA
 
@@ -90,8 +90,9 @@ config PV_LINEAR_PT
          If unsure, say Y.
 
 config HVM
-	def_bool !PV_SHIM_EXCLUSIVE
-	prompt "HVM support"
+	bool "HVM support"
+	depends on !PV_SHIM_EXCLUSIVE
+	default y
 	---help---
 	  Interfaces to support HVM domains.  HVM domains require hardware
 	  virtualisation extensions (e.g. Intel VT-x, AMD SVM), but can boot
#2 lore raw

PATCH v3 2/3 x86/shadow: refactor shadow_vram_{get,put}_l1e()

In reply to Jan Beulich

By passing the functions an MFN and flags, only a single instance of each is needed; they were pretty large for being inline functions anyway.

While moving the code, also adjust coding style and add const where sensible / possible.

---
v2: New.

xen/arch/x86/mm/shadow/hvm.c +98 -0
@@ -903,6 +903,104 @@ int shadow_track_dirty_vram(struct domai
     return rc;
 }
 
+void shadow_vram_get_mfn(mfn_t mfn, unsigned int l1f,
+                         mfn_t sl1mfn, const void *sl1e,
+                         const struct domain *d)
+{
+    unsigned long gfn;
+    struct sh_dirty_vram *dirty_vram = d->arch.hvm.dirty_vram;
+
+    ASSERT(is_hvm_domain(d));
+
+    if ( !dirty_vram /* tracking disabled? */ ||
+         !(l1f & _PAGE_RW) /* read-only mapping? */ ||
+         !mfn_valid(mfn) /* mfn can be invalid in mmio_direct */)
+        return;
+
+    gfn = gfn_x(mfn_to_gfn(d, mfn));
+    /* Page sharing not supported on shadow PTs */
+    BUG_ON(SHARED_M2P(gfn));
+
+    if ( (gfn >= dirty_vram->begin_pfn) && (gfn < dirty_vram->end_pfn) )
+    {
+        unsigned long i = gfn - dirty_vram->begin_pfn;
+        const struct page_info *page = mfn_to_page(mfn);
+
+        if ( (page->u.inuse.type_info & PGT_count_mask) == 1 )
+            /* Initial guest reference, record it */
+            dirty_vram->sl1ma[i] = mfn_to_maddr(sl1mfn) |
+                                   PAGE_OFFSET(sl1e);
+    }
+}
+
+void shadow_vram_put_mfn(mfn_t mfn, unsigned int l1f,
+                         mfn_t sl1mfn, const void *sl1e,
+                         const struct domain *d)
+{
+    unsigned long gfn;
+    struct sh_dirty_vram *dirty_vram = d->arch.hvm.dirty_vram;
+
+    ASSERT(is_hvm_domain(d));
+
+    if ( !dirty_vram /* tracking disabled? */ ||
+         !(l1f & _PAGE_RW) /* read-only mapping? */ ||
+         !mfn_valid(mfn) /* mfn can be invalid in mmio_direct */)
+        return;
+
+    gfn = gfn_x(mfn_to_gfn(d, mfn));
+    /* Page sharing not supported on shadow PTs */
+    BUG_ON(SHARED_M2P(gfn));
+
+    if ( (gfn >= dirty_vram->begin_pfn) && (gfn < dirty_vram->end_pfn) )
+    {
+        unsigned long i = gfn - dirty_vram->begin_pfn;
+        const struct page_info *page = mfn_to_page(mfn);
+        bool dirty = false;
+        paddr_t sl1ma = mfn_to_maddr(sl1mfn) | PAGE_OFFSET(sl1e);
+
+        if ( (page->u.inuse.type_info & PGT_count_mask) == 1 )
+        {
+            /* Last reference */
+            if ( dirty_vram->sl1ma[i] == INVALID_PADDR )
+            {
+                /* We didn't know it was that one, let's say it is dirty */
+                dirty = true;
+            }
+            else
+            {
+                ASSERT(dirty_vram->sl1ma[i] == sl1ma);
+                dirty_vram->sl1ma[i] = INVALID_PADDR;
+                if ( l1f & _PAGE_DIRTY )
+                    dirty = true;
+            }
+        }
+        else
+        {
+            /* We had more than one reference, just consider the page dirty. */
+            dirty = true;
+            /* Check that it's not the one we recorded. */
+            if ( dirty_vram->sl1ma[i] == sl1ma )
+            {
+                /* Too bad, we remembered the wrong one... */
+                dirty_vram->sl1ma[i] = INVALID_PADDR;
+            }
+            else
+            {
+                /*
+                 * Ok, our recorded sl1e is still pointing to this page, let's
+                 * just hope it will remain.
+                 */
+            }
+        }
+
+        if ( dirty )
+        {
+            dirty_vram->dirty_bitmap[i / 8] |= 1 << (i % 8);
+            dirty_vram->last_dirty = NOW();
+        }
+    }
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/mm/shadow/multi.c +17 -113

166 lines not on this page · raw message

xen/arch/x86/mm/shadow/private.h +8 -0
@@ -410,6 +410,14 @@ void shadow_update_paging_modes(struct v
  * With user_only == 1, unhooks only the user-mode mappings. */
 void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
 
+/* VRAM dirty tracking helpers. */
+void shadow_vram_get_mfn(mfn_t mfn, unsigned int l1f,
+                         mfn_t sl1mfn, const void *sl1e,
+                         const struct domain *d);
+void shadow_vram_put_mfn(mfn_t mfn, unsigned int l1f,
+                         mfn_t sl1mfn, const void *sl1e,
+                         const struct domain *d);
+
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
 /* Allow a shadowed page to go out of sync */
 int sh_unsync(struct vcpu *v, mfn_t gmfn);
Acked-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] x86/shadow: refactor shadow_vram_{get,put}_l1e()

Quoted 8 lines · Jan Beulich

By passing the functions an MFN and flags, only a single instance of each is needed; they were pretty large for being inline functions anyway.

While moving the code, also adjust coding style and add const where sensible / possible.

Thanks, Roger.

Acked-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] x86/shadow: refactor shadow_vram_{get,put}_l1e()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

By passing the functions an MFN and flags, only a single instance of each is needed; they were pretty large for being inline functions anyway.

While moving the code, also adjust coding style and add const where sensible / possible.

#5 lore raw

PATCH v3 3/3 x86/shadow: sh_{make,destroy}_monitor_table() are "even more" HVM-only

In reply to Jan Beulich

With them depending on just the number of shadow levels, there's no need for more than one instance of them, and hence no need for any hook (IOW 452219e24648 ["x86/shadow: monitor table is HVM-only"] didn't go quite far enough). Move the functions to hvm.c while dropping the dead is_pv_32bit_domain() code paths.

While moving the code, replace a stale comment reference to sh_install_xen_entries_in_l4(). Doing so made me notice the function also didn't have its prototype dropped in 8d7b633adab7 ("x86/mm:
Consolidate all Xen L4 slot writing into init_xen_l4_slots()"), which gets done here as well.

---
v3: New.
---
TBD: In principle both functions could have their first parameter
constified. In fact, "destroy" doesn't depend on the vCPU at all and hence could be passed a struct domain *. Not sure whether such an asymmetry would be acceptable.
In principle "make" would also not need passing of the number of shadow levels (can be derived from v), which would result in yet another asymmetry.
If these asymmetries were acceptable, "make" could then also update v->arch.hvm.monitor_table, instead of doing so at both call sites.
TBD: Collides with Andrew's "xen/x86: Fix memory leak in vcpu_create()
error path".

xen/arch/x86/mm/shadow/common.c +10 -4
@@ -2467,7 +2467,9 @@ static void sh_update_paging_modes(struc
 
         if ( pagetable_is_null(v->arch.hvm.monitor_table) )
         {
-            mfn_t mmfn = v->arch.paging.mode->shadow.make_monitor_table(v);
+            mfn_t mmfn = sh_make_monitor_table(
+                             v, v->arch.paging.mode->shadow.shadow_levels);
+
             v->arch.hvm.monitor_table = pagetable_from_mfn(mmfn);
             make_cr3(v, mmfn);
             hvm_update_host_cr3(v);
@@ -2504,7 +2506,8 @@ static void sh_update_paging_modes(struc
 
                 old_mfn = pagetable_get_mfn(v->arch.hvm.monitor_table);
                 v->arch.hvm.monitor_table = pagetable_null();
-                new_mfn = v->arch.paging.mode->shadow.make_monitor_table(v);
+                new_mfn = sh_make_monitor_table(
+                              v, v->arch.paging.mode->shadow.shadow_levels);
                 v->arch.hvm.monitor_table = pagetable_from_mfn(new_mfn);
                 SHADOW_PRINTK("new monitor table %"PRI_mfn "\n",
                                mfn_x(new_mfn));
@@ -2516,7 +2519,8 @@ static void sh_update_paging_modes(struc
                 if ( v == current )
                     write_ptbase(v);
                 hvm_update_host_cr3(v);
-                old_mode->shadow.destroy_monitor_table(v, old_mfn);
+                sh_destroy_monitor_table(v, old_mfn,
+                                         old_mode->shadow.shadow_levels);
             }
         }
 
@@ -2801,7 +2805,9 @@ void shadow_teardown(struct domain *d, b
                     mfn_t mfn = pagetable_get_mfn(v->arch.hvm.monitor_table);
 
                     if ( mfn_valid(mfn) && (mfn_x(mfn) != 0) )
-                        v->arch.paging.mode->shadow.destroy_monitor_table(v, mfn);
+                        sh_destroy_monitor_table(
+                            v, mfn,
+                            v->arch.paging.mode->shadow.shadow_levels);
                     v->arch.hvm.monitor_table = pagetable_null();
                 }
 #endif /* CONFIG_HVM */
xen/arch/x86/mm/shadow/hvm.c +82 -0

88 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +0 -124

142 lines not on this page · raw message

xen/arch/x86/mm/shadow/private.h +8 -3
@@ -366,9 +366,6 @@ void sh_set_toplevel_shadow(struct vcpu
                                                  mfn_t gmfn,
                                                  uint32_t shadow_type));
 
-/* Install the xen mappings in various flavours of shadow */
-void sh_install_xen_entries_in_l4(struct domain *, mfn_t gl4mfn, mfn_t sl4mfn);
-
 /* Update the shadows in response to a pagetable write from Xen */
 int sh_validate_guest_entry(struct vcpu *v, mfn_t gmfn, void *entry, u32 size);
 
@@ -410,6 +407,14 @@ void shadow_update_paging_modes(struct v
  * With user_only == 1, unhooks only the user-mode mappings. */
 void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
 
+/*
+ * sh_{make,destroy}_monitor_table() depend only on the number of shadow
+ * levels.
+ */
+mfn_t sh_make_monitor_table(struct vcpu *v, unsigned int shadow_levels);
+void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn,
+                              unsigned int shadow_levels);
+
 /* VRAM dirty tracking helpers. */
 void shadow_vram_get_mfn(mfn_t mfn, unsigned int l1f,
                          mfn_t sl1mfn, const void *sl1e,
xen/arch/x86/mm/shadow/types.h +0 -9
@@ -262,15 +262,6 @@ static inline shadow_l4e_t shadow_l4e_fr
 #define sh_rm_write_access_from_sl1p INTERNAL_NAME(sh_rm_write_access_from_sl1p)
 #endif
 
-/* sh_make_monitor_table depends only on the number of shadow levels */
-#define sh_make_monitor_table \
-        SHADOW_SH_NAME(sh_make_monitor_table, SHADOW_PAGING_LEVELS)
-#define sh_destroy_monitor_table \
-        SHADOW_SH_NAME(sh_destroy_monitor_table, SHADOW_PAGING_LEVELS)
-
-mfn_t sh_make_monitor_table(struct vcpu *v);
-void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn);
-
 #if SHADOW_PAGING_LEVELS == 3
 #define MFN_FITS_IN_HVM_CR3(_MFN) !(mfn_x(_MFN) >> 20)
 #endif
xen/include/asm-x86/paging.h +0 -2
@@ -107,8 +107,6 @@ struct shadow_paging_mode {
                                             mfn_t gmfn);
 #endif
 #ifdef CONFIG_HVM
-    mfn_t         (*make_monitor_table    )(struct vcpu *v);
-    void          (*destroy_monitor_table )(struct vcpu *v, mfn_t mmfn);
     int           (*guess_wrmap           )(struct vcpu *v, 
                                             unsigned long vaddr, mfn_t gmfn);
     void          (*pagetable_dying       )(paddr_t gpa);
Acked-by on v3 3/3
#6 lore raw

Re: [PATCH v3 3/3] x86/shadow: sh_{make,destroy}_monitor_table() are "even more" HVM-only

Quoted 6 lines · Jan Beulich

With them depending on just the number of shadow levels, there's no need for more than one instance of them, and hence no need for any hook (IOW 452219e24648 ["x86/shadow: monitor table is HVM-only"] didn't go quite far enough). Move the functions to hvm.c while dropping the dead is_pv_32bit_domain() code paths.

Quoted 7 lines

While moving the code, replace a stale comment reference to sh_install_xen_entries_in_l4(). Doing so made me notice the function also didn't have its prototype dropped in 8d7b633adab7 ("x86/mm:
Consolidate all Xen L4 slot writing into init_xen_l4_slots()"), which gets done here as well.

Quoted 10 lines

---
v3: New.
---
TBD: In principle both functions could have their first parameter
constified. In fact, "destroy" doesn't depend on the vCPU at all and hence could be passed a struct domain *. Not sure whether such an asymmetry would be acceptable.
In principle "make" would also not need passing of the number of shadow levels (can be derived from v), which would result in yet another asymmetry.

I'm not specially fuzzed either way - having const v would be good IMO.

Thanks, Roger.

Acked-by on v3 3/3
#7 lore raw

Re: [PATCH v3 3/3] x86/shadow: sh_{make,destroy}_monitor_table() are "even more" HVM-only

In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

With them depending on just the number of shadow levels, there's no need for more than one instance of them, and hence no need for any hook (IOW 452219e24648 ["x86/shadow: monitor table is HVM-only"] didn't go quite far enough). Move the functions to hvm.c while dropping the dead is_pv_32bit_domain() code paths.

Quoted 7 lines

While moving the code, replace a stale comment reference to sh_install_xen_entries_in_l4(). Doing so made me notice the function also didn't have its prototype dropped in 8d7b633adab7 ("x86/mm:
Consolidate all Xen L4 slot writing into init_xen_l4_slots()"), which gets done here as well.

Quoted 9 lines

TBD: In principle both functions could have their first parameter
constified. In fact, "destroy" doesn't depend on the vCPU at all and hence could be passed a struct domain *. Not sure whether such an asymmetry would be acceptable.
In principle "make" would also not need passing of the number of shadow levels (can be derived from v), which would result in yet another asymmetry.
If these asymmetries were acceptable, "make" could then also update v->arch.hvm.monitor_table, instead of doing so at both call sites.

Feel free to add consts, but please don't change the function parameters any more than that. I would rather keep them as a matched pair, and leave the hvm.monitor_table updates in the caller, where it's easier to see why they're not symmetrical.

Cheers

Tim.

#8 lore raw

Ping: [PATCH v3 0/3] x86: shim building adjustments (plus shadow follow-on)

In reply to Jan Beulich

Tim,

Quoted 13 lines · Jan Beulich

The change addressing the shadow related build issue noticed by Andrew went in already. The build breakage goes beyond this specific combination though - PV_SHIM_EXCLUSIVE plus HVM is similarly an issue. This is what the 1st patch tries to take care of, in a shape already on irc noticed to be controversial. I'm submitting the change nevertheless because for the moment there looks to be a majority in favor of going this route. One argument not voiced there yet: What good does it do to allow a user to enable HVM when then on the resulting hypervisor they still can't run HVM guests (for the hypervisor still being a dedicated PV shim one). On top of this, the alternative approach is likely going to get ugly.

Quoted 7 lines

The shadow related adjustments are here merely because the want to make them was noticed in the context of the patch which has already gone in.

1: don't permit HVM and PV_SHIM_EXCLUSIVE at the same time 2: refactor shadow_vram_{get,put}_l1e()
3: sh_{make,destroy}_monitor_table() are "even more" HVM-only

unless you tell me otherwise I'm intending to commit the latter two with Roger's acks some time next week. Of course it would still be nice to know your view on the first of the TBDs in patch 3 (which may result in further changes, in a follow-up).

Jan

#9 lore raw

Re: Ping: [PATCH v3 0/3] x86: shim building adjustments (plus shadow follow-on)

Quoted 6 lines · Jan Beulich

Tim,

unless you tell me otherwise I'm intending to commit the latter two with Roger's acks some time next week. Of course it would still be nice to know your view on the first of the TBDs in patch 3 (which may result in further changes, in a follow-up).

Ack, sorry for the dropped patches, and thank you for the ping. I've now replied to everything that I think is wating for my review.

Cheers,

Tim.

#10 lore raw

PATCH v4 1/1 x86/shim: don't permit HVM and PV_SHIM_EXCLUSIVE at the same time

In reply to Jan Beulich

This combination doesn't really make sense (and there likely are more); in particular even if the code built with both options set, HVM guests wouldn't work (and I think one wouldn't be able to create one in the first place). The alternative here would be some presumably intrusive #ifdef-ary to get this combination to actually build (but still not work) again.

Fixes 8b5b49ceb3d9 ("x86: don't include domctl and alike in shim-exclusive builds")

---
v4: Default HVM to !PV_SHIM, as suggested by Jürgen.
v2: Restore lost default setting.

xen/arch/x86/Kconfig +4 -3
@@ -23,7 +23,7 @@ config X86
 	select HAS_PDX
 	select HAS_SCHED_GRANULARITY
 	select HAS_UBSAN
-	select HAS_VPCI if !PV_SHIM_EXCLUSIVE && HVM
+	select HAS_VPCI if HVM
 	select NEEDS_LIBELF
 	select NUMA
 
@@ -90,9 +90,10 @@ config PV_LINEAR_PT
          If unsure, say Y.
 
 config HVM
-	def_bool !PV_SHIM_EXCLUSIVE
+	bool "HVM support"
+	depends on !PV_SHIM_EXCLUSIVE
+	default !PV_SHIM
 	select IOREQ_SERVER
-	prompt "HVM support"
 	---help---
 	  Interfaces to support HVM domains.  HVM domains require hardware
 	  virtualisation extensions (e.g. Intel VT-x, AMD SVM), but can boot
#11 lore raw

Ping: [PATCH v4] x86/shim: don't permit HVM and PV_SHIM_EXCLUSIVE at the same time

Quoted 10 lines · Jan Beulich

This combination doesn't really make sense (and there likely are more); in particular even if the code built with both options set, HVM guests wouldn't work (and I think one wouldn't be able to create one in the first place). The alternative here would be some presumably intrusive #ifdef-ary to get this combination to actually build (but still not work) again.

Fixes 8b5b49ceb3d9 ("x86: don't include domctl and alike in shim-exclusive builds")

Andrew,

I know you're not happy with this change, but I also don't recall you proposing any sufficiently straightforward alternative. Nor do I recall you responding to my claim that the combination is useless anyway, since the PV shim exists solely to create a single PV guest. Since I have Roger's ack, may I ask that you firmly NACK the change here (accompanied by an alternative proposal), if you're really convinced that your view on the situation is the only sensible one? If I don't hear back by the end of the week, I guess I'll put the change in early next week.

Jan

Quoted 29 lines

---
v4: Default HVM to !PV_SHIM, as suggested by Jürgen.
v2: Restore lost default setting.

xen/arch/x86/Kconfig +4 -3
@@ -23,7 +23,7 @@ config X86
 	select HAS_PDX
 	select HAS_SCHED_GRANULARITY
 	select HAS_UBSAN
-	select HAS_VPCI if !PV_SHIM_EXCLUSIVE && HVM
+	select HAS_VPCI if HVM
 	select NEEDS_LIBELF
 	select NUMA
 
@@ -90,9 +90,10 @@ config PV_LINEAR_PT
          If unsure, say Y.
 
 config HVM
-	def_bool !PV_SHIM_EXCLUSIVE
+	bool "HVM support"
+	depends on !PV_SHIM_EXCLUSIVE
+	default !PV_SHIM
 	select IOREQ_SERVER
-	prompt "HVM support"
 	---help---
 	  Interfaces to support HVM domains.  HVM domains require hardware
 	  virtualisation extensions (e.g. Intel VT-x, AMD SVM), but can boot