numa.c 20 KB

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