numa_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Generic VM initialization for x86-64 NUMA setups.
  3. * Copyright 2002,2003 Andi Kleen, SuSE Labs.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/string.h>
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/memblock.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/ctype.h>
  13. #include <linux/module.h>
  14. #include <linux/nodemask.h>
  15. #include <linux/sched.h>
  16. #include <linux/acpi.h>
  17. #include <asm/e820.h>
  18. #include <asm/proto.h>
  19. #include <asm/dma.h>
  20. #include <asm/acpi.h>
  21. #include <asm/amd_nb.h>
  22. #include "numa_internal.h"
  23. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  24. EXPORT_SYMBOL(node_data);
  25. nodemask_t numa_nodes_parsed __initdata;
  26. static struct numa_meminfo numa_meminfo
  27. #ifndef CONFIG_MEMORY_HOTPLUG
  28. __initdata
  29. #endif
  30. ;
  31. static int numa_distance_cnt;
  32. static u8 *numa_distance;
  33. static void * __init early_node_mem(int nodeid, unsigned long start,
  34. unsigned long end, unsigned long size,
  35. unsigned long align)
  36. {
  37. unsigned long mem;
  38. /*
  39. * put it on high as possible
  40. * something will go with NODE_DATA
  41. */
  42. if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
  43. start = MAX_DMA_PFN<<PAGE_SHIFT;
  44. if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
  45. end > (MAX_DMA32_PFN<<PAGE_SHIFT))
  46. start = MAX_DMA32_PFN<<PAGE_SHIFT;
  47. mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
  48. if (mem != MEMBLOCK_ERROR)
  49. return __va(mem);
  50. /* extend the search scope */
  51. end = max_pfn_mapped << PAGE_SHIFT;
  52. start = MAX_DMA_PFN << PAGE_SHIFT;
  53. mem = memblock_find_in_range(start, end, size, align);
  54. if (mem != MEMBLOCK_ERROR)
  55. return __va(mem);
  56. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  57. size, nodeid);
  58. return NULL;
  59. }
  60. static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
  61. struct numa_meminfo *mi)
  62. {
  63. /* ignore zero length blks */
  64. if (start == end)
  65. return 0;
  66. /* whine about and ignore invalid blks */
  67. if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
  68. pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
  69. nid, start, end);
  70. return 0;
  71. }
  72. if (mi->nr_blks >= NR_NODE_MEMBLKS) {
  73. pr_err("NUMA: too many memblk ranges\n");
  74. return -EINVAL;
  75. }
  76. mi->blk[mi->nr_blks].start = start;
  77. mi->blk[mi->nr_blks].end = end;
  78. mi->blk[mi->nr_blks].nid = nid;
  79. mi->nr_blks++;
  80. return 0;
  81. }
  82. /**
  83. * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
  84. * @idx: Index of memblk to remove
  85. * @mi: numa_meminfo to remove memblk from
  86. *
  87. * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
  88. * decrementing @mi->nr_blks.
  89. */
  90. void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
  91. {
  92. mi->nr_blks--;
  93. memmove(&mi->blk[idx], &mi->blk[idx + 1],
  94. (mi->nr_blks - idx) * sizeof(mi->blk[0]));
  95. }
  96. /**
  97. * numa_add_memblk - Add one numa_memblk to numa_meminfo
  98. * @nid: NUMA node ID of the new memblk
  99. * @start: Start address of the new memblk
  100. * @end: End address of the new memblk
  101. *
  102. * Add a new memblk to the default numa_meminfo.
  103. *
  104. * RETURNS:
  105. * 0 on success, -errno on failure.
  106. */
  107. int __init numa_add_memblk(int nid, u64 start, u64 end)
  108. {
  109. return numa_add_memblk_to(nid, start, end, &numa_meminfo);
  110. }
  111. /* Initialize bootmem allocator for a node */
  112. void __init
  113. setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  114. {
  115. unsigned long start_pfn, last_pfn, nodedata_phys;
  116. const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
  117. int nid;
  118. if (!end)
  119. return;
  120. /*
  121. * Don't confuse VM with a node that doesn't have the
  122. * minimum amount of memory:
  123. */
  124. if (end && (end - start) < NODE_MIN_SIZE)
  125. return;
  126. start = roundup(start, ZONE_ALIGN);
  127. printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
  128. start, end);
  129. start_pfn = start >> PAGE_SHIFT;
  130. last_pfn = end >> PAGE_SHIFT;
  131. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
  132. SMP_CACHE_BYTES);
  133. if (node_data[nodeid] == NULL)
  134. return;
  135. nodedata_phys = __pa(node_data[nodeid]);
  136. memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
  137. printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
  138. nodedata_phys + pgdat_size - 1);
  139. nid = early_pfn_to_nid(nodedata_phys >> PAGE_SHIFT);
  140. if (nid != nodeid)
  141. printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
  142. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  143. NODE_DATA(nodeid)->node_id = nodeid;
  144. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  145. NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
  146. node_set_online(nodeid);
  147. }
  148. /**
  149. * numa_cleanup_meminfo - Cleanup a numa_meminfo
  150. * @mi: numa_meminfo to clean up
  151. *
  152. * Sanitize @mi by merging and removing unncessary memblks. Also check for
  153. * conflicts and clear unused memblks.
  154. *
  155. * RETURNS:
  156. * 0 on success, -errno on failure.
  157. */
  158. int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
  159. {
  160. const u64 low = 0;
  161. const u64 high = (u64)max_pfn << PAGE_SHIFT;
  162. int i, j, k;
  163. for (i = 0; i < mi->nr_blks; i++) {
  164. struct numa_memblk *bi = &mi->blk[i];
  165. /* make sure all blocks are inside the limits */
  166. bi->start = max(bi->start, low);
  167. bi->end = min(bi->end, high);
  168. /* and there's no empty block */
  169. if (bi->start >= bi->end) {
  170. numa_remove_memblk_from(i--, mi);
  171. continue;
  172. }
  173. for (j = i + 1; j < mi->nr_blks; j++) {
  174. struct numa_memblk *bj = &mi->blk[j];
  175. unsigned long start, end;
  176. /*
  177. * See whether there are overlapping blocks. Whine
  178. * about but allow overlaps of the same nid. They
  179. * will be merged below.
  180. */
  181. if (bi->end > bj->start && bi->start < bj->end) {
  182. if (bi->nid != bj->nid) {
  183. pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
  184. bi->nid, bi->start, bi->end,
  185. bj->nid, bj->start, bj->end);
  186. return -EINVAL;
  187. }
  188. pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
  189. bi->nid, bi->start, bi->end,
  190. bj->start, bj->end);
  191. }
  192. /*
  193. * Join together blocks on the same node, holes
  194. * between which don't overlap with memory on other
  195. * nodes.
  196. */
  197. if (bi->nid != bj->nid)
  198. continue;
  199. start = max(min(bi->start, bj->start), low);
  200. end = min(max(bi->end, bj->end), high);
  201. for (k = 0; k < mi->nr_blks; k++) {
  202. struct numa_memblk *bk = &mi->blk[k];
  203. if (bi->nid == bk->nid)
  204. continue;
  205. if (start < bk->end && end > bk->start)
  206. break;
  207. }
  208. if (k < mi->nr_blks)
  209. continue;
  210. printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
  211. bi->nid, bi->start, bi->end, bj->start, bj->end,
  212. start, end);
  213. bi->start = start;
  214. bi->end = end;
  215. numa_remove_memblk_from(j--, mi);
  216. }
  217. }
  218. for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
  219. mi->blk[i].start = mi->blk[i].end = 0;
  220. mi->blk[i].nid = NUMA_NO_NODE;
  221. }
  222. return 0;
  223. }
  224. /*
  225. * Set nodes, which have memory in @mi, in *@nodemask.
  226. */
  227. static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
  228. const struct numa_meminfo *mi)
  229. {
  230. int i;
  231. for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
  232. if (mi->blk[i].start != mi->blk[i].end &&
  233. mi->blk[i].nid != NUMA_NO_NODE)
  234. node_set(mi->blk[i].nid, *nodemask);
  235. }
  236. /**
  237. * numa_reset_distance - Reset NUMA distance table
  238. *
  239. * The current table is freed. The next numa_set_distance() call will
  240. * create a new one.
  241. */
  242. void __init numa_reset_distance(void)
  243. {
  244. size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
  245. /* numa_distance could be 1LU marking allocation failure, test cnt */
  246. if (numa_distance_cnt)
  247. memblock_x86_free_range(__pa(numa_distance),
  248. __pa(numa_distance) + size);
  249. numa_distance_cnt = 0;
  250. numa_distance = NULL; /* enable table creation */
  251. }
  252. static int __init numa_alloc_distance(void)
  253. {
  254. nodemask_t nodes_parsed;
  255. size_t size;
  256. int i, j, cnt = 0;
  257. u64 phys;
  258. /* size the new table and allocate it */
  259. nodes_parsed = numa_nodes_parsed;
  260. numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
  261. for_each_node_mask(i, nodes_parsed)
  262. cnt = i;
  263. cnt++;
  264. size = cnt * cnt * sizeof(numa_distance[0]);
  265. phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
  266. size, PAGE_SIZE);
  267. if (phys == MEMBLOCK_ERROR) {
  268. pr_warning("NUMA: Warning: can't allocate distance table!\n");
  269. /* don't retry until explicitly reset */
  270. numa_distance = (void *)1LU;
  271. return -ENOMEM;
  272. }
  273. memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
  274. numa_distance = __va(phys);
  275. numa_distance_cnt = cnt;
  276. /* fill with the default distances */
  277. for (i = 0; i < cnt; i++)
  278. for (j = 0; j < cnt; j++)
  279. numa_distance[i * cnt + j] = i == j ?
  280. LOCAL_DISTANCE : REMOTE_DISTANCE;
  281. printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
  282. return 0;
  283. }
  284. /**
  285. * numa_set_distance - Set NUMA distance from one NUMA to another
  286. * @from: the 'from' node to set distance
  287. * @to: the 'to' node to set distance
  288. * @distance: NUMA distance
  289. *
  290. * Set the distance from node @from to @to to @distance. If distance table
  291. * doesn't exist, one which is large enough to accommodate all the currently
  292. * known nodes will be created.
  293. *
  294. * If such table cannot be allocated, a warning is printed and further
  295. * calls are ignored until the distance table is reset with
  296. * numa_reset_distance().
  297. *
  298. * If @from or @to is higher than the highest known node at the time of
  299. * table creation or @distance doesn't make sense, the call is ignored.
  300. * This is to allow simplification of specific NUMA config implementations.
  301. */
  302. void __init numa_set_distance(int from, int to, int distance)
  303. {
  304. if (!numa_distance && numa_alloc_distance() < 0)
  305. return;
  306. if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
  307. printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
  308. from, to, distance);
  309. return;
  310. }
  311. if ((u8)distance != distance ||
  312. (from == to && distance != LOCAL_DISTANCE)) {
  313. pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  314. from, to, distance);
  315. return;
  316. }
  317. numa_distance[from * numa_distance_cnt + to] = distance;
  318. }
  319. int __node_distance(int from, int to)
  320. {
  321. if (from >= numa_distance_cnt || to >= numa_distance_cnt)
  322. return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
  323. return numa_distance[from * numa_distance_cnt + to];
  324. }
  325. EXPORT_SYMBOL(__node_distance);
  326. /*
  327. * Sanity check to catch more bad NUMA configurations (they are amazingly
  328. * common). Make sure the nodes cover all memory.
  329. */
  330. static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
  331. {
  332. unsigned long numaram, e820ram;
  333. int i;
  334. numaram = 0;
  335. for (i = 0; i < mi->nr_blks; i++) {
  336. unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
  337. unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
  338. numaram += e - s;
  339. numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
  340. if ((long)numaram < 0)
  341. numaram = 0;
  342. }
  343. e820ram = max_pfn - (memblock_x86_hole_size(0,
  344. max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
  345. /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
  346. if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
  347. printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
  348. (numaram << PAGE_SHIFT) >> 20,
  349. (e820ram << PAGE_SHIFT) >> 20);
  350. return false;
  351. }
  352. return true;
  353. }
  354. static int __init numa_register_memblks(struct numa_meminfo *mi)
  355. {
  356. int i, nid;
  357. /* Account for nodes with cpus and no memory */
  358. node_possible_map = numa_nodes_parsed;
  359. numa_nodemask_from_meminfo(&node_possible_map, mi);
  360. if (WARN_ON(nodes_empty(node_possible_map)))
  361. return -EINVAL;
  362. for (i = 0; i < mi->nr_blks; i++)
  363. memblock_x86_register_active_regions(mi->blk[i].nid,
  364. mi->blk[i].start >> PAGE_SHIFT,
  365. mi->blk[i].end >> PAGE_SHIFT);
  366. /* for out of order entries */
  367. sort_node_map();
  368. if (!numa_meminfo_cover_memory(mi))
  369. return -EINVAL;
  370. /* Finally register nodes. */
  371. for_each_node_mask(nid, node_possible_map) {
  372. u64 start = (u64)max_pfn << PAGE_SHIFT;
  373. u64 end = 0;
  374. for (i = 0; i < mi->nr_blks; i++) {
  375. if (nid != mi->blk[i].nid)
  376. continue;
  377. start = min(mi->blk[i].start, start);
  378. end = max(mi->blk[i].end, end);
  379. }
  380. if (start < end)
  381. setup_node_bootmem(nid, start, end);
  382. }
  383. return 0;
  384. }
  385. /**
  386. * dummy_numma_init - Fallback dummy NUMA init
  387. *
  388. * Used if there's no underlying NUMA architecture, NUMA initialization
  389. * fails, or NUMA is disabled on the command line.
  390. *
  391. * Must online at least one node and add memory blocks that cover all
  392. * allowed memory. This function must not fail.
  393. */
  394. static int __init dummy_numa_init(void)
  395. {
  396. printk(KERN_INFO "%s\n",
  397. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  398. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  399. 0LU, max_pfn << PAGE_SHIFT);
  400. node_set(0, numa_nodes_parsed);
  401. numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
  402. return 0;
  403. }
  404. static int __init numa_init(int (*init_func)(void))
  405. {
  406. int i;
  407. int ret;
  408. for (i = 0; i < MAX_LOCAL_APIC; i++)
  409. set_apicid_to_node(i, NUMA_NO_NODE);
  410. nodes_clear(numa_nodes_parsed);
  411. nodes_clear(node_possible_map);
  412. nodes_clear(node_online_map);
  413. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  414. remove_all_active_ranges();
  415. numa_reset_distance();
  416. ret = init_func();
  417. if (ret < 0)
  418. return ret;
  419. ret = numa_cleanup_meminfo(&numa_meminfo);
  420. if (ret < 0)
  421. return ret;
  422. numa_emulation(&numa_meminfo, numa_distance_cnt);
  423. ret = numa_register_memblks(&numa_meminfo);
  424. if (ret < 0)
  425. return ret;
  426. for (i = 0; i < nr_cpu_ids; i++) {
  427. int nid = early_cpu_to_node(i);
  428. if (nid == NUMA_NO_NODE)
  429. continue;
  430. if (!node_online(nid))
  431. numa_clear_node(i);
  432. }
  433. numa_init_array();
  434. return 0;
  435. }
  436. void __init initmem_init(void)
  437. {
  438. if (!numa_off) {
  439. #ifdef CONFIG_ACPI_NUMA
  440. if (!numa_init(x86_acpi_numa_init))
  441. return;
  442. #endif
  443. #ifdef CONFIG_AMD_NUMA
  444. if (!numa_init(amd_numa_init))
  445. return;
  446. #endif
  447. }
  448. numa_init(dummy_numa_init);
  449. }
  450. unsigned long __init numa_free_all_bootmem(void)
  451. {
  452. unsigned long pages = 0;
  453. int i;
  454. for_each_online_node(i)
  455. pages += free_all_bootmem_node(NODE_DATA(i));
  456. pages += free_all_memory_core_early(MAX_NUMNODES);
  457. return pages;
  458. }
  459. int __cpuinit numa_cpu_node(int cpu)
  460. {
  461. int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  462. if (apicid != BAD_APICID)
  463. return __apicid_to_node[apicid];
  464. return NUMA_NO_NODE;
  465. }
  466. #ifdef CONFIG_MEMORY_HOTPLUG
  467. int memory_add_physaddr_to_nid(u64 start)
  468. {
  469. struct numa_meminfo *mi = &numa_meminfo;
  470. int nid = mi->blk[0].nid;
  471. int i;
  472. for (i = 0; i < mi->nr_blks; i++)
  473. if (mi->blk[i].start <= start && mi->blk[i].end > start)
  474. nid = mi->blk[i].nid;
  475. return nid;
  476. }
  477. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  478. #endif