sparse-vmemmap.c 5.9 KB

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