nsalloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsalloc - Namespace allocation and deletion utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #define _COMPONENT ACPI_NAMESPACE
  46. ACPI_MODULE_NAME("nsalloc")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ns_create_node
  50. *
  51. * PARAMETERS: Name - Name of the new node (4 char ACPI name)
  52. *
  53. * RETURN: New namespace node (Null on failure)
  54. *
  55. * DESCRIPTION: Create a namespace node
  56. *
  57. ******************************************************************************/
  58. struct acpi_namespace_node *acpi_ns_create_node(u32 name)
  59. {
  60. struct acpi_namespace_node *node;
  61. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  62. u32 temp;
  63. #endif
  64. ACPI_FUNCTION_TRACE(ns_create_node);
  65. node = acpi_os_acquire_object(acpi_gbl_namespace_cache);
  66. if (!node) {
  67. return_PTR(NULL);
  68. }
  69. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
  70. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  71. temp = acpi_gbl_ns_node_list->total_allocated -
  72. acpi_gbl_ns_node_list->total_freed;
  73. if (temp > acpi_gbl_ns_node_list->max_occupied) {
  74. acpi_gbl_ns_node_list->max_occupied = temp;
  75. }
  76. #endif
  77. node->name.integer = name;
  78. ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
  79. return_PTR(node);
  80. }
  81. /*******************************************************************************
  82. *
  83. * FUNCTION: acpi_ns_delete_node
  84. *
  85. * PARAMETERS: Node - Node to be deleted
  86. *
  87. * RETURN: None
  88. *
  89. * DESCRIPTION: Delete a namespace node
  90. *
  91. ******************************************************************************/
  92. void acpi_ns_delete_node(struct acpi_namespace_node *node)
  93. {
  94. struct acpi_namespace_node *parent_node;
  95. struct acpi_namespace_node *prev_node;
  96. struct acpi_namespace_node *next_node;
  97. ACPI_FUNCTION_TRACE_PTR(ns_delete_node, node);
  98. parent_node = acpi_ns_get_parent_node(node);
  99. prev_node = NULL;
  100. next_node = parent_node->child;
  101. /* Find the node that is the previous peer in the parent's child list */
  102. while (next_node != node) {
  103. prev_node = next_node;
  104. next_node = prev_node->peer;
  105. }
  106. if (prev_node) {
  107. /* Node is not first child, unlink it */
  108. prev_node->peer = next_node->peer;
  109. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  110. prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
  111. }
  112. } else {
  113. /* Node is first child (has no previous peer) */
  114. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  115. /* No peers at all */
  116. parent_node->child = NULL;
  117. } else { /* Link peer list to parent */
  118. parent_node->child = next_node->peer;
  119. }
  120. }
  121. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  122. /* Detach an object if there is one, then delete the node */
  123. acpi_ns_detach_object(node);
  124. (void)acpi_os_release_object(acpi_gbl_namespace_cache, node);
  125. return_VOID;
  126. }
  127. /*******************************************************************************
  128. *
  129. * FUNCTION: acpi_ns_install_node
  130. *
  131. * PARAMETERS: walk_state - Current state of the walk
  132. * parent_node - The parent of the new Node
  133. * Node - The new Node to install
  134. * Type - ACPI object type of the new Node
  135. *
  136. * RETURN: None
  137. *
  138. * DESCRIPTION: Initialize a new namespace node and install it amongst
  139. * its peers.
  140. *
  141. * Note: Current namespace lookup is linear search. This appears
  142. * to be sufficient as namespace searches consume only a small
  143. * fraction of the execution time of the ACPI subsystem.
  144. *
  145. ******************************************************************************/
  146. void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
  147. struct acpi_namespace_node *node, /* New Child */
  148. acpi_object_type type)
  149. {
  150. acpi_owner_id owner_id = 0;
  151. struct acpi_namespace_node *child_node;
  152. ACPI_FUNCTION_TRACE(ns_install_node);
  153. /*
  154. * Get the owner ID from the Walk state. The owner ID is used to track
  155. * table deletion and deletion of objects created by methods.
  156. */
  157. if (walk_state) {
  158. owner_id = walk_state->owner_id;
  159. }
  160. /* Link the new entry into the parent and existing children */
  161. child_node = parent_node->child;
  162. if (!child_node) {
  163. parent_node->child = node;
  164. node->flags |= ANOBJ_END_OF_PEER_LIST;
  165. node->peer = parent_node;
  166. } else {
  167. while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
  168. child_node = child_node->peer;
  169. }
  170. child_node->peer = node;
  171. /* Clear end-of-list flag */
  172. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  173. node->flags |= ANOBJ_END_OF_PEER_LIST;
  174. node->peer = parent_node;
  175. }
  176. /* Init the new entry */
  177. node->owner_id = owner_id;
  178. node->type = (u8) type;
  179. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  180. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  181. acpi_ut_get_node_name(node),
  182. acpi_ut_get_type_name(node->type), node, owner_id,
  183. acpi_ut_get_node_name(parent_node),
  184. acpi_ut_get_type_name(parent_node->type),
  185. parent_node));
  186. return_VOID;
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_ns_delete_children
  191. *
  192. * PARAMETERS: parent_node - Delete this objects children
  193. *
  194. * RETURN: None.
  195. *
  196. * DESCRIPTION: Delete all children of the parent object. In other words,
  197. * deletes a "scope".
  198. *
  199. ******************************************************************************/
  200. void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
  201. {
  202. struct acpi_namespace_node *child_node;
  203. struct acpi_namespace_node *next_node;
  204. u8 flags;
  205. ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node);
  206. if (!parent_node) {
  207. return_VOID;
  208. }
  209. /* If no children, all done! */
  210. child_node = parent_node->child;
  211. if (!child_node) {
  212. return_VOID;
  213. }
  214. /* Deallocate all children at this level */
  215. do {
  216. /* Get the things we need */
  217. next_node = child_node->peer;
  218. flags = child_node->flags;
  219. /* Grandchildren should have all been deleted already */
  220. if (child_node->child) {
  221. ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
  222. parent_node, child_node));
  223. }
  224. /* Now we can free this child object */
  225. ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
  226. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  227. "Object %p, Remaining %X\n", child_node,
  228. acpi_gbl_current_node_count));
  229. /* Detach an object if there is one, then free the child node */
  230. acpi_ns_detach_object(child_node);
  231. /* Now we can delete the node */
  232. (void)acpi_os_release_object(acpi_gbl_namespace_cache,
  233. child_node);
  234. /* And move on to the next child in the list */
  235. child_node = next_node;
  236. } while (!(flags & ANOBJ_END_OF_PEER_LIST));
  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_FUNCTION_TRACE(ns_delete_namespace_subtree);
  258. if (!parent_node) {
  259. return_VOID;
  260. }
  261. /*
  262. * Traverse the tree of objects until we bubble back up
  263. * to where we started.
  264. */
  265. while (level > 0) {
  266. /* Get the next node in this scope (NULL if none) */
  267. child_node =
  268. acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  269. child_node);
  270. if (child_node) {
  271. /* Found a child node - detach any attached object */
  272. acpi_ns_detach_object(child_node);
  273. /* Check if this node has any children */
  274. if (acpi_ns_get_next_node
  275. (ACPI_TYPE_ANY, child_node, NULL)) {
  276. /*
  277. * There is at least one child of this node,
  278. * visit the node
  279. */
  280. level++;
  281. parent_node = child_node;
  282. child_node = NULL;
  283. }
  284. } else {
  285. /*
  286. * No more children of this parent node.
  287. * Move up to the grandparent.
  288. */
  289. level--;
  290. /*
  291. * Now delete all of the children of this parent
  292. * all at the same time.
  293. */
  294. acpi_ns_delete_children(parent_node);
  295. /* New "last child" is this parent node */
  296. child_node = parent_node;
  297. /* Move up the tree to the grandparent */
  298. parent_node = acpi_ns_get_parent_node(parent_node);
  299. }
  300. }
  301. return_VOID;
  302. }
  303. /*******************************************************************************
  304. *
  305. * FUNCTION: acpi_ns_delete_namespace_by_owner
  306. *
  307. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  308. *
  309. * RETURN: Status
  310. *
  311. * DESCRIPTION: Delete entries within the namespace that are owned by a
  312. * specific ID. Used to delete entire ACPI tables. All
  313. * reference counts are updated.
  314. *
  315. * MUTEX: Locks namespace during deletion walk.
  316. *
  317. ******************************************************************************/
  318. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  319. {
  320. struct acpi_namespace_node *child_node;
  321. struct acpi_namespace_node *deletion_node;
  322. struct acpi_namespace_node *parent_node;
  323. u32 level;
  324. acpi_status status;
  325. ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
  326. if (owner_id == 0) {
  327. return_VOID;
  328. }
  329. /* Lock namespace for possible update */
  330. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  331. if (ACPI_FAILURE(status)) {
  332. return_VOID;
  333. }
  334. deletion_node = NULL;
  335. parent_node = acpi_gbl_root_node;
  336. child_node = NULL;
  337. level = 1;
  338. /*
  339. * Traverse the tree of nodes until we bubble back up
  340. * to where we started.
  341. */
  342. while (level > 0) {
  343. /*
  344. * Get the next child of this parent node. When child_node is NULL,
  345. * the first child of the parent is returned
  346. */
  347. child_node =
  348. acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
  349. child_node);
  350. if (deletion_node) {
  351. acpi_ns_delete_children(deletion_node);
  352. acpi_ns_delete_node(deletion_node);
  353. deletion_node = NULL;
  354. }
  355. if (child_node) {
  356. if (child_node->owner_id == owner_id) {
  357. /* Found a matching child node - detach any attached object */
  358. acpi_ns_detach_object(child_node);
  359. }
  360. /* Check if this node has any children */
  361. if (acpi_ns_get_next_node
  362. (ACPI_TYPE_ANY, child_node, NULL)) {
  363. /*
  364. * There is at least one child of this node,
  365. * visit the node
  366. */
  367. level++;
  368. parent_node = child_node;
  369. child_node = NULL;
  370. } else if (child_node->owner_id == owner_id) {
  371. deletion_node = child_node;
  372. }
  373. } else {
  374. /*
  375. * No more children of this parent node.
  376. * Move up to the grandparent.
  377. */
  378. level--;
  379. if (level != 0) {
  380. if (parent_node->owner_id == owner_id) {
  381. deletion_node = parent_node;
  382. }
  383. }
  384. /* New "last child" is this parent node */
  385. child_node = parent_node;
  386. /* Move up the tree to the grandparent */
  387. parent_node = acpi_ns_get_parent_node(parent_node);
  388. }
  389. }
  390. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  391. return_VOID;
  392. }