nsalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsalloc - Namespace allocation and deletion utilities
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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.info_flags |=
  200. ACPI_METHOD_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_status status;
  285. ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree);
  286. if (!parent_node) {
  287. return_VOID;
  288. }
  289. /* Lock namespace for possible update */
  290. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  291. if (ACPI_FAILURE(status)) {
  292. return_VOID;
  293. }
  294. /*
  295. * Traverse the tree of objects until we bubble back up
  296. * to where we started.
  297. */
  298. while (level > 0) {
  299. /* Get the next node in this scope (NULL if none) */
  300. child_node = acpi_ns_get_next_node(parent_node, child_node);
  301. if (child_node) {
  302. /* Found a child node - detach any attached object */
  303. acpi_ns_detach_object(child_node);
  304. /* Check if this node has any children */
  305. if (child_node->child) {
  306. /*
  307. * There is at least one child of this node,
  308. * visit the node
  309. */
  310. level++;
  311. parent_node = child_node;
  312. child_node = NULL;
  313. }
  314. } else {
  315. /*
  316. * No more children of this parent node.
  317. * Move up to the grandparent.
  318. */
  319. level--;
  320. /*
  321. * Now delete all of the children of this parent
  322. * all at the same time.
  323. */
  324. acpi_ns_delete_children(parent_node);
  325. /* New "last child" is this parent node */
  326. child_node = parent_node;
  327. /* Move up the tree to the grandparent */
  328. parent_node = parent_node->parent;
  329. }
  330. }
  331. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  332. return_VOID;
  333. }
  334. /*******************************************************************************
  335. *
  336. * FUNCTION: acpi_ns_delete_namespace_by_owner
  337. *
  338. * PARAMETERS: owner_id - All nodes with this owner will be deleted
  339. *
  340. * RETURN: Status
  341. *
  342. * DESCRIPTION: Delete entries within the namespace that are owned by a
  343. * specific ID. Used to delete entire ACPI tables. All
  344. * reference counts are updated.
  345. *
  346. * MUTEX: Locks namespace during deletion walk.
  347. *
  348. ******************************************************************************/
  349. void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
  350. {
  351. struct acpi_namespace_node *child_node;
  352. struct acpi_namespace_node *deletion_node;
  353. struct acpi_namespace_node *parent_node;
  354. u32 level;
  355. acpi_status status;
  356. ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id);
  357. if (owner_id == 0) {
  358. return_VOID;
  359. }
  360. /* Lock namespace for possible update */
  361. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  362. if (ACPI_FAILURE(status)) {
  363. return_VOID;
  364. }
  365. deletion_node = NULL;
  366. parent_node = acpi_gbl_root_node;
  367. child_node = NULL;
  368. level = 1;
  369. /*
  370. * Traverse the tree of nodes until we bubble back up
  371. * to where we started.
  372. */
  373. while (level > 0) {
  374. /*
  375. * Get the next child of this parent node. When child_node is NULL,
  376. * the first child of the parent is returned
  377. */
  378. child_node = acpi_ns_get_next_node(parent_node, child_node);
  379. if (deletion_node) {
  380. acpi_ns_delete_children(deletion_node);
  381. acpi_ns_remove_node(deletion_node);
  382. deletion_node = NULL;
  383. }
  384. if (child_node) {
  385. if (child_node->owner_id == owner_id) {
  386. /* Found a matching child node - detach any attached object */
  387. acpi_ns_detach_object(child_node);
  388. }
  389. /* Check if this node has any children */
  390. if (child_node->child) {
  391. /*
  392. * There is at least one child of this node,
  393. * visit the node
  394. */
  395. level++;
  396. parent_node = child_node;
  397. child_node = NULL;
  398. } else if (child_node->owner_id == owner_id) {
  399. deletion_node = child_node;
  400. }
  401. } else {
  402. /*
  403. * No more children of this parent node.
  404. * Move up to the grandparent.
  405. */
  406. level--;
  407. if (level != 0) {
  408. if (parent_node->owner_id == owner_id) {
  409. deletion_node = parent_node;
  410. }
  411. }
  412. /* New "last child" is this parent node */
  413. child_node = parent_node;
  414. /* Move up the tree to the grandparent */
  415. parent_node = parent_node->parent;
  416. }
  417. }
  418. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  419. return_VOID;
  420. }