utdelete.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*******************************************************************************
  2. *
  3. * Module Name: utdelete - object deletion and reference count utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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. #include <acpi/acpi.h>
  43. #include <acpi/acinterp.h>
  44. #include <acpi/acnamesp.h>
  45. #include <acpi/acevents.h>
  46. #include <acpi/amlcode.h>
  47. #define _COMPONENT ACPI_UTILITIES
  48. ACPI_MODULE_NAME ("utdelete")
  49. /* Local prototypes */
  50. static void
  51. acpi_ut_delete_internal_obj (
  52. union acpi_operand_object *object);
  53. static void
  54. acpi_ut_update_ref_count (
  55. union acpi_operand_object *object,
  56. u32 action);
  57. /*******************************************************************************
  58. *
  59. * FUNCTION: acpi_ut_delete_internal_obj
  60. *
  61. * PARAMETERS: Object - Object to be deleted
  62. *
  63. * RETURN: None
  64. *
  65. * DESCRIPTION: Low level object deletion, after reference counts have been
  66. * updated (All reference counts, including sub-objects!)
  67. *
  68. ******************************************************************************/
  69. static void
  70. acpi_ut_delete_internal_obj (
  71. union acpi_operand_object *object)
  72. {
  73. void *obj_pointer = NULL;
  74. union acpi_operand_object *handler_desc;
  75. union acpi_operand_object *second_desc;
  76. union acpi_operand_object *next_desc;
  77. ACPI_FUNCTION_TRACE_PTR ("ut_delete_internal_obj", object);
  78. if (!object) {
  79. return_VOID;
  80. }
  81. /*
  82. * Must delete or free any pointers within the object that are not
  83. * actual ACPI objects (for example, a raw buffer pointer).
  84. */
  85. switch (ACPI_GET_OBJECT_TYPE (object)) {
  86. case ACPI_TYPE_STRING:
  87. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
  88. object, object->string.pointer));
  89. /* Free the actual string buffer */
  90. if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
  91. /* But only if it is NOT a pointer into an ACPI table */
  92. obj_pointer = object->string.pointer;
  93. }
  94. break;
  95. case ACPI_TYPE_BUFFER:
  96. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
  97. object, object->buffer.pointer));
  98. /* Free the actual buffer */
  99. if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
  100. /* But only if it is NOT a pointer into an ACPI table */
  101. obj_pointer = object->buffer.pointer;
  102. }
  103. break;
  104. case ACPI_TYPE_PACKAGE:
  105. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
  106. object->package.count));
  107. /*
  108. * Elements of the package are not handled here, they are deleted
  109. * separately
  110. */
  111. /* Free the (variable length) element pointer array */
  112. obj_pointer = object->package.elements;
  113. break;
  114. case ACPI_TYPE_DEVICE:
  115. if (object->device.gpe_block) {
  116. (void) acpi_ev_delete_gpe_block (object->device.gpe_block);
  117. }
  118. /* Walk the handler list for this device */
  119. handler_desc = object->device.handler;
  120. while (handler_desc) {
  121. next_desc = handler_desc->address_space.next;
  122. acpi_ut_remove_reference (handler_desc);
  123. handler_desc = next_desc;
  124. }
  125. break;
  126. case ACPI_TYPE_MUTEX:
  127. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  128. "***** Mutex %p, Semaphore %p\n",
  129. object, object->mutex.semaphore));
  130. acpi_ex_unlink_mutex (object);
  131. (void) acpi_os_delete_semaphore (object->mutex.semaphore);
  132. break;
  133. case ACPI_TYPE_EVENT:
  134. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  135. "***** Event %p, Semaphore %p\n",
  136. object, object->event.semaphore));
  137. (void) acpi_os_delete_semaphore (object->event.semaphore);
  138. object->event.semaphore = NULL;
  139. break;
  140. case ACPI_TYPE_METHOD:
  141. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  142. "***** Method %p\n", object));
  143. /* Delete the method semaphore if it exists */
  144. if (object->method.semaphore) {
  145. (void) acpi_os_delete_semaphore (object->method.semaphore);
  146. object->method.semaphore = NULL;
  147. }
  148. break;
  149. case ACPI_TYPE_REGION:
  150. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  151. "***** Region %p\n", object));
  152. second_desc = acpi_ns_get_secondary_object (object);
  153. if (second_desc) {
  154. /*
  155. * Free the region_context if and only if the handler is one of the
  156. * default handlers -- and therefore, we created the context object
  157. * locally, it was not created by an external caller.
  158. */
  159. handler_desc = object->region.handler;
  160. if (handler_desc) {
  161. if (handler_desc->address_space.hflags & ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
  162. obj_pointer = second_desc->extra.region_context;
  163. }
  164. acpi_ut_remove_reference (handler_desc);
  165. }
  166. /* Now we can free the Extra object */
  167. acpi_ut_delete_object_desc (second_desc);
  168. }
  169. break;
  170. case ACPI_TYPE_BUFFER_FIELD:
  171. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  172. "***** Buffer Field %p\n", object));
  173. second_desc = acpi_ns_get_secondary_object (object);
  174. if (second_desc) {
  175. acpi_ut_delete_object_desc (second_desc);
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. /* Free any allocated memory (pointer within the object) found above */
  182. if (obj_pointer) {
  183. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
  184. obj_pointer));
  185. ACPI_MEM_FREE (obj_pointer);
  186. }
  187. /* Now the object can be safely deleted */
  188. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object %p [%s]\n",
  189. object, acpi_ut_get_object_type_name (object)));
  190. acpi_ut_delete_object_desc (object);
  191. return_VOID;
  192. }
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_ut_delete_internal_object_list
  196. *
  197. * PARAMETERS: obj_list - Pointer to the list to be deleted
  198. *
  199. * RETURN: None
  200. *
  201. * DESCRIPTION: This function deletes an internal object list, including both
  202. * simple objects and package objects
  203. *
  204. ******************************************************************************/
  205. void
  206. acpi_ut_delete_internal_object_list (
  207. union acpi_operand_object **obj_list)
  208. {
  209. union acpi_operand_object **internal_obj;
  210. ACPI_FUNCTION_TRACE ("ut_delete_internal_object_list");
  211. /* Walk the null-terminated internal list */
  212. for (internal_obj = obj_list; *internal_obj; internal_obj++) {
  213. acpi_ut_remove_reference (*internal_obj);
  214. }
  215. /* Free the combined parameter pointer list and object array */
  216. ACPI_MEM_FREE (obj_list);
  217. return_VOID;
  218. }
  219. /*******************************************************************************
  220. *
  221. * FUNCTION: acpi_ut_update_ref_count
  222. *
  223. * PARAMETERS: Object - Object whose ref count is to be updated
  224. * Action - What to do
  225. *
  226. * RETURN: New ref count
  227. *
  228. * DESCRIPTION: Modify the ref count and return it.
  229. *
  230. ******************************************************************************/
  231. static void
  232. acpi_ut_update_ref_count (
  233. union acpi_operand_object *object,
  234. u32 action)
  235. {
  236. u16 count;
  237. u16 new_count;
  238. ACPI_FUNCTION_NAME ("ut_update_ref_count");
  239. if (!object) {
  240. return;
  241. }
  242. count = object->common.reference_count;
  243. new_count = count;
  244. /*
  245. * Perform the reference count action
  246. * (increment, decrement, or force delete)
  247. */
  248. switch (action) {
  249. case REF_INCREMENT:
  250. new_count++;
  251. object->common.reference_count = new_count;
  252. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  253. "Obj %p Refs=%X, [Incremented]\n",
  254. object, new_count));
  255. break;
  256. case REF_DECREMENT:
  257. if (count < 1) {
  258. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  259. "Obj %p Refs=%X, can't decrement! (Set to 0)\n",
  260. object, new_count));
  261. new_count = 0;
  262. }
  263. else {
  264. new_count--;
  265. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  266. "Obj %p Refs=%X, [Decremented]\n",
  267. object, new_count));
  268. }
  269. if (ACPI_GET_OBJECT_TYPE (object) == ACPI_TYPE_METHOD) {
  270. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  271. "Method Obj %p Refs=%X, [Decremented]\n",
  272. object, new_count));
  273. }
  274. object->common.reference_count = new_count;
  275. if (new_count == 0) {
  276. acpi_ut_delete_internal_obj (object);
  277. }
  278. break;
  279. case REF_FORCE_DELETE:
  280. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  281. "Obj %p Refs=%X, Force delete! (Set to 0)\n",
  282. object, count));
  283. new_count = 0;
  284. object->common.reference_count = new_count;
  285. acpi_ut_delete_internal_obj (object);
  286. break;
  287. default:
  288. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown action (%X)\n", action));
  289. break;
  290. }
  291. /*
  292. * Sanity check the reference count, for debug purposes only.
  293. * (A deleted object will have a huge reference count)
  294. */
  295. if (count > ACPI_MAX_REFERENCE_COUNT) {
  296. ACPI_DEBUG_PRINT ((ACPI_DB_WARN,
  297. "**** Warning **** Large Reference Count (%X) in object %p\n\n",
  298. count, object));
  299. }
  300. return;
  301. }
  302. /*******************************************************************************
  303. *
  304. * FUNCTION: acpi_ut_update_object_reference
  305. *
  306. * PARAMETERS: Object - Increment ref count for this object
  307. * and all sub-objects
  308. * Action - Either REF_INCREMENT or REF_DECREMENT or
  309. * REF_FORCE_DELETE
  310. *
  311. * RETURN: Status
  312. *
  313. * DESCRIPTION: Increment the object reference count
  314. *
  315. * Object references are incremented when:
  316. * 1) An object is attached to a Node (namespace object)
  317. * 2) An object is copied (all subobjects must be incremented)
  318. *
  319. * Object references are decremented when:
  320. * 1) An object is detached from an Node
  321. *
  322. ******************************************************************************/
  323. acpi_status
  324. acpi_ut_update_object_reference (
  325. union acpi_operand_object *object,
  326. u16 action)
  327. {
  328. acpi_status status = AE_OK;
  329. union acpi_generic_state *state_list = NULL;
  330. union acpi_operand_object *next_object = NULL;
  331. union acpi_generic_state *state;
  332. acpi_native_uint i;
  333. ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
  334. while (object) {
  335. /* Make sure that this isn't a namespace handle */
  336. if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
  337. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  338. "Object %p is NS handle\n", object));
  339. return_ACPI_STATUS (AE_OK);
  340. }
  341. /*
  342. * All sub-objects must have their reference count incremented also.
  343. * Different object types have different subobjects.
  344. */
  345. switch (ACPI_GET_OBJECT_TYPE (object)) {
  346. case ACPI_TYPE_DEVICE:
  347. acpi_ut_update_ref_count (object->device.system_notify, action);
  348. acpi_ut_update_ref_count (object->device.device_notify, action);
  349. break;
  350. case ACPI_TYPE_PACKAGE:
  351. /*
  352. * We must update all the sub-objects of the package,
  353. * each of whom may have their own sub-objects.
  354. */
  355. for (i = 0; i < object->package.count; i++) {
  356. /*
  357. * Push each element onto the stack for later processing.
  358. * Note: There can be null elements within the package,
  359. * these are simply ignored
  360. */
  361. status = acpi_ut_create_update_state_and_push (
  362. object->package.elements[i], action, &state_list);
  363. if (ACPI_FAILURE (status)) {
  364. goto error_exit;
  365. }
  366. }
  367. break;
  368. case ACPI_TYPE_BUFFER_FIELD:
  369. next_object = object->buffer_field.buffer_obj;
  370. break;
  371. case ACPI_TYPE_LOCAL_REGION_FIELD:
  372. next_object = object->field.region_obj;
  373. break;
  374. case ACPI_TYPE_LOCAL_BANK_FIELD:
  375. next_object = object->bank_field.bank_obj;
  376. status = acpi_ut_create_update_state_and_push (
  377. object->bank_field.region_obj, action, &state_list);
  378. if (ACPI_FAILURE (status)) {
  379. goto error_exit;
  380. }
  381. break;
  382. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  383. next_object = object->index_field.index_obj;
  384. status = acpi_ut_create_update_state_and_push (
  385. object->index_field.data_obj, action, &state_list);
  386. if (ACPI_FAILURE (status)) {
  387. goto error_exit;
  388. }
  389. break;
  390. case ACPI_TYPE_LOCAL_REFERENCE:
  391. /*
  392. * The target of an Index (a package, string, or buffer) must track
  393. * changes to the ref count of the index.
  394. */
  395. if (object->reference.opcode == AML_INDEX_OP) {
  396. next_object = object->reference.object;
  397. }
  398. break;
  399. case ACPI_TYPE_REGION:
  400. default:
  401. break;/* No subobjects */
  402. }
  403. /*
  404. * Now we can update the count in the main object. This can only
  405. * happen after we update the sub-objects in case this causes the
  406. * main object to be deleted.
  407. */
  408. acpi_ut_update_ref_count (object, action);
  409. object = NULL;
  410. /* Move on to the next object to be updated */
  411. if (next_object) {
  412. object = next_object;
  413. next_object = NULL;
  414. }
  415. else if (state_list) {
  416. state = acpi_ut_pop_generic_state (&state_list);
  417. object = state->update.object;
  418. acpi_ut_delete_generic_state (state);
  419. }
  420. }
  421. return_ACPI_STATUS (AE_OK);
  422. error_exit:
  423. ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
  424. acpi_format_exception (status)));
  425. return_ACPI_STATUS (status);
  426. }
  427. /*******************************************************************************
  428. *
  429. * FUNCTION: acpi_ut_add_reference
  430. *
  431. * PARAMETERS: Object - Object whose reference count is to be
  432. * incremented
  433. *
  434. * RETURN: None
  435. *
  436. * DESCRIPTION: Add one reference to an ACPI object
  437. *
  438. ******************************************************************************/
  439. void
  440. acpi_ut_add_reference (
  441. union acpi_operand_object *object)
  442. {
  443. ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
  444. /* Ensure that we have a valid object */
  445. if (!acpi_ut_valid_internal_object (object)) {
  446. return_VOID;
  447. }
  448. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  449. "Obj %p Current Refs=%X [To Be Incremented]\n",
  450. object, object->common.reference_count));
  451. /* Increment the reference count */
  452. (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
  453. return_VOID;
  454. }
  455. /*******************************************************************************
  456. *
  457. * FUNCTION: acpi_ut_remove_reference
  458. *
  459. * PARAMETERS: Object - Object whose ref count will be decremented
  460. *
  461. * RETURN: None
  462. *
  463. * DESCRIPTION: Decrement the reference count of an ACPI internal object
  464. *
  465. ******************************************************************************/
  466. void
  467. acpi_ut_remove_reference (
  468. union acpi_operand_object *object)
  469. {
  470. ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
  471. /*
  472. * Allow a NULL pointer to be passed in, just ignore it. This saves
  473. * each caller from having to check. Also, ignore NS nodes.
  474. *
  475. */
  476. if (!object ||
  477. (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
  478. return_VOID;
  479. }
  480. /* Ensure that we have a valid object */
  481. if (!acpi_ut_valid_internal_object (object)) {
  482. return_VOID;
  483. }
  484. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  485. "Obj %p Current Refs=%X [To Be Decremented]\n",
  486. object, object->common.reference_count));
  487. /*
  488. * Decrement the reference count, and only actually delete the object
  489. * if the reference count becomes 0. (Must also decrement the ref count
  490. * of all subobjects!)
  491. */
  492. (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
  493. return_VOID;
  494. }