sparse.c 21 KB

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