Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2979131imc; Wed, 13 Mar 2019 06:07:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqyPFNLJNNKoYQ7obKoQScd6Z+LPrpcYYQxh5zFJNexwGLzrEj6fr5Bid2Bs5V5WDuiLD7vN X-Received: by 2002:a65:6559:: with SMTP id a25mr29175847pgw.99.1552482434603; Wed, 13 Mar 2019 06:07:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552482434; cv=none; d=google.com; s=arc-20160816; b=P+cCYZNSEssviwkztXOEW0hB5eTrRGl7k2pGW/j8mm42Wwv8LYFRoAq0KzfwNFHvjD D2EM5gpMr7aaJ+wRGYZRKUhCvDZoFYEkZxTc3jdupq2lHt4+155d1te2nGtLhpIJEwrn QQXjc0U0OwqRs24HWhuT34k6DPPocqhLwOBAT7/GjnNW185JcY+VoMnwTSDmale/lAZ5 K7gpTBZPz0e9rtW3FOlCWfxzp3mbIKNUc192o+A6B/iVVLujjV+ZGQkr0PqachsdCdt3 RJMneobwmlzpXKF3ZzxwzHAIPf9LrdPGxJuKYio1xbN6pwixHF/+e7GQAx5OPRN3z/Ib ZmCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:from:subject:dkim-signature; bh=3QzugJyo0DbBXxWiB5s2q16zO/wHd1razpwaj/rgL4c=; b=esTchs3DeT2WmY4k3IsxubS2SI9pCrOHF9JQzpZQfa3Iz6ia5Bi9TwXRdvaIvHIx2n oh5DdotW5B+72lC/4EDN8FiFXJfwe8Z9TM1iAVlLIYQQqsdhyAge0D7MNWBo5Sb/R2gs WREChSpGeqikWSYSwcQB3SnnbGetm+Vnfn+Ca02Az4+zbltWItGTjoAwfChtnNcILi8E IfS22sF5lERA/WT/FQYIUTtmS496eVhEHuoOkzBHPoe7A7utD0P0NCP+B0Uz/fgT4tuA KRVAAiyZIH0ULvCCkcKOhPTjAk+hAV9fti3b4vYHqgfWRcXi332tlcNwX+wWzBV9qpfD zasw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@scalemp.com header.s=default header.b=C56NC71A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=scalemp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z11si2855260plk.225.2019.03.13.06.06.55; Wed, 13 Mar 2019 06:07:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@scalemp.com header.s=default header.b=C56NC71A; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=scalemp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726418AbfCMNFK (ORCPT + 99 others); Wed, 13 Mar 2019 09:05:10 -0400 Received: from www.scalemp.com ([169.44.78.149]:54499 "EHLO scalemp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725832AbfCMNFJ (ORCPT ); Wed, 13 Mar 2019 09:05:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=scalemp.com ; s=default; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:References:Cc:To:From:Subject:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=3QzugJyo0DbBXxWiB5s2q16zO/wHd1razpwaj/rgL4c=; b=C56NC71Avh20PBJpAy53OHdeCc viGwAw8a1qYu3c5HgpIiQ38ndqNZHGfo7yOwz57oIHe5t19LwtHVUJygxIcb2MMNehyBbejCBz2uL iEvkcLYy6jm0Ixqw1dRt+xvNh3j6r3Hz4WlzJGeEBGnPz1pSNizBJGlbVvqURJv3htaI=; Received: from bzq-80-45-146.static.bezeqint.net ([82.80.45.146]:33032 helo=[10.100.0.166]) by hosting.virtualsmp.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1h43ZD-000gnU-9h; Wed, 13 Mar 2019 09:05:07 -0400 Subject: Re: [PATCH] svm: Fix AVIC incomplete IPI emulation From: Oren Twaig To: "Suthikulpanit, Suravee" Cc: "kvm@vger.kernel.org" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , "Shai (Shai@ScaleMP.com)" , pbonzini@redhat.com, rkrcmar@redhat.com References: <752f5b0a-381c-d559-ec8a-dc41b1e36010@scalemp.com> Message-ID: <58fabfcd-7f32-a9fe-1244-6d9af2b94582@scalemp.com> Date: Wed, 13 Mar 2019 15:05:04 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <752f5b0a-381c-d559-ec8a-dc41b1e36010@scalemp.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hosting.virtualsmp.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - scalemp.com X-Get-Message-Sender-Via: hosting.virtualsmp.com: authenticated_id: oren@scalemp.com X-Authenticated-Sender: hosting.virtualsmp.com: oren@scalemp.com X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Suravee, Turns out, the _same_ bug was already discussed in the past by yourself, Paolo and Radim (both now 'cc'-ed) Please read it here: https://patchwork.kernel.org/patch/8292231/ After reading that thread, I have couple of questions: First, You wrote : "I have tried NOT setting the IRR, and only kick_vcpu(). And things seem to work fine. Therefore, I think your analysis is likely to be correct." AFAIU, it means that the below patch is wrong just as Paolo suggested in his original answer and you did fixed it back than, but the code is now back ? Second, Did you made sure (with your HW desginer) that what the specifications refer as "atomically" means that the IRR is set (i.e the cacheline is taken exclusively) _before_ the isRunning bit is _read_ ? Because, if it doesn't, just like Paolo suggested, it means there is no way to use the feature as a "sending" vcpu can send IPI without any exit and the receiving cpu will never see the IRR bit as the sending cpu didn't made sure the IRR is set _before_ reading the isRunning. Thanks, Oren On 03/13/2019 09:30 AM, Oren Twaig wrote: > Hi Suravee, > > Please see below.. > > On 03/11/2019 01:38 PM, Suthikulpanit, Suravee wrote: >> Hi Oren, >> >> Sorry for delay response. >> >> On 3/5/19 1:15 AM, Oren Twaig wrote: >>> Hello Suravee, >>> >>> According to AMD's SDM, the target-not-running incomplete >>> ipi exit is only received if any of the destination cpus had the >>> not-running bit set in the avic backing page. >> I believe you are referring to the "isRunning" (IR) bit is in the >> AVIC physical APIC ID table entry. > I meant cause ID=1 in IPI Delivery Failure Cause (SDM rev 3.30, sep > 2018, Table 15-28): > > " > 1: IPI Target Not Running: > > IsRunning bit of the target for a > Singlecast/Broadcast/Multicast IPI is not set in > the physical APIC ID table. > > " > >> >>> However, not before the CPU _already_ set the relevant IRR bit >>> in all these cpus. >> Not sure what you meant here. > > Here is the full snippet from the specifications: > " > 5.For every valid destination: > - Atomically set the appropriate IRR bit in each of the destinations’ >   vAPIC backing page. > - Check the IsRunning status of each destination. > - If the destination IsRunning bit is set, send a doorbell message >   using the host physical core number from the Physical APIC ID table. > > 6. If any destinations are identified as not currently scheduled on >  a physical core (i.e., the IsRunning >  bit for that virtual processor is not set), cause a #VMEXIT. > " > > According to the specification above, the HW should first > set the appropriate bit in the IRR (Interrupt Request Register) > _before_ causing VMEXIT of IPI-delivery-not-completed > with  ID=1 (Target not running). > >> >>> In this change, the patch forces KVM to send another interrupt >>> to the vcpu whether SVM already did that or not. Which means >>> the vcpu/s, under some conditions, can get an EXTRA interrupt >>> it never intended to get >> Example: >>>     1. vcpu B: Is in "not-running" state. >>>     2. vcpu A: Writes to the ICR to send vector 80 to vcpu B >>>     3. vcpu A: SVM updates vcpu B IRR with bit 80 >>>     4. vcpu A: SVM exits on incomplete IPI target-not-running exit. >>>     5. vcpu A: Now stops executing any code @ hypervisor level. >>>     6. vcpu B: Due to another interrupt (like lapic timer) >>>        resumes running the guest. While handling interrupts, >>>        it also handles interrupt vector 80 (as it's in his IRR) >>>     7. vcpu A: resumes executing the below code and sends >>>        an _additional_interrupt to vcpu B. >>> >>> Overall, vcpu B got two interrupts. The second is unwanted and >>> not documented in the system architecture. >>> >>> Can you please elaborate more to why the implementation >>> below conflict with the specifications (which was the code >>> before this commit) ? >> This patch was introduced to fix an issue where the SVM driver tries to >> handle the step 5 above by scheduling vcpu B into _running_ state to >> handle >> the IPI from vcpu A. However, prior to this patch, vcpu B was never get >> scheduled to run unless there are other interrupts (e.g. timer). > Exactly. Only what needed here is *only* to wakeup the vcpu B. Why ? > because > the apic of vcpu B _already_ contains the interrupt in the pending > IRR. Than, once vcpu B will run it will process the IRR which contains > the vector placed by the HW and will deliver it. >> This should not be the case as Vcpu B should have been running >> regardless >> of other interrupts. So, I don't think step 6 and 7 above are correct. > The example of vcpu A that stops executing is just to highlight that > the code can't depend on that the kvm code of vcpu A will finish the > ICR "fake" call before vcpu B runs (beacuse of any interrupt) and process > that IRR request placed by the HW. >> >> The issue was caused by the apic->irr_pending not set to true when >> trying to >> get vcpu B scheduled. This flag is checked in apic_find_highest_irr() >> before >> searching for the highest bit. >> >> To fix the issue, I decided to leverage the existing emulation code for >> ICR and ICR2, which in turn calls apic_send_ipi() to deliver >> interrupt to vpu B. >> >> However, looking a bit more closely, I notice the logic in >> svm_deliver_avic_intr() >> should also have been changed from kvm_vcpu_wake_up() to kvm_vcpu_kick() >> since the latter will result in clearing the IRR bit for the IPI vector >> when trying to send IPI as part of the following call path. >> >>     vcpu_enter_guest() >>       |-- inject_pending_event() >>         |-- kvm_cpu_get_interrupt() >>           |--  kvm_get_apic_interrupt() >>             |-- apic_clear_irr() >>             |-- apic_set_isr() >>             |-- apic_update_ppr() .... >> >> Please see the patch below. >> >> Not sure if this would address the problem you are seeing. > I still think there a bug here where vcpu B will get two interrupts > instead of one. > > Thanks, > Oren >> >> Thanks, >> Suravee >> >> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c >> index 24dfa6a93711..d2841c3dbc04 100644 >> --- a/arch/x86/kvm/svm.c >> +++ b/arch/x86/kvm/svm.c >> @@ -5219,11 +5256,13 @@ static void svm_deliver_avic_intr(struct >> kvm_vcpu *vcpu, int vec) >>           kvm_lapic_set_irr(vec, vcpu->arch.apic); >>           smp_mb__after_atomic(); >> >> -       if (avic_vcpu_is_running(vcpu)) >> +       if (avic_vcpu_is_running(vcpu)) { >>                   wrmsrl(SVM_AVIC_DOORBELL, >>                          kvm_cpu_get_apicid(vcpu->cpu)); >> -       else >> -               kvm_vcpu_wake_up(vcpu); >> +       } else { >> +               kvm_make_request(KVM_REQ_EVENT, vcpu); >> +               kvm_vcpu_kick(vcpu); >> +       } >>    } >> >>    static void svm_ir_list_del(struct vcpu_svm *svm, struct >> amd_iommu_pi_data *pi) > >