From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CBFAC6FA86 for ; Wed, 28 Sep 2022 18:17:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234508AbiI1SRR (ORCPT ); Wed, 28 Sep 2022 14:17:17 -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 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. >