From: "Bingyu.Xian" <shanbeeyoo@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: Atish Patra <atish.patra@linux.dev>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Quan Zhou <zhouquan@iscas.ac.cn>,
James Houghton <jthoughton@google.com>,
Bingyu Xian <shanbeeyoo@gmail.com>
Subject: [RFC PATCH] RISC-V: KVM: Enable KVM_MEM_USERFAULT support
Date: Wed, 29 Jul 2026 15:08:43 +0800 [thread overview]
Message-ID: <20260729070843.730578-1-shanbeeyoo@gmail.com> (raw)
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 <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Bingyu Xian <shanbeeyoo@gmail.com>
---
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
next reply other threads:[~2026-07-29 7:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 7:08 Bingyu.Xian [this message]
2026-07-29 7:25 ` [RFC PATCH] RISC-V: KVM: Enable KVM_MEM_USERFAULT support sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260729070843.730578-1-shanbeeyoo@gmail.com \
--to=shanbeeyoo@gmail.com \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=atish.patra@linux.dev \
--cc=jthoughton@google.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=zhouquan@iscas.ac.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox