linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
@ 2014-03-13 19:36 Venkatesh Srinivas
  2014-03-14  8:44 ` Peter Zijlstra
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Venkatesh Srinivas @ 2014-03-13 19:36 UTC (permalink / raw)
  To: linux-kernel, eranian, peterz, mingo, ak, zheng.z.yan; +Cc: Venkatesh Srinivas

CPUs which should support the RAPL counters according to
Family/Model/Stepping may still issue #GP when attempting to access
the RAPL MSRs. This may happen when Linux is running under KVM and
we are passing-through host F/M/S data, for example. Use rdmsrl_safe
to first access the RAPL_POWER_UNIT MSR; if this fails, do not
attempt to use this PMU.

Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
index 5ad35ad..95700e5 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
@@ -511,6 +511,7 @@ static int rapl_cpu_prepare(int cpu)
 	struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
 	int phys_id = topology_physical_package_id(cpu);
 	u64 ms;
+	u64 msr_rapl_power_unit_bits;
 
 	if (pmu)
 		return 0;
@@ -518,6 +519,9 @@ static int rapl_cpu_prepare(int cpu)
 	if (phys_id < 0)
 		return -1;
 
+	if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
+		return -1;
+
 	pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
 	if (!pmu)
 		return -1;
@@ -531,8 +535,7 @@ static int rapl_cpu_prepare(int cpu)
 	 *
 	 * we cache in local PMU instance
 	 */
-	rdmsrl(MSR_RAPL_POWER_UNIT, pmu->hw_unit);
-	pmu->hw_unit = (pmu->hw_unit >> 8) & 0x1FULL;
+	pmu->hw_unit = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
 	pmu->pmu = &rapl_pmu_class;
 
 	/*
@@ -649,7 +652,9 @@ static int __init rapl_pmu_init(void)
 	get_online_cpus();
 
 	for_each_online_cpu(cpu) {
-		rapl_cpu_prepare(cpu);
+		ret = rapl_cpu_prepare(cpu);
+		if (ret)
+			goto out;
 		rapl_cpu_init(cpu);
 	}
 
@@ -672,6 +677,7 @@ static int __init rapl_pmu_init(void)
 		hweight32(rapl_cntr_mask),
 		ktime_to_ms(pmu->timer_interval));
 
+out:
 	put_online_cpus();
 
 	return 0;
-- 
1.9.0.279.gdc9e3eb


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-13 19:36 [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU Venkatesh Srinivas
@ 2014-03-14  8:44 ` Peter Zijlstra
  2014-03-14 13:56   ` Andi Kleen
  2014-04-18 13:08 ` [tip:perf/core] perf/x86/intel: Use rdmsrl_safe() " tip-bot for Venkatesh Srinivas
  2014-04-23 14:31 ` [PATCH] perf/x86/intel: Use rdmsrl_safe " Stephane Eranian
  2 siblings, 1 reply; 19+ messages in thread
From: Peter Zijlstra @ 2014-03-14  8:44 UTC (permalink / raw)
  To: Venkatesh Srinivas; +Cc: linux-kernel, eranian, mingo, ak, zheng.z.yan

On Thu, Mar 13, 2014 at 12:36:26PM -0700, Venkatesh Srinivas wrote:
> CPUs which should support the RAPL counters according to
> Family/Model/Stepping may still issue #GP when attempting to access
> the RAPL MSRs. This may happen when Linux is running under KVM and
> we are passing-through host F/M/S data, for example. Use rdmsrl_safe
> to first access the RAPL_POWER_UNIT MSR; if this fails, do not
> attempt to use this PMU.

This kvm thing is getting tedious :-(

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-14  8:44 ` Peter Zijlstra
@ 2014-03-14 13:56   ` Andi Kleen
  2014-03-14 15:21     ` Venkatesh Srinivas
  0 siblings, 1 reply; 19+ messages in thread
From: Andi Kleen @ 2014-03-14 13:56 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Venkatesh Srinivas, linux-kernel, eranian, mingo, zheng.z.yan

