nsalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. 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 = acpi_ns_get_parent_node(node);
  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 = prev_node->peer;
  144. }
  145. if (prev_node) {
  146. /* Node is not first child, unlink it */
  147. prev_node->peer = next_node->peer;
  148. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  149. prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
  150. }
  151. } else {
  152. /* Node is first child (has no previous peer) */
  153. if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
  154. /* No peers at all */
  155. parent_node->child = NULL;
  156. } else { /* Link peer list to parent */
  157. parent_node->child = next_node->peer;
  158. }
  159. }
  160. /* Delete the node and any attached objects */
  161. acpi_ns_delete_node(node);
  162. return_VOID;
  163. }
  164. /*******************************************************************************
  165. *
  166. * FUNCTION: acpi_ns_install_node
  167. *
  168. * PARAMETERS: walk_state - Current state of the walk
  169. * parent_node - The parent of the new Node
  170. * Node - The new Node to install
  171. * Type - ACPI object type of the new Node
  172. *
  173. * RETURN: None
  174. *
  175. * DESCRIPTION: Initialize a new namespace node and install it amongst
  176. * its peers.
  177. *
  178. * Note: Current namespace lookup is linear search. This appears
  179. * to be sufficient as namespace searches consume only a small
  180. * fraction of the execution time of the ACPI subsystem.
  181. *
  182. ******************************************************************************/
  183. void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
  184. struct acpi_namespace_node *node, /* New Child */
  185. acpi_object_type type)
  186. {
  187. acpi_owner_id owner_id = 0;
  188. struct acpi_namespace_node *child_node;
  189. ACPI_FUNCTION_TRACE(ns_install_node);
  190. /*
  191. * Get the owner ID from the Walk state. The owner ID is used to track
  192. * table deletion and deletion of objects created by methods.
  193. */
  194. if (walk_state) {
  195. owner_id = walk_state->owner_id;
  196. }
  197. /* Link the new entry into the parent and existing children */
  198. child_node = parent_node->child;
  199. if (!child_node) {
  200. parent_node->child = node;
  201. node->flags |= ANOBJ_END_OF_PEER_LIST;
  202. node->peer = parent_node;
  203. } else {
  204. while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
  205. child_node = child_node->peer;
  206. }
  207. child_node->peer = node;
  208. /* Clear end-of-list flag */
  209. child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
  210. node->flags |= ANOBJ_END_OF_PEER_LIST;
  211. node->peer = parent_node;
  212. }
  213. /* Init the new entry */
  214. node->owner_id = owner_id;
  215. node->type = (u8) type;
  216. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  217. "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
  218. acpi_ut_get_node_name(node),
  219. acpi_ut_get_type_name(node->type), node, owner_id,
  220. acpi_ut_get_node_name(parent_node),
  221. acpi_ut_get_type_name(parent_node->type),
  222. parent_node));
  223. return_VOID;
  224. }
  225. /*******************************************************************************
  226. *
  227. * FUNCTION: acpi_ns_delete_children
  228. *
  229. * PARAMETERS: parent_node - Delete this objects children
  230. *
  231. * RETURN: None.
  232. *
  233. * DESCRIPTION: Delete all children of the parent object. In other words,
  234. * deletes a "scope".
  235. *
  236. ******************************************************************************/
  237. void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
  238. {
  239. struct acpi_namespace_node *child_node;
  240. struct acpi_namespace_node *next_node;
  241. u8 flags;
  242. ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node);
  243. if (!parent_node) {
  244. return_VOID;
  245. }
  246. /* If no children, all done! */
  247. child_node = parent_node->child;
  248. if (!child_node) {
  249. return_VOID;
  250. }
  251. /* Deallocate all children at this level */
  252. do {
  253. /* Get the things we need */
  254. next_node = child_node->peer;
  255. flags = child_node->flags;
  256. /* Grandchildren should have all been deleted already */
  257. if (child_node->child) {
  258. ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
  259. parent_node, child_node));
  260. }
  261. /*
  262. * Delete this child node and move on to the next child in the list.
  263. * No need to unlink the node since we are deleting the entire branch.
  264. */
  265. acpi_ns_delete_node(child_node);
  266. child_node = next_node;
  267. } while (!(flags & ANOBJ_END_OF_PEER_LIST));
  268. /* Clear the parent's child pointer */
  269. parent_node->child = NULL;
  270. return_VOID;
  271. }
  272. /*******************************************************************************
  273. *
  274. * FUNCTION: acpi_ns_delete_namespace_subtree
  275. *
  276. * PARAMETERS: parent_node - Root of the subtree to be deleted
  277. *
  278. * RETURN: None.
  279. *
  280. * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
  281. * stored within the subtree.
  282. *
  283. ******************************************************************************/
  284. void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
  285. {
  286. struct acpi_namespace_node *child_node = NULL;
  287. u32 level = 1;
  288. ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree);
  289. if (!parent_node) {
  290. return_VOID;
  291. }
  292. /*
  293. * Traverse the tree of objects until we bubble back up
  294. * to where we started.
  295. */
  296. while (level > 0) {
  297. /* Get the next node in this scope (NULL if none) */
  298. child_node = acpi_ns_get_next_node(parent_node, child_node);
  299. if (child_node) {
  300. /* Found a child node - detach any attached object */
  301. acpi_ns_detach_object(child_node);
  302. /* Check if this node has any children */
  303. if (child_node->child) {
  304. /*
  305. * There is at least one child of this node,
  306. * visit the node
  307. */
  308. level++;
  309. parent_node = child_node;
  310. child_node = NULL;
  311. }
  312. } else {
  313. /*
  314. * No more children of this parent node.
  315. * Move up to the grandparent.
  316. */
  317. level--;
  318. /*
  319. * Now delete all of the children of this parent
  320. * all at the same time.
  321. */
  322. acpi_ns_delete_children(parent_node);
  323. /* New "last child" is this parent node */
  324. child_node = parent_node;
  325. /* Move up the tree to the grandparent */
  326. parent_node = acpi_ns_get_parent_node(parent_node);
  327. }
  328. }
  329. return_VOID;
  330. }
  331. /*******************************************************************************
  332. *
  333. * FUNCTION: acpi_ns_delete_namespace_by_owner
  334. *
  335. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  336. *
  337. * RETURN: Status
  338. *
  339. * DESCRIPTION: Delete entries within the namespace that are owned by a
  340. * specific ID. Used to delete entire ACPI tables. All
  341. * reference counts are updated.
  342. *
  343. * MUTEX: Locks namespace during deletion walk.
  344. *
  345. ******************************************************************************/
  346. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  347. {
  348. struct acpi_namespace_node *child_node;
  349. struct acpi_namespace_node *deletion_node;
  350. struct acpi_namespace_node *parent_node;
  351. u32 level;
  352. acpi_status status;
  353. ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
  354. if (owner_id == 0) {
  355. return_VOID;
  356. }
  357. /* Lock namespace for possible update */
  358. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  359. if (ACPI_FAILURE(status)) {
  360. return_VOID;
  361. }
  362. deletion_node = NULL;
  363. parent_node = acpi_gbl_root_node;
  364. child_node = NULL;
  365. level = 1;
  366. /*
  367. * Traverse the tree of nodes until we bubble back up
  368. * to where we started.
  369. */
  370. while (level > 0) {
  371. /*
  372. * Get the next child of this parent node. When child_node is NULL,
  373. * the first child of the parent is returned
  374. */
  375. child_node = acpi_ns_get_next_node(parent_node, child_node);
  376. if (deletion_node) {
  377. acpi_ns_delete_children(deletion_node);
  378. acpi_ns_remove_node(deletion_node);
  379. deletion_node = NULL;
  380. }
  381. if (child_node) {
  382. if (child_node->owner_id == owner_id) {
  383. /* Found a matching child node - detach any attached object */
  384. acpi_ns_detach_object(child_node);
  385. }
  386. /* Check if this node has any children */
  387. if (child_node->child) {
  388. /*
  389. * There is at least one child of this node,
  390. * visit the node
  391. */
  392. level++;
  393. parent_node = child_node;
  394. child_node = NULL;
  395. } else if (child_node->owner_id == owner_id) {
  396. deletion_node = child_node;
  397. }
  398. } else {
  399. /*
  400. * No more children of this parent node.
  401. * Move up to the grandparent.
  402. */
  403. level--;
  404. if (level != 0) {
  405. if (parent_node->owner_id == owner_id) {
  406. deletion_node = parent_node;
  407. }
  408. }
  409. /* New "last child" is this parent node */
  410. child_node = parent_node;
  411. /* Move up the tree to the grandparent */
  412. parent_node = acpi_ns_get_parent_node(parent_node);
  413. }
  414. }
  415. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  416. return_VOID;
  417. }