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

[PATCH v2 5/7] x86: extend do_mmu_update() to support returning the old PTE value


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Kevin Lampis <kevin.lampis@xxxxxxxxxx>
  • Date: Thu, 10 Sep 2026 21:31:11 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • 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=XeS3HADGVZC3svsA8mdXVt6dTdJRoxy4Cwr2pNlCGck=; b=o0KJ6fyFiK48aM+1qxqdJq1lEQeWHgRpRa7iIyn27owuQDyM+DWZuVe6m09Y3x1EYdFtvSkjvIrX8+QnlSWulIEn3ETnJ3ohn8uPfEirorCFiemYGgSAGNjr7KpbnNtRtlaaBvPoC3wgZG7Tyb4dB1NTSD+AL1+yyS2V2yE5QMdqEMbFCI/5G9tm2Ti9Z2LtkHYlY6/oMbzEOrCErU69tmK/n+7gM3iNkchlf0ZAfvQLitVpTPpoV69JtEg9T1eKl90E9M5eg7NbitJWdT8Z6C1LfCkBMA7/h6q0yW+rAtA4NLAm3W4MTfTpJ9kPCi3zWmvWYLC7aWA47DrAAY7k9g==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZGcAE1nabQSJCg4idVFZUBIqfvhOVsBKkt0mw2dbi2VGzFpLVrgjY8IN04qL2nMNpYBEBYM1zk6QCRu+ALO0/lNa/HGoBCyAkDOhLTg2KqW7qPum+CjyP6mQn/lNIn7tKpY0E9zcXi0Mjxx3xhEWyMZcGpkPZAD6LVsZZsluJYm8pA6oc2aVT+fDZfQeFXgutvQue78qObY54QDT7RQmVE94AywxxwU4urtUB5OKgU5hPvDattT8iA9fCRplnWHOUZi/wJPguVFV0t9jLN9NiPDBUalfBvGk4tvKFcoWHY0kY1ZZDz4vZCwd4cKFYBcX3OOrvpYOFdv2WH1tMriZqg==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: jbeulich@xxxxxxxx, andrew.cooper3@xxxxxxxxxx, teddy.astie@xxxxxxxxxx, Kevin Lampis <kevin.lampis@xxxxxxxxxx>
  • Delivery-date: Thu, 10 Sep 2026 20:29:52 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

A new sub-op MMU_PT_UPDATE_SWAP when used will do an atomic swap to set
the new value and return the old PTE value through the req.val field.

- The new PTE value in req.val must be 0, at the moment this is intended
  for clearing only.

- Only l1 PTEs are supported because they are the most frequent and have the
  biggest performance impact.

- The old PTE value is passed back to the guest through the req.val field

If MMU_PT_UPDATE_SWAP is not set then the old behavior is preserved
  do_mmu_update -> mod_l1_entry -> UPDATE_ENTRY -> paging_write_guest_entry

The new MMU_PT_UPDATE_SWAP call chain looks like this
  do_mmu_update -> mod_l1_entry -> UPDATE_ENTRY -> paging_cmpxchg_guest_entry

Signed-off-by: Kevin Lampis <kevin.lampis@xxxxxxxxxx>
---
Changes in v2:
- Add a sub-op to mmu_update instead of new hypercall
- Rename to "swap" instead of "clear", although only `0` is accepted
- Remove spurious curly brace from `case PGT_l1_page_table:`
- Restructure `case PGT_l1_page_table:` to avoid diff churn
- Add missing `MMU_PT_UPDATE_SWAP` check for the second
  `PGT_writable_page` block

