From: "Jeff Brasen" <jbrasen@nvidia.com>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: "devel@edk2.groups.io" <devel@edk2.groups.io>,
"ardb+tianocore@kernel.org" <ardb+tianocore@kernel.org>,
"abner.chang@amd.com" <abner.chang@amd.com>,
"git@danielschaefer.me" <git@danielschaefer.me>,
"quic_llindhol@quicinc.com" <quic_llindhol@quicinc.com>
Subject: Re: [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for space on offset allocation
Date: Wed, 7 Sep 2022 20:56:07 +0000 [thread overview]
Message-ID: <DS7PR12MB5789339DC769D01CDB9B4F85CB419@DS7PR12MB5789.namprd12.prod.outlook.com> (raw)
In-Reply-To: <CAMj1kXFtYpx-6oeybrpOhtT18cXUFurhGyFoH1Pmm=85wJ5CqQ@mail.gmail.com>
> -----Original Message-----
> From: Ard Biesheuvel <ardb@kernel.org>
> Sent: Wednesday, September 7, 2022 10:16 AM
> To: Jeff Brasen <jbrasen@nvidia.com>
> Cc: devel@edk2.groups.io; ardb+tianocore@kernel.org;
> abner.chang@amd.com; git@danielschaefer.me; quic_llindhol@quicinc.com
> Subject: Re: [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for
> space on offset allocation
>
> External email: Use caution opening links or attachments
>
>
> On Wed, 7 Sept 2022 at 17:46, Jeff Brasen <jbrasen@nvidia.com> wrote:
> >
> > Update check for enough space to occur prior to alignment offset
> > modification. This prevents a case where EfiFreeMemoryTop could be
> > less than EfiFreeMemoryBottom
> >
> > Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
>
> Thanks for the respin. I have caught up in the mean time.
>
>
> > ---
> > .../MemoryAllocationLib.c | 53 +++++++++++--------
> > 1 file changed, 30 insertions(+), 23 deletions(-)
> >
> > diff --git
> >
> a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> >
> b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> > index 2cc2a71121..9208826565 100644
> > ---
> >
> a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> > +++
> b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib
> > +++ .c
> > @@ -27,37 +27,44 @@ InternalAllocatePages (
> >
> > Hob.Raw = GetHobList ();
> >
> > - // Check to see if on 4k boundary
> > Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
> > + //
> > + // Verify that there is sufficient memory to satisfy the allocation
> > + and padding prior to updating anything // if
> > + ((Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages *
> EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) - Offset) <
> Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
> > + if (Offset != 0) {
> > + DEBUG ((DEBUG_ERROR, "Offset applied without enough
> space\r\n"));
> > + } else {
> > + DEBUG ((DEBUG_ERROR, "Out of memory\r\n"));
> > + }
> > +
> > + ASSERT (FALSE);
> > + return 0;
> > + }
> > +
> > + // Check to see if on 4k boundary
> > if (Offset != 0) {
> > // If not aligned, make the allocation aligned.
> > Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
> > }
> >
>
> I understand how you are trying to stick with the original code as much as
> possible, but this is all extremely clunky, and I'd prefer we just clean it up, if
> you don't mind.
>
> Would something like the below work for you as well?
Yup this looks good to me and cleaner as well.
>
>
>
> diff --git
> a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> index 2cc2a7112197..547117dc13d6 100644
> ---
> a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> +++
> b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
> @@ -23,41 +23,36 @@ 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;
> }
>
> /**
prev parent reply other threads:[~2022-09-07 20:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-07 15:45 [PATCH v2] EmbeddedPkg/PrePiMemoryAllocationLib: Check for space on offset allocation Jeff Brasen
2022-09-07 16:16 ` Ard Biesheuvel
2022-09-07 20:56 ` Jeff Brasen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DS7PR12MB5789339DC769D01CDB9B4F85CB419@DS7PR12MB5789.namprd12.prod.outlook.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox