Skip to contentSkip to conversation

KVM: arm64: Dirty logging fixes/cleanups

PATCH Applied Wei-Lin Chang 5 participants 9 messages Oct 29 – Nov 3

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

v2 cover KVM: arm64: Dirty logging fixes/cleanups

Hi,

These are two fixes/cleanups I found when reading the code. It fixes dirty logging detection in ring-only mode and updates a stale comment. Please view the patches for more detail.

* Changes from v1 ([1]):

- Keep memslot_is_logging() and use kvm_slot_dirty_track_enabled() in it. This preserves the RO memslot check.

- Change patch 2 to updating the stale comment only, the original change is obsolete now.

- Collect Reviewed-by and Tested-by tags from Fuad (thanks!).

Thanks!

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

Wei-Lin Chang (2):
KVM: arm64: Fix dirty logging detection in ring-only mode
KVM: arm64: Remove stale remark on canonical IPA alignment

 arch/arm64/kvm/mmu.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 KVM: arm64: Fix dirty logging detection in ring-only mode

When checking whether a memslot has dirty logging enabled, the KVM_MEM_LOG_DIRTY_PAGES flag is the source of truth. Previously in memslot_is_logging() we were testing the dirty bitmap only and did not consider dirty ring.

With the ring and no bitmap, a logged memslot can still get block mappings, so guest writes to most of a block are never logged and a migration can copy stale memory.

Fix this by using kvm_slot_dirty_track_enabled() instead which checks KVM_MEM_LOG_DIRTY_PAGES, within memslot_is_logging().

Fixes 9cb1096f8590 ("KVM: arm64: Enable ring-based dirty memory tracking")
---
 arch/arm64/kvm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +1 -1
index 705f6d84770e..c7df646af79f 100644
@@ -176,7 +176,7 @@ static int kvm_mmu_split_huge_pages(struct kvm *kvm, phys_addr_t addr,
 
 static bool memslot_is_logging(struct kvm_memory_slot *memslot)
 {
-	return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
+	return kvm_slot_dirty_track_enabled(memslot) && !(memslot->flags & KVM_MEM_READONLY);
 }
 
 static int pkvm_flush_remote_tlbs(struct kvm *kvm)
Signature · 1 line
2.43.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] KVM: arm64: Fix dirty logging detection in ring-only mode

Quoted 12 lines · Wei-Lin Chang

When checking whether a memslot has dirty logging enabled, the KVM_MEM_LOG_DIRTY_PAGES flag is the source of truth. Previously in memslot_is_logging() we were testing the dirty bitmap only and did not consider dirty ring.

With the ring and no bitmap, a logged memslot can still get block mappings, so guest writes to most of a block are never logged and a migration can copy stale memory.

Fix this by using kvm_slot_dirty_track_enabled() instead which checks KVM_MEM_LOG_DIRTY_PAGES, within memslot_is_logging().

Quoted 9 lines
Fixes 9cb1096f8590 ("KVM: arm64: Enable ring-based dirty memory tracking")
---
  arch/arm64/kvm/mmu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] KVM: arm64: Fix dirty logging detection in ring-only mode

In reply to Wei-Lin Chang
Quoted 37 lines · Wei-Lin Chang

When checking whether a memslot has dirty logging enabled, the KVM_MEM_LOG_DIRTY_PAGES flag is the source of truth. Previously in memslot_is_logging() we were testing the dirty bitmap only and did not consider dirty ring.

With the ring and no bitmap, a logged memslot can still get block mappings, so guest writes to most of a block are never logged and a migration can copy stale memory.

Fix this by using kvm_slot_dirty_track_enabled() instead which checks KVM_MEM_LOG_DIRTY_PAGES, within memslot_is_logging().

Fixes 9cb1096f8590 ("KVM: arm64: Enable ring-based dirty memory tracking")
---
 arch/arm64/kvm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +1 -1
