sparse.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 >= 0) && \
  173. (section_nr <= __highest_present_section_nr)); \
  174. section_nr = next_present_section_nr(section_nr))
  175. /*
  176. * Record how many memory sections are marked as present
  177. * during system bootup.
  178. */
  179. static int __initdata nr_present_sections;
  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_virt_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. nr_present_sections++;
  205. }
  206. }
  207. }
  208. /*
  209. * Subtle, we encode the real pfn into the mem_map such that
  210. * the identity pfn - section_mem_map will return the actual
  211. * physical page frame number.
  212. */
  213. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  214. {
  215. unsigned long coded_mem_map =
  216. (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  217. BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL<<PFN_SECTION_SHIFT));
  218. BUG_ON(coded_mem_map & ~SECTION_MAP_MASK);
  219. return coded_mem_map;
  220. }
  221. /*
  222. * Decode mem_map from the coded memmap
  223. */
  224. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  225. {
  226. /* mask off the extra low bits of information */
  227. coded_mem_map &= SECTION_MAP_MASK;
  228. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  229. }
  230. static void __meminit sparse_init_one_section(struct mem_section *ms,
  231. unsigned long pnum, struct page *mem_map,
  232. unsigned long *pageblock_bitmap)
  233. {
  234. ms->section_mem_map &= ~SECTION_MAP_MASK;
  235. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
  236. SECTION_HAS_MEM_MAP;
  237. ms->pageblock_flags = pageblock_bitmap;
  238. }
  239. unsigned long usemap_size(void)
  240. {
  241. return BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS) * sizeof(unsigned long);
  242. }
  243. #ifdef CONFIG_MEMORY_HOTPLUG
  244. static unsigned long *__kmalloc_section_usemap(void)
  245. {
  246. return kmalloc(usemap_size(), GFP_KERNEL);
  247. }
  248. #endif /* CONFIG_MEMORY_HOTPLUG */
  249. #ifdef CONFIG_MEMORY_HOTREMOVE
  250. static unsigned long * __init
  251. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  252. unsigned long size)
  253. {
  254. unsigned long goal, limit;
  255. unsigned long *p;
  256. int nid;
  257. /*
  258. * A page may contain usemaps for other sections preventing the
  259. * page being freed and making a section unremovable while
  260. * other sections referencing the usemap remain active. Similarly,
  261. * a pgdat can prevent a section being removed. If section A
  262. * contains a pgdat and section B contains the usemap, both
  263. * sections become inter-dependent. This allocates usemaps
  264. * from the same section as the pgdat where possible to avoid
  265. * this problem.
  266. */
  267. goal = __pa(pgdat) & (PAGE_SECTION_MASK << PAGE_SHIFT);
  268. limit = goal + (1UL << PA_SECTION_SHIFT);
  269. nid = early_pfn_to_nid(goal >> PAGE_SHIFT);
  270. again:
  271. p = memblock_virt_alloc_try_nid_nopanic(size,
  272. SMP_CACHE_BYTES, goal, limit,
  273. nid);
  274. if (!p && limit) {
  275. limit = 0;
  276. goto again;
  277. }
  278. return p;
  279. }
  280. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  281. {
  282. unsigned long usemap_snr, pgdat_snr;
  283. static unsigned long old_usemap_snr;
  284. static unsigned long old_pgdat_snr;
  285. struct pglist_data *pgdat = NODE_DATA(nid);
  286. int usemap_nid;
  287. /* First call */
  288. if (!old_usemap_snr) {
  289. old_usemap_snr = NR_MEM_SECTIONS;
  290. old_pgdat_snr = NR_MEM_SECTIONS;
  291. }
  292. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  293. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  294. if (usemap_snr == pgdat_snr)
  295. return;
  296. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  297. /* skip redundant message */
  298. return;
  299. old_usemap_snr = usemap_snr;
  300. old_pgdat_snr = pgdat_snr;
  301. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  302. if (usemap_nid != nid) {
  303. pr_info("node %d must be removed before remove section %ld\n",
  304. nid, usemap_snr);
  305. return;
  306. }
  307. /*
  308. * There is a circular dependency.
  309. * Some platforms allow un-removable section because they will just
  310. * gather other removable sections for dynamic partitioning.
  311. * Just notify un-removable section's number here.
  312. */
  313. pr_info("Section %ld and %ld (node %d) have a circular dependency on usemap and pgdat allocations\n",
  314. usemap_snr, pgdat_snr, nid);
  315. }
  316. #else
  317. static unsigned long * __init
  318. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  319. unsigned long size)
  320. {
  321. return memblock_virt_alloc_node_nopanic(size, pgdat->node_id);
  322. }
  323. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  324. {
  325. }
  326. #endif /* CONFIG_MEMORY_HOTREMOVE */
  327. static void __init sparse_early_usemaps_alloc_node(void *data,
  328. unsigned long pnum_begin,
  329. unsigned long pnum_end,
  330. unsigned long usemap_count, int nodeid)
  331. {
  332. void *usemap;
  333. unsigned long pnum;
  334. unsigned long **usemap_map = (unsigned long **)data;
  335. int size = usemap_size();
  336. int nr_consumed_maps = 0;
  337. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
  338. size * usemap_count);
  339. if (!usemap) {
  340. pr_warn("%s: allocation failed\n", __func__);
  341. return;
  342. }
  343. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  344. if (!present_section_nr(pnum))
  345. continue;
  346. usemap_map[nr_consumed_maps] = usemap;
  347. usemap += size;
  348. check_usemap_section_nr(nodeid, usemap_map[nr_consumed_maps]);
  349. nr_consumed_maps++;
  350. }
  351. }
  352. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  353. static unsigned long __init section_map_size(void)
  354. {
  355. return ALIGN(sizeof(struct page) * PAGES_PER_SECTION, PMD_SIZE);
  356. }
  357. #else
  358. static unsigned long __init section_map_size(void)
  359. {
  360. return PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  361. }
  362. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid,
  363. struct vmem_altmap *altmap)
  364. {
  365. unsigned long size = section_map_size();
  366. struct page *map = sparse_buffer_alloc(size);
  367. if (map)
  368. return map;
  369. map = memblock_virt_alloc_try_nid(size,
  370. PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
  371. BOOTMEM_ALLOC_ACCESSIBLE, nid);
  372. return map;
  373. }
  374. void __init sparse_mem_maps_populate_node(struct page **map_map,
  375. unsigned long pnum_begin,
  376. unsigned long pnum_end,
  377. unsigned long map_count, int nodeid)
  378. {
  379. unsigned long pnum;
  380. int nr_consumed_maps = 0;
  381. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  382. if (!present_section_nr(pnum))
  383. continue;
  384. map_map[nr_consumed_maps] =
  385. sparse_mem_map_populate(pnum, nodeid, NULL);
  386. if (map_map[nr_consumed_maps++])
  387. continue;
  388. pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
  389. __func__);
  390. }
  391. }
  392. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  393. static void *sparsemap_buf __meminitdata;
  394. static void *sparsemap_buf_end __meminitdata;
  395. static void __init sparse_buffer_init(unsigned long size, int nid)
  396. {
  397. WARN_ON(sparsemap_buf); /* forgot to call sparse_buffer_fini()? */
  398. sparsemap_buf =
  399. memblock_virt_alloc_try_nid_raw(size, PAGE_SIZE,
  400. __pa(MAX_DMA_ADDRESS),
  401. BOOTMEM_ALLOC_ACCESSIBLE, nid);
  402. sparsemap_buf_end = sparsemap_buf + size;
  403. }
  404. static void __init sparse_buffer_fini(void)
  405. {
  406. unsigned long size = sparsemap_buf_end - sparsemap_buf;
  407. if (sparsemap_buf && size > 0)
  408. memblock_free_early(__pa(sparsemap_buf), size);
  409. sparsemap_buf = NULL;
  410. }
  411. void * __meminit sparse_buffer_alloc(unsigned long size)
  412. {
  413. void *ptr = NULL;
  414. if (sparsemap_buf) {
  415. ptr = PTR_ALIGN(sparsemap_buf, size);
  416. if (ptr + size > sparsemap_buf_end)
  417. ptr = NULL;
  418. else
  419. sparsemap_buf = ptr + size;
  420. }
  421. return ptr;
  422. }
  423. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  424. static void __init sparse_early_mem_maps_alloc_node(void *data,
  425. unsigned long pnum_begin,
  426. unsigned long pnum_end,
  427. unsigned long map_count, int nodeid)
  428. {
  429. struct page **map_map = (struct page **)data;
  430. sparse_buffer_init(section_map_size() * map_count, nodeid);
  431. sparse_mem_maps_populate_node(map_map, pnum_begin, pnum_end,
  432. map_count, nodeid);
  433. sparse_buffer_fini();
  434. }
  435. #else
  436. static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  437. {
  438. struct page *map;
  439. struct mem_section *ms = __nr_to_section(pnum);
  440. int nid = sparse_early_nid(ms);
  441. map = sparse_mem_map_populate(pnum, nid, NULL);
  442. if (map)
  443. return map;
  444. pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
  445. __func__);
  446. return NULL;
  447. }
  448. #endif
  449. void __weak __meminit vmemmap_populate_print_last(void)
  450. {
  451. }
  452. /**
  453. * alloc_usemap_and_memmap - memory alloction for pageblock flags and vmemmap
  454. * @map: usemap_map for pageblock flags or mmap_map for vmemmap
  455. * @unit_size: size of map unit
  456. */
  457. static void __init alloc_usemap_and_memmap(void (*alloc_func)
  458. (void *, unsigned long, unsigned long,
  459. unsigned long, int), void *data,
  460. int data_unit_size)
  461. {
  462. unsigned long pnum;
  463. unsigned long map_count;
  464. int nodeid_begin = 0;
  465. unsigned long pnum_begin = 0;
  466. for_each_present_section_nr(0, pnum) {
  467. struct mem_section *ms;
  468. ms = __nr_to_section(pnum);
  469. nodeid_begin = sparse_early_nid(ms);
  470. pnum_begin = pnum;
  471. break;
  472. }
  473. map_count = 1;
  474. for_each_present_section_nr(pnum_begin + 1, pnum) {
  475. struct mem_section *ms;
  476. int nodeid;
  477. ms = __nr_to_section(pnum);
  478. nodeid = sparse_early_nid(ms);
  479. if (nodeid == nodeid_begin) {
  480. map_count++;
  481. continue;
  482. }
  483. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  484. alloc_func(data, pnum_begin, pnum,
  485. map_count, nodeid_begin);
  486. /* new start, update count etc*/
  487. nodeid_begin = nodeid;
  488. pnum_begin = pnum;
  489. data += map_count * data_unit_size;
  490. map_count = 1;
  491. }
  492. /* ok, last chunk */
  493. alloc_func(data, pnum_begin, __highest_present_section_nr+1,
  494. map_count, nodeid_begin);
  495. }
  496. /*
  497. * Allocate the accumulated non-linear sections, allocate a mem_map
  498. * for each and record the physical to section mapping.
  499. */
  500. void __init sparse_init(void)
  501. {
  502. unsigned long pnum;
  503. struct page *map;
  504. unsigned long *usemap;
  505. unsigned long **usemap_map;
  506. int size;
  507. int nr_consumed_maps = 0;
  508. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  509. int size2;
  510. struct page **map_map;
  511. #endif
  512. /* see include/linux/mmzone.h 'struct mem_section' definition */
  513. BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
  514. /* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */
  515. set_pageblock_order();
  516. /*
  517. * map is using big page (aka 2M in x86 64 bit)
  518. * usemap is less one page (aka 24 bytes)
  519. * so alloc 2M (with 2M align) and 24 bytes in turn will
  520. * make next 2M slip to one more 2M later.
  521. * then in big system, the memory will have a lot of holes...
  522. * here try to allocate 2M pages continuously.
  523. *
  524. * powerpc need to call sparse_init_one_section right after each
  525. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  526. */
  527. size = sizeof(unsigned long *) * nr_present_sections;
  528. usemap_map = memblock_virt_alloc(size, 0);
  529. if (!usemap_map)
  530. panic("can not allocate usemap_map\n");
  531. alloc_usemap_and_memmap(sparse_early_usemaps_alloc_node,
  532. (void *)usemap_map,
  533. sizeof(usemap_map[0]));
  534. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  535. size2 = sizeof(struct page *) * nr_present_sections;
  536. map_map = memblock_virt_alloc(size2, 0);
  537. if (!map_map)
  538. panic("can not allocate map_map\n");
  539. alloc_usemap_and_memmap(sparse_early_mem_maps_alloc_node,
  540. (void *)map_map,
  541. sizeof(map_map[0]));
  542. #endif
  543. /*
  544. * The number of present sections stored in nr_present_sections
  545. * are kept the same since mem sections are marked as present in
  546. * memory_present(). In this for loop, we need check which sections
  547. * failed to allocate memmap or usemap, then clear its
  548. * ->section_mem_map accordingly. During this process, we need
  549. * increase 'nr_consumed_maps' whether its allocation of memmap
  550. * or usemap failed or not, so that after we handle the i-th
  551. * memory section, can get memmap and usemap of (i+1)-th section
  552. * correctly.
  553. */
  554. for_each_present_section_nr(0, pnum) {
  555. struct mem_section *ms;
  556. if (nr_consumed_maps >= nr_present_sections) {
  557. pr_err("nr_consumed_maps goes beyond nr_present_sections\n");
  558. break;
  559. }
  560. ms = __nr_to_section(pnum);
  561. usemap = usemap_map[nr_consumed_maps];
  562. if (!usemap) {
  563. ms->section_mem_map = 0;
  564. nr_consumed_maps++;
  565. continue;
  566. }
  567. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  568. map = map_map[nr_consumed_maps];
  569. #else
  570. map = sparse_early_mem_map_alloc(pnum);
  571. #endif
  572. if (!map) {
  573. ms->section_mem_map = 0;
  574. nr_consumed_maps++;
  575. continue;
  576. }
  577. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  578. usemap);
  579. nr_consumed_maps++;
  580. }
  581. vmemmap_populate_print_last();
  582. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  583. memblock_free_early(__pa(map_map), size2);
  584. #endif
  585. memblock_free_early(__pa(usemap_map), size);
  586. }
  587. #ifdef CONFIG_MEMORY_HOTPLUG
  588. /* Mark all memory sections within the pfn range as online */
  589. void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
  590. {
  591. unsigned long pfn;
  592. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  593. unsigned long section_nr = pfn_to_section_nr(pfn);
  594. struct mem_section *ms;
  595. /* onlining code should never touch invalid ranges */
  596. if (WARN_ON(!valid_section_nr(section_nr)))
  597. continue;
  598. ms = __nr_to_section(section_nr);
  599. ms->section_mem_map |= SECTION_IS_ONLINE;
  600. }
  601. }
  602. #ifdef CONFIG_MEMORY_HOTREMOVE
  603. /* Mark all memory sections within the pfn range as online */
  604. void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
  605. {
  606. unsigned long pfn;
  607. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  608. unsigned long section_nr = pfn_to_section_nr(pfn);
  609. struct mem_section *ms;
  610. /*
  611. * TODO this needs some double checking. Offlining code makes
  612. * sure to check pfn_valid but those checks might be just bogus
  613. */
  614. if (WARN_ON(!valid_section_nr(section_nr)))
  615. continue;
  616. ms = __nr_to_section(section_nr);
  617. ms->section_mem_map &= ~SECTION_IS_ONLINE;
  618. }
  619. }
  620. #endif
  621. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  622. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  623. struct vmem_altmap *altmap)
  624. {
  625. /* This will make the necessary allocations eventually. */
  626. return sparse_mem_map_populate(pnum, nid, altmap);
  627. }
  628. static void __kfree_section_memmap(struct page *memmap,
  629. struct vmem_altmap *altmap)
  630. {
  631. unsigned long start = (unsigned long)memmap;
  632. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  633. vmemmap_free(start, end, altmap);
  634. }
  635. #ifdef CONFIG_MEMORY_HOTREMOVE
  636. static void free_map_bootmem(struct page *memmap)
  637. {
  638. unsigned long start = (unsigned long)memmap;
  639. unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
  640. vmemmap_free(start, end, NULL);
  641. }
  642. #endif /* CONFIG_MEMORY_HOTREMOVE */
  643. #else
  644. static struct page *__kmalloc_section_memmap(void)
  645. {
  646. struct page *page, *ret;
  647. unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
  648. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  649. if (page)
  650. goto got_map_page;
  651. ret = vmalloc(memmap_size);
  652. if (ret)
  653. goto got_map_ptr;
  654. return NULL;
  655. got_map_page:
  656. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  657. got_map_ptr:
  658. return ret;
  659. }
  660. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  661. struct vmem_altmap *altmap)
  662. {
  663. return __kmalloc_section_memmap();
  664. }
  665. static void __kfree_section_memmap(struct page *memmap,
  666. struct vmem_altmap *altmap)
  667. {
  668. if (is_vmalloc_addr(memmap))
  669. vfree(memmap);
  670. else
  671. free_pages((unsigned long)memmap,
  672. get_order(sizeof(struct page) * PAGES_PER_SECTION));
  673. }
  674. #ifdef CONFIG_MEMORY_HOTREMOVE
  675. static void free_map_bootmem(struct page *memmap)
  676. {
  677. unsigned long maps_section_nr, removing_section_nr, i;
  678. unsigned long magic, nr_pages;
  679. struct page *page = virt_to_page(memmap);
  680. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  681. >> PAGE_SHIFT;
  682. for (i = 0; i < nr_pages; i++, page++) {
  683. magic = (unsigned long) page->freelist;
  684. BUG_ON(magic == NODE_INFO);
  685. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  686. removing_section_nr = page_private(page);
  687. /*
  688. * When this function is called, the removing section is
  689. * logical offlined state. This means all pages are isolated
  690. * from page allocator. If removing section's memmap is placed
  691. * on the same section, it must not be freed.
  692. * If it is freed, page allocator may allocate it which will
  693. * be removed physically soon.
  694. */
  695. if (maps_section_nr != removing_section_nr)
  696. put_page_bootmem(page);
  697. }
  698. }
  699. #endif /* CONFIG_MEMORY_HOTREMOVE */
  700. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  701. /*
  702. * returns the number of sections whose mem_maps were properly
  703. * set. If this is <=0, then that means that the passed-in
  704. * map was not consumed and must be freed.
  705. */
  706. int __meminit sparse_add_one_section(struct pglist_data *pgdat,
  707. unsigned long start_pfn, struct vmem_altmap *altmap)
  708. {
  709. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  710. struct mem_section *ms;
  711. struct page *memmap;
  712. unsigned long *usemap;
  713. unsigned long flags;
  714. int ret;
  715. /*
  716. * no locking for this, because it does its own
  717. * plus, it does a kmalloc
  718. */
  719. ret = sparse_index_init(section_nr, pgdat->node_id);
  720. if (ret < 0 && ret != -EEXIST)
  721. return ret;
  722. ret = 0;
  723. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, altmap);
  724. if (!memmap)
  725. return -ENOMEM;
  726. usemap = __kmalloc_section_usemap();
  727. if (!usemap) {
  728. __kfree_section_memmap(memmap, altmap);
  729. return -ENOMEM;
  730. }
  731. pgdat_resize_lock(pgdat, &flags);
  732. ms = __pfn_to_section(start_pfn);
  733. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  734. ret = -EEXIST;
  735. goto out;
  736. }
  737. #ifdef CONFIG_DEBUG_VM
  738. /*
  739. * Poison uninitialized struct pages in order to catch invalid flags
  740. * combinations.
  741. */
  742. memset(memmap, PAGE_POISON_PATTERN, sizeof(struct page) * PAGES_PER_SECTION);
  743. #endif
  744. section_mark_present(ms);
  745. sparse_init_one_section(ms, section_nr, memmap, usemap);
  746. out:
  747. pgdat_resize_unlock(pgdat, &flags);
  748. if (ret < 0) {
  749. kfree(usemap);
  750. __kfree_section_memmap(memmap, altmap);
  751. }
  752. return ret;
  753. }
  754. #ifdef CONFIG_MEMORY_HOTREMOVE
  755. #ifdef CONFIG_MEMORY_FAILURE
  756. static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  757. {
  758. int i;
  759. if (!memmap)
  760. return;
  761. for (i = 0; i < nr_pages; i++) {
  762. if (PageHWPoison(&memmap[i])) {
  763. atomic_long_sub(1, &num_poisoned_pages);
  764. ClearPageHWPoison(&memmap[i]);
  765. }
  766. }
  767. }
  768. #else
  769. static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  770. {
  771. }
  772. #endif
  773. static void free_section_usemap(struct page *memmap, unsigned long *usemap,
  774. struct vmem_altmap *altmap)
  775. {
  776. struct page *usemap_page;
  777. if (!usemap)
  778. return;
  779. usemap_page = virt_to_page(usemap);
  780. /*
  781. * Check to see if allocation came from hot-plug-add
  782. */
  783. if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
  784. kfree(usemap);
  785. if (memmap)
  786. __kfree_section_memmap(memmap, altmap);
  787. return;
  788. }
  789. /*
  790. * The usemap came from bootmem. This is packed with other usemaps
  791. * on the section which has pgdat at boot time. Just keep it as is now.
  792. */
  793. if (memmap)
  794. free_map_bootmem(memmap);
  795. }
  796. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
  797. unsigned long map_offset, struct vmem_altmap *altmap)
  798. {
  799. struct page *memmap = NULL;
  800. unsigned long *usemap = NULL, flags;
  801. struct pglist_data *pgdat = zone->zone_pgdat;
  802. pgdat_resize_lock(pgdat, &flags);
  803. if (ms->section_mem_map) {
  804. usemap = ms->pageblock_flags;
  805. memmap = sparse_decode_mem_map(ms->section_mem_map,
  806. __section_nr(ms));
  807. ms->section_mem_map = 0;
  808. ms->pageblock_flags = NULL;
  809. }
  810. pgdat_resize_unlock(pgdat, &flags);
  811. clear_hwpoisoned_pages(memmap + map_offset,
  812. PAGES_PER_SECTION - map_offset);
  813. free_section_usemap(memmap, usemap, altmap);
  814. }
  815. #endif /* CONFIG_MEMORY_HOTREMOVE */
  816. #endif /* CONFIG_MEMORY_HOTPLUG */