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

Re: [PATCH v2 3/4] xen: move alloc/free_vcpu_struct() to common code


  • To: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Thu, 18 Dec 2025 18:34:57 +0000
  • 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=I4u6/yMZAyGVb2ODTm32SHrOl5NZ5ws1xt8qRPWyPuI=; b=hs3/Zy6QW2oqReBkFUM1mCUBV+G4ESfd3SmjX9xuvzJ6b/q78XBqB4EqJ+assoSTAJ/rOFVs2EncQBTuDQDqiLO/F3ph4i0GZ1lPKFVTB9SNwTREUsb06INNab5mCAuAjRHSxYXq4bhISpF+P8Am1f8X3qA0F55zHFkNluoyE0jvQBRwh6R2xP9Q/BGYQJiMLElDMx/KOj1p6ZExyyDXlkZHqlty751wtSjdb97SPKOwdLihVUO6Hg4JpmjH/AwgfVJI/I8E+F/ciosV8DZCn+Ga2FLCzPTsRejsio6cQekbFdhROo+ddYJBH3I5TO+raeZl2H+BfNqvD1RKeBlccQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gCX5zy37EZpaeZKqKC1vDsXsjqgWJ/83frrgGFdpGG+xfpcUgVKvYr20dLnvAmSjprDxBzUkFMdQpAxyZqjS77BNQrdyZZpduTCHZpd5SoaL6JOKEk49KDR3yYeXJTJgV6Bft+IRsWVn5SuR/tfnIj9t93BgG2tWE3TbVSJIwspUBIgpX4Vgq0d8+w3rXGIJZDuie87QyN0l8O6oo2NZ88mD+b7iNJ1Zra5bnyxjVYAuimxzoqRLJpb8oalod1naT1+xNCizkU5/4Z4lvI8oN+ZhIKO9MUsk4ixjm6vZrDOspSI6bNAujWt1iaFt4uAUPNGR5KqVIY7JxHeWJPjsEQ==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, Timothy Pearson <tpearson@xxxxxxxxxxxxxxxxxxxxx>, Alistair Francis <alistair.francis@xxxxxxx>, Bob Eshleman <bobbyeshleman@xxxxxxxxx>, Connor Davis <connojdavis@xxxxxxxxx>
  • Delivery-date: Thu, 18 Dec 2025 18:35:31 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 18/12/2025 5:28 pm, Oleksii Kurochko wrote:
> alloc_vcpu_struct() and free_vcpu_struct() contain little
> architecture-specific logic and are suitable for sharing across
> architectures. Move both helpers to common code.
>
> To support the remaining architectural differences, introduce
> arch_vcpu_struct_memflags(), allowing architectures to override the
> memory flags passed to alloc_xenheap_pages(). This is currently needed
> by x86, which may require MEMF_bits(32) for HVM guests using shadow
> paging.
>
> The ARM implementation of alloc/free_vcpu_struct() is removed and
> replaced by the common version. Stub implementations are also dropped
> from PPC and RISC-V.
>
> Finally, make alloc_vcpu_struct() and free_vcpu_struct() static to
> common/domain.c, as they are no longer used outside common code.
>
> No functional changes.
>
> Signed-off-by: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
> ---
> Changes in v2:
>  - Rework alloc/free_vcpu_struct() to work with only one page.
>  - Return back the comment about the restriction inside x86's
>    arch_vcpu_struct_memflags().
>  - Drop MAX_PAGES_PER_VCPU.
> ---
>  xen/arch/arm/domain.c             | 17 -----------------
>  xen/arch/ppc/stubs.c              | 10 ----------
>  xen/arch/riscv/stubs.c            | 10 ----------
>  xen/arch/x86/domain.c             | 17 ++---------------
>  xen/arch/x86/include/asm/domain.h |  3 +++
>  xen/common/domain.c               | 20 ++++++++++++++++++++
>  xen/include/xen/domain.h          |  4 ----
>  7 files changed, 25 insertions(+), 56 deletions(-)

Much nicer.

> diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
> index 7632d5e2d6..68c7503eda 100644
> --- a/xen/arch/x86/domain.c
> +++ b/xen/arch/x86/domain.c
> @@ -493,28 +493,15 @@ unsigned int arch_domain_struct_memflags(void)
>      return MEMF_bits(bits);
>  }
>  
> -struct vcpu *alloc_vcpu_struct(const struct domain *d)
> +unsigned int arch_vcpu_struct_memflags(const struct domain *d)
>  {
> -    struct vcpu *v;
>      /*
>       * This structure contains embedded PAE PDPTEs, used when an HVM guest
>       * runs on shadow pagetables outside of 64-bit mode. In this case the CPU
>       * may require that the shadow CR3 points below 4GB, and hence the whole
>       * structure must satisfy this restriction. Thus we specify 
> MEMF_bits(32).
>       */
> -    unsigned int memflags =
> -        (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
> -
> -    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
> -    v = alloc_xenheap_pages(0, memflags);
> -    if ( v != NULL )
> -        clear_page(v);
> -    return v;
> -}
> -
> -void free_vcpu_struct(struct vcpu *v)
> -{
> -    free_xenheap_page(v);
> +    return (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
>  }
>  
>  /* Initialise various registers to their architectural INIT/RESET state. */
> diff --git a/xen/arch/x86/include/asm/domain.h 
> b/xen/arch/x86/include/asm/domain.h
> index 7e5cbd11a4..576f9202b4 100644
> --- a/xen/arch/x86/include/asm/domain.h
> +++ b/xen/arch/x86/include/asm/domain.h
> @@ -15,6 +15,9 @@
>  unsigned int arch_domain_struct_memflags(void);
>  #define arch_domain_struct_memflags arch_domain_struct_memflags
>  
> +unsigned int arch_vcpu_struct_memflags(const struct domain *d);
> +#define arch_vcpu_struct_memflags arch_vcpu_struct_memflags
> +

Given the single caller and simplicity, this would be better as a static
inline, except it can't yet for header reasons.  So, instead, I'd
suggest simply:

/*
 * $COMMENT
 */
#define arch_vcpu_struct_memflags(d) \
(is_hvm_domain(d) && paging_mode_shadow(d) ? MEMF_bits(32) : 0)

This form does double expand 'd'.  The more complex form would be to
match
https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=c37bcb35f928c81cbeea7c05f86b6779f8a2b8c4
with a local variable.

> diff --git a/xen/common/domain.c b/xen/common/domain.c
> index 93c71bc766..92fc0684fc 100644
> --- a/xen/common/domain.c
> +++ b/xen/common/domain.c
> @@ -392,6 +392,26 @@ static int vcpu_teardown(struct vcpu *v)
>      return 0;
>  }
>  
> +static struct vcpu *alloc_vcpu_struct(const struct domain *d)
> +{
> +#ifndef arch_vcpu_struct_memflags
> +# define arch_vcpu_struct_memflags(d) 0

((void)d, 0)

~Andrew



 


Rackspace

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