Skip to content

Commit 5cab1c9

Browse files
sean-jcgregkh
authored andcommitted
KVM: x86/mmu: Ensure hugepage is in by slot before checking max mapping level
commit ef057cb upstream. When recovering hugepages in the shadow MMU, verify that the base gfn of the shadow page is actually contained within the target memslot, *before* querying the max mapping level given the shadow page's gfn. Failure to pre-check the validity of the gfn can lead to an out-of-bounds access to the slot's lpage_info (which typically manifests as a host #PF because the lpage_info is vmalloc'd) if the guest creates a hugepage mapping (in its PTEs) that extends "below" the bounds of a memslot. When faulting in memory for a guest, and the size of the guest mapping is greater than KVM's (current) max mapping, then KVM will create a "direct" shadow page (direct in that there are no gPTEs to shadow, and so the target gfn is a direct calculation given the base gfn of the shadow page). The hugepage recovery flow looks for such direct shadow pages, as forcing 4KiB mappings when dirty logging generates the guest > host mapping size case. When the 4KiB restriction is lifted, then KVM can replace the shadow page with a hugepage. But if KVM originally used a smaller mapping than the guest because the range of memory covered by the guest hugepage exceeds the bounds of a memslot, then KVM will link a direct shadow page with a gfn that is outside the bounds of the memslot being used to fault in memory. The rmap entry added for the leaf mapping is correct and within bounds, but the gfn of the leaf SPTE's parent shadow page will be out of bounds. BUG: unable to handle page fault for address: ffffc90000806ffc #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 100000067 P4D 100000067 PUD 1002a7067 PMD 10612f067 PTE 0 Oops: Oops: 0000 [#1] SMP CPU: 13 UID: 1000 PID: 757 Comm: mmu_stress_test Not tainted 7.1.0-rc1-48ce1e26eace-x86_pir_to_irr_comments-vm rockchip-linux#341 PREEMPT Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:kvm_mmu_max_mapping_level+0x79/0x2b0 [kvm] Call Trace: <TASK> kvm_mmu_recover_huge_pages+0x21b/0x320 [kvm] kvm_set_memslot+0x1ee/0x590 [kvm] kvm_set_memory_region.part.0+0x3a1/0x4d0 [kvm] kvm_vm_ioctl+0x9bf/0x15d0 [kvm] __x64_sys_ioctl+0x8a/0xd0 do_syscall_64+0xb7/0xbb0 entry_SYSCALL_64_after_hwframe+0x4b/0x53 RIP: 0033:0x7f21c0f1a9bf </TASK> Don't bother pre-checking the bounds of the potential hugepage, i.e. don't check that e.g. sp->gfn + KVM_PAGES_PER_HPAGE(sp->role.level + 1) is also within the memslot, as the checks performed by kvm_mmu_max_mapping_level() are a superset of the basic bounds checks. I.e. pre-checking the full range would be a dubious micro-optimization. Fixes: 9eba50f ("KVM: x86/mmu: Consult max mapping level when zapping collapsible SPTEs") Cc: stable@vger.kernel.org Cc: David Matlack <dmatlack@google.com> Cc: James Houghton <jthoughton@google.com> Cc: Alexander Bulekov <bkov@amazon.com> Cc: Fred Griffoul <fgriffo@amazon.co.uk> Cc: Alexander Graf <graf@amazon.de> Cc: David Woodhouse <dwmw@amazon.co.uk> Cc: Filippo Sironi <sironi@amazon.de> Cc: Ivan Orlov <iorlov@amazon.co.uk> Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b1337aa commit 5cab1c9

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6417,13 +6417,19 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
64176417
sp = sptep_to_sp(sptep);
64186418

64196419
/*
6420-
* We cannot do huge page mapping for indirect shadow pages,
6421-
* which are found on the last rmap (level = 1) when not using
6422-
* tdp; such shadow pages are synced with the page table in
6423-
* the guest, and the guest page table is using 4K page size
6424-
* mapping if the indirect sp has level = 1.
6420+
* Direct shadow page can be replaced by a hugepage if the host
6421+
* mapping level allows it and the memslot maps all of the host
6422+
* hugepage. Note! If the memslot maps only part of the
6423+
* hugepage, sp->gfn may be below slot->base_gfn, and querying
6424+
* the max mapping level would cause an out-of-bounds lpage_info
6425+
* access. So the gfn bounds check *must* be done first.
6426+
*
6427+
* Indirect shadow pages are created when the guest page tables
6428+
* are using 4K pages. Since the host mapping is always
6429+
* constrained by the page size in the guest, indirect shadow
6430+
* pages are never collapsible.
64256431
*/
6426-
if (sp->role.direct &&
6432+
if (sp->role.direct && is_gfn_in_memslot(slot, sp->gfn) &&
64276433
sp->role.level < kvm_mmu_max_mapping_level(kvm, slot, sp->gfn,
64286434
PG_LEVEL_NUM)) {
64296435
kvm_zap_one_rmap_spte(kvm, rmap_head, sptep);

include/linux/kvm_host.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,11 @@ int kvm_request_irq_source_id(struct kvm *kvm);
16211621
void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
16221622
bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args);
16231623

1624+
static inline bool is_gfn_in_memslot(const struct kvm_memory_slot *slot, gfn_t gfn)
1625+
{
1626+
return gfn >= slot->base_gfn && gfn < slot->base_gfn + slot->npages;
1627+
}
1628+
16241629
/*
16251630
* Returns a pointer to the memslot if it contains gfn.
16261631
* Otherwise returns NULL.
@@ -1631,7 +1636,7 @@ try_get_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
16311636
if (!slot)
16321637
return NULL;
16331638

1634-
if (gfn >= slot->base_gfn && gfn < slot->base_gfn + slot->npages)
1639+
if (is_gfn_in_memslot(slot, gfn))
16351640
return slot;
16361641
else
16371642
return NULL;

0 commit comments

Comments
 (0)