Add support for KVM_MEM_USERFAULT on RISC-V, which allows userspace VMMs (e.g. QEMU) to handle post-copy live migration via userfaultfd. The implementation follows the same pattern as x86 and arm64: 1. Force PAGE_SIZE mappings when KVM_MEM_USERFAULT is set on a memslot. The userfault bitmap is tracked at PAGE_SIZE granularity, so block mappings must be disabled to ensure every guest page fault reaches the bitmap check in gstage_page_fault(). 2. Check the userfault bitmap in gstage_page_fault() before installing a G-stage mapping. If the faulting gfn is marked as pending userspace resolution, prepare a KVM_EXIT_MEMORY_FAULT exit and return 0 instead of proceeding with the mapping. 3. Advertise KVM_CAP_USERFAULT capability to userspace. The mmu_notifier invalidation flush on userfault enable guarantees that we never fault on a page that's already mapped, so the bitmap set bits can be safely checked without holding the mmu_lock during the lookup. Builds on the gstage fault path type cleanup ("RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types"), which is sent separately as an independent fix. Depends-on: James Houghton's KVM_MEM_USERFAULT generic series Link: https://lore.kernel.org/kvm/20260618205753.2600064-1-jthoughton@google.com/ Assisted-by: YuanSheng: deepseek-v4-pro Co-developed-by: Quan Zhou Signed-off-by: Quan Zhou Signed-off-by: Bingyu Xian --- arch/riscv/kvm/mmu.c | 6 ++++-- arch/riscv/kvm/vcpu_exit.c | 7 +++++++ arch/riscv/kvm/vm.c | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index d2a06a54be17..c33810b71d89 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -583,7 +583,9 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, else vma_pageshift = PAGE_SHIFT; vma_pagesize = 1ULL << vma_pageshift; - if (logging || (vma->vm_flags & VM_PFNMAP)) + bool userfault = !!(memslot->flags & KVM_MEM_USERFAULT); + + if (logging || userfault || (vma->vm_flags & VM_PFNMAP)) vma_pagesize = PAGE_SIZE; else if (is_hugetlb) vma_pagesize = hugetlb_mapping_size(memslot, hva, vma_pagesize); @@ -640,7 +642,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, * possible. Hugetlb mappings already selected their target size above, * so do not promote them through the THP helper. */ - if (!logging && !is_hugetlb && vma_pagesize == PAGE_SIZE) + if (!logging && !userfault && !is_hugetlb && vma_pagesize == PAGE_SIZE) vma_pagesize = transparent_hugepage_adjust(kvm, memslot, hva, &hfn, &gpa); if (writable) { diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 76cceecee5ab..0919e1c8a6ad 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -63,6 +63,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, }; } + if (kvm_slot_userfault_fault(memslot, gfn)) { + kvm_prepare_memory_fault_exit(vcpu, fault_addr, PAGE_SIZE, + trap->scause == EXC_STORE_GUEST_PAGE_FAULT, + trap->scause == EXC_INST_GUEST_PAGE_FAULT, false); + return 0; + } + ret = kvm_riscv_mmu_map(vcpu, memslot, fault_addr, hva, (trap->scause == EXC_STORE_GUEST_PAGE_FAULT) ? true : false, &host_map); diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index a9f083feeb76..1bb3918c70da 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_READONLY_MEM: case KVM_CAP_MP_STATE: case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_USERFAULT: case KVM_CAP_SET_GUEST_DEBUG: r = 1; break; -- 2.54.0