* [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
* Re: [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 8:45 UTC (permalink / raw)
To: Anup Patel; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] RISC-V: KVM: unconditional kvm_vcpu_kick() in
kvm_riscv_local_tlb_sanitize()
- [High] RISC-V: KVM: deferred and async tlb_sanitize_mask update in
kvm_riscv_local_tlb_sanitize()
- [Medium] RISC-V: KVM: kvm_riscv_reset_vcpu() clears last_exit_cpu
before TLB sanitize
- [Medium] RISC-V: KVM: HFENCE requests consumed before
kvm_riscv_local_tlb_sanitize()
--
commit fb6737229a46ed1835b0561db70c3203e3250bc6
Author: Anup Patel <anup.patel@oss.qualcomm.com>
RISC-V: KVM: Sanitize TLB on both previous and next host CPU
This commit adds a per-vCPU cpumask to track host CPUs that need TLB
sanitization after a vCPU migrates, kicking sibling vCPUs when setting
their masks.
> diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
> index 2ae34632cdcb1..cfff35d0173ee 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;
> + }
[Severity: High]
Can a vCPU still run with stale TLB entries left by a sibling vCPU on the
same host CPU when the sibling has not yet entered guest mode on another
CPU?
tlb_sanitize_mask is only updated when the migrating vCPU reaches
kvm_riscv_local_tlb_sanitize() on a new host CPU:
CPU0 CPU1
vcpu1 runs in guest mode
vcpu1 exits (last_exit_cpu = 0)
vcpu1 schedules out
vcpu0 runs in guest mode
(populates CPU0 TLB)
vcpu0 exits (last_exit_cpu = 0)
vcpu0 blocks in WFI or schedules out
vcpu1 schedules back on CPU0
kvm_riscv_local_tlb_sanitize(vcpu1)
tlb_sanitize_mask has bit 0 clear
last_exit_cpu == vcpu1->cpu (0 == 0)
returns without flushing (vcpu0 has not entered guest mode)
vcpu1 enters guest mode <-- uses vcpu0's stale TLB entries
If vcpu0 and vcpu1 time-share CPU0 without migrating to another CPU,
last_exit_cpu == vcpu->cpu holds for both and CPU0's TLB is never
sanitized across switches between them.
Also, when vcpu0 does migrate to CPU1 while vcpu1 is running in guest mode
on CPU0, kvm_vcpu_kick() calls __kvm_vcpu_kick(tvcpu, false), which sends
an asynchronous reschedule IPI and returns immediately. vcpu0 then enters
guest mode on CPU1 before vcpu1 has exited guest mode and flushed CPU0's
TLB.
[Severity: Medium]
Can a pending HFENCE or TLB flush request for a vCPU's previous host CPU
be lost if the vCPU migrates to another CPU and then migrates back before
reaching kvm_riscv_local_tlb_sanitize()?
In kvm_arch_vcpu_ioctl_run(), kvm_riscv_check_vcpu_requests() runs with
preemption enabled before kvm_riscv_local_tlb_sanitize():
CPU0 CPU1
vcpu0 exits (last_exit_cpu = 0)
(KVM_REQ_HFENCE queued on vcpu0)
vcpu0 schedules on CPU1
kvm_riscv_check_vcpu_requests(vcpu0)
clears KVM_REQ_HFENCE
flushes TLB on CPU1
(preempted or guest entry aborts)
vcpu0 schedules back on CPU0
kvm_riscv_local_tlb_sanitize(vcpu0)
tlb_sanitize_mask has bit 0 clear
last_exit_cpu == vcpu0->cpu (0 == 0)
returns without flushing <-- HFENCE on CPU0 lost
> +
> + /*
> + * 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);
> + }
> + }
[Severity: High]
Does calling kvm_vcpu_kick(tvcpu) unconditionally for every other vCPU in
the VM force unnecessary VM-exits and spurious WFI wakeups on every vCPU
migration?
Only a sibling vCPU currently executing on vcpu->arch.last_exit_cpu can
observe stale TLB entries on that physical CPU. Kicking every tvcpu sends
reschedule IPIs and forces VM-exits on sibling vCPUs running on unrelated
host CPUs (which exit and re-enter without flushing because their CPU is
not set in tlb_sanitize_mask). For sibling vCPUs sleeping in guest WFI,
kvm_vcpu_kick() calls kvm_vcpu_wake_up(), which wakes them from
kvm_vcpu_block() even though they are already outside guest mode,
inflating halt_wakeup stats and shrinking tvcpu->halt_poll_ns.
In addition, because vcpu->arch.last_exit_cpu is set in every sibling
vCPU's tlb_sanitize_mask and cpumask_clear_cpu() only clears it on the
single vCPU that sanitizes that CPU, the remaining N - 2 vCPUs retain the
bit and perform redundant HFENCE flushes whenever they later run on that
host CPU.
> diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
> index 8b4f70c77b8f9..84ae8ae194092 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);
[Severity: Medium]
Does resetting vcpu->arch.last_exit_cpu to -1 in kvm_riscv_reset_vcpu()
drop the pre-reset host CPU before kvm_riscv_local_tlb_sanitize() can
propagate it to sibling vCPUs?
When a vCPU is reset at runtime via KVM_REQ_VCPU_RESET (from SBI HSM
hart_start) or KVM_MP_STATE_INIT_RECEIVED, kvm_riscv_reset_vcpu() runs
before kvm_riscv_local_tlb_sanitize(). Because last_exit_cpu is already
-1 when the restarted vCPU reaches kvm_riscv_local_tlb_sanitize(), the
previous host CPU is never set in sibling vCPUs' tlb_sanitize_mask, and
clearing vcpu->arch.tlb_sanitize_mask also drops any pending sanitization
requests already queued for this vCPU.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130082259.2524917-1-anup.patel@oss.qualcomm.com?part=1
^ permalink raw reply [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