All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Optimise restore memory allocation
@ 2025-08-27 12:33 Frediano Ziglio
  2025-08-27 17:23 ` Andrew Cooper
  0 siblings, 1 reply; 4+ messages in thread
From: Frediano Ziglio @ 2025-08-27 12:33 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Anthony PERARD, Juergen Gross,
	Roger Pau Monné, Andrew Cooper

Try to allocate larger order pages.
With some test memory program stressing TLB (many small random
memory accesses) you can get 15% performance improves.
On the first memory iteration the sender is currently sending
memory in 4mb aligned chunks which allows the receiver to
allocate most pages as 2mb superpages instead of single 4kb pages.

Signed-off-by: Frediano Ziglio <frediano.ziglio@cloud.com>
---
 tools/libs/guest/xg_sr_restore.c | 39 ++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/tools/libs/guest/xg_sr_restore.c b/tools/libs/guest/xg_sr_restore.c
index 06231ca826..8dcb1b19c5 100644
--- a/tools/libs/guest/xg_sr_restore.c
+++ b/tools/libs/guest/xg_sr_restore.c
@@ -129,6 +129,8 @@ static int pfn_set_populated(struct xc_sr_context *ctx, xen_pfn_t pfn)
     return 0;
 }
 
+#define IS_POWER_OF_2(n) (((n) & ((n) - 1)) == 0)
+
 /*
  * Given a set of pfns, obtain memory from Xen to fill the physmap for the
  * unpopulated subset.  If types is NULL, no page type checking is performed
@@ -141,6 +143,7 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned int count,
     xen_pfn_t *mfns = malloc(count * sizeof(*mfns)),
         *pfns = malloc(count * sizeof(*pfns));
     unsigned int i, nr_pfns = 0;
+    bool contiguous = true;
     int rc = -1;
 
     if ( !mfns || !pfns )
@@ -159,18 +162,46 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned int count,
             if ( rc )
                 goto err;
             pfns[nr_pfns] = mfns[nr_pfns] = original_pfns[i];
+            if ( pfns[nr_pfns] != pfns[0] + nr_pfns )
+                contiguous = false;
             ++nr_pfns;
         }
     }
 
     if ( nr_pfns )
     {
-        rc = xc_domain_populate_physmap_exact(
-            xch, ctx->domid, nr_pfns, 0, 0, mfns);
+        /* try optimizing using larger order */
+        rc = -1;
+        /*
+         * The "nr_pfns <= (1 << 18)" check is mainly for paranoia, it should
+         * never happen, the sender would have to send a really large packet.
+         */
+        if ( contiguous && nr_pfns <= (1 << 18) &&
+             IS_POWER_OF_2(nr_pfns) && (pfns[0] & (nr_pfns - 1)) == 0 )
+        {
+            const unsigned int extent_order = __builtin_ffs(nr_pfns) - 1;
+
+            rc = xc_domain_populate_physmap_exact(
+                xch, ctx->domid, 1, extent_order, 0, mfns);
+            if ( rc )
+                mfns[0] = pfns[0];
+            else
+            {
+                for ( i = 1; i < nr_pfns; ++i )
+                    mfns[i] = mfns[0] + i;
+            }
+        }
+
+        /* if using larger order fails fall back to single pages */
         if ( rc )
         {
-            PERROR("Failed to populate physmap");
-            goto err;
+            rc = xc_domain_populate_physmap_exact(
+                xch, ctx->domid, nr_pfns, 0, 0, mfns);
+            if ( rc )
+            {
+                PERROR("Failed to populate physmap");
+                goto err;
+            }
         }
 
         for ( i = 0; i < nr_pfns; ++i )
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-29  9:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-27 12:33 [PATCH] Optimise restore memory allocation Frediano Ziglio
2025-08-27 17:23 ` Andrew Cooper
2025-08-28 13:30   ` Oleksii Kurochko
2025-08-29  9:34   ` Andrew Cooper

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.