Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU
@ 2026-11-30  8:22 Anup Patel
  2026-11-30  8:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Anup Patel @ 2026-11-30  8:22 UTC (permalink / raw)
  To: Palmer Dabbelt, Paul Walmsley, Atish Patra, Andrew Jones
  Cc: Paolo Bonzini, Anup Patel, kvm, kvm-riscv, linux-riscv,
	linux-kernel, Anup Patel, Yaxing Guo

Currently, KVM RISC-V only sanitize TLB on the next host CPU after
VCPU migration but the VCPU might have left stale TLBs on the previous
host CPU which may affect other VCPUs of the same Guest.

To address the above issue, sanitize TLB on previous host CPU before
the VCPU runs on the next host CPU. This can be achieved by introducing
per-VCPU sanitize TLB mask where each VCPU sanitizes TLB before running
if current host CPU is set in it's sanitize TLB mask. The VCPU must also
kick other VCPU of same Guest after setting previous host CPU in their
sanitize TLB mask.

Co-developed-by: Yaxing Guo <guoyaxing@bosc.ac.cn>
Signed-off-by: Yaxing Guo <guoyaxing@bosc.ac.cn>
Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
---
 arch/riscv/include/asm/kvm_host.h |  4 ++++
 arch/riscv/kvm/tlb.c              | 29 +++++++++++++++++++++++++----
 arch/riscv/kvm/vcpu.c             |  2 ++
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h
index d705b538875d..bdce08ca4b10 100644
--- a/arch/riscv/include/asm/kvm_host.h
+++ b/arch/riscv/include/asm/kvm_host.h
@@ -9,6 +9,7 @@
 #ifndef __RISCV_KVM_HOST_H__
 #define __RISCV_KVM_HOST_H__
 
+#include <linux/cpumask.h>
 #include <linux/types.h>
 #include <linux/kvm.h>
 #include <linux/kvm_types.h>
@@ -186,6 +187,9 @@ struct kvm_vcpu_arch {
 	/* ISA feature bits (similar to MISA) */
 	DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX);
 
+	/* Sanitize TLB host CPU mask */
+	cpumask_t tlb_sanitize_mask;
+
 	/* Vendor, Arch, and Implementation details */
 	unsigned long mvendorid;
 	unsigned long marchid;
diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
index 2ae34632cdcb..cfff35d0173e 100644
--- a/arch/riscv/kvm/tlb.c
+++ b/arch/riscv/kvm/tlb.c
@@ -207,11 +207,32 @@ void kvm_riscv_local_hfence_vvma_all(unsigned long vmid)
 
 void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu)
 {
-	unsigned long vmid;
+	struct kvm_vcpu *tvcpu;
+	unsigned long i, vmid;
 
-	if (!kvm_riscv_gstage_vmid_bits() ||
-	    vcpu->arch.last_exit_cpu == vcpu->cpu)
-		return;
+	if (!cpumask_test_cpu(vcpu->cpu, &vcpu->arch.tlb_sanitize_mask)) {
+		if (!kvm_riscv_gstage_vmid_bits() ||
+		    vcpu->arch.last_exit_cpu == vcpu->cpu)
+			return;
+	}
+
+	/*
+	 * The VCPU might have left stale TLBs on the previous host CPU so
+	 * mark previous host CPU in the tlb_sanitize_mask of other VCPUs
+	 * and force VM-exit for other VCPUs using kvm_vcpu_kick().
+	 */
+	if (vcpu->arch.last_exit_cpu > -1 && vcpu->arch.last_exit_cpu != vcpu->cpu) {
+		kvm_for_each_vcpu(i, tvcpu, vcpu->kvm) {
+			if (tvcpu == vcpu)
+				continue;
+			cpumask_set_cpu(vcpu->arch.last_exit_cpu,
+					&tvcpu->arch.tlb_sanitize_mask);
+			kvm_vcpu_kick(tvcpu);
+		}
+	}
+
+	/* Clear current host CPU from the tlb_sanitize_mask */
+	cpumask_clear_cpu(vcpu->cpu, &vcpu->arch.tlb_sanitize_mask);
 
 	/*
 	 * On RISC-V platforms with hardware VMID support, we share same
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 570b6864e94c..1fe48918f524 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -107,6 +107,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu, bool kvm_sbi_reset)
 
 	vcpu->arch.last_exit_cpu = -1;
 
+	cpumask_clear(&vcpu->arch.tlb_sanitize_mask);
+
 	kvm_riscv_vcpu_context_reset(vcpu, kvm_sbi_reset);
 
 	kvm_riscv_vcpu_fp_reset(vcpu);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  8:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  8:22 [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU Anup Patel
2026-11-30  8:45 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox