utdelete.c 18 KB

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