Skip to contentSkip to conversation

Verify dirty logging works properly with page stats

PATCH In review Mingwei Zhang 3 participants 15 messages Mar 21, 2022 – Mar 23, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Verify dirty logging works properly with page stats

This patchset is to verify if dirty logging works properly by checking page stats from the per-VM interface. We discovered one performance bug in disallowed_hugepage_adjust() which prevents KVM from recovering large pages for the guest. The selftest logic added later could help validate the problem.

The patchset borrowes two patches come from Ben's series: "[PATCH 00/13]
KVM: x86: Add a cap to disable NX hugepages on a VM" [1], which completes the selftest library functions to use the stats interface.

[1] https://lore.kernel.org/all/[email protected]/T/

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

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

 arch/x86/kvm/mmu/mmu.c                        |  14 +-
 .../selftests/kvm/dirty_log_perf_test.c       |  52 +++++
 .../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 ++++++++++++++++++
 5 files changed, 268 insertions(+), 2 deletions(-)
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#1 lore raw

PATCH 1/4 selftests: KVM: Dump VM stats in binary stats test

From: Ben Gardon <[email protected]>

Add kvm_util library functions to read KVM stats through the binary stats interface and then dump them to stdout when running the binary stats test. Subsequent commits will extend the kvm_util code and use it to make assertions in a test for NX hugepages.

---
 .../selftests/kvm/include/kvm_util_base.h     |   1 +
 .../selftests/kvm/kvm_binary_stats_test.c     |   3 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 143 ++++++++++++++++++
 3 files changed, 147 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
index 4ed6aa049a91..160b9ad8474a 100644
@@ -392,6 +392,7 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);
 
 int vm_get_stats_fd(struct kvm_vm *vm);
 int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
+void dump_vm_stats(struct kvm_vm *vm);
 
 uint32_t guest_get_vcpuid(void);
 
tools/testing/selftests/kvm/kvm_binary_stats_test.c +3 -0
index 17f65d514915..afc4701ce8dd 100644
@@ -174,6 +174,9 @@ static void vm_stats_test(struct kvm_vm *vm)
 	stats_test(stats_fd);
 	close(stats_fd);
 	TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
+
+	/* Dump VM stats */
+	dump_vm_stats(vm);
 }
 
 static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
tools/testing/selftests/kvm/lib/kvm_util.c +143 -0

146 lines not on this page · raw message

Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#2 lore raw

PATCH 2/4 selftests: KVM: Test reading a single stat

In reply to Mingwei Zhang

From: Ben Gardon <[email protected]>

Retrieve the value of a single stat by name in the binary stats test to ensure the kvm_util library functions work.

---
 .../selftests/kvm/include/kvm_util_base.h     |  1 +
 .../selftests/kvm/kvm_binary_stats_test.c     |  3 ++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 53 +++++++++++++++++++
 3 files changed, 57 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
index 160b9ad8474a..a07964c95941 100644
@@ -393,6 +393,7 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);
 int vm_get_stats_fd(struct kvm_vm *vm);
 int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
 void dump_vm_stats(struct kvm_vm *vm);
+uint64_t vm_get_single_stat(struct kvm_vm *vm, const char *stat_name);
 
 uint32_t guest_get_vcpuid(void);
 
tools/testing/selftests/kvm/kvm_binary_stats_test.c +3 -0
index afc4701ce8dd..97bde355f105 100644
@@ -177,6 +177,9 @@ static void vm_stats_test(struct kvm_vm *vm)
 
 	/* Dump VM stats */
 	dump_vm_stats(vm);
+
+	/* Read a single stat. */
+	printf("remote_tlb_flush: %lu\n", vm_get_single_stat(vm, "remote_tlb_flush"));
 }
 
 static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
tools/testing/selftests/kvm/lib/kvm_util.c +53 -0

56 lines not on this page · raw message

Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#3 lore raw

PATCH 3/4 KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

