Skip to content

Commit b1a8657

Browse files
suomilewissean-jc
authored andcommitted
selftests: kvm/x86: Add testing for KVM_SET_PMU_EVENT_FILTER
Test that masked events are not using invalid bits, and if they are, ensure the pmu event filter is not accepted by KVM_SET_PMU_EVENT_FILTER. The only valid bits that can be used for masked events are set when using KVM_PMU_ENCODE_MASKED_ENTRY() with one exception: If any of the high bits (35:32) of the event select are set when using Intel, the pmu event filter will fail. Also, because validation was not being done prior to the introduction of masked events, only expect validation to fail when masked events are used. E.g. in the first test a filter event with all its bits set is accepted by KVM_SET_PMU_EVENT_FILTER when flags = 0. Signed-off-by: Aaron Lewis <aaronlewis@google.com> Link: https://lore.kernel.org/r/20221220161236.555143-7-aaronlewis@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 6a6b17a commit b1a8657

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,47 @@ static bool use_amd_pmu(void)
404404
is_zen3(family, model));
405405
}
406406

407+
static int run_filter_test(struct kvm_vcpu *vcpu, const uint64_t *events,
408+
int nevents, uint32_t flags)
409+
{
410+
struct kvm_pmu_event_filter *f;
411+
int r;
412+
413+
f = create_pmu_event_filter(events, nevents, KVM_PMU_EVENT_ALLOW, flags);
414+
r = __vm_ioctl(vcpu->vm, KVM_SET_PMU_EVENT_FILTER, f);
415+
free(f);
416+
417+
return r;
418+
}
419+
420+
static void test_filter_ioctl(struct kvm_vcpu *vcpu)
421+
{
422+
uint64_t e = ~0ul;
423+
int r;
424+
425+
/*
426+
* Unfortunately having invalid bits set in event data is expected to
427+
* pass when flags == 0 (bits other than eventsel+umask).
428+
*/
429+
r = run_filter_test(vcpu, &e, 1, 0);
430+
TEST_ASSERT(r == 0, "Valid PMU Event Filter is failing");
431+
432+
r = run_filter_test(vcpu, &e, 1, KVM_PMU_EVENT_FLAG_MASKED_EVENTS);
433+
TEST_ASSERT(r != 0, "Invalid PMU Event Filter is expected to fail");
434+
435+
e = KVM_PMU_EVENT_ENCODE_MASKED_ENTRY(0xff, 0xff, 0xff, 0xf);
436+
r = run_filter_test(vcpu, &e, 1, KVM_PMU_EVENT_FLAG_MASKED_EVENTS);
437+
TEST_ASSERT(r == 0, "Valid PMU Event Filter is failing");
438+
}
439+
407440
int main(int argc, char *argv[])
408441
{
409442
void (*guest_code)(void);
410443
struct kvm_vcpu *vcpu;
411444
struct kvm_vm *vm;
412445

413446
TEST_REQUIRE(kvm_has_cap(KVM_CAP_PMU_EVENT_FILTER));
447+
TEST_REQUIRE(kvm_has_cap(KVM_CAP_PMU_EVENT_MASKED_EVENTS));
414448

415449
TEST_REQUIRE(use_intel_pmu() || use_amd_pmu());
416450
guest_code = use_intel_pmu() ? intel_guest_code : amd_guest_code;
@@ -431,6 +465,8 @@ int main(int argc, char *argv[])
431465
test_not_member_deny_list(vcpu);
432466
test_not_member_allow_list(vcpu);
433467

468+
test_filter_ioctl(vcpu);
469+
434470
kvm_vm_free(vm);
435471

436472
test_pmu_config_disable(guest_code);

0 commit comments

Comments
 (0)