numa.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Common code for 32 and 64-bit NUMA */
  3. #include <linux/acpi.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/init.h>
  8. #include <linux/memblock.h>
  9. #include <linux/mmzone.h>
  10. #include <linux/ctype.h>
  11. #include <linux/nodemask.h>
  12. #include <linux/sched.h>
  13. #include <linux/topology.h>
  14. #include <linux/sort.h>
  15. #include <asm/e820/api.h>
  16. #include <asm/proto.h>
  17. #include <asm/dma.h>
  18. #include <asm/amd_nb.h>
  19. #include "numa_internal.h"
  20. int numa_off;
  21. nodemask_t numa_nodes_parsed __initdata;
  22. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  23. EXPORT_SYMBOL(node_data);
  24. static struct numa_meminfo numa_meminfo __initdata_or_meminfo;
  25. static struct numa_meminfo numa_reserved_meminfo __initdata_or_meminfo;
  26. static int numa_distance_cnt;
  27. static u8 *numa_distance;
  28. static __init int numa_setup(char *opt)
  29. {
  30. if (!opt)
  31. return -EINVAL;
  32. if (!strncmp(opt, "off", 3))
  33. numa_off = 1;
  34. if (!strncmp(opt, "fake=", 5))
  35. return numa_emu_cmdline(opt + 5);
  36. if (!strncmp(opt, "noacpi", 6))
  37. disable_srat();
  38. if (!strncmp(opt, "nohmat", 6))
  39. disable_hmat();
  40. return 0;
  41. }
  42. early_param("numa", numa_setup);
  43. /*
  44. * apicid, cpu, node mappings
  45. */
  46. s16 __apicid_to_node[MAX_LOCAL_APIC] = {
  47. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  48. };
  49. int numa_cpu_node(int cpu)
  50. {
  51. int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  52. if (apicid != BAD_APICID)
  53. return __apicid_to_node[apicid];
  54. return NUMA_NO_NODE;
  55. }
  56. cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
  57. EXPORT_SYMBOL(node_to_cpumask_map);
  58. /*
  59. * Map cpu index to node index
  60. */
  61. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  62. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  63. void numa_set_node(int cpu, int node)
  64. {
  65. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  66. /* early setting, no percpu area yet */
  67. if (cpu_to_node_map) {
  68. cpu_to_node_map[cpu] = node;
  69. return;
  70. }
  71. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  72. if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
  73. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  74. dump_stack();
  75. return;
  76. }
  77. #endif
  78. per_cpu(x86_cpu_to_node_map, cpu) = node;
  79. set_cpu_numa_node(cpu, node);
  80. }
  81. void numa_clear_node(int cpu)
  82. {
  83. numa_set_node(cpu, NUMA_NO_NODE);
  84. }
  85. /*
  86. * Allocate node_to_cpumask_map based on number of available nodes
  87. * Requires node_possible_map to be valid.
  88. *
  89. * Note: cpumask_of_node() is not valid until after this is done.
  90. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  91. */
  92. void __init setup_node_to_cpumask_map(void)
  93. {
  94. unsigned int node;
  95. /* setup nr_node_ids if not done yet */
  96. if (nr_node_ids == MAX_NUMNODES)
  97. setup_nr_node_ids();
  98. /* allocate the map */
  99. for (node = 0; node < nr_node_ids; node++)
  100. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  101. /* cpumask_of_node() will now work */
  102. pr_debug("Node to cpumask map for %u nodes\n", nr_node_ids);
  103. }
  104. static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
  105. struct numa_meminfo *mi)
  106. {
  107. /* ignore zero length blks */
  108. if (start == end)
  109. return 0;
  110. /* whine about and ignore invalid blks */
  111. if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
  112. pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  113. nid, start, end - 1);
  114. return 0;
  115. }
  116. if (mi->nr_blks >= NR_NODE_MEMBLKS) {
  117. pr_err("too many memblk ranges\n");
  118. return -EINVAL;
  119. }
  120. mi->blk[mi->nr_blks].start = start;
  121. mi->blk[mi->nr_blks].end = end;
  122. mi->blk[mi->nr_blks].nid = nid;
  123. mi->nr_blks++;
  124. return 0;
  125. }
  126. /**
  127. * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
  128. * @idx: Index of memblk to remove
  129. * @mi: numa_meminfo to remove memblk from
  130. *
  131. * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
  132. * decrementing @mi->nr_blks.
  133. */
  134. void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
  135. {
  136. mi->nr_blks--;
  137. memmove(&mi->blk[idx], &mi->blk[idx + 1],
  138. (mi->nr_blks - idx) * sizeof(mi->blk[0]));
  139. }
  140. /**
  141. * numa_move_tail_memblk - Move a numa_memblk from one numa_meminfo to another
  142. * @dst: numa_meminfo to append block to
  143. * @idx: Index of memblk to remove
  144. * @src: numa_meminfo to remove memblk from
  145. */
  146. static void __init numa_move_tail_memblk(struct numa_meminfo *dst, int idx,
  147. struct numa_meminfo *src)
  148. {
  149. dst->blk[dst->nr_blks++] = src->blk[idx];
  150. numa_remove_memblk_from(idx, src);
  151. }
  152. /**
  153. * numa_add_memblk - Add one numa_memblk to numa_meminfo
  154. * @nid: NUMA node ID of the new memblk
  155. * @start: Start address of the new memblk
  156. * @end: End address of the new memblk
  157. *
  158. * Add a new memblk to the default numa_meminfo.
  159. *
  160. * RETURNS:
  161. * 0 on success, -errno on failure.
  162. */
  163. int __init numa_add_memblk(int nid, u64 start, u64 end)
  164. {
  165. return numa_add_memblk_to(nid, start, end, &numa_meminfo);
  166. }
  167. /* Allocate NODE_DATA for a node on the local memory */
  168. static void __init alloc_node_data(int nid)
  169. {
  170. const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
  171. u64 nd_pa;
  172. void *nd;
  173. int tnid;
  174. /*
  175. * Allocate node data. Try node-local memory and then any node.
  176. * Never allocate in DMA zone.
  177. */
  178. nd_pa = memblock_phys_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
  179. if (!nd_pa) {
  180. pr_err("Cannot find %zu bytes in any node (initial node: %d)\n",
  181. nd_size, nid);
  182. return;
  183. }
  184. nd = __va(nd_pa);
  185. /* report and initialize */
  186. printk(KERN_INFO "NODE_DATA(%d) allocated [mem %#010Lx-%#010Lx]\n", nid,
  187. nd_pa, nd_pa + nd_size - 1);
  188. tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
  189. if (tnid != nid)
  190. printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid);
  191. node_data[nid] = nd;
  192. memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
  193. node_set_online(nid);
  194. }
  195. /**
  196. * numa_cleanup_meminfo - Cleanup a numa_meminfo
  197. * @mi: numa_meminfo to clean up
  198. *
  199. * Sanitize @mi by merging and removing unnecessary memblks. Also check for
  200. * conflicts and clear unused memblks.
  201. *
  202. * RETURNS:
  203. * 0 on success, -errno on failure.
  204. */
  205. int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
  206. {
  207. const u64 low = 0;
  208. const u64 high = PFN_PHYS(max_pfn);
  209. int i, j, k;
  210. /* first, trim all entries */
  211. for (i = 0; i < mi->nr_blks; i++) {
  212. struct numa_memblk *bi = &mi->blk[i];
  213. /* move / save reserved memory ranges */
  214. if (!memblock_overlaps_region(&memblock.memory,
  215. bi->start, bi->end - bi->start)) {
  216. numa_move_tail_memblk(&numa_reserved_meminfo, i--, mi);
  217. continue;
  218. }
  219. /* make sure all non-reserved blocks are inside the limits */
  220. bi->start = max(bi->start, low);
  221. /* preserve info for non-RAM areas above 'max_pfn': */
  222. if (bi->end > high) {
  223. numa_add_memblk_to(bi->nid, high, bi->end,
  224. &numa_reserved_meminfo);
  225. bi->end = high;
  226. }
  227. /* and there's no empty block */
  228. if (bi->start >= bi->end)
  229. numa_remove_memblk_from(i--, mi);
  230. }
  231. /* merge neighboring / overlapping entries */
  232. for (i = 0; i < mi->nr_blks; i++) {
  233. struct numa_memblk *bi = &mi->blk[i];
  234. for (j = i + 1; j < mi->nr_blks; j++) {
  235. struct numa_memblk *bj = &mi->blk[j];
  236. u64 start, end;
  237. /*
  238. * See whether there are overlapping blocks. Whine
  239. * about but allow overlaps of the same nid. They
  240. * will be merged below.
  241. */
  242. if (bi->end > bj->start && bi->start < bj->end) {
  243. if (bi->nid != bj->nid) {
  244. pr_err("node %d [mem %#010Lx-%#010Lx] overlaps with node %d [mem %#010Lx-%#010Lx]\n",
  245. bi->nid, bi->start, bi->end - 1,
  246. bj->nid, bj->start, bj->end - 1);
  247. return -EINVAL;
  248. }
  249. pr_warn("Warning: node %d [mem %#010Lx-%#010Lx] overlaps with itself [mem %#010Lx-%#010Lx]\n",
  250. bi->nid, bi->start, bi->end - 1,
  251. bj->start, bj->end - 1);
  252. }
  253. /*
  254. * Join together blocks on the same node, holes
  255. * between which don't overlap with memory on other
  256. * nodes.
  257. */
  258. if (bi->nid != bj->nid)
  259. continue;
  260. start = min(bi->start, bj->start);
  261. end = max(bi->end, bj->end);
  262. for (k = 0; k < mi->nr_blks; k++) {
  263. struct numa_memblk *bk = &mi->blk[k];
  264. if (bi->nid == bk->nid)
  265. continue;
  266. if (start < bk->end && end > bk->start)
  267. break;
  268. }
  269. if (k < mi->nr_blks)
  270. continue;
  271. printk(KERN_INFO "NUMA: Node %d [mem %#010Lx-%#010Lx] + [mem %#010Lx-%#010Lx] -> [mem %#010Lx-%#010Lx]\n",
  272. bi->nid, bi->start, bi->end - 1, bj->start,
  273. bj->end - 1, start, end - 1);
  274. bi->start = start;
  275. bi->end = end;
  276. numa_remove_memblk_from(j--, mi);
  277. }
  278. }
  279. /* clear unused ones */
  280. for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
  281. mi->blk[i].start = mi->blk[i].end = 0;
  282. mi->blk[i].nid = NUMA_NO_NODE;
  283. }
  284. return 0;
  285. }
  286. /*
  287. * Set nodes, which have memory in @mi, in *@nodemask.
  288. */
  289. static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
  290. const struct numa_meminfo *mi)
  291. {
  292. int i;
  293. for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
  294. if (mi->blk[i].start != mi->blk[i].end &&
  295. mi->blk[i].nid != NUMA_NO_NODE)
  296. node_set(mi->blk[i].nid, *nodemask);
  297. }
  298. /**
  299. * numa_reset_distance - Reset NUMA distance table
  300. *
  301. * The current table is freed. The next numa_set_distance() call will
  302. * create a new one.
  303. */
  304. void __init numa_reset_distance(void)
  305. {
  306. size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
  307. /* numa_distance could be 1LU marking allocation failure, test cnt */
  308. if (numa_distance_cnt)
  309. memblock_free(numa_distance, size);
  310. numa_distance_cnt = 0;
  311. numa_distance = NULL; /* enable table creation */
  312. }
  313. static int __init numa_alloc_distance(void)
  314. {
  315. nodemask_t nodes_parsed;
  316. size_t size;
  317. int i, j, cnt = 0;
  318. u64 phys;
  319. /* size the new table and allocate it */
  320. nodes_parsed = numa_nodes_parsed;
  321. numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
  322. for_each_node_mask(i, nodes_parsed)
  323. cnt = i;
  324. cnt++;
  325. size = cnt * cnt * sizeof(numa_distance[0]);
  326. phys = memblock_phys_alloc_range(size, PAGE_SIZE, 0,
  327. PFN_PHYS(max_pfn_mapped));
  328. if (!phys) {
  329. pr_warn("Warning: can't allocate distance table!\n");
  330. /* don't retry until explicitly reset */
  331. numa_distance = (void *)1LU;
  332. return -ENOMEM;
  333. }
  334. numa_distance = __va(phys);
  335. numa_distance_cnt = cnt;
  336. /* fill with the default distances */
  337. for (i = 0; i < cnt; i++)
  338. for (j = 0; j < cnt; j++)
  339. numa_distance[i * cnt + j] = i == j ?
  340. LOCAL_DISTANCE : REMOTE_DISTANCE;
  341. printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
  342. return 0;
  343. }
  344. /**
  345. * numa_set_distance - Set NUMA distance from one NUMA to another
  346. * @from: the 'from' node to set distance
  347. * @to: the 'to' node to set distance
  348. * @distance: NUMA distance
  349. *
  350. * Set the distance from node @from to @to to @distance. If distance table
  351. * doesn't exist, one which is large enough to accommodate all the currently
  352. * known nodes will be created.
  353. *
  354. * If such table cannot be allocated, a warning is printed and further
  355. * calls are ignored until the distance table is reset with
  356. * numa_reset_distance().
  357. *
  358. * If @from or @to is higher than the highest known node or lower than zero
  359. * at the time of table creation or @distance doesn't make sense, the call
  360. * is ignored.
  361. * This is to allow simplification of specific NUMA config implementations.
  362. */
  363. void __init numa_set_distance(int from, int to, int distance)
  364. {
  365. if (!numa_distance && numa_alloc_distance() < 0)
  366. return;
  367. if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
  368. from < 0 || to < 0) {
  369. pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
  370. from, to, distance);
  371. return;
  372. }
  373. if ((u8)distance != distance ||
  374. (from == to && distance != LOCAL_DISTANCE)) {
  375. pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  376. from, to, distance);
  377. return;
  378. }
  379. numa_distance[from * numa_distance_cnt + to] = distance;
  380. }
  381. int __node_distance(int from, int to)
  382. {
  383. if (from >= numa_distance_cnt || to >= numa_distance_cnt)
  384. return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
  385. return numa_distance[from * numa_distance_cnt + to];
  386. }
  387. EXPORT_SYMBOL(__node_distance);
  388. /*
  389. * Sanity check to catch more bad NUMA configurations (they are amazingly
  390. * common). Make sure the nodes cover all memory.
  391. */
  392. static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
  393. {
  394. u64 numaram, e820ram;
  395. int i;
  396. numaram = 0;
  397. for (i = 0; i < mi->nr_blks; i++) {
  398. u64 s = mi->blk[i].start >> PAGE_SHIFT;
  399. u64 e = mi->blk[i].end >> PAGE_SHIFT;
  400. numaram += e - s;
  401. numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
  402. if ((s64)numaram < 0)
  403. numaram = 0;
  404. }
  405. e820ram = max_pfn - absent_pages_in_range(0, max_pfn);
  406. /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
  407. if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
  408. printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB e820 RAM. Not used.\n",
  409. (numaram << PAGE_SHIFT) >> 20,
  410. (e820ram << PAGE_SHIFT) >> 20);
  411. return false;
  412. }
  413. return true;
  414. }
  415. /*
  416. * Mark all currently memblock-reserved physical memory (which covers the
  417. * kernel's own memory ranges) as hot-unswappable.
  418. */
  419. static void __init numa_clear_kernel_node_hotplug(void)
  420. {
  421. nodemask_t reserved_nodemask = NODE_MASK_NONE;
  422. struct memblock_region *mb_region;
  423. int i;
  424. /*
  425. * We have to do some preprocessing of memblock regions, to
  426. * make them suitable for reservation.
  427. *
  428. * At this time, all memory regions reserved by memblock are
  429. * used by the kernel, but those regions are not split up
  430. * along node boundaries yet, and don't necessarily have their
  431. * node ID set yet either.
  432. *
  433. * So iterate over all memory known to the x86 architecture,
  434. * and use those ranges to set the nid in memblock.reserved.
  435. * This will split up the memblock regions along node
  436. * boundaries and will set the node IDs as well.
  437. */
  438. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  439. struct numa_memblk *mb = numa_meminfo.blk + i;
  440. int ret;
  441. ret = memblock_set_node(mb->start, mb->end - mb->start, &memblock.reserved, mb->nid);
  442. WARN_ON_ONCE(ret);
  443. }
  444. /*
  445. * Now go over all reserved memblock regions, to construct a
  446. * node mask of all kernel reserved memory areas.
  447. *
  448. * [ Note, when booting with mem=nn[kMG] or in a kdump kernel,
  449. * numa_meminfo might not include all memblock.reserved
  450. * memory ranges, because quirks such as trim_snb_memory()
  451. * reserve specific pages for Sandy Bridge graphics. ]
  452. */
  453. for_each_reserved_mem_region(mb_region) {
  454. int nid = memblock_get_region_node(mb_region);
  455. if (nid != MAX_NUMNODES)
  456. node_set(nid, reserved_nodemask);
  457. }
  458. /*
  459. * Finally, clear the MEMBLOCK_HOTPLUG flag for all memory
  460. * belonging to the reserved node mask.
  461. *
  462. * Note that this will include memory regions that reside
  463. * on nodes that contain kernel memory - entire nodes
  464. * become hot-unpluggable:
  465. */
  466. for (i = 0; i < numa_meminfo.nr_blks; i++) {
  467. struct numa_memblk *mb = numa_meminfo.blk + i;
  468. if (!node_isset(mb->nid, reserved_nodemask))
  469. continue;
  470. memblock_clear_hotplug(mb->start, mb->end - mb->start);
  471. }
  472. }
  473. static int __init numa_register_memblks(struct numa_meminfo *mi)
  474. {
  475. int i, nid;
  476. /* Account for nodes with cpus and no memory */
  477. node_possible_map = numa_nodes_parsed;
  478. numa_nodemask_from_meminfo(&node_possible_map, mi);
  479. if (WARN_ON(nodes_empty(node_possible_map)))
  480. return -EINVAL;
  481. for (i = 0; i < mi->nr_blks; i++) {
  482. struct numa_memblk *mb = &mi->blk[i];
  483. memblock_set_node(mb->start, mb->end - mb->start,
  484. &memblock.memory, mb->nid);
  485. }
  486. /*
  487. * At very early time, the kernel have to use some memory such as
  488. * loading the kernel image. We cannot prevent this anyway. So any
  489. * node the kernel resides in should be un-hotpluggable.
  490. *
  491. * And when we come here, alloc node data won't fail.
  492. */
  493. numa_clear_kernel_node_hotplug();
  494. /*
  495. * If sections array is gonna be used for pfn -> nid mapping, check
  496. * whether its granularity is fine enough.
  497. */
  498. if (IS_ENABLED(NODE_NOT_IN_PAGE_FLAGS)) {
  499. unsigned long pfn_align = node_map_pfn_alignment();
  500. if (pfn_align && pfn_align < PAGES_PER_SECTION) {
  501. pr_warn("Node alignment %LuMB < min %LuMB, rejecting NUMA config\n",
  502. PFN_PHYS(pfn_align) >> 20,
  503. PFN_PHYS(PAGES_PER_SECTION) >> 20);
  504. return -EINVAL;
  505. }
  506. }
  507. if (!numa_meminfo_cover_memory(mi))
  508. return -EINVAL;
  509. /* Finally register nodes. */
  510. for_each_node_mask(nid, node_possible_map) {
  511. u64 start = PFN_PHYS(max_pfn);
  512. u64 end = 0;
  513. for (i = 0; i < mi->nr_blks; i++) {
  514. if (nid != mi->blk[i].nid)
  515. continue;
  516. start = min(mi->blk[i].start, start);
  517. end = max(mi->blk[i].end, end);
  518. }
  519. if (start >= end)
  520. continue;
  521. alloc_node_data(nid);
  522. }
  523. /* Dump memblock with node info and return. */
  524. memblock_dump_all();
  525. return 0;
  526. }
  527. /*
  528. * There are unfortunately some poorly designed mainboards around that
  529. * only connect memory to a single CPU. This breaks the 1:1 cpu->node
  530. * mapping. To avoid this fill in the mapping for all possible CPUs,
  531. * as the number of CPUs is not known yet. We round robin the existing
  532. * nodes.
  533. */
  534. static void __init numa_init_array(void)
  535. {
  536. int rr, i;
  537. rr = first_node(node_online_map);
  538. for (i = 0; i < nr_cpu_ids; i++) {
  539. if (early_cpu_to_node(i) != NUMA_NO_NODE)
  540. continue;
  541. numa_set_node(i, rr);
  542. rr = next_node_in(rr, node_online_map);
  543. }
  544. }
  545. static int __init numa_init(int (*init_func)(void))
  546. {
  547. int i;
  548. int ret;
  549. for (i = 0; i < MAX_LOCAL_APIC; i++)
  550. set_apicid_to_node(i, NUMA_NO_NODE);
  551. nodes_clear(numa_nodes_parsed);
  552. nodes_clear(node_possible_map);
  553. nodes_clear(node_online_map);
  554. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  555. WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.memory,
  556. MAX_NUMNODES));
  557. WARN_ON(memblock_set_node(0, ULLONG_MAX, &memblock.reserved,
  558. MAX_NUMNODES));
  559. /* In case that parsing SRAT failed. */
  560. WARN_ON(memblock_clear_hotplug(0, ULLONG_MAX));
  561. numa_reset_distance();
  562. ret = init_func();
  563. if (ret < 0)
  564. return ret;
  565. /*
  566. * We reset memblock back to the top-down direction
  567. * here because if we configured ACPI_NUMA, we have
  568. * parsed SRAT in init_func(). It is ok to have the
  569. * reset here even if we did't configure ACPI_NUMA
  570. * or acpi numa init fails and fallbacks to dummy
  571. * numa init.
  572. */
  573. memblock_set_bottom_up(false);
  574. ret = numa_cleanup_meminfo(&numa_meminfo);
  575. if (ret < 0)
  576. return ret;
  577. numa_emulation(&numa_meminfo, numa_distance_cnt);
  578. ret = numa_register_memblks(&numa_meminfo);
  579. if (ret < 0)
  580. return ret;
  581. for (i = 0; i < nr_cpu_ids; i++) {
  582. int nid = early_cpu_to_node(i);
  583. if (nid == NUMA_NO_NODE)
  584. continue;
  585. if (!node_online(nid))
  586. numa_clear_node(i);
  587. }
  588. numa_init_array();
  589. return 0;
  590. }
  591. /**
  592. * dummy_numa_init - Fallback dummy NUMA init
  593. *
  594. * Used if there's no underlying NUMA architecture, NUMA initialization
  595. * fails, or NUMA is disabled on the command line.
  596. *
  597. * Must online at least one node and add memory blocks that cover all
  598. * allowed memory. This function must not fail.
  599. */
  600. static int __init dummy_numa_init(void)
  601. {
  602. printk(KERN_INFO "%s\n",
  603. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  604. printk(KERN_INFO "Faking a node at [mem %#018Lx-%#018Lx]\n",
  605. 0LLU, PFN_PHYS(max_pfn) - 1);
  606. node_set(0, numa_nodes_parsed);
  607. numa_add_memblk(0, 0, PFN_PHYS(max_pfn));
  608. return 0;
  609. }
  610. /**
  611. * x86_numa_init - Initialize NUMA
  612. *
  613. * Try each configured NUMA initialization method until one succeeds. The
  614. * last fallback is dummy single node config encompassing whole memory and
  615. * never fails.
  616. */
  617. void __init x86_numa_init(void)
  618. {
  619. if (!numa_off) {
  620. #ifdef CONFIG_ACPI_NUMA
  621. if (!numa_init(x86_acpi_numa_init))
  622. return;
  623. #endif
  624. #ifdef CONFIG_AMD_NUMA
  625. if (!numa_init(amd_numa_init))
  626. return;
  627. #endif
  628. }
  629. numa_init(dummy_numa_init);
  630. }
  631. /*
  632. * A node may exist which has one or more Generic Initiators but no CPUs and no
  633. * memory.
  634. *
  635. * This function must be called after init_cpu_to_node(), to ensure that any
  636. * memoryless CPU nodes have already been brought online, and before the
  637. * node_data[nid] is needed for zone list setup in build_all_zonelists().
  638. *
  639. * When this function is called, any nodes containing either memory and/or CPUs
  640. * will already be online and there is no need to do anything extra, even if
  641. * they also contain one or more Generic Initiators.
  642. */
  643. void __init init_gi_nodes(void)
  644. {
  645. int nid;
  646. /*
  647. * Exclude this node from
  648. * bringup_nonboot_cpus
  649. * cpu_up
  650. * __try_online_node
  651. * register_one_node
  652. * because node_subsys is not initialized yet.
  653. * TODO remove dependency on node_online
  654. */
  655. for_each_node_state(nid, N_GENERIC_INITIATOR)
  656. if (!node_online(nid))
  657. node_set_online(nid);
  658. }
  659. /*
  660. * Setup early cpu_to_node.
  661. *
  662. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  663. * and apicid_to_node[] tables have valid entries for a CPU.
  664. * This means we skip cpu_to_node[] initialisation for NUMA
  665. * emulation and faking node case (when running a kernel compiled
  666. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  667. * is already initialized in a round robin manner at numa_init_array,
  668. * prior to this call, and this initialization is good enough
  669. * for the fake NUMA cases.
  670. *
  671. * Called before the per_cpu areas are setup.
  672. */
  673. void __init init_cpu_to_node(void)
  674. {
  675. int cpu;
  676. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  677. BUG_ON(cpu_to_apicid == NULL);
  678. for_each_possible_cpu(cpu) {
  679. int node = numa_cpu_node(cpu);
  680. if (node == NUMA_NO_NODE)
  681. continue;
  682. /*
  683. * Exclude this node from
  684. * bringup_nonboot_cpus
  685. * cpu_up
  686. * __try_online_node
  687. * register_one_node
  688. * because node_subsys is not initialized yet.
  689. * TODO remove dependency on node_online
  690. */
  691. if (!node_online(node))
  692. node_set_online(node);
  693. numa_set_node(cpu, node);
  694. }
  695. }
  696. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  697. # ifndef CONFIG_NUMA_EMU
  698. void numa_add_cpu(int cpu)
  699. {
  700. cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  701. }
  702. void numa_remove_cpu(int cpu)
  703. {
  704. cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  705. }
  706. # endif /* !CONFIG_NUMA_EMU */
  707. #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  708. int __cpu_to_node(int cpu)
  709. {
  710. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  711. printk(KERN_WARNING
  712. "cpu_to_node(%d): usage too early!\n", cpu);
  713. dump_stack();
  714. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  715. }
  716. return per_cpu(x86_cpu_to_node_map, cpu);
  717. }
  718. EXPORT_SYMBOL(__cpu_to_node);
  719. /*
  720. * Same function as cpu_to_node() but used if called before the
  721. * per_cpu areas are setup.
  722. */
  723. int early_cpu_to_node(int cpu)
  724. {
  725. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  726. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  727. if (!cpu_possible(cpu)) {
  728. printk(KERN_WARNING
  729. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  730. dump_stack();
  731. return NUMA_NO_NODE;
  732. }
  733. return per_cpu(x86_cpu_to_node_map, cpu);
  734. }
  735. void debug_cpumask_set_cpu(int cpu, int node, bool enable)
  736. {
  737. struct cpumask *mask;
  738. if (node == NUMA_NO_NODE) {
  739. /* early_cpu_to_node() already emits a warning and trace */
  740. return;
  741. }
  742. mask = node_to_cpumask_map[node];
  743. if (!cpumask_available(mask)) {
  744. pr_err("node_to_cpumask_map[%i] NULL\n", node);
  745. dump_stack();
  746. return;
  747. }
  748. if (enable)
  749. cpumask_set_cpu(cpu, mask);
  750. else
  751. cpumask_clear_cpu(cpu, mask);
  752. printk(KERN_DEBUG "%s cpu %d node %d: mask now %*pbl\n",
  753. enable ? "numa_add_cpu" : "numa_remove_cpu",
  754. cpu, node, cpumask_pr_args(mask));
  755. return;
  756. }
  757. # ifndef CONFIG_NUMA_EMU
  758. static void numa_set_cpumask(int cpu, bool enable)
  759. {
  760. debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable);
  761. }
  762. void numa_add_cpu(int cpu)
  763. {
  764. numa_set_cpumask(cpu, true);
  765. }
  766. void numa_remove_cpu(int cpu)
  767. {
  768. numa_set_cpumask(cpu, false);
  769. }
  770. # endif /* !CONFIG_NUMA_EMU */
  771. /*
  772. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  773. */
  774. const struct cpumask *cpumask_of_node(int node)
  775. {
  776. if ((unsigned)node >= nr_node_ids) {
  777. printk(KERN_WARNING
  778. "cpumask_of_node(%d): (unsigned)node >= nr_node_ids(%u)\n",
  779. node, nr_node_ids);
  780. dump_stack();
  781. return cpu_none_mask;
  782. }
  783. if (!cpumask_available(node_to_cpumask_map[node])) {
  784. printk(KERN_WARNING
  785. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  786. node);
  787. dump_stack();
  788. return cpu_online_mask;
  789. }
  790. return node_to_cpumask_map[node];
  791. }
  792. EXPORT_SYMBOL(cpumask_of_node);
  793. #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
  794. #ifdef CONFIG_NUMA_KEEP_MEMINFO
  795. static int meminfo_to_nid(struct numa_meminfo *mi, u64 start)
  796. {
  797. int i;
  798. for (i = 0; i < mi->nr_blks; i++)
  799. if (mi->blk[i].start <= start && mi->blk[i].end > start)
  800. return mi->blk[i].nid;
  801. return NUMA_NO_NODE;
  802. }
  803. int phys_to_target_node(phys_addr_t start)
  804. {
  805. int nid = meminfo_to_nid(&numa_meminfo, start);
  806. /*
  807. * Prefer online nodes, but if reserved memory might be
  808. * hot-added continue the search with reserved ranges.
  809. */
  810. if (nid != NUMA_NO_NODE)
  811. return nid;
  812. return meminfo_to_nid(&numa_reserved_meminfo, start);
  813. }
  814. EXPORT_SYMBOL_GPL(phys_to_target_node);
  815. int memory_add_physaddr_to_nid(u64 start)
  816. {
  817. int nid = meminfo_to_nid(&numa_meminfo, start);
  818. if (nid == NUMA_NO_NODE)
  819. nid = numa_meminfo.blk[0].nid;
  820. return nid;
  821. }
  822. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  823. static int __init cmp_memblk(const void *a, const void *b)
  824. {
  825. const struct numa_memblk *ma = *(const struct numa_memblk **)a;
  826. const struct numa_memblk *mb = *(const struct numa_memblk **)b;
  827. return ma->start - mb->start;
  828. }
  829. static struct numa_memblk *numa_memblk_list[NR_NODE_MEMBLKS] __initdata;
  830. /**
  831. * numa_fill_memblks - Fill gaps in numa_meminfo memblks
  832. * @start: address to begin fill
  833. * @end: address to end fill
  834. *
  835. * Find and extend numa_meminfo memblks to cover the @start-@end
  836. * physical address range, such that the first memblk includes
  837. * @start, the last memblk includes @end, and any gaps in between
  838. * are filled.
  839. *
  840. * RETURNS:
  841. * 0 : Success
  842. * NUMA_NO_MEMBLK : No memblk exists in @start-@end range
  843. */
  844. int __init numa_fill_memblks(u64 start, u64 end)
  845. {
  846. struct numa_memblk **blk = &numa_memblk_list[0];
  847. struct numa_meminfo *mi = &numa_meminfo;
  848. int count = 0;
  849. u64 prev_end;
  850. /*
  851. * Create a list of pointers to numa_meminfo memblks that
  852. * overlap start, end. Exclude (start == bi->end) since
  853. * end addresses in both a CFMWS range and a memblk range
  854. * are exclusive.
  855. *
  856. * This list of pointers is used to make in-place changes
  857. * that fill out the numa_meminfo memblks.
  858. */
  859. for (int i = 0; i < mi->nr_blks; i++) {
  860. struct numa_memblk *bi = &mi->blk[i];
  861. if (start < bi->end && end >= bi->start) {
  862. blk[count] = &mi->blk[i];
  863. count++;
  864. }
  865. }
  866. if (!count)
  867. return NUMA_NO_MEMBLK;
  868. /* Sort the list of pointers in memblk->start order */
  869. sort(&blk[0], count, sizeof(blk[0]), cmp_memblk, NULL);
  870. /* Make sure the first/last memblks include start/end */
  871. blk[0]->start = min(blk[0]->start, start);
  872. blk[count - 1]->end = max(blk[count - 1]->end, end);
  873. /*
  874. * Fill any gaps by tracking the previous memblks
  875. * end address and backfilling to it if needed.
  876. */
  877. prev_end = blk[0]->end;
  878. for (int i = 1; i < count; i++) {
  879. struct numa_memblk *curr = blk[i];
  880. if (prev_end >= curr->start) {
  881. if (prev_end < curr->end)
  882. prev_end = curr->end;
  883. } else {
  884. curr->start = prev_end;
  885. prev_end = curr->end;
  886. }
  887. }
  888. return 0;
  889. }
  890. #endif