When dirty logging is disabled after a G-stage PMD mapping has been split, the fault path may see a 4K G-stage leaf while the backing host page is still THP-backed. The existing kvm_riscv_gstage_map_page() comment says that this path should update the small leaf and leave huge mapping recovery to a later ioctl path.
However, transparent_hugepage_adjust() runs before that G-stage lookup and rewrites the fault GPA to the PMD base. If the original fault is not at the PMD base, the lookup can find and update the wrong 4K leaf. Check the original fault GPA in transparent_hugepage_adjust(). If it already has a 4K G-stage leaf, skip THP adjustment and keep handling the fault at PAGE_SIZE granularity. Signed-off-by: Jinyu Tang <[email protected]> --- arch/riscv/kvm/mmu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index bfd6168ebe30..2fabcd409991 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -500,10 +500,21 @@ static int get_hva_mapping_size(struct kvm *kvm, static unsigned long transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot, + struct kvm_gstage *gstage, unsigned long hva, kvm_pfn_t *hfnp, gpa_t *gpa) { kvm_pfn_t hfn = *hfnp; + u32 ptep_level; + pte_t *ptep; + + /* + * Keep the existing split G-stage leaf and update the original + * faulting 4K page in the vCPU fault path. + */ + if (kvm_riscv_gstage_get_leaf(gstage, *gpa, &ptep, &ptep_level) && + !ptep_level) + return PAGE_SIZE; /* * Make sure the adjustment is done only for THP pages. Also make @@ -730,7 +741,8 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, * so do not promote them through the THP helper. */ if (!logging && !is_hugetlb && vma_pagesize == PAGE_SIZE) - vma_pagesize = transparent_hugepage_adjust(kvm, memslot, hva, &hfn, &gpa); + vma_pagesize = transparent_hugepage_adjust(kvm, memslot, &gstage, + hva, &hfn, &gpa); if (writable) { mark_page_dirty_in_slot(kvm, memslot, gfn); -- 2.43.0

