Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp412933pxm; Fri, 25 Feb 2022 10:23:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxMm0pLqqszfy9BacFpmyzpt/B7M1FTSBsxxHlk7XMNjJaAUVkY5Bdigiidw9pF/VWTXT5J X-Received: by 2002:a17:902:8d81:b0:14d:964b:6186 with SMTP id v1-20020a1709028d8100b0014d964b6186mr8664011plo.108.1645813418699; Fri, 25 Feb 2022 10:23:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645813418; cv=none; d=google.com; s=arc-20160816; b=t2VjACOTbS9od+0t5C4c46LxIgUsFq3cfXcj+/kYsWuPJs5anpQ9j4bMuuGqX3Katj IgCs1VSBWCaM9hgW74FYMOkljddP5H5dzy5tPf6Wn/tsg+02MltIOzjpp0hVwoDCw2wW xGgO+hZcuZ82yCwINmuM/H86vwwCFrNzuqYypq+n+D951v/RyAxMxahpb5N4tMdgSusc 2s4MYrRiNMuhxklgObomkUBOGLwpF3XxX5ZY75pVs0vO9y8J7f7jrEVIU2T/yx3ZmxsR 1nQmYGaBfzvnqw5U8Z8iOTmy5CLnLDZF8OF7kInG/sSZPCTd3ew+peaQcYthHiO4zmhn FI2w== 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=ojV+v8nU5UgxoBRpf2GOrPq7WAn6iAeXjWuAfgZUBsE=; b=dRpndABXeBaS7Z5l+ztwE4/2BQ2mWu4bBif2hZDdd23/eZriiCBR0NJxeczoYhJpZl uBJfuvAarrfT+81DMk5gGY858xI0/CRXxbVYnQz13vpaj7K68Pn5kkGi+L3LtXBgP3Jt vE578V5MIHkguAk4qtMx921NO1o56lPuL/ERkmuHhVvICcTXrvrHLZ9J5hMzYrJAsIEu sIoWhezR8dC2KBpqrJoCV0z4ZdzCTfH5Fg3CECegu3va3ijxowJx8Anm2G1raxj1O9YT xsHdmQ/TowtKSlWMkBtpaIGv0qUMdApkLEtTuNDewMiNJlxL3oMXL+mqdPvsl3PcmitU gaVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZR07CEUP; 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 o62-20020a634141000000b00373a24e7c4dsi2584694pga.69.2022.02.25.10.23.22; Fri, 25 Feb 2022 10:23:38 -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=ZR07CEUP; 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 S240627AbiBYRah (ORCPT + 99 others); Fri, 25 Feb 2022 12:30:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237679AbiBYRag (ORCPT ); Fri, 25 Feb 2022 12:30:36 -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 ESMTP id 6D0A060D8B for ; Fri, 25 Feb 2022 09:30:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645810203; 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=ojV+v8nU5UgxoBRpf2GOrPq7WAn6iAeXjWuAfgZUBsE=; b=ZR07CEUPfzLTaJja7B5dmHrX04orjBOP74svMGoxtsIqNgP93gmeYPXjY/NhKjPWVMCZeK rMdzgKPkcm6O78Da1vIGhj4XDw3mcarlCkuStzudOIP6xiKhz0VGb5aBkYzaj6j6WlC9Y+ BVw9CDyssOFcPUyosC8ucYAoZFqqn10= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-54-OnJVrDTmMeWgqctbBlz31w-1; Fri, 25 Feb 2022 12:30:00 -0500 X-MC-Unique: OnJVrDTmMeWgqctbBlz31w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A0C7E1006AA6; Fri, 25 Feb 2022 17:29:57 +0000 (UTC) Received: from starship (unknown [10.40.195.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EF3380009; Fri, 25 Feb 2022 17:29:45 +0000 (UTC) Message-ID: <113db01c73b8fe061b8226e75849317bac7873a5.camel@redhat.com> Subject: Re: [PATCH v6 9/9] KVM: VMX: Optimize memory allocation for PID-pointer table From: Maxim Levitsky To: Zeng Guang , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, Dave Hansen , Tony Luck , Kan Liang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Kim Phillips , Jarkko Sakkinen , Jethro Beekman , Kai Huang Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Robert Hu , Gao Chao Date: Fri, 25 Feb 2022 19:29:39 +0200 In-Reply-To: <20220225082223.18288-10-guang.zeng@intel.com> References: <20220225082223.18288-1-guang.zeng@intel.com> <20220225082223.18288-10-guang.zeng@intel.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.79 on 10.5.11.11 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, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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-02-25 at 16:22 +0800, Zeng Guang wrote: > Current kvm allocates 8 pages in advance for Posted Interrupt Descriptor > pointer (PID-pointer) table to accommodate vCPUs with APIC ID up to > KVM_MAX_VCPU_IDS - 1. This policy wastes some memory because most of > VMs have less than 512 vCPUs and then just need one page. > > If user hypervisor specify max practical vcpu id prior to vCPU creation, > IPIv can allocate only essential memory for PID-pointer table and reduce > the memory footprint of VMs. > > Suggested-by: Sean Christopherson > Signed-off-by: Zeng Guang > --- > arch/x86/kvm/vmx/vmx.c | 45 ++++++++++++++++++++++++++++-------------- > 1 file changed, 30 insertions(+), 15 deletions(-) > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 0cb141c277ef..22bfb4953289 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -230,9 +230,6 @@ static const struct { > }; > > #define L1D_CACHE_ORDER 4 > - > -/* PID(Posted-Interrupt Descriptor)-pointer table entry is 64-bit long */ > -#define MAX_PID_TABLE_ORDER get_order(KVM_MAX_VCPU_IDS * sizeof(u64)) > #define PID_TABLE_ENTRY_VALID 1 > > static void *vmx_l1d_flush_pages; > @@ -4434,6 +4431,24 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) > return exec_control; > } > > +static int vmx_alloc_pid_table(struct kvm_vmx *kvm_vmx) > +{ > + struct page *pages; > + > + if(kvm_vmx->pid_table) > + return 0; > + > + pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, > + get_order(kvm_vmx->kvm.arch.max_vcpu_id * sizeof(u64))); > + > + if (!pages) > + return -ENOMEM; > + > + kvm_vmx->pid_table = (void *)page_address(pages); > + kvm_vmx->pid_last_index = kvm_vmx->kvm.arch.max_vcpu_id - 1; > + return 0; > +} > + > #define VMX_XSS_EXIT_BITMAP 0 > > static void init_vmcs(struct vcpu_vmx *vmx) > @@ -7159,6 +7174,16 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) > goto free_vmcs; > } > > + if (enable_ipiv && kvm_vcpu_apicv_active(vcpu)) { > + struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); > + > + mutex_lock(&vcpu->kvm->lock); > + err = vmx_alloc_pid_table(kvm_vmx); > + mutex_unlock(&vcpu->kvm->lock); > + if (err) > + goto free_vmcs; > + } This could be dangerous. If APICv is temporary inhibited, this code won't run and we will end up without PID table. I think that kvm_vcpu_apicv_active should be just dropped from this condition. Best regards, Maxim Levitsky > + > return 0; > > free_vmcs: > @@ -7202,17 +7227,6 @@ static int vmx_vm_init(struct kvm *kvm) > } > } > > - if (enable_ipiv) { > - struct page *pages; > - > - pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, MAX_PID_TABLE_ORDER); > - if (!pages) > - return -ENOMEM; > - > - to_kvm_vmx(kvm)->pid_table = (void *)page_address(pages); > - to_kvm_vmx(kvm)->pid_last_index = KVM_MAX_VCPU_IDS - 1; > - } > - > return 0; > } > > @@ -7809,7 +7823,8 @@ static void vmx_vm_destroy(struct kvm *kvm) > struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); > > if (kvm_vmx->pid_table) > - free_pages((unsigned long)kvm_vmx->pid_table, MAX_PID_TABLE_ORDER); > + free_pages((unsigned long)kvm_vmx->pid_table, > + get_order((kvm_vmx->pid_last_index + 1) * sizeof(u64))); > } > > static struct kvm_x86_ops vmx_x86_ops __initdata = {