sparse.c 20 KB

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