sparse.c 21 KB

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