utdelete.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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;
  329. u32 i;
  330. union acpi_generic_state *state_list = NULL;
  331. union acpi_generic_state *state;
  332. union acpi_operand_object *tmp;
  333. ACPI_FUNCTION_TRACE_PTR ("ut_update_object_reference", object);
  334. /* Ignore a null object ptr */
  335. if (!object) {
  336. return_ACPI_STATUS (AE_OK);
  337. }
  338. /* Make sure that this isn't a namespace handle */
  339. if (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) {
  340. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  341. "Object %p is NS handle\n", object));
  342. return_ACPI_STATUS (AE_OK);
  343. }
  344. state = acpi_ut_create_update_state (object, action);
  345. while (state) {
  346. object = state->update.object;
  347. action = state->update.value;
  348. acpi_ut_delete_generic_state (state);
  349. /*
  350. * All sub-objects must have their reference count incremented also.
  351. * Different object types have different subobjects.
  352. */
  353. switch (ACPI_GET_OBJECT_TYPE (object)) {
  354. case ACPI_TYPE_DEVICE:
  355. tmp = object->device.system_notify;
  356. if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  357. object->device.system_notify = NULL;
  358. acpi_ut_update_ref_count (tmp, action);
  359. tmp = object->device.device_notify;
  360. if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  361. object->device.device_notify = NULL;
  362. acpi_ut_update_ref_count (tmp, action);
  363. break;
  364. case ACPI_TYPE_PACKAGE:
  365. /*
  366. * We must update all the sub-objects of the package
  367. * (Each of whom may have their own sub-objects, etc.
  368. */
  369. for (i = 0; i < object->package.count; i++) {
  370. /*
  371. * Push each element onto the stack for later processing.
  372. * Note: There can be null elements within the package,
  373. * these are simply ignored
  374. */
  375. status = acpi_ut_create_update_state_and_push (
  376. object->package.elements[i], action, &state_list);
  377. if (ACPI_FAILURE (status)) {
  378. goto error_exit;
  379. }
  380. tmp = object->package.elements[i];
  381. if (tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  382. object->package.elements[i] = NULL;
  383. }
  384. break;
  385. case ACPI_TYPE_BUFFER_FIELD:
  386. status = acpi_ut_create_update_state_and_push (
  387. object->buffer_field.buffer_obj, action, &state_list);
  388. if (ACPI_FAILURE (status)) {
  389. goto error_exit;
  390. }
  391. tmp = object->buffer_field.buffer_obj;
  392. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  393. object->buffer_field.buffer_obj = NULL;
  394. break;
  395. case ACPI_TYPE_LOCAL_REGION_FIELD:
  396. status = acpi_ut_create_update_state_and_push (
  397. object->field.region_obj, action, &state_list);
  398. if (ACPI_FAILURE (status)) {
  399. goto error_exit;
  400. }
  401. tmp = object->field.region_obj;
  402. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  403. object->field.region_obj = NULL;
  404. break;
  405. case ACPI_TYPE_LOCAL_BANK_FIELD:
  406. status = acpi_ut_create_update_state_and_push (
  407. object->bank_field.bank_obj, action, &state_list);
  408. if (ACPI_FAILURE (status)) {
  409. goto error_exit;
  410. }
  411. tmp = object->bank_field.bank_obj;
  412. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  413. object->bank_field.bank_obj = NULL;
  414. status = acpi_ut_create_update_state_and_push (
  415. object->bank_field.region_obj, action, &state_list);
  416. if (ACPI_FAILURE (status)) {
  417. goto error_exit;
  418. }
  419. tmp = object->bank_field.region_obj;
  420. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  421. object->bank_field.region_obj = NULL;
  422. break;
  423. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  424. status = acpi_ut_create_update_state_and_push (
  425. object->index_field.index_obj, action, &state_list);
  426. if (ACPI_FAILURE (status)) {
  427. goto error_exit;
  428. }
  429. tmp = object->index_field.index_obj;
  430. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  431. object->index_field.index_obj = NULL;
  432. status = acpi_ut_create_update_state_and_push (
  433. object->index_field.data_obj, action, &state_list);
  434. if (ACPI_FAILURE (status)) {
  435. goto error_exit;
  436. }
  437. tmp = object->index_field.data_obj;
  438. if ( tmp && (tmp->common.reference_count <= 1) && action == REF_DECREMENT)
  439. object->index_field.data_obj = NULL;
  440. break;
  441. case ACPI_TYPE_LOCAL_REFERENCE:
  442. /*
  443. * The target of an Index (a package, string, or buffer) must track
  444. * changes to the ref count of the index.
  445. */
  446. if (object->reference.opcode == AML_INDEX_OP) {
  447. status = acpi_ut_create_update_state_and_push (
  448. object->reference.object, action, &state_list);
  449. if (ACPI_FAILURE (status)) {
  450. goto error_exit;
  451. }
  452. }
  453. break;
  454. case ACPI_TYPE_REGION:
  455. default:
  456. /* No subobjects */
  457. break;
  458. }
  459. /*
  460. * Now we can update the count in the main object. This can only
  461. * happen after we update the sub-objects in case this causes the
  462. * main object to be deleted.
  463. */
  464. acpi_ut_update_ref_count (object, action);
  465. /* Move on to the next object to be updated */
  466. state = acpi_ut_pop_generic_state (&state_list);
  467. }
  468. return_ACPI_STATUS (AE_OK);
  469. error_exit:
  470. ACPI_REPORT_ERROR (("Could not update object reference count, %s\n",
  471. acpi_format_exception (status)));
  472. return_ACPI_STATUS (status);
  473. }
  474. /*******************************************************************************
  475. *
  476. * FUNCTION: acpi_ut_add_reference
  477. *
  478. * PARAMETERS: Object - Object whose reference count is to be
  479. * incremented
  480. *
  481. * RETURN: None
  482. *
  483. * DESCRIPTION: Add one reference to an ACPI object
  484. *
  485. ******************************************************************************/
  486. void
  487. acpi_ut_add_reference (
  488. union acpi_operand_object *object)
  489. {
  490. ACPI_FUNCTION_TRACE_PTR ("ut_add_reference", object);
  491. /* Ensure that we have a valid object */
  492. if (!acpi_ut_valid_internal_object (object)) {
  493. return_VOID;
  494. }
  495. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  496. "Obj %p Current Refs=%X [To Be Incremented]\n",
  497. object, object->common.reference_count));
  498. /* Increment the reference count */
  499. (void) acpi_ut_update_object_reference (object, REF_INCREMENT);
  500. return_VOID;
  501. }
  502. /*******************************************************************************
  503. *
  504. * FUNCTION: acpi_ut_remove_reference
  505. *
  506. * PARAMETERS: Object - Object whose ref count will be decremented
  507. *
  508. * RETURN: None
  509. *
  510. * DESCRIPTION: Decrement the reference count of an ACPI internal object
  511. *
  512. ******************************************************************************/
  513. void
  514. acpi_ut_remove_reference (
  515. union acpi_operand_object *object)
  516. {
  517. ACPI_FUNCTION_TRACE_PTR ("ut_remove_reference", object);
  518. /*
  519. * Allow a NULL pointer to be passed in, just ignore it. This saves
  520. * each caller from having to check. Also, ignore NS nodes.
  521. *
  522. */
  523. if (!object ||
  524. (ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED)) {
  525. return_VOID;
  526. }
  527. /* Ensure that we have a valid object */
  528. if (!acpi_ut_valid_internal_object (object)) {
  529. return_VOID;
  530. }
  531. ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
  532. "Obj %p Current Refs=%X [To Be Decremented]\n",
  533. object, object->common.reference_count));
  534. /*
  535. * Decrement the reference count, and only actually delete the object
  536. * if the reference count becomes 0. (Must also decrement the ref count
  537. * of all subobjects!)
  538. */
  539. (void) acpi_ut_update_object_reference (object, REF_DECREMENT);
  540. return_VOID;
  541. }