numa_emulation.c 13 KB

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