sparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. printk(KERN_INFO
  274. "node %d must be removed before remove section %ld\n",
  275. nid, usemap_snr);
  276. return;
  277. }
  278. /*
  279. * There is a circular dependency.
  280. * Some platforms allow un-removable section because they will just
  281. * gather other removable sections for dynamic partitioning.
  282. * Just notify un-removable section's number here.
  283. */
  284. printk(KERN_INFO "Section %ld and %ld (node %d)", usemap_snr,
  285. pgdat_snr, nid);
  286. printk(KERN_CONT
  287. " have a circular dependency on usemap and pgdat allocations\n");
  288. }
  289. #else
  290. static unsigned long * __init
  291. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  292. unsigned long size)
  293. {
  294. return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
  295. }
  296. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  297. {
  298. }
  299. #endif /* CONFIG_MEMORY_HOTREMOVE */
  300. static void __init sparse_early_usemaps_alloc_node(void *data,
  301. unsigned long pnum_begin,
  302. unsigned long pnum_end,
  303. unsigned long usemap_count, int nodeid)
  304. {
  305. void *usemap;
  306. unsigned long pnum;
  307. unsigned long **usemap_map = (unsigned long **)data;
  308. int size = usemap_size();
  309. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
  310. size * usemap_count);
  311. if (!usemap) {
  312. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  313. return;
  314. }
  315. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  316. if (!present_section_nr(pnum))
  317. continue;
  318. usemap_map[pnum] = usemap;
  319. usemap += size;
  320. check_usemap_section_nr(nodeid, usemap_map[pnum]);
  321. }
  322. }
  323. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  324. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  325. {
  326. struct page *map;
  327. unsigned long size;
  328. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  329. if (map)
  330. return map;
  331. size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  332. map = memblock_virt_alloc_try_nid(size,
  333. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  334. BOOTMEM_ALLOC_ACCESSIBLE, nid);
  335. return map;
  336. }
  337. void __init sparse_mem_maps_populate_node(struct page **map_map,
  338. unsigned long pnum_begin,
  339. unsigned long pnum_end,
  340. unsigned long map_count, int nodeid)
  341. {
  342. void *map;
  343. unsigned long pnum;
  344. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  345. map = alloc_remap(nodeid, size * map_count);
  346. if (map) {
  347. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  348. if (!present_section_nr(pnum))
  349. continue;
  350. map_map[pnum] = map;
  351. map += size;
  352. }
  353. return;
  354. }
  355. size = PAGE_ALIGN(size);
  356. map = memblock_virt_alloc_try_nid(size * map_count,
  357. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  358. BOOTMEM_ALLOC_ACCESSIBLE, nodeid);
  359. if (map) {
  360. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  361. if (!present_section_nr(pnum))
  362. continue;
  363. map_map[pnum] = map;
  364. map += size;
  365. }
  366. return;
  367. }
  368. /* fallback */
  369. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  370. struct mem_section *ms;
  371. if (!present_section_nr(pnum))
  372. continue;
  373. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  374. if (map_map[pnum])
  375. continue;
  376. ms = __nr_to_section(pnum);
  377. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  378. "some memory will not be available.\n", __func__);
  379. ms->section_mem_map = 0;
  380. }
  381. }
  382. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  383. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  384. static void __init sparse_early_mem_maps_alloc_node(void *data,
  385. unsigned long pnum_begin,
  386. unsigned long pnum_end,
  387. unsigned long map_count, int nodeid)
  388. {
  389. struct page **map_map = (struct page **)data;
  390. sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
  391. map_count, nodeid);
  392. }
  393. #else
  394. static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  395. {
  396. struct page *map;
  397. struct mem_section *ms = __nr_to_section(pnum);
  398. int nid = sparse_early_nid(ms);
  399. map = sparse_mem_map_populate(pnum, nid);
  400. if (map)
  401. return map;
  402. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  403. "some memory will not be available.\n", __func__);
  404. ms->section_mem_map = 0;
  405. return NULL;
  406. }
  407. #endif
  408. void __weak __meminit vmemmap_populate_print_last(void)
  409. {
  410. }
  411. /**
  412. * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
  413. * @map: usemap_map for pageblock flags or mmap_map for vmemmap
  414. */
  415. static void __init alloc_usemap_and_memmap(void (*alloc_func)
  416. (void *, unsigned long, unsigned long,
  417. unsigned long, int), void *data)
  418. {
  419. unsigned long pnum;
  420. unsigned long map_count;
  421. int nodeid_begin = 0;
  422. unsigned long pnum_begin = 0;
  423. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  424. struct mem_section *ms;
  425. if (!present_section_nr(pnum))
  426. continue;
  427. ms = __nr_to_section(pnum);
  428. nodeid_begin = sparse_early_nid(ms);
  429. pnum_begin = pnum;
  430. break;
  431. }
  432. map_count = 1;
  433. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  434. struct mem_section *ms;
  435. int nodeid;
  436. if (!present_section_nr(pnum))
  437. continue;
  438. ms = __nr_to_section(pnum);
  439. nodeid = sparse_early_nid(ms);
  440. if (nodeid == nodeid_begin) {
  441. map_count++;
  442. continue;
  443. }
  444. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  445. alloc_func(data, pnum_begin, pnum,
  446. map_count, nodeid_begin);
  447. /* new start, update count etc*/
  448. nodeid_begin = nodeid;
  449. pnum_begin = pnum;
  450. map_count = 1;
  451. }
  452. /* ok, last chunk */
  453. alloc_func(data, pnum_begin, NR_MEM_SECTIONS,
  454. map_count, nodeid_begin);
  455. }
  456. /*
  457. * Allocate the accumulated non-linear sections, allocate a mem_map
  458. * for each and record the physical to section mapping.
  459. */
  460. void __init sparse_init(void)
  461. {
  462. unsigned long pnum;
  463. struct page *map;
  464. unsigned long *usemap;
  465. unsigned long **usemap_map;
  466. int size;
  467. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  468. int size2;
  469. struct page **map_map;
  470. #endif
  471. /* see include/linux/mmzone.h 'struct mem_section' definition */
  472. BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
  473. /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
  474. set_pageblock_order();
  475. /*
  476. * map is using big page (aka 2M in x86 64 bit)
  477. * usemap is less one page (aka 24 bytes)
  478. * so alloc 2M (with 2M align) and 24 bytes in turn will
  479. * make next 2M slip to one more 2M later.
  480. * then in big system, the memory will have a lot of holes...
  481. * here try to allocate 2M pages continuously.
  482. *
  483. * powerpc need to call sparse_init_one_section right after each
  484. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  485. */
  486. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  487. usemap_map = memblock_virt_alloc(size, 0);
  488. if (!usemap_map)
  489. panic("can not allocate usemap_map\n");
  490. alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
  491. (void *)usemap_map);
  492. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  493. size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
  494. map_map = memblock_virt_alloc(size2, 0);
  495. if (!map_map)
  496. panic("can not allocate map_map\n");
  497. alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
  498. (void *)map_map);
  499. #endif
  500. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  501. if (!present_section_nr(pnum))
  502. continue;
  503. usemap = usemap_map[pnum];
  504. if (!usemap)
  505. continue;
  506. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  507. map = map_map[pnum];
  508. #else
  509. map = sparse_early_mem_map_alloc(pnum);
  510. #endif
  511. if (!map)
  512. continue;
  513. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  514. usemap);
  515. }
  516. vmemmap_populate_print_last();
  517. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  518. memblock_free_early(__pa(map_map), size2);
  519. #endif
  520. memblock_free_early(__pa(usemap_map), size);
  521. }
  522. #ifdef CONFIG_MEMORY_HOTPLUG
  523. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  524. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
  525. {
  526. /* This will make the necessary allocations eventually. */
  527. return sparse_mem_map_populate(pnum, nid);
  528. }
  529. static void __kfree_section_memmap(struct page *memmap)
  530. {
  531. unsigned long start = (unsigned long)memmap;
  532. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  533. vmemmap_free(start, end);
  534. }
  535. #ifdef CONFIG_MEMORY_HOTREMOVE
  536. static void free_map_bootmem(struct page *memmap)
  537. {
  538. unsigned long start = (unsigned long)memmap;
  539. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  540. vmemmap_free(start, end);
  541. }
  542. #endif /* CONFIG_MEMORY_HOTREMOVE */
  543. #else
  544. static struct page *__kmalloc_section_memmap(void)
  545. {
  546. struct page *page, *ret;
  547. unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
  548. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  549. if (page)
  550. goto got_map_page;
  551. ret = vmalloc(memmap_size);
  552. if (ret)
  553. goto got_map_ptr;
  554. return NULL;
  555. got_map_page:
  556. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  557. got_map_ptr:
  558. return ret;
  559. }
  560. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid)
  561. {
  562. return __kmalloc_section_memmap();
  563. }
  564. static void __kfree_section_memmap(struct page *memmap)
  565. {
  566. if (is_vmalloc_addr(memmap))
  567. vfree(memmap);
  568. else
  569. free_pages((unsigned long)memmap,
  570. get_order(sizeof(struct page) * PAGES_PER_SECTION));
  571. }
  572. #ifdef CONFIG_MEMORY_HOTREMOVE
  573. static void free_map_bootmem(struct page *memmap)
  574. {
  575. unsigned long maps_section_nr, removing_section_nr, i;
  576. unsigned long magic, nr_pages;
  577. struct page *page = virt_to_page(memmap);
  578. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  579. >> PAGE_SHIFT;
  580. for (i = 0; i < nr_pages; i++, page++) {
  581. magic = (unsigned long) page->lru.next;
  582. BUG_ON(magic == NODE_INFO);
  583. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  584. removing_section_nr = page->private;
  585. /*
  586. * When this function is called, the removing section is
  587. * logical offlined state. This means all pages are isolated
  588. * from page allocator. If removing section's memmap is placed
  589. * on the same section, it must not be freed.
  590. * If it is freed, page allocator may allocate it which will
  591. * be removed physically soon.
  592. */
  593. if (maps_section_nr != removing_section_nr)
  594. put_page_bootmem(page);
  595. }
  596. }
  597. #endif /* CONFIG_MEMORY_HOTREMOVE */
  598. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  599. /*
  600. * returns the number of sections whose mem_maps were properly
  601. * set. If this is <=0, then that means that the passed-in
  602. * map was not consumed and must be freed.
  603. */
  604. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn)
  605. {
  606. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  607. struct pglist_data *pgdat = zone->zone_pgdat;
  608. struct mem_section *ms;
  609. struct page *memmap;
  610. unsigned long *usemap;
  611. unsigned long flags;
  612. int ret;
  613. /*
  614. * no locking for this, because it does its own
  615. * plus, it does a kmalloc
  616. */
  617. ret = sparse_index_init(section_nr, pgdat->node_id);
  618. if (ret < 0 && ret != -EEXIST)
  619. return ret;
  620. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id);
  621. if (!memmap)
  622. return -ENOMEM;
  623. usemap = __kmalloc_section_usemap();
  624. if (!usemap) {
  625. __kfree_section_memmap(memmap);
  626. return -ENOMEM;
  627. }
  628. pgdat_resize_lock(pgdat, &flags);
  629. ms = __pfn_to_section(start_pfn);
  630. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  631. ret = -EEXIST;
  632. goto out;
  633. }
  634. memset(memmap, 0, sizeof(struct page) * PAGES_PER_SECTION);
  635. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  636. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  637. out:
  638. pgdat_resize_unlock(pgdat, &flags);
  639. if (ret <= 0) {
  640. kfree(usemap);
  641. __kfree_section_memmap(memmap);
  642. }
  643. return ret;
  644. }
  645. #ifdef CONFIG_MEMORY_HOTREMOVE
  646. #ifdef CONFIG_MEMORY_FAILURE
  647. static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  648. {
  649. int i;
  650. if (!memmap)
  651. return;
  652. for (i = 0; i < PAGES_PER_SECTION; i++) {
  653. if (PageHWPoison(&memmap[i])) {
  654. atomic_long_sub(1, &num_poisoned_pages);
  655. ClearPageHWPoison(&memmap[i]);
  656. }
  657. }
  658. }
  659. #else
  660. static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  661. {
  662. }
  663. #endif
  664. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  665. {
  666. struct page *usemap_page;
  667. if (!usemap)
  668. return;
  669. usemap_page = virt_to_page(usemap);
  670. /*
  671. * Check to see if allocation came from hot-plug-add
  672. */
  673. if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
  674. kfree(usemap);
  675. if (memmap)
  676. __kfree_section_memmap(memmap);
  677. return;
  678. }
  679. /*
  680. * The usemap came from bootmem. This is packed with other usemaps
  681. * on the section which has pgdat at boot time. Just keep it as is now.
  682. */
  683. if (memmap)
  684. free_map_bootmem(memmap);
  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, flags;
  690. struct pglist_data *pgdat = zone->zone_pgdat;
  691. pgdat_resize_lock(pgdat, &flags);
  692. if (ms->section_mem_map) {
  693. usemap = ms->pageblock_flags;
  694. memmap = sparse_decode_mem_map(ms->section_mem_map,
  695. __section_nr(ms));
  696. ms->section_mem_map = 0;
  697. ms->pageblock_flags = NULL;
  698. }
  699. pgdat_resize_unlock(pgdat, &flags);
  700. clear_hwpoisoned_pages(memmap, PAGES_PER_SECTION);
  701. free_section_usemap(memmap, usemap);
  702. }
  703. #endif /* CONFIG_MEMORY_HOTREMOVE */
  704. #endif /* CONFIG_MEMORY_HOTPLUG */