numa.c 22 KB

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