sparse.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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/highmem.h>
  9. #include <linux/export.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/vmalloc.h>
  12. #include "internal.h"
  13. #include <asm/dma.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/pgtable.h>
  16. /*
  17. * Permanent SPARSEMEM data:
  18. *
  19. * 1) mem_section - memory sections, mem_map's for valid memory
  20. */
  21. #ifdef CONFIG_SPARSEMEM_EXTREME
  22. struct mem_section *mem_section[NR_SECTION_ROOTS]
  23. ____cacheline_internodealigned_in_smp;
  24. #else
  25. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  26. ____cacheline_internodealigned_in_smp;
  27. #endif
  28. EXPORT_SYMBOL(mem_section);
  29. #ifdef NODE_NOT_IN_PAGE_FLAGS
  30. /*
  31. * If we did not store the node number in the page then we have to
  32. * do a lookup in the section_to_node_table in order to find which
  33. * node the page belongs to.
  34. */
  35. #if MAX_NUMNODES <= 256
  36. static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  37. #else
  38. static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  39. #endif
  40. int page_to_nid(const struct page *page)
  41. {
  42. return section_to_node_table[page_to_section(page)];
  43. }
  44. EXPORT_SYMBOL(page_to_nid);
  45. static void set_section_nid(unsigned long section_nr, int nid)
  46. {
  47. section_to_node_table[section_nr] = nid;
  48. }
  49. #else /* !NODE_NOT_IN_PAGE_FLAGS */
  50. static inline void set_section_nid(unsigned long section_nr, int nid)
  51. {
  52. }
  53. #endif
  54. #ifdef CONFIG_SPARSEMEM_EXTREME
  55. static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
  56. {
  57. struct mem_section *section = NULL;
  58. unsigned long array_size = SECTIONS_PER_ROOT *
  59. sizeof(struct mem_section);
  60. if (slab_is_available()) {
  61. if (node_state(nid, N_HIGH_MEMORY))
  62. section = kzalloc_node(array_size, GFP_KERNEL, nid);
  63. else
  64. section = kzalloc(array_size, GFP_KERNEL);
  65. } else {
  66. section = alloc_bootmem_node(NODE_DATA(nid), array_size);
  67. }
  68. return section;
  69. }
  70. static int __meminit sparse_index_init(unsigned long section_nr, int nid)
  71. {
  72. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  73. struct mem_section *section;
  74. if (mem_section[root])
  75. return -EEXIST;
  76. section = sparse_index_alloc(nid);
  77. if (!section)
  78. return -ENOMEM;
  79. mem_section[root] = section;
  80. return 0;
  81. }
  82. #else /* !SPARSEMEM_EXTREME */
  83. static inline int sparse_index_init(unsigned long section_nr, int nid)
  84. {
  85. return 0;
  86. }
  87. #endif
  88. /*
  89. * Although written for the SPARSEMEM_EXTREME case, this happens
  90. * to also work for the flat array case because
  91. * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
  92. */
  93. int __section_nr(struct mem_section* ms)
  94. {
  95. unsigned long root_nr;
  96. struct mem_section* root;
  97. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  98. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  99. if (!root)
  100. continue;
  101. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  102. break;
  103. }
  104. VM_BUG_ON(root_nr == NR_SECTION_ROOTS);
  105. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  106. }
  107. /*
  108. * During early boot, before section_mem_map is used for an actual
  109. * mem_map, we use section_mem_map to store the section's NUMA
  110. * node. This keeps us from having to use another data structure. The
  111. * node information is cleared just before we store the real mem_map.
  112. */
  113. static inline unsigned long sparse_encode_early_nid(int nid)
  114. {
  115. return (nid << SECTION_NID_SHIFT);
  116. }
  117. static inline int sparse_early_nid(struct mem_section *section)
  118. {
  119. return (section->section_mem_map >> SECTION_NID_SHIFT);
  120. }
  121. /* Validate the physical addressing limitations of the model */
  122. void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
  123. unsigned long *end_pfn)
  124. {
  125. unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
  126. /*
  127. * Sanity checks - do not allow an architecture to pass
  128. * in larger pfns than the maximum scope of sparsemem:
  129. */
  130. if (*start_pfn > max_sparsemem_pfn) {
  131. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  132. "Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  133. *start_pfn, *end_pfn, max_sparsemem_pfn);
  134. WARN_ON_ONCE(1);
  135. *start_pfn = max_sparsemem_pfn;
  136. *end_pfn = max_sparsemem_pfn;
  137. } else if (*end_pfn > max_sparsemem_pfn) {
  138. mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
  139. "End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
  140. *start_pfn, *end_pfn, max_sparsemem_pfn);
  141. WARN_ON_ONCE(1);
  142. *end_pfn = max_sparsemem_pfn;
  143. }
  144. }
  145. /* Record a memory area against a node. */
  146. void __init memory_present(int nid, unsigned long start, unsigned long end)
  147. {
  148. unsigned long pfn;
  149. start &= PAGE_SECTION_MASK;
  150. mminit_validate_memmodel_limits(&start, &end);
  151. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  152. unsigned long section = pfn_to_section_nr(pfn);
  153. struct mem_section *ms;
  154. sparse_index_init(section, nid);
  155. set_section_nid(section, nid);
  156. ms = __nr_to_section(section);
  157. if (!ms->section_mem_map)
  158. ms->section_mem_map = sparse_encode_early_nid(nid) |
  159. SECTION_MARKED_PRESENT;
  160. }
  161. }
  162. /*
  163. * Only used by the i386 NUMA architecures, but relatively
  164. * generic code.
  165. */
  166. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  167. unsigned long end_pfn)
  168. {
  169. unsigned long pfn;
  170. unsigned long nr_pages = 0;
  171. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  172. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  173. if (nid != early_pfn_to_nid(pfn))
  174. continue;
  175. if (pfn_present(pfn))
  176. nr_pages += PAGES_PER_SECTION;
  177. }
  178. return nr_pages * sizeof(struct page);
  179. }
  180. /*
  181. * Subtle, we encode the real pfn into the mem_map such that
  182. * the identity pfn - section_mem_map will return the actual
  183. * physical page frame number.
  184. */
  185. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  186. {
  187. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  188. }
  189. /*
  190. * Decode mem_map from the coded memmap
  191. */
  192. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  193. {
  194. /* mask off the extra low bits of information */
  195. coded_mem_map &= SECTION_MAP_MASK;
  196. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  197. }
  198. static int __meminit sparse_init_one_section(struct mem_section *ms,
  199. unsigned long pnum, struct page *mem_map,
  200. unsigned long *pageblock_bitmap)
  201. {
  202. if (!present_section(ms))
  203. return -EINVAL;
  204. ms->section_mem_map &= ~SECTION_MAP_MASK;
  205. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
  206. SECTION_HAS_MEM_MAP;
  207. ms->pageblock_flags = pageblock_bitmap;
  208. return 1;
  209. }
  210. unsigned long usemap_size(void)
  211. {
  212. unsigned long size_bytes;
  213. size_bytes = roundup(SECTION_BLOCKFLAGS_BITS, 8) / 8;
  214. size_bytes = roundup(size_bytes, sizeof(unsigned long));
  215. return size_bytes;
  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 retmain 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 = ___alloc_bootmem_node_nopanic(NODE_DATA(nid), size,
  246. SMP_CACHE_BYTES, goal, limit);
  247. if (!p && limit) {
  248. limit = 0;
  249. goto again;
  250. }
  251. return p;
  252. }
  253. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  254. {
  255. unsigned long usemap_snr, pgdat_snr;
  256. static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
  257. static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
  258. struct pglist_data *pgdat = NODE_DATA(nid);
  259. int usemap_nid;
  260. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  261. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  262. if (usemap_snr == pgdat_snr)
  263. return;
  264. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  265. /* skip redundant message */
  266. return;
  267. old_usemap_snr = usemap_snr;
  268. old_pgdat_snr = pgdat_snr;
  269. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  270. if (usemap_nid != nid) {
  271. printk(KERN_INFO
  272. "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. printk(KERN_INFO "Section %ld and %ld (node %d)", usemap_snr,
  283. pgdat_snr, nid);
  284. printk(KERN_CONT
  285. " have a circular dependency on usemap and pgdat allocations\n");
  286. }
  287. #else
  288. static unsigned long * __init
  289. sparse_early_usemaps_alloc_pgdat_section(struct pglist_data *pgdat,
  290. unsigned long size)
  291. {
  292. return alloc_bootmem_node_nopanic(pgdat, size);
  293. }
  294. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  295. {
  296. }
  297. #endif /* CONFIG_MEMORY_HOTREMOVE */
  298. static void __init sparse_early_usemaps_alloc_node(void *data,
  299. unsigned long pnum_begin,
  300. unsigned long pnum_end,
  301. unsigned long usemap_count, int nodeid)
  302. {
  303. void *usemap;
  304. unsigned long pnum;
  305. unsigned long **usemap_map = (unsigned long **)data;
  306. int size = usemap_size();
  307. usemap = sparse_early_usemaps_alloc_pgdat_section(NODE_DATA(nodeid),
  308. size * usemap_count);
  309. if (!usemap) {
  310. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  311. return;
  312. }
  313. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  314. if (!present_section_nr(pnum))
  315. continue;
  316. usemap_map[pnum] = usemap;
  317. usemap += size;
  318. check_usemap_section_nr(nodeid, usemap_map[pnum]);
  319. }
  320. }
  321. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  322. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  323. {
  324. struct page *map;
  325. unsigned long size;
  326. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  327. if (map)
  328. return map;
  329. size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  330. map = __alloc_bootmem_node_high(NODE_DATA(nid), size,
  331. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  332. return map;
  333. }
  334. void __init sparse_mem_maps_populate_node(struct page **map_map,
  335. unsigned long pnum_begin,
  336. unsigned long pnum_end,
  337. unsigned long map_count, int nodeid)
  338. {
  339. void *map;
  340. unsigned long pnum;
  341. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  342. map = alloc_remap(nodeid, size * map_count);
  343. if (map) {
  344. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  345. if (!present_section_nr(pnum))
  346. continue;
  347. map_map[pnum] = map;
  348. map += size;
  349. }
  350. return;
  351. }
  352. size = PAGE_ALIGN(size);
  353. map = __alloc_bootmem_node_high(NODE_DATA(nodeid), size * map_count,
  354. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  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. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  374. "some memory will not be available.\n", __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. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  399. "some memory will not be available.\n", __func__);
  400. ms->section_mem_map = 0;
  401. return NULL;
  402. }
  403. #endif
  404. void __attribute__((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 = alloc_bootmem(size);
  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 = alloc_bootmem(size2);
  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. free_bootmem(__pa(map_map), size2);
  515. #endif
  516. free_bootmem(__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. unsigned long nr_pages)
  522. {
  523. /* This will make the necessary allocations eventually. */
  524. return sparse_mem_map_populate(pnum, nid);
  525. }
  526. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  527. {
  528. unsigned long start = (unsigned long)memmap;
  529. unsigned long end = (unsigned long)(memmap + nr_pages);
  530. vmemmap_free(start, end);
  531. }
  532. #ifdef CONFIG_MEMORY_HOTREMOVE
  533. static void free_map_bootmem(struct page *memmap, unsigned long nr_pages)
  534. {
  535. unsigned long start = (unsigned long)memmap;
  536. unsigned long end = (unsigned long)(memmap + nr_pages);
  537. vmemmap_free(start, end);
  538. }
  539. #endif /* CONFIG_MEMORY_HOTREMOVE */
  540. #else
  541. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  542. {
  543. struct page *page, *ret;
  544. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  545. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  546. if (page)
  547. goto got_map_page;
  548. ret = vmalloc(memmap_size);
  549. if (ret)
  550. goto got_map_ptr;
  551. return NULL;
  552. got_map_page:
  553. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  554. got_map_ptr:
  555. return ret;
  556. }
  557. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  558. unsigned long nr_pages)
  559. {
  560. return __kmalloc_section_memmap(nr_pages);
  561. }
  562. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  563. {
  564. if (is_vmalloc_addr(memmap))
  565. vfree(memmap);
  566. else
  567. free_pages((unsigned long)memmap,
  568. get_order(sizeof(struct page) * nr_pages));
  569. }
  570. #ifdef CONFIG_MEMORY_HOTREMOVE
  571. static void free_map_bootmem(struct page *memmap, unsigned long nr_pages)
  572. {
  573. unsigned long maps_section_nr, removing_section_nr, i;
  574. unsigned long magic;
  575. struct page *page = virt_to_page(memmap);
  576. for (i = 0; i < nr_pages; i++, page++) {
  577. magic = (unsigned long) page->lru.next;
  578. BUG_ON(magic == NODE_INFO);
  579. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  580. removing_section_nr = page->private;
  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. int nr_pages)
  602. {
  603. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  604. struct pglist_data *pgdat = zone->zone_pgdat;
  605. struct mem_section *ms;
  606. struct page *memmap;
  607. unsigned long *usemap;
  608. unsigned long flags;
  609. int ret;
  610. /*
  611. * no locking for this, because it does its own
  612. * plus, it does a kmalloc
  613. */
  614. ret = sparse_index_init(section_nr, pgdat->node_id);
  615. if (ret < 0 && ret != -EEXIST)
  616. return ret;
  617. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  618. if (!memmap)
  619. return -ENOMEM;
  620. usemap = __kmalloc_section_usemap();
  621. if (!usemap) {
  622. __kfree_section_memmap(memmap, nr_pages);
  623. return -ENOMEM;
  624. }
  625. pgdat_resize_lock(pgdat, &flags);
  626. ms = __pfn_to_section(start_pfn);
  627. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  628. ret = -EEXIST;
  629. goto out;
  630. }
  631. memset(memmap, 0, sizeof(struct page) * nr_pages);
  632. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  633. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  634. out:
  635. pgdat_resize_unlock(pgdat, &flags);
  636. if (ret <= 0) {
  637. kfree(usemap);
  638. __kfree_section_memmap(memmap, nr_pages);
  639. }
  640. return ret;
  641. }
  642. #ifdef CONFIG_MEMORY_HOTREMOVE
  643. #ifdef CONFIG_MEMORY_FAILURE
  644. static void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  645. {
  646. int i;
  647. if (!memmap)
  648. return;
  649. for (i = 0; i < PAGES_PER_SECTION; i++) {
  650. if (PageHWPoison(&memmap[i])) {
  651. atomic_long_sub(1, &num_poisoned_pages);
  652. ClearPageHWPoison(&memmap[i]);
  653. }
  654. }
  655. }
  656. #else
  657. static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages)
  658. {
  659. }
  660. #endif
  661. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  662. {
  663. struct page *usemap_page;
  664. unsigned long nr_pages;
  665. if (!usemap)
  666. return;
  667. usemap_page = virt_to_page(usemap);
  668. /*
  669. * Check to see if allocation came from hot-plug-add
  670. */
  671. if (PageSlab(usemap_page) || PageCompound(usemap_page)) {
  672. kfree(usemap);
  673. if (memmap)
  674. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  675. return;
  676. }
  677. /*
  678. * The usemap came from bootmem. This is packed with other usemaps
  679. * on the section which has pgdat at boot time. Just keep it as is now.
  680. */
  681. if (memmap) {
  682. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  683. >> PAGE_SHIFT;
  684. free_map_bootmem(memmap, nr_pages);
  685. }
  686. }
  687. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  688. {
  689. struct page *memmap = NULL;
  690. unsigned long *usemap = NULL, flags;
  691. struct pglist_data *pgdat = zone->zone_pgdat;
  692. pgdat_resize_lock(pgdat, &flags);
  693. if (ms->section_mem_map) {
  694. usemap = ms->pageblock_flags;
  695. memmap = sparse_decode_mem_map(ms->section_mem_map,
  696. __section_nr(ms));
  697. ms->section_mem_map = 0;
  698. ms->pageblock_flags = NULL;
  699. }
  700. pgdat_resize_unlock(pgdat, &flags);
  701. clear_hwpoisoned_pages(memmap, PAGES_PER_SECTION);
  702. free_section_usemap(memmap, usemap);
  703. }
  704. #endif /* CONFIG_MEMORY_HOTREMOVE */
  705. #endif /* CONFIG_MEMORY_HOTPLUG */