All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Roger Pau Monné" <roger@xenproject.org>
To: George Dunlap <gwd@xenproject.org>
Cc: xen-devel@lists.xenproject.org, Jan Beulich <jbeulich@suse.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Teddy Astie <teddy.astie@vates.tech>
Subject: Re: [PATCH v3 14/18] x86/domain_page: key the directmap fast paths on the loaded view
Date: Thu, 8 Oct 2026 20:51:05 +0200	[thread overview]
Message-ID: <asfmGUr17vgaXmYk@Mac.lan> (raw)
In-Reply-To: <20261007104051.48841-15-gwd@xenproject.org>

On Wed, Oct 07, 2026 at 11:40:47AM +0100, George Dunlap wrote:
> map_domain_page() returns directmap addresses whenever it can: for any
> MFN the directmap covers in release builds, and for vCPUs without a
> mapcache.  Once guest contexts may run on the sparse view of the
> directmap, modify this to take the fast path returning the directmap
> address only when the full directmap is loaded.
> 
> The release-build shortcut ahead of the vCPU lookup looks at the vCPU
> whose page-tables are loaded, without completing a lazy context
> switch: if that vCPU is not on the sparse view, its page-tables map
> the page through the directmap, and completing the switch would only
> move the CPU to the idle vCPU's page-tables, which map it too.
> 
> mapcache_current_vcpu() may complete the lazy context switch
> (sync_local_execstate()) and switch us onto the idle vCPU (which has a
> full view), so check again if we can use the directmap afterwards.
> 
> A vCPU on the sparse view always has a mapcache: HVM ones through
> per-vCPU page-tables, PV ones through the domain-wide mapcache, which
> mapcache_domain_init() now sets up for a domain using the sparse view in
> release builds too, rather than only when the directmap does not cover
> all memory.
> 
> map_domain_page_global() mappings must be valid in every context: use
> vmap() for all of them once a sparse view is configured, as
> arch_mfns_in_directmap() then says.
> 
> Add d->arch.sparse_dmap and sparse_dmap_active for this; nothing sets
> them yet.
> 
> No functional change.
> 
> Assisted-by: Claude Code:claude-opus-5-5
> Signed-off-by: George Dunlap <gwd@xenproject.org>
> ---
> Changes in v3:
> - New in this version.
> ---
>  xen/arch/x86/domain_page.c                  | 44 +++++++++++++++++++--
>  xen/arch/x86/include/asm/domain.h           |  6 +++
>  xen/arch/x86/include/asm/sparse-directmap.h |  8 ++++
>  xen/arch/x86/sparse-directmap.c             |  1 +
>  4 files changed, 56 insertions(+), 3 deletions(-)
> 
> diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c
> index 0500bb9dba..48f85b670d 100644
> --- a/xen/arch/x86/domain_page.c
> +++ b/xen/arch/x86/domain_page.c
> @@ -61,6 +61,16 @@ static inline struct vcpu *mapcache_current_vcpu(void)
>      return ACCESS_ONCE(this_cpu(pgtable_vcpu));
>  }
>  
> +/*
> + * Whether the directmap in @v's page-tables is the sparse view, which maps
> + * none of the heap's memory beyond the xenheap: the mapcache must serve
> + * every mapping then.
> + */
> +static bool on_sparse_dmap(const struct vcpu *v)
> +{
> +    return sparse_dmap_active && v->domain->arch.sparse_dmap;
> +}
> +
>  /*
>   * The accounting for v's mapcache: its own for a vCPU-PT domain, the domain's
>   * otherwise, in which case *dcache is set so the caller takes the domain-wide
> @@ -142,17 +152,39 @@ void *map_domain_page(mfn_t mfn)
>  
>  #ifdef NDEBUG
>      if ( mfn_x(mfn) <= PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) )
> -        return mfn_to_virt(mfn_x(mfn));
> +    {
> +        const struct vcpu *pt_vcpu = this_cpu(pgtable_vcpu);
> +
> +        /*
> +         * The page-tables loaded map @mfn through the directmap, unless they
> +         * carry the sparse view.  Don't complete a lazy context switch to
> +         * find out: it would only move this CPU to the idle vCPU's
> +         * page-tables, which map @mfn too.
> +         */
> +        if ( !pt_vcpu || !on_sparse_dmap(pt_vcpu) )
> +            return mfn_to_virt(mfn_x(mfn));
> +    }
>  #endif
>  
> +    /* No vCPU (early boot), or no mapcache: the full directmap is loaded. */
>      v = mapcache_current_vcpu();
>      if ( !v )
>          return mfn_to_virt(mfn_x(mfn));
>  
> +#ifdef NDEBUG
> +    /* The lookup may have completed a lazy switch off the sparse view. */
> +    if ( !on_sparse_dmap(v) &&
> +         mfn_x(mfn) <= PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) )
> +        return mfn_to_virt(mfn_x(mfn));
> +#endif
> +
>      cache = vcpu_mapcache(v, &dcache);
>      vcache = &v->arch.mapcache;
>      if ( !cache->inuse )
> +    {
> +        ASSERT(!on_sparse_dmap(v));
>          return mfn_to_virt(mfn_x(mfn));
> +    }
>  
>      perfc_incr(map_domain_page_count);
>  
> @@ -307,7 +339,9 @@ void mapcache_domain_init(struct domain *d)
>      ASSERT(is_pv_domain(d));
>  
>  #ifdef NDEBUG
> -    if ( !mem_hotplug && max_page <= PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) )
> +    /* Not needed when the directmap loaded for the domain covers everything. */
> +    if ( !d->arch.sparse_dmap && !mem_hotplug &&
> +         max_page <= PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) )
>          return;
>  #endif
>  
> @@ -392,8 +426,12 @@ void *map_domain_page_global(mfn_t mfn)
>               system_state < SYS_STATE_active) ||
>              local_irq_is_enabled()));
>  
> +    /*
> +     * Global mappings must hold in every context: a sparse view of the
> +     * directmap rules the directmap out.
> +     */
>  #ifdef NDEBUG
> -    if ( mfn_x(mfn) <= PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) )
> +    if ( arch_mfns_in_directmap(mfn_x(mfn), 1) )
>          return mfn_to_virt(mfn_x(mfn));
>  #endif
>  
> diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/domain.h
> index 80415ab2c2..76a22b4040 100644
> --- a/xen/arch/x86/include/asm/domain.h
> +++ b/xen/arch/x86/include/asm/domain.h
> @@ -499,6 +499,12 @@ struct arch_domain
>       */
>      bool vcpu_pt;
>  
> +    /*
> +     * Root page-tables carry the sparse view of the directmap, once it is
> +     * active (see asm/sparse-directmap.h).
> +     */
> +    bool sparse_dmap;