In reply to Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging). Moreover, existing code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +12 -2
index 5628d0ba637e..4d358c273f6c 100644
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
 	    cur_level == fault->goal_level &&
 	    is_shadow_present_pte(spte) &&
 	    !is_large_pte(spte)) {
+		struct kvm_mmu_page *sp;
+		u64 page_mask;
+		/*
+		 * When nx hugepage flag is not set, there is no reason to
+		 * go down to another level. This helps demand paging to
+		 * generate large mappings.
+		 */
+		sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+		if (!sp->lpage_disallowed)
+			return;
 		/*
 		 * A small SPTE exists for this pfn, but FNAME(fetch)
 		 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
 		 * patching back for them into pfn the next 9 bits of
 		 * the address.
 		 */
-		u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-				KVM_PAGES_PER_HPAGE(cur_level - 1);
+		page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+			KVM_PAGES_PER_HPAGE(cur_level - 1);
 		fault->pfn |= fault->gfn & page_mask;
 		fault->goal_level--;
 	}
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#4 lore raw

Re: [PATCH 3/4] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

Quoted 27 lines · Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging). Moreover, existing code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 5628d0ba637e..4d358c273f6c 100644

arch/x86/kvm/mmu/mmu.c void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_ · line 2928

Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
            cur_level == fault->goal_level &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
+               struct kvm_mmu_page *sp;
+               u64 page_mask;
+               /*
+                * When nx hugepage flag is not set, there is no reason to
+                * go down to another level. This helps demand paging to
+                * generate large mappings.
+                */

This comment is relevant to Google's internal demand paging scheme, but isn't really relevant to UFFD demand paging.
Still, as demonstrated by the next commit, this is important for dirty loggin, so I'd suggest updating this comment to refer to that instead.

Quoted 20 lines
arch/x86/kvm/mmu/mmu.c +5 -3
+               sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+               if (!sp->lpage_disallowed)
+                       return;
                /*
                 * A small SPTE exists for this pfn, but FNAME(fetch)
                 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-                               KVM_PAGES_PER_HPAGE(cur_level - 1);
+               page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+                       KVM_PAGES_PER_HPAGE(cur_level - 1);
                fault->pfn |= fault->gfn & page_mask;
                fault->goal_level--;
        }
--

2.35.1.894.gb6a874cedc-goog

#5 lore raw

Re: [PATCH 3/4] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

Quoted 35 lines · Ben Gardon
Quoted 34 lines · Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging). Moreover, existing code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +4 -0
index 5628d0ba637e..4d358c273f6c 100644
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
            cur_level == fault->goal_level &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
+               struct kvm_mmu_page *sp;
+               u64 page_mask;
+               /*
+                * When nx hugepage flag is not set, there is no reason to
Quoted 9 lines

arch/x86/kvm/mmu/mmu.c void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_ · line 2928

Quoted 3 lines
arch/x86/kvm/mmu/mmu.c +3 -0
+                * go down to another level. This helps demand paging to
+                * generate large mappings.
+                */

This comment is relevant to Google's internal demand paging scheme, but isn't really relevant to UFFD demand paging.
Still, as demonstrated by the next commit, this is important for dirty loggin, so I'd suggest updating this comment to refer to that instead.

Ah, leaking my true motivation :-) Definitely will update the comment.

