summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorduntan <dun.tan@intel.com>2023-05-18 15:31:01 +0800
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2023-06-21 08:00:04 +0000
commitfcd71642df9a3a5932a2db116acc6fe23458d8f5 (patch)
tree800f29e0162ff5128703c71c162b943da1c6a442
parent56f65e4bad770b0d33eb54b3c180c9ec5057d8f2 (diff)
downloadedk2-fcd71642df9a3a5932a2db116acc6fe23458d8f5.tar.gz
edk2-fcd71642df9a3a5932a2db116acc6fe23458d8f5.tar.bz2
edk2-fcd71642df9a3a5932a2db116acc6fe23458d8f5.zip
MdePkg: Code optimization to SMM InternalAllocateAlignedPages
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 <dun.tan@intel.com> Cc: Ray Ni <ray.ni@intel.com> Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com> Cc: Liming Gao <gaoliming@byosoft.com.cn> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
-rw-r--r--MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c2
1 files changed, 1 insertions, 1 deletions
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.
//