sparse.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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_usemap_alloc_pgdat_section(struct pglist_data *pgdat)
  238. {
  239. unsigned long section_nr;
  240. /*
  241. * A page may contain usemaps for other sections preventing the
  242. * page being freed and making a section unremovable while
  243. * other sections referencing the usemap retmain active. Similarly,
  244. * a pgdat can prevent a section being removed. If section A
  245. * contains a pgdat and section B contains the usemap, both
  246. * sections become inter-dependent. This allocates usemaps
  247. * from the same section as the pgdat where possible to avoid
  248. * this problem.
  249. */
  250. section_nr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  251. return alloc_bootmem_section(usemap_size(), section_nr);
  252. }
  253. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  254. {
  255. unsigned long usemap_snr, pgdat_snr;
  256. static unsigned long old_usemap_snr = NR_MEM_SECTIONS;
  257. static unsigned long old_pgdat_snr = NR_MEM_SECTIONS;
  258. struct pglist_data *pgdat = NODE_DATA(nid);
  259. int usemap_nid;
  260. usemap_snr = pfn_to_section_nr(__pa(usemap) >> PAGE_SHIFT);
  261. pgdat_snr = pfn_to_section_nr(__pa(pgdat) >> PAGE_SHIFT);
  262. if (usemap_snr == pgdat_snr)
  263. return;
  264. if (old_usemap_snr == usemap_snr && old_pgdat_snr == pgdat_snr)
  265. /* skip redundant message */
  266. return;
  267. old_usemap_snr = usemap_snr;
  268. old_pgdat_snr = pgdat_snr;
  269. usemap_nid = sparse_early_nid(__nr_to_section(usemap_snr));
  270. if (usemap_nid != nid) {
  271. printk(KERN_INFO
  272. "node %d must be removed before remove section %ld\n",
  273. nid, usemap_snr);
  274. return;
  275. }
  276. /*
  277. * There is a circular dependency.
  278. * Some platforms allow un-removable section because they will just
  279. * gather other removable sections for dynamic partitioning.
  280. * Just notify un-removable section's number here.
  281. */
  282. printk(KERN_INFO "Section %ld and %ld (node %d)", usemap_snr,
  283. pgdat_snr, nid);
  284. printk(KERN_CONT
  285. " have a circular dependency on usemap and pgdat allocations\n");
  286. }
  287. #else
  288. static unsigned long * __init
  289. sparse_early_usemap_alloc_pgdat_section(struct pglist_data *pgdat)
  290. {
  291. return NULL;
  292. }
  293. static void __init check_usemap_section_nr(int nid, unsigned long *usemap)
  294. {
  295. }
  296. #endif /* CONFIG_MEMORY_HOTREMOVE */
  297. static unsigned long *__init sparse_early_usemap_alloc(unsigned long pnum)
  298. {
  299. unsigned long *usemap;
  300. struct mem_section *ms = __nr_to_section(pnum);
  301. int nid = sparse_early_nid(ms);
  302. usemap = sparse_early_usemap_alloc_pgdat_section(NODE_DATA(nid));
  303. if (usemap)
  304. return usemap;
  305. usemap = alloc_bootmem_node(NODE_DATA(nid), usemap_size());
  306. if (usemap) {
  307. check_usemap_section_nr(nid, usemap);
  308. return usemap;
  309. }
  310. /* Stupid: suppress gcc warning for SPARSEMEM && !NUMA */
  311. nid = 0;
  312. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  313. return NULL;
  314. }
  315. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  316. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  317. {
  318. struct page *map;
  319. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  320. if (map)
  321. return map;
  322. map = alloc_bootmem_pages_node(NODE_DATA(nid),
  323. PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION));
  324. return map;
  325. }
  326. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  327. static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  328. {
  329. struct page *map;
  330. struct mem_section *ms = __nr_to_section(pnum);
  331. int nid = sparse_early_nid(ms);
  332. map = sparse_mem_map_populate(pnum, nid);
  333. if (map)
  334. return map;
  335. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  336. "some memory will not be available.\n", __func__);
  337. ms->section_mem_map = 0;
  338. return NULL;
  339. }
  340. void __attribute__((weak)) __meminit vmemmap_populate_print_last(void)
  341. {
  342. }
  343. /*
  344. * Allocate the accumulated non-linear sections, allocate a mem_map
  345. * for each and record the physical to section mapping.
  346. */
  347. void __init sparse_init(void)
  348. {
  349. unsigned long pnum;
  350. struct page *map;
  351. unsigned long *usemap;
  352. unsigned long **usemap_map;
  353. int size;
  354. /*
  355. * map is using big page (aka 2M in x86 64 bit)
  356. * usemap is less one page (aka 24 bytes)
  357. * so alloc 2M (with 2M align) and 24 bytes in turn will
  358. * make next 2M slip to one more 2M later.
  359. * then in big system, the memory will have a lot of holes...
  360. * here try to allocate 2M pages continously.
  361. *
  362. * powerpc need to call sparse_init_one_section right after each
  363. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  364. */
  365. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  366. usemap_map = alloc_bootmem(size);
  367. if (!usemap_map)
  368. panic("can not allocate usemap_map\n");
  369. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  370. if (!present_section_nr(pnum))
  371. continue;
  372. usemap_map[pnum] = sparse_early_usemap_alloc(pnum);
  373. }
  374. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  375. if (!present_section_nr(pnum))
  376. continue;
  377. usemap = usemap_map[pnum];
  378. if (!usemap)
  379. continue;
  380. map = sparse_early_mem_map_alloc(pnum);
  381. if (!map)
  382. continue;
  383. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  384. usemap);
  385. }
  386. vmemmap_populate_print_last();
  387. free_bootmem(__pa(usemap_map), size);
  388. }
  389. #ifdef CONFIG_MEMORY_HOTPLUG
  390. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  391. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  392. unsigned long nr_pages)
  393. {
  394. /* This will make the necessary allocations eventually. */
  395. return sparse_mem_map_populate(pnum, nid);
  396. }
  397. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  398. {
  399. return; /* XXX: Not implemented yet */
  400. }
  401. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  402. {
  403. }
  404. #else
  405. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  406. {
  407. struct page *page, *ret;
  408. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  409. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  410. if (page)
  411. goto got_map_page;
  412. ret = vmalloc(memmap_size);
  413. if (ret)
  414. goto got_map_ptr;
  415. return NULL;
  416. got_map_page:
  417. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  418. got_map_ptr:
  419. memset(ret, 0, memmap_size);
  420. return ret;
  421. }
  422. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  423. unsigned long nr_pages)
  424. {
  425. return __kmalloc_section_memmap(nr_pages);
  426. }
  427. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  428. {
  429. if (is_vmalloc_addr(memmap))
  430. vfree(memmap);
  431. else
  432. free_pages((unsigned long)memmap,
  433. get_order(sizeof(struct page) * nr_pages));
  434. }
  435. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  436. {
  437. unsigned long maps_section_nr, removing_section_nr, i;
  438. int magic;
  439. for (i = 0; i < nr_pages; i++, page++) {
  440. magic = atomic_read(&page->_mapcount);
  441. BUG_ON(magic == NODE_INFO);
  442. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  443. removing_section_nr = page->private;
  444. /*
  445. * When this function is called, the removing section is
  446. * logical offlined state. This means all pages are isolated
  447. * from page allocator. If removing section's memmap is placed
  448. * on the same section, it must not be freed.
  449. * If it is freed, page allocator may allocate it which will
  450. * be removed physically soon.
  451. */
  452. if (maps_section_nr != removing_section_nr)
  453. put_page_bootmem(page);
  454. }
  455. }
  456. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  457. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  458. {
  459. struct page *usemap_page;
  460. unsigned long nr_pages;
  461. if (!usemap)
  462. return;
  463. usemap_page = virt_to_page(usemap);
  464. /*
  465. * Check to see if allocation came from hot-plug-add
  466. */
  467. if (PageSlab(usemap_page)) {
  468. kfree(usemap);
  469. if (memmap)
  470. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  471. return;
  472. }
  473. /*
  474. * The usemap came from bootmem. This is packed with other usemaps
  475. * on the section which has pgdat at boot time. Just keep it as is now.
  476. */
  477. if (memmap) {
  478. struct page *memmap_page;
  479. memmap_page = virt_to_page(memmap);
  480. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  481. >> PAGE_SHIFT;
  482. free_map_bootmem(memmap_page, nr_pages);
  483. }
  484. }
  485. /*
  486. * returns the number of sections whose mem_maps were properly
  487. * set. If this is <=0, then that means that the passed-in
  488. * map was not consumed and must be freed.
  489. */
  490. int __meminit sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  491. int nr_pages)
  492. {
  493. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  494. struct pglist_data *pgdat = zone->zone_pgdat;
  495. struct mem_section *ms;
  496. struct page *memmap;
  497. unsigned long *usemap;
  498. unsigned long flags;
  499. int ret;
  500. /*
  501. * no locking for this, because it does its own
  502. * plus, it does a kmalloc
  503. */
  504. ret = sparse_index_init(section_nr, pgdat->node_id);
  505. if (ret < 0 && ret != -EEXIST)
  506. return ret;
  507. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  508. if (!memmap)
  509. return -ENOMEM;
  510. usemap = __kmalloc_section_usemap();
  511. if (!usemap) {
  512. __kfree_section_memmap(memmap, nr_pages);
  513. return -ENOMEM;
  514. }
  515. pgdat_resize_lock(pgdat, &flags);
  516. ms = __pfn_to_section(start_pfn);
  517. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  518. ret = -EEXIST;
  519. goto out;
  520. }
  521. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  522. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  523. out:
  524. pgdat_resize_unlock(pgdat, &flags);
  525. if (ret <= 0) {
  526. kfree(usemap);
  527. __kfree_section_memmap(memmap, nr_pages);
  528. }
  529. return ret;
  530. }
  531. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  532. {
  533. struct page *memmap = NULL;
  534. unsigned long *usemap = NULL;
  535. if (ms->section_mem_map) {
  536. usemap = ms->pageblock_flags;
  537. memmap = sparse_decode_mem_map(ms->section_mem_map,
  538. __section_nr(ms));
  539. ms->section_mem_map = 0;
  540. ms->pageblock_flags = NULL;
  541. }
  542. free_section_usemap(memmap, usemap);
  543. }
  544. #endif