1 /******************************************************************************
3 * Module Name: uttrack - Memory allocation tracking routines (debug only)
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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(const 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 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
;
133 /* Check for an inadvertent size of zero bytes */
136 ACPI_WARNING((module
, line
,
137 "Attempt to allocate zero bytes, allocating 1 byte"));
142 acpi_os_allocate(size
+ sizeof(struct acpi_debug_mem_header
));
145 /* Report allocation error */
147 ACPI_WARNING((module
, line
,
148 "Could not allocate size %u", (u32
)size
));
154 acpi_ut_track_allocation(allocation
, size
, ACPI_MEM_MALLOC
,
155 component
, module
, line
);
156 if (ACPI_FAILURE(status
)) {
157 acpi_os_free(allocation
);
161 acpi_gbl_global_list
->total_allocated
++;
162 acpi_gbl_global_list
->total_size
+= (u32
)size
;
163 acpi_gbl_global_list
->current_total_size
+= (u32
)size
;
165 if (acpi_gbl_global_list
->current_total_size
>
166 acpi_gbl_global_list
->max_occupied
) {
167 acpi_gbl_global_list
->max_occupied
=
168 acpi_gbl_global_list
->current_total_size
;
171 return ((void *)&allocation
->user_space
);
174 /*******************************************************************************
176 * FUNCTION: acpi_ut_allocate_zeroed_and_track
178 * PARAMETERS: size - Size of the allocation
179 * component - Component type of caller
180 * module - Source file name of caller
181 * line - Line number of caller
183 * RETURN: Address of the allocated memory on success, NULL on failure.
185 * DESCRIPTION: Subsystem equivalent of calloc.
187 ******************************************************************************/
189 void *acpi_ut_allocate_zeroed_and_track(acpi_size size
,
191 const char *module
, u32 line
)
193 struct acpi_debug_mem_block
*allocation
;
196 /* Check for an inadvertent size of zero bytes */
199 ACPI_WARNING((module
, line
,
200 "Attempt to allocate zero bytes, allocating 1 byte"));
205 acpi_os_allocate_zeroed(size
+
206 sizeof(struct acpi_debug_mem_header
));
209 /* Report allocation error */
211 ACPI_ERROR((module
, line
,
212 "Could not allocate size %u", (u32
)size
));
216 status
= acpi_ut_track_allocation(allocation
, size
,
217 ACPI_MEM_CALLOC
, component
, module
,
219 if (ACPI_FAILURE(status
)) {
220 acpi_os_free(allocation
);
224 acpi_gbl_global_list
->total_allocated
++;
225 acpi_gbl_global_list
->total_size
+= (u32
)size
;
226 acpi_gbl_global_list
->current_total_size
+= (u32
)size
;
228 if (acpi_gbl_global_list
->current_total_size
>
229 acpi_gbl_global_list
->max_occupied
) {
230 acpi_gbl_global_list
->max_occupied
=
231 acpi_gbl_global_list
->current_total_size
;
234 return ((void *)&allocation
->user_space
);
237 /*******************************************************************************
239 * FUNCTION: acpi_ut_free_and_track
241 * PARAMETERS: allocation - Address of the memory to deallocate
242 * component - Component type of caller
243 * module - Source file name of caller
244 * line - Line number of caller
248 * DESCRIPTION: Frees the memory at Allocation
250 ******************************************************************************/
253 acpi_ut_free_and_track(void *allocation
,
254 u32 component
, const char *module
, u32 line
)
256 struct acpi_debug_mem_block
*debug_block
;
259 ACPI_FUNCTION_TRACE_PTR(ut_free
, allocation
);
261 if (NULL
== allocation
) {
262 ACPI_ERROR((module
, line
, "Attempt to delete a NULL address"));
267 debug_block
= ACPI_CAST_PTR(struct acpi_debug_mem_block
,
268 (((char *)allocation
) -
269 sizeof(struct acpi_debug_mem_header
)));
271 acpi_gbl_global_list
->total_freed
++;
272 acpi_gbl_global_list
->current_total_size
-= debug_block
->size
;
275 acpi_ut_remove_allocation(debug_block
, component
, module
, line
);
276 if (ACPI_FAILURE(status
)) {
277 ACPI_EXCEPTION((AE_INFO
, status
, "Could not free memory"));
280 acpi_os_free(debug_block
);
281 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "%p freed (block %p)\n",
282 allocation
, debug_block
));
286 /*******************************************************************************
288 * FUNCTION: acpi_ut_find_allocation
290 * PARAMETERS: allocation - Address of allocated memory
292 * RETURN: Three cases:
293 * 1) List is empty, NULL is returned.
294 * 2) Element was found. Returns Allocation parameter.
295 * 3) Element was not found. Returns position where it should be
296 * inserted into the list.
298 * DESCRIPTION: Searches for an element in the global allocation tracking list.
299 * If the element is not found, returns the location within the
300 * list where the element should be inserted.
302 * Note: The list is ordered by larger-to-smaller addresses.
304 * This global list is used to detect memory leaks in ACPICA as
305 * well as other issues such as an attempt to release the same
306 * internal object more than once. Although expensive as far
307 * as cpu time, this list is much more helpful for finding these
308 * types of issues than using memory leak detectors outside of
311 ******************************************************************************/
313 static struct acpi_debug_mem_block
*acpi_ut_find_allocation(struct
317 struct acpi_debug_mem_block
*element
;
319 element
= acpi_gbl_global_list
->list_head
;
325 * Search for the address.
327 * Note: List is ordered by larger-to-smaller addresses, on the
328 * assumption that a new allocation usually has a larger address
329 * than previous allocations.
331 while (element
> allocation
) {
333 /* Check for end-of-list */
335 if (!element
->next
) {
339 element
= element
->next
;
342 if (element
== allocation
) {
346 return (element
->previous
);
349 /*******************************************************************************
351 * FUNCTION: acpi_ut_track_allocation
353 * PARAMETERS: allocation - Address of allocated memory
354 * size - Size of the allocation
355 * alloc_type - MEM_MALLOC or MEM_CALLOC
356 * component - Component type of caller
357 * module - Source file name of caller
358 * line - Line number of caller
362 * DESCRIPTION: Inserts an element into the global allocation tracking list.
364 ******************************************************************************/
367 acpi_ut_track_allocation(struct acpi_debug_mem_block
*allocation
,
370 u32 component
, const char *module
, u32 line
)
372 struct acpi_memory_list
*mem_list
;
373 struct acpi_debug_mem_block
*element
;
374 acpi_status status
= AE_OK
;
376 ACPI_FUNCTION_TRACE_PTR(ut_track_allocation
, allocation
);
378 if (acpi_gbl_disable_mem_tracking
) {
379 return_ACPI_STATUS(AE_OK
);
382 mem_list
= acpi_gbl_global_list
;
383 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
384 if (ACPI_FAILURE(status
)) {
385 return_ACPI_STATUS(status
);
389 * Search the global list for this address to make sure it is not
390 * already present. This will catch several kinds of problems.
392 element
= acpi_ut_find_allocation(allocation
);
393 if (element
== allocation
) {
395 "UtTrackAllocation: Allocation (%p) already present in global list!",
397 goto unlock_and_exit
;
400 /* Fill in the instance data */
402 allocation
->size
= (u32
)size
;
403 allocation
->alloc_type
= alloc_type
;
404 allocation
->component
= component
;
405 allocation
->line
= line
;
407 strncpy(allocation
->module
, module
, ACPI_MAX_MODULE_NAME
);
408 allocation
->module
[ACPI_MAX_MODULE_NAME
- 1] = 0;
412 /* Insert at list head */
414 if (mem_list
->list_head
) {
415 ((struct acpi_debug_mem_block
*)(mem_list
->list_head
))->
416 previous
= allocation
;
419 allocation
->next
= mem_list
->list_head
;
420 allocation
->previous
= NULL
;
422 mem_list
->list_head
= allocation
;
424 /* Insert after element */
426 allocation
->next
= element
->next
;
427 allocation
->previous
= element
;
430 (element
->next
)->previous
= allocation
;
433 element
->next
= allocation
;
437 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
438 return_ACPI_STATUS(status
);
441 /*******************************************************************************
443 * FUNCTION: acpi_ut_remove_allocation
445 * PARAMETERS: allocation - Address of allocated memory
446 * component - Component type of caller
447 * module - Source file name of caller
448 * line - Line number of caller
452 * DESCRIPTION: Deletes an element from the global allocation tracking list.
454 ******************************************************************************/
457 acpi_ut_remove_allocation(struct acpi_debug_mem_block
*allocation
,
458 u32 component
, const char *module
, u32 line
)
460 struct acpi_memory_list
*mem_list
;
463 ACPI_FUNCTION_NAME(ut_remove_allocation
);
465 if (acpi_gbl_disable_mem_tracking
) {
469 mem_list
= acpi_gbl_global_list
;
470 if (NULL
== mem_list
->list_head
) {
472 /* No allocations! */
474 ACPI_ERROR((module
, line
,
475 "Empty allocation list, nothing to free!"));
480 status
= acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
);
481 if (ACPI_FAILURE(status
)) {
487 if (allocation
->previous
) {
488 (allocation
->previous
)->next
= allocation
->next
;
490 mem_list
->list_head
= allocation
->next
;
493 if (allocation
->next
) {
494 (allocation
->next
)->previous
= allocation
->previous
;
497 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Freeing %p, size 0%X\n",
498 &allocation
->user_space
, allocation
->size
));
500 /* Mark the segment as deleted */
502 memset(&allocation
->user_space
, 0xEA, allocation
->size
);
504 status
= acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
508 /*******************************************************************************
510 * FUNCTION: acpi_ut_dump_allocation_info
516 * DESCRIPTION: Print some info about the outstanding allocations.
518 ******************************************************************************/
520 void acpi_ut_dump_allocation_info(void)
523 struct acpi_memory_list *mem_list;
526 ACPI_FUNCTION_TRACE(ut_dump_allocation_info
);
529 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
530 ("%30s: %4d (%3d Kb)\n", "Current allocations",
531 mem_list->current_count,
532 ROUND_UP_TO_1K (mem_list->current_size)));
534 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
535 ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
536 mem_list->max_concurrent_count,
537 ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
539 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
540 ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
541 running_object_count,
542 ROUND_UP_TO_1K (running_object_size)));
544 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
545 ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
547 ROUND_UP_TO_1K (running_alloc_size)));
549 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
550 ("%30s: %4d (%3d Kb)\n", "Current Nodes",
551 acpi_gbl_current_node_count,
552 ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
554 ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
555 ("%30s: %4d (%3d Kb)\n", "Max Nodes",
556 acpi_gbl_max_concurrent_node_count,
557 ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
558 sizeof (struct acpi_namespace_node)))));
563 /*******************************************************************************
565 * FUNCTION: acpi_ut_dump_allocations
567 * PARAMETERS: component - Component(s) to dump info for.
568 * module - Module to dump info for. NULL means all.
572 * DESCRIPTION: Print a list of all outstanding allocations.
574 ******************************************************************************/
576 void acpi_ut_dump_allocations(u32 component
, const char *module
)
578 struct acpi_debug_mem_block
*element
;
579 union acpi_descriptor
*descriptor
;
580 u32 num_outstanding
= 0;
583 ACPI_FUNCTION_TRACE(ut_dump_allocations
);
585 if (acpi_gbl_disable_mem_tracking
) {
590 * Walk the allocation list.
592 if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY
))) {
596 element
= acpi_gbl_global_list
->list_head
;
598 if ((element
->component
& component
) &&
600 || (0 == strcmp(module
, element
->module
)))) {
602 ACPI_CAST_PTR(union acpi_descriptor
,
603 &element
->user_space
);
606 sizeof(struct acpi_common_descriptor
)) {
607 acpi_os_printf("%p Length 0x%04X %9.9s-%u "
608 "[Not a Descriptor - too small]\n",
609 descriptor
, element
->size
,
610 element
->module
, element
->line
);
612 /* Ignore allocated objects that are in a cache */
614 if (ACPI_GET_DESCRIPTOR_TYPE(descriptor
) !=
615 ACPI_DESC_TYPE_CACHED
) {
617 ("%p Length 0x%04X %9.9s-%u [%s] ",
618 descriptor
, element
->size
,
619 element
->module
, element
->line
,
620 acpi_ut_get_descriptor_name
623 /* Validate the descriptor type using Type field and length */
625 descriptor_type
= 0; /* Not a valid descriptor type */
627 switch (ACPI_GET_DESCRIPTOR_TYPE
629 case ACPI_DESC_TYPE_OPERAND
:
633 acpi_operand_object
))
636 ACPI_DESC_TYPE_OPERAND
;
640 case ACPI_DESC_TYPE_PARSER
:
644 acpi_parse_object
)) {
646 ACPI_DESC_TYPE_PARSER
;
650 case ACPI_DESC_TYPE_NAMED
:
654 acpi_namespace_node
))
657 ACPI_DESC_TYPE_NAMED
;
666 /* Display additional info for the major descriptor types */
668 switch (descriptor_type
) {
669 case ACPI_DESC_TYPE_OPERAND
:
672 ("%12.12s RefCount 0x%04X\n",
673 acpi_ut_get_type_name
674 (descriptor
->object
.common
.
676 descriptor
->object
.common
.
680 case ACPI_DESC_TYPE_PARSER
:
683 ("AmlOpcode 0x%04hX\n",
688 case ACPI_DESC_TYPE_NAMED
:
690 acpi_os_printf("%4.4s\n",
691 acpi_ut_get_node_name
698 acpi_os_printf("\n");
707 element
= element
->next
;
710 (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY
);
714 if (!num_outstanding
) {
715 ACPI_INFO(("No outstanding allocations"));
717 ACPI_ERROR((AE_INFO
, "%u(0x%X) Outstanding allocations",
718 num_outstanding
, num_outstanding
));
724 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */