sparse.c 20 KB

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