sparse.c 7.2 KB

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