toptree.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NUMA support for s390
  4. *
  5. * A tree structure used for machine topology mangling
  6. *
  7. * Copyright IBM Corp. 2015
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/bootmem.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/list.h>
  13. #include <linux/list_sort.h>
  14. #include <linux/slab.h>
  15. #include <asm/numa.h>
  16. #include "toptree.h"
  17. /**
  18. * toptree_alloc - Allocate and initialize a new tree node.
  19. * @level: The node's vertical level; level 0 contains the leaves.
  20. * @id: ID number, explicitly not unique beyond scope of node's siblings
  21. *
  22. * Allocate a new tree node and initialize it.
  23. *
  24. * RETURNS:
  25. * Pointer to the new tree node or NULL on error
  26. */
  27. struct toptree __ref *toptree_alloc(int level, int id)
  28. {
  29. struct toptree *res;
  30. if (slab_is_available())
  31. res = kzalloc(sizeof(*res), GFP_KERNEL);
  32. else
  33. res = memblock_virt_alloc(sizeof(*res), 8);
  34. if (!res)
  35. return res;
  36. INIT_LIST_HEAD(&res->children);
  37. INIT_LIST_HEAD(&res->sibling);
  38. cpumask_clear(&res->mask);
  39. res->level = level;
  40. res->id = id;
  41. return res;
  42. }
  43. /**
  44. * toptree_remove - Remove a tree node from a tree
  45. * @cand: Pointer to the node to remove
  46. *
  47. * The node is detached from its parent node. The parent node's
  48. * masks will be updated to reflect the loss of the child.
  49. */
  50. static void toptree_remove(struct toptree *cand)
  51. {
  52. struct toptree *oldparent;
  53. list_del_init(&cand->sibling);
  54. oldparent = cand->parent;
  55. cand->parent = NULL;
  56. toptree_update_mask(oldparent);
  57. }
  58. /**
  59. * toptree_free - discard a tree node
  60. * @cand: Pointer to the tree node to discard
  61. *
  62. * Checks if @cand is attached to a parent node. Detaches it
  63. * cleanly using toptree_remove. Possible children are freed
  64. * recursively. In the end @cand itself is freed.
  65. */
  66. void __ref toptree_free(struct toptree *cand)
  67. {
  68. struct toptree *child, *tmp;
  69. if (cand->parent)
  70. toptree_remove(cand);
  71. toptree_for_each_child_safe(child, tmp, cand)
  72. toptree_free(child);
  73. if (slab_is_available())
  74. kfree(cand);
  75. else
  76. memblock_free_early((unsigned long)cand, sizeof(*cand));
  77. }
  78. /**
  79. * toptree_update_mask - Update node bitmasks
  80. * @cand: Pointer to a tree node
  81. *
  82. * The node's cpumask will be updated by combining all children's
  83. * masks. Then toptree_update_mask is called recursively for the
  84. * parent if applicable.
  85. *
  86. * NOTE:
  87. * This must not be called on leaves. If called on a leaf, its
  88. * CPU mask is cleared and lost.
  89. */
  90. void toptree_update_mask(struct toptree *cand)
  91. {
  92. struct toptree *child;
  93. cpumask_clear(&cand->mask);
  94. list_for_each_entry(child, &cand->children, sibling)
  95. cpumask_or(&cand->mask, &cand->mask, &child->mask);
  96. if (cand->parent)
  97. toptree_update_mask(cand->parent);
  98. }
  99. /**
  100. * toptree_insert - Insert a tree node into tree
  101. * @cand: Pointer to the node to insert
  102. * @target: Pointer to the node to which @cand will added as a child
  103. *
  104. * Insert a tree node into a tree. Masks will be updated automatically.
  105. *
  106. * RETURNS:
  107. * 0 on success, -1 if NULL is passed as argument or the node levels
  108. * don't fit.
  109. */
  110. static int toptree_insert(struct toptree *cand, struct toptree *target)
  111. {
  112. if (!cand || !target)
  113. return -1;
  114. if (target->level != (cand->level + 1))
  115. return -1;
  116. list_add_tail(&cand->sibling, &target->children);
  117. cand->parent = target;
  118. toptree_update_mask(target);
  119. return 0;
  120. }
  121. /**
  122. * toptree_move_children - Move all child nodes of a node to a new place
  123. * @cand: Pointer to the node whose children are to be moved
  124. * @target: Pointer to the node to which @cand's children will be attached
  125. *
  126. * Take all child nodes of @cand and move them using toptree_move.
  127. */
  128. static void toptree_move_children(struct toptree *cand, struct toptree *target)
  129. {
  130. struct toptree *child, *tmp;
  131. toptree_for_each_child_safe(child, tmp, cand)
  132. toptree_move(child, target);
  133. }
  134. /**
  135. * toptree_unify - Merge children with same ID
  136. * @cand: Pointer to node whose direct children should be made unique
  137. *
  138. * When mangling the tree it is possible that a node has two or more children
  139. * which have the same ID. This routine merges these children into one and
  140. * moves all children of the merged nodes into the unified node.
  141. */
  142. void toptree_unify(struct toptree *cand)
  143. {
  144. struct toptree *child, *tmp, *cand_copy;
  145. /* Threads cannot be split, cores are not split */
  146. if (cand->level < 2)
  147. return;
  148. cand_copy = toptree_alloc(cand->level, 0);
  149. toptree_for_each_child_safe(child, tmp, cand) {
  150. struct toptree *tmpchild;
  151. if (!cpumask_empty(&child->mask)) {
  152. tmpchild = toptree_get_child(cand_copy, child->id);
  153. toptree_move_children(child, tmpchild);
  154. }
  155. toptree_free(child);
  156. }
  157. toptree_move_children(cand_copy, cand);
  158. toptree_free(cand_copy);
  159. toptree_for_each_child(child, cand)
  160. toptree_unify(child);
  161. }
  162. /**
  163. * toptree_move - Move a node to another context
  164. * @cand: Pointer to the node to move
  165. * @target: Pointer to the node where @cand should go
  166. *
  167. * In the easiest case @cand is exactly on the level below @target
  168. * and will be immediately moved to the target.
  169. *
  170. * If @target's level is not the direct parent level of @cand,
  171. * nodes for the missing levels are created and put between
  172. * @cand and @target. The "stacking" nodes' IDs are taken from
  173. * @cand's parents.
  174. *
  175. * After this it is likely to have redundant nodes in the tree
  176. * which are addressed by means of toptree_unify.
  177. */
  178. void toptree_move(struct toptree *cand, struct toptree *target)
  179. {
  180. struct toptree *stack_target, *real_insert_point, *ptr, *tmp;
  181. if (cand->level + 1 == target->level) {
  182. toptree_remove(cand);
  183. toptree_insert(cand, target);
  184. return;
  185. }
  186. real_insert_point = NULL;
  187. ptr = cand;
  188. stack_target = NULL;
  189. do {
  190. tmp = stack_target;
  191. stack_target = toptree_alloc(ptr->level + 1,
  192. ptr->parent->id);
  193. toptree_insert(tmp, stack_target);
  194. if (!real_insert_point)
  195. real_insert_point = stack_target;
  196. ptr = ptr->parent;
  197. } while (stack_target->level < (target->level - 1));
  198. toptree_remove(cand);
  199. toptree_insert(cand, real_insert_point);
  200. toptree_insert(stack_target, target);
  201. }
  202. /**
  203. * toptree_get_child - Access a tree node's child by its ID
  204. * @cand: Pointer to tree node whose child is to access
  205. * @id: The desired child's ID
  206. *
  207. * @cand's children are searched for a child with matching ID.
  208. * If no match can be found, a new child with the desired ID
  209. * is created and returned.
  210. */
  211. struct toptree *toptree_get_child(struct toptree *cand, int id)
  212. {
  213. struct toptree *child;
  214. toptree_for_each_child(child, cand)
  215. if (child->id == id)
  216. return child;
  217. child = toptree_alloc(cand->level-1, id);
  218. toptree_insert(child, cand);
  219. return child;
  220. }
  221. /**
  222. * toptree_first - Find the first descendant on specified level
  223. * @context: Pointer to tree node whose descendants are to be used
  224. * @level: The level of interest
  225. *
  226. * RETURNS:
  227. * @context's first descendant on the specified level, or NULL
  228. * if there is no matching descendant
  229. */
  230. struct toptree *toptree_first(struct toptree *context, int level)
  231. {
  232. struct toptree *child, *tmp;
  233. if (context->level == level)
  234. return context;
  235. if (!list_empty(&context->children)) {
  236. list_for_each_entry(child, &context->children, sibling) {
  237. tmp = toptree_first(child, level);
  238. if (tmp)
  239. return tmp;
  240. }
  241. }
  242. return NULL;
  243. }
  244. /**
  245. * toptree_next_sibling - Return next sibling
  246. * @cur: Pointer to a tree node
  247. *
  248. * RETURNS:
  249. * If @cur has a parent and is not the last in the parent's children list,
  250. * the next sibling is returned. Or NULL when there are no siblings left.
  251. */
  252. static struct toptree *toptree_next_sibling(struct toptree *cur)
  253. {
  254. if (cur->parent == NULL)
  255. return NULL;
  256. if (cur == list_last_entry(&cur->parent->children,
  257. struct toptree, sibling))
  258. return NULL;
  259. return (struct toptree *) list_next_entry(cur, sibling);
  260. }
  261. /**
  262. * toptree_next - Tree traversal function
  263. * @cur: Pointer to current element
  264. * @context: Pointer to the root node of the tree or subtree to
  265. * be traversed.
  266. * @level: The level of interest.
  267. *
  268. * RETURNS:
  269. * Pointer to the next node on level @level
  270. * or NULL when there is no next node.
  271. */
  272. struct toptree *toptree_next(struct toptree *cur, struct toptree *context,
  273. int level)
  274. {
  275. struct toptree *cur_context, *tmp;
  276. if (!cur)
  277. return NULL;
  278. if (context->level == level)
  279. return NULL;
  280. tmp = toptree_next_sibling(cur);
  281. if (tmp != NULL)
  282. return tmp;
  283. cur_context = cur;
  284. while (cur_context->level < context->level - 1) {
  285. /* Step up */
  286. cur_context = cur_context->parent;
  287. /* Step aside */
  288. tmp = toptree_next_sibling(cur_context);
  289. if (tmp != NULL) {
  290. /* Step down */
  291. tmp = toptree_first(tmp, level);
  292. if (tmp != NULL)
  293. return tmp;
  294. }
  295. }
  296. return NULL;
  297. }
  298. /**
  299. * toptree_count - Count descendants on specified level
  300. * @context: Pointer to node whose descendants are to be considered
  301. * @level: Only descendants on the specified level will be counted
  302. *
  303. * RETURNS:
  304. * Number of descendants on the specified level
  305. */
  306. int toptree_count(struct toptree *context, int level)
  307. {
  308. struct toptree *cur;
  309. int cnt = 0;
  310. toptree_for_each(cur, context, level)
  311. cnt++;
  312. return cnt;
  313. }