sparse-vmemmap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/module.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 * __init_refok __earlyonly_bootmem_alloc(int node,
  38. unsigned long size,
  39. unsigned long align,
  40. unsigned long goal)
  41. {
  42. return __alloc_bootmem_node_high(NODE_DATA(node), size, align, goal);
  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(node,
  53. GFP_KERNEL | __GFP_ZERO, get_order(size));
  54. else
  55. page = alloc_pages(GFP_KERNEL | __GFP_ZERO,
  56. get_order(size));
  57. if (page)
  58. return page_address(page);
  59. return NULL;
  60. } else
  61. return __earlyonly_bootmem_alloc(node, size, size,
  62. __pa(MAX_DMA_ADDRESS));
  63. }
  64. /* need to make sure size is all the same during early stage */
  65. void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node)
  66. {
  67. void *ptr;
  68. if (!vmemmap_buf)
  69. return vmemmap_alloc_block(size, node);
  70. /* take the from buf */
  71. ptr = (void *)ALIGN((unsigned long)vmemmap_buf, size);
  72. if (ptr + size > vmemmap_buf_end)
  73. return vmemmap_alloc_block(size, node);
  74. vmemmap_buf = ptr + size;
  75. return ptr;
  76. }
  77. void __meminit vmemmap_verify(pte_t *pte, int node,
  78. unsigned long start, unsigned long end)
  79. {
  80. unsigned long pfn = pte_pfn(*pte);
  81. int actual_node = early_pfn_to_nid(pfn);
  82. if (node_distance(actual_node, node) > LOCAL_DISTANCE)
  83. printk(KERN_WARNING "[%lx-%lx] potential offnode "
  84. "page_structs\n", start, end - 1);
  85. }
  86. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
  87. {
  88. pte_t *pte = pte_offset_kernel(pmd, addr);
  89. if (pte_none(*pte)) {
  90. pte_t entry;
  91. void *p = vmemmap_alloc_block_buf(PAGE_SIZE, node);
  92. if (!p)
  93. return NULL;
  94. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  95. set_pte_at(&init_mm, addr, pte, entry);
  96. }
  97. return pte;
  98. }
  99. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  100. {
  101. pmd_t *pmd = pmd_offset(pud, addr);
  102. if (pmd_none(*pmd)) {
  103. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  104. if (!p)
  105. return NULL;
  106. pmd_populate_kernel(&init_mm, pmd, p);
  107. }
  108. return pmd;
  109. }
  110. pud_t * __meminit vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node)
  111. {
  112. pud_t *pud = pud_offset(pgd, addr);
  113. if (pud_none(*pud)) {
  114. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  115. if (!p)
  116. return NULL;
  117. pud_populate(&init_mm, pud, p);
  118. }
  119. return pud;
  120. }
  121. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  122. {
  123. pgd_t *pgd = pgd_offset_k(addr);
  124. if (pgd_none(*pgd)) {
  125. void *p = vmemmap_alloc_block(PAGE_SIZE, node);
  126. if (!p)
  127. return NULL;
  128. pgd_populate(&init_mm, pgd, p);
  129. }
  130. return pgd;
  131. }
  132. int __meminit vmemmap_populate_basepages(struct page *start_page,
  133. unsigned long size, int node)
  134. {
  135. unsigned long addr = (unsigned long)start_page;
  136. unsigned long end = (unsigned long)(start_page + size);
  137. pgd_t *pgd;
  138. pud_t *pud;
  139. pmd_t *pmd;
  140. pte_t *pte;
  141. for (; addr < end; addr += PAGE_SIZE) {
  142. pgd = vmemmap_pgd_populate(addr, node);
  143. if (!pgd)
  144. return -ENOMEM;
  145. pud = vmemmap_pud_populate(pgd, addr, node);
  146. if (!pud)
  147. return -ENOMEM;
  148. pmd = vmemmap_pmd_populate(pud, addr, node);
  149. if (!pmd)
  150. return -ENOMEM;
  151. pte = vmemmap_pte_populate(pmd, addr, node);
  152. if (!pte)
  153. return -ENOMEM;
  154. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  155. }
  156. return 0;
  157. }
  158. struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid)
  159. {
  160. struct page *map = pfn_to_page(pnum * PAGES_PER_SECTION);
  161. int error = vmemmap_populate(map, PAGES_PER_SECTION, nid);
  162. if (error)
  163. return NULL;
  164. return map;
  165. }
  166. void __init sparse_mem_maps_populate_node(struct page **map_map,
  167. unsigned long pnum_begin,
  168. unsigned long pnum_end,
  169. unsigned long map_count, int nodeid)
  170. {
  171. unsigned long pnum;
  172. unsigned long size = sizeof(struct page) * PAGES_PER_SECTION;
  173. void *vmemmap_buf_start;
  174. size = ALIGN(size, PMD_SIZE);
  175. vmemmap_buf_start = __earlyonly_bootmem_alloc(nodeid, size * map_count,
  176. PMD_SIZE, __pa(MAX_DMA_ADDRESS));
  177. if (vmemmap_buf_start) {
  178. vmemmap_buf = vmemmap_buf_start;
  179. vmemmap_buf_end = vmemmap_buf_start + size * map_count;
  180. }
  181. for (pnum = pnum_begin; pnum < pnum_end; pnum++) {
  182. struct mem_section *ms;
  183. if (!present_section_nr(pnum))
  184. continue;
  185. map_map[pnum] = sparse_mem_map_populate(pnum, nodeid);
  186. if (map_map[pnum])
  187. continue;
  188. ms = __nr_to_section(pnum);
  189. printk(KERN_ERR "%s: sparsemem memory map backing failed "
  190. "some memory will not be available.\n", __func__);
  191. ms->section_mem_map = 0;
  192. }
  193. if (vmemmap_buf_start) {
  194. /* need to free left buf */
  195. free_bootmem(__pa(vmemmap_buf), vmemmap_buf_end - vmemmap_buf);
  196. vmemmap_buf = NULL;
  197. vmemmap_buf_end = NULL;
  198. }
  199. }