Commit e644896f authored by Like Xu's avatar Like Xu Committed by Paolo Bonzini

KVM: x86/pmu: Fix and isolate TSX-specific performance event logic

HSW_IN_TX* bits are used in generic code which are not supported on
AMD. Worse, these bits overlap with AMD EventSelect[11:8] and hence
using HSW_IN_TX* bits unconditionally in generic code is resulting in
unintentional pmu behavior on AMD. For example, if EventSelect[11:8]
is 0x2, pmc_reprogram_counter() wrongly assumes that
HSW_IN_TX_CHECKPOINTED is set and thus forces sampling period to be 0.

Also per the SDM, both bits 32 and 33 "may only be set if the processor
supports HLE or RTM" and for "IN_TXCP (bit 33): this bit may only be set
for IA32_PERFEVTSEL2."

Opportunistically eliminate code redundancy, because if the HSW_IN_TX*
bit is set in pmc->eventsel, it is already set in attr.config.
Reported-by: default avatarRavi Bangoria <ravi.bangoria@amd.com>
Reported-by: default avatarJim Mattson <jmattson@google.com>
Fixes: 103af0a9 ("perf, kvm: Support the in_tx/in_tx_cp modifiers in KVM arch perfmon emulation v5")
Co-developed-by: default avatarRavi Bangoria <ravi.bangoria@amd.com>
Signed-off-by: default avatarRavi Bangoria <ravi.bangoria@amd.com>
Signed-off-by: default avatarLike Xu <likexu@tencent.com>
Message-Id: <20220309084257.88931-1-likexu@tencent.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 5959ff4a
...@@ -96,8 +96,7 @@ static void kvm_perf_overflow(struct perf_event *perf_event, ...@@ -96,8 +96,7 @@ static void kvm_perf_overflow(struct perf_event *perf_event,
static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type,
u64 config, bool exclude_user, u64 config, bool exclude_user,
bool exclude_kernel, bool intr, bool exclude_kernel, bool intr)
bool in_tx, bool in_tx_cp)
{ {
struct perf_event *event; struct perf_event *event;
struct perf_event_attr attr = { struct perf_event_attr attr = {
...@@ -116,16 +115,14 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, ...@@ -116,16 +115,14 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type,
attr.sample_period = get_sample_period(pmc, pmc->counter); attr.sample_period = get_sample_period(pmc, pmc->counter);
if (in_tx) if ((attr.config & HSW_IN_TX_CHECKPOINTED) &&
attr.config |= HSW_IN_TX; guest_cpuid_is_intel(pmc->vcpu)) {
if (in_tx_cp) {
/* /*
* HSW_IN_TX_CHECKPOINTED is not supported with nonzero * HSW_IN_TX_CHECKPOINTED is not supported with nonzero
* period. Just clear the sample period so at least * period. Just clear the sample period so at least
* allocating the counter doesn't fail. * allocating the counter doesn't fail.
*/ */
attr.sample_period = 0; attr.sample_period = 0;
attr.config |= HSW_IN_TX_CHECKPOINTED;
} }
event = perf_event_create_kernel_counter(&attr, -1, current, event = perf_event_create_kernel_counter(&attr, -1, current,
...@@ -233,9 +230,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) ...@@ -233,9 +230,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
pmc_reprogram_counter(pmc, type, config, pmc_reprogram_counter(pmc, type, config,
!(eventsel & ARCH_PERFMON_EVENTSEL_USR), !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
!(eventsel & ARCH_PERFMON_EVENTSEL_OS), !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
eventsel & ARCH_PERFMON_EVENTSEL_INT, eventsel & ARCH_PERFMON_EVENTSEL_INT);
(eventsel & HSW_IN_TX),
(eventsel & HSW_IN_TX_CHECKPOINTED));
} }
EXPORT_SYMBOL_GPL(reprogram_gp_counter); EXPORT_SYMBOL_GPL(reprogram_gp_counter);
...@@ -271,7 +266,7 @@ void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx) ...@@ -271,7 +266,7 @@ void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx)
kvm_x86_ops.pmu_ops->pmc_perf_hw_id(pmc), kvm_x86_ops.pmu_ops->pmc_perf_hw_id(pmc),
!(en_field & 0x2), /* exclude user */ !(en_field & 0x2), /* exclude user */
!(en_field & 0x1), /* exclude kernel */ !(en_field & 0x1), /* exclude kernel */
pmi, false, false); pmi);
} }
EXPORT_SYMBOL_GPL(reprogram_fixed_counter); EXPORT_SYMBOL_GPL(reprogram_fixed_counter);
......
...@@ -389,6 +389,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) ...@@ -389,6 +389,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
struct kvm_pmc *pmc; struct kvm_pmc *pmc;
u32 msr = msr_info->index; u32 msr = msr_info->index;
u64 data = msr_info->data; u64 data = msr_info->data;
u64 reserved_bits;
switch (msr) { switch (msr) {
case MSR_CORE_PERF_FIXED_CTR_CTRL: case MSR_CORE_PERF_FIXED_CTR_CTRL:
...@@ -443,7 +444,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) ...@@ -443,7 +444,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
if (data == pmc->eventsel) if (data == pmc->eventsel)
return 0; return 0;
if (!(data & pmu->reserved_bits)) { reserved_bits = pmu->reserved_bits;
if ((pmc->idx == 2) &&
(pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
if (!(data & reserved_bits)) {
reprogram_gp_counter(pmc, data); reprogram_gp_counter(pmc, data);
return 0; return 0;
} }
...@@ -534,8 +539,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) ...@@ -534,8 +539,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
entry = kvm_find_cpuid_entry(vcpu, 7, 0); entry = kvm_find_cpuid_entry(vcpu, 7, 0);
if (entry && if (entry &&
(boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) && (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
(entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; pmu->reserved_bits ^= HSW_IN_TX;
pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
}
bitmap_set(pmu->all_valid_pmc_idx, bitmap_set(pmu->all_valid_pmc_idx,
0, pmu->nr_arch_gp_counters); 0, pmu->nr_arch_gp_counters);
......
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