sparse.c 16 KB

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