numa_memblks.c 15 KB

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