Skip to contentSkip to conversation

perf/x86/intel: Use rdmsrl_safe when initializing RAPL PMU.

PATCH In review Venkatesh Srinivas 7 participants 18 messages Mar 13, 2014 – Apr 23, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +9 -3
index 5ad35ad..95700e5 100644
@@ -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;
Signature · 1 line
1.9.0.279.gdc9e3eb
maintainer
#1 lore raw
Quoted 6 lines · 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.

This kvm thing is getting tedious :-(

#2 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · 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.

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

Signature · 1 line
[email protected] -- Speaking for myself only
#3 lore raw
Quoted 9 lines · Andi Kleen
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · 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.

Quoted 4 lines
Quoted 1 line

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

Quoted 1 line

From a guest's perspective, MSR_RAPL_POWER_UNIT is unimplemented; kvm matches

this behavior.

Thanks,
-- vs;

#4 lore raw
Quoted 6 lines

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

#5 lore raw
Quoted 7 lines · Andi Kleen
Quoted 6 lines

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.

Quoted 6 lines

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

#6 lore raw
Quoted 11 lines · David Ahern
Quoted 10 lines · Andi Kleen
Quoted 6 lines

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.

Quoted 7 lines
Quoted 3 lines

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;

#7 lore raw

[tip:perf/core] perf/x86/intel: Use rdmsrl_safe() when initializing RAPL PMU

In reply to Venkatesh Srinivas
perf/x86/intel: Use rdmsrl_safe() when initializing RAPL PMU
Commit-ID:  24223657806a0ebd0ae5c9caaf7b021091889cf2
Gitweb:     http://git.kernel.org/tip/24223657806a0ebd0ae5c9caaf7b021091889cf2
Author:     Venkatesh Srinivas <[email protected]>
AuthorDate: Thu, 13 Mar 2014 12:36:26 -0700
Committer:  Ingo Molnar <[email protected]>
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.

[ The patch also silently fixes another bug: rapl_pmu_init() didn't handle the memory alloc failure case previously. ]
---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +9 -3
index 4b9a9e9..7c87424 100644
@@ -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;
#8 lore raw
In reply to Venkatesh Srinivas
Quoted 25 lines · 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.

---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +1 -0
index 5ad35ad..95700e5 100644
@@ -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;

arch/x86/kernel/cpu/perf_event_intel_rapl.c static int rapl_cpu_prepare(int cpu) · line 524

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c +3 -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.

Quoted 35 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c +5 -4
        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

maintainer
#9 lore raw
Quoted 38 lines · Stephane Eranian
Quoted 25 lines · 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.

---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +1 -0
index 5ad35ad..95700e5 100644
@@ -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;

arch/x86/kernel/cpu/perf_event_intel_rapl.c static int rapl_cpu_prepare(int cpu) · line 524

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c +3 -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.

Quoted 2 lines

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.

#10 lore raw
Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Stephane Eranian
Quoted 25 lines · 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.

---
 arch/x86/kernel/cpu/perf_event_intel_rapl.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +1 -0
index 5ad35ad..95700e5 100644
@@ -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;

arch/x86/kernel/cpu/perf_event_intel_rapl.c static int rapl_cpu_prepare(int cpu) · line 524

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c +3 -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.

Quoted 9 lines
Quoted 2 lines

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.

maintainer
#11 lore raw
Quoted 10 lines · Stephane Eranian

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.

#12 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Stephane Eranian

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
Quoted 5 lines
Quoted 1 line

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?

maintainer
#13 lore raw
Quoted 1 line · Stephane Eranian

Wouldn't rdmsrl_safe() return 0 on success?

Yes.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#14 lore raw
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Stephane Eranian

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?

Quoted 6 lines

--
Regards/Gruss,
Boris.

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

maintainer
#15 lore raw
Quoted 10 lines · Stephane Eranian
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Stephane Eranian

Wouldn't rdmsrl_safe() return 0 on success?

Yes.

then the if() test is wrong:
if (!rdmsrl_safe())
return -1;

Quoted 5 lines

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 "!".

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#16 lore raw
Quoted 15 lines · Borislav Petkov
Quoted 14 lines · Stephane Eranian
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Stephane Eranian

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;

Quoted 9 lines
Quoted 1 line

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.

Quoted 7 lines

--
Regards/Gruss,
Boris.

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

maintainer
#17 lore raw
Quoted 2 lines · Stephane Eranian

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.

Signature · 5 lines
Regards/Gruss,
    Boris.

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