Skip to contentSkip to conversation

KVM: riscv: Set Accessed bit in dirty-log write fault fast path

PATCH In review SeungJu Cheon 4 participants 4 messages Oct 16 – Nov 13

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

PATCH 1/1 KVM: riscv: Set Accessed bit in dirty-log write fault fast path

kvm_riscv_mmu_dirty_log_write_fault_fast() considers a leaf with W and D set already handled, even if aging has cleared its A bit. On hardware that faults when A is clear, the fast path returns true without setting A and skips the slow path that would set it. The guest retries the same store, but A is still clear, so it faults again. This repeats indefinitely, preventing the guest from making progress.

Host reclaim or page_idle can trigger this while dirty logging is enabled, potentially hanging vCPUs during live migration.

Require _PAGE_ACCESSED in addition to W and D before treating the fault as handled, and set A along with W and D. The existing cmpxchg loop retries if aging changes the PTE before the update.

Fixes 7705be59eb2d ("KVM: riscv: Fast-path dirty logging write faults")

---
Testing:

On a HiFive Premier P550 (EIC7700X, which traps on clear A/D bits), run:

dirty_log_perf_test -v 3 -b 256M -i 3

While the test runs, repeatedly mark all system RAM idle through /sys/kernel/mm/page_idle/bitmap.

Before the fix, the test did not complete its first iteration in over 11 minutes, with lock_stat showing about 16k read-side mmu_lock acquisitions per second from the fast path.

After the fix, all three iterations complete, each taking about 29 seconds under the same aging workload.

 arch/riscv/kvm/mmu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/kvm/mmu.c +4 -3
index 78a1826c466d..042d1e3b3547 100644
@@ -591,14 +591,15 @@ static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm,
 			dirty_marked = true;
 		}
 
