numa_emulation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * NUMA emulation
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/errno.h>
  6. #include <linux/topology.h>
  7. #include <linux/memblock.h>
  8. #include <asm/dma.h>
  9. #include "numa_internal.h"
  10. static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
  11. static char *emu_cmdline __initdata;
  12. void __init numa_emu_cmdline(char *str)
  13. {
  14. emu_cmdline = str;
  15. }
  16. static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
  17. {
  18. int i;
  19. for (i = 0; i < mi->nr_blks; i++)
  20. if (mi->blk[i].nid == nid)
  21. return i;
  22. return -ENOENT;
  23. }
  24. /*
  25. * Sets up nid to range from @start to @end. The return value is -errno if
  26. * something went wrong, 0 otherwise.
  27. */
  28. static int __init emu_setup_memblk(struct numa_meminfo *ei,
  29. struct numa_meminfo *pi,
  30. int nid, int phys_blk, u64 size)
  31. {
  32. struct numa_memblk *eb = &ei->blk[ei->nr_blks];
  33. struct numa_memblk *pb = &pi->blk[phys_blk];
  34. if (ei->nr_blks >= NR_NODE_MEMBLKS) {
  35. pr_err("NUMA: Too many emulated memblks, failing emulation\n");
  36. return -EINVAL;
  37. }
  38. ei->nr_blks++;
  39. eb->start = pb->start;
  40. eb->end = pb->start + size;
  41. eb->nid = nid;
  42. if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
  43. emu_nid_to_phys[nid] = pb->nid;
  44. pb->start += size;
  45. if (pb->start >= pb->end) {
  46. WARN_ON_ONCE(pb->start > pb->end);
  47. numa_remove_memblk_from(phys_blk, pi);
  48. }
  49. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  50. eb->start, eb->end, (eb->end - eb->start) >> 20);
  51. return 0;
  52. }
  53. /*
  54. * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
  55. * to max_addr. The return value is the number of nodes allocated.
  56. */
  57. static int __init split_nodes_interleave(struct numa_meminfo *ei,
  58. struct numa_meminfo *pi,
  59. u64 addr, u64 max_addr, int nr_nodes)
  60. {
  61. nodemask_t physnode_mask = NODE_MASK_NONE;
  62. u64 size;
  63. int big;
  64. int nid = 0;
  65. int i, ret;
  66. if (nr_nodes <= 0)
  67. return -1;
  68. if (nr_nodes > MAX_NUMNODES) {
  69. pr_info("numa=fake=%d too large, reducing to %d\n",
  70. nr_nodes, MAX_NUMNODES);
  71. nr_nodes = MAX_NUMNODES;
  72. }
  73. size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
  74. /*
  75. * Calculate the number of big nodes that can be allocated as a result
  76. * of consolidating the remainder.
  77. */
  78. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
  79. FAKE_NODE_MIN_SIZE;
  80. size &= FAKE_NODE_MIN_HASH_MASK;
  81. if (!size) {
  82. pr_err("Not enough memory for each node. "
  83. "NUMA emulation disabled.\n");
  84. return -1;
  85. }
  86. for (i = 0; i < pi->nr_blks; i++)
  87. node_set(pi->blk[i].nid, physnode_mask);
  88. /*
  89. * Continue to fill physical nodes with fake nodes until there is no
  90. * memory left on any of them.
  91. */
  92. while (nodes_weight(physnode_mask)) {
  93. for_each_node_mask(i, physnode_mask) {
  94. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  95. u64 start, limit, end;
  96. int phys_blk;
  97. phys_blk = emu_find_memblk_by_nid(i, pi);
  98. if (phys_blk < 0) {
  99. node_clear(i, physnode_mask);
  100. continue;
  101. }
  102. start = pi->blk[phys_blk].start;
  103. limit = pi->blk[phys_blk].end;
  104. end = start + size;
  105. if (nid < big)
  106. end += FAKE_NODE_MIN_SIZE;
  107. /*
  108. * Continue to add memory to this fake node if its
  109. * non-reserved memory is less than the per-node size.
  110. */
  111. while (end - start -
  112. memblock_x86_hole_size(start, end) < size) {
  113. end += FAKE_NODE_MIN_SIZE;
  114. if (end > limit) {
  115. end = limit;
  116. break;
  117. }
  118. }
  119. /*
  120. * If there won't be at least FAKE_NODE_MIN_SIZE of
  121. * non-reserved memory in ZONE_DMA32 for the next node,
  122. * this one must extend to the boundary.
  123. */
  124. if (end < dma32_end && dma32_end - end -
  125. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  126. end = dma32_end;
  127. /*
  128. * If there won't be enough non-reserved memory for the
  129. * next node, this one must extend to the end of the
  130. * physical node.
  131. */
  132. if (limit - end -
  133. memblock_x86_hole_size(end, limit) < size)
  134. end = limit;
  135. ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
  136. phys_blk,
  137. min(end, limit) - start);
  138. if (ret < 0)
  139. return ret;
  140. }
  141. }
  142. return 0;
  143. }
  144. /*
  145. * Returns the end address of a node so that there is at least `size' amount of
  146. * non-reserved memory or `max_addr' is reached.
  147. */
  148. static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
  149. {
  150. u64 end = start + size;
  151. while (end - start - memblock_x86_hole_size(start, end) < size) {
  152. end += FAKE_NODE_MIN_SIZE;
  153. if (end > max_addr) {
  154. end = max_addr;
  155. break;
  156. }
  157. }
  158. return end;
  159. }
  160. /*
  161. * Sets up fake nodes of `size' interleaved over physical nodes ranging from
  162. * `addr' to `max_addr'. The return value is the number of nodes allocated.
  163. */
  164. static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
  165. struct numa_meminfo *pi,
  166. u64 addr, u64 max_addr, u64 size)
  167. {
  168. nodemask_t physnode_mask = NODE_MASK_NONE;
  169. u64 min_size;
  170. int nid = 0;
  171. int i, ret;
  172. if (!size)
  173. return -1;
  174. /*
  175. * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
  176. * increased accordingly if the requested size is too small. This
  177. * creates a uniform distribution of node sizes across the entire
  178. * machine (but not necessarily over physical nodes).
  179. */
  180. min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
  181. MAX_NUMNODES;
  182. min_size = max(min_size, FAKE_NODE_MIN_SIZE);
  183. if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
  184. min_size = (min_size + FAKE_NODE_MIN_SIZE) &
  185. FAKE_NODE_MIN_HASH_MASK;
  186. if (size < min_size) {
  187. pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
  188. size >> 20, min_size >> 20);
  189. size = min_size;
  190. }
  191. size &= FAKE_NODE_MIN_HASH_MASK;
  192. for (i = 0; i < pi->nr_blks; i++)
  193. node_set(pi->blk[i].nid, physnode_mask);
  194. /*
  195. * Fill physical nodes with fake nodes of size until there is no memory
  196. * left on any of them.
  197. */
  198. while (nodes_weight(physnode_mask)) {
  199. for_each_node_mask(i, physnode_mask) {
  200. u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
  201. u64 start, limit, end;
  202. int phys_blk;
  203. phys_blk = emu_find_memblk_by_nid(i, pi);
  204. if (phys_blk < 0) {
  205. node_clear(i, physnode_mask);
  206. continue;
  207. }
  208. start = pi->blk[phys_blk].start;
  209. limit = pi->blk[phys_blk].end;
  210. end = find_end_of_node(start, limit, size);
  211. /*
  212. * If there won't be at least FAKE_NODE_MIN_SIZE of
  213. * non-reserved memory in ZONE_DMA32 for the next node,
  214. * this one must extend to the boundary.
  215. */
  216. if (end < dma32_end && dma32_end - end -
  217. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  218. end = dma32_end;
  219. /*
  220. * If there won't be enough non-reserved memory for the
  221. * next node, this one must extend to the end of the
  222. * physical node.
  223. */
  224. if (limit - end -
  225. memblock_x86_hole_size(end, limit) < size)
  226. end = limit;
  227. ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
  228. phys_blk,
  229. min(end, limit) - start);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. }
  234. return 0;
  235. }
  236. /**
  237. * numa_emulation - Emulate NUMA nodes
  238. * @numa_meminfo: NUMA configuration to massage
  239. * @numa_dist_cnt: The size of the physical NUMA distance table
  240. *
  241. * Emulate NUMA nodes according to the numa=fake kernel parameter.
  242. * @numa_meminfo contains the physical memory configuration and is modified
  243. * to reflect the emulated configuration on success. @numa_dist_cnt is
  244. * used to determine the size of the physical distance table.
  245. *
  246. * On success, the following modifications are made.
  247. *
  248. * - @numa_meminfo is updated to reflect the emulated nodes.
  249. *
  250. * - __apicid_to_node[] is updated such that APIC IDs are mapped to the
  251. * emulated nodes.
  252. *
  253. * - NUMA distance table is rebuilt to represent distances between emulated
  254. * nodes. The distances are determined considering how emulated nodes
  255. * are mapped to physical nodes and match the actual distances.
  256. *
  257. * - emu_nid_to_phys[] reflects how emulated nodes are mapped to physical
  258. * nodes. This is used by numa_add_cpu() and numa_remove_cpu().
  259. *
  260. * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
  261. * identity mapping and no other modification is made.
  262. */
  263. void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
  264. {
  265. static struct numa_meminfo ei __initdata;
  266. static struct numa_meminfo pi __initdata;
  267. const u64 max_addr = max_pfn << PAGE_SHIFT;
  268. u8 *phys_dist = NULL;
  269. size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
  270. int max_emu_nid, dfl_phys_nid;
  271. int i, j, ret;
  272. if (!emu_cmdline)
  273. goto no_emu;
  274. memset(&ei, 0, sizeof(ei));
  275. pi = *numa_meminfo;
  276. for (i = 0; i < MAX_NUMNODES; i++)
  277. emu_nid_to_phys[i] = NUMA_NO_NODE;
  278. /*
  279. * If the numa=fake command-line contains a 'M' or 'G', it represents
  280. * the fixed node size. Otherwise, if it is just a single number N,
  281. * split the system RAM into N fake nodes.
  282. */
  283. if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
  284. u64 size;
  285. size = memparse(emu_cmdline, &emu_cmdline);
  286. ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
  287. } else {
  288. unsigned long n;
  289. n = simple_strtoul(emu_cmdline, NULL, 0);
  290. ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
  291. }
  292. if (ret < 0)
  293. goto no_emu;
  294. if (numa_cleanup_meminfo(&ei) < 0) {
  295. pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
  296. goto no_emu;
  297. }
  298. /* copy the physical distance table */
  299. if (numa_dist_cnt) {
  300. u64 phys;
  301. phys = memblock_find_in_range(0,
  302. (u64)max_pfn_mapped << PAGE_SHIFT,
  303. phys_size, PAGE_SIZE);
  304. if (phys == MEMBLOCK_ERROR) {
  305. pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
  306. goto no_emu;
  307. }
  308. memblock_x86_reserve_range(phys, phys + phys_size, "TMP NUMA DIST");
  309. phys_dist = __va(phys);
  310. for (i = 0; i < numa_dist_cnt; i++)
  311. for (j = 0; j < numa_dist_cnt; j++)
  312. phys_dist[i * numa_dist_cnt + j] =
  313. node_distance(i, j);
  314. }
  315. /*
  316. * Determine the max emulated nid and the default phys nid to use
  317. * for unmapped nodes.
  318. */
  319. max_emu_nid = 0;
  320. dfl_phys_nid = NUMA_NO_NODE;
  321. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++) {
  322. if (emu_nid_to_phys[i] != NUMA_NO_NODE) {
  323. max_emu_nid = i;
  324. if (dfl_phys_nid == NUMA_NO_NODE)
  325. dfl_phys_nid = emu_nid_to_phys[i];
  326. }
  327. }
  328. if (dfl_phys_nid == NUMA_NO_NODE) {
  329. pr_warning("NUMA: Warning: can't determine default physical node, disabling emulation\n");
  330. goto no_emu;
  331. }
  332. /* commit */
  333. *numa_meminfo = ei;
  334. /*
  335. * Transform __apicid_to_node table to use emulated nids by
  336. * reverse-mapping phys_nid. The maps should always exist but fall
  337. * back to zero just in case.
  338. */
  339. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  340. if (__apicid_to_node[i] == NUMA_NO_NODE)
  341. continue;
  342. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  343. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  344. break;
  345. __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
  346. }
  347. /* make sure all emulated nodes are mapped to a physical node */
  348. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  349. if (emu_nid_to_phys[i] == NUMA_NO_NODE)
  350. emu_nid_to_phys[i] = dfl_phys_nid;
  351. /* transform distance table */
  352. numa_reset_distance();
  353. for (i = 0; i < max_emu_nid + 1; i++) {
  354. for (j = 0; j < max_emu_nid + 1; j++) {
  355. int physi = emu_nid_to_phys[i];
  356. int physj = emu_nid_to_phys[j];
  357. int dist;
  358. if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
  359. dist = physi == physj ?
  360. LOCAL_DISTANCE : REMOTE_DISTANCE;
  361. else
  362. dist = phys_dist[physi * numa_dist_cnt + physj];
  363. numa_set_distance(i, j, dist);
  364. }
  365. }
  366. /* free the copied physical distance table */
  367. if (phys_dist)
  368. memblock_x86_free_range(__pa(phys_dist), __pa(phys_dist) + phys_size);
  369. return;
  370. no_emu:
  371. /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
  372. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  373. emu_nid_to_phys[i] = i;
  374. }
  375. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  376. void __cpuinit numa_add_cpu(int cpu)
  377. {
  378. int physnid, nid;
  379. nid = early_cpu_to_node(cpu);
  380. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  381. physnid = emu_nid_to_phys[nid];
  382. /*
  383. * Map the cpu to each emulated node that is allocated on the physical
  384. * node of the cpu's apic id.
  385. */
  386. for_each_online_node(nid)
  387. if (emu_nid_to_phys[nid] == physnid)
  388. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  389. }
  390. void __cpuinit numa_remove_cpu(int cpu)
  391. {
  392. int i;
  393. for_each_online_node(i)
  394. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  395. }
  396. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  397. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  398. {
  399. struct cpumask *mask;
  400. int nid, physnid, i;
  401. nid = early_cpu_to_node(cpu);
  402. if (nid == NUMA_NO_NODE) {
  403. /* early_cpu_to_node() already emits a warning and trace */
  404. return;
  405. }
  406. physnid = emu_nid_to_phys[nid];
  407. for_each_online_node(i) {
  408. if (emu_nid_to_phys[nid] != physnid)
  409. continue;
  410. mask = debug_cpumask_set_cpu(cpu, enable);
  411. if (!mask)
  412. return;
  413. if (enable)
  414. cpumask_set_cpu(cpu, mask);
  415. else
  416. cpumask_clear_cpu(cpu, mask);
  417. }
  418. }
  419. void __cpuinit numa_add_cpu(int cpu)
  420. {
  421. numa_set_cpumask(cpu, 1);
  422. }
  423. void __cpuinit numa_remove_cpu(int cpu)
  424. {
  425. numa_set_cpumask(cpu, 0);
  426. }
  427. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */