mode_emu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * NUMA support for s390
  3. *
  4. * NUMA emulation (aka fake NUMA) distributes the available memory to nodes
  5. * without using real topology information about the physical memory of the
  6. * machine.
  7. *
  8. * It distributes the available CPUs to nodes while respecting the original
  9. * machine topology information. This is done by trying to avoid to separate
  10. * CPUs which reside on the same book or even on the same MC.
  11. *
  12. * Because the current Linux scheduler code requires a stable cpu to node
  13. * mapping, cores are pinned to nodes when the first CPU thread is set online.
  14. *
  15. * Copyright IBM Corp. 2015
  16. */
  17. #define KMSG_COMPONENT "numa_emu"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/memblock.h>
  22. #include <linux/node.h>
  23. #include <linux/memory.h>
  24. #include <linux/slab.h>
  25. #include <asm/smp.h>
  26. #include <asm/topology.h>
  27. #include "numa_mode.h"
  28. #include "toptree.h"
  29. /* Distances between the different system components */
  30. #define DIST_EMPTY 0
  31. #define DIST_CORE 1
  32. #define DIST_MC 2
  33. #define DIST_BOOK 3
  34. #define DIST_MAX 4
  35. /* Node distance reported to common code */
  36. #define EMU_NODE_DIST 10
  37. /* Node ID for free (not yet pinned) cores */
  38. #define NODE_ID_FREE -1
  39. /* Different levels of toptree */
  40. enum toptree_level {CORE, MC, BOOK, NODE, TOPOLOGY};
  41. /* The two toptree IDs */
  42. enum {TOPTREE_ID_PHYS, TOPTREE_ID_NUMA};
  43. /* Number of NUMA nodes */
  44. static int emu_nodes = 1;
  45. /* NUMA stripe size */
  46. static unsigned long emu_size;
  47. /*
  48. * Node to core pinning information updates are protected by
  49. * "sched_domains_mutex".
  50. */
  51. static struct {
  52. s32 to_node_id[CONFIG_NR_CPUS]; /* Pinned core to node mapping */
  53. int total; /* Total number of pinned cores */
  54. int per_node_target; /* Cores per node without extra cores */
  55. int per_node[MAX_NUMNODES]; /* Number of cores pinned to node */
  56. } *emu_cores;
  57. /*
  58. * Pin a core to a node
  59. */
  60. static void pin_core_to_node(int core_id, int node_id)
  61. {
  62. if (emu_cores->to_node_id[core_id] == NODE_ID_FREE) {
  63. emu_cores->per_node[node_id]++;
  64. emu_cores->to_node_id[core_id] = node_id;
  65. emu_cores->total++;
  66. } else {
  67. WARN_ON(emu_cores->to_node_id[core_id] != node_id);
  68. }
  69. }
  70. /*
  71. * Number of pinned cores of a node
  72. */
  73. static int cores_pinned(struct toptree *node)
  74. {
  75. return emu_cores->per_node[node->id];
  76. }
  77. /*
  78. * ID of the node where the core is pinned (or NODE_ID_FREE)
  79. */
  80. static int core_pinned_to_node_id(struct toptree *core)
  81. {
  82. return emu_cores->to_node_id[core->id];
  83. }
  84. /*
  85. * Number of cores in the tree that are not yet pinned
  86. */
  87. static int cores_free(struct toptree *tree)
  88. {
  89. struct toptree *core;
  90. int count = 0;
  91. toptree_for_each(core, tree, CORE) {
  92. if (core_pinned_to_node_id(core) == NODE_ID_FREE)
  93. count++;
  94. }
  95. return count;
  96. }
  97. /*
  98. * Return node of core
  99. */
  100. static struct toptree *core_node(struct toptree *core)
  101. {
  102. return core->parent->parent->parent;
  103. }
  104. /*
  105. * Return book of core
  106. */
  107. static struct toptree *core_book(struct toptree *core)
  108. {
  109. return core->parent->parent;
  110. }
  111. /*
  112. * Return mc of core
  113. */
  114. static struct toptree *core_mc(struct toptree *core)
  115. {
  116. return core->parent;
  117. }
  118. /*
  119. * Distance between two cores
  120. */
  121. static int dist_core_to_core(struct toptree *core1, struct toptree *core2)
  122. {
  123. if (core_book(core1)->id != core_book(core2)->id)
  124. return DIST_BOOK;
  125. if (core_mc(core1)->id != core_mc(core2)->id)
  126. return DIST_MC;
  127. /* Same core or sibling on same MC */
  128. return DIST_CORE;
  129. }
  130. /*
  131. * Distance of a node to a core
  132. */
  133. static int dist_node_to_core(struct toptree *node, struct toptree *core)
  134. {
  135. struct toptree *core_node;
  136. int dist_min = DIST_MAX;
  137. toptree_for_each(core_node, node, CORE)
  138. dist_min = min(dist_min, dist_core_to_core(core_node, core));
  139. return dist_min == DIST_MAX ? DIST_EMPTY : dist_min;
  140. }
  141. /*
  142. * Unify will delete empty nodes, therefore recreate nodes.
  143. */
  144. static void toptree_unify_tree(struct toptree *tree)
  145. {
  146. int nid;
  147. toptree_unify(tree);
  148. for (nid = 0; nid < emu_nodes; nid++)
  149. toptree_get_child(tree, nid);
  150. }
  151. /*
  152. * Find the best/nearest node for a given core and ensure that no node
  153. * gets more than "emu_cores->per_node_target + extra" cores.
  154. */
  155. static struct toptree *node_for_core(struct toptree *numa, struct toptree *core,
  156. int extra)
  157. {
  158. struct toptree *node, *node_best = NULL;
  159. int dist_cur, dist_best, cores_target;
  160. cores_target = emu_cores->per_node_target + extra;
  161. dist_best = DIST_MAX;
  162. node_best = NULL;
  163. toptree_for_each(node, numa, NODE) {
  164. /* Already pinned cores must use their nodes */
  165. if (core_pinned_to_node_id(core) == node->id) {
  166. node_best = node;
  167. break;
  168. }
  169. /* Skip nodes that already have enough cores */
  170. if (cores_pinned(node) >= cores_target)
  171. continue;
  172. dist_cur = dist_node_to_core(node, core);
  173. if (dist_cur < dist_best) {
  174. dist_best = dist_cur;
  175. node_best = node;
  176. }
  177. }
  178. return node_best;
  179. }
  180. /*
  181. * Find the best node for each core with respect to "extra" core count
  182. */
  183. static void toptree_to_numa_single(struct toptree *numa, struct toptree *phys,
  184. int extra)
  185. {
  186. struct toptree *node, *core, *tmp;
  187. toptree_for_each_safe(core, tmp, phys, CORE) {
  188. node = node_for_core(numa, core, extra);
  189. if (!node)
  190. return;
  191. toptree_move(core, node);
  192. pin_core_to_node(core->id, node->id);
  193. }
  194. }
  195. /*
  196. * Move structures of given level to specified NUMA node
  197. */
  198. static void move_level_to_numa_node(struct toptree *node, struct toptree *phys,
  199. enum toptree_level level, bool perfect)
  200. {
  201. int cores_free, cores_target = emu_cores->per_node_target;
  202. struct toptree *cur, *tmp;
  203. toptree_for_each_safe(cur, tmp, phys, level) {
  204. cores_free = cores_target - toptree_count(node, CORE);
  205. if (perfect) {
  206. if (cores_free == toptree_count(cur, CORE))
  207. toptree_move(cur, node);
  208. } else {
  209. if (cores_free >= toptree_count(cur, CORE))
  210. toptree_move(cur, node);
  211. }
  212. }
  213. }
  214. /*
  215. * Move structures of a given level to NUMA nodes. If "perfect" is specified
  216. * move only perfectly fitting structures. Otherwise move also smaller
  217. * than needed structures.
  218. */
  219. static void move_level_to_numa(struct toptree *numa, struct toptree *phys,
  220. enum toptree_level level, bool perfect)
  221. {
  222. struct toptree *node;
  223. toptree_for_each(node, numa, NODE)
  224. move_level_to_numa_node(node, phys, level, perfect);
  225. }
  226. /*
  227. * For the first run try to move the big structures
  228. */
  229. static void toptree_to_numa_first(struct toptree *numa, struct toptree *phys)
  230. {
  231. struct toptree *core;
  232. /* Always try to move perfectly fitting structures first */
  233. move_level_to_numa(numa, phys, BOOK, true);
  234. move_level_to_numa(numa, phys, BOOK, false);
  235. move_level_to_numa(numa, phys, MC, true);
  236. move_level_to_numa(numa, phys, MC, false);
  237. /* Now pin all the moved cores */
  238. toptree_for_each(core, numa, CORE)
  239. pin_core_to_node(core->id, core_node(core)->id);
  240. }
  241. /*
  242. * Allocate new topology and create required nodes
  243. */
  244. static struct toptree *toptree_new(int id, int nodes)
  245. {
  246. struct toptree *tree;
  247. int nid;
  248. tree = toptree_alloc(TOPOLOGY, id);
  249. if (!tree)
  250. goto fail;
  251. for (nid = 0; nid < nodes; nid++) {
  252. if (!toptree_get_child(tree, nid))
  253. goto fail;
  254. }
  255. return tree;
  256. fail:
  257. panic("NUMA emulation could not allocate topology");
  258. }
  259. /*
  260. * Allocate and initialize core to node mapping
  261. */
  262. static void create_core_to_node_map(void)
  263. {
  264. int i;
  265. emu_cores = kzalloc(sizeof(*emu_cores), GFP_KERNEL);
  266. if (emu_cores == NULL)
  267. panic("Could not allocate cores to node memory");
  268. for (i = 0; i < ARRAY_SIZE(emu_cores->to_node_id); i++)
  269. emu_cores->to_node_id[i] = NODE_ID_FREE;
  270. }
  271. /*
  272. * Move cores from physical topology into NUMA target topology
  273. * and try to keep as much of the physical topology as possible.
  274. */
  275. static struct toptree *toptree_to_numa(struct toptree *phys)
  276. {
  277. static int first = 1;
  278. struct toptree *numa;
  279. int cores_total;
  280. cores_total = emu_cores->total + cores_free(phys);
  281. emu_cores->per_node_target = cores_total / emu_nodes;
  282. numa = toptree_new(TOPTREE_ID_NUMA, emu_nodes);
  283. if (first) {
  284. toptree_to_numa_first(numa, phys);
  285. first = 0;
  286. }
  287. toptree_to_numa_single(numa, phys, 0);
  288. toptree_to_numa_single(numa, phys, 1);
  289. toptree_unify_tree(numa);
  290. WARN_ON(cpumask_weight(&phys->mask));
  291. return numa;
  292. }
  293. /*
  294. * Create a toptree out of the physical topology that we got from the hypervisor
  295. */
  296. static struct toptree *toptree_from_topology(void)
  297. {
  298. struct toptree *phys, *node, *book, *mc, *core;
  299. struct cpu_topology_s390 *top;
  300. int cpu;
  301. phys = toptree_new(TOPTREE_ID_PHYS, 1);
  302. for_each_online_cpu(cpu) {
  303. top = &per_cpu(cpu_topology, cpu);
  304. node = toptree_get_child(phys, 0);
  305. book = toptree_get_child(node, top->book_id);
  306. mc = toptree_get_child(book, top->socket_id);
  307. core = toptree_get_child(mc, top->core_id);
  308. if (!book || !mc || !core)
  309. panic("NUMA emulation could not allocate memory");
  310. cpumask_set_cpu(cpu, &core->mask);
  311. toptree_update_mask(mc);
  312. }
  313. return phys;
  314. }
  315. /*
  316. * Add toptree core to topology and create correct CPU masks
  317. */
  318. static void topology_add_core(struct toptree *core)
  319. {
  320. struct cpu_topology_s390 *top;
  321. int cpu;
  322. for_each_cpu(cpu, &core->mask) {
  323. top = &per_cpu(cpu_topology, cpu);
  324. cpumask_copy(&top->thread_mask, &core->mask);
  325. cpumask_copy(&top->core_mask, &core_mc(core)->mask);
  326. cpumask_copy(&top->book_mask, &core_book(core)->mask);
  327. cpumask_set_cpu(cpu, &node_to_cpumask_map[core_node(core)->id]);
  328. top->node_id = core_node(core)->id;
  329. }
  330. }
  331. /*
  332. * Apply toptree to topology and create CPU masks
  333. */
  334. static void toptree_to_topology(struct toptree *numa)
  335. {
  336. struct toptree *core;
  337. int i;
  338. /* Clear all node masks */
  339. for (i = 0; i < MAX_NUMNODES; i++)
  340. cpumask_clear(&node_to_cpumask_map[i]);
  341. /* Rebuild all masks */
  342. toptree_for_each(core, numa, CORE)
  343. topology_add_core(core);
  344. }
  345. /*
  346. * Show the node to core mapping
  347. */
  348. static void print_node_to_core_map(void)
  349. {
  350. int nid, cid;
  351. if (!numa_debug_enabled)
  352. return;
  353. printk(KERN_DEBUG "NUMA node to core mapping\n");
  354. for (nid = 0; nid < emu_nodes; nid++) {
  355. printk(KERN_DEBUG " node %3d: ", nid);
  356. for (cid = 0; cid < ARRAY_SIZE(emu_cores->to_node_id); cid++) {
  357. if (emu_cores->to_node_id[cid] == nid)
  358. printk(KERN_CONT "%d ", cid);
  359. }
  360. printk(KERN_CONT "\n");
  361. }
  362. }
  363. /*
  364. * Transfer physical topology into a NUMA topology and modify CPU masks
  365. * according to the NUMA topology.
  366. *
  367. * Must be called with "sched_domains_mutex" lock held.
  368. */
  369. static void emu_update_cpu_topology(void)
  370. {
  371. struct toptree *phys, *numa;
  372. if (emu_cores == NULL)
  373. create_core_to_node_map();
  374. phys = toptree_from_topology();
  375. numa = toptree_to_numa(phys);
  376. toptree_free(phys);
  377. toptree_to_topology(numa);
  378. toptree_free(numa);
  379. print_node_to_core_map();
  380. }
  381. /*
  382. * If emu_size is not set, use CONFIG_EMU_SIZE. Then round to minimum
  383. * alignment (needed for memory hotplug).
  384. */
  385. static unsigned long emu_setup_size_adjust(unsigned long size)
  386. {
  387. size = size ? : CONFIG_EMU_SIZE;
  388. size = roundup(size, memory_block_size_bytes());
  389. return size;
  390. }
  391. /*
  392. * If we have not enough memory for the specified nodes, reduce the node count.
  393. */
  394. static int emu_setup_nodes_adjust(int nodes)
  395. {
  396. int nodes_max;
  397. nodes_max = memblock.memory.total_size / emu_size;
  398. nodes_max = max(nodes_max, 1);
  399. if (nodes_max >= nodes)
  400. return nodes;
  401. pr_warn("Not enough memory for %d nodes, reducing node count\n", nodes);
  402. return nodes_max;
  403. }
  404. /*
  405. * Early emu setup
  406. */
  407. static void emu_setup(void)
  408. {
  409. emu_size = emu_setup_size_adjust(emu_size);
  410. emu_nodes = emu_setup_nodes_adjust(emu_nodes);
  411. pr_info("Creating %d nodes with memory stripe size %ld MB\n",
  412. emu_nodes, emu_size >> 20);
  413. }
  414. /*
  415. * Return node id for given page number
  416. */
  417. static int emu_pfn_to_nid(unsigned long pfn)
  418. {
  419. return (pfn / (emu_size >> PAGE_SHIFT)) % emu_nodes;
  420. }
  421. /*
  422. * Return stripe size
  423. */
  424. static unsigned long emu_align(void)
  425. {
  426. return emu_size;
  427. }
  428. /*
  429. * Return distance between two nodes
  430. */
  431. static int emu_distance(int node1, int node2)
  432. {
  433. return (node1 != node2) * EMU_NODE_DIST;
  434. }
  435. /*
  436. * Define callbacks for generic s390 NUMA infrastructure
  437. */
  438. const struct numa_mode numa_mode_emu = {
  439. .name = "emu",
  440. .setup = emu_setup,
  441. .update_cpu_topology = emu_update_cpu_topology,
  442. .__pfn_to_nid = emu_pfn_to_nid,
  443. .align = emu_align,
  444. .distance = emu_distance,
  445. };
  446. /*
  447. * Kernel parameter: emu_nodes=<n>
  448. */
  449. static int __init early_parse_emu_nodes(char *p)
  450. {
  451. int count;
  452. if (kstrtoint(p, 0, &count) != 0 || count <= 0)
  453. return 0;
  454. if (count <= 0)
  455. return 0;
  456. emu_nodes = min(count, MAX_NUMNODES);
  457. return 0;
  458. }
  459. early_param("emu_nodes", early_parse_emu_nodes);
  460. /*
  461. * Kernel parameter: emu_size=[<n>[k|M|G|T]]
  462. */
  463. static int __init early_parse_emu_size(char *p)
  464. {
  465. emu_size = memparse(p, NULL);
  466. return 0;
  467. }
  468. early_param("emu_size", early_parse_emu_size);