[PATCH v3 0/6] Verify dirty logging works properly with page stats

From: Mingwei Zhang
Date: Fri Apr 01 2022 - 02:36:46 EST


This patch set aims to fix a bug in which KVM incorrectly assumes a large
page as a NX huge page. The bug would prevent guest VM from regaining large
pages and cause performance issue. We fix the bug by explicitly checking
the lpage_disallowed field in the shadow page. Moreover, to fix the bug
properly for TDP MMU, we integrate two patches from Sean that ensures that
we update lpage_disallowed in shadow page before making spte visible to
guest.

To verify the bug fixed, we use dirty logging as the testing target and
dirty_log_perf_test as the selftest binary. By adding the code to check the
page stats from the per-VM interface, we discovered that VMs could regain
large pages after dirty logging disabled. We also verify the existence of
the bug if running with unpatched kernels.

To make the selftest working properly with per-VM stats interface, we
borrowes two patches come from Ben's series: "[PATCH 00/13] KVM: x86: Add a
cap to disable NX hugepages on a VM" [1].

[1] https://lore.kernel.org/all/20220310164532.1821490-2-bgardon@xxxxxxxxxx/T/


v2 -> v3:
- Update lpage_disallowed before making spte visible [seanjc].
- Adding tdp_mmu_pages stats [seanjc]
- update comments in selftest [bgardon]

v2: https://lore.kernel.org/lkml/20220323184915.1335049-1-mizhang@xxxxxxxxxx/T/

v1 -> v2:
- Update the commit message. [dmatlack]
- Update the comments in patch 3/4 to clarify the motivation. [bgardon]
- Add another iteration in dirty_log_perf_test to regain pages [bgardon]


Ben Gardon (2):
KVM: selftests: Dump VM stats in binary stats test
KVM: selftests: Test reading a single stat

Mingwei Zhang (2):
KVM: x86/mmu: explicitly check nx_hugepage in
disallowed_hugepage_adjust()
selftests: KVM: use page stats to check if dirty logging works
properly

Sean Christopherson (2):
KVM: x86/mmu: Set lpage_disallowed in TDP MMU before setting SPTE
KVM: x86/mmu: Track the number of TDP MMU pages, but not the actual
pages

arch/x86/include/asm/kvm_host.h | 11 +-
arch/x86/kvm/mmu/mmu.c | 28 ++-
arch/x86/kvm/mmu/mmu_internal.h | 2 +-
arch/x86/kvm/mmu/tdp_mmu.c | 36 ++--
.../selftests/kvm/dirty_log_perf_test.c | 53 +++++
.../selftests/kvm/include/kvm_util_base.h | 2 +
.../selftests/kvm/kvm_binary_stats_test.c | 6 +
tools/testing/selftests/kvm/lib/kvm_util.c | 196 ++++++++++++++++++
8 files changed, 303 insertions(+), 31 deletions(-)

--
2.35.1.1094.g7c7d902a7c-goog