xref: /openbmc/linux/arch/x86/kvm/pmu.c (revision e6e8c6c2)
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 <asm/cpu_device_id.h>
20 #include "x86.h"
21 #include "cpuid.h"
22 #include "lapic.h"
23 #include "pmu.h"
24 
25 /* This is enough to filter the vast majority of currently defined events. */
26 #define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
27 
28 struct x86_pmu_capability __read_mostly kvm_pmu_cap;
29 EXPORT_SYMBOL_GPL(kvm_pmu_cap);
30 
31 static const struct x86_cpu_id vmx_icl_pebs_cpu[] = {
32 	X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, NULL),
33 	X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, NULL),
34 	{}
35 };
36 
37 /* NOTE:
38  * - Each perf counter is defined as "struct kvm_pmc";
39  * - There are two types of perf counters: general purpose (gp) and fixed.
40  *   gp counters are stored in gp_counters[] and fixed counters are stored
41  *   in fixed_counters[] respectively. Both of them are part of "struct
42  *   kvm_pmu";
43  * - pmu.c understands the difference between gp counters and fixed counters.
44  *   However AMD doesn't support fixed-counters;
45  * - There are three types of index to access perf counters (PMC):
46  *     1. MSR (named msr): For example Intel has MSR_IA32_PERFCTRn and AMD
47  *        has MSR_K7_PERFCTRn and, for families 15H and later,
48  *        MSR_F15H_PERF_CTRn, where MSR_F15H_PERF_CTR[0-3] are
49  *        aliased to MSR_K7_PERFCTRn.
50  *     2. MSR Index (named idx): This normally is used by RDPMC instruction.
51  *        For instance AMD RDPMC instruction uses 0000_0003h in ECX to access
52  *        C001_0007h (MSR_K7_PERCTR3). Intel has a similar mechanism, except
53  *        that it also supports fixed counters. idx can be used to as index to
54  *        gp and fixed counters.
55  *     3. Global PMC Index (named pmc): pmc is an index specific to PMU
56  *        code. Each pmc, stored in kvm_pmc.idx field, is unique across
57  *        all perf counters (both gp and fixed). The mapping relationship
58  *        between pmc and perf counters is as the following:
59  *        * Intel: [0 .. INTEL_PMC_MAX_GENERIC-1] <=> gp counters
60  *                 [INTEL_PMC_IDX_FIXED .. INTEL_PMC_IDX_FIXED + 2] <=> fixed
61  *        * AMD:   [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H
62  *          and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] <=> gp counters
63  */
64 
65 static struct kvm_pmu_ops kvm_pmu_ops __read_mostly;
66 
67 #define KVM_X86_PMU_OP(func)					     \
68 	DEFINE_STATIC_CALL_NULL(kvm_x86_pmu_##func,			     \
69 				*(((struct kvm_pmu_ops *)0)->func));
70 #define KVM_X86_PMU_OP_OPTIONAL KVM_X86_PMU_OP
71 #include <asm/kvm-x86-pmu-ops.h>
72 
73 void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)
74 {
75 	memcpy(&kvm_pmu_ops, pmu_ops, sizeof(kvm_pmu_ops));
76 
77 #define __KVM_X86_PMU_OP(func) \
78 	static_call_update(kvm_x86_pmu_##func, kvm_pmu_ops.func);
79 #define KVM_X86_PMU_OP(func) \
80 	WARN_ON(!kvm_pmu_ops.func); __KVM_X86_PMU_OP(func)
81 #define KVM_X86_PMU_OP_OPTIONAL __KVM_X86_PMU_OP
82 #include <asm/kvm-x86-pmu-ops.h>
83 #undef __KVM_X86_PMU_OP
84 }
85 
86 static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
87 {
88 	return static_call(kvm_x86_pmu_pmc_is_enabled)(pmc);
89 }
90 
91 static void kvm_pmi_trigger_fn(struct irq_work *irq_work)
92 {
93 	struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work);
94 	struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
95 
96 	kvm_pmu_deliver_pmi(vcpu);
97 }
98 
99 static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
100 {
101 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
102 	bool skip_pmi = false;
103 
104 	/* Ignore counters that have been reprogrammed already. */
105 	if (test_and_set_bit(pmc->idx, pmu->reprogram_pmi))
106 		return;
107 
108 	if (pmc->perf_event && pmc->perf_event->attr.precise_ip) {
109 		/* Indicate PEBS overflow PMI to guest. */
110 		skip_pmi = __test_and_set_bit(GLOBAL_STATUS_BUFFER_OVF_BIT,
111 					      (unsigned long *)&pmu->global_status);
112 	} else {
113 		__set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
114 	}
115 	kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
116 
117 	if (!pmc->intr || skip_pmi)
118 		return;
119 
120 	/*
121 	 * Inject PMI. If vcpu was in a guest mode during NMI PMI
122 	 * can be ejected on a guest mode re-entry. Otherwise we can't
123 	 * be sure that vcpu wasn't executing hlt instruction at the
124 	 * time of vmexit and is not going to re-enter guest mode until
125 	 * woken up. So we should wake it, but this is impossible from
126 	 * NMI context. Do it from irq work instead.
127 	 */
128 	if (in_pmi && !kvm_handling_nmi_from_guest(pmc->vcpu))
129 		irq_work_queue(&pmc_to_pmu(pmc)->irq_work);
130 	else
131 		kvm_make_request(KVM_REQ_PMI, pmc->vcpu);
132 }
133 
134 static void kvm_perf_overflow(struct perf_event *perf_event,
135 			      struct perf_sample_data *data,
136 			      struct pt_regs *regs)
137 {
138 	struct kvm_pmc *pmc = perf_event->overflow_handler_context;
139 
140 	__kvm_perf_overflow(pmc, true);
141 }
142 
143 static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type,
144 				  u64 config, bool exclude_user,
145 				  bool exclude_kernel, bool intr)
146 {
147 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
148 	struct perf_event *event;
149 	struct perf_event_attr attr = {
150 		.type = type,
151 		.size = sizeof(attr),
152 		.pinned = true,
153 		.exclude_idle = true,
154 		.exclude_host = 1,
155 		.exclude_user = exclude_user,
156 		.exclude_kernel = exclude_kernel,
157 		.config = config,
158 	};
159 	bool pebs = test_bit(pmc->idx, (unsigned long *)&pmu->pebs_enable);
160 
161 	attr.sample_period = get_sample_period(pmc, pmc->counter);
162 
163 	if ((attr.config & HSW_IN_TX_CHECKPOINTED) &&
164 	    guest_cpuid_is_intel(pmc->vcpu)) {
165 		/*
166 		 * HSW_IN_TX_CHECKPOINTED is not supported with nonzero
167 		 * period. Just clear the sample period so at least
168 		 * allocating the counter doesn't fail.
169 		 */
170 		attr.sample_period = 0;
171 	}
172 	if (pebs) {
173 		/*
174 		 * The non-zero precision level of guest event makes the ordinary
175 		 * guest event becomes a guest PEBS event and triggers the host
176 		 * PEBS PMI handler to determine whether the PEBS overflow PMI
177 		 * comes from the host counters or the guest.
178 		 *
179 		 * For most PEBS hardware events, the difference in the software
180 		 * precision levels of guest and host PEBS events will not affect
181 		 * the accuracy of the PEBS profiling result, because the "event IP"
182 		 * in the PEBS record is calibrated on the guest side.
183 		 *
184 		 * On Icelake everything is fine. Other hardware (GLC+, TNT+) that
185 		 * could possibly care here is unsupported and needs changes.
186 		 */
187 		attr.precise_ip = 1;
188 		if (x86_match_cpu(vmx_icl_pebs_cpu) && pmc->idx == 32)
189 			attr.precise_ip = 3;
190 	}
191 
192 	event = perf_event_create_kernel_counter(&attr, -1, current,
193 						 kvm_perf_overflow, pmc);
194 	if (IS_ERR(event)) {
195 		pr_debug_ratelimited("kvm_pmu: event creation failed %ld for pmc->idx = %d\n",
196 			    PTR_ERR(event), pmc->idx);
197 		return;
198 	}
199 
200 	pmc->perf_event = event;
201 	pmc_to_pmu(pmc)->event_count++;
202 	clear_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi);
203 	pmc->is_paused = false;
204 	pmc->intr = intr || pebs;
205 }
206 
207 static void pmc_pause_counter(struct kvm_pmc *pmc)
208 {
209 	u64 counter = pmc->counter;
210 
211 	if (!pmc->perf_event || pmc->is_paused)
212 		return;
213 
214 	/* update counter, reset event value to avoid redundant accumulation */
215 	counter += perf_event_pause(pmc->perf_event, true);
216 	pmc->counter = counter & pmc_bitmask(pmc);
217 	pmc->is_paused = true;
218 }
219 
220 static bool pmc_resume_counter(struct kvm_pmc *pmc)
221 {
222 	if (!pmc->perf_event)
223 		return false;
224 
225 	/* recalibrate sample period and check if it's accepted by perf core */
226 	if (perf_event_period(pmc->perf_event,
227 			      get_sample_period(pmc, pmc->counter)))
228 		return false;
229 
230 	if (!test_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->pebs_enable) &&
231 	    pmc->perf_event->attr.precise_ip)
232 		return false;
233 
234 	/* reuse perf_event to serve as pmc_reprogram_counter() does*/
235 	perf_event_enable(pmc->perf_event);
236 	pmc->is_paused = false;
237 
238 	clear_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->reprogram_pmi);
239 	return true;
240 }
241 
242 static int cmp_u64(const void *pa, const void *pb)
243 {
244 	u64 a = *(u64 *)pa;
245 	u64 b = *(u64 *)pb;
246 
247 	return (a > b) - (a < b);
248 }
249 
250 static bool check_pmu_event_filter(struct kvm_pmc *pmc)
251 {
252 	struct kvm_pmu_event_filter *filter;
253 	struct kvm *kvm = pmc->vcpu->kvm;
254 	bool allow_event = true;
255 	__u64 key;
256 	int idx;
257 
258 	if (!static_call(kvm_x86_pmu_hw_event_available)(pmc))
259 		return false;
260 
261 	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
262 	if (!filter)
263 		goto out;
264 
265 	if (pmc_is_gp(pmc)) {
266 		key = pmc->eventsel & AMD64_RAW_EVENT_MASK_NB;
267 		if (bsearch(&key, filter->events, filter->nevents,
268 			    sizeof(__u64), cmp_u64))
269 			allow_event = filter->action == KVM_PMU_EVENT_ALLOW;
270 		else
271 			allow_event = filter->action == KVM_PMU_EVENT_DENY;
272 	} else {
273 		idx = pmc->idx - INTEL_PMC_IDX_FIXED;
274 		if (filter->action == KVM_PMU_EVENT_DENY &&
275 		    test_bit(idx, (ulong *)&filter->fixed_counter_bitmap))
276 			allow_event = false;
277 		if (filter->action == KVM_PMU_EVENT_ALLOW &&
278 		    !test_bit(idx, (ulong *)&filter->fixed_counter_bitmap))
279 			allow_event = false;
280 	}
281 
282 out:
283 	return allow_event;
284 }
285 
286 void reprogram_counter(struct kvm_pmc *pmc)
287 {
288 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
289 	u64 eventsel = pmc->eventsel;
290 	u64 new_config = eventsel;
291 	u8 fixed_ctr_ctrl;
292 
293 	pmc_pause_counter(pmc);
294 
295 	if (!pmc_speculative_in_use(pmc) || !pmc_is_enabled(pmc))
296 		return;
297 
298 	if (!check_pmu_event_filter(pmc))
299 		return;
300 
301 	if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
302 		printk_once("kvm pmu: pin control bit is ignored\n");
303 
304 	if (pmc_is_fixed(pmc)) {
305 		fixed_ctr_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl,
306 						  pmc->idx - INTEL_PMC_IDX_FIXED);
307 		if (fixed_ctr_ctrl & 0x1)
308 			eventsel |= ARCH_PERFMON_EVENTSEL_OS;
309 		if (fixed_ctr_ctrl & 0x2)
310 			eventsel |= ARCH_PERFMON_EVENTSEL_USR;
311 		if (fixed_ctr_ctrl & 0x8)
312 			eventsel |= ARCH_PERFMON_EVENTSEL_INT;
313 		new_config = (u64)fixed_ctr_ctrl;
314 	}
315 
316 	if (pmc->current_config == new_config && pmc_resume_counter(pmc))
317 		return;
318 
319 	pmc_release_perf_event(pmc);
320 
321 	pmc->current_config = new_config;
322 	pmc_reprogram_counter(pmc, PERF_TYPE_RAW,
323 			      (eventsel & pmu->raw_event_mask),
324 			      !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
325 			      !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
326 			      eventsel & ARCH_PERFMON_EVENTSEL_INT);
327 }
328 EXPORT_SYMBOL_GPL(reprogram_counter);
329 
330 void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
331 {
332 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
333 	int bit;
334 
335 	for_each_set_bit(bit, pmu->reprogram_pmi, X86_PMC_IDX_MAX) {
336 		struct kvm_pmc *pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, bit);
337 
338 		if (unlikely(!pmc || !pmc->perf_event)) {
339 			clear_bit(bit, pmu->reprogram_pmi);
340 			continue;
341 		}
342 		reprogram_counter(pmc);
343 	}
344 
345 	/*
346 	 * Unused perf_events are only released if the corresponding MSRs
347 	 * weren't accessed during the last vCPU time slice. kvm_arch_sched_in
348 	 * triggers KVM_REQ_PMU if cleanup is needed.
349 	 */
350 	if (unlikely(pmu->need_cleanup))
351 		kvm_pmu_cleanup(vcpu);
352 }
353 
354 /* check if idx is a valid index to access PMU */
355 bool kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
356 {
357 	return static_call(kvm_x86_pmu_is_valid_rdpmc_ecx)(vcpu, idx);
358 }
359 
360 bool is_vmware_backdoor_pmc(u32 pmc_idx)
361 {
362 	switch (pmc_idx) {
363 	case VMWARE_BACKDOOR_PMC_HOST_TSC:
364 	case VMWARE_BACKDOOR_PMC_REAL_TIME:
365 	case VMWARE_BACKDOOR_PMC_APPARENT_TIME:
366 		return true;
367 	}
368 	return false;
369 }
370 
371 static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
372 {
373 	u64 ctr_val;
374 
375 	switch (idx) {
376 	case VMWARE_BACKDOOR_PMC_HOST_TSC:
377 		ctr_val = rdtsc();
378 		break;
379 	case VMWARE_BACKDOOR_PMC_REAL_TIME:
380 		ctr_val = ktime_get_boottime_ns();
381 		break;
382 	case VMWARE_BACKDOOR_PMC_APPARENT_TIME:
383 		ctr_val = ktime_get_boottime_ns() +
384 			vcpu->kvm->arch.kvmclock_offset;
385 		break;
386 	default:
387 		return 1;
388 	}
389 
390 	*data = ctr_val;
391 	return 0;
392 }
393 
394 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
395 {
396 	bool fast_mode = idx & (1u << 31);
397 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
398 	struct kvm_pmc *pmc;
399 	u64 mask = fast_mode ? ~0u : ~0ull;
400 
401 	if (!pmu->version)
402 		return 1;
403 
404 	if (is_vmware_backdoor_pmc(idx))
405 		return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
406 
407 	pmc = static_call(kvm_x86_pmu_rdpmc_ecx_to_pmc)(vcpu, idx, &mask);
408 	if (!pmc)
409 		return 1;
410 
411 	if (!(kvm_read_cr4(vcpu) & X86_CR4_PCE) &&
412 	    (static_call(kvm_x86_get_cpl)(vcpu) != 0) &&
413 	    (kvm_read_cr0(vcpu) & X86_CR0_PE))
414 		return 1;
415 
416 	*data = pmc_read_counter(pmc) & mask;
417 	return 0;
418 }
419 
420 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
421 {
422 	if (lapic_in_kernel(vcpu)) {
423 		static_call_cond(kvm_x86_pmu_deliver_pmi)(vcpu);
424 		kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
425 	}
426 }
427 
428 bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
429 {
430 	return static_call(kvm_x86_pmu_msr_idx_to_pmc)(vcpu, msr) ||
431 		static_call(kvm_x86_pmu_is_valid_msr)(vcpu, msr);
432 }
433 
434 static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)
435 {
436 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
437 	struct kvm_pmc *pmc = static_call(kvm_x86_pmu_msr_idx_to_pmc)(vcpu, msr);
438 
439 	if (pmc)
440 		__set_bit(pmc->idx, pmu->pmc_in_use);
441 }
442 
443 int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
444 {
445 	return static_call(kvm_x86_pmu_get_msr)(vcpu, msr_info);
446 }
447 
448 int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
449 {
450 	kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
451 	return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
452 }
453 
454 /* refresh PMU settings. This function generally is called when underlying
455  * settings are changed (such as changes of PMU CPUID by guest VMs), which
456  * should rarely happen.
457  */
458 void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
459 {
460 	static_call(kvm_x86_pmu_refresh)(vcpu);
461 }
462 
463 void kvm_pmu_reset(struct kvm_vcpu *vcpu)
464 {
465 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
466 
467 	irq_work_sync(&pmu->irq_work);
468 	static_call(kvm_x86_pmu_reset)(vcpu);
469 }
470 
471 void kvm_pmu_init(struct kvm_vcpu *vcpu)
472 {
473 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
474 
475 	memset(pmu, 0, sizeof(*pmu));
476 	static_call(kvm_x86_pmu_init)(vcpu);
477 	init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn);
478 	pmu->event_count = 0;
479 	pmu->need_cleanup = false;
480 	kvm_pmu_refresh(vcpu);
481 }
482 
483 /* Release perf_events for vPMCs that have been unused for a full time slice.  */
484 void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
485 {
486 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
487 	struct kvm_pmc *pmc = NULL;
488 	DECLARE_BITMAP(bitmask, X86_PMC_IDX_MAX);
489 	int i;
490 
491 	pmu->need_cleanup = false;
492 
493 	bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
494 		      pmu->pmc_in_use, X86_PMC_IDX_MAX);
495 
496 	for_each_set_bit(i, bitmask, X86_PMC_IDX_MAX) {
497 		pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i);
498 
499 		if (pmc && pmc->perf_event && !pmc_speculative_in_use(pmc))
500 			pmc_stop_counter(pmc);
501 	}
502 
503 	static_call_cond(kvm_x86_pmu_cleanup)(vcpu);
504 
505 	bitmap_zero(pmu->pmc_in_use, X86_PMC_IDX_MAX);
506 }
507 
508 void kvm_pmu_destroy(struct kvm_vcpu *vcpu)
509 {
510 	kvm_pmu_reset(vcpu);
511 }
512 
513 static void kvm_pmu_incr_counter(struct kvm_pmc *pmc)
514 {
515 	u64 prev_count;
516 
517 	prev_count = pmc->counter;
518 	pmc->counter = (pmc->counter + 1) & pmc_bitmask(pmc);
519 
520 	reprogram_counter(pmc);
521 	if (pmc->counter < prev_count)
522 		__kvm_perf_overflow(pmc, false);
523 }
524 
525 static inline bool eventsel_match_perf_hw_id(struct kvm_pmc *pmc,
526 	unsigned int perf_hw_id)
527 {
528 	return !((pmc->eventsel ^ perf_get_hw_event_config(perf_hw_id)) &
529 		AMD64_RAW_EVENT_MASK_NB);
530 }
531 
532 static inline bool cpl_is_matched(struct kvm_pmc *pmc)
533 {
534 	bool select_os, select_user;
535 	u64 config = pmc->current_config;
536 
537 	if (pmc_is_gp(pmc)) {
538 		select_os = config & ARCH_PERFMON_EVENTSEL_OS;
539 		select_user = config & ARCH_PERFMON_EVENTSEL_USR;
540 	} else {
541 		select_os = config & 0x1;
542 		select_user = config & 0x2;
543 	}
544 
545 	return (static_call(kvm_x86_get_cpl)(pmc->vcpu) == 0) ? select_os : select_user;
546 }
547 
548 void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id)
549 {
550 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
551 	struct kvm_pmc *pmc;
552 	int i;
553 
554 	for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) {
555 		pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i);
556 
557 		if (!pmc || !pmc_is_enabled(pmc) || !pmc_speculative_in_use(pmc))
558 			continue;
559 
560 		/* Ignore checks for edge detect, pin control, invert and CMASK bits */
561 		if (eventsel_match_perf_hw_id(pmc, perf_hw_id) && cpl_is_matched(pmc))
562 			kvm_pmu_incr_counter(pmc);
563 	}
564 }
565 EXPORT_SYMBOL_GPL(kvm_pmu_trigger_event);
566 
567 int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
568 {
569 	struct kvm_pmu_event_filter tmp, *filter;
570 	size_t size;
571 	int r;
572 
573 	if (copy_from_user(&tmp, argp, sizeof(tmp)))
574 		return -EFAULT;
575 
576 	if (tmp.action != KVM_PMU_EVENT_ALLOW &&
577 	    tmp.action != KVM_PMU_EVENT_DENY)
578 		return -EINVAL;
579 
580 	if (tmp.flags != 0)
581 		return -EINVAL;
582 
583 	if (tmp.nevents > KVM_PMU_EVENT_FILTER_MAX_EVENTS)
584 		return -E2BIG;
585 
586 	size = struct_size(filter, events, tmp.nevents);
587 	filter = kmalloc(size, GFP_KERNEL_ACCOUNT);
588 	if (!filter)
589 		return -ENOMEM;
590 
591 	r = -EFAULT;
592 	if (copy_from_user(filter, argp, size))
593 		goto cleanup;
594 
595 	/* Ensure nevents can't be changed between the user copies. */
596 	*filter = tmp;
597 
598 	/*
599 	 * Sort the in-kernel list so that we can search it with bsearch.
600 	 */
601 	sort(&filter->events, filter->nevents, sizeof(__u64), cmp_u64, NULL);
602 
603 	mutex_lock(&kvm->lock);
604 	filter = rcu_replace_pointer(kvm->arch.pmu_event_filter, filter,
605 				     mutex_is_locked(&kvm->lock));
606 	mutex_unlock(&kvm->lock);
607 
608 	synchronize_srcu_expedited(&kvm->srcu);
609 	r = 0;
610 cleanup:
611 	kfree(filter);
612 	return r;
613 }
614