nsalloc.c 13 KB

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