Skip to contentSkip to conversation

p2m: Allow non-leaf entries to be replaced by leaf entries

PATCH In review George Dunlap 2 participants 2 messages Jan 21, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 p2m: Allow non-leaf entries to be replaced by leaf entries

# HG changeset patch
# User George Dunlap <[email protected]> # Date 1295624256 0
# Node ID 9ca9331c97808b6aa02697619743cf6e8030587a # Parent 003acf02d416d657f750b7a7748fa8c5a932222c
p2m: Allow non-leaf entries to be replaced by leaf entries

Allow l2 and l3 p2m tables to be replaced with 2MB and 1GB pages respectively, freeing the p2m table page properly. This allows, for example, a sequence of 512 singleton zero pages to be replaced with a superpage populate-on-demand entry.

Changes:
* Add a p2m_free_ptp() corresponding to p2m_alloc_ptp(), which will handle everything related to the freeing properly.
* Add p2m_free_entry(), based on ept_free_entry(), which will free intermediate tables recursively.
* For both ept and p2m, when replacing non-leaf entries with leaf entries, keep old entry and call *_free_entry() after new entry has been written and proper flushes have been done.

xen/arch/x86/mm/hap/hap.c +5 -3
diff -r 003acf02d416 -r 9ca9331c9780 xen/arch/x86/mm/hap/hap.c
@@ -333,9 +333,11 @@
 
     ASSERT(page_get_owner(pg) == d);
     /* Should have just the one ref we gave it in alloc_p2m_page() */
-    if ( (pg->count_info & PGC_count_mask) != 1 )
-        HAP_ERROR("Odd p2m page count c=%#lx t=%"PRtype_info"\n",
-                  pg->count_info, pg->u.inuse.type_info);
+    if ( (pg->count_info & PGC_count_mask) != 1 ) {
+        HAP_ERROR("Odd p2m page %p count c=%#lx t=%"PRtype_info"\n",
+                     pg, pg->count_info, pg->u.inuse.type_info);
+        WARN();
+    }
     pg->count_info &= ~PGC_count_mask;
     /* Free should not decrement domain's total allocation, since
      * these pages were allocated without an owner. */
xen/arch/x86/mm/hap/p2m-ept.c +20 -6
diff -r 003acf02d416 -r 9ca9331c9780 xen/arch/x86/mm/hap/p2m-ept.c
@@ -166,8 +166,6 @@
 /* free ept sub tree behind an entry */
 void ept_free_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry, int level)
 {
-    struct domain *d = p2m->domain;
-
     /* End if the entry is a leaf entry. */
     if ( level == 0 || !is_epte_present(ept_entry) ||
          is_epte_superpage(ept_entry) )
@@ -180,8 +178,8 @@
             ept_free_entry(p2m, epte + i, level - 1);
         unmap_domain_page(epte);
     }
-
-    d->arch.paging.free_page(d, mfn_to_page(ept_entry->mfn));
+    
+    p2m_free_ptp(p2m, mfn_to_page(ept_entry->mfn));
 }
 
 static int ept_split_super_page(struct p2m_domain *p2m, ept_entry_t *ept_entry,
@@ -317,6 +315,7 @@
     int vtd_pte_present = 0;
     int needs_sync = 1;
     struct domain *d = p2m->domain;
+    ept_entry_t old_entry = { .epte = 0 };
 
     /*
      * the caller must make sure:
@@ -357,8 +356,12 @@
     vtd_pte_present = is_epte_present(ept_entry) ? 1 : 0;
 
     /*
-     * When we are here, we must be on a leaf ept entry
-     * with i == target or i > target.
+     * If we're here with i > target, we must be at a leaf node, and
+     * we need to break up the superpage.
+     *
+     * If we're here with i == target and i > 0, we need to check to see
+     * if we're replacing a non-leaf entry (i.e., pointing to an N-1 table)
+     * with a leaf entry (a 1GiB or 2MiB page), and handle things appropriately.
      */
 
     if ( i == target )
@@ -370,6 +373,10 @@
         if ( !is_epte_present(ept_entry) )
             needs_sync = 0;
 
+        /* If we're replacing a non-leaf entry with a leaf entry (1GiB or 2MiB),
+         * the intermediate tables will be freed below after the ept flush */
+        old_entry = *ept_entry;
+
         if ( mfn_valid(mfn_x(mfn)) || direct_mmio || p2m_is_paged(p2mt) ||
              (p2mt == p2m_ram_paging_in_start) )
         {
@@ -487,6 +494,13 @@
         }
     }
 
+    /* Release the old intermediate tables, if any.  This has to be the
+       last thing we do, after the ept_sync_domain() and removal
+       from the iommu tables, so as to avoid a potential
+       use-after-free. */
+    if ( is_epte_present(&old_entry) )
+        ept_free_entry(p2m, &old_entry, target);
+
     return rv;
 }
 
xen/arch/x86/mm/p2m.c +55 -8

113 lines not on this page · raw message

xen/include/asm-x86/p2m.h +1 -0
diff -r 003acf02d416 -r 9ca9331c9780 xen/include/asm-x86/p2m.h
@@ -541,6 +541,7 @@
 #endif
 
 struct page_info *p2m_alloc_ptp(struct p2m_domain *p2m, unsigned long type);
+void p2m_free_ptp(struct p2m_domain *p2m, struct page_info *pg);
 
 #endif /* _XEN_P2M_H */
#1 lore raw
Quoted 16 lines · George Dunlap

p2m: Allow non-leaf entries to be replaced by leaf entries

Allow l2 and l3 p2m tables to be replaced with 2MB and 1GB pages respectively, freeing the p2m table page properly. This allows, for example, a sequence of 512 singleton zero pages to be replaced with a superpage populate-on-demand entry.

Changes:
* Add a p2m_free_ptp() corresponding to p2m_alloc_ptp(), which will handle everything related to the freeing properly.
* Add p2m_free_entry(), based on ept_free_entry(), which will free intermediate tables recursively.
* For both ept and p2m, when replacing non-leaf entries with leaf entries, keep old entry and call *_free_entry() after new entry has been written and proper flushes have been done.

Quoted 1 line

Applied, thanks. (I made a few small changes to help the debug=y build.)

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)