Commit 68ab7476 authored by Don Zickus's avatar Don Zickus Committed by Ingo Molnar

perf/x86: Tweak broken BIOS rules during check_hw_exists()

I stumbled upon an AMD box that had the BIOS using a hardware performance
counter. Instead of printing out a warning and continuing, it failed and
blocked further perf counter usage.

Looking through the history, I found this commit:

  a5ebe0ba ("perf/x86: Check all MSRs before passing hw check")

which tweaked the rules for a Xen guest on an almost identical box and now
changed the behaviour.

Unfortunately the rules were tweaked incorrectly and will always lead to
MSR failures even though the MSRs are completely fine.

What happens now is in arch/x86/kernel/cpu/perf_event.c::check_hw_exists():

<snip>
        for (i = 0; i < x86_pmu.num_counters; i++) {
                reg = x86_pmu_config_addr(i);
                ret = rdmsrl_safe(reg, &val);
                if (ret)
                        goto msr_fail;
                if (val & ARCH_PERFMON_EVENTSEL_ENABLE) {
                        bios_fail = 1;
                        val_fail = val;
                        reg_fail = reg;
                }
        }

<snip>
        /*
         * Read the current value, change it and read it back to see if it
         * matches, this is needed to detect certain hardware emulators
         * (qemu/kvm) that don't trap on the MSR access and always return 0s.
         */
        reg = x86_pmu_event_addr(0);
				^^^^

if the first perf counter is enabled, then this routine will always fail
because the counter is running. :-(

        if (rdmsrl_safe(reg, &val))
                goto msr_fail;
        val ^= 0xffffUL;
        ret = wrmsrl_safe(reg, val);
        ret |= rdmsrl_safe(reg, &val_new);
        if (ret || val != val_new)
                goto msr_fail;

The above bios_fail used to be a 'goto' which is why it worked in the past.

Further, most vendors have migrated to using fixed counters to hide their
evilness hence this problem rarely shows up now days except on a few old boxes.

I fixed my problem and kept the spirit of the original Xen fix, by recording a
safe non-enable register to be used safely for the reading/writing check.
Because it is not enabled, this passes on bare metal boxes (like metal), but
should continue to throw an msr_fail on Xen guests because the register isn't
emulated yet.

Now I get a proper bios_fail error message and Xen should still see their
msr_fail message (untested).
Signed-off-by: default avatarDon Zickus <dzickus@redhat.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: george.dunlap@eu.citrix.com
Cc: konrad.wilk@oracle.com
Link: http://lkml.kernel.org/r/1431976608-56970-1-git-send-email-dzickus@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent f73ec48c
...@@ -190,6 +190,7 @@ static bool check_hw_exists(void) ...@@ -190,6 +190,7 @@ static bool check_hw_exists(void)
u64 val, val_fail, val_new= ~0; u64 val, val_fail, val_new= ~0;
int i, reg, reg_fail, ret = 0; int i, reg, reg_fail, ret = 0;
int bios_fail = 0; int bios_fail = 0;
int reg_safe = -1;
/* /*
* Check to see if the BIOS enabled any of the counters, if so * Check to see if the BIOS enabled any of the counters, if so
...@@ -204,6 +205,8 @@ static bool check_hw_exists(void) ...@@ -204,6 +205,8 @@ static bool check_hw_exists(void)
bios_fail = 1; bios_fail = 1;
val_fail = val; val_fail = val;
reg_fail = reg; reg_fail = reg;
} else {
reg_safe = i;
} }
} }
...@@ -221,12 +224,23 @@ static bool check_hw_exists(void) ...@@ -221,12 +224,23 @@ static bool check_hw_exists(void)
} }
} }
/*
* If all the counters are enabled, the below test will always
* fail. The tools will also become useless in this scenario.
* Just fail and disable the hardware counters.
*/
if (reg_safe == -1) {
reg = reg_safe;
goto msr_fail;
}
/* /*
* Read the current value, change it and read it back to see if it * Read the current value, change it and read it back to see if it
* matches, this is needed to detect certain hardware emulators * matches, this is needed to detect certain hardware emulators
* (qemu/kvm) that don't trap on the MSR access and always return 0s. * (qemu/kvm) that don't trap on the MSR access and always return 0s.
*/ */
reg = x86_pmu_event_addr(0); reg = x86_pmu_event_addr(reg_safe);
if (rdmsrl_safe(reg, &val)) if (rdmsrl_safe(reg, &val))
goto msr_fail; goto msr_fail;
val ^= 0xffffUL; val ^= 0xffffUL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment