Skip to contentSkip to conversation

p2m cleanups

RFC PATCH New Tim Deegan 1 participant 14 messages May 13, 2011

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

RFC PATCH 1/1 p2m cleanups

This series is the current state of my p2m cleanups; it passes basic smoke-tests but needs more serious testing before it's ready to go in. I'm posting it as an RFC for now so anyone who's interested in it can see the way things are going and maybe even give feedback before I get too far down this particular rabbit-hole.

So far, it:
- fixes a lot of locking bugs
- tidies up the p2m interface, mostly to stop the callers needing to know about internal p2m cruft.
- collects all the x86/mm locks together, defining and enforcing a locking order.

Next stages:
- Fix the ugly "need_lock" semi-recursive locking.
- Fix the idiom where many callers of gfn_to_mfn_* immediately have to page in or unshare the result by moving that logic into p2m code and requesting it with a flag.
- Disentangle the page allocators from hap and shadow, and put them under their own lock, which will allow me to undo some of the contortions the log-dirty code goes to when allocating memory.
- Sort out the interactions between nested HAP and memory sharing/paging.

After all that (phew!) I should be in a position to look at integrating the p2m code and the core typecounting code better, to avoid the TOCTOU races that have been pointed out in the memory sharing code.

Cheers,

Tim.

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

PATCH 1/1 x86/mm/p2m: Mark internal functions static

# HG changeset patch
# User Tim Deegan <[email protected]> # Date 1305302438 -3600
# Node ID c5aebdd80c6d8e10113342c023b9e42d20daf2f0 # Parent f9bb0bbea7c27e256275cfe58c16c0077542984e x86/mm/p2m: Mark internal functions static

xen/arch/x86/mm/p2m-ept.c +1 -1
diff -r f9bb0bbea7c2 -r c5aebdd80c6d xen/arch/x86/mm/p2m-ept.c
@@ -164,7 +164,7 @@ static int ept_set_middle_entry(struct p
 }
 
 /* free ept sub tree behind an entry */
-void ept_free_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry, int level)
+static void ept_free_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry, int level)
 {
     /* End if the entry is a leaf entry. */
     if ( level == 0 || !is_epte_present(ept_entry) ||
xen/arch/x86/mm/p2m-pt.c +5 -36

66 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +30 -0
diff -r f9bb0bbea7c2 -r c5aebdd80c6d xen/arch/x86/mm/p2m.c
@@ -169,6 +169,36 @@ int set_p2m_entry(struct p2m_domain *p2m
     return rc;
 }
 
+struct page_info *p2m_alloc_ptp(struct p2m_domain *p2m, unsigned long type)
+{
+    struct page_info *pg;
+
+    ASSERT(p2m);
+    ASSERT(p2m->domain);
+    ASSERT(p2m->domain->arch.paging.alloc_page);
+    pg = p2m->domain->arch.paging.alloc_page(p2m->domain);
+    if (pg == NULL)
+        return NULL;
+
+    page_list_add_tail(pg, &p2m->pages);
+    pg->u.inuse.type_info = type | 1 | PGT_validated;
+
+    return pg;
+}
+
+void p2m_free_ptp(struct p2m_domain *p2m, struct page_info *pg)
+{
+    ASSERT(pg);
+    ASSERT(p2m);
+    ASSERT(p2m->domain);
+    ASSERT(p2m->domain->arch.paging.free_page);
+
+    page_list_del(pg, &p2m->pages);
+    p2m->domain->arch.paging.free_page(p2m->domain, pg);
+
+    return;
+}
+
 // Allocate a new p2m table for a domain.
 //
 // The structure of the p2m table is that of a pagetable for xen (i.e. it is
xen/include/asm-x86/p2m.h +0 -11
diff -r f9bb0bbea7c2 -r c5aebdd80c6d xen/include/asm-x86/p2m.h
@@ -499,21 +499,11 @@ static inline unsigned long mfn_to_gfn(s
 /* Init the datastructures for later use by the p2m code */
 int p2m_init(struct domain *d);
 
-/* PTE flags for various types of p2m entry */
-unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn);
-
 /* Allocate a new p2m table for a domain. 
  *
  * Returns 0 for success or -errno. */
 int p2m_alloc_table(struct p2m_domain *p2m);
 
-/* Find the next level's P2M entry, checking for out-of-range gfn's...
- * Returns NULL on error.
- */
-l1_pgentry_t *
-p2m_find_entry(void *table, unsigned long *gfn_remainder,
-               unsigned long gfn, uint32_t shift, uint32_t max);
-
 /* Return all the p2m resources to Xen. */
 void p2m_teardown(struct p2m_domain *p2m);
 void p2m_final_teardown(struct domain *d);
@@ -584,7 +574,6 @@ static inline void guest_physmap_remove_
 }
 
 /* Change types across all p2m entries in a domain */
-void p2m_change_type_global(struct p2m_domain *p2m, p2m_type_t ot, p2m_type_t nt);
 void p2m_change_entry_type_global(struct p2m_domain *p2m, p2m_type_t ot, p2m_type_t nt);
 
 /* Compare-exchange the type of a single p2m entry */
author
#2 lore raw

PATCH 1/1 x86/mm/p2m: little fixes and tidying up

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]> # Date 1305302438 -3600
# Node ID 5f762329e746d99b1b4e68de6df71ec10d676cb9 # Parent c5aebdd80c6d8e10113342c023b9e42d20daf2f0 x86/mm/p2m: little fixes and tidying up

