numa.c 23 KB

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