From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796028309; b=qHeDqEAu+pOXIpRE94r2zU1GmFLqsDN4vpgcrN/fqcJxT5kDUXABf5/nge2HHT4KZNQj iyd9rj24Apj2AGevsdiMotOOSrakKGj9pW3amo0BMBCvYLCZ02liFYm0x5UXsFlF9Y+dS tDUklbIbf0pwds8KeJKaBJdcOz4TwGoTksHUlSd6JnIQHp3SSk+GDk02VlcnaIC53S8NE bpPN6Oa84hxrIt96u7+bO/tULwvikElCNNfvBGxQx08Lu1alFrzZRdcWFUd3xZyq2wOMa SvSrDxLWjjjfzfWI8dfYTVHAotSmFlWCe/JrglouHJ+cQnEi3wKsMvNMRgQP6DXOG0Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796028309; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=1ZW3RGoTQ/PEMcm8F1ooMbNxmFqvnf0tSuCd3nXYcIk=; b=q4uqpfL519fs4M449hZUFxKruvA0IwsFWwBmbLX/9SqV9QK+XSTHGp84cYhvIQ93ZqdH sU6rYr7bGUXtRhqdmzZbcP6QDDvR2CaRvJl2/jDwdmlVc7CA5WsvzkUmRWVrOGzt4l4uk N5blJMyc+sR8YPRbqkTOX04JUbE7/nKSoSfcygz/csGUmbLwimUlRXRoyfl6PmrH7zFpt lX7lh5pVZ9sWSvhZD5UXaeAERdHg78u9DQ5Tsgbz6VkrGpope9Ehc6G7Uci2hztLTxGa7 Yker1TSMDx9yQcOJTTaGmUdKC0COZXf380lTQOQ7/fiw/qg42kDQrUC2I94k1Gh90bA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fRuVVj+E; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E991606A683 for ; Mon, 30 Nov 2026 08:45:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fRuVVj+E Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC1111F00896; Mon, 30 Nov 2026 08:45:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796028308; bh=1ZW3RGoTQ/PEMcm8F1ooMbNxmFqvnf0tSuCd3nXYcIk=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=fRuVVj+E4uJE5ikSfP5xXDop3aAvfKSFHicoRdT5tfIu84c6Dt2TcDWwogN/3LJKV mF382xmGc/I2eccrnPyBzZNzAaEUg2nX7NbvqsjuWxIHDT1IFfTvljWcOLgr6JYNAP iAX+knCfKAGMDTe2DJks/RCSRzhc02hlTI2+nr+ron6DeFL9v5wE7tiSqEn34rJVaE jyQIu3INqdnASpvWP660+XD1eHuQJ6fkKah34+yzo8Q3G3Hzla9e9IrB0eOQwRI5yc V0Rqeb8750HpirBheAOAAj+VleRSqn8aj2bucy0wW0a37TrJwtK1E7WEYqArh5GdEc TYHgBBXamlwAA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] RISC-V: KVM: Sanitize TLB on both previous and next host CPU Reply-To: sashiko-reviews@lists.linux.dev To: "Anup Patel" Cc: kvm@vger.kernel.org In-Reply-To: <20261130082259.2524917-1-anup.patel@oss.qualcomm.com> References: <20261130082259.2524917-1-anup.patel@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 08:45:08 +0000 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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) > =20 > void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu) > { > - unsigned long vmid; > + struct kvm_vcpu *tvcpu; > + unsigned long i, vmid; > =20 > - if (!kvm_riscv_gstage_vmid_bits() || > - vcpu->arch.last_exit_cpu =3D=3D 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 =3D=3D 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 =3D 0) vcpu1 schedules out vcpu0 runs in guest mode (populates CPU0 TLB) vcpu0 exits (last_exit_cpu =3D 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 =3D=3D vcpu1->cpu (0 =3D=3D 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 =3D=3D 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 =3D 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 =3D=3D vcpu0->cpu (0 =3D=3D 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 !=3D vcpu= ->cpu) { > + kvm_for_each_vcpu(i, tvcpu, vcpu->kvm) { > + if (tvcpu =3D=3D 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 *vcp= u, bool kvm_sbi_reset) > =20 > vcpu->arch.last_exit_cpu =3D -1; > =20 > + 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130082259.2524= 917-1-anup.patel@oss.qualcomm.com?part=3D1