nsalloc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: nsalloc - Namespace allocation and deletion utilities
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acnamesp.h"
  10. #define _COMPONENT ACPI_NAMESPACE
  11. ACPI_MODULE_NAME("nsalloc")
  12. /*******************************************************************************
  13. *
  14. * FUNCTION: acpi_ns_create_node
  15. *
  16. * PARAMETERS: name - Name of the new node (4 char ACPI name)
  17. *
  18. * RETURN: New namespace node (Null on failure)
  19. *
  20. * DESCRIPTION: Create a namespace node
  21. *
  22. ******************************************************************************/
  23. struct acpi_namespace_node *acpi_ns_create_node(u32 name)
  24. {
  25. struct acpi_namespace_node *node;
  26. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  27. u32 temp;
  28. #endif
  29. ACPI_FUNCTION_TRACE(ns_create_node);
  30. node = acpi_os_acquire_object(acpi_gbl_namespace_cache);
  31. if (!node) {
  32. return_PTR(NULL);
  33. }
  34. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
  35. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  36. temp = acpi_gbl_ns_node_list->total_allocated -
  37. acpi_gbl_ns_node_list->total_freed;
  38. if (temp > acpi_gbl_ns_node_list->max_occupied) {
  39. acpi_gbl_ns_node_list->max_occupied = temp;
  40. }
  41. #endif
  42. node->name.integer = name;
  43. ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
  44. return_PTR(node);
  45. }
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ns_delete_node
  49. *
  50. * PARAMETERS: node - Node to be deleted
  51. *
  52. * RETURN: None
  53. *
  54. * DESCRIPTION: Delete a namespace node. All node deletions must come through
  55. * here. Detaches any attached objects, including any attached
  56. * data. If a handler is associated with attached data, it is
  57. * invoked before the node is deleted.
  58. *
  59. ******************************************************************************/
  60. void acpi_ns_delete_node(struct acpi_namespace_node *node)
  61. {
  62. union acpi_operand_object *obj_desc;
  63. union acpi_operand_object *next_desc;
  64. ACPI_FUNCTION_NAME(ns_delete_node);
  65. /* Detach an object if there is one */
  66. acpi_ns_detach_object(node);
  67. /*
  68. * Delete an attached data object list if present (objects that were
  69. * attached via acpi_attach_data). Note: After any normal object is
  70. * detached above, the only possible remaining object(s) are data
  71. * objects, in a linked list.
  72. */
  73. obj_desc = node->object;
  74. while (obj_desc && (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA)) {
  75. /* Invoke the attached data deletion handler if present */
  76. if (obj_desc->data.handler) {
  77. obj_desc->data.handler(node, obj_desc->data.pointer);
  78. }
  79. next_desc = obj_desc->common.next_object;
  80. acpi_ut_remove_reference(obj_desc);
  81. obj_desc = next_desc;
  82. }
  83. /* Special case for the statically allocated root node */
  84. if (node == acpi_gbl_root_node) {
  85. return;
  86. }
  87. /* Now we can delete the node */
  88. (void)acpi_os_release_object(acpi_gbl_namespace_cache, node);
  89. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  90. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n",
  91. node, acpi_gbl_current_node_count));
  92. }
  93. /*******************************************************************************
  94. *
  95. * FUNCTION: acpi_ns_remove_node
  96. *
  97. * PARAMETERS: node - Node to be removed/deleted
  98. *
  99. * RETURN: None
  100. *
  101. * DESCRIPTION: Remove (unlink) and delete a namespace node
  102. *
  103. ******************************************************************************/
  104. void acpi_ns_remove_node(struct acpi_namespace_node *node)
  105. {
  106. struct acpi_namespace_node *parent_node;
  107. struct acpi_namespace_node *prev_node;
  108. struct acpi_namespace_node *next_node;
  109. ACPI_FUNCTION_TRACE_PTR(ns_remove_node, node);
  110. parent_node = node->parent;
  111. prev_node = NULL;
  112. next_node = parent_node->child;
  113. /* Find the node that is the previous peer in the parent's child list */
  114. while (next_node != node) {
  115. prev_node = next_node;
  116. next_node = next_node->peer;
  117. }
  118. if (prev_node) {
  119. /* Node is not first child, unlink it */
  120. prev_node->peer = node->peer;
  121. } else {
  122. /*
  123. * Node is first child (has no previous peer).
  124. * Link peer list to parent
  125. */
  126. parent_node->child = node->peer;
  127. }
  128. /* Delete the node and any attached objects */
  129. acpi_ns_delete_node(node);
  130. return_VOID;
  131. }
  132. /*******************************************************************************
  133. *
  134. * FUNCTION: acpi_ns_install_node
  135. *
  136. * PARAMETERS: walk_state - Current state of the walk
  137. * parent_node - The parent of the new Node
  138. * node - The new Node to install
  139. * type - ACPI object type of the new Node
  140. *
  141. * RETURN: None
  142. *
  143. * DESCRIPTION: Initialize a new namespace node and install it amongst
  144. * its peers.
  145. *
  146. * Note: Current namespace lookup is linear search. This appears
  147. * to be sufficient as namespace searches consume only a small
  148. * fraction of the execution time of the ACPI subsystem.
  149. *
  150. ******************************************************************************/
  151. void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
  152. struct acpi_namespace_node *node, /* New Child */
  153. acpi_object_type type)
  154. {
  155. acpi_owner_id owner_id = 0;
  156. struct acpi_namespace_node *child_node;
  157. ACPI_FUNCTION_TRACE(ns_install_node);
  158. if (walk_state) {
  159. /*
  160. * Get the owner ID from the Walk state. The owner ID is used to
  161. * track table deletion and deletion of objects created by methods.
  162. */
  163. owner_id = walk_state->owner_id;
  164. if ((walk_state->method_desc) &&
  165. (parent_node != walk_state->method_node)) {
  166. /*
  167. * A method is creating a new node that is not a child of the
  168. * method (it is non-local). Mark the executing method as having
  169. * modified the namespace. This is used for cleanup when the
  170. * method exits.
  171. */
  172. walk_state->method_desc->method.info_flags |=
  173. ACPI_METHOD_MODIFIED_NAMESPACE;
  174. }
  175. }
  176. /* Link the new entry into the parent and existing children */
  177. node->peer = NULL;
  178. node->parent = parent_node;
  179. child_node = parent_node->child;
  180. if (!child_node) {
  181. parent_node->child = node;
  182. } else {
  183. /* Add node to the end of the peer list */
  184. while (child_node->peer) {
  185. child_node = child_node->peer;
  186. }
  187. child_node->peer = node;
  188. }
  189. /* Init the new entry */
  190. node->owner_id = owner_id;
  191. node->type = (u8) type;
  192. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  193. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  194. acpi_ut_get_node_name(node),
  195. acpi_ut_get_type_name(node->type), node, owner_id,
  196. acpi_ut_get_node_name(parent_node),
  197. acpi_ut_get_type_name(parent_node->type),
  198. parent_node));
  199. return_VOID;
  200. }
  201. /*******************************************************************************
  202. *
  203. * FUNCTION: acpi_ns_delete_children
  204. *
  205. * PARAMETERS: parent_node - Delete this objects children
  206. *
  207. * RETURN: None.
  208. *
  209. * DESCRIPTION: Delete all children of the parent object. In other words,
  210. * deletes a "scope".
  211. *
  212. ******************************************************************************/
  213. void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
  214. {
  215. struct acpi_namespace_node *next_node;
  216. struct acpi_namespace_node *node_to_delete;
  217. ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node);
  218. if (!parent_node) {
  219. return_VOID;
  220. }
  221. /* Deallocate all children at this level */
  222. next_node = parent_node->child;
  223. while (next_node) {
  224. /* Grandchildren should have all been deleted already */
  225. if (next_node->child) {
  226. ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
  227. parent_node, next_node));
  228. }
  229. /*
  230. * Delete this child node and move on to the next child in the list.
  231. * No need to unlink the node since we are deleting the entire branch.
  232. */
  233. node_to_delete = next_node;
  234. next_node = next_node->peer;
  235. acpi_ns_delete_node(node_to_delete);
  236. };
  237. /* Clear the parent's child pointer */
  238. parent_node->child = NULL;
  239. return_VOID;
  240. }
  241. /*******************************************************************************
  242. *
  243. * FUNCTION: acpi_ns_delete_namespace_subtree
  244. *
  245. * PARAMETERS: parent_node - Root of the subtree to be deleted
  246. *
  247. * RETURN: None.
  248. *
  249. * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
  250. * stored within the subtree.
  251. *
  252. ******************************************************************************/
  253. void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
  254. {
  255. struct acpi_namespace_node *child_node = NULL;
  256. u32 level = 1;
  257. acpi_status status;
  258. ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree);
  259. if (!parent_node) {
  260. return_VOID;
  261. }
  262. /* Lock namespace for possible update */
  263. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  264. if (ACPI_FAILURE(status)) {
  265. return_VOID;
  266. }
  267. /*
  268. * Traverse the tree of objects until we bubble back up
  269. * to where we started.
  270. */
  271. while (level > 0) {
  272. /* Get the next node in this scope (NULL if none) */
  273. child_node = acpi_ns_get_next_node(parent_node, child_node);
  274. if (child_node) {
  275. /* Found a child node - detach any attached object */
  276. acpi_ns_detach_object(child_node);
  277. /* Check if this node has any children */
  278. if (child_node->child) {
  279. /*
  280. * There is at least one child of this node,
  281. * visit the node
  282. */
  283. level++;
  284. parent_node = child_node;
  285. child_node = NULL;
  286. }
  287. } else {
  288. /*
  289. * No more children of this parent node.
  290. * Move up to the grandparent.
  291. */
  292. level--;
  293. /*
  294. * Now delete all of the children of this parent
  295. * all at the same time.
  296. */
  297. acpi_ns_delete_children(parent_node);
  298. /* New "last child" is this parent node */
  299. child_node = parent_node;
  300. /* Move up the tree to the grandparent */
  301. parent_node = parent_node->parent;
  302. }
  303. }
  304. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  305. return_VOID;
  306. }
  307. /*******************************************************************************
  308. *
  309. * FUNCTION: acpi_ns_delete_namespace_by_owner
  310. *
  311. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  312. *
  313. * RETURN: Status
  314. *
  315. * DESCRIPTION: Delete entries within the namespace that are owned by a
  316. * specific ID. Used to delete entire ACPI tables. All
  317. * reference counts are updated.
  318. *
  319. * MUTEX: Locks namespace during deletion walk.
  320. *
  321. ******************************************************************************/
  322. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  323. {
  324. struct acpi_namespace_node *child_node;
  325. struct acpi_namespace_node *deletion_node;
  326. struct acpi_namespace_node *parent_node;
  327. u32 level;
  328. acpi_status status;
  329. ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
  330. if (owner_id == 0) {
  331. return_VOID;
  332. }
  333. /* Lock namespace for possible update */
  334. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  335. if (ACPI_FAILURE(status)) {
  336. return_VOID;
  337. }
  338. deletion_node = NULL;
  339. parent_node = acpi_gbl_root_node;
  340. child_node = NULL;
  341. level = 1;
  342. /*
  343. * Traverse the tree of nodes until we bubble back up
  344. * to where we started.
  345. */
  346. while (level > 0) {
  347. /*
  348. * Get the next child of this parent node. When child_node is NULL,
  349. * the first child of the parent is returned
  350. */
  351. child_node = acpi_ns_get_next_node(parent_node, child_node);
  352. if (deletion_node) {
  353. acpi_ns_delete_children(deletion_node);
  354. acpi_ns_remove_node(deletion_node);
  355. deletion_node = NULL;
  356. }
  357. if (child_node) {
  358. if (child_node->owner_id == owner_id) {
  359. /* Found a matching child node - detach any attached object */
  360. acpi_ns_detach_object(child_node);
  361. }
  362. /* Check if this node has any children */
  363. if (child_node->child) {
  364. /*
  365. * There is at least one child of this node,
  366. * visit the node
  367. */
  368. level++;
  369. parent_node = child_node;
  370. child_node = NULL;
  371. } else if (child_node->owner_id == owner_id) {
  372. deletion_node = child_node;
  373. }
  374. } else {
  375. /*
  376. * No more children of this parent node.
  377. * Move up to the grandparent.
  378. */
  379. level--;
  380. if (level != 0) {
  381. if (parent_node->owner_id == owner_id) {
  382. deletion_node = parent_node;
  383. }
  384. }
  385. /* New "last child" is this parent node */
  386. child_node = parent_node;
  387. /* Move up the tree to the grandparent */
  388. parent_node = parent_node->parent;
  389. }
  390. }
  391. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  392. return_VOID;
  393. }