utdelete.c 17 KB

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