sparse.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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;
  218. struct mem_section *ms = __nr_to_section(pnum);
  219. int nid = sparse_early_nid(ms);
  220. usemap = alloc_bootmem_node(NODE_DATA(nid), usemap_size());
  221. if (usemap)
  222. return usemap;
  223. /* Stupid: suppress gcc warning for SPARSEMEM && !NUMA */
  224. nid = 0;
  225. printk(KERN_WARNING "%s: allocation failed\n", __func__);
  226. return NULL;
  227. }
  228. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  229. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  230. {
  231. struct page *map;
  232. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  233. if (map)
  234. return map;
  235. map = alloc_bootmem_pages_node(NODE_DATA(nid),
  236. PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION));
  237. return map;
  238. }
  239. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  240. struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  241. {
  242. struct page *map;
  243. struct mem_section *ms = __nr_to_section(pnum);
  244. int nid = sparse_early_nid(ms);
  245. map = sparse_mem_map_populate(pnum, nid);
  246. if (map)
  247. return map;
  248. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  249. "some memory will not be available.\n", __func__);
  250. ms->section_mem_map = 0;
  251. return NULL;
  252. }
  253. void __attribute__((weak)) __meminit vmemmap_populate_print_last(void)
  254. {
  255. }
  256. /*
  257. * Allocate the accumulated non-linear sections, allocate a mem_map
  258. * for each and record the physical to section mapping.
  259. */
  260. void __init sparse_init(void)
  261. {
  262. unsigned long pnum;
  263. struct page *map;
  264. unsigned long *usemap;
  265. unsigned long **usemap_map;
  266. int size;
  267. /*
  268. * map is using big page (aka 2M in x86 64 bit)
  269. * usemap is less one page (aka 24 bytes)
  270. * so alloc 2M (with 2M align) and 24 bytes in turn will
  271. * make next 2M slip to one more 2M later.
  272. * then in big system, the memory will have a lot of holes...
  273. * here try to allocate 2M pages continously.
  274. *
  275. * powerpc need to call sparse_init_one_section right after each
  276. * sparse_early_mem_map_alloc, so allocate usemap_map at first.
  277. */
  278. size = sizeof(unsigned long *) * NR_MEM_SECTIONS;
  279. usemap_map = alloc_bootmem(size);
  280. if (!usemap_map)
  281. panic("can not allocate usemap_map\n");
  282. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  283. if (!present_section_nr(pnum))
  284. continue;
  285. usemap_map[pnum] = sparse_early_usemap_alloc(pnum);
  286. }
  287. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  288. if (!present_section_nr(pnum))
  289. continue;
  290. usemap = usemap_map[pnum];
  291. if (!usemap)
  292. continue;
  293. map = sparse_early_mem_map_alloc(pnum);
  294. if (!map)
  295. continue;
  296. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  297. usemap);
  298. }
  299. vmemmap_populate_print_last();
  300. free_bootmem(__pa(usemap_map), size);
  301. }
  302. #ifdef CONFIG_MEMORY_HOTPLUG
  303. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  304. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  305. unsigned long nr_pages)
  306. {
  307. /* This will make the necessary allocations eventually. */
  308. return sparse_mem_map_populate(pnum, nid);
  309. }
  310. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  311. {
  312. return; /* XXX: Not implemented yet */
  313. }
  314. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  315. {
  316. }
  317. #else
  318. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  319. {
  320. struct page *page, *ret;
  321. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  322. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  323. if (page)
  324. goto got_map_page;
  325. ret = vmalloc(memmap_size);
  326. if (ret)
  327. goto got_map_ptr;
  328. return NULL;
  329. got_map_page:
  330. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  331. got_map_ptr:
  332. memset(ret, 0, memmap_size);
  333. return ret;
  334. }
  335. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  336. unsigned long nr_pages)
  337. {
  338. return __kmalloc_section_memmap(nr_pages);
  339. }
  340. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  341. {
  342. if (is_vmalloc_addr(memmap))
  343. vfree(memmap);
  344. else
  345. free_pages((unsigned long)memmap,
  346. get_order(sizeof(struct page) * nr_pages));
  347. }
  348. static void free_map_bootmem(struct page *page, unsigned long nr_pages)
  349. {
  350. unsigned long maps_section_nr, removing_section_nr, i;
  351. int magic;
  352. for (i = 0; i < nr_pages; i++, page++) {
  353. magic = atomic_read(&page->_mapcount);
  354. BUG_ON(magic == NODE_INFO);
  355. maps_section_nr = pfn_to_section_nr(page_to_pfn(page));
  356. removing_section_nr = page->private;
  357. /*
  358. * When this function is called, the removing section is
  359. * logical offlined state. This means all pages are isolated
  360. * from page allocator. If removing section's memmap is placed
  361. * on the same section, it must not be freed.
  362. * If it is freed, page allocator may allocate it which will
  363. * be removed physically soon.
  364. */
  365. if (maps_section_nr != removing_section_nr)
  366. put_page_bootmem(page);
  367. }
  368. }
  369. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  370. static void free_section_usemap(struct page *memmap, unsigned long *usemap)
  371. {
  372. struct page *usemap_page;
  373. unsigned long nr_pages;
  374. if (!usemap)
  375. return;
  376. usemap_page = virt_to_page(usemap);
  377. /*
  378. * Check to see if allocation came from hot-plug-add
  379. */
  380. if (PageSlab(usemap_page)) {
  381. kfree(usemap);
  382. if (memmap)
  383. __kfree_section_memmap(memmap, PAGES_PER_SECTION);
  384. return;
  385. }
  386. /*
  387. * The usemap came from bootmem. This is packed with other usemaps
  388. * on the section which has pgdat at boot time. Just keep it as is now.
  389. */
  390. if (memmap) {
  391. struct page *memmap_page;
  392. memmap_page = virt_to_page(memmap);
  393. nr_pages = PAGE_ALIGN(PAGES_PER_SECTION * sizeof(struct page))
  394. >> PAGE_SHIFT;
  395. free_map_bootmem(memmap_page, nr_pages);
  396. }
  397. }
  398. /*
  399. * returns the number of sections whose mem_maps were properly
  400. * set. If this is <=0, then that means that the passed-in
  401. * map was not consumed and must be freed.
  402. */
  403. int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  404. int nr_pages)
  405. {
  406. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  407. struct pglist_data *pgdat = zone->zone_pgdat;
  408. struct mem_section *ms;
  409. struct page *memmap;
  410. unsigned long *usemap;
  411. unsigned long flags;
  412. int ret;
  413. /*
  414. * no locking for this, because it does its own
  415. * plus, it does a kmalloc
  416. */
  417. ret = sparse_index_init(section_nr, pgdat->node_id);
  418. if (ret < 0 && ret != -EEXIST)
  419. return ret;
  420. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  421. if (!memmap)
  422. return -ENOMEM;
  423. usemap = __kmalloc_section_usemap();
  424. if (!usemap) {
  425. __kfree_section_memmap(memmap, nr_pages);
  426. return -ENOMEM;
  427. }
  428. pgdat_resize_lock(pgdat, &flags);
  429. ms = __pfn_to_section(start_pfn);
  430. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  431. ret = -EEXIST;
  432. goto out;
  433. }
  434. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  435. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  436. out:
  437. pgdat_resize_unlock(pgdat, &flags);
  438. if (ret <= 0) {
  439. kfree(usemap);
  440. __kfree_section_memmap(memmap, nr_pages);
  441. }
  442. return ret;
  443. }
  444. void sparse_remove_one_section(struct zone *zone, struct mem_section *ms)
  445. {
  446. struct page *memmap = NULL;
  447. unsigned long *usemap = NULL;
  448. if (ms->section_mem_map) {
  449. usemap = ms->pageblock_flags;
  450. memmap = sparse_decode_mem_map(ms->section_mem_map,
  451. __section_nr(ms));
  452. ms->section_mem_map = 0;
  453. ms->pageblock_flags = NULL;
  454. }
  455. free_section_usemap(memmap, usemap);
  456. }
  457. #endif