utdelete.c 18 KB

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