[PATCH 13/16] KVM: x86/tdp_mmu: Introduce shared, private KVM MMU root types

Rick Edgecombe
2024-05-15T01:00:12Z

From: Isaku Yamahata <isaku.yamahata@intel.com>

Add more types, shared and private to enum kvm_tdp_mmu_root_types to
specify KVM MMU roots [1] so that the iterator on the root page table can
consistently filter the root page table type.

TDX KVM will operate on KVM page tables with specified types. Shared page
table, private page table, or both. Introduce an enum to specify those
page table types and make the iterator take it with the specified root
type. Valid or not, and shared, private, or both. Enhance
tdp_mmu_root_match() to understand private vs shared.

Suggested-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/kvm/ZivazWQw1oCU8VBC@google.com/ [1]
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
TDX MMU Part 1:
- New patch
---
arch/x86/kvm/mmu/tdp_mmu.c | 12 +++++++++++-
arch/x86/kvm/mmu/tdp_mmu.h | 14 ++++++++++----
2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 7af395073e92..8914c5b0d5ab 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -95,10 +95,20 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root)
static bool tdp_mmu_root_match(struct kvm_mmu_page *root,
enum kvm_tdp_mmu_root_types types)
{
+ if (WARN_ON_ONCE(types == BUGGY_KVM_ROOTS))
+ return false;
+ if (WARN_ON_ONCE(!(types & (KVM_SHARED_ROOTS | KVM_PRIVATE_ROOTS))))
+ return false;
+
if ((types & KVM_VALID_ROOTS) && root->role.invalid)
return false;

- return true;
+ if ((types & KVM_SHARED_ROOTS) && !is_private_sp(root))
+ return true;
+ if ((types & KVM_PRIVATE_ROOTS) && is_private_sp(root))
+ return true;
+
+ return false;
}

/*
diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h
index 30f2ab88a642..6a65498b481c 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.h
+++ b/arch/x86/kvm/mmu/tdp_mmu.h
@@ -20,12 +20,18 @@ __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)
void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root);

enum kvm_tdp_mmu_root_types {
- KVM_VALID_ROOTS = BIT(0),
-
- KVM_ANY_ROOTS = 0,
- KVM_ANY_VALID_ROOTS = KVM_VALID_ROOTS,
+ BUGGY_KVM_ROOTS = BUGGY_KVM_INVALIDATION,
+ KVM_SHARED_ROOTS = KVM_PROCESS_SHARED,
+ KVM_PRIVATE_ROOTS = KVM_PROCESS_PRIVATE,
+ KVM_VALID_ROOTS = BIT(2),
+ KVM_ANY_VALID_ROOTS = KVM_SHARED_ROOTS | KVM_PRIVATE_ROOTS | KVM_VALID_ROOTS,
+ KVM_ANY_ROOTS = KVM_SHARED_ROOTS | KVM_PRIVATE_ROOTS,
};

+static_assert(!(KVM_SHARED_ROOTS & KVM_VALID_ROOTS));
+static_assert(!(KVM_PRIVATE_ROOTS & KVM_VALID_ROOTS));
+static_assert(KVM_PRIVATE_ROOTS == (KVM_SHARED_ROOTS << 1));
+
bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, gfn_t start, gfn_t end, bool flush);
bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
void kvm_tdp_mmu_zap_all(struct kvm *kvm);
--
2.34.1