Quoted 20 lines
Quoted 20 lines
arch/x86/kvm/mmu/mmu.c +5 -3
+               sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+               if (!sp->lpage_disallowed)
+                       return;
                /*
                 * A small SPTE exists for this pfn, but FNAME(fetch)
                 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-                               KVM_PAGES_PER_HPAGE(cur_level - 1);
+               page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+                       KVM_PAGES_PER_HPAGE(cur_level - 1);
                fault->pfn |= fault->gfn & page_mask;
                fault->goal_level--;
        }
--

2.35.1.894.gb6a874cedc-goog

#6 lore raw

Re: [PATCH 3/4] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

In reply to Mingwei Zhang
Quoted 5 lines · Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging).

KVM calls kvm_mmu_zap_collapsible_sptes() when dirty logging is disabled. Why is that not sufficient?

Quoted 4 lines

Moreover, existing
code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Good point. Although, that code just got reverted. Maybe say something like:

This assumption may not be true in the future if KVM gains support
for zapping only leaf SPTEs.

Quoted 4 lines

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

s/potentiall/potentially/

Quoted 45 lines
Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +12 -3
index 5628d0ba637e..4d358c273f6c 100644
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
            cur_level == fault->goal_level &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
+               struct kvm_mmu_page *sp;
+               u64 page_mask;
+               /*
+                * When nx hugepage flag is not set, there is no reason to
+                * go down to another level. This helps demand paging to
+                * generate large mappings.
+                */
+               sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+               if (!sp->lpage_disallowed)
+                       return;
                /*
                 * A small SPTE exists for this pfn, but FNAME(fetch)
                 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-                               KVM_PAGES_PER_HPAGE(cur_level - 1);
+               page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+                       KVM_PAGES_PER_HPAGE(cur_level - 1);
                fault->pfn |= fault->gfn & page_mask;
                fault->goal_level--;
        }
--

2.35.1.894.gb6a874cedc-goog

#7 lore raw

Re: [PATCH 3/4] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

Quoted 10 lines · David Matlack
Quoted 5 lines · Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging).

KVM calls kvm_mmu_zap_collapsible_sptes() when dirty logging is disabled. Why is that not sufficient?

Ahh I see, kvm_mmu_zap_collapsible_sptes() only zaps the leaf SPTEs. Could you add a blurb about this in the commit message for future reference?

Quoted 10 lines
Quoted 5 lines

Moreover, existing
code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Good point. Although, that code just got reverted. Maybe say something like:

This assumption may not be true in the future if KVM gains support
for zapping only leaf SPTEs.

Nevermind, support for zapping leaf SPTEs already exists for zapping collapsible SPTEs.

Quoted 53 lines
Quoted 5 lines

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

s/potentiall/potentially/

Quoted 45 lines
Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +12 -3
index 5628d0ba637e..4d358c273f6c 100644
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
            cur_level == fault->goal_level &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
+               struct kvm_mmu_page *sp;
+               u64 page_mask;
+               /*
+                * When nx hugepage flag is not set, there is no reason to
+                * go down to another level. This helps demand paging to
+                * generate large mappings.
+                */
+               sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+               if (!sp->lpage_disallowed)
+                       return;
                /*
                 * A small SPTE exists for this pfn, but FNAME(fetch)
                 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-                               KVM_PAGES_PER_HPAGE(cur_level - 1);
+               page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+                       KVM_PAGES_PER_HPAGE(cur_level - 1);
                fault->pfn |= fault->gfn & page_mask;
                fault->goal_level--;
        }
--

2.35.1.894.gb6a874cedc-goog

#8 lore raw

Re: [PATCH 3/4] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust()

Quoted 9 lines · David Matlack
Quoted 8 lines · David Matlack
Quoted 5 lines · Mingwei Zhang

Add extra check to specify the case of nx hugepage and allow KVM to reconstruct large mapping after dirty logging is disabled. Existing code works only for nx hugepage but the condition is too general in that does not consider other usage case (such as dirty logging).

Quoted 7 lines
Quoted 2 lines

KVM calls kvm_mmu_zap_collapsible_sptes() when dirty logging is disabled. Why is that not sufficient?

Ahh I see, kvm_mmu_zap_collapsible_sptes() only zaps the leaf SPTEs. Could you add a blurb about this in the commit message for future reference?

will do.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Moreover, existing
code assumes that a present PMD or PUD indicates that there exist 'smaller SPTEs' under the paging structure. This assumption may no be true if consider the zapping leafs only behavior in MMU.

Quoted 17 lines
Quoted 4 lines

Good point. Although, that code just got reverted. Maybe say something like:

This assumption may not be true in the future if KVM gains support
for zapping only leaf SPTEs.

Nevermind, support for zapping leaf SPTEs already exists for zapping collapsible SPTEs.

Quoted 7 lines
Quoted 5 lines

Missing the check causes KVM incorrectly regards the faulting page as a NX huge page and refuse to map it at desired level. And this leads to back performance in shadow mmu and potentiall TDP mmu.

s/potentiall/potentially/

Thanks for that.

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
Fixes b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation")
---
 arch/x86/kvm/mmu/mmu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +12 -3
index 5628d0ba637e..4d358c273f6c 100644
@@ -2919,6 +2919,16 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
            cur_level == fault->goal_level &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
+               struct kvm_mmu_page *sp;
+               u64 page_mask;
+               /*
+                * When nx hugepage flag is not set, there is no reason to
+                * go down to another level. This helps demand paging to
+                * generate large mappings.
+                */
+               sp = to_shadow_page(spte & PT64_BASE_ADDR_MASK);
+               if (!sp->lpage_disallowed)
+                       return;
                /*
                 * A small SPTE exists for this pfn, but FNAME(fetch)
                 * and __direct_map would like to create a large PTE
@@ -2926,8 +2936,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
-                               KVM_PAGES_PER_HPAGE(cur_level - 1);
+               page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
+                       KVM_PAGES_PER_HPAGE(cur_level - 1);
                fault->pfn |= fault->gfn & page_mask;
                fault->goal_level--;
        }
--

2.35.1.894.gb6a874cedc-goog

#9 lore raw

PATCH 4/4 selftests: KVM: use dirty logging to check if page stats work correctly

In reply to Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.
- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +52 -0
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N		2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 				 p->slots, p->backing_src,
 				 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+		    "4K page is non zero");
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+		    "2M page is non zero");
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+		    "1G page is non zero");
+#endif
 	perf_test_set_wr_fract(vm, p->wr_fract);
 
 	guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	pr_info("Populate memory time: %ld.%.9lds\n",
 		ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+		    "4K page is zero");
+	if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+		TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+			    "2M page is zero");
+	if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+		TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+			    "1G page is zero");
+#endif
 	/* Enable dirty logging */
 	clock_gettime(CLOCK_MONOTONIC, &start);
 	enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 				iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
 		}
 	}
