From: Dietmar Hahn Subject: Re: [PATCH] VPMU issue on Nehalem cpus Date: Mon, 22 Nov 2010 07:36:25 +0100 Message-ID: <201011220736.26457.dietmar.hahn@ts.fujitsu.com> References: <201011191129.07773.dietmar.hahn@ts.fujitsu.com> <4CE668580200007800023533@vpn.id2.novell.com> <04F972F38B3C4E4E91C4697DA8BF9F524E1739BEEB@shsmsx502.ccr.corp.intel.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0261275609==" Return-path: In-Reply-To: <04F972F38B3C4E4E91C4697DA8BF9F524E1739BEEB@shsmsx502.ccr.corp.intel.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "Shan, Haitao" Cc: "xen-devel@lists.xensource.com" , Jan Beulich List-Id: xen-devel@lists.xenproject.org --===============0261275609== Content-Type: multipart/alternative; boundary="Boundary-01=_q9g6MN12HQ/LdPI" Content-Transfer-Encoding: 7bit --Boundary-01=_q9g6MN12HQ/LdPI Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable Hi Haitao, Am 20.11.2010 schrieb ""Shan, Haitao" ": > Hi, Jan, >=20 > The actual handler core2_vpmu_do_interrupt is never called under a NMI ha= ndler context. The reason is that I don't want to place such an interrupt o= n behalf of guest to be of so high a priority. So, even guest programs the = virtual APIC to use NMI, the underlying HW does not. It uses a vector (0xF8= ? I don't remember clearly). >=20 > This issue this patch solves is likely a HW bug but I have not find any d= ocumented errata. On some NHM processors, when a PMI is received, you will = observe the counter that triggers this interrupt is zero (which means it ha= s just gone overflowed). If you unmask the PMI sources (as you might know, = PMI gets automatically masked when received), you will immediately get anot= her interrupt. This is how you get an interrupt loop here (not NMI loop, bu= t PMI loop). > The reason why native oprofile works is that oprofile actually reprogram = the counter before unmask the interrupt. In our VPMU implementation in Xen,= the PMI handler does nothing but pending an interrupt to guest, then it un= masks the PMI and return. > I find that reprograming the counter to be 1 works around this issue (any= number that is *not* zero should work actually). > Another working around would be only unmasking the real PMI when guests u= nmask its virtual PMIs. This work around is not very promising, since by th= e time guests unmask the virtual PMIs, the vcpu might be migrated to other = CPUs. You need complex tracking and an IPI to do the right unmask on the co= rrect physical CPUs. >=20 > Hope I can explain the whole matter clearly here. >=20 >=20 > BTW: As I observed on other processors, when a PMI is received, the count= er is never zero (some values just slightly greater than zero). >=20 > Shan Haitao very good explanation, many thanks! Dietmar. >=20 > -----Original Message----- > From: Jan Beulich [mailto:JBeulich@novell.com]=20 > Sent: Friday, November 19, 2010 7:07 PM > To: Dietmar Hahn > Cc: Shan, Haitao; xen-devel@lists.xensource.com > Subject: Re: [Xen-devel] [PATCH] VPMU issue on Nehalem cpus >=20 > >>> On 19.11.10 at 11:29, Dietmar Hahn wrot= e: > > +/* > > + * QUIRK to workaround an issue on Nehalem processors currently seen > > + * on family 6 cpus E5520 (model 26) and X7542 (model 46). > > + * The issue leads to endless NMI loops on the processor. > > + * If a counter triggers an NMI and while the NMI handler is running a= nother > > + * counter overflows the second counter triggers endless new NMIs. > > + * A solution is to read all flagged counters and if the value is 0 wr= ite > > + * 1 into it. > > + */ >=20 > Two things I don't understand here: One is that I can't see how > from the NMI handler control would get to > core2_vpmu_do_interrupt() - afaics, this gets called only in the > context of the (vectored) smp_pmu_apic_interrupt(). The other > is that if nested interrupts occur, how would you prevent this > by writing ones into zero counters? That is, in the best case I > could see this shrinking the window within which unintended > nested interrupts would occur. Or is it that the secondary > interrupts only occur after the first one returned? Is this (mis-) > behavior documented somewhere? >=20 > > +static int is_nmi_quirk; >=20 > bool_t __read_mostly? >=20 > > + > > +static void check_nmi_quirk(void) > > +{ > > + u8 family =3D current_cpu_data.x86; > > + u8 cpu_model =3D current_cpu_data.x86_model; > > + is_nmi_quirk =3D 0; > > + if ( family =3D=3D 6 ) > > + { > > + if ( cpu_model =3D=3D 46 || cpu_model =3D=3D 26 ) > > + is_nmi_quirk =3D 1; > > + } > > +} > > + > > +static int core2_get_pmc_count(void); > > +static void handle_nmi_quirk(u64 msr_content) > > +{ > > + int num_gen_pmc =3D core2_get_pmc_count(); > > + int num_fix_pmc =3D 3; > > + int i; > > + u64 val; > > + > > + if ( !is_nmi_quirk ) > > + return; > > + > > + val =3D msr_content & ((1 << num_gen_pmc) - 1); >=20 > What's the point of masking if the subsequent loop looks at the > bottom so many bits only anyway? >=20 > > + for ( i =3D 0; i < num_gen_pmc; i++ ) > > + { > > + if ( val & 0x1 ) > > + { > > + u64 cnt; > > + rdmsrl(MSR_P6_PERFCTR0 + i, cnt); > > + if ( cnt =3D=3D 0 ) > > + wrmsrl(MSR_P6_PERFCTR0 + i, 1); > > + } > > + val >>=3D 1; > > + } > > + val =3D (msr_content >> 32) & ((1 << num_fix_pmc) - 1); >=20 > Same here. >=20 > > + for ( i =3D 0; i < num_fix_pmc; i++ ) > > + { > > + if ( val & 0x1 ) > > + { > > + u64 cnt; > > + rdmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, cnt); > > + if ( cnt =3D=3D 0 ) > > + wrmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, 1); > > + } > > + val >>=3D 1; > > + } > > +} > > + > > +#define CHECK_HANDLE_NMI_QUIRK(msr_content) \ > > + if ( is_nmi_quirk ) \ > > + handle_nmi_quirk(msr_content); > > + >=20 > Why do you need a macro here if you use it only once? >=20 > > u32 core2_counters_msr[] =3D { > > MSR_CORE_PERF_FIXED_CTR0, > > MSR_CORE_PERF_FIXED_CTR1, > > @@ -494,6 +558,9 @@ > > rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, msr_content); > > if ( !msr_content ) > > return 0; > > + > > + CHECK_HANDLE_NMI_QUIRK(msr_content) > > + > > core2_vpmu_cxt->global_ovf_status |=3D msr_content; > > msr_content =3D 0xC000000700000000 | ((1 << core2_get_pmc_count())= - 1); > > wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, msr_content); >=20 > Jan >=20 >=20 >=20 =2D-=20 Dietmar Hahn TSP ES&S SWE OS =46UJITSU =46ujitsu Technology Solutions Domagkstra=DFe 28, D-80807 M=FCnchen, Germany Tel: +49 (89) 3222 2952 Email: dietmar.hahn@ts.fujitsu.com Web: http://ts.fujitsu.com Company details: http://ts.fujitsu.com/imprint.html --Boundary-01=_q9g6MN12HQ/LdPI Content-Type: text/html; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable

