Skip to contentSkip to conversation

x86/p2m: acquire temporary reference when removing foreign mappings

PATCH In review Jiaqing Zhao 2 participants 2 messages Nov 25

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/p2m: acquire temporary reference when removing foreign mappings

As p2m_entry_modify() drops the mapping's reference before the P2M entry is cleared, since ac607edc2e26 ("x86/p2m: clarify foreign map handling in p2m_entry_modify()"), p2m_entry_modify() requires the caller to hold its own reference when removing a foreign entry. However, in relinquish_p2m_mapping() and the special page handling paths, foreign pages are unmapped without taking a reference of their own, causing the unmap to fail with -EILSEQ.

Acquire a temporary reference in p2m_remove_entry() for foreign pages when unmapping, force IOTLB flushing, and drop it after the P2M TLB flush. Use p2m_remove_page() in relinquish_p2m_mapping() so it takes the fix.

Fixes ac607edc2e26 ("x86/p2m: clarify foreign map handling in p2m_entry_modify()")

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Teddy Astie <[email protected]>

---
 xen/arch/x86/mm/p2m.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
xen/arch/x86/mm/p2m.c +32 -3
index bb0356396c..cf56826a17 100644
@@ -542,6 +542,8 @@ p2m_remove_entry(struct p2m_domain *p2m, gfn_t gfn, mfn_t mfn,
     p2m_type_t t;
     p2m_access_t a;
     int rc;
+    struct page_info *fpage = NULL;
+    bool dont_flush = false;
 
     ASSERT(gfn_locked_by_me(p2m, gfn));
     P2M_DEBUG("removing gfn=%#lx mfn=%#lx\n", gfn_x(gfn), mfn_x(mfn));
@@ -561,6 +563,25 @@ p2m_remove_entry(struct p2m_domain *p2m, gfn_t gfn, mfn_t mfn,
              ((gfn_x(gfn) + i) & ((1UL << cur_order) - 1));
     }
 
+    /*
+     * p2m_entry_modify() puts the mapping's reference before the entry is
+     * cleared, and an outer p2m_lock() holder may defer the TLB flush. Hold
+     * a temporary reference, and force IOTLB flushing, until both are done.
+     */
+    if ( p2m_is_foreign(t) && page_order == PAGE_ORDER_4K )
+    {
+        fpage = get_page_from_mfn_and_type(p2m->domain, mfn, t);
+        if ( !fpage )
+        {
+            P2M_ERROR("%pd: cannot reference foreign mfn %#lx at gfn %#lx\n",
+                      p2m->domain, mfn_x(mfn), gfn_x(gfn));
+            return -EBUSY;
+        }
+
+        dont_flush = this_cpu(iommu_dont_flush_iotlb);
+        this_cpu(iommu_dont_flush_iotlb) = false;
+    }
+
     if ( mfn_valid(mfn) )
     {
         for ( i = 0; i < (1UL << page_order); i++ )
@@ -579,6 +600,14 @@ p2m_remove_entry(struct p2m_domain *p2m, gfn_t gfn, mfn_t mfn,
 
     rc = p2m_set_entry(p2m, gfn, INVALID_MFN, page_order, p2m_invalid,
                        p2m->default_access);
+
+    if ( fpage )
+    {
+        p2m_tlb_flush_sync(p2m);
+        put_page(fpage);
+        this_cpu(iommu_dont_flush_iotlb) = dont_flush;
+    }
+
     if ( likely(!rc) || !mfn_valid(mfn) )
         return rc;
 
@@ -2156,8 +2185,9 @@ int relinquish_p2m_mapping(struct domain *d)
         unsigned int order;
         p2m_type_t t;
         p2m_access_t a;
+        mfn_t mfn;
 
-        _get_gfn_type_access(p2m, _gfn(gfn), &t, &a, 0, &order, 0);
+        mfn = _get_gfn_type_access(p2m, _gfn(gfn), &t, &a, 0, &order, 0);
         ASSERT(IS_ALIGNED(gfn, 1UL << order));
 
         if ( t == p2m_map_foreign )
@@ -2165,8 +2195,7 @@ int relinquish_p2m_mapping(struct domain *d)
             ASSERT(p2m->nr_foreign);
             ASSERT(order == 0);
 
-            rc = p2m_set_entry(p2m, _gfn(gfn), INVALID_MFN, order, p2m_invalid,
-                               p2m->default_access);
+            rc = p2m_remove_page(d, _gfn(gfn), mfn, order);
             if ( rc )
             {
                 printk(XENLOG_ERR
Signature · 1 line
2.53.0
#1 lore raw
Quoted 13 lines · Jiaqing Zhao

As p2m_entry_modify() drops the mapping's reference before the P2M entry is cleared, since ac607edc2e26 ("x86/p2m: clarify foreign map handling in p2m_entry_modify()"), p2m_entry_modify() requires the caller to hold its own reference when removing a foreign entry. However, in relinquish_p2m_mapping() and the special page handling paths, foreign pages are unmapped without taking a reference of their own, causing the unmap to fail with -EILSEQ.

Acquire a temporary reference in p2m_remove_entry() for foreign pages when unmapping, force IOTLB flushing, and drop it after the P2M TLB flush. Use p2m_remove_page() in relinquish_p2m_mapping() so it takes the fix.

Quoted 7 lines
Fixes ac607edc2e26 ("x86/p2m: clarify foreign map handling in p2m_entry_modify()")

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: Teddy Astie <[email protected]>

It really ought to be obvious that patches of this nature should not be posted publicly.

If you had spoken to the security team, or (in this case) your colleagues, you'd have learnt that this has already been reported.  You would also have found that this fix is incorrect.

What you have done is zero-day one of your colleagues who is attempting to make a safe fix for this issue.

~Andrew