RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types
Subsystems (4)
PATCH 1/1 RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types
Two small fixes to the RISC-V G-stage page fault path, both suggested during review of the in-progress KVM Userfault port.
1. Treat -EEXIST from kvm_riscv_gstage_map_page() as a quiet success.
When a concurrent vCPU installs the same G-stage mapping while we are waiting for mmu_lock, 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.
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.
2. Clean up fault path types.
- vma_pageshift: short -> unsigned int. Bit widths and shift counts conventionally use unsigned int in the kernel.
- fault_addr: unsigned long -> gpa_t. RV32 with Sv32x4 has 34-bit guest physical addresses that would be truncated when stored in a 32-bit unsigned long; gpa_t is u64 on RV32 and holds the full address. No functional change on RV64.
These type changes are in preparation for sharing a common struct kvm_page_fault across architectures, as requested during review.
No functional change on RV64 beyond silencing the spurious -EEXIST.
These are independent fixes with no dependencies; they can be merged on their own. The KVM_MEM_USERFAULT port that motivated them will be sent separately as an RFC once the generic userfault series lands.
--- arch/riscv/kvm/mmu.c | 8 +++++--- arch/riscv/kvm/vcpu_exit.c | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/riscv/kvm/mmu.c +5 -3
@@ -541,7 +541,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,kvm_pfn_t hfn;bool is_hugetlb;bool writable; -short vma_pageshift; +unsigned int vma_pageshift;gfn_t gfn = gpa >>PAGE_SHIFT ;struct vm_area_struct *vma;struct kvm *kvm = vcpu->kvm;
@@ -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; }
arch/riscv/kvm/vcpu_exit.c +2 -1
@@ -17,7 +17,8 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret;
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fails to resolve the 34-bit GPA truncation on RV32 because the address calculation (trap->htval << 2) is still evaluated using 32-bit arithmetic before assignment. --
--- Patch [1]: [PATCH] RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types --- commit 9da8ae12634bbc92cd912d48c6458250997cb454
Author: Bingyu.Xian <[email protected]>
RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types
This commit updates the G-stage page fault path to treat -EEXIST from kvm_riscv_gstage_map_page() as a success, aligning RISC-V with other architectures. It also updates the type of vma_pageshift to unsigned int and fault_addr to gpa_t, intending to prevent 34-bit guest physical address truncation on RV32.
[ ... ]
arch/riscv/kvm/vcpu_exit.c static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 21
Quoted 11 lines
arch/riscv/kvm/vcpu_exit.c +2
-1@@ -17,7 +17,8 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;
[Severity: High]
This is a pre-existing issue, but does the calculation for fault_addr still truncate the address on RV32 before this assignment?
The commit message notes this type change is meant to prevent 34-bit guest physical addresses from being truncated. However, looking further down in gstage_page_fault(), the actual calculation is:
fault_addr = (trap->htval << 2) | (trap->stval & 0x3);
Because trap->htval is an unsigned long, the shift (trap->htval << 2) is evaluated as a 32-bit operation on RV32. This discards the upper bits of the 34-bit guest physical address before the final result is widened and stored in the new 64-bit gpa_t variable.
Should trap->htval be explicitly cast to gpa_t or u64 before the shift operation to ensure the higher bits are preserved?
Quoted 3 lines
arch/riscv/kvm/vcpu_exit.c +0
-0bool writable;gfn_t gfn;int ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types
In reply to Bingyu.XianTwo small fixes to the RISC-V G-stage page fault path, both suggested during review of the in-progress KVM Userfault port.
1. Treat -EEXIST from kvm_riscv_gstage_map_page() as a quiet success.
When a concurrent vCPU installs the same G-stage mapping while we are waiting for mmu_lock, 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.
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.
2. Clean up fault path types.
- vma_pageshift: short -> unsigned int. Bit widths and shift counts conventionally use unsigned int in the kernel.
- fault_addr: unsigned long -> gpa_t. RV32 with Sv32x4 has 34-bit guest physical addresses. fault_addr is widened to gpa_t (u64) to hold the full address, and the shift reconstructing it, (trap->htval << 2), is cast to gpa_t before the shift: htval is unsigned long, so on RV32 the shift would otherwise be evaluated in 32-bit arithmetic and drop bits 32/33 before the result is widened. No functional change on RV64.
These type changes are in preparation for sharing a common struct kvm_page_fault across architectures, as requested during review.
No functional change on RV64 beyond silencing the spurious -EEXIST.
These are independent fixes with no dependencies; they can be merged on their own. The KVM_MEM_USERFAULT port that motivated them will be sent separately as an RFC once the generic userfault series lands.
---
Changes since v1:
- Cast trap->htval to gpa_t before the <<2 shift so the 34-bit guest physical address is not truncated by 32-bit arithmetic on RV32. v1 only widened the destination (fault_addr -> gpa_t); the shift itself still dropped bits 32/33 before the result was widened.
arch/riscv/kvm/mmu.c | 8 +++++--- arch/riscv/kvm/vcpu_exit.c | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-)
arch/riscv/kvm/mmu.c +5 -3
@@ -541,7 +541,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,kvm_pfn_t hfn;bool is_hugetlb;bool writable; -short vma_pageshift; +unsigned int vma_pageshift;gfn_t gfn = gpa >>PAGE_SHIFT ;struct vm_area_struct *vma;struct kvm *kvm = vcpu->kvm;
@@ -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; }
arch/riscv/kvm/vcpu_exit.c +3 -2
@@ -17,12 +17,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret; - fault_addr = (trap->htval <<2 ) | (trap->stval &0x3 ); + fault_addr = ((gpa_t )trap->htval <<2 ) | (trap->stval &0x3 ); gfn = fault_addr >>PAGE_SHIFT ; memslot =gfn_to_memslot (vcpu->kvm, gfn); hva =gfn_to_hva_memslot_prot (memslot, gfn, &writable);
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The widened 34-bit fault_addr on RV32 is silently truncated back to 32 bits when passed to MMIO load/store handlers. --
--- Patch [1]: [PATCH v2] RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types --- commit 0f1ae40b2c79a54468668b54d25a4ee2ead28522
Author: Bingyu.Xian <[email protected]>
RISC-V: KVM: Fix spurious -EEXIST and clean up gstage fault path types
This commit treats -EEXIST from kvm_riscv_gstage_map_page() as a quiet success, aligning RISC-V with x86 and arm64. It also widens fault_addr to gpa_t to prevent 34-bit guest physical addresses from being truncated by 32-bit arithmetic on RV32.
Quoted 7 lines
arch/riscv/kvm/vcpu_exit.c +0
-0@@ -17,12 +17,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;
arch/riscv/kvm/vcpu_exit.c static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, · line 26
Quoted 10 lines
arch/riscv/kvm/vcpu_exit.c +3
-2struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret; - fault_addr = (trap->htval <<2 ) | (trap->stval &0x3 ); + fault_addr = ((gpa_t )trap->htval <<2 ) | (trap->stval &0x3 );
[Severity: High]
This is a pre-existing issue, but since the patch explicitly widens fault_addr to gpa_t to hold 34-bit addresses on RV32, will this address be truncated back to 32 bits when passed to the MMIO handlers later in this function?
Looking at arch/riscv/kvm/vcpu_insn.c, the handlers still expect an unsigned long:
int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,
unsigned long fault_addr, unsigned long htinst)
and
int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
unsigned long fault_addr, unsigned long htinst)
Does this mean MMIO accesses to addresses above 4GB on RV32 will still be silently aliased into the lower 4GB when gstage_page_fault() calls these handlers?
Quoted 3 lines
arch/riscv/kvm/vcpu_exit.c +0
-0gfn = fault_addr >>PAGE_SHIFT ; memslot =gfn_to_memslot (vcpu->kvm, gfn); hva =gfn_to_hva_memslot_prot (memslot, gfn, &writable);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
v3 cover RISC-V: KVM: G-stage fault path fix and cleanup
In reply to Bingyu.XianThree small changes to the RISC-V G-stage page fault path, sent as separate patches per review feedback.
1/3: Treat -EEXIST from kvm_riscv_gstage_map_page() as a quiet
success, aligning RISC-V with x86 and arm64. Real bug fix;
Fixes: 9d05c1fee837, Cc: stable. 2/3: vma_pageshift: short -> unsigned int. No-functional-change
type cleanup in preparation for sharing a common struct kvm_page_fault across architectures.
3/3: Widen the G-stage fault address to gpa_t end to end -- the
local in gstage_page_fault(), the (gpa_t) cast before the <<2 shift, and the fault_addr parameters of kvm_riscv_vcpu_mmio_load/ store(). This completes the RV32 34-bit GPA fix that v2 only
half-addressed: v2 widened the local and cast the shift, but the address was still narrowed to 32 bits at the MMIO handler call (unsigned long parameter), aliasing accesses above 4 GB into the low 4 GB. The handlers' internal uses (run->mmio.phys_addr is __u64, kvm_io_bus_read/write() take gpa_t) are already 64-bit. Also in preparation for sharing a common struct kvm_page_fault. No functional change on RV64. Not Cc'd to stable: RV32 KVM is unsupported and this is untested at runtime.
Changes since v2 [1]:
- Split into separate patches per feedback.
- Completed the fault_addr widening v2 left half-done: widened the kvm_riscv_vcpu_mmio_load/store() fault_addr parameters to gpa_t so the 34-bit address is not truncated at the MMIO handler call.
The KVM_MEM_USERFAULT port that motivated this cleanup will follow as a separate RFC.
[1] https://lore.kernel.org/kvm/[email protected]/
Bingyu.Xian (3):
RISC-V: KVM: Treat -EEXIST from G-stage map as success
RISC-V: KVM: Use unsigned int for vma_pageshift
RISC-V: KVM: Widen G-stage fault address to gpa_t
arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++-- arch/riscv/kvm/mmu.c | 8 +++++--- arch/riscv/kvm/vcpu_exit.c | 5 +++-- arch/riscv/kvm/vcpu_insn.c | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
PATCH v3 1/3 RISC-V: KVM: Treat -EEXIST from G-stage map as success
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.
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.
--- arch/riscv/kvm/mmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/riscv/kvm/mmu.c +4 -2
@@ -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; }
Signature · 1 line
2.54.0
Anup Patel
maintainerRe: [PATCH v3 1/3] RISC-V: KVM: Treat -EEXIST from G-stage map as success
Quoted 7 lines · Bingyu.Xian
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-EEXISTall 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
Quoted 39 lines
Align RISC-V with x86 and arm64, which already swallow
-EEXISTin their respective fault handlers. This also letskvm_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")Assisted-by YuanSheng: deepseek-v4-proCo-developed-by Quan Zhou <[email protected]>Signed-off-by Quan Zhou <[email protected]>Signed-off-by Bingyu Xian <[email protected]>--- arch/riscv/kvm/mmu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)arch/riscv/kvm/mmu.c +4
-3@@ -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
PATCH v3 2/3 RISC-V: KVM: Use unsigned int for vma_pageshift
In reply to Bingyu.Xianvma_pageshift in kvm_riscv_mmu_map() was declared short. Use unsigned int, the conventional kernel type for bit widths and shift counts, in preparation for sharing a common struct kvm_page_fault across architectures. No functional change.
--- arch/riscv/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kvm/mmu.c +1 -1
@@ -541,7 +541,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,kvm_pfn_t hfn;bool is_hugetlb;bool writable; -short vma_pageshift; +unsigned int vma_pageshift;gfn_t gfn = gpa >>PAGE_SHIFT ;struct vm_area_struct *vma;struct kvm *kvm = vcpu->kvm;
Signature · 1 line
2.54.0
Re: [PATCH v3 2/3] RISC-V: KVM: Use unsigned int for vma_pageshift
Quoted 10 lines · Bingyu.Xian
vma_pageshiftinkvm_riscv_mmu_map()was declared short. Use unsigned int, the conventional kernel type for bit widths and shift counts, in preparation for sharing a common structkvm_page_faultacross architectures. No functional change.Assisted-by YuanSheng: deepseek-v4-proCo-developed-by Quan Zhou <[email protected]>Signed-off-by Quan Zhou <[email protected]>Signed-off-by Bingyu Xian <[email protected]>
LGTM.
Queued this patch for Linux-7.3
Thanks,
Anup
Quoted 20 lines
--- arch/riscv/kvm/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kvm/mmu.c +1
-2@@ -541,7 +541,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,kvm_pfn_t hfn;bool is_hugetlb;bool writable; -short vma_pageshift; +unsigned int vma_pageshift;gfn_t gfn = gpa >>PAGE_SHIFT ;struct vm_area_struct *vma;struct kvm *kvm = vcpu->kvm; --2.54.0
PATCH v3 3/3 RISC-V: KVM: Widen G-stage fault address to gpa_t
In reply to Bingyu.Xianfault_addr in gstage_page_fault() and the fault_addr parameter of kvm_riscv_vcpu_mmio_load/store() are unsigned long. On RV32 with Sv32x4, guest physical addresses are 34 bits, so a 32-bit unsigned long truncates bits 32/33 at two points:
- reconstruction: fault_addr = (trap->htval << 2) | ... is evaluated
in 32-bit arithmetic, dropping bits 32/33 before widening;
- the MMIO handler call: even with the local widened, the handler's unsigned long parameter narrows it back to 32 bits, aliasing accesses above 4 GB into the low 4 GB.
Widen fault_addr to gpa_t end to end: the local in gstage_page_fault(), the (gpa_t) cast before the <<2 shift, and the fault_addr parameters of kvm_riscv_vcpu_mmio_load/store(). The handlers' internal uses (run->mmio.phys_addr is __u64, kvm_io_bus_read/write() take gpa_t) are already 64-bit, so no further changes are needed.
Also in preparation for sharing a common struct kvm_page_fault across architectures, where fault_addr is gpa_t. No functional change on RV64.
--- arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++-- arch/riscv/kvm/vcpu_exit.c | 5 +++-- arch/riscv/kvm/vcpu_insn.c | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-)
arch/riscv/include/asm/kvm_vcpu_insn.h +2 -2
@@ -38,10 +38,10 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_cpu_trap *trap);int kvm_riscv_vcpu_mmio_load (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst);int kvm_riscv_vcpu_mmio_store (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst);int kvm_riscv_vcpu_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);
arch/riscv/kvm/vcpu_exit.c +3 -2
@@ -17,12 +17,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret; - fault_addr = (trap->htval <<2 ) | (trap->stval &0x3 ); + fault_addr = ((gpa_t )trap->htval <<2 ) | (trap->stval &0x3 ); gfn = fault_addr >>PAGE_SHIFT ; memslot =gfn_to_memslot (vcpu->kvm, gfn); hva =gfn_to_hva_memslot_prot (memslot, gfn, &writable);
arch/riscv/kvm/vcpu_insn.c +2 -2
@@ -371,7 +371,7 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,* Returns < 0 to report failure and exit run-loop */ int kvm_riscv_vcpu_mmio_load (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst) {u8 data_buf[8 ];
@@ -494,7 +494,7 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,* Returns < 0 to report failure and exit run-loop */ int kvm_riscv_vcpu_mmio_store (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst) {u8 data8;
Signature · 1 line
2.54.0
Re: [PATCH v3 3/3] RISC-V: KVM: Widen G-stage fault address to gpa_t
Quoted 18 lines · Bingyu.Xian
fault_addringstage_page_fault()and thefault_addrparameter of kvm_riscv_vcpu_mmio_load/store() are unsigned long. On RV32 with Sv32x4, guest physical addresses are 34 bits, so a 32-bit unsigned long truncates bits 32/33 at two points:- reconstruction:
fault_addr= (trap->htval<< 2) | ... is evaluatedin 32-bit arithmetic, dropping bits 32/33 before widening;- the MMIO handler call: even with the local widened, the handler's unsigned long parameter narrows it back to 32 bits, aliasing accesses above 4 GB into the low 4 GB.
Widen
fault_addrtogpa_tend to end: the local ingstage_page_fault(), the (gpa_t) cast before the <<2 shift, and thefault_addrparameters of kvm_riscv_vcpu_mmio_load/store(). The handlers' internal uses (run->mmio.phys_addris__u64, kvm_io_bus_read/write() takegpa_t) are already 64-bit, so no further changes are needed.
Quoted 8 lines
Also in preparation for sharing a common struct
kvm_page_faultacross architectures, wherefault_addrisgpa_t. No functional change on RV64.Fixes 9d05c1fee837 ("RISC-V: KVM: Implement stage2 page table programming")Assisted-by YuanSheng: deepseek-v4-proCo-developed-by Quan Zhou <[email protected]>Signed-off-by Quan Zhou <[email protected]>Signed-off-by Bingyu Xian <[email protected]>
LGTM.
Queued this patch for Linux-7.3
Thanks,
Anup
Quoted 68 lines
--- arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++-- arch/riscv/kvm/vcpu_exit.c | 5 +++-- arch/riscv/kvm/vcpu_insn.c | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-)arch/riscv/include/asm/kvm_vcpu_insn.h +2
-2@@ -38,10 +38,10 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_cpu_trap *trap);int kvm_riscv_vcpu_mmio_load (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst);int kvm_riscv_vcpu_mmio_store (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst);int kvm_riscv_vcpu_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);arch/riscv/kvm/vcpu_exit.c +3
-2@@ -17,12 +17,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret; - fault_addr = (trap->htval <<2 ) | (trap->stval &0x3 ); + fault_addr = ((gpa_t )trap->htval <<2 ) | (trap->stval &0x3 ); gfn = fault_addr >>PAGE_SHIFT ; memslot =gfn_to_memslot (vcpu->kvm, gfn); hva =gfn_to_hva_memslot_prot (memslot, gfn, &writable);arch/riscv/kvm/vcpu_insn.c +2
-3@@ -371,7 +371,7 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,* Returns < 0 to report failure and exit run-loop */ int kvm_riscv_vcpu_mmio_load (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst) {u8 data_buf[8 ];@@ -494,7 +494,7 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run,* Returns < 0 to report failure and exit run-loop */ int kvm_riscv_vcpu_mmio_store (struct kvm_vcpu *vcpu,struct kvm_run *run, -unsigned long fault_addr, +gpa_t fault_addr,unsigned long htinst) {u8 data8; --2.54.0
Anup Patel
maintainerQuoted 20 lines · Bingyu.Xian
Two small fixes to the RISC-V G-stage page fault path, both suggested during review of the in-progress KVM Userfault port.
1. Treat
-EEXISTfromkvm_riscv_gstage_map_page()as a quiet success.When a concurrent vCPU installs the same G-stage mapping while we are waiting for
mmu_lock,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-EEXISTall the way to userspace.Align RISC-V with x86 and arm64, which already swallow
-EEXISTin their respective fault handlers. This also letskvm_release_faultin_page()drop its "ret && ret !=-EEXIST" special
case: with ret normalized to 0 the regular release path is correct.2. Clean up fault path types.
Quoted 7 lines
-
vma_pageshift: short -> unsigned int. Bit widths and shift counts conventionally use unsigned int in the kernel.-
fault_addr: unsigned long ->gpa_t. RV32 with Sv32x4 has 34-bit guest physical addresses that would be truncated when stored in a 32-bit unsigned long;gpa_tis u64 on RV32 and holds the full address. No functional change on RV64.
Don't squash multiple changes into one patch.
Create separate patches with appropriate Fixes tags.
Regards,
Anup
Quoted 67 lines
These type changes are in preparation for sharing a common struct
kvm_page_faultacross architectures, as requested during review.No functional change on RV64 beyond silencing the spurious
-EEXIST.These are independent fixes with no dependencies; they can be merged on their own. The
KVM_MEM_USERFAULTport that motivated them will be sent separately as an RFC once the generic userfault series lands.Fixes 9d05c1fee837 ("RISC-V: KVM: Implement stage2 page table programming")Assisted-by YuanSheng: deepseek-v4-proCo-developed-by Quan Zhou <[email protected]>Signed-off-by Quan Zhou <[email protected]>Signed-off-by Bingyu Xian <[email protected]>--- arch/riscv/kvm/mmu.c | 8 +++++--- arch/riscv/kvm/vcpu_exit.c | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-)arch/riscv/kvm/mmu.c +5
-3@@ -541,7 +541,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,kvm_pfn_t hfn;bool is_hugetlb;bool writable; -short vma_pageshift; +unsigned int vma_pageshift;gfn_t gfn = gpa >>PAGE_SHIFT ;struct vm_area_struct *vma;struct kvm *kvm = vcpu->kvm;@@ -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; }arch/riscv/kvm/vcpu_exit.c +2
-2@@ -17,7 +17,8 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, {struct kvm_gstage_mapping host_map;struct kvm_memory_slot *memslot; -unsigned long hva, fault_addr; +unsigned long hva; +gpa_t fault_addr;bool writable;gfn_t gfn;int ret; --2.54.0