Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp13026217rwl; Wed, 4 Jan 2023 02:25:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXtpvcEeSq2OU3rCWSY5BItEPH/McemJKF9U8sKU9nt5D6c0jPwEASe1M9PFQRrMbFvKZRob X-Received: by 2002:a50:8e5d:0:b0:46f:a73d:6bdb with SMTP id 29-20020a508e5d000000b0046fa73d6bdbmr41867884edx.25.1672827911867; Wed, 04 Jan 2023 02:25:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672827911; cv=none; d=google.com; s=arc-20160816; b=tqG8Yt1T+JBM2aEzRcM/McNSD3grvP+4sTVbj9AmWdoH3vG2zc6i4H9tfXgg1539CL cpMGERhyeKNV9CJmtmpqUA5pWgT2HqIrLXEEOMWuotZqW+I6TT/J553Njc9yMY7aCTvR I4PkGjqzT+SkmuW7DuU17WcDSVjBFRHpJFqLGfyV09VR/erarV8ando49iWo9H5CAnpM 6ozk7iQTWu9uwefmDImh468mBtXdAgO5e6lP72CkJ0MuXpRbdp/kd6coZUZd3dHrHHmu ZfGb0jjW49yyXW+1luVoYtdMisw3CfV3BW483a5/M4hvKNIny9OfI9RFSTXooRoIs2u5 dgjg== 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=T7HpEnvLFRP9Wc3pt+AlvGW4vqhHztK91tjZWI6KGnk=; b=fMMSF2LoCsHRwrqsax50oPM0eel2/RSzJFRzZ+eal2NLIXvRo27vUwcKhBqP1GpVi1 LNOVJIZ+i+30kakrDrMrC1dY+1gJX7mabxTLnV8/G/f33WTd2EY1M/1TSyOlxzAbOA4P jxzniWODtOhIoojkFbjbG0PS8bjRo8cxnaq6QwHsRxLlD5c8MvreP0vYsQdzvYFThxSn e+WQKfbdBPhqwLSgMkPErQ32Ys+JZsYBmutTsDMR7AnMeTrDdQ1uxnXIJl0/RDiQVHvS uJ5zwE7XawKSF5eWrI9LyKTidOARmS8gB6Cd8WdFTWBEgGTgjsboXlNH2OMj5kEnisw0 dDlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="PtBrc/RZ"; 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 m20-20020aa7d354000000b0047866149cdasi26408561edr.388.2023.01.04.02.24.56; Wed, 04 Jan 2023 02:25:11 -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="PtBrc/RZ"; 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 S234422AbjADKJu (ORCPT + 59 others); Wed, 4 Jan 2023 05:09:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234672AbjADKJW (ORCPT ); Wed, 4 Jan 2023 05:09:22 -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 7D415167D6 for ; Wed, 4 Jan 2023 02:08:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672826923; 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=T7HpEnvLFRP9Wc3pt+AlvGW4vqhHztK91tjZWI6KGnk=; b=PtBrc/RZbxxUmJw5lut2UduhR2BQr3vLadYikWAdmoiK1WYznPYG4HKyhcZnbeX92Tn29q PrWzupVd8RhKwuHgwc3NdwKklCtMWR1+2pNvfIOpL8Rfggo47Br4b11btaZGXjQio73qDo iFYP+NP3F/kp2DdYL3QIuLvycLMyf0A= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-119-vfKIEyfhOgG000b_8b4jTQ-1; Wed, 04 Jan 2023 05:08:42 -0500 X-MC-Unique: vfKIEyfhOgG000b_8b4jTQ-1 Received: by mail-wm1-f69.google.com with SMTP id n18-20020a05600c4f9200b003d993e08485so10582806wmq.2 for ; Wed, 04 Jan 2023 02:08:42 -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=T7HpEnvLFRP9Wc3pt+AlvGW4vqhHztK91tjZWI6KGnk=; b=PXozE0HqQ9wbCb7ZZey7c5XRsIQvodzunnTbRZwIeHL6i8B0LxcLbKpObrSFTfJ+YZ t7IsEgz8gj8JfaYHfAycoP6gp0Hdf31etMJuxVbrunHRk7eelf3Dsyqb+dwFk3ZpelqM c7CluwwXQGxX+PvnkD7o8NvEWyFDWa/xTPRmjbTny49RHNZcW+8wOCJVdm+0bQElX9mV 6BYdtXe/9Lu+bZ20vkzh9mM4egooLOiC6IjRuJS4IEB9KYWNaffeW+5rNlNkB1J2hzBD CUMJ9iBWgZiUxB4Mv5l/1sSCVAA3Sah1B3+o6pnt8wbkCgA7zXZcxIr9B7aNwcvsaFdV 4pLw== X-Gm-Message-State: AFqh2kpB50UyPyXQ9KLugZUKWHZWrsYu1cBXIM/2PPmKPutInIuR0MHj a/5NdCqFF6Kmada5bzMPjvaI2QtuklBgP6TeOUGXAu0xIIBK+e3AgL4k9otBge4g0BHQAiD5MCv qjk928Dc7dThfIuOQ5tXH6r4K X-Received: by 2002:adf:ee83:0:b0:270:213a:b53d with SMTP id b3-20020adfee83000000b00270213ab53dmr28007205wro.33.1672826921388; Wed, 04 Jan 2023 02:08:41 -0800 (PST) X-Received: by 2002:adf:ee83:0:b0:270:213a:b53d with SMTP id b3-20020adfee83000000b00270213ab53dmr28007190wro.33.1672826921142; Wed, 04 Jan 2023 02:08:41 -0800 (PST) Received: from starship ([89.237.103.62]) by smtp.gmail.com with ESMTPSA id u17-20020adfeb51000000b0026e94493858sm33190358wrn.106.2023.01.04.02.08.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 02:08:40 -0800 (PST) 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: Wed, 04 Jan 2023 12:08:38 +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 Thu, 2022-12-29 at 10:27 +0200, mlevitsk@redhat.com wrote: > 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. Sean, please don't forget to fix this isssue in the next patch series. Best regards, Maxim Levitsky > > 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 > >