From: Frediano Ziglio <freddy77@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: "Edwin Török" <edwin.torok@citrix.com>,
"Jan Beulich" <jbeulich@suse.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Teddy Astie" <teddy.astie@vates.tech>,
"Anthony PERARD" <anthony.perard@vates.tech>,
"Juergen Gross" <jgross@suse.com>,
"Frediano Ziglio" <frediano.ziglio@citrix.com>
Subject: [PATCH v5 06/16] libs/call: cache up to 4 pages in hypercall bounce buffers
Date: Sat, 13 Jun 2026 22:47:39 +0100 [thread overview]
Message-ID: <20260613214749.20620-7-frediano.ziglio@cloud.com> (raw)
In-Reply-To: <20260613214749.20620-1-frediano.ziglio@cloud.com>
From: Edwin Török <edwin.torok@citrix.com>
During migration there are a lot of mmap/munmap calls,
because `xc_get_pfn_type_batch` exceeds the default hypercall bounce
buffer cache size, and needs to allocate every time it is called.
`munmap` is slow, especially in a PV Dom0 (takes an emulation fault),
so is best avoided.
Eventually it'd be good if the memory pool from xmalloc_tlsf.c
was reused here, but for now make it handle the commonly encountered
sizes (so far up to 4 pages).
Signed-off-by: Edwin Török <edwin.torok@citrix.com>
Signed-off-by: Frediano Ziglio <frediano.ziglio@citrix.com>
--
Changes since v2:
- change prefix in subject.
Changes since v4:
- fix off-by-one bug.
---
tools/libs/call/buffer.c | 28 +++++++++++++++++-----------
tools/libs/call/core.c | 3 ++-
tools/libs/call/private.h | 8 +++++---
3 files changed, 24 insertions(+), 15 deletions(-)
diff --git a/tools/libs/call/buffer.c b/tools/libs/call/buffer.c
index 155e4f9d43..bde780c32c 100644
--- a/tools/libs/call/buffer.c
+++ b/tools/libs/call/buffer.c
@@ -56,13 +56,13 @@ static void *cache_alloc(xencall_handle *xcall, size_t nr_pages)
if ( xcall->buffer_current_allocations > xcall->buffer_maximum_allocations )
xcall->buffer_maximum_allocations = xcall->buffer_current_allocations;
- if ( nr_pages > 1 )
+ if ( nr_pages > ARRAY_SIZE(xcall->buffer_cache) )
{
xcall->buffer_cache_toobig++;
}
- else if ( xcall->buffer_cache_nr > 0 )
+ else if ( xcall->buffer_cache_nr[nr_pages-1] > 0 )
{
- p = xcall->buffer_cache[--xcall->buffer_cache_nr];
+ p = xcall->buffer_cache[nr_pages-1][--xcall->buffer_cache_nr[nr_pages-1]];
xcall->buffer_cache_hits++;
}
else
@@ -84,10 +84,10 @@ static int cache_free(xencall_handle *xcall, void *p, size_t nr_pages)
xcall->buffer_total_releases++;
xcall->buffer_current_allocations--;
- if ( nr_pages == 1 &&
- xcall->buffer_cache_nr < BUFFER_CACHE_SIZE )
+ if ( nr_pages && nr_pages <= ARRAY_SIZE(xcall->buffer_cache) &&
+ xcall->buffer_cache_nr[nr_pages-1] < BUFFER_CACHE_SIZE )
{
- xcall->buffer_cache[xcall->buffer_cache_nr++] = p;
+ xcall->buffer_cache[nr_pages-1][xcall->buffer_cache_nr[nr_pages-1]++] = p;
rc = 1;
}
@@ -108,17 +108,23 @@ void buffer_release_cache(xencall_handle *xcall)
DBGPRINTF("current allocations:%d maximum allocations:%d",
xcall->buffer_current_allocations,
xcall->buffer_maximum_allocations);
- DBGPRINTF("cache current size:%d",
- xcall->buffer_cache_nr);
+ for ( unsigned i = 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++i )
+ {
+ DBGPRINTF("cache current size[%u pages]:%d", i+1,
+ xcall->buffer_cache_nr[i]);
+ }
DBGPRINTF("cache hits:%d misses:%d toobig:%d",
xcall->buffer_cache_hits,
xcall->buffer_cache_misses,
xcall->buffer_cache_toobig);
- while ( xcall->buffer_cache_nr > 0 )
+ for ( unsigned i = 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++i )
{
- p = xcall->buffer_cache[--xcall->buffer_cache_nr];
- osdep_free_pages(xcall, p, 1);
+ while ( xcall->buffer_cache_nr[i] > 0 )
+ {
+ p = xcall->buffer_cache[i][--xcall->buffer_cache_nr[i]];
+ osdep_free_pages(xcall, p, i + 1);
+ }
}
cache_unlock(xcall);
diff --git a/tools/libs/call/core.c b/tools/libs/call/core.c
index 02c4f8e1ae..dd8877c1a0 100644
--- a/tools/libs/call/core.c
+++ b/tools/libs/call/core.c
@@ -14,6 +14,7 @@
*/
#include <stdlib.h>
+#include <string.h>
#include "private.h"
@@ -44,7 +45,7 @@ xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
xentoolcore__register_active_handle(&xcall->tc_ah);
xcall->flags = open_flags;
- xcall->buffer_cache_nr = 0;
+ memset(xcall->buffer_cache_nr, 0, sizeof(xcall->buffer_cache_nr));
xcall->buffer_total_allocations = 0;
xcall->buffer_total_releases = 0;
diff --git a/tools/libs/call/private.h b/tools/libs/call/private.h
index 9c3aa432ef..8e6a208975 100644
--- a/tools/libs/call/private.h
+++ b/tools/libs/call/private.h
@@ -31,13 +31,15 @@ struct xencall_handle {
Xentoolcore__Active_Handle tc_ah;
/*
- * A simple cache of unused, single page, hypercall buffers
+ * A simple cache of unused, small, hypercall buffers
+ * buffer_cache[i]'s size is (i+1) pages
*
* Protected by a global lock.
*/
#define BUFFER_CACHE_SIZE 4
- int buffer_cache_nr;
- void *buffer_cache[BUFFER_CACHE_SIZE];
+#define BUFFER_CACHE_NRPAGES 4
+ int buffer_cache_nr[BUFFER_CACHE_NRPAGES];
+ void *buffer_cache[BUFFER_CACHE_NRPAGES][BUFFER_CACHE_SIZE];
/*
* Hypercall buffer statistics. All protected by the global
--
2.43.0
next prev parent reply other threads:[~2026-06-13 21:48 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-13 21:47 [PATCH v5 00/16] xenguest optimisations Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 01/16] libs/guest: Reduce number of parts in write_split_record Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 02/16] libs/guest: Reduce number of I/O vectors in write_batch Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 03/16] " Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 04/16] libs/guest: Use a single write_exact in write_headers Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 05/16] libs/guest: allocate various migration arrays just once Frediano Ziglio
2026-06-13 21:47 ` Frediano Ziglio [this message]
2026-06-13 21:47 ` [PATCH v5 07/16] libs/guest: avoids using 2 indexes Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 08/16] libs/guest: fill directly iov structure Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 09/16] libs/ctrl: Allows writev_exact to change iov array Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 10/16] libs/guest: add xg_foreignmemory_copy_{from,to} Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 11/16] PoC: libs/guest: use foreign copy during migration Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 12/16] xen: implement new foreign copy hypercall Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 13/16] privcmd: Add definition for new Linux privcmd to access new Xen hypercall Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 14/16] libs/guest: use new hypercall if available Frediano Ziglio
2026-06-13 21:47 ` [PATCH v5 15/16] libs/guest: finalize PoC Frediano Ziglio
2026-06-13 21:47 ` [PATCH Linux v5 16/16] xen/privcmd: Add new ABI to allow copying foreign memory Frediano Ziglio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260613214749.20620-7-frediano.ziglio@cloud.com \
--to=freddy77@gmail.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=edwin.torok@citrix.com \
--cc=frediano.ziglio@citrix.com \
--cc=jbeulich@suse.com \
--cc=jgross@suse.com \
--cc=roger.pau@citrix.com \
--cc=teddy.astie@vates.tech \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.