From nobody Sat May 18 15:49:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+105009+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+105009+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1684391901; cv=none; d=zohomail.com; s=zohoarc; b=fB7WqdjGU9LWG+7xoQLTT/DQ4PpTzWNvuo14xpaiKes+b24xJH8DZoT1efcLkE36KB+LZODqEQtlHtxSL9lknF8VZSVHey3xhJbR49ecUQ7TyAGXX/VqnHqplAEg4JkGVKAQXB5NZPgKfqCzlxRqYykplOCQTHLxGXuq+eJs67s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684391901; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=JZ1/BG5P7qka8FLfvhm1uuLMK4ylA7gPuzQF7n3hH8I=; b=FeECDmdv3UGnBGzIyecTfKTpIDH0QlIGkbISyMHKO+6LUvUt4CE7VpE/OgXu5SLEB3zyI28a0fMxzd/G8gAPlm+WvDFD/k5PIZvzifxVI9NPtkKo6uUrEqXNhHE3iO8evj6Rh4N8FApkLNBYwmG2GxGrL/AZlm75GbEIV41KHck= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+105009+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1684391901572665.1429040969319; Wed, 17 May 2023 23:38:21 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id YWNuYY1788612xRlyw3W89KV; Wed, 17 May 2023 23:38:21 -0700 X-Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web11.11053.1684391899637907147 for ; Wed, 17 May 2023 23:38:20 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="332348532" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="332348532" X-Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2023 23:38:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="791834792" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="791834792" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.158]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2023 23:38:17 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [edk2-devel] [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages Date: Thu, 18 May 2023 14:37:15 +0800 Message-Id: <20230518063715.1703-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dun.tan@intel.com X-Gm-Message-State: 1NNsSgGtz4Mm5gtoSj2u8n0Rx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1684391901; bh=C+f2eAylLdPDojZcYglwMT8VZeKPn8QuuMmlP62fkOI=; h=Cc:Date:From:Reply-To:Subject:To; b=H2gBI3lET+srNJeqP0Gjy8ZRG55JAXtGiJWukYI01eghtijhb91XXsB9Ap43eMesbno /06r28cDJaR3ORJu2Lu9DN6aOFIlgGpXbRQOpY2d68V/sVUOgo7qwmcLGrXJebNG1SsRN AZUKNnha06WRE6nuXYWVWPg5B+eMnW2tcWA= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1684391902159100001 Content-Type: text/plain; charset="utf-8" This commit is code optimization to InternalAllocateAlignedPages of SmmMemoryAllocationLib which can reduce free memory fragments. Also it can reduce one pre-allocation page. Let's take a simple example: The expected pages size is 8KB, Alignment value is 8KB. In original InternalAllocateAlignedPages(), the first step is to allocate 4 pages and then find the first 8KB-aligned address in the allocated 4 pages. If the limit address is already 8KB aligned, then the allocated 4 pages contains two 8KB-aligned 8KB ranges. The lower 2 pages will be selected and removed from free pages. Then the higher 2 pages will be free. Since the whole memory allocation is from high address to lower address, then the higher 2 pages cann't be merged other free pages, causing the free memory fragments. However, when only allocate 3(2+2-1) pages, we can avoid the free memory fragments in specific case. Also 3 pages must contain a 8kb-aligned 8kb range, which meets the requirement. If the limit of allocated 3 pages is 8KB-aligned, then the last 8K range will be selected and removed from free pages. The lower 4Kb will be free and merged with left lower free memory. This can reduce free memory fragments in smm. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu --- MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c b/= MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c index 3ab2c1ebfd..99a8259325 100644 --- a/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c @@ -322,7 +322,7 @@ InternalAllocateAlignedPages ( // Calculate the total number of pages since alignment is larger than = page size. // AlignmentMask =3D Alignment - 1; - RealPages =3D Pages + EFI_SIZE_TO_PAGES (Alignment); + RealPages =3D Pages + EFI_SIZE_TO_PAGES (Alignment) - 1; // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not ov= erflow. // --=20 2.31.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#105009): https://edk2.groups.io/g/devel/message/105009 Mute This Topic: https://groups.io/mt/98986400/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-