sparse.c 14 KB

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