1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Kernel-based Virtual Machine -- Performance Monitoring Unit support 4 * 5 * Copyright 2015 Red Hat, Inc. and/or its affiliates. 6 * 7 * Authors: 8 * Avi Kivity <avi@redhat.com> 9 * Gleb Natapov <gleb@redhat.com> 10 * Wei Huang <wei@redhat.com> 11 */ 12 13#include <linux/types.h> 14#include <linux/kvm_host.h> 15#include <linux/perf_event.h> 16#include <linux/bsearch.h> 17#include <linux/sort.h> 18#include <asm/perf_event.h> 19#include "x86.h" 20#include "cpuid.h" 21#include "lapic.h" 22#include "pmu.h" 23 24/* This is enough to filter the vast majority of currently defined events. */ 25#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300 26 27/* NOTE: 28 * - Each perf counter is defined as "struct kvm_pmc"; 29 * - There are two types of perf counters: general purpose (gp) and fixed. 30 * gp counters are stored in gp_counters[] and fixed counters are stored 31 * in fixed_counters[] respectively. Both of them are part of "struct 32 * kvm_pmu"; 33 * - pmu.c understands the difference between gp counters and fixed counters. 34 * However AMD doesn't support fixed-counters; 35 * - There are three types of index to access perf counters (PMC): 36 * 1. MSR (named msr): For example Intel has MSR_IA32_PERFCTRn and AMD 37 * has MSR_K7_PERFCTRn. 38 * 2. MSR Index (named idx): This normally is used by RDPMC instruction. 39 * For instance AMD RDPMC instruction uses 0000_0003h in ECX to access 40 * C001_0007h (MSR_K7_PERCTR3). Intel has a similar mechanism, except 41 * that it also supports fixed counters. idx can be used to as index to 42 * gp and fixed counters. 43 * 3. Global PMC Index (named pmc): pmc is an index specific to PMU 44 * code. Each pmc, stored in kvm_pmc.idx field, is unique across 45 * all perf counters (both gp and fixed). The mapping relationship 46 * between pmc and perf counters is as the following: 47 * * Intel: [0 .. INTEL_PMC_MAX_GENERIC-1] <=> gp counters 48 * [INTEL_PMC_IDX_FIXED .. INTEL_PMC_IDX_FIXED + 2] <=> fixed 49 * * AMD: [0 .. AMD64_NUM_COUNTERS-1] <=> gp counters 50 */ 51 52static void kvm_pmi_trigger_fn(struct irq_work *irq_work) 53{ 54 struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); 55 struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); 56 57 kvm_pmu_deliver_pmi(vcpu); 58} 59 60static void kvm_perf_overflow(struct perf_event *perf_event, 61 struct perf_sample_data *data, 62 struct pt_regs *regs) 63{ 64 struct kvm_pmc *pmc = perf_event->overflow_handler_context; 65 struct kvm_pmu *pmu = pmc_to_pmu(pmc); 66 67 if (!test_and_set_bit(pmc->idx, pmu->reprogram_pmi)) { 68 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); 69 kvm_make_request(KVM_REQ_PMU, pmc->vcpu); 70 } 71} 72 73static void kvm_perf_overflow_intr(struct perf_event *perf_event, 74 struct perf_sample_data *data, 75 struct pt_regs *regs) 76{ 77 struct kvm_pmc *pmc = perf_event->overflow_handler_context; 78 struct kvm_pmu *pmu = pmc_to_pmu(pmc); 79 80 if (!test_and_set_bit(pmc->idx, pmu->reprogram_pmi)) { 81 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); 82 kvm_make_request(KVM_REQ_PMU, pmc->vcpu); 83 84 /* 85 * Inject PMI. If vcpu was in a guest mode during NMI PMI 86 * can be ejected on a guest mode re-entry. Otherwise we can't 87 * be sure that vcpu wasn't executing hlt instruction at the 88 * time of vmexit and is not going to re-enter guest mode until 89 * woken up. So we should wake it, but this is impossible from 90 * NMI context. Do it from irq work instead. 91 */ 92 if (!kvm_is_in_guest()) 93 irq_work_queue(&pmc_to_pmu(pmc)->irq_work); 94 else 95 kvm_make_request(KVM_REQ_PMI, pmc->vcpu); 96 } 97} 98 99static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, 100 u64 config, bool exclude_user, 101 bool exclude_kernel, bool intr, 102 bool in_tx, bool in_tx_cp) 103{ 104 struct perf_event *event; 105 struct perf_event_attr attr = { 106 .type = type, 107 .size = sizeof(attr), 108 .pinned = true, 109 .exclude_idle = true, 110 .exclude_host = 1, 111 .exclude_user = exclude_user, 112 .exclude_kernel = exclude_kernel, 113 .config = config, 114 }; 115 116 attr.sample_period = get_sample_period(pmc, pmc->counter); 117 118 if (in_tx) 119 attr.config |= HSW_IN_TX; 120 if (in_tx_cp) { 121 /* 122 * HSW_IN_TX_CHECKPOINTED is not supported with nonzero 123 * period. Just clear the sample period so at least 124 * allocating the counter doesn't fail. 125 */ 126 attr.sample_period = 0; 127 attr.config |= HSW_IN_TX_CHECKPOINTED; 128 } 129 130 event = perf_event_create_kernel_counter(&attr, -1, current, 131 intr ? kvm_perf_overflow_intr : 132 kvm_perf_overflow, pmc); 133 if (IS_ERR(event)) { 134 pr_debug_ratelimited("kvm_pmu: event creation failed %ld for pmc->idx = %d\n", 135 PTR_ERR(event), pmc->idx); 136 return; 137 } 138 139 pmc->perf_event = event; 140 pmc_to_pmu(pmc)->event_count++; 141 clear_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi); 142} 143 144static void pmc_pause_counter(struct kvm_pmc *pmc) 145{ 146 u64 counter = pmc->counter; 147 148 if (!pmc->perf_event) 149 return; 150 151 /* update counter, reset event value to avoid redundant accumulation */ 152 counter += perf_event_pause(pmc->perf_event, true); 153 pmc->counter = counter & pmc_bitmask(pmc); 154} 155 156static bool pmc_resume_counter(struct kvm_pmc *pmc) 157{ 158 if (!pmc->perf_event) 159 return false; 160 161 /* recalibrate sample period and check if it's accepted by perf core */ 162 if (perf_event_period(pmc->perf_event, 163 get_sample_period(pmc, pmc->counter))) 164 return false; 165 166 /* reuse perf_event to serve as pmc_reprogram_counter() does*/ 167 perf_event_enable(pmc->perf_event); 168 169 clear_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->reprogram_pmi); 170 return true; 171} 172 173static int cmp_u64(const void *pa, const void *pb) 174{ 175 u64 a = *(u64 *)pa; 176 u64 b = *(u64 *)pb; 177 178 return (a > b) - (a < b); 179} 180 181void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) 182{ 183 u64 config; 184 u32 type = PERF_TYPE_RAW; 185 struct kvm *kvm = pmc->vcpu->kvm; 186 struct kvm_pmu_event_filter *filter; 187 struct kvm_pmu *pmu = vcpu_to_pmu(pmc->vcpu); 188 bool allow_event = true; 189 190 if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) 191 printk_once("kvm pmu: pin control bit is ignored\n"); 192 193 pmc->eventsel = eventsel; 194 195 pmc_pause_counter(pmc); 196 197 if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)) 198 return; 199 200 filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); 201 if (filter) { 202 __u64 key = eventsel & AMD64_RAW_EVENT_MASK_NB; 203 204 if (bsearch(&key, filter->events, filter->nevents, 205 sizeof(__u64), cmp_u64)) 206 allow_event = filter->action == KVM_PMU_EVENT_ALLOW; 207 else 208 allow_event = filter->action == KVM_PMU_EVENT_DENY; 209 } 210 if (!allow_event) 211 return; 212 213 if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE | 214 ARCH_PERFMON_EVENTSEL_INV | 215 ARCH_PERFMON_EVENTSEL_CMASK | 216 HSW_IN_TX | 217 HSW_IN_TX_CHECKPOINTED))) { 218 config = kvm_x86_ops.pmu_ops->pmc_perf_hw_id(pmc); 219 if (config != PERF_COUNT_HW_MAX) 220 type = PERF_TYPE_HARDWARE; 221 } 222 223 if (type == PERF_TYPE_RAW) 224 config = eventsel & pmu->raw_event_mask; 225 226 if (pmc->current_config == eventsel && pmc_resume_counter(pmc)) 227 return; 228 229 pmc_release_perf_event(pmc); 230 231 pmc->current_config = eventsel; 232 pmc_reprogram_counter(pmc, type, config, 233 !(eventsel & ARCH_PERFMON_EVENTSEL_USR), 234 !(eventsel & ARCH_PERFMON_EVENTSEL_OS), 235 eventsel & ARCH_PERFMON_EVENTSEL_INT, 236 (eventsel & HSW_IN_TX), 237 (eventsel & HSW_IN_TX_CHECKPOINTED)); 238} 239EXPORT_SYMBOL_GPL(reprogram_gp_counter); 240 241void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx) 242{ 243 unsigned en_field = ctrl & 0x3; 244 bool pmi = ctrl & 0x8; 245 struct kvm_pmu_event_filter *filter; 246 struct kvm *kvm = pmc->vcpu->kvm; 247 248 pmc_pause_counter(pmc); 249 250 if (!en_field || !pmc_is_enabled(pmc)) 251 return; 252 253 filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); 254 if (filter) { 255 if (filter->action == KVM_PMU_EVENT_DENY && 256 test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) 257 return; 258 if (filter->action == KVM_PMU_EVENT_ALLOW && 259 !test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) 260 return; 261 } 262 263 if (pmc->current_config == (u64)ctrl && pmc_resume_counter(pmc)) 264 return; 265 266 pmc_release_perf_event(pmc); 267 268 pmc->current_config = (u64)ctrl; 269 pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE, 270 kvm_x86_ops.pmu_ops->find_fixed_event(idx), 271 !(en_field & 0x2), /* exclude user */ 272 !(en_field & 0x1), /* exclude kernel */ 273 pmi, false, false); 274} 275EXPORT_SYMBOL_GPL(reprogram_fixed_counter); 276 277void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx) 278{ 279 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, pmc_idx); 280 281 if (!pmc) 282 return; 283 284 if (pmc_is_gp(pmc)) 285 reprogram_gp_counter(pmc, pmc->eventsel); 286 else { 287 int idx = pmc_idx - INTEL_PMC_IDX_FIXED; 288 u8 ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, idx); 289 290 reprogram_fixed_counter(pmc, ctrl, idx); 291 } 292} 293EXPORT_SYMBOL_GPL(reprogram_counter); 294 295void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) 296{ 297 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 298 int bit; 299 300 for_each_set_bit(bit, pmu->reprogram_pmi, X86_PMC_IDX_MAX) { 301 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, bit); 302 303 if (unlikely(!pmc || !pmc->perf_event)) { 304 clear_bit(bit, pmu->reprogram_pmi); 305 continue; 306 } 307 308 reprogram_counter(pmu, bit); 309 } 310 311 /* 312 * Unused perf_events are only released if the corresponding MSRs 313 * weren't accessed during the last vCPU time slice. kvm_arch_sched_in 314 * triggers KVM_REQ_PMU if cleanup is needed. 315 */ 316 if (unlikely(pmu->need_cleanup)) 317 kvm_pmu_cleanup(vcpu); 318} 319 320/* check if idx is a valid index to access PMU */ 321int kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) 322{ 323 return kvm_x86_ops.pmu_ops->is_valid_rdpmc_ecx(vcpu, idx); 324} 325 326bool is_vmware_backdoor_pmc(u32 pmc_idx) 327{ 328 switch (pmc_idx) { 329 case VMWARE_BACKDOOR_PMC_HOST_TSC: 330 case VMWARE_BACKDOOR_PMC_REAL_TIME: 331 case VMWARE_BACKDOOR_PMC_APPARENT_TIME: 332 return true; 333 } 334 return false; 335} 336 337static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) 338{ 339 u64 ctr_val; 340 341 switch (idx) { 342 case VMWARE_BACKDOOR_PMC_HOST_TSC: 343 ctr_val = rdtsc(); 344 break; 345 case VMWARE_BACKDOOR_PMC_REAL_TIME: 346 ctr_val = ktime_get_boottime_ns(); 347 break; 348 case VMWARE_BACKDOOR_PMC_APPARENT_TIME: 349 ctr_val = ktime_get_boottime_ns() + 350 vcpu->kvm->arch.kvmclock_offset; 351 break; 352 default: 353 return 1; 354 } 355 356 *data = ctr_val; 357 return 0; 358} 359 360int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) 361{ 362 bool fast_mode = idx & (1u << 31); 363 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 364 struct kvm_pmc *pmc; 365 u64 mask = fast_mode ? ~0u : ~0ull; 366 367 if (!pmu->version) 368 return 1; 369 370 if (is_vmware_backdoor_pmc(idx)) 371 return kvm_pmu_rdpmc_vmware(vcpu, idx, data); 372 373 pmc = kvm_x86_ops.pmu_ops->rdpmc_ecx_to_pmc(vcpu, idx, &mask); 374 if (!pmc) 375 return 1; 376 377 if (!(kvm_read_cr4(vcpu) & X86_CR4_PCE) && 378 (kvm_x86_ops.get_cpl(vcpu) != 0) && 379 (kvm_read_cr0(vcpu) & X86_CR0_PE)) 380 return 1; 381 382 *data = pmc_read_counter(pmc) & mask; 383 return 0; 384} 385 386void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu) 387{ 388 if (lapic_in_kernel(vcpu)) 389 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC); 390} 391 392bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) 393{ 394 return kvm_x86_ops.pmu_ops->msr_idx_to_pmc(vcpu, msr) || 395 kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, msr); 396} 397 398static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr) 399{ 400 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 401 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->msr_idx_to_pmc(vcpu, msr); 402 403 if (pmc) 404 __set_bit(pmc->idx, pmu->pmc_in_use); 405} 406 407int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 408{ 409 return kvm_x86_ops.pmu_ops->get_msr(vcpu, msr_info); 410} 411 412int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) 413{ 414 kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index); 415 return kvm_x86_ops.pmu_ops->set_msr(vcpu, msr_info); 416} 417 418/* refresh PMU settings. This function generally is called when underlying 419 * settings are changed (such as changes of PMU CPUID by guest VMs), which 420 * should rarely happen. 421 */ 422void kvm_pmu_refresh(struct kvm_vcpu *vcpu) 423{ 424 kvm_x86_ops.pmu_ops->refresh(vcpu); 425} 426 427void kvm_pmu_reset(struct kvm_vcpu *vcpu) 428{ 429 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 430 431 irq_work_sync(&pmu->irq_work); 432 kvm_x86_ops.pmu_ops->reset(vcpu); 433} 434 435void kvm_pmu_init(struct kvm_vcpu *vcpu) 436{ 437 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 438 439 memset(pmu, 0, sizeof(*pmu)); 440 kvm_x86_ops.pmu_ops->init(vcpu); 441 init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); 442 pmu->event_count = 0; 443 pmu->need_cleanup = false; 444 kvm_pmu_refresh(vcpu); 445} 446 447static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc) 448{ 449 struct kvm_pmu *pmu = pmc_to_pmu(pmc); 450 451 if (pmc_is_fixed(pmc)) 452 return fixed_ctrl_field(pmu->fixed_ctr_ctrl, 453 pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3; 454 455 return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE; 456} 457 458/* Release perf_events for vPMCs that have been unused for a full time slice. */ 459void kvm_pmu_cleanup(struct kvm_vcpu *vcpu) 460{ 461 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); 462 struct kvm_pmc *pmc = NULL; 463 DECLARE_BITMAP(bitmask, X86_PMC_IDX_MAX); 464 int i; 465 466 pmu->need_cleanup = false; 467 468 bitmap_andnot(bitmask, pmu->all_valid_pmc_idx, 469 pmu->pmc_in_use, X86_PMC_IDX_MAX); 470 471 for_each_set_bit(i, bitmask, X86_PMC_IDX_MAX) { 472 pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, i); 473 474 if (pmc && pmc->perf_event && !pmc_speculative_in_use(pmc)) 475 pmc_stop_counter(pmc); 476 } 477 478 bitmap_zero(pmu->pmc_in_use, X86_PMC_IDX_MAX); 479} 480 481void kvm_pmu_destroy(struct kvm_vcpu *vcpu) 482{ 483 kvm_pmu_reset(vcpu); 484} 485 486int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) 487{ 488 struct kvm_pmu_event_filter tmp, *filter; 489 size_t size; 490 int r; 491 492 if (copy_from_user(&tmp, argp, sizeof(tmp))) 493 return -EFAULT; 494 495 if (tmp.action != KVM_PMU_EVENT_ALLOW && 496 tmp.action != KVM_PMU_EVENT_DENY) 497 return -EINVAL; 498 499 if (tmp.flags != 0) 500 return -EINVAL; 501 502 if (tmp.nevents > KVM_PMU_EVENT_FILTER_MAX_EVENTS) 503 return -E2BIG; 504 505 size = struct_size(filter, events, tmp.nevents); 506 filter = kmalloc(size, GFP_KERNEL_ACCOUNT); 507 if (!filter) 508 return -ENOMEM; 509 510 r = -EFAULT; 511 if (copy_from_user(filter, argp, size)) 512 goto cleanup; 513 514 /* Ensure nevents can't be changed between the user copies. */ 515 *filter = tmp; 516 517 /* 518 * Sort the in-kernel list so that we can search it with bsearch. 519 */ 520 sort(&filter->events, filter->nevents, sizeof(__u64), cmp_u64, NULL); 521 522 mutex_lock(&kvm->lock); 523 filter = rcu_replace_pointer(kvm->arch.pmu_event_filter, filter, 524 mutex_is_locked(&kvm->lock)); 525 mutex_unlock(&kvm->lock); 526 527 synchronize_srcu_expedited(&kvm->srcu); 528 r = 0; 529cleanup: 530 kfree(filter); 531 return r; 532} 533