Skip to contentSkip to conversation

x86/P2M: allow 2M superpage use for shadowed guests

PATCH In review Jan Beulich 2 participants 6 messages Aug 12, 2022 – Aug 16, 2022

lore
Share & tools
lore
#0 lore raw

v3 cover x86/P2M: allow 2M superpage use for shadowed guests

I did notice this anomaly in the context of IOMMU side work.

1: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part I) 2: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part II) 3: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part III) 4: P2M: allow 2M superpage use for shadowed guests

Jan

#1 lore raw

PATCH v3 1/4 x86/shadow: slightly consolidate sh_unshadow_for_p2m_change() (part I)

Replace a p2m_is_ram() check in the 2M case by an explicit _PAGE_PRESENT one, to make more obvious that the subsequent l1e_get_mfn() actually retrieves something that really is an MFN. It doesn't really matter whether it's RAM, as the subsequent comparison with the original MFN is going to lead to zapping of everything except the "same MFN again" case.

---
v2: Split from previous bigger patch.

xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -856,7 +856,7 @@ static void cf_check sh_unshadow_for_p2m
             for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
             {
                 if ( !npte ||
-                     !p2m_is_ram(p2m_flags_to_type(l1e_get_flags(npte[i]))) ||
+                     !(l1e_get_flags(npte[i]) & _PAGE_PRESENT) ||
                      !mfn_eq(l1e_get_mfn(npte[i]), omfn) )
                 {
                     /* This GFN->MFN mapping has gone away */
#2 lore raw

PATCH v3 2/4 x86/shadow: slightly consolidate sh_unshadow_for_p2m_change() (part II)

In reply to Jan Beulich

Pull common checks out of the switch(). This includes extending a _PAGE_PRESENT check to L1 as well, which presumably was deemed redundant with p2m_is_valid() || p2m_is_grant(), but I think we are better off being explicit in all cases. Note that for L2 (or higher) the grant check isn't strictly necessary, as grants are only ever single pages. Leave a respective assertion.

With _PAGE_PRESENT checked uniformly, the suspicious mfn_valid(omfn) checks can be dropped rather than moved/folded - if anything we'd need to compare against INVALID_MFN, but that won't come out of l1e_get_mfn().

For L1 replace the moved out condition with a PTE comparison: There's no need for any update or flushing when the two match.

---
v3: Compare PTEs, not MFNs. Drop mfn_valid().
v2: Split from previous bigger patch. Add grant related assertion for

    L2.
xen/arch/x86/mm/shadow/hvm.c +14 -5
@@ -810,19 +810,27 @@ static void cf_check sh_unshadow_for_p2m
     if ( unlikely(!d->arch.paging.shadow.total_pages) )
         return;
 
+    /* Only previously present / valid entries need processing. */
+    if ( !(oflags & _PAGE_PRESENT) ||
+         (!p2m_is_valid(p2mt) && !p2m_is_grant(p2mt)) )
+        return;
+
     switch ( level )
     {
     default:
         /*
          * The following assertion is to make sure we don't step on 1GB host
-         * page support of HVM guest.
+         * page support of HVM guest. Plus we rely on ->set_entry() to never
+         * be called with orders above PAGE_ORDER_2M, not even to install
+         * non-present entries (which in principle ought to be fine even
+         * without respective large page support).
          */
-        ASSERT(!((oflags & _PAGE_PRESENT) && (oflags & _PAGE_PSE)));
+        ASSERT(!(oflags & _PAGE_PSE));
         break;
 
     /* If we're removing an MFN from the p2m, remove it from the shadows too */
     case 1:
-        if ( (p2m_is_valid(p2mt) || p2m_is_grant(p2mt)) && mfn_valid(omfn) )
+        if ( l1e_get_intpte(old) != l1e_get_intpte(new) )
         {
             sh_remove_all_shadows_and_parents(d, omfn);
             if ( sh_remove_all_mappings(d, omfn, _gfn(gfn)) )
@@ -836,10 +844,11 @@ static void cf_check sh_unshadow_for_p2m
      * scheme, that's OK, but otherwise they must be unshadowed.
      */
     case 2:
-        if ( !(oflags & _PAGE_PRESENT) || !(oflags & _PAGE_PSE) )
+        if ( !(oflags & _PAGE_PSE) )
             break;
 
-        if ( p2m_is_valid(p2mt) && mfn_valid(omfn) )
+        ASSERT(!p2m_is_grant(p2mt));
+
         {
             unsigned int i;
             mfn_t nmfn = l1e_get_mfn(new);
#3 lore raw

PATCH v3 3/4 x86/shadow: slightly consolidate sh_unshadow_for_p2m_change() (part III)

In reply to Jan Beulich

In preparation for reactivating the presently dead 2M page path of the function, also deal with the case of replacing an L1 page table all in one go. Note that the prior comparing of MFNs to bypass the removal of shadows was insufficient (but kind of benign, for being dead code so far) - at the very least the R/W bit also needs considering there (to be on the safe side, compare the full [virtual] PTEs).

While adjusting the first conditional in the loop for the use of the new local variable "nflags", also drop mfn_valid(): If anything we'd need to compare against INVALID_MFN, but that won't come out of l1e_get_mfn().

---
v3: Compare (virtual) PTEs, not MFNs. Correct MFN increment at the
bottom of the loop. Respect PAT bit.
v2: Split from previous bigger patch.
---
The two mfn_add()s dealing with PAT aren't pretty, but short of us also having mfn_sub() the cast there is pretty much unavoidable (alternatives not really looking any neater).

xen/arch/x86/mm/shadow/hvm.c +39 -14
@@ -844,40 +844,65 @@ static void cf_check sh_unshadow_for_p2m
      * scheme, that's OK, but otherwise they must be unshadowed.
      */
     case 2:
-        if ( !(oflags & _PAGE_PSE) )
-            break;
-
-        ASSERT(!p2m_is_grant(p2mt));
-
         {
             unsigned int i;
             mfn_t nmfn = l1e_get_mfn(new);
-            l1_pgentry_t *npte = NULL;
+            unsigned int nflags = l1e_get_flags(new);
+            l1_pgentry_t *npte = NULL, *opte = NULL;
+
+            BUILD_BUG_ON(_PAGE_PAT != _PAGE_PSE);
 
+            if ( !(nflags & _PAGE_PRESENT) )
+                nmfn = INVALID_MFN;
             /* If we're replacing a superpage with a normal L1 page, map it */
-            if ( (l1e_get_flags(new) & _PAGE_PRESENT) &&
-                 !(l1e_get_flags(new) & _PAGE_PSE) &&
-                 mfn_valid(nmfn) )
+            else if ( !(nflags & _PAGE_PSE) )
                 npte = map_domain_page(nmfn);
+            else if ( !(mfn_x(nmfn) & (_PAGE_PSE_PAT >> PAGE_SHIFT)) )
+                nflags &= ~_PAGE_PSE;
+            else
+                nmfn = mfn_add(nmfn, -(long)(_PAGE_PSE_PAT >> PAGE_SHIFT));
+
+            /* If we're replacing a normal L1 page, map it as well. */
+            if ( !(oflags & _PAGE_PSE) )
+                opte = map_domain_page(omfn);
+            else if ( !(mfn_x(omfn) & (_PAGE_PSE_PAT >> PAGE_SHIFT)) )
+                oflags &= ~_PAGE_PSE;
+            else
+                omfn = mfn_add(omfn, -(long)(_PAGE_PSE_PAT >> PAGE_SHIFT));
 
             gfn &= ~(L1_PAGETABLE_ENTRIES - 1);
 
             for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
             {
-                if ( !npte ||
-                     !(l1e_get_flags(npte[i]) & _PAGE_PRESENT) ||
-                     !mfn_eq(l1e_get_mfn(npte[i]), omfn) )
+                if ( opte )
+                {
+                    oflags = l1e_get_flags(opte[i]);
+                    if ( !(oflags & _PAGE_PRESENT) )
+                        continue;
+                    omfn = l1e_get_mfn(opte[i]);
+                }
+
+                if ( npte )
+                {
+                    nflags = l1e_get_flags(npte[i]);
+                    nmfn = nflags & _PAGE_PRESENT
+                           ? l1e_get_mfn(npte[i]) : INVALID_MFN;
+                }
+
+                if ( !mfn_eq(nmfn, omfn) || nflags != oflags )
                 {
                     /* This GFN->MFN mapping has gone away */
                     sh_remove_all_shadows_and_parents(d, omfn);
                     if ( sh_remove_all_mappings(d, omfn, _gfn(gfn + i)) )
                         flush = true;
                 }
+
                 omfn = mfn_add(omfn, 1);
+                nmfn = mfn_add(nmfn, !mfn_eq(nmfn, INVALID_MFN));
             }
 
-            if ( npte )
-                unmap_domain_page(npte);
+            unmap_domain_page(opte);
+            unmap_domain_page(npte);
         }
 
         break;
#4 lore raw

PATCH v3 4/4 x86/P2M: allow 2M superpage use for shadowed guests

In reply to Jan Beulich

For guests in shadow mode the P2M table gets used only by software. The only place where it matters whether superpages in the P2M can be dealt with is sh_unshadow_for_p2m_change(): The table is never made accessible to hardware for address translation, and the only checks of _PAGE_PSE in P2M entries in shadow code are in this function (all others are against guest page table entries). That function has been capable of handling them even before commit 0ca1669871f8a ("P2M: check whether hap mode is enabled before using 2mb pages") disabled 2M use in this case for dubious reasons ("potential errors when hap is disabled").

While doing this, move "order" into more narrow scope and replace the local variable "d" by a new "hap" one.

---
v2: Extend description.
---
Strictly speaking "fn_mask" could also be "unsigned int"; I wasn't sure whether changing that would cause objections.

While at least sh_unshadow_for_p2m_change() presently relies on this behavior, it is somewhat odd (and inefficient) for p2m_set_entry() to split even non-present mappings.

xen/arch/x86/mm/p2m.c +9 -15
@@ -403,28 +403,22 @@ struct page_info *p2m_get_page_from_gfn(
 int p2m_set_entry(struct p2m_domain *p2m, gfn_t gfn, mfn_t mfn,
                   unsigned int page_order, p2m_type_t p2mt, p2m_access_t p2ma)
 {
-    struct domain *d = p2m->domain;
+    bool hap = hap_enabled(p2m->domain);
     unsigned long todo = 1ul << page_order;
-    unsigned int order;
     int set_rc, rc = 0;
 
     ASSERT(gfn_locked_by_me(p2m, gfn));
 
     while ( todo )
     {
-        if ( hap_enabled(d) )
-        {
-            unsigned long fn_mask = !mfn_eq(mfn, INVALID_MFN) ? mfn_x(mfn) : 0;
-
-            fn_mask |= gfn_x(gfn) | todo;
-
-            order = (!(fn_mask & ((1ul << PAGE_ORDER_1G) - 1)) &&
-                     hap_has_1gb) ? PAGE_ORDER_1G :
-                    (!(fn_mask & ((1ul << PAGE_ORDER_2M) - 1)) &&
-                     hap_has_2mb) ? PAGE_ORDER_2M : PAGE_ORDER_4K;
-        }
-        else
-            order = 0;
+        unsigned long fn_mask = (!mfn_eq(mfn, INVALID_MFN) ? mfn_x(mfn) : 0) |
+                                gfn_x(gfn) | todo;
+        unsigned int order = (!(fn_mask & ((1ul << PAGE_ORDER_1G) - 1)) &&
+                              hap && hap_has_1gb)
+                             ? PAGE_ORDER_1G
+                             : (!(fn_mask & ((1ul << PAGE_ORDER_2M) - 1)) &&
+                                (!hap || hap_has_2mb))
+                               ? PAGE_ORDER_2M : PAGE_ORDER_4K;
 
         set_rc = p2m->set_entry(p2m, gfn, mfn, order, p2mt, p2ma, -1);
         if ( set_rc )
Acked-by on the v3 series
#5 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

I did notice this anomaly in the context of IOMMU side work.

1: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part I) 2: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part II) 3: shadow: slightly consolidate sh_unshadow_for_p2m_change() (part III) 4: P2M: allow 2M superpage use for shadowed guests

FWIW I think that adding some kind of mfn_mask() opreration would be neater and more understandable than subtracting the PSE flag.

Cheers,

Tim.