sparse.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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;
  79. root_nr < NR_MEM_SECTIONS;
  80. root_nr += SECTIONS_PER_ROOT) {
  81. root = __nr_to_section(root_nr);
  82. if (!root)
  83. continue;
  84. if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
  85. break;
  86. }
  87. return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
  88. }
  89. /* Record a memory area against a node. */
  90. void memory_present(int nid, unsigned long start, unsigned long end)
  91. {
  92. unsigned long pfn;
  93. start &= PAGE_SECTION_MASK;
  94. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
  95. unsigned long section = pfn_to_section_nr(pfn);
  96. struct mem_section *ms;
  97. sparse_index_init(section, nid);
  98. ms = __nr_to_section(section);
  99. if (!ms->section_mem_map)
  100. ms->section_mem_map = SECTION_MARKED_PRESENT;
  101. }
  102. }
  103. /*
  104. * Only used by the i386 NUMA architecures, but relatively
  105. * generic code.
  106. */
  107. unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
  108. unsigned long end_pfn)
  109. {
  110. unsigned long pfn;
  111. unsigned long nr_pages = 0;
  112. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  113. if (nid != early_pfn_to_nid(pfn))
  114. continue;
  115. if (pfn_valid(pfn))
  116. nr_pages += PAGES_PER_SECTION;
  117. }
  118. return nr_pages * sizeof(struct page);
  119. }
  120. /*
  121. * Subtle, we encode the real pfn into the mem_map such that
  122. * the identity pfn - section_mem_map will return the actual
  123. * physical page frame number.
  124. */
  125. static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
  126. {
  127. return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
  128. }
  129. /*
  130. * We need this if we ever free the mem_maps. While not implemented yet,
  131. * this function is included for parity with its sibling.
  132. */
  133. static __attribute((unused))
  134. struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
  135. {
  136. return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
  137. }
  138. static int sparse_init_one_section(struct mem_section *ms,
  139. unsigned long pnum, struct page *mem_map)
  140. {
  141. if (!valid_section(ms))
  142. return -EINVAL;
  143. ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
  144. return 1;
  145. }
  146. static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
  147. {
  148. struct page *map;
  149. int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
  150. struct mem_section *ms = __nr_to_section(pnum);
  151. map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
  152. if (map)
  153. return map;
  154. map = alloc_bootmem_node(NODE_DATA(nid),
  155. sizeof(struct page) * PAGES_PER_SECTION);
  156. if (map)
  157. return map;
  158. printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
  159. ms->section_mem_map = 0;
  160. return NULL;
  161. }
  162. static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
  163. {
  164. struct page *page, *ret;
  165. unsigned long memmap_size = sizeof(struct page) * nr_pages;
  166. page = alloc_pages(GFP_KERNEL, get_order(memmap_size));
  167. if (page)
  168. goto got_map_page;
  169. ret = vmalloc(memmap_size);
  170. if (ret)
  171. goto got_map_ptr;
  172. return NULL;
  173. got_map_page:
  174. ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
  175. got_map_ptr:
  176. memset(ret, 0, memmap_size);
  177. return ret;
  178. }
  179. static int vaddr_in_vmalloc_area(void *addr)
  180. {
  181. if (addr >= (void *)VMALLOC_START &&
  182. addr < (void *)VMALLOC_END)
  183. return 1;
  184. return 0;
  185. }
  186. static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
  187. {
  188. if (vaddr_in_vmalloc_area(memmap))
  189. vfree(memmap);
  190. else
  191. free_pages((unsigned long)memmap,
  192. get_order(sizeof(struct page) * nr_pages));
  193. }
  194. /*
  195. * Allocate the accumulated non-linear sections, allocate a mem_map
  196. * for each and record the physical to section mapping.
  197. */
  198. void sparse_init(void)
  199. {
  200. unsigned long pnum;
  201. struct page *map;
  202. for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
  203. if (!valid_section_nr(pnum))
  204. continue;
  205. map = sparse_early_mem_map_alloc(pnum);
  206. if (!map)
  207. continue;
  208. sparse_init_one_section(__nr_to_section(pnum), pnum, map);
  209. }
  210. }
  211. /*
  212. * returns the number of sections whose mem_maps were properly
  213. * set. If this is <=0, then that means that the passed-in
  214. * map was not consumed and must be freed.
  215. */
  216. int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
  217. int nr_pages)
  218. {
  219. unsigned long section_nr = pfn_to_section_nr(start_pfn);
  220. struct pglist_data *pgdat = zone->zone_pgdat;
  221. struct mem_section *ms;
  222. struct page *memmap;
  223. unsigned long flags;
  224. int ret;
  225. /*
  226. * no locking for this, because it does its own
  227. * plus, it does a kmalloc
  228. */
  229. sparse_index_init(section_nr, pgdat->node_id);
  230. memmap = __kmalloc_section_memmap(nr_pages);
  231. pgdat_resize_lock(pgdat, &flags);
  232. ms = __pfn_to_section(start_pfn);
  233. if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
  234. ret = -EEXIST;
  235. goto out;
  236. }
  237. ms->section_mem_map |= SECTION_MARKED_PRESENT;
  238. ret = sparse_init_one_section(ms, section_nr, memmap);
  239. out:
  240. pgdat_resize_unlock(pgdat, &flags);
  241. if (ret <= 0)
  242. __kfree_section_memmap(memmap, nr_pages);
  243. return ret;
  244. }