Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp976084rwb; Wed, 28 Sep 2022 11:27:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7vQJf6WIrZwA7RJNXvql7E2IAvw2YS0GgaVIYRUyZHw7LW83RBrMBpOibTKW+W0pzkvS9y X-Received: by 2002:a17:906:db05:b0:741:5730:270e with SMTP id xj5-20020a170906db0500b007415730270emr27851060ejb.609.1664389670500; Wed, 28 Sep 2022 11:27:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664389670; cv=none; d=google.com; s=arc-20160816; b=HLcpo3FaN6ZWNhoClG/ZmNFnpqk77Cs/p4qQS88ew5hfHazBPmFopyowZ2j8tLLZm5 9I+5Tdx1plYzCdkMMzFgpLz+F63xpDfRf96c64JCT2/tntyZO1BxaDXfsDyw5ldX9v/Z ApwOQANqec79hs97dByeuUiJfs1c9t4gbVwz3BgXiynEdUrrsr41oezS1ZlsuQiKtUGR mhzJRLhnup7iTKplBfe3uRjmHkOegwL612Gsf46HYolV9GQw5vZwpIk36q/A1fNCR1h2 h/zi6ZvMP5VKtXJRHSw0wahQ1W/0+a10iThrECgG2M/ombdzwoeyeAs+nhBbbT5fDasi cyXQ== 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=xMO4mKpfNczNxeZW1DT6bXLOYvg3cQC3Gxcwr3VGJ+Y=; b=OLNFjP+bD7DvtOgrRepWbw/3YMb8EBcUpUylYmKF8rwLnoHDwFm+BBvhx1plRs7I96 UsjG8NiGm8GrTrTMcOJOgMkNA78fH/y9XcTCSiIl9WVFYJWSOtfDFoR0QpHrPaoTRKGO 6pNs/09YFJdb1+Q+tp0RcZ4/058U0SCObISfY1upyBh8Wh97Or5p6AAIhEBCJ0yMgg76 yWGUzWjp9wOlqkeLLUG2ybRN89IfgMtNrx0zDOJKsea/b6ofGZ+dFxpua3LqL3wifPrb a5laZmjP2SXS8bp9ZhMqyES9Q8bAJujXNwlvdA56ka9qj5k3CzQKEqWGs4cl/s2F5TeD QG1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Qvsll+Hq; 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 s25-20020a170906455900b007813c565e59si4721889ejq.210.2022.09.28.11.27.24; Wed, 28 Sep 2022 11:27:50 -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=@redhat.com header.s=mimecast20190719 header.b=Qvsll+Hq; 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 S234700AbiI1SRP (ORCPT + 99 others); Wed, 28 Sep 2022 14:17:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234738AbiI1SRE (ORCPT ); Wed, 28 Sep 2022 14:17:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4174C106F76 for ; Wed, 28 Sep 2022 11:16:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664388967; 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=xMO4mKpfNczNxeZW1DT6bXLOYvg3cQC3Gxcwr3VGJ+Y=; b=Qvsll+HqKttUGjb3BSWyXdNklncTguL9SaLZSmyXIvmf3kKMH8/p3C2OHCKo6oazjeoXm4 U+n+Dmh+IAbFax6XBwuraR02abCrk71tJPG4GsczSs57ScGtJnNRAYLTQUq1f6kmVBBgdH J1UszpxMm67sJOCi7f2jC+lvmhAAauc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-131-6yJpjjx-NnuSdGWveV2tog-1; Wed, 28 Sep 2022 14:16:04 -0400 X-MC-Unique: 6yJpjjx-NnuSdGWveV2tog-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 691113826242; Wed, 28 Sep 2022 18:16:03 +0000 (UTC) Received: from starship (unknown [10.40.193.233]) by smtp.corp.redhat.com (Postfix) with ESMTP id 89F33492CA2; Wed, 28 Sep 2022 18:16:01 +0000 (UTC) Message-ID: <6fcec4adb331452c64adf255c367af86072bd9d3.camel@redhat.com> Subject: Re: [PATCH v3 05/28] KVM: x86: Don't inhibit APICv/AVIC if xAPIC ID mismatch is due to 32-bit ID From: Maxim Levitsky To: Sean Christopherson Cc: Alejandro Jimenez , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Li RongQing Date: Wed, 28 Sep 2022 21:16:00 +0300 In-Reply-To: References: <20220920233134.940511-1-seanjc@google.com> <20220920233134.940511-6-seanjc@google.com> <1aea43e831cd7ed90c325b2c90bc6f3f9a1805b5.camel@redhat.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.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, 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 Wed, 2022-09-28 at 18:03 +0000, Sean Christopherson wrote: > On Wed, Sep 28, 2022, Maxim Levitsky wrote: > > On Wed, 2022-09-28 at 16:51 +0000, Sean Christopherson wrote: > > > > > 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". > > True - I didn't bother to fix it yet because it shouldn't cause harm, but > > sure this needs to be fixed. > > It's probably benign on its own, but with the missing "hardware enabled" check, > it could be problematic if userspace does KVM_GET_LAPIC while the APIC is hardware > disabled, after the APIC was previously in x2APIC mode. I'm guessing QEMU does > KVM_GET_LAPIC state when emulating reboot, hence the potential for being involved > in the bug Alejandro is seeing. > > > > 2. The refresh needs to be noted after memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s)); > > Are you sure? The check is first because if it fails, then error is returned to userspace > > and the KVM's state left unchanged. > > > > I assume you are talking about > > > > .... > > r = kvm_apic_state_fixup(vcpu, s, true); > > if (r) { > > kvm_recalculate_apic_map(vcpu->kvm); > > return r; > > } > > memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s)); > > This isn't a failure path though, it's purely a "take note of the update", and > KVM needs to do that processing _after_ the actual update. Specifically, > kvm_lapic_xapic_id_updated() consumes the internal APIC state: Yes, I somehow blindly assumed that kvm_apic_state_fixup actually checks the new state and not the existing state. Probably because my original code did that, I think it just checked the 'id' variable.. Oh well. Thanks for catching this bug! Best regards, Maxim Levitsky > > if (kvm_xapic_id(apic) == apic->vcpu->vcpu_id) > return; > > Calling that before the internal state has been set with the incoming state from > userspace is simply wrong. > > The check that the x2APIC ID is "correct" stays where it is, this is purely the > "is the xAPIC ID different" path. >