[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jiaqing Zhao <Zhao.Jiaqing@xxxxxxx>
  • Date: Wed, 25 Nov 2026 19:22:32 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=el9PPCLrYtUeZTLF/RYkA/k6bJtLGzghYnWNQGSVbiU=; b=JBg/cJ0Kd7pPDQQbUlO8nZjinxjkAnzkpqKsTTRingVLkwLkxuL5lr4nAGmJLoWdg4YvWRJzQMA9dH327ZKEe8sT0mqRdc3S4SvL17ydE7wJjvLFSYWpCwlqltC+iBj2BgrSQuBafiNAP51JQhOALybKEJOIdCJvQjet8hpo4IrWtgbvoFz/envI0lPv6s6sBljGO4xUL6fVx/FSigtZSqhDikbomMW16En7yvCH/mCe5/vaHIlPEvjQpibKTy1bcwzuhpwEum324HtdHWzuWBUxhTEUPcQfDEiQlguDubLs1WHv0yvtsSnO3AdQc6MhJOh3ifcoD3njBYbge5iuuw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ADL+Y4v3Eb5evBG8ZNuFTDscHjPgIG1NAR9rmEB4DsT68hZ4V4NPn3w/N5C9JN2Osl6ah45hfr4n25M68NaVl5VhCBCxXqjEn//Xm7+SqEzOiyTm/9EZZufHiv8l/t+7xVpuQ22wJpNwtf7AdYZcbrL1363N6PdHIMn27Al6Nr3FIzretwZQQoZhpGHPfcbOIkRpgdDZT2tHh1a97fXOfpc3BJj9OIl2VBAFbvAt0K/sRkoqzY5XMp9tdluWoMP1DbR+SDH7FDzr5NVlxeBA2iY6oBFtTfxbuZkVdr+INI/zsZ++s494HRum6CB1rmQWMGMgd7jGNqKtu1Ea29OsEA==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=amd.com header.i="@amd.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Cc: Jiaqing Zhao <Zhao.Jiaqing@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>
  • Delivery-date: Wed, 25 Nov 2026 11:23:12 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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()")
Signed-off-by: Jiaqing Zhao <Zhao.Jiaqing@xxxxxxx>
---
Cc: Jan Beulich <jbeulich@xxxxxxxx>
Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Cc: "Roger Pau Monné" <roger@xxxxxxxxxxxxxx>
Cc: Teddy Astie <teddy.astie@xxxxxxxxxx>
---
 xen/arch/x86/mm/p2m.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
index bb0356396c..cf56826a17 100644
--- a/xen/arch/x86/mm/p2m.c
+++ b/xen/arch/x86/mm/p2m.c
@@ -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
-- 
2.53.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.