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 CC68AC4332F for ; Thu, 8 Dec 2022 21:59:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230103AbiLHV7b (ORCPT ); Thu, 8 Dec 2022 16:59:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230143AbiLHV67 (ORCPT ); Thu, 8 Dec 2022 16:58:59 -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 ESMTPS id 37BC45FDE for ; Thu, 8 Dec 2022 13:57:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536678; 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=zJ0em+eljjUI8oUv+HNXrXjRViEN7vmZYx2pW3NMCY4=; b=YMN9mpbhbWjY1mVi3wfyCn5ZtXYSHc8Ezzqci3TxK668xwpvshUFpeV5Bg/gZIYyK53uuB kjubjIPPj6vpAszdW8NcUz4LOYmU4bntGhQlcW6q1BvbJ0pEIKBeJwACncroiGnnqfqOmp X44YOsDyVobvbImaUgL15KzR4jE3UZk= 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-518-yzRp5xPNNhSCsRnKM7mjDQ-1; Thu, 08 Dec 2022 16:57:54 -0500 X-MC-Unique: yzRp5xPNNhSCsRnKM7mjDQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7F98529AA387; Thu, 8 Dec 2022 21:57:54 +0000 (UTC) Received: from starship (unknown [10.35.206.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 186951121331; Thu, 8 Dec 2022 21:57:52 +0000 (UTC) Message-ID: <95dfb8f37cd218ea706ed8ee7c105656c282fcba.camel@redhat.com> Subject: Re: [PATCH v4 20/32] KVM: x86: Skip redundant x2APIC logical mode optimized cluster setup From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alejandro Jimenez , Suravee Suthikulpanit , Li RongQing Date: Thu, 08 Dec 2022 23:57:52 +0200 In-Reply-To: <20221001005915.2041642-21-seanjc@google.com> References: <20221001005915.2041642-1-seanjc@google.com> <20221001005915.2041642-21-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 3.1 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 2022-10-01 at 00:59 +0000, Sean Christopherson wrote: > Skip the optimized cluster[] setup for x2APIC logical mode, as KVM reuses > the optimized map's phys_map[] and doesn't actually need to insert the > target apic into the cluster[]. The LDR is derived from the x2APIC ID, > and both are read-only in KVM, thus the vCPU's cluster[ldr] is guaranteed > to be the same entry as the vCPU's phys_map[x2apic_id] entry. > > Skipping the unnecessary setup will allow a future fix for aliased xAPIC > logical IDs to simply require that cluster[ldr] is non-NULL, i.e. won't > have to special case x2APIC. > Alternatively, the future check could allow "cluster[ldr] == apic", but > that ends up being terribly confusing because cluster[ldr] is only set > at the very end, i.e. it's only possible due to x2APIC's shenanigans. > > Another alternative would be to send x2APIC down a separate path _after_ > the calculation and then assert that all of the above, but the resulting > code is rather messy, and it's arguably unnecessary since asserting that > the actual LDR matches the expected LDR means that simply testing that > interrupts are delivered correctly provides the same guarantees. Yes, I think it makes sense to not touch the cluser map in x2apic mode. The map even explicitly named as for xapic (xapic_flat_map, xapic_cluster_map) Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky > > Reported-by: Suravee Suthikulpanit > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/lapic.c | 22 +++++++++++++++++----- > 1 file changed, 17 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 9989893fef69..fca8bbb44375 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -166,6 +166,11 @@ static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu) > return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE; > } > > +static inline u32 kvm_apic_calc_x2apic_ldr(u32 id) > +{ > + return ((id >> 4) << 16) | (1 << (id & 0xf)); > +} > + > static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, > u32 dest_id, struct kvm_lapic ***cluster, u16 *mask) { > switch (map->logical_mode) { > @@ -315,6 +320,18 @@ void kvm_recalculate_apic_map(struct kvm *kvm) > } > new->logical_mode = logical_mode; > > + /* > + * In x2APIC mode, the LDR is read-only and derived directly > + * from the x2APIC ID, thus is guaranteed to be addressable. > + * KVM reuses kvm_apic_map.phys_map to optimize logical mode > + * x2APIC interrupts by reversing the LDR calculation to get > + * cluster of APICs, i.e. no additional work is required. > + */ > + if (apic_x2apic_mode(apic)) { > + WARN_ON_ONCE(ldr != kvm_apic_calc_x2apic_ldr(x2apic_id)); > + continue; > + } > + > if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr, > &cluster, &mask))) { > new->logical_mode = KVM_APIC_MODE_MAP_DISABLED; > @@ -381,11 +398,6 @@ static inline void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val) > atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY); > } > > -static inline u32 kvm_apic_calc_x2apic_ldr(u32 id) > -{ > - return ((id >> 4) << 16) | (1 << (id & 0xf)); > -} > - > static inline void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id) > { > u32 ldr = kvm_apic_calc_x2apic_ldr(id);