utdelete.c 18 KB

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