public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation
@ 2022-09-22 20:40 Jeff Brasen
  2022-09-22 21:33 ` Ard Biesheuvel
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Brasen @ 2022-09-22 20:40 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, abner.chang, git, quic_llindhol, Jeff Brasen

Update check for enough space to occur prior to alignment offset.
This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom.

Change-Id: I58c5d378523c881a4afc655e7ace4c009130c781
---
 .../MemoryAllocationLib.c                     | 48 ++++++++-----------
 1 file changed, 21 insertions(+), 27 deletions(-)

diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
index 2cc2a71121..08a0add340 100644
--- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
+++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
@@ -23,41 +23,35 @@ InternalAllocatePages (
   )
 {
   EFI_PEI_HOB_POINTERS  Hob;
-  EFI_PHYSICAL_ADDRESS  Offset;
+  EFI_PHYSICAL_ADDRESS  NewTop;
 
   Hob.Raw = GetHobList ();
 
-  // Check to see if on 4k boundary
-  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
-  if (Offset != 0) {
-    // If not aligned, make the allocation aligned.
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
-  }
+  NewTop  = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
+  NewTop -= Pages * EFI_PAGE_SIZE;
 
   //
   // Verify that there is sufficient memory to satisfy the allocation
   //
-  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
-    return 0;
-  } else {
-    //
-    // Update the PHIT to reflect the memory usage
-    //
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
-
-    // This routine used to create a memory allocation HOB a la PEI, but that's not
-    // necessary for us.
-
-    //
-    // Create a memory allocation HOB.
-    //
-    BuildMemoryAllocationHob (
-      Hob.HandoffInformationTable->EfiFreeMemoryTop,
-      Pages * EFI_PAGE_SIZE,
-      MemoryType
-      );
-    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
+  if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
+    return NULL;
   }
+
+  //
+  // Update the PHIT to reflect the memory usage
+  //
+  Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
+
+  //
+  // Create a memory allocation HOB.
+  //
+  BuildMemoryAllocationHob (
+    Hob.HandoffInformationTable->EfiFreeMemoryTop,
+    Pages * EFI_PAGE_SIZE,
+    MemoryType
+    );
+
+  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
 }
 
 /**
-- 
2.25.1


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

* Re: [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation
  2022-09-22 20:40 Jeff Brasen
@ 2022-09-22 21:33 ` Ard Biesheuvel
  0 siblings, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2022-09-22 21:33 UTC (permalink / raw)
  To: Jeff Brasen; +Cc: devel, ardb+tianocore, abner.chang, git, quic_llindhol

On Thu, 22 Sept 2022 at 22:40, Jeff Brasen <jbrasen@nvidia.com> wrote:
>
> Update check for enough space to occur prior to alignment offset.
> This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom.
>
> Change-Id: I58c5d378523c881a4afc655e7ace4c009130c781

Thanks for respinning this. Care to add a signed-off-by ?


