numa.c 20 KB

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