sparse-vmemmap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Virtual Memory Map support
  3. *
  4. * (C) 2007 sgi. Christoph Lameter.
  5. *
  6. * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn,
  7. * virt_to_page, page_address() to be implemented as a base offset
  8. * calculation without memory access.
  9. *
  10. * However, virtual mappings need a page table and TLBs. Many Linux
  11. * architectures already map their physical space using 1-1 mappings
  12. * via TLBs. For those arches the virtual memory map is essentially
  13. * for free if we use the same page size as the 1-1 mappings. In that
  14. * case the overhead consists of a few additional pages that are
  15. * allocated to create a view of memory for vmemmap.
  16. *
  17. * The architecture is expected to provide a vmemmap_populate() function
  18. * to instantiate the mapping.
  19. */
  20. #include <linux/mm.h>
  21. #include <linux/mmzone.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/memremap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/sched.h>
  29. #include <asm/dma.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/pgtable.h>
  32. /*
  33. * Allocate a block of memory to be used to back the virtual memory map
  34. * or to back the page tables that are used to create the mapping.
  35. * Uses the main allocators if they are available, else bootmem.
  36. */
  37. static void * __ref __earlyonly_bootmem_alloc(int node,
  38. unsigned long size,
  39. unsigned long align,
  40. unsigned long goal)
  41. {
  42. return memblock_virt_alloc_try_nid(size, align, goal,
  43. BOOTMEM_ALLOC_ACCESSIBLE, node);
  44. }
  45. static void *vmemmap_buf;
  46. static void *vmemmap_buf_end;
  47. void * __meminit vmemmap_alloc_block(unsigned long size, int node)
  48. {
  49. /* If the main allocator is up use that, fallback to bootmem. */
  50. if (slab_is_available()) {
  51. struct page *page;
  52. if (node_state(node, N_HIGH_MEMORY))
  53. page = alloc_pages_node(
  54. node, GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT,
  55. get_order(size));
  56. else
  57. page = alloc_pages(
  58. GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT,
  59. get_order(size));
  60. if (page)
  61. return page_address(page);
  62. return NULL;
  63. } else
  64. return __earlyonly_bootmem_alloc(node, size, size,
  65. __pa(MAX_DMA_ADDRESS));
  66. }
  67. /* need to make sure size is all the same during early stage */
  68. static void * __meminit alloc_block_buf(unsigned long size, int node)
  69. {
  70. void *ptr;
  71. if (!vmemmap_buf)
  72. return vmemmap_alloc_block(size, node);
  73. /* take the from buf */
  74. ptr = (void *)ALIGN((unsigned long)vmemmap_buf, size);
  75. if (ptr + size > vmemmap_buf_end)
  76. return vmemmap_alloc_block(size, node);
  77. vmemmap_buf = ptr + size;
  78. return ptr;
  79. }
  80. static unsigned long __meminit vmem_altmap_next_pfn(struct vmem_altmap *altmap)
  81. {
  82. return altmap->base_pfn + altmap->reserve + altmap->alloc
  83. + altmap->align;
  84. }
  85. static unsigned long __meminit vmem_altmap_nr_free(struct vmem_altmap *altmap)
  86. {
  87. unsigned long allocated = altmap->alloc + altmap->align;
  88. if (altmap->free > allocated)
  89. return altmap->free - allocated;
  90. return 0;
  91. }
  92. /**
  93. * vmem_altmap_alloc - allocate pages from the vmem_altmap reservation
  94. * @altmap - reserved page pool for the allocation
  95. * @nr_pfns - size (in pages) of the allocation
  96. *
  97. * Allocations are aligned to the size of the request
  98. */
  99. static unsigned long __meminit vmem_altmap_alloc(struct vmem_altmap *altmap,
  100. unsigned long nr_pfns)
  101. {
  102. unsigned long pfn = vmem_altmap_next_pfn(altmap);
  103. unsigned long nr_align;
  104. nr_align = 1UL << find_first_bit(&nr_pfns, BITS_PER_LONG);
  105. nr_align = ALIGN(pfn, nr_align) - pfn;
  106. if (nr_pfns + nr_align > vmem_altmap_nr_free(altmap))
  107. return ULONG_MAX;
  108. altmap->alloc += nr_pfns;
  109. altmap->align += nr_align;
  110. return pfn + nr_align;
  111. }
  112. static void * __meminit altmap_alloc_block_buf(unsigned long size,
  113. struct vmem_altmap *altmap)
  114. {
  115. unsigned long pfn, nr_pfns;
  116. void *ptr;
  117. if (size & ~PAGE_MASK) {
  118. pr_warn_once("%s: allocations must be multiple of PAGE_SIZE (%ld)\n",
  119. __func__, size);
  120. return NULL;
  121. }
  122. nr_pfns = size >> PAGE_SHIFT;
  123. pfn = vmem_altmap_alloc(altmap, nr_pfns);
  124. if (pfn < ULONG_MAX)
  125. ptr = __va(__pfn_to_phys(pfn));
  126. else
  127. ptr = NULL;
  128. pr_debug("%s: pfn: %#lx alloc: %ld align: %ld nr: %#lx\n",
  129. __func__, pfn, altmap->alloc, altmap->align, nr_pfns);
  130. return ptr;
  131. }
  132. /* need to make sure size is all the same during early stage */
  133. void * __meminit __vmemmap_alloc_block_buf(unsigned long size, int node,
  134. struct vmem_altmap *altmap)
  135. {
  136. if (altmap)
  137. return altmap_alloc_block_buf(size, altmap);
  138. return alloc_block_buf(size, node);
  139. }
  140. void __meminit vmemmap_verify(pte_t *pte, int node,
  141. unsigned long start, unsigned long end)
  142. {
  143. unsigned long pfn = pte_pfn(*pte);
  144. int actual_node = early_pfn_to_nid(pfn);
  145. if (node_distance(actual_node, node) > LOCAL_DISTANCE)
  146. pr_warn("[%lx-%lx] potential offnode page_structs\n",
  147. start, end - 1);
  148. }
  149. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
  150. {
  151. pte_t *pte = pte_offset_kernel(pmd, addr);
  152. if (pte_none(*pte)) {
  153. pte_t entry;
  154. void *p = alloc_block_buf(PAGE_SIZE, node);
  155. if (!p)
  156. return NULL;
  157. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  158. set_pte_at(&init_mm, addr, pte, entry);
  159. }
  160. return pte;
  161. }
  162. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  163. {
  164. pmd_t *pmd = pmd_offset(pud, addr);
  165. if (pmd_none(*pmd)) {
  166. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  167. if (!p)
  168. return NULL;
  169. pmd_populate_kernel(&init_mm, pmd, p);
  170. }
  171. return pmd;
  172. }
  173. pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
  174. {
  175. pud_t *pud = pud_offset(pgd, addr);
  176. if (pud_none(*pud)) {
  177. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  178. if (!p)
  179. return NULL;
  180. pud_populate(&init_mm, pud, p);
  181. }
  182. return pud;
  183. }
  184. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  185. {
  186. pgd_t *pgd = pgd_offset_k(addr);
  187. if (pgd_none(*pgd)) {
  188. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  189. if (!p)
  190. return NULL;
  191. pgd_populate(&init_mm, pgd, p);
  192. }
  193. return pgd;
  194. }
  195. int __meminit vmemmap_populate_basepages(unsigned long start,
  196. unsigned long end, int node)
  197. {
  198. unsigned long addr = start;
  199. pgd_t *pgd;
  200. pud_t *pud;
  201. pmd_t *pmd;
  202. pte_t *pte;
  203. for (; addr < end; addr += PAGE_SIZE) {
  204. pgd = vmemmap_pgd_populate(addr, node);
  205. if (!pgd)
  206. return -ENOMEM;
  207. pud = vmemmap_pud_populate(pgd, addr, node);
  208. if (!pud)
  209. return -ENOMEM;
  210. pmd = vmemmap_pmd_populate(pud, addr, node);
  211. if (!pmd)
  212. return -ENOMEM;
  213. pte = vmemmap_pte_populate(pmd, addr, node);
  214. if (!pte)
  215. return -ENOMEM;
  216. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  217. }
  218. return 0;
  219. }
  220. struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
  221. {
  222. unsigned long start;
  223. unsigned long end;
  224. struct page *map;
  225. map = pfn_to_page(pnum * PAGES_PER_SECTION);
  226. start = (unsigned long)map;
  227. end = (unsigned long)(map + PAGES_PER_SECTION);
  228. if (vmemmap_populate(start, end, nid))
  229. return NULL;
  230. return map;
  231. }
  232. void __init sparse_mem_maps_populate_node(struct page **map_map,
  233. unsigned long pnum_begin,
  234. unsigned long pnum_end,
  235. unsigned long map_count, int nodeid)
  236. {
  237. unsigned long pnum;
  238. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  239. void *vmemmap_buf_start;
  240. size = ALIGN(size, PMD_SIZE);
  241. vmemmap_buf_start = __earlyonly_bootmem_alloc(nodeid, size * map_count,
  242. PMD_SIZE, __pa(MAX_DMA_ADDRESS));
  243. if (vmemmap_buf_start) {
  244. vmemmap_buf = vmemmap_buf_start;
  245. vmemmap_buf_end = vmemmap_buf_start + size * map_count;
  246. }
  247. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  248. struct mem_section *ms;
  249. if (!present_section_nr(pnum))
  250. continue;
  251. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  252. if (map_map[pnum])
  253. continue;
  254. ms = __nr_to_section(pnum);
  255. pr_err("%s: sparsemem memory map backing failed some memory will not be available\n",
  256. __func__);
  257. ms->section_mem_map = 0;
  258. }
  259. if (vmemmap_buf_start) {
  260. /* need to free left buf */
  261. memblock_free_early(__pa(vmemmap_buf),
  262. vmemmap_buf_end - vmemmap_buf);
  263. vmemmap_buf = NULL;
  264. vmemmap_buf_end = NULL;
  265. }
  266. }