Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1911169pxm; Thu, 24 Feb 2022 11:52:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJxGofW1mjjeiFGBpwMKwCdVL+ci+9UQbLK/4s8nmabkdUVlMvYaerQvVqd31biAJfjs2ABD X-Received: by 2002:a17:902:7044:b0:14e:e1b1:bb0e with SMTP id h4-20020a170902704400b0014ee1b1bb0emr4278373plt.65.1645732351309; Thu, 24 Feb 2022 11:52:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645732351; cv=none; d=google.com; s=arc-20160816; b=aRdhnlHqAqXrz5YAwdLc4RmHLvu8uZifasGk42YGUMCnIjIqTIFaVnjJ5r9bey2EMb xyZHQPGTn/elK69iiInbGa6b+smmNlYooXq4+Xvdp9DQd+VyG0ccoJKgZl4BnmUVbHzr YJK2q1rNpOk6wpEA5wg2Ln6IJV+1j4pBQ0IemSRjKYSf6I+IeXkYJXDaAWb4//+pdqDk UoPDZ6yCAtjk1WhtdR7QTQDmWjI6VgSxY00hqv4GtRSysM0/HZVWQhFr0oF1j1HdjePA jlpDnlL23vqIi/zUHKTcBDFlVpAHC6ejuONauiHq3r5X70eKVqkp6KTGA+/E2RSuTcG/ jojg== 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=zOV5EV1UzXliH9U/EzhEDJEFZZoLbZzEL9tR71RF+1A=; b=k2pNpZurPMj3yZT8PbgBbTvLZ6nwNQkTnxu8ML3tN0xpCyKMixRj72fHsH6KN8epck W/rZgcTjeieO1C1QW11ucke1UZBKn6vU92xqrtRS7Fnq8DTWxdRRXbnqoJ7JMcj7g72p r/DmFhW7QxGEQsqKrM14Qu1SxelDlxLxNIzlCVrbdB08x28hyllrfQFkElNQxgz8aorS BSIWLTAKf0fqN8ozKTetREI9wpeA+tChweBeMMVJ0+rt5tzoa0T8G9nUpS8w+aKIVcgQ UthhSV4KpnK1JGIqINXJ5D4w2VEZuts1SRTy1sXOAXT+6olpa9CpoHTnyoMA12+DDdNf PCvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="TNcj/xEz"; 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 4-20020a170902c14400b0014fbef75549si77931plj.444.2022.02.24.11.52.15; Thu, 24 Feb 2022 11:52:31 -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="TNcj/xEz"; 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 S231388AbiBXRa0 (ORCPT + 99 others); Thu, 24 Feb 2022 12:30:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231868AbiBXRaZ (ORCPT ); Thu, 24 Feb 2022 12:30:25 -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 ESMTP id 9456E1D8A96 for ; Thu, 24 Feb 2022 09:29:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645723764; 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=zOV5EV1UzXliH9U/EzhEDJEFZZoLbZzEL9tR71RF+1A=; b=TNcj/xEzGik3AWkTtN7fEmpNUOCqO/Fk2zRzh6tNCV0gDNlUsOVOg/e7RKNc51w4aSbKVz VlkKPGKD6ISJlyfJnxW+ZIAUMqmgw3Gmn7XSnoBlUBKzzQVvuYVjEUW6yhWkBUDBxmX0zw JYpelPGpd+5TXRLe65wlw/OyNWADsOI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-508-Cq01rZ-aOKORCANWokIZVQ-1; Thu, 24 Feb 2022 12:29:21 -0500 X-MC-Unique: Cq01rZ-aOKORCANWokIZVQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74E2018766D0; Thu, 24 Feb 2022 17:29:19 +0000 (UTC) Received: from starship (unknown [10.40.195.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id E91AF866DD; Thu, 24 Feb 2022 17:29:16 +0000 (UTC) Message-ID: <334aadda53c7837d71e7c9d11b772a4a66b58df3.camel@redhat.com> Subject: Re: [RFC PATCH 06/13] KVM: SVM: Add logic to determine x2APIC mode From: Maxim Levitsky To: Suravee Suthikulpanit , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, joro@8bytes.org, jon.grimm@amd.com, wei.huang2@amd.com, terry.bowman@amd.com Date: Thu, 24 Feb 2022 19:29:15 +0200 In-Reply-To: <20220221021922.733373-7-suravee.suthikulpanit@amd.com> References: <20220221021922.733373-1-suravee.suthikulpanit@amd.com> <20220221021922.733373-7-suravee.suthikulpanit@amd.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 2.79 on 10.5.11.12 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 Sun, 2022-02-20 at 20:19 -0600, Suravee Suthikulpanit wrote: > Introduce avic_update_vapic_bar(), which checks the x2APIC enable bit > of the APIC Base register and update the new struct vcpu_svm.x2apic_enabled > to keep track of current APIC mode of each vCPU, > > Signed-off-by: Suravee Suthikulpanit > --- > arch/x86/kvm/svm/avic.c | 13 +++++++++++++ > arch/x86/kvm/svm/svm.c | 4 ++++ > arch/x86/kvm/svm/svm.h | 2 ++ > 3 files changed, 19 insertions(+) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 1999076966fd..60f30e48d816 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -609,6 +609,19 @@ void avic_post_state_restore(struct kvm_vcpu *vcpu) > avic_handle_ldr_update(vcpu); > } > > +void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data) > +{ > + svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK; > + > + /* Set x2APIC mode bit if guest enable x2apic mode. */ > + svm->x2apic_enabled = (avic_mode == AVIC_MODE_X2 && > + kvm_apic_mode(data) == LAPIC_MODE_X2APIC); > + pr_debug("vcpu_id:%d switch to %s\n", svm->vcpu.vcpu_id, > + svm->x2apic_enabled ? "x2APIC" : "xAPIC"); > + vmcb_mark_dirty(svm->vmcb, VMCB_AVIC); > + kvm_vcpu_update_apicv(&svm->vcpu); > +} > + > void svm_set_virtual_apic_mode(struct kvm_vcpu *vcpu) > { > return; > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 3687026f2859..4e6dc1feeac7 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -2867,6 +2867,10 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) > svm->msr_decfg = data; > break; > } > + case MSR_IA32_APICBASE: > + if (kvm_vcpu_apicv_active(vcpu)) > + avic_update_vapic_bar(to_svm(vcpu), data); > + fallthrough; > default: > return kvm_set_msr_common(vcpu, msr); > } > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > index 1a0bf6b853df..bfbebb933da2 100644 > --- a/arch/x86/kvm/svm/svm.h > +++ b/arch/x86/kvm/svm/svm.h > @@ -225,6 +225,7 @@ struct vcpu_svm { > u32 dfr_reg; > struct page *avic_backing_page; > u64 *avic_physical_id_cache; > + bool x2apic_enabled; > > /* > * Per-vcpu list of struct amd_svm_iommu_ir: > @@ -566,6 +567,7 @@ void avic_init_vmcb(struct vcpu_svm *svm); > int avic_incomplete_ipi_interception(struct kvm_vcpu *vcpu); > int avic_unaccelerated_access_interception(struct kvm_vcpu *vcpu); > int avic_init_vcpu(struct vcpu_svm *svm); > +void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data); > void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu); > void avic_vcpu_put(struct kvm_vcpu *vcpu); > void avic_post_state_restore(struct kvm_vcpu *vcpu); Have you looked at how this is done on Intel's APICv side? You need to implement .set_virtual_apic_mode instead. (look at vmx_set_virtual_apic_mode) I also don't think you need x2apic_enabled boolean. You can already know if a vCPU uses apic or x2apic via kvm_get_apic_mode(vcpu); in fact I don't think avic code should have any bookeeping in regard to x2apic/x2avic mode, but rather kvm's apic mode (which is read directly from apic base msr (vcpu->arch.apic_base), should enable avic, or x2avic if possible, or inhibit avic if not possible. e.g it should drive the bits in vmcb and such. Best regards, Maxim Levitsky