From: "Jan Beulich" Subject: [PATCH 1/3] x86: fix guest CPUID handling Date: Wed, 30 Apr 2014 15:22:31 +0100 Message-ID: <53612347020000780000DD53@mail.emea.novell.com> References: <536120AF020000780000DD1B@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part6A585537.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WfVPA-0002mS-Gi for xen-devel@lists.xenproject.org; Wed, 30 Apr 2014 14:22:36 +0000 In-Reply-To: <536120AF020000780000DD1B@mail.emea.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Ian Campbell , Keir Fraser , Ian Jackson , Tim Deegan List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part6A585537.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The way XEN_DOMCTL_set_cpuid got handled so far allowed for surprises to the caller. With this set of operations - set leaf A (using array index 0) - set leaf B (using array index 1) - clear leaf A (clearing array index 0) - set leaf B (using array index 0) - clear leaf B (clearing array index 0) the entry for leaf B at array index 1 would still be in place, while the caller would expect it to be cleared. While looking at the use sites of d->arch.cpuid[] I also noticed that the allocation of the array needlessly uses the zeroing form - the relevant fields of the array elements get set in a loop immediately following the allocation. Signed-off-by: Jan Beulich --- 2014-04-16.orig/xen/arch/x86/domain.c 2014-02-05 15:21:00.0000000= 00 +0100 +++ 2014-04-16/xen/arch/x86/domain.c 2014-04-30 13:53:18.000000000 = +0200 @@ -549,7 +549,7 @@ int arch_domain_create(struct domain *d, =20 if ( !is_idle_domain(d) ) { - d->arch.cpuids =3D xzalloc_array(cpuid_input_t, MAX_CPUID_INPUT); + d->arch.cpuids =3D xmalloc_array(cpuid_input_t, MAX_CPUID_INPUT); rc =3D -ENOMEM; if ( d->arch.cpuids =3D=3D NULL ) goto fail; --- 2014-04-16.orig/xen/arch/x86/domctl.c 2014-02-05 15:21:01.0000000= 00 +0100 +++ 2014-04-16/xen/arch/x86/domctl.c 2014-04-30 13:43:30.000000000 = +0200 @@ -1006,14 +1006,18 @@ long arch_do_domctl( case XEN_DOMCTL_set_cpuid: { xen_domctl_cpuid_t *ctl =3D &domctl->u.cpuid; - cpuid_input_t *cpuid =3D NULL;=20 + cpuid_input_t *cpuid, *unused =3D NULL; =20 for ( i =3D 0; i < MAX_CPUID_INPUT; i++ ) { cpuid =3D &d->arch.cpuids[i]; =20 if ( cpuid->input[0] =3D=3D XEN_CPUID_INPUT_UNUSED ) - break; + { + if ( !unused ) + unused =3D cpuid; + continue; + } =20 if ( (cpuid->input[0] =3D=3D ctl->input[0]) && ((cpuid->input[1] =3D=3D XEN_CPUID_INPUT_UNUSED) || @@ -1021,15 +1025,12 @@ long arch_do_domctl( break; } =20 - if ( i =3D=3D MAX_CPUID_INPUT ) - { - ret =3D -ENOENT; - } + if ( i < MAX_CPUID_INPUT ) + *cpuid =3D *ctl; + else if ( unused ) + *unused =3D *ctl; else - { - memcpy(cpuid, ctl, sizeof(cpuid_input_t)); - ret =3D 0; - } + ret =3D -ENOENT; } break; =20 --=__Part6A585537.1__= Content-Type: text/plain; name="x86-domctl-cpuid.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-domctl-cpuid.patch" x86: fix guest CPUID handling=0A=0AThe way XEN_DOMCTL_set_cpuid got = handled so far allowed for surprises=0Ato the caller. With this set of = operations=0A- set leaf A (using array index 0)=0A- set leaf B (using = array index 1)=0A- clear leaf A (clearing array index 0)=0A- set leaf B = (using array index 0)=0A- clear leaf B (clearing array index 0)=0Athe = entry for leaf B at array index 1 would still be in place, while=0Athe = caller would expect it to be cleared.=0A=0AWhile looking at the use sites = of d->arch.cpuid[] I also noticed that=0Athe allocation of the array = needlessly uses the zeroing form - the=0Arelevant fields of the array = elements get set in a loop immediately=0Afollowing the allocation.=0A=0ASig= ned-off-by: Jan Beulich =0A=0A--- 2014-04-16.orig/xen/ar= ch/x86/domain.c 2014-02-05 15:21:00.000000000 +0100=0A+++ 2014-04-16/xen/ar= ch/x86/domain.c 2014-04-30 13:53:18.000000000 +0200=0A@@ -549,7 +549,7 @@ = int arch_domain_create(struct domain *d,=0A =0A if ( !is_idle_domain(d)= )=0A {=0A- d->arch.cpuids =3D xzalloc_array(cpuid_input_t, = MAX_CPUID_INPUT);=0A+ d->arch.cpuids =3D xmalloc_array(cpuid_input_t= , MAX_CPUID_INPUT);=0A rc =3D -ENOMEM;=0A if ( d->arch.cpui= ds =3D=3D NULL )=0A goto fail;=0A--- 2014-04-16.orig/xen/arch/x= 86/domctl.c 2014-02-05 15:21:01.000000000 +0100=0A+++ 2014-04-16/xen/ar= ch/x86/domctl.c 2014-04-30 13:43:30.000000000 +0200=0A@@ -1006,14 +1006,18 = @@ long arch_do_domctl(=0A case XEN_DOMCTL_set_cpuid:=0A {=0A = xen_domctl_cpuid_t *ctl =3D &domctl->u.cpuid;=0A- cpuid_input_t = *cpuid =3D NULL; =0A+ cpuid_input_t *cpuid, *unused =3D NULL;=0A = =0A for ( i =3D 0; i < MAX_CPUID_INPUT; i++ )=0A {=0A = cpuid =3D &d->arch.cpuids[i];=0A =0A if ( cpuid->input[0= ] =3D=3D XEN_CPUID_INPUT_UNUSED )=0A- break;=0A+ = {=0A+ if ( !unused )=0A+ unused =3D = cpuid;=0A+ continue;=0A+ }=0A =0A if = ( (cpuid->input[0] =3D=3D ctl->input[0]) &&=0A ((cpuid->in= put[1] =3D=3D XEN_CPUID_INPUT_UNUSED) ||=0A@@ -1021,15 +1025,12 @@ long = arch_do_domctl(=0A break;=0A }=0A =0A- = if ( i =3D=3D MAX_CPUID_INPUT )=0A- {=0A- ret =3D = -ENOENT;=0A- }=0A+ if ( i < MAX_CPUID_INPUT )=0A+ = *cpuid =3D *ctl;=0A+ else if ( unused )=0A+ *unused =3D = *ctl;=0A else=0A- {=0A- memcpy(cpuid, ctl, = sizeof(cpuid_input_t));=0A- ret =3D 0;=0A- }=0A+ = ret =3D -ENOENT;=0A }=0A break;=0A =0A --=__Part6A585537.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part6A585537.1__=--