numa_emulation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NUMA emulation
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/topology.h>
  8. #include <linux/memblock.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.
  65. *
  66. * Returns zero on success or negative on error.
  67. */
  68. static int __init split_nodes_interleave(struct numa_meminfo *ei,
  69. struct numa_meminfo *pi,
  70. u64 addr, u64 max_addr, int nr_nodes)
  71. {
  72. nodemask_t physnode_mask = numa_nodes_parsed;
  73. u64 size;
  74. int big;
  75. int nid = 0;
  76. int i, ret;
  77. if (nr_nodes <= 0)
  78. return -1;
  79. if (nr_nodes > MAX_NUMNODES) {
  80. pr_info("numa=fake=%d too large, reducing to %d\n",
  81. nr_nodes, MAX_NUMNODES);
  82. nr_nodes = MAX_NUMNODES;
  83. }
  84. /*
  85. * Calculate target node size. x86_32 freaks on __udivdi3() so do
  86. * the division in ulong number of pages and convert back.
  87. */
  88. size = max_addr - addr - mem_hole_size(addr, max_addr);
  89. size = PFN_PHYS((unsigned long)(size >> PAGE_SHIFT) / nr_nodes);
  90. /*
  91. * Calculate the number of big nodes that can be allocated as a result
  92. * of consolidating the remainder.
  93. */
  94. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
  95. FAKE_NODE_MIN_SIZE;
  96. size &= FAKE_NODE_MIN_HASH_MASK;
  97. if (!size) {
  98. pr_err("Not enough memory for each node. "
  99. "NUMA emulation disabled.\n");
  100. return -1;
  101. }
  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. static u64 uniform_size(u64 max_addr, u64 base, u64 hole, int nr_nodes)
  173. {
  174. unsigned long max_pfn = PHYS_PFN(max_addr);
  175. unsigned long base_pfn = PHYS_PFN(base);
  176. unsigned long hole_pfns = PHYS_PFN(hole);
  177. return PFN_PHYS((max_pfn - base_pfn - hole_pfns) / nr_nodes);
  178. }
  179. /*
  180. * Sets up fake nodes of `size' interleaved over physical nodes ranging from
  181. * `addr' to `max_addr'.
  182. *
  183. * Returns zero on success or negative on error.
  184. */
  185. static int __init split_nodes_size_interleave_uniform(struct numa_meminfo *ei,
  186. struct numa_meminfo *pi,
  187. u64 addr, u64 max_addr, u64 size,
  188. int nr_nodes, struct numa_memblk *pblk,
  189. int nid)
  190. {
  191. nodemask_t physnode_mask = numa_nodes_parsed;
  192. int i, ret, uniform = 0;
  193. u64 min_size;
  194. if ((!size && !nr_nodes) || (nr_nodes && !pblk))
  195. return -1;
  196. /*
  197. * In the 'uniform' case split the passed in physical node by
  198. * nr_nodes, in the non-uniform case, ignore the passed in
  199. * physical block and try to create nodes of at least size
  200. * @size.
  201. *
  202. * In the uniform case, split the nodes strictly by physical
  203. * capacity, i.e. ignore holes. In the non-uniform case account
  204. * for holes and treat @size as a minimum floor.
  205. */
  206. if (!nr_nodes)
  207. nr_nodes = MAX_NUMNODES;
  208. else {
  209. nodes_clear(physnode_mask);
  210. node_set(pblk->nid, physnode_mask);
  211. uniform = 1;
  212. }
  213. if (uniform) {
  214. min_size = uniform_size(max_addr, addr, 0, nr_nodes);
  215. size = min_size;
  216. } else {
  217. /*
  218. * The limit on emulated nodes is MAX_NUMNODES, so the
  219. * size per node is increased accordingly if the
  220. * requested size is too small. This creates a uniform
  221. * distribution of node sizes across the entire machine
  222. * (but not necessarily over physical nodes).
  223. */
  224. min_size = uniform_size(max_addr, addr,
  225. mem_hole_size(addr, max_addr), nr_nodes);
  226. }
  227. min_size = ALIGN(max(min_size, FAKE_NODE_MIN_SIZE), FAKE_NODE_MIN_SIZE);
  228. if (size < min_size) {
  229. pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
  230. size >> 20, min_size >> 20);
  231. size = min_size;
  232. }
  233. size = ALIGN_DOWN(size, FAKE_NODE_MIN_SIZE);
  234. /*
  235. * Fill physical nodes with fake nodes of size until there is no memory
  236. * left on any of them.
  237. */
  238. while (nodes_weight(physnode_mask)) {
  239. for_each_node_mask(i, physnode_mask) {
  240. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  241. u64 start, limit, end;
  242. int phys_blk;
  243. phys_blk = emu_find_memblk_by_nid(i, pi);
  244. if (phys_blk < 0) {
  245. node_clear(i, physnode_mask);
  246. continue;
  247. }
  248. start = pi->blk[phys_blk].start;
  249. limit = pi->blk[phys_blk].end;
  250. if (uniform)
  251. end = start + size;
  252. else
  253. end = find_end_of_node(start, limit, size);
  254. /*
  255. * If there won't be at least FAKE_NODE_MIN_SIZE of
  256. * non-reserved memory in ZONE_DMA32 for the next node,
  257. * this one must extend to the boundary.
  258. */
  259. if (end < dma32_end && dma32_end - end -
  260. mem_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  261. end = dma32_end;
  262. /*
  263. * If there won't be enough non-reserved memory for the
  264. * next node, this one must extend to the end of the
  265. * physical node.
  266. */
  267. if ((limit - end - mem_hole_size(end, limit) < size)
  268. && !uniform)
  269. end = limit;
  270. ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
  271. phys_blk,
  272. min(end, limit) - start);
  273. if (ret < 0)
  274. return ret;
  275. }
  276. }
  277. return nid;
  278. }
  279. static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
  280. struct numa_meminfo *pi,
  281. u64 addr, u64 max_addr, u64 size)
  282. {
  283. return split_nodes_size_interleave_uniform(ei, pi, addr, max_addr, size,
  284. 0, NULL, NUMA_NO_NODE);
  285. }
  286. int __init setup_emu2phys_nid(int *dfl_phys_nid)
  287. {
  288. int i, max_emu_nid = 0;
  289. *dfl_phys_nid = NUMA_NO_NODE;
  290. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++) {
  291. if (emu_nid_to_phys[i] != NUMA_NO_NODE) {
  292. max_emu_nid = i;
  293. if (*dfl_phys_nid == NUMA_NO_NODE)
  294. *dfl_phys_nid = emu_nid_to_phys[i];
  295. }
  296. }
  297. return max_emu_nid;
  298. }
  299. /**
  300. * numa_emulation - Emulate NUMA nodes
  301. * @numa_meminfo: NUMA configuration to massage
  302. * @numa_dist_cnt: The size of the physical NUMA distance table
  303. *
  304. * Emulate NUMA nodes according to the numa=fake kernel parameter.
  305. * @numa_meminfo contains the physical memory configuration and is modified
  306. * to reflect the emulated configuration on success. @numa_dist_cnt is
  307. * used to determine the size of the physical distance table.
  308. *
  309. * On success, the following modifications are made.
  310. *
  311. * - @numa_meminfo is updated to reflect the emulated nodes.
  312. *
  313. * - __apicid_to_node[] is updated such that APIC IDs are mapped to the
  314. * emulated nodes.
  315. *
  316. * - NUMA distance table is rebuilt to represent distances between emulated
  317. * nodes. The distances are determined considering how emulated nodes
  318. * are mapped to physical nodes and match the actual distances.
  319. *
  320. * - emu_nid_to_phys[] reflects how emulated nodes are mapped to physical
  321. * nodes. This is used by numa_add_cpu() and numa_remove_cpu().
  322. *
  323. * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
  324. * identity mapping and no other modification is made.
  325. */
  326. void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
  327. {
  328. static struct numa_meminfo ei __initdata;
  329. static struct numa_meminfo pi __initdata;
  330. const u64 max_addr = PFN_PHYS(max_pfn);
  331. u8 *phys_dist = NULL;
  332. size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
  333. int max_emu_nid, dfl_phys_nid;
  334. int i, j, ret;
  335. if (!emu_cmdline)
  336. goto no_emu;
  337. memset(&ei, 0, sizeof(ei));
  338. pi = *numa_meminfo;
  339. for (i = 0; i < MAX_NUMNODES; i++)
  340. emu_nid_to_phys[i] = NUMA_NO_NODE;
  341. /*
  342. * If the numa=fake command-line contains a 'M' or 'G', it represents
  343. * the fixed node size. Otherwise, if it is just a single number N,
  344. * split the system RAM into N fake nodes.
  345. */
  346. if (strchr(emu_cmdline, 'U')) {
  347. nodemask_t physnode_mask = numa_nodes_parsed;
  348. unsigned long n;
  349. int nid = 0;
  350. n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
  351. ret = -1;
  352. for_each_node_mask(i, physnode_mask) {
  353. /*
  354. * The reason we pass in blk[0] is due to
  355. * numa_remove_memblk_from() called by
  356. * emu_setup_memblk() will delete entry 0
  357. * and then move everything else up in the pi.blk
  358. * array. Therefore we should always be looking
  359. * at blk[0].
  360. */
  361. ret = split_nodes_size_interleave_uniform(&ei, &pi,
  362. pi.blk[0].start, pi.blk[0].end, 0,
  363. n, &pi.blk[0], nid);
  364. if (ret < 0)
  365. break;
  366. if (ret < n) {
  367. pr_info("%s: phys: %d only got %d of %ld nodes, failing\n",
  368. __func__, i, ret, n);
  369. ret = -1;
  370. break;
  371. }
  372. nid = ret;
  373. }
  374. } else if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
  375. u64 size;
  376. size = memparse(emu_cmdline, &emu_cmdline);
  377. ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
  378. } else {
  379. unsigned long n;
  380. n = simple_strtoul(emu_cmdline, &emu_cmdline, 0);
  381. ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
  382. }
  383. if (*emu_cmdline == ':')
  384. emu_cmdline++;
  385. if (ret < 0)
  386. goto no_emu;
  387. if (numa_cleanup_meminfo(&ei) < 0) {
  388. pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
  389. goto no_emu;
  390. }
  391. /* copy the physical distance table */
  392. if (numa_dist_cnt) {
  393. u64 phys;
  394. phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
  395. phys_size, PAGE_SIZE);
  396. if (!phys) {
  397. pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
  398. goto no_emu;
  399. }
  400. memblock_reserve(phys, phys_size);
  401. phys_dist = __va(phys);
  402. for (i = 0; i < numa_dist_cnt; i++)
  403. for (j = 0; j < numa_dist_cnt; j++)
  404. phys_dist[i * numa_dist_cnt + j] =
  405. node_distance(i, j);
  406. }
  407. /*
  408. * Determine the max emulated nid and the default phys nid to use
  409. * for unmapped nodes.
  410. */
  411. max_emu_nid = setup_emu2phys_nid(&dfl_phys_nid);
  412. /* commit */
  413. *numa_meminfo = ei;
  414. /* Make sure numa_nodes_parsed only contains emulated nodes */
  415. nodes_clear(numa_nodes_parsed);
  416. for (i = 0; i < ARRAY_SIZE(ei.blk); i++)
  417. if (ei.blk[i].start != ei.blk[i].end &&
  418. ei.blk[i].nid != NUMA_NO_NODE)
  419. node_set(ei.blk[i].nid, numa_nodes_parsed);
  420. /*
  421. * Transform __apicid_to_node table to use emulated nids by
  422. * reverse-mapping phys_nid. The maps should always exist but fall
  423. * back to zero just in case.
  424. */
  425. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  426. if (__apicid_to_node[i] == NUMA_NO_NODE)
  427. continue;
  428. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  429. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  430. break;
  431. __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
  432. }
  433. /* make sure all emulated nodes are mapped to a physical node */
  434. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  435. if (emu_nid_to_phys[i] == NUMA_NO_NODE)
  436. emu_nid_to_phys[i] = dfl_phys_nid;
  437. /* transform distance table */
  438. numa_reset_distance();
  439. for (i = 0; i < max_emu_nid + 1; i++) {
  440. for (j = 0; j < max_emu_nid + 1; j++) {
  441. int physi = emu_nid_to_phys[i];
  442. int physj = emu_nid_to_phys[j];
  443. int dist;
  444. if (get_option(&emu_cmdline, &dist) == 2)
  445. ;
  446. else if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
  447. dist = physi == physj ?
  448. LOCAL_DISTANCE : REMOTE_DISTANCE;
  449. else
  450. dist = phys_dist[physi * numa_dist_cnt + physj];
  451. numa_set_distance(i, j, dist);
  452. }
  453. }
  454. /* free the copied physical distance table */
  455. if (phys_dist)
  456. memblock_free(__pa(phys_dist), phys_size);
  457. return;
  458. no_emu:
  459. /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
  460. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  461. emu_nid_to_phys[i] = i;
  462. }
  463. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  464. void numa_add_cpu(int cpu)
  465. {
  466. int physnid, nid;
  467. nid = early_cpu_to_node(cpu);
  468. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  469. physnid = emu_nid_to_phys[nid];
  470. /*
  471. * Map the cpu to each emulated node that is allocated on the physical
  472. * node of the cpu's apic id.
  473. */
  474. for_each_online_node(nid)
  475. if (emu_nid_to_phys[nid] == physnid)
  476. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  477. }
  478. void numa_remove_cpu(int cpu)
  479. {
  480. int i;
  481. for_each_online_node(i)
  482. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  483. }
  484. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  485. static void numa_set_cpumask(int cpu, bool enable)
  486. {
  487. int nid, physnid;
  488. nid = early_cpu_to_node(cpu);
  489. if (nid == NUMA_NO_NODE) {
  490. /* early_cpu_to_node() already emits a warning and trace */
  491. return;
  492. }
  493. physnid = emu_nid_to_phys[nid];
  494. for_each_online_node(nid) {
  495. if (emu_nid_to_phys[nid] != physnid)
  496. continue;
  497. debug_cpumask_set_cpu(cpu, nid, enable);
  498. }
  499. }
  500. void numa_add_cpu(int cpu)
  501. {
  502. numa_set_cpumask(cpu, true);
  503. }
  504. void numa_remove_cpu(int cpu)
  505. {
  506. numa_set_cpumask(cpu, false);
  507. }
  508. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */