numa.c 22 KB

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