Skip to content

Commit 338b522

Browse files
kliang2Ingo Molnar
authored andcommitted
perf/x86/intel: Protect LBR and extra_regs against KVM lying
With -cpu host, KVM reports LBR and extra_regs support, if the host has support. When the guest perf driver tries to access LBR or extra_regs MSR, it #GPs all MSR accesses,since KVM doesn't handle LBR and extra_regs support. So check the related MSRs access right once at initialization time to avoid the error access at runtime. For reproducing the issue, please build the kernel with CONFIG_KVM_INTEL = y (for host kernel). And CONFIG_PARAVIRT = n and CONFIG_KVM_GUEST = n (for guest kernel). Start the guest with -cpu host. Run perf record with --branch-any or --branch-filter in guest to trigger LBR Run perf stat offcore events (E.g. LLC-loads/LLC-load-misses ...) in guest to trigger offcore_rsp #GP Signed-off-by: Kan Liang <kan.liang@intel.com> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Cc: Andi Kleen <ak@linux.intel.com> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Maria Dimakopoulou <maria.n.dimakopoulou@gmail.com> Cc: Mark Davies <junk@eslaf.co.uk> Cc: Paul Mackerras <paulus@samba.org> Cc: Stephane Eranian <eranian@google.com> Cc: Yan, Zheng <zheng.z.yan@intel.com> Link: http://lkml.kernel.org/r/1405365957-20202-1-git-send-email-kan.liang@intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4a1c0f2 commit 338b522

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

arch/x86/kernel/cpu/perf_event.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
118118
continue;
119119
if (event->attr.config1 & ~er->valid_mask)
120120
return -EINVAL;
121+
/* Check if the extra msrs can be safely accessed*/
122+
if (!er->extra_msr_access)
123+
return -ENXIO;
121124

122125
reg->idx = er->idx;
123126
reg->config = event->attr.config1;

arch/x86/kernel/cpu/perf_event.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,16 @@ struct extra_reg {
295295
u64 config_mask;
296296
u64 valid_mask;
297297
int idx; /* per_xxx->regs[] reg index */
298+
bool extra_msr_access;
298299
};
299300

300301
#define EVENT_EXTRA_REG(e, ms, m, vm, i) { \
301-
.event = (e), \
302-
.msr = (ms), \
303-
.config_mask = (m), \
304-
.valid_mask = (vm), \
305-
.idx = EXTRA_REG_##i, \
302+
.event = (e), \
303+
.msr = (ms), \
304+
.config_mask = (m), \
305+
.valid_mask = (vm), \
306+
.idx = EXTRA_REG_##i, \
307+
.extra_msr_access = true, \
306308
}
307309

308310
#define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \

arch/x86/kernel/cpu/perf_event_intel.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,41 @@ static void intel_snb_check_microcode(void)
21822182
}
21832183
}
21842184

2185+
/*
2186+
* Under certain circumstances, access certain MSR may cause #GP.
2187+
* The function tests if the input MSR can be safely accessed.
2188+
*/
2189+
static bool check_msr(unsigned long msr, u64 mask)
2190+
{
2191+
u64 val_old, val_new, val_tmp;
2192+
2193+
/*
2194+
* Read the current value, change it and read it back to see if it
2195+
* matches, this is needed to detect certain hardware emulators
2196+
* (qemu/kvm) that don't trap on the MSR access and always return 0s.
2197+
*/
2198+
if (rdmsrl_safe(msr, &val_old))
2199+
return false;
2200+
2201+
/*
2202+
* Only change the bits which can be updated by wrmsrl.
2203+
*/
2204+
val_tmp = val_old ^ mask;
2205+
if (wrmsrl_safe(msr, val_tmp) ||
2206+
rdmsrl_safe(msr, &val_new))
2207+
return false;
2208+
2209+
if (val_new != val_tmp)
2210+
return false;
2211+
2212+
/* Here it's sure that the MSR can be safely accessed.
2213+
* Restore the old value and return.
2214+
*/
2215+
wrmsrl(msr, val_old);
2216+
2217+
return true;
2218+
}
2219+
21852220
static __init void intel_sandybridge_quirk(void)
21862221
{
21872222
x86_pmu.check_microcode = intel_snb_check_microcode;
@@ -2271,7 +2306,8 @@ __init int intel_pmu_init(void)
22712306
union cpuid10_ebx ebx;
22722307
struct event_constraint *c;
22732308
unsigned int unused;
2274-
int version;
2309+
struct extra_reg *er;
2310+
int version, i;
22752311

22762312
if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
22772313
switch (boot_cpu_data.x86) {
@@ -2577,6 +2613,34 @@ __init int intel_pmu_init(void)
25772613
}
25782614
}
25792615

2616+
/*
2617+
* Access LBR MSR may cause #GP under certain circumstances.
2618+
* E.g. KVM doesn't support LBR MSR
2619+
* Check all LBT MSR here.
2620+
* Disable LBR access if any LBR MSRs can not be accessed.
2621+
*/
2622+
if (x86_pmu.lbr_nr && !check_msr(x86_pmu.lbr_tos, 0x3UL))
2623+
x86_pmu.lbr_nr = 0;
2624+
for (i = 0; i < x86_pmu.lbr_nr; i++) {
2625+
if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
2626+
check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
2627+
x86_pmu.lbr_nr = 0;
2628+
}
2629+
2630+
/*
2631+
* Access extra MSR may cause #GP under certain circumstances.
2632+
* E.g. KVM doesn't support offcore event
2633+
* Check all extra_regs here.
2634+
*/
2635+
if (x86_pmu.extra_regs) {
2636+
for (er = x86_pmu.extra_regs; er->msr; er++) {
2637+
er->extra_msr_access = check_msr(er->msr, 0x1ffUL);
2638+
/* Disable LBR select mapping */
2639+
if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
2640+
x86_pmu.lbr_sel_map = NULL;
2641+
}
2642+
}
2643+
25802644
/* Support full width counters using alternative MSR range */
25812645
if (x86_pmu.intel_cap.full_width_write) {
25822646
x86_pmu.max_period = x86_pmu.cntval_mask;

0 commit comments

Comments
 (0)