+#ifdef __x86_64__
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+		    "4K page is zero after dirty logging");
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+		    "2M page is non-zero after dirty logging");
+	TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+		    "1G page is non-zero after dirty logging");
+#endif
 
 	/* Disable dirty logging */
 	clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	pr_info("Disabling dirty logging time: %ld.%.9lds\n",
 		ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+	/*
+	 * Increment iteration to run the vcpus again to verify if huge pages
+	 * come back.
+	 */
+	iteration++;
+	pr_info("Starting the final iteration to verify page stats\n");
+
+	for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+		while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+		       != iteration)
+			;
+	}
+
+	if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+		TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+			    "2M page is zero");
+	if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+		TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+			    "1G page is zero");
+#endif
+
 	/* Tell the vcpu thread to quit */
 	host_quit = true;
 	perf_test_join_vcpu_threads(nr_vcpus);
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#10 lore raw

Re: [PATCH 4/4] selftests: KVM: use dirty logging to check if page stats work correctly

Quoted 3 lines · Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if

Note this is only true if eager page splitting is enabled. It would be more accurate to say:
"While dirty logging is enabled, KVM will re-map any accessed page in NPT/EPT at 4K."

Quoted 3 lines

the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is

It might be worth having a follow up commit which checks if eager splitting is enabled and changes the assertions accordingly.

Quoted 8 lines

disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.

Note a key stage here is after dirty logging is enabled, and then the VM touches all the memory in the data region.
I believe that's the point at which you're making the assertion that all mappings are 4k currently, which is the right place if eager splitting is not enabled.

Quoted 48 lines

- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +12 -0
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N               2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+                   "4K page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non zero");
+#endif
        perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 240

Quoted 7 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +4 -0
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Populate memory time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)

This should also handle 2M hugetlb memory.
I think there might be a library function to translate backing src type to page size too, which could make this check cleaner.

Quoted 11 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +6 -0
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
        /* Enable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
        enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                iteration, ts_diff.tv_sec, ts_diff.tv_nsec);

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 299

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +8 -0
                }
        }
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non-zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non-zero after dirty logging");
+#endif

Note this is after dirty logging has been enabled, AND all pages in the data region have been written by the guest.

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +3 -0
        /* Disable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Disabling dirty logging time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       /*
+        * Increment iteration to run the vcpus again to verify if huge pages

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 320

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +10 -0
+        * come back.
+        */
+       iteration++;
+       pr_info("Starting the final iteration to verify page stats\n");
+
+       for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+               while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+                      != iteration)
+                       ;
+       }

We might as well do this on all archs. Even without the stats, it at least validates that disabling dirty logging doesn't break the VM.

Quoted 15 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +9 -1
+
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
+
        /* Tell the vcpu thread to quit */
        host_quit = true;
        perf_test_join_vcpu_threads(nr_vcpus);
--

2.35.1.894.gb6a874cedc-goog

#11 lore raw

Re: [PATCH 4/4] selftests: KVM: use dirty logging to check if page stats work correctly

Quoted 10 lines · Ben Gardon
Quoted 3 lines · Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if

Note this is only true if eager page splitting is enabled. It would be more accurate to say:
"While dirty logging is enabled, KVM will re-map any accessed page in NPT/EPT at 4K."

Quoted 6 lines
Quoted 3 lines

the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is

It might be worth having a follow up commit which checks if eager splitting is enabled and changes the assertions accordingly.

