Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1721995rwb; Thu, 8 Dec 2022 14:32:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf45TOJdRnF7zXSGoiShkUkekmzpRsyHL8JPSDL1HBdhOOLXUFwaytXybjfWIbTlYerISgl1 X-Received: by 2002:aa7:dccf:0:b0:46d:66e6:5dd4 with SMTP id w15-20020aa7dccf000000b0046d66e65dd4mr5202641edu.373.1670538737110; Thu, 08 Dec 2022 14:32:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670538737; cv=none; d=google.com; s=arc-20160816; b=B2wmCe2bDljRyw8jcQ5sfFrXLlH0ZdN6m8hrGAVF6r/NXurXG/UuMkBDqEthZjntaI aJn7QvM8aAsfdX4xIH3BG4n5ZJTshQI6vWS3RIYi/wEPzHKpwl8wQ7qcEMsLDqqZwpyp X3bNzWSnjOz4eweaR546ctxujisuRN+iZXhat/43HYKpQatVivPFAblF/KDUqWIBBxg2 Y+p+4jsxxUcm36iBj6bw3H5QX32co6+hUeKMh8VahZNKZbMMajCVab6RSM88MimD2VWr PGozXCiijHKacHXnHmdgT9AqeJtXYTL6Hp3Rsehox8ySlrB7iGCLmSbGs3QacLKJTFFl f+7w== 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=FYrDLJGucSIpdbIsOIn8UE6xyaF/+dOPy8B3O9dEgSQ=; b=Al2nka229oT8A8vlXClmJzKX7G03x4BgxdOQeEwKKajiIZrfYse9PepRf1iBxqfjBB YYNAhRJSZ/ov4u76Jc0ZpxXGeO6pj7vGFk8XhM6DgwE3vkw9Kp+5RJMJCtJCj3MO2dPY xHJ/rUrJhxn7JkrPFzwLe1G5fb8Q3Vf8iUzZ/akaTYYeiT4DYy8dlKZWzgen2eyb9lII cpqyENm4Ibfp7uIuSeVZRDmd5kHkysvWOWPgOshtcyPHMzHyhZwWwLVMpPlpjyftJNsx Y5Sb1E8HtLZUtE/Xl0NF3bI0s9fl55qalcwa2uKekuUoPWPMAU2P96Mr066y0s2Ogz6X Imtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LRpn3mul; 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 t7-20020a056402524700b00468eee74e58si8904649edd.273.2022.12.08.14.31.59; Thu, 08 Dec 2022 14:32:17 -0800 (PST) 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=LRpn3mul; 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 S230171AbiLHWD0 (ORCPT + 73 others); Thu, 8 Dec 2022 17:03:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230166AbiLHWCy (ORCPT ); Thu, 8 Dec 2022 17:02:54 -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 D34D5A0FB4 for ; Thu, 8 Dec 2022 14:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536840; 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=FYrDLJGucSIpdbIsOIn8UE6xyaF/+dOPy8B3O9dEgSQ=; b=LRpn3mul2RrtqYhFuaj/jY9tY0Qq2v/1T7/rdSXt1XMEG+dtmPzqXOPdlA8EKR1ZM0qZVL wFpAeir91CH8fom7ZlgzOp9LJdAIE6/U3mlk5ml9nDE2P6b5oqpKThSuWpYXiu2hovPXEH 6jDypusBDuQbZYiPSfSY7QsoYaoW4Sg= 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-16-BbYH1xuZPpenyluuYR9TYw-1; Thu, 08 Dec 2022 17:00:36 -0500 X-MC-Unique: BbYH1xuZPpenyluuYR9TYw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 875141C087A9; Thu, 8 Dec 2022 22:00:35 +0000 (UTC) Received: from starship (unknown [10.35.206.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 24BAB2024CC0; Thu, 8 Dec 2022 22:00:33 +0000 (UTC) Message-ID: Subject: Re: [PATCH v4 28/32] KVM: SVM: Require logical ID to be power-of-2 for AVIC entry 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: Fri, 09 Dec 2022 00:00:33 +0200 In-Reply-To: <20221001005915.2041642-29-seanjc@google.com> References: <20221001005915.2041642-1-seanjc@google.com> <20221001005915.2041642-29-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.4 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,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 Sat, 2022-10-01 at 00:59 +0000, Sean Christopherson wrote: > Do not modify AVIC's logical ID table if the logical ID portion of the > LDR is not a power-of-2, i.e. if the LDR has multiple bits set. Taking > only the first bit means that KVM will fail to match MDAs that intersect > with "higher" bits in the "ID" > > The "ID" acts as a bitmap, but is referred to as an ID because theres an > implicit, unenforced "requirement" that software only set one bit. This > edge case is arguably out-of-spec behavior, but KVM cleanly handles it > in all other cases, e.g. the optimized logical map (and AVIC!) is also > disabled in this scenario. > > Refactor the code to consolidate the checks, and so that the code looks > more like avic_kick_target_vcpus_fast(). > > Fixes: 18f40c53e10f ("svm: Add VMEXIT handlers for AVIC") > Cc: Suravee Suthikulpanit > Cc: Maxim Levitsky > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 30 +++++++++++++++--------------- > 1 file changed, 15 insertions(+), 15 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 4b6fc9d64f4d..a9e4e09f83fc 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -513,26 +513,26 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu) > static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) > { > struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); > - int index; > u32 *logical_apic_id_table; > - int dlid = GET_APIC_LOGICAL_ID(ldr); > + u32 cluster, index; > > - if (!dlid) > - return NULL; > + ldr = GET_APIC_LOGICAL_ID(ldr); > > - if (flat) { /* flat */ > - index = ffs(dlid) - 1; > - if (index > 7) > + if (flat) { > + cluster = 0; > + } else { > + cluster = (ldr >> 4) << 2; > + if (cluster >= 0xf) > return NULL; > - } else { /* cluster */ > - int cluster = (dlid & 0xf0) >> 4; > - int apic = ffs(dlid & 0x0f) - 1; > - > - if ((apic < 0) || (apic > 7) || > - (cluster >= 0xf)) > - return NULL; > - index = (cluster << 2) + apic; > + ldr &= 0xf; > } > + if (!ldr || !is_power_of_2(ldr)) > + return NULL; > + > + index = __ffs(ldr); > + if (WARN_ON_ONCE(index > 7)) > + return NULL; > + index += (cluster << 2); > > logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page); > Looks good. For future refactoring, I also suggest to rename this function to 'avic_get_logical_id_table_entry' to stress the fact that it gets a pointer to the AVIC's data structure. Same for 'avic_get_physical_id_entry' And also while at it : the 'svm->avic_physical_id_cache', is a very misleading name, It should be svm->avic_physical_id_table_entry_ptr with a comment explaining that is is the pointer to physid table entry. Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky