Skip to contentSkip to conversation

x86/mm: address aspects noticed during XSA-410 work

PATCH Superseded Jan Beulich 2 participants 24 messages Dec 21, 2022 – Jan 9, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/mm: address aspects noticed during XSA-410 work

Various tidying changes accumulated during that effort. They're all functionally independent afaict, but there may be contextual interactions between some of them.

1: paging: fold HAP and shadow memory alloc related fields
2: paging: fold most HAP and shadow final teardown
3: paging: move update_paging_modes() hook
4: paging: move and conditionalize flush_tlb() hook
5: PV: drop dead paging_update_paging_modes() call during Dom0 construction 6: shadow: adjust and move sh_type_to_size[]
7: shadow: don't open-code copy_domain_page()
8: shadow: drop zero initialization from shadow_domain_init()

Jan

#1 lore raw

PATCH 1/8 x86/paging: fold HAP and shadow memory alloc related fields

Especially with struct shadow_domain and struct hap_domain not living in a union inside struct paging_domain, let's avoid the duplication: The fields are named and used in identical ways, and only one of HAP or shadow can be in use for a domain. This then also renders involved expressions slightly more legible.

---
Quite likely more folding of code is possible with this. For example {hap,shadow}_set_allocation() are now yet more similar than they already were.

xen/arch/x86/include/asm/domain.h +7 -10
@@ -99,12 +99,6 @@ struct shadow_domain {
     unsigned int      opt_flags;    /* runtime tunable optimizations on/off */
     struct page_list_head pinned_shadows;
 
-    /* Memory allocation */
-    struct page_list_head freelist;
-    unsigned int      total_pages;  /* number of pages allocated */
-    unsigned int      free_pages;   /* number of pages on freelists */
-    unsigned int      p2m_pages;    /* number of pages allocated to p2m */
-
     /* 1-to-1 map for use when HVM vcpus have paging disabled */
     pagetable_t unpaged_pagetable;
 
@@ -179,10 +173,6 @@ struct shadow_vcpu {
 /*            hardware assisted paging          */
 /************************************************/
 struct hap_domain {
-    struct page_list_head freelist;
-    unsigned int      total_pages;  /* number of pages allocated */
-    unsigned int      free_pages;   /* number of pages on freelists */
-    unsigned int      p2m_pages;    /* number of pages allocated to p2m */
 };
 
 /************************************************/
@@ -218,6 +208,13 @@ struct paging_domain {
     struct shadow_domain    shadow;
     /* extension for hardware-assited paging */
     struct hap_domain       hap;
+
+    /* Memory allocation (common to shadow and HAP) */
+    struct page_list_head   freelist;
+    unsigned int            total_pages;  /* number of pages allocated */
+    unsigned int            free_pages;   /* number of pages on freelists */
+    unsigned int            p2m_pages;    /* number of pages allocated to p2m */
+
     /* log dirty support */
     struct log_dirty_domain log_dirty;
 
xen/arch/x86/include/asm/hap.h +0 -1
@@ -47,7 +47,6 @@ int   hap_track_dirty_vram(struct domain
 extern const struct paging_mode *hap_paging_get_mode(struct vcpu *);
 int hap_set_allocation(struct domain *d, unsigned int pages, bool *preempted);
 unsigned int hap_get_allocation(struct domain *d);
-int hap_get_allocation_bytes(struct domain *d, uint64_t *size);
 
 #endif /* XEN_HAP_H */
 
xen/arch/x86/include/asm/shadow.h +0 -4
@@ -97,8 +97,6 @@ void shadow_blow_tables_per_domain(struc
 int shadow_set_allocation(struct domain *d, unsigned int pages,
                           bool *preempted);
 
-int shadow_get_allocation_bytes(struct domain *d, uint64_t *size);
-
 #else /* !CONFIG_SHADOW_PAGING */
 
 #define shadow_vcpu_teardown(v) ASSERT(is_pv_vcpu(v))
@@ -110,8 +108,6 @@ int shadow_get_allocation_bytes(struct d
     ({ ASSERT_UNREACHABLE(); -EOPNOTSUPP; })
 #define shadow_set_allocation(d, pages, preempted) \
     ({ ASSERT_UNREACHABLE(); -EOPNOTSUPP; })
-#define shadow_get_allocation_bytes(d, size) \
-    ({ ASSERT_UNREACHABLE(); -EOPNOTSUPP; })
 
 static inline void sh_remove_shadows(struct domain *d, mfn_t gmfn,
                                      int fast, int all) {}
xen/arch/x86/mm/hap/hap.c +29 -42

173 lines not on this page · raw message

xen/arch/x86/mm/paging.c +7 -6
@@ -651,6 +651,7 @@ int paging_domain_init(struct domain *d)
     if ( (rc = p2m_init(d)) != 0 )
         return rc;
 
+    INIT_PAGE_LIST_HEAD(&d->arch.paging.freelist);
     mm_lock_init(&d->arch.paging.lock);
 
     /* This must be initialized separately from the rest of the
@@ -979,17 +980,17 @@ int __init paging_set_allocation(struct
 
 int arch_get_paging_mempool_size(struct domain *d, uint64_t *size)
 {
-    int rc;
+    unsigned long pages;
 
     if ( is_pv_domain(d) )                 /* TODO: Relax in due course */
         return -EOPNOTSUPP;
 
-    if ( hap_enabled(d) )
-        rc = hap_get_allocation_bytes(d, size);
-    else
-        rc = shadow_get_allocation_bytes(d, size);
+    pages = d->arch.paging.total_pages;
+    pages += d->arch.paging.p2m_pages;
 
-    return rc;
+    *size = pages << PAGE_SHIFT;
+
+    return 0;
 }
 
 int arch_set_paging_mempool_size(struct domain *d, uint64_t size)
xen/arch/x86/mm/shadow/common.c +47 -69

302 lines not on this page · raw message

xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -809,7 +809,7 @@ static void cf_check sh_unshadow_for_p2m
      * If there are any shadows, update them.  But if shadow_teardown()
      * has already been called then it's not safe to try.
      */
-    if ( unlikely(!d->arch.paging.shadow.total_pages) )
+    if ( unlikely(!d->arch.paging.total_pages) )
         return;
 
     /* Only previously present / valid entries need processing. */
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] x86/paging: fold HAP and shadow memory alloc related fields

Quoted 7 lines · Jan Beulich

Especially with struct shadow_domain and struct hap_domain not living in a union inside struct paging_domain, let's avoid the duplication: The fields are named and used in identical ways, and only one of HAP or shadow can be in use for a domain. This then also renders involved expressions slightly more legible.

suggestions.

Quoted 4 lines

---
Quite likely more folding of code is possible with this. For example {hap,shadow}_set_allocation() are now yet more similar than they already were.

TBH, I think it wants to go further still.  paging_mempool was intentionally common because all HAP architectures need it, and I really don't want to be playing XSA-410 with every new architecture.

But this is fine to defer until further work.

xen/arch/x86/include/asm/domain.h struct shadow_vcpu { · line 176

Quoted 11 lines
xen/arch/x86/include/asm/domain.h +0 -4
@@ -179,10 +173,6 @@ struct shadow_vcpu {
 /*            hardware assisted paging          */
 /************************************************/
 struct hap_domain {
-    struct page_list_head freelist;
-    unsigned int      total_pages;  /* number of pages allocated */
-    unsigned int      free_pages;   /* number of pages on freelists */
-    unsigned int      p2m_pages;    /* number of pages allocated to p2m */
 };

Do we want to drop hap_domain?  I can't forsee needing to put anything back into it.

Quoted 8 lines
xen/arch/x86/mm/paging.c +1 -1
@@ -979,17 +980,17 @@ int __init paging_set_allocation(struct
 
 int arch_get_paging_mempool_size(struct domain *d, uint64_t *size)
 {
-    int rc;
+    unsigned long pages;

xen/arch/x86/mm/paging.c int __init paging_set_allocation(struct · line 989

Quoted 10 lines
xen/arch/x86/mm/paging.c +2 -4
 
     if ( is_pv_domain(d) )                 /* TODO: Relax in due course */
         return -EOPNOTSUPP;
 
-    if ( hap_enabled(d) )
-        rc = hap_get_allocation_bytes(d, size);
-    else
-        rc = shadow_get_allocation_bytes(d, size);
+    pages = d->arch.paging.total_pages;
+    pages += d->arch.paging.p2m_pages;

Any chance I can talk you into having a second space before the =, so we get:

pages  = d->arch.paging.total_pages;
pages += d->arch.paging.p2m_pages;

nicely aligned vertically?

~Andrew

#3 lore raw

Re: [PATCH 1/8] x86/paging: fold HAP and shadow memory alloc related fields

Quoted 11 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

Especially with struct shadow_domain and struct hap_domain not living in a union inside struct paging_domain, let's avoid the duplication: The fields are named and used in identical ways, and only one of HAP or shadow can be in use for a domain. This then also renders involved expressions slightly more legible.

suggestions.

Thanks.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/include/asm/domain.h +0 -0
@@ -179,10 +173,6 @@ struct shadow_vcpu {
 /*            hardware assisted paging          */
 /************************************************/
 struct hap_domain {
Quoted 8 lines

xen/arch/x86/include/asm/domain.h struct shadow_vcpu { · line 176

Quoted 5 lines
xen/arch/x86/include/asm/domain.h +0 -4
-    struct page_list_head freelist;
-    unsigned int      total_pages;  /* number of pages allocated */
-    unsigned int      free_pages;   /* number of pages on freelists */
-    unsigned int      p2m_pages;    /* number of pages allocated to p2m */
 };

Do we want to drop hap_domain?  I can't forsee needing to put anything back into it.

I would prefer to keep it, even if it's unclear what may want putting there. It's not obvious to me at all that nothing ever will.

Quoted 22 lines
Quoted 8 lines
xen/arch/x86/mm/paging.c +1 -1
@@ -979,17 +980,17 @@ int __init paging_set_allocation(struct
 
 int arch_get_paging_mempool_size(struct domain *d, uint64_t *size)
 {
-    int rc;
+    unsigned long pages;

xen/arch/x86/mm/paging.c int __init paging_set_allocation(struct · line 989

Quoted 10 lines
xen/arch/x86/mm/paging.c +2 -4
 
     if ( is_pv_domain(d) )                 /* TODO: Relax in due course */
         return -EOPNOTSUPP;
 
-    if ( hap_enabled(d) )
-        rc = hap_get_allocation_bytes(d, size);
-    else
-        rc = shadow_get_allocation_bytes(d, size);
+    pages = d->arch.paging.total_pages;
+    pages += d->arch.paging.p2m_pages;

Any chance I can talk you into having a second space before the =, so we get:

Quoted 4 lines

pages  = d->arch.paging.total_pages;
pages += d->arch.paging.p2m_pages;

nicely aligned vertically?

Sure, I can do that. I recall I was actually pondering whether to ...

Jan

#4 lore raw

PATCH 2/8 x86/paging: fold most HAP and shadow final teardown

In reply to Jan Beulich

HAP does a few things beyond what's common, which are left there at least for now. Common operations, however, are moved to paging_final_teardown(), allowing shadow_final_teardown() to go away.

While moving (and hence generalizing) the respective SHADOW_PRINTK() drop the logging of total_pages from the 2nd instance - the value is necessarily zero after {hap,shadow}_set_allocation().

---
The remaining parts of hap_final_teardown() could be moved as well, at the price of a CONFIG_HVM conditional. I wasn't sure whether that was deemed reasonable.

xen/arch/x86/include/asm/shadow.h +0 -3
@@ -78,9 +78,6 @@ int shadow_domctl(struct domain *d,
 void shadow_vcpu_teardown(struct vcpu *v);
 void shadow_teardown(struct domain *d, bool *preempted);
 
-/* Call once all of the references to the domain have gone away */
-void shadow_final_teardown(struct domain *d);
-
 void sh_remove_shadows(struct domain *d, mfn_t gmfn, int fast, int all);
 
 /* Adjust shadows ready for a guest page to change its type. */
xen/arch/x86/mm/hap/hap.c +2 -14
@@ -268,8 +268,8 @@ static void hap_free(struct domain *d, m
 
     /*
      * For dying domains, actually free the memory here. This way less work is
-     * left to hap_final_teardown(), which cannot easily have preemption checks
-     * added.
+     * left to paging_final_teardown(), which cannot easily have preemption
+     * checks added.
      */
     if ( unlikely(d->is_dying) )
     {
@@ -552,18 +552,6 @@ void hap_final_teardown(struct domain *d
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         p2m_teardown(d->arch.nested_p2m[i], true, NULL);
     }
-
-    if ( d->arch.paging.total_pages != 0 )
-        hap_teardown(d, NULL);
-
-    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
-    /* Free any memory that the p2m teardown released */
-    paging_lock(d);
-    hap_set_allocation(d, 0, NULL);
-    ASSERT(d->arch.paging.p2m_pages == 0);
-    ASSERT(d->arch.paging.free_pages == 0);
-    ASSERT(d->arch.paging.total_pages == 0);
-    paging_unlock(d);
 }
 
 void hap_vcpu_teardown(struct vcpu *v)
xen/arch/x86/mm/paging.c +38 -2
@@ -842,10 +842,46 @@ int paging_teardown(struct domain *d)
 /* Call once all of the references to the domain have gone away */
 void paging_final_teardown(struct domain *d)
 {
-    if ( hap_enabled(d) )
+    bool hap = hap_enabled(d);
+
+    PAGING_PRINTK("%pd final teardown starts.  Pages total = %u, free = %u, p2m = %u\n",
+                  d, d->arch.paging.total_pages,
+                  d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+
+    if ( hap )
         hap_final_teardown(d);
+
+    /*
+     * Double-check that the domain didn't have any paging memory.
+     * It is possible for a domain that never got domain_kill()ed
+     * to get here with its paging allocation intact.
+     */
+    if ( d->arch.paging.total_pages )
+    {
+        if ( hap )
+            hap_teardown(d, NULL);
+        else
+            shadow_teardown(d, NULL);
+    }
+
+    /* It is now safe to pull down the p2m map. */
+    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
+
+    /* Free any paging memory that the p2m teardown released. */
+    paging_lock(d);
+
+    if ( hap )
+        hap_set_allocation(d, 0, NULL);
     else
-        shadow_final_teardown(d);
+        shadow_set_allocation(d, 0, NULL);
+
+    PAGING_PRINTK("%pd final teardown done.  Pages free = %u, p2m = %u\n",
+                  d, d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+    ASSERT(!d->arch.paging.p2m_pages);
+    ASSERT(!d->arch.paging.free_pages);
+    ASSERT(!d->arch.paging.total_pages);
+
+    paging_unlock(d);
 
     p2m_final_teardown(d);
 }
xen/arch/x86/mm/shadow/common.c +1 -30
@@ -1232,7 +1232,7 @@ void shadow_free(struct domain *d, mfn_t
 
         /*
          * For dying domains, actually free the memory here. This way less
-         * work is left to shadow_final_teardown(), which cannot easily have
+         * work is left to paging_final_teardown(), which cannot easily have
          * preemption checks added.
          */
         if ( unlikely(dying) )
@@ -2940,35 +2940,6 @@ out:
     }
 }
 
-void shadow_final_teardown(struct domain *d)
-/* Called by arch_domain_destroy(), when it's safe to pull down the p2m map. */
-{
-    SHADOW_PRINTK("dom %u final teardown starts."
-                   "  Shadow pages total = %u, free = %u, p2m=%u\n",
-                   d->domain_id, d->arch.paging.total_pages,
-                   d->arch.paging.free_pages, d->arch.paging.p2m_pages);
-
-    /* Double-check that the domain didn't have any shadow memory.
-     * It is possible for a domain that never got domain_kill()ed
-     * to get here with its shadow allocation intact. */
-    if ( d->arch.paging.total_pages != 0 )
-        shadow_teardown(d, NULL);
-
-    /* It is now safe to pull down the p2m map. */
-    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
-    /* Free any shadow memory that the p2m teardown released */
-    paging_lock(d);
-    shadow_set_allocation(d, 0, NULL);
-    SHADOW_PRINTK("dom %u final teardown done."
-                   "  Shadow pages total = %u, free = %u, p2m=%u\n",
-                   d->domain_id, d->arch.paging.total_pages,
-                   d->arch.paging.free_pages, d->arch.paging.p2m_pages);
-    ASSERT(d->arch.paging.p2m_pages == 0);
-    ASSERT(d->arch.paging.free_pages == 0);
-    ASSERT(d->arch.paging.total_pages == 0);
-    paging_unlock(d);
-}
-
 static int shadow_one_bit_enable(struct domain *d, u32 mode)
 /* Turn on a single shadow mode feature */
 {
#5 lore raw

Re: [PATCH 2/8] x86/paging: fold most HAP and shadow final teardown

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 847

Quoted 10 lines · Jan Beulich
xen/arch/x86/mm/paging.c +3 -1
@@ -842,10 +842,46 @@ int paging_teardown(struct domain *d)
 /* Call once all of the references to the domain have gone away */
 void paging_final_teardown(struct domain *d)
 {
-    if ( hap_enabled(d) )
+    bool hap = hap_enabled(d);
+
+    PAGING_PRINTK("%pd final teardown starts.  Pages total = %u, free = %u, p2m = %u\n",

PAGING_PRINTK() already includes __func__, so just "%pd start: total %u, free %u, p2m %u\n" which is shorter.

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 857

Quoted 10 lines
xen/arch/x86/mm/paging.c +9 -0
+                  d, d->arch.paging.total_pages,
+                  d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+
+    if ( hap )
         hap_final_teardown(d);
+
+    /*
+     * Double-check that the domain didn't have any paging memory.
+     * It is possible for a domain that never got domain_kill()ed
+     * to get here with its paging allocation intact.

I know you're mostly just moving this comment, but it's misleading.

This path is used for the domain_create() error path, and there will be a nonzero allocation for HVM guests.

I think we do want to rework this eventually - we will simplify things massively by splitting the things can can only happen for a domain which has run into relinquish_resources.

At a minimum, I'd suggest dropping the first sentence.  "double check" implies it's an extraordinary case, which isn't warranted here IMO.

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 870

Quoted 13 lines
xen/arch/x86/mm/paging.c +13 -0
+     */
+    if ( d->arch.paging.total_pages )
+    {
+        if ( hap )
+            hap_teardown(d, NULL);
+        else
+            shadow_teardown(d, NULL);
+    }
+
+    /* It is now safe to pull down the p2m map. */
+    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
+
+    /* Free any paging memory that the p2m teardown released. */

I don't think this isn't true any more.  410 also made HAP/shadow free pages fully for a dying domain, so p2m_teardown() at this point won't add to the free memory pool.

I think the subsequent *_set_allocation() can be dropped, and the assertions left.

~Andrew

#6 lore raw

Re: [PATCH 2/8] x86/paging: fold most HAP and shadow final teardown

Quoted 14 lines · Andrew Cooper

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 847

Quoted 10 lines · Jan Beulich
xen/arch/x86/mm/paging.c +3 -1
@@ -842,10 +842,46 @@ int paging_teardown(struct domain *d)
 /* Call once all of the references to the domain have gone away */
 void paging_final_teardown(struct domain *d)
 {
-    if ( hap_enabled(d) )
+    bool hap = hap_enabled(d);
+
+    PAGING_PRINTK("%pd final teardown starts.  Pages total = %u, free = %u, p2m = %u\n",

PAGING_PRINTK() already includes __func__, so just "%pd start: total %u, free %u, p2m %u\n" which is shorter.

Hmm, yes, can do.

Quoted 16 lines

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 857

Quoted 10 lines
xen/arch/x86/mm/paging.c +9 -0
+                  d, d->arch.paging.total_pages,
+                  d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+
+    if ( hap )
         hap_final_teardown(d);
+
+    /*
+     * Double-check that the domain didn't have any paging memory.
+     * It is possible for a domain that never got domain_kill()ed
+     * to get here with its paging allocation intact.

I know you're mostly just moving this comment, but it's misleading.

This path is used for the domain_create() error path, and there will be a nonzero allocation for HVM guests.

Quoted 6 lines

I think we do want to rework this eventually - we will simplify things massively by splitting the things can can only happen for a domain which has run into relinquish_resources.

At a minimum, I'd suggest dropping the first sentence.  "double check" implies it's an extraordinary case, which isn't warranted here IMO.

Instead of dropping I think I would prefer to make it start "Make sure ...".

Quoted 18 lines

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 870

Quoted 13 lines
xen/arch/x86/mm/paging.c +13 -0
+     */
+    if ( d->arch.paging.total_pages )
+    {
+        if ( hap )
+            hap_teardown(d, NULL);
+        else
+            shadow_teardown(d, NULL);
+    }
+
+    /* It is now safe to pull down the p2m map. */
+    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
+
+    /* Free any paging memory that the p2m teardown released. */

I don't think this isn't true any more.  410 also made HAP/shadow free pages fully for a dying domain, so p2m_teardown() at this point won't add to the free memory pool.

Quoted 2 lines

I think the subsequent *_set_allocation() can be dropped, and the assertions left.

I agree, but if anything this will want to be a separate patch then.

Jan

#7 lore raw

Re: [PATCH 2/8] x86/paging: fold most HAP and shadow final teardown

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 857

Quoted 10 lines · Jan Beulich
xen/arch/x86/mm/paging.c +9 -0
+                  d, d->arch.paging.total_pages,
+                  d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+
+    if ( hap )
         hap_final_teardown(d);
+
+    /*
+     * Double-check that the domain didn't have any paging memory.
+     * It is possible for a domain that never got domain_kill()ed
+     * to get here with its paging allocation intact.

I know you're mostly just moving this comment, but it's misleading.

This path is used for the domain_create() error path, and there will be a nonzero allocation for HVM guests.

Quoted 8 lines
Quoted 6 lines

I think we do want to rework this eventually - we will simplify things massively by splitting the things can can only happen for a domain which has run into relinquish_resources.

At a minimum, I'd suggest dropping the first sentence.  "double check" implies it's an extraordinary case, which isn't warranted here IMO.

Instead of dropping I think I would prefer to make it start "Make sure ...".

That's still awkward grammar, and a bit misleading IMO.  How about rewriting it entirely?

/* Remove remaining paging memory.  This can be nonzero on certain error paths. */

Quoted 18 lines
Quoted 18 lines
Quoted 4 lines
xen/arch/x86/mm/paging.c +3 -0
+     */
+    if ( d->arch.paging.total_pages )
+    {

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 870

Quoted 10 lines
xen/arch/x86/mm/paging.c +10 -0
+        if ( hap )
+            hap_teardown(d, NULL);
+        else
+            shadow_teardown(d, NULL);
+    }
+
+    /* It is now safe to pull down the p2m map. */
+    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
+
+    /* Free any paging memory that the p2m teardown released. */

I don't think this isn't true any more.  410 also made HAP/shadow free pages fully for a dying domain, so p2m_teardown() at this point won't add to the free memory pool.

Quoted 3 lines
Quoted 2 lines

I think the subsequent *_set_allocation() can be dropped, and the assertions left.

I agree, but if anything this will want to be a separate patch then.

I'd be happy putting that in this patch, but if you don't want to combine it, then it should be a prerequisite IMO, so we don't have a "clean up $X" patch which is shuffling buggy code around.

~Andrew

#8 lore raw

Re: [PATCH 2/8] x86/paging: fold most HAP and shadow final teardown

Quoted 24 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 857

Quoted 10 lines · Jan Beulich
xen/arch/x86/mm/paging.c +9 -0
+                  d, d->arch.paging.total_pages,
+                  d->arch.paging.free_pages, d->arch.paging.p2m_pages);
+
+    if ( hap )
         hap_final_teardown(d);
+
+    /*
+     * Double-check that the domain didn't have any paging memory.
+     * It is possible for a domain that never got domain_kill()ed
+     * to get here with its paging allocation intact.

I know you're mostly just moving this comment, but it's misleading.

This path is used for the domain_create() error path, and there will be a nonzero allocation for HVM guests.

I think we do want to rework this eventually - we will simplify things massively by splitting the things can can only happen for a domain which has run into relinquish_resources.

At a minimum, I'd suggest dropping the first sentence.  "double check" implies it's an extraordinary case, which isn't warranted here IMO.

Quoted 8 lines
Quoted 2 lines

Instead of dropping I think I would prefer to make it start "Make sure ...".

That's still awkward grammar, and a bit misleading IMO.  How about rewriting it entirely?

/* Remove remaining paging memory.  This can be nonzero on certain error paths. */

Fine with me, changed.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

xen/arch/x86/mm/paging.c int paging_teardown(struct domain *d) · line 870

Quoted 13 lines
xen/arch/x86/mm/paging.c +13 -0
+     */
+    if ( d->arch.paging.total_pages )
+    {
+        if ( hap )
+            hap_teardown(d, NULL);
+        else
+            shadow_teardown(d, NULL);
+    }
+
+    /* It is now safe to pull down the p2m map. */
+    p2m_teardown(p2m_get_hostp2m(d), true, NULL);
+
+    /* Free any paging memory that the p2m teardown released. */

I don't think this isn't true any more.  410 also made HAP/shadow free pages fully for a dying domain, so p2m_teardown() at this point won't add to the free memory pool.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

I think the subsequent *_set_allocation() can be dropped, and the assertions left.

I agree, but if anything this will want to be a separate patch then.

I'd be happy putting that in this patch, but if you don't want to combine it, then it should be a prerequisite IMO, so we don't have a "clean up $X" patch which is shuffling buggy code around.

Well, doing it the other way around (as I already had it before your reply) also has its advantages. Are you feeling very strong about the order?

Jan

#9 lore raw

PATCH 3/8 x86/paging: move update_paging_modes() hook

In reply to Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

While there rename the hook and HAP's as well as shadow's hook functions to use singular; I never understood why plural was used. (Renaming in particular the wrapper would be touching quite a lot of other code.)

xen/arch/x86/include/asm/domain.h +2 -0
@@ -235,6 +235,8 @@ struct paging_domain {
      * (used by p2m and log-dirty code for their tries) */
     struct page_info * (*alloc_page)(struct domain *d);
     void (*free_page)(struct domain *d, struct page_info *pg);
+
+    void (*update_paging_mode)(struct vcpu *v);
 };
 
 struct paging_vcpu {
xen/arch/x86/include/asm/paging.h +1 -2
@@ -140,7 +140,6 @@ struct paging_mode {
 #endif
     void          (*update_cr3            )(struct vcpu *v, int do_locking,
                                             bool noflush);
-    void          (*update_paging_modes   )(struct vcpu *v);
     bool          (*flush_tlb             )(const unsigned long *vcpu_bitmap);
 
     unsigned int guest_levels;
@@ -316,7 +315,7 @@ static inline void paging_update_cr3(str
  * has changed, and when bringing up a VCPU for the first time. */
 static inline void paging_update_paging_modes(struct vcpu *v)
 {
-    paging_get_hostmode(v)->update_paging_modes(v);
+    v->domain->arch.paging.update_paging_mode(v);
 }
 
 #ifdef CONFIG_PV
xen/arch/x86/mm/hap/hap.c +6 -5
@@ -443,6 +443,9 @@ static void hap_destroy_monitor_table(st
 /************************************************/
 /*          HAP DOMAIN LEVEL FUNCTIONS          */
 /************************************************/
+
+static void cf_check hap_update_paging_mode(struct vcpu *v);
+
 void hap_domain_init(struct domain *d)
 {
     static const struct log_dirty_ops hap_ops = {
@@ -453,6 +456,8 @@ void hap_domain_init(struct domain *d)
 
     /* Use HAP logdirty mechanism. */
     paging_log_dirty_init(d, &hap_ops);
+
+    d->arch.paging.update_paging_mode = hap_update_paging_mode;
 }
 
 /* return 0 for success, -errno for failure */
@@ -772,7 +777,7 @@ hap_paging_get_mode(struct vcpu *v)
                                       &hap_paging_protected_mode);
 }
 
-static void cf_check hap_update_paging_modes(struct vcpu *v)
+static void cf_check hap_update_paging_mode(struct vcpu *v)
 {
     struct domain *d = v->domain;
     unsigned long cr3_gfn = v->arch.hvm.guest_cr[3] >> PAGE_SHIFT;
@@ -842,7 +847,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_real_mode,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_real_mode,
     .update_cr3             = hap_update_cr3,
-    .update_paging_modes    = hap_update_paging_modes,
     .flush_tlb              = flush_tlb,
     .guest_levels           = 1
 };
@@ -853,7 +857,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_2_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_2_levels,
     .update_cr3             = hap_update_cr3,
-    .update_paging_modes    = hap_update_paging_modes,
     .flush_tlb              = flush_tlb,
     .guest_levels           = 2
 };
@@ -864,7 +867,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_3_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_3_levels,
     .update_cr3             = hap_update_cr3,
-    .update_paging_modes    = hap_update_paging_modes,
     .flush_tlb              = flush_tlb,
     .guest_levels           = 3
 };
@@ -875,7 +877,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_4_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_4_levels,
     .update_cr3             = hap_update_cr3,
-    .update_paging_modes    = hap_update_paging_modes,
     .flush_tlb              = flush_tlb,
     .guest_levels           = 4
 };
xen/arch/x86/mm/shadow/common.c +10 -1
@@ -45,6 +45,8 @@ static int cf_check sh_enable_log_dirty(
 static int cf_check sh_disable_log_dirty(struct domain *);
 static void cf_check sh_clean_dirty_bitmap(struct domain *);
 
+static void cf_check shadow_update_paging_mode(struct vcpu *);
+
 /* Set up the shadow-specific parts of a domain struct at start of day.
  * Called for every domain from arch_domain_create() */
 int shadow_domain_init(struct domain *d)
@@ -60,6 +62,8 @@ int shadow_domain_init(struct domain *d)
     /* Use shadow pagetables for log-dirty support */
     paging_log_dirty_init(d, &sh_ops);
 
+    d->arch.paging.update_paging_mode = shadow_update_paging_mode;
+
 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
     d->arch.paging.shadow.oos_active = 0;
 #endif
@@ -2558,7 +2562,12 @@ static void sh_update_paging_modes(struc
     v->arch.paging.mode->update_cr3(v, 0, false);
 }
 
-void cf_check shadow_update_paging_modes(struct vcpu *v)
+/*
+ * Update all the things that are derived from the guest's CR0/CR3/CR4.
+ * Called to initialize paging structures if the paging mode has changed,
+ * and when bringing up a VCPU for the first time.
+ */
+static void cf_check shadow_update_paging_mode(struct vcpu *v)
 {
     paging_lock(v->domain);
     sh_update_paging_modes(v);
xen/arch/x86/mm/shadow/multi.c +0 -1
@@ -4198,7 +4198,6 @@ const struct paging_mode sh_paging_mode
     .gva_to_gfn                    = sh_gva_to_gfn,
 #endif
     .update_cr3                    = sh_update_cr3,
-    .update_paging_modes           = shadow_update_paging_modes,
     .flush_tlb                     = shadow_flush_tlb,
     .guest_levels                  = GUEST_PAGING_LEVELS,
     .shadow.detach_old_tables      = sh_detach_old_tables,
xen/arch/x86/mm/shadow/none.c +8 -6
@@ -18,6 +18,11 @@ static void cf_check _clean_dirty_bitmap
     ASSERT(is_pv_domain(d));
 }
 
+static void cf_check _update_paging_mode(struct vcpu *v)
+{
+    ASSERT_UNREACHABLE();
+}
+
 int shadow_domain_init(struct domain *d)
 {
     static const struct log_dirty_ops sh_none_ops = {
@@ -27,6 +32,9 @@ int shadow_domain_init(struct domain *d)
     };
 
     paging_log_dirty_init(d, &sh_none_ops);
+
+    d->arch.paging.update_paging_mode = _update_paging_mode;
+
     return is_hvm_domain(d) ? -EOPNOTSUPP : 0;
 }
 
@@ -57,11 +65,6 @@ static void cf_check _update_cr3(struct
     ASSERT_UNREACHABLE();
 }
 
-static void cf_check _update_paging_modes(struct vcpu *v)
-{
-    ASSERT_UNREACHABLE();
-}
-
 static const struct paging_mode sh_paging_none = {
     .page_fault                    = _page_fault,
     .invlpg                        = _invlpg,
@@ -69,7 +72,6 @@ static const struct paging_mode sh_pagin
     .gva_to_gfn                    = _gva_to_gfn,
 #endif
     .update_cr3                    = _update_cr3,
-    .update_paging_modes           = _update_paging_modes,
 };
 
 void shadow_vcpu_init(struct vcpu *v)
xen/arch/x86/mm/shadow/private.h +0 -5
@@ -426,11 +426,6 @@ void cf_check sh_write_guest_entry(
 intpte_t cf_check sh_cmpxchg_guest_entry(
     struct vcpu *v, intpte_t *p, intpte_t old, intpte_t new, mfn_t gmfn);
 
-/* Update all the things that are derived from the guest's CR0/CR3/CR4.
- * Called to initialize paging structures if the paging mode
- * has changed, and when bringing up a VCPU for the first time. */
-void cf_check shadow_update_paging_modes(struct vcpu *v);
-
 /* Unhook the non-Xen mappings in this top-level shadow mfn.
  * With user_only == 1, unhooks only the user-mode mappings. */
 void shadow_unhook_mappings(struct domain *d, mfn_t smfn, int user_only);
#10 lore raw

Re: [PATCH 3/8] x86/paging: move update_paging_modes() hook

Quoted 8 lines · Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

While there rename the hook and HAP's as well as shadow's hook functions to use singular; I never understood why plural was used. (Renaming in particular the wrapper would be touching quite a lot of other code.)

There are always two modes; Xen's, and the guest's.

This was probably more visible back in the 32-bit days, but remnants of it are still around with the fact that struct vcpu needs to be below the 4G boundary for the PDPTRs for when the guest isn't in Long Mode.

HAP also hides it fairly well given the uniformity of EPT/NPT (and always 4 levels in a 64-bit Xen), but I suspect it will become more visible again when we start supporting LA57.

All of that said, I have debated the utility of this hook.  It mixes up various concepts including the singleton construction of monitor tables,  and TLB flushing (which should not happen for a mov cr3 with NOFLUSH set), and with those concepts abstracted properly, the only remaining action is caching the right ops pointer.

I'm not convinced it will survive a concerned attempt to fix the HVM p2m vs guest tlb flushing confusion.

xen/arch/x86/mm/shadow/none.c int shadow_domain_init(struct domain *d) · line 38

Quoted 10 lines
xen/arch/x86/mm/shadow/none.c +3 -0
@@ -27,6 +32,9 @@ int shadow_domain_init(struct domain *d)
     };
 
     paging_log_dirty_init(d, &sh_none_ops);
+
+    d->arch.paging.update_paging_mode = _update_paging_mode;
+
     return is_hvm_domain(d) ? -EOPNOTSUPP : 0;

I know you haven't changed the logic here, but this hook looks broken. Why do we fail it right at the end for HVM domains?

~Andrew

#11 lore raw

Re: [PATCH 3/8] x86/paging: move update_paging_modes() hook

Quoted 12 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

While there rename the hook and HAP's as well as shadow's hook functions to use singular; I never understood why plural was used. (Renaming in particular the wrapper would be touching quite a lot of other code.)

There are always two modes; Xen's, and the guest's.

Quoted 7 lines

This was probably more visible back in the 32-bit days, but remnants of it are still around with the fact that struct vcpu needs to be below the 4G boundary for the PDPTRs for when the guest isn't in Long Mode.

HAP also hides it fairly well given the uniformity of EPT/NPT (and always 4 levels in a 64-bit Xen), but I suspect it will become more visible again when we start supporting LA57.

So does this boil down to a request to undo the rename? Or undo it just for shadow code (as the HAP function really does only one thing)? As to LA57, I'm not convinced it'll become more visible again then, but of course without actually doing that work it's all hand-waving anyway.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm/shadow/none.c +0 -0
@@ -27,6 +32,9 @@ int shadow_domain_init(struct domain *d)
     };
 
Quoted 8 lines

xen/arch/x86/mm/shadow/none.c int shadow_domain_init(struct domain *d) · line 38

Quoted 5 lines
xen/arch/x86/mm/shadow/none.c +3 -0
     paging_log_dirty_init(d, &sh_none_ops);
+
+    d->arch.paging.update_paging_mode = _update_paging_mode;
+
     return is_hvm_domain(d) ? -EOPNOTSUPP : 0;

I know you haven't changed the logic here, but this hook looks broken. Why do we fail it right at the end for HVM domains?

It's been a long time, but I guess my thinking back then was that it's better to put in place pointers which other code may rely on being non- NULL.

Jan

#12 lore raw

Re: [PATCH 3/8] x86/paging: move update_paging_modes() hook

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
Quoted 8 lines · Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

While there rename the hook and HAP's as well as shadow's hook functions to use singular; I never understood why plural was used. (Renaming in particular the wrapper would be touching quite a lot of other code.)

There are always two modes; Xen's, and the guest's.

This was probably more visible back in the 32-bit days, but remnants of it are still around with the fact that struct vcpu needs to be below the 4G boundary for the PDPTRs for when the guest isn't in Long Mode.

Quoted 7 lines
Quoted 3 lines

HAP also hides it fairly well given the uniformity of EPT/NPT (and always 4 levels in a 64-bit Xen), but I suspect it will become more visible again when we start supporting LA57.

So does this boil down to a request to undo the rename? Or undo it just for shadow code (as the HAP function really does only one thing)? As to LA57, I'm not convinced it'll become more visible again then, but of course without actually doing that work it's all hand-waving anyway.

With LA57, HAP really does become 2 things.  Using a 5-level walk at the HAP level is a prerequisite for being able to VMEntry with CR4.LA57 set, on both Intel and AMD hardware IIRC.

Then, for VMs which don't elect to enable LA57, we will be in a 4-on-5 (to borrow the shadow terminology) situation.

The comment by paging_update_paging_modes() is fairly clear about this operation being strictly related to guest state, which further demonstrates that hap version playing with the monitor table is wrong.

Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/shadow/none.c +0 -0
@@ -27,6 +32,9 @@ int shadow_domain_init(struct domain *d)
     };
 
Quoted 7 lines

xen/arch/x86/mm/shadow/none.c int shadow_domain_init(struct domain *d) · line 38

Quoted 5 lines
xen/arch/x86/mm/shadow/none.c +3 -0
     paging_log_dirty_init(d, &sh_none_ops);
+
+    d->arch.paging.update_paging_mode = _update_paging_mode;
+
     return is_hvm_domain(d) ? -EOPNOTSUPP : 0;

I know you haven't changed the logic here, but this hook looks broken. Why do we fail it right at the end for HVM domains?

Quoted 3 lines

It's been a long time, but I guess my thinking back then was that it's better to put in place pointers which other code may rely on being non- NULL.

Any chance we could gain a /* set up pointers for safety, then fail */ then ?

~Andrew

#13 lore raw

PATCH 4/8 x86/paging: move and conditionalize flush_tlb() hook

In reply to Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

The hook also is used for HVM guests only, so make respective pieces conditional upon CONFIG_HVM.

While there also add __must_check to the hook declaration, as it's imperative that callers deal with getting back "false".

While moving the shadow implementation, introduce a "curr" local variable.

xen/arch/x86/include/asm/domain.h +5 -0
@@ -237,6 +237,11 @@ struct paging_domain {
     void (*free_page)(struct domain *d, struct page_info *pg);
 
     void (*update_paging_mode)(struct vcpu *v);
+
+#ifdef CONFIG_HVM
+    /* Flush selected vCPUs TLBs.  NULL for all. */
+    bool __must_check (*flush_tlb)(const unsigned long *vcpu_bitmap);
+#endif
 };
 
 struct paging_vcpu {
xen/arch/x86/include/asm/paging.h +6 -7
@@ -140,7 +140,6 @@ struct paging_mode {
 #endif
     void          (*update_cr3            )(struct vcpu *v, int do_locking,
                                             bool noflush);
-    bool          (*flush_tlb             )(const unsigned long *vcpu_bitmap);
 
     unsigned int guest_levels;
 
@@ -300,6 +299,12 @@ static inline unsigned long paging_ga_to
         page_order);
 }
 
+/* Flush selected vCPUs TLBs.  NULL for all. */
+static inline bool paging_flush_tlb(const unsigned long *vcpu_bitmap)
+{
+    return current->domain->arch.paging.flush_tlb(vcpu_bitmap);
+}
+
 #endif /* CONFIG_HVM */
 
 /* Update all the things that are derived from the guest's CR3.
@@ -408,12 +413,6 @@ static always_inline unsigned int paging
     return bits;
 }
 
-/* Flush selected vCPUs TLBs.  NULL for all. */
-static inline bool paging_flush_tlb(const unsigned long *vcpu_bitmap)
-{
-    return paging_get_hostmode(current)->flush_tlb(vcpu_bitmap);
-}
-
 #endif /* XEN_PAGING_H */
 
 /*
xen/arch/x86/mm/hap/hap.c +2 -4
@@ -445,6 +445,7 @@ static void hap_destroy_monitor_table(st
 /************************************************/
 
 static void cf_check hap_update_paging_mode(struct vcpu *v);
+static bool cf_check flush_tlb(const unsigned long *vcpu_bitmap);
 
 void hap_domain_init(struct domain *d)
 {
@@ -458,6 +459,7 @@ void hap_domain_init(struct domain *d)
     paging_log_dirty_init(d, &hap_ops);
 
     d->arch.paging.update_paging_mode = hap_update_paging_mode;
+    d->arch.paging.flush_tlb          = flush_tlb;
 }
 
 /* return 0 for success, -errno for failure */
@@ -847,7 +849,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_real_mode,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_real_mode,
     .update_cr3             = hap_update_cr3,
-    .flush_tlb              = flush_tlb,
     .guest_levels           = 1
 };
 
@@ -857,7 +858,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_2_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_2_levels,
     .update_cr3             = hap_update_cr3,
-    .flush_tlb              = flush_tlb,
     .guest_levels           = 2
 };
 
@@ -867,7 +867,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_3_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_3_levels,
     .update_cr3             = hap_update_cr3,
-    .flush_tlb              = flush_tlb,
     .guest_levels           = 3
 };
 
@@ -877,7 +876,6 @@ static const struct paging_mode hap_pagi
     .gva_to_gfn             = hap_gva_to_gfn_4_levels,
     .p2m_ga_to_gfn          = hap_p2m_ga_to_gfn_4_levels,
     .update_cr3             = hap_update_cr3,
-    .flush_tlb              = flush_tlb,
     .guest_levels           = 4
 };
 
xen/arch/x86/mm/shadow/common.c +1 -60

73 lines not on this page · raw message

xen/arch/x86/mm/shadow/hvm.c +60 -0

66 lines not on this page · raw message

xen/arch/x86/mm/shadow/multi.c +0 -1
@@ -4198,7 +4198,6 @@ const struct paging_mode sh_paging_mode
     .gva_to_gfn                    = sh_gva_to_gfn,
 #endif
     .update_cr3                    = sh_update_cr3,
-    .flush_tlb                     = shadow_flush_tlb,
     .guest_levels                  = GUEST_PAGING_LEVELS,
     .shadow.detach_old_tables      = sh_detach_old_tables,
 #ifdef CONFIG_PV
Reviewed-by on v1 4/8
#14 lore raw

Re: [PATCH 4/8] x86/paging: move and conditionalize flush_tlb() hook

Quoted 4 lines · Jan Beulich

The hook isn't mode dependent, hence it's misplaced in struct paging_mode. (Or alternatively I see no reason why the alloc_page() and free_page() hooks don't also live there.) Move it to struct paging_domain.

How you flush the TLBs has absolutely nothing to do with what mode the guest is in.

But this hook too confuses p2m flushes with vcpu flushes.

Quoted 10 lines

The hook also is used for HVM guests only, so make respective pieces conditional upon CONFIG_HVM.

While there also add __must_check to the hook declaration, as it's imperative that callers deal with getting back "false".

While moving the shadow implementation, introduce a "curr" local variable.

observations.

xen/arch/x86/include/asm/paging.h static inline unsigned long paging_ga_to · line 305

Quoted 10 lines
xen/arch/x86/include/asm/paging.h +4 -0
@@ -300,6 +299,12 @@ static inline unsigned long paging_ga_to
         page_order);
 }
 
+/* Flush selected vCPUs TLBs.  NULL for all. */
+static inline bool paging_flush_tlb(const unsigned long *vcpu_bitmap)
+{
+    return current->domain->arch.paging.flush_tlb(vcpu_bitmap);

Not for this patch, but for cases like this, we should probably drop the function pointer.

There are only two options, and they're invariant for the context, so

if ( hap )
    hap_flush_tlb(...);
else
    shadow_flush_tlb(...);

will almost certainly be faster on any CPU that Xen is liable to run on.  Especially as HAP is probably ~100% common case.

Quoted 5 lines
xen/arch/x86/mm/shadow/hvm.c +0 -0
@@ -688,6 +688,66 @@ static void sh_emulate_unmap_dest(struct
     atomic_inc(&v->domain->arch.paging.shadow.gtable_dirty_version);
 }

xen/arch/x86/mm/shadow/hvm.c static void sh_emulate_unmap_dest(struct · line 699

Quoted 10 lines
xen/arch/x86/mm/shadow/hvm.c +9 -0
 
+static bool flush_vcpu(const struct vcpu *v, const unsigned long *vcpu_bitmap)
+{
+    return !vcpu_bitmap || test_bit(v->vcpu_id, vcpu_bitmap);
+}
+
+/* Flush TLB of selected vCPUs.  NULL for all. */
+bool cf_check shadow_flush_tlb(const unsigned long *vcpu_bitmap)
+{
+    static DEFINE_PER_CPU(cpumask_t, flush_cpumask);

The hap and shadow variants both have a static percpu mask like this.

However, this is an irqs-on region with no nested locking, so I suspect this path can share one of the scheduler percpu variables too.

~Andrew

#15 lore raw

PATCH 5/8 x86/PV: drop dead paging_update_paging_modes() call during Dom0 construction

In reply to Jan Beulich

The function won't ever be invoked, as paging_mode_enabled() always returns false here due to the immediately preceding clearing of d->arch.paging.mode. While compilers recognize this and eliminate the call, make this explicit in the source (which likely 9a28170f2da2 ["pvh
dom0: construct_dom0 changes"] should have done right away, albeit even before that the call looks to have been pointless - shadow mode enabling has occurred later virtually forever).

While there also update an adjacent partly stale comment.

xen/arch/x86/pv/dom0_build.c +2 -6
@@ -10,7 +10,6 @@
 #include <xen/init.h>
 #include <xen/libelf.h>
 #include <xen/multiboot.h>
-#include <xen/paging.h>
 #include <xen/pfn.h>
 #include <xen/sched.h>
 #include <xen/softirq.h>
@@ -804,11 +803,8 @@ int __init dom0_construct_pv(struct doma
 
     d->arch.paging.mode = 0;
 
-    /* Set up CR3 value for write_ptbase */
-    if ( paging_mode_enabled(d) )
-        paging_update_paging_modes(v);
-    else
-        update_cr3(v);
+    /* Set up CR3 value for switch_cr3_cr4(). */
+    update_cr3(v);
 
     /* We run on dom0's page tables for the final part of the build process. */
     switch_cr3_cr4(cr3_pa(v->arch.cr3), read_cr4());
Reviewed-by on v1 5/8
#16 lore raw

Re: [PATCH 5/8] x86/PV: drop dead paging_update_paging_modes() call during Dom0 construction

Quoted 11 lines · Jan Beulich

The function won't ever be invoked, as paging_mode_enabled() always returns false here due to the immediately preceding clearing of d->arch.paging.mode. While compilers recognize this and eliminate the call, make this explicit in the source (which likely 9a28170f2da2 ["pvh
dom0: construct_dom0 changes"] should have done right away, albeit even before that the call looks to have been pointless - shadow mode enabling has occurred later virtually forever).

While there also update an adjacent partly stale comment.

#17 lore raw

PATCH 6/8 x86/shadow: adjust and move sh_type_to_size[]

In reply to Jan Beulich

Drop the SH_type_none entry - there are no allocation attempts with this type, and there also shouldn't be any. Adjust the shadow_size() alternative path to match that change. Also generalize two related assertions.

While there move the entire table and the respective part of the comment there to hvm.c, resulting in one less #ifdef. In the course of the movement switch to using designated initializers.

---
In principle the SH_type_l2h_64_shadow entry could be dropped here as well (for being a PV32-only thing), but I wasn't sure whether that would be deemed okay right here.

xen/arch/x86/mm/shadow/common.c +2 -35
@@ -838,44 +838,11 @@ sh_validate_guest_entry(struct vcpu *v,
  * not contiguous in memory; functions for handling offsets into them are
  * defined in shadow/multi.c (shadow_l1_index() etc.)
  *
- * This table shows the allocation behaviour of the different modes:
- *
- * Xen paging      64b  64b  64b
- * Guest paging    32b  pae  64b
- * PV or HVM       HVM  HVM   *
- * Shadow paging   pae  pae  64b
- *
- * sl1 size         8k   4k   4k
- * sl2 size        16k   4k   4k
- * sl3 size         -    -    4k
- * sl4 size         -    -    4k
- *
  * In HVM guests, the p2m table is built out of shadow pages, and we provide
  * a function for the p2m management to steal pages, in max-order chunks, from
  * the free pool.
  */
 
-#ifdef CONFIG_HVM
-const u8 sh_type_to_size[] = {
-    1, /* SH_type_none           */
-    2, /* SH_type_l1_32_shadow   */
-    2, /* SH_type_fl1_32_shadow  */
-    4, /* SH_type_l2_32_shadow   */
-    1, /* SH_type_l1_pae_shadow  */
-    1, /* SH_type_fl1_pae_shadow */
-    1, /* SH_type_l2_pae_shadow  */
-    1, /* SH_type_l1_64_shadow   */
-    1, /* SH_type_fl1_64_shadow  */
-    1, /* SH_type_l2_64_shadow   */
-    1, /* SH_type_l2h_64_shadow  */
-    1, /* SH_type_l3_64_shadow   */
-    1, /* SH_type_l4_64_shadow   */
-    1, /* SH_type_p2m_table      */
-    1, /* SH_type_monitor_table  */
-    1  /* SH_type_oos_snapshot   */
-};
-#endif
-
 /*
  * Figure out the least acceptable quantity of shadow memory.
  * The minimum memory requirement for always being able to free up a
@@ -1121,7 +1088,7 @@ mfn_t shadow_alloc(struct domain *d,
     unsigned int i;
 
     ASSERT(paging_locked_by_me(d));
-    ASSERT(shadow_type != SH_type_none);
+    ASSERT(pages);
     perfc_incr(shadow_alloc);
 
     if ( d->arch.paging.free_pages < pages )
@@ -1201,9 +1168,9 @@ void shadow_free(struct domain *d, mfn_t
     perfc_incr(shadow_free);
 
     shadow_type = sp->u.sh.type;
-    ASSERT(shadow_type != SH_type_none);
     ASSERT(sp->u.sh.head || (shadow_type > SH_type_max_shadow));
     pages = shadow_size(shadow_type);
+    ASSERT(pages);
     pin_list = &d->arch.paging.shadow.pinned_shadows;
 
     for ( i = 0; i < pages; i++ )
xen/arch/x86/mm/shadow/hvm.c +31 -0
@@ -33,6 +33,37 @@
 
 #include "private.h"
 
+/*
+ * This table shows the allocation behaviour of the different modes:
+ *
+ * Xen paging      64b  64b  64b
+ * Guest paging    32b  pae  64b
+ * PV or HVM       HVM  HVM   *
+ * Shadow paging   pae  pae  64b
+ *
+ * sl1 size         8k   4k   4k
+ * sl2 size        16k   4k   4k
+ * sl3 size         -    -    4k
+ * sl4 size         -    -    4k
+ */
+const uint8_t sh_type_to_size[] = {
+    [SH_type_l1_32_shadow]   = 2,
+    [SH_type_fl1_32_shadow]  = 2,
+    [SH_type_l2_32_shadow]   = 4,
+    [SH_type_l1_pae_shadow]  = 1,
+    [SH_type_fl1_pae_shadow] = 1,
+    [SH_type_l2_pae_shadow]  = 1,
+    [SH_type_l1_64_shadow]   = 1,
+    [SH_type_fl1_64_shadow]  = 1,
+    [SH_type_l2_64_shadow]   = 1,
+    [SH_type_l2h_64_shadow]  = 1,
+    [SH_type_l3_64_shadow]   = 1,
+    [SH_type_l4_64_shadow]   = 1,
+    [SH_type_p2m_table]      = 1,
+    [SH_type_monitor_table]  = 1,
+    [SH_type_oos_snapshot]   = 1,
+};
+
 /**************************************************************************/
 /* x86 emulator support for the shadow code
  */
xen/arch/x86/mm/shadow/private.h +1 -1
@@ -363,7 +363,7 @@ shadow_size(unsigned int shadow_type)
     return sh_type_to_size[shadow_type];
 #else
     ASSERT(shadow_type < SH_type_unused);
-    return 1;
+    return shadow_type != SH_type_none;
 #endif
 }
 
Reviewed-by on v1 6/8
#18 lore raw

Re: [PATCH 6/8] x86/shadow: adjust and move sh_type_to_size[]

Quoted 10 lines · Jan Beulich

Drop the SH_type_none entry - there are no allocation attempts with this type, and there also shouldn't be any. Adjust the shadow_size() alternative path to match that change. Also generalize two related assertions.

While there move the entire table and the respective part of the comment there to hvm.c, resulting in one less #ifdef. In the course of the movement switch to using designated initializers.

Quoted 25 lines
xen/arch/x86/mm/shadow/hvm.c +19 -0
@@ -33,6 +33,37 @@
 
 #include "private.h"
 
+/*
+ * This table shows the allocation behaviour of the different modes:
+ *
+ * Xen paging      64b  64b  64b
+ * Guest paging    32b  pae  64b
+ * PV or HVM       HVM  HVM   *
+ * Shadow paging   pae  pae  64b
+ *
+ * sl1 size         8k   4k   4k
+ * sl2 size        16k   4k   4k
+ * sl3 size         -    -    4k
+ * sl4 size         -    -    4k
+ */
+const uint8_t sh_type_to_size[] = {
+    [SH_type_l1_32_shadow]   = 2,
+    [SH_type_fl1_32_shadow]  = 2,
+    [SH_type_l2_32_shadow]   = 4,
+    [SH_type_l1_pae_shadow]  = 1,
+    [SH_type_fl1_pae_shadow] = 1,

xen/arch/x86/mm/shadow/hvm.c line 64

Quoted 10 lines
xen/arch/x86/mm/shadow/hvm.c +10 -0
+    [SH_type_l2_pae_shadow]  = 1,
+    [SH_type_l1_64_shadow]   = 1,
+    [SH_type_fl1_64_shadow]  = 1,
+    [SH_type_l2_64_shadow]   = 1,
+    [SH_type_l2h_64_shadow]  = 1,
+    [SH_type_l3_64_shadow]   = 1,
+    [SH_type_l4_64_shadow]   = 1,
+    [SH_type_p2m_table]      = 1,
+    [SH_type_monitor_table]  = 1,
+    [SH_type_oos_snapshot]   = 1,

... this feels like it's wanting to turn into a (1 + ...) expression.  I can't see anything that prevents us from reordering the SH_type constants, but

1 + (idx == 1 /* l1 */ || idx == /* fl1 */) + 2 * (idx == 3 /* l2 */);

doesn't obviously simplify further.

~Andrew

#19 lore raw

Re: [PATCH 6/8] x86/shadow: adjust and move sh_type_to_size[]

Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

Drop the SH_type_none entry - there are no allocation attempts with this type, and there also shouldn't be any. Adjust the shadow_size() alternative path to match that change. Also generalize two related assertions.

Quoted 7 lines
Quoted 5 lines

While there move the entire table and the respective part of the comment there to hvm.c, resulting in one less #ifdef. In the course of the movement switch to using designated initializers.

Thanks.

Quoted 40 lines
Quoted 25 lines
xen/arch/x86/mm/shadow/hvm.c +19 -0
@@ -33,6 +33,37 @@
 
 #include "private.h"
 
+/*
+ * This table shows the allocation behaviour of the different modes:
+ *
+ * Xen paging      64b  64b  64b
+ * Guest paging    32b  pae  64b
+ * PV or HVM       HVM  HVM   *
+ * Shadow paging   pae  pae  64b
+ *
+ * sl1 size         8k   4k   4k
+ * sl2 size        16k   4k   4k
+ * sl3 size         -    -    4k
+ * sl4 size         -    -    4k
+ */
+const uint8_t sh_type_to_size[] = {
+    [SH_type_l1_32_shadow]   = 2,
+    [SH_type_fl1_32_shadow]  = 2,
+    [SH_type_l2_32_shadow]   = 4,
+    [SH_type_l1_pae_shadow]  = 1,
+    [SH_type_fl1_pae_shadow] = 1,

xen/arch/x86/mm/shadow/hvm.c line 64

Quoted 10 lines
xen/arch/x86/mm/shadow/hvm.c +10 -0
+    [SH_type_l2_pae_shadow]  = 1,
+    [SH_type_l1_64_shadow]   = 1,
+    [SH_type_fl1_64_shadow]  = 1,
+    [SH_type_l2_64_shadow]   = 1,
+    [SH_type_l2h_64_shadow]  = 1,
+    [SH_type_l3_64_shadow]   = 1,
+    [SH_type_l4_64_shadow]   = 1,
+    [SH_type_p2m_table]      = 1,
+    [SH_type_monitor_table]  = 1,
+    [SH_type_oos_snapshot]   = 1,

... this feels like it's wanting to turn into a (1 + ...) expression.  I can't see anything that prevents us from reordering the SH_type constants, but

Quoted 3 lines

1 + (idx == 1 /* l1 */ || idx == /* fl1 */) + 2 * (idx == 3 /* l2 */);

doesn't obviously simplify further.

But that would then undermine the cases where the function returns 0, which the two (generalized in this change) assertions actually check for not having got back. This would also become relevant for the l2h slot, which - if not right here (see the respective remark) - will become zero in a subsequent patch when !PV32.

Jan

#20 lore raw

PATCH 7/8 x86/shadow: don't open-code copy_domain_page()

In reply to Jan Beulich

Let's use the library-like function that we have.

xen/arch/x86/mm/shadow/common.c +1 -6
@@ -455,7 +455,6 @@ static void _sh_resync(struct vcpu *v, m
 static void oos_hash_add(struct vcpu *v, mfn_t gmfn)
 {
     int i, idx, oidx, swap = 0;
-    void *gptr, *gsnpptr;
     mfn_t *oos = v->arch.paging.shadow.oos;
     mfn_t *oos_snapshot = v->arch.paging.shadow.oos_snapshot;
     struct oos_fixup *oos_fixup = v->arch.paging.shadow.oos_fixup;
@@ -488,11 +487,7 @@ static void oos_hash_add(struct vcpu *v,
     if ( swap )
         SWAP(oos_snapshot[idx], oos_snapshot[oidx]);
 
-    gptr = map_domain_page(oos[oidx]);
-    gsnpptr = map_domain_page(oos_snapshot[oidx]);
-    memcpy(gsnpptr, gptr, PAGE_SIZE);
-    unmap_domain_page(gptr);
-    unmap_domain_page(gsnpptr);
+    copy_domain_page(oos_snapshot[oidx], oos[oidx]);
 }
 
 /* Remove an MFN from the list of out-of-sync guest pagetables */
#22 lore raw

PATCH 8/8 x86/shadow: drop zero initialization from shadow_domain_init()

In reply to Jan Beulich

There's no need for this as struct domain starts out zero-filled.

xen/arch/x86/mm/shadow/common.c +0 -4
@@ -64,12 +64,8 @@ int shadow_domain_init(struct domain *d)
 
     d->arch.paging.update_paging_mode = shadow_update_paging_mode;
 
-#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
-    d->arch.paging.shadow.oos_active = 0;
-#endif
 #ifdef CONFIG_HVM
     d->arch.paging.flush_tlb = shadow_flush_tlb;
-    d->arch.paging.shadow.pagetable_dying_op = 0;
 #endif
 
     return 0;