1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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. acpi_ut_allocate and acpi_ut_allocate_zeroed call
51 * acpi_ut_track_allocation to add an element to the list; deletion
52 * occurs in the body of acpi_ut_free.
55 #include <acpi/acpi.h>
58 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
60 #define _COMPONENT ACPI_UTILITIES
61 ACPI_MODULE_NAME("uttrack")
63 /* Local prototypes */
64 static struct acpi_debug_mem_block
*acpi_ut_find_allocation(struct
69 acpi_ut_track_allocation(struct acpi_debug_mem_block
*address
,
72 u32 component
, const char *module
, u32 line
);
75 acpi_ut_remove_allocation(struct acpi_debug_mem_block
*address
,
76 u32 component
, const char *module
, u32 line
);
78 /*******************************************************************************
80 * FUNCTION: acpi_ut_create_list
82 * PARAMETERS: cache_name - Ascii name for the cache
83 * object_size - Size of each cached object
84 * return_cache - Where the new cache object is returned
88 * DESCRIPTION: Create a local memory list for tracking purposed
90 ******************************************************************************/
93 acpi_ut_create_list(char *list_name
,
94 u16 object_size
, struct acpi_memory_list
**return_cache
)
96 struct acpi_memory_list
*cache
;
98 cache
= acpi_os_allocate(sizeof(struct acpi_memory_list
));
100 return (AE_NO_MEMORY
);
103 ACPI_MEMSET(cache
, 0, sizeof(struct acpi_memory_list
));
105 cache
->list_name
= list_name
;
106 cache
->object_size
= object_size
;
108 *return_cache
= cache
;
112 /*******************************************************************************
114 * FUNCTION: acpi_ut_allocate_and_track
116 * PARAMETERS: size - Size of the allocation
117 * component - Component type of caller
118 * module - Source file name of caller
119 * line - Line number of caller
121 * RETURN: Address of the allocated memory on success, NULL on failure.
123 * DESCRIPTION: The subsystem's equivalent of malloc.
125 ******************************************************************************/
127 void *acpi_ut_allocate_and_track(acpi_size size
,
128 u32 component
, const char *module
, u32 line
)
130 struct acpi_debug_mem_block
*allocation
;
134 acpi_ut_allocate(size
+ sizeof(struct acpi_debug_mem_header
),
135 component
, module
, line
);
140 status
= acpi_ut_track_allocation(allocation
, size
,
141 ACPI_MEM_MALLOC
, component
, module
,
143 if (ACPI_FAILURE(status
)) {
144 acpi_os_free(allocation
);
148 acpi_gbl_global_list
->total_allocated
++;
149 acpi_gbl_global_list
->total_size
+= (u32
)size
;
150 acpi_gbl_global_list
->current_total_size
+= (u32
)size
;
151 if (acpi_gbl_global_list
->current_total_size
>
152 acpi_gbl_global_list
->max_occupied
) {
153 acpi_gbl_global_list
->max_occupied
=
154 acpi_gbl_global_list
->current_total_size
;
157 return ((void *)&allocation
->user_space
);
160 /*******************************************************************************
162 * FUNCTION: acpi_ut_allocate_zeroed_and_track
164 * PARAMETERS: size - Size of the allocation
165 * component - Component type of caller
166 * module - Source file name of caller
167 * line - Line number of caller
169 * RETURN: Address of the allocated memory on success, NULL on failure.
171 * DESCRIPTION: Subsystem equivalent of calloc.
173 ******************************************************************************/
175 void *acpi_ut_allocate_zeroed_and_track(acpi_size size
,
177 const char *module
, u32 line
)
179 struct acpi_debug_mem_block
*allocation
;
183 acpi_ut_allocate_zeroed(size
+ sizeof(struct acpi_debug_mem_header
),
184 component
, module
, line
);
187 /* Report allocation error */
189 ACPI_ERROR((module
, line
,
190 "Could not allocate size %u", (u32
)size
));
194 status
= acpi_ut_track_allocation(allocation
, size
,
195 ACPI_MEM_CALLOC
, component
, module
,
197 if (ACPI_FAILURE(status
)) {
198 acpi_os_free(allocation
);
202 acpi_gbl_global_list
->total_allocated
++;
203 acpi_gbl_global_list
->total_size
+= (u32
)size
;
204 acpi_gbl_global_list
->current_total_size
+= (u32
)size
;
205 if (acpi_gbl_global_list
->current_total_size
>
206 acpi_gbl_global_list
->max_occupied
) {
207 acpi_gbl_global_list
->max_occupied
=
208 acpi_gbl_global_list
->current_total_size
;
211 return ((void *)&allocation
->user_space
);
214 /*******************************************************************************
216 * FUNCTION: acpi_ut_free_and_track
218 * PARAMETERS: allocation - Address of the memory to deallocate
219 * component - Component type of caller
220 * module - Source file name of caller
221 * line - Line number of caller
225 * DESCRIPTION: Frees the memory at Allocation
227 ******************************************************************************/
230 acpi_ut_free_and_track(void *allocation
,
231 u32 component
, const char *module
, u32 line
)
233 struct acpi_debug_mem_block
*debug_block
;
236 ACPI_FUNCTION_TRACE_PTR(ut_free
, allocation
);
238 if (NULL
== allocation
) {
239 ACPI_ERROR((module
, line
, "Attempt to delete a NULL address"));
244 debug_block
= ACPI_CAST_PTR(struct acpi_debug_mem_block
,
245 (((char *)allocation
) -
246 sizeof(struct acpi_debug_mem_header
)));
248 acpi_gbl_global_list
->total_freed
++;
249 acpi_gbl_global_list
->current_total_size
-= debug_block
->size
;
251 status
= acpi_ut_remove_allocation(debug_block
,
252 component
, module
, line
);
253 if (ACPI_FAILURE(status
)) {
254 ACPI_EXCEPTION((AE_INFO
, status
, "Could not free memory"));
257 acpi_os_free(debug_block
);
258 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "%p freed\n", allocation
));
262 /*******************************************************************************
264 * FUNCTION: acpi_ut_find_allocation
266 * PARAMETERS: allocation - Address of allocated memory
268 * RETURN: Three cases:
269 * 1) List is empty, NULL is returned.
270 * 2) Element was found. Returns Allocation parameter.
271 * 3) Element was not found. Returns position where it should be
272 * inserted into the list.
274 * DESCRIPTION: Searches for an element in the global allocation tracking list.
275 * If the element is not found, returns the location within the
276 * list where the element should be inserted.
278 * Note: The list is ordered by larger-to-smaller addresses.
280 * This global list is used to detect memory leaks in ACPICA as
281 * well as other issues such as an attempt to release the same
282 * internal object more than once. Although expensive as far
283 * as cpu time, this list is much more helpful for finding these
284 * types of issues than using memory leak detectors outside of
287 ******************************************************************************/
289 static struct acpi_debug_mem_block
*acpi_ut_find_allocation(struct
293 struct acpi_debug_mem_block
*element
;
295 element
= acpi_gbl_global_list
->list_head
;
301 * Search for the address.
303 * Note: List is ordered by larger-to-smaller addresses, on the
304 * assumption that a new allocation usually has a larger address
305 * than previous allocations.
307 while (element
> allocation
) {
309 /* Check for end-of-list */
311 if (!element
->next
) {
315 element
= element
->next
;
318 if (element
== allocation
) {
322 return (element
->previous
);
325 /*******************************************************************************
327 * FUNCTION: acpi_ut_track_allocation
329 * PARAMETERS: allocation - Address of allocated memory
330 * size - Size of the allocation
331 * alloc_type - MEM_MALLOC or MEM_CALLOC
332 * component - Component type of caller
333 * module - Source file name of caller
334 * line - Line number of caller
338 * DESCRIPTION: Inserts an element into the global allocation tracking list.
340 ******************************************************************************/
343 acpi_ut_track_allocation(struct acpi_debug_mem_block
*allocation
,
346 u32 component
, const char *module
, u32 line
)
348 struct acpi_memory_list
*mem_list
;
349 struct acpi_debug_mem_block
*element
;
350 acpi_status status
= AE_OK
;
352 ACPI_FUNCTION_TRACE_PTR(ut_track_allocation
, allocation
);
354 if (acpi_gbl_disable_mem_tracking
) {
355 return_ACPI_STATUS(AE_OK
);
358 mem_list
= acpi_gbl_global_list
;
359 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
360 if (ACPI_FAILURE(status
)) {
361 return_ACPI_STATUS(status
);
365 * Search the global list for this address to make sure it is not
366 * already present. This will catch several kinds of problems.
368 element
= acpi_ut_find_allocation(allocation
);
369 if (element
== allocation
) {
371 "UtTrackAllocation: Allocation (%p) already present in global list!",
373 goto unlock_and_exit
;
376 /* Fill in the instance data */
378 allocation
->size
= (u32
)size
;
379 allocation
->alloc_type
= alloc_type
;
380 allocation
->component
= component
;
381 allocation
->line
= line
;
383 ACPI_STRNCPY(allocation
->module
, module
, ACPI_MAX_MODULE_NAME
);
384 allocation
->module
[ACPI_MAX_MODULE_NAME
- 1] = 0;
388 /* Insert at list head */
390 if (mem_list
->list_head
) {
391 ((struct acpi_debug_mem_block
*)(mem_list
->list_head
))->
392 previous
= allocation
;
395 allocation
->next
= mem_list
->list_head
;
396 allocation
->previous
= NULL
;
398 mem_list
->list_head
= allocation
;
400 /* Insert after element */
402 allocation
->next
= element
->next
;
403 allocation
->previous
= element
;
406 (element
->next
)->previous
= allocation
;
409 element
->next
= allocation
;
413 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
414 return_ACPI_STATUS(status
);
417 /*******************************************************************************
419 * FUNCTION: acpi_ut_remove_allocation
421 * PARAMETERS: allocation - Address of allocated memory
422 * component - Component type of caller
423 * module - Source file name of caller
424 * line - Line number of caller
428 * DESCRIPTION: Deletes an element from the global allocation tracking list.
430 ******************************************************************************/
433 acpi_ut_remove_allocation(struct acpi_debug_mem_block
*allocation
,
434 u32 component
, const char *module
, u32 line
)
436 struct acpi_memory_list
*mem_list
;
439 ACPI_FUNCTION_TRACE(ut_remove_allocation
);
441 if (acpi_gbl_disable_mem_tracking
) {
442 return_ACPI_STATUS(AE_OK
);
445 mem_list
= acpi_gbl_global_list
;
446 if (NULL
== mem_list
->list_head
) {
448 /* No allocations! */
450 ACPI_ERROR((module
, line
,
451 "Empty allocation list, nothing to free!"));
453 return_ACPI_STATUS(AE_OK
);
456 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
457 if (ACPI_FAILURE(status
)) {
458 return_ACPI_STATUS(status
);
463 if (allocation
->previous
) {
464 (allocation
->previous
)->next
= allocation
->next
;
466 mem_list
->list_head
= allocation
->next
;
469 if (allocation
->next
) {
470 (allocation
->next
)->previous
= allocation
->previous
;
473 /* Mark the segment as deleted */
475 ACPI_MEMSET(&allocation
->user_space
, 0xEA, allocation
->size
);
477 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Freeing size 0%X\n",
480 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
481 return_ACPI_STATUS(status
);
484 /*******************************************************************************
486 * FUNCTION: acpi_ut_dump_allocation_info
492 * DESCRIPTION: Print some info about the outstanding allocations.
494 ******************************************************************************/
496 void acpi_ut_dump_allocation_info(void)
499 struct acpi_memory_list *mem_list;
502 ACPI_FUNCTION_TRACE(ut_dump_allocation_info
);
505 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
506 ("%30s: %4d (%3d Kb)\n", "Current allocations",
507 mem_list->current_count,
508 ROUND_UP_TO_1K (mem_list->current_size)));
510 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
511 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
512 mem_list->max_concurrent_count,
513 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
515 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
516 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
517 running_object_count,
518 ROUND_UP_TO_1K (running_object_size)));
520 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
521 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
523 ROUND_UP_TO_1K (running_alloc_size)));
525 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
526 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
527 acpi_gbl_current_node_count,
528 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
530 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
531 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
532 acpi_gbl_max_concurrent_node_count,
533 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
534 sizeof (struct acpi_namespace_node)))));
539 /*******************************************************************************
541 * FUNCTION: acpi_ut_dump_allocations
543 * PARAMETERS: component - Component(s) to dump info for.
544 * module - Module to dump info for. NULL means all.
548 * DESCRIPTION: Print a list of all outstanding allocations.
550 ******************************************************************************/
552 void acpi_ut_dump_allocations(u32 component
, const char *module
)
554 struct acpi_debug_mem_block
*element
;
555 union acpi_descriptor
*descriptor
;
556 u32 num_outstanding
= 0;
559 ACPI_FUNCTION_TRACE(ut_dump_allocations
);
561 if (acpi_gbl_disable_mem_tracking
) {
566 * Walk the allocation list.
568 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
))) {
572 element
= acpi_gbl_global_list
->list_head
;
574 if ((element
->component
& component
) &&
576 || (0 == ACPI_STRCMP(module
, element
->module
)))) {
578 ACPI_CAST_PTR(union acpi_descriptor
,
579 &element
->user_space
);
582 sizeof(struct acpi_common_descriptor
)) {
583 acpi_os_printf("%p Length 0x%04X %9.9s-%u "
584 "[Not a Descriptor - too small]\n",
585 descriptor
, element
->size
,
586 element
->module
, element
->line
);
588 /* Ignore allocated objects that are in a cache */
590 if (ACPI_GET_DESCRIPTOR_TYPE(descriptor
) !=
591 ACPI_DESC_TYPE_CACHED
) {
593 ("%p Length 0x%04X %9.9s-%u [%s] ",
594 descriptor
, element
->size
,
595 element
->module
, element
->line
,
596 acpi_ut_get_descriptor_name
599 /* Validate the descriptor type using Type field and length */
601 descriptor_type
= 0; /* Not a valid descriptor type */
603 switch (ACPI_GET_DESCRIPTOR_TYPE
605 case ACPI_DESC_TYPE_OPERAND
:
608 acpi_operand_object
))
611 ACPI_DESC_TYPE_OPERAND
;
615 case ACPI_DESC_TYPE_PARSER
:
618 acpi_parse_object
)) {
620 ACPI_DESC_TYPE_PARSER
;
624 case ACPI_DESC_TYPE_NAMED
:
627 acpi_namespace_node
))
630 ACPI_DESC_TYPE_NAMED
;
638 /* Display additional info for the major descriptor types */
640 switch (descriptor_type
) {
641 case ACPI_DESC_TYPE_OPERAND
:
643 ("%12.12s RefCount 0x%04X\n",
644 acpi_ut_get_type_name
645 (descriptor
->object
.common
.
647 descriptor
->object
.common
.
651 case ACPI_DESC_TYPE_PARSER
:
653 ("AmlOpcode 0x%04hX\n",
658 case ACPI_DESC_TYPE_NAMED
:
659 acpi_os_printf("%4.4s\n",
660 acpi_ut_get_node_name
666 acpi_os_printf("\n");
675 element
= element
->next
;
678 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
682 if (!num_outstanding
) {
683 ACPI_INFO((AE_INFO
, "No outstanding allocations"));
685 ACPI_ERROR((AE_INFO
, "%u(0x%X) Outstanding allocations",
686 num_outstanding
, num_outstanding
));
692 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */