numa_emulation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 i, j, ret;
  271. if (!emu_cmdline)
  272. goto no_emu;
  273. memset(&ei, 0, sizeof(ei));
  274. pi = *numa_meminfo;
  275. for (i = 0; i < MAX_NUMNODES; i++)
  276. emu_nid_to_phys[i] = NUMA_NO_NODE;
  277. /*
  278. * If the numa=fake command-line contains a 'M' or 'G', it represents
  279. * the fixed node size. Otherwise, if it is just a single number N,
  280. * split the system RAM into N fake nodes.
  281. */
  282. if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
  283. u64 size;
  284. size = memparse(emu_cmdline, &emu_cmdline);
  285. ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
  286. } else {
  287. unsigned long n;
  288. n = simple_strtoul(emu_cmdline, NULL, 0);
  289. ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
  290. }
  291. if (ret < 0)
  292. goto no_emu;
  293. if (numa_cleanup_meminfo(&ei) < 0) {
  294. pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
  295. goto no_emu;
  296. }
  297. /* copy the physical distance table */
  298. if (numa_dist_cnt) {
  299. u64 phys;
  300. phys = memblock_find_in_range(0,
  301. (u64)max_pfn_mapped << PAGE_SHIFT,
  302. phys_size, PAGE_SIZE);
  303. if (phys == MEMBLOCK_ERROR) {
  304. pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
  305. goto no_emu;
  306. }
  307. memblock_x86_reserve_range(phys, phys + phys_size, "TMP NUMA DIST");
  308. phys_dist = __va(phys);
  309. for (i = 0; i < numa_dist_cnt; i++)
  310. for (j = 0; j < numa_dist_cnt; j++)
  311. phys_dist[i * numa_dist_cnt + j] =
  312. node_distance(i, j);
  313. }
  314. /* commit */
  315. *numa_meminfo = ei;
  316. /*
  317. * Transform __apicid_to_node table to use emulated nids by
  318. * reverse-mapping phys_nid. The maps should always exist but fall
  319. * back to zero just in case.
  320. */
  321. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  322. if (__apicid_to_node[i] == NUMA_NO_NODE)
  323. continue;
  324. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  325. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  326. break;
  327. __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
  328. }
  329. /* make sure all emulated nodes are mapped to a physical node */
  330. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  331. if (emu_nid_to_phys[i] == NUMA_NO_NODE)
  332. emu_nid_to_phys[i] = 0;
  333. /*
  334. * Transform distance table. numa_set_distance() ignores all
  335. * out-of-bound distances. Just call it for every possible node
  336. * combination.
  337. */
  338. numa_reset_distance();
  339. for (i = 0; i < MAX_NUMNODES; i++) {
  340. for (j = 0; j < MAX_NUMNODES; j++) {
  341. int physi = emu_nid_to_phys[i];
  342. int physj = emu_nid_to_phys[j];
  343. int dist;
  344. if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
  345. dist = physi == physj ?
  346. LOCAL_DISTANCE : REMOTE_DISTANCE;
  347. else
  348. dist = phys_dist[physi * numa_dist_cnt + physj];
  349. numa_set_distance(i, j, dist);
  350. }
  351. }
  352. /* free the copied physical distance table */
  353. if (phys_dist)
  354. memblock_x86_free_range(__pa(phys_dist), __pa(phys_dist) + phys_size);
  355. return;
  356. no_emu:
  357. /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
  358. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  359. emu_nid_to_phys[i] = i;
  360. }
  361. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  362. void __cpuinit numa_add_cpu(int cpu)
  363. {
  364. int physnid, nid;
  365. nid = early_cpu_to_node(cpu);
  366. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  367. physnid = emu_nid_to_phys[nid];
  368. /*
  369. * Map the cpu to each emulated node that is allocated on the physical
  370. * node of the cpu's apic id.
  371. */
  372. for_each_online_node(nid)
  373. if (emu_nid_to_phys[nid] == physnid)
  374. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  375. }
  376. void __cpuinit numa_remove_cpu(int cpu)
  377. {
  378. int i;
  379. for_each_online_node(i)
  380. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  381. }
  382. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  383. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  384. {
  385. struct cpumask *mask;
  386. int nid, physnid, i;
  387. nid = early_cpu_to_node(cpu);
  388. if (nid == NUMA_NO_NODE) {
  389. /* early_cpu_to_node() already emits a warning and trace */
  390. return;
  391. }
  392. physnid = emu_nid_to_phys[nid];
  393. for_each_online_node(i) {
  394. if (emu_nid_to_phys[nid] != physnid)
  395. continue;
  396. mask = debug_cpumask_set_cpu(cpu, enable);
  397. if (!mask)
  398. return;
  399. if (enable)
  400. cpumask_set_cpu(cpu, mask);
  401. else
  402. cpumask_clear_cpu(cpu, mask);
  403. }
  404. }
  405. void __cpuinit numa_add_cpu(int cpu)
  406. {
  407. numa_set_cpumask(cpu, 1);
  408. }
  409. void __cpuinit numa_remove_cpu(int cpu)
  410. {
  411. numa_set_cpumask(cpu, 0);
  412. }
  413. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */