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

Re: [PATCH 11/13] x86/HVM: introduce hvm_load_vcpu_instance()


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Thu, 26 Nov 2026 14:43:39 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:Content-Language:References:Cc:To:From:Subject:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Paul Durrant <paul@xxxxxxx>
  • Delivery-date: Thu, 26 Nov 2026 13:43:44 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 26.11.2026 14:41, Jan Beulich wrote:
> Use this to remove open-coding of domain_vcpu().
> 
> Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
> ---
> lapic_load_{hidden,regs}() are left alone, as I fear analysis tools would
> otherwise pick on the missing NULL checks.

I'm sorry, I should have Cc-ed Paul as well.

Jan

> --- a/xen/arch/x86/cpu/mcheck/vmce.c
> +++ b/xen/arch/x86/cpu/mcheck/vmce.c
> @@ -364,15 +364,14 @@ static int cf_check vmce_save_vcpu_ctxt(
>  
>  static int cf_check vmce_load_vcpu_ctxt(struct domain *d, 
> hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h);
> -    struct vcpu *v;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct hvm_vmce_vcpu ctxt;
>      int err;
>  
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          err = -EINVAL;
>      }
>      else
> --- a/xen/arch/x86/hvm/hvm.c
> +++ b/xen/arch/x86/hvm/hvm.c
> @@ -804,14 +804,13 @@ static int cf_check hvm_save_tsc_adjust(
>  
>  static int cf_check hvm_load_tsc_adjust(struct domain *d, 
> hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h);
> -    struct vcpu *v;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct hvm_tsc_adjust ctxt;
>  
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> @@ -1018,18 +1017,16 @@ unsigned long hvm_cr4_guest_valid_bits(c
>  static int cf_check hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t 
> *h)
>  {
>      const struct cpu_policy *p = d->arch.cpu_policy;
> -    unsigned int vcpuid = hvm_load_instance(h);
> -    struct vcpu *v;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct hvm_hw_cpu ctxt;
>      struct segment_register seg;
>      const char *errstr;
>      unsigned long valid;
>  
> -    /* Which vcpu is this? */
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%u has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> @@ -1267,19 +1264,17 @@ CHECK_FIELD_(struct, xsave_hdr, reserved
>  static int cf_check hvm_load_cpu_xsave_states(
>      struct domain *d, hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid, size;
> +    unsigned int size;
>      int err;
> -    struct vcpu *v;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct hvm_hw_cpu_xsave *ctxt;
>      const struct hvm_save_descriptor *desc;
>      unsigned int i, desc_start, desc_length;
>  
> -    /* Which vcpu is this? */
> -    vcpuid = hvm_load_instance(h);
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> @@ -1298,14 +1293,14 @@ static int cf_check hvm_load_cpu_xsave_s
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore: not enough data left to read xsave 
> descriptor\n",
> -               d->domain_id, vcpuid);
> +               d->domain_id, v->vcpu_id);
>          return -ENODATA;
>      }
>      if ( desc->length + sizeof (*desc) > h->size - h->cur)
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore: not enough data left to read %u xsave 
> bytes\n",
> -               d->domain_id, vcpuid, desc->length);
> +               d->domain_id, v->vcpu_id, desc->length);
>          return -ENODATA;
>      }
>      if ( desc->length < offsetof(struct hvm_hw_cpu_xsave, save_area) +
> @@ -1313,7 +1308,7 @@ static int cf_check hvm_load_cpu_xsave_s
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore mismatch: xsave length %u < %zu\n",
> -               d->domain_id, vcpuid, desc->length,
> +               d->domain_id, v->vcpu_id, desc->length,
>                 offsetof(struct hvm_hw_cpu_xsave,
>                          save_area) + XSTATE_AREA_MIN_SIZE);
>          return -EINVAL;
> @@ -1331,7 +1326,7 @@ static int cf_check hvm_load_cpu_xsave_s
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore: inconsistent xsave state (feat=%#"PRIx64
>                 " accum=%#"PRIx64" xcr0=%#"PRIx64" bv=%#"PRIx64" err=%d)\n",
> -               d->domain_id, vcpuid, ctxt->xfeature_mask, ctxt->xcr0_accum,
> +               d->domain_id, v->vcpu_id, ctxt->xfeature_mask, 
> ctxt->xcr0_accum,
>                 ctxt->xcr0, ctxt->save_area.xsave_hdr.xstate_bv, err);
>          return err;
>      }
> @@ -1350,13 +1345,13 @@ static int cf_check hvm_load_cpu_xsave_s
>              {
>                  printk(XENLOG_G_WARNING
>                         "HVM%d.%u restore mismatch: xsave length %#x > %#x 
> (non-zero data at %#x)\n",
> -                       d->domain_id, vcpuid, desc->length, size, i);
> +                       d->domain_id, v->vcpu_id, desc->length, size, i);
>                  return -EOPNOTSUPP;
>              }
>          }
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%u restore mismatch: xsave length %#x > %#x\n",
> -               d->domain_id, vcpuid, desc->length, size);
> +               d->domain_id, v->vcpu_id, desc->length, size);
>          /* Rewind desc_length to ignore the extraneous zeros. */
>          desc_length = size;
>      }
> @@ -1365,14 +1360,14 @@ static int cf_check hvm_load_cpu_xsave_s
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%u restore: compressed xsave state not supported\n",
> -               d->domain_id, vcpuid);
> +               d->domain_id, v->vcpu_id);
>          return -EOPNOTSUPP;
>      }
>      else if ( desc_length != size )
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%u restore mismatch: xsave length %#x != %#x\n",
> -               d->domain_id, vcpuid, desc_length, size);
> +               d->domain_id, v->vcpu_id, desc_length, size);
>          return -EINVAL;
>      }
>      /* Checking finished */
> @@ -1481,15 +1476,15 @@ static int cf_check hvm_save_cpu_msrs(st
>  
>  static int cf_check hvm_load_cpu_msrs(struct domain *d, hvm_domain_context_t 
> *h)
>  {
> -    unsigned int i, vcpuid = hvm_load_instance(h);
> -    struct vcpu *v;
> +    unsigned int i;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      const struct hvm_save_descriptor *desc;
>      struct hvm_msr *ctxt;
>  
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> @@ -1499,21 +1494,21 @@ static int cf_check hvm_load_cpu_msrs(st
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore: not enough data left to read MSR 
> descriptor\n",
> -               d->domain_id, vcpuid);
> +               d->domain_id, v->vcpu_id);
>          return -ENODATA;
>      }
>      if ( desc->length + sizeof (*desc) > h->size - h->cur)
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore: not enough data left to read %u MSR 
> bytes\n",
> -               d->domain_id, vcpuid, desc->length);
> +               d->domain_id, v->vcpu_id, desc->length);
>          return -ENODATA;
>      }
>      if ( desc->length < HVM_CPU_MSR_SIZE(1) )
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore mismatch: MSR length %u < %zu\n",
> -               d->domain_id, vcpuid, desc->length, HVM_CPU_MSR_SIZE(1));
> +               d->domain_id, v->vcpu_id, desc->length, HVM_CPU_MSR_SIZE(1));
>          return -EINVAL;
>      }
>  
> @@ -1525,7 +1520,7 @@ static int cf_check hvm_load_cpu_msrs(st
>      {
>          printk(XENLOG_G_WARNING
>                 "HVM%d.%d restore mismatch: MSR length %u != %zu\n",
> -               d->domain_id, vcpuid, desc->length,
> +               d->domain_id, v->vcpu_id, desc->length,
>                 HVM_CPU_MSR_SIZE(ctxt->count));
>          return -EOPNOTSUPP;
>      }
> --- a/xen/arch/x86/hvm/mtrr.c
> +++ b/xen/arch/x86/hvm/mtrr.c
> @@ -735,15 +735,15 @@ static int cf_check hvm_save_mtrr_msr(st
>  
>  static int cf_check hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t 
> *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h), i;
> -    struct vcpu *v = domain_vcpu(d, vcpuid);
> +    unsigned int i;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct mtrr_state *m;
>      struct hvm_hw_mtrr hw_mtrr;
>  
>      if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> --- a/xen/arch/x86/hvm/viridian/viridian.c
> +++ b/xen/arch/x86/hvm/viridian/viridian.c
> @@ -1152,14 +1152,13 @@ static int cf_check viridian_save_vcpu_c
>  static int cf_check viridian_load_vcpu_ctxt(
>      struct domain *d, hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h);
> -    struct vcpu *v;
> +    struct vcpu *v = hvm_load_vcpu_instance(d, h);
>      struct hvm_viridian_vcpu_context ctxt;
>  
> -    if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
> +    if ( !v )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> --- a/xen/arch/x86/hvm/vlapic.c
> +++ b/xen/arch/x86/hvm/vlapic.c
> @@ -1577,16 +1577,17 @@ static void lapic_load_fixup(struct vlap
>  }
>  
>  
> -static int lapic_check_common(const struct domain *d, unsigned int vcpuid)
> +static int lapic_check_common(const struct domain *d,
> +                              const hvm_domain_context_t *h)
>  {
>      if ( !has_vlapic(d) )
>          return -ENODEV;
>  
>      /* Which vlapic to load? */
> -    if ( !domain_vcpu(d, vcpuid) )
> +    if ( !hvm_load_vcpu_instance(d, h) )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vCPU %u\n",
> -                d->domain_id, vcpuid);
> +                d->domain_id, hvm_load_instance(h));
>          return -EINVAL;
>      }
>  
> @@ -1596,9 +1597,8 @@ static int lapic_check_common(const stru
>  static int cf_check lapic_check_hidden(const struct domain *d,
>                                         hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h);
>      struct hvm_hw_lapic s;
> -    int rc = lapic_check_common(d, vcpuid);
> +    int rc = lapic_check_common(d, h);
>  
>      if ( rc )
>          return rc;
> @@ -1638,10 +1638,9 @@ static int cf_check lapic_load_hidden(st
>  static int cf_check lapic_check_regs(const struct domain *d,
>                                       hvm_domain_context_t *h)
>  {
> -    unsigned int vcpuid = hvm_load_instance(h);
>      int rc;
>  
> -    if ( (rc = lapic_check_common(d, vcpuid)) )
> +    if ( (rc = lapic_check_common(d, h)) )
>          return rc;
>  
>      if ( !hvm_get_entry(LAPIC_REGS, h) )
> --- a/xen/arch/x86/include/asm/hvm/save.h
> +++ b/xen/arch/x86/include/asm/hvm/save.h
> @@ -6,8 +6,8 @@
>  #ifndef __XEN_HVM_SAVE_H__
>  #define __XEN_HVM_SAVE_H__
>  
> -#include <xen/types.h>
>  #include <xen/init.h>
> +#include <xen/sched.h>
>  #include <public/xen.h>
>  #include <public/hvm/save.h>
>  
> @@ -97,6 +97,13 @@ static inline unsigned int hvm_load_inst
>      return d->instance;
>  }
>  
> +/* Unmarshalling: Wrapper around the above for per-vCPU instances. */
> +static inline struct vcpu *hvm_load_vcpu_instance(
> +    const struct domain *d, const struct hvm_domain_context *h)
> +{
> +    return domain_vcpu(d, hvm_load_instance(h));
> +}
> +
>  /* Handler types for different types of save-file entry. 
>   * The save handler may save multiple instances of a type into the buffer;
>   * the load handler will be called once for each instance found when
> 




 


Rackspace

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