sparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sparse memory mappings.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/mmzone.h>
  8. #include <linux/memblock.h>
  9. #include <linux/compiler.h>
  10. #include <linux/highmem.h>
  11. #include <linux/export.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/vmalloc.h>
  14. #include "internal.h"
  15. #include <asm/dma.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/pgtable.h>
  18. /*
  19. * Permanent SPARSEMEM data:
  20. *
  21. * 1) mem_section - memory sections, mem_map's for valid memory
  22. */
  23. #ifdef CONFIG_SPARSEMEM_EXTREME
  24. struct mem_section **mem_section;
  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 noinline struct mem_section __ref *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. section = kzalloc_node(array_size, GFP_KERNEL, nid);
  63. else
  64. section = memblock_alloc_node(array_size, SMP_CACHE_BYTES,
  65. nid);
  66. return section;
  67. }
  68. static int __meminit sparse_index_init(unsigned long section_nr, int nid)
  69. {
  70. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  71. struct mem_section *section;
  72. if (mem_section[root])
  73. return -EEXIST;
  74. section = sparse_index_alloc(nid);
  75. if (!section)
  76. return -ENOMEM;
  77. mem_section[root] = section;
  78. return 0;
  79. }
  80. #else /* !SPARSEMEM_EXTREME */
  81. static inline int sparse_index_init(unsigned long section_nr, int nid)
  82. {
  83. return 0;
  84. }
  85. #endif
  86. #ifdef CONFIG_SPARSEMEM_EXTREME
  87. int __section_nr(struct mem_section* ms)
  88. {
  89. unsigned long root_nr;
  90. struct mem_section *root = NULL;
  91. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  92. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  93. if (!root)
  94. continue;
  95. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  96. break;
  97. }
  98. VM_BUG_ON(!root);
  99. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  100. }
  101. #else
  102. int __section_nr(struct mem_section* ms)
  103. {
  104. return (int)(ms - mem_section[0]);
  105. }
  106. #endif
  107. /*
  108. * During early boot, before section_mem_map is used for an actual
  109. * mem_map, we use section_mem_map to store the section's NUMA
  110. * node. This keeps us from having to use another data structure. The
  111. * node information is cleared just before we store the real mem_map.
  112. */
  113. static inline unsigned long sparse_encode_early_nid(int nid)
  114. {
  115. return (nid << SECTION_NID_SHIFT);
  116. }
  117. static inline int sparse_early_nid(struct mem_section *section)
  118. {
  119. return (section->section_mem_map >> SECTION_NID_SHIFT);
  120. }
  121. /* Validate the physical addressing limitations of the model */
  122. void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
  123. unsigned long *end_pfn)
  124. {
  125. unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
  126. /*
  127. * Sanity checks - do not allow an architecture to pass
  128. * in larger pfns than the maximum scope of sparsemem:
  129. */
  130. if (*start_pfn > max_sparsemem_pfn) {
  131. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  132. "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  133. *start_pfn, *end_pfn, max_sparsemem_pfn);
  134. WARN_ON_ONCE(1);
  135. *start_pfn = max_sparsemem_pfn;
  136. *end_pfn = max_sparsemem_pfn;
  137. } else if (*end_pfn > max_sparsemem_pfn) {
  138. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  139. "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  140. *start_pfn, *end_pfn, max_sparsemem_pfn);
  141. WARN_ON_ONCE(1);
  142. *end_pfn = max_sparsemem_pfn;
  143. }
  144. }
  145. /*
  146. * There are a number of times that we loop over NR_MEM_SECTIONS,
  147. * looking for section_present() on each. But, when we have very
  148. * large physical address spaces, NR_MEM_SECTIONS can also be
  149. * very large which makes the loops quite long.
  150. *
  151. * Keeping track of this gives us an easy way to break out of
  152. * those loops early.
  153. */
  154. int __highest_present_section_nr;
  155. static void section_mark_present(struct mem_section *ms)
  156. {
  157. int section_nr = __section_nr(ms);
  158. if (section_nr > __highest_present_section_nr)
  159. __highest_present_section_nr = section_nr;
  160. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  161. }
  162. static inline int next_present_section_nr(int section_nr)
  163. {
  164. do {
  165. section_nr++;
  166. if (present_section_nr(section_nr))
  167. return section_nr;
  168. } while ((section_nr <= __highest_present_section_nr));
  169. return -1;
  170. }
  171. #define for_each_present_section_nr(start, section_nr) \
  172. for (section_nr = next_present_section_nr(start-1); \
  173. ((section_nr >= 0) && \
  174. (section_nr <= __highest_present_section_nr)); \
  175. section_nr = next_present_section_nr(section_nr))
  176. static inline unsigned long first_present_section_nr(void)
  177. {
  178. return next_present_section_nr(-1);
  179. }
  180. /* Record a memory area against a node. */
  181. void __init memory_present(int nid, unsigned long start, unsigned long end)
  182. {
  183. unsigned long pfn;
  184. #ifdef CONFIG_SPARSEMEM_EXTREME
  185. if (unlikely(!mem_section)) {
  186. unsigned long size, align;
  187. size = sizeof(struct mem_section*) * NR_SECTION_ROOTS;
  188. align = 1 << (INTERNODE_CACHE_SHIFT);
  189. mem_section = memblock_alloc(size, align);
  190. }
  191. #endif
  192. start &= PAGE_SECTION_MASK;
  193. mminit_validate_memmodel_limits(&start, &end);
  194. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  195. unsigned long section = pfn_to_section_nr(pfn);
  196. struct mem_section *ms;
  197. sparse_index_init(section, nid);
  198. set_section_nid(section, nid);
  199. ms = __nr_to_section(section);
  200. if (!ms->section_mem_map) {
  201. ms->section_mem_map = sparse_encode_early_nid(nid) |
  202. SECTION_IS_ONLINE;
  203. section_mark_present(ms);
  204. }
  205. }
  206. }
  207. /*
  208. * Mark all memblocks as present using memory_present(). This is a
  209. * convienence function that is useful for a number of arches
  210. * to mark all of the systems memory as present during initialization.
  211. */
  212. void __init memblocks_present(void)
  213. {
  214. struct memblock_region *reg;
  215. for_each_memblock(memory, reg) {
  216. memory_present(memblock_get_region_node(reg),
  217. memblock_region_memory_base_pfn(reg),
  218. memblock_region_memory_end_pfn(reg));
  219. }
  220. }
  221. /*
  222. * Subtle, we encode the real pfn into the mem_map such that
  223. * the identity pfn - section_mem_map will return the actual
  224. * physical page frame number.
  225. */
  226. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  227. {
  228. unsigned long coded_mem_map =
  229. (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  230. BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
  231. BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
  232. return coded_mem_map;
  233. }
  234. /*
  235. * Decode mem_map from the coded memmap
  236. */
  237. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  238. {
  239. /* mask off the extra low bits of information */
  240. coded_mem_map &= SECTION_MAP_MASK;
  241. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  242. }
  243. static void __meminit sparse_init_one_section(struct mem_section *ms,
  244. unsigned long pnum, struct page *mem_map,
  245. unsigned long *pageblock_bitmap)
  246. {
  247. ms->section_mem_map &= ~SECTION_MAP_MASK;
  248. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
  249. SECTION_HAS_MEM_MAP;
  250. ms->pageblock_flags = pageblock_bitmap;
  251. }
  252. unsigned long usemap_size(void)
  253. {
  254. return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
  255. }
  256. #ifdef CONFIG_MEMORY_HOTPLUG
  257. static unsigned long *__kmalloc_section_usemap(void)
  258. {
  259. return kmalloc(usemap_size(), GFP_KERNEL);
  260. }
  261. #endif /* CONFIG_MEMORY_HOTPLUG */
  262. #ifdef CONFIG_MEMORY_HOTREMOVE
  263. static unsigned long * __init
  264. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  265. unsigned long size)
  266. {
  267. unsigned long goal, limit;
  268. unsigned long *p;
  269. int nid;
  270. /*
  271. * A page may contain usemaps for other sections preventing the
  272. * page being freed and making a section unremovable while
  273. * other sections referencing the usemap remain active. Similarly,
  274. * a pgdat can prevent a section being removed. If section A
  275. * contains a pgdat and section B contains the usemap, both
  276. * sections become inter-dependent. This allocates usemaps
  277. * from the same section as the pgdat where possible to avoid
  278. * this problem.
  279. */
  280. goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
  281. limit = goal + (1UL << PA_SECTION_SHIFT);
  282. nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
  283. again:
  284. p = memblock_alloc_try_nid_nopanic(size,
  285. SMP_CACHE_BYTES, goal, limit,
  286. nid);
  287. if (!p && limit) {
  288. limit = 0;
  289. goto again;
  290. }
  291. return p;
  292. }
  293. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  294. {
  295. unsigned long usemap_snr, pgdat_snr;
  296. static unsigned long old_usemap_snr;
  297. static unsigned long old_pgdat_snr;
  298. struct pglist_data *pgdat = NODE_DATA(nid);
  299. int usemap_nid;
  300. /* First call */
  301. if (!old_usemap_snr) {
  302. old_usemap_snr = NR_MEM_SECTIONS;
  303. old_pgdat_snr = NR_MEM_SECTIONS;
  304. }
  305. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  306. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  307. if (usemap_snr == pgdat_snr)
  308. return;
  309. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  310. /* skip redundant message */
  311. return;
  312. old_usemap_snr = usemap_snr;
  313. old_pgdat_snr = pgdat_snr;
  314. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  315. if (usemap_nid != nid) {
  316. pr_info("node %d must be removed before remove section %ld\n",
  317. nid, usemap_snr);
  318. return;
  319. }
  320. /*
  321. * There is a circular dependency.
  322. * Some platforms allow un-removable section because they will just
  323. * gather other removable sections for dynamic partitioning.
  324. * Just notify un-removable section's number here.
  325. */
  326. pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
  327. usemap_snr, pgdat_snr, nid);
  328. }
  329. #else
  330. static unsigned long * __init
  331. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  332. unsigned long size)
  333. {
  334. return memblock_alloc_node_nopanic(size, pgdat->node_id);
  335. }
  336. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  337. {
  338. }
  339. #endif /* CONFIG_MEMORY_HOTREMOVE */
  340. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  341. static unsigned long __init section_map_size(void)
  342. {
  343. return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
  344. }
  345. #else
  346. static unsigned long __init section_map_size(void)
  347. {
  348. return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  349. }
  350. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
  351. struct vmem_altmap *altmap)
  352. {
  353. unsigned long size = section_map_size();
  354. struct page *map = sparse_buffer_alloc(size);
  355. if (map)
  356. return map;
  357. map = memblock_alloc_try_nid(size,
  358. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  359. MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  360. return map;
  361. }
  362. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  363. static void *sparsemap_buf __meminitdata;
  364. static void *sparsemap_buf_end __meminitdata;
  365. static void __init sparse_buffer_init(unsigned long size, int nid)
  366. {
  367. WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
  368. sparsemap_buf =
  369. memblock_alloc_try_nid_raw(size, PAGE_SIZE,
  370. __pa(MAX_DMA_ADDRESS),
  371. MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  372. sparsemap_buf_end = sparsemap_buf + size;
  373. }
  374. static void __init sparse_buffer_fini(void)
  375. {
  376. unsigned long size = sparsemap_buf_end - sparsemap_buf;
  377. if (sparsemap_buf && size > 0)
  378. memblock_free_early(__pa(sparsemap_buf), size);
  379. sparsemap_buf = NULL;
  380. }
  381. void * __meminit sparse_buffer_alloc(unsigned long size)
  382. {
  383. void *ptr = NULL;
  384. if (sparsemap_buf) {
  385. ptr = PTR_ALIGN(sparsemap_buf, size);
  386. if (ptr + size > sparsemap_buf_end)
  387. ptr = NULL;
  388. else
  389. sparsemap_buf = ptr + size;
  390. }
  391. return ptr;
  392. }
  393. void __weak __meminit vmemmap_populate_print_last(void)
  394. {
  395. }
  396. /*
  397. * Initialize sparse on a specific node. The node spans [pnum_begin, pnum_end)
  398. * And number of present sections in this node is map_count.
  399. */
  400. static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
  401. unsigned long pnum_end,
  402. unsigned long map_count)
  403. {
  404. unsigned long pnum, usemap_longs, *usemap;
  405. struct page *map;
  406. usemap_longs = BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS);
  407. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nid),
  408. usemap_size() *
  409. map_count);
  410. if (!usemap) {
  411. pr_err("%s: node[%d] usemap allocation failed", __func__, nid);
  412. goto failed;
  413. }
  414. sparse_buffer_init(map_count * section_map_size(), nid);
  415. for_each_present_section_nr(pnum_begin, pnum) {
  416. if (pnum >= pnum_end)
  417. break;
  418. map = sparse_mem_map_populate(pnum, nid, NULL);
  419. if (!map) {
  420. pr_err("%s: node[%d] memory map backing failed. Some memory will not be available.",
  421. __func__, nid);
  422. pnum_begin = pnum;
  423. goto failed;
  424. }
  425. check_usemap_section_nr(nid, usemap);
  426. sparse_init_one_section(__nr_to_section(pnum), pnum, map, usemap);
  427. usemap += usemap_longs;
  428. }
  429. sparse_buffer_fini();
  430. return;
  431. failed:
  432. /* We failed to allocate, mark all the following pnums as not present */
  433. for_each_present_section_nr(pnum_begin, pnum) {
  434. struct mem_section *ms;
  435. if (pnum >= pnum_end)
  436. break;
  437. ms = __nr_to_section(pnum);
  438. ms->section_mem_map = 0;
  439. }
  440. }
  441. /*
  442. * Allocate the accumulated non-linear sections, allocate a mem_map
  443. * for each and record the physical to section mapping.
  444. */
  445. void __init sparse_init(void)
  446. {
  447. unsigned long pnum_begin = first_present_section_nr();
  448. int nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
  449. unsigned long pnum_end, map_count = 1;
  450. /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
  451. set_pageblock_order();
  452. for_each_present_section_nr(pnum_begin + 1, pnum_end) {
  453. int nid = sparse_early_nid(__nr_to_section(pnum_end));
  454. if (nid == nid_begin) {
  455. map_count++;
  456. continue;
  457. }
  458. /* Init node with sections in range [pnum_begin, pnum_end) */
  459. sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
  460. nid_begin = nid;
  461. pnum_begin = pnum_end;
  462. map_count = 1;
  463. }
  464. /* cover the last node */
  465. sparse_init_nid(nid_begin, pnum_begin, pnum_end, map_count);
  466. vmemmap_populate_print_last();
  467. }
  468. #ifdef CONFIG_MEMORY_HOTPLUG
  469. /* Mark all memory sections within the pfn range as online */
  470. void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
  471. {
  472. unsigned long pfn;
  473. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  474. unsigned long section_nr = pfn_to_section_nr(pfn);
  475. struct mem_section *ms;
  476. /* onlining code should never touch invalid ranges */
  477. if (WARN_ON(!valid_section_nr(section_nr)))
  478. continue;
  479. ms = __nr_to_section(section_nr);
  480. ms->section_mem_map |= SECTION_IS_ONLINE;
  481. }
  482. }
  483. #ifdef CONFIG_MEMORY_HOTREMOVE
  484. /* Mark all memory sections within the pfn range as online */
  485. void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
  486. {
  487. unsigned long pfn;
  488. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  489. unsigned long section_nr = pfn_to_section_nr(pfn);
  490. struct mem_section *ms;
  491. /*
  492. * TODO this needs some double checking. Offlining code makes
  493. * sure to check pfn_valid but those checks might be just bogus
  494. */
  495. if (WARN_ON(!valid_section_nr(section_nr)))
  496. continue;
  497. ms = __nr_to_section(section_nr);
  498. ms->section_mem_map &= ~SECTION_IS_ONLINE;
  499. }
  500. }
  501. #endif
  502. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  503. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  504. struct vmem_altmap *altmap)
  505. {
  506. /* This will make the necessary allocations eventually. */
  507. return sparse_mem_map_populate(pnum, nid, altmap);
  508. }
  509. static void __kfree_section_memmap(struct page *memmap,
  510. struct vmem_altmap *altmap)
  511. {
  512. unsigned long start = (unsigned long)memmap;
  513. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  514. vmemmap_free(start, end, altmap);
  515. }
  516. #ifdef CONFIG_MEMORY_HOTREMOVE
  517. static void free_map_bootmem(struct page *memmap)
  518. {
  519. unsigned long start = (unsigned long)memmap;
  520. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  521. vmemmap_free(start, end, NULL);
  522. }
  523. #endif /* CONFIG_MEMORY_HOTREMOVE */
  524. #else
  525. static struct page *__kmalloc_section_memmap(void)
  526. {
  527. struct page *page, *ret;
  528. unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
  529. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  530. if (page)
  531. goto got_map_page;
  532. ret = vmalloc(memmap_size);
  533. if (ret)
  534. goto got_map_ptr;
  535. return NULL;
  536. got_map_page:
  537. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  538. got_map_ptr:
  539. return ret;
  540. }
  541. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  542. struct vmem_altmap *altmap)
  543. {
  544. return __kmalloc_section_memmap();
  545. }
  546. static void __kfree_section_memmap(struct page *memmap,
  547. struct vmem_altmap *altmap)
  548. {
  549. if (is_vmalloc_addr(memmap))
  550. vfree(memmap);
  551. else
  552. free_pages((unsigned long)memmap,
  553. get_order(sizeof(struct page) * PAGES_PER_SECTION));
  554. }
  555. #ifdef CONFIG_MEMORY_HOTREMOVE
  556. static void free_map_bootmem(struct page *memmap)
  557. {
  558. unsigned long maps_section_nr, removing_section_nr, i;
  559. unsigned long magic, nr_pages;
  560. struct page *page = virt_to_page(memmap);
  561. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  562. >> PAGE_SHIFT;
  563. for (i = 0; i < nr_pages; i++, page++) {
  564. magic = (unsigned long) page->freelist;
  565. BUG_ON(magic == NODE_INFO);
  566. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  567. removing_section_nr = page_private(page);
  568. /*
  569. * When this function is called, the removing section is
  570. * logical offlined state. This means all pages are isolated
  571. * from page allocator. If removing section's memmap is placed
  572. * on the same section, it must not be freed.
  573. * If it is freed, page allocator may allocate it which will
  574. * be removed physically soon.
  575. */
  576. if (maps_section_nr != removing_section_nr)
  577. put_page_bootmem(page);
  578. }
  579. }
  580. #endif /* CONFIG_MEMORY_HOTREMOVE */
  581. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  582. /*
  583. * returns the number of sections whose mem_maps were properly
  584. * set. If this is <=0, then that means that the passed-in
  585. * map was not consumed and must be freed.
  586. */
  587. int __meminit sparse_add_one_section(struct pglist_data *pgdat,
  588. unsigned long start_pfn, struct vmem_altmap *altmap)
  589. {
  590. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  591. struct mem_section *ms;
  592. struct page *memmap;
  593. unsigned long *usemap;
  594. unsigned long flags;
  595. int ret;
  596. /*
  597. * no locking for this, because it does its own
  598. * plus, it does a kmalloc
  599. */
  600. ret = sparse_index_init(section_nr, pgdat->node_id);
  601. if (ret < 0 && ret != -EEXIST)
  602. return ret;
  603. ret = 0;
  604. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, altmap);
  605. if (!memmap)
  606. return -ENOMEM;
  607. usemap = __kmalloc_section_usemap();
  608. if (!usemap) {
  609. __kfree_section_memmap(memmap, altmap);
  610. return -ENOMEM;
  611. }
  612. pgdat_resize_lock(pgdat, &flags);
  613. ms = __pfn_to_section(start_pfn);
  614. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  615. ret = -EEXIST;
  616. goto out;
  617. }
  618. /*
  619. * Poison uninitialized struct pages in order to catch invalid flags
  620. * combinations.
  621. */
  622. page_init_poison(memmap, sizeof(struct page) * PAGES_PER_SECTION);
  623. section_mark_present(ms);
  624. sparse_init_one_section(ms, section_nr, memmap, usemap);
  625. out:
  626. pgdat_resize_unlock(pgdat, &flags);
  627. if (ret < 0) {
  628. kfree(usemap);
  629. __kfree_section_memmap(memmap, altmap);
  630. }
  631. return ret;
  632. }
  633. #ifdef CONFIG_MEMORY_HOTREMOVE
  634. #ifdef CONFIG_MEMORY_FAILURE
  635. static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  636. {
  637. int i;
  638. if (!memmap)
  639. return;
  640. for (i = 0; i < nr_pages; i++) {
  641. if (PageHWPoison(&memmap[i])) {
  642. atomic_long_sub(1, &num_poisoned_pages);
  643. ClearPageHWPoison(&memmap[i]);
  644. }
  645. }
  646. }
  647. #else
  648. static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  649. {
  650. }
  651. #endif
  652. static void free_section_usemap(struct page *memmap, unsigned long *usemap,
  653. struct vmem_altmap *altmap)
  654. {
  655. struct page *usemap_page;
  656. if (!usemap)
  657. return;
  658. usemap_page = virt_to_page(usemap);
  659. /*
  660. * Check to see if allocation came from hot-plug-add
  661. */
  662. if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
  663. kfree(usemap);
  664. if (memmap)
  665. __kfree_section_memmap(memmap, altmap);
  666. return;
  667. }
  668. /*
  669. * The usemap came from bootmem. This is packed with other usemaps
  670. * on the section which has pgdat at boot time. Just keep it as is now.
  671. */
  672. if (memmap)
  673. free_map_bootmem(memmap);
  674. }
  675. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
  676. unsigned long map_offset, struct vmem_altmap *altmap)
  677. {
  678. struct page *memmap = NULL;
  679. unsigned long *usemap = NULL, flags;
  680. struct pglist_data *pgdat = zone->zone_pgdat;
  681. pgdat_resize_lock(pgdat, &flags);
  682. if (ms->section_mem_map) {
  683. usemap = ms->pageblock_flags;
  684. memmap = sparse_decode_mem_map(ms->section_mem_map,
  685. __section_nr(ms));
  686. ms->section_mem_map = 0;
  687. ms->pageblock_flags = NULL;
  688. }
  689. pgdat_resize_unlock(pgdat, &flags);
  690. clear_hwpoisoned_pages(memmap + map_offset,
  691. PAGES_PER_SECTION - map_offset);
  692. free_section_usemap(memmap, usemap, altmap);
  693. }
  694. #endif /* CONFIG_MEMORY_HOTREMOVE */
  695. #endif /* CONFIG_MEMORY_HOTPLUG */