numa_memblks.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/array_size.h>
  3. #include <linux/sort.h>
  4. #include <linux/printk.h>
  5. #include <linux/memblock.h>
  6. #include <linux/numa.h>
  7. #include <linux/numa_memblks.h>
  8. #include <asm/numa.h>
  9. int numa_distance_cnt;
  10. static u8 *numa_distance;
  11. nodemask_t numa_nodes_parsed __initdata;
  12. static struct numa_meminfo numa_meminfo __initdata_or_meminfo;
  13. static struct numa_meminfo numa_reserved_meminfo __initdata_or_meminfo;
  14. /*
  15. * Set nodes, which have memory in @mi, in *@nodemask.
  16. */
  17. static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
  18. const struct numa_meminfo *mi)
  19. {
  20. int i;
  21. for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
  22. if (mi->blk[i].start != mi->blk[i].end &&
  23. mi->blk[i].nid != NUMA_NO_NODE)
  24. node_set(mi->blk[i].nid, *nodemask);
  25. }
  26. /**
  27. * numa_reset_distance - Reset NUMA distance table
  28. *
  29. * The current table is freed. The next numa_set_distance() call will
  30. * create a new one.
  31. */
  32. void __init numa_reset_distance(void)
  33. {
  34. size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
  35. /* numa_distance could be 1LU marking allocation failure, test cnt */
  36. if (numa_distance_cnt)
  37. memblock_free(numa_distance, size);
  38. numa_distance_cnt = 0;
  39. numa_distance = NULL; /* enable table creation */
  40. }
  41. static int __init numa_alloc_distance(void)
  42. {
  43. nodemask_t nodes_parsed;
  44. size_t size;
  45. int i, j, cnt = 0;
  46. /* size the new table and allocate it */
  47. nodes_parsed = numa_nodes_parsed;
  48. numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
  49. for_each_node_mask(i, nodes_parsed)
  50. cnt = i;
  51. cnt++;
  52. size = cnt * cnt * sizeof(numa_distance[0]);
  53. numa_distance = memblock_alloc(size, PAGE_SIZE);
  54. if (!numa_distance) {
  55. pr_warn("Warning: can't allocate distance table!\n");
  56. /* don't retry until explicitly reset */
  57. numa_distance = (void *)1LU;
  58. return -ENOMEM;
  59. }
  60. numa_distance_cnt = cnt;
  61. /* fill with the default distances */
  62. for (i = 0; i < cnt; i++)
  63. for (j = 0; j < cnt; j++)
  64. numa_distance[i * cnt + j] = i == j ?
  65. LOCAL_DISTANCE : REMOTE_DISTANCE;
  66. pr_debug("NUMA: Initialized distance table, cnt=%d\n", cnt);
  67. return 0;
  68. }
  69. /**
  70. * numa_set_distance - Set NUMA distance from one NUMA to another
  71. * @from: the 'from' node to set distance
  72. * @to: the 'to' node to set distance
  73. * @distance: NUMA distance
  74. *
  75. * Set the distance from node @from to @to to @distance. If distance table
  76. * doesn't exist, one which is large enough to accommodate all the currently
  77. * known nodes will be created.
  78. *
  79. * If such table cannot be allocated, a warning is printed and further
  80. * calls are ignored until the distance table is reset with
  81. * numa_reset_distance().
  82. *
  83. * If @from or @to is higher than the highest known node or lower than zero
  84. * at the time of table creation or @distance doesn't make sense, the call
  85. * is ignored.
  86. * This is to allow simplification of specific NUMA config implementations.
  87. */
  88. void __init numa_set_distance(int from, int to, int distance)
  89. {
  90. if (!numa_distance && numa_alloc_distance() < 0)
  91. return;
  92. if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
  93. from < 0 || to < 0) {
  94. pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
  95. from, to, distance);
  96. return;
  97. }
  98. if ((u8)distance != distance ||
  99. (from == to && distance != LOCAL_DISTANCE)) {
  100. pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  101. from, to, distance);
  102. return;
  103. }
  104. numa_distance[from * numa_distance_cnt + to] = distance;
  105. }
  106. int __node_distance(int from, int to)
  107. {
  108. if (from >= numa_distance_cnt || to >= numa_distance_cnt)
  109. return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
  110. return numa_distance[from * numa_distance_cnt + to];
  111. }
  112. EXPORT_SYMBOL(__node_distance);
  113. static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
  114. struct numa_meminfo *mi)
  115. {
  116. /* ignore zero length blks */
  117. if (start == end)
  118. return 0;
  119. /* whine about and ignore invalid blks */
  120. if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
  121. pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  122. nid, start, end - 1);
  123. return 0;
  124. }
  125. if (mi->nr_blks >= NR_NODE_MEMBLKS) {
  126. pr_err("too many memblk ranges\n");
  127. return -EINVAL;
  128. }
  129. mi->blk[mi->nr_blks].start = start;
  130. mi->blk[mi->nr_blks].end = end;
  131. mi->blk[mi->nr_blks].nid = nid;
  132. mi->nr_blks++;
  133. return 0;
  134. }
  135. /**
  136. * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
  137. * @idx: Index of memblk to remove
  138. * @mi: numa_meminfo to remove memblk from
  139. *
  140. * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
  141. * decrementing @mi->nr_blks.
  142. */
  143. void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
  144. {
  145. mi->nr_blks--;
  146. memmove(&mi->blk[idx], &mi->blk[idx + 1],
  147. (mi->nr_blks - idx) * sizeof(mi->blk[0]));
  148. }
  149. /**
  150. * numa_move_tail_memblk - Move a numa_memblk from one numa_meminfo to another
  151. * @dst: numa_meminfo to append block to
  152. * @idx: Index of memblk to remove
  153. * @src: numa_meminfo to remove memblk from
  154. */
  155. static void __init numa_move_tail_memblk(struct numa_meminfo *dst, int idx,
  156. struct numa_meminfo *src)
  157. {
  158. dst->blk[dst->nr_blks++] = src->blk[idx];
  159. numa_remove_memblk_from(idx, src);
  160. }
  161. /**
  162. * numa_add_memblk - Add one numa_memblk to numa_meminfo
  163. * @nid: NUMA node ID of the new memblk
  164. * @start: Start address of the new memblk
  165. * @end: End address of the new memblk
  166. *
  167. * Add a new memblk to the default numa_meminfo.
  168. *
  169. * RETURNS:
  170. * 0 on success, -errno on failure.
  171. */
  172. int __init numa_add_memblk(int nid, u64 start, u64 end)
  173. {
  174. return numa_add_memblk_to(nid, start, end, &numa_meminfo);
  175. }
  176. /**
  177. * numa_add_reserved_memblk - Add one numa_memblk to numa_reserved_meminfo
  178. * @nid: NUMA node ID of the new memblk
  179. * @start: Start address of the new memblk
  180. * @end: End address of the new memblk
  181. *
  182. * Add a new memblk to the numa_reserved_meminfo.
  183. *
  184. * Usage Case: numa_cleanup_meminfo() reconciles all numa_memblk instances
  185. * against memblock_type information and moves any that intersect reserved
  186. * ranges to numa_reserved_meminfo. However, when that information is known
  187. * ahead of time, we use numa_add_reserved_memblk() to add the numa_memblk
  188. * to numa_reserved_meminfo directly.
  189. *
  190. * RETURNS:
  191. * 0 on success, -errno on failure.
  192. */
  193. int __init numa_add_reserved_memblk(int nid, u64 start, u64 end)
  194. {
  195. return numa_add_memblk_to(nid, start, end, &numa_reserved_meminfo);
  196. }
  197. /**
  198. * numa_cleanup_meminfo - Cleanup a numa_meminfo
  199. * @mi: numa_meminfo to clean up
  200. *
  201. * Sanitize @mi by merging and removing unnecessary memblks. Also check for
  202. * conflicts and clear unused memblks.
  203. *
  204. * RETURNS:
  205. * 0 on success, -errno on failure.
  206. */
  207. int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
  208. {
  209. const u64 low = memblock_start_of_DRAM();
  210. const u64 high = memblock_end_of_DRAM();
  211. int i, j, k;
  212. /* first, trim all entries */
  213. for (i = 0; i < mi->nr_blks; i++) {
  214. struct numa_memblk *bi = &mi->blk[i];
  215. /* move / save reserved memory ranges */
  216. if (!memblock_overlaps_region(&memblock.memory,
  217. bi->start, bi->end - bi->start)) {
  218. numa_move_tail_memblk(&numa_reserved_meminfo, i--, mi);
  219. continue;
  220. }
  221. /* make sure all non-reserved blocks are inside the limits */
  222. bi->start = max(bi->start, low);
  223. /* preserve info for non-RAM areas above 'max_pfn': */
  224. if (bi->end > high) {
  225. numa_add_memblk_to(bi->nid, high, bi->end,
  226. &numa_reserved_meminfo);
  227. bi->end = high;
  228. }
  229. /* and there's no empty block */
  230. if (bi->start >= bi->end)
  231. numa_remove_memblk_from(i--, mi);
  232. }
  233. /* merge neighboring / overlapping entries */
  234. for (i = 0; i < mi->nr_blks; i++) {
  235. struct numa_memblk *bi = &mi->blk[i];
  236. for (j = i + 1; j < mi->nr_blks; j++) {
  237. struct numa_memblk *bj = &mi->blk[j];
  238. u64 start, end;
  239. /*
  240. * See whether there are overlapping blocks. Whine
  241. * about but allow overlaps of the same nid. They
  242. * will be merged below.
  243. */
  244. if (bi->end > bj->start && bi->start < bj->end) {
  245. if (bi->nid != bj->nid) {
  246. pr_err("node %d [mem %#010Lx-%#010Lx] overlaps with node %d [mem %#010Lx-%#010Lx]\n",
  247. bi->nid, bi->start, bi->end - 1,
  248. bj->nid, bj->start, bj->end - 1);
  249. return -EINVAL;
  250. }
  251. pr_warn("Warning: node %d [mem %#010Lx-%#010Lx] overlaps with itself [mem %#010Lx-%#010Lx]\n",
  252. bi->nid, bi->start, bi->end - 1,
  253. bj->start, bj->end - 1);
  254. }
  255. /*
  256. * Join together blocks on the same node, holes
  257. * between which don't overlap with memory on other
  258. * nodes.
  259. */
  260. if (bi->nid != bj->nid)
  261. continue;
  262. start = min(bi->start, bj->start);
  263. end = max(bi->end, bj->end);
  264. for (k = 0; k < mi->nr_blks; k++) {
  265. struct numa_memblk *bk = &mi->blk[k];
  266. if (bi->nid == bk->nid)
  267. continue;
  268. if (start < bk->end && end > bk->start)
  269. break;
  270. }
  271. if (k < mi->nr_blks)
  272. continue;
  273. pr_info("NUMA: Node %d [mem %#010Lx-%#010Lx] + [mem %#010Lx-%#010Lx] -> [mem %#010Lx-%#010Lx]\n",
  274. bi->nid, bi->start, bi->end - 1, bj->start,
  275. bj->end - 1, start, end - 1);
  276. bi->start = start;
  277. bi->end = end;
  278. numa_remove_memblk_from(j--, mi);
  279. }
  280. }
  281. /* clear unused ones */
  282. for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
  283. mi->blk[i].start = mi->blk[i].end = 0;
  284. mi->blk[i].nid = NUMA_NO_NODE;
  285. }
  286. return 0;
  287. }
  288. /*
  289. * Mark all currently memblock-reserved physical memory (which covers the
  290. * kernel's own memory ranges) as hot-unswappable.
  291. */
  292. static void __init numa_clear_kernel_node_hotplug(void)
  293. {
  294. nodemask_t reserved_nodemask = NODE_MASK_NONE;
  295. struct memblock_region *mb_region;
  296. int i;
  297. /*
  298. * We have to do some preprocessing of memblock regions, to
  299. * make them suitable for reservation.
  300. *
  301. * At this time, all memory regions reserved by memblock are
  302. * used by the kernel, but those regions are not split up
  303. * along node boundaries yet, and don't necessarily have their
  304. * node ID set yet either.
  305. *
  306. * So iterate over all parsed memory blocks and use those ranges to
  307. * set the nid in memblock.reserved. This will split up the
  308. * memblock regions along node boundaries and will set the node IDs
  309. * as well.
  310. */
  311. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  312. struct numa_memblk *mb = numa_meminfo.blk + i;
  313. int ret;
  314. ret = memblock_set_node(mb->start, mb->end - mb->start,
  315. &memblock.reserved, mb->nid);
  316. WARN_ON_ONCE(ret);
  317. }
  318. /*
  319. * Now go over all reserved memblock regions, to construct a
  320. * node mask of all kernel reserved memory areas.
  321. *
  322. * [ Note, when booting with mem=nn[kMG] or in a kdump kernel,
  323. * numa_meminfo might not include all memblock.reserved
  324. * memory ranges, because quirks such as trim_snb_memory()
  325. * reserve specific pages for Sandy Bridge graphics. ]
  326. */
  327. for_each_reserved_mem_region(mb_region) {
  328. int nid = memblock_get_region_node(mb_region);
  329. if (numa_valid_node(nid))
  330. node_set(nid, reserved_nodemask);
  331. }
  332. /*
  333. * Finally, clear the MEMBLOCK_HOTPLUG flag for all memory
  334. * belonging to the reserved node mask.
  335. *
  336. * Note that this will include memory regions that reside
  337. * on nodes that contain kernel memory - entire nodes
  338. * become hot-unpluggable:
  339. */
  340. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  341. struct numa_memblk *mb = numa_meminfo.blk + i;
  342. if (!node_isset(mb->nid, reserved_nodemask))
  343. continue;
  344. memblock_clear_hotplug(mb->start, mb->end - mb->start);
  345. }
  346. }
  347. static int __init numa_register_meminfo(struct numa_meminfo *mi)
  348. {
  349. int i;
  350. /* Account for nodes with cpus and no memory */
  351. node_possible_map = numa_nodes_parsed;
  352. numa_nodemask_from_meminfo(&node_possible_map, mi);
  353. if (WARN_ON(nodes_empty(node_possible_map)))
  354. return -EINVAL;
  355. for (i = 0; i < mi->nr_blks; i++) {
  356. struct numa_memblk *mb = &mi->blk[i];
  357. memblock_set_node(mb->start, mb->end - mb->start,
  358. &memblock.memory, mb->nid);
  359. }
  360. /*
  361. * At very early time, the kernel have to use some memory such as
  362. * loading the kernel image. We cannot prevent this anyway. So any
  363. * node the kernel resides in should be un-hotpluggable.
  364. *
  365. * And when we come here, alloc node data won't fail.
  366. */
  367. numa_clear_kernel_node_hotplug();
  368. /*
  369. * If sections array is gonna be used for pfn -> nid mapping, check
  370. * whether its granularity is fine enough.
  371. */
  372. if (IS_ENABLED(NODE_NOT_IN_PAGE_FLAGS)) {
  373. unsigned long pfn_align = node_map_pfn_alignment();
  374. if (pfn_align && pfn_align < PAGES_PER_SECTION) {
  375. unsigned long node_align_mb = PFN_PHYS(pfn_align) / SZ_1M;
  376. unsigned long sect_align_mb = PFN_PHYS(PAGES_PER_SECTION) / SZ_1M;
  377. pr_warn("Node alignment %luMB < min %luMB, rejecting NUMA config\n",
  378. node_align_mb, sect_align_mb);
  379. return -EINVAL;
  380. }
  381. }
  382. return 0;
  383. }
  384. int __init numa_memblks_init(int (*init_func)(void),
  385. bool memblock_force_top_down)
  386. {
  387. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  388. int ret;
  389. nodes_clear(numa_nodes_parsed);
  390. nodes_clear(node_possible_map);
  391. nodes_clear(node_online_map);
  392. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  393. WARN_ON(memblock_set_node(0, max_addr, &memblock.memory, NUMA_NO_NODE));
  394. WARN_ON(memblock_set_node(0, max_addr, &memblock.reserved,
  395. NUMA_NO_NODE));
  396. /* In case that parsing SRAT failed. */
  397. WARN_ON(memblock_clear_hotplug(0, max_addr));
  398. numa_reset_distance();
  399. ret = init_func();
  400. if (ret < 0)
  401. return ret;
  402. /*
  403. * We reset memblock back to the top-down direction
  404. * here because if we configured ACPI_NUMA, we have
  405. * parsed SRAT in init_func(). It is ok to have the
  406. * reset here even if we didn't configure ACPI_NUMA
  407. * or acpi numa init fails and fallbacks to dummy
  408. * numa init.
  409. */
  410. if (memblock_force_top_down)
  411. memblock_set_bottom_up(false);
  412. ret = numa_cleanup_meminfo(&numa_meminfo);
  413. if (ret < 0)
  414. return ret;
  415. numa_emulation(&numa_meminfo, numa_distance_cnt);
  416. return numa_register_meminfo(&numa_meminfo);
  417. }
  418. static int __init cmp_memblk(const void *a, const void *b)
  419. {
  420. const struct numa_memblk *ma = *(const struct numa_memblk **)a;
  421. const struct numa_memblk *mb = *(const struct numa_memblk **)b;
  422. return (ma->start > mb->start) - (ma->start < mb->start);
  423. }
  424. static struct numa_memblk *numa_memblk_list[NR_NODE_MEMBLKS] __initdata;
  425. /**
  426. * numa_fill_memblks - Fill gaps in numa_meminfo memblks
  427. * @start: address to begin fill
  428. * @end: address to end fill
  429. *
  430. * Find and extend numa_meminfo memblks to cover the physical
  431. * address range @start-@end
  432. *
  433. * RETURNS:
  434. * 0 : Success
  435. * NUMA_NO_MEMBLK : No memblks exist in address range @start-@end
  436. */
  437. int __init numa_fill_memblks(u64 start, u64 end)
  438. {
  439. struct numa_memblk **blk = &numa_memblk_list[0];
  440. struct numa_meminfo *mi = &numa_meminfo;
  441. int count = 0;
  442. u64 prev_end;
  443. /*
  444. * Create a list of pointers to numa_meminfo memblks that
  445. * overlap start, end. The list is used to make in-place
  446. * changes that fill out the numa_meminfo memblks.
  447. */
  448. for (int i = 0; i < mi->nr_blks; i++) {
  449. struct numa_memblk *bi = &mi->blk[i];
  450. if (memblock_addrs_overlap(start, end - start, bi->start,
  451. bi->end - bi->start)) {
  452. blk[count] = &mi->blk[i];
  453. count++;
  454. }
  455. }
  456. if (!count)
  457. return NUMA_NO_MEMBLK;
  458. /* Sort the list of pointers in memblk->start order */
  459. sort(&blk[0], count, sizeof(blk[0]), cmp_memblk, NULL);
  460. /* Make sure the first/last memblks include start/end */
  461. blk[0]->start = min(blk[0]->start, start);
  462. blk[count - 1]->end = max(blk[count - 1]->end, end);
  463. /*
  464. * Fill any gaps by tracking the previous memblks
  465. * end address and backfilling to it if needed.
  466. */
  467. prev_end = blk[0]->end;
  468. for (int i = 1; i < count; i++) {
  469. struct numa_memblk *curr = blk[i];
  470. if (prev_end >= curr->start) {
  471. if (prev_end < curr->end)
  472. prev_end = curr->end;
  473. } else {
  474. curr->start = prev_end;
  475. prev_end = curr->end;
  476. }
  477. }
  478. return 0;
  479. }
  480. #ifdef CONFIG_NUMA_KEEP_MEMINFO
  481. static int meminfo_to_nid(struct numa_meminfo *mi, u64 start)
  482. {
  483. int i;
  484. for (i = 0; i < mi->nr_blks; i++)
  485. if (mi->blk[i].start <= start && mi->blk[i].end > start)
  486. return mi->blk[i].nid;
  487. return NUMA_NO_NODE;
  488. }
  489. int phys_to_target_node(u64 start)
  490. {
  491. int nid = meminfo_to_nid(&numa_meminfo, start);
  492. int reserved_nid = meminfo_to_nid(&numa_reserved_meminfo, start);
  493. /*
  494. * Prefer online nodes unless the address is also described
  495. * by reserved ranges, in which case use the reserved nid.
  496. */
  497. if (nid != NUMA_NO_NODE && reserved_nid == NUMA_NO_NODE)
  498. return nid;
  499. return reserved_nid;
  500. }
  501. EXPORT_SYMBOL_GPL(phys_to_target_node);
  502. int memory_add_physaddr_to_nid(u64 start)
  503. {
  504. int nid = meminfo_to_nid(&numa_meminfo, start);
  505. if (nid == NUMA_NO_NODE)
  506. nid = numa_meminfo.blk[0].nid;
  507. return nid;
  508. }
  509. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  510. #endif /* CONFIG_NUMA_KEEP_MEMINFO */