Hi Haitao,<= /p>

Am 20.11.20= 10 schrieb ""Shan, Haitao" <haitao.shan@intel.com>&quo= t;:

> Hi, Ja= n,

>

> The ac= tual handler core2_vpmu_do_interrupt is never called under a NMI handler co= ntext. The reason is that I don't want to place such an interrupt on behalf= of guest to be of so high a priority. So, even guest programs the virtual = APIC to use NMI, the underlying HW does not. It uses a vector (0xF8? I don'= t remember clearly).

>

> This i= ssue this patch solves is likely a HW bug but I have not find any documente= d errata. On some NHM processors, when a PMI is received, you will observe = the counter that triggers this interrupt is zero (which means it has just g= one overflowed). If you unmask the PMI sources (as you might know, PMI gets= automatically masked when received), you will immediately get another inte= rrupt. This is how you get an interrupt loop here (not NMI loop, but PMI lo= op).

> The re= ason why native oprofile works is that oprofile actually reprogram the coun= ter before unmask the interrupt. In our VPMU implementation in Xen, the PMI= handler does nothing but pending an interrupt to guest, then it unmasks th= e PMI and return.

> I find= that reprograming the counter to be 1 works around this issue (any number = that is *not* zero should work actually).

> Anothe= r working around would be only unmasking the real PMI when guests unmask it= s virtual PMIs. This work around is not very promising, since by the time g= uests unmask the virtual PMIs, the vcpu might be migrated to other CPUs. Yo= u need complex tracking and an IPI to do the right unmask on the correct ph= ysical CPUs.

>

> Hope I= can explain the whole matter clearly here.

>

>

> BTW: A= s I observed on other processors, when a PMI is received, the counter is ne= ver zero (some values just slightly greater than zero).

>

> Shan H= aitao

very good e= xplanation, many thanks!

Dietmar.

>

> -----O= riginal Message-----

> From: = Jan Beulich [mailto:JBeulich@novell.com]

> Sent: = =46riday, November 19, 2010 7:07 PM

> To: Di= etmar Hahn

> Cc: Sh= an, Haitao; xen-devel@lists.xensource.com

> Subjec= t: Re: [Xen-devel] [PATCH] VPMU issue on Nehalem cpus

