nsalloc.c 14 KB

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