Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1750467rwb; Thu, 8 Dec 2022 15:00:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf62pXBh2XYfkUmsabUCVfn8asgTOXgf/4nx9lMRl61QnPmxHL8h+PtYMkCcK/gouJGBGe/I X-Received: by 2002:a17:907:d0c:b0:7bd:ece7:ae66 with SMTP id gn12-20020a1709070d0c00b007bdece7ae66mr4820953ejc.34.1670540443000; Thu, 08 Dec 2022 15:00:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670540442; cv=none; d=google.com; s=arc-20160816; b=VNwEdbl1zfKuyy6BI6bbjF1c332MtGykaQII87KVWdsRYnkszSDBFVktqOQaZPFloS v8DT5EZ+dJGTTceSN9XHAmKM06i3g6agTiy7T9YTKOY+ujY2FjCMLDAW9X1REs9oW5NE UodAFqsQC+NN3EPOEg0H1zcs724gUF/SEDZKlJXD+0s6OgWDS1QuJvf6a8n+t7E04qaT dzAqVe8NBb0OsGhNxGQgA22KjZYsvUCbPUTvZNV1A/28Y4dCNsbfVg/ZlmwGi9teXqxN o3bH1Ekkr/vdK0M2X4BZ4cFJM58UOncwrxKw9HGJvY3SigMegkFoP78u9nHI0SqBhaan GDCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=Mq8oV63M1unZbJY5qyv2v3uiyUc953tHIdO7sV0O7go=; b=ZXFyLdV+kIEea+tPvdIlgY6f9KdmXg+FHVPeamXYVciNnoak54e9soDLq9hyzq5Ji6 TRVK0zsZao8mSelsJBBLdqlq4Nm/FbDoQBpAFMzKMchVJToWwJ+4NguUqRd0/As9xgzo zq9DWVMX+bsP1HYDeDTDNrB8Miu4oFUHIXWFMwMcz6IVqnUpGBpQG+GHbXxj0MRvm5Qa RJHqbB5BOgGUvLJfrizwdfmrw0H/srwtZESKgoXDFTpdXhkM4HlIT1OC7oHCGpIItysW VgiqE6TEUxaL3bNWZSq9yoYHPPoLkLREDgYD1ZtyeW4YKyKMwEgmzZZPb1yf014CGETn EaYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=donQVbOL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s5-20020a17090699c500b007c0fa8181c3si10926325ejn.454.2022.12.08.15.00.24; Thu, 08 Dec 2022 15:00:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=donQVbOL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229969AbiLHVzO (ORCPT + 73 others); Thu, 8 Dec 2022 16:55:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbiLHVzB (ORCPT ); Thu, 8 Dec 2022 16:55:01 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F14319D8A3 for ; Thu, 8 Dec 2022 13:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mq8oV63M1unZbJY5qyv2v3uiyUc953tHIdO7sV0O7go=; b=donQVbOL4pECnEKHfmgc/6uLMvlkc61U/X5lfgCOzEzsIbzYbzsLHi/kuAKgngCbZJJOyl Pnjdcqtiu0FVvveBjS7bs4znxgBAwNTcNBldj2ZcveK/Z0e6uIDj1ylY8hddvgdCGE3p1I ZrRXAVfRNN7lmOWU61JWMksruNM3x5k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-LI4v0INVNPW0cThlFCCAFQ-1; Thu, 08 Dec 2022 16:53:59 -0500 X-MC-Unique: LI4v0INVNPW0cThlFCCAFQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C292C803B38; Thu, 8 Dec 2022 21:53:58 +0000 (UTC) Received: from starship (unknown [10.35.206.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5A2C5492CA2; Thu, 8 Dec 2022 21:53:57 +0000 (UTC) Message-ID: Subject: Re: [PATCH v4 08/32] KVM: SVM: Don't put/load AVIC when setting virtual APIC mode From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alejandro Jimenez , Suravee Suthikulpanit , Li RongQing Date: Thu, 08 Dec 2022 23:53:56 +0200 In-Reply-To: <20221001005915.2041642-9-seanjc@google.com> References: <20221001005915.2041642-1-seanjc@google.com> <20221001005915.2041642-9-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 2022-10-01 at 00:58 +0000, Sean Christopherson wrote: > Move the VMCB updates from avic_refresh_apicv_exec_ctrl() into > avic_set_virtual_apic_mode() and invert the dependency being said > functions to avoid calling avic_vcpu_{load,put}() and > avic_set_pi_irte_mode() when "only" setting the virtual APIC mode. > > avic_set_virtual_apic_mode() is invoked from common x86 with preemption > enabled, which makes avic_vcpu_{load,put}() unhappy. Luckily, calling > those and updating IRTE stuff is unnecessary as the only reason > avic_set_virtual_apic_mode() is called is to handle transitions between > xAPIC and x2APIC that don't also toggle APICv activation. And if > activation doesn't change, there's no need to fiddle with the physical > APIC ID table or update IRTE. > > The "full" refresh is guaranteed to be called if activation changes in > this case as the only call to the "set" path is: > > kvm_vcpu_update_apicv(vcpu); > static_call_cond(kvm_x86_set_virtual_apic_mode)(vcpu); > > and kvm_vcpu_update_apicv() invokes the refresh if activation changes: > > if (apic->apicv_active == activate) > goto out; > > apic->apicv_active = activate; > kvm_apic_update_apicv(vcpu); > static_call(kvm_x86_refresh_apicv_exec_ctrl)(vcpu); > > Rename the helper to reflect that it is also called during "refresh". > > WARNING: CPU: 183 PID: 49186 at arch/x86/kvm/svm/avic.c:1081 avic_vcpu_put+0xde/0xf0 [kvm_amd] > CPU: 183 PID: 49186 Comm: stable Tainted: G O 6.0.0-smp--fcddbca45f0a-sink #34 > Hardware name: Google, Inc. Arcadia_IT_80/Arcadia_IT_80, BIOS 10.48.0 01/27/2022 > RIP: 0010:avic_vcpu_put+0xde/0xf0 [kvm_amd] > avic_refresh_apicv_exec_ctrl+0x142/0x1c0 [kvm_amd] > avic_set_virtual_apic_mode+0x5a/0x70 [kvm_amd] > kvm_lapic_set_base+0x149/0x1a0 [kvm] > kvm_set_apic_base+0x8f/0xd0 [kvm] > kvm_set_msr_common+0xa3a/0xdc0 [kvm] > svm_set_msr+0x364/0x6b0 [kvm_amd] > __kvm_set_msr+0xb8/0x1c0 [kvm] > kvm_emulate_wrmsr+0x58/0x1d0 [kvm] > msr_interception+0x1c/0x30 [kvm_amd] > svm_invoke_exit_handler+0x31/0x100 [kvm_amd] > svm_handle_exit+0xfc/0x160 [kvm_amd] > vcpu_enter_guest+0x21bb/0x23e0 [kvm] > vcpu_run+0x92/0x450 [kvm] > kvm_arch_vcpu_ioctl_run+0x43e/0x6e0 [kvm] > kvm_vcpu_ioctl+0x559/0x620 [kvm] > > Fixes: 05c4fe8c1bd9 ("KVM: SVM: Refresh AVIC configuration when changing APIC mode") > Cc: stable@vger.kernel.org > Cc: Suravee Suthikulpanit > Cc: Maxim Levitsky > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 31 +++++++++++++++---------------- > arch/x86/kvm/svm/svm.c | 2 +- > arch/x86/kvm/svm/svm.h | 2 +- > 3 files changed, 17 insertions(+), 18 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 3b2c88b168ba..97ad0661f963 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -747,18 +747,6 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu) > avic_handle_ldr_update(vcpu); > } > > -void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu) > -{ > - if (!lapic_in_kernel(vcpu) || avic_mode == AVIC_MODE_NONE) > - return; > - > - if (kvm_get_apic_mode(vcpu) == LAPIC_MODE_INVALID) { > - WARN_ONCE(true, "Invalid local APIC state (vcpu_id=%d)", vcpu->vcpu_id); > - return; > - } > - avic_refresh_apicv_exec_ctrl(vcpu); > -} > - > static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) > { > int ret = 0; > @@ -1100,17 +1088,18 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) > WRITE_ONCE(*(svm->avic_physical_id_cache), entry); > } > > - > -void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) > +void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) > { > struct vcpu_svm *svm = to_svm(vcpu); > struct vmcb *vmcb = svm->vmcb01.ptr; > - bool activated = kvm_vcpu_apicv_active(vcpu); > + > + if (!lapic_in_kernel(vcpu) || avic_mode == AVIC_MODE_NONE) > + return; > > if (!enable_apicv) > return; > > - if (activated) { > + if (kvm_vcpu_apicv_active(vcpu)) { > /** > * During AVIC temporary deactivation, guest could update > * APIC ID, DFR and LDR registers, which would not be trapped > @@ -1124,6 +1113,16 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) > avic_deactivate_vmcb(svm); > } > vmcb_mark_dirty(vmcb, VMCB_AVIC); > +} > + > +void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) > +{ > + bool activated = kvm_vcpu_apicv_active(vcpu); > + > + if (!enable_apicv) > + return; > + > + avic_refresh_virtual_apic_mode(vcpu); > > if (activated) > avic_vcpu_load(vcpu, vcpu->cpu); > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 58f0077d9357..afae97ea9a06 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -4798,7 +4798,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { > .enable_nmi_window = svm_enable_nmi_window, > .enable_irq_window = svm_enable_irq_window, > .update_cr8_intercept = svm_update_cr8_intercept, > - .set_virtual_apic_mode = avic_set_virtual_apic_mode, > + .set_virtual_apic_mode = avic_refresh_virtual_apic_mode, > .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl, > .check_apicv_inhibit_reasons = avic_check_apicv_inhibit_reasons, > .apicv_post_state_restore = avic_apicv_post_state_restore, > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > index 6a7686bf6900..7a95f50e80e7 100644 > --- a/arch/x86/kvm/svm/svm.h > +++ b/arch/x86/kvm/svm/svm.h > @@ -646,7 +646,7 @@ void avic_vcpu_blocking(struct kvm_vcpu *vcpu); > void avic_vcpu_unblocking(struct kvm_vcpu *vcpu); > void avic_ring_doorbell(struct kvm_vcpu *vcpu); > unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu); > -void avic_set_virtual_apic_mode(struct kvm_vcpu *vcpu); > +void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu); > > > /* sev.c */ Makes sense. I missed that during x2avic review. That warning about preemption is a good one. Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky