nsalloc.c 12 KB

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