From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web11.11657.1684395091896332465 for ; Thu, 18 May 2023 00:31:31 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=mT3UM77G; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: dun.tan@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684395091; x=1715931091; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=pMklTojeooaRDBOImENdzncyF1b/os68dmPKynl0cZY=; b=mT3UM77GcoKI9lTGfuLKqgAuuRSsQnA61z3L/qRQRXrNbp1MrWClRYXh RP8VW1c+4FGWBx0kdFYxh42+ZfNiy9hSl9lJuBTlKIUNA8ejcFZ+wNofW qutWAwPK2IPbn1+L2kJgNKYahQXEvLYStUJy1AP1itJNZSLv5PAWmRZbF 8aXMBb0xPIh6MBqKr7kwFmT8dzUfyTcIlG38o2BH+q7oK8GZAfdkMf2D/ wJYQo6tuAhIVJ+XWcGvE1pc/w+PgcbucG0y2wSHzCe1OH+bnv75M7xKSN GiiNvWa4IYuTKhfU4hRVPYSF2cRkDOjL0ta1VBNxZ6fX6YQaRUi2gJY/r A==; X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="438337203" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="438337203" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 00:31:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10713"; a="948590538" X-IronPort-AV: E=Sophos;i="5.99,284,1677571200"; d="scan'208";a="948590538" Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.158]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 00:31:10 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages Date: Thu, 18 May 2023 15:31:01 +0800 Message-Id: <20230518073101.1838-1-dun.tan@intel.com> X-Mailer: git-send-email 2.31.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 allocated 4 pages. If the upper limit address of allocated 4 pages 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 low address, then the higher 2 pages cann't be merged with 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 upper limit address of allocated 3 pages is 8KB-aligned, then the higher 2 pages range of allocated 3 pages is 8KB-aligned and will be selected and removed from free pages. The remaining lower one page of allocated 3 pages 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 = Alignment - 1; - RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment) - 1; // // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. // -- 2.31.1.windows.1