numa.c 20 KB

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