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