What do you mean with "once it is active"?  Either a domain is using
the sparse direct map or not, but it shouldn't have an external
dependency on anything.  Or maybe I'm reading the comment the wrong
way.

> +
>      /* Emulated devices enabled bitmap. */
>      uint32_t emulation_flags;
>  } __cacheline_aligned;
> diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/include/asm/sparse-directmap.h
> index 48c1f3c951..512f8064d6 100644
> --- a/xen/arch/x86/include/asm/sparse-directmap.h
> +++ b/xen/arch/x86/include/asm/sparse-directmap.h
> @@ -22,6 +22,12 @@ extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm;
>  /* Root of the sparse view, once built (NULL before, or if not configured). */
>  extern l4_pgentry_t *sparse_dmap_root;
>  
> +/*
> + * Set once guest contexts run on the sparse view: from then on, the root
> + * page-tables of domains with d->arch.sparse_dmap carry it.
> + */
> +extern bool sparse_dmap_active;

Shouldn't sparse_dmap_active be opt_sparse_dmap_pv ||
opt_sparse_dmap_hvm?  I'm afraid I don't understand the point of
tracking whether there are domain using the sparse direct map or not,
that's already tracked by d->arch.sparse_dmap?

Thanks, Roger.


  reply	other threads:[~2026-10-08 18:51 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é
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é [this message]
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=asfmGUr17vgaXmYk@Mac.lan \
    --to=roger@xenproject.org \
    --cc=andrew.cooper3@citrix.com \
    --cc=gwd@xenproject.org \
    --cc=jbeulich@suse.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.