sparse.c 16 KB

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