toptree.c 8.8 KB

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