utdelete.c 18 KB

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