sparse.c 8.3 KB

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