From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web12.853.1663882410257898103 for ; Thu, 22 Sep 2022 14:33:30 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=W8BHRIhG; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8C3B862D78 for ; Thu, 22 Sep 2022 21:33:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F034CC43140 for ; Thu, 22 Sep 2022 21:33:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663882409; bh=1VhIN5vHsIrjOATL8cDGBZhXpgLW1dLg/eGcWLqagas=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=W8BHRIhGJnOoCk9aFBX1zZixSa0ztx3bH2YI3BxplyYx/OsaHIaL0MMDUNhC7mvos eo0f3L6YAE1NbJzNAZ5eXyjzXIYsvLnP7yGbSWInhoYagOhcSTpUOQvB+++6PUo9k7 m/1rYQCXzt31IVrpmtTBLxtaBjBpHbRAzO5y/ZLHvHt+efX2uF8N2iawMXaoa1XfuR WyysrYHSOtQEWpI3kiBvx4GnMh9f4rXj7Ks6/Zz2Aet9Igg5uRGOla2rxf89Rt9tjO TZCaBotUfx8jHQPkM/Sb6oquh4iaMYX168jzt184eej2SIdJUrbkqvDsDva5LNmw+f iPmH3A0aZo+9g== Received: by mail-lf1-f54.google.com with SMTP id a3so16763052lfk.9 for ; Thu, 22 Sep 2022 14:33:28 -0700 (PDT) X-Gm-Message-State: ACrzQf0weRZ/mRGCVqASdk02aa46gOp/FG7KcIIoYmi+tqJOiBANYu5C Ij8ZR0WSgO2G/zhc4TOJGae+X7OQiJKYjQ80i2E= X-Google-Smtp-Source: AMsMyM74i2pGSM4IBb7QBiBfqWLo4HsYB632NGsvnHI2BjYc1zHI3oQNrOmSQVUGroGFKum61qZ2MbICTRZmuEuE2aA= X-Received: by 2002:ac2:4431:0:b0:497:aaf5:83eb with SMTP id w17-20020ac24431000000b00497aaf583ebmr2014266lfl.228.1663882406986; Thu, 22 Sep 2022 14:33:26 -0700 (PDT) MIME-Version: 1.0 References: <38bfcc0559d4b02b629c07fbdc434e5f1971e0ff.1663879160.git.jbrasen@nvidia.com> In-Reply-To: <38bfcc0559d4b02b629c07fbdc434e5f1971e0ff.1663879160.git.jbrasen@nvidia.com> From: "Ard Biesheuvel" Date: Thu, 22 Sep 2022 23:33:15 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation To: Jeff Brasen Cc: devel@edk2.groups.io, ardb+tianocore@kernel.org, abner.chang@amd.com, git@danielschaefer.me, quic_llindhol@quicinc.com Content-Type: text/plain; charset="UTF-8" On Thu, 22 Sept 2022 at 22:40, Jeff Brasen 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 >