sparse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. usemap = alloc_bootmem_node(NODE_DATA(nodeid), size * usemap_count);
  312. if (!usemap) {
  313. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  314. return;
  315. }
  316. }
  317. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  318. if (!present_section_nr(pnum))
  319. continue;
  320. usemap_map[pnum] = usemap;
  321. usemap += size;
  322. check_usemap_section_nr(nodeid, usemap_map[pnum]);
  323. }
  324. }
  325. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  326. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  327. {
  328. struct page *map;
  329. unsigned long size;
  330. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  331. if (map)
  332. return map;
  333. size = PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION);
  334. map = __alloc_bootmem_node_high(NODE_DATA(nid), size,
  335. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  336. return map;
  337. }
  338. void __init sparse_mem_maps_populate_node(struct page **map_map,
  339. unsigned long pnum_begin,
  340. unsigned long pnum_end,
  341. unsigned long map_count, int nodeid)
  342. {
  343. void *map;
  344. unsigned long pnum;
  345. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  346. map = alloc_remap(nodeid, size * map_count);
  347. if (map) {
  348. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  349. if (!present_section_nr(pnum))
  350. continue;
  351. map_map[pnum] = map;
  352. map += size;
  353. }
  354. return;
  355. }
  356. size = PAGE_ALIGN(size);
  357. map = __alloc_bootmem_node_high(NODE_DATA(nodeid), size * map_count,
  358. PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  359. if (map) {
  360. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  361. if (!present_section_nr(pnum))
  362. continue;
  363. map_map[pnum] = map;
  364. map += size;
  365. }
  366. return;
  367. }
  368. /* fallback */
  369. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  370. struct mem_section *ms;
  371. if (!present_section_nr(pnum))
  372. continue;
  373. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  374. if (map_map[pnum])
  375. continue;
  376. ms = __nr_to_section(pnum);
  377. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  378. "some memory will not be available.\n", __func__);
  379. ms->section_mem_map = 0;
  380. }
  381. }
  382. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  383. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  384. static void __init sparse_early_mem_maps_alloc_node(struct page **map_map,
  385. unsigned long pnum_begin,
  386. unsigned long pnum_end,
  387. unsigned long map_count, int nodeid)
  388. {
  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. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  402. "some memory will not be available.\n", __func__);
  403. ms->section_mem_map = 0;
  404. return NULL;
  405. }
  406. #endif
  407. void __attribute__((weak)) __meminit vmemmap_populate_print_last(void)
  408. {
  409. }
  410. /*
  411. * Allocate the accumulated non-linear sections, allocate a mem_map
  412. * for each and record the physical to section mapping.
  413. */
  414. void __init sparse_init(void)
  415. {
  416. unsigned long pnum;
  417. struct page *map;
  418. unsigned long *usemap;
  419. unsigned long **usemap_map;
  420. int size;
  421. int nodeid_begin = 0;
  422. unsigned long pnum_begin = 0;
  423. unsigned long usemap_count;
  424. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  425. unsigned long map_count;
  426. int size2;
  427. struct page **map_map;
  428. #endif
  429. /*
  430. * map is using big page (aka 2M in x86 64 bit)
  431. * usemap is less one page (aka 24 bytes)
  432. * so alloc 2M (with 2M align) and 24 bytes in turn will
  433. * make next 2M slip to one more 2M later.
  434. * then in big system, the memory will have a lot of holes...
  435. * here try to allocate 2M pages continuously.
  436. *
  437. * powerpc need to call sparse_init_one_section right after each
  438. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  439. */
  440. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  441. usemap_map = alloc_bootmem(size);
  442. if (!usemap_map)
  443. panic("can not allocate usemap_map\n");
  444. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  445. struct mem_section *ms;
  446. if (!present_section_nr(pnum))
  447. continue;
  448. ms = __nr_to_section(pnum);
  449. nodeid_begin = sparse_early_nid(ms);
  450. pnum_begin = pnum;
  451. break;
  452. }
  453. usemap_count = 1;
  454. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  455. struct mem_section *ms;
  456. int nodeid;
  457. if (!present_section_nr(pnum))
  458. continue;
  459. ms = __nr_to_section(pnum);
  460. nodeid = sparse_early_nid(ms);
  461. if (nodeid == nodeid_begin) {
  462. usemap_count++;
  463. continue;
  464. }
  465. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  466. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, pnum,
  467. usemap_count, nodeid_begin);
  468. /* new start, update count etc*/
  469. nodeid_begin = nodeid;
  470. pnum_begin = pnum;
  471. usemap_count = 1;
  472. }
  473. /* ok, last chunk */
  474. sparse_early_usemaps_alloc_node(usemap_map, pnum_begin, NR_MEM_SECTIONS,
  475. usemap_count, nodeid_begin);
  476. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  477. size2 = sizeof(struct page *) * NR_MEM_SECTIONS;
  478. map_map = alloc_bootmem(size2);
  479. if (!map_map)
  480. panic("can not allocate map_map\n");
  481. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  482. struct mem_section *ms;
  483. if (!present_section_nr(pnum))
  484. continue;
  485. ms = __nr_to_section(pnum);
  486. nodeid_begin = sparse_early_nid(ms);
  487. pnum_begin = pnum;
  488. break;
  489. }
  490. map_count = 1;
  491. for (pnum = pnum_begin + 1; pnum < NR_MEM_SECTIONS; pnum++) {
  492. struct mem_section *ms;
  493. int nodeid;
  494. if (!present_section_nr(pnum))
  495. continue;
  496. ms = __nr_to_section(pnum);
  497. nodeid = sparse_early_nid(ms);
  498. if (nodeid == nodeid_begin) {
  499. map_count++;
  500. continue;
  501. }
  502. /* ok, we need to take cake of from pnum_begin to pnum - 1*/
  503. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, pnum,
  504. map_count, nodeid_begin);
  505. /* new start, update count etc*/
  506. nodeid_begin = nodeid;
  507. pnum_begin = pnum;
  508. map_count = 1;
  509. }
  510. /* ok, last chunk */
  511. sparse_early_mem_maps_alloc_node(map_map, pnum_begin, NR_MEM_SECTIONS,
  512. map_count, nodeid_begin);
  513. #endif
  514. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  515. if (!present_section_nr(pnum))
  516. continue;
  517. usemap = usemap_map[pnum];
  518. if (!usemap)
  519. continue;
  520. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  521. map = map_map[pnum];
  522. #else
  523. map = sparse_early_mem_map_alloc(pnum);
  524. #endif
  525. if (!map)
  526. continue;
  527. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  528. usemap);
  529. }
  530. vmemmap_populate_print_last();
  531. #ifdef CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER
  532. free_bootmem(__pa(map_map), size2);
  533. #endif
  534. free_bootmem(__pa(usemap_map), size);
  535. }
  536. #ifdef CONFIG_MEMORY_HOTPLUG
  537. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  538. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  539. unsigned long nr_pages)
  540. {
  541. /* This will make the necessary allocations eventually. */
  542. return sparse_mem_map_populate(pnum, nid);
  543. }
  544. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  545. {
  546. return; /* XXX: Not implemented yet */
  547. }
  548. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  549. {
  550. }
  551. #else
  552. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  553. {
  554. struct page *page, *ret;
  555. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  556. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  557. if (page)
  558. goto got_map_page;
  559. ret = vmalloc(memmap_size);
  560. if (ret)
  561. goto got_map_ptr;
  562. return NULL;
  563. got_map_page:
  564. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  565. got_map_ptr:
  566. memset(ret, 0, memmap_size);
  567. return ret;
  568. }
  569. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  570. unsigned long nr_pages)
  571. {
  572. return __kmalloc_section_memmap(nr_pages);
  573. }
  574. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  575. {
  576. if (is_vmalloc_addr(memmap))
  577. vfree(memmap);
  578. else
  579. free_pages((unsigned long)memmap,
  580. get_order(sizeof(struct page) * nr_pages));
  581. }
  582. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  583. {
  584. unsigned long maps_section_nr, removing_section_nr, i;
  585. unsigned long magic;
  586. for (i = 0; i < nr_pages; i++, page++) {
  587. magic = (unsigned long) page->lru.next;
  588. BUG_ON(magic == NODE_INFO);
  589. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  590. removing_section_nr = page->private;
  591. /*
  592. * When this function is called, the removing section is
  593. * logical offlined state. This means all pages are isolated
  594. * from page allocator. If removing section's memmap is placed
  595. * on the same section, it must not be freed.
  596. * If it is freed, page allocator may allocate it which will
  597. * be removed physically soon.
  598. */
  599. if (maps_section_nr != removing_section_nr)
  600. put_page_bootmem(page);
  601. }
  602. }
  603. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  604. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  605. {
  606. struct page *usemap_page;
  607. unsigned long nr_pages;
  608. if (!usemap)
  609. return;
  610. usemap_page = virt_to_page(usemap);
  611. /*
  612. * Check to see if allocation came from hot-plug-add
  613. */
  614. if (PageSlab(usemap_page)) {
  615. kfree(usemap);
  616. if (memmap)
  617. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  618. return;
  619. }
  620. /*
  621. * The usemap came from bootmem. This is packed with other usemaps
  622. * on the section which has pgdat at boot time. Just keep it as is now.
  623. */
  624. if (memmap) {
  625. struct page *memmap_page;
  626. memmap_page = virt_to_page(memmap);
  627. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  628. >> PAGE_SHIFT;
  629. free_map_bootmem(memmap_page, nr_pages);
  630. }
  631. }
  632. /*
  633. * returns the number of sections whose mem_maps were properly
  634. * set. If this is <=0, then that means that the passed-in
  635. * map was not consumed and must be freed.
  636. */
  637. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  638. int nr_pages)
  639. {
  640. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  641. struct pglist_data *pgdat = zone->zone_pgdat;
  642. struct mem_section *ms;
  643. struct page *memmap;
  644. unsigned long *usemap;
  645. unsigned long flags;
  646. int ret;
  647. /*
  648. * no locking for this, because it does its own
  649. * plus, it does a kmalloc
  650. */
  651. ret = sparse_index_init(section_nr, pgdat->node_id);
  652. if (ret < 0 && ret != -EEXIST)
  653. return ret;
  654. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  655. if (!memmap)
  656. return -ENOMEM;
  657. usemap = __kmalloc_section_usemap();
  658. if (!usemap) {
  659. __kfree_section_memmap(memmap, nr_pages);
  660. return -ENOMEM;
  661. }
  662. pgdat_resize_lock(pgdat, &flags);
  663. ms = __pfn_to_section(start_pfn);
  664. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  665. ret = -EEXIST;
  666. goto out;
  667. }
  668. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  669. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  670. out:
  671. pgdat_resize_unlock(pgdat, &flags);
  672. if (ret <= 0) {
  673. kfree(usemap);
  674. __kfree_section_memmap(memmap, nr_pages);
  675. }
  676. return ret;
  677. }
  678. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  679. {
  680. struct page *memmap = NULL;
  681. unsigned long *usemap = NULL;
  682. if (ms->section_mem_map) {
  683. usemap = ms->pageblock_flags;
  684. memmap = sparse_decode_mem_map(ms->section_mem_map,
  685. __section_nr(ms));
  686. ms->section_mem_map = 0;
  687. ms->pageblock_flags = NULL;
  688. }
  689. free_section_usemap(memmap, usemap);
  690. }
  691. #endif