sparse.c 20 KB

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