utdelete.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utdelete - object deletion and reference count utilities
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acinterp.h"
  10. #include "acnamesp.h"
  11. #include "acevents.h"
  12. #define _COMPONENT ACPI_UTILITIES
  13. ACPI_MODULE_NAME("utdelete")
  14. /* Local prototypes */
  15. static void acpi_ut_delete_internal_obj(union acpi_operand_object *object);
  16. static void
  17. acpi_ut_update_ref_count(union acpi_operand_object *object, u32 action);
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_ut_delete_internal_obj
  21. *
  22. * PARAMETERS: object - Object to be deleted
  23. *
  24. * RETURN: None
  25. *
  26. * DESCRIPTION: Low level object deletion, after reference counts have been
  27. * updated (All reference counts, including sub-objects!)
  28. *
  29. ******************************************************************************/
  30. static void acpi_ut_delete_internal_obj(union acpi_operand_object *object)
  31. {
  32. void *obj_pointer = NULL;
  33. union acpi_operand_object *handler_desc;
  34. union acpi_operand_object *second_desc;
  35. union acpi_operand_object *next_desc;
  36. union acpi_operand_object *start_desc;
  37. union acpi_operand_object **last_obj_ptr;
  38. ACPI_FUNCTION_TRACE_PTR(ut_delete_internal_obj, object);
  39. if (!object) {
  40. return_VOID;
  41. }
  42. /*
  43. * Must delete or free any pointers within the object that are not
  44. * actual ACPI objects (for example, a raw buffer pointer).
  45. */
  46. switch (object->common.type) {
  47. case ACPI_TYPE_STRING:
  48. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  49. "**** String %p, ptr %p\n", object,
  50. object->string.pointer));
  51. /* Free the actual string buffer */
  52. if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
  53. /* But only if it is NOT a pointer into an ACPI table */
  54. obj_pointer = object->string.pointer;
  55. }
  56. break;
  57. case ACPI_TYPE_BUFFER:
  58. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  59. "**** Buffer %p, ptr %p\n", object,
  60. object->buffer.pointer));
  61. /* Free the actual buffer */
  62. if (!(object->common.flags & AOPOBJ_STATIC_POINTER)) {
  63. /* But only if it is NOT a pointer into an ACPI table */
  64. obj_pointer = object->buffer.pointer;
  65. }
  66. break;
  67. case ACPI_TYPE_PACKAGE:
  68. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  69. " **** Package of count %X\n",
  70. object->package.count));
  71. /*
  72. * Elements of the package are not handled here, they are deleted
  73. * separately
  74. */
  75. /* Free the (variable length) element pointer array */
  76. obj_pointer = object->package.elements;
  77. break;
  78. /*
  79. * These objects have a possible list of notify handlers.
  80. * Device object also may have a GPE block.
  81. */
  82. case ACPI_TYPE_DEVICE:
  83. if (object->device.gpe_block) {
  84. (void)acpi_ev_delete_gpe_block(object->device.
  85. gpe_block);
  86. }
  87. ACPI_FALLTHROUGH;
  88. case ACPI_TYPE_PROCESSOR:
  89. case ACPI_TYPE_THERMAL:
  90. /* Walk the address handler list for this object */
  91. handler_desc = object->common_notify.handler;
  92. while (handler_desc) {
  93. next_desc = handler_desc->address_space.next;
  94. acpi_ut_remove_reference(handler_desc);
  95. handler_desc = next_desc;
  96. }
  97. break;
  98. case ACPI_TYPE_MUTEX:
  99. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  100. "***** Mutex %p, OS Mutex %p\n",
  101. object, object->mutex.os_mutex));
  102. if (object == acpi_gbl_global_lock_mutex) {
  103. /* Global Lock has extra semaphore */
  104. (void)
  105. acpi_os_delete_semaphore
  106. (acpi_gbl_global_lock_semaphore);
  107. acpi_gbl_global_lock_semaphore = NULL;
  108. acpi_os_delete_mutex(object->mutex.os_mutex);
  109. acpi_gbl_global_lock_mutex = NULL;
  110. } else {
  111. acpi_ex_unlink_mutex(object);
  112. acpi_os_delete_mutex(object->mutex.os_mutex);
  113. }
  114. break;
  115. case ACPI_TYPE_EVENT:
  116. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  117. "***** Event %p, OS Semaphore %p\n",
  118. object, object->event.os_semaphore));
  119. (void)acpi_os_delete_semaphore(object->event.os_semaphore);
  120. object->event.os_semaphore = NULL;
  121. break;
  122. case ACPI_TYPE_METHOD:
  123. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  124. "***** Method %p\n", object));
  125. /* Delete the method mutex if it exists */
  126. if (object->method.mutex) {
  127. acpi_os_delete_mutex(object->method.mutex->mutex.
  128. os_mutex);
  129. acpi_ut_delete_object_desc(object->method.mutex);
  130. object->method.mutex = NULL;
  131. }
  132. if (object->method.node) {
  133. object->method.node = NULL;
  134. }
  135. break;
  136. case ACPI_TYPE_REGION:
  137. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  138. "***** Region %p\n", object));
  139. /*
  140. * Update address_range list. However, only permanent regions
  141. * are installed in this list. (Not created within a method)
  142. */
  143. if (!(object->region.node->flags & ANOBJ_TEMPORARY)) {
  144. acpi_ut_remove_address_range(object->region.space_id,
  145. object->region.node);
  146. }
  147. second_desc = acpi_ns_get_secondary_object(object);
  148. if (second_desc) {
  149. /*
  150. * Free the region_context if and only if the handler is one of the
  151. * default handlers -- and therefore, we created the context object
  152. * locally, it was not created by an external caller.
  153. */
  154. handler_desc = object->region.handler;
  155. if (handler_desc) {
  156. next_desc =
  157. handler_desc->address_space.region_list;
  158. start_desc = next_desc;
  159. last_obj_ptr =
  160. &handler_desc->address_space.region_list;
  161. /* Remove the region object from the handler list */
  162. while (next_desc) {
  163. if (next_desc == object) {
  164. *last_obj_ptr =
  165. next_desc->region.next;
  166. break;
  167. }
  168. /* Walk the linked list of handlers */
  169. last_obj_ptr = &next_desc->region.next;
  170. next_desc = next_desc->region.next;
  171. /* Prevent infinite loop if list is corrupted */
  172. if (next_desc == start_desc) {
  173. ACPI_ERROR((AE_INFO,
  174. "Circular region list in address handler object %p",
  175. handler_desc));
  176. return_VOID;
  177. }
  178. }
  179. if (handler_desc->address_space.handler_flags &
  180. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
  181. /* Deactivate region and free region context */
  182. if (handler_desc->address_space.setup) {
  183. (void)handler_desc->
  184. address_space.setup(object,
  185. ACPI_REGION_DEACTIVATE,
  186. handler_desc->
  187. address_space.
  188. context,
  189. &second_desc->
  190. extra.
  191. region_context);
  192. }
  193. }
  194. acpi_ut_remove_reference(handler_desc);
  195. }
  196. /* Now we can free the Extra object */
  197. acpi_ut_delete_object_desc(second_desc);
  198. }
  199. if (object->field.internal_pcc_buffer) {
  200. ACPI_FREE(object->field.internal_pcc_buffer);
  201. }
  202. break;
  203. case ACPI_TYPE_BUFFER_FIELD:
  204. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  205. "***** Buffer Field %p\n", object));
  206. second_desc = acpi_ns_get_secondary_object(object);
  207. if (second_desc) {
  208. acpi_ut_delete_object_desc(second_desc);
  209. }
  210. break;
  211. case ACPI_TYPE_LOCAL_BANK_FIELD:
  212. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  213. "***** Bank Field %p\n", object));
  214. second_desc = acpi_ns_get_secondary_object(object);
  215. if (second_desc) {
  216. acpi_ut_delete_object_desc(second_desc);
  217. }
  218. break;
  219. case ACPI_TYPE_LOCAL_ADDRESS_HANDLER:
  220. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  221. "***** Address handler %p\n", object));
  222. acpi_os_delete_mutex(object->address_space.context_mutex);
  223. break;
  224. default:
  225. break;
  226. }
  227. /* Free any allocated memory (pointer within the object) found above */
  228. if (obj_pointer) {
  229. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  230. "Deleting Object Subptr %p\n", obj_pointer));
  231. ACPI_FREE(obj_pointer);
  232. }
  233. /* Now the object can be safely deleted */
  234. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  235. "%s: Deleting Object %p [%s]\n",
  236. ACPI_GET_FUNCTION_NAME, object,
  237. acpi_ut_get_object_type_name(object)));
  238. acpi_ut_delete_object_desc(object);
  239. return_VOID;
  240. }
  241. /*******************************************************************************
  242. *
  243. * FUNCTION: acpi_ut_delete_internal_object_list
  244. *
  245. * PARAMETERS: obj_list - Pointer to the list to be deleted
  246. *
  247. * RETURN: None
  248. *
  249. * DESCRIPTION: This function deletes an internal object list, including both
  250. * simple objects and package objects
  251. *
  252. ******************************************************************************/
  253. void acpi_ut_delete_internal_object_list(union acpi_operand_object **obj_list)
  254. {
  255. union acpi_operand_object **internal_obj;
  256. ACPI_FUNCTION_ENTRY();
  257. /* Walk the null-terminated internal list */
  258. for (internal_obj = obj_list; *internal_obj; internal_obj++) {
  259. acpi_ut_remove_reference(*internal_obj);
  260. }
  261. /* Free the combined parameter pointer list and object array */
  262. ACPI_FREE(obj_list);
  263. return;
  264. }
  265. /*******************************************************************************
  266. *
  267. * FUNCTION: acpi_ut_update_ref_count
  268. *
  269. * PARAMETERS: object - Object whose ref count is to be updated
  270. * action - What to do (REF_INCREMENT or REF_DECREMENT)
  271. *
  272. * RETURN: None. Sets new reference count within the object
  273. *
  274. * DESCRIPTION: Modify the reference count for an internal acpi object
  275. *
  276. ******************************************************************************/
  277. static void
  278. acpi_ut_update_ref_count(union acpi_operand_object *object, u32 action)
  279. {
  280. u16 original_count;
  281. u16 new_count = 0;
  282. acpi_cpu_flags lock_flags;
  283. char *message;
  284. ACPI_FUNCTION_NAME(ut_update_ref_count);
  285. if (!object) {
  286. return;
  287. }
  288. /*
  289. * Always get the reference count lock. Note: Interpreter and/or
  290. * Namespace is not always locked when this function is called.
  291. */
  292. lock_flags = acpi_os_acquire_lock(acpi_gbl_reference_count_lock);
  293. original_count = object->common.reference_count;
  294. /* Perform the reference count action (increment, decrement) */
  295. switch (action) {
  296. case REF_INCREMENT:
  297. new_count = original_count + 1;
  298. object->common.reference_count = new_count;
  299. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  300. /* The current reference count should never be zero here */
  301. if (!original_count) {
  302. ACPI_WARNING((AE_INFO,
  303. "Obj %p, Reference Count was zero before increment\n",
  304. object));
  305. }
  306. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  307. "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
  308. object, object->common.type,
  309. acpi_ut_get_object_type_name(object),
  310. new_count));
  311. message = "Incremement";
  312. break;
  313. case REF_DECREMENT:
  314. /* The current reference count must be non-zero */
  315. if (original_count) {
  316. new_count = original_count - 1;
  317. object->common.reference_count = new_count;
  318. }
  319. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  320. if (!original_count) {
  321. ACPI_WARNING((AE_INFO,
  322. "Obj %p, Reference Count is already zero, cannot decrement\n",
  323. object));
  324. return;
  325. }
  326. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  327. "%s: Obj %p Type %.2X Refs %.2X [Decremented]\n",
  328. ACPI_GET_FUNCTION_NAME, object,
  329. object->common.type, new_count));
  330. /* Actually delete the object on a reference count of zero */
  331. if (new_count == 0) {
  332. acpi_ut_delete_internal_obj(object);
  333. }
  334. message = "Decrement";
  335. break;
  336. default:
  337. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  338. ACPI_ERROR((AE_INFO, "Unknown Reference Count action (0x%X)",
  339. action));
  340. return;
  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 (new_count > ACPI_MAX_REFERENCE_COUNT) {
  347. ACPI_WARNING((AE_INFO,
  348. "Large Reference Count (0x%X) in object %p, Type=0x%.2X Operation=%s",
  349. new_count, object, object->common.type, message));
  350. }
  351. }
  352. /*******************************************************************************
  353. *
  354. * FUNCTION: acpi_ut_update_object_reference
  355. *
  356. * PARAMETERS: object - Increment or decrement the ref count for
  357. * this object and all sub-objects
  358. * action - Either REF_INCREMENT or REF_DECREMENT
  359. *
  360. * RETURN: Status
  361. *
  362. * DESCRIPTION: Increment or decrement the object reference count
  363. *
  364. * Object references are incremented when:
  365. * 1) An object is attached to a Node (namespace object)
  366. * 2) An object is copied (all subobjects must be incremented)
  367. *
  368. * Object references are decremented when:
  369. * 1) An object is detached from an Node
  370. *
  371. ******************************************************************************/
  372. acpi_status
  373. acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
  374. {
  375. acpi_status status = AE_OK;
  376. union acpi_generic_state *state_list = NULL;
  377. union acpi_operand_object *next_object = NULL;
  378. union acpi_operand_object *prev_object;
  379. union acpi_generic_state *state;
  380. u32 i;
  381. ACPI_FUNCTION_NAME(ut_update_object_reference);
  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 (AE_OK);
  388. }
  389. /*
  390. * All sub-objects must have their reference count updated
  391. * also. 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. /*
  399. * Update the notify objects for these types (if present)
  400. * Two lists, system and device notify handlers.
  401. */
  402. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  403. prev_object =
  404. object->common_notify.notify_list[i];
  405. while (prev_object) {
  406. next_object =
  407. prev_object->notify.next[i];
  408. acpi_ut_update_ref_count(prev_object,
  409. action);
  410. prev_object = next_object;
  411. }
  412. }
  413. break;
  414. case ACPI_TYPE_PACKAGE:
  415. /*
  416. * We must update all the sub-objects of the package,
  417. * each of whom may have their own sub-objects.
  418. */
  419. for (i = 0; i < object->package.count; i++) {
  420. /*
  421. * Null package elements are legal and can be simply
  422. * ignored.
  423. */
  424. next_object = object->package.elements[i];
  425. if (!next_object) {
  426. continue;
  427. }
  428. switch (next_object->common.type) {
  429. case ACPI_TYPE_INTEGER:
  430. case ACPI_TYPE_STRING:
  431. case ACPI_TYPE_BUFFER:
  432. /*
  433. * For these very simple sub-objects, we can just
  434. * update the reference count here and continue.
  435. * Greatly increases performance of this operation.
  436. */
  437. acpi_ut_update_ref_count(next_object,
  438. action);
  439. break;
  440. default:
  441. /*
  442. * For complex sub-objects, push them onto the stack
  443. * for later processing (this eliminates recursion.)
  444. */
  445. status =
  446. acpi_ut_create_update_state_and_push
  447. (next_object, action, &state_list);
  448. if (ACPI_FAILURE(status)) {
  449. goto error_exit;
  450. }
  451. break;
  452. }
  453. }
  454. next_object = NULL;
  455. break;
  456. case ACPI_TYPE_BUFFER_FIELD:
  457. next_object = object->buffer_field.buffer_obj;
  458. break;
  459. case ACPI_TYPE_LOCAL_BANK_FIELD:
  460. next_object = object->bank_field.bank_obj;
  461. status =
  462. acpi_ut_create_update_state_and_push(object->
  463. bank_field.
  464. region_obj,
  465. action,
  466. &state_list);
  467. if (ACPI_FAILURE(status)) {
  468. goto error_exit;
  469. }
  470. break;
  471. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  472. next_object = object->index_field.index_obj;
  473. status =
  474. acpi_ut_create_update_state_and_push(object->
  475. index_field.
  476. data_obj,
  477. action,
  478. &state_list);
  479. if (ACPI_FAILURE(status)) {
  480. goto error_exit;
  481. }
  482. break;
  483. case ACPI_TYPE_LOCAL_REFERENCE:
  484. /*
  485. * The target of an Index (a package, string, or buffer) or a named
  486. * reference must track changes to the ref count of the index or
  487. * target object.
  488. */
  489. if ((object->reference.class == ACPI_REFCLASS_INDEX) ||
  490. (object->reference.class == ACPI_REFCLASS_NAME)) {
  491. next_object = object->reference.object;
  492. }
  493. break;
  494. case ACPI_TYPE_LOCAL_REGION_FIELD:
  495. case ACPI_TYPE_REGION:
  496. default:
  497. break; /* No subobjects for all other types */
  498. }
  499. /*
  500. * Now we can update the count in the main object. This can only
  501. * happen after we update the sub-objects in case this causes the
  502. * main object to be deleted.
  503. */
  504. acpi_ut_update_ref_count(object, action);
  505. object = NULL;
  506. /* Move on to the next object to be updated */
  507. if (next_object) {
  508. object = next_object;
  509. next_object = NULL;
  510. } else if (state_list) {
  511. state = acpi_ut_pop_generic_state(&state_list);
  512. object = state->update.object;
  513. acpi_ut_delete_generic_state(state);
  514. }
  515. }
  516. return (AE_OK);
  517. error_exit:
  518. ACPI_EXCEPTION((AE_INFO, status,
  519. "Could not update object reference count"));
  520. /* Free any stacked Update State objects */
  521. while (state_list) {
  522. state = acpi_ut_pop_generic_state(&state_list);
  523. acpi_ut_delete_generic_state(state);
  524. }
  525. return (status);
  526. }
  527. /*******************************************************************************
  528. *
  529. * FUNCTION: acpi_ut_add_reference
  530. *
  531. * PARAMETERS: object - Object whose reference count is to be
  532. * incremented
  533. *
  534. * RETURN: None
  535. *
  536. * DESCRIPTION: Add one reference to an ACPI object
  537. *
  538. ******************************************************************************/
  539. void acpi_ut_add_reference(union acpi_operand_object *object)
  540. {
  541. ACPI_FUNCTION_NAME(ut_add_reference);
  542. /* Ensure that we have a valid object */
  543. if (!acpi_ut_valid_internal_object(object)) {
  544. return;
  545. }
  546. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  547. "Obj %p Current Refs=%X [To Be Incremented]\n",
  548. object, object->common.reference_count));
  549. /* Increment the reference count */
  550. (void)acpi_ut_update_object_reference(object, REF_INCREMENT);
  551. return;
  552. }
  553. /*******************************************************************************
  554. *
  555. * FUNCTION: acpi_ut_remove_reference
  556. *
  557. * PARAMETERS: object - Object whose ref count will be decremented
  558. *
  559. * RETURN: None
  560. *
  561. * DESCRIPTION: Decrement the reference count of an ACPI internal object
  562. *
  563. ******************************************************************************/
  564. void acpi_ut_remove_reference(union acpi_operand_object *object)
  565. {
  566. ACPI_FUNCTION_NAME(ut_remove_reference);
  567. /*
  568. * Allow a NULL pointer to be passed in, just ignore it. This saves
  569. * each caller from having to check. Also, ignore NS nodes.
  570. */
  571. if (!object ||
  572. (ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED)) {
  573. return;
  574. }
  575. /* Ensure that we have a valid object */
  576. if (!acpi_ut_valid_internal_object(object)) {
  577. return;
  578. }
  579. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  580. "%s: Obj %p Current Refs=%X [To Be Decremented]\n",
  581. ACPI_GET_FUNCTION_NAME, object,
  582. object->common.reference_count));
  583. /*
  584. * Decrement the reference count, and only actually delete the object
  585. * if the reference count becomes 0. (Must also decrement the ref count
  586. * of all subobjects!)
  587. */
  588. (void)acpi_ut_update_object_reference(object, REF_DECREMENT);
  589. return;
  590. }