Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp904766rwb; Wed, 28 Sep 2022 10:29:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ZuUGTn+eG1QT+bf8l20SVmwH6GrWAKyo/Ms49b0lWFron/Hzh174jqlFbglitqUy13aI/ X-Received: by 2002:a17:906:5a6b:b0:73c:c9ee:8b5c with SMTP id my43-20020a1709065a6b00b0073cc9ee8b5cmr27833025ejc.310.1664386145331; Wed, 28 Sep 2022 10:29:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664386145; cv=none; d=google.com; s=arc-20160816; b=fpCn5lTHAlz4awIai15Nf5PwFGoGPQJaLuuHJGh8jO4QzgDqYAa2OzmTTZHMA/xsJf smlaYVRVX80sPyU+HouYTM1Pj+4/w5lXk/NQjjxmKhhj2D98NHNLRFs728SUJcaW69+i BxAkY3InCAoEQrZFuqkdYMmAbMGOAwVoD5thm4rfP9F70uXeVrH8rfIiSZhz0SAUi0tc 7eeRDkvvUA6d3e3v+esbzHE1V/hSh8vn5uejFM7WlgjR5I0tOVi892gcU7VwLy491nlf vW5W2D0UIsqhNSJ6+rb/orkeRoLd+VWgRgD4i4C22tOATnguAriuPksGse0VzPnM4Pyx QSfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=h0/015q9L2PTjIc0Q6cg/qKsJjdE+BVvbhky4mrZGZM=; b=fAPbyOsnDb3RQB4lyOm8yr1HgbIeYHvU/9H7RxBtgYbUXidu3YIOIkX2MG1HTB6l6q F30KUtVu96iogxL8dcOSJz2N4+IKrgZlYxaP/2xLuINZmGfCP9rNKJH0KK4eUrM3a8CJ O+jOcdmA/AXtjn3KRKSvdXIUhrhnE6Zht1rImf2E/WMoVQJP1a603tkS2eScUDqR/tEp fJBljEqXaKCwSU1VGbAGMZynICtDscpcR0Urkb2tc9S26Ydcj+OGMsjZJ4e3VhMLWezE eODah6Qva9ET07SWQDb8HjlLqlbRZPM8BtVcb/jtUJVfsRK9gIeNpsewwwu/PQsNxfJV V7bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PMsN7p0c; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l15-20020a1709062a8f00b0077a0529e045si4736891eje.218.2022.09.28.10.28.39; Wed, 28 Sep 2022 10:29:05 -0700 (PDT) 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=@google.com header.s=20210112 header.b=PMsN7p0c; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233494AbiI1QvY (ORCPT + 99 others); Wed, 28 Sep 2022 12:51:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234423AbiI1QvN (ORCPT ); Wed, 28 Sep 2022 12:51:13 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53D8FE7E34 for ; Wed, 28 Sep 2022 09:51:12 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id y136so13053835pfb.3 for ; Wed, 28 Sep 2022 09:51:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=h0/015q9L2PTjIc0Q6cg/qKsJjdE+BVvbhky4mrZGZM=; b=PMsN7p0cKAT4VWUKKy/cQ5tldNzUKfl8S/iKkTeJd2YgRdYeuJp+T5PUfCv+fHny+d hqnF0i1t43QpVhk6oUsPKh3o8uGSWVEn7Vf8ngPTvhmR6R4Bz8UtguBd+nlIiNUSiN/5 pHzNa2j6eGOH2COHhtWC71kdLHGPo0teUkL0IyAblnYmo3DPb2XaivaHU6ia0G6w5fBg WRqrZfMSFPs71USTIU21Zzfjv0PGrOsXfaCHZ31OmS7Xt5Nm4pAoANf34lbGcM6Z2gBN EK2Bn5+0ChzxYBRq9pBBOzxbP+1doa6IHmGaJ/0lS1RaPf1/XGlOzBCQX9FPYwisqp85 m7Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=h0/015q9L2PTjIc0Q6cg/qKsJjdE+BVvbhky4mrZGZM=; b=Ox35pBbHIPtBiROXN5nRaPkxZKYg4VR+valUOz7c/IqfoH3oMKVXzQWhmO0JFZC37H N3ecQ0D9JR1EGwxnp+XgoZIuFWDuEE9syHMo4oRgHn7/Olz45sRHE3fF5vV1FKyY3Dur CuDzP5fLMG30jzCj+auxi+6um1gthHEBnL0uBnjFILLnztUlskKmx4LjKGQw5hhnlwGF Ok+bjSPFUKfICfZDYPb2LFyEotExz2Z5RjwS5O6qr3UjLQU1muI5QKCIBC17lB6zo0b9 5e7En8DnGcLBYqXwTinwvSRxkMrO+IoD9UJBbxQGBM5qVyJs4+EzJEAavyljjEmA81Iv HbhA== X-Gm-Message-State: ACrzQf3NRtTfRjBha88H34Z1JkMgI/Wuh/J2V+UrK8DXxiA0qA1SXP5S Sqn2xdAn1REoDaVbs/suztlqGA== X-Received: by 2002:a63:50e:0:b0:438:d16d:e8c1 with SMTP id 14-20020a63050e000000b00438d16de8c1mr28317051pgf.505.1664383871552; Wed, 28 Sep 2022 09:51:11 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id d2-20020a170903230200b00176677a893bsm617874plh.82.2022.09.28.09.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 09:51:10 -0700 (PDT) Date: Wed, 28 Sep 2022 16:51:07 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: Alejandro Jimenez , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Li RongQing Subject: Re: [PATCH v3 05/28] KVM: x86: Don't inhibit APICv/AVIC if xAPIC ID mismatch is due to 32-bit ID Message-ID: References: <20220920233134.940511-1-seanjc@google.com> <20220920233134.940511-6-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, Sep 28, 2022, Maxim Levitsky wrote: > On Tue, 2022-09-27 at 23:15 -0400, Alejandro Jimenez wrote: > > > > On 9/20/2022 7:31 PM, Sean Christopherson wrote: > > > Truncate the vcpu_id, a.k.a. x2APIC ID, to an 8-bit value when comparing > > > it against the xAPIC ID to avoid false positives (sort of) on systems > > > with >255 CPUs, i.e. with IDs that don't fit into a u8. The intent of > > > APIC_ID_MODIFIED is to inhibit APICv/AVIC when the xAPIC is changed from > > > it's original value, > > > > > > The mismatch isn't technically a false positive, as architecturally the > > > xAPIC IDs do end up being aliased in this scenario, and neither APICv > > > nor AVIC correctly handles IPI virtualization when there is aliasing. > > > However, KVM already deliberately does not honor the aliasing behavior > > > that results when an x2APIC ID gets truncated to an xAPIC ID. I.e. the > > > resulting APICv/AVIC behavior is aligned with KVM's existing behavior > > > when KVM's x2APIC hotplug hack is effectively enabled. > > > > > > If/when KVM provides a way to disable the hotplug hack, APICv/AVIC can > > > piggyback whatever logic disables the optimized APIC map (which is what > > > provides the hotplug hack), i.e. so that KVM's optimized map and APIC > > > virtualization yield the same behavior. > > > > > > For now, fix the immediate problem of APIC virtualization being disabled > > > for large VMs, which is a much more pressing issue than ensuring KVM > > > honors architectural behavior for APIC ID aliasing. > > > > I built a host kernel with this entire series on top of mainline > > v6.0-rc6, and booting a guest with AVIC enabled works as expected on the > > initial boot. The issue is that during the first reboot AVIC is > > inhibited due to APICV_INHIBIT_REASON_APIC_ID_MODIFIED, and I see > > constant inhibition events due to APICV_INHIBIT_REASON_IRQWIN as seen in > > > APICV_INHIBIT_REASON_IRQWIN is OK, because that happens about every time > the good old PIT timer fires which happens on reboot. > > APICV_INHIBIT_REASON_APIC_ID_MODIFIED should not happen as you noted, > this needs investigation. Ya, I'll take a look. > > It happens regardless of vCPU count (tested with 2, 32, 255, 380, and > > 512 vCPUs). This state persists for all subsequent reboots, until the VM > > is terminated. For vCPU counts < 256, when x2apic is disabled the > > problem does not occur, and AVIC continues to work properly after reboots. Bit of a shot in the dark, but does the below fix the issue? There are two issues with calling kvm_lapic_xapic_id_updated() from kvm_apic_state_fixup(): 1. The xAPIC ID should only be refreshed on "set". 2. The refresh needs to be noted after memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s)); and a third bug in the helper itself, as changes to the ID should be ignored if the APIC is hardward disabled since the ID is reset to the vcpu_id when the APIC is hardware enabled (architecturally behavior). --- arch/x86/kvm/lapic.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 804d529d9bfb..b8b2faf5abc7 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2159,6 +2159,9 @@ static void kvm_lapic_xapic_id_updated(struct kvm_lapic *apic) { struct kvm *kvm = apic->vcpu->kvm; + if (!kvm_apic_hw_enabled(apic)) + return; + if (KVM_BUG_ON(apic_x2apic_mode(apic), kvm)) return; @@ -2875,8 +2878,6 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR); __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32); } - } else { - kvm_lapic_xapic_id_updated(vcpu->arch.apic); } return 0; @@ -2912,6 +2913,9 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s) } memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s)); + if (!apic_x2apic_mode(vcpu->arch.apic)) + kvm_lapic_xapic_id_updated(vcpu->arch.apic); + atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY); kvm_recalculate_apic_map(vcpu->kvm); kvm_apic_set_version(vcpu); base-commit: 0b502152c0b8523f399bdb53096e2d620c5795b5 --