Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp5696824rwl; Thu, 29 Dec 2022 01:10:41 -0800 (PST) X-Google-Smtp-Source: AMrXdXtWxIGIODY7PbK30Frs2VErpLFwNogOBwkYYeXlX42icahidU/piJ3mo65nDmclGqy/Adp4 X-Received: by 2002:a17:907:c30c:b0:7c1:337e:5755 with SMTP id tl12-20020a170907c30c00b007c1337e5755mr24425339ejc.26.1672305040805; Thu, 29 Dec 2022 01:10:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672305040; cv=none; d=google.com; s=arc-20160816; b=U9sbxyeJrw3bt6qx+pq9lOmrNaJammeAeYfg1AaTe9ahOXGfgSosSm688cgmhUw9aL tTnLfooWQaHOEU1T3gCw6Sw0lt7wZ5ag3LMKnVver01sPuKP0EkjAr1kWYoPDaa80eVQ 70N8+CMtBtsyvtdBXtSD0VTrjybn6mvkd+7SFcbk4ji/bPNW9YozlCoKC0dKGcqkbGv5 ZITOacDrICc5zqNpE4i6y+vwTgjDaWafEiyCxmZkB7DGju2kOP/FEOKupHtaY7X5sToi hdyXqS8c26Xm927jOjkD0MAUMcW/KLbRxPzq03d9QpdQPtGld20JOeElLsGfayeMrl8E jGdQ== 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=3gwn9uw8GpeaAYNSjcfDG9cgBntGteGPOXdgf7cDThI=; b=P6elLP2J2ZhImHptnpHYDJX5YQe1ADat+ZzkO3noBPj6V1TFdaefhLzwL913NxMyG5 SQSlv2Sz+3d7nI77OhyWfq8H6OyjHztBH/x6qi9Xs1zgIsDtXJNsEUbET6w9Dl1rL7IO iTXiw+2/pClp0WLwr+yZtJY59ONDxlF+OHu0K/rC4HtprIHInleDxFF0+e68gQlx+C31 sug2ySJZ4eDejHENVtRcF2dRMVvi34qKia8tRYGUFKq6MWjVXSSQsM6vV4gShXdnDKE2 5rj/2kXhTvFwwLDxOFZn7gr0XIsJmi1Vu3ALNLqXD2lxkWnATI9LMt92idNqpDhYlMYv q70g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=f+n+jcDx; 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 cq18-20020a056402221200b0048461eff750si9722439edb.563.2022.12.29.01.10.25; Thu, 29 Dec 2022 01:10:40 -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=f+n+jcDx; 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 S233184AbiL2I15 (ORCPT + 62 others); Thu, 29 Dec 2022 03:27:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233182AbiL2I14 (ORCPT ); Thu, 29 Dec 2022 03:27:56 -0500 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 51B6ADC7 for ; Thu, 29 Dec 2022 00:27:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672302433; 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=3gwn9uw8GpeaAYNSjcfDG9cgBntGteGPOXdgf7cDThI=; b=f+n+jcDxrJej5p33uVFnAZ7zsKK8JRj/vvAl3ACECEfHrVK+VLZpFQ0dlk2GMtHizEO8GL KV7FT4huP3j9gJlRqaTxrNWc+6cU8bqtRmbwOUe+9D/JzwRhhA2U4NKPaKUI/cKRYawBF9 r8+uxVmnCgS5hN3x56X9Yk3wt9DJdEc= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-249-9cxv75FDN7GZYUN1y-C08A-1; Thu, 29 Dec 2022 03:27:12 -0500 X-MC-Unique: 9cxv75FDN7GZYUN1y-C08A-1 Received: by mail-wm1-f70.google.com with SMTP id fl12-20020a05600c0b8c00b003d96f0a7f36so7980727wmb.1 for ; Thu, 29 Dec 2022 00:27:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=3gwn9uw8GpeaAYNSjcfDG9cgBntGteGPOXdgf7cDThI=; b=DSwmFwuKGZaHUfMTeZGIjuWFrMf7yKG9oL5UfHOE0gcGQ6vZgD84sNPougzeaUIghx 5QcTQ2szopgvyWkV/7bWwm7/RnqZ5S/m70vdmg7iaqqKJwVRgTXboQVFjeSieGazwCMg rgjoSy+lGM1yFd3urQrWQH22t9+2lofQbhXniRgrR7tQ3bOoapZWSOS+Q1pcdZTGfFa+ wZelrBns+/f3ILjh75XtmuLZUqqCbj6m4tJ/8vLF3iFqW2NWZyxUz/c0aXeMpH1P4Iax kv6M+8jIL9RXtpLXzntNx2bDTsNwSfa2FLvNCKjOb9xO3K2M0TVPsRMLvKer4YCyly3s Z9bg== X-Gm-Message-State: AFqh2kr43ieImc0u2StSG0XhtqJsITriYSxI883tVzSqPeQCFSGIxIkA QvDM4uYIhPE6bxBohOEGyqTRKppmLXolMPQCzTsGdUH47hhIGEyC0mcPRmqrdU+cuAy+MhuGukY +VSyIDY4wPtQsiSvBsyYuKWRj X-Received: by 2002:a05:600c:6020:b0:3d1:f0b4:8827 with SMTP id az32-20020a05600c602000b003d1f0b48827mr19496190wmb.25.1672302430934; Thu, 29 Dec 2022 00:27:10 -0800 (PST) X-Received: by 2002:a05:600c:6020:b0:3d1:f0b4:8827 with SMTP id az32-20020a05600c602000b003d1f0b48827mr19496182wmb.25.1672302430729; Thu, 29 Dec 2022 00:27:10 -0800 (PST) Received: from starship ([89.237.103.62]) by smtp.gmail.com with ESMTPSA id f18-20020a05600c4e9200b003d35c845cbbsm32258609wmq.21.2022.12.29.00.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Dec 2022 00:27:10 -0800 (PST) Message-ID: Subject: Re: [PATCH v4 28/32] KVM: SVM: Require logical ID to be power-of-2 for AVIC entry From: mlevitsk@redhat.com To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alejandro Jimenez , Suravee Suthikulpanit , Li RongQing Date: Thu, 29 Dec 2022 10:27:08 +0200 In-Reply-To: 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-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 Fri, 2022-12-09 at 00:00 +0200, Maxim Levitsky wrote: > 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. I hate to say it but this patch has a bug: We have both 'cluster = (ldr >> 4) << 2' and then 'index += (cluster << 2)' One of the shifts has to go. Best regards, Maxim Levitsky > > 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