>

> >&g= t;> On 19.11.10 at 11:29, Dietmar Hahn <dietmar.hahn@ts.fujitsu.com&g= t; wrote:

> > += /*

> > += * QUIRK to workaround an issue on Nehalem processors currently seen

> > += * on family 6 cpus E5520 (model 26) and X7542 (model 46).

> > += * The issue leads to endless NMI loops on the processor.

> > += * If a counter triggers an NMI and while the NMI handler is running anothe= r

> > += * counter overflows the second counter triggers endless new NMIs.

> > += * A solution is to read all flagged counters and if the value is 0 write

> > += * 1 into it.

> > += */

>

> Two th= ings I don't understand here: One is that I can't see how

> from t= he NMI handler control would get to

> core2_= vpmu_do_interrupt() - afaics, this gets called only in the

> contex= t of the (vectored) smp_pmu_apic_interrupt(). The other

> is tha= t if nested interrupts occur, how would you prevent this

> by wri= ting ones into zero counters? That is, in the best case I

> could = see this shrinking the window within which unintended

> nested= interrupts would occur. Or is it that the secondary

> interr= upts only occur after the first one returned? Is this (mis-)

> behavi= or documented somewhere?

>

> > += static int is_nmi_quirk;

>

> bool_t= __read_mostly?

>

> > +=

> > += static void check_nmi_quirk(void)

> > += {

> > += u8 family =3D current_cpu_data.x86;

> > += u8 cpu_model =3D current_cpu_data.x86_model;

> > += is_nmi_quirk =3D 0;

> > += if ( family =3D=3D 6 )

> > += {

> > += if ( cpu_model =3D=3D 46 || cpu_model =3D=3D 26 )

> > += is_nmi_quirk =3D 1;

> > += }

> > += }

> > +=

> > += static int core2_get_pmc_count(void);

> > += static void handle_nmi_quirk(u64 msr_content)

> > += {

> > += int num_gen_pmc =3D core2_get_pmc_count();

> > += int num_fix_pmc =3D 3;

> > += int i;

> > += u64 val;

> > +=

> > += if ( !is_nmi_quirk )

> > += return;

> > +=

> > += val =3D msr_content & ((1 << num_gen_pmc) - 1);

>

> What's= the point of masking if the subsequent loop looks at the

> bottom= so many bits only anyway?

>

> > += for ( i =3D 0; i < num_gen_pmc; i++ )

> > += {

> > += if ( val & 0x1 )

> > += {

> > += u64 cnt;

> > += rdmsrl(MSR_P6_PERFCTR0 + i, cnt);

> > += if ( cnt =3D=3D 0 )

> > += wrmsrl(MSR_P6_PERFCTR0 + i, 1);

> > += }

> > += val >>=3D 1;

> > += }

> > += val =3D (msr_content >> 32) & ((1 << num_fix_pmc) - 1);=

>

> Same h= ere.

>

> > += for ( i =3D 0; i < num_fix_pmc; i++ )

> > += {

> > += if ( val & 0x1 )

> > += {

> > += u64 cnt;

> > += rdmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, cnt);

> > += if ( cnt =3D=3D 0 )

> > += wrmsrl(MSR_CORE_PERF_FIXED_CTR0 + i, 1);

> > += }

> > += val >>=3D 1;

> > += }

> > += }

> > +=

> > += #define CHECK_HANDLE_NMI_QUIRK(msr_content) \

> > += if ( is_nmi_quirk ) \

> > += handle_nmi_quirk(msr_content);

> > +=

>

> Why do= you need a macro here if you use it only once?

>

> > = u32 core2_counters_msr[] =3D {

> > = MSR_CORE_PERF_FIXED_CTR0,

> > = MSR_CORE_PERF_FIXED_CTR1,

> > @= @ -494,6 +558,9 @@

> > = rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, msr_content);

> > = if ( !msr_content )

> > = return 0;

> > +=

> > += CHECK_HANDLE_NMI_QUIRK(msr_content)

> > +=

> > = core2_vpmu_cxt->global_ovf_status |=3D msr_content;

> > = msr_content =3D 0xC000000700000000 | ((1 << core2_get_pmc_count()= ) - 1);

> > = wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, msr_content);

>

> Jan

>

>

>

--

Dietmar Hah= n

TSP ES&= S SWE OS

FUJITSU

Fujitsu Tec= hnology Solutions

Domagkstra= =DFe 28, D-80807 M=FCnchen, Germany

Tel: +49 (8= 9) 3222 2952

Email: diet= mar.hahn@ts.fujitsu.com

Web: http:= //ts.fujitsu.com

Company det= ails: http://ts.fujitsu.com/imprint.html

--Boundary-01=_q9g6MN12HQ/LdPI-- --===============0261275609== 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.xensource.com http://lists.xensource.com/xen-devel --===============0261275609==--