So eager splitting is still pending for review, right? But yes, I can add one after the feature get merged.

Quoted 10 lines
Quoted 9 lines

disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.

Quoted 5 lines

Note a key stage here is after dirty logging is enabled, and then the VM touches all the memory in the data region.
I believe that's the point at which you're making the assertion that all mappings are 4k currently, which is the right place if eager splitting is not enabled.

Oh, sorry. This one should be after dirty logging is done, not 'just after turning on dirty logging'. Will update it.

Quoted 52 lines
Quoted 52 lines

- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +12 -0
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N               2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+                   "4K page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non zero");
+#endif
        perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Populate memory time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
Quoted 8 lines

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 240

Quoted 4 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +4 -0
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)

This should also handle 2M hugetlb memory.
I think there might be a library function to translate backing src type to page size too, which could make this check cleaner.

Ack.

Quoted 33 lines
Quoted 12 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +6 -0
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
        /* Enable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
        enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                iteration, ts_diff.tv_sec, ts_diff.tv_nsec);

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 299

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +8 -0
                }
        }
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non-zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non-zero after dirty logging");
+#endif

Note this is after dirty logging has been enabled, AND all pages in the data region have been written by the guest.

Quoted 7 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +0 -0
        /* Disable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Disabling dirty logging time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
Quoted 17 lines

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 320

Quoted 13 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +13 -0
+#ifdef __x86_64__
+       /*
+        * Increment iteration to run the vcpus again to verify if huge pages
+        * come back.
+        */
+       iteration++;
+       pr_info("Starting the final iteration to verify page stats\n");
+
+       for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+               while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+                      != iteration)
+                       ;
+       }

We might as well do this on all archs. Even without the stats, it at least validates that disabling dirty logging doesn't break the VM.

Ack.

Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +9 -1
+
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
+
        /* Tell the vcpu thread to quit */
        host_quit = true;
        perf_test_join_vcpu_threads(nr_vcpus);
--

2.35.1.894.gb6a874cedc-goog

#12 lore raw

Re: [PATCH 4/4] selftests: KVM: use dirty logging to check if page stats work correctly

In reply to Ben Gardon
Quoted 83 lines · Ben Gardon
Quoted 3 lines · Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if

Note this is only true if eager page splitting is enabled. It would be more accurate to say:
"While dirty logging is enabled, KVM will re-map any accessed page in NPT/EPT at 4K."

Quoted 3 lines

the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is

It might be worth having a follow up commit which checks if eager splitting is enabled and changes the assertions accordingly.

Quoted 8 lines

disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.

Note a key stage here is after dirty logging is enabled, and then the VM touches all the memory in the data region.
I believe that's the point at which you're making the assertion that all mappings are 4k currently, which is the right place if eager splitting is not enabled.

Quoted 51 lines

- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +12 -0
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N               2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+                   "4K page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non zero");
+#endif
        perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Populate memory time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
Quoted 9 lines

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 240

Quoted 4 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +4 -0
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)

This should also handle 2M hugetlb memory.
I think there might be a library function to translate backing src type to page size too, which could make this check cleaner.

Just went through the selftest code again, it seems this logic a quite x86 and there were no similar checks in other places. So I think I'll just add another condition here for now.

Quoted 64 lines
Quoted 11 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +6 -0
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
        /* Enable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
        enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                iteration, ts_diff.tv_sec, ts_diff.tv_nsec);

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 299

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +8 -0
                }
        }
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non-zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non-zero after dirty logging");
+#endif

Note this is after dirty logging has been enabled, AND all pages in the data region have been written by the guest.

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +3 -0
        /* Disable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Disabling dirty logging time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       /*
+        * Increment iteration to run the vcpus again to verify if huge pages

tools/testing/selftests/kvm/dirty_log_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 320

Quoted 10 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +10 -0
+        * come back.
+        */
+       iteration++;
+       pr_info("Starting the final iteration to verify page stats\n");
+
+       for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+               while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+                      != iteration)
+                       ;
+       }

We might as well do this on all archs. Even without the stats, it at least validates that disabling dirty logging doesn't break the VM.

Quoted 15 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +9 -1
+
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
+
        /* Tell the vcpu thread to quit */
        host_quit = true;
        perf_test_join_vcpu_threads(nr_vcpus);
--

2.35.1.894.gb6a874cedc-goog

#13 lore raw

