* [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions
@ 2025-09-12 13:43 fangyu.yu
2025-09-12 14:01 ` fangyu.yu
2025-11-06 6:18 ` Anup Patel
0 siblings, 2 replies; 5+ messages in thread
From: fangyu.yu @ 2025-09-12 13:43 UTC (permalink / raw)
To: anup, atish.patra, paul.walmsley, palmer, aou, alex, pbonzini,
graf, jiangyifei
Cc: guoren, kvm, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
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 <fangyu.yu@linux.alibaba.com>
---
arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 97dec18e6989..a8b93aa4d8ec 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -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);
--
2.49.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions
2025-09-12 13:43 [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions fangyu.yu
@ 2025-09-12 14:01 ` fangyu.yu
2025-09-13 1:24 ` fangyu.yu
2025-11-06 6:18 ` Anup Patel
1 sibling, 1 reply; 5+ messages in thread
From: fangyu.yu @ 2025-09-12 14:01 UTC (permalink / raw)
To: fangyu.yu
Cc: alex, anup, aou, atish.patra, graf, guoren, jiangyifei, kvm-riscv,
kvm, linux-kernel, linux-riscv, palmer, paul.walmsley, pbonzini
>From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
>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 <fangyu.yu@linux.alibaba.com>
>---
> arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++---
> 1 file changed, 18 insertions(+), 3 deletions(-)
>
>diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
>index 97dec18e6989..a8b93aa4d8ec 100644
>--- a/arch/riscv/kvm/vcpu_insn.c
>+++ b/arch/riscv/kvm/vcpu_insn.c
>@@ -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:
Here should be EXC_STORE_GUEST_PAGE_FAULT, I will fix it next version.
>+ 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
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions
2025-09-12 14:01 ` fangyu.yu
@ 2025-09-13 1:24 ` fangyu.yu
0 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2025-09-13 1:24 UTC (permalink / raw)
To: fangyu.yu
Cc: alex, anup, aou, atish.patra, graf, guoren, jiangyifei, kvm-riscv,
kvm, linux-kernel, linux-riscv, palmer, paul.walmsley, pbonzini
>>From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>>
>>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 <fangyu.yu@linux.alibaba.com>
>>---
>> arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++---
>> 1 file changed, 18 insertions(+), 3 deletions(-)
>>
>>diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
>>index 97dec18e6989..a8b93aa4d8ec 100644
>>--- a/arch/riscv/kvm/vcpu_insn.c
>>+++ b/arch/riscv/kvm/vcpu_insn.c
>>@@ -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:
>
>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.
>
>>+ 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
>
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions
2025-09-12 13:43 [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions fangyu.yu
2025-09-12 14:01 ` fangyu.yu
@ 2025-11-06 6:18 ` Anup Patel
2025-11-06 13:29 ` fangyu.yu
1 sibling, 1 reply; 5+ messages in thread
From: Anup Patel @ 2025-11-06 6:18 UTC (permalink / raw)
To: fangyu.yu
Cc: atish.patra, paul.walmsley, palmer, aou, alex, pbonzini, graf,
jiangyifei, guoren, kvm, kvm-riscv, linux-riscv, linux-kernel
On Fri, Sep 12, 2025 at 7:13 PM <fangyu.yu@linux.alibaba.com> wrote:
>
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> 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 <fangyu.yu@linux.alibaba.com>
> ---
> arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++---
> 1 file changed, 18 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
> index 97dec18e6989..a8b93aa4d8ec 100644
> --- a/arch/riscv/kvm/vcpu_insn.c
> +++ b/arch/riscv/kvm/vcpu_insn.c
> @@ -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.
> + 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.
> + 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.
> + case EXC_LOAD_GUEST_PAGE_FAULT:
> + return KVM_INSN_CONTINUE_SAME_SEPC;
Same comment as kvm_riscv_vcpu_virtual_insn().
> + 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.
> + case EXC_LOAD_GUEST_PAGE_FAULT:
> + return KVM_INSN_CONTINUE_SAME_SEPC;
Same comment as kvm_riscv_vcpu_virtual_insn().
> + 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
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions
2025-11-06 6:18 ` Anup Patel
@ 2025-11-06 13:29 ` fangyu.yu
0 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2025-11-06 13:29 UTC (permalink / raw)
To: anup
Cc: alex, aou, atish.patra, fangyu.yu, graf, guoren, jiangyifei,
kvm-riscv, kvm, linux-kernel, linux-riscv, palmer, paul.walmsley,
pbonzini
>> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>>
>> 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 <fangyu.yu@linux.alibaba.com>
>> ---
>> arch/riscv/kvm/vcpu_insn.c | 21 ++++++++++++++++++---
>> 1 file changed, 18 insertions(+), 3 deletions(-)
>>
>> diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
>> index 97dec18e6989..a8b93aa4d8ec 100644
>> --- a/arch/riscv/kvm/vcpu_insn.c
>> +++ b/arch/riscv/kvm/vcpu_insn.c
>> @@ -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.
>
>> + 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.
>
>> + 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.
>
>> + case EXC_LOAD_GUEST_PAGE_FAULT:
>> + return KVM_INSN_CONTINUE_SAME_SEPC;
>
>Same comment as kvm_riscv_vcpu_virtual_insn().
>
>> + 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.
>
>> + case EXC_LOAD_GUEST_PAGE_FAULT:
>> + return KVM_INSN_CONTINUE_SAME_SEPC;
>
>Same comment as kvm_riscv_vcpu_virtual_insn().
>
>> + 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
Hi Anup:
Thanks for the review.
I will follow your suggestions in the next version.
Thanks,
Fangyu
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-11-06 13:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-12 13:43 [PATCH] RISC-V: KVM: Fix guest page fault within HLV* instructions fangyu.yu
2025-09-12 14:01 ` fangyu.yu
2025-09-13 1:24 ` fangyu.yu
2025-11-06 6:18 ` Anup Patel
2025-11-06 13:29 ` fangyu.yu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).