-		if ((pte_val(old_pte) & (_PAGE_WRITE | _PAGE_DIRTY)) ==
-		    (_PAGE_WRITE | _PAGE_DIRTY)) {
+		if ((pte_val(old_pte) &
+		     (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) ==
+		    (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) {
 			new_pte = old_pte;
 			ret = true;
 			break;
 		}
 
-		new_pte = pte_mkdirty(pte_mkwrite_novma(old_pte));
+		new_pte = pte_mkyoung(pte_mkdirty(pte_mkwrite_novma(old_pte)));
 
 		if (kvm_riscv_gstage_try_update_pte(&gstage, ptep_level, gpa,
 						    ptep, old_pte, new_pte)) {
Signature · 1 line
2.52.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] kvm: riscv: host memory corruption via dirty logging fast path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] kvm: riscv: host memory corruption via dirty logging fast path --

commit b1648eda48b2a005ceca6ca39f80fd20aed60966
Author: SeungJu Cheon <[email protected]>
KVM: riscv: Set Accessed bit in dirty-log write fault fast path

This patch fixes an issue where the fast path for dirty logging write faults loops indefinitely on hardware that faults when the Accessed bit is clear. It ensures the accessed bit is set alongside write and dirty bits.

Quoted 4 lines
arch/riscv/kvm/mmu.c +0 -0
index 371fccaf9df08..21637b7959730 100644

[ ... ]

Quoted 1 line
arch/riscv/kvm/mmu.c +0 -0
@@ -591,14 +591,15 @@ static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm,

[ ... ]

arch/riscv/kvm/mmu.c static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm, · line 602

Quoted 2 lines
arch/riscv/kvm/mmu.c +1 -1
-		new_pte = pte_mkdirty(pte_mkwrite_novma(old_pte));
+		new_pte = pte_mkyoung(pte_mkdirty(pte_mkwrite_novma(old_pte)));

[Severity: Critical]
Does this unconditionally set the _PAGE_WRITE bit on the stage-2 PTE without verifying if the original mapping was host-writable?

When dirty logging is enabled, a guest write to a previously read-only mapped page can trigger kvm_riscv_mmu_map() with logging enabled:

kvm_riscv_mmu_map()

    if (is_write && logging &&
        kvm_riscv_mmu_dirty_log_write_fault_fast(kvm, memslot, gpa, out_map))
        return 0;

If we unconditionally add _PAGE_WRITE here via pte_mkwrite_novma(), could an unprivileged guest bypass host write-protection by reading an unallocated page (like the shared zero page) and then writing to it during live migration?

This appears to bypass the host writability checks in the slow path's __kvm_faultin_pfn(), which might allow a guest to write directly into host read-only pages, such as CoW mappings or the shared zero page.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to SeungJu Cheon
Quoted 6 lines · SeungJu Cheon
arch/riscv/kvm/mmu.c +0 -2
@@ -591,14 +591,15 @@ static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm,
 			dirty_marked = true;
 		}
 
-		if ((pte_val(old_pte) & (_PAGE_WRITE | _PAGE_DIRTY)) ==
-		    (_PAGE_WRITE | _PAGE_DIRTY)) {

arch/riscv/kvm/mmu.c static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm, · line 602

Quoted 10 lines
arch/riscv/kvm/mmu.c +4 -1
+		if ((pte_val(old_pte) &
+		     (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) ==
+		    (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) {
 			new_pte = old_pte;
 			ret = true;
 			break;
 		}
 
-		new_pte = pte_mkdirty(pte_mkwrite_novma(old_pte));
+		new_pte = pte_mkyoung(pte_mkdirty(pte_mkwrite_novma(old_pte)));

I tested this on QEMU 10.2.1 with Svade enabled and Svadu disabled. With dirty logging enabled, the test clears A through page-idle tracking, write-protects the page with KVM_GET_DIRTY_LOG, and resumes the guest write. The unpatched kernel times out after 10 seconds, while the same test completes with this patch applied.

Thanks,
Jinyu

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to SeungJu Cheon
Quoted 12 lines · SeungJu Cheon

kvm_riscv_mmu_dirty_log_write_fault_fast() considers a leaf with W and D set already handled, even if aging has cleared its A bit. On hardware that faults when A is clear, the fast path returns true without setting A and skips the slow path that would set it. The guest retries the same store, but A is still clear, so it faults again. This repeats indefinitely, preventing the guest from making progress.

Host reclaim or page_idle can trigger this while dirty logging is enabled, potentially hanging vCPUs during live migration.

Quoted 7 lines

Require _PAGE_ACCESSED in addition to W and D before treating the fault as handled, and set A along with W and D. The existing cmpxchg loop retries if aging changes the PTE before the update.

Fixes 7705be59eb2d ("KVM: riscv: Fast-path dirty logging write faults")

LGTM.

Queued this patch as a fix for Linux-7.4-rcX

Thanks,
Anup

Quoted 47 lines

---
Testing:

On a HiFive Premier P550 (EIC7700X, which traps on clear A/D bits), run:

dirty_log_perf_test -v 3 -b 256M -i 3

While the test runs, repeatedly mark all system RAM idle through /sys/kernel/mm/page_idle/bitmap.

Before the fix, the test did not complete its first iteration in over 11 minutes, with lock_stat showing about 16k read-side mmu_lock acquisitions per second from the fast path.

After the fix, all three iterations complete, each taking about 29 seconds under the same aging workload.

 arch/riscv/kvm/mmu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/kvm/mmu.c +4 -4
index 78a1826c466d..042d1e3b3547 100644
@@ -591,14 +591,15 @@ static bool kvm_riscv_mmu_dirty_log_write_fault_fast(struct kvm *kvm,
                        dirty_marked = true;
                }
 
-               if ((pte_val(old_pte) & (_PAGE_WRITE | _PAGE_DIRTY)) ==
-                   (_PAGE_WRITE | _PAGE_DIRTY)) {
+               if ((pte_val(old_pte) &
+                    (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) ==
+                   (_PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)) {
                        new_pte = old_pte;
                        ret = true;
                        break;
                }
 
-               new_pte = pte_mkdirty(pte_mkwrite_novma(old_pte));
+               new_pte = pte_mkyoung(pte_mkdirty(pte_mkwrite_novma(old_pte)));
 
                if (kvm_riscv_gstage_try_update_pte(&gstage, ptep_level, gpa,
                                                    ptep, old_pte, new_pte)) {
--

2.52.0