nsalloc.c 14 KB

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