sparse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <asm/dma.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/pgtable.h>
  14. /*
  15. * Permanent SPARSEMEM data:
  16. *
  17. * 1) mem_section - memory sections, mem_map's for valid memory
  18. */
  19. #ifdef CONFIG_SPARSEMEM_EXTREME
  20. struct mem_section *mem_section[NR_SECTION_ROOTS]
  21. ____cacheline_internodealigned_in_smp;
  22. #else
  23. struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
  24. ____cacheline_internodealigned_in_smp;
  25. #endif
  26. EXPORT_SYMBOL(mem_section);
  27. #ifdef NODE_NOT_IN_PAGE_FLAGS
  28. /*
  29. * If we did not store the node number in the page then we have to
  30. * do a lookup in the section_to_node_table in order to find which
  31. * node the page belongs to.
  32. */
  33. #if MAX_NUMNODES <= 256
  34. static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  35. #else
  36. static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
  37. #endif
  38. int page_to_nid(struct page *page)
  39. {
  40. return section_to_node_table[page_to_section(page)];
  41. }
  42. EXPORT_SYMBOL(page_to_nid);
  43. static void set_section_nid(unsigned long section_nr, int nid)
  44. {
  45. section_to_node_table[section_nr] = nid;
  46. }
  47. #else /* !NODE_NOT_IN_PAGE_FLAGS */
  48. static inline void set_section_nid(unsigned long section_nr, int nid)
  49. {
  50. }
  51. #endif
  52. #ifdef CONFIG_SPARSEMEM_EXTREME
  53. static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
  54. {
  55. struct mem_section *section = NULL;
  56. unsigned long array_size = SECTIONS_PER_ROOT *
  57. sizeof(struct mem_section);
  58. if (slab_is_available())
  59. section = kmalloc_node(array_size, GFP_KERNEL, nid);
  60. else
  61. section = alloc_bootmem_node(NODE_DATA(nid), array_size);
  62. if (section)
  63. memset(section, 0, array_size);
  64. return section;
  65. }
  66. static int __meminit sparse_index_init(unsigned long section_nr, int nid)
  67. {
  68. static DEFINE_SPINLOCK(index_init_lock);
  69. unsigned long root = SECTION_NR_TO_ROOT(section_nr);
  70. struct mem_section *section;
  71. int ret = 0;
  72. if (mem_section[root])
  73. return -EEXIST;
  74. section = sparse_index_alloc(nid);
  75. /*
  76. * This lock keeps two different sections from
  77. * reallocating for the same index
  78. */
  79. spin_lock(&index_init_lock);
  80. if (mem_section[root]) {
  81. ret = -EEXIST;
  82. goto out;
  83. }
  84. mem_section[root] = section;
  85. out:
  86. spin_unlock(&index_init_lock);
  87. return ret;
  88. }
  89. #else /* !SPARSEMEM_EXTREME */
  90. static inline int sparse_index_init(unsigned long section_nr, int nid)
  91. {
  92. return 0;
  93. }
  94. #endif
  95. /*
  96. * Although written for the SPARSEMEM_EXTREME case, this happens
  97. * to also work for the flat array case because
  98. * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
  99. */
  100. int __section_nr(struct mem_section* ms)
  101. {
  102. unsigned long root_nr;
  103. struct mem_section* root;
  104. for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
  105. root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
  106. if (!root)
  107. continue;
  108. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  109. break;
  110. }
  111. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  112. }
  113. /*
  114. * During early boot, before section_mem_map is used for an actual
  115. * mem_map, we use section_mem_map to store the section's NUMA
  116. * node. This keeps us from having to use another data structure. The
  117. * node information is cleared just before we store the real mem_map.
  118. */
  119. static inline unsigned long sparse_encode_early_nid(int nid)
  120. {
  121. return (nid << SECTION_NID_SHIFT);
  122. }
  123. static inline int sparse_early_nid(struct mem_section *section)
  124. {
  125. return (section->section_mem_map >> SECTION_NID_SHIFT);
  126. }
  127. /* Record a memory area against a node. */
  128. void __init memory_present(int nid, unsigned long start, unsigned long end)
  129. {
  130. unsigned long pfn;
  131. start &= PAGE_SECTION_MASK;
  132. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  133. unsigned long section = pfn_to_section_nr(pfn);
  134. struct mem_section *ms;
  135. sparse_index_init(section, nid);
  136. set_section_nid(section, nid);
  137. ms = __nr_to_section(section);
  138. if (!ms->section_mem_map)
  139. ms->section_mem_map = sparse_encode_early_nid(nid) |
  140. SECTION_MARKED_PRESENT;
  141. }
  142. }
  143. /*
  144. * Only used by the i386 NUMA architecures, but relatively
  145. * generic code.
  146. */
  147. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  148. unsigned long end_pfn)
  149. {
  150. unsigned long pfn;
  151. unsigned long nr_pages = 0;
  152. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  153. if (nid != early_pfn_to_nid(pfn))
  154. continue;
  155. if (pfn_present(pfn))
  156. nr_pages += PAGES_PER_SECTION;
  157. }
  158. return nr_pages * sizeof(struct page);
  159. }
  160. /*
  161. * Subtle, we encode the real pfn into the mem_map such that
  162. * the identity pfn - section_mem_map will return the actual
  163. * physical page frame number.
  164. */
  165. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  166. {
  167. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  168. }
  169. /*
  170. * We need this if we ever free the mem_maps. While not implemented yet,
  171. * this function is included for parity with its sibling.
  172. */
  173. static __attribute((unused))
  174. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  175. {
  176. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  177. }
  178. static int __meminit sparse_init_one_section(struct mem_section *ms,
  179. unsigned long pnum, struct page *mem_map,
  180. unsigned long *pageblock_bitmap)
  181. {
  182. if (!present_section(ms))
  183. return -EINVAL;
  184. ms->section_mem_map &= ~SECTION_MAP_MASK;
  185. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum) |
  186. SECTION_HAS_MEM_MAP;
  187. ms->pageblock_flags = pageblock_bitmap;
  188. return 1;
  189. }
  190. static unsigned long usemap_size(void)
  191. {
  192. unsigned long size_bytes;
  193. size_bytes = roundup(SECTION_BLOCKFLAGS_BITS, 8) / 8;
  194. size_bytes = roundup(size_bytes, sizeof(unsigned long));
  195. return size_bytes;
  196. }
  197. #ifdef CONFIG_MEMORY_HOTPLUG
  198. static unsigned long *__kmalloc_section_usemap(void)
  199. {
  200. return kmalloc(usemap_size(), GFP_KERNEL);
  201. }
  202. #endif /* CONFIG_MEMORY_HOTPLUG */
  203. static unsigned long *sparse_early_usemap_alloc(unsigned long pnum)
  204. {
  205. unsigned long *usemap;
  206. struct mem_section *ms = __nr_to_section(pnum);
  207. int nid = sparse_early_nid(ms);
  208. usemap = alloc_bootmem_node(NODE_DATA(nid), usemap_size());
  209. if (usemap)
  210. return usemap;
  211. /* Stupid: suppress gcc warning for SPARSEMEM && !NUMA */
  212. nid = 0;
  213. printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
  214. return NULL;
  215. }
  216. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  217. struct page __init *sparse_mem_map_populate(unsigned long pnum, int nid)
  218. {
  219. struct page *map;
  220. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  221. if (map)
  222. return map;
  223. map = alloc_bootmem_node(NODE_DATA(nid),
  224. sizeof(struct page) * PAGES_PER_SECTION);
  225. return map;
  226. }
  227. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  228. struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
  229. {
  230. struct page *map;
  231. struct mem_section *ms = __nr_to_section(pnum);
  232. int nid = sparse_early_nid(ms);
  233. map = sparse_mem_map_populate(pnum, nid);
  234. if (map)
  235. return map;
  236. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  237. "some memory will not be available.\n", __FUNCTION__);
  238. ms->section_mem_map = 0;
  239. return NULL;
  240. }
  241. /*
  242. * Allocate the accumulated non-linear sections, allocate a mem_map
  243. * for each and record the physical to section mapping.
  244. */
  245. void __init sparse_init(void)
  246. {
  247. unsigned long pnum;
  248. struct page *map;
  249. unsigned long *usemap;
  250. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  251. if (!present_section_nr(pnum))
  252. continue;
  253. map = sparse_early_mem_map_alloc(pnum);
  254. if (!map)
  255. continue;
  256. usemap = sparse_early_usemap_alloc(pnum);
  257. if (!usemap)
  258. continue;
  259. sparse_init_one_section(__nr_to_section(pnum), pnum, map,
  260. usemap);
  261. }
  262. }
  263. #ifdef CONFIG_MEMORY_HOTPLUG
  264. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  265. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  266. unsigned long nr_pages)
  267. {
  268. /* This will make the necessary allocations eventually. */
  269. return sparse_mem_map_populate(pnum, nid);
  270. }
  271. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  272. {
  273. return; /* XXX: Not implemented yet */
  274. }
  275. #else
  276. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  277. {
  278. struct page *page, *ret;
  279. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  280. page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
  281. if (page)
  282. goto got_map_page;
  283. ret = vmalloc(memmap_size);
  284. if (ret)
  285. goto got_map_ptr;
  286. return NULL;
  287. got_map_page:
  288. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  289. got_map_ptr:
  290. memset(ret, 0, memmap_size);
  291. return ret;
  292. }
  293. static inline struct page *kmalloc_section_memmap(unsigned long pnum, int nid,
  294. unsigned long nr_pages)
  295. {
  296. return __kmalloc_section_memmap(nr_pages);
  297. }
  298. static int vaddr_in_vmalloc_area(void *addr)
  299. {
  300. if (addr >= (void *)VMALLOC_START &&
  301. addr < (void *)VMALLOC_END)
  302. return 1;
  303. return 0;
  304. }
  305. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  306. {
  307. if (vaddr_in_vmalloc_area(memmap))
  308. vfree(memmap);
  309. else
  310. free_pages((unsigned long)memmap,
  311. get_order(sizeof(struct page) * nr_pages));
  312. }
  313. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  314. /*
  315. * returns the number of sections whose mem_maps were properly
  316. * set. If this is <=0, then that means that the passed-in
  317. * map was not consumed and must be freed.
  318. */
  319. int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  320. int nr_pages)
  321. {
  322. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  323. struct pglist_data *pgdat = zone->zone_pgdat;
  324. struct mem_section *ms;
  325. struct page *memmap;
  326. unsigned long *usemap;
  327. unsigned long flags;
  328. int ret;
  329. /*
  330. * no locking for this, because it does its own
  331. * plus, it does a kmalloc
  332. */
  333. sparse_index_init(section_nr, pgdat->node_id);
  334. memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages);
  335. usemap = __kmalloc_section_usemap();
  336. pgdat_resize_lock(pgdat, &flags);
  337. ms = __pfn_to_section(start_pfn);
  338. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  339. ret = -EEXIST;
  340. goto out;
  341. }
  342. if (!usemap) {
  343. ret = -ENOMEM;
  344. goto out;
  345. }
  346. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  347. ret = sparse_init_one_section(ms, section_nr, memmap, usemap);
  348. out:
  349. pgdat_resize_unlock(pgdat, &flags);
  350. if (ret <= 0)
  351. __kfree_section_memmap(memmap, nr_pages);
  352. return ret;
  353. }
  354. #endif