sparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * sparse memory mappings.
  3. */
  4. #include <linux/mm.h>
  5. #include <linux/slab.h>
  6. #include <linux/mmzone.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/highmem.h>
  9. #include <linux/export.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/vmalloc.h>
  12. #include "internal.h"
  13. #include <asm/dma.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/pgtable.h>
  16. /*
  17. * Permanent SPARSEMEM data:
  18. *
  19. * 1) mem_section - memory sections, mem_map's for valid memory
  20. */
  21. #ifdef CONFIG_SPARSEMEM_EXTREME
  22. struct mem_section *mem_section[NR_SECTION_ROOTS]
  23. ____cacheline_internodealigned_in_smp;
  24. #else
  25. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  26. ____cacheline_internodealigned_in_smp;
  27. #endif
  28. EXPORT_SYMBOL(mem_section);
  29. #ifdef NODE_NOT_IN_PAGE_FLAGS
  30. /*
  31. * If we did not store the node number in the page then we have to
  32. * do a lookup in the section_to_node_table in order to find which
  33. * node the page belongs to.
  34. */
  35. #if MAX_NUMNODES <= 256
  36. static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  37. #else
  38. static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  39. #endif
  40. int page_to_nid(const struct page *page)
  41. {
  42. return section_to_node_table[page_to_section(page)];
  43. }
  44. EXPORT_SYMBOL(page_to_nid);
  45. static void set_section_nid(unsigned long section_nr, int nid)
  46. {
  47. section_to_node_table[section_nr] = nid;
  48. }
  49. #else /* !NODE_NOT_IN_PAGE_FLAGS */
  50. static inline void set_section_nid(unsigned long section_nr, int nid)
  51. {
  52. }
  53. #endif
  54. #ifdef CONFIG_SPARSEMEM_EXTREME
  55. static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
  56. {
  57. struct mem_section *section = NULL;
  58. unsigned long array_size = SECTIONS_PER_ROOT *
  59. sizeof(struct mem_section);
  60. if (slab_is_available()) {
  61. if (node_state(nid, N_HIGH_MEMORY))
  62. section = kzalloc_node(array_size, GFP_KERNEL, nid);
  63. else
  64. section = kzalloc(array_size, GFP_KERNEL);
  65. } else {
  66. section = alloc_bootmem_node(NODE_DATA(nid), array_size);
  67. }
  68. return section;
  69. }
  70. static int __meminit sparse_index_init(unsigned long section_nr, int nid)
  71. {
  72. static DEFINE_SPINLOCK(index_init_lock);
  73. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  74. struct mem_section *section;
  75. int ret = 0;
  76. if (mem_section[root])
  77. return -EEXIST;
  78. section = sparse_index_alloc(nid);
  79. if (!section)
  80. return -ENOMEM;
  81. /*
  82. * This lock keeps two different sections from
  83. * reallocating for the same index
  84. */
  85. spin_lock(&index_init_lock);
  86. if (mem_section[root]) {
  87. ret = -EEXIST;
  88. goto out;
  89. }
  90. mem_section[root] = section;
  91. out:
  92. spin_unlock(&index_init_lock);
  93. return ret;
  94. }
  95. #else /* !SPARSEMEM_EXTREME */
  96. static inline int sparse_index_init(unsigned long section_nr, int nid)
  97. {
  98. return 0;
  99. }
  100. #endif
  101. /*
  102. * Although written for the SPARSEMEM_EXTREME case, this happens
  103. * to also work for the flat array case because
  104. * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
  105. */
  106. int __section_nr(struct mem_section* ms)
  107. {
  108. unsigned long root_nr;
  109. struct mem_section* root;
  110. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  111. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  112. if (!root)
  113. continue;
  114. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  115. break;
  116. }
  117. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  118. }
  119. /*
  120. * During early boot, before section_mem_map is used for an actual
  121. * mem_map, we use section_mem_map to store the section's NUMA
  122. * node. This keeps us from having to use another data structure. The
  123. * node information is cleared just before we store the real mem_map.
  124. */
  125. static inline unsigned long sparse_encode_early_nid(int nid)
  126. {
  127. return (nid << SECTION_NID_SHIFT);
  128. }
  129. static inline int sparse_early_nid(struct mem_section *section)
  130. {
  131. return (section->section_mem_map >> SECTION_NID_SHIFT);
  132. }
  133. /* Validate the physical addressing limitations of the model */
  134. void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
  135. unsigned long *end_pfn)
  136. {
  137. unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
  138. /*
  139. * Sanity checks - do not allow an architecture to pass
  140. * in larger pfns than the maximum scope of sparsemem:
  141. */
  142. if (*start_pfn > max_sparsemem_pfn) {
  143. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  144. "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  145. *start_pfn, *end_pfn, max_sparsemem_pfn);
  146. WARN_ON_ONCE(1);
  147. *start_pfn = max_sparsemem_pfn;
  148. *end_pfn = max_sparsemem_pfn;
  149. } else if (*end_pfn > max_sparsemem_pfn) {
  150. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  151. "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  152. *start_pfn, *end_pfn, max_sparsemem_pfn);
  153. WARN_ON_ONCE(1);
  154. *end_pfn = max_sparsemem_pfn;
  155. }
  156. }
  157. /* Record a memory area against a node. */
  158. void __init memory_present(int nid, unsigned long start, unsigned long end)
  159. {
  160. unsigned long pfn;
  161. start &= PAGE_SECTION_MASK;
  162. mminit_validate_memmodel_limits(&start, &end);
  163. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  164. unsigned long section = pfn_to_section_nr(pfn);
  165. struct mem_section *ms;
  166. sparse_index_init(section, nid);
  167. set_section_nid(section, nid);
  168. ms = __nr_to_section(section);
  169. if (!ms->section_mem_map)
  170. ms->section_mem_map = sparse_encode_early_nid(nid) |
  171. SECTION_MARKED_PRESENT;
  172. }
  173. }
  174. /*
  175. * Only used by the i386 NUMA architecures, but relatively
  176. * generic code.
  177. */
  178. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  179. unsigned long end_pfn)
  180. {
  181. unsigned long pfn;
  182. unsigned long nr_pages = 0;
  183. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  184. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  185. if (nid != early_pfn_to_nid(pfn))
  186. continue;
  187. if (pfn_present(pfn))
  188. nr_pages += PAGES_PER_SECTION;
  189. }
  190. return nr_pages * sizeof(struct page);
  191. }
  192. /*
  193. * Subtle, we encode the real pfn into the mem_map such that
  194. * the identity pfn - section_mem_map will return the actual
  195. * physical page frame number.
  196. */
  197. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  198. {
  199. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  200. }
  201. /*
  202. * Decode mem_map from the coded memmap
  203. */
  204. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  205. {
  206. /* mask off the extra low bits of information */
  207. coded_mem_map &= SECTION_MAP_MASK;
  208. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  209. }
  210. static int __meminit sparse_init_one_section(struct mem_section *ms,
  211. unsigned long pnum, struct page *mem_map,
  212. unsigned long *pageblock_bitmap)
  213. {
  214. if (!present_section(ms))
  215. return -EINVAL;
  216. ms->section_mem_map &= ~SECTION_MAP_MASK;
  217. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
  218. SECTION_HAS_MEM_MAP;
  219. ms->pageblock_flags = pageblock_bitmap;
  220. return 1;
  221. }
  222. unsigned long usemap_size(void)
  223. {
  224. unsigned long size_bytes;
  225. size_bytes = roundup(SECTION_BLOCKFLAGS_BITS, 8) / 8;
  226. size_bytes = roundup(size_bytes, sizeof(unsigned long));
  227. return size_bytes;
  228. }
  229. #ifdef CONFIG_MEMORY_HOTPLUG
  230. static unsigned long *__kmalloc_section_usemap(void)
  231. {
  232. return kmalloc(usemap_size(), GFP_KERNEL);
  233. }
  234. #endif /* CONFIG_MEMORY_HOTPLUG */
  235. #ifdef CONFIG_MEMORY_HOTREMOVE
  236. static unsigned long * __init
  237. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  238. unsigned long size)
  239. {
  240. unsigned long goal, limit;
  241. unsigned long *p;
  242. int nid;
  243. /*
  244. * A page may contain usemaps for other sections preventing the
  245. * page being freed and making a section unremovable while
  246. * other sections referencing the usemap retmain active. Similarly,
  247. * a pgdat can prevent a section being removed. If section A
  248. * contains a pgdat and section B contains the usemap, both
  249. * sections become inter-dependent. This allocates usemaps
  250. * from the same section as the pgdat where possible to avoid
  251. * this problem.
  252. */
  253. goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
  254. limit = goal + (1UL << PA_SECTION_SHIFT);
  255. nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
  256. again:
  257. p = ___alloc_bootmem_node_nopanic(NODE_DATA(nid), size,
  258. SMP_CACHE_BYTES, goal, limit);
  259. if (!p && limit) {
  260. limit = 0;
  261. goto again;
  262. }
  263. return p;
  264. }
  265. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  266. {
  267. unsigned long usemap_snr, pgdat_snr;
  268. static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
  269. static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
  270. struct pglist_data *pgdat = NODE_DATA(nid);
  271. int usemap_nid;
  272. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  273. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  274. if (usemap_snr == pgdat_snr)
  275. return;
  276. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  277. /* skip redundant message */
  278. return;
  279. old_usemap_snr = usemap_snr;
  280. old_pgdat_snr = pgdat_snr;
  281. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  282. if (usemap_nid != nid) {
  283. printk(KERN_INFO
  284. "node %d must be removed before remove section %ld\n",
  285. nid, usemap_snr);
  286. return;
  287. }
  288. /*
  289. * There is a circular dependency.
  290. * Some platforms allow un-removable section because they will just
  291. * gather other removable sections for dynamic partitioning.
  292. * Just notify un-removable section's number here.
  293. */
  294. printk(KERN_INFO "Section %ld and %ld (node %d)", usemap_snr,
  295. pgdat_snr, nid);
  296. printk(KERN_CONT
  297. " have a circular dependency on usemap and pgdat allocations\n");
  298. }
  299. #else
  300. static unsigned long * __init
  301. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  302. unsigned long size)
  303. {
  304. return alloc_bootmem_node_nopanic(pgdat, size);
  305. }
  306. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  307. {
  308. }
  309. #endif /* CONFIG_MEMORY_HOTREMOVE */
  310. static void __init sparse_early_usemaps_alloc_node(unsigned long**usemap_map,
  311. unsigned long pnum_begin,
  312. unsigned long pnum_end,
  313. unsigned long usemap_count, int nodeid)
  314. {
  315. void *usemap;
  316. unsigned long pnum;
  317. int size = usemap_size();
  318. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
  319. size * usemap_count);
  320. if (!usemap) {
  321. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  322. return;
  323. }
  324. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  325. if (!present_section_nr(pnum))
  326. continue;
  327. usemap_map[pnum] = usemap;
  328. usemap += size;
  329. check_usemap_section_nr(nodeid, usemap_map[pnum]);
  330. }
  331. }
  332. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  333. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  334. {
  335. struct page *map;
  336. unsigned long size;
  337. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  338. if (map)
  339. return map;
  340. size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  341. map = __alloc_bootmem_node_high(NODE_DATA(nid), size,
  342. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  343. return map;
  344. }
  345. void __init sparse_mem_maps_populate_node(struct page **map_map,
  346. unsigned long pnum_begin,
  347. unsigned long pnum_end,
  348. unsigned long map_count, int nodeid)
  349. {
  350. void *map;
  351. unsigned long pnum;
  352. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  353. map = alloc_remap(nodeid, size * map_count);
  354. if (map) {
  355. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  356. if (!present_section_nr(pnum))
  357. continue;
  358. map_map[pnum] = map;
  359. map += size;
  360. }
  361. return;
  362. }
  363. size = PAGE_ALIGN(size);
  364. map = __alloc_bootmem_node_high(NODE_DATA(nodeid), size * map_count,
  365. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  366. if (map) {
  367. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  368. if (!present_section_nr(pnum))
  369. continue;
  370. map_map[pnum] = map;
  371. map += size;
  372. }
  373. return;
  374. }
  375. /* fallback */
  376. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  377. struct mem_section *ms;
  378. if (!present_section_nr(pnum))
  379. continue;
  380. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  381. if (map_map[pnum])
  382. continue;
  383. ms = __nr_to_section(pnum);
  384. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  385. "some memory will not be available.\n", __func__);
  386. ms->section_mem_map = 0;
  387. }
  388. }
  389. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  390. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  391. static void __init sparse_early_mem_maps_alloc_node(struct page **map_map,
  392. unsigned long pnum_begin,
  393. unsigned long pnum_end,
  394. unsigned long map_count, int nodeid)
  395. {
  396. sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
  397. map_count, nodeid);
  398. }
  399. #else
  400. static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  401. {
  402. struct page *map;
  403. struct mem_section *ms = __nr_to_section(pnum);
  404. int nid = sparse_early_nid(ms);
  405. map = sparse_mem_map_populate(pnum, nid);
  406. if (map)
  407. return map;
  408. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  409. "some memory will not be available.\n", __func__);
  410. ms->section_mem_map = 0;
  411. return NULL;
  412. }
  413. #endif
  414. void __attribute__((weak)) __meminit vmemmap_populate_print_last(void)
  415. {
  416. }
  417. /*
  418. * Allocate the accumulated non-linear sections, allocate a mem_map
  419. * for each and record the physical to section mapping.
  420. */
  421. void __init sparse_init(void)
  422. {
  423. unsigned long pnum;
  424. struct page *map;
  425. unsigned long *usemap;
  426. unsigned long **usemap_map;
  427. int size;
  428. int nodeid_begin = 0;
  429. unsigned long pnum_begin = 0;
  430. unsigned long usemap_count;
  431. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  432. unsigned long map_count;
  433. int size2;
  434. struct page **map_map;
  435. #endif
  436. /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
  437. set_pageblock_order();
  438. /*
  439. * map is using big page (aka 2M in x86 64 bit)
  440. * usemap is less one page (aka 24 bytes)
  441. * so alloc 2M (with 2M align) and 24 bytes in turn will
  442. * make next 2M slip to one more 2M later.
  443. * then in big system, the memory will have a lot of holes...
  444. * here try to allocate 2M pages continuously.
  445. *
  446. * powerpc need to call sparse_init_one_section right after each
  447. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  448. */
  449. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  450. usemap_map = alloc_bootmem(size);
  451. if (!usemap_map)
  452. panic("can not allocate usemap_map\n");
  453. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  454. struct mem_section *ms;
  455. if (!present_section_nr(pnum))
  456. continue;
  457. ms = __nr_to_section(pnum);
  458. nodeid_begin = sparse_early_nid(ms);
  459. pnum_begin = pnum;
  460. break;
  461. }
  462. usemap_count = 1;
  463. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  464. struct mem_section *ms;
  465. int nodeid;
  466. if (!present_section_nr(pnum))
  467. continue;
  468. ms = __nr_to_section(pnum);
  469. nodeid = sparse_early_nid(ms);
  470. if (nodeid == nodeid_begin) {
  471. usemap_count++;
  472. continue;
  473. }
  474. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  475. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, pnum,
  476. usemap_count, nodeid_begin);
  477. /* new start, update count etc*/
  478. nodeid_begin = nodeid;
  479. pnum_begin = pnum;
  480. usemap_count = 1;
  481. }
  482. /* ok, last chunk */
  483. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, NR_MEM_SECTIONS,
  484. usemap_count, nodeid_begin);
  485. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  486. size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
  487. map_map = alloc_bootmem(size2);
  488. if (!map_map)
  489. panic("can not allocate map_map\n");
  490. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  491. struct mem_section *ms;
  492. if (!present_section_nr(pnum))
  493. continue;
  494. ms = __nr_to_section(pnum);
  495. nodeid_begin = sparse_early_nid(ms);
  496. pnum_begin = pnum;
  497. break;
  498. }
  499. map_count = 1;
  500. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  501. struct mem_section *ms;
  502. int nodeid;
  503. if (!present_section_nr(pnum))
  504. continue;
  505. ms = __nr_to_section(pnum);
  506. nodeid = sparse_early_nid(ms);
  507. if (nodeid == nodeid_begin) {
  508. map_count++;
  509. continue;
  510. }
  511. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  512. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, pnum,
  513. map_count, nodeid_begin);
  514. /* new start, update count etc*/
  515. nodeid_begin = nodeid;
  516. pnum_begin = pnum;
  517. map_count = 1;
  518. }
  519. /* ok, last chunk */
  520. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, NR_MEM_SECTIONS,
  521. map_count, nodeid_begin);
  522. #endif
  523. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  524. if (!present_section_nr(pnum))
  525. continue;
  526. usemap = usemap_map[pnum];
  527. if (!usemap)
  528. continue;
  529. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  530. map = map_map[pnum];
  531. #else
  532. map = sparse_early_mem_map_alloc(pnum);
  533. #endif
  534. if (!map)
  535. continue;
  536. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  537. usemap);
  538. }
  539. vmemmap_populate_print_last();
  540. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  541. free_bootmem(__pa(map_map), size2);
  542. #endif
  543. free_bootmem(__pa(usemap_map), size);
  544. }
  545. #ifdef CONFIG_MEMORY_HOTPLUG
  546. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  547. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  548. unsigned long nr_pages)
  549. {
  550. /* This will make the necessary allocations eventually. */
  551. return sparse_mem_map_populate(pnum, nid);
  552. }
  553. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  554. {
  555. return; /* XXX: Not implemented yet */
  556. }
  557. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  558. {
  559. }
  560. #else
  561. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  562. {
  563. struct page *page, *ret;
  564. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  565. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  566. if (page)
  567. goto got_map_page;
  568. ret = vmalloc(memmap_size);
  569. if (ret)
  570. goto got_map_ptr;
  571. return NULL;
  572. got_map_page:
  573. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  574. got_map_ptr:
  575. memset(ret, 0, memmap_size);
  576. return ret;
  577. }
  578. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  579. unsigned long nr_pages)
  580. {
  581. return __kmalloc_section_memmap(nr_pages);
  582. }
  583. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  584. {
  585. if (is_vmalloc_addr(memmap))
  586. vfree(memmap);
  587. else
  588. free_pages((unsigned long)memmap,
  589. get_order(sizeof(struct page) * nr_pages));
  590. }
  591. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  592. {
  593. unsigned long maps_section_nr, removing_section_nr, i;
  594. unsigned long magic;
  595. for (i = 0; i < nr_pages; i++, page++) {
  596. magic = (unsigned long) page->lru.next;
  597. BUG_ON(magic == NODE_INFO);
  598. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  599. removing_section_nr = page->private;
  600. /*
  601. * When this function is called, the removing section is
  602. * logical offlined state. This means all pages are isolated
  603. * from page allocator. If removing section's memmap is placed
  604. * on the same section, it must not be freed.
  605. * If it is freed, page allocator may allocate it which will
  606. * be removed physically soon.
  607. */
  608. if (maps_section_nr != removing_section_nr)
  609. put_page_bootmem(page);
  610. }
  611. }
  612. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  613. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  614. {
  615. struct page *usemap_page;
  616. unsigned long nr_pages;
  617. if (!usemap)
  618. return;
  619. usemap_page = virt_to_page(usemap);
  620. /*
  621. * Check to see if allocation came from hot-plug-add
  622. */
  623. if (PageSlab(usemap_page)) {
  624. kfree(usemap);
  625. if (memmap)
  626. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  627. return;
  628. }
  629. /*
  630. * The usemap came from bootmem. This is packed with other usemaps
  631. * on the section which has pgdat at boot time. Just keep it as is now.
  632. */
  633. if (memmap) {
  634. struct page *memmap_page;
  635. memmap_page = virt_to_page(memmap);
  636. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  637. >> PAGE_SHIFT;
  638. free_map_bootmem(memmap_page, nr_pages);
  639. }
  640. }
  641. /*
  642. * returns the number of sections whose mem_maps were properly
  643. * set. If this is <=0, then that means that the passed-in
  644. * map was not consumed and must be freed.
  645. */
  646. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  647. int nr_pages)
  648. {
  649. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  650. struct pglist_data *pgdat = zone->zone_pgdat;
  651. struct mem_section *ms;
  652. struct page *memmap;
  653. unsigned long *usemap;
  654. unsigned long flags;
  655. int ret;
  656. /*
  657. * no locking for this, because it does its own
  658. * plus, it does a kmalloc
  659. */
  660. ret = sparse_index_init(section_nr, pgdat->node_id);
  661. if (ret < 0 && ret != -EEXIST)
  662. return ret;
  663. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  664. if (!memmap)
  665. return -ENOMEM;
  666. usemap = __kmalloc_section_usemap();
  667. if (!usemap) {
  668. __kfree_section_memmap(memmap, nr_pages);
  669. return -ENOMEM;
  670. }
  671. pgdat_resize_lock(pgdat, &flags);
  672. ms = __pfn_to_section(start_pfn);
  673. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  674. ret = -EEXIST;
  675. goto out;
  676. }
  677. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  678. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  679. out:
  680. pgdat_resize_unlock(pgdat, &flags);
  681. if (ret <= 0) {
  682. kfree(usemap);
  683. __kfree_section_memmap(memmap, nr_pages);
  684. }
  685. return ret;
  686. }
  687. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  688. {
  689. struct page *memmap = NULL;
  690. unsigned long *usemap = NULL;
  691. if (ms->section_mem_map) {
  692. usemap = ms->pageblock_flags;
  693. memmap = sparse_decode_mem_map(ms->section_mem_map,
  694. __section_nr(ms));
  695. ms->section_mem_map = 0;
  696. ms->pageblock_flags = NULL;
  697. }
  698. free_section_usemap(memmap, usemap);
  699. }
  700. #endif