sparse.c 23 KB

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