1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2019, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 * These procedures are used for tracking memory leaks in the subsystem, and
46 * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
48 * Each memory allocation is tracked via a doubly linked list. Each
49 * element contains the caller's component, module name, function name, and
50 * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
51 * AcpiUtTrackAllocation to add an element to the list; deletion
52 * occurs in the body of AcpiUtFree.
58 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
60 #define _COMPONENT ACPI_UTILITIES
61 ACPI_MODULE_NAME ("uttrack")
64 /* Local prototypes */
66 static ACPI_DEBUG_MEM_BLOCK
*
67 AcpiUtFindAllocation (
68 ACPI_DEBUG_MEM_BLOCK
*Allocation
);
71 AcpiUtTrackAllocation (
72 ACPI_DEBUG_MEM_BLOCK
*Address
,
80 AcpiUtRemoveAllocation (
81 ACPI_DEBUG_MEM_BLOCK
*Address
,
87 /*******************************************************************************
89 * FUNCTION: AcpiUtCreateList
91 * PARAMETERS: CacheName - Ascii name for the cache
92 * ObjectSize - Size of each cached object
93 * ReturnCache - Where the new cache object is returned
97 * DESCRIPTION: Create a local memory list for tracking purposed
99 ******************************************************************************/
103 const char *ListName
,
105 ACPI_MEMORY_LIST
**ReturnCache
)
107 ACPI_MEMORY_LIST
*Cache
;
110 Cache
= AcpiOsAllocateZeroed (sizeof (ACPI_MEMORY_LIST
));
113 return (AE_NO_MEMORY
);
116 Cache
->ListName
= ListName
;
117 Cache
->ObjectSize
= ObjectSize
;
119 *ReturnCache
= Cache
;
124 /*******************************************************************************
126 * FUNCTION: AcpiUtAllocateAndTrack
128 * PARAMETERS: Size - Size of the allocation
129 * Component - Component type of caller
130 * Module - Source file name of caller
131 * Line - Line number of caller
133 * RETURN: Address of the allocated memory on success, NULL on failure.
135 * DESCRIPTION: The subsystem's equivalent of malloc.
137 ******************************************************************************/
140 AcpiUtAllocateAndTrack (
146 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
150 /* Check for an inadvertent size of zero bytes */
154 ACPI_WARNING ((Module
, Line
,
155 "Attempt to allocate zero bytes, allocating 1 byte"));
159 Allocation
= AcpiOsAllocate (Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
));
162 /* Report allocation error */
164 ACPI_WARNING ((Module
, Line
,
165 "Could not allocate size %u", (UINT32
) Size
));
170 Status
= AcpiUtTrackAllocation (
171 Allocation
, Size
, ACPI_MEM_MALLOC
, Component
, Module
, Line
);
172 if (ACPI_FAILURE (Status
))
174 AcpiOsFree (Allocation
);
178 AcpiGbl_GlobalList
->TotalAllocated
++;
179 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
180 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
182 if (AcpiGbl_GlobalList
->CurrentTotalSize
>
183 AcpiGbl_GlobalList
->MaxOccupied
)
185 AcpiGbl_GlobalList
->MaxOccupied
=
186 AcpiGbl_GlobalList
->CurrentTotalSize
;
189 return ((void *) &Allocation
->UserSpace
);
193 /*******************************************************************************
195 * FUNCTION: AcpiUtAllocateZeroedAndTrack
197 * PARAMETERS: Size - Size of the allocation
198 * Component - Component type of caller
199 * Module - Source file name of caller
200 * Line - Line number of caller
202 * RETURN: Address of the allocated memory on success, NULL on failure.
204 * DESCRIPTION: Subsystem equivalent of calloc.
206 ******************************************************************************/
209 AcpiUtAllocateZeroedAndTrack (
215 ACPI_DEBUG_MEM_BLOCK
*Allocation
;
219 /* Check for an inadvertent size of zero bytes */
223 ACPI_WARNING ((Module
, Line
,
224 "Attempt to allocate zero bytes, allocating 1 byte"));
228 Allocation
= AcpiOsAllocateZeroed (
229 Size
+ sizeof (ACPI_DEBUG_MEM_HEADER
));
232 /* Report allocation error */
234 ACPI_ERROR ((Module
, Line
,
235 "Could not allocate size %u", (UINT32
) Size
));
239 Status
= AcpiUtTrackAllocation (Allocation
, Size
,
240 ACPI_MEM_CALLOC
, Component
, Module
, Line
);
241 if (ACPI_FAILURE (Status
))
243 AcpiOsFree (Allocation
);
247 AcpiGbl_GlobalList
->TotalAllocated
++;
248 AcpiGbl_GlobalList
->TotalSize
+= (UINT32
) Size
;
249 AcpiGbl_GlobalList
->CurrentTotalSize
+= (UINT32
) Size
;
251 if (AcpiGbl_GlobalList
->CurrentTotalSize
>
252 AcpiGbl_GlobalList
->MaxOccupied
)
254 AcpiGbl_GlobalList
->MaxOccupied
=
255 AcpiGbl_GlobalList
->CurrentTotalSize
;
258 return ((void *) &Allocation
->UserSpace
);
262 /*******************************************************************************
264 * FUNCTION: AcpiUtFreeAndTrack
266 * PARAMETERS: Allocation - Address of the memory to deallocate
267 * Component - Component type of caller
268 * Module - Source file name of caller
269 * Line - Line number of caller
273 * DESCRIPTION: Frees the memory at Allocation
275 ******************************************************************************/
284 ACPI_DEBUG_MEM_BLOCK
*DebugBlock
;
288 ACPI_FUNCTION_TRACE_PTR (UtFree
, Allocation
);
291 if (NULL
== Allocation
)
293 ACPI_ERROR ((Module
, Line
,
294 "Attempt to delete a NULL address"));
299 DebugBlock
= ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK
,
300 (((char *) Allocation
) - sizeof (ACPI_DEBUG_MEM_HEADER
)));
302 AcpiGbl_GlobalList
->TotalFreed
++;
303 AcpiGbl_GlobalList
->CurrentTotalSize
-= DebugBlock
->Size
;
305 Status
= AcpiUtRemoveAllocation (DebugBlock
, Component
, Module
, Line
);
306 if (ACPI_FAILURE (Status
))
308 ACPI_EXCEPTION ((AE_INFO
, Status
, "Could not free memory"));
311 AcpiOsFree (DebugBlock
);
312 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "%p freed (block %p)\n",
313 Allocation
, DebugBlock
));
318 /*******************************************************************************
320 * FUNCTION: AcpiUtFindAllocation
322 * PARAMETERS: Allocation - Address of allocated memory
324 * RETURN: Three cases:
325 * 1) List is empty, NULL is returned.
326 * 2) Element was found. Returns Allocation parameter.
327 * 3) Element was not found. Returns position where it should be
328 * inserted into the list.
330 * DESCRIPTION: Searches for an element in the global allocation tracking list.
331 * If the element is not found, returns the location within the
332 * list where the element should be inserted.
334 * Note: The list is ordered by larger-to-smaller addresses.
336 * This global list is used to detect memory leaks in ACPICA as
337 * well as other issues such as an attempt to release the same
338 * internal object more than once. Although expensive as far
339 * as cpu time, this list is much more helpful for finding these
340 * types of issues than using memory leak detectors outside of
343 ******************************************************************************/
345 static ACPI_DEBUG_MEM_BLOCK
*
346 AcpiUtFindAllocation (
347 ACPI_DEBUG_MEM_BLOCK
*Allocation
)
349 ACPI_DEBUG_MEM_BLOCK
*Element
;
352 Element
= AcpiGbl_GlobalList
->ListHead
;
359 * Search for the address.
361 * Note: List is ordered by larger-to-smaller addresses, on the
362 * assumption that a new allocation usually has a larger address
363 * than previous allocations.
365 while (Element
> Allocation
)
367 /* Check for end-of-list */
374 Element
= Element
->Next
;
377 if (Element
== Allocation
)
382 return (Element
->Previous
);
386 /*******************************************************************************
388 * FUNCTION: AcpiUtTrackAllocation
390 * PARAMETERS: Allocation - Address of allocated memory
391 * Size - Size of the allocation
392 * AllocType - MEM_MALLOC or MEM_CALLOC
393 * Component - Component type of caller
394 * Module - Source file name of caller
395 * Line - Line number of caller
399 * DESCRIPTION: Inserts an element into the global allocation tracking list.
401 ******************************************************************************/
404 AcpiUtTrackAllocation (
405 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
412 ACPI_MEMORY_LIST
*MemList
;
413 ACPI_DEBUG_MEM_BLOCK
*Element
;
414 ACPI_STATUS Status
= AE_OK
;
417 ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation
, Allocation
);
420 if (AcpiGbl_DisableMemTracking
)
422 return_ACPI_STATUS (AE_OK
);
425 MemList
= AcpiGbl_GlobalList
;
426 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
427 if (ACPI_FAILURE (Status
))
429 return_ACPI_STATUS (Status
);
433 * Search the global list for this address to make sure it is not
434 * already present. This will catch several kinds of problems.
436 Element
= AcpiUtFindAllocation (Allocation
);
437 if (Element
== Allocation
)
439 ACPI_ERROR ((AE_INFO
,
440 "UtTrackAllocation: Allocation (%p) already present in global list!",
445 /* Fill in the instance data */
447 Allocation
->Size
= (UINT32
) Size
;
448 Allocation
->AllocType
= AllocType
;
449 Allocation
->Component
= Component
;
450 Allocation
->Line
= Line
;
452 AcpiUtSafeStrncpy (Allocation
->Module
, (char *) Module
, ACPI_MAX_MODULE_NAME
);
456 /* Insert at list head */
458 if (MemList
->ListHead
)
460 ((ACPI_DEBUG_MEM_BLOCK
*)(MemList
->ListHead
))->Previous
=
464 Allocation
->Next
= MemList
->ListHead
;
465 Allocation
->Previous
= NULL
;
467 MemList
->ListHead
= Allocation
;
471 /* Insert after element */
473 Allocation
->Next
= Element
->Next
;
474 Allocation
->Previous
= Element
;
478 (Element
->Next
)->Previous
= Allocation
;
481 Element
->Next
= Allocation
;
486 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
487 return_ACPI_STATUS (Status
);
491 /*******************************************************************************
493 * FUNCTION: AcpiUtRemoveAllocation
495 * PARAMETERS: Allocation - Address of allocated memory
496 * Component - Component type of caller
497 * Module - Source file name of caller
498 * Line - Line number of caller
502 * DESCRIPTION: Deletes an element from the global allocation tracking list.
504 ******************************************************************************/
507 AcpiUtRemoveAllocation (
508 ACPI_DEBUG_MEM_BLOCK
*Allocation
,
513 ACPI_MEMORY_LIST
*MemList
;
517 ACPI_FUNCTION_NAME (UtRemoveAllocation
);
520 if (AcpiGbl_DisableMemTracking
)
525 MemList
= AcpiGbl_GlobalList
;
526 if (NULL
== MemList
->ListHead
)
528 /* No allocations! */
530 ACPI_ERROR ((Module
, Line
,
531 "Empty allocation list, nothing to free!"));
536 Status
= AcpiUtAcquireMutex (ACPI_MTX_MEMORY
);
537 if (ACPI_FAILURE (Status
))
544 if (Allocation
->Previous
)
546 (Allocation
->Previous
)->Next
= Allocation
->Next
;
550 MemList
->ListHead
= Allocation
->Next
;
553 if (Allocation
->Next
)
555 (Allocation
->Next
)->Previous
= Allocation
->Previous
;
558 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS
, "Freeing %p, size 0%X\n",
559 &Allocation
->UserSpace
, Allocation
->Size
));
561 /* Mark the segment as deleted */
563 memset (&Allocation
->UserSpace
, 0xEA, Allocation
->Size
);
565 Status
= AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
570 /*******************************************************************************
572 * FUNCTION: AcpiUtDumpAllocationInfo
578 * DESCRIPTION: Print some info about the outstanding allocations.
580 ******************************************************************************/
583 AcpiUtDumpAllocationInfo (
587 ACPI_MEMORY_LIST *MemList;
590 ACPI_FUNCTION_TRACE (UtDumpAllocationInfo
);
593 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
594 ("%30s: %4d (%3d Kb)\n", "Current allocations",
595 MemList->CurrentCount,
596 ROUND_UP_TO_1K (MemList->CurrentSize)));
598 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
599 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
600 MemList->MaxConcurrentCount,
601 ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
604 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
605 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
607 ROUND_UP_TO_1K (RunningObjectSize)));
609 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
610 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
612 ROUND_UP_TO_1K (RunningAllocSize)));
615 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
616 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
617 AcpiGbl_CurrentNodeCount,
618 ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
620 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
621 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
622 AcpiGbl_MaxConcurrentNodeCount,
623 ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
624 sizeof (ACPI_NAMESPACE_NODE)))));
630 /*******************************************************************************
632 * FUNCTION: AcpiUtDumpAllocations
634 * PARAMETERS: Component - Component(s) to dump info for.
635 * Module - Module to dump info for. NULL means all.
639 * DESCRIPTION: Print a list of all outstanding allocations.
641 ******************************************************************************/
644 AcpiUtDumpAllocations (
648 ACPI_DEBUG_MEM_BLOCK
*Element
;
649 ACPI_DESCRIPTOR
*Descriptor
;
650 UINT32 NumOutstanding
= 0;
651 UINT8 DescriptorType
;
654 ACPI_FUNCTION_TRACE (UtDumpAllocations
);
657 if (AcpiGbl_DisableMemTracking
)
663 * Walk the allocation list.
665 if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY
)))
670 if (!AcpiGbl_GlobalList
)
675 Element
= AcpiGbl_GlobalList
->ListHead
;
678 if ((Element
->Component
& Component
) &&
679 ((Module
== NULL
) || (0 == strcmp (Module
, Element
->Module
))))
681 Descriptor
= ACPI_CAST_PTR (
682 ACPI_DESCRIPTOR
, &Element
->UserSpace
);
684 if (Element
->Size
< sizeof (ACPI_COMMON_DESCRIPTOR
))
686 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%4.4u "
687 "[Not a Descriptor - too small]\n",
688 Descriptor
, Element
->Size
, Element
->Module
,
693 /* Ignore allocated objects that are in a cache */
695 if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
) !=
696 ACPI_DESC_TYPE_CACHED
)
698 AcpiOsPrintf ("%p Length 0x%04X %9.9s-%4.4u [%s] ",
699 Descriptor
, Element
->Size
, Element
->Module
,
700 Element
->Line
, AcpiUtGetDescriptorName (Descriptor
));
702 /* Optional object hex dump */
704 if (AcpiGbl_VerboseLeakDump
)
707 AcpiUtDumpBuffer ((UINT8
*) Descriptor
, Element
->Size
,
711 /* Validate the descriptor type using Type field and length */
713 DescriptorType
= 0; /* Not a valid descriptor type */
715 switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor
))
717 case ACPI_DESC_TYPE_OPERAND
:
719 if (Element
->Size
== sizeof (ACPI_OPERAND_OBJECT
))
721 DescriptorType
= ACPI_DESC_TYPE_OPERAND
;
725 case ACPI_DESC_TYPE_PARSER
:
727 if (Element
->Size
== sizeof (ACPI_PARSE_OBJECT
))
729 DescriptorType
= ACPI_DESC_TYPE_PARSER
;
733 case ACPI_DESC_TYPE_NAMED
:
735 if (Element
->Size
== sizeof (ACPI_NAMESPACE_NODE
))
737 DescriptorType
= ACPI_DESC_TYPE_NAMED
;
746 /* Display additional info for the major descriptor types */
748 switch (DescriptorType
)
750 case ACPI_DESC_TYPE_OPERAND
:
752 AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
753 AcpiUtGetTypeName (Descriptor
->Object
.Common
.Type
),
754 Descriptor
->Object
.Common
.ReferenceCount
);
757 case ACPI_DESC_TYPE_PARSER
:
759 AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
760 Descriptor
->Op
.Asl
.AmlOpcode
);
763 case ACPI_DESC_TYPE_NAMED
:
765 AcpiOsPrintf ("%4.4s\n",
766 AcpiUtGetNodeName (&Descriptor
->Node
));
771 AcpiOsPrintf ( "\n");
780 Element
= Element
->Next
;
784 (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY
);
790 ACPI_INFO (("No outstanding allocations"));
794 ACPI_ERROR ((AE_INFO
, "%u (0x%X) Outstanding cache allocations",
795 NumOutstanding
, NumOutstanding
));
801 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */