sparse.c 23 KB

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