mode_emu.c 13 KB

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