From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web08.654.1663955930840138447 for ; Fri, 23 Sep 2022 10:58:51 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aodC9TEl; spf=pass (domain: kernel.org, ip: 145.40.68.75, 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 ams.source.kernel.org (Postfix) with ESMTPS id D5665B8231F for ; Fri, 23 Sep 2022 17:58:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F20FC43470 for ; Fri, 23 Sep 2022 17:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663955927; bh=/CroLl2cAMUDdpIBMZzpHcHXSgh+xDm1aOSeISuwYy0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=aodC9TElMx9/Kbm5FnBVAobVQ8pCpA3Y9eBXHgRFHPApiAYva3ds88eWL6XmBpwBM Cz+kFJGVjOBZUHFq/P+M7wUuuflW7OuY49SCZqtBPdLYycbAjkFoIHaH+pWAmdWO/a P/5NtRQVVW/k9uQVAUXtRknwzGMHYsYjz450xWTZOPzG78C0yw+x6OX6aZh9ZpYPPq DHm2lu4jGlF10pf5NSIegrDIpZDb2AlCb9mbE/2Lgj9WSXAiAeYmOoqjwl/DEyFKy7 74tbLoT0EGMx9+YTt4Ni4eNBSktd4DT3vqTd8m27ejD0cpuQJ2HQ0a9Hl/K+4JWVnb 3Udf45dXz24XQ== Received: by mail-lf1-f49.google.com with SMTP id w8so1410325lft.12 for ; Fri, 23 Sep 2022 10:58:47 -0700 (PDT) X-Gm-Message-State: ACrzQf0Z+SRhXJoLfKvcnvBysVAXWgtFTJSrl7UIJnPhDZRwzrSHNC3g hQC2FA7m5jtcF4X1DbmjwII3hGFBV+BLaANjzWE= X-Google-Smtp-Source: AMsMyM7aa109CSZkWYwHN8shAb4ouPqaHQksXrVXt4DVJoyhoyV/0mQ91xehWrG5+hHYL0OLrqs5S973Va/ApywoDXE= X-Received: by 2002:a05:6512:13a1:b0:48d:f14:9059 with SMTP id p33-20020a05651213a100b0048d0f149059mr4056239lfa.110.1663955925639; Fri, 23 Sep 2022 10:58:45 -0700 (PDT) MIME-Version: 1.0 References: <38bfcc0559d4b02b629c07fbdc434e5f1971e0ff.1663948563.git.jbrasen@nvidia.com> In-Reply-To: <38bfcc0559d4b02b629c07fbdc434e5f1971e0ff.1663948563.git.jbrasen@nvidia.com> From: "Ard Biesheuvel" Date: Fri, 23 Sep 2022 19:58:30 +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 Fri, 23 Sept 2022 at 17:56, Jeff Brasen wrote: > > Update check for enough space to occur prior to alignment offset. > This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom. > > Signed-off-by: Jeff Brasen 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 >