[PATCH v4 10/11] KVM: x86/mmu: Use static key/branches for checking if TDP MMU is enabled

From: Sean Christopherson
Date: Wed Oct 12 2022 - 14:18:43 EST


Now that the TDP MMU being enabled is read-only after the vendor module
is loaded, use a static key to track whether or not the TDP MMU is
enabled to avoid conditional branches in hot paths, e.g. in
direct_page_fault() and fast_page_fault().

Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/mmu.h | 5 +++--
arch/x86/kvm/mmu/mmu.c | 14 ++++++++++----
2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index 1ad6d02e103f..bc0d8a5c09f9 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -2,6 +2,7 @@
#ifndef __KVM_X86_MMU_H
#define __KVM_X86_MMU_H

+#include <linux/jump_label.h>
#include <linux/kvm_host.h>
#include "kvm_cache_regs.h"
#include "cpuid.h"
@@ -230,13 +231,13 @@ static inline bool kvm_shadow_root_allocated(struct kvm *kvm)
}

#ifdef CONFIG_X86_64
-extern bool tdp_mmu_enabled;
+DECLARE_STATIC_KEY_TRUE(tdp_mmu_enabled);
#endif

static inline bool is_tdp_mmu_enabled(void)
{
#ifdef CONFIG_X86_64
- return tdp_mmu_enabled;
+ return static_branch_likely(&tdp_mmu_enabled);
#else
return false;
#endif
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 4792d76edd6d..a5ba7b41263d 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -101,8 +101,10 @@ bool tdp_enabled = false;
#ifdef CONFIG_X86_64
static bool __ro_after_init tdp_mmu_allowed;

-bool __read_mostly tdp_mmu_enabled = true;
-module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0444);
+static bool __read_mostly __tdp_mmu_enabled = true;
+module_param_named(tdp_mmu, __tdp_mmu_enabled, bool, 0444);
+
+DEFINE_STATIC_KEY_TRUE(tdp_mmu_enabled);
#endif

static int max_huge_page_level __read_mostly;
@@ -5702,7 +5704,11 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
max_tdp_level = tdp_max_root_level;

#ifdef CONFIG_X86_64
- tdp_mmu_enabled = tdp_mmu_allowed && tdp_enabled;
+ __tdp_mmu_enabled = tdp_mmu_allowed && tdp_enabled;
+ if (__tdp_mmu_enabled)
+ static_branch_enable(&tdp_mmu_enabled);
+ else
+ static_branch_disable(&tdp_mmu_enabled);
#endif
/*
* max_huge_page_level reflects KVM's MMU capabilities irrespective
@@ -6712,7 +6718,7 @@ void __init kvm_mmu_x86_module_init(void)
* TDP MMU is actually enabled is determined in kvm_configure_mmu()
* when the vendor module is loaded.
*/
- tdp_mmu_allowed = tdp_mmu_enabled;
+ tdp_mmu_allowed = __tdp_mmu_enabled;
#endif

kvm_mmu_spte_module_init();
--
2.38.0.rc1.362.ged0d419d3c-goog