sparse.c 20 KB

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