xen/arch/x86/mm/p2m-pod.c +5 -8
diff -r c5aebdd80c6d -r 5f762329e746 xen/arch/x86/mm/p2m-pod.c
@@ -60,7 +60,6 @@ extern void audit_p2m(struct p2m_domain 
 # define audit_p2m(_p2m, _m2p) do { (void)(_p2m),(_m2p); } while (0)
 #endif /* P2M_AUDIT */
 
-#define SUPERPAGE_PAGES (1UL << 9)
 #define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
 
 /*
@@ -706,8 +705,7 @@ p2m_pod_zero_check_superpage(struct p2m_
     }
 
     /* Try to remove the page, restoring old mapping if it fails. */
-    set_p2m_entry(p2m, gfn,
-                  _mfn(POPULATE_ON_DEMAND_MFN), 9,
+    set_p2m_entry(p2m, gfn, _mfn(0), 9,
                   p2m_populate_on_demand, p2m->default_access);
 
     /* Make none of the MFNs are used elsewhere... for example, mapped
@@ -819,8 +817,7 @@ p2m_pod_zero_check(struct p2m_domain *p2
         }
 
         /* Try to remove the page, restoring old mapping if it fails. */
-        set_p2m_entry(p2m, gfns[i],
-                      _mfn(POPULATE_ON_DEMAND_MFN), 0,
+        set_p2m_entry(p2m, gfns[i], _mfn(0), 0,
                       p2m_populate_on_demand, p2m->default_access);
 
         /* See if the page was successfully unmapped.  (Allow one refcount
@@ -984,7 +981,7 @@ p2m_pod_demand_populate(struct p2m_domai
          * set_p2m_entry() should automatically shatter the 1GB page into 
          * 512 2MB pages. The rest of 511 calls are unnecessary.
          */
-        set_p2m_entry(p2m, gfn_aligned, _mfn(POPULATE_ON_DEMAND_MFN), 9,
+        set_p2m_entry(p2m, gfn_aligned, _mfn(0), 9,
                       p2m_populate_on_demand, p2m->default_access);
         audit_p2m(p2m, 1);
         p2m_unlock(p2m);
@@ -1072,7 +1069,7 @@ remap_and_retry:
     /* Remap this 2-meg region in singleton chunks */
     gfn_aligned = (gfn>>order)<<order;
     for(i=0; i<(1<<order); i++)
-        set_p2m_entry(p2m, gfn_aligned+i, _mfn(POPULATE_ON_DEMAND_MFN), 0,
+        set_p2m_entry(p2m, gfn_aligned+i, _mfn(0), 0,
                       p2m_populate_on_demand, p2m->default_access);
     if ( tb_init_done )
     {
@@ -1132,7 +1129,7 @@ guest_physmap_mark_populate_on_demand(st
     }
 
     /* Now, actually do the two-way mapping */
-    if ( !set_p2m_entry(p2m, gfn, _mfn(POPULATE_ON_DEMAND_MFN), order,
+    if ( !set_p2m_entry(p2m, gfn, _mfn(0), order,
                         p2m_populate_on_demand, p2m->default_access) )
         rc = -EINVAL;
     else
xen/arch/x86/mm/p2m-pt.c +12 -16
diff -r c5aebdd80c6d -r 5f762329e746 xen/arch/x86/mm/p2m-pt.c
@@ -68,9 +68,6 @@
 #define P2M_BASE_FLAGS \
         (_PAGE_PRESENT | _PAGE_USER | _PAGE_DIRTY | _PAGE_ACCESSED)
 
-#define SUPERPAGE_PAGES (1UL << 9)
-#define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
-
 static unsigned long p2m_type_to_flags(p2m_type_t t, mfn_t mfn)
 {
     unsigned long flags;
@@ -84,32 +81,31 @@ static unsigned long p2m_type_to_flags(p
 #else
     flags = (t & 0x7UL) << 9;
 #endif
-#ifndef HAVE_GRANT_MAP_P2M
-    BUG_ON(p2m_is_grant(t));
+
+#ifndef __x86_64__
+    /* 32-bit builds don't support a lot of the p2m types */
+    BUG_ON(t > p2m_populate_on_demand);
 #endif
+
     switch(t)
     {
     case p2m_invalid:
+    case p2m_mmio_dm:
+    case p2m_populate_on_demand:
     default:
         return flags;
+    case p2m_ram_ro:
+    case p2m_grant_map_ro:
+    case p2m_ram_logdirty:
+    case p2m_ram_shared:
+        return flags | P2M_BASE_FLAGS;
     case p2m_ram_rw:
     case p2m_grant_map_rw:
         return flags | P2M_BASE_FLAGS | _PAGE_RW;
-    case p2m_ram_logdirty:
-        return flags | P2M_BASE_FLAGS;
-    case p2m_ram_ro:
-    case p2m_grant_map_ro:
-        return flags | P2M_BASE_FLAGS;
-    case p2m_ram_shared:
-        return flags | P2M_BASE_FLAGS;
-    case p2m_mmio_dm:
-        return flags;
     case p2m_mmio_direct:
         if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) )
             flags |= _PAGE_RW;
         return flags | P2M_BASE_FLAGS | _PAGE_PCD;
-    case p2m_populate_on_demand:
-        return flags;
     }
 }
 
xen/arch/x86/mm/p2m.c +27 -26

72 lines not on this page · raw message

xen/include/asm-x86/p2m.h +90 -84

235 lines not on this page · raw message

xen/include/asm-x86/x86_32/page.h +1 -0
diff -r c5aebdd80c6d -r 5f762329e746 xen/include/asm-x86/x86_32/page.h
@@ -15,6 +15,7 @@
 #define L3_PAGETABLE_ENTRIES    4
 #define ROOT_PAGETABLE_ENTRIES  L3_PAGETABLE_ENTRIES
 #define SUPERPAGE_ORDER         PAGETABLE_ORDER
+#define SUPERPAGE_PAGES         (1<<SUPERPAGE_ORDER)
 
 /*
  * Architecturally, physical addresses may be up to 52 bits. However, the
xen/include/asm-x86/x86_64/page.h +1 -0
diff -r c5aebdd80c6d -r 5f762329e746 xen/include/asm-x86/x86_64/page.h
@@ -17,6 +17,7 @@
 #define L4_PAGETABLE_ENTRIES    (1<<PAGETABLE_ORDER)
 #define ROOT_PAGETABLE_ENTRIES  L4_PAGETABLE_ENTRIES
 #define SUPERPAGE_ORDER         PAGETABLE_ORDER
+#define SUPERPAGE_PAGES         (1<<SUPERPAGE_ORDER)
 
 #define __PAGE_OFFSET           DIRECTMAP_VIRT_START
 #define __XEN_VIRT_START        XEN_VIRT_START
author
#3 lore raw

PATCH 1/1 x86/mm/p2m: hide the current-domain fast-path inside the p2m-pt code

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302438 -3600
# Node ID 747679982b74d9e20f4b77e11b59f2be83accd03
# Parent 5f762329e746d99b1b4e68de6df71ec10d676cb9 x86/mm/p2m: hide the current-domain fast-path inside the p2m-pt code.

The other implementations of the p2m interface don't have this, and it will go away entirely when 32-bit builds go away, so take it out of the interface.

xen/arch/x86/hvm/hvm.c +1 -1
diff -r 5f762329e746 -r 747679982b74 xen/arch/x86/hvm/hvm.c
@@ -1218,7 +1218,7 @@ int hvm_hap_nested_page_fault(unsigned l
     }
 
     p2m = p2m_get_hostp2m(v->domain);
-    mfn = gfn_to_mfn_type_current(p2m, gfn, &p2mt, &p2ma, p2m_guest);
+    mfn = gfn_to_mfn_type_p2m(p2m, gfn, &p2mt, &p2ma, p2m_guest);
 
     /* Check access permissions first, then handle faults */
     if ( access_valid && (mfn_x(mfn) != INVALID_MFN) )
xen/arch/x86/mm/p2m-ept.c +0 -8
diff -r 5f762329e746 -r 747679982b74 xen/arch/x86/mm/p2m-ept.c
@@ -692,13 +692,6 @@ out:
     return;
 }
 
-static mfn_t ept_get_entry_current(struct p2m_domain *p2m,
-                                   unsigned long gfn, p2m_type_t *t, p2m_access_t *a,
-                                   p2m_query_t q)
-{
-    return ept_get_entry(p2m, gfn, t, a, q);
-}
-
 /*
  * To test if the new emt type is the same with old,
  * return 1 to not to reset ept entry.
@@ -824,7 +817,6 @@ void ept_p2m_init(struct p2m_domain *p2m
 {
     p2m->set_entry = ept_set_entry;
     p2m->get_entry = ept_get_entry;
-    p2m->get_entry_current = ept_get_entry_current;
     p2m->change_entry_type_global = ept_change_entry_type_global;
 }
 
xen/arch/x86/mm/p2m-pt.c +178 -180

382 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +0 -2
diff -r 5f762329e746 -r 747679982b74 xen/arch/x86/mm/p2m.c
@@ -109,7 +109,6 @@ p2m_init_nestedp2m(struct domain *d)
         if (p2m == NULL)
             return -ENOMEM;
         p2m_initialise(d, p2m);
-        p2m->get_entry_current = p2m->get_entry;
         p2m->write_p2m_entry = nestedp2m_write_p2m_entry;
     }
 
@@ -964,7 +963,6 @@ p2m_flush_locked(struct p2m_domain *p2m)
 
     p2m_teardown(p2m);
     p2m_initialise(p2m->domain, p2m);
-    p2m->get_entry_current = p2m->get_entry;
     p2m->write_p2m_entry = nestedp2m_write_p2m_entry;
     return p2m_alloc_table(p2m);
 }
xen/include/asm-x86/p2m.h +21 -38

83 lines not on this page · raw message

author
#4 lore raw

PATCH 1/1 x86/mm/p2m: merge gfn_to_mfn_unshare with other gfn_to_mfn paths

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302438 -3600
# Node ID 8938effd3d7cc9f008d45b6569ffb14bc8a91ed6
# Parent 747679982b74d9e20f4b77e11b59f2be83accd03 x86/mm/p2m: merge gfn_to_mfn_unshare with other gfn_to_mfn paths.

gfn_to_mfn_unshare() had its own function despite all other lookup types being handled in one place. Merge it into _gfn_to_mfn_type(), so that it gets the benefit of broken-page protection, for example, and tidy its interfaces up to fit.

The unsharing code still has a lot of bugs, e.g.
- failure to alloc for unshare on a foreign lookup still BUG()s,
- at least one race condition in unshare-and-retry
- p2m_* lookup types should probably be flags, not enum
but it's cleaner and will make later p2m cleanups easier.

xen/arch/x86/hvm/emulate.c +1 -1
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/hvm/emulate.c
@@ -63,7 +63,7 @@ static int hvmemul_do_io(
     int rc;
 
     /* Check for paged out page */
-    ram_mfn = gfn_to_mfn_unshare(p2m, ram_gfn, &p2mt, 0);
+    ram_mfn = gfn_to_mfn_unshare(p2m, ram_gfn, &p2mt);
     if ( p2m_is_paging(p2mt) )
     {
         p2m_mem_paging_populate(p2m, ram_gfn);
xen/arch/x86/hvm/hvm.c +6 -6
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/hvm/hvm.c
@@ -356,7 +356,7 @@ static int hvm_set_ioreq_page(
     unsigned long mfn;
     void *va;
 
-    mfn = mfn_x(gfn_to_mfn_unshare(p2m, gmfn, &p2mt, 0));
+    mfn = mfn_x(gfn_to_mfn_unshare(p2m, gmfn, &p2mt));
     if ( !p2m_is_ram(p2mt) )
         return -EINVAL;
     if ( p2m_is_paging(p2mt) )
@@ -1775,7 +1775,7 @@ static void *__hvm_map_guest_frame(unsig
     struct p2m_domain *p2m = p2m_get_hostp2m(current->domain);
 
     mfn = mfn_x(writable
-                ? gfn_to_mfn_unshare(p2m, gfn, &p2mt, 0)
+                ? gfn_to_mfn_unshare(p2m, gfn, &p2mt)
                 : gfn_to_mfn(p2m, gfn, &p2mt));
     if ( (p2m_is_shared(p2mt) && writable) || !p2m_is_ram(p2mt) )
         return NULL;
@@ -2237,7 +2237,7 @@ static enum hvm_copy_result __hvm_copy(
             gfn = addr >> PAGE_SHIFT;
         }
 
-        mfn = mfn_x(gfn_to_mfn_unshare(p2m, gfn, &p2mt, 0));
+        mfn = mfn_x(gfn_to_mfn_unshare(p2m, gfn, &p2mt));
 
         if ( p2m_is_paging(p2mt) )
         {
@@ -3696,7 +3696,7 @@ long do_hvm_op(unsigned long op, XEN_GUE
         rc = -EINVAL;
         if ( is_hvm_domain(d) )
         {
-            gfn_to_mfn_unshare(p2m_get_hostp2m(d), a.pfn, &t, 0);
+            gfn_to_mfn_unshare(p2m_get_hostp2m(d), a.pfn, &t);
             if ( p2m_is_mmio(t) )
                 a.mem_type =  HVMMEM_mmio_dm;
             else if ( p2m_is_readonly(t) )
@@ -3751,7 +3751,7 @@ long do_hvm_op(unsigned long op, XEN_GUE
             p2m_type_t t;
             p2m_type_t nt;
             mfn_t mfn;
-            mfn = gfn_to_mfn_unshare(p2m, pfn, &t, 0);
+            mfn = gfn_to_mfn_unshare(p2m, pfn, &t);
             if ( p2m_is_paging(t) )
             {
                 p2m_mem_paging_populate(p2m, pfn);
@@ -3849,7 +3849,7 @@ long do_hvm_op(unsigned long op, XEN_GUE
             mfn_t mfn;
             int success;
 
-            mfn = gfn_to_mfn_unshare(p2m, pfn, &t, 0);
+            mfn = gfn_to_mfn_unshare(p2m, pfn, &t);
 
             p2m_lock(p2m);
             success = p2m->set_entry(p2m, pfn, mfn, 0, t, memaccess[a.hvmmem_access]);
xen/arch/x86/mm.c +1 -1
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/mm.c
@@ -4652,7 +4652,7 @@ long arch_memory_op(int op, XEN_GUEST_HA
             p2m_type_t p2mt;
 
             xatp.idx = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(d),
-                                                xatp.idx, &p2mt, 0));
+                                                xatp.idx, &p2mt));
             /* If the page is still shared, exit early */
             if ( p2m_is_shared(p2mt) )
             {
xen/arch/x86/mm/guest_walk.c +1 -1
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/mm/guest_walk.c
@@ -93,7 +93,7 @@ static inline void *map_domain_gfn(struc
                                    uint32_t *rc) 
 {
     /* Translate the gfn, unsharing if shared */
-    *mfn = gfn_to_mfn_unshare(p2m, gfn_x(gfn), p2mt, 0);
+    *mfn = gfn_to_mfn_unshare(p2m, gfn_x(gfn), p2mt);
     if ( p2m_is_paging(*p2mt) )
     {
         p2m_mem_paging_populate(p2m, gfn_x(gfn));
xen/arch/x86/mm/hap/guest_walk.c +2 -2
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/mm/hap/guest_walk.c
@@ -57,7 +57,7 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PA
     walk_t gw;
 
     /* Get the top-level table's MFN */
-    top_mfn = gfn_to_mfn_unshare(p2m, cr3 >> PAGE_SHIFT, &p2mt, 0);
+    top_mfn = gfn_to_mfn_unshare(p2m, cr3 >> PAGE_SHIFT, &p2mt);
     if ( p2m_is_paging(p2mt) )
     {
         p2m_mem_paging_populate(p2m, cr3 >> PAGE_SHIFT);
@@ -89,7 +89,7 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PA
     if ( missing == 0 )
     {
         gfn_t gfn = guest_l1e_get_gfn(gw.l1e);
-        gfn_to_mfn_unshare(p2m, gfn_x(gfn), &p2mt, 0);
+        gfn_to_mfn_unshare(p2m, gfn_x(gfn), &p2mt);
         if ( p2m_is_paging(p2mt) )
         {
             p2m_mem_paging_populate(p2m, gfn_x(gfn));
xen/arch/x86/mm/mem_sharing.c +14 -17
diff -r 747679982b74 -r 8938effd3d7c xen/arch/x86/mm/mem_sharing.c
@@ -293,8 +293,7 @@ static void mem_sharing_audit(void)
 
 
 static struct page_info* mem_sharing_alloc_page(struct domain *d, 
-                                                unsigned long gfn,
-                                                int must_succeed)
+                                                unsigned long gfn)
 {
     struct page_info* page;
     struct vcpu *v = current;
@@ -306,21 +305,20 @@ static struct page_info* mem_sharing_all
     memset(&req, 0, sizeof(req));
     req.type = MEM_EVENT_TYPE_SHARED;
 
-    if(must_succeed) 
+    if ( v->domain != d )
     {
-        /* We do not support 'must_succeed' any more. External operations such
-         * as grant table mappings may fail with OOM condition! 
-         */
-        BUG();
+        /* XXX This path needs some attention.  For now, just fail foreign 
+         * XXX requests to unshare if there's no memory.  This replaces 
+         * XXX old code that BUG()ed here; the callers now BUG()
+         * XXX elewhere. */
+        gdprintk(XENLOG_ERR, 
+                 "Failed alloc on unshare path for foreign (%d) lookup\n",
+                 d->domain_id);
+        return page;
     }
-    else
-    {
-        /* All foreign attempts to unshare pages should be handled through
-         * 'must_succeed' case. */
-        ASSERT(v->domain->domain_id == d->domain_id);
-        vcpu_pause_nosync(v);
-        req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
-    }
+
+    vcpu_pause_nosync(v);
+    req.flags |= MEM_EVENT_FLAG_VCPU_PAUSED;
 
     /* XXX: Need to reserve a request, not just check the ring! */
     if(mem_event_check_ring(d)) return page;
@@ -693,8 +691,7 @@ gfn_found:
     if(ret == 0) goto private_page_found;
         
     old_page = page;
-    page = mem_sharing_alloc_page(d, gfn, flags & MEM_SHARING_MUST_SUCCEED);
-    BUG_ON(!page && (flags & MEM_SHARING_MUST_SUCCEED));
+    page = mem_sharing_alloc_page(d, gfn);
     if(!page) 
     {
         /* We've failed to obtain memory for private page. Need to re-add the
xen/common/grant_table.c +8 -2
diff -r 747679982b74 -r 8938effd3d7c xen/common/grant_table.c
@@ -109,7 +109,8 @@ static unsigned inline int max_nr_maptra
 #define gfn_to_mfn_private(_d, _gfn) ({                     \
     p2m_type_t __p2mt;                                      \
     unsigned long __x;                                      \
-    __x = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(_d), _gfn, &__p2mt, 1));  \
+    __x = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(_d), _gfn, &__p2mt));  \
+    BUG_ON(p2m_is_shared(__p2mt)); /* XXX fixme */          \
     if ( !p2m_is_valid(__p2mt) )                            \
         __x = INVALID_MFN;                                  \
     __x; })
@@ -152,7 +153,12 @@ static int __get_paged_frame(unsigned lo
     if ( readonly )
         mfn = gfn_to_mfn(p2m, gfn, &p2mt);
     else
-        mfn = gfn_to_mfn_unshare(p2m, gfn, &p2mt, 1);
+    {
+        mfn = gfn_to_mfn_unshare(p2m, gfn, &p2mt);
+        BUG_ON(p2m_is_shared(p2mt));
+        /* XXX Here, and above in gfn_to_mfn_private, need to handle
+         * XXX failure to unshare. */
+    }
 
     if ( p2m_is_valid(p2mt) ) {
         *frame = mfn_x(mfn);
xen/common/memory.c +1 -1
diff -r 747679982b74 -r 8938effd3d7c xen/common/memory.c
@@ -363,7 +363,7 @@ static long memory_exchange(XEN_GUEST_HA
                 p2m_type_t p2mt;
 
                 /* Shared pages cannot be exchanged */
-                mfn = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(d), gmfn + k, &p2mt, 0));
+                mfn = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(d), gmfn + k, &p2mt));
                 if ( p2m_is_shared(p2mt) )
                 {
                     rc = -ENOMEM;
xen/include/asm-x86/mem_sharing.h +0 -1
diff -r 747679982b74 -r 8938effd3d7c xen/include/asm-x86/mem_sharing.h
@@ -34,7 +34,6 @@ int mem_sharing_nominate_page(struct p2m
                               unsigned long gfn,
                               int expected_refcnt,
                               shr_handle_t *phandle);
-#define MEM_SHARING_MUST_SUCCEED      (1<<0)
 #define MEM_SHARING_DESTROY_GFN       (1<<1)
 int mem_sharing_unshare_page(struct p2m_domain *p2m, 
                              unsigned long gfn, 
xen/include/asm-x86/p2m.h +13 -29

60 lines not on this page · raw message

author
#5 lore raw

PATCH 1/1 x86/mm/p2m: Make p2m interfaces take struct domain arguments

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302438 -3600
# Node ID b04da6168b3ae9de58a9fe1f4ec178479d5c6f71 # Parent 8938effd3d7cc9f008d45b6569ffb14bc8a91ed6 x86/mm/p2m: Make p2m interfaces take struct domain arguments.

As part of the nested HVM patch series, many p2m functions were changed to take pointers to p2m tables rather than to domains. This patch reverses that for almost all of them, which:
- gets rid of a lot of "p2m_get_hostp2m(d)" in code which really shouldn't have to know anything about how gfns become mfns.
- ties sharing and paging interfaces to a domain, which is what they actually act on, rather than a particular p2m table.

In developing this patch it became clear that memory-sharing and nested HVM are unlikely to work well together. I haven't tried to fix that here beyond adding some assertions around suspect paths (as this patch is big enough with just the interface changes)

xen/arch/x86/cpu/mcheck/vmce.c +2 -10
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/cpu/mcheck/vmce.c
@@ -577,7 +577,6 @@ int is_vmce_ready(struct mcinfo_bank *ba
 int unmmap_broken_page(struct domain *d, mfn_t mfn, unsigned long gfn)
 {
     mfn_t r_mfn;
-    struct p2m_domain *p2m;
     p2m_type_t pt;
 
     /* Always trust dom0's MCE handler will prevent future access */
@@ -590,18 +589,11 @@ int unmmap_broken_page(struct domain *d,
     if ( !is_hvm_domain(d) || !paging_mode_hap(d) )
         return -ENOSYS;
 
-    p2m = p2m_get_hostp2m(d);
-    ASSERT(p2m);
-
-    /* This only happen for PoD memory, which should be handled seperetely */
-    if (gfn > p2m->max_mapped_pfn)
-        return -EINVAL;
-
-    r_mfn = gfn_to_mfn_query(p2m, gfn, &pt);
+    r_mfn = gfn_to_mfn_query(d, gfn, &pt);
     if ( p2m_to_mask(pt) & P2M_UNMAP_TYPES)
     {
         ASSERT(mfn_x(r_mfn) == mfn_x(mfn));
-        p2m_change_type(p2m, gfn, pt, p2m_ram_broken);
+        p2m_change_type(d, gfn, pt, p2m_ram_broken);
         return 0;
     }
 
xen/arch/x86/debug.c +1 -1
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/debug.c
@@ -61,7 +61,7 @@ dbg_hvm_va2mfn(dbgva_t vaddr, struct dom
         return INVALID_MFN;
     }
 
-    mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(dp), gfn, &gfntype)); 
+    mfn = mfn_x(gfn_to_mfn(dp, gfn, &gfntype)); 
     if ( p2m_is_readonly(gfntype) && toaddr )
     {
         DBGP2("kdb:p2m_is_readonly: gfntype:%x\n", gfntype);
xen/arch/x86/domain.c +1 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/domain.c
@@ -163,9 +163,7 @@ void dump_pageframe_info(struct domain *
     }
 
     if ( is_hvm_domain(d) )
-    {
-        p2m_pod_dump_data(p2m_get_hostp2m(d));
-    }
+        p2m_pod_dump_data(d);
 
     spin_lock(&d->page_alloc_lock);
     page_list_for_each ( page, &d->xenpage_list )
xen/arch/x86/domctl.c +2 -2
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/domctl.c
@@ -987,7 +987,7 @@ long arch_do_domctl(
 
             ret = iomem_permit_access(d, mfn, mfn + nr_mfns - 1);
             for ( i = 0; i < nr_mfns; i++ )
-                set_mmio_p2m_entry(p2m_get_hostp2m(d), gfn+i, _mfn(mfn+i));
+                set_mmio_p2m_entry(d, gfn+i, _mfn(mfn+i));
         }
         else
         {
@@ -996,7 +996,7 @@ long arch_do_domctl(
                  gfn, mfn, nr_mfns);
 
             for ( i = 0; i < nr_mfns; i++ )
-                clear_mmio_p2m_entry(p2m_get_hostp2m(d), gfn+i);
+                clear_mmio_p2m_entry(d, gfn+i);
             ret = iomem_deny_access(d, mfn, mfn + nr_mfns - 1);
         }
 
xen/arch/x86/hvm/emulate.c +4 -6
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/hvm/emulate.c
@@ -55,7 +55,6 @@ static int hvmemul_do_io(
     paddr_t value = ram_gpa;
     int value_is_ptr = (p_data == NULL);
     struct vcpu *curr = current;
-    struct p2m_domain *p2m = p2m_get_hostp2m(curr->domain);
     ioreq_t *p = get_ioreq(curr);
     unsigned long ram_gfn = paddr_to_pfn(ram_gpa);
     p2m_type_t p2mt;
@@ -63,10 +62,10 @@ static int hvmemul_do_io(
     int rc;
 
     /* Check for paged out page */
-    ram_mfn = gfn_to_mfn_unshare(p2m, ram_gfn, &p2mt);
+    ram_mfn = gfn_to_mfn_unshare(curr->domain, ram_gfn, &p2mt);
     if ( p2m_is_paging(p2mt) )
     {
-        p2m_mem_paging_populate(p2m, ram_gfn);
+        p2m_mem_paging_populate(curr->domain, ram_gfn);
         return X86EMUL_RETRY;
     }
     if ( p2m_is_shared(p2mt) )
@@ -640,7 +639,6 @@ static int hvmemul_rep_movs(
     unsigned long saddr, daddr, bytes;
     paddr_t sgpa, dgpa;
     uint32_t pfec = PFEC_page_present;
-    struct p2m_domain *p2m = p2m_get_hostp2m(current->domain);
     p2m_type_t p2mt;
     int rc, df = !!(ctxt->regs->eflags & X86_EFLAGS_DF);
     char *buf;
@@ -671,12 +669,12 @@ static int hvmemul_rep_movs(
     if ( rc != X86EMUL_OKAY )
         return rc;
 
-    (void)gfn_to_mfn(p2m, sgpa >> PAGE_SHIFT, &p2mt);
+    (void)gfn_to_mfn(current->domain, sgpa >> PAGE_SHIFT, &p2mt);
     if ( !p2m_is_ram(p2mt) && !p2m_is_grant(p2mt) )
         return hvmemul_do_mmio(
             sgpa, reps, bytes_per_rep, dgpa, IOREQ_READ, df, NULL);
 
-    (void)gfn_to_mfn(p2m, dgpa >> PAGE_SHIFT, &p2mt);
+    (void)gfn_to_mfn(current->domain, dgpa >> PAGE_SHIFT, &p2mt);
     if ( !p2m_is_ram(p2mt) && !p2m_is_grant(p2mt) )
         return hvmemul_do_mmio(
             dgpa, reps, bytes_per_rep, sgpa, IOREQ_WRITE, df, NULL);
xen/arch/x86/hvm/hvm.c +23 -33

203 lines not on this page · raw message

xen/arch/x86/hvm/mtrr.c +1 -1
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/hvm/mtrr.c
@@ -392,7 +392,7 @@ uint32_t get_pat_flags(struct vcpu *v,
     {
         struct domain *d = v->domain;
         p2m_type_t p2mt;
-        gfn_to_mfn(p2m_get_hostp2m(d), paddr_to_pfn(gpaddr), &p2mt);
+        gfn_to_mfn(d, paddr_to_pfn(gpaddr), &p2mt);
         if (p2m_is_ram(p2mt))
             gdprintk(XENLOG_WARNING,
                     "Conflict occurs for a given guest l1e flags:%x "
xen/arch/x86/hvm/stdvga.c +3 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/hvm/stdvga.c
@@ -469,7 +469,7 @@ static int mmio_move(struct hvm_hw_stdvg
     int i;
     int sign = p->df ? -1 : 1;
     p2m_type_t p2mt;
-    struct p2m_domain *p2m = p2m_get_hostp2m(current->domain);
+    struct domain *d = current->domain;
 
     if ( p->data_is_ptr )
     {
@@ -482,7 +482,7 @@ static int mmio_move(struct hvm_hw_stdvg
                 if ( hvm_copy_to_guest_phys(data, &tmp, p->size) !=
                      HVMCOPY_okay )
                 {
-                    (void)gfn_to_mfn(p2m, data >> PAGE_SHIFT, &p2mt);
+                    (void)gfn_to_mfn(d, data >> PAGE_SHIFT, &p2mt);
                     /*
                      * The only case we handle is vga_mem <-> vga_mem.
                      * Anything else disables caching and leaves it to qemu-dm.
@@ -504,7 +504,7 @@ static int mmio_move(struct hvm_hw_stdvg
                 if ( hvm_copy_from_guest_phys(&tmp, data, p->size) !=
                      HVMCOPY_okay )
                 {
-                    (void)gfn_to_mfn(p2m, data >> PAGE_SHIFT, &p2mt);
+                    (void)gfn_to_mfn(d, data >> PAGE_SHIFT, &p2mt);
                     if ( (p2mt != p2m_mmio_dm) || (data < VGA_MEM_BASE) ||
                          ((data + p->size) > (VGA_MEM_BASE + VGA_MEM_SIZE)) )
                         return 0;
xen/arch/x86/hvm/svm/svm.c +4 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/hvm/svm/svm.c
@@ -242,7 +242,7 @@ static int svm_vmcb_restore(struct vcpu 
     {
         if ( c->cr0 & X86_CR0_PG )
         {
-            mfn = mfn_x(gfn_to_mfn(p2m, c->cr3 >> PAGE_SHIFT, &p2mt));
+            mfn = mfn_x(gfn_to_mfn(v->domain, c->cr3 >> PAGE_SHIFT, &p2mt));
             if ( !p2m_is_ram(p2mt) || !get_page(mfn_to_page(mfn), v->domain) )
             {
                 gdprintk(XENLOG_ERR, "Invalid CR3 value=0x%"PRIx64"\n",
@@ -1088,6 +1088,7 @@ static void svm_do_nested_pgfault(struct
     unsigned long gfn = gpa >> PAGE_SHIFT;
     mfn_t mfn;
     p2m_type_t p2mt;
+    p2m_access_t p2ma;
     struct p2m_domain *p2m = NULL;
 
     ret = hvm_hap_nested_page_fault(gpa, 0, ~0ul, 0, 0, 0, 0);
@@ -1104,7 +1105,7 @@ static void svm_do_nested_pgfault(struct
         p2m = p2m_get_p2m(v);
         _d.gpa = gpa;
         _d.qualification = 0;
-        _d.mfn = mfn_x(gfn_to_mfn_query(p2m, gfn, &_d.p2mt));
+        _d.mfn = mfn_x(gfn_to_mfn_type_p2m(p2m, gfn, &_d.p2mt, &p2ma, p2m_query));
         
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
@@ -1124,7 +1125,7 @@ static void svm_do_nested_pgfault(struct
     if ( p2m == NULL )
         p2m = p2m_get_p2m(v);
     /* Everything else is an error. */
-    mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
+    mfn = gfn_to_mfn_type_p2m(p2m, gfn, &p2mt, &p2ma, p2m_guest);
     gdprintk(XENLOG_ERR,
          "SVM violation gpa %#"PRIpaddr", mfn %#lx, type %i\n",
          gpa, mfn_x(mfn), p2mt);
xen/arch/x86/hvm/vmx/vmx.c +8 -11
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/hvm/vmx/vmx.c
@@ -476,8 +476,7 @@ static int vmx_restore_cr0_cr3(
     {
         if ( cr0 & X86_CR0_PG )
         {
-            mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(v->domain),
-                cr3 >> PAGE_SHIFT, &p2mt));
+            mfn = mfn_x(gfn_to_mfn(v->domain, cr3 >> PAGE_SHIFT, &p2mt));
             if ( !p2m_is_ram(p2mt) || !get_page(mfn_to_page(mfn), v->domain) )
             {
                 gdprintk(XENLOG_ERR, "Invalid CR3 value=0x%lx\n", cr3);
@@ -993,8 +992,7 @@ static void vmx_load_pdptrs(struct vcpu 
     if ( cr3 & 0x1fUL )
         goto crash;
 
-    mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(v->domain),
-        cr3 >> PAGE_SHIFT, &p2mt));
+    mfn = mfn_x(gfn_to_mfn(v->domain, cr3 >> PAGE_SHIFT, &p2mt));
     if ( !p2m_is_ram(p2mt) )
         goto crash;
 
@@ -1752,8 +1750,7 @@ static int vmx_alloc_vlapic_mapping(stru
     if ( apic_va == NULL )
         return -ENOMEM;
     share_xen_page_with_guest(virt_to_page(apic_va), d, XENSHARE_writable);
-    set_mmio_p2m_entry(
-        p2m_get_hostp2m(d), paddr_to_pfn(APIC_DEFAULT_PHYS_BASE),
+    set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE),
         _mfn(virt_to_mfn(apic_va)));
     d->arch.hvm_domain.vmx.apic_access_mfn = virt_to_mfn(apic_va);
 
@@ -1959,7 +1956,7 @@ static void ept_handle_violation(unsigne
     unsigned long gla, gfn = gpa >> PAGE_SHIFT;
     mfn_t mfn;
     p2m_type_t p2mt;
-    struct p2m_domain *p2m = p2m_get_hostp2m(current->domain);
+    struct domain *d = current->domain;
 
     if ( tb_init_done )
     {
@@ -1972,7 +1969,7 @@ static void ept_handle_violation(unsigne
 
         _d.gpa = gpa;
         _d.qualification = qualification;
-        _d.mfn = mfn_x(gfn_to_mfn_query(p2m, gfn, &_d.p2mt));
+        _d.mfn = mfn_x(gfn_to_mfn_query(d, gfn, &_d.p2mt));
         
         __trace_var(TRC_HVM_NPF, 0, sizeof(_d), &_d);
     }
@@ -1988,7 +1985,7 @@ static void ept_handle_violation(unsigne
         return;
 
     /* Everything else is an error. */
-    mfn = gfn_to_mfn_guest(p2m, gfn, &p2mt);
+    mfn = gfn_to_mfn_guest(d, gfn, &p2mt);
     gdprintk(XENLOG_ERR, "EPT violation %#lx (%c%c%c/%c%c%c), "
              "gpa %#"PRIpaddr", mfn %#lx, type %i.\n", 
              qualification, 
@@ -2000,7 +1997,7 @@ static void ept_handle_violation(unsigne
              (qualification & EPT_EFFECTIVE_EXEC) ? 'x' : '-',
              gpa, mfn_x(mfn), p2mt);
 
-    ept_walk_table(current->domain, gfn);
+    ept_walk_table(d, gfn);
 
     if ( qualification & EPT_GLA_VALID )
     {
@@ -2008,7 +2005,7 @@ static void ept_handle_violation(unsigne
         gdprintk(XENLOG_ERR, " --- GLA %#lx\n", gla);
     }
 
-    domain_crash(current->domain);
+    domain_crash(d);
 }
 
 static void vmx_failed_vmentry(unsigned int exit_reason,
xen/arch/x86/mm.c +15 -23
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm.c
@@ -1807,8 +1807,7 @@ static int mod_l1_entry(l1_pgentry_t *pl
     if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
     {
         /* Translate foreign guest addresses. */
-        mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(pg_dom),
-            l1e_get_pfn(nl1e), &p2mt));
+        mfn = mfn_x(gfn_to_mfn(pg_dom, l1e_get_pfn(nl1e), &p2mt));
         if ( !p2m_is_ram(p2mt) || unlikely(mfn == INVALID_MFN) )
             return -EINVAL;
         ASSERT((mfn & ~(PADDR_MASK >> PAGE_SHIFT)) == 0);
@@ -3481,13 +3480,13 @@ int do_mmu_update(
 
             req.ptr -= cmd;
             gmfn = req.ptr >> PAGE_SHIFT;
-            mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(pt_owner), gmfn, &p2mt));
+            mfn = mfn_x(gfn_to_mfn(pt_owner, gmfn, &p2mt));
             if ( !p2m_is_valid(p2mt) )
               mfn = INVALID_MFN;
 
             if ( p2m_is_paged(p2mt) )
             {
-                p2m_mem_paging_populate(p2m_get_hostp2m(pg_owner), gmfn);
+                p2m_mem_paging_populate(pg_owner, gmfn);
 
                 rc = -ENOENT;
                 break;
@@ -3519,13 +3518,11 @@ int do_mmu_update(
                 {
                     l1_pgentry_t l1e = l1e_from_intpte(req.val);
                     p2m_type_t l1e_p2mt;
-                    gfn_to_mfn(p2m_get_hostp2m(pg_owner),
-                        l1e_get_pfn(l1e), &l1e_p2mt);
+                    gfn_to_mfn(pg_owner, l1e_get_pfn(l1e), &l1e_p2mt);
 
                     if ( p2m_is_paged(l1e_p2mt) )
                     {
-                        p2m_mem_paging_populate(p2m_get_hostp2m(pg_owner),
-                            l1e_get_pfn(l1e));
+                        p2m_mem_paging_populate(pg_owner, l1e_get_pfn(l1e));
                         rc = -ENOENT;
                         break;
                     }
@@ -3543,7 +3540,7 @@ int do_mmu_update(
                         /* Unshare the page for RW foreign mappings */
                         if ( l1e_get_flags(l1e) & _PAGE_RW )
                         {
-                            rc = mem_sharing_unshare_page(p2m_get_hostp2m(pg_owner), 
+                            rc = mem_sharing_unshare_page(pg_owner, 
                                                           l1e_get_pfn(l1e), 
                                                           0);
                             if ( rc )
@@ -3561,12 +3558,11 @@ int do_mmu_update(
                 {
                     l2_pgentry_t l2e = l2e_from_intpte(req.val);
                     p2m_type_t l2e_p2mt;
-                    gfn_to_mfn(p2m_get_hostp2m(pg_owner), l2e_get_pfn(l2e), &l2e_p2mt);
+                    gfn_to_mfn(pg_owner, l2e_get_pfn(l2e), &l2e_p2mt);
 
                     if ( p2m_is_paged(l2e_p2mt) )
                     {
-                        p2m_mem_paging_populate(p2m_get_hostp2m(pg_owner),
-                            l2e_get_pfn(l2e));
+                        p2m_mem_paging_populate(pg_owner, l2e_get_pfn(l2e));
                         rc = -ENOENT;
                         break;
                     }
@@ -3590,12 +3586,11 @@ int do_mmu_update(
                 {
                     l3_pgentry_t l3e = l3e_from_intpte(req.val);
                     p2m_type_t l3e_p2mt;
-                    gfn_to_mfn(p2m_get_hostp2m(pg_owner), l3e_get_pfn(l3e), &l3e_p2mt);
+                    gfn_to_mfn(pg_owner, l3e_get_pfn(l3e), &l3e_p2mt);
 
                     if ( p2m_is_paged(l3e_p2mt) )
                     {
-                        p2m_mem_paging_populate(p2m_get_hostp2m(pg_owner),
-                            l3e_get_pfn(l3e));
+                        p2m_mem_paging_populate(pg_owner, l3e_get_pfn(l3e));
                         rc = -ENOENT;
                         break;
                     }
@@ -3619,13 +3614,11 @@ int do_mmu_update(
                 {
                     l4_pgentry_t l4e = l4e_from_intpte(req.val);
                     p2m_type_t l4e_p2mt;
-                    gfn_to_mfn(p2m_get_hostp2m(pg_owner),
-                        l4e_get_pfn(l4e), &l4e_p2mt);
+                    gfn_to_mfn(pg_owner, l4e_get_pfn(l4e), &l4e_p2mt);
 
                     if ( p2m_is_paged(l4e_p2mt) )
                     {
-                        p2m_mem_paging_populate(p2m_get_hostp2m(pg_owner),
-                            l4e_get_pfn(l4e));
+                        p2m_mem_paging_populate(pg_owner, l4e_get_pfn(l4e));
                         rc = -ENOENT;
                         break;
                     }
@@ -4002,7 +3995,7 @@ static int create_grant_p2m_mapping(uint
         p2mt = p2m_grant_map_ro;
     else
         p2mt = p2m_grant_map_rw;
-    rc = guest_physmap_add_entry(p2m_get_hostp2m(current->domain),
+    rc = guest_physmap_add_entry(current->domain,
                                  addr >> PAGE_SHIFT, frame, 0, p2mt);
     if ( rc )
         return GNTST_general_error;
@@ -4052,7 +4045,7 @@ static int replace_grant_p2m_mapping(
     if ( new_addr != 0 || (flags & GNTMAP_contains_pte) )
         return GNTST_general_error;
 
-    old_mfn = gfn_to_mfn(p2m_get_hostp2m(d), gfn, &type);
+    old_mfn = gfn_to_mfn(d, gfn, &type);
     if ( !p2m_is_grant(type) || mfn_x(old_mfn) != frame )
     {
         gdprintk(XENLOG_WARNING,
@@ -4651,8 +4644,7 @@ long arch_memory_op(int op, XEN_GUEST_HA
         {
             p2m_type_t p2mt;
 
-            xatp.idx = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(d),
-                                                xatp.idx, &p2mt));
+            xatp.idx = mfn_x(gfn_to_mfn_unshare(d, xatp.idx, &p2mt));
             /* If the page is still shared, exit early */
             if ( p2m_is_shared(p2mt) )
             {
xen/arch/x86/mm/guest_walk.c +5 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/guest_walk.c
@@ -92,12 +92,14 @@ static inline void *map_domain_gfn(struc
                                    p2m_type_t *p2mt,
                                    uint32_t *rc) 
 {
+    p2m_access_t a;
+
     /* Translate the gfn, unsharing if shared */
-    *mfn = gfn_to_mfn_unshare(p2m, gfn_x(gfn), p2mt);
+    *mfn = gfn_to_mfn_type_p2m(p2m, gfn_x(gfn), p2mt, &a, p2m_unshare);
     if ( p2m_is_paging(*p2mt) )
     {
-        p2m_mem_paging_populate(p2m, gfn_x(gfn));
-
+        ASSERT(!p2m_is_nestedp2m(p2m));
+        p2m_mem_paging_populate(p2m->domain, gfn_x(gfn));
         *rc = _PAGE_PAGED;
         return NULL;
     }
xen/arch/x86/mm/hap/guest_walk.c +8 -4
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/hap/guest_walk.c
@@ -54,13 +54,16 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PA
     mfn_t top_mfn;
     void *top_map;
     p2m_type_t p2mt;
+    p2m_access_t p2ma;
     walk_t gw;
 
     /* Get the top-level table's MFN */
-    top_mfn = gfn_to_mfn_unshare(p2m, cr3 >> PAGE_SHIFT, &p2mt);
+    top_mfn = gfn_to_mfn_type_p2m(p2m, cr3 >> PAGE_SHIFT, 
+                                  &p2mt, &p2ma, p2m_unshare);
     if ( p2m_is_paging(p2mt) )
     {
-        p2m_mem_paging_populate(p2m, cr3 >> PAGE_SHIFT);
+        ASSERT(!p2m_is_nestedp2m(p2m));
+        p2m_mem_paging_populate(p2m->domain, cr3 >> PAGE_SHIFT);
 
         pfec[0] = PFEC_page_paged;
         return INVALID_GFN;
@@ -89,10 +92,11 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PA
     if ( missing == 0 )
     {
         gfn_t gfn = guest_l1e_get_gfn(gw.l1e);
-        gfn_to_mfn_unshare(p2m, gfn_x(gfn), &p2mt);
+        gfn_to_mfn_type_p2m(p2m, gfn_x(gfn), &p2mt, &p2ma, p2m_unshare);
         if ( p2m_is_paging(p2mt) )
         {
-            p2m_mem_paging_populate(p2m, gfn_x(gfn));
+            ASSERT(!p2m_is_nestedp2m(p2m));
+            p2m_mem_paging_populate(p2m->domain, gfn_x(gfn));
 
             pfec[0] = PFEC_page_paged;
             return INVALID_GFN;
xen/arch/x86/mm/hap/hap.c +6 -9
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/hap/hap.c
@@ -71,7 +71,7 @@ static int hap_enable_vram_tracking(stru
 
     /* set l1e entries of P2M table to be read-only. */
     for (i = dirty_vram->begin_pfn; i < dirty_vram->end_pfn; i++)
-        p2m_change_type(p2m_get_hostp2m(d), i, p2m_ram_rw, p2m_ram_logdirty);
+        p2m_change_type(d, i, p2m_ram_rw, p2m_ram_logdirty);
 
     flush_tlb_mask(d->domain_dirty_cpumask);
     return 0;
@@ -91,7 +91,7 @@ static int hap_disable_vram_tracking(str
 
     /* set l1e entries of P2M table with normal mode */
     for (i = dirty_vram->begin_pfn; i < dirty_vram->end_pfn; i++)
-        p2m_change_type(p2m_get_hostp2m(d), i, p2m_ram_logdirty, p2m_ram_rw);
+        p2m_change_type(d, i, p2m_ram_logdirty, p2m_ram_rw);
 
     flush_tlb_mask(d->domain_dirty_cpumask);
     return 0;
@@ -107,7 +107,7 @@ static void hap_clean_vram_tracking(stru
 
     /* set l1e entries of P2M table to be read-only. */
     for (i = dirty_vram->begin_pfn; i < dirty_vram->end_pfn; i++)
-        p2m_change_type(p2m_get_hostp2m(d), i, p2m_ram_rw, p2m_ram_logdirty);
+        p2m_change_type(d, i, p2m_ram_rw, p2m_ram_logdirty);
 
     flush_tlb_mask(d->domain_dirty_cpumask);
 }
@@ -201,8 +201,7 @@ static int hap_enable_log_dirty(struct d
     hap_unlock(d);
 
     /* set l1e entries of P2M table to be read-only. */
-    p2m_change_entry_type_global(p2m_get_hostp2m(d),
-        p2m_ram_rw, p2m_ram_logdirty);
+    p2m_change_entry_type_global(d, p2m_ram_rw, p2m_ram_logdirty);
     flush_tlb_mask(d->domain_dirty_cpumask);
     return 0;
 }
@@ -214,16 +213,14 @@ static int hap_disable_log_dirty(struct 
     hap_unlock(d);
 
     /* set l1e entries of P2M table with normal mode */
-    p2m_change_entry_type_global(p2m_get_hostp2m(d),
-        p2m_ram_logdirty, p2m_ram_rw);
+    p2m_change_entry_type_global(d, p2m_ram_logdirty, p2m_ram_rw);
     return 0;
 }
 
 static void hap_clean_dirty_bitmap(struct domain *d)
 {
     /* set l1e entries of P2M table to be read-only. */
-    p2m_change_entry_type_global(p2m_get_hostp2m(d),
-        p2m_ram_rw, p2m_ram_logdirty);
+    p2m_change_entry_type_global(d, p2m_ram_rw, p2m_ram_logdirty);
     flush_tlb_mask(d->domain_dirty_cpumask);
 }
 
xen/arch/x86/mm/hap/nested_hap.c +3 -2
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/hap/nested_hap.c
@@ -123,9 +123,10 @@ nestedhap_walk_L0_p2m(struct p2m_domain 
 {
     mfn_t mfn;
     p2m_type_t p2mt;
+    p2m_access_t p2ma;
 
-    /* we use gfn_to_mfn_query() function to walk L0 P2M table */
-    mfn = gfn_to_mfn_query(p2m, L1_gpa >> PAGE_SHIFT, &p2mt);
+    /* walk L0 P2M table */
+    mfn = gfn_to_mfn_type_p2m(p2m, L1_gpa >> PAGE_SHIFT, &p2mt, &p2ma, p2m_query);
 
     if ( p2m_is_paging(p2mt) || p2m_is_shared(p2mt) || !p2m_is_ram(p2mt) )
         return NESTEDHVM_PAGEFAULT_ERROR;
xen/arch/x86/mm/mem_event.c +2 -2
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/mem_event.c
@@ -252,7 +252,7 @@ int mem_event_domctl(struct domain *d, x
             /* Get MFN of ring page */
             guest_get_eff_l1e(v, ring_addr, &l1e);
             gfn = l1e_get_pfn(l1e);
-            ring_mfn = gfn_to_mfn(p2m_get_hostp2m(dom_mem_event), gfn, &p2mt);
+            ring_mfn = gfn_to_mfn(dom_mem_event, gfn, &p2mt);
 
             rc = -EINVAL;
             if ( unlikely(!mfn_valid(mfn_x(ring_mfn))) )
@@ -261,7 +261,7 @@ int mem_event_domctl(struct domain *d, x
             /* Get MFN of shared page */
             guest_get_eff_l1e(v, shared_addr, &l1e);
             gfn = l1e_get_pfn(l1e);
-            shared_mfn = gfn_to_mfn(p2m_get_hostp2m(dom_mem_event), gfn, &p2mt);
+            shared_mfn = gfn_to_mfn(dom_mem_event, gfn, &p2mt);
 
             rc = -EINVAL;
             if ( unlikely(!mfn_valid(mfn_x(shared_mfn))) )
xen/arch/x86/mm/mem_paging.c +4 -5
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/mem_paging.c
@@ -29,34 +29,33 @@ int mem_paging_domctl(struct domain *d, 
                       XEN_GUEST_HANDLE(void) u_domctl)
 {
     int rc;
-    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     switch( mec->op )
     {
     case XEN_DOMCTL_MEM_EVENT_OP_PAGING_NOMINATE:
     {
         unsigned long gfn = mec->gfn;
-        rc = p2m_mem_paging_nominate(p2m, gfn);
+        rc = p2m_mem_paging_nominate(d, gfn);
     }
     break;
 
     case XEN_DOMCTL_MEM_EVENT_OP_PAGING_EVICT:
     {
         unsigned long gfn = mec->gfn;
-        rc = p2m_mem_paging_evict(p2m, gfn);
+        rc = p2m_mem_paging_evict(d, gfn);
     }
     break;
 
     case XEN_DOMCTL_MEM_EVENT_OP_PAGING_PREP:
     {
         unsigned long gfn = mec->gfn;
-        rc = p2m_mem_paging_prep(p2m, gfn);
+        rc = p2m_mem_paging_prep(d, gfn);
     }
     break;
 
     case XEN_DOMCTL_MEM_EVENT_OP_PAGING_RESUME:
     {
-        p2m_mem_paging_resume(p2m);
+        p2m_mem_paging_resume(d);
         rc = 0;
     }
     break;
xen/arch/x86/mm/mem_sharing.c +12 -17
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/mem_sharing.c
@@ -251,7 +251,6 @@ static void mem_sharing_audit(void)
             list_for_each(le, &e->gfns)
             {
                 struct domain *d;
-                struct p2m_domain *p2m;
                 p2m_type_t t;
                 mfn_t mfn;
 
@@ -263,8 +262,7 @@ static void mem_sharing_audit(void)
                             g->domain, g->gfn, mfn_x(e->mfn));
                     continue;
                 }
-                p2m = p2m_get_hostp2m(d);
-                mfn = gfn_to_mfn(p2m, g->gfn, &t); 
+                mfn = gfn_to_mfn(d, g->gfn, &t); 
                 if(mfn_x(mfn) != mfn_x(e->mfn))
                     MEM_SHARING_DEBUG("Incorrect P2M for d=%d, PFN=%lx."
                                       "Expecting MFN=%ld, got %ld\n",
@@ -376,7 +374,7 @@ int mem_sharing_debug_gfn(struct domain 
     mfn_t mfn;
     struct page_info *page;
 
-    mfn = gfn_to_mfn(p2m_get_hostp2m(d), gfn, &p2mt);
+    mfn = gfn_to_mfn(d, gfn, &p2mt);
     page = mfn_to_page(mfn);
 
     printk("Debug for domain=%d, gfn=%lx, ", 
@@ -486,7 +484,7 @@ int mem_sharing_debug_gref(struct domain
     return mem_sharing_debug_gfn(d, gfn); 
 }
 
-int mem_sharing_nominate_page(struct p2m_domain *p2m, 
+int mem_sharing_nominate_page(struct domain *d,
                               unsigned long gfn,
                               int expected_refcnt,
                               shr_handle_t *phandle)
@@ -498,12 +496,11 @@ int mem_sharing_nominate_page(struct p2m
     shr_handle_t handle;
     shr_hash_entry_t *hash_entry;
     struct gfn_info *gfn_info;
-    struct domain *d = p2m->domain;
 
     *phandle = 0UL;
 
     shr_lock(); 
-    mfn = gfn_to_mfn(p2m, gfn, &p2mt);
+    mfn = gfn_to_mfn(d, gfn, &p2mt);
 
     /* Check if mfn is valid */
     ret = -EINVAL;
@@ -541,7 +538,7 @@ int mem_sharing_nominate_page(struct p2m
     }
 
     /* Change the p2m type */
-    if(p2m_change_type(p2m, gfn, p2mt, p2m_ram_shared) != p2mt) 
+    if(p2m_change_type(d, gfn, p2mt, p2m_ram_shared) != p2mt) 
     {
         /* This is unlikely, as the type must have changed since we've checked
          * it a few lines above.
@@ -603,7 +600,7 @@ int mem_sharing_share_pages(shr_handle_t
         list_del(&gfn->list);
         d = get_domain_by_id(gfn->domain);
         BUG_ON(!d);
-        BUG_ON(set_shared_p2m_entry(p2m_get_hostp2m(d), gfn->gfn, se->mfn) == 0);
+        BUG_ON(set_shared_p2m_entry(d, gfn->gfn, se->mfn) == 0);
         put_domain(d);
         list_add(&gfn->list, &se->gfns);
         put_page_and_type(cpage);
@@ -622,7 +619,7 @@ err_out:
     return ret;
 }
 
-int mem_sharing_unshare_page(struct p2m_domain *p2m,
+int mem_sharing_unshare_page(struct domain *d,
                              unsigned long gfn, 
                              uint16_t flags)
 {
@@ -635,13 +632,12 @@ int mem_sharing_unshare_page(struct p2m_
     struct gfn_info *gfn_info = NULL;
     shr_handle_t handle;
     struct list_head *le;
-    struct domain *d = p2m->domain;
 
     mem_sharing_audit();
     /* Remove the gfn_info from the list */
     shr_lock();
     
-    mfn = gfn_to_mfn(p2m, gfn, &p2mt);
+    mfn = gfn_to_mfn(d, gfn, &p2mt);
     
     /* Has someone already unshared it? */
     if (!p2m_is_shared(p2mt)) {
@@ -707,7 +703,7 @@ gfn_found:
     unmap_domain_page(s);
     unmap_domain_page(t);
 
-    BUG_ON(set_shared_p2m_entry(p2m, gfn, page_to_mfn(page)) == 0);
+    BUG_ON(set_shared_p2m_entry(d, gfn, page_to_mfn(page)) == 0);
     put_page_and_type(old_page);
 
 private_page_found:    
@@ -718,7 +714,7 @@ private_page_found:
     else
         atomic_dec(&nr_saved_mfns);
 
-    if(p2m_change_type(p2m, gfn, p2m_ram_shared, p2m_ram_rw) != 
+    if(p2m_change_type(d, gfn, p2m_ram_shared, p2m_ram_rw) != 
                                                 p2m_ram_shared) 
     {
         printk("Could not change p2m type.\n");
@@ -751,7 +747,7 @@ int mem_sharing_domctl(struct domain *d,
             shr_handle_t handle;
             if(!mem_sharing_enabled(d))
                 return -EINVAL;
-            rc = mem_sharing_nominate_page(p2m_get_hostp2m(d), gfn, 0, &handle);
+            rc = mem_sharing_nominate_page(d, gfn, 0, &handle);
             mec->u.nominate.handle = handle;
             mem_sharing_audit();
         }
@@ -767,8 +763,7 @@ int mem_sharing_domctl(struct domain *d,
                 return -EINVAL;
             if(mem_sharing_gref_to_gfn(d, gref, &gfn) < 0)
                 return -EINVAL;
-            rc = mem_sharing_nominate_page(p2m_get_hostp2m(d),
-                gfn, 3, &handle);
+            rc = mem_sharing_nominate_page(d, gfn, 3, &handle);
             mec->u.nominate.handle = handle;
             mem_sharing_audit();
         }
xen/arch/x86/mm/p2m-pod.c +8 -8
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/p2m-pod.c
@@ -536,7 +536,7 @@ p2m_pod_decrease_reservation(struct doma
     {
         p2m_type_t t;
 
-        gfn_to_mfn_query(p2m, gpfn + i, &t);
+        gfn_to_mfn_query(d, gpfn + i, &t);
 
         if ( t == p2m_populate_on_demand )
             pod++;
@@ -576,7 +576,7 @@ p2m_pod_decrease_reservation(struct doma
         mfn_t mfn;
         p2m_type_t t;
 
-        mfn = gfn_to_mfn_query(p2m, gpfn + i, &t);
+        mfn = gfn_to_mfn_query(d, gpfn + i, &t);
         if ( t == p2m_populate_on_demand )
         {
             set_p2m_entry(p2m, gpfn + i, _mfn(INVALID_MFN), 0, p2m_invalid, p2m->default_access);
@@ -624,9 +624,9 @@ out:
     return ret;
 }
 
-void
-p2m_pod_dump_data(struct p2m_domain *p2m)
+void p2m_pod_dump_data(struct domain *d)
 {
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
     printk("    PoD entries=%d cachesize=%d\n",
            p2m->pod.entry_count, p2m->pod.count);
 }
@@ -657,7 +657,7 @@ p2m_pod_zero_check_superpage(struct p2m_
     for ( i=0; i<SUPERPAGE_PAGES; i++ )
     {
         
-        mfn = gfn_to_mfn_query(p2m, gfn + i, &type);
+        mfn = gfn_to_mfn_query(d, gfn + i, &type);
 
         if ( i == 0 )
         {
@@ -785,7 +785,7 @@ p2m_pod_zero_check(struct p2m_domain *p2
     /* First, get the gfn list, translate to mfns, and map the pages. */
     for ( i=0; i<count; i++ )
     {
-        mfns[i] = gfn_to_mfn_query(p2m, gfns[i], types + i);
+        mfns[i] = gfn_to_mfn_query(d, gfns[i], types + i);
         /* If this is ram, and not a pagetable or from the xen heap, and probably not mapped
            elsewhere, map it; otherwise, skip. */
         if ( p2m_is_ram(types[i])
@@ -924,7 +924,7 @@ p2m_pod_emergency_sweep(struct p2m_domai
     /* FIXME: Figure out how to avoid superpages */
     for ( i=p2m->pod.reclaim_single; i > 0 ; i-- )
     {
-        gfn_to_mfn_query(p2m, i, &t );
+        gfn_to_mfn_query(p2m->domain, i, &t );
         if ( p2m_is_ram(t) )
         {
             gfns[j] = i;
@@ -1113,7 +1113,7 @@ guest_physmap_mark_populate_on_demand(st
     /* Make sure all gpfns are unused */
     for ( i = 0; i < (1UL << order); i++ )
     {
-        omfn = gfn_to_mfn_query(p2m, gfn + i, &ot);
+        omfn = gfn_to_mfn_query(d, gfn + i, &ot);
         if ( p2m_is_ram(ot) )
         {
             printk("%s: gfn_to_mfn returned type %d!\n",
xen/arch/x86/mm/p2m-pt.c +5 -6
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/p2m-pt.c
@@ -699,10 +699,9 @@ out:
     return mfn;
 }
 
-
 static mfn_t
-p2m_gfn_to_mfn(struct p2m_domain *p2m, unsigned long gfn, p2m_type_t *t, p2m_access_t *a,
-               p2m_query_t q)
+p2m_gfn_to_mfn(struct p2m_domain *p2m, unsigned long gfn, 
+               p2m_type_t *t, p2m_access_t *a, p2m_query_t q)
 {
     mfn_t mfn;
     paddr_t addr = ((paddr_t)gfn) << PAGE_SHIFT;
@@ -719,8 +718,6 @@ p2m_gfn_to_mfn(struct p2m_domain *p2m, u
     /* Not implemented except with EPT */
     *a = p2m_access_rwx; 
 
-    mfn = pagetable_get_mfn(p2m_get_pagetable(p2m));
-
     if ( gfn > p2m->max_mapped_pfn )
         /* This pfn is higher than the highest the p2m map currently holds */
         return _mfn(INVALID_MFN);
@@ -729,6 +726,8 @@ p2m_gfn_to_mfn(struct p2m_domain *p2m, u
     if ( p2m == p2m_get_hostp2m(current->domain) )
         return p2m_gfn_to_mfn_current(p2m, gfn, t, a, q);
 
+    mfn = pagetable_get_mfn(p2m_get_pagetable(p2m));
+
 #if CONFIG_PAGING_LEVELS >= 4
     {
         l4_pgentry_t *l4e = map_domain_page(mfn_x(mfn));
@@ -1081,7 +1080,7 @@ void audit_p2m(struct p2m_domain *p2m, i
 
         if ( test_linear && (gfn <= p2m->max_mapped_pfn) )
         {
-            lp2mfn = mfn_x(gfn_to_mfn_query(p2m, gfn, &type));
+            lp2mfn = mfn_x(gfn_to_mfn_type_p2m(p2m, gfn, &type, p2m_query));
             if ( lp2mfn != mfn_x(p2mfn) )
             {
                 P2M_PRINTK("linear mismatch gfn %#lx -> mfn %#lx "
xen/arch/x86/mm/p2m.c +44 -33

250 lines not on this page · raw message

xen/arch/x86/mm/shadow/common.c +2 -2
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/shadow/common.c
@@ -3712,7 +3712,7 @@ int shadow_track_dirty_vram(struct domai
 
         /* Iterate over VRAM to track dirty bits. */
         for ( i = 0; i < nr; i++ ) {
-            mfn_t mfn = gfn_to_mfn(p2m, begin_pfn + i, &t);
+            mfn_t mfn = gfn_to_mfn(d, begin_pfn + i, &t);
             struct page_info *page;
             int dirty = 0;
             paddr_t sl1ma = dirty_vram->sl1ma[i];
@@ -3797,7 +3797,7 @@ int shadow_track_dirty_vram(struct domai
                 /* was clean for more than two seconds, try to disable guest
                  * write access */
                 for ( i = begin_pfn; i < end_pfn; i++ ) {
-                    mfn_t mfn = gfn_to_mfn(p2m, i, &t);
+                    mfn_t mfn = gfn_to_mfn(d, i, &t);
                     if (mfn_x(mfn) != INVALID_MFN)
                         flush_tlb |= sh_remove_write_access(d->vcpu[0], mfn, 1, 0);
                 }
xen/arch/x86/mm/shadow/multi.c +20 -26

193 lines not on this page · raw message

xen/arch/x86/mm/shadow/types.h +2 -2
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/mm/shadow/types.h
@@ -193,9 +193,9 @@ static inline shadow_l4e_t shadow_l4e_fr
 
  /* Override gfn_to_mfn to work with gfn_t */
 #undef gfn_to_mfn_query
-#define gfn_to_mfn_query(d, g, t) _gfn_to_mfn_type((d), gfn_x(g), (t), p2m_query)
+#define gfn_to_mfn_query(d, g, t) gfn_to_mfn_type((d), gfn_x(g), (t), p2m_query)
 #undef gfn_to_mfn_guest
-#define gfn_to_mfn_guest(d, g, t) _gfn_to_mfn_type((d), gfn_x(g), (t), p2m_guest)
+#define gfn_to_mfn_guest(d, g, t) gfn_to_mfn_type((d), gfn_x(g), (t), p2m_guest)
 
 /* The shadow types needed for the various levels. */
 
xen/arch/x86/msi.c +1 -1
diff -r 8938effd3d7c -r b04da6168b3a xen/arch/x86/msi.c
@@ -663,7 +663,7 @@ static int msix_capability_init(struct p
             WARN();
 
         if ( dev->domain )
-            p2m_change_entry_type_global(p2m_get_hostp2m(dev->domain),
+            p2m_change_entry_type_global(dev->domain,
                                          p2m_mmio_direct, p2m_mmio_direct);
         if ( !dev->domain || !paging_mode_translate(dev->domain) )
         {
xen/common/grant_table.c +4 -6
diff -r 8938effd3d7c -r b04da6168b3a xen/common/grant_table.c
@@ -109,7 +109,7 @@ static unsigned inline int max_nr_maptra
 #define gfn_to_mfn_private(_d, _gfn) ({                     \
     p2m_type_t __p2mt;                                      \
     unsigned long __x;                                      \
-    __x = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(_d), _gfn, &__p2mt));  \
+    __x = mfn_x(gfn_to_mfn_unshare((_d), (_gfn), &__p2mt)); \
     BUG_ON(p2m_is_shared(__p2mt)); /* XXX fixme */          \
     if ( !p2m_is_valid(__p2mt) )                            \
         __x = INVALID_MFN;                                  \
@@ -145,16 +145,14 @@ static int __get_paged_frame(unsigned lo
 {
     int rc = GNTST_okay;
 #if defined(P2M_PAGED_TYPES) || defined(P2M_SHARED_TYPES)
-    struct p2m_domain *p2m;
     p2m_type_t p2mt;
     mfn_t mfn;
 
-    p2m = p2m_get_hostp2m(rd);
     if ( readonly )
-        mfn = gfn_to_mfn(p2m, gfn, &p2mt);
+        mfn = gfn_to_mfn(rd, gfn, &p2mt);
     else
     {
-        mfn = gfn_to_mfn_unshare(p2m, gfn, &p2mt);
+        mfn = gfn_to_mfn_unshare(rd, gfn, &p2mt);
         BUG_ON(p2m_is_shared(p2mt));
         /* XXX Here, and above in gfn_to_mfn_private, need to handle
          * XXX failure to unshare. */
@@ -164,7 +162,7 @@ static int __get_paged_frame(unsigned lo
         *frame = mfn_x(mfn);
         if ( p2m_is_paging(p2mt) )
         {
-            p2m_mem_paging_populate(p2m, gfn);
+            p2m_mem_paging_populate(rd, gfn);
             rc = GNTST_eagain;
         }
     } else {
xen/common/memory.c +3 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/common/memory.c
@@ -162,11 +162,11 @@ int guest_remove_page(struct domain *d, 
     unsigned long mfn;
 
 #ifdef CONFIG_X86
-    mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(d), gmfn, &p2mt)); 
+    mfn = mfn_x(gfn_to_mfn(d, gmfn, &p2mt)); 
     if ( unlikely(p2m_is_paging(p2mt)) )
     {
         guest_physmap_remove_page(d, gmfn, mfn, 0);
-        p2m_mem_paging_drop_page(p2m_get_hostp2m(d), gmfn);
+        p2m_mem_paging_drop_page(d, gmfn);
         return 1;
     }
 #else
@@ -363,7 +363,7 @@ static long memory_exchange(XEN_GUEST_HA
                 p2m_type_t p2mt;
 
                 /* Shared pages cannot be exchanged */
-                mfn = mfn_x(gfn_to_mfn_unshare(p2m_get_hostp2m(d), gmfn + k, &p2mt));
+                mfn = mfn_x(gfn_to_mfn_unshare(d, gmfn + k, &p2mt));
                 if ( p2m_is_shared(p2mt) )
                 {
                     rc = -ENOMEM;
xen/common/tmem_xen.c +1 -1
diff -r 8938effd3d7c -r b04da6168b3a xen/common/tmem_xen.c
@@ -109,7 +109,7 @@ static inline void *cli_get_page(tmem_cl
     struct page_info *page;
     int ret;
 
-    cli_mfn = mfn_x(gfn_to_mfn(p2m_get_hostp2m(current->domain), cmfn, &t));
+    cli_mfn = mfn_x(gfn_to_mfn(current->domain, cmfn, &t));
     if ( t != p2m_ram_rw || !mfn_valid(cli_mfn) )
             return NULL;
     page = mfn_to_page(cli_mfn);
xen/include/asm-x86/guest_pt.h +1 -1
diff -r 8938effd3d7c -r b04da6168b3a xen/include/asm-x86/guest_pt.h
@@ -53,7 +53,7 @@ gfn_to_paddr(gfn_t gfn)
 
 /* Override gfn_to_mfn to work with gfn_t */
 #undef gfn_to_mfn
-#define gfn_to_mfn(d, g, t) _gfn_to_mfn_type((d), gfn_x(g), (t), p2m_alloc)
+#define gfn_to_mfn(d, g, t) gfn_to_mfn_type((d), gfn_x(g), (t), p2m_alloc)
 
 
 /* Types of the guest's page tables and access functions for them */
xen/include/asm-x86/mem_sharing.h +2 -3
diff -r 8938effd3d7c -r b04da6168b3a xen/include/asm-x86/mem_sharing.h
@@ -30,16 +30,15 @@
 typedef uint64_t shr_handle_t; 
 
 unsigned int mem_sharing_get_nr_saved_mfns(void);
-int mem_sharing_nominate_page(struct p2m_domain *p2m, 
+int mem_sharing_nominate_page(struct domain *d, 
                               unsigned long gfn,
                               int expected_refcnt,
                               shr_handle_t *phandle);
 #define MEM_SHARING_DESTROY_GFN       (1<<1)
-int mem_sharing_unshare_page(struct p2m_domain *p2m, 
+int mem_sharing_unshare_page(struct domain *d, 
                              unsigned long gfn, 
                              uint16_t flags);
 int mem_sharing_sharing_resume(struct domain *d);
-int mem_sharing_cache_resize(struct p2m_domain *p2m, int new_size);
 int mem_sharing_domctl(struct domain *d, 
                        xen_domctl_mem_sharing_op_t *mec);
 void mem_sharing_init(void);
xen/include/asm-x86/p2m.h +49 -47

204 lines not on this page · raw message

author
#6 lore raw

PATCH 1/1 x86/mm/p2m: paging_p2m_ga_to_gfn() doesn't need so many arguments

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302438 -3600
# Node ID fdcdfbf227bc11494ecdd820ba2fe6d1a4223b99
# Parent b04da6168b3ae9de58a9fe1f4ec178479d5c6f71 x86/mm/p2m: paging_p2m_ga_to_gfn() doesn't need so many arguments

It has only one caller and is always called with p2m == hostp2m and mode == hostmode. Also, since it's only called from nested HAP code, remove the check of paging_mode_hap(). Then rename it to reflect its new interface.

xen/arch/x86/mm/hap/nested_hap.c +5 -9
diff -r b04da6168b3a -r fdcdfbf227bc xen/arch/x86/mm/hap/nested_hap.c
@@ -143,17 +143,15 @@ nestedhap_walk_L0_p2m(struct p2m_domain 
  * L1_gpa. The result value tells what to do next.
  */
 static int
-nestedhap_walk_L1_p2m(struct vcpu *v, struct p2m_domain *p2m,
-    paddr_t L2_gpa, paddr_t *L1_gpa)
+nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa)
 {
     uint32_t pfec;
     unsigned long nested_cr3, gfn;
-    const struct paging_mode *mode = paging_get_hostmode(v);
     
     nested_cr3 = nhvm_vcpu_hostcr3(v);
 
-    /* walk the guest table */
-    gfn = paging_p2m_ga_to_gfn(v, p2m, mode, nested_cr3, L2_gpa, &pfec);
+    /* Walk the guest-supplied NPT table, just as if it were a pagetable */
+    gfn = paging_ga_to_gfn_cr3(v, nested_cr3, L2_gpa, &pfec);
 
     if ( gfn == INVALID_GFN ) 
         return NESTEDHVM_PAGEFAULT_INJECT;
@@ -178,10 +176,8 @@ nestedhvm_hap_nested_page_fault(struct v
     p2m = p2m_get_hostp2m(d); /* L0 p2m */
     nested_p2m = p2m_get_nestedp2m(v, nhvm_vcpu_hostcr3(v));
 
-    /* walk the L1 P2M table, note we have to pass p2m
-     * and not nested_p2m here or we fail the walk forever,
-     * otherwise. */
-    rv = nestedhap_walk_L1_p2m(v, p2m, L2_gpa, &L1_gpa);
+    /* walk the L1 P2M table */
+    rv = nestedhap_walk_L1_p2m(v, L2_gpa, &L1_gpa);
 
     /* let caller to handle these two cases */
     switch (rv) {
xen/include/asm-x86/paging.h +6 -13
diff -r b04da6168b3a -r fdcdfbf227bc xen/include/asm-x86/paging.h
@@ -265,26 +265,19 @@ unsigned long paging_gva_to_gfn(struct v
                                 unsigned long va,
                                 uint32_t *pfec);
 
-/* Translates a guest virtual address to guest physical address
- * where the specified cr3 is translated to host physical address
- * using the specified p2m table.
- * This allows to do page walks in the guest or even in the nested guest.
- * It returns the guest's gfn or the nested guest's gfn.
+/* Translate a guest address using a particular CR3 value.  This is used
+ * to by nested HAP code, to walk the guest-supplied NPT tables as if
+ * they were pagetables.
  * Use 'paddr_t' for the guest address so it won't overflow when
  * guest or nested guest is in 32bit PAE mode.
  */
-static inline unsigned long paging_p2m_ga_to_gfn(struct vcpu *v,
-                                                 struct p2m_domain *p2m,
-                                                 const struct paging_mode *mode,
+static inline unsigned long paging_ga_to_gfn_cr3(struct vcpu *v,
                                                  unsigned long cr3,
                                                  paddr_t ga,
                                                  uint32_t *pfec)
 {
-    if ( is_hvm_domain(v->domain) && paging_mode_hap(v->domain) )
-        return mode->p2m_ga_to_gfn(v, p2m, cr3, ga, pfec);
-
-    /* shadow paging */
-    return paging_gva_to_gfn(v, ga, pfec);
+    struct p2m_domain *p2m = v->domain->arch.p2m;
+    return paging_get_hostmode(v)->p2m_ga_to_gfn(v, p2m, cr3, ga, pfec);
 }
 
 /* Update all the things that are derived from the guest's CR3.
author
#7 lore raw

PATCH 1/1 x86/mm: Fix memory-sharing code's locking discipline

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302438 -3600
# Node ID 078133a7063061dba94cea4ec207d6c60add5633 # Parent fdcdfbf227bc11494ecdd820ba2fe6d1a4223b99 x86/mm: Fix memory-sharing code's locking discipline.

memshr_audit is sometimes called with the shr_lock held. Make it so for every call.

Move the unsharing loop in p2m_teardown out of the p2m_lock to avoid deadlocks.

xen/arch/x86/mm/mem_sharing.c +7 -13
diff -r fdcdfbf227bc -r 078133a70630 xen/arch/x86/mm/mem_sharing.c
@@ -224,7 +224,7 @@ static void mem_sharing_audit(void)
     int bucket;
     struct page_info *pg;
 
-    shr_lock();
+    ASSERT(shr_locked_by_me());
 
     for(bucket=0; bucket < SHR_HASH_LENGTH; bucket++)
     {
@@ -284,8 +284,6 @@ static void mem_sharing_audit(void)
             e = e->next;
         }
     }
-
-    shr_unlock();
 }
 #endif
 
@@ -633,10 +631,10 @@ int mem_sharing_unshare_page(struct doma
     shr_handle_t handle;
     struct list_head *le;
 
+    shr_lock();
     mem_sharing_audit();
+    
     /* Remove the gfn_info from the list */
-    shr_lock();
-    
     mfn = gfn_to_mfn(d, gfn, &p2mt);
     
     /* Has someone already unshared it? */
@@ -736,7 +734,6 @@ int mem_sharing_domctl(struct domain *d,
         case XEN_DOMCTL_MEM_SHARING_OP_CONTROL:
         {
             d->arch.hvm_domain.mem_sharing_enabled = mec->u.enable;
-            mem_sharing_audit();
             rc = 0;
         }
         break;
@@ -749,7 +746,6 @@ int mem_sharing_domctl(struct domain *d,
                 return -EINVAL;
             rc = mem_sharing_nominate_page(d, gfn, 0, &handle);
             mec->u.nominate.handle = handle;
-            mem_sharing_audit();
         }
         break;
 
@@ -765,7 +761,6 @@ int mem_sharing_domctl(struct domain *d,
                 return -EINVAL;
             rc = mem_sharing_nominate_page(d, gfn, 3, &handle);
             mec->u.nominate.handle = handle;
-            mem_sharing_audit();
         }
         break;
 
@@ -774,7 +769,6 @@ int mem_sharing_domctl(struct domain *d,
             shr_handle_t sh = mec->u.share.source_handle;
             shr_handle_t ch = mec->u.share.client_handle;
             rc = mem_sharing_share_pages(sh, ch); 
-            mem_sharing_audit();
         }
         break;
 
@@ -782,7 +776,6 @@ int mem_sharing_domctl(struct domain *d,
         {
             if(!mem_sharing_enabled(d))
                 return -EINVAL;
-            mem_sharing_audit();
             rc = mem_sharing_sharing_resume(d);
         }
         break;
@@ -791,7 +784,6 @@ int mem_sharing_domctl(struct domain *d,
         {
             unsigned long gfn = mec->u.debug.u.gfn;
             rc = mem_sharing_debug_gfn(d, gfn);
-            mem_sharing_audit();
         }
         break;
 
@@ -799,7 +791,6 @@ int mem_sharing_domctl(struct domain *d,
         {
             unsigned long mfn = mec->u.debug.u.mfn;
             rc = mem_sharing_debug_mfn(mfn);
-            mem_sharing_audit();
         }
         break;
 
@@ -807,7 +798,6 @@ int mem_sharing_domctl(struct domain *d,
         {
             grant_ref_t gref = mec->u.debug.u.gref;
             rc = mem_sharing_debug_gref(d, gref);
-            mem_sharing_audit();
         }
         break;
 
@@ -816,6 +806,10 @@ int mem_sharing_domctl(struct domain *d,
             break;
     }
 
+    shr_lock();
+    mem_sharing_audit();
+    shr_unlock();
+
     return rc;
 }
 
xen/arch/x86/mm/p2m.c +3 -3
diff -r fdcdfbf227bc -r 078133a70630 xen/arch/x86/mm/p2m.c
@@ -304,12 +304,10 @@ void p2m_teardown(struct p2m_domain *p2m
     if (p2m == NULL)
         return;
 
-    p2m_lock(p2m);
-
 #ifdef __x86_64__
     for ( gfn=0; gfn < p2m->max_mapped_pfn; gfn++ )
     {
-        mfn = p2m->get_entry(p2m, gfn, &t, &a, p2m_query);
+        mfn = gfn_to_mfn_type_p2m(p2m, gfn, &t, &a, p2m_query);
         if ( mfn_valid(mfn) && (t == p2m_ram_shared) )
         {
             ASSERT(!p2m_is_nestedp2m(p2m));
@@ -319,6 +317,8 @@ void p2m_teardown(struct p2m_domain *p2m
     }
 #endif
 
+    p2m_lock(p2m);
+
     p2m->phys_table = pagetable_null();
 
     while ( (pg = page_list_remove_head(&p2m->pages)) )
author
#8 lore raw

PATCH 1/1 x86/mm/p2m: Remove recursive-locking code from set_shared_p2m_entry()

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302439 -3600
# Node ID 6d09021a0e2dfd15816e2c0f62fcc9b01c1a7562
# Parent 078133a7063061dba94cea4ec207d6c60add5633 x86/mm/p2m: Remove recursive-locking code from set_shared_p2m_entry().

It should no longer be needed now that the shr_lock discipline is fixed.

xen/arch/x86/mm/p2m.c +2 -5
diff -r 078133a70630 -r 6d09021a0e2d xen/arch/x86/mm/p2m.c
@@ -609,7 +609,6 @@ set_shared_p2m_entry(struct domain *d, u
 {
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
     int rc = 0;
-    int need_lock = !p2m_locked_by_me(p2m);
     p2m_type_t ot;
     mfn_t omfn;
 
@@ -625,11 +624,9 @@ set_shared_p2m_entry(struct domain *d, u
     set_gpfn_from_mfn(mfn_x(omfn), INVALID_M2P_ENTRY);
 
     P2M_DEBUG("set shared %lx %lx\n", gfn, mfn_x(mfn));
-    if ( need_lock ) 
-        p2m_lock(p2m);
+    p2m_lock(p2m);
     rc = set_p2m_entry(p2m, gfn, mfn, 0, p2m_ram_shared, p2m->default_access);
-    if ( need_lock ) 
-        p2m_unlock(p2m);
+    p2m_unlock(p2m);
     if ( 0 == rc )
         gdprintk(XENLOG_ERR,
             "set_mmio_p2m_entry: set_p2m_entry failed! mfn=%08lx\n",
author
#9 lore raw

PATCH 1/1 x86/mm/p2m: Fix locking discipline around p2m updates

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302439 -3600
# Node ID f2b42201dc1a2fea7f0dbf36c511b780caf55c85 # Parent 6d09021a0e2dfd15816e2c0f62fcc9b01c1a7562 x86/mm/p2m: Fix locking discipline around p2m updates.

Direct callers of the p2m setting functions must hold the p2m lock.

xen/arch/x86/mm/hap/nested_hap.c +4 -1
diff -r 6d09021a0e2d -r f2b42201dc1a xen/arch/x86/mm/hap/nested_hap.c
@@ -103,9 +103,12 @@ nestedhap_fix_p2m(struct p2m_domain *p2m
     ASSERT(p2m);
     ASSERT(p2m->set_entry);
 
-    rv = p2m->set_entry(p2m, L2_gpa >> PAGE_SHIFT,
+    p2m_lock(p2m);
+    rv = set_p2m_entry(p2m, L2_gpa >> PAGE_SHIFT,
                          page_to_mfn(maddr_to_page(L0_gpa)),
                          0 /*4K*/, p2mt, p2ma);
+    p2m_unlock(p2m);
+
     if (rv == 0) {
         gdprintk(XENLOG_ERR,
 		"failed to set entry for 0x%"PRIx64" -> 0x%"PRIx64"\n",
xen/arch/x86/mm/p2m.c +2 -0
diff -r 6d09021a0e2d -r f2b42201dc1a xen/arch/x86/mm/p2m.c
@@ -148,6 +148,8 @@ int set_p2m_entry(struct p2m_domain *p2m
     unsigned int order;
     int rc = 1;
 
+    ASSERT(p2m_locked_by_me(p2m));
+
     while ( todo )
     {
         if ( hap_enabled(d) )
author
#10 lore raw

PATCH 1/1 x86/mm/p2m: Fix locking discipline around p2m lookups

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302439 -3600
# Node ID ba2902017f010e1a3a756f4053175d71027244e8 # Parent f2b42201dc1a2fea7f0dbf36c511b780caf55c85 x86/mm/p2m: Fix locking discipline around p2m lookups.

All gfn_to_mfn* functions except _query() might take the p2m lock, so can't be called with a p2m, shadow, hap or log_dirty lock held. The remaining offender is the memory sharing code, which calls _unshare() from inside the pagetable walker! Fixing that is too big for a cleanup patch like this one.

xen/arch/x86/hvm/mtrr.c +1 -1
diff -r f2b42201dc1a -r ba2902017f01 xen/arch/x86/hvm/mtrr.c
@@ -392,7 +392,7 @@ uint32_t get_pat_flags(struct vcpu *v,
     {
         struct domain *d = v->domain;
         p2m_type_t p2mt;
-        gfn_to_mfn(d, paddr_to_pfn(gpaddr), &p2mt);
+        gfn_to_mfn_query(d, paddr_to_pfn(gpaddr), &p2mt);
         if (p2m_is_ram(p2mt))
             gdprintk(XENLOG_WARNING,
                     "Conflict occurs for a given guest l1e flags:%x "
xen/arch/x86/mm/p2m.c +3 -3
diff -r f2b42201dc1a -r ba2902017f01 xen/arch/x86/mm/p2m.c
@@ -574,7 +574,7 @@ set_mmio_p2m_entry(struct domain *d, uns
     if ( 0 == rc )
         gdprintk(XENLOG_ERR,
             "set_mmio_p2m_entry: set_p2m_entry failed! mfn=%08lx\n",
-            mfn_x(gfn_to_mfn(d, gfn, &ot)));
+            mfn_x(gfn_to_mfn_query(d, gfn, &ot)));
     return rc;
 }
 
@@ -631,8 +631,8 @@ set_shared_p2m_entry(struct domain *d, u
     p2m_unlock(p2m);
     if ( 0 == rc )
         gdprintk(XENLOG_ERR,
-            "set_mmio_p2m_entry: set_p2m_entry failed! mfn=%08lx\n",
-            gmfn_to_mfn(p2m->domain, gfn));
+            "set_shared_p2m_entry: set_p2m_entry failed! mfn=%08lx\n",
+            mfn_x(gfn_to_mfn_query(d, gfn, &ot)));
     return rc;
 }
 
xen/arch/x86/mm/shadow/common.c +2 -2
diff -r f2b42201dc1a -r ba2902017f01 xen/arch/x86/mm/shadow/common.c
@@ -3712,7 +3712,7 @@ int shadow_track_dirty_vram(struct domai
 
         /* Iterate over VRAM to track dirty bits. */
         for ( i = 0; i < nr; i++ ) {
-            mfn_t mfn = gfn_to_mfn(d, begin_pfn + i, &t);
+            mfn_t mfn = gfn_to_mfn_query(d, begin_pfn + i, &t);
             struct page_info *page;
             int dirty = 0;
             paddr_t sl1ma = dirty_vram->sl1ma[i];
@@ -3797,7 +3797,7 @@ int shadow_track_dirty_vram(struct domai
                 /* was clean for more than two seconds, try to disable guest
                  * write access */
                 for ( i = begin_pfn; i < end_pfn; i++ ) {
-                    mfn_t mfn = gfn_to_mfn(d, i, &t);
+                    mfn_t mfn = gfn_to_mfn_query(d, i, &t);
                     if (mfn_x(mfn) != INVALID_MFN)
                         flush_tlb |= sh_remove_write_access(d->vcpu[0], mfn, 1, 0);
                 }
xen/include/asm-x86/p2m.h +4 -0
diff -r f2b42201dc1a -r ba2902017f01 xen/include/asm-x86/p2m.h
@@ -411,6 +411,10 @@ static inline mfn_t gfn_to_mfn_type(stru
     return gfn_to_mfn_type_p2m(p2m, gfn, t, &a, q);
 }
 
+/* Syntactic sugar: most callers will use one of these. 
+ * N.B. gfn_to_mfn_query() is the _only_ one guaranteed not to take the
+ * p2m lock; none of the others can be called with the p2m, hap or
+ * shadow lock held. */
 #define gfn_to_mfn(d, g, t)         gfn_to_mfn_type((d), (g), (t), p2m_alloc)
 #define gfn_to_mfn_query(d, g, t)   gfn_to_mfn_type((d), (g), (t), p2m_query)
 #define gfn_to_mfn_guest(d, g, t)   gfn_to_mfn_type((d), (g), (t), p2m_guest)
author
#11 lore raw

PATCH 1/1 x86/mm/p2m: Move p2m code in HVMOP_[gs]et_mem_access into p2m.c

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302439 -3600
# Node ID 72da3d3b68d68c9b5e834e9b707abf2e2c61bdc4
# Parent ba2902017f010e1a3a756f4053175d71027244e8 x86/mm/p2m: Move p2m code in HVMOP_[gs]et_mem_access into p2m.c

It uses p2m internals like the p2m lock and function pointers so belongs behind the p2m interface.

xen/arch/x86/hvm/hvm.c +11 -86
diff -r ba2902017f01 -r 72da3d3b68d6 xen/arch/x86/hvm/hvm.c
@@ -3786,21 +3786,6 @@ long do_hvm_op(unsigned long op, XEN_GUE
     {
         struct xen_hvm_set_mem_access a;
         struct domain *d;
-        struct p2m_domain *p2m;
-        unsigned long pfn;
-        
-        p2m_access_t memaccess[] = {
-            p2m_access_n,
-            p2m_access_r,
-            p2m_access_w,
-            p2m_access_rw,
-            p2m_access_x,
-            p2m_access_rx,
-            p2m_access_wx,
-            p2m_access_rwx,
-            p2m_access_rx2rw,
-            0,  /* HVMMEM_access_default -- will get set below */
-        };
 
         if ( copy_from_guest(&a, arg, 1) )
             return -EFAULT;
@@ -3813,42 +3798,13 @@ long do_hvm_op(unsigned long op, XEN_GUE
         if ( !is_hvm_domain(d) )
             goto param_fail5;
 
-        p2m = p2m_get_hostp2m(d);
-        memaccess[HVMMEM_access_default] = p2m->default_access;
-
-        /* If request to set default access */
-        if ( a.first_pfn == ~0ull ) 
-        {
-            rc = 0;
-            p2m->default_access = memaccess[a.hvmmem_access];
-            goto param_fail5;
-        }
-
         rc = -EINVAL;
         if ( (a.first_pfn > domain_get_maximum_gpfn(d)) ||
              ((a.first_pfn + a.nr - 1) < a.first_pfn) ||
              ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d)) )
             goto param_fail5;
             
-        if ( a.hvmmem_access >= ARRAY_SIZE(memaccess) )
-            goto param_fail5;
-
-        for ( pfn = a.first_pfn; pfn < a.first_pfn + a.nr; pfn++ )
-        {
-            p2m_type_t t;
-            mfn_t mfn;
-            int success;
-
-            mfn = gfn_to_mfn_unshare(d, pfn, &t);
-
-            p2m_lock(p2m);
-            success = p2m->set_entry(p2m, pfn, mfn, 0, t, memaccess[a.hvmmem_access]);
-            p2m_unlock(p2m);
-            if ( !success )
-                goto param_fail5;
-        }
-
-        rc = 0;
+        rc = p2m_set_mem_access(d, a.first_pfn, a.nr, a.hvmmem_access);
 
     param_fail5:
         rcu_unlock_domain(d);
@@ -3859,23 +3815,7 @@ long do_hvm_op(unsigned long op, XEN_GUE
     {
         struct xen_hvm_get_mem_access a;
         struct domain *d;
-        struct p2m_domain *p2m;
-        p2m_type_t t;
-        p2m_access_t ac;
-        mfn_t mfn;
-
-        /* Interface access to internal p2m accesses */
-        hvmmem_access_t memaccess[] = {
-            HVMMEM_access_n,
-            HVMMEM_access_r,
-            HVMMEM_access_w,
-            HVMMEM_access_rw,
-            HVMMEM_access_x,
-            HVMMEM_access_rx,
-            HVMMEM_access_wx,
-            HVMMEM_access_rwx,
-            HVMMEM_access_rx2rw
-        };
+        hvmmem_access_t access;
 
         if ( copy_from_guest(&a, arg, 1) )
             return -EFAULT;
@@ -3888,30 +3828,15 @@ long do_hvm_op(unsigned long op, XEN_GUE
         if ( !is_hvm_domain(d) )
             goto param_fail6;
 
-        p2m = p2m_get_hostp2m(d);
-        
-        if ( a.pfn == ~0ull ) 
-        {
-            a.hvmmem_access = memaccess[p2m->default_access];
-        }
-        else {
-            rc = -EINVAL;
-            if ( (a.pfn > domain_get_maximum_gpfn(d)) )
-                goto param_fail6;
-
-            rc = -ESRCH;
-            mfn = p2m->get_entry(p2m, a.pfn, &t, &ac, p2m_query);
-
-            if ( mfn_x(mfn) == INVALID_MFN )
-                goto param_fail6;
-            
-            rc = -ERANGE;
-            if ( ac >= ARRAY_SIZE(memaccess) )
-                goto param_fail6;
-        
-            a.hvmmem_access = memaccess[ac];
-        }
-
+        rc = -EINVAL;
+        if ( (a.pfn > domain_get_maximum_gpfn(d)) && a.pfn != ~0ull )
+            goto param_fail6;
+
+        rc = p2m_get_mem_access(d, a.pfn, &access);
+        if ( rc != 0 )
+            goto param_fail6;
+
+        a.hvmmem_access = access;
         rc = copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
 
     param_fail6:
xen/arch/x86/mm/p2m.c +95 -0

101 lines not on this page · raw message

xen/include/asm-x86/p2m.h +11 -0
diff -r ba2902017f01 -r 72da3d3b68d6 xen/include/asm-x86/p2m.h
@@ -553,6 +553,17 @@ void p2m_mem_access_check(unsigned long 
                           bool_t access_r, bool_t access_w, bool_t access_x);
 /* Resumes the running of the VCPU, restarting the last instruction */
 void p2m_mem_access_resume(struct p2m_domain *p2m);
+
+/* Set access type for a region of pfns.
+ * If start_pfn == -1ul, sets the default access type */
+int p2m_set_mem_access(struct domain *d, unsigned long start_pfn, 
+                       uint32_t nr, hvmmem_access_t access);
+
+/* Get access type for a pfn
+ * If pfn == -1ul, gets the default access type */
+int p2m_get_mem_access(struct domain *d, unsigned long pfn, 
+                       hvmmem_access_t *access);
+
 #else
 static inline void p2m_mem_access_check(unsigned long gpa, bool_t gla_valid, 
                                         unsigned long gla, bool_t access_r, 
author
#12 lore raw

PATCH 1/1 x86/mm/p2m: Fix locking discipline around log-dirty teardown

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302439 -3600
# Node ID 93e48d4ea41425022c1c82ce7a6de298cd355ab0 # Parent 72da3d3b68d68c9b5e834e9b707abf2e2c61bdc4 x86/mm/p2m: Fix locking discipline around log-dirty teardown.

It's not safe to call paging_free_log_dirty_page with the log-dirty lock held.

xen/arch/x86/mm/paging.c +17 -8
diff -r 72da3d3b68d6 -r 93e48d4ea414 xen/arch/x86/mm/paging.c
@@ -156,10 +156,16 @@ void paging_free_log_dirty_bitmap(struct
 {
     mfn_t *l4, *l3, *l2;
     int i4, i3, i2;
+    struct page_list_head to_free;    
+    struct page_info *pg, *tmp;
 
     if ( !mfn_valid(d->arch.paging.log_dirty.top) )
         return;
 
+    INIT_PAGE_LIST_HEAD(&to_free);
+
+    log_dirty_lock(d);
+
     l4 = map_domain_page(mfn_x(d->arch.paging.log_dirty.top));
 
     for ( i4 = 0; i4 < LOGDIRTY_NODE_ENTRIES; i4++ )
@@ -178,22 +184,28 @@ void paging_free_log_dirty_bitmap(struct
 
             for ( i2 = 0; i2 < LOGDIRTY_NODE_ENTRIES; i2++ )
                 if ( mfn_valid(l2[i2]) )
-                    paging_free_log_dirty_page(d, l2[i2]);
+                    page_list_add_tail(mfn_to_page(l2[i2]), &to_free);
 
             unmap_domain_page(l2);
-            paging_free_log_dirty_page(d, l3[i3]);
+            page_list_add_tail(mfn_to_page(l3[i3]), &to_free);
         }
 
         unmap_domain_page(l3);
-        paging_free_log_dirty_page(d, l4[i4]);
+        page_list_add_tail(mfn_to_page(l4[i4]), &to_free);
     }
 
     unmap_domain_page(l4);
-    paging_free_log_dirty_page(d, d->arch.paging.log_dirty.top);
+    page_list_add_tail(mfn_to_page(d->arch.paging.log_dirty.top), &to_free);
 
     d->arch.paging.log_dirty.top = _mfn(INVALID_MFN);
     ASSERT(d->arch.paging.log_dirty.allocs == 0);
     d->arch.paging.log_dirty.failed_allocs = 0;
+
+    log_dirty_unlock(d);
+    
+    /* Return the memory now that we're not holding the log-dirty lock */
+    page_list_for_each_safe(pg, tmp, &to_free)
+        paging_free_log_dirty_page(d, page_to_mfn(pg));
 }
 
 int paging_log_dirty_enable(struct domain *d)
@@ -217,10 +229,8 @@ int paging_log_dirty_disable(struct doma
     domain_pause(d);
     /* Safe because the domain is paused. */
     ret = d->arch.paging.log_dirty.disable_log_dirty(d);
-    log_dirty_lock(d);
     if ( !paging_mode_log_dirty(d) )
         paging_free_log_dirty_bitmap(d);
-    log_dirty_unlock(d);
     domain_unpause(d);
 
     return ret;
@@ -672,10 +682,9 @@ void paging_log_dirty_init(struct domain
 /* This function fress log dirty bitmap resources. */
 static void paging_log_dirty_teardown(struct domain*d)
 {
-    log_dirty_lock(d);
     paging_free_log_dirty_bitmap(d);
-    log_dirty_unlock(d);
 }
+
 /************************************************/
 /*           CODE FOR PAGING SUPPORT            */
 /************************************************/
author
#13 lore raw

PATCH 1/1 x86/mm: dedup the various copies of the shadow lock functions

In reply to Tim Deegan

# HG changeset patch
# User Tim Deegan <[email protected]>
# Date 1305302661 -3600
# Node ID bb86ec407a0548fd65a0e0f32874db7c2bb1a9c8 # Parent 93e48d4ea41425022c1c82ce7a6de298cd355ab0 x86/mm: dedup the various copies of the shadow lock functions

Define the lock and unlock functions once, and list all the locks in one place so (a) it's obvious what the locking discipline is and (b) none of the locks are visible to non-mm code. Automatically enforce that these locks never get taken in the wrong order.

xen/arch/x86/hvm/vmx/vmx.c +0 -1
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/hvm/vmx/vmx.c
@@ -1215,7 +1215,6 @@ void ept_sync_domain(struct domain *d)
         return;
 
     ASSERT(local_irq_is_enabled());
-    ASSERT(p2m_locked_by_me(p2m_get_hostp2m(d)));
 
     /*
      * Flush active cpus synchronously. Flush others the next time this domain
xen/arch/x86/mm/hap/hap.c +1 -1
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/hap/hap.c
@@ -572,7 +572,7 @@ static void hap_destroy_monitor_table(st
 /************************************************/
 void hap_domain_init(struct domain *d)
 {
-    hap_lock_init(d);
+    mm_lock_init(&d->arch.paging.hap.lock);
     INIT_PAGE_LIST_HEAD(&d->arch.paging.hap.freelist);
 }
 
xen/arch/x86/mm/hap/private.h +2 -0
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/hap/private.h
@@ -20,6 +20,8 @@
 #ifndef __HAP_PRIVATE_H__
 #define __HAP_PRIVATE_H__
 
+#include "../mm-locks.h"
+
 /********************************************/
 /*          GUEST TRANSLATION FUNCS         */
 /********************************************/
xen/arch/x86/mm/mem_sharing.c +4 -40

66 lines not on this page · raw message

xen/arch/x86/mm/mm-locks.h new file +161 -0

161 lines not on this page · raw message

xen/arch/x86/mm/p2m-ept.c +2 -0
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/p2m-ept.c
@@ -32,6 +32,8 @@
 #include <xen/keyhandler.h>
 #include <xen/softirq.h>
 
+#include "mm-locks.h"
+
 #define atomic_read_ept_entry(__pepte)                              \
     ( (ept_entry_t) { .epte = atomic_read64(&(__pepte)->epte) } )
 #define atomic_write_ept_entry(__pepte, __epte)                     \
xen/arch/x86/mm/p2m-pod.c +4 -2
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/p2m-pod.c
@@ -32,7 +32,9 @@
 #include <xen/event.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/svm/amd-iommu-proto.h>
- 
+
+#include "mm-locks.h"
+
 /* Printouts */
 #define P2M_PRINTK(_f, _a...)                                \
     debugtrace_printk("p2m: %s(): " _f, __func__, ##_a)
@@ -393,7 +395,7 @@ p2m_pod_empty_cache(struct domain *d)
 
     /* After this barrier no new PoD activities can happen. */
     BUG_ON(!d->is_dying);
-    spin_barrier(&p2m->lock);
+    spin_barrier(&p2m->lock.lock);
 
     spin_lock(&d->page_alloc_lock);
 
xen/arch/x86/mm/p2m-pt.c +2 -0
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/p2m-pt.c
@@ -38,6 +38,8 @@
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/svm/amd-iommu-proto.h>
 
+#include "mm-locks.h"
+
 /* Debugging and auditing of the P2M code? */
 #define P2M_AUDIT     0
 #define P2M_DEBUGGING 0
xen/arch/x86/mm/p2m.c +4 -2
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/p2m.c
@@ -37,6 +37,8 @@
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/svm/amd-iommu-proto.h>
 
+#include "mm-locks.h"
+
 /* turn on/off 1GB host page table support for hap, default on */
 static bool_t __read_mostly opt_hap_1gb = 1;
 boolean_param("hap_1gb", opt_hap_1gb);
@@ -78,7 +80,7 @@ extern void p2m_pt_init(struct p2m_domai
 static void p2m_initialise(struct domain *d, struct p2m_domain *p2m)
 {
     memset(p2m, 0, sizeof(*p2m));
-    p2m_lock_init(p2m);
+    mm_lock_init(&p2m->lock);
     INIT_PAGE_LIST_HEAD(&p2m->pages);
     INIT_PAGE_LIST_HEAD(&p2m->pod.super);
     INIT_PAGE_LIST_HEAD(&p2m->pod.single);
@@ -103,7 +105,7 @@ p2m_init_nestedp2m(struct domain *d)
     uint8_t i;
     struct p2m_domain *p2m;
 
-    nestedp2m_lock_init(d);
+    mm_lock_init(&d->arch.nested_p2m_lock);
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         d->arch.nested_p2m[i] = p2m = xmalloc(struct p2m_domain);
         if (p2m == NULL)
xen/arch/x86/mm/paging.c +9 -47

80 lines not on this page · raw message

xen/arch/x86/mm/shadow/common.c +1 -1
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/shadow/common.c
@@ -45,7 +45,7 @@ DEFINE_PER_CPU(uint32_t,trace_shadow_pat
  * Called for every domain from arch_domain_create() */
 void shadow_domain_init(struct domain *d, unsigned int domcr_flags)
 {
-    shadow_lock_init(d);
+    mm_lock_init(&d->arch.paging.shadow.lock);
     INIT_PAGE_LIST_HEAD(&d->arch.paging.shadow.freelist);
     INIT_PAGE_LIST_HEAD(&d->arch.paging.shadow.pinned_shadows);
 
xen/arch/x86/mm/shadow/multi.c +1 -1
diff -r 93e48d4ea414 -r bb86ec407a05 xen/arch/x86/mm/shadow/multi.c
@@ -3136,7 +3136,7 @@ static int sh_page_fault(struct vcpu *v,
     if ( unlikely(shadow_locked_by_me(d)) )
     {
         SHADOW_ERROR("Recursive shadow fault: lock was taken by %s\n",
-                     d->arch.paging.shadow.locker_function);
+                     d->arch.paging.shadow.lock.locker_function);
         return 0;
     }
 
xen/arch/x86/mm/shadow/private.h +1 -51

64 lines not on this page · raw message

xen/include/asm-x86/domain.h +5 -12
diff -r 93e48d4ea414 -r bb86ec407a05 xen/include/asm-x86/domain.h
@@ -91,9 +91,8 @@ void hypercall_page_initialise(struct do
 /*          shadow paging extension             */
 /************************************************/
 struct shadow_domain {
-    spinlock_t        lock;  /* shadow domain lock */
-    int               locker; /* processor which holds the lock */
-    const char       *locker_function; /* Func that took it */
+    mm_lock_t         lock;  /* shadow domain lock */
+
     unsigned int      opt_flags;    /* runtime tunable optimizations on/off */
     struct page_list_head pinned_shadows;
 
@@ -159,9 +158,7 @@ struct shadow_vcpu {
 /*            hardware assisted paging          */
 /************************************************/
 struct hap_domain {
-    spinlock_t        lock;
-    int               locker;
-    const char       *locker_function;
+    mm_lock_t         lock;
 
     struct page_list_head freelist;
     unsigned int      total_pages;  /* number of pages allocated */
@@ -174,9 +171,7 @@ struct hap_domain {
 /************************************************/
 struct log_dirty_domain {
     /* log-dirty lock */
-    spinlock_t     lock;
-    int            locker; /* processor that holds the lock */
-    const char    *locker_function; /* func that took it */
+    mm_lock_t     lock;
 
     /* log-dirty radix tree to record dirty pages */
     mfn_t          top;
@@ -280,9 +275,7 @@ struct arch_domain
 
     /* nestedhvm: translate l2 guest physical to host physical */
     struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
-    spinlock_t nested_p2m_lock;
-    int nested_p2m_locker;
-    const char *nested_p2m_function;
+    mm_lock_t nested_p2m_lock;
 
     /* NB. protected by d->event_lock and by irq_desc[irq].lock */
     struct radix_tree_root irq_pirq;
xen/include/asm-x86/hap.h +0 -35
diff -r 93e48d4ea414 -r bb86ec407a05 xen/include/asm-x86/hap.h
@@ -47,41 +47,6 @@ hap_unmap_domain_page(void *p)
 }
 
 /************************************************/
-/*           locking for hap code               */
-/************************************************/
-#define hap_lock_init(_d)                                   \
-    do {                                                    \
-        spin_lock_init(&(_d)->arch.paging.hap.lock);        \
-        (_d)->arch.paging.hap.locker = -1;                  \
-        (_d)->arch.paging.hap.locker_function = "nobody";   \
-    } while (0)
-
-#define hap_locked_by_me(_d)                     \
-    (current->processor == (_d)->arch.paging.hap.locker)
-
-#define hap_lock(_d)                                                       \
-    do {                                                                   \
-        if ( unlikely((_d)->arch.paging.hap.locker == current->processor) )\
-        {                                                                  \
-            printk("Error: hap lock held by %s\n",                         \
-                   (_d)->arch.paging.hap.locker_function);                 \
-            BUG();                                                         \
-        }                                                                  \
-        spin_lock(&(_d)->arch.paging.hap.lock);                            \
-        ASSERT((_d)->arch.paging.hap.locker == -1);                        \
-        (_d)->arch.paging.hap.locker = current->processor;                 \
-        (_d)->arch.paging.hap.locker_function = __func__;                  \
-    } while (0)
-
-#define hap_unlock(_d)                                              \
-    do {                                                            \
-        ASSERT((_d)->arch.paging.hap.locker == current->processor); \
-        (_d)->arch.paging.hap.locker = -1;                          \
-        (_d)->arch.paging.hap.locker_function = "nobody";           \
-        spin_unlock(&(_d)->arch.paging.hap.lock);                   \
-    } while (0)
-
-/************************************************/
 /*        hap domain level functions            */
 /************************************************/
 void  hap_domain_init(struct domain *d);
xen/include/asm-x86/mm.h +9 -0
diff -r 93e48d4ea414 -r bb86ec407a05 xen/include/asm-x86/mm.h
@@ -4,6 +4,7 @@
 
 #include <xen/config.h>
 #include <xen/list.h>
+#include <xen/spinlock.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
 
@@ -597,4 +598,12 @@ unsigned long domain_get_maximum_gpfn(st
 
 extern struct domain *dom_xen, *dom_io, *dom_cow;	/* for vmcoreinfo */
 
+/* Definition of an mm lock: spinlock with extra fields for debugging */
+typedef struct mm_lock {
+    spinlock_t         lock; 
+    int                unlock_level;
+    int                locker;          /* processor which holds the lock */
+    const char        *locker_function; /* func that took it */
+} mm_lock_t;
+
 #endif /* __ASM_X86_MM_H__ */
xen/include/asm-x86/p2m.h +1 -77

90 lines not on this page · raw message