Skip to content

Commit 81d480f

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Wrap sanity check on number of TDP MMU pages with KVM_PROVE_MMU
Wrap the TDP MMU page counter in CONFIG_KVM_PROVE_MMU so that the sanity check is omitted from production builds, and more importantly to remove the atomic accesses to account pages. A one-off memory leak in production is relatively uninteresting, and a WARN_ON won't help mitigate a systemic issue; it's as much about helping triage memory leaks as it is about detecting them in the first place, and doesn't magically stop the leaks. I.e. production environments will be quite sad if a severe KVM bug escapes, regardless of whether or not KVM WARNs. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20250315023448.2358456-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 0297cdc commit 81d480f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,13 @@ struct kvm_arch {
14721472
struct once nx_once;
14731473

14741474
#ifdef CONFIG_X86_64
1475-
/* The number of TDP MMU pages across all roots. */
1475+
#ifdef CONFIG_KVM_PROVE_MMU
1476+
/*
1477+
* The number of TDP MMU pages across all roots. Used only to sanity
1478+
* check that KVM isn't leaking TDP MMU pages.
1479+
*/
14761480
atomic64_t tdp_mmu_pages;
1481+
#endif
14771482

14781483
/*
14791484
* List of struct kvm_mmu_pages being used as roots.

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
4040
kvm_tdp_mmu_invalidate_roots(kvm, KVM_VALID_ROOTS);
4141
kvm_tdp_mmu_zap_invalidated_roots(kvm, false);
4242

43-
WARN_ON(atomic64_read(&kvm->arch.tdp_mmu_pages));
43+
#ifdef CONFIG_KVM_PROVE_MMU
44+
KVM_MMU_WARN_ON(atomic64_read(&kvm->arch.tdp_mmu_pages));
45+
#endif
4446
WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
4547

4648
/*
@@ -325,13 +327,17 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
325327
static void tdp_account_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
326328
{
327329
kvm_account_pgtable_pages((void *)sp->spt, +1);
330+
#ifdef CONFIG_KVM_PROVE_MMU
328331
atomic64_inc(&kvm->arch.tdp_mmu_pages);
332+
#endif
329333
}
330334

331335
static void tdp_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
332336
{
333337
kvm_account_pgtable_pages((void *)sp->spt, -1);
338+
#ifdef CONFIG_KVM_PROVE_MMU
334339
atomic64_dec(&kvm->arch.tdp_mmu_pages);
340+
#endif
335341
}
336342

337343
/**

0 commit comments

Comments
 (0)