Convert vm_get_stat() to macro to detect typos at compile time.
Add a concatenation trickery to trigger compiler error if vm stat doesn't exist, so that it is not possible to pass a vcpu stat into vm_get_stat().
Suggested-by: Sean Christopherson seanjc@google.com Signed-off-by: Manali Shukla manali.shukla@amd.com --- .../testing/selftests/kvm/include/kvm_util.h | 33 +++++++++++++++---- .../kvm/include/x86_64/kvm_util_arch.h | 16 +++++++++ 2 files changed, 42 insertions(+), 7 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 5dd3acf174f8..bd486a2899ca 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -539,16 +539,35 @@ static inline int check_##type##_##stat##_exists(void) \
#define STAT_EXISTS(type, stat) (check_##type##_##stat##_exists())
+#define DEFINE_GENERIC_VM_STAT \ + DEFINE_CHECK_STAT(vm, remote_tlb_flush) \ + DEFINE_CHECK_STAT(vm, remote_tlb_flush_requests) \ + +/* + * Define a default empty macro for architectures which do not specify + * arch specific vm stats. + */ +#ifndef DEFINE_ARCH_VM_STAT +#define DEFINE_ARCH_VM_STAT +#endif + +DEFINE_GENERIC_VM_STAT +DEFINE_ARCH_VM_STAT + +#undef DEFINE_GENERIC_VM_STAT +#undef DEFINE_ARCH_VM_STAT + void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, size_t max_elements);
-static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) -{ - uint64_t data; - - __vm_get_stat(vm, stat_name, &data, 1); - return data; -} +#define vm_get_stat(vm, stat_name) \ +({ \ + uint64_t data; \ + \ + STAT_EXISTS(vm, stat_name); \ + __vm_get_stat(vm, #stat_name, &data, 1); \ + data; \ +})
#define DEFINE_GENERIC_VCPU_STAT \ DEFINE_CHECK_STAT(vcpu, halt_successfull_poll) \ diff --git a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h index 3cdc3c856ed2..6341c786dc9a 100644 --- a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h @@ -48,6 +48,22 @@ do { \ } \ } while (0)
+#define DEFINE_ARCH_VM_STAT \ + DEFINE_CHECK_STAT(vm, mmu_shadow_zapped) \ + DEFINE_CHECK_STAT(vm, mmu_pte_write) \ + DEFINE_CHECK_STAT(vm, mmu_pde_zapped) \ + DEFINE_CHECK_STAT(vm, mmu_flooded) \ + DEFINE_CHECK_STAT(vm, mmu_recycled) \ + DEFINE_CHECK_STAT(vm, mmu_cache_miss) \ + DEFINE_CHECK_STAT(vm, mmu_unsync) \ + DEFINE_CHECK_STAT(vm, pages_4k) \ + DEFINE_CHECK_STAT(vm, pages_2m) \ + DEFINE_CHECK_STAT(vm, pages_1g) \ + DEFINE_CHECK_STAT(vm, pages) \ + DEFINE_CHECK_STAT(vm, nx_lpage_splits) \ + DEFINE_CHECK_STAT(vm, max_mmu_page_hash_collisions) \ + DEFINE_CHECK_STAT(vm, max_mmu_rmap_size) \ + #define DEFINE_ARCH_VCPU_STAT \ DEFINE_CHECK_STAT(vcpu, pf_taken) \ DEFINE_CHECK_STAT(vcpu, pf_fixed) \