Re: [PATCH 4/4] selftests: KVM: use dirty logging to check if page stats work correctly

In reply to Mingwei Zhang
Quoted 20 lines · Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.
- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Quoted 6 lines
Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>

Woops, got a mail bounce from this. Should be:

Quoted 99 lines
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +52 -1
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N               2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+                   "4K page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non zero");
+#endif
        perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Populate memory time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
        /* Enable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
        enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
                }
        }
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non-zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non-zero after dirty logging");
+#endif
 
        /* Disable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Disabling dirty logging time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       /*
+        * Increment iteration to run the vcpus again to verify if huge pages
+        * come back.
+        */
+       iteration++;
+       pr_info("Starting the final iteration to verify page stats\n");
+
+       for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+               while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+                      != iteration)
+                       ;
+       }
+
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
+
        /* Tell the vcpu thread to quit */
        host_quit = true;
        perf_test_join_vcpu_threads(nr_vcpus);
--

2.35.1.894.gb6a874cedc-goog

#14 lore raw

Re: [PATCH 4/4] selftests: KVM: use dirty logging to check if page stats work correctly

Quoted 26 lines · Ben Gardon
Quoted 25 lines · Mingwei Zhang

When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. Moreover, when dirty logging is disabled, KVM zaps corresponding SPTEs and we could check whether the large pages come back when guest touches the pages again.

So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations:
- just after vm is created;
- after populating memory into vm but before enabling dirty logging;
- just after turning on dirty logging.
- after one final iteration after turning off dirty logging.

Tested using commands:
- ./dirty_log_perf_test -s anonymous_hugetlb_1gb
- ./dirty_log_perf_test -s anonymous_thp

Cc Sean Christopherson <[email protected]>
Cc David Matlack <[email protected]>
Cc Jing Zhang <[email protected]>
Cc Peter Xu <[email protected]>
Quoted 5 lines
Quoted 1 line

Woops, got a mail bounce from this. Should be:

Oh... sorry about that. Will discuss with you offline. Really want to avoid this in the future.

Quoted 99 lines
Quoted 99 lines
---
 .../selftests/kvm/dirty_log_perf_test.c       | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
tools/testing/selftests/kvm/dirty_log_perf_test.c +52 -1
index 1954b964d1cf..ab0457d91658 100644
@@ -19,6 +19,10 @@
 #include "perf_test_util.h"
 #include "guest_modes.h"
 
+#ifdef __x86_64__
+#include "processor.h"
+#endif
+
 /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/
 #define TEST_HOST_LOOP_N               2UL
 
@@ -185,6 +189,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                 p->slots, p->backing_src,
                                 p->partition_vcpu_memory_access);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") == 0,
+                   "4K page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non zero");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non zero");
+#endif
        perf_test_set_wr_fract(vm, p->wr_fract);
 
        guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm);
@@ -222,6 +234,16 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Populate memory time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
        /* Enable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
        enable_dirty_logging(vm, p->slots);
@@ -267,6 +289,14 @@ static void run_test(enum vm_guest_mode mode, void *arg)
                                iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
                }
        }
+#ifdef __x86_64__
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_4k") != 0,
+                   "4K page is zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") == 0,
+                   "2M page is non-zero after dirty logging");
+       TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") == 0,
+                   "1G page is non-zero after dirty logging");
+#endif
 
        /* Disable dirty logging */
        clock_gettime(CLOCK_MONOTONIC, &start);
@@ -275,6 +305,28 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("Disabling dirty logging time: %ld.%.9lds\n",
                ts_diff.tv_sec, ts_diff.tv_nsec);
 
+#ifdef __x86_64__
+       /*
+        * Increment iteration to run the vcpus again to verify if huge pages
+        * come back.
+        */
+       iteration++;
+       pr_info("Starting the final iteration to verify page stats\n");
+
+       for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) {
+               while (READ_ONCE(vcpu_last_completed_iteration[vcpu_id])
+                      != iteration)
+                       ;
+       }
+
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_2m") != 0,
+                           "2M page is zero");
+       if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB)
+               TEST_ASSERT(vm_get_single_stat(vm, "pages_1g") != 0,
+                           "1G page is zero");
+#endif
+
        /* Tell the vcpu thread to quit */
        host_quit = true;
        perf_test_join_vcpu_threads(nr_vcpus);
--

2.35.1.894.gb6a874cedc-goog