On Fri, Mar 14, 2014 at 09:44:29AM +0100, Peter Zijlstra wrote:
> On Thu, Mar 13, 2014 at 12:36:26PM -0700, Venkatesh Srinivas wrote:
> > CPUs which should support the RAPL counters according to
> > Family/Model/Stepping may still issue #GP when attempting to access
> > the RAPL MSRs. This may happen when Linux is running under KVM and
> > we are passing-through host F/M/S data, for example. Use rdmsrl_safe
> > to first access the RAPL_POWER_UNIT MSR; if this fails, do not
> > attempt to use this PMU.
> 
> This kvm thing is getting tedious :-(

It sounds bogus to me too, VMs are supposed to return 0 
on reading unknown MSRs, no #GP.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-14 13:56   ` Andi Kleen
@ 2014-03-14 15:21     ` Venkatesh Srinivas
  2014-03-14 16:17       ` Andi Kleen
  0 siblings, 1 reply; 19+ messages in thread
From: Venkatesh Srinivas @ 2014-03-14 15:21 UTC (permalink / raw)
  To: Andi Kleen
  Cc: Peter Zijlstra, linux-kernel, Stephane Eranian, mingo, zheng.z.yan

On Fri, Mar 14, 2014 at 6:56 AM, Andi Kleen <ak@linux.intel.com> wrote:
> On Fri, Mar 14, 2014 at 09:44:29AM +0100, Peter Zijlstra wrote:
>> On Thu, Mar 13, 2014 at 12:36:26PM -0700, Venkatesh Srinivas wrote:
>> > CPUs which should support the RAPL counters according to
>> > Family/Model/Stepping may still issue #GP when attempting to access
>> > the RAPL MSRs. This may happen when Linux is running under KVM and
>> > we are passing-through host F/M/S data, for example. Use rdmsrl_safe
>> > to first access the RAPL_POWER_UNIT MSR; if this fails, do not
>> > attempt to use this PMU.
>>
>> This kvm thing is getting tedious :-(
>
> It sounds bogus to me too, VMs are supposed to return 0
> on reading unknown MSRs, no #GP.

The Intel ISR section for RDMSR seems to say: "Specifying a reserved
or unimplemented
MSR address in ECX will also cause a general protection exception".

>From a guest's perspective, MSR_RAPL_POWER_UNIT is unimplemented; kvm matches
this behavior.

Thanks,
-- vs;

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-14 15:21     ` Venkatesh Srinivas
@ 2014-03-14 16:17       ` Andi Kleen
  2014-03-14 16:57         ` David Ahern
  0 siblings, 1 reply; 19+ messages in thread
From: Andi Kleen @ 2014-03-14 16:17 UTC (permalink / raw)
  To: Venkatesh Srinivas
  Cc: Peter Zijlstra, linux-kernel, Stephane Eranian, mingo, zheng.z.yan

> The Intel ISR section for RDMSR seems to say: "Specifying a reserved
> or unimplemented
> MSR address in ECX will also cause a general protection exception".
> 
> From a guest's perspective, MSR_RAPL_POWER_UNIT is unimplemented; kvm matches
> this behavior.

MSRs are model specific and defined per model number. If you report a model
number you're expected to implement the MSRs defined for that model number.

AFAIK Xen just reports 0 for unknown MSRs (and I'm surprised KVM doesn't too)

I would suggest to fix KVM.

-Andi 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-14 16:17       ` Andi Kleen
@ 2014-03-14 16:57         ` David Ahern
  2014-03-14 23:07           ` Venkatesh Srinivas
  0 siblings, 1 reply; 19+ messages in thread
From: David Ahern @ 2014-03-14 16:57 UTC (permalink / raw)
  To: Andi Kleen, Venkatesh Srinivas
  Cc: Peter Zijlstra, linux-kernel, Stephane Eranian, mingo, zheng.z.yan

On 3/14/14, 10:17 AM, Andi Kleen wrote:
>> The Intel ISR section for RDMSR seems to say: "Specifying a reserved
>> or unimplemented
>> MSR address in ECX will also cause a general protection exception".
>>
>>  From a guest's perspective, MSR_RAPL_POWER_UNIT is unimplemented; kvm matches
>> this behavior.
>
> MSRs are model specific and defined per model number. If you report a model
> number you're expected to implement the MSRs defined for that model number.
>
> AFAIK Xen just reports 0 for unknown MSRs (and I'm surprised KVM doesn't too)
>
> I would suggest to fix KVM.

I believe ignore_msrs parameter to kvm handles that.

David


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-14 16:57         ` David Ahern
@ 2014-03-14 23:07           ` Venkatesh Srinivas
  0 siblings, 0 replies; 19+ messages in thread
From: Venkatesh Srinivas @ 2014-03-14 23:07 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andi Kleen, Peter Zijlstra, David Ahern, Stephane Eranian, mingo,
	zheng.z.yan, virtualization

On Fri, Mar 14, 2014 at 10:57:58AM -0600, David Ahern wrote:
>On 3/14/14, 10:17 AM, Andi Kleen wrote:
>>>The Intel ISR section for RDMSR seems to say: "Specifying a reserved
>>>or unimplemented
>>>MSR address in ECX will also cause a general protection exception".
>>>
>>> From a guest's perspective, MSR_RAPL_POWER_UNIT is unimplemented; kvm matches
>>>this behavior.
>>
>>MSRs are model specific and defined per model number. If you report a model
>>number you're expected to implement the MSRs defined for that model number.
>>
>>AFAIK Xen just reports 0 for unknown MSRs (and I'm surprised KVM doesn't too)
>>
>>I would suggest to fix KVM.
>
>I believe ignore_msrs parameter to kvm handles that.
>
>David

Hi,

cc-ing the virtualization mailing list for more detail on the kvm
default for ignore_msrs (it defaults off).

1) Just returning 0 for unsupported MSRs is not workable -- 0 may be a
    meaningful value for an MSR. RDMSR/WRMSR already have a mechanism
    for out-of-band errors, #GP.

2) #GP has been KVM's default behavior for quite some time. Even if we
    believe changing KVM's default is appropriate, Linux w/ the RAPL PMU
    code enabled will fail to boot on existing KVM versions. W/ this
    change, Linux will boot on prior KVM versions.

