sparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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/compiler.h>
  9. #include <linux/highmem.h>
  10. #include <linux/export.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/vmalloc.h>
  13. #include "internal.h"
  14. #include <asm/dma.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/pgtable.h>
  17. /*
  18. * Permanent SPARSEMEM data:
  19. *
  20. * 1) mem_section - memory sections, mem_map's for valid memory
  21. */
  22. #ifdef CONFIG_SPARSEMEM_EXTREME
  23. struct mem_section *mem_section[NR_SECTION_ROOTS]
  24. ____cacheline_internodealigned_in_smp;
  25. #else
  26. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  27. ____cacheline_internodealigned_in_smp;
  28. #endif
  29. EXPORT_SYMBOL(mem_section);
  30. #ifdef NODE_NOT_IN_PAGE_FLAGS
  31. /*
  32. * If we did not store the node number in the page then we have to
  33. * do a lookup in the section_to_node_table in order to find which
  34. * node the page belongs to.
  35. */
  36. #if MAX_NUMNODES <= 256
  37. static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  38. #else
  39. static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  40. #endif
  41. int page_to_nid(const struct page *page)
  42. {
  43. return section_to_node_table[page_to_section(page)];
  44. }
  45. EXPORT_SYMBOL(page_to_nid);
  46. static void set_section_nid(unsigned long section_nr, int nid)
  47. {
  48. section_to_node_table[section_nr] = nid;
  49. }
  50. #else /* !NODE_NOT_IN_PAGE_FLAGS */
  51. static inline void set_section_nid(unsigned long section_nr, int nid)
  52. {
  53. }
  54. #endif
  55. #ifdef CONFIG_SPARSEMEM_EXTREME
  56. static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
  57. {
  58. struct mem_section *section = NULL;
  59. unsigned long array_size = SECTIONS_PER_ROOT *
  60. sizeof(struct mem_section);
  61. if (slab_is_available()) {
  62. if (node_state(nid, N_HIGH_MEMORY))
  63. section = kzalloc_node(array_size, GFP_KERNEL, nid);
  64. else
  65. section = kzalloc(array_size, GFP_KERNEL);
  66. } else {
  67. section = memblock_virt_alloc_node(array_size, nid);
  68. }
  69. return section;
  70. }
  71. static int __meminit sparse_index_init(unsigned long section_nr, int nid)
  72. {
  73. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  74. struct mem_section *section;
  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 0;
  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 remain 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 = memblock_virt_alloc_try_nid_nopanic(size,
  247. SMP_CACHE_BYTES, goal, limit,
  248. nid);
  249. if (!p && limit) {
  250. limit = 0;
  251. goto again;
  252. }
  253. return p;
  254. }
  255. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  256. {
  257. unsigned long usemap_snr, pgdat_snr;
  258. static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
  259. static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
  260. struct pglist_data *pgdat = NODE_DATA(nid);
  261. int usemap_nid;
  262. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  263. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  264. if (usemap_snr == pgdat_snr)
  265. return;
  266. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  267. /* skip redundant message */
  268. return;
  269. old_usemap_snr = usemap_snr;
  270. old_pgdat_snr = pgdat_snr;
  271. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  272. if (usemap_nid != nid) {
  273. pr_info("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. pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
  284. usemap_snr, pgdat_snr, nid);
  285. }
  286. #else
  287. static unsigned long * __init
  288. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  289. unsigned long size)
  290. {
  291. return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
  292. }
  293. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  294. {
  295. }
  296. #endif /* CONFIG_MEMORY_HOTREMOVE */
  297. static void __init sparse_early_usemaps_alloc_node(void *data,
  298. unsigned long pnum_begin,
  299. unsigned long pnum_end,
  300. unsigned long usemap_count, int nodeid)
  301. {
  302. void *usemap;
  303. unsigned long pnum;
  304. unsigned long **usemap_map = (unsigned long **)data;
  305. int size = usemap_size();
  306. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
  307. size * usemap_count);
  308. if (!usemap) {
  309. pr_warn("%s: allocation failed\n", __func__);
  310. return;
  311. }
  312. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  313. if (!present_section_nr(pnum))
  314. continue;
  315. usemap_map[pnum] = usemap;
  316. usemap += size;
  317. check_usemap_section_nr(nodeid, usemap_map[pnum]);
  318. }
  319. }
  320. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  321. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  322. {
  323. struct page *map;
  324. unsigned long size;
  325. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  326. if (map)
  327. return map;
  328. size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  329. map = memblock_virt_alloc_try_nid(size,
  330. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  331. BOOTMEM_ALLOC_ACCESSIBLE, nid);
  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 = memblock_virt_alloc_try_nid(size * map_count,
  354. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  355. BOOTMEM_ALLOC_ACCESSIBLE, nodeid);
  356. if (map) {
  357. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  358. if (!present_section_nr(pnum))
  359. continue;
  360. map_map[pnum] = map;
  361. map += size;
  362. }
  363. return;
  364. }
  365. /* fallback */
  366. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  367. struct mem_section *ms;
  368. if (!present_section_nr(pnum))
  369. continue;
  370. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  371. if (map_map[pnum])
  372. continue;
  373. ms = __nr_to_section(pnum);
  374. pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
  375. __func__);
  376. ms->section_mem_map = 0;
  377. }
  378. }
  379. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  380. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  381. static void __init sparse_early_mem_maps_alloc_node(void *data,
  382. unsigned long pnum_begin,
  383. unsigned long pnum_end,
  384. unsigned long map_count, int nodeid)
  385. {
  386. struct page **map_map = (struct page **)data;
  387. sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
  388. map_count, nodeid);
  389. }
  390. #else
  391. static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  392. {
  393. struct page *map;
  394. struct mem_section *ms = __nr_to_section(pnum);
  395. int nid = sparse_early_nid(ms);
  396. map = sparse_mem_map_populate(pnum, nid);
  397. if (map)
  398. return map;
  399. pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
  400. __func__);
  401. ms->section_mem_map = 0;
  402. return NULL;
  403. }
  404. #endif
  405. void __weak __meminit vmemmap_populate_print_last(void)
  406. {
  407. }
  408. /**
  409. * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
  410. * @map: usemap_map for pageblock flags or mmap_map for vmemmap
  411. */
  412. static void __init alloc_usemap_and_memmap(void (*alloc_func)
  413. (void *, unsigned long, unsigned long,
  414. unsigned long, int), void *data)
  415. {
  416. unsigned long pnum;
  417. unsigned long map_count;
  418. int nodeid_begin = 0;
  419. unsigned long pnum_begin = 0;
  420. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  421. struct mem_section *ms;
  422. if (!present_section_nr(pnum))
  423. continue;
  424. ms = __nr_to_section(pnum);
  425. nodeid_begin = sparse_early_nid(ms);
  426. pnum_begin = pnum;
  427. break;
  428. }
  429. map_count = 1;
  430. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  431. struct mem_section *ms;
  432. int nodeid;
  433. if (!present_section_nr(pnum))
  434. continue;
  435. ms = __nr_to_section(pnum);
  436. nodeid = sparse_early_nid(ms);
  437. if (nodeid == nodeid_begin) {
  438. map_count++;
  439. continue;
  440. }
  441. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  442. alloc_func(data, pnum_begin, pnum,
  443. map_count, nodeid_begin);
  444. /* new start, update count etc*/
  445. nodeid_begin = nodeid;
  446. pnum_begin = pnum;
  447. map_count = 1;
  448. }
  449. /* ok, last chunk */
  450. alloc_func(data, pnum_begin, NR_MEM_SECTIONS,
  451. map_count, nodeid_begin);
  452. }
  453. /*
  454. * Allocate the accumulated non-linear sections, allocate a mem_map
  455. * for each and record the physical to section mapping.
  456. */
  457. void __init sparse_init(void)
  458. {
  459. unsigned long pnum;
  460. struct page *map;
  461. unsigned long *usemap;
  462. unsigned long **usemap_map;
  463. int size;
  464. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  465. int size2;
  466. struct page **map_map;
  467. #endif
  468. /* see include/linux/mmzone.h 'struct mem_section' definition */
  469. BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
  470. /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
  471. set_pageblock_order();
  472. /*
  473. * map is using big page (aka 2M in x86 64 bit)
  474. * usemap is less one page (aka 24 bytes)
  475. * so alloc 2M (with 2M align) and 24 bytes in turn will
  476. * make next 2M slip to one more 2M later.
  477. * then in big system, the memory will have a lot of holes...
  478. * here try to allocate 2M pages continuously.
  479. *
  480. * powerpc need to call sparse_init_one_section right after each
  481. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  482. */
  483. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  484. usemap_map = memblock_virt_alloc(size, 0);
  485. if (!usemap_map)
  486. panic("can not allocate usemap_map\n");
  487. alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
  488. (void *)usemap_map);
  489. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  490. size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
  491. map_map = memblock_virt_alloc(size2, 0);
  492. if (!map_map)
  493. panic("can not allocate map_map\n");
  494. alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
  495. (void *)map_map);
  496. #endif
  497. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  498. if (!present_section_nr(pnum))
  499. continue;
  500. usemap = usemap_map[pnum];
  501. if (!usemap)
  502. continue;
  503. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  504. map = map_map[pnum];
  505. #else
  506. map = sparse_early_mem_map_alloc(pnum);
  507. #endif
  508. if (!map)
  509. continue;
  510. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  511. usemap);
  512. }
  513. vmemmap_populate_print_last();
  514. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  515. memblock_free_early(__pa(map_map), size2);
  516. #endif
  517. memblock_free_early(__pa(usemap_map), size);
  518. }
  519. #ifdef CONFIG_MEMORY_HOTPLUG
  520. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  521. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
  522. {
  523. /* This will make the necessary allocations eventually. */
  524. return sparse_mem_map_populate(pnum, nid);
  525. }
  526. static void __kfree_section_memmap(struct page *memmap)
  527. {
  528. unsigned long start = (unsigned long)memmap;
  529. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  530. vmemmap_free(start, end);
  531. }
  532. #ifdef CONFIG_MEMORY_HOTREMOVE
  533. static void free_map_bootmem(struct page *memmap)
  534. {
  535. unsigned long start = (unsigned long)memmap;
  536. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  537. vmemmap_free(start, end);
  538. }
  539. #endif /* CONFIG_MEMORY_HOTREMOVE */
  540. #else
  541. static struct page *__kmalloc_section_memmap(void)
  542. {
  543. struct page *page, *ret;
  544. unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
  545. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  546. if (page)
  547. goto got_map_page;
  548. ret = vmalloc(memmap_size);
  549. if (ret)
  550. goto got_map_ptr;
  551. return NULL;
  552. got_map_page:
  553. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  554. got_map_ptr:
  555. return ret;
  556. }
  557. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
  558. {
  559. return __kmalloc_section_memmap();
  560. }
  561. static void __kfree_section_memmap(struct page *memmap)
  562. {
  563. if (is_vmalloc_addr(memmap))
  564. vfree(memmap);
  565. else
  566. free_pages((unsigned long)memmap,
  567. get_order(sizeof(struct page) * PAGES_PER_SECTION));
  568. }
  569. #ifdef CONFIG_MEMORY_HOTREMOVE
  570. static void free_map_bootmem(struct page *memmap)
  571. {
  572. unsigned long maps_section_nr, removing_section_nr, i;
  573. unsigned long magic, nr_pages;
  574. struct page *page = virt_to_page(memmap);
  575. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  576. >> PAGE_SHIFT;
  577. for (i = 0; i < nr_pages; i++, page++) {
  578. magic = (unsigned long) page->lru.next;
  579. BUG_ON(magic == NODE_INFO);
  580. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  581. removing_section_nr = page->private;
  582. /*
  583. * When this function is called, the removing section is
  584. * logical offlined state. This means all pages are isolated
  585. * from page allocator. If removing section's memmap is placed
  586. * on the same section, it must not be freed.
  587. * If it is freed, page allocator may allocate it which will
  588. * be removed physically soon.
  589. */
  590. if (maps_section_nr != removing_section_nr)
  591. put_page_bootmem(page);
  592. }
  593. }
  594. #endif /* CONFIG_MEMORY_HOTREMOVE */
  595. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  596. /*
  597. * returns the number of sections whose mem_maps were properly
  598. * set. If this is <=0, then that means that the passed-in
  599. * map was not consumed and must be freed.
  600. */
  601. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn)
  602. {
  603. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  604. struct pglist_data *pgdat = zone->zone_pgdat;
  605. struct mem_section *ms;
  606. struct page *memmap;
  607. unsigned long *usemap;
  608. unsigned long flags;
  609. int ret;
  610. /*
  611. * no locking for this, because it does its own
  612. * plus, it does a kmalloc
  613. */
  614. ret = sparse_index_init(section_nr, pgdat->node_id);
  615. if (ret < 0 && ret != -EEXIST)
  616. return ret;
  617. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id);
  618. if (!memmap)
  619. return -ENOMEM;
  620. usemap = __kmalloc_section_usemap();
  621. if (!usemap) {
  622. __kfree_section_memmap(memmap);
  623. return -ENOMEM;
  624. }
  625. pgdat_resize_lock(pgdat, &flags);
  626. ms = __pfn_to_section(start_pfn);
  627. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  628. ret = -EEXIST;
  629. goto out;
  630. }
  631. memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION);
  632. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  633. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  634. out:
  635. pgdat_resize_unlock(pgdat, &flags);
  636. if (ret <= 0) {
  637. kfree(usemap);
  638. __kfree_section_memmap(memmap);
  639. }
  640. return ret;
  641. }
  642. #ifdef CONFIG_MEMORY_HOTREMOVE
  643. #ifdef CONFIG_MEMORY_FAILURE
  644. static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  645. {
  646. int i;
  647. if (!memmap)
  648. return;
  649. for (i = 0; i < nr_pages; i++) {
  650. if (PageHWPoison(&memmap[i])) {
  651. atomic_long_sub(1, &num_poisoned_pages);
  652. ClearPageHWPoison(&memmap[i]);
  653. }
  654. }
  655. }
  656. #else
  657. static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  658. {
  659. }
  660. #endif
  661. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  662. {
  663. struct page *usemap_page;
  664. if (!usemap)
  665. return;
  666. usemap_page = virt_to_page(usemap);
  667. /*
  668. * Check to see if allocation came from hot-plug-add
  669. */
  670. if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
  671. kfree(usemap);
  672. if (memmap)
  673. __kfree_section_memmap(memmap);
  674. return;
  675. }
  676. /*
  677. * The usemap came from bootmem. This is packed with other usemaps
  678. * on the section which has pgdat at boot time. Just keep it as is now.
  679. */
  680. if (memmap)
  681. free_map_bootmem(memmap);
  682. }
  683. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
  684. unsigned long map_offset)
  685. {
  686. struct page *memmap = NULL;
  687. unsigned long *usemap = NULL, flags;
  688. struct pglist_data *pgdat = zone->zone_pgdat;
  689. pgdat_resize_lock(pgdat, &flags);
  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. pgdat_resize_unlock(pgdat, &flags);
  698. clear_hwpoisoned_pages(memmap + map_offset,
  699. PAGES_PER_SECTION - map_offset);
  700. free_section_usemap(memmap, usemap);
  701. }
  702. #endif /* CONFIG_MEMORY_HOTREMOVE */
  703. #endif /* CONFIG_MEMORY_HOTPLUG */