Skip to content

Commit c602274

Browse files
sean-jcgregkh
authored andcommitted
KVM: x86/mmu: Document the "rules" for using host_pfn_mapping_level()
[ Upstream commit 65e3b44 ] Add a comment to document how host_pfn_mapping_level() can be used safely, as the line between safe and dangerous is quite thin. E.g. if KVM were to ever support in-place promotion to create huge pages, consuming the level is safe if the caller holds mmu_lock and checks that there's an existing _leaf_ SPTE, but unsafe if the caller only checks that there's a non-leaf SPTE. Opportunistically tweak the existing comments to explicitly document why KVM needs to use READ_ONCE(). No functional change intended. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20220715232107.3775620-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Stable-dep-of: 2abd528 ("KVM: x86: Check for invalid/obsolete root *after* making MMU pages available") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Kenta Akagi <k@mgml.me> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 94f50cc commit c602274

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,31 @@ static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
28732873
__direct_pte_prefetch(vcpu, sp, sptep);
28742874
}
28752875

2876+
/*
2877+
* Lookup the mapping level for @gfn in the current mm.
2878+
*
2879+
* WARNING! Use of host_pfn_mapping_level() requires the caller and the end
2880+
* consumer to be tied into KVM's handlers for MMU notifier events!
2881+
*
2882+
* There are several ways to safely use this helper:
2883+
*
2884+
* - Check mmu_notifier_retry_hva() after grabbing the mapping level, before
2885+
* consuming it. In this case, mmu_lock doesn't need to be held during the
2886+
* lookup, but it does need to be held while checking the MMU notifier.
2887+
*
2888+
* - Hold mmu_lock AND ensure there is no in-progress MMU notifier invalidation
2889+
* event for the hva. This can be done by explicit checking the MMU notifier
2890+
* or by ensuring that KVM already has a valid mapping that covers the hva.
2891+
*
2892+
* - Do not use the result to install new mappings, e.g. use the host mapping
2893+
* level only to decide whether or not to zap an entry. In this case, it's
2894+
* not required to hold mmu_lock (though it's highly likely the caller will
2895+
* want to hold mmu_lock anyways, e.g. to modify SPTEs).
2896+
*
2897+
* Note! The lookup can still race with modifications to host page tables, but
2898+
* the above "rules" ensure KVM will not _consume_ the result of the walk if a
2899+
* race with the primary MMU occurs.
2900+
*/
28762901
static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
28772902
const struct kvm_memory_slot *slot)
28782903
{

0 commit comments

Comments
 (0)