sparse.c 6.7 KB

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