Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anup Patel" <anup.patel@oss.qualcomm.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU
Date: Mon, 30 Nov 2026 08:45:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-240698@kernel.org> (raw)
In-Reply-To: <20261130082259.2524917-1-anup.patel@oss.qualcomm.com>

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

      reply	other threads:[~2026-11-30  8:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-240698@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anup.patel@oss.qualcomm.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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