> ---
>  .../MemoryAllocationLib.c                     | 48 ++++++++-----------
>  1 file changed, 21 insertions(+), 27 deletions(-)
>
> diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> index 2cc2a71121..08a0add340 100644
> --- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> +++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> @@ -23,41 +23,35 @@ InternalAllocatePages (
>    )
>  {
>    EFI_PEI_HOB_POINTERS  Hob;
> -  EFI_PHYSICAL_ADDRESS  Offset;
> +  EFI_PHYSICAL_ADDRESS  NewTop;
>
>    Hob.Raw = GetHobList ();
>
> -  // Check to see if on 4k boundary
> -  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
> -  if (Offset != 0) {
> -    // If not aligned, make the allocation aligned.
> -    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
> -  }
> +  NewTop  = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
> +  NewTop -= Pages * EFI_PAGE_SIZE;
>
>    //
>    // Verify that there is sufficient memory to satisfy the allocation
>    //
> -  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
> -    return 0;
> -  } else {
> -    //
> -    // Update the PHIT to reflect the memory usage
> -    //
> -    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
> -
> -    // This routine used to create a memory allocation HOB a la PEI, but that's not
> -    // necessary for us.
> -
> -    //
> -    // Create a memory allocation HOB.
> -    //
> -    BuildMemoryAllocationHob (
> -      Hob.HandoffInformationTable->EfiFreeMemoryTop,
> -      Pages * EFI_PAGE_SIZE,
> -      MemoryType
> -      );
> -    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
> +  if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
> +    return NULL;
>    }
> +
> +  //
> +  // Update the PHIT to reflect the memory usage
> +  //
> +  Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
> +
> +  //
> +  // Create a memory allocation HOB.
> +  //
> +  BuildMemoryAllocationHob (
> +    Hob.HandoffInformationTable->EfiFreeMemoryTop,
> +    Pages * EFI_PAGE_SIZE,
> +    MemoryType
> +    );
> +
> +  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
>  }
>
>  /**
> --
> 2.25.1
>

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

* [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation
@ 2022-09-23 15:56 Jeff Brasen
  2022-09-23 17:58 ` Ard Biesheuvel
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Brasen @ 2022-09-23 15:56 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, abner.chang, git, quic_llindhol, Jeff Brasen

Update check for enough space to occur prior to alignment offset.
This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom.

Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
---
 .../MemoryAllocationLib.c                     | 48 ++++++++-----------
 1 file changed, 21 insertions(+), 27 deletions(-)

diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
index 2cc2a71121..08a0add340 100644
--- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
+++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
@@ -23,41 +23,35 @@ InternalAllocatePages (
   )
 {
   EFI_PEI_HOB_POINTERS  Hob;
-  EFI_PHYSICAL_ADDRESS  Offset;
+  EFI_PHYSICAL_ADDRESS  NewTop;
 
   Hob.Raw = GetHobList ();
 
-  // Check to see if on 4k boundary
-  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
-  if (Offset != 0) {
-    // If not aligned, make the allocation aligned.
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
-  }
+  NewTop  = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
+  NewTop -= Pages * EFI_PAGE_SIZE;
 
   //
   // Verify that there is sufficient memory to satisfy the allocation
   //
-  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
-    return 0;
-  } else {
-    //
-    // Update the PHIT to reflect the memory usage
-    //
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
-
-    // This routine used to create a memory allocation HOB a la PEI, but that's not
-    // necessary for us.
-
-    //
-    // Create a memory allocation HOB.
-    //
-    BuildMemoryAllocationHob (
-      Hob.HandoffInformationTable->EfiFreeMemoryTop,
-      Pages * EFI_PAGE_SIZE,
-      MemoryType
-      );
-    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
+  if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
+    return NULL;
   }
+
+  //
+  // Update the PHIT to reflect the memory usage
+  //
+  Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
+
+  //
+  // Create a memory allocation HOB.
+  //
+  BuildMemoryAllocationHob (
+    Hob.HandoffInformationTable->EfiFreeMemoryTop,
+    Pages * EFI_PAGE_SIZE,
+    MemoryType
+    );
+
+  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
 }
 
 /**
-- 
2.25.1


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

* Re: [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation
  2022-09-23 15:56 [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation Jeff Brasen
@ 2022-09-23 17:58 ` Ard Biesheuvel
  0 siblings, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2022-09-23 17:58 UTC (permalink / raw)
  To: Jeff Brasen; +Cc: devel, ardb+tianocore, abner.chang, git, quic_llindhol

On Fri, 23 Sept 2022 at 17:56, Jeff Brasen <jbrasen@nvidia.com> wrote:
>
> Update check for enough space to occur prior to alignment offset.
> This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom.
>
> Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>

Merged as #3396

Thanks,

> ---
>  .../MemoryAllocationLib.c                     | 48 ++++++++-----------
>  1 file changed, 21 insertions(+), 27 deletions(-)
>
> diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> index 2cc2a71121..08a0add340 100644
> --- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> +++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> @@ -23,41 +23,35 @@ InternalAllocatePages (
>    )
>  {
>    EFI_PEI_HOB_POINTERS  Hob;
> -  EFI_PHYSICAL_ADDRESS  Offset;
> +  EFI_PHYSICAL_ADDRESS  NewTop;
>
>    Hob.Raw = GetHobList ();
>
> -  // Check to see if on 4k boundary
> -  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
> -  if (Offset != 0) {
> -    // If not aligned, make the allocation aligned.
> -    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
> -  }
> +  NewTop  = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
> +  NewTop -= Pages * EFI_PAGE_SIZE;
>
>    //
>    // Verify that there is sufficient memory to satisfy the allocation
>    //
> -  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
> -    return 0;
> -  } else {
> -    //
> -    // Update the PHIT to reflect the memory usage
> -    //
> -    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
> -
> -    // This routine used to create a memory allocation HOB a la PEI, but that's not
> -    // necessary for us.
> -
> -    //
> -    // Create a memory allocation HOB.
> -    //
> -    BuildMemoryAllocationHob (
> -      Hob.HandoffInformationTable->EfiFreeMemoryTop,
> -      Pages * EFI_PAGE_SIZE,
> -      MemoryType
> -      );
> -    return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
> +  if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
> +    return NULL;
>    }
> +
> +  //
> +  // Update the PHIT to reflect the memory usage
> +  //
> +  Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
> +
> +  //
> +  // Create a memory allocation HOB.
> +  //
> +  BuildMemoryAllocationHob (
> +    Hob.HandoffInformationTable->EfiFreeMemoryTop,
> +    Pages * EFI_PAGE_SIZE,
> +    MemoryType
> +    );
> +
> +  return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
>  }
>
>  /**
> --
> 2.25.1
>

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

end of thread, other threads:[~2022-09-23 17:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-23 15:56 [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation Jeff Brasen
2022-09-23 17:58 ` Ard Biesheuvel
  -- strict thread matches above, loose matches on Subject: below --
2022-09-22 20:40 Jeff Brasen
2022-09-22 21:33 ` Ard Biesheuvel

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