numa.c 21 KB

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