Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp847701rwe; Wed, 31 Aug 2022 12:07:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR4LtHkurQm7uYBQ5MzvA5ZLCVb3yrMwIgnlCpbcBCJncWmbfbmAaAKopLQXktXdxkhADcZw X-Received: by 2002:a65:6047:0:b0:42b:313e:d331 with SMTP id a7-20020a656047000000b0042b313ed331mr23608979pgp.179.1661972841799; Wed, 31 Aug 2022 12:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661972841; cv=none; d=google.com; s=arc-20160816; b=mAnPodAfhuaJiEdFEMOm9WL0AuMGq+b1ghJyZdRLpkfxAm+8jtmFzKP64CUCE2QqBK KRxiRHfbqEM5VtTaMGhYJEotO2NuB/6jCYSSDZBfQ4rp5g8/00djwFcl/w6HoKrWGDZa 3uFHBHadazVgbKdFHnHS0cDPiDwAM4rk51IBB1LloOeaVPRMkn5k3dWqpNxRglVtjtzD ggav+akTf2ejPPBLsB1+FpVQTx7THPCwhNAETwOCzGmreStwPxXfN9izfnTtKMQR9WZA u4ji5o0uyCDvg/NQNw20YnfS2dx9cUmNqMUrPjT1+4ngJ4nURlxMkvT0prFK0AgNmFFa bQ8Q== 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=Si1iwu1R0to21pjaPNDmF4Mnk8lbHWDBxAleduKvEiQ=; b=WmcLENu/s1BTH9LVofaScKwfNd3/+fHdbblgCNY7YgYxwUWB4nzYDz2BKzLOXcf/i0 fhi89uL+J8W3w+TzNeoFP/k/8O48FEnsVgkEYc/HJM8C+9VLEM/+d9zcD+vmNnhxBM8f 7uswiJoug5oOntiFAyFS39le6ln1pM5IdKui/d/+d1VuKW5r2QgblZKBqCjBOo6XHNfL QQF2ILhuOZyvA+KRflbHAKGKj4+LdtZAfLWfyoW0N1tDWbRaIZYz+l43IK/FXvFvAG4T pS5mLoKNdkbhmLDaHlfTTvPH2Effu64RX7E6Prr/bc0tM35SNut2/0cFRvqAdkGEpkNL wKKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ei2LUAod; 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 mh8-20020a17090b4ac800b001f229bc3dc3si2869582pjb.104.2022.08.31.12.07.10; Wed, 31 Aug 2022 12:07:21 -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=Ei2LUAod; 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 S232778AbiHaSa1 (ORCPT + 99 others); Wed, 31 Aug 2022 14:30:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232733AbiHaSaT (ORCPT ); Wed, 31 Aug 2022 14:30:19 -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 99C7F1AF3E for ; Wed, 31 Aug 2022 11:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661970307; 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=Si1iwu1R0to21pjaPNDmF4Mnk8lbHWDBxAleduKvEiQ=; b=Ei2LUAod+IAMMWE3xarFs+anHkCOq0bLJbbJt/grYcUgtr7SGUtn4adl8aogASyyLzoF6R uxYZkURMuG92Ce8MroLNpYl8ilyO802KTT6kKxkUC2ckxbv9gb7+Cmq4g7FE3sEqBLeQSK IBkPHDEMpI8LJY6CZcM6NW7GUxX6qjU= 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-423-OndAVcPJPzGN_yIJL-9Dkg-1; Wed, 31 Aug 2022 14:25:03 -0400 X-MC-Unique: OndAVcPJPzGN_yIJL-9Dkg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 089598037AA; Wed, 31 Aug 2022 18:25:03 +0000 (UTC) Received: from starship (unknown [10.40.194.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 673DE40CF8EE; Wed, 31 Aug 2022 18:25:01 +0000 (UTC) Message-ID: Subject: Re: [PATCH 17/19] KVM: SVM: Handle multiple logical targets in AVIC kick fastpath From: Maxim Levitsky To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suravee Suthikulpanit , Li RongQing Date: Wed, 31 Aug 2022 21:25:00 +0300 In-Reply-To: References: <20220831003506.4117148-1-seanjc@google.com> <20220831003506.4117148-18-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 2.84 on 10.11.54.1 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, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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-08-31 at 18:19 +0000, Sean Christopherson wrote: > On Wed, Aug 31, 2022, Maxim Levitsky wrote: > > On Wed, 2022-08-31 at 00:35 +0000, Sean Christopherson wrote: > > > +static void avic_kick_vcpu_by_logical_id(struct kvm *kvm, u32 *avic_logical_id_table, > > > + u32 logid_index, u32 icrl) > > > +{ > > > + u32 physical_id; > > > + > > > + if (!avic_logical_id_table) { > > ^ Typo, the '!' shoudn't be there. > > Ouch. I suspect the tests pass because this just ends up routing events through > the slow path. I try to concoct a testcase to expose this bug. > > > > +static bool is_optimized_logical_map_enabled(struct kvm *kvm) > > > +{ > > > + struct kvm_apic_map *map; > > > + bool enabled; > > > + > > > + rcu_read_lock(); > > > + map = rcu_dereference(kvm->arch.apic_map); > > > + enabled = map && map->logical_mode != KVM_APIC_MODE_MAP_DISABLED; > > > + rcu_read_unlock(); > > > + return enabled; > > > +} > > > > This function doesn't belong to avic, it should be in common KVM code. > > I'll move it. I'm not expecting any additional users, but I agree it belongs > elsewhere. Actually, might be a moot point (see below). > > > > @@ -394,50 +449,27 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source > > > if (unlikely(!bitmap)) > > > return 0; > > > > > > - if (!is_power_of_2(bitmap)) > > > - /* multiple logical destinations, use slow path */ > > > + /* > > > + * Use the slow path if more than one bit is set in the bitmap > > > + * and KVM's optimized logical map is disabled to avoid kicking > > > + * a vCPU multiple times. If the optimized map is disabled, a > > > + * vCPU _may_ have multiple bits set in its logical ID, i.e. > > > + * may have multiple entries in the logical table. > > > + */ > > > + if (!is_power_of_2(bitmap) && > > > + !is_optimized_logical_map_enabled(kvm)) > > > return -EINVAL; > > > > I hate to say it but there is another issue here, which I know about for a while > > but haven't gotten yet to fix. > > > > The issue is that AVIC's logical to physical map can't cover all the corner cases > > that you discovered - it only supports the sane subset: for each cluster, and for each bit > > in the mask, it has a physical apic id - so things like logical ids with multiple bits, > > having same logical id for multiple vcpus and so on can't work. > > > > In this case we need to either inhibit AVIC (I support this 100%), > > I like the idea of inhibiting. > > > or clear its logical ID map, so all logicical IPIs VM exit, and then they > > can be emulated. > > > > I haven't studied it formally but the code which rebuilds the AVIC's logical ID map > > starts at 'avic_handle_ldr_update'. > > I suspected there are issues here, but the new tests passed (somewhat surprisingly) > so I stopped trying to decipher the AVIC LDR handling. > > Eww. And the VM-Exit trap logic is broken too. If the guest updates and disables > its LDR, SVM returns immediately and doesn't call into common APIC code, i.e. doesn't > recalc the optimized map. E.g. if the guest clears its LDR, the optimized map will > be left as is and the vCPU will receive interrupts using its old LDR. > > case APIC_LDR: > if (avic_handle_ldr_update(vcpu)) > return 0; > break; > > Rather than handling this purely in AVIC code, what if we a key off of > the optimized map being enabled? E.g. drop the return from avic_handle_ldr_update() > and in the kvm_recalculate_apic_map() do: > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 3b6ef36b3963..6e188010b614 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -364,6 +364,11 @@ void kvm_recalculate_apic_map(struct kvm *kvm) > cluster[ldr] = apic; > } > out: > + if (!new || new->logical_mode == KVM_APIC_MODE_MAP_DISABLED) > + kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_MAP_DISABLED); > + else > + kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_MAP_DISABLED); > + This looks very good, it will even work on APICv, because the 'check_apicv_inhibit_reasons' will not return true for this new reason (APICv IPIv I think doesn't deal with logical destination at all); Best regards, Maxim Levitsky > old = rcu_dereference_protected(kvm->arch.apic_map, > lockdep_is_held(&kvm->arch.apic_map_lock)); > rcu_assign_pointer(kvm->arch.apic_map, new); >