numa_emulation.c 15 KB

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