RISC-V: KVM: Fix guest page fault within HLV* instructions
Subsystems (3)
PATCH 1/1 RISC-V: KVM: Fix guest page fault within HLV* instructions
From: Fangyu Yu <[email protected]>
When executing HLV* instructions at the HS mode, a guest page fault may occur when a g-stage page table migration between triggering the virtual instruction exception and executing the HLV* instruction.
This may be a corner case, and one simpler way to handle this is to re-execute the instruction where the virtual instruction exception occurred, and the guest page fault will be automatically handled.
--- arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
arch/riscv/kvm/vcpu_insn.c +18 -3
@@ -448,7 +448,12 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default : utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);return 1 ;
@@ -503,7 +508,12 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);
@@ -629,7 +639,12 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);
Signature · 1 line
2.49.0
PATCH 1/1 RISC-V: KVM: Fix guest page fault within HLV* instructions
Quoted 49 lines
From: Fangyu Yu <[email protected]>
When executing HLV* instructions at the HS mode, a guest page fault may occur when a g-stage page table migration between triggering the virtual instruction exception and executing the HLV* instruction.
This may be a corner case, and one simpler way to handle this is to re-execute the instruction where the virtual instruction exception occurred, and the guest page fault will be automatically handled.
Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")Signed-off-by Fangyu Yu <[email protected]>---
arch/riscv/kvm/vcpu_insn.c| 21 ++++++++++++++++++---1 file changed, 18 insertions(+), 3 deletions(-)arch/riscv/kvm/vcpu_insn.c +12
-2@@ -448,7 +448,12 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default : utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);return 1 ;@@ -503,7 +508,12 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 645
Quoted 8 lines
arch/riscv/kvm/vcpu_insn.c +4
-1@@ -629,7 +639,12 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT :
Here should be EXC_STORE_GUEST_PAGE_FAULT, I will fix it next version.
Quoted 7 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);Signature · 1 line
2.49.0
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
Quoted 51 lines
Quoted 51 lines
From: Fangyu Yu <[email protected]>
When executing HLV* instructions at the HS mode, a guest page fault may occur when a g-stage page table migration between triggering the virtual instruction exception and executing the HLV* instruction.
This may be a corner case, and one simpler way to handle this is to re-execute the instruction where the virtual instruction exception occurred, and the guest page fault will be automatically handled.
Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")Signed-off-by Fangyu Yu <[email protected]>---
arch/riscv/kvm/vcpu_insn.c| 21 ++++++++++++++++++---1 file changed, 18 insertions(+), 3 deletions(-)arch/riscv/kvm/vcpu_insn.c +12
-2@@ -448,7 +448,12 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default : utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);return 1 ;@@ -503,7 +508,12 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);@@ -629,7 +639,12 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn = kvm_riscv_vcpu_unpriv_read(vcpu, true, ct->sepc,
Quoted 8 lines
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 454
Quoted 6 lines
arch/riscv/kvm/vcpu_insn.c +4
-1&utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ; +case EXC_LOAD_GUEST_PAGE_FAULT :Here should be
EXC_STORE_GUEST_PAGE_FAULT, I will fix it next version.
Please ignore this comment, EXC_LOAD_GUEST_PAGE_FAULT is correct.
Quoted 9 lines
Quoted 8 lines
arch/riscv/kvm/vcpu_insn.c +2
-1+return KVM_INSN_CONTINUE_SAME_SEPC ; +default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap); --2.49.0
Anup Patel
maintainerQuoted 22 lines · On Fri, Sep 12, 2025 at 7:13 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
When executing HLV* instructions at the HS mode, a guest page fault may occur when a g-stage page table migration between triggering the virtual instruction exception and executing the HLV* instruction.
This may be a corner case, and one simpler way to handle this is to re-execute the instruction where the virtual instruction exception occurred, and the guest page fault will be automatically handled.
Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")Signed-off-by Fangyu Yu <[email protected]>--- arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)arch/riscv/kvm/vcpu_insn.c +0
-0
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 453
Quoted 7 lines
arch/riscv/kvm/vcpu_insn.c +3
-1@@ -448,7 +448,12 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;
This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 455
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;
The KVM_INSN_xyz enum values are only for functions called via system_opcode_insn() so return 1 over here just like the below default case.
Also, add some comments over here about why we are simply continuing the guest.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 513
Quoted 12 lines
arch/riscv/kvm/vcpu_insn.c +4
-1+default : utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);return 1 ;@@ -503,7 +508,12 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;
This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 515
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;
Same comment as kvm_riscv_vcpu_virtual_insn().
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 644
Quoted 12 lines
arch/riscv/kvm/vcpu_insn.c +4
-1+default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);@@ -629,7 +639,12 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;
This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 646
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;
Same comment as kvm_riscv_vcpu_virtual_insn().
Quoted 8 lines
arch/riscv/kvm/vcpu_insn.c +1
-1+default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap); --2.49.0
Regards,
Anup
Quoted 93 lines
Quoted 21 lines
From: Fangyu Yu <[email protected]>
When executing HLV* instructions at the HS mode, a guest page fault may occur when a g-stage page table migration between triggering the virtual instruction exception and executing the HLV* instruction.
This may be a corner case, and one simpler way to handle this is to re-execute the instruction where the virtual instruction exception occurred, and the guest page fault will be automatically handled.
Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")Signed-off-by Fangyu Yu <[email protected]>--- arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)arch/riscv/kvm/vcpu_insn.c +0
-0arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 453
Quoted 7 lines
arch/riscv/kvm/vcpu_insn.c +3
-1@@ -448,7 +448,12 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 455
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;The
KVM_INSN_xyzenum values are only for functions called viasystem_opcode_insn()so return 1 over here just like the below default case.Also, add some comments over here about why we are simply continuing the guest.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 513
Quoted 12 lines
arch/riscv/kvm/vcpu_insn.c +4
-1+default : utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);return 1 ;@@ -503,7 +508,12 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 515
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;Same comment as
kvm_riscv_vcpu_virtual_insn().arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 644
Quoted 12 lines
arch/riscv/kvm/vcpu_insn.c +4
-1+default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap);@@ -629,7 +639,12 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,*/ insn =kvm_riscv_vcpu_unpriv_read (vcpu, true, ct->sepc, &utrap); -if (utrap.scause) { +switch (utrap.scause) { +case 0 : +break ;This looks like an unrelated change so drop it or send a separate patch with proper explanation.
arch/riscv/kvm/vcpu_insn.c int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 646
Quoted 2 lines
arch/riscv/kvm/vcpu_insn.c +2
-0+case EXC_LOAD_GUEST_PAGE_FAULT : +return KVM_INSN_CONTINUE_SAME_SEPC ;Same comment as
kvm_riscv_vcpu_virtual_insn().Quoted 8 lines
arch/riscv/kvm/vcpu_insn.c +1
-1+default :/* Redirect trap if we failed to read instruction */ utrap.sepc = ct->sepc;kvm_riscv_vcpu_trap_redirect (vcpu, &utrap); --2.49.0
Quoted 2 lines
Regards,
Anup
Hi Anup:
Thanks for the review.
I will follow your suggestions in the next version.
Thanks,
Fangyu