index 705f6d84770e..c7df646af79f 100644
@@ -176,7 +176,7 @@ static int kvm_mmu_split_huge_pages(struct kvm *kvm, phys_addr_t addr,
 
 static bool memslot_is_logging(struct kvm_memory_slot *memslot)
 {
-	return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
+	return kvm_slot_dirty_track_enabled(memslot) && !(memslot->flags & KVM_MEM_READONLY);
 }
 
 static int pkvm_flush_remote_tlbs(struct kvm *kvm)
Signature · 1 line
2.43.0

Awesome, thanks for taking the suggestion!
Leo

#4 lore raw

PATCH v2 2/2 KVM: arm64: Remove stale remark on canonical IPA alignment

In reply to Wei-Lin Chang

Commit 3b8ce2b5132ae ("KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()") created a variable to store the canonical IPA, instead of calculating it when needed. It moved the aligning just before mark_page_dirty_in_slot() to an earlier place but did not update the comment.

Remove the stale part of the comment.

---
 arch/arm64/kvm/mmu.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +1 -5
index c7df646af79f..695c6229a216 100644
@@ -2232,11 +2232,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	kvm_fault_unlock(kvm);
 	kfree(mapping);
 
-	/*
-	 * Mark the page dirty only if the fault is handled successfully,
-	 * making sure we adjust the canonical IPA if the mapping size has
-	 * been updated (via a THP upgrade, for example).
-	 */
+	/* Mark the page dirty only if the fault is handled successfully. */
 	if (writable && !ret)
 		mark_page_dirty_in_slot(kvm, s2fd->memslot,
 					gpa_to_gfn(canonical_ipa));
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] KVM: arm64: Remove stale remark on canonical IPA alignment

Quoted 10 lines · Wei-Lin Chang

Commit 3b8ce2b5132ae ("KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()") created a variable to store the canonical IPA, instead of calculating it when needed. It moved the aligning just before mark_page_dirty_in_slot() to an earlier place but did not update the comment.

Remove the stale part of the comment.

Cheers,
/fuad

Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] KVM: arm64: Remove stale remark on canonical IPA alignment

In reply to Wei-Lin Chang
Quoted 6 lines · Wei-Lin Chang

Commit 3b8ce2b5132ae ("KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()") created a variable to store the canonical IPA, instead of calculating it when needed. It moved the aligning just before mark_page_dirty_in_slot() to an earlier place but did not update the comment.

Quoted 7 lines

Remove the stale part of the comment.

---
  arch/arm64/kvm/mmu.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
Reviewed-by on v2 2/2
#7 lore raw

Re: [PATCH v2 2/2] KVM: arm64: Remove stale remark on canonical IPA alignment

In reply to Wei-Lin Chang
Quoted 33 lines · Wei-Lin Chang

Commit 3b8ce2b5132ae ("KVM: arm64: Use a variable for the canonical IPA in kvm_s2_fault_map()") created a variable to store the canonical IPA, instead of calculating it when needed. It moved the aligning just before mark_page_dirty_in_slot() to an earlier place but did not update the comment.

Remove the stale part of the comment.

---
 arch/arm64/kvm/mmu.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/mmu.c +1 -5
index c7df646af79f..695c6229a216 100644
@@ -2232,11 +2232,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	kvm_fault_unlock(kvm);
 	kfree(mapping);
 
-	/*
-	 * Mark the page dirty only if the fault is handled successfully,
-	 * making sure we adjust the canonical IPA if the mapping size has
-	 * been updated (via a THP upgrade, for example).
-	 */
+	/* Mark the page dirty only if the fault is handled successfully. */
 	if (writable && !ret)
 		mark_page_dirty_in_slot(kvm, s2fd->memslot,
 					gpa_to_gfn(canonical_ipa));
Signature · 1 line
2.43.0
maintainer Applied
#8 lore raw
In reply to Wei-Lin Chang
Quoted 10 lines · Wei-Lin Chang

These are two fixes/cleanups I found when reading the code. It fixes dirty logging detection in ring-only mode and updates a stale comment. Please view the patches for more detail.

* Changes from v1 ([1]):

- Keep memslot_is_logging() and use kvm_slot_dirty_track_enabled() in it. This preserves the RO memslot check.

[...]

Applied to fixes, thanks!

[1/2] KVM: arm64: Fix dirty logging detection in ring-only mode

      commit: 98b2eab9345c502cb13ef2e347ab5a6c288db482
[2/2] KVM: arm64: Remove stale remark on canonical IPA alignment
      commit: 4bc0c9cd84767a978046c3b2a73dc932bdb8d9f9

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.