KVM: riscv: Allow rescheduling during G-stage unmap and teardown
Subsystems (3)
PATCH v6 1/1 KVM: riscv: Allow rescheduling during G-stage unmap and teardown
Removing a populated memslot or destroying a VM can walk a large G-stage range while holding mmu_lock. Limit the time spent holding the lock continuously and let other tasks run while tearing down a VM.
Process sleepable memslot and MMU notifier unmaps in PMD-sized chunks. Advance past holes and fully covered block mappings, even when they extend past a chunk. Finish any pending remote TLB invalidations before yielding the lock, and read the current root under the lock before walking the next chunk. Flush the remaining batch before returning.
Detach the root and request the full VM TLB flush under mmu_lock before starting teardown. The VM is being rolled back during setup, its owner mm is exiting, or the last KVM reference has been dropped. No vCPU can still run with the detached root in these cases.
Recursively clear the old tree outside mmu_lock and check for rescheduling as page tables are removed. Keep RCU-delayed freeing for the root and its descendants so lockless aging walkers can finish.
Codex assisted with implementation and QEMU validation. Sashiko identified the redundant final range flush during review.
---
Changes in v6
- Add the co-developer credit and sign-off trailers.
Changes in v5
- Rebase on the RISC-V KVM queue at ffdc855900a8.
- Keep the existing G-stage initializer and lockless aging path.
- Use an outer PMD-sized walk for memslot and notifier unmaps. Batch invalidations until a possible lock drop or the end of the walk.
- Detach the G-stage root before the recursive VM teardown and allow rescheduling while reclaiming the old page tables.
v4: https://lore.kernel.org/r/[email protected]
arch/riscv/include/asm/kvm_gstage.h | 6 +- arch/riscv/kvm/gstage.c | 77 ++++++++++---------- arch/riscv/kvm/mmu.c | 106 +++++++++++++++++----------- 3 files changed, 106 insertions(+), 83 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +3 -3
@@ -80,10 +80,10 @@ enum kvm_riscv_gstage_op {bool kvm_riscv_gstage_op_pte (struct kvm_gstage *gstage,gpa_t addr,pte_t *ptep,u32 ptep_level,enum kvm_riscv_gstage_op op); -bool kvm_riscv_gstage_unmap_range (struct kvm_gstage *gstage, -gpa_t start,gpa_t size,bool may_block); +s64 kvm_riscv_gstage_unmap_range (struct kvm_gstage *gstage, +gpa_t addr,gpa_t end,gpa_t chunk_end,bool *flush); -bool kvm_riscv_gstage_unmap_all (struct kvm_gstage *gstage); +void kvm_riscv_gstage_unmap_all (struct kvm_gstage *gstage);bool kvm_riscv_gstage_wp_range (struct kvm_gstage *gstage,gpa_t start,gpa_t end); bool kvm_riscv_gstage_age_range(struct kvm_gstage *gstage, gpa_t start,
arch/riscv/kvm/gstage.c +40 -37
126 lines not on this page · raw message
arch/riscv/kvm/mmu.c +63 -43
153 lines not on this page · raw message
base-commit: ffdc855900a894034a63a46d699e0326dea3c991
Signature · 1 line
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: riscv: PMD yield flushes hit hfence_queue and per-page loop --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: riscv: PMD yield flushes hit hfence_queue and per-page loop
--
commit a9cf70d57632be8dd08b712ee49dab08ded702eb
Author: Can Qi <[email protected]>
KVM: riscv: Allow rescheduling during G-stage unmap and teardown
This commit chunks sleepable G-stage unmaps in PMD-sized spans so mmu_lock can be dropped between batches, and detaches the G-stage root before recursively freeing page tables outside mmu_lock.
Quoted 45 lines
arch/riscv/kvm/mmu.c +27
-7@@ -85,20 +85,56 @@ int kvm_riscv_mmu_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa,return ret; } -void kvm_riscv_mmu_iounmap (struct kvm *kvm,gpa_t gpa,unsigned long size) +/* +* Process one PMD-sized span at a time, so base-page mappings cannot +* hold mmu_lock for an entire large memslot when rescheduling is needed. +* Keep one batch of invalidations for as long as the write lock is held. +*/ +static void mmu_unmap_range (struct kvm *kvm,gpa_t addr,gpa_t end,bool may_block) { -struct kvm_gstage gstage; -bool flush; +gfn_t batch_start = addr >>PAGE_SHIFT ; +bool flush = false; -kvm_riscv_gstage_init (&gstage, kvm); +lockdep_assert_held_write (&kvm->mmu_lock); -write_lock (&kvm->mmu_lock); - flush =kvm_riscv_gstage_unmap_range (&gstage, gpa, size, false); -write_unlock (&kvm->mmu_lock); +while (addr < end) { +gpa_t chunk_end = may_block ?min (end,ALIGN (addr +1 ,PMD_SIZE )) : end; +struct kvm_gstage gstage; +gfn_t next_gfn; +s64 next; + +/* Teardown can detach the root while the lock is dropped. */ +kvm_riscv_gstage_init (&gstage, kvm); +if (!gstage.pgd) +break ; + + next =kvm_riscv_gstage_unmap_range (&gstage, addr, end, chunk_end, &flush); +if (WARN_ON_ONCE (next <0 )) +break ; + + addr = next; +if (!may_block || addr == end || + !(need_resched () ||rwlock_needbreak (&kvm->mmu_lock)))
arch/riscv/kvm/mmu.c int kvm_riscv_mmu_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa, · line 127
Quoted 10 lines
arch/riscv/kvm/mmu.c +10
-0+continue ; + +/* A concurrent invalidator must not miss PTEs we have cleared. */ + next_gfn = addr >>PAGE_SHIFT ; +if (flush) +kvm_flush_remote_tlbs_range (kvm, batch_start, next_gfn - batch_start); + flush = false; + batch_start = next_gfn; +cond_resched_rwlock_write (&kvm->mmu_lock); + }
[Severity: Medium]
Can yielding after a single PMD chunk trigger per-page HFENCE.GVMA loops and overflow the vCPU hfence_queue?
When kvm_arch_flush_shadow_memslot() removes a large populated memslot under mmu_lock contention, mmu_unmap_range() can yield after one PMD_SIZE chunk and call kvm_flush_remote_tlbs_range() for PTRS_PER_PTE pages (512 on RV64, 1024 on RV32) while holding write_lock(&kvm->mmu_lock).
kvm_riscv_local_hfence_gvma_vmid_gpa() only falls back to a full-VMID flush when the page count exceeds PTRS_PER_PTE:
arch/riscv/kvm/tlb.c:kvm_riscv_local_hfence_gvma_vmid_gpa() {
...
if (PTRS_PER_PTE < (gpsz >> order)) {
kvm_riscv_local_hfence_gvma_vmid_all(vmid);
return;
}
...
}Because a single PMD spans exactly PTRS_PER_PTE pages, each yield runs 512 (RV64) or 1024 (RV32) per-page HFENCE.GVMA instructions per remote vCPU synchronously under mmu_lock, instead of one full-VMID flush.
Each yield also enqueues an entry in every vCPU's 64-entry hfence_queue. Even after 64 yields overflow into KVM_REQ_TLB_FLUSH, kvm_riscv_check_vcpu_requests() still drains all 64 entries after the full flush, running 32,768 (RV64) or 65,536 (RV32) redundant HFENCE.GVMA calls.
Using kvm_flush_remote_tlbs() before yielding would coalesce into a single KVM_REQ_TLB_FLUSH and avoid both the per-page loop and hfence_queue growth.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1