To support subsequent page table reclamation, introduce a recursive G-stage page table walker, similar to the arm64 stage-2 page table walker. Walk adjacent PTEs directly within each page table instead of repeatedly walking from the root. Initial tests with memslot_perf_test under QEMU TCG showed a 6-10% increase in single-page unmap time with the recursive walker. Retaining the existing leaf lookup path in preliminary tests brought the unmap time close to the baseline. Since single-page unmapping does not benefit from walking adjacent PTEs, handle it separately and move the recursive walk into a separate function. Unwind the recursive walk before dropping mmu_lock for rescheduling, as page table pointers may become invalid while the lock is released. Resume the walk from the updated address after reacquiring the lock. Signed-off-by: Tan Chi --- arch/riscv/kvm/gstage.c | 119 ++++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 24 deletions(-) diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c index f4ed659b11e9..dca3cd1c35e4 100644 --- a/arch/riscv/kvm/gstage.c +++ b/arch/riscv/kvm/gstage.c @@ -437,48 +437,119 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr, return flush; } -bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage, - gpa_t start, gpa_t size, bool may_block) +/* + * Walk the G-stage page tables recursively to unmap the given range. + * Walk adjacent PTEs directly within each table to avoid repeated + * lookups from the root page table. + * + * Return: + * 0 - The walk completed. + * -EAGAIN - Rescheduling is needed. + * Other negative values indicate errors. + */ +static int gstage_unmap_walk(struct kvm_gstage *gstage, + pte_t *table, u32 level, + gpa_t *addr, gpa_t end, + bool may_block, bool *flush) { - int ret; - pte_t *ptep; - u32 ptep_level; - bool found_leaf; unsigned long page_size; - gpa_t addr = start, end = start + size; - bool flush = false; + pte_t *ptep, *child; + gpa_t child_end; + pte_t pte; + int ret; - while (addr < end) { - found_leaf = kvm_riscv_gstage_get_leaf(gstage, addr, &ptep, &ptep_level); - ret = gstage_level_to_page_size(gstage, ptep_level, &page_size); - if (ret) - break; + ret = gstage_level_to_page_size(gstage, level, &page_size); + if (ret) + return ret; - if (!found_leaf) { - addr = ALIGN(addr + 1, page_size); - } else { - if (!(addr & (page_size - 1)) && ((end - addr) >= page_size)) - flush |= kvm_riscv_gstage_op_pte(gstage, addr, ptep, - ptep_level, GSTAGE_OP_CLEAR); - else { + ptep = &table[gstage_pte_index(gstage, *addr, level)]; + + for (; *addr < end; ptep++) { + pte = ptep_get(ptep); + + if (!pte_val(pte)) { + *addr = ALIGN(*addr + 1, page_size); + } else if (gstage_pte_leaf(pte)) { + if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) { + *flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep, + level, GSTAGE_OP_CLEAR); + } else { WARN_ONCE(1, "Skip unmap range addr: %#llx, end: %#llx, page_size: %#lx\n", - addr, end, page_size); + *addr, end, page_size); } - addr += page_size; + *addr = ALIGN(*addr + 1, page_size); + } else if (level) { + child = (pte_t *)gstage_pte_page_vaddr(pte); + child_end = min(end, ALIGN(*addr + 1, page_size)); + ret = gstage_unmap_walk(gstage, child, level - 1, + addr, child_end, may_block, flush); + if (ret < 0) + return ret; + } else { + *addr = ALIGN(*addr + 1, page_size); } + /* + * Unwind the walk before dropping mmu_lock, as the + * page table pointers may become invalid while the lock is released. + */ + if (*addr < end && may_block && + (need_resched() || + rwlock_needbreak(&gstage->kvm->mmu_lock))) + return -EAGAIN; + } + + return 0; +} + +static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gstage, + gpa_t start, gpa_t size, bool may_block) +{ + int ret; + gpa_t addr = start, end = start + size; + bool flush = false; + + if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) + may_block = false; + + while (addr < end) { + ret = gstage_unmap_walk(gstage, (pte_t *)gstage->pgd, + gstage->pgd_levels - 1, + &addr, end, may_block, &flush); + + if (ret != -EAGAIN || addr >= end) + break; + /* * If the range is too large, release the kvm->mmu_lock * to prevent starvation and lockup detector warnings. */ - if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < end) - cond_resched_rwlock_write(&gstage->kvm->mmu_lock); + cond_resched_rwlock_write(&gstage->kvm->mmu_lock); } return flush; } +bool kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage, + gpa_t start, gpa_t size, bool may_block) +{ + pte_t *ptep; + u32 level; + + if (size == PAGE_SIZE && IS_ALIGNED(start, PAGE_SIZE)) { + if (!kvm_riscv_gstage_get_leaf(gstage, start, &ptep, &level)) + return false; + + if (!level) + return kvm_riscv_gstage_op_pte(gstage, start, ptep, + level, GSTAGE_OP_CLEAR); + } + + return gstage_unmap_range_recursive(gstage, start, size, + may_block); +} + /* * Clear every G-stage mapping and free all page tables below the PGD. * kvm_riscv_gstage_unmap_range() only clears leaf PTEs and leaves the -- 2.53.0