]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg: improve scalability of memory pools
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
1 /** @file
2 UEFI Memory pool management functions.
3
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeMain.h"
16 #include "Imem.h"
17
18 #define POOL_FREE_SIGNATURE SIGNATURE_32('p','f','r','0')
19 typedef struct {
20 UINT32 Signature;
21 UINT32 Index;
22 LIST_ENTRY Link;
23 } POOL_FREE;
24
25
26 #define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')
27 typedef struct {
28 UINT32 Signature;
29 UINT32 Reserved;
30 EFI_MEMORY_TYPE Type;
31 UINTN Size;
32 CHAR8 Data[1];
33 } POOL_HEAD;
34
35 #define SIZE_OF_POOL_HEAD OFFSET_OF(POOL_HEAD,Data)
36
37 #define POOL_TAIL_SIGNATURE SIGNATURE_32('p','t','a','l')
38 typedef struct {
39 UINT32 Signature;
40 UINT32 Reserved;
41 UINTN Size;
42 } POOL_TAIL;
43
44 #define POOL_OVERHEAD (SIZE_OF_POOL_HEAD + sizeof(POOL_TAIL))
45
46 #define HEAD_TO_TAIL(a) \
47 ((POOL_TAIL *) (((CHAR8 *) (a)) + (a)->Size - sizeof(POOL_TAIL)));
48
49 //
50 // Each element is the sum of the 2 previous ones: this allows us to migrate
51 // blocks between bins by splitting them up, while not wasting too much memory
52 // as we would in a strict power-of-2 sequence
53 //
54 STATIC CONST UINT16 mPoolSizeTable[] = {
55 64, 128, 192, 320, 512, 832, 1344, 2176, 3520, 5696, 9216, 14912, 24128
56 };
57
58 #define SIZE_TO_LIST(a) (GetPoolIndexFromSize (a))
59 #define LIST_TO_SIZE(a) (mPoolSizeTable [a])
60
61 #define MAX_POOL_LIST (sizeof (mPoolSizeTable) / sizeof (mPoolSizeTable[0]))
62
63 #define MAX_POOL_SIZE (MAX_ADDRESS - POOL_OVERHEAD)
64
65 //
66 // Globals
67 //
68
69 #define POOL_SIGNATURE SIGNATURE_32('p','l','s','t')
70 typedef struct {
71 INTN Signature;
72 UINTN Used;
73 EFI_MEMORY_TYPE MemoryType;
74 LIST_ENTRY FreeList[MAX_POOL_LIST];
75 LIST_ENTRY Link;
76 } POOL;
77
78 //
79 // Pool header for each memory type.
80 //
81 POOL mPoolHead[EfiMaxMemoryType];
82
83 //
84 // List of pool header to search for the appropriate memory type.
85 //
86 LIST_ENTRY mPoolHeadList = INITIALIZE_LIST_HEAD_VARIABLE (mPoolHeadList);
87
88 STATIC
89 UINTN
90 GetPoolIndexFromSize (
91 UINTN Size
92 )
93 {
94 UINTN Index;
95
96 for (Index = 0; Index < MAX_POOL_LIST; Index++) {
97 if (mPoolSizeTable [Index] >= Size) {
98 return Index;
99 }
100 }
101 return MAX_POOL_LIST;
102 }
103
104 /**
105 Called to initialize the pool.
106
107 **/
108 VOID
109 CoreInitializePool (
110 VOID
111 )
112 {
113 UINTN Type;
114 UINTN Index;
115
116 for (Type=0; Type < EfiMaxMemoryType; Type++) {
117 mPoolHead[Type].Signature = 0;
118 mPoolHead[Type].Used = 0;
119 mPoolHead[Type].MemoryType = (EFI_MEMORY_TYPE) Type;
120 for (Index=0; Index < MAX_POOL_LIST; Index++) {
121 InitializeListHead (&mPoolHead[Type].FreeList[Index]);
122 }
123 }
124 }
125
126
127 /**
128 Look up pool head for specified memory type.
129
130 @param MemoryType Memory type of which pool head is looked for
131
132 @return Pointer of Corresponding pool head.
133
134 **/
135 POOL *
136 LookupPoolHead (
137 IN EFI_MEMORY_TYPE MemoryType
138 )
139 {
140 LIST_ENTRY *Link;
141 POOL *Pool;
142 UINTN Index;
143
144 if ((UINT32)MemoryType < EfiMaxMemoryType) {
145 return &mPoolHead[MemoryType];
146 }
147
148 //
149 // MemoryType values in the range 0x80000000..0xFFFFFFFF are reserved for use by UEFI
150 // OS loaders that are provided by operating system vendors
151 //
152 if ((INT32)MemoryType < 0) {
153
154 for (Link = mPoolHeadList.ForwardLink; Link != &mPoolHeadList; Link = Link->ForwardLink) {
155 Pool = CR(Link, POOL, Link, POOL_SIGNATURE);
156 if (Pool->MemoryType == MemoryType) {
157 return Pool;
158 }
159 }
160
161 Pool = CoreAllocatePoolI (EfiBootServicesData, sizeof (POOL));
162 if (Pool == NULL) {
163 return NULL;
164 }
165
166 Pool->Signature = POOL_SIGNATURE;
167 Pool->Used = 0;
168 Pool->MemoryType = MemoryType;
169 for (Index=0; Index < MAX_POOL_LIST; Index++) {
170 InitializeListHead (&Pool->FreeList[Index]);
171 }
172
173 InsertHeadList (&mPoolHeadList, &Pool->Link);
174
175 return Pool;
176 }
177
178 return NULL;
179 }
180
181
182
183 /**
184 Allocate pool of a particular type.
185
186 @param PoolType Type of pool to allocate
187 @param Size The amount of pool to allocate
188 @param Buffer The address to return a pointer to the allocated
189 pool
190
191 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
192 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
193 @retval EFI_SUCCESS Pool successfully allocated.
194
195 **/
196 EFI_STATUS
197 EFIAPI
198 CoreInternalAllocatePool (
199 IN EFI_MEMORY_TYPE PoolType,
200 IN UINTN Size,
201 OUT VOID **Buffer
202 )
203 {
204 EFI_STATUS Status;
205
206 //
207 // If it's not a valid type, fail it
208 //
209 if ((PoolType >= EfiMaxMemoryType && PoolType <= 0x7fffffff) ||
210 PoolType == EfiConventionalMemory) {
211 return EFI_INVALID_PARAMETER;
212 }
213
214 if (Buffer == NULL) {
215 return EFI_INVALID_PARAMETER;
216 }
217
218 *Buffer = NULL;
219
220 //
221 // If size is too large, fail it
222 // Base on the EFI spec, return status of EFI_OUT_OF_RESOURCES
223 //
224 if (Size > MAX_POOL_SIZE) {
225 return EFI_OUT_OF_RESOURCES;
226 }
227
228 //
229 // Acquire the memory lock and make the allocation
230 //
231 Status = CoreAcquireLockOrFail (&gMemoryLock);
232 if (EFI_ERROR (Status)) {
233 return EFI_OUT_OF_RESOURCES;
234 }
235
236 *Buffer = CoreAllocatePoolI (PoolType, Size);
237 CoreReleaseMemoryLock ();
238 return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;
239 }
240
241 /**
242 Allocate pool of a particular type.
243
244 @param PoolType Type of pool to allocate
245 @param Size The amount of pool to allocate
246 @param Buffer The address to return a pointer to the allocated
247 pool
248
249 @retval EFI_INVALID_PARAMETER PoolType not valid or Buffer is NULL.
250 @retval EFI_OUT_OF_RESOURCES Size exceeds max pool size or allocation failed.
251 @retval EFI_SUCCESS Pool successfully allocated.
252
253 **/
254 EFI_STATUS
255 EFIAPI
256 CoreAllocatePool (
257 IN EFI_MEMORY_TYPE PoolType,
258 IN UINTN Size,
259 OUT VOID **Buffer
260 )
261 {
262 EFI_STATUS Status;
263
264 Status = CoreInternalAllocatePool (PoolType, Size, Buffer);
265 if (!EFI_ERROR (Status)) {
266 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);
267 }
268 return Status;
269 }
270
271 /**
272 Internal function to allocate pool of a particular type.
273 Caller must have the memory lock held
274
275 @param PoolType Type of pool to allocate
276 @param Size The amount of pool to allocate
277
278 @return The allocate pool, or NULL
279
280 **/
281 VOID *
282 CoreAllocatePoolI (
283 IN EFI_MEMORY_TYPE PoolType,
284 IN UINTN Size
285 )
286 {
287 POOL *Pool;
288 POOL_FREE *Free;
289 POOL_HEAD *Head;
290 POOL_TAIL *Tail;
291 CHAR8 *NewPage;
292 VOID *Buffer;
293 UINTN Index;
294 UINTN FSize;
295 UINTN Offset;
296 UINTN NoPages;
297 UINTN Granularity;
298
299 ASSERT_LOCKED (&gMemoryLock);
300
301 if (PoolType == EfiACPIReclaimMemory ||
302 PoolType == EfiACPIMemoryNVS ||
303 PoolType == EfiRuntimeServicesCode ||
304 PoolType == EfiRuntimeServicesData) {
305
306 Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
307 } else {
308 Granularity = DEFAULT_PAGE_ALLOCATION;
309 }
310
311 //
312 // Adjust the size by the pool header & tail overhead
313 //
314
315 //
316 // Adjusting the Size to be of proper alignment so that
317 // we don't get an unaligned access fault later when
318 // pool_Tail is being initialized
319 //
320 Size = ALIGN_VARIABLE (Size);
321
322 Size += POOL_OVERHEAD;
323 Index = SIZE_TO_LIST(Size);
324 Pool = LookupPoolHead (PoolType);
325 if (Pool== NULL) {
326 return NULL;
327 }
328 Head = NULL;
329
330 //
331 // If allocation is over max size, just allocate pages for the request
332 // (slow)
333 //
334 if (Index >= SIZE_TO_LIST (Granularity)) {
335 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
336 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
337 Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity);
338 goto Done;
339 }
340
341 //
342 // If there's no free pool in the proper list size, go get some more pages
343 //
344 if (IsListEmpty (&Pool->FreeList[Index])) {
345
346 //
347 // Get another page
348 //
349 NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);
350 if (NewPage == NULL) {
351 goto Done;
352 }
353
354 //
355 // Carve up new page into free pool blocks
356 //
357 Offset = 0;
358 while (Offset < Granularity) {
359 ASSERT (Index < MAX_POOL_LIST);
360 FSize = LIST_TO_SIZE(Index);
361
362 while (Offset + FSize <= Granularity) {
363 Free = (POOL_FREE *) &NewPage[Offset];
364 Free->Signature = POOL_FREE_SIGNATURE;
365 Free->Index = (UINT32)Index;
366 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
367 Offset += FSize;
368 }
369
370 Index -= 1;
371 }
372
373 ASSERT (Offset == Granularity);
374 Index = SIZE_TO_LIST(Size);
375 }
376
377 //
378 // Remove entry from free pool list
379 //
380 Free = CR (Pool->FreeList[Index].ForwardLink, POOL_FREE, Link, POOL_FREE_SIGNATURE);
381 RemoveEntryList (&Free->Link);
382
383 Head = (POOL_HEAD *) Free;
384
385 Done:
386 Buffer = NULL;
387
388 if (Head != NULL) {
389
390 //
391 // If we have a pool buffer, fill in the header & tail info
392 //
393 Head->Signature = POOL_HEAD_SIGNATURE;
394 Head->Size = Size;
395 Head->Type = (EFI_MEMORY_TYPE) PoolType;
396 Tail = HEAD_TO_TAIL (Head);
397 Tail->Signature = POOL_TAIL_SIGNATURE;
398 Tail->Size = Size;
399 Buffer = Head->Data;
400 DEBUG_CLEAR_MEMORY (Buffer, Size - POOL_OVERHEAD);
401
402 DEBUG ((
403 DEBUG_POOL,
404 "AllocatePoolI: Type %x, Addr %p (len %lx) %,ld\n", PoolType,
405 Buffer,
406 (UINT64)(Size - POOL_OVERHEAD),
407 (UINT64) Pool->Used
408 ));
409
410 //
411 // Account the allocation
412 //
413 Pool->Used += Size;
414
415 } else {
416 DEBUG ((DEBUG_ERROR | DEBUG_POOL, "AllocatePool: failed to allocate %ld bytes\n", (UINT64) Size));
417 }
418
419 return Buffer;
420 }
421
422
423
424 /**
425 Frees pool.
426
427 @param Buffer The allocated pool entry to free
428
429 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
430 @retval EFI_SUCCESS Pool successfully freed.
431
432 **/
433 EFI_STATUS
434 EFIAPI
435 CoreInternalFreePool (
436 IN VOID *Buffer
437 )
438 {
439 EFI_STATUS Status;
440
441 if (Buffer == NULL) {
442 return EFI_INVALID_PARAMETER;
443 }
444
445 CoreAcquireMemoryLock ();
446 Status = CoreFreePoolI (Buffer);
447 CoreReleaseMemoryLock ();
448 return Status;
449 }
450
451 /**
452 Frees pool.
453
454 @param Buffer The allocated pool entry to free
455
456 @retval EFI_INVALID_PARAMETER Buffer is not a valid value.
457 @retval EFI_SUCCESS Pool successfully freed.
458
459 **/
460 EFI_STATUS
461 EFIAPI
462 CoreFreePool (
463 IN VOID *Buffer
464 )
465 {
466 EFI_STATUS Status;
467
468 Status = CoreInternalFreePool (Buffer);
469 if (!EFI_ERROR (Status)) {
470 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer);
471 }
472 return Status;
473 }
474
475 /**
476 Internal function to free a pool entry.
477 Caller must have the memory lock held
478
479 @param Buffer The allocated pool entry to free
480
481 @retval EFI_INVALID_PARAMETER Buffer not valid
482 @retval EFI_SUCCESS Buffer successfully freed.
483
484 **/
485 EFI_STATUS
486 CoreFreePoolI (
487 IN VOID *Buffer
488 )
489 {
490 POOL *Pool;
491 POOL_HEAD *Head;
492 POOL_TAIL *Tail;
493 POOL_FREE *Free;
494 UINTN Index;
495 UINTN NoPages;
496 UINTN Size;
497 CHAR8 *NewPage;
498 UINTN FSize;
499 UINTN Offset;
500 BOOLEAN AllFree;
501 UINTN Granularity;
502
503 ASSERT(Buffer != NULL);
504 //
505 // Get the head & tail of the pool entry
506 //
507 Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);
508 ASSERT(Head != NULL);
509
510 if (Head->Signature != POOL_HEAD_SIGNATURE) {
511 return EFI_INVALID_PARAMETER;
512 }
513
514 Tail = HEAD_TO_TAIL (Head);
515 ASSERT(Tail != NULL);
516
517 //
518 // Debug
519 //
520 ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);
521 ASSERT (Head->Size == Tail->Size);
522 ASSERT_LOCKED (&gMemoryLock);
523
524 if (Tail->Signature != POOL_TAIL_SIGNATURE) {
525 return EFI_INVALID_PARAMETER;
526 }
527
528 if (Head->Size != Tail->Size) {
529 return EFI_INVALID_PARAMETER;
530 }
531
532 //
533 // Determine the pool type and account for it
534 //
535 Size = Head->Size;
536 Pool = LookupPoolHead (Head->Type);
537 if (Pool == NULL) {
538 return EFI_INVALID_PARAMETER;
539 }
540 Pool->Used -= Size;
541 DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));
542
543 if (Head->Type == EfiACPIReclaimMemory ||
544 Head->Type == EfiACPIMemoryNVS ||
545 Head->Type == EfiRuntimeServicesCode ||
546 Head->Type == EfiRuntimeServicesData) {
547
548 Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
549 } else {
550 Granularity = DEFAULT_PAGE_ALLOCATION;
551 }
552
553 //
554 // Determine the pool list
555 //
556 Index = SIZE_TO_LIST(Size);
557 DEBUG_CLEAR_MEMORY (Head, Size);
558
559 //
560 // If it's not on the list, it must be pool pages
561 //
562 if (Index >= SIZE_TO_LIST (Granularity)) {
563
564 //
565 // Return the memory pages back to free memory
566 //
567 NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;
568 NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);
569 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);
570
571 } else {
572
573 //
574 // Put the pool entry onto the free pool list
575 //
576 Free = (POOL_FREE *) Head;
577 ASSERT(Free != NULL);
578 Free->Signature = POOL_FREE_SIGNATURE;
579 Free->Index = (UINT32)Index;
580 InsertHeadList (&Pool->FreeList[Index], &Free->Link);
581
582 //
583 // See if all the pool entries in the same page as Free are freed pool
584 // entries
585 //
586 NewPage = (CHAR8 *)((UINTN)Free & ~(Granularity - 1));
587 Free = (POOL_FREE *) &NewPage[0];
588 ASSERT(Free != NULL);
589
590 if (Free->Signature == POOL_FREE_SIGNATURE) {
591
592 Index = Free->Index;
593
594 AllFree = TRUE;
595 Offset = 0;
596
597 while ((Offset < Granularity) && (AllFree)) {
598 FSize = LIST_TO_SIZE(Index);
599 while (Offset + FSize <= Granularity) {
600 Free = (POOL_FREE *) &NewPage[Offset];
601 ASSERT(Free != NULL);
602 if (Free->Signature != POOL_FREE_SIGNATURE) {
603 AllFree = FALSE;
604 }
605 Offset += FSize;
606 }
607 Index -= 1;
608 }
609
610 if (AllFree) {
611
612 //
613 // All of the pool entries in the same page as Free are free pool
614 // entries
615 // Remove all of these pool entries from the free loop lists.
616 //
617 Free = (POOL_FREE *) &NewPage[0];
618 ASSERT(Free != NULL);
619 Index = Free->Index;
620 Offset = 0;
621
622 while (Offset < Granularity) {
623 FSize = LIST_TO_SIZE(Index);
624 while (Offset + FSize <= Granularity) {
625 Free = (POOL_FREE *) &NewPage[Offset];
626 ASSERT(Free != NULL);
627 RemoveEntryList (&Free->Link);
628 Offset += FSize;
629 }
630 Index -= 1;
631 }
632
633 //
634 // Free the page
635 //
636 CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity));
637 }
638 }
639 }
640
641 //
642 // If this is an OS specific memory type, then check to see if the last
643 // portion of that memory type has been freed. If it has, then free the
644 // list entry for that memory type
645 //
646 if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {
647 RemoveEntryList (&Pool->Link);
648 CoreFreePoolI (Pool);
649 }
650
651 return EFI_SUCCESS;
652 }
653