mode_emu.c 13 KB

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