Skip to content

Commit 064a5ab

Browse files
committed
KVM: x86/mmu: Cleanup sanity check of SPTEs at SP free
Massage the error message for the sanity check on SPTEs when freeing a shadow page to be more verbose, and to print out all shadow-present SPTEs, not just the first SPTE encountered. Printing all SPTEs can be quite valuable for debug, e.g. highlights whether the leak is a one-off or widepsread, or possibly the result of memory corruption (something else in the kernel stomping on KVM's SPTEs). Opportunistically move the MMU_WARN_ON() into the helper itself, which will allow a future cleanup to use BUILD_BUG_ON_INVALID() as the stub for MMU_WARN_ON(). BUILD_BUG_ON_INVALID() works as intended and results in the compiler complaining about is_empty_shadow_page() not being declared. Link: https://lore.kernel.org/r/20230729004722.1056172-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c7784ee commit 064a5ab

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,21 +1693,19 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
16931693
return young;
16941694
}
16951695

1696-
#ifdef MMU_DEBUG
1697-
static int is_empty_shadow_page(u64 *spt)
1696+
static void kvm_mmu_check_sptes_at_free(struct kvm_mmu_page *sp)
16981697
{
1698+
#ifdef MMU_DEBUG
16991699
int i;
17001700

17011701
for (i = 0; i < SPTE_ENT_PER_PAGE; i++) {
1702-
if (is_shadow_present_pte(spt[i])) {
1703-
printk(KERN_ERR "%s: %p %llx\n", __func__,
1704-
&spt[i], spt[i]);
1705-
return 0;
1706-
}
1702+
if (MMU_WARN_ON(is_shadow_present_pte(sp->spt[i])))
1703+
pr_err_ratelimited("SPTE %llx (@ %p) for gfn %llx shadow-present at free",
1704+
sp->spt[i], &sp->spt[i],
1705+
kvm_mmu_page_get_gfn(sp, i));
17071706
}
1708-
return 1;
1709-
}
17101707
#endif
1708+
}
17111709

17121710
/*
17131711
* This value is the sum of all of the kvm instances's
@@ -1735,7 +1733,8 @@ static void kvm_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
17351733

17361734
static void kvm_mmu_free_shadow_page(struct kvm_mmu_page *sp)
17371735
{
1738-
MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
1736+
kvm_mmu_check_sptes_at_free(sp);
1737+
17391738
hlist_del(&sp->hash_link);
17401739
list_del(&sp->link);
17411740
free_page((unsigned long)sp->spt);

0 commit comments

Comments
 (0)