sparse-vmemmap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/highmem.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/sched.h>
  28. #include <asm/dma.h>
  29. #include <asm/pgalloc.h>
  30. #include <asm/pgtable.h>
  31. /*
  32. * Allocate a block of memory to be used to back the virtual memory map
  33. * or to back the page tables that are used to create the mapping.
  34. * Uses the main allocators if they are available, else bootmem.
  35. */
  36. static void * __init_refok __earlyonly_bootmem_alloc(int node,
  37. unsigned long size,
  38. unsigned long align,
  39. unsigned long goal)
  40. {
  41. return memblock_virt_alloc_try_nid(size, align, goal,
  42. BOOTMEM_ALLOC_ACCESSIBLE, node);
  43. }
  44. static void *vmemmap_buf;
  45. static void *vmemmap_buf_end;
  46. void * __meminit vmemmap_alloc_block(unsigned long size, int node)
  47. {
  48. /* If the main allocator is up use that, fallback to bootmem. */
  49. if (slab_is_available()) {
  50. struct page *page;
  51. if (node_state(node, N_HIGH_MEMORY))
  52. page = alloc_pages_node(
  53. node, GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT,
  54. get_order(size));
  55. else
  56. page = alloc_pages(
  57. GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT,
  58. get_order(size));
  59. if (page)
  60. return page_address(page);
  61. return NULL;
  62. } else
  63. return __earlyonly_bootmem_alloc(node, size, size,
  64. __pa(MAX_DMA_ADDRESS));
  65. }
  66. /* need to make sure size is all the same during early stage */
  67. void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node)
  68. {
  69. void *ptr;
  70. if (!vmemmap_buf)
  71. return vmemmap_alloc_block(size, node);
  72. /* take the from buf */
  73. ptr = (void *)ALIGN((unsigned long)vmemmap_buf, size);
  74. if (ptr + size > vmemmap_buf_end)
  75. return vmemmap_alloc_block(size, node);
  76. vmemmap_buf = ptr + size;
  77. return ptr;
  78. }
  79. void __meminit vmemmap_verify(pte_t *pte, int node,
  80. unsigned long start, unsigned long end)
  81. {
  82. unsigned long pfn = pte_pfn(*pte);
  83. int actual_node = early_pfn_to_nid(pfn);
  84. if (node_distance(actual_node, node) > LOCAL_DISTANCE)
  85. printk(KERN_WARNING "[%lx-%lx] potential offnode "
  86. "page_structs\n", start, end - 1);
  87. }
  88. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
  89. {
  90. pte_t *pte = pte_offset_kernel(pmd, addr);
  91. if (pte_none(*pte)) {
  92. pte_t entry;
  93. void *p = vmemmap_alloc_block_buf(PAGE_SIZE, node);
  94. if (!p)
  95. return NULL;
  96. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  97. set_pte_at(&init_mm, addr, pte, entry);
  98. }
  99. return pte;
  100. }
  101. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  102. {
  103. pmd_t *pmd = pmd_offset(pud, addr);
  104. if (pmd_none(*pmd)) {
  105. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  106. if (!p)
  107. return NULL;
  108. pmd_populate_kernel(&init_mm, pmd, p);
  109. }
  110. return pmd;
  111. }
  112. pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
  113. {
  114. pud_t *pud = pud_offset(pgd, addr);
  115. if (pud_none(*pud)) {
  116. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  117. if (!p)
  118. return NULL;
  119. pud_populate(&init_mm, pud, p);
  120. }
  121. return pud;
  122. }
  123. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  124. {
  125. pgd_t *pgd = pgd_offset_k(addr);
  126. if (pgd_none(*pgd)) {
  127. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  128. if (!p)
  129. return NULL;
  130. pgd_populate(&init_mm, pgd, p);
  131. }
  132. return pgd;
  133. }
  134. int __meminit vmemmap_populate_basepages(unsigned long start,
  135. unsigned long end, int node)
  136. {
  137. unsigned long addr = start;
  138. pgd_t *pgd;
  139. pud_t *pud;
  140. pmd_t *pmd;
  141. pte_t *pte;
  142. for (; addr < end; addr += PAGE_SIZE) {
  143. pgd = vmemmap_pgd_populate(addr, node);
  144. if (!pgd)
  145. return -ENOMEM;
  146. pud = vmemmap_pud_populate(pgd, addr, node);
  147. if (!pud)
  148. return -ENOMEM;
  149. pmd = vmemmap_pmd_populate(pud, addr, node);
  150. if (!pmd)
  151. return -ENOMEM;
  152. pte = vmemmap_pte_populate(pmd, addr, node);
  153. if (!pte)
  154. return -ENOMEM;
  155. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  156. }
  157. return 0;
  158. }
  159. struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
  160. {
  161. unsigned long start;
  162. unsigned long end;
  163. struct page *map;
  164. map = pfn_to_page(pnum * PAGES_PER_SECTION);
  165. start = (unsigned long)map;
  166. end = (unsigned long)(map + PAGES_PER_SECTION);
  167. if (vmemmap_populate(start, end, nid))
  168. return NULL;
  169. return map;
  170. }
  171. void __init sparse_mem_maps_populate_node(struct page **map_map,
  172. unsigned long pnum_begin,
  173. unsigned long pnum_end,
  174. unsigned long map_count, int nodeid)
  175. {
  176. unsigned long pnum;
  177. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  178. void *vmemmap_buf_start;
  179. size = ALIGN(size, PMD_SIZE);
  180. vmemmap_buf_start = __earlyonly_bootmem_alloc(nodeid, size * map_count,
  181. PMD_SIZE, __pa(MAX_DMA_ADDRESS));
  182. if (vmemmap_buf_start) {
  183. vmemmap_buf = vmemmap_buf_start;
  184. vmemmap_buf_end = vmemmap_buf_start + size * map_count;
  185. }
  186. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  187. struct mem_section *ms;
  188. if (!present_section_nr(pnum))
  189. continue;
  190. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  191. if (map_map[pnum])
  192. continue;
  193. ms = __nr_to_section(pnum);
  194. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  195. "some memory will not be available.\n", __func__);
  196. ms->section_mem_map = 0;
  197. }
  198. if (vmemmap_buf_start) {
  199. /* need to free left buf */
  200. memblock_free_early(__pa(vmemmap_buf),
  201. vmemmap_buf_end - vmemmap_buf);
  202. vmemmap_buf = NULL;
  203. vmemmap_buf_end = NULL;
  204. }
  205. }