From: "Jan Beulich" Subject: [PATCH v2] hvm/vpmu: Prevent dump handlers from incorrectly mutating state Date: Thu, 12 Sep 2013 08:45:02 +0100 Message-ID: <52318D1F02000078000F29E7@nat28.tlf.novell.com> References: <1378918711-21543-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part95A6FBEE.3__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VK1aT-0007n0-9x for xen-devel@lists.xenproject.org; Thu, 12 Sep 2013 07:45:13 +0000 In-Reply-To: <1378918711-21543-1-git-send-email-andrew.cooper3@citrix.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: Andrew Cooper , xen-devel Cc: Keir Fraser , suravee.suthikulpanit@amd.com, Jacob Shin , Eddie Dong , Jun Nakajima , Boris Ostrovsky 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. --=__Part95A6FBEE.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Discovered by Coverity, CID 1055181 core2_vpmu_dump() was incorrectly setting VPMU_CONTEXT_LOADED when it was intending to check for it. Signed-off-by: Andrew Cooper This would have been avoided if the dump function declared all its pointers "const" - doing this now (also in SVM). Also fixing some indentation issues at once. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/svm/vpmu.c +++ b/xen/arch/x86/hvm/svm/vpmu.c @@ -415,10 +415,10 @@ static void amd_vpmu_destroy(struct vcpu } =20 /* VPMU part of the 'q' keyhandler */ -static void amd_vpmu_dump(struct vcpu *v) +static void amd_vpmu_dump(const struct vcpu *v) { - struct vpmu_struct *vpmu =3D vcpu_vpmu(v); - struct amd_vpmu_context *ctxt =3D vpmu->context; + const struct vpmu_struct *vpmu =3D vcpu_vpmu(v); + const struct amd_vpmu_context *ctxt =3D vpmu->context; unsigned int i; =20 printk(" VPMU state: 0x%x ", vpmu->flags); @@ -447,9 +447,9 @@ static void amd_vpmu_dump(struct vcpu *v =20 rdmsrl(ctrls[i], ctrl); rdmsrl(counters[i], cntr); - printk(" 0x%08x: 0x%lx (0x%lx in HW) 0x%08x: 0x%lx (0x%lx = in HW)\n", - ctrls[i], ctxt->ctrls[i], ctrl, - counters[i], ctxt->counters[i], cntr); + printk(" %#x: %#lx (%#lx in HW) %#x: %#lx (%#lx in = HW)\n", + ctrls[i], ctxt->ctrls[i], ctrl, + counters[i], ctxt->counters[i], cntr); } } =20 --- a/xen/arch/x86/hvm/vmx/vpmu_core2.c +++ b/xen/arch/x86/hvm/vmx/vpmu_core2.c @@ -678,11 +678,11 @@ static void core2_vpmu_do_cpuid(unsigned } =20 /* Dump vpmu info on console, called in the context of keyhandler 'q'. */ -static void core2_vpmu_dump(struct vcpu *v) +static void core2_vpmu_dump(const struct vcpu *v) { - struct vpmu_struct *vpmu =3D vcpu_vpmu(v); + const struct vpmu_struct *vpmu =3D vcpu_vpmu(v); int i, num; - struct core2_vpmu_context *core2_vpmu_cxt =3D NULL; + const struct core2_vpmu_context *core2_vpmu_cxt =3D NULL; u64 val; =20 if ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) ) @@ -690,7 +690,7 @@ static void core2_vpmu_dump(struct vcpu=20 =20 if ( !vpmu_is_set(vpmu, VPMU_RUNNING) ) { - if ( vpmu_set(vpmu, VPMU_CONTEXT_LOADED) ) + if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) ) printk(" vPMU loaded\n"); else printk(" vPMU allocated\n"); @@ -703,10 +703,11 @@ static void core2_vpmu_dump(struct vcpu=20 /* Print the contents of the counter and its configuration msr. */ for ( i =3D 0; i < num; i++ ) { - struct arch_msr_pair* msr_pair =3D core2_vpmu_cxt->arch_msr_pair; + const struct arch_msr_pair *msr_pair =3D core2_vpmu_cxt->arch_msr_= pair; + if ( core2_vpmu_cxt->pmu_enable->arch_pmc_enable[i] ) printk(" general_%d: 0x%016lx ctrl: 0x%016lx\n", - i, msr_pair[i].counter, msr_pair[i].control);= + i, msr_pair[i].counter, msr_pair[i].control); } /* * The configuration of the fixed counter is 4 bits each in the @@ -716,9 +717,9 @@ static void core2_vpmu_dump(struct vcpu=20 for ( i =3D 0; i < core2_fix_counters.num; i++ ) { if ( core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i] ) - printk(" fixed_%d: 0x%016lx ctrl: 0x%lx\n", - i, core2_vpmu_cxt->fix_counters[i], - val & FIXED_CTR_CTRL_MASK); + printk(" fixed_%d: 0x%016lx ctrl: %#lx\n", + i, core2_vpmu_cxt->fix_counters[i], + val & FIXED_CTR_CTRL_MASK); val >>=3D FIXED_CTR_CTRL_BITS; } } --- a/xen/include/asm-x86/hvm/vpmu.h +++ b/xen/include/asm-x86/hvm/vpmu.h @@ -54,7 +54,7 @@ struct arch_vpmu_ops { void (*arch_vpmu_destroy)(struct vcpu *v); int (*arch_vpmu_save)(struct vcpu *v); void (*arch_vpmu_load)(struct vcpu *v); - void (*arch_vpmu_dump)(struct vcpu *v); + void (*arch_vpmu_dump)(const struct vcpu *); }; =20 int vmx_vpmu_initialise(struct vcpu *, unsigned int flags); --=__Part95A6FBEE.3__= Content-Type: text/plain; name="x86-vPMU-dump-ro.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-vPMU-dump-ro.patch" hvm/vpmu: Prevent dump handlers from incorrectly mutating state=0A=0ADiscov= ered by Coverity, CID 1055181=0A=0Acore2_vpmu_dump() was incorrectly = setting VPMU_CONTEXT_LOADED when it=0Awas intending to check for it.=0A=0AS= igned-off-by: Andrew Cooper =0A=0AThis would = have been avoided if the dump function declared all its=0Apointers "const" = - doing this now (also in SVM).=0A=0AAlso fixing some indentation issues = at once.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/hvm/svm/vpmu.c=0A+++ b/xen/arch/x86/hvm/svm/vpmu.c=0A@@ = -415,10 +415,10 @@ static void amd_vpmu_destroy(struct vcpu=0A }=0A =0A /* = VPMU part of the 'q' keyhandler */=0A-static void amd_vpmu_dump(struct = vcpu *v)=0A+static void amd_vpmu_dump(const struct vcpu *v)=0A {=0A- = struct vpmu_struct *vpmu =3D vcpu_vpmu(v);=0A- struct amd_vpmu_context = *ctxt =3D vpmu->context;=0A+ const struct vpmu_struct *vpmu =3D = vcpu_vpmu(v);=0A+ const struct amd_vpmu_context *ctxt =3D vpmu->context;= =0A unsigned int i;=0A =0A printk(" VPMU state: 0x%x ", = vpmu->flags);=0A@@ -447,9 +447,9 @@ static void amd_vpmu_dump(struct vcpu = *v=0A =0A rdmsrl(ctrls[i], ctrl);=0A rdmsrl(counters[i], = cntr);=0A- printk(" 0x%08x: 0x%lx (0x%lx in HW) 0x%08x: = 0x%lx (0x%lx in HW)\n",=0A- ctrls[i], ctxt->ctrls[i], ctrl,=0A- = counters[i], ctxt->counters[i], cntr);=0A+ printk(" = %#x: %#lx (%#lx in HW) %#x: %#lx (%#lx in HW)\n",=0A+ = ctrls[i], ctxt->ctrls[i], ctrl,=0A+ counters[i], ctxt->counte= rs[i], cntr);=0A }=0A }=0A =0A--- a/xen/arch/x86/hvm/vmx/vpmu_core2.c= =0A+++ b/xen/arch/x86/hvm/vmx/vpmu_core2.c=0A@@ -678,11 +678,11 @@ static = void core2_vpmu_do_cpuid(unsigned=0A }=0A =0A /* Dump vpmu info on = console, called in the context of keyhandler 'q'. */=0A-static void = core2_vpmu_dump(struct vcpu *v)=0A+static void core2_vpmu_dump(const = struct vcpu *v)=0A {=0A- struct vpmu_struct *vpmu =3D vcpu_vpmu(v);=0A+ = const struct vpmu_struct *vpmu =3D vcpu_vpmu(v);=0A int i, num;=0A- = struct core2_vpmu_context *core2_vpmu_cxt =3D NULL;=0A+ const struct = core2_vpmu_context *core2_vpmu_cxt =3D NULL;=0A u64 val;=0A =0A if = ( !vpmu_is_set(vpmu, VPMU_CONTEXT_ALLOCATED) )=0A@@ -690,7 +690,7 @@ = static void core2_vpmu_dump(struct vcpu =0A =0A if ( !vpmu_is_set(vpmu,= VPMU_RUNNING) )=0A {=0A- if ( vpmu_set(vpmu, VPMU_CONTEXT_LOADE= D) )=0A+ if ( vpmu_is_set(vpmu, VPMU_CONTEXT_LOADED) )=0A = printk(" vPMU loaded\n");=0A else=0A printk(" = vPMU allocated\n");=0A@@ -703,10 +703,11 @@ static void core2_vpmu_dump(str= uct vcpu =0A /* Print the contents of the counter and its configuration= msr. */=0A for ( i =3D 0; i < num; i++ )=0A {=0A- struct = arch_msr_pair* msr_pair =3D core2_vpmu_cxt->arch_msr_pair;=0A+ = const struct arch_msr_pair *msr_pair =3D core2_vpmu_cxt->arch_msr_pair;=0A+= =0A if ( core2_vpmu_cxt->pmu_enable->arch_pmc_enable[i] )=0A = printk(" general_%d: 0x%016lx ctrl: 0x%016lx\n",=0A- = i, msr_pair[i].counter, msr_pair[i].control);=0A+ = i, msr_pair[i].counter, msr_pair[i].control);=0A }=0A = /*=0A * The configuration of the fixed counter is 4 bits each in = the=0A@@ -716,9 +717,9 @@ static void core2_vpmu_dump(struct vcpu =0A = for ( i =3D 0; i < core2_fix_counters.num; i++ )=0A {=0A if ( = core2_vpmu_cxt->pmu_enable->fixed_ctr_enable[i] )=0A- printk(" = fixed_%d: 0x%016lx ctrl: 0x%lx\n",=0A- = i, core2_vpmu_cxt->fix_counters[i],=0A- val & = FIXED_CTR_CTRL_MASK);=0A+ printk(" fixed_%d: 0x%016lx = ctrl: %#lx\n",=0A+ i, core2_vpmu_cxt->fix_counters[i],=0A= + val & FIXED_CTR_CTRL_MASK);=0A val >>=3D = FIXED_CTR_CTRL_BITS;=0A }=0A }=0A--- a/xen/include/asm-x86/hvm/vpmu.h= =0A+++ b/xen/include/asm-x86/hvm/vpmu.h=0A@@ -54,7 +54,7 @@ struct = arch_vpmu_ops {=0A void (*arch_vpmu_destroy)(struct vcpu *v);=0A = int (*arch_vpmu_save)(struct vcpu *v);=0A void (*arch_vpmu_load)(struct= vcpu *v);=0A- void (*arch_vpmu_dump)(struct vcpu *v);=0A+ void = (*arch_vpmu_dump)(const struct vcpu *);=0A };=0A =0A int vmx_vpmu_initialis= e(struct vcpu *, unsigned int flags);=0A --=__Part95A6FBEE.3__= 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 --=__Part95A6FBEE.3__=--