Skip to content

Commit 7c0c52c

Browse files
committed
MdeModulePkg: Refinement of AcceptMemoryResource.
Refine the code of AcceptMemoryResource. Existing problems: 1. CoreAddMemorySpace() should not be called in the AcceptMemoryResource() because it will try to allocate memory for GCD map entry. 2. AcceptMemoryResource() is called in both CoreAllocatePool() and CoreAllocatePages(). Refine this by changing FindFreePages() and modifying the CoreConvertPagesEx() to handle AllocateAddress case.
1 parent 13d5bc2 commit 7c0c52c

3 files changed

Lines changed: 37 additions & 23 deletions

File tree

MdeModulePkg/Core/Dxe/Mem/Imem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ CoreAllocatePoolPages (
6969
);
7070

7171

72+
EFI_STATUS
73+
AcceptMemoryResource (
74+
IN UINTN Type,
75+
IN UINTN AcceptSize,
76+
IN OUT EFI_PHYSICAL_ADDRESS *Memory
77+
);
7278

7379
/**
7480
Internal function. Frees pool pages allocated via AllocatePoolPages ()

MdeModulePkg/Core/Dxe/Mem/Page.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -391,26 +391,20 @@ CoreFreeMemoryMapStack (
391391

392392
EFI_STATUS
393393
AcceptMemoryResource (
394-
IN UINTN Type,
394+
IN EFI_ALLOCATE_TYPE Type,
395395
IN UINTN AcceptSize,
396396
IN OUT EFI_PHYSICAL_ADDRESS *Memory
397397
)
398398
{
399399
LIST_ENTRY *Link;
400400
EFI_GCD_MAP_ENTRY *GcdEntry;
401-
EFI_PHYSICAL_ADDRESS UnacceptedEntryBase;
402-
UINTN UnacceptedEntryEnd;
403-
UINTN UnacceptedEntryLength;
404-
UINTN UnacceptedEntryCapability;
401+
EFI_GCD_MAP_ENTRY UnacceptedEntry;
405402
MEMORY_ACCEPT_PROTOCOL *MemoryAcceptProtocol;
406403
UINTN Start;
407404
UINTN End;
408405
EFI_STATUS Status;
409406

410-
Status = EFI_OUT_OF_RESOURCES;
411-
UnacceptedEntryBase = 0;
412-
UnacceptedEntryEnd = 0;
413-
AcceptSize = (AcceptSize + SIZE_32MB - 1) & ~(SIZE_32MB - 1);
407+
AcceptSize = (AcceptSize + SIZE_32MB - 1) & ~(SIZE_32MB - 1);
414408

415409
if (AcceptSize == 0) {
416410
return EFI_INVALID_PARAMETER;
@@ -427,6 +421,7 @@ AcceptMemoryResource (
427421
}
428422

429423
if (Type == AllocateMaxAddress) {
424+
430425
if (*Memory < EFI_PAGE_MASK) {
431426
return EFI_INVALID_PARAMETER;
432427
}
@@ -458,6 +453,7 @@ AcceptMemoryResource (
458453
GcdEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
459454

460455
if (GcdEntry->GcdMemoryType == EfiGcdMemoryTypeUnaccepted) {
456+
461457
if (Type == AllocateMaxAddress) {
462458
if (GcdEntry->BaseAddress + AcceptSize - 1 > *Memory) {
463459
continue;
@@ -468,15 +464,13 @@ AcceptMemoryResource (
468464
}
469465
}
470466

471-
UnacceptedEntryBase = GcdEntry->BaseAddress;
472-
UnacceptedEntryEnd = GcdEntry->EndAddress;
473-
UnacceptedEntryLength = GcdEntry->EndAddress - GcdEntry->BaseAddress + 1;
474-
UnacceptedEntryCapability = GcdEntry->Capabilities;
467+
UnacceptedEntry = *GcdEntry;
468+
475469
//
476470
// Remove the target memory space from GCD.
477471
//
478-
if (AcceptSize <= UnacceptedEntryLength) {
479-
CoreRemoveMemorySpace (GcdEntry->BaseAddress, UnacceptedEntryLength);
472+
if (AcceptSize <= UnacceptedEntry.EndAddress - UnacceptedEntry.BaseAddress + 1) {
473+
CoreRemoveMemorySpace (GcdEntry->BaseAddress, UnacceptedEntry.EndAddress - UnacceptedEntry.BaseAddress + 1);
480474

481475
if (Type != AllocateAddress) {
482476
Start = GcdEntry->BaseAddress;
@@ -500,16 +494,21 @@ AcceptMemoryResource (
500494
return EFI_OUT_OF_RESOURCES;
501495
}
502496

497+
//
498+
// Fix me! CoreAddMemorySpace() should not be called in the allocation process
499+
// because it will allocate memory for GCD map entry.
500+
//
501+
503502
//
504503
// Add the remain lower part of unaccepted memory to the
505504
// Gcd memory space and memory map.
506505
//
507-
if (Start > UnacceptedEntryBase) {
506+
if (Start > UnacceptedEntry.BaseAddress) {
508507
CoreAddMemorySpace (
509508
EfiGcdMemoryTypeUnaccepted,
510-
UnacceptedEntryBase,
511-
Start - UnacceptedEntryBase,
512-
UnacceptedEntryCapability
509+
UnacceptedEntry.BaseAddress,
510+
Start - UnacceptedEntry.BaseAddress,
511+
UnacceptedEntry.Capabilities
513512
);
514513
}
515514

@@ -528,12 +527,12 @@ AcceptMemoryResource (
528527
// Add the remain higher part of unaccepted memory to the
529528
// Gcd memory space and memory map.
530529
//
531-
if (UnacceptedEntryEnd > End) {
530+
if (UnacceptedEntry.EndAddress > End) {
532531
CoreAddMemorySpace (
533532
EfiGcdMemoryTypeUnaccepted,
534533
End + 1,
535-
UnacceptedEntryEnd - End,
536-
UnacceptedEntryCapability
534+
UnacceptedEntry.EndAddress - End,
535+
UnacceptedEntry.Capabilities
537536
);
538537
}
539538

@@ -2193,7 +2192,6 @@ CoreAllocatePoolPages (
21932192
)
21942193
{
21952194
UINT64 Start;
2196-
21972195
//
21982196
// Find the pages to convert
21992197
//

MdeModulePkg/Core/Dxe/Mem/Pool.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,16 @@ CoreAllocatePool (
276276
EFI_STATUS Status;
277277

278278
Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
279+
280+
if (Status == EFI_OUT_OF_RESOURCES) {
281+
Status = AcceptMemoryResource (AllocateAnyPages, Size, NULL);
282+
if (!EFI_ERROR (Status)) {
283+
Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
284+
} else {
285+
Status = EFI_OUT_OF_RESOURCES;
286+
}
287+
}
288+
279289
if (!EFI_ERROR (Status)) {
280290
CoreUpdateProfile (
281291
(EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),

0 commit comments

Comments
 (0)