Thanks,
-- vs;

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [tip:perf/core] perf/x86/intel: Use rdmsrl_safe() when initializing RAPL PMU
  2014-03-13 19:36 [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU Venkatesh Srinivas
  2014-03-14  8:44 ` Peter Zijlstra
@ 2014-04-18 13:08 ` tip-bot for Venkatesh Srinivas
  2014-04-23 14:31 ` [PATCH] perf/x86/intel: Use rdmsrl_safe " Stephane Eranian
  2 siblings, 0 replies; 19+ messages in thread
From: tip-bot for Venkatesh Srinivas @ 2014-04-18 13:08 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: linux-kernel, hpa, mingo, peterz, tglx, venkateshs

Commit-ID:  24223657806a0ebd0ae5c9caaf7b021091889cf2
Gitweb:     http://git.kernel.org/tip/24223657806a0ebd0ae5c9caaf7b021091889cf2
Author:     Venkatesh Srinivas <venkateshs@google.com>
AuthorDate: Thu, 13 Mar 2014 12:36:26 -0700
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Fri, 18 Apr 2014 12:14:26 +0200

perf/x86/intel: Use rdmsrl_safe() when initializing RAPL PMU

CPUs which should support the RAPL counters according to
Family/Model/Stepping may still issue #GP when attempting to access
the RAPL MSRs. This may happen when Linux is running under KVM and
we are passing-through host F/M/S data, for example. Use rdmsrl_safe
to first access the RAPL_POWER_UNIT MSR; if this fails, do not
attempt to use this PMU.

Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1394739386-22260-1-git-send-email-venkateshs@google.com
Cc: zheng.z.yan@intel.com
Cc: eranian@google.com
Cc: ak@linux.intel.com
Cc: linux-kernel@vger.kernel.org
[ The patch also silently fixes another bug: rapl_pmu_init() didn't handle the memory alloc failure case previously. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
index 4b9a9e9..7c87424 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
@@ -535,6 +535,7 @@ static int rapl_cpu_prepare(int cpu)
 	struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
 	int phys_id = topology_physical_package_id(cpu);
 	u64 ms;
+	u64 msr_rapl_power_unit_bits;
 
 	if (pmu)
 		return 0;
@@ -542,6 +543,9 @@ static int rapl_cpu_prepare(int cpu)
 	if (phys_id < 0)
 		return -1;
 
+	if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
+		return -1;
+
 	pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
 	if (!pmu)
 		return -1;
@@ -555,8 +559,7 @@ static int rapl_cpu_prepare(int cpu)
 	 *
 	 * we cache in local PMU instance
 	 */
-	rdmsrl(MSR_RAPL_POWER_UNIT, pmu->hw_unit);
-	pmu->hw_unit = (pmu->hw_unit >> 8) & 0x1FULL;
+	pmu->hw_unit = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
 	pmu->pmu = &rapl_pmu_class;
 
 	/*
@@ -677,7 +680,9 @@ static int __init rapl_pmu_init(void)
 	cpu_notifier_register_begin();
 
 	for_each_online_cpu(cpu) {
-		rapl_cpu_prepare(cpu);
+		ret = rapl_cpu_prepare(cpu);
+		if (ret)
+			goto out;
 		rapl_cpu_init(cpu);
 	}
 
@@ -700,6 +705,7 @@ static int __init rapl_pmu_init(void)
 		hweight32(rapl_cntr_mask),
 		ktime_to_ms(pmu->timer_interval));
 
+out:
 	cpu_notifier_register_done();
 
 	return 0;

^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-03-13 19:36 [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU Venkatesh Srinivas
  2014-03-14  8:44 ` Peter Zijlstra
  2014-04-18 13:08 ` [tip:perf/core] perf/x86/intel: Use rdmsrl_safe() " tip-bot for Venkatesh Srinivas
@ 2014-04-23 14:31 ` Stephane Eranian
  2014-04-23 14:45   ` Peter Zijlstra
  2 siblings, 1 reply; 19+ messages in thread
From: Stephane Eranian @ 2014-04-23 14:31 UTC (permalink / raw)
  To: Venkatesh Srinivas; +Cc: LKML, Peter Zijlstra, mingo, ak, Yan, Zheng

On Thu, Mar 13, 2014 at 8:36 PM, Venkatesh Srinivas
<venkateshs@google.com> wrote:
> CPUs which should support the RAPL counters according to
> Family/Model/Stepping may still issue #GP when attempting to access
> the RAPL MSRs. This may happen when Linux is running under KVM and
> we are passing-through host F/M/S data, for example. Use rdmsrl_safe
> to first access the RAPL_POWER_UNIT MSR; if this fails, do not
> attempt to use this PMU.
>
> Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
> ---
>  arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> index 5ad35ad..95700e5 100644
> --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> @@ -511,6 +511,7 @@ static int rapl_cpu_prepare(int cpu)
>         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
>         int phys_id = topology_physical_package_id(cpu);
>         u64 ms;
> +       u64 msr_rapl_power_unit_bits;
>
>         if (pmu)
>                 return 0;
> @@ -518,6 +519,9 @@ static int rapl_cpu_prepare(int cpu)
>         if (phys_id < 0)
>                 return -1;
>
> +       if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
> +               return -1;
> +
I have a problem with this patch on native hardware. This
rdmsrl_safe() systematically
fails when I know the MSR is perfectly valid on the CPU. Consequently, RAPL PMU
is disabled when I tried on IvyBridge and Haswell CPUs.

I don't know the internals of rdmsrl_safe(). Maybe it is invoked too
early in the boot process.

Please fix this.

>         pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
>         if (!pmu)
>                 return -1;
> @@ -531,8 +535,7 @@ static int rapl_cpu_prepare(int cpu)
>          *
>          * we cache in local PMU instance
>          */
> -       rdmsrl(MSR_RAPL_POWER_UNIT, pmu->hw_unit);
> -       pmu->hw_unit = (pmu->hw_unit >> 8) & 0x1FULL;
> +       pmu->hw_unit = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
>         pmu->pmu = &rapl_pmu_class;
>
>         /*
> @@ -649,7 +652,9 @@ static int __init rapl_pmu_init(void)
>         get_online_cpus();
>
>         for_each_online_cpu(cpu) {
> -               rapl_cpu_prepare(cpu);
> +               ret = rapl_cpu_prepare(cpu);
> +               if (ret)
> +                       goto out;
>                 rapl_cpu_init(cpu);
>         }
>
> @@ -672,6 +677,7 @@ static int __init rapl_pmu_init(void)
>                 hweight32(rapl_cntr_mask),
>                 ktime_to_ms(pmu->timer_interval));
>
> +out:
>         put_online_cpus();
>
>         return 0;
> --
> 1.9.0.279.gdc9e3eb
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 14:31 ` [PATCH] perf/x86/intel: Use rdmsrl_safe " Stephane Eranian
@ 2014-04-23 14:45   ` Peter Zijlstra
  2014-04-23 14:49     ` Stephane Eranian
  0 siblings, 1 reply; 19+ messages in thread
From: Peter Zijlstra @ 2014-04-23 14:45 UTC (permalink / raw)
  To: Stephane Eranian; +Cc: Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 04:31:32PM +0200, Stephane Eranian wrote:
> On Thu, Mar 13, 2014 at 8:36 PM, Venkatesh Srinivas
> <venkateshs@google.com> wrote:
> > CPUs which should support the RAPL counters according to
> > Family/Model/Stepping may still issue #GP when attempting to access
> > the RAPL MSRs. This may happen when Linux is running under KVM and
> > we are passing-through host F/M/S data, for example. Use rdmsrl_safe
> > to first access the RAPL_POWER_UNIT MSR; if this fails, do not
> > attempt to use this PMU.
> >
> > Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
> > ---
> >  arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
> >  1 file changed, 9 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> > index 5ad35ad..95700e5 100644
> > --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> > +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
> > @@ -511,6 +511,7 @@ static int rapl_cpu_prepare(int cpu)
> >         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
> >         int phys_id = topology_physical_package_id(cpu);
> >         u64 ms;
> > +       u64 msr_rapl_power_unit_bits;
> >
> >         if (pmu)
> >                 return 0;
> > @@ -518,6 +519,9 @@ static int rapl_cpu_prepare(int cpu)
> >         if (phys_id < 0)
> >                 return -1;
> >
> > +       if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
> > +               return -1;
> > +
> I have a problem with this patch on native hardware. This
> rdmsrl_safe() systematically
> fails when I know the MSR is perfectly valid on the CPU. Consequently, RAPL PMU
> is disabled when I tried on IvyBridge and Haswell CPUs.
> 
> I don't know the internals of rdmsrl_safe(). Maybe it is invoked too
> early in the boot process.

Weird; so the way it works is that it adds an exception table entry for
the wrmsr instruction, so when the wrmsr generates a fault due to being
an invalid msr the fault handler looks at the exception table, and finds
the entry, which instructs it to continue execution at the error path
and return -EFAULT.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 14:45   ` Peter Zijlstra
@ 2014-04-23 14:49     ` Stephane Eranian
  2014-04-23 15:09       ` Peter Zijlstra
  0 siblings, 1 reply; 19+ messages in thread
From: Stephane Eranian @ 2014-04-23 14:49 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 4:45 PM, Peter Zijlstra <peterz@infradead.org> wrote:
> On Wed, Apr 23, 2014 at 04:31:32PM +0200, Stephane Eranian wrote:
>> On Thu, Mar 13, 2014 at 8:36 PM, Venkatesh Srinivas
>> <venkateshs@google.com> wrote:
>> > CPUs which should support the RAPL counters according to
>> > Family/Model/Stepping may still issue #GP when attempting to access
>> > the RAPL MSRs. This may happen when Linux is running under KVM and
>> > we are passing-through host F/M/S data, for example. Use rdmsrl_safe
>> > to first access the RAPL_POWER_UNIT MSR; if this fails, do not
>> > attempt to use this PMU.
>> >
>> > Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
>> > ---
>> >  arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
>> >  1 file changed, 9 insertions(+), 3 deletions(-)
>> >
>> > diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
>> > index 5ad35ad..95700e5 100644
>> > --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
>> > +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
>> > @@ -511,6 +511,7 @@ static int rapl_cpu_prepare(int cpu)
>> >         struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
>> >         int phys_id = topology_physical_package_id(cpu);
>> >         u64 ms;
>> > +       u64 msr_rapl_power_unit_bits;
>> >
>> >         if (pmu)
>> >                 return 0;
>> > @@ -518,6 +519,9 @@ static int rapl_cpu_prepare(int cpu)
>> >         if (phys_id < 0)
>> >                 return -1;
>> >
>> > +       if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
>> > +               return -1;
>> > +
>> I have a problem with this patch on native hardware. This
>> rdmsrl_safe() systematically
>> fails when I know the MSR is perfectly valid on the CPU. Consequently, RAPL PMU
>> is disabled when I tried on IvyBridge and Haswell CPUs.
>>
>> I don't know the internals of rdmsrl_safe(). Maybe it is invoked too
>> early in the boot process.
>
> Weird; so the way it works is that it adds an exception table entry for
> the wrmsr instruction, so when the wrmsr generates a fault due to being
> an invalid msr the fault handler looks at the exception table, and finds
> the entry, which instructs it to continue execution at the error path
> and return -EFAULT.
>
On your machine, booted with 3.15-rc2, do you have /sys/devices/power?
If not, and you have at least an SNB, you should have RAPL and that
RAPL_UNIT MSR.

Proof is that if you read that MSR using /dev/cpu/msr it works just fine:
# modprobe msr
# rdmsr 0x606
a1003

So something is broken somewhere.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 14:49     ` Stephane Eranian
@ 2014-04-23 15:09       ` Peter Zijlstra
  2014-04-23 15:14         ` Stephane Eranian
  0 siblings, 1 reply; 19+ messages in thread
From: Peter Zijlstra @ 2014-04-23 15:09 UTC (permalink / raw)
  To: Stephane Eranian; +Cc: Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 04:49:55PM +0200, Stephane Eranian wrote:
> On your machine, booted with 3.15-rc2, do you have /sys/devices/power?
> If not, and you have at least an SNB, you should have RAPL and that
> RAPL_UNIT MSR.
> 
> Proof is that if you read that MSR using /dev/cpu/msr it works just fine:
> # modprobe msr
> # rdmsr 0x606
> a1003
> 
> So something is broken somewhere.

I've not got SNB+ class hardware (for testing). But I believe you that
there's something funny, I just do not understanding how *msr_safe()
could cause this.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:09       ` Peter Zijlstra
@ 2014-04-23 15:14         ` Stephane Eranian
  2014-04-23 15:16           ` Borislav Petkov
  0 siblings, 1 reply; 19+ messages in thread
From: Stephane Eranian @ 2014-04-23 15:14 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 5:09 PM, Peter Zijlstra <peterz@infradead.org> wrote:
> On Wed, Apr 23, 2014 at 04:49:55PM +0200, Stephane Eranian wrote:
>> On your machine, booted with 3.15-rc2, do you have /sys/devices/power?
>> If not, and you have at least an SNB, you should have RAPL and that
>> RAPL_UNIT MSR.
>>
>> Proof is that if you read that MSR using /dev/cpu/msr it works just fine:
>> # modprobe msr
>> # rdmsr 0x606
>> a1003
>>
>> So something is broken somewhere.
>
> I've not got SNB+ class hardware (for testing). But I believe you that
> there's something funny, I just do not understanding how *msr_safe()
> could cause this.

Or the logic of the test in rapl_cpu_prepare() is wrong, maybe?
Wouldn't rdmsrl_safe() return 0 on success?

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:14         ` Stephane Eranian
@ 2014-04-23 15:16           ` Borislav Petkov
  2014-04-23 15:18             ` Stephane Eranian
  0 siblings, 1 reply; 19+ messages in thread
From: Borislav Petkov @ 2014-04-23 15:16 UTC (permalink / raw)
  To: Stephane Eranian
  Cc: Peter Zijlstra, Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 05:14:33PM +0200, Stephane Eranian wrote:
> Wouldn't rdmsrl_safe() return 0 on success?

Yes.

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:16           ` Borislav Petkov
@ 2014-04-23 15:18             ` Stephane Eranian
  2014-04-23 15:35               ` Borislav Petkov
  0 siblings, 1 reply; 19+ messages in thread
From: Stephane Eranian @ 2014-04-23 15:18 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Peter Zijlstra, Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 5:16 PM, Borislav Petkov <bp@alien8.de> wrote:
> On Wed, Apr 23, 2014 at 05:14:33PM +0200, Stephane Eranian wrote:
>> Wouldn't rdmsrl_safe() return 0 on success?
>
> Yes.
>
then the if() test is wrong:
if (!rdmsrl_safe())
   return -1;

Should be:
if (rdmsrl_safe())
  return -1;

Or am I missing something?


> --
> Regards/Gruss,
>     Boris.
>
> Sent from a fat crate under my desk. Formatting is fine.
> --

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:18             ` Stephane Eranian
@ 2014-04-23 15:35               ` Borislav Petkov
  2014-04-23 15:44                 ` Stephane Eranian
  0 siblings, 1 reply; 19+ messages in thread
From: Borislav Petkov @ 2014-04-23 15:35 UTC (permalink / raw)
  To: Stephane Eranian
  Cc: Peter Zijlstra, Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 05:18:29PM +0200, Stephane Eranian wrote:
> On Wed, Apr 23, 2014 at 5:16 PM, Borislav Petkov <bp@alien8.de> wrote:
> > On Wed, Apr 23, 2014 at 05:14:33PM +0200, Stephane Eranian wrote:
> >> Wouldn't rdmsrl_safe() return 0 on success?
> >
> > Yes.
> >
> then the if() test is wrong:
> if (!rdmsrl_safe())
>    return -1;
> 
> Should be:
> if (rdmsrl_safe())
>   return -1;
> 
> Or am I missing something?

Yeah, that is wrong:

        if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
                return -1;

On error we return -EIO, on success 0. Just remove the "!".


-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:35               ` Borislav Petkov
@ 2014-04-23 15:44                 ` Stephane Eranian
  2014-04-23 15:55                   ` Borislav Petkov
  0 siblings, 1 reply; 19+ messages in thread
From: Stephane Eranian @ 2014-04-23 15:44 UTC (permalink / raw)
  To: Borislav Petkov
  Cc: Peter Zijlstra, Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 5:35 PM, Borislav Petkov <bp@alien8.de> wrote:
> On Wed, Apr 23, 2014 at 05:18:29PM +0200, Stephane Eranian wrote:
>> On Wed, Apr 23, 2014 at 5:16 PM, Borislav Petkov <bp@alien8.de> wrote:
>> > On Wed, Apr 23, 2014 at 05:14:33PM +0200, Stephane Eranian wrote:
>> >> Wouldn't rdmsrl_safe() return 0 on success?
>> >
>> > Yes.
>> >
>> then the if() test is wrong:
>> if (!rdmsrl_safe())
>>    return -1;
>>
>> Should be:
>> if (rdmsrl_safe())
>>   return -1;
>>
>> Or am I missing something?
>
> Yeah, that is wrong:
>
>         if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
>                 return -1;
>
> On error we return -EIO, on success 0. Just remove the "!".
>
Yeah, will post a patch ASAP. A comment describing return values
where rdmsrl_safe() is defined would maybe help avoid problems in the future.
Thanks.

>
> --
> Regards/Gruss,
>     Boris.
>
> Sent from a fat crate under my desk. Formatting is fine.
> --

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
  2014-04-23 15:44                 ` Stephane Eranian
@ 2014-04-23 15:55                   ` Borislav Petkov
  0 siblings, 0 replies; 19+ messages in thread
From: Borislav Petkov @ 2014-04-23 15:55 UTC (permalink / raw)
  To: Stephane Eranian
  Cc: Peter Zijlstra, Venkatesh Srinivas, LKML, mingo, ak, Yan, Zheng

On Wed, Apr 23, 2014 at 05:44:30PM +0200, Stephane Eranian wrote:
> A comment describing return values where rdmsrl_safe() is defined
> would maybe help avoid problems in the future.

If it helps, sure. But please as a separate patch and to x86 guys.

-- 
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.
@ 2014-03-07  4:42 Venkatesh Srinivas
  0 siblings, 0 replies; 19+ messages in thread
From: Venkatesh Srinivas @ 2014-03-07  4:42 UTC (permalink / raw)
  To: eranian, linux-kernel; +Cc: Venkatesh Srinivas

CPUs which should support the RAPL counters according to
Family/Model/Stepping may still issue #GP when attempting to access
the RAPL MSRs. This may happen when Linux is running under KVM and
we are passing-through host F/M/S data, for example. Use rdmsrl_safe
to first access the RAPL_POWER_UNIT MSR; if this fails, do not
attempt to use this PMU.

Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
index 5ad35ad..95700e5 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
@@ -511,6 +511,7 @@ static int rapl_cpu_prepare(int cpu)
 	struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu);
 	int phys_id = topology_physical_package_id(cpu);
 	u64 ms;
+	u64 msr_rapl_power_unit_bits;
 
 	if (pmu)
 		return 0;
@@ -518,6 +519,9 @@ static int rapl_cpu_prepare(int cpu)
 	if (phys_id < 0)
 		return -1;
 
+	if (!rdmsrl_safe(MSR_RAPL_POWER_UNIT, &msr_rapl_power_unit_bits))
+		return -1;
+
 	pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
 	if (!pmu)
 		return -1;
@@ -531,8 +535,7 @@ static int rapl_cpu_prepare(int cpu)
 	 *
 	 * we cache in local PMU instance
 	 */
-	rdmsrl(MSR_RAPL_POWER_UNIT, pmu->hw_unit);
-	pmu->hw_unit = (pmu->hw_unit >> 8) & 0x1FULL;
+	pmu->hw_unit = (msr_rapl_power_unit_bits >> 8) & 0x1FULL;
 	pmu->pmu = &rapl_pmu_class;
 
 	/*
@@ -649,7 +652,9 @@ static int __init rapl_pmu_init(void)
 	get_online_cpus();
 
 	for_each_online_cpu(cpu) {
-		rapl_cpu_prepare(cpu);
+		ret = rapl_cpu_prepare(cpu);
+		if (ret)
+			goto out;
 		rapl_cpu_init(cpu);
 	}
 
@@ -672,6 +677,7 @@ static int __init rapl_pmu_init(void)
 		hweight32(rapl_cntr_mask),
 		ktime_to_ms(pmu->timer_interval));
 
+out:
 	put_online_cpus();
 
 	return 0;
-- 
1.9.0.279.gdc9e3eb


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2014-04-23 15:55 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-13 19:36 [PATCH] perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU Venkatesh Srinivas
2014-03-14  8:44 ` Peter Zijlstra
2014-03-14 13:56   ` Andi Kleen
2014-03-14 15:21     ` Venkatesh Srinivas
2014-03-14 16:17       ` Andi Kleen
2014-03-14 16:57         ` David Ahern
2014-03-14 23:07           ` Venkatesh Srinivas
2014-04-18 13:08 ` [tip:perf/core] perf/x86/intel: Use rdmsrl_safe() " tip-bot for Venkatesh Srinivas
2014-04-23 14:31 ` [PATCH] perf/x86/intel: Use rdmsrl_safe " Stephane Eranian
2014-04-23 14:45   ` Peter Zijlstra
2014-04-23 14:49     ` Stephane Eranian
2014-04-23 15:09       ` Peter Zijlstra
2014-04-23 15:14         ` Stephane Eranian
2014-04-23 15:16           ` Borislav Petkov
2014-04-23 15:18             ` Stephane Eranian
2014-04-23 15:35               ` Borislav Petkov
2014-04-23 15:44                 ` Stephane Eranian
2014-04-23 15:55                   ` Borislav Petkov
  -- strict thread matches above, loose matches on Subject: below --
2014-03-07  4:42 Venkatesh Srinivas

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).