Skip to content

Commit c5f2d56

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Add helper to convert root hpa to shadow page
Add a dedicated helper for converting a root hpa to a shadow page in anticipation of using a "dummy" root to handle the scenario where KVM needs to load a valid shadow root (from hardware's perspective), but the guest doesn't have a visible root to shadow. Similar to PAE roots, the dummy root won't have an associated kvm_mmu_page and will need special handling when finding a shadow page given a root. Opportunistically retrieve the root shadow page in kvm_mmu_sync_roots() *after* verifying the root is unsync (the dummy root can never be unsync). Link: https://lore.kernel.org/r/20230729005200.1057358-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 09c8726 commit c5f2d56

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,11 +3543,7 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
35433543
if (!VALID_PAGE(*root_hpa))
35443544
return;
35453545

3546-
/*
3547-
* The "root" may be a special root, e.g. a PAE entry, treat it as a
3548-
* SPTE to ensure any non-PA bits are dropped.
3549-
*/
3550-
sp = spte_to_child_sp(*root_hpa);
3546+
sp = root_to_sp(*root_hpa);
35513547
if (WARN_ON_ONCE(!sp))
35523548
return;
35533549

@@ -3593,7 +3589,7 @@ void kvm_mmu_free_roots(struct kvm *kvm, struct kvm_mmu *mmu,
35933589
&invalid_list);
35943590

35953591
if (free_active_root) {
3596-
if (to_shadow_page(mmu->root.hpa)) {
3592+
if (root_to_sp(mmu->root.hpa)) {
35973593
mmu_free_root_page(kvm, &mmu->root.hpa, &invalid_list);
35983594
} else if (mmu->pae_root) {
35993595
for (i = 0; i < 4; ++i) {
@@ -3617,6 +3613,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
36173613
void kvm_mmu_free_guest_mode_roots(struct kvm *kvm, struct kvm_mmu *mmu)
36183614
{
36193615
unsigned long roots_to_free = 0;
3616+
struct kvm_mmu_page *sp;
36203617
hpa_t root_hpa;
36213618
int i;
36223619

@@ -3631,8 +3628,8 @@ void kvm_mmu_free_guest_mode_roots(struct kvm *kvm, struct kvm_mmu *mmu)
36313628
if (!VALID_PAGE(root_hpa))
36323629
continue;
36333630

3634-
if (!to_shadow_page(root_hpa) ||
3635-
to_shadow_page(root_hpa)->role.guest_mode)
3631+
sp = root_to_sp(root_hpa);
3632+
if (!sp || sp->role.guest_mode)
36363633
roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
36373634
}
36383635

@@ -3987,7 +3984,7 @@ static bool is_unsync_root(hpa_t root)
39873984
* requirement isn't satisfied.
39883985
*/
39893986
smp_rmb();
3990-
sp = to_shadow_page(root);
3987+
sp = root_to_sp(root);
39913988

39923989
/*
39933990
* PAE roots (somewhat arbitrarily) aren't backed by shadow pages, the
@@ -4017,11 +4014,12 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
40174014

40184015
if (vcpu->arch.mmu->cpu_role.base.level >= PT64_ROOT_4LEVEL) {
40194016
hpa_t root = vcpu->arch.mmu->root.hpa;
4020-
sp = to_shadow_page(root);
40214017

40224018
if (!is_unsync_root(root))
40234019
return;
40244020

4021+
sp = root_to_sp(root);
4022+
40254023
write_lock(&vcpu->kvm->mmu_lock);
40264024
mmu_sync_children(vcpu, sp, true);
40274025
write_unlock(&vcpu->kvm->mmu_lock);
@@ -4351,7 +4349,7 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,
43514349
static bool is_page_fault_stale(struct kvm_vcpu *vcpu,
43524350
struct kvm_page_fault *fault)
43534351
{
4354-
struct kvm_mmu_page *sp = to_shadow_page(vcpu->arch.mmu->root.hpa);
4352+
struct kvm_mmu_page *sp = root_to_sp(vcpu->arch.mmu->root.hpa);
43554353

43564354
/* Special roots, e.g. pae_root, are not backed by shadow pages. */
43574355
if (sp && is_obsolete_sp(vcpu->kvm, sp))
@@ -4531,7 +4529,7 @@ static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t pgd,
45314529
{
45324530
return (role.direct || pgd == root->pgd) &&
45334531
VALID_PAGE(root->hpa) &&
4534-
role.word == to_shadow_page(root->hpa)->role.word;
4532+
role.word == root_to_sp(root->hpa)->role.word;
45354533
}
45364534

45374535
/*
@@ -4605,7 +4603,7 @@ static bool fast_pgd_switch(struct kvm *kvm, struct kvm_mmu *mmu,
46054603
* having to deal with PDPTEs. We may add support for 32-bit hosts/VMs
46064604
* later if necessary.
46074605
*/
4608-
if (VALID_PAGE(mmu->root.hpa) && !to_shadow_page(mmu->root.hpa))
4606+
if (VALID_PAGE(mmu->root.hpa) && !root_to_sp(mmu->root.hpa))
46094607
kvm_mmu_free_roots(kvm, mmu, KVM_MMU_ROOT_CURRENT);
46104608

46114609
if (VALID_PAGE(mmu->root.hpa))
@@ -4653,7 +4651,7 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd)
46534651
*/
46544652
if (!new_role.direct)
46554653
__clear_sp_write_flooding_count(
4656-
to_shadow_page(vcpu->arch.mmu->root.hpa));
4654+
root_to_sp(vcpu->arch.mmu->root.hpa));
46574655
}
46584656
EXPORT_SYMBOL_GPL(kvm_mmu_new_pgd);
46594657

@@ -5508,7 +5506,7 @@ static bool is_obsolete_root(struct kvm *kvm, hpa_t root_hpa)
55085506
* (c) KVM doesn't track previous roots for PAE paging, and the guest
55095507
* is unlikely to zap an in-use PGD.
55105508
*/
5511-
sp = to_shadow_page(root_hpa);
5509+
sp = root_to_sp(root_hpa);
55125510
return !sp || is_obsolete_sp(kvm, sp);
55135511
}
55145512

arch/x86/kvm/mmu/spte.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ static inline struct kvm_mmu_page *sptep_to_sp(u64 *sptep)
236236
return to_shadow_page(__pa(sptep));
237237
}
238238

239+
static inline struct kvm_mmu_page *root_to_sp(hpa_t root)
240+
{
241+
/*
242+
* The "root" may be a special root, e.g. a PAE entry, treat it as a
243+
* SPTE to ensure any non-PA bits are dropped.
244+
*/
245+
return spte_to_child_sp(root);
246+
}
247+
239248
static inline bool is_mmio_spte(u64 spte)
240249
{
241250
return (spte & shadow_mmio_mask) == shadow_mmio_value &&

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ static inline void tdp_mmu_iter_set_spte(struct kvm *kvm, struct tdp_iter *iter,
689689
else
690690

691691
#define tdp_mmu_for_each_pte(_iter, _mmu, _start, _end) \
692-
for_each_tdp_pte(_iter, to_shadow_page(_mmu->root.hpa), _start, _end)
692+
for_each_tdp_pte(_iter, root_to_sp(_mmu->root.hpa), _start, _end)
693693

694694
/*
695695
* Yield if the MMU lock is contended or this thread needs to return control

0 commit comments

Comments
 (0)