utdelete.c 18 KB

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