<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Brian,<div class=""><br class=""></div><div class="">I think all the PEI Allocate Pool does is make a HOB [1]. I don’t think we can remove HOBs when memory is freed as that would change  pointers to pool. It may be possible to mark a region as free and allocate from that list batch 1st, and just over allocate space if needed. It could be as simple as just adding a new HOB type to represent free pool. </div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">[1] <a href="https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878" class="">https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878</a></div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">Andrew Fish<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 22, 2022, at 12:39 PM, Brian J. Johnson <<a href="mailto:brian.johnson@hpe.com" class="">brian.johnson@hpe.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
  
  <div class="">
    <div class="moz-cite-prefix">
      <div id="rwhMsgHeader" class="">Sorry for the late response to this
        thread...</div>
      <div class=""><br class="">
      </div>
      <div class="">PEI has grown greatly in complexity, as Andrew pointed out. 
        Do we (TianoCore community) think it's time to add a real pool
        manager to PEI?  There's getting to be quite a bit of
        post-DRAM-initialization PEI code on some platforms.  And
        really, having a limited amount of pre-DRAM memory available is
        an even better reason for having an effective pool allocator, so
        the memory can be freed and reused.</div>
      <div class=""><br class="">
      </div>
      <div class="">FWIW we (HPE) needed to add a private pool allocator to
        manage memory for a proprietary h/w initialization module which
        needs to run in post-DRAM PEI, and depends on allocating and
        freeing memory in different phases of its operation.<br class="">
      </div>
      <div class=""><br class="">
      </div>
      <div class="">Brian J. Johnson<br class="">
      </div>
      <div class=""><br class="">
        <hr id="rwhMsgHdrDivider" style="border:0;border-top:1px solid
          #B5C4DF;padding:0;margin:10px 0 5px 0;width:100%;" class="">
        <div style="font-family: sans-serif !important; font-size: 11pt !important;" class=""><b class="">From:</b> Ayush
          Singh [<a class="moz-txt-link-freetext" href="mailto:ayushdevel1325@gmail.com">mailto:ayushdevel1325@gmail.com</a>]</div>
        <div style="font-family: sans-serif !important; font-size: 11pt !important;" class=""><b class="">Sent:</b> Friday,
          June 10, 2022, 12:22 AM</div>
        <div style="font-family: sans-serif !important; font-size: 11pt !important;" class=""><b class="">To:</b> Andrew Fish
          <a class="moz-txt-link-rfc2396E" href="mailto:afish@apple.com"><afish@apple.com></a></div>
        <div style="font-family: sans-serif !important; font-size: 11pt !important;" class=""><b class="">Cc:</b>
          <a class="moz-txt-link-abbreviated" href="mailto:devel@edk2.groups.io">devel@edk2.groups.io</a></div>
        <div style="font-family: sans-serif !important; font-size: 11pt !important;" class=""><b class="">Subject:</b>
          [edk2-devel] Clarification of Memory management in PEI phase</div>
        <br class="">
      </div>
    </div>
    <blockquote type="cite" cite="mid:ULX8DR.0063NOJGICAW1@gmail.com" style="border:none !important; margin-left:0px !important;
      margin-right:0px !important; margin-top:0px !important;
      padding-left:0px !important; padding-right:0px !important" class="">
      
      <div id="geary-body" dir="auto" class="">
        <div class="">Thanks for the wonderful answer. </div>
        <div class=""><br class="">
        </div>
        <div class="">Ayush Singh</div>
      </div>
      <div id="geary-quote" dir="auto" class=""><br class="">
        On Thu, Jun 9 2022 at 01:26:58 PM -0700, Andrew Fish
        <a class="moz-txt-link-rfc2396E" href="mailto:afish@apple.com"><afish@apple.com></a> wrote:<br class="">
        <blockquote type="cite" style="border:none !important;
          margin-left:0px !important; margin-right:0px !important;
          margin-top:0px !important; padding-left:0px !important;
          padding-right:0px !important" class="">
          <div class="plaintext" style="white-space: break-spaces;">
<blockquote style="border:none !important; margin-left:0px !important; margin-right:0px !important; margin-top:0px !important; padding-left:0px !important; padding-right:0px !important" class=""> On Jun 9, 2022, at 10:28 AM, Ayush Singh <<a href="mailto:ayushdevel1325@gmail.com" moz-do-not-send="true" class="moz-txt-link-freetext">ayushdevel1325@gmail.com</a>wrote:
 
 Hello everyone,
 
 Can anyone help me with understanding dynamic memory management in PEI
 phase? In the UEFI Platform Integration Specification, version 1.7
 Errata A, Section 4.6, PEI Memory services are given which include:
 
 1. InstallPeiMemory()
</blockquote>
This is basically:
(*PeiServices)->InstallPeiMemory (PeiServices, MemoryBegin, MemoryLength);

This is how you tell the PEI Core the location of the memory that will can be used in PEI. 

<blockquote style="border:none !important; margin-left:0px !important; margin-right:0px !important; margin-top:0px !important; padding-left:0px !important; padding-right:0px !important" class=""> 2. AllocatePages()
 3. AllocatePool()
 4. CopyMem()
 5. SetMem()
 6. FreePages()
 
 However, no `FreePool()` service seems to be present. So how is the
 memory allocated using `AllocatePool()` freed?
 
</blockquote>
It basically gets Freed when you transition to the DXE phase. 

To step back for a minute I think it is important to remember that the main job of PEI is to initialize DRAM, and deal with S3 (resuming from suspend to RAM). So as soon as you have DRAM you are kind done and ready for the DXE IPL so you can load the DXE Phase and start up EFI. Remember PEI is Pre EFI. The reality is programming DRAM is complex and lots of code got written, then lots more code got written and PEI has become large for some ports. That was never the intent. PEI is designed as a way to run C code when you code is running from ROM and you donā��t have any DRAM. For x86 not having DRAM means you are using the cache as RAM. For some SoCs there is actually an SRAM you can use. Thus the PEI memory allocation scheme is designed to deal with this very constrained environment. 

You start PEI with a heap and stack. You can also allocate HOBs (Hand Off Blocks). A pool allocation in PEI is just a HOB. See [1]. There is no way to free a HOB. So the AllocatePool() kind of leaks into DXE too as an entry in the HOB list. But when the OS called gBS->ExitBootServices() that frees all non runtime memory back to the OS. 

If you look a AllocatePages/FreePages you will see AllocatePages creates a HOB that points to the memory region, and FreePages just marks that HOB as not used. That code is also in this file [1]. 

TL;DR there is no pool manager in PEI. 

[1] <a href="https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878" moz-do-not-send="true" class="moz-txt-link-freetext">https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878</a>


Thanks,

Andrew Fish

<blockquote style="border:none !important; margin-left:0px !important; margin-right:0px !important; margin-top:0px !important; padding-left:0px !important; padding-right:0px !important" class=""> Ayush Singh
 
 
 
 
 
</blockquote>
</div>
        </blockquote>
      </div>
      
    </blockquote>
    <div class="moz-signature" signature-switch-id="84333aeb-5735-4b50-bbd8-46184f1ade9a">
      <div style="text-indent: 65%" class="">Brian</div>
      <br class="">
------------------------------------------------------------------------------------------------------------------------------<br class="">
      <pre class="">   "It's OK to be stuck.  99% of the time in your own [research] work, 
    you're stuck."
                                           -- Mark Lawrence
</pre>
    </div>
  </div>

</div></blockquote></div><br class=""></div></body></html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr>   Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/90698">View/Reply Online (#90698)</a> |    |  <a target="_blank" href="https://groups.io/mt/91651322/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>