[edk2-devel] [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages

duntan posted 1 patch 11 months, 3 weeks ago
Failed in applying to current master (apply log)
There is a newer version of this series
MdePkg/Library/SmmMemoryAllocationLib/MemoryAllocationLib.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[edk2-devel] [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
Posted by duntan 11 months, 3 weeks ago
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 <dun.tan@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
---
 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



-=-=-=-=-=-=-=-=-=-=-=-
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]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
Posted by Ni, Ray 11 months, 3 weeks ago
Dun,
I can understand what you are trying to describe in the commit message.
I have some suggestions/questions that might help you refine the commit message a bit better.

Code changes look good to me.

Thanks,
Ray
> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Thursday, May 18, 2023 2:37 PM
> To: devel@edk2.groups.io
> Cc: Ni, Ray <ray.ni@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu,
> Zhiguang <zhiguang.liu@intel.com>
> Subject: [PATCH] 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 the
> allocated 4 pages. If the limit address is already 8KB aligned, then
1. What's "limit address"?

> 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
2. What's "last 8k"?


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#105010): https://edk2.groups.io/g/devel/message/105010
Mute This Topic: https://groups.io/mt/98986400/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3901457/1787277/102458076/xyzzy [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
Posted by duntan 11 months, 3 weeks ago
Ray,
Thanks for the comments. I'll refine the commit message according to your suggestions in V2 version patch.

Thanks,
Dun

-----Original Message-----
From: Ni, Ray <ray.ni@intel.com> 
Sent: Thursday, May 18, 2023 2:59 PM
To: Tan, Dun <dun.tan@intel.com>; devel@edk2.groups.io
Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
Subject: RE: [PATCH] MdePkg: Code optimization to SMM InternalAllocateAlignedPages

Dun,
I can understand what you are trying to describe in the commit message.
I have some suggestions/questions that might help you refine the commit message a bit better.

Code changes look good to me.

Thanks,
Ray
> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Thursday, May 18, 2023 2:37 PM
> To: devel@edk2.groups.io
> Cc: Ni, Ray <ray.ni@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; 
> Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: [PATCH] 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 the 
> allocated 4 pages. If the limit address is already 8KB aligned, then
1. What's "limit address"?

> 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
2. What's "last 8k"?


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#105011): https://edk2.groups.io/g/devel/message/105011
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]
-=-=-=-=-=-=-=-=-=-=-=-