utdelete.c 18 KB

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