numa_emulation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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];
  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] = pb->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 [mem %#018Lx-%#018Lx] (%LuMB)\n",
  59. nid, eb->start, eb->end - 1, (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, &emu_cmdline, 0);
  300. ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
  301. }
  302. if (*emu_cmdline == ':')
  303. emu_cmdline++;
  304. if (ret < 0)
  305. goto no_emu;
  306. if (numa_cleanup_meminfo(&ei) < 0) {
  307. pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
  308. goto no_emu;
  309. }
  310. /* copy the physical distance table */
  311. if (numa_dist_cnt) {
  312. u64 phys;
  313. phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
  314. phys_size, PAGE_SIZE);
  315. if (!phys) {
  316. pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
  317. goto no_emu;
  318. }
  319. memblock_reserve(phys, phys_size);
  320. phys_dist = __va(phys);
  321. for (i = 0; i < numa_dist_cnt; i++)
  322. for (j = 0; j < numa_dist_cnt; j++)
  323. phys_dist[i * numa_dist_cnt + j] =
  324. node_distance(i, j);
  325. }
  326. /*
  327. * Determine the max emulated nid and the default phys nid to use
  328. * for unmapped nodes.
  329. */
  330. max_emu_nid = 0;
  331. dfl_phys_nid = NUMA_NO_NODE;
  332. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++) {
  333. if (emu_nid_to_phys[i] != NUMA_NO_NODE) {
  334. max_emu_nid = i;
  335. if (dfl_phys_nid == NUMA_NO_NODE)
  336. dfl_phys_nid = emu_nid_to_phys[i];
  337. }
  338. }
  339. if (dfl_phys_nid == NUMA_NO_NODE) {
  340. pr_warning("NUMA: Warning: can't determine default physical node, disabling emulation\n");
  341. goto no_emu;
  342. }
  343. /* commit */
  344. *numa_meminfo = ei;
  345. /*
  346. * Transform __apicid_to_node table to use emulated nids by
  347. * reverse-mapping phys_nid. The maps should always exist but fall
  348. * back to zero just in case.
  349. */
  350. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  351. if (__apicid_to_node[i] == NUMA_NO_NODE)
  352. continue;
  353. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  354. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  355. break;
  356. __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
  357. }
  358. /* make sure all emulated nodes are mapped to a physical node */
  359. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  360. if (emu_nid_to_phys[i] == NUMA_NO_NODE)
  361. emu_nid_to_phys[i] = dfl_phys_nid;
  362. /* transform distance table */
  363. numa_reset_distance();
  364. for (i = 0; i < max_emu_nid + 1; i++) {
  365. for (j = 0; j < max_emu_nid + 1; j++) {
  366. int physi = emu_nid_to_phys[i];
  367. int physj = emu_nid_to_phys[j];
  368. int dist;
  369. if (get_option(&emu_cmdline, &dist) == 2)
  370. ;
  371. else if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
  372. dist = physi == physj ?
  373. LOCAL_DISTANCE : REMOTE_DISTANCE;
  374. else
  375. dist = phys_dist[physi * numa_dist_cnt + physj];
  376. numa_set_distance(i, j, dist);
  377. }
  378. }
  379. /* free the copied physical distance table */
  380. if (phys_dist)
  381. memblock_free(__pa(phys_dist), phys_size);
  382. return;
  383. no_emu:
  384. /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
  385. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  386. emu_nid_to_phys[i] = i;
  387. }
  388. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  389. void numa_add_cpu(int cpu)
  390. {
  391. int physnid, nid;
  392. nid = early_cpu_to_node(cpu);
  393. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  394. physnid = emu_nid_to_phys[nid];
  395. /*
  396. * Map the cpu to each emulated node that is allocated on the physical
  397. * node of the cpu's apic id.
  398. */
  399. for_each_online_node(nid)
  400. if (emu_nid_to_phys[nid] == physnid)
  401. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  402. }
  403. void numa_remove_cpu(int cpu)
  404. {
  405. int i;
  406. for_each_online_node(i)
  407. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  408. }
  409. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  410. static void numa_set_cpumask(int cpu, bool enable)
  411. {
  412. int nid, physnid;
  413. nid = early_cpu_to_node(cpu);
  414. if (nid == NUMA_NO_NODE) {
  415. /* early_cpu_to_node() already emits a warning and trace */
  416. return;
  417. }
  418. physnid = emu_nid_to_phys[nid];
  419. for_each_online_node(nid) {
  420. if (emu_nid_to_phys[nid] != physnid)
  421. continue;
  422. debug_cpumask_set_cpu(cpu, nid, enable);
  423. }
  424. }
  425. void numa_add_cpu(int cpu)
  426. {
  427. numa_set_cpumask(cpu, true);
  428. }
  429. void numa_remove_cpu(int cpu)
  430. {
  431. numa_set_cpumask(cpu, false);
  432. }
  433. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */