From: Dapeng Mi <dapeng1...@linux.intel.com>

Mediated vPMU needs to call KVM_CAP_PMU_CAPABILITY ioctl to enable it.
Thus add a helper vm_create_with_one_vcpu_with_pmu() to create PMU
enabled VM and replace vm_create_with_one_vcpu() helper with this new
helper in pmu tests.

Signed-off-by: Dapeng Mi <dapeng1...@linux.intel.com>
Signed-off-by: Mingwei Zhang <mizh...@google.com>
---
 .../testing/selftests/kvm/include/kvm_util.h  |  3 +++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 23 +++++++++++++++++++
 .../selftests/kvm/x86/pmu_counters_test.c     |  4 +++-
 .../selftests/kvm/x86/pmu_event_filter_test.c |  8 ++++---
 4 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h 
b/tools/testing/selftests/kvm/include/kvm_util.h
index 4c4e5a847f67..a73b0b98be5e 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -961,6 +961,9 @@ static inline struct kvm_vm 
*vm_create_shape_with_one_vcpu(struct vm_shape shape
        return __vm_create_shape_with_one_vcpu(shape, vcpu, 0, guest_code);
 }
 
+struct kvm_vm *vm_create_with_one_vcpu_with_pmu(struct kvm_vcpu **vcpu,
+                                               void *guest_code);
+
 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm);
 
 void kvm_pin_this_task_to_pcpu(uint32_t pcpu);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c 
b/tools/testing/selftests/kvm/lib/kvm_util.c
index 33fefeb3ca44..18143ec2e751 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -545,6 +545,29 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm 
*vm)
        return vm_vcpu_recreate(vm, 0);
 }
 
+struct kvm_vm *vm_create_with_one_vcpu_with_pmu(struct kvm_vcpu **vcpu,
+                                               void *guest_code)
+{
+       struct kvm_vm *vm;
+       int r;
+
+       r = kvm_check_cap(KVM_CAP_PMU_CAPABILITY);
+       if (!(r & KVM_PMU_CAP_DISABLE))
+               return NULL;
+
+       vm = vm_create(1);
+
+       /*
+        * KVM_CAP_PMU_CAPABILITY ioctl must be explicitly called to enable
+        * mediated vPMU.
+        */
+       vm_enable_cap(vm, KVM_CAP_PMU_CAPABILITY, !KVM_PMU_CAP_DISABLE);
+
+       *vcpu = vm_vcpu_add(vm, 0, guest_code);
+
+       return vm;
+}
+
 void kvm_pin_this_task_to_pcpu(uint32_t pcpu)
 {
        cpu_set_t mask;
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c 
b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index 698cb36989db..441c66f314fb 100644
--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
@@ -40,7 +40,9 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct 
kvm_vcpu **vcpu,
 {
        struct kvm_vm *vm;
 
-       vm = vm_create_with_one_vcpu(vcpu, guest_code);
+       vm = vm_create_with_one_vcpu_with_pmu(vcpu, guest_code);
+       assert(vm);
+
        sync_global_to_guest(vm, kvm_pmu_version);
 
        /*
diff --git a/tools/testing/selftests/kvm/x86/pmu_event_filter_test.c 
b/tools/testing/selftests/kvm/x86/pmu_event_filter_test.c
index c15513cd74d1..1c7d265a0003 100644
--- a/tools/testing/selftests/kvm/x86/pmu_event_filter_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_event_filter_test.c
@@ -822,8 +822,9 @@ static void test_fixed_counter_bitmap(void)
         * fixed performance counters.
         */
        for (idx = 0; idx < nr_fixed_counters; idx++) {
-               vm = vm_create_with_one_vcpu(&vcpu,
-                                            
intel_run_fixed_counter_guest_code);
+               vm = vm_create_with_one_vcpu_with_pmu(&vcpu,
+                                                     
intel_run_fixed_counter_guest_code);
+               assert(vm);
                vcpu_args_set(vcpu, 1, idx);
                __test_fixed_counter_bitmap(vcpu, idx, nr_fixed_counters);
                kvm_vm_free(vm);
@@ -843,7 +844,8 @@ int main(int argc, char *argv[])
        TEST_REQUIRE(use_intel_pmu() || use_amd_pmu());
        guest_code = use_intel_pmu() ? intel_guest_code : amd_guest_code;
 
-       vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+       vm = vm_create_with_one_vcpu_with_pmu(&vcpu, guest_code);
+       assert(vm);
 
        TEST_REQUIRE(sanity_check_pmu(vcpu));
 
-- 
2.49.0.395.g12beb8f557-goog


Reply via email to