numa_64.c 14 KB

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