(Jan) I decided not to remove the `rc = -EINVAL` lines. Even though `rc`
already has this value I think it's more readable like this. Let me know
if I misunderstood the issue.
---
 xen/arch/x86/mm.c        | 48 +++++++++++++++++++++++++++++++++++++++-
 xen/include/public/xen.h |  1 +
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 59ad5ce16486..b498a8460935 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -4055,6 +4055,7 @@ long do_mmu_update(
         case MMU_NORMAL_PT_UPDATE:
         case MMU_PT_UPDATE_PRESERVE_AD:
         case MMU_PT_UPDATE_NO_TRANSLATE:
+        case MMU_PT_UPDATE_SWAP:
         {
             p2m_type_t p2mt;
 
@@ -4118,6 +4119,30 @@ long do_mmu_update(
                 switch ( page->u.inuse.type_info & PGT_type_mask )
                 {
                 case PGT_l1_page_table:
+                    if ( cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        l1_pgentry_t ol1e;
+
+                        if ( unlikely(req.val != 0) )
+                        {
+                            rc = -EINVAL;
+                            break;
+                        }
+
+                        update_flags |= PTE_UPDATE_SWAP;
+
+                        rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
+                                          update_flags, v, pg_owner, &ol1e);
+
+                        if ( !rc )
+                        {
+                            req.val = ol1e.l1;
+                            if ( unlikely(copy_to_guest(ureqs, &req, 1)) )
+                                rc = -EFAULT;
+                        }
+                        break;
+                    }
+
                     rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
                                       update_flags, v, pg_owner, NULL);
                     break;
@@ -4125,6 +4150,11 @@ long do_mmu_update(
                 case PGT_l2_page_table:
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+                    if ( unlikely(cmd == MMU_PT_UPDATE_SWAP) )
+                    {
+                        rc = -EINVAL;
+                        break;
+                    }
                     rc = mod_l2_entry(va, l2e_from_intpte(req.val), mfn,
                                       update_flags, v);
                     if ( !rc )
@@ -4134,6 +4164,11 @@ long do_mmu_update(
                 case PGT_l3_page_table:
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+                    if ( unlikely(cmd == MMU_PT_UPDATE_SWAP) )
+                    {
+                        rc = -EINVAL;
+                        break;
+                    }
                     rc = mod_l3_entry(va, l3e_from_intpte(req.val), mfn,
                                       update_flags, v);
                     if ( !rc )
@@ -4143,6 +4178,11 @@ long do_mmu_update(
                 case PGT_l4_page_table:
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+                    if ( unlikely(cmd == MMU_PT_UPDATE_SWAP) )
+                    {
+                        rc = -EINVAL;
+                        break;
+                    }
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
                                       update_flags, v);
                     if ( !rc )
@@ -4172,6 +4212,11 @@ long do_mmu_update(
                     break;
 
                 case PGT_writable_page:
+                    if ( unlikely(cmd == MMU_PT_UPDATE_SWAP) )
+                    {
+                        rc = -EINVAL;
+                        break;
+                    }
                     perfc_incr(writable_mmu_updates);
                     paging_write_guest_entry(v, va, req.val, mfn);
                     rc = 0;
@@ -4181,7 +4226,8 @@ long do_mmu_update(
                 if ( rc == -EINTR )
                     rc = -ERESTART;
             }
-            else if ( get_page_type(page, PGT_writable_page) )
+            else if ( likely(cmd != MMU_PT_UPDATE_SWAP) &&
+                      get_page_type(page, PGT_writable_page) )
             {
                 perfc_incr(writable_mmu_updates);
                 paging_write_guest_entry(v, va, req.val, mfn);
diff --git a/xen/include/public/xen.h b/xen/include/public/xen.h
index 2149b8dd3808..559190688a1e 100644
--- a/xen/include/public/xen.h
+++ b/xen/include/public/xen.h
@@ -349,6 +349,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define MMU_PT_UPDATE_PRESERVE_AD  2 /* atomically: *ptr = val | (*ptr&(A|D)) 
*/
 #define MMU_PT_UPDATE_NO_TRANSLATE 3 /* checked '*ptr = val'. ptr is MA.      
*/
                                      /* val never translated.                 
*/
+#define MMU_PT_UPDATE_SWAP         4
 
 /*
  * MMU EXTENDED OPERATIONS
-- 
2.52.0




 


Rackspace

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