From: "Roger Pau Monné" <roger@xenproject.org>
To: George Dunlap <gwd@xenproject.org>
Cc: xen-devel@lists.xenproject.org,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Jan Beulich" <jbeulich@suse.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Teddy Astie" <teddy.astie@vates.tech>
Subject: Re: [PATCH v3 08/18] x86/domain_page: prepare the mapcache for per-vCPU accounting
Date: Thu, 8 Oct 2026 10:22:01 +0200 [thread overview]
Message-ID: <asdSqe9KC_J3OLpv@Mac.lan> (raw)
In-Reply-To: <20261007104051.48841-9-gwd@xenproject.org>
On Wed, Oct 07, 2026 at 11:40:41AM +0100, George Dunlap wrote:
> From: Roger Pau Monné <roger.pau@citrix.com>
>
> Currently, only PV domains have a mapcache, which is domain-wide one.
> In preparation for enabling per-vCPU mapcaches for HVM domains, split
> the accounting out into a struct mapcache, embedded in struct
> mapcache_domain.
>
> Introduce has_mapcache(), for whether a vCPU has a mapcache, and
> vcpu_mapcache(), which returns the accounting for a vCPU's mapcache
> and, in *dcache, the domain-wide mapcache whose lock and TLB epoch the
> caller has to take into account. For now has_mapcache() is
> is_pv_vcpu(), and vcpu_mapcache() always returns the domain's
> accounting. map_domain_page() and unmap_domain_page() stop repeating
> the is_pv_vcpu() check mapcache_current_vcpu() has made.
>
> Move map_domain_page()'s handling of the domain-wide lock and TLB epoch
> into three helpers: mapcache_lock() takes the lock and catches up with
> a wrap another vCPU caused, mapcache_new_epoch() starts a new epoch
> after a wrap, and mapcache_unlock() drops the lock. A per-vCPU
> mapcache can then skip them without map_domain_page() being
> re-indented.
>
> struct mapcache_domain keeps its size (40 bytes, x86_64 debug build),
> and so do the structures containing it.
>
> No functional change.
>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claude Code:claude-opus-5-5
> Signed-off-by: George Dunlap <gwd@xenproject.org>
> ---
> Changes in v3:
> - New in this version, split out of "x86/hvm: introduce per-vCPU
> mapcache for vCPU-PT domains" for review.
> ---
> xen/arch/x86/domain_page.c | 155 ++++++++++++++++++++----------
> xen/arch/x86/include/asm/domain.h | 19 ++--
> 2 files changed, 116 insertions(+), 58 deletions(-)
>
> diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c
> index 9119f735f5..5d90f77bed 100644
> --- a/xen/arch/x86/domain_page.c
> +++ b/xen/arch/x86/domain_page.c
> @@ -18,17 +18,23 @@
> #include <asm/hardirq.h>
> #include <asm/setup.h>
>
> +/*
> + * Whether v has a mapcache: PV vCPUs (whose domain-wide mapcache may still be
> + * disabled, see mapcache_domain_init()). HVM vCPUs reach everything through
> + * the directmap, which covers all physical address space for them.
I've been wondering whether there could be a way to write this comment
so that it doesn't need rewording once we switch to per-vCPU
mapcaches, as to avoid adding a comment here that will very likely be
completely re-worded by a later change in the series. However I
cannot find anything much better rather than stripping most of the
comment content.
> + */
> +static bool has_mapcache(const struct vcpu *v)
> +{
> + return is_pv_vcpu(v);
> +}
> +
> static inline struct vcpu *mapcache_current_vcpu(void)
> {
> struct vcpu *v = this_cpu(pgtable_vcpu);
> struct vcpu *curr = current;
>
> - /*
> - * During early boot pgtable_vcpu is not set, callers must handle NULL.
> - * Non-PV domains don't have a mapcache, the directmap covers all physical
> - * address space.
> - */
> - if ( !v || !is_pv_vcpu(v) )
> + /* During early boot pgtable_vcpu is not set, callers must handle NULL. */
> + if ( !v || !has_mapcache(v) )
> return NULL;
>
> /*
> @@ -54,6 +60,55 @@ static inline struct vcpu *mapcache_current_vcpu(void)
> return ACCESS_ONCE(this_cpu(pgtable_vcpu));
> }
>
> +/*
> + * The accounting for v's mapcache: the domain's, with *dcache set so the
> + * caller takes the domain-wide lock and TLB-epoch machinery into account.
> + */
> +static struct mapcache *vcpu_mapcache(struct vcpu *v,
> + struct mapcache_domain **dcache)
> +{
> + struct domain *d = v->domain;
> +
> + *dcache = &d->arch.pv.mapcache;
> + return &(*dcache)->cache;
> +}
> +
> +/*
> + * The domain-wide mapcache's lock and TLB-epoch machinery, taken around
> + * allocating a slot: the domain's vCPUs share the mapcache's slots, and each
> + * catches up with a wrap another one caused, flushing its TLB if it has not
> + * been flushed since.
Thinking forward about how this function will end up looking when we
have per-vCPU map caches, do you maybe want to place this comment
inside the function body itself?
I expect per-vCPU splitting will add a short-circuit at the top of the
function that will skip taking the lock or doing the flushing, and
hence the comment won't need shuffling around?
> + */
> +static void mapcache_lock(struct mapcache_domain *dcache,
Maybe mapcache_enter() would be a better suited name once there's no
longer a need to take a lock for the mapcache? Just wondering, no
strong opinion really (the more that I don't even know how the code
will end up looking yet).
> + struct mapcache_vcpu *vcache)
> +{
> + spin_lock(&dcache->lock);
> +
> + /* Has some other CPU caused a wrap? We must flush if so. */
> + if ( unlikely(dcache->epoch != vcache->shadow_epoch) )
> + {
> + vcache->shadow_epoch = dcache->epoch;
> + if ( NEED_FLUSH(this_cpu(tlbflush_time), dcache->tlbflush_timestamp) )
> + {
> + perfc_incr(domain_page_tlb_flush);
> + flush_tlb_local();
> + }
> + }
> +}
> +
> +/* A wrap has just flushed this CPU's TLB: start a new epoch. */
> +static void mapcache_new_epoch(struct mapcache_domain *dcache,
> + struct mapcache_vcpu *vcache)
> +{
Since this is now a function on its own, I would possibly assert that
dcache->lock is taken on entry.
Let me know what you think of the suggestions above, overall I think
the logic is fine.
Thanks, Roger.
next prev parent reply other threads:[~2026-10-08 8:22 UTC|newest]
Thread overview: 66+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-07 10:40 [PATCH v3 00/18] ASI: HVM per-vCPU areas and sparse-view directmap George Dunlap
2026-10-07 10:40 ` [PATCH v3 01/18] x86/PoD: map one page at a time in p2m_pod_zero_check() George Dunlap
2026-10-07 10:51 ` Jan Beulich
2026-10-07 12:33 ` Andrew Cooper
2026-10-07 12:38 ` Andrew Cooper
2026-10-08 11:09 ` George Dunlap
2026-10-08 22:24 ` Andrew Cooper
2026-10-07 10:40 ` [PATCH v3 02/18] x86/mm: purge unneeded destroy_perdomain_mapping() George Dunlap
2026-10-07 14:06 ` Roger Pau Monné
2026-10-07 14:24 ` Jan Beulich
2026-10-08 12:19 ` George Dunlap
2026-10-08 7:55 ` Jan Beulich
2026-10-08 8:31 ` Roger Pau Monné
2026-10-08 12:27 ` George Dunlap
2026-10-07 10:40 ` [PATCH v3 03/18] x86/mm: prepare destroy_perdomain_mapping() for per-vCPU perdomain areas George Dunlap
2026-10-07 15:58 ` Roger Pau Monné
2026-10-08 13:27 ` George Dunlap
2026-10-07 10:40 ` [PATCH v3 04/18] x86/domain_page: drop redundant create_perdomain_mapping() call George Dunlap
2026-10-07 16:14 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 05/18] x86/mm: prepare create_perdomain_mapping() for per-vCPU perdomain areas George Dunlap
2026-10-07 16:31 ` Roger Pau Monné
2026-10-12 12:44 ` George Dunlap
2026-10-08 8:05 ` Jan Beulich
2026-10-07 10:40 ` [PATCH v3 06/18] x86/spec-ctrl: introduce Address Space Isolation command line option George Dunlap
2026-10-07 16:52 ` Roger Pau Monné
2026-10-12 12:47 ` George Dunlap
2026-10-08 8:24 ` Jan Beulich
2026-10-12 12:48 ` George Dunlap
2026-10-07 10:40 ` [PATCH v3 07/18] x86/hvm: introduce per-vCPU L3 page-table George Dunlap
2026-10-07 19:21 ` Roger Pau Monné
2026-10-08 20:55 ` George Dunlap
2026-10-08 8:40 ` Jan Beulich
2026-10-08 21:00 ` George Dunlap
2026-10-09 7:06 ` Jan Beulich
2026-10-07 10:40 ` [PATCH v3 08/18] x86/domain_page: prepare the mapcache for per-vCPU accounting George Dunlap
2026-10-08 8:22 ` Roger Pau Monné [this message]
2026-10-12 14:19 ` George Dunlap
2026-10-13 10:50 ` Roger Pau Monné
2026-10-12 13:48 ` Jan Beulich
2026-10-12 16:07 ` George Dunlap
2026-10-13 6:25 ` Jan Beulich
2026-10-13 9:53 ` George Dunlap
2026-10-13 11:56 ` Jan Beulich
2026-10-07 10:40 ` [PATCH v3 09/18] x86/domain_page: move struct mapcache_vcpu to struct arch_vcpu George Dunlap
2026-10-08 8:28 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 10/18] x86/domain_page: don't unmap a mapcache entry that is not present George Dunlap
2026-10-08 11:03 ` Roger Pau Monné
2026-10-13 10:33 ` George Dunlap
2026-10-13 10:55 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 11/18] x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains George Dunlap
2026-10-08 13:56 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 12/18] x86/mm: let Xen's page-table walker operate on a given root George Dunlap
2026-10-08 14:58 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 13/18] x86/mm: build and maintain a sparse view of the directmap George Dunlap
2026-10-07 15:20 ` George Dunlap
2026-10-08 18:02 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 14/18] x86/domain_page: key the directmap fast paths on the loaded view George Dunlap
2026-10-08 18:51 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 15/18] x86/mm: flush owned pages before reuse with the sparse directmap view George Dunlap
2026-10-08 19:00 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 16/18] x86/domain_page: keep the maphash's slots, not their mappings George Dunlap
2026-10-08 19:08 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 17/18] x86/mm: run guest contexts on the sparse directmap view George Dunlap
2026-10-09 7:58 ` Roger Pau Monné
2026-10-07 10:40 ` [PATCH v3 18/18] x86/spec-ctrl: wire the sparse directmap view into the asi= option George Dunlap
2026-10-08 19:21 ` Roger Pau Monné
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=asdSqe9KC_J3OLpv@Mac.lan \
--to=roger@xenproject.org \
--cc=andrew.cooper3@citrix.com \
--cc=gwd@xenproject.org \
--cc=jbeulich@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.