From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=PTpOVydO; spf=pass (domain: intel.com, ip: 192.55.52.151, 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=1684391899; x=1715927899; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=HnBjrlCbguUM0QtFouuVaSvS/SbqSbWwBQCkRlaXlyE=; b=PTpOVydOxeWvOlgk2Y0vNJenI0QsQSkWcq8xlC0foTtioUd0/+H8cPnt R1stc4QVlvTDqpALnXK9gC6zmaG08reaoTH3IsysvVlGz+rbs874U/Fpx bYiWgQMDBdOf2HH+4PzsfiIJULddV6tHtij+3OQdBTCFXvAFHRVOgL3Ci ymDRHLeMIbg0TzbmhO21MEk8EuHaimsWWlZaxOff3GVXPOamYwlPMlqR9 EFDV2V50z2HCKs78od2nXARDMFgoiIvhUtspzfnxxP3mhOZYaFlQMBQlL dcxjwdBP8G1eZ4Ag+NegOufcgZ2EMnm6+hLM+6DLcJyV45sea0T/jbqvB Q==; 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" 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" 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: [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> 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 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 = 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