mode_emu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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_DRAWER 4
  35. #define DIST_MAX 5
  36. /* Node distance reported to common code */
  37. #define EMU_NODE_DIST 10
  38. /* Node ID for free (not yet pinned) cores */
  39. #define NODE_ID_FREE -1
  40. /* Different levels of toptree */
  41. enum toptree_level {CORE, MC, BOOK, DRAWER, NODE, TOPOLOGY};
  42. /* The two toptree IDs */
  43. enum {TOPTREE_ID_PHYS, TOPTREE_ID_NUMA};
  44. /* Number of NUMA nodes */
  45. static int emu_nodes = 1;
  46. /* NUMA stripe size */
  47. static unsigned long emu_size;
  48. /*
  49. * Node to core pinning information updates are protected by
  50. * "sched_domains_mutex".
  51. */
  52. static struct {
  53. s32 to_node_id[CONFIG_NR_CPUS]; /* Pinned core to node mapping */
  54. int total; /* Total number of pinned cores */
  55. int per_node_target; /* Cores per node without extra cores */
  56. int per_node[MAX_NUMNODES]; /* Number of cores pinned to node */
  57. } *emu_cores;
  58. /*
  59. * Pin a core to a node
  60. */
  61. static void pin_core_to_node(int core_id, int node_id)
  62. {
  63. if (emu_cores->to_node_id[core_id] == NODE_ID_FREE) {
  64. emu_cores->per_node[node_id]++;
  65. emu_cores->to_node_id[core_id] = node_id;
  66. emu_cores->total++;
  67. } else {
  68. WARN_ON(emu_cores->to_node_id[core_id] != node_id);
  69. }
  70. }
  71. /*
  72. * Number of pinned cores of a node
  73. */
  74. static int cores_pinned(struct toptree *node)
  75. {
  76. return emu_cores->per_node[node->id];
  77. }
  78. /*
  79. * ID of the node where the core is pinned (or NODE_ID_FREE)
  80. */
  81. static int core_pinned_to_node_id(struct toptree *core)
  82. {
  83. return emu_cores->to_node_id[core->id];
  84. }
  85. /*
  86. * Number of cores in the tree that are not yet pinned
  87. */
  88. static int cores_free(struct toptree *tree)
  89. {
  90. struct toptree *core;
  91. int count = 0;
  92. toptree_for_each(core, tree, CORE) {
  93. if (core_pinned_to_node_id(core) == NODE_ID_FREE)
  94. count++;
  95. }
  96. return count;
  97. }
  98. /*
  99. * Return node of core
  100. */
  101. static struct toptree *core_node(struct toptree *core)
  102. {
  103. return core->parent->parent->parent->parent;
  104. }
  105. /*
  106. * Return drawer of core
  107. */
  108. static struct toptree *core_drawer(struct toptree *core)
  109. {
  110. return core->parent->parent->parent;
  111. }
  112. /*
  113. * Return book of core
  114. */
  115. static struct toptree *core_book(struct toptree *core)
  116. {
  117. return core->parent->parent;
  118. }
  119. /*
  120. * Return mc of core
  121. */
  122. static struct toptree *core_mc(struct toptree *core)
  123. {
  124. return core->parent;
  125. }
  126. /*
  127. * Distance between two cores
  128. */
  129. static int dist_core_to_core(struct toptree *core1, struct toptree *core2)
  130. {
  131. if (core_drawer(core1)->id != core_drawer(core2)->id)
  132. return DIST_DRAWER;
  133. if (core_book(core1)->id != core_book(core2)->id)
  134. return DIST_BOOK;
  135. if (core_mc(core1)->id != core_mc(core2)->id)
  136. return DIST_MC;
  137. /* Same core or sibling on same MC */
  138. return DIST_CORE;
  139. }
  140. /*
  141. * Distance of a node to a core
  142. */
  143. static int dist_node_to_core(struct toptree *node, struct toptree *core)
  144. {
  145. struct toptree *core_node;
  146. int dist_min = DIST_MAX;
  147. toptree_for_each(core_node, node, CORE)
  148. dist_min = min(dist_min, dist_core_to_core(core_node, core));
  149. return dist_min == DIST_MAX ? DIST_EMPTY : dist_min;
  150. }
  151. /*
  152. * Unify will delete empty nodes, therefore recreate nodes.
  153. */
  154. static void toptree_unify_tree(struct toptree *tree)
  155. {
  156. int nid;
  157. toptree_unify(tree);
  158. for (nid = 0; nid < emu_nodes; nid++)
  159. toptree_get_child(tree, nid);
  160. }
  161. /*
  162. * Find the best/nearest node for a given core and ensure that no node
  163. * gets more than "emu_cores->per_node_target + extra" cores.
  164. */
  165. static struct toptree *node_for_core(struct toptree *numa, struct toptree *core,
  166. int extra)
  167. {
  168. struct toptree *node, *node_best = NULL;
  169. int dist_cur, dist_best, cores_target;
  170. cores_target = emu_cores->per_node_target + extra;
  171. dist_best = DIST_MAX;
  172. node_best = NULL;
  173. toptree_for_each(node, numa, NODE) {
  174. /* Already pinned cores must use their nodes */
  175. if (core_pinned_to_node_id(core) == node->id) {
  176. node_best = node;
  177. break;
  178. }
  179. /* Skip nodes that already have enough cores */
  180. if (cores_pinned(node) >= cores_target)
  181. continue;
  182. dist_cur = dist_node_to_core(node, core);
  183. if (dist_cur < dist_best) {
  184. dist_best = dist_cur;
  185. node_best = node;
  186. }
  187. }
  188. return node_best;
  189. }
  190. /*
  191. * Find the best node for each core with respect to "extra" core count
  192. */
  193. static void toptree_to_numa_single(struct toptree *numa, struct toptree *phys,
  194. int extra)
  195. {
  196. struct toptree *node, *core, *tmp;
  197. toptree_for_each_safe(core, tmp, phys, CORE) {
  198. node = node_for_core(numa, core, extra);
  199. if (!node)
  200. return;
  201. toptree_move(core, node);
  202. pin_core_to_node(core->id, node->id);
  203. }
  204. }
  205. /*
  206. * Move structures of given level to specified NUMA node
  207. */
  208. static void move_level_to_numa_node(struct toptree *node, struct toptree *phys,
  209. enum toptree_level level, bool perfect)
  210. {
  211. int cores_free, cores_target = emu_cores->per_node_target;
  212. struct toptree *cur, *tmp;
  213. toptree_for_each_safe(cur, tmp, phys, level) {
  214. cores_free = cores_target - toptree_count(node, CORE);
  215. if (perfect) {
  216. if (cores_free == toptree_count(cur, CORE))
  217. toptree_move(cur, node);
  218. } else {
  219. if (cores_free >= toptree_count(cur, CORE))
  220. toptree_move(cur, node);
  221. }
  222. }
  223. }
  224. /*
  225. * Move structures of a given level to NUMA nodes. If "perfect" is specified
  226. * move only perfectly fitting structures. Otherwise move also smaller
  227. * than needed structures.
  228. */
  229. static void move_level_to_numa(struct toptree *numa, struct toptree *phys,
  230. enum toptree_level level, bool perfect)
  231. {
  232. struct toptree *node;
  233. toptree_for_each(node, numa, NODE)
  234. move_level_to_numa_node(node, phys, level, perfect);
  235. }
  236. /*
  237. * For the first run try to move the big structures
  238. */
  239. static void toptree_to_numa_first(struct toptree *numa, struct toptree *phys)
  240. {
  241. struct toptree *core;
  242. /* Always try to move perfectly fitting structures first */
  243. move_level_to_numa(numa, phys, DRAWER, true);
  244. move_level_to_numa(numa, phys, DRAWER, false);
  245. move_level_to_numa(numa, phys, BOOK, true);
  246. move_level_to_numa(numa, phys, BOOK, false);
  247. move_level_to_numa(numa, phys, MC, true);
  248. move_level_to_numa(numa, phys, MC, false);
  249. /* Now pin all the moved cores */
  250. toptree_for_each(core, numa, CORE)
  251. pin_core_to_node(core->id, core_node(core)->id);
  252. }
  253. /*
  254. * Allocate new topology and create required nodes
  255. */
  256. static struct toptree *toptree_new(int id, int nodes)
  257. {
  258. struct toptree *tree;
  259. int nid;
  260. tree = toptree_alloc(TOPOLOGY, id);
  261. if (!tree)
  262. goto fail;
  263. for (nid = 0; nid < nodes; nid++) {
  264. if (!toptree_get_child(tree, nid))
  265. goto fail;
  266. }
  267. return tree;
  268. fail:
  269. panic("NUMA emulation could not allocate topology");
  270. }
  271. /*
  272. * Allocate and initialize core to node mapping
  273. */
  274. static void create_core_to_node_map(void)
  275. {
  276. int i;
  277. emu_cores = kzalloc(sizeof(*emu_cores), GFP_KERNEL);
  278. if (emu_cores == NULL)
  279. panic("Could not allocate cores to node memory");
  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_online_cpu(cpu) {
  315. top = &per_cpu(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, top->core_id);
  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 = &per_cpu(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. /*
  378. * Transfer physical topology into a NUMA topology and modify CPU masks
  379. * according to the NUMA topology.
  380. *
  381. * Must be called with "sched_domains_mutex" lock held.
  382. */
  383. static void emu_update_cpu_topology(void)
  384. {
  385. struct toptree *phys, *numa;
  386. if (emu_cores == NULL)
  387. create_core_to_node_map();
  388. phys = toptree_from_topology();
  389. numa = toptree_to_numa(phys);
  390. toptree_free(phys);
  391. toptree_to_topology(numa);
  392. toptree_free(numa);
  393. print_node_to_core_map();
  394. }
  395. /*
  396. * If emu_size is not set, use CONFIG_EMU_SIZE. Then round to minimum
  397. * alignment (needed for memory hotplug).
  398. */
  399. static unsigned long emu_setup_size_adjust(unsigned long size)
  400. {
  401. unsigned long size_new;
  402. size = size ? : CONFIG_EMU_SIZE;
  403. size_new = roundup(size, memory_block_size_bytes());
  404. if (size_new == size)
  405. return size;
  406. pr_warn("Increasing memory stripe size from %ld MB to %ld MB\n",
  407. size >> 20, size_new >> 20);
  408. return size_new;
  409. }
  410. /*
  411. * If we have not enough memory for the specified nodes, reduce the node count.
  412. */
  413. static int emu_setup_nodes_adjust(int nodes)
  414. {
  415. int nodes_max;
  416. nodes_max = memblock.memory.total_size / emu_size;
  417. nodes_max = max(nodes_max, 1);
  418. if (nodes_max >= nodes)
  419. return nodes;
  420. pr_warn("Not enough memory for %d nodes, reducing node count\n", nodes);
  421. return nodes_max;
  422. }
  423. /*
  424. * Early emu setup
  425. */
  426. static void emu_setup(void)
  427. {
  428. int nid;
  429. emu_size = emu_setup_size_adjust(emu_size);
  430. emu_nodes = emu_setup_nodes_adjust(emu_nodes);
  431. for (nid = 0; nid < emu_nodes; nid++)
  432. node_set(nid, node_possible_map);
  433. pr_info("Creating %d nodes with memory stripe size %ld MB\n",
  434. emu_nodes, emu_size >> 20);
  435. }
  436. /*
  437. * Return node id for given page number
  438. */
  439. static int emu_pfn_to_nid(unsigned long pfn)
  440. {
  441. return (pfn / (emu_size >> PAGE_SHIFT)) % emu_nodes;
  442. }
  443. /*
  444. * Return stripe size
  445. */
  446. static unsigned long emu_align(void)
  447. {
  448. return emu_size;
  449. }
  450. /*
  451. * Return distance between two nodes
  452. */
  453. static int emu_distance(int node1, int node2)
  454. {
  455. return (node1 != node2) * EMU_NODE_DIST;
  456. }
  457. /*
  458. * Define callbacks for generic s390 NUMA infrastructure
  459. */
  460. const struct numa_mode numa_mode_emu = {
  461. .name = "emu",
  462. .setup = emu_setup,
  463. .update_cpu_topology = emu_update_cpu_topology,
  464. .__pfn_to_nid = emu_pfn_to_nid,
  465. .align = emu_align,
  466. .distance = emu_distance,
  467. };
  468. /*
  469. * Kernel parameter: emu_nodes=<n>
  470. */
  471. static int __init early_parse_emu_nodes(char *p)
  472. {
  473. int count;
  474. if (kstrtoint(p, 0, &count) != 0 || count <= 0)
  475. return 0;
  476. if (count <= 0)
  477. return 0;
  478. emu_nodes = min(count, MAX_NUMNODES);
  479. return 0;
  480. }
  481. early_param("emu_nodes", early_parse_emu_nodes);
  482. /*
  483. * Kernel parameter: emu_size=[<n>[k|M|G|T]]
  484. */
  485. static int __init early_parse_emu_size(char *p)
  486. {
  487. emu_size = memparse(p, NULL);
  488. return 0;
  489. }
  490. early_param("emu_size", early_parse_emu_size);