* [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
@ 2025-10-21 8:31 Hui Min Mina Chou
2025-10-21 10:10 ` Radim Krčmář
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Hui Min Mina Chou @ 2025-10-21 8:31 UTC (permalink / raw)
To: anup, atish.patra, pjw, palmer, aou, alex
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, tim609, minachou,
ben717, az70021
From: Hui Min Mina Chou <minachou@andestech.com>
If multiple VCPUs of the same Guest/VM run on the same Host CPU,
hfence.vvma only flushes that Host CPU’s VS-stage TLB. Other Host CPUs
may retain stale VS-stage entries. When a VCPU later migrates to a
different Host CPU, it can hit these stale GVA to GPA mappings, causing
unexpected faults in the Guest.
To fix this, kvm_riscv_gstage_vmid_sanitize() is extended to flush both
G-stage and VS-stage TLBs whenever a VCPU migrates to a different Host CPU.
This ensures that no stale VS-stage mappings remain after VCPU migration.
Fixes: 92e450507d56 ("RISC-V: KVM: Cleanup stale TLB entries when host CPU changes")
Signed-off-by: Hui Min Mina Chou <minachou@andestech.com>
Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
---
Changes in v2:
- Updated Fixes commit to 92e450507d56
- Renamed function to kvm_riscv_local_tlb_sanitize
arch/riscv/kvm/vmid.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c
index 3b426c800480..6323f5383d36 100644
--- a/arch/riscv/kvm/vmid.c
+++ b/arch/riscv/kvm/vmid.c
@@ -125,7 +125,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
kvm_make_request(KVM_REQ_UPDATE_HGATP, v);
}
-void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu)
+void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
{
unsigned long vmid;
@@ -146,4 +146,10 @@ void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu)
vmid = READ_ONCE(vcpu->kvm->arch.vmid.vmid);
kvm_riscv_local_hfence_gvma_vmid_all(vmid);
+
+ /*
+ * Flush VS-stage TLBs entry after VCPU migration to avoid using
+ * stale entries.
+ */
+ kvm_riscv_local_hfence_vvma_all(vmid);
}
--
2.34.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
2025-10-21 8:31 [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries Hui Min Mina Chou
@ 2025-10-21 10:10 ` Radim Krčmář
2025-10-22 20:34 ` kernel test robot
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Radim Krčmář @ 2025-10-21 10:10 UTC (permalink / raw)
To: Hui Min Mina Chou, anup, atish.patra, pjw, palmer, aou, alex
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, tim609, ben717,
az70021, linux-riscv
2025-10-21T16:31:05+08:00, Hui Min Mina Chou <minachou@andestech.com>:
> From: Hui Min Mina Chou <minachou@andestech.com>
>
> If multiple VCPUs of the same Guest/VM run on the same Host CPU,
> hfence.vvma only flushes that Host CPU’s VS-stage TLB. Other Host CPUs
> may retain stale VS-stage entries. When a VCPU later migrates to a
> different Host CPU, it can hit these stale GVA to GPA mappings, causing
> unexpected faults in the Guest.
>
> To fix this, kvm_riscv_gstage_vmid_sanitize() is extended to flush both
> G-stage and VS-stage TLBs whenever a VCPU migrates to a different Host CPU.
> This ensures that no stale VS-stage mappings remain after VCPU migration.
>
> Fixes: 92e450507d56 ("RISC-V: KVM: Cleanup stale TLB entries when host CPU changes")
> Signed-off-by: Hui Min Mina Chou <minachou@andestech.com>
> Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
> ---
The vvma flush is not necessary on implementation that have a single TLB
for the combined mapping, but there is no good way of detecting that,
Reviewed-by: Radim Krčmář <rkrcmar@ventanamicro.com>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
2025-10-21 8:31 [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries Hui Min Mina Chou
2025-10-21 10:10 ` Radim Krčmář
@ 2025-10-22 20:34 ` kernel test robot
2025-10-22 21:27 ` kernel test robot
2025-10-24 3:52 ` Anup Patel
3 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2025-10-22 20:34 UTC (permalink / raw)
To: Hui Min Mina Chou, anup, atish.patra, pjw, palmer, aou, alex
Cc: oe-kbuild-all, kvm, kvm-riscv, linux-riscv, linux-kernel, tim609,
minachou, ben717, az70021
Hi Hui,
kernel test robot noticed the following build warnings:
[auto build test WARNING on kvm/queue]
[also build test WARNING on kvm/next mst-vhost/linux-next linus/master v6.18-rc2 next-20251022]
[cannot apply to kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Min-Mina-Chou/RISC-V-KVM-flush-VS-stage-TLB-after-VCPU-migration-to-prevent-stale-entries/20251021-163357
base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link: https://lore.kernel.org/r/20251021083105.4029305-1-minachou%40andestech.com
patch subject: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
config: riscv-randconfig-r072-20251023 (https://download.01.org/0day-ci/archive/20251023/202510230412.vKIvCmwU-lkp@intel.com/config)
compiler: riscv64-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251023/202510230412.vKIvCmwU-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202510230412.vKIvCmwU-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> arch/riscv/kvm/vmid.c:126:6: warning: no previous prototype for 'kvm_riscv_local_tlb_sanitize' [-Wmissing-prototypes]
126 | void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/kvm_riscv_local_tlb_sanitize +126 arch/riscv/kvm/vmid.c
125
> 126 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
2025-10-21 8:31 [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries Hui Min Mina Chou
2025-10-21 10:10 ` Radim Krčmář
2025-10-22 20:34 ` kernel test robot
@ 2025-10-22 21:27 ` kernel test robot
2025-10-24 3:52 ` Anup Patel
3 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2025-10-22 21:27 UTC (permalink / raw)
To: Hui Min Mina Chou, anup, atish.patra, pjw, palmer, aou, alex
Cc: oe-kbuild-all, kvm, kvm-riscv, linux-riscv, linux-kernel, tim609,
minachou, ben717, az70021
Hi Hui,
kernel test robot noticed the following build warnings:
[auto build test WARNING on kvm/queue]
[also build test WARNING on kvm/next mst-vhost/linux-next linus/master v6.18-rc2 next-20251022]
[cannot apply to kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hui-Min-Mina-Chou/RISC-V-KVM-flush-VS-stage-TLB-after-VCPU-migration-to-prevent-stale-entries/20251021-163357
base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link: https://lore.kernel.org/r/20251021083105.4029305-1-minachou%40andestech.com
patch subject: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
config: riscv-allmodconfig (https://download.01.org/0day-ci/archive/20251023/202510230552.uCekjUFE-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 754ebc6ebb9fb9fbee7aef33478c74ea74949853)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251023/202510230552.uCekjUFE-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202510230552.uCekjUFE-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> arch/riscv/kvm/vmid.c:126:6: warning: no previous prototype for function 'kvm_riscv_local_tlb_sanitize' [-Wmissing-prototypes]
126 | void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
| ^
arch/riscv/kvm/vmid.c:126:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
126 | void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
| ^
| static
1 warning generated.
vim +/kvm_riscv_local_tlb_sanitize +126 arch/riscv/kvm/vmid.c
125
> 126 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
2025-10-21 8:31 [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries Hui Min Mina Chou
` (2 preceding siblings ...)
2025-10-22 21:27 ` kernel test robot
@ 2025-10-24 3:52 ` Anup Patel
2025-10-30 8:44 ` Mina Chou
3 siblings, 1 reply; 6+ messages in thread
From: Anup Patel @ 2025-10-24 3:52 UTC (permalink / raw)
To: Hui Min Mina Chou
Cc: anup, atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv,
linux-riscv, linux-kernel, tim609, ben717, az70021
On Tue, Oct 21, 2025 at 2:02 PM Hui Min Mina Chou
<minachou@andestech.com> wrote:
>
> From: Hui Min Mina Chou <minachou@andestech.com>
>
> If multiple VCPUs of the same Guest/VM run on the same Host CPU,
> hfence.vvma only flushes that Host CPU’s VS-stage TLB. Other Host CPUs
> may retain stale VS-stage entries. When a VCPU later migrates to a
> different Host CPU, it can hit these stale GVA to GPA mappings, causing
> unexpected faults in the Guest.
>
> To fix this, kvm_riscv_gstage_vmid_sanitize() is extended to flush both
> G-stage and VS-stage TLBs whenever a VCPU migrates to a different Host CPU.
> This ensures that no stale VS-stage mappings remain after VCPU migration.
>
> Fixes: 92e450507d56 ("RISC-V: KVM: Cleanup stale TLB entries when host CPU changes")
> Signed-off-by: Hui Min Mina Chou <minachou@andestech.com>
> Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
Here's what the non-normative text says about HFENCE.GVMA ...
"Conceptually, an implementation might contain two address-translation
caches: one that
maps guest virtual addresses to guest physical addresses, and another
that maps guest
physical addresses to supervisor physical addresses. HFENCE.GVMA need
not flush the
former cache, but it must flush entries from the latter cache that
match the HFENCE.GVMA’s
address and VMID arguments."
"More commonly, implementations contain address-translation caches
that map guest virtual
addresses directly to supervisor physical addresses, removing a level
of indirection. For such
implementations, any entry whose guest virtual address maps to a guest
physical address that
matches the HFENCE.GVMA’s address and VMID arguments must be flushed.
Selectively
flushing entries in this fashion requires tagging them with the guest
physical address, which is
costly, and so a common technique is to flush all entries that match
the HFENCE.GVMA’s
VMID argument, regardless of the address argument."
This means ...
For implementations (most common) which have TLBs caching
guest virtual address to supervisor physical address, the
kvm_riscv_local_hfence_gvma_vmid_all() is sufficient upon
VCPU migrating to a different host CPU.
For implementations (relatively uncommon) which have TLBs
caching guest virtual address to guest physical address, the
HFENCE.GVMA will not touch guest virtual address to guest
physical address mapping and KVM must explicitly sanitize
VS-stage mappings using HFENCE.VVMA (like this patch)
when migrating VCPU to a different host CPU.
We should not penalize all implementations by explicitly calling
kvm_riscv_local_hfence_vvma_all() rather this should be only
done on implementations where it is required using a static jump.
One possible way of detecting whether the underlying implementation
needs explicit HFENCE.VVMA upon VCPU is to use marchid,
mimpid, and mvendorid. Another way is to use implementation
specific CPU compatible strings.
Regards,
Anup
> ---
> Changes in v2:
> - Updated Fixes commit to 92e450507d56
> - Renamed function to kvm_riscv_local_tlb_sanitize
>
> arch/riscv/kvm/vmid.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c
> index 3b426c800480..6323f5383d36 100644
> --- a/arch/riscv/kvm/vmid.c
> +++ b/arch/riscv/kvm/vmid.c
> @@ -125,7 +125,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
> kvm_make_request(KVM_REQ_UPDATE_HGATP, v);
> }
>
> -void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu)
> +void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
> {
> unsigned long vmid;
>
> @@ -146,4 +146,10 @@ void kvm_riscv_gstage_vmid_sanitize(struct kvm_vcpu *vcpu)
>
> vmid = READ_ONCE(vcpu->kvm->arch.vmid.vmid);
> kvm_riscv_local_hfence_gvma_vmid_all(vmid);
> +
> + /*
> + * Flush VS-stage TLBs entry after VCPU migration to avoid using
> + * stale entries.
> + */
> + kvm_riscv_local_hfence_vvma_all(vmid);
> }
> --
> 2.34.1
>
>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries
2025-10-24 3:52 ` Anup Patel
@ 2025-10-30 8:44 ` Mina Chou
0 siblings, 0 replies; 6+ messages in thread
From: Mina Chou @ 2025-10-30 8:44 UTC (permalink / raw)
To: Anup Patel
Cc: anup, atish.patra, pjw, palmer, aou, alex, kvm, kvm-riscv,
linux-riscv, linux-kernel, tim609, ben717, az70021
Hi Anup,
>
> Here's what the non-normative text says about HFENCE.GVMA ...
>
> "Conceptually, an implementation might contain two address-translation
> caches: one that
> maps guest virtual addresses to guest physical addresses, and another
> that maps guest
> physical addresses to supervisor physical addresses. HFENCE.GVMA need
> not flush the
> former cache, but it must flush entries from the latter cache that
> match the HFENCE.GVMA???s
> address and VMID arguments."
> "More commonly, implementations contain address-translation caches
> that map guest virtual
> addresses directly to supervisor physical addresses, removing a level
> of indirection. For such
> implementations, any entry whose guest virtual address maps to a guest
> physical address that
> matches the HFENCE.GVMA???s address and VMID arguments must be flushed.
> Selectively
> flushing entries in this fashion requires tagging them with the guest
> physical address, which is
> costly, and so a common technique is to flush all entries that match
> the HFENCE.GVMA???s
> VMID argument, regardless of the address argument."
>
> This means ...
>
> For implementations (most common) which have TLBs caching
> guest virtual address to supervisor physical address, the
> kvm_riscv_local_hfence_gvma_vmid_all() is sufficient upon
> VCPU migrating to a different host CPU.
>
> For implementations (relatively uncommon) which have TLBs
> caching guest virtual address to guest physical address, the
> HFENCE.GVMA will not touch guest virtual address to guest
> physical address mapping and KVM must explicitly sanitize
> VS-stage mappings using HFENCE.VVMA (like this patch)
> when migrating VCPU to a different host CPU.
>
> We should not penalize all implementations by explicitly calling
> kvm_riscv_local_hfence_vvma_all() rather this should be only
> done on implementations where it is required using a static jump.
> One possible way of detecting whether the underlying implementation
> needs explicit HFENCE.VVMA upon VCPU is to use marchid,
> mimpid, and mvendorid. Another way is to use implementation
> specific CPU compatible strings.
>
> Regards,
> Anup
>
>
>
Thanks for the detailed explanation! Our implementation does require the
extra hfence.vvma, so we'll add a check to make sure it only runs on
the platforms that actually need it.
Thanks again for your feedback.
Best regards,
Mina
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-10-30 8:46 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-21 8:31 [PATCH v2] RISC-V: KVM: flush VS-stage TLB after VCPU migration to prevent stale entries Hui Min Mina Chou
2025-10-21 10:10 ` Radim Krčmář
2025-10-22 20:34 ` kernel test robot
2025-10-22 21:27 ` kernel test robot
2025-10-24 3:52 ` Anup Patel
2025-10-30 8:44 ` Mina Chou
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).