public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
@ 2023-05-18  7:31 duntan
  2023-05-18  8:55 ` Ni, Ray
  0 siblings, 1 reply; 4+ messages in thread
From: duntan @ 2023-05-18  7:31 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Michael D Kinney, Liming Gao, Zhiguang Liu

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>
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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
  2023-05-18  7:31 [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages duntan
@ 2023-05-18  8:55 ` Ni, Ray
  2023-05-18 15:37   ` Michael D Kinney
  0 siblings, 1 reply; 4+ messages in thread
From: Ni, Ray @ 2023-05-18  8:55 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io
  Cc: Kinney, Michael D, Gao, Liming, Liu, Zhiguang

Reviewed-by: Ray Ni <ray.ni@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Thursday, May 18, 2023 3:31 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 V2] 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>
> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
  2023-05-18  8:55 ` Ni, Ray
@ 2023-05-18 15:37   ` Michael D Kinney
  2023-05-18 23:30     ` Ni, Ray
  0 siblings, 1 reply; 4+ messages in thread
From: Michael D Kinney @ 2023-05-18 15:37 UTC (permalink / raw)
  To: Ni, Ray, Tan, Dun, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang, Kinney, Michael D

Is this considered a critical bug for stable tag release?

Is there are HSD and if it is critical, all memory allocation lobs should be fixed.  Right?

Mike

> -----Original Message-----
> From: Ni, Ray <ray.ni@intel.com>
> Sent: Thursday, May 18, 2023 1:55 AM
> 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 V2] MdePkg: Code optimization to SMM
> InternalAllocateAlignedPages
> 
> Reviewed-by: Ray Ni <ray.ni@intel.com>
> 
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com>
> > Sent: Thursday, May 18, 2023 3:31 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 V2] 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>
> > 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages
  2023-05-18 15:37   ` Michael D Kinney
@ 2023-05-18 23:30     ` Ni, Ray
  0 siblings, 0 replies; 4+ messages in thread
From: Ni, Ray @ 2023-05-18 23:30 UTC (permalink / raw)
  To: Kinney, Michael D, Tan, Dun, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang

Mike,
No. The code is there for couple years. With that fact, I don't think it's a critical bug.

Yes. all memory allocation libs should be fixed. We start with the SMM one first to see feedbacks.

Thanks,
Ray

> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Thursday, May 18, 2023 11:38 PM
> To: Ni, Ray <ray.ni@intel.com>; Tan, Dun <dun.tan@intel.com>;
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Subject: RE: [Patch V2] MdePkg: Code optimization to SMM
> InternalAllocateAlignedPages
> 
> Is this considered a critical bug for stable tag release?
> 
> Is there are HSD and if it is critical, all memory allocation lobs should be fixed.
> Right?
> 
> Mike
> 
> > -----Original Message-----
> > From: Ni, Ray <ray.ni@intel.com>
> > Sent: Thursday, May 18, 2023 1:55 AM
> > 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 V2] MdePkg: Code optimization to SMM
> > InternalAllocateAlignedPages
> >
> > Reviewed-by: Ray Ni <ray.ni@intel.com>
> >
> > > -----Original Message-----
> > > From: Tan, Dun <dun.tan@intel.com>
> > > Sent: Thursday, May 18, 2023 3:31 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 V2] 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>
> > > 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-18 23:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-18  7:31 [Patch V2] MdePkg: Code optimization to SMM InternalAllocateAlignedPages duntan
2023-05-18  8:55 ` Ni, Ray
2023-05-18 15:37   ` Michael D Kinney
2023-05-18 23:30     ` Ni, Ray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox