sparse.c 21 KB

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