sparse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  112. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  113. if (!root)
  114. continue;
  115. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  116. break;
  117. }
  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. /*
  438. * map is using big page (aka 2M in x86 64 bit)
  439. * usemap is less one page (aka 24 bytes)
  440. * so alloc 2M (with 2M align) and 24 bytes in turn will
  441. * make next 2M slip to one more 2M later.
  442. * then in big system, the memory will have a lot of holes...
  443. * here try to allocate 2M pages continuously.
  444. *
  445. * powerpc need to call sparse_init_one_section right after each
  446. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  447. */
  448. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  449. usemap_map = alloc_bootmem(size);
  450. if (!usemap_map)
  451. panic("can not allocate usemap_map\n");
  452. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  453. struct mem_section *ms;
  454. if (!present_section_nr(pnum))
  455. continue;
  456. ms = __nr_to_section(pnum);
  457. nodeid_begin = sparse_early_nid(ms);
  458. pnum_begin = pnum;
  459. break;
  460. }
  461. usemap_count = 1;
  462. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  463. struct mem_section *ms;
  464. int nodeid;
  465. if (!present_section_nr(pnum))
  466. continue;
  467. ms = __nr_to_section(pnum);
  468. nodeid = sparse_early_nid(ms);
  469. if (nodeid == nodeid_begin) {
  470. usemap_count++;
  471. continue;
  472. }
  473. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  474. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, pnum,
  475. usemap_count, nodeid_begin);
  476. /* new start, update count etc*/
  477. nodeid_begin = nodeid;
  478. pnum_begin = pnum;
  479. usemap_count = 1;
  480. }
  481. /* ok, last chunk */
  482. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, NR_MEM_SECTIONS,
  483. usemap_count, nodeid_begin);
  484. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  485. size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
  486. map_map = alloc_bootmem(size2);
  487. if (!map_map)
  488. panic("can not allocate map_map\n");
  489. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  490. struct mem_section *ms;
  491. if (!present_section_nr(pnum))
  492. continue;
  493. ms = __nr_to_section(pnum);
  494. nodeid_begin = sparse_early_nid(ms);
  495. pnum_begin = pnum;
  496. break;
  497. }
  498. map_count = 1;
  499. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  500. struct mem_section *ms;
  501. int nodeid;
  502. if (!present_section_nr(pnum))
  503. continue;
  504. ms = __nr_to_section(pnum);
  505. nodeid = sparse_early_nid(ms);
  506. if (nodeid == nodeid_begin) {
  507. map_count++;
  508. continue;
  509. }
  510. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  511. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, pnum,
  512. map_count, nodeid_begin);
  513. /* new start, update count etc*/
  514. nodeid_begin = nodeid;
  515. pnum_begin = pnum;
  516. map_count = 1;
  517. }
  518. /* ok, last chunk */
  519. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, NR_MEM_SECTIONS,
  520. map_count, nodeid_begin);
  521. #endif
  522. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  523. if (!present_section_nr(pnum))
  524. continue;
  525. usemap = usemap_map[pnum];
  526. if (!usemap)
  527. continue;
  528. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  529. map = map_map[pnum];
  530. #else
  531. map = sparse_early_mem_map_alloc(pnum);
  532. #endif
  533. if (!map)
  534. continue;
  535. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  536. usemap);
  537. }
  538. vmemmap_populate_print_last();
  539. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  540. free_bootmem(__pa(map_map), size2);
  541. #endif
  542. free_bootmem(__pa(usemap_map), size);
  543. }
  544. #ifdef CONFIG_MEMORY_HOTPLUG
  545. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  546. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  547. unsigned long nr_pages)
  548. {
  549. /* This will make the necessary allocations eventually. */
  550. return sparse_mem_map_populate(pnum, nid);
  551. }
  552. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  553. {
  554. return; /* XXX: Not implemented yet */
  555. }
  556. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  557. {
  558. }
  559. #else
  560. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  561. {
  562. struct page *page, *ret;
  563. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  564. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  565. if (page)
  566. goto got_map_page;
  567. ret = vmalloc(memmap_size);
  568. if (ret)
  569. goto got_map_ptr;
  570. return NULL;
  571. got_map_page:
  572. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  573. got_map_ptr:
  574. memset(ret, 0, memmap_size);
  575. return ret;
  576. }
  577. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  578. unsigned long nr_pages)
  579. {
  580. return __kmalloc_section_memmap(nr_pages);
  581. }
  582. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  583. {
  584. if (is_vmalloc_addr(memmap))
  585. vfree(memmap);
  586. else
  587. free_pages((unsigned long)memmap,
  588. get_order(sizeof(struct page) * nr_pages));
  589. }
  590. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  591. {
  592. unsigned long maps_section_nr, removing_section_nr, i;
  593. unsigned long magic;
  594. for (i = 0; i < nr_pages; i++, page++) {
  595. magic = (unsigned long) page->lru.next;
  596. BUG_ON(magic == NODE_INFO);
  597. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  598. removing_section_nr = page->private;
  599. /*
  600. * When this function is called, the removing section is
  601. * logical offlined state. This means all pages are isolated
  602. * from page allocator. If removing section's memmap is placed
  603. * on the same section, it must not be freed.
  604. * If it is freed, page allocator may allocate it which will
  605. * be removed physically soon.
  606. */
  607. if (maps_section_nr != removing_section_nr)
  608. put_page_bootmem(page);
  609. }
  610. }
  611. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  612. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  613. {
  614. struct page *usemap_page;
  615. unsigned long nr_pages;
  616. if (!usemap)
  617. return;
  618. usemap_page = virt_to_page(usemap);
  619. /*
  620. * Check to see if allocation came from hot-plug-add
  621. */
  622. if (PageSlab(usemap_page)) {
  623. kfree(usemap);
  624. if (memmap)
  625. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  626. return;
  627. }
  628. /*
  629. * The usemap came from bootmem. This is packed with other usemaps
  630. * on the section which has pgdat at boot time. Just keep it as is now.
  631. */
  632. if (memmap) {
  633. struct page *memmap_page;
  634. memmap_page = virt_to_page(memmap);
  635. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  636. >> PAGE_SHIFT;
  637. free_map_bootmem(memmap_page, nr_pages);
  638. }
  639. }
  640. /*
  641. * returns the number of sections whose mem_maps were properly
  642. * set. If this is <=0, then that means that the passed-in
  643. * map was not consumed and must be freed.
  644. */
  645. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  646. int nr_pages)
  647. {
  648. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  649. struct pglist_data *pgdat = zone->zone_pgdat;
  650. struct mem_section *ms;
  651. struct page *memmap;
  652. unsigned long *usemap;
  653. unsigned long flags;
  654. int ret;
  655. /*
  656. * no locking for this, because it does its own
  657. * plus, it does a kmalloc
  658. */
  659. ret = sparse_index_init(section_nr, pgdat->node_id);
  660. if (ret < 0 && ret != -EEXIST)
  661. return ret;
  662. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  663. if (!memmap)
  664. return -ENOMEM;
  665. usemap = __kmalloc_section_usemap();
  666. if (!usemap) {
  667. __kfree_section_memmap(memmap, nr_pages);
  668. return -ENOMEM;
  669. }
  670. pgdat_resize_lock(pgdat, &flags);
  671. ms = __pfn_to_section(start_pfn);
  672. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  673. ret = -EEXIST;
  674. goto out;
  675. }
  676. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  677. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  678. out:
  679. pgdat_resize_unlock(pgdat, &flags);
  680. if (ret <= 0) {
  681. kfree(usemap);
  682. __kfree_section_memmap(memmap, nr_pages);
  683. }
  684. return ret;
  685. }
  686. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  687. {
  688. struct page *memmap = NULL;
  689. unsigned long *usemap = NULL;
  690. if (ms->section_mem_map) {
  691. usemap = ms->pageblock_flags;
  692. memmap = sparse_decode_mem_map(ms->section_mem_map,
  693. __section_nr(ms));
  694. ms->section_mem_map = 0;
  695. ms->pageblock_flags = NULL;
  696. }
  697. free_section_usemap(memmap, usemap);
  698. }
  699. #endif