From: Anup Patel <anup.patel@oss.qualcomm.com>
To: Palmer Dabbelt <palmer@dabbelt.com>,
Paul Walmsley <pjw@kernel.org>,
Atish Patra <atish.patra@linux.dev>,
Andrew Jones <andrew.jones@linux.dev>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Anup Patel <anup@brainfault.org>,
kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Anup Patel <anup.patel@oss.qualcomm.com>,
Yaxing Guo <guoyaxing@bosc.ac.cn>
Subject: [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU
Date: Mon, 30 Nov 2026 13:52:59 +0530 [thread overview]
Message-ID: <20261130082259.2524917-1-anup.patel@oss.qualcomm.com> (raw)
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
reply other threads:[~2026-11-30 8:23 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130082259.2524917-1-anup.patel@oss.qualcomm.com \
--to=anup.patel@oss.qualcomm.com \
--cc=andrew.jones@linux.dev \
--cc=anup@brainfault.org \
--cc=atish.patra@linux.dev \
--cc=guoyaxing@bosc.ac.cn \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=pjw@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®