From nobody Mon Feb 9 02:51:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92893+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92893+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1661581365; cv=none; d=zohomail.com; s=zohoarc; b=j8RrO3wodtRJLVanz/tPOAqct2K0C0R8CcSTVWFwM7r1aIHqqpd1wocpHyxa8L/l3gwP+HZWExtXdisds+9Qa74DabcAvUSmadd5ZfKVFE60b509GrxbndCoywSJrnxzSHyRt6ifQY5SqIwshnmbUiwhmUHk5zFIVoV9U7JIX/A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661581365; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=79CuToS/QwzICt5z8lEBfNagnIv4bs9XREF9vBSgwlE=; b=merJoITh/iTF3P69dsc3plVBGgsr4QUqafkuKqcRBkeQ4xcpwW3O+jaCtAEuP1uC9mJiqwyo76drbZN3ztuYRrEZXH82SOKQjLFTDpzXsWYT7Lf/b0wN64fKTdkMX+7XX6z/AaJmcin3mA3N3sp8ac/22AEymySlAsvO6PFaxsU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92893+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1661581365308273.82452921373715; Fri, 26 Aug 2022 23:22:45 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id F851YY1788612x5AEUcp3h5i; Fri, 26 Aug 2022 23:22:45 -0700 X-Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web12.46431.1661581332997885650 for ; Fri, 26 Aug 2022 23:22:44 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10451"; a="356355997" X-IronPort-AV: E=Sophos;i="5.93,267,1654585200"; d="scan'208";a="356355997" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 23:22:43 -0700 X-IronPort-AV: E=Sophos;i="5.93,267,1654585200"; d="scan'208";a="671738946" X-Received: from mxu9-mobl1.ccr.corp.intel.com ([10.249.171.119]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 23:22:41 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Jiaqi Gao , Jian J Wang , Liming Gao , Dandan Bi , Erdem Aktas , James Bottomley , Jiewen Yao , Tom Lendacky , Gerd Hoffmann , Min Xu Subject: [edk2-devel] [PATCH V2 14/14] MdeModulePkg: Pool and page functions accept memory when OOM occurs Date: Sat, 27 Aug 2022 14:21:21 +0800 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,min.m.xu@intel.com X-Gm-Message-State: ZdWrcIBnA13xzdBVMP8YICWzx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1661581365; bh=rd0bkKy44yTY04qyAj43Eov736Iu/dbzyMJzAs7Yopw=; h=Cc:Date:From:Reply-To:Subject:To; b=KnSy+nBBCRe8E2tiJPqf7q61tDTrNrBjH+KNJbn6jwBm+TIT2ZszhVOh3fajSXvN0Pu tpRsMLACm3fc8uMnB6A6cyH+g1bgyFsLjrkDpuwNJo8TIAVBpdRIN9s+sLWfg0Ygw4qEd uN1QXb9Lu6LhSZlRZVOq44Z4hFypip7qVKw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1661581367293100003 Content-Type: text/plain; charset="utf-8" From: Jiaqi Gao RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3937 When CoreAllocatePages() / CoreAllocatePool() meets error of EFI_OUT_OF_RESOURCES, locate the EdkiiMemoryAcceptProtocol and accept extra memory dynamically. Firstly, find the unaccpeted memory region with enough size in GCD entries. Then locate the EdkiiMemoryAcceptProtocol and accept the memory. Finally, update the GCD memory and gMemoryMap entries. After updating the memory infomation, CoreInternalAllocatePages() / CoreInternalAllocatePool() will be recalled to allocate pages / pool. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Cc: Gerd Hoffmann Signed-off-by: Jiaqi Gao Signed-off-by: Min Xu --- MdeModulePkg/Core/Dxe/DxeMain.inf | 1 + MdeModulePkg/Core/Dxe/Mem/Imem.h | 16 +++ MdeModulePkg/Core/Dxe/Mem/Page.c | 190 ++++++++++++++++++++++++++++++ MdeModulePkg/Core/Dxe/Mem/Pool.c | 14 +++ 4 files changed, 221 insertions(+) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index e4bca895773d..371ba45357be 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -169,6 +169,7 @@ gEfiVariableArchProtocolGuid ## CONSUMES gEfiCapsuleArchProtocolGuid ## CONSUMES gEfiWatchdogTimerArchProtocolGuid ## CONSUMES + gEdkiiMemoryAcceptProtocolGuid ## CONSUMES =20 [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdLoadFixAddressBootTimeCodePageNumber = ## SOMETIMES_CONSUMES diff --git a/MdeModulePkg/Core/Dxe/Mem/Imem.h b/MdeModulePkg/Core/Dxe/Mem/I= mem.h index 2f0bf2bf631f..22e0d0e44030 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Imem.h +++ b/MdeModulePkg/Core/Dxe/Mem/Imem.h @@ -47,6 +47,22 @@ typedef struct { // Internal prototypes // =20 +/** + Internal function. Used by the pool and page functions to accept memory + when OOM occurs. + + @param Type The type of allocation to perform. + @param AcceptSize Size of memory to be accepted. + @param Memory Accept memory address + +**/ +EFI_STATUS +AcceptMemoryResource ( + IN EFI_ALLOCATE_TYPE Type, + IN UINTN AcceptSize, + IN OUT EFI_PHYSICAL_ADDRESS *Memory + ); + /** Internal function. Used by the pool functions to allocate pages to back pool allocation requests. diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/P= age.c index 160289c1f9ec..513792a7fe04 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -10,6 +10,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Imem.h" #include "HeapGuard.h" #include +#include =20 // // Entry for tracking the memory regions for each memory type to coalesce = similar memory types @@ -379,6 +380,176 @@ CoreFreeMemoryMapStack ( mFreeMapStack -=3D 1; } =20 +/** + Used to accept memory when OOM occurs. + + @param Type The type of allocation to perform. + @param AcceptSize Size of memory to be accepted. + @param Memory Accept memory address + +**/ +EFI_STATUS +AcceptMemoryResource ( + IN EFI_ALLOCATE_TYPE Type, + IN UINTN AcceptSize, + IN OUT EFI_PHYSICAL_ADDRESS *Memory + ) +{ + #ifdef MDE_CPU_X64 + + LIST_ENTRY *Link; + EFI_GCD_MAP_ENTRY *GcdEntry; + EFI_GCD_MAP_ENTRY UnacceptedEntry; + EDKII_MEMORY_ACCEPT_PROTOCOL *MemoryAcceptProtocol; + UINTN Start; + UINTN End; + EFI_STATUS Status; + + // + // We accept n*32MB at one time to improve the efficiency. + // + AcceptSize =3D (AcceptSize + SIZE_32MB - 1) & ~(SIZE_32MB - 1); + + if (AcceptSize =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + Status =3D gBS->LocateProtocol (&gEdkiiMemoryAcceptProtocolGuid, NULL, (= VOID **)&MemoryAcceptProtocol); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + if (Type =3D=3D AllocateAddress) { + Start =3D *Memory; + End =3D *Memory + AcceptSize; + } + + if (Type =3D=3D AllocateMaxAddress) { + if (*Memory < EFI_PAGE_MASK) { + return EFI_INVALID_PARAMETER; + } + + if ((*Memory & EFI_PAGE_MASK) !=3D EFI_PAGE_MASK) { + // + // Change MaxAddress to be 1 page lower + // + *Memory -=3D EFI_PAGE_SIZE; + + // + // Set MaxAddress to a page boundary + // + *Memory &=3D ~(UINT64)EFI_PAGE_MASK; + + // + // Set MaxAddress to end of the page + // + *Memory |=3D EFI_PAGE_MASK; + } + } + + // + // Traverse the mGcdMemorySpaceMap to find out the unaccepted + // memory entry with big enough size. + // + Link =3D mGcdMemorySpaceMap.ForwardLink; + while (Link !=3D &mGcdMemorySpaceMap) { + GcdEntry =3D CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE); + if (GcdEntry->GcdMemoryType =3D=3D EFI_GCD_MEMORY_TYPE_UNACCEPTED) { + if (Type =3D=3D AllocateMaxAddress) { + if (GcdEntry->BaseAddress + AcceptSize - 1 > *Memory) { + Link =3D Link->ForwardLink; + continue; + } + } else if (Type =3D=3D AllocateAddress) { + if ((GcdEntry->BaseAddress > *Memory) || (GcdEntry->EndAddress < *= Memory + AcceptSize - 1)) { + Link =3D Link->ForwardLink; + continue; + } + } + + // + // Is the entry big enough? + // + if (AcceptSize <=3D GcdEntry->EndAddress - GcdEntry->BaseAddress + 1= ) { + UnacceptedEntry =3D *GcdEntry; + if (Type !=3D AllocateAddress) { + Start =3D UnacceptedEntry.BaseAddress; + End =3D UnacceptedEntry.BaseAddress + AcceptSize - 1; + } + + break; + } + } + + Link =3D Link->ForwardLink; + } + + if (Link =3D=3D &mGcdMemorySpaceMap) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Accept memory using the interface provide by the protocol. + // + Status =3D MemoryAcceptProtocol->AcceptMemory (MemoryAcceptProtocol, Sta= rt, AcceptSize); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + + // + // If memory is accepted successfully, remove the target memory space fr= om GCD. + // + CoreRemoveMemorySpace (UnacceptedEntry.BaseAddress, UnacceptedEntry.EndA= ddress - UnacceptedEntry.BaseAddress + 1); + + // + // Add the remain lower part of unaccepted memory to the + // Gcd memory space and memory map. + // + if (Start > UnacceptedEntry.BaseAddress) { + CoreAddMemorySpace ( + EFI_GCD_MEMORY_TYPE_UNACCEPTED, + UnacceptedEntry.BaseAddress, + Start - UnacceptedEntry.BaseAddress, + UnacceptedEntry.Capabilities + ); + } + + // + // Update accepted part of the memory entry to type of EfiGcdMemoryTypeS= ystemMemory + // and add the range to the memory map. + // + CoreAddMemorySpace ( + EfiGcdMemoryTypeSystemMemory, + Start, + AcceptSize, + // + // Hardcode memory space attributes. + // + EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_XP | EFI_MEMORY_RO | EFI_MEMORY_RP + ); + + // + // Add the remain higher part of unaccepted memory to the + // Gcd memory space and memory map. + // + if (UnacceptedEntry.EndAddress > End) { + CoreAddMemorySpace ( + EFI_GCD_MEMORY_TYPE_UNACCEPTED, + End + 1, + UnacceptedEntry.EndAddress - End, + UnacceptedEntry.Capabilities + ); + } + + return EFI_SUCCESS; + + #else + + return EFI_UNSUPPORTED; + + #endif +} + /** Find untested but initialized memory regions in GCD map and convert them= to be DXE allocatable. =20 @@ -1486,6 +1657,25 @@ CoreAllocatePages ( Memory, NeedGuard ); + #ifdef MDE_CPU_X64 + + if (Status =3D=3D EFI_OUT_OF_RESOURCES) { + Status =3D AcceptMemoryResource (Type, NumberOfPages << EFI_PAGE_SHIFT= , Memory); + if (!EFI_ERROR (Status)) { + Status =3D CoreInternalAllocatePages ( + Type, + MemoryType, + NumberOfPages, + Memory, + NeedGuard + ); + } else { + Status =3D EFI_OUT_OF_RESOURCES; + } + } + + #endif + if (!EFI_ERROR (Status)) { CoreUpdateProfile ( (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0), diff --git a/MdeModulePkg/Core/Dxe/Mem/Pool.c b/MdeModulePkg/Core/Dxe/Mem/P= ool.c index 7aaf501600cf..9e8c8611c1ef 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Pool.c +++ b/MdeModulePkg/Core/Dxe/Mem/Pool.c @@ -273,6 +273,20 @@ CoreAllocatePool ( EFI_STATUS Status; =20 Status =3D CoreInternalAllocatePool (PoolType, Size, Buffer); + + #ifdef MDE_CPU_X64 + + if (Status =3D=3D EFI_OUT_OF_RESOURCES) { + Status =3D AcceptMemoryResource (AllocateAnyPages, Size, NULL); + if (!EFI_ERROR (Status)) { + Status =3D CoreInternalAllocatePool (PoolType, Size, Buffer); + } else { + Status =3D EFI_OUT_OF_RESOURCES; + } + } + + #endif + if (!EFI_ERROR (Status)) { CoreUpdateProfile ( (EFI_PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0), --=20 2.29.2.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#92893): https://edk2.groups.io/g/devel/message/92893 Mute This Topic: https://groups.io/mt/93285612/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-