xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/uttrack.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: uttrack - Memory allocation tracking routines (debug only)
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *****************************************************************************/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * These procedures are used for tracking memory leaks in the subsystem, and
12*4882a593Smuzhiyun  * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Each memory allocation is tracked via a doubly linked list. Each
15*4882a593Smuzhiyun  * element contains the caller's component, module name, function name, and
16*4882a593Smuzhiyun  * line number. acpi_ut_allocate and acpi_ut_allocate_zeroed call
17*4882a593Smuzhiyun  * acpi_ut_track_allocation to add an element to the list; deletion
18*4882a593Smuzhiyun  * occurs in the body of acpi_ut_free.
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <acpi/acpi.h>
22*4882a593Smuzhiyun #include "accommon.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #ifdef ACPI_DBG_TRACK_ALLOCATIONS
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define _COMPONENT          ACPI_UTILITIES
27*4882a593Smuzhiyun ACPI_MODULE_NAME("uttrack")
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* Local prototypes */
30*4882a593Smuzhiyun static struct acpi_debug_mem_block *acpi_ut_find_allocation(struct
31*4882a593Smuzhiyun 							    acpi_debug_mem_block
32*4882a593Smuzhiyun 							    *allocation);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static acpi_status
35*4882a593Smuzhiyun acpi_ut_track_allocation(struct acpi_debug_mem_block *address,
36*4882a593Smuzhiyun 			 acpi_size size,
37*4882a593Smuzhiyun 			 u8 alloc_type,
38*4882a593Smuzhiyun 			 u32 component, const char *module, u32 line);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static acpi_status
41*4882a593Smuzhiyun acpi_ut_remove_allocation(struct acpi_debug_mem_block *address,
42*4882a593Smuzhiyun 			  u32 component, const char *module, u32 line);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*******************************************************************************
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_create_list
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * PARAMETERS:  cache_name      - Ascii name for the cache
49*4882a593Smuzhiyun  *              object_size     - Size of each cached object
50*4882a593Smuzhiyun  *              return_cache    - Where the new cache object is returned
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * RETURN:      Status
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * DESCRIPTION: Create a local memory list for tracking purposed
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  ******************************************************************************/
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun acpi_status
acpi_ut_create_list(const char * list_name,u16 object_size,struct acpi_memory_list ** return_cache)59*4882a593Smuzhiyun acpi_ut_create_list(const char *list_name,
60*4882a593Smuzhiyun 		    u16 object_size, struct acpi_memory_list **return_cache)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct acpi_memory_list *cache;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	cache = acpi_os_allocate_zeroed(sizeof(struct acpi_memory_list));
65*4882a593Smuzhiyun 	if (!cache) {
66*4882a593Smuzhiyun 		return (AE_NO_MEMORY);
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	cache->list_name = list_name;
70*4882a593Smuzhiyun 	cache->object_size = object_size;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	*return_cache = cache;
73*4882a593Smuzhiyun 	return (AE_OK);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*******************************************************************************
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_allocate_and_track
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * PARAMETERS:  size                - Size of the allocation
81*4882a593Smuzhiyun  *              component           - Component type of caller
82*4882a593Smuzhiyun  *              module              - Source file name of caller
83*4882a593Smuzhiyun  *              line                - Line number of caller
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * RETURN:      Address of the allocated memory on success, NULL on failure.
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * DESCRIPTION: The subsystem's equivalent of malloc.
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  ******************************************************************************/
90*4882a593Smuzhiyun 
acpi_ut_allocate_and_track(acpi_size size,u32 component,const char * module,u32 line)91*4882a593Smuzhiyun void *acpi_ut_allocate_and_track(acpi_size size,
92*4882a593Smuzhiyun 				 u32 component, const char *module, u32 line)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	struct acpi_debug_mem_block *allocation;
95*4882a593Smuzhiyun 	acpi_status status;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* Check for an inadvertent size of zero bytes */
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (!size) {
100*4882a593Smuzhiyun 		ACPI_WARNING((module, line,
101*4882a593Smuzhiyun 			      "Attempt to allocate zero bytes, allocating 1 byte"));
102*4882a593Smuzhiyun 		size = 1;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	allocation =
106*4882a593Smuzhiyun 	    acpi_os_allocate(size + sizeof(struct acpi_debug_mem_header));
107*4882a593Smuzhiyun 	if (!allocation) {
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		/* Report allocation error */
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 		ACPI_WARNING((module, line,
112*4882a593Smuzhiyun 			      "Could not allocate size %u", (u32)size));
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 		return (NULL);
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	status =
118*4882a593Smuzhiyun 	    acpi_ut_track_allocation(allocation, size, ACPI_MEM_MALLOC,
119*4882a593Smuzhiyun 				     component, module, line);
120*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
121*4882a593Smuzhiyun 		acpi_os_free(allocation);
122*4882a593Smuzhiyun 		return (NULL);
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	acpi_gbl_global_list->total_allocated++;
126*4882a593Smuzhiyun 	acpi_gbl_global_list->total_size += (u32)size;
127*4882a593Smuzhiyun 	acpi_gbl_global_list->current_total_size += (u32)size;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (acpi_gbl_global_list->current_total_size >
130*4882a593Smuzhiyun 	    acpi_gbl_global_list->max_occupied) {
131*4882a593Smuzhiyun 		acpi_gbl_global_list->max_occupied =
132*4882a593Smuzhiyun 		    acpi_gbl_global_list->current_total_size;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	return ((void *)&allocation->user_space);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*******************************************************************************
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_allocate_zeroed_and_track
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * PARAMETERS:  size                - Size of the allocation
143*4882a593Smuzhiyun  *              component           - Component type of caller
144*4882a593Smuzhiyun  *              module              - Source file name of caller
145*4882a593Smuzhiyun  *              line                - Line number of caller
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * RETURN:      Address of the allocated memory on success, NULL on failure.
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * DESCRIPTION: Subsystem equivalent of calloc.
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  ******************************************************************************/
152*4882a593Smuzhiyun 
acpi_ut_allocate_zeroed_and_track(acpi_size size,u32 component,const char * module,u32 line)153*4882a593Smuzhiyun void *acpi_ut_allocate_zeroed_and_track(acpi_size size,
154*4882a593Smuzhiyun 					u32 component,
155*4882a593Smuzhiyun 					const char *module, u32 line)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct acpi_debug_mem_block *allocation;
158*4882a593Smuzhiyun 	acpi_status status;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* Check for an inadvertent size of zero bytes */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (!size) {
163*4882a593Smuzhiyun 		ACPI_WARNING((module, line,
164*4882a593Smuzhiyun 			      "Attempt to allocate zero bytes, allocating 1 byte"));
165*4882a593Smuzhiyun 		size = 1;
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	allocation =
169*4882a593Smuzhiyun 	    acpi_os_allocate_zeroed(size +
170*4882a593Smuzhiyun 				    sizeof(struct acpi_debug_mem_header));
171*4882a593Smuzhiyun 	if (!allocation) {
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		/* Report allocation error */
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		ACPI_ERROR((module, line,
176*4882a593Smuzhiyun 			    "Could not allocate size %u", (u32)size));
177*4882a593Smuzhiyun 		return (NULL);
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	status = acpi_ut_track_allocation(allocation, size,
181*4882a593Smuzhiyun 					  ACPI_MEM_CALLOC, component, module,
182*4882a593Smuzhiyun 					  line);
183*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
184*4882a593Smuzhiyun 		acpi_os_free(allocation);
185*4882a593Smuzhiyun 		return (NULL);
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	acpi_gbl_global_list->total_allocated++;
189*4882a593Smuzhiyun 	acpi_gbl_global_list->total_size += (u32)size;
190*4882a593Smuzhiyun 	acpi_gbl_global_list->current_total_size += (u32)size;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (acpi_gbl_global_list->current_total_size >
193*4882a593Smuzhiyun 	    acpi_gbl_global_list->max_occupied) {
194*4882a593Smuzhiyun 		acpi_gbl_global_list->max_occupied =
195*4882a593Smuzhiyun 		    acpi_gbl_global_list->current_total_size;
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return ((void *)&allocation->user_space);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /*******************************************************************************
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_free_and_track
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * PARAMETERS:  allocation          - Address of the memory to deallocate
206*4882a593Smuzhiyun  *              component           - Component type of caller
207*4882a593Smuzhiyun  *              module              - Source file name of caller
208*4882a593Smuzhiyun  *              line                - Line number of caller
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * RETURN:      None
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * DESCRIPTION: Frees the memory at Allocation
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  ******************************************************************************/
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun void
acpi_ut_free_and_track(void * allocation,u32 component,const char * module,u32 line)217*4882a593Smuzhiyun acpi_ut_free_and_track(void *allocation,
218*4882a593Smuzhiyun 		       u32 component, const char *module, u32 line)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct acpi_debug_mem_block *debug_block;
221*4882a593Smuzhiyun 	acpi_status status;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE_PTR(ut_free, allocation);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (NULL == allocation) {
226*4882a593Smuzhiyun 		ACPI_ERROR((module, line, "Attempt to delete a NULL address"));
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		return_VOID;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
232*4882a593Smuzhiyun 				    (((char *)allocation) -
233*4882a593Smuzhiyun 				     sizeof(struct acpi_debug_mem_header)));
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	acpi_gbl_global_list->total_freed++;
236*4882a593Smuzhiyun 	acpi_gbl_global_list->current_total_size -= debug_block->size;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	status =
239*4882a593Smuzhiyun 	    acpi_ut_remove_allocation(debug_block, component, module, line);
240*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
241*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Could not free memory"));
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	acpi_os_free(debug_block);
245*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed (block %p)\n",
246*4882a593Smuzhiyun 			  allocation, debug_block));
247*4882a593Smuzhiyun 	return_VOID;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /*******************************************************************************
251*4882a593Smuzhiyun  *
252*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_find_allocation
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * PARAMETERS:  allocation              - Address of allocated memory
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * RETURN:      Three cases:
257*4882a593Smuzhiyun  *              1) List is empty, NULL is returned.
258*4882a593Smuzhiyun  *              2) Element was found. Returns Allocation parameter.
259*4882a593Smuzhiyun  *              3) Element was not found. Returns position where it should be
260*4882a593Smuzhiyun  *                  inserted into the list.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * DESCRIPTION: Searches for an element in the global allocation tracking list.
263*4882a593Smuzhiyun  *              If the element is not found, returns the location within the
264*4882a593Smuzhiyun  *              list where the element should be inserted.
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  *              Note: The list is ordered by larger-to-smaller addresses.
267*4882a593Smuzhiyun  *
268*4882a593Smuzhiyun  *              This global list is used to detect memory leaks in ACPICA as
269*4882a593Smuzhiyun  *              well as other issues such as an attempt to release the same
270*4882a593Smuzhiyun  *              internal object more than once. Although expensive as far
271*4882a593Smuzhiyun  *              as cpu time, this list is much more helpful for finding these
272*4882a593Smuzhiyun  *              types of issues than using memory leak detectors outside of
273*4882a593Smuzhiyun  *              the ACPICA code.
274*4882a593Smuzhiyun  *
275*4882a593Smuzhiyun  ******************************************************************************/
276*4882a593Smuzhiyun 
acpi_ut_find_allocation(struct acpi_debug_mem_block * allocation)277*4882a593Smuzhiyun static struct acpi_debug_mem_block *acpi_ut_find_allocation(struct
278*4882a593Smuzhiyun 							    acpi_debug_mem_block
279*4882a593Smuzhiyun 							    *allocation)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	struct acpi_debug_mem_block *element;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	element = acpi_gbl_global_list->list_head;
284*4882a593Smuzhiyun 	if (!element) {
285*4882a593Smuzhiyun 		return (NULL);
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/*
289*4882a593Smuzhiyun 	 * Search for the address.
290*4882a593Smuzhiyun 	 *
291*4882a593Smuzhiyun 	 * Note: List is ordered by larger-to-smaller addresses, on the
292*4882a593Smuzhiyun 	 * assumption that a new allocation usually has a larger address
293*4882a593Smuzhiyun 	 * than previous allocations.
294*4882a593Smuzhiyun 	 */
295*4882a593Smuzhiyun 	while (element > allocation) {
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 		/* Check for end-of-list */
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		if (!element->next) {
300*4882a593Smuzhiyun 			return (element);
301*4882a593Smuzhiyun 		}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		element = element->next;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (element == allocation) {
307*4882a593Smuzhiyun 		return (element);
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	return (element->previous);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /*******************************************************************************
314*4882a593Smuzhiyun  *
315*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_track_allocation
316*4882a593Smuzhiyun  *
317*4882a593Smuzhiyun  * PARAMETERS:  allocation          - Address of allocated memory
318*4882a593Smuzhiyun  *              size                - Size of the allocation
319*4882a593Smuzhiyun  *              alloc_type          - MEM_MALLOC or MEM_CALLOC
320*4882a593Smuzhiyun  *              component           - Component type of caller
321*4882a593Smuzhiyun  *              module              - Source file name of caller
322*4882a593Smuzhiyun  *              line                - Line number of caller
323*4882a593Smuzhiyun  *
324*4882a593Smuzhiyun  * RETURN:      Status
325*4882a593Smuzhiyun  *
326*4882a593Smuzhiyun  * DESCRIPTION: Inserts an element into the global allocation tracking list.
327*4882a593Smuzhiyun  *
328*4882a593Smuzhiyun  ******************************************************************************/
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun static acpi_status
acpi_ut_track_allocation(struct acpi_debug_mem_block * allocation,acpi_size size,u8 alloc_type,u32 component,const char * module,u32 line)331*4882a593Smuzhiyun acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
332*4882a593Smuzhiyun 			 acpi_size size,
333*4882a593Smuzhiyun 			 u8 alloc_type,
334*4882a593Smuzhiyun 			 u32 component, const char *module, u32 line)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	struct acpi_memory_list *mem_list;
337*4882a593Smuzhiyun 	struct acpi_debug_mem_block *element;
338*4882a593Smuzhiyun 	acpi_status status = AE_OK;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE_PTR(ut_track_allocation, allocation);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	if (acpi_gbl_disable_mem_tracking) {
343*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_OK);
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	mem_list = acpi_gbl_global_list;
347*4882a593Smuzhiyun 	status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
348*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
349*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
350*4882a593Smuzhiyun 	}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	/*
353*4882a593Smuzhiyun 	 * Search the global list for this address to make sure it is not
354*4882a593Smuzhiyun 	 * already present. This will catch several kinds of problems.
355*4882a593Smuzhiyun 	 */
356*4882a593Smuzhiyun 	element = acpi_ut_find_allocation(allocation);
357*4882a593Smuzhiyun 	if (element == allocation) {
358*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO,
359*4882a593Smuzhiyun 			    "UtTrackAllocation: Allocation (%p) already present in global list!",
360*4882a593Smuzhiyun 			    allocation));
361*4882a593Smuzhiyun 		goto unlock_and_exit;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	/* Fill in the instance data */
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	allocation->size = (u32)size;
367*4882a593Smuzhiyun 	allocation->alloc_type = alloc_type;
368*4882a593Smuzhiyun 	allocation->component = component;
369*4882a593Smuzhiyun 	allocation->line = line;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	acpi_ut_safe_strncpy(allocation->module, (char *)module,
372*4882a593Smuzhiyun 			     ACPI_MAX_MODULE_NAME);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	if (!element) {
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		/* Insert at list head */
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 		if (mem_list->list_head) {
379*4882a593Smuzhiyun 			((struct acpi_debug_mem_block *)(mem_list->list_head))->
380*4882a593Smuzhiyun 			    previous = allocation;
381*4882a593Smuzhiyun 		}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 		allocation->next = mem_list->list_head;
384*4882a593Smuzhiyun 		allocation->previous = NULL;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		mem_list->list_head = allocation;
387*4882a593Smuzhiyun 	} else {
388*4882a593Smuzhiyun 		/* Insert after element */
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 		allocation->next = element->next;
391*4882a593Smuzhiyun 		allocation->previous = element;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 		if (element->next) {
394*4882a593Smuzhiyun 			(element->next)->previous = allocation;
395*4882a593Smuzhiyun 		}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		element->next = allocation;
398*4882a593Smuzhiyun 	}
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun unlock_and_exit:
401*4882a593Smuzhiyun 	status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
402*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /*******************************************************************************
406*4882a593Smuzhiyun  *
407*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_remove_allocation
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * PARAMETERS:  allocation          - Address of allocated memory
410*4882a593Smuzhiyun  *              component           - Component type of caller
411*4882a593Smuzhiyun  *              module              - Source file name of caller
412*4882a593Smuzhiyun  *              line                - Line number of caller
413*4882a593Smuzhiyun  *
414*4882a593Smuzhiyun  * RETURN:      Status
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  * DESCRIPTION: Deletes an element from the global allocation tracking list.
417*4882a593Smuzhiyun  *
418*4882a593Smuzhiyun  ******************************************************************************/
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun static acpi_status
acpi_ut_remove_allocation(struct acpi_debug_mem_block * allocation,u32 component,const char * module,u32 line)421*4882a593Smuzhiyun acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
422*4882a593Smuzhiyun 			  u32 component, const char *module, u32 line)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	struct acpi_memory_list *mem_list;
425*4882a593Smuzhiyun 	acpi_status status;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	ACPI_FUNCTION_NAME(ut_remove_allocation);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	if (acpi_gbl_disable_mem_tracking) {
430*4882a593Smuzhiyun 		return (AE_OK);
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	mem_list = acpi_gbl_global_list;
434*4882a593Smuzhiyun 	if (NULL == mem_list->list_head) {
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 		/* No allocations! */
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 		ACPI_ERROR((module, line,
439*4882a593Smuzhiyun 			    "Empty allocation list, nothing to free!"));
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 		return (AE_OK);
442*4882a593Smuzhiyun 	}
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
445*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
446*4882a593Smuzhiyun 		return (status);
447*4882a593Smuzhiyun 	}
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/* Unlink */
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (allocation->previous) {
452*4882a593Smuzhiyun 		(allocation->previous)->next = allocation->next;
453*4882a593Smuzhiyun 	} else {
454*4882a593Smuzhiyun 		mem_list->list_head = allocation->next;
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (allocation->next) {
458*4882a593Smuzhiyun 		(allocation->next)->previous = allocation->previous;
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing %p, size 0%X\n",
462*4882a593Smuzhiyun 			  &allocation->user_space, allocation->size));
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/* Mark the segment as deleted */
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	memset(&allocation->user_space, 0xEA, allocation->size);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
469*4882a593Smuzhiyun 	return (status);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*******************************************************************************
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_dump_allocation_info
475*4882a593Smuzhiyun  *
476*4882a593Smuzhiyun  * PARAMETERS:  None
477*4882a593Smuzhiyun  *
478*4882a593Smuzhiyun  * RETURN:      None
479*4882a593Smuzhiyun  *
480*4882a593Smuzhiyun  * DESCRIPTION: Print some info about the outstanding allocations.
481*4882a593Smuzhiyun  *
482*4882a593Smuzhiyun  ******************************************************************************/
483*4882a593Smuzhiyun 
acpi_ut_dump_allocation_info(void)484*4882a593Smuzhiyun void acpi_ut_dump_allocation_info(void)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun /*
487*4882a593Smuzhiyun 	struct acpi_memory_list         *mem_list;
488*4882a593Smuzhiyun */
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ut_dump_allocation_info);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
494*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Current allocations",
495*4882a593Smuzhiyun 		mem_list->current_count,
496*4882a593Smuzhiyun 		ROUND_UP_TO_1K (mem_list->current_size)));
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
499*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
500*4882a593Smuzhiyun 		mem_list->max_concurrent_count,
501*4882a593Smuzhiyun 		ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
504*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
505*4882a593Smuzhiyun 		running_object_count,
506*4882a593Smuzhiyun 		ROUND_UP_TO_1K (running_object_size)));
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
509*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
510*4882a593Smuzhiyun 		running_alloc_count,
511*4882a593Smuzhiyun 		ROUND_UP_TO_1K (running_alloc_size)));
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
514*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Current Nodes",
515*4882a593Smuzhiyun 		acpi_gbl_current_node_count,
516*4882a593Smuzhiyun 		ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
519*4882a593Smuzhiyun 		("%30s: %4d (%3d Kb)\n", "Max Nodes",
520*4882a593Smuzhiyun 		acpi_gbl_max_concurrent_node_count,
521*4882a593Smuzhiyun 		ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
522*4882a593Smuzhiyun 			sizeof (struct acpi_namespace_node)))));
523*4882a593Smuzhiyun */
524*4882a593Smuzhiyun 	return_VOID;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun /*******************************************************************************
528*4882a593Smuzhiyun  *
529*4882a593Smuzhiyun  * FUNCTION:    acpi_ut_dump_allocations
530*4882a593Smuzhiyun  *
531*4882a593Smuzhiyun  * PARAMETERS:  component           - Component(s) to dump info for.
532*4882a593Smuzhiyun  *              module              - Module to dump info for. NULL means all.
533*4882a593Smuzhiyun  *
534*4882a593Smuzhiyun  * RETURN:      None
535*4882a593Smuzhiyun  *
536*4882a593Smuzhiyun  * DESCRIPTION: Print a list of all outstanding allocations.
537*4882a593Smuzhiyun  *
538*4882a593Smuzhiyun  ******************************************************************************/
539*4882a593Smuzhiyun 
acpi_ut_dump_allocations(u32 component,const char * module)540*4882a593Smuzhiyun void acpi_ut_dump_allocations(u32 component, const char *module)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun 	struct acpi_debug_mem_block *element;
543*4882a593Smuzhiyun 	union acpi_descriptor *descriptor;
544*4882a593Smuzhiyun 	u32 num_outstanding = 0;
545*4882a593Smuzhiyun 	u8 descriptor_type;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(ut_dump_allocations);
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	if (acpi_gbl_disable_mem_tracking) {
550*4882a593Smuzhiyun 		return_VOID;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	/*
554*4882a593Smuzhiyun 	 * Walk the allocation list.
555*4882a593Smuzhiyun 	 */
556*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
557*4882a593Smuzhiyun 		return_VOID;
558*4882a593Smuzhiyun 	}
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	if (!acpi_gbl_global_list) {
561*4882a593Smuzhiyun 		goto exit;
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	element = acpi_gbl_global_list->list_head;
565*4882a593Smuzhiyun 	while (element) {
566*4882a593Smuzhiyun 		if ((element->component & component) &&
567*4882a593Smuzhiyun 		    ((module == NULL)
568*4882a593Smuzhiyun 		     || (0 == strcmp(module, element->module)))) {
569*4882a593Smuzhiyun 			descriptor =
570*4882a593Smuzhiyun 			    ACPI_CAST_PTR(union acpi_descriptor,
571*4882a593Smuzhiyun 					  &element->user_space);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 			if (element->size <
574*4882a593Smuzhiyun 			    sizeof(struct acpi_common_descriptor)) {
575*4882a593Smuzhiyun 				acpi_os_printf("%p Length 0x%04X %9.9s-%4.4u "
576*4882a593Smuzhiyun 					       "[Not a Descriptor - too small]\n",
577*4882a593Smuzhiyun 					       descriptor, element->size,
578*4882a593Smuzhiyun 					       element->module, element->line);
579*4882a593Smuzhiyun 			} else {
580*4882a593Smuzhiyun 				/* Ignore allocated objects that are in a cache */
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 				if (ACPI_GET_DESCRIPTOR_TYPE(descriptor) !=
583*4882a593Smuzhiyun 				    ACPI_DESC_TYPE_CACHED) {
584*4882a593Smuzhiyun 					acpi_os_printf
585*4882a593Smuzhiyun 					    ("%p Length 0x%04X %9.9s-%4.4u [%s] ",
586*4882a593Smuzhiyun 					     descriptor, element->size,
587*4882a593Smuzhiyun 					     element->module, element->line,
588*4882a593Smuzhiyun 					     acpi_ut_get_descriptor_name
589*4882a593Smuzhiyun 					     (descriptor));
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 					/* Optional object hex dump */
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 					if (acpi_gbl_verbose_leak_dump) {
594*4882a593Smuzhiyun 						acpi_os_printf("\n");
595*4882a593Smuzhiyun 						acpi_ut_dump_buffer((u8 *)
596*4882a593Smuzhiyun 								    descriptor,
597*4882a593Smuzhiyun 								    element->
598*4882a593Smuzhiyun 								    size,
599*4882a593Smuzhiyun 								    DB_BYTE_DISPLAY,
600*4882a593Smuzhiyun 								    0);
601*4882a593Smuzhiyun 					}
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 					/* Validate the descriptor type using Type field and length */
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 					descriptor_type = 0;	/* Not a valid descriptor type */
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 					switch (ACPI_GET_DESCRIPTOR_TYPE
608*4882a593Smuzhiyun 						(descriptor)) {
609*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_OPERAND:
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 						if (element->size ==
612*4882a593Smuzhiyun 						    sizeof(union
613*4882a593Smuzhiyun 							   acpi_operand_object))
614*4882a593Smuzhiyun 						{
615*4882a593Smuzhiyun 							descriptor_type =
616*4882a593Smuzhiyun 							    ACPI_DESC_TYPE_OPERAND;
617*4882a593Smuzhiyun 						}
618*4882a593Smuzhiyun 						break;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_PARSER:
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 						if (element->size ==
623*4882a593Smuzhiyun 						    sizeof(union
624*4882a593Smuzhiyun 							   acpi_parse_object)) {
625*4882a593Smuzhiyun 							descriptor_type =
626*4882a593Smuzhiyun 							    ACPI_DESC_TYPE_PARSER;
627*4882a593Smuzhiyun 						}
628*4882a593Smuzhiyun 						break;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_NAMED:
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 						if (element->size ==
633*4882a593Smuzhiyun 						    sizeof(struct
634*4882a593Smuzhiyun 							   acpi_namespace_node))
635*4882a593Smuzhiyun 						{
636*4882a593Smuzhiyun 							descriptor_type =
637*4882a593Smuzhiyun 							    ACPI_DESC_TYPE_NAMED;
638*4882a593Smuzhiyun 						}
639*4882a593Smuzhiyun 						break;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 					default:
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 						break;
644*4882a593Smuzhiyun 					}
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 					/* Display additional info for the major descriptor types */
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 					switch (descriptor_type) {
649*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_OPERAND:
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 						acpi_os_printf
652*4882a593Smuzhiyun 						    ("%12.12s RefCount 0x%04X\n",
653*4882a593Smuzhiyun 						     acpi_ut_get_type_name
654*4882a593Smuzhiyun 						     (descriptor->object.common.
655*4882a593Smuzhiyun 						      type),
656*4882a593Smuzhiyun 						     descriptor->object.common.
657*4882a593Smuzhiyun 						     reference_count);
658*4882a593Smuzhiyun 						break;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_PARSER:
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 						acpi_os_printf
663*4882a593Smuzhiyun 						    ("AmlOpcode 0x%04X\n",
664*4882a593Smuzhiyun 						     descriptor->op.asl.
665*4882a593Smuzhiyun 						     aml_opcode);
666*4882a593Smuzhiyun 						break;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 					case ACPI_DESC_TYPE_NAMED:
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 						acpi_os_printf("%4.4s\n",
671*4882a593Smuzhiyun 							       acpi_ut_get_node_name
672*4882a593Smuzhiyun 							       (&descriptor->
673*4882a593Smuzhiyun 								node));
674*4882a593Smuzhiyun 						break;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 					default:
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 						acpi_os_printf("\n");
679*4882a593Smuzhiyun 						break;
680*4882a593Smuzhiyun 					}
681*4882a593Smuzhiyun 				}
682*4882a593Smuzhiyun 			}
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 			num_outstanding++;
685*4882a593Smuzhiyun 		}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 		element = element->next;
688*4882a593Smuzhiyun 	}
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun exit:
691*4882a593Smuzhiyun 	(void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	/* Print summary */
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (!num_outstanding) {
696*4882a593Smuzhiyun 		ACPI_INFO(("No outstanding allocations"));
697*4882a593Smuzhiyun 	} else {
698*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO, "%u (0x%X) Outstanding cache allocations",
699*4882a593Smuzhiyun 			    num_outstanding, num_outstanding));
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	return_VOID;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun #endif				/* ACPI_DBG_TRACK_ALLOCATIONS */
706