From: Paolo Bonzini Date: Mon, 29 Nov 2021 17:53:48 +0000 Subject: Re: [PATCH v2 11/43] KVM: Don't block+unblock when halt-polling is successful Message-Id: <496c2fc6-26b0-9b5d-32f4-2f9e9dd6a064@redhat.com> List-Id: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-12-seanjc@google.com> <4e883728e3e5201a94eb46b56315afca5e95ad9c.camel@redhat.com> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable To: Sean Christopherson , Maxim Levitsky Cc: Cornelia Huck , Wanpeng Li , kvm@vger.kernel.org, David Hildenbrand , linux-kernel@vger.kernel.org, Paul Mackerras , Atish Patra , linux-riscv@lists.infradead.org, Claudio Imbrenda , kvmarm@lists.cs.columbia.edu, Janosch Frank , Marc Zyngier , Joerg Roedel , Huacai Chen , Christian Borntraeger , Aleksandar Markovic , Albert Ou , kvm-ppc@vger.kernel.org, Paul Walmsley , David Matlack , linux-arm-kernel@lists.infradead.org, Jim Mattson , Anup Patel , linux-mips@vger.kernel.org, Palmer Dabbelt , kvm-riscv@lists.infradead.org, Vitaly Kuznetsov On 11/29/21 18:25, Sean Christopherson wrote: > If a posted interrupt arrives after KVM has done its final search through= the vIRR, > but before avic_update_iommu_vcpu_affinity() is called, the posted interr= upt will > be set in the vIRR without triggering a host IRQ to wake the vCPU via the= GA log. >=20 > I.e. KVM is missing an equivalent to VMX's posted interrupt check for an = outstanding > notification after switching to the wakeup vector. BTW Maxim reported that it can break even without assigned devices. > For now, the least awful approach is sadly to keep the vcpu_(un)blocking(= ) hooks. I agree that the hooks cannot be dropped but the bug is reproducible=20 with this patch, where the hooks are still there. With the hooks in place, you have: kvm_vcpu_blocking(vcpu) avic_set_running(vcpu, false) avic_vcpu_put(vcpu) avic_update_iommu_vcpu_affinity() WRITE_ONCE(...) // clear IS_RUNNING bit set_current_state() smp_mb() kvm_vcpu_check_block() return kvm_arch_vcpu_runnable() || ... return kvm_vcpu_has_events() || ... return kvm_cpu_has_interrupt() || ... return kvm_apic_has_interrupt() || ... return apic_has_interrupt_for_ppr() apic_find_highest_irr() scan vIRR This covers the barrier between the write of is_running and the read of=20 vIRR, and the other side should be correct as well. in particular,=20 reads of is_running always come after an atomic write to vIRR, and hence=20 after an implicit full memory barrier. svm_deliver_avic_intr() has an=20 smp_mb__after_atomic() after writing IRR; avic_kick_target_vcpus() even=20 has an explicit barrier in srcu_read_lock(), between the microcode's=20 write to vIRR and its own call to avic_vcpu_is_running(). Still it does seem to be a race that happens when IS_RUNNING=3Dtrue but=20 vcpu->mode =3D OUTSIDE_GUEST_MODE. This patch makes the race easier to=20 trigger because it moves IS_RUNNING=FAlse later. Paolo