uttrack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /******************************************************************************
  2. *
  3. * Module Name: uttrack - Memory allocation tracking routines (debug only)
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. /*
  43. * These procedures are used for tracking memory leaks in the subsystem, and
  44. * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
  45. *
  46. * Each memory allocation is tracked via a doubly linked list. Each
  47. * element contains the caller's component, module name, function name, and
  48. * line number. acpi_ut_allocate and acpi_ut_allocate_zeroed call
  49. * acpi_ut_track_allocation to add an element to the list; deletion
  50. * occurs in the body of acpi_ut_free.
  51. */
  52. #include <acpi/acpi.h>
  53. #include "accommon.h"
  54. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  55. #define _COMPONENT ACPI_UTILITIES
  56. ACPI_MODULE_NAME("uttrack")
  57. /* Local prototypes */
  58. static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation);
  59. static acpi_status
  60. acpi_ut_track_allocation(struct acpi_debug_mem_block *address,
  61. acpi_size size,
  62. u8 alloc_type,
  63. u32 component, const char *module, u32 line);
  64. static acpi_status
  65. acpi_ut_remove_allocation(struct acpi_debug_mem_block *address,
  66. u32 component, const char *module, u32 line);
  67. /*******************************************************************************
  68. *
  69. * FUNCTION: acpi_ut_create_list
  70. *
  71. * PARAMETERS: cache_name - Ascii name for the cache
  72. * object_size - Size of each cached object
  73. * return_cache - Where the new cache object is returned
  74. *
  75. * RETURN: Status
  76. *
  77. * DESCRIPTION: Create a local memory list for tracking purposed
  78. *
  79. ******************************************************************************/
  80. acpi_status
  81. acpi_ut_create_list(char *list_name,
  82. u16 object_size, struct acpi_memory_list **return_cache)
  83. {
  84. struct acpi_memory_list *cache;
  85. cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
  86. if (!cache) {
  87. return (AE_NO_MEMORY);
  88. }
  89. ACPI_MEMSET(cache, 0, sizeof(struct acpi_memory_list));
  90. cache->list_name = list_name;
  91. cache->object_size = object_size;
  92. *return_cache = cache;
  93. return (AE_OK);
  94. }
  95. /*******************************************************************************
  96. *
  97. * FUNCTION: acpi_ut_allocate_and_track
  98. *
  99. * PARAMETERS: size - Size of the allocation
  100. * component - Component type of caller
  101. * module - Source file name of caller
  102. * line - Line number of caller
  103. *
  104. * RETURN: Address of the allocated memory on success, NULL on failure.
  105. *
  106. * DESCRIPTION: The subsystem's equivalent of malloc.
  107. *
  108. ******************************************************************************/
  109. void *acpi_ut_allocate_and_track(acpi_size size,
  110. u32 component, const char *module, u32 line)
  111. {
  112. struct acpi_debug_mem_block *allocation;
  113. acpi_status status;
  114. allocation =
  115. acpi_ut_allocate(size + sizeof(struct acpi_debug_mem_header),
  116. component, module, line);
  117. if (!allocation) {
  118. return (NULL);
  119. }
  120. status = acpi_ut_track_allocation(allocation, size,
  121. ACPI_MEM_MALLOC, component, module,
  122. line);
  123. if (ACPI_FAILURE(status)) {
  124. acpi_os_free(allocation);
  125. return (NULL);
  126. }
  127. acpi_gbl_global_list->total_allocated++;
  128. acpi_gbl_global_list->total_size += (u32)size;
  129. acpi_gbl_global_list->current_total_size += (u32)size;
  130. if (acpi_gbl_global_list->current_total_size >
  131. acpi_gbl_global_list->max_occupied) {
  132. acpi_gbl_global_list->max_occupied =
  133. acpi_gbl_global_list->current_total_size;
  134. }
  135. return ((void *)&allocation->user_space);
  136. }
  137. /*******************************************************************************
  138. *
  139. * FUNCTION: acpi_ut_allocate_zeroed_and_track
  140. *
  141. * PARAMETERS: size - Size of the allocation
  142. * component - Component type of caller
  143. * module - Source file name of caller
  144. * line - Line number of caller
  145. *
  146. * RETURN: Address of the allocated memory on success, NULL on failure.
  147. *
  148. * DESCRIPTION: Subsystem equivalent of calloc.
  149. *
  150. ******************************************************************************/
  151. void *acpi_ut_allocate_zeroed_and_track(acpi_size size,
  152. u32 component,
  153. const char *module, u32 line)
  154. {
  155. struct acpi_debug_mem_block *allocation;
  156. acpi_status status;
  157. allocation =
  158. acpi_ut_allocate_zeroed(size + sizeof(struct acpi_debug_mem_header),
  159. component, module, line);
  160. if (!allocation) {
  161. /* Report allocation error */
  162. ACPI_ERROR((module, line,
  163. "Could not allocate size %u", (u32)size));
  164. return (NULL);
  165. }
  166. status = acpi_ut_track_allocation(allocation, size,
  167. ACPI_MEM_CALLOC, component, module,
  168. line);
  169. if (ACPI_FAILURE(status)) {
  170. acpi_os_free(allocation);
  171. return (NULL);
  172. }
  173. acpi_gbl_global_list->total_allocated++;
  174. acpi_gbl_global_list->total_size += (u32)size;
  175. acpi_gbl_global_list->current_total_size += (u32)size;
  176. if (acpi_gbl_global_list->current_total_size >
  177. acpi_gbl_global_list->max_occupied) {
  178. acpi_gbl_global_list->max_occupied =
  179. acpi_gbl_global_list->current_total_size;
  180. }
  181. return ((void *)&allocation->user_space);
  182. }
  183. /*******************************************************************************
  184. *
  185. * FUNCTION: acpi_ut_free_and_track
  186. *
  187. * PARAMETERS: allocation - Address of the memory to deallocate
  188. * component - Component type of caller
  189. * module - Source file name of caller
  190. * line - Line number of caller
  191. *
  192. * RETURN: None
  193. *
  194. * DESCRIPTION: Frees the memory at Allocation
  195. *
  196. ******************************************************************************/
  197. void
  198. acpi_ut_free_and_track(void *allocation,
  199. u32 component, const char *module, u32 line)
  200. {
  201. struct acpi_debug_mem_block *debug_block;
  202. acpi_status status;
  203. ACPI_FUNCTION_TRACE_PTR(ut_free, allocation);
  204. if (NULL == allocation) {
  205. ACPI_ERROR((module, line, "Attempt to delete a NULL address"));
  206. return_VOID;
  207. }
  208. debug_block = ACPI_CAST_PTR(struct acpi_debug_mem_block,
  209. (((char *)allocation) -
  210. sizeof(struct acpi_debug_mem_header)));
  211. acpi_gbl_global_list->total_freed++;
  212. acpi_gbl_global_list->current_total_size -= debug_block->size;
  213. status = acpi_ut_remove_allocation(debug_block,
  214. component, module, line);
  215. if (ACPI_FAILURE(status)) {
  216. ACPI_EXCEPTION((AE_INFO, status, "Could not free memory"));
  217. }
  218. acpi_os_free(debug_block);
  219. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p freed\n", allocation));
  220. return_VOID;
  221. }
  222. /*******************************************************************************
  223. *
  224. * FUNCTION: acpi_ut_find_allocation
  225. *
  226. * PARAMETERS: allocation - Address of allocated memory
  227. *
  228. * RETURN: A list element if found; NULL otherwise.
  229. *
  230. * DESCRIPTION: Searches for an element in the global allocation tracking list.
  231. *
  232. ******************************************************************************/
  233. static struct acpi_debug_mem_block *acpi_ut_find_allocation(void *allocation)
  234. {
  235. struct acpi_debug_mem_block *element;
  236. ACPI_FUNCTION_ENTRY();
  237. element = acpi_gbl_global_list->list_head;
  238. /* Search for the address. */
  239. while (element) {
  240. if (element == allocation) {
  241. return (element);
  242. }
  243. element = element->next;
  244. }
  245. return (NULL);
  246. }
  247. /*******************************************************************************
  248. *
  249. * FUNCTION: acpi_ut_track_allocation
  250. *
  251. * PARAMETERS: allocation - Address of allocated memory
  252. * size - Size of the allocation
  253. * alloc_type - MEM_MALLOC or MEM_CALLOC
  254. * component - Component type of caller
  255. * module - Source file name of caller
  256. * line - Line number of caller
  257. *
  258. * RETURN: None.
  259. *
  260. * DESCRIPTION: Inserts an element into the global allocation tracking list.
  261. *
  262. ******************************************************************************/
  263. static acpi_status
  264. acpi_ut_track_allocation(struct acpi_debug_mem_block *allocation,
  265. acpi_size size,
  266. u8 alloc_type,
  267. u32 component, const char *module, u32 line)
  268. {
  269. struct acpi_memory_list *mem_list;
  270. struct acpi_debug_mem_block *element;
  271. acpi_status status = AE_OK;
  272. ACPI_FUNCTION_TRACE_PTR(ut_track_allocation, allocation);
  273. if (acpi_gbl_disable_mem_tracking) {
  274. return_ACPI_STATUS(AE_OK);
  275. }
  276. mem_list = acpi_gbl_global_list;
  277. status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
  278. if (ACPI_FAILURE(status)) {
  279. return_ACPI_STATUS(status);
  280. }
  281. /*
  282. * Search list for this address to make sure it is not already on the list.
  283. * This will catch several kinds of problems.
  284. */
  285. element = acpi_ut_find_allocation(allocation);
  286. if (element) {
  287. ACPI_ERROR((AE_INFO,
  288. "UtTrackAllocation: Allocation already present in list! (%p)",
  289. allocation));
  290. ACPI_ERROR((AE_INFO, "Element %p Address %p",
  291. element, allocation));
  292. goto unlock_and_exit;
  293. }
  294. /* Fill in the instance data. */
  295. allocation->size = (u32)size;
  296. allocation->alloc_type = alloc_type;
  297. allocation->component = component;
  298. allocation->line = line;
  299. ACPI_STRNCPY(allocation->module, module, ACPI_MAX_MODULE_NAME);
  300. allocation->module[ACPI_MAX_MODULE_NAME - 1] = 0;
  301. /* Insert at list head */
  302. if (mem_list->list_head) {
  303. ((struct acpi_debug_mem_block *)(mem_list->list_head))->
  304. previous = allocation;
  305. }
  306. allocation->next = mem_list->list_head;
  307. allocation->previous = NULL;
  308. mem_list->list_head = allocation;
  309. unlock_and_exit:
  310. status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
  311. return_ACPI_STATUS(status);
  312. }
  313. /*******************************************************************************
  314. *
  315. * FUNCTION: acpi_ut_remove_allocation
  316. *
  317. * PARAMETERS: allocation - Address of allocated memory
  318. * component - Component type of caller
  319. * module - Source file name of caller
  320. * line - Line number of caller
  321. *
  322. * RETURN:
  323. *
  324. * DESCRIPTION: Deletes an element from the global allocation tracking list.
  325. *
  326. ******************************************************************************/
  327. static acpi_status
  328. acpi_ut_remove_allocation(struct acpi_debug_mem_block *allocation,
  329. u32 component, const char *module, u32 line)
  330. {
  331. struct acpi_memory_list *mem_list;
  332. acpi_status status;
  333. ACPI_FUNCTION_TRACE(ut_remove_allocation);
  334. if (acpi_gbl_disable_mem_tracking) {
  335. return_ACPI_STATUS(AE_OK);
  336. }
  337. mem_list = acpi_gbl_global_list;
  338. if (NULL == mem_list->list_head) {
  339. /* No allocations! */
  340. ACPI_ERROR((module, line,
  341. "Empty allocation list, nothing to free!"));
  342. return_ACPI_STATUS(AE_OK);
  343. }
  344. status = acpi_ut_acquire_mutex(ACPI_MTX_MEMORY);
  345. if (ACPI_FAILURE(status)) {
  346. return_ACPI_STATUS(status);
  347. }
  348. /* Unlink */
  349. if (allocation->previous) {
  350. (allocation->previous)->next = allocation->next;
  351. } else {
  352. mem_list->list_head = allocation->next;
  353. }
  354. if (allocation->next) {
  355. (allocation->next)->previous = allocation->previous;
  356. }
  357. /* Mark the segment as deleted */
  358. ACPI_MEMSET(&allocation->user_space, 0xEA, allocation->size);
  359. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Freeing size 0%X\n",
  360. allocation->size));
  361. status = acpi_ut_release_mutex(ACPI_MTX_MEMORY);
  362. return_ACPI_STATUS(status);
  363. }
  364. /*******************************************************************************
  365. *
  366. * FUNCTION: acpi_ut_dump_allocation_info
  367. *
  368. * PARAMETERS:
  369. *
  370. * RETURN: None
  371. *
  372. * DESCRIPTION: Print some info about the outstanding allocations.
  373. *
  374. ******************************************************************************/
  375. void acpi_ut_dump_allocation_info(void)
  376. {
  377. /*
  378. struct acpi_memory_list *mem_list;
  379. */
  380. ACPI_FUNCTION_TRACE(ut_dump_allocation_info);
  381. /*
  382. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  383. ("%30s: %4d (%3d Kb)\n", "Current allocations",
  384. mem_list->current_count,
  385. ROUND_UP_TO_1K (mem_list->current_size)));
  386. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  387. ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
  388. mem_list->max_concurrent_count,
  389. ROUND_UP_TO_1K (mem_list->max_concurrent_size)));
  390. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  391. ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
  392. running_object_count,
  393. ROUND_UP_TO_1K (running_object_size)));
  394. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  395. ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
  396. running_alloc_count,
  397. ROUND_UP_TO_1K (running_alloc_size)));
  398. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  399. ("%30s: %4d (%3d Kb)\n", "Current Nodes",
  400. acpi_gbl_current_node_count,
  401. ROUND_UP_TO_1K (acpi_gbl_current_node_size)));
  402. ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
  403. ("%30s: %4d (%3d Kb)\n", "Max Nodes",
  404. acpi_gbl_max_concurrent_node_count,
  405. ROUND_UP_TO_1K ((acpi_gbl_max_concurrent_node_count *
  406. sizeof (struct acpi_namespace_node)))));
  407. */
  408. return_VOID;
  409. }
  410. /*******************************************************************************
  411. *
  412. * FUNCTION: acpi_ut_dump_allocations
  413. *
  414. * PARAMETERS: component - Component(s) to dump info for.
  415. * module - Module to dump info for. NULL means all.
  416. *
  417. * RETURN: None
  418. *
  419. * DESCRIPTION: Print a list of all outstanding allocations.
  420. *
  421. ******************************************************************************/
  422. void acpi_ut_dump_allocations(u32 component, const char *module)
  423. {
  424. struct acpi_debug_mem_block *element;
  425. union acpi_descriptor *descriptor;
  426. u32 num_outstanding = 0;
  427. u8 descriptor_type;
  428. ACPI_FUNCTION_TRACE(ut_dump_allocations);
  429. if (acpi_gbl_disable_mem_tracking) {
  430. return_VOID;
  431. }
  432. /*
  433. * Walk the allocation list.
  434. */
  435. if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_MEMORY))) {
  436. return_VOID;
  437. }
  438. element = acpi_gbl_global_list->list_head;
  439. while (element) {
  440. if ((element->component & component) &&
  441. ((module == NULL)
  442. || (0 == ACPI_STRCMP(module, element->module)))) {
  443. descriptor =
  444. ACPI_CAST_PTR(union acpi_descriptor,
  445. &element->user_space);
  446. if (element->size <
  447. sizeof(struct acpi_common_descriptor)) {
  448. acpi_os_printf("%p Length 0x%04X %9.9s-%u "
  449. "[Not a Descriptor - too small]\n",
  450. descriptor, element->size,
  451. element->module, element->line);
  452. } else {
  453. /* Ignore allocated objects that are in a cache */
  454. if (ACPI_GET_DESCRIPTOR_TYPE(descriptor) !=
  455. ACPI_DESC_TYPE_CACHED) {
  456. acpi_os_printf
  457. ("%p Length 0x%04X %9.9s-%u [%s] ",
  458. descriptor, element->size,
  459. element->module, element->line,
  460. acpi_ut_get_descriptor_name
  461. (descriptor));
  462. /* Validate the descriptor type using Type field and length */
  463. descriptor_type = 0; /* Not a valid descriptor type */
  464. switch (ACPI_GET_DESCRIPTOR_TYPE
  465. (descriptor)) {
  466. case ACPI_DESC_TYPE_OPERAND:
  467. if (element->size ==
  468. sizeof(union
  469. acpi_operand_object))
  470. {
  471. descriptor_type =
  472. ACPI_DESC_TYPE_OPERAND;
  473. }
  474. break;
  475. case ACPI_DESC_TYPE_PARSER:
  476. if (element->size ==
  477. sizeof(union
  478. acpi_parse_object)) {
  479. descriptor_type =
  480. ACPI_DESC_TYPE_PARSER;
  481. }
  482. break;
  483. case ACPI_DESC_TYPE_NAMED:
  484. if (element->size ==
  485. sizeof(struct
  486. acpi_namespace_node))
  487. {
  488. descriptor_type =
  489. ACPI_DESC_TYPE_NAMED;
  490. }
  491. break;
  492. default:
  493. break;
  494. }
  495. /* Display additional info for the major descriptor types */
  496. switch (descriptor_type) {
  497. case ACPI_DESC_TYPE_OPERAND:
  498. acpi_os_printf
  499. ("%12.12s RefCount 0x%04X\n",
  500. acpi_ut_get_type_name
  501. (descriptor->object.common.
  502. type),
  503. descriptor->object.common.
  504. reference_count);
  505. break;
  506. case ACPI_DESC_TYPE_PARSER:
  507. acpi_os_printf
  508. ("AmlOpcode 0x%04hX\n",
  509. descriptor->op.asl.
  510. aml_opcode);
  511. break;
  512. case ACPI_DESC_TYPE_NAMED:
  513. acpi_os_printf("%4.4s\n",
  514. acpi_ut_get_node_name
  515. (&descriptor->
  516. node));
  517. break;
  518. default:
  519. acpi_os_printf("\n");
  520. break;
  521. }
  522. }
  523. }
  524. num_outstanding++;
  525. }
  526. element = element->next;
  527. }
  528. (void)acpi_ut_release_mutex(ACPI_MTX_MEMORY);
  529. /* Print summary */
  530. if (!num_outstanding) {
  531. ACPI_INFO((AE_INFO, "No outstanding allocations"));
  532. } else {
  533. ACPI_ERROR((AE_INFO, "%u(0x%X) Outstanding allocations",
  534. num_outstanding, num_outstanding));
  535. }
  536. return_VOID;
  537. }
  538. #endif /* ACPI_DBG_TRACK_ALLOCATIONS */