From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.groups.io with SMTP id smtpd.web12.31704.1610357020953233163 for ; Mon, 11 Jan 2021 01:23:41 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QVo1UyIF; spf=pass (domain: redhat.com, ip: 216.205.24.124, mailfrom: lersek@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610357020; 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=ftnsQo9DTw0edI9mrqJw6jGdbMqEiHPi/J1BvguxIlw=; b=QVo1UyIFO26kPa2z9iP8CRSrV8jazRot6VCCEhv0z0LfWifYKxeCjDG5RkwjhftWP4IVc3 4mFLkpPli6vC+hcixI9LA5oahcsS0LLNpG1oCMFdUqkRbuddUUYGBgpzKnkS+cfL1iVPu/ n4DbvuG+rFlS6ISFrwxrOh4BHp4Dcc0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-173-rwaRDj9AMDa_Ln4GcaAM6g-1; Mon, 11 Jan 2021 04:23:36 -0500 X-MC-Unique: rwaRDj9AMDa_Ln4GcaAM6g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2ECFA1009446; Mon, 11 Jan 2021 09:23:35 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-113-91.ams2.redhat.com [10.36.113.91]) by smtp.corp.redhat.com (Postfix) with ESMTP id F229419D9F; Mon, 11 Jan 2021 09:23:33 +0000 (UTC) Subject: Re: [edk2-devel] [PATCH V4] UefiCpuPkg PiSmmCpuDxeSmm: Reduce SMRAM consumption in CpuS3.c To: devel@edk2.groups.io, star.zeng@intel.com Cc: Ray Ni , Eric Dong References: <20210111015419.28368-1-star.zeng@intel.com> From: "Laszlo Ersek" Message-ID: <9bb35b73-2e92-9e13-5f8c-b8b99b7dbbcd@redhat.com> Date: Mon, 11 Jan 2021 10:23:33 +0100 MIME-Version: 1.0 In-Reply-To: <20210111015419.28368-1-star.zeng@intel.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=lersek@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 01/11/21 02:54, Zeng, Star wrote: > This patch makes two refinements to reduce SMRAM consumption in CpuS3.c. > 1. Only do CopyRegisterTable() when register table is not empty, > IsRegisterTableEmpty() is created to check whether the register table > is empty or not. > > Take empty PreSmmInitRegisterTable as example, about 24K SMRAM consumption > could be reduced when mAcpiCpuData.NumberOfCpus=1024. > sizeof (CPU_REGISTER_TABLE) = 24 > mAcpiCpuData.NumberOfCpus = 1024 = 1K > mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE) = 24K > > 2. Only copy table entries buffer instead of whole buffer. > AllocatedSize in SourceRegisterTableList is the whole buffer size. > Actually, only the table entries buffer needs to be copied, and the size > is TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY). > > Take AllocatedSize=0x1000=4096, TableLength=100 and NumberOfCpus=1024 as example, > about 1696K SMRAM consumption could be reduced. > sizeof (CPU_REGISTER_TABLE_ENTRY) = 24 > TableLength = 100 > TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY) = 2400 > AllocatedSize = 0x1000 = 4096 > AllocatedSize - TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY) = 4096 - 2400 = 1696 > NumberOfCpus = 1024 = 1K > NumberOfCpus * (AllocatedSize - TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY)) = 1696K > > This patch also corrects the CopyRegisterTable() function description. > > Signed-off-by: Star Zeng > Reviewed-by: Ray Ni > Reviewed-by: Laszlo Ersek > Cc: Ray Ni > Cc: Eric Dong > Cc: Laszlo Ersek > --- > > Notes: > V2: Use "DestinationRegisterTableList[Index].TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY)" directly to cover Ray's comment. > V3: Handle "RegisterTable == NULL" case to cover Laszlo's comment. > V4: Add @param[in] for NumberOfCpus parameter of IsRegisterTableEmpty(). > > UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 74 ++++++++++++++++++++++++------- > 1 file changed, 57 insertions(+), 17 deletions(-) Merged as commit e992cc3f4859, via . Thanks Laszlo > > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > index 9592430636ec..ab7f39aa2bd4 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c > @@ -1,7 +1,7 @@ > /** @file > Code for Processor S3 restoration > > -Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.
> +Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.
> SPDX-License-Identifier: BSD-2-Clause-Patent > > **/ > @@ -487,6 +487,9 @@ SetRegister ( > } else { > RegisterTables = (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.RegisterTable; > } > + if (RegisterTables == NULL) { > + return; > + } > > InitApicId = GetInitialApicId (); > RegisterTable = NULL; > @@ -948,7 +951,7 @@ InitSmmS3ResumeState ( > } > > /** > - Copy register table from ACPI NVS memory into SMRAM. > + Copy register table from non-SMRAM into SMRAM. > > @param[in] DestinationRegisterTableList Points to destination register table. > @param[in] SourceRegisterTableList Points to source register table. > @@ -967,7 +970,8 @@ CopyRegisterTable ( > > CopyMem (DestinationRegisterTableList, SourceRegisterTableList, NumberOfCpus * sizeof (CPU_REGISTER_TABLE)); > for (Index = 0; Index < NumberOfCpus; Index++) { > - if (DestinationRegisterTableList[Index].AllocatedSize != 0) { > + if (DestinationRegisterTableList[Index].TableLength != 0) { > + DestinationRegisterTableList[Index].AllocatedSize = DestinationRegisterTableList[Index].TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY); > RegisterTableEntry = AllocateCopyPool ( > DestinationRegisterTableList[Index].AllocatedSize, > (VOID *)(UINTN)SourceRegisterTableList[Index].RegisterTableEntry > @@ -978,6 +982,34 @@ CopyRegisterTable ( > } > } > > +/** > + Check whether the register table is empty or not. > + > + @param[in] RegisterTable Point to the register table. > + @param[in] NumberOfCpus Number of CPUs. > + > + @retval TRUE The register table is empty. > + @retval FALSE The register table is not empty. > +**/ > +BOOLEAN > +IsRegisterTableEmpty ( > + IN CPU_REGISTER_TABLE *RegisterTable, > + IN UINT32 NumberOfCpus > + ) > +{ > + UINTN Index; > + > + if (RegisterTable != NULL) { > + for (Index = 0; Index < NumberOfCpus; Index++) { > + if (RegisterTable[Index].TableLength != 0) { > + return FALSE; > + } > + } > + } > + > + return TRUE; > +} > + > /** > Get ACPI CPU data. > > @@ -1032,23 +1064,31 @@ GetAcpiCpuData ( > > CopyMem ((VOID *)(UINTN)mAcpiCpuData.IdtrProfile, (VOID *)(UINTN)AcpiCpuData->IdtrProfile, sizeof (IA32_DESCRIPTOR)); > > - mAcpiCpuData.PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE)); > - ASSERT (mAcpiCpuData.PreSmmInitRegisterTable != 0); > + if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->PreSmmInitRegisterTable, mAcpiCpuData.NumberOfCpus)) { > + mAcpiCpuData.PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE)); > + ASSERT (mAcpiCpuData.PreSmmInitRegisterTable != 0); > > - CopyRegisterTable ( > - (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.PreSmmInitRegisterTable, > - (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->PreSmmInitRegisterTable, > - mAcpiCpuData.NumberOfCpus > - ); > + CopyRegisterTable ( > + (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.PreSmmInitRegisterTable, > + (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->PreSmmInitRegisterTable, > + mAcpiCpuData.NumberOfCpus > + ); > + } else { > + mAcpiCpuData.PreSmmInitRegisterTable = 0; > + } > > - mAcpiCpuData.RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE)); > - ASSERT (mAcpiCpuData.RegisterTable != 0); > + if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->RegisterTable, mAcpiCpuData.NumberOfCpus)) { > + mAcpiCpuData.RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE)); > + ASSERT (mAcpiCpuData.RegisterTable != 0); > > - CopyRegisterTable ( > - (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.RegisterTable, > - (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->RegisterTable, > - mAcpiCpuData.NumberOfCpus > - ); > + CopyRegisterTable ( > + (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.RegisterTable, > + (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->RegisterTable, > + mAcpiCpuData.NumberOfCpus > + ); > + } else { > + mAcpiCpuData.RegisterTable = 0; > + } > > // > // Copy AP's GDT, IDT and Machine Check handler into SMRAM. >