sparse.c 20 KB

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