sparse.c 7.2 KB

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