xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Paul Durrant <Paul.Durrant@citrix.com>
To: 'Jan Beulich' <JBeulich@suse.com>,
	xen-devel <xen-devel@lists.xenproject.org>
Cc: Andrew Cooper <Andrew.Cooper3@citrix.com>
Subject: Re: [PATCH] x86/HVM: improve a few state load checks
Date: Mon, 16 Jul 2018 14:05:42 +0000	[thread overview]
Message-ID: <25ec81ac057b4143859e1479bfa32d49@AMSPEX02CL02.citrite.net> (raw)
In-Reply-To: <5B4CA48002000078001D470D@prv1-mh.provo.novell.com>

> -----Original Message-----
> From: Jan Beulich [mailto:JBeulich@suse.com]
> Sent: 16 July 2018 14:58
> To: xen-devel <xen-devel@lists.xenproject.org>
> Cc: Andrew Cooper <Andrew.Cooper3@citrix.com>; Paul Durrant
> <Paul.Durrant@citrix.com>
> Subject: [PATCH] x86/HVM: improve a few state load checks
> 
> Using plain int for instance numbers looks quite dangerous without
> being aware that hvm_load_instance() returns an unsigned quantity. Make
> this more explicit. Also replace uint16_t uses by unsigned int.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> 
> --- a/xen/arch/x86/hvm/hvm.c
> +++ b/xen/arch/x86/hvm/hvm.c
> @@ -976,14 +976,13 @@ unsigned long hvm_cr4_guest_valid_bits(c
> 
>  static int hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
>  {
> -    int vcpuid;
> +    unsigned int vcpuid = hvm_load_instance(h);
>      struct vcpu *v;
>      struct hvm_hw_cpu ctxt;
>      struct segment_register seg;
>      const char *errstr;
> 
>      /* Which vcpu is this? */
> -    vcpuid = hvm_load_instance(h);
>      if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )

This open coded pattern:

vcpuid = hvm_load_instance(h);
if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
{ ...

seems to be repeated an awful lot. Is it time, perhaps, to introduce a helper function that incorporates the check?

  Paul

>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%u has no vcpu%u\n",
> --- a/xen/arch/x86/hvm/mtrr.c
> +++ b/xen/arch/x86/hvm/mtrr.c
> @@ -768,7 +768,7 @@ static int hvm_save_mtrr_msr(struct doma
> 
>  static int hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t
> *h)
>  {
> -    int vcpuid, i;
> +    unsigned int vcpuid, i;
>      struct vcpu *v;
>      struct mtrr_state *mtrr_state;
>      struct hvm_hw_mtrr hw_mtrr;
> --- a/xen/arch/x86/hvm/viridian.c
> +++ b/xen/arch/x86/hvm/viridian.c
> @@ -1048,11 +1048,10 @@ static int viridian_save_vcpu_ctxt(struc
> 
>  static int viridian_load_vcpu_ctxt(struct domain *d, hvm_domain_context_t
> *h)
>  {
> -    int vcpuid;
> +    unsigned int vcpuid = hvm_load_instance(h);
>      struct vcpu *v;
>      struct hvm_viridian_vcpu_context ctxt;
> 
> -    vcpuid = hvm_load_instance(h);
>      if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no vcpu%u\n",
> --- a/xen/arch/x86/hvm/vlapic.c
> +++ b/xen/arch/x86/hvm/vlapic.c
> @@ -1507,7 +1507,7 @@ static void lapic_load_fixup(struct vlap
> 
>  static int lapic_load_hidden(struct domain *d, hvm_domain_context_t *h)
>  {
> -    uint16_t vcpuid;
> +    unsigned int vcpuid = hvm_load_instance(h);
>      struct vcpu *v;
>      struct vlapic *s;
> 
> @@ -1515,7 +1515,6 @@ static int lapic_load_hidden(struct doma
>          return -ENODEV;
> 
>      /* Which vlapic to load? */
> -    vcpuid = hvm_load_instance(h);
>      if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no apic%u\n",
> @@ -1542,7 +1541,7 @@ static int lapic_load_hidden(struct doma
> 
>  static int lapic_load_regs(struct domain *d, hvm_domain_context_t *h)
>  {
> -    uint16_t vcpuid;
> +    unsigned int vcpuid = hvm_load_instance(h);
>      struct vcpu *v;
>      struct vlapic *s;
> 
> @@ -1550,7 +1549,6 @@ static int lapic_load_regs(struct domain
>          return -ENODEV;
> 
>      /* Which vlapic to load? */
> -    vcpuid = hvm_load_instance(h);
>      if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
>      {
>          dprintk(XENLOG_G_ERR, "HVM restore: dom%d has no apic%u\n",
> --- a/xen/arch/x86/hvm/vpic.c
> +++ b/xen/arch/x86/hvm/vpic.c
> @@ -393,13 +393,12 @@ static int vpic_save(struct domain *d, h
>  static int vpic_load(struct domain *d, hvm_domain_context_t *h)
>  {
>      struct hvm_hw_vpic *s;
> -    uint16_t inst;
> +    unsigned int inst = hvm_load_instance(h);
> 
>      if ( !has_vpic(d) )
>          return -ENODEV;
> 
>      /* Which PIC is this? */
> -    inst = hvm_load_instance(h);
>      if ( inst > 1 )
>          return -EINVAL;
>      s = &d->arch.hvm_domain.vpic[inst];
> --- a/xen/include/asm-x86/hvm/save.h
> +++ b/xen/include/asm-x86/hvm/save.h
> @@ -84,10 +84,10 @@ void _hvm_read_entry(struct hvm_domain_c
>      _hvm_load_entry(_x, _h, _dst, 0)
> 
>  /* Unmarshalling: what is the instance ID of the next entry? */
> -static inline uint16_t hvm_load_instance(struct hvm_domain_context *h)
> +static inline unsigned int hvm_load_instance(const struct
> hvm_domain_context *h)
>  {
> -    struct hvm_save_descriptor *d
> -        = (struct hvm_save_descriptor *)&h->data[h->cur];
> +    const struct hvm_save_descriptor *d = (const void *)&h->data[h->cur];
> +
>      return d->instance;
>  }
> 
> 
> 


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-07-16 14:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-16 13:58 [PATCH] x86/HVM: improve a few state load checks Jan Beulich
2018-07-16 14:01 ` Andrew Cooper
2018-07-16 14:05 ` Paul Durrant [this message]
2018-07-16 14:07   ` Andrew Cooper
2018-07-16 14:09     ` Paul Durrant

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=25ec81ac057b4143859e1479bfa32d49@AMSPEX02CL02.citrite.net \
    --to=paul.durrant@citrix.com \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).