utdelete.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. /*lint -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. break;
  200. case ACPI_TYPE_BUFFER_FIELD:
  201. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  202. "***** Buffer Field %p\n", object));
  203. second_desc = acpi_ns_get_secondary_object(object);
  204. if (second_desc) {
  205. acpi_ut_delete_object_desc(second_desc);
  206. }
  207. break;
  208. case ACPI_TYPE_LOCAL_BANK_FIELD:
  209. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  210. "***** Bank Field %p\n", object));
  211. second_desc = acpi_ns_get_secondary_object(object);
  212. if (second_desc) {
  213. acpi_ut_delete_object_desc(second_desc);
  214. }
  215. break;
  216. default:
  217. break;
  218. }
  219. /* Free any allocated memory (pointer within the object) found above */
  220. if (obj_pointer) {
  221. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  222. "Deleting Object Subptr %p\n", obj_pointer));
  223. ACPI_FREE(obj_pointer);
  224. }
  225. /* Now the object can be safely deleted */
  226. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  227. "%s: Deleting Object %p [%s]\n",
  228. ACPI_GET_FUNCTION_NAME, object,
  229. acpi_ut_get_object_type_name(object)));
  230. acpi_ut_delete_object_desc(object);
  231. return_VOID;
  232. }
  233. /*******************************************************************************
  234. *
  235. * FUNCTION: acpi_ut_delete_internal_object_list
  236. *
  237. * PARAMETERS: obj_list - Pointer to the list to be deleted
  238. *
  239. * RETURN: None
  240. *
  241. * DESCRIPTION: This function deletes an internal object list, including both
  242. * simple objects and package objects
  243. *
  244. ******************************************************************************/
  245. void acpi_ut_delete_internal_object_list(union acpi_operand_object **obj_list)
  246. {
  247. union acpi_operand_object **internal_obj;
  248. ACPI_FUNCTION_ENTRY();
  249. /* Walk the null-terminated internal list */
  250. for (internal_obj = obj_list; *internal_obj; internal_obj++) {
  251. acpi_ut_remove_reference(*internal_obj);
  252. }
  253. /* Free the combined parameter pointer list and object array */
  254. ACPI_FREE(obj_list);
  255. return;
  256. }
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_ut_update_ref_count
  260. *
  261. * PARAMETERS: object - Object whose ref count is to be updated
  262. * action - What to do (REF_INCREMENT or REF_DECREMENT)
  263. *
  264. * RETURN: None. Sets new reference count within the object
  265. *
  266. * DESCRIPTION: Modify the reference count for an internal acpi object
  267. *
  268. ******************************************************************************/
  269. static void
  270. acpi_ut_update_ref_count(union acpi_operand_object *object, u32 action)
  271. {
  272. u16 original_count;
  273. u16 new_count = 0;
  274. acpi_cpu_flags lock_flags;
  275. ACPI_FUNCTION_NAME(ut_update_ref_count);
  276. if (!object) {
  277. return;
  278. }
  279. /*
  280. * Always get the reference count lock. Note: Interpreter and/or
  281. * Namespace is not always locked when this function is called.
  282. */
  283. lock_flags = acpi_os_acquire_lock(acpi_gbl_reference_count_lock);
  284. original_count = object->common.reference_count;
  285. /* Perform the reference count action (increment, decrement) */
  286. switch (action) {
  287. case REF_INCREMENT:
  288. new_count = original_count + 1;
  289. object->common.reference_count = new_count;
  290. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  291. /* The current reference count should never be zero here */
  292. if (!original_count) {
  293. ACPI_WARNING((AE_INFO,
  294. "Obj %p, Reference Count was zero before increment\n",
  295. object));
  296. }
  297. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  298. "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
  299. object, object->common.type,
  300. acpi_ut_get_object_type_name(object),
  301. new_count));
  302. break;
  303. case REF_DECREMENT:
  304. /* The current reference count must be non-zero */
  305. if (original_count) {
  306. new_count = original_count - 1;
  307. object->common.reference_count = new_count;
  308. }
  309. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  310. if (!original_count) {
  311. ACPI_WARNING((AE_INFO,
  312. "Obj %p, Reference Count is already zero, cannot decrement\n",
  313. object));
  314. }
  315. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  316. "%s: Obj %p Type %.2X Refs %.2X [Decremented]\n",
  317. ACPI_GET_FUNCTION_NAME, object,
  318. object->common.type, new_count));
  319. /* Actually delete the object on a reference count of zero */
  320. if (new_count == 0) {
  321. acpi_ut_delete_internal_obj(object);
  322. }
  323. break;
  324. default:
  325. acpi_os_release_lock(acpi_gbl_reference_count_lock, lock_flags);
  326. ACPI_ERROR((AE_INFO, "Unknown Reference Count action (0x%X)",
  327. action));
  328. return;
  329. }
  330. /*
  331. * Sanity check the reference count, for debug purposes only.
  332. * (A deleted object will have a huge reference count)
  333. */
  334. if (new_count > ACPI_MAX_REFERENCE_COUNT) {
  335. ACPI_WARNING((AE_INFO,
  336. "Large Reference Count (0x%X) in object %p, Type=0x%.2X",
  337. new_count, object, object->common.type));
  338. }
  339. }
  340. /*******************************************************************************
  341. *
  342. * FUNCTION: acpi_ut_update_object_reference
  343. *
  344. * PARAMETERS: object - Increment ref count for this object
  345. * and all sub-objects
  346. * action - Either REF_INCREMENT or REF_DECREMENT
  347. *
  348. * RETURN: Status
  349. *
  350. * DESCRIPTION: Increment the object reference count
  351. *
  352. * Object references are incremented when:
  353. * 1) An object is attached to a Node (namespace object)
  354. * 2) An object is copied (all subobjects must be incremented)
  355. *
  356. * Object references are decremented when:
  357. * 1) An object is detached from an Node
  358. *
  359. ******************************************************************************/
  360. acpi_status
  361. acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
  362. {
  363. acpi_status status = AE_OK;
  364. union acpi_generic_state *state_list = NULL;
  365. union acpi_operand_object *next_object = NULL;
  366. union acpi_operand_object *prev_object;
  367. union acpi_generic_state *state;
  368. u32 i;
  369. ACPI_FUNCTION_NAME(ut_update_object_reference);
  370. while (object) {
  371. /* Make sure that this isn't a namespace handle */
  372. if (ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED) {
  373. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  374. "Object %p is NS handle\n", object));
  375. return (AE_OK);
  376. }
  377. /*
  378. * All sub-objects must have their reference count incremented
  379. * also. Different object types have different subobjects.
  380. */
  381. switch (object->common.type) {
  382. case ACPI_TYPE_DEVICE:
  383. case ACPI_TYPE_PROCESSOR:
  384. case ACPI_TYPE_POWER:
  385. case ACPI_TYPE_THERMAL:
  386. /*
  387. * Update the notify objects for these types (if present)
  388. * Two lists, system and device notify handlers.
  389. */
  390. for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
  391. prev_object =
  392. object->common_notify.notify_list[i];
  393. while (prev_object) {
  394. next_object =
  395. prev_object->notify.next[i];
  396. acpi_ut_update_ref_count(prev_object,
  397. action);
  398. prev_object = next_object;
  399. }
  400. }
  401. break;
  402. case ACPI_TYPE_PACKAGE:
  403. /*
  404. * We must update all the sub-objects of the package,
  405. * each of whom may have their own sub-objects.
  406. */
  407. for (i = 0; i < object->package.count; i++) {
  408. /*
  409. * Null package elements are legal and can be simply
  410. * ignored.
  411. */
  412. next_object = object->package.elements[i];
  413. if (!next_object) {
  414. continue;
  415. }
  416. switch (next_object->common.type) {
  417. case ACPI_TYPE_INTEGER:
  418. case ACPI_TYPE_STRING:
  419. case ACPI_TYPE_BUFFER:
  420. /*
  421. * For these very simple sub-objects, we can just
  422. * update the reference count here and continue.
  423. * Greatly increases performance of this operation.
  424. */
  425. acpi_ut_update_ref_count(next_object,
  426. action);
  427. break;
  428. default:
  429. /*
  430. * For complex sub-objects, push them onto the stack
  431. * for later processing (this eliminates recursion.)
  432. */
  433. status =
  434. acpi_ut_create_update_state_and_push
  435. (next_object, action, &state_list);
  436. if (ACPI_FAILURE(status)) {
  437. goto error_exit;
  438. }
  439. break;
  440. }
  441. }
  442. next_object = NULL;
  443. break;
  444. case ACPI_TYPE_BUFFER_FIELD:
  445. next_object = object->buffer_field.buffer_obj;
  446. break;
  447. case ACPI_TYPE_LOCAL_REGION_FIELD:
  448. next_object = object->field.region_obj;
  449. break;
  450. case ACPI_TYPE_LOCAL_BANK_FIELD:
  451. next_object = object->bank_field.bank_obj;
  452. status =
  453. acpi_ut_create_update_state_and_push(object->
  454. bank_field.
  455. region_obj,
  456. action,
  457. &state_list);
  458. if (ACPI_FAILURE(status)) {
  459. goto error_exit;
  460. }
  461. break;
  462. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  463. next_object = object->index_field.index_obj;
  464. status =
  465. acpi_ut_create_update_state_and_push(object->
  466. index_field.
  467. data_obj,
  468. action,
  469. &state_list);
  470. if (ACPI_FAILURE(status)) {
  471. goto error_exit;
  472. }
  473. break;
  474. case ACPI_TYPE_LOCAL_REFERENCE:
  475. /*
  476. * The target of an Index (a package, string, or buffer) or a named
  477. * reference must track changes to the ref count of the index or
  478. * target object.
  479. */
  480. if ((object->reference.class == ACPI_REFCLASS_INDEX) ||
  481. (object->reference.class == ACPI_REFCLASS_NAME)) {
  482. next_object = object->reference.object;
  483. }
  484. break;
  485. case ACPI_TYPE_REGION:
  486. default:
  487. break; /* No subobjects for all other types */
  488. }
  489. /*
  490. * Now we can update the count in the main object. This can only
  491. * happen after we update the sub-objects in case this causes the
  492. * main object to be deleted.
  493. */
  494. acpi_ut_update_ref_count(object, action);
  495. object = NULL;
  496. /* Move on to the next object to be updated */
  497. if (next_object) {
  498. object = next_object;
  499. next_object = NULL;
  500. } else if (state_list) {
  501. state = acpi_ut_pop_generic_state(&state_list);
  502. object = state->update.object;
  503. acpi_ut_delete_generic_state(state);
  504. }
  505. }
  506. return (AE_OK);
  507. error_exit:
  508. ACPI_EXCEPTION((AE_INFO, status,
  509. "Could not update object reference count"));
  510. /* Free any stacked Update State objects */
  511. while (state_list) {
  512. state = acpi_ut_pop_generic_state(&state_list);
  513. acpi_ut_delete_generic_state(state);
  514. }
  515. return (status);
  516. }
  517. /*******************************************************************************
  518. *
  519. * FUNCTION: acpi_ut_add_reference
  520. *
  521. * PARAMETERS: object - Object whose reference count is to be
  522. * incremented
  523. *
  524. * RETURN: None
  525. *
  526. * DESCRIPTION: Add one reference to an ACPI object
  527. *
  528. ******************************************************************************/
  529. void acpi_ut_add_reference(union acpi_operand_object *object)
  530. {
  531. ACPI_FUNCTION_NAME(ut_add_reference);
  532. /* Ensure that we have a valid object */
  533. if (!acpi_ut_valid_internal_object(object)) {
  534. return;
  535. }
  536. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  537. "Obj %p Current Refs=%X [To Be Incremented]\n",
  538. object, object->common.reference_count));
  539. /* Increment the reference count */
  540. (void)acpi_ut_update_object_reference(object, REF_INCREMENT);
  541. return;
  542. }
  543. /*******************************************************************************
  544. *
  545. * FUNCTION: acpi_ut_remove_reference
  546. *
  547. * PARAMETERS: object - Object whose ref count will be decremented
  548. *
  549. * RETURN: None
  550. *
  551. * DESCRIPTION: Decrement the reference count of an ACPI internal object
  552. *
  553. ******************************************************************************/
  554. void acpi_ut_remove_reference(union acpi_operand_object *object)
  555. {
  556. ACPI_FUNCTION_NAME(ut_remove_reference);
  557. /*
  558. * Allow a NULL pointer to be passed in, just ignore it. This saves
  559. * each caller from having to check. Also, ignore NS nodes.
  560. */
  561. if (!object ||
  562. (ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED)) {
  563. return;
  564. }
  565. /* Ensure that we have a valid object */
  566. if (!acpi_ut_valid_internal_object(object)) {
  567. return;
  568. }
  569. ACPI_DEBUG_PRINT_RAW((ACPI_DB_ALLOCATIONS,
  570. "%s: Obj %p Current Refs=%X [To Be Decremented]\n",
  571. ACPI_GET_FUNCTION_NAME, object,
  572. object->common.reference_count));
  573. /*
  574. * Decrement the reference count, and only actually delete the object
  575. * if the reference count becomes 0. (Must also decrement the ref count
  576. * of all subobjects!)
  577. */
  578. (void)acpi_ut_update_object_reference(object, REF_DECREMENT);
  579. return;
  580. }