utdelete.c 18 KB

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