[PATCH v3 1/3] RISC-V: KVM: Treat -EEXIST from G-stage map as success

Anup Patel anup at brainfault.org
Wed Jul 29 09:06:14 PDT 2026


On Wed, Jul 29, 2026 at 5:37 PM Bingyu.Xian <shanbeeyoo at gmail.com> wrote:
>
> When a concurrent vCPU installs the same G-stage mapping while we are
> waiting for mmu_lock, kvm_riscv_gstage_map_page() returns -EEXIST.
> This is not an error -- the page is correctly mapped and the faulting
> vCPU can simply retry the guest instruction -- but KVM was treating it
> as one: printing "Failed to map in G-stage" to dmesg and propagating
> -EEXIST all the way to userspace.

The kvm_riscv_gstage_map_page() return -EEXIST only when a leaf-PTE
of higher-order already exists. It does not return error when leaf-PTE at
same level already exists so this is an invalid fix.

Regards,
Anup

>
> Align RISC-V with x86 and arm64, which already swallow -EEXIST in
> their respective fault handlers.  This also lets kvm_release_faultin_page()
> drop its "ret && ret != -EEXIST" special case: with ret normalized to 0
> the regular release path is correct.
>
> Fixes: 9d05c1fee837 ("RISC-V: KVM: Implement stage2 page table programming")
> Cc: stable at vger.kernel.org
> Assisted-by: YuanSheng: deepseek-v4-pro
> Co-developed-by: Quan Zhou <zhouquan at iscas.ac.cn>
> Signed-off-by: Quan Zhou <zhouquan at iscas.ac.cn>
> Signed-off-by: Bingyu Xian <shanbeeyoo at gmail.com>
> ---
>  arch/riscv/kvm/mmu.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
> index 8a0aa5e0e216..943d8dc72105 100644
> --- a/arch/riscv/kvm/mmu.c
> +++ b/arch/riscv/kvm/mmu.c
> @@ -652,11 +652,13 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
>                                                 vma_pagesize, true, true, out_map);
>         }
>
> -       if (ret)
> +       if (ret == -EEXIST)
> +               ret = 0;
> +       else if (ret)
>                 kvm_err("Failed to map in G-stage\n");
>
>  out_unlock:
> -       kvm_release_faultin_page(kvm, page, ret && ret != -EEXIST, writable);
> +       kvm_release_faultin_page(kvm, page, ret, writable);
>         write_unlock(&kvm->mmu_lock);
>         return ret;
>  }
> --
> 2.54.0
>



More information about the linux-riscv mailing list