numa.c 23 KB

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