sparse-vmemmap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtual Memory Map support
  4. *
  5. * (C) 2007 sgi. Christoph Lameter.
  6. *
  7. * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn,
  8. * virt_to_page, page_address() to be implemented as a base offset
  9. * calculation without memory access.
  10. *
  11. * However, virtual mappings need a page table and TLBs. Many Linux
  12. * architectures already map their physical space using 1-1 mappings
  13. * via TLBs. For those arches the virtual memory map is essentially
  14. * for free if we use the same page size as the 1-1 mappings. In that
  15. * case the overhead consists of a few additional pages that are
  16. * allocated to create a view of memory for vmemmap.
  17. *
  18. * The architecture is expected to provide a vmemmap_populate() function
  19. * to instantiate the mapping.
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/mmzone.h>
  23. #include <linux/memblock.h>
  24. #include <linux/memremap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/sched.h>
  30. #include <asm/dma.h>
  31. #include <asm/pgalloc.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_alloc_try_nid_raw(size, align, goal,
  43. MEMBLOCK_ALLOC_ACCESSIBLE, node);
  44. }
  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. gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
  50. int order = get_order(size);
  51. static bool warned;
  52. struct page *page;
  53. page = alloc_pages_node(node, gfp_mask, order);
  54. if (page)
  55. return page_address(page);
  56. if (!warned) {
  57. warn_alloc(gfp_mask & ~__GFP_NOWARN, NULL,
  58. "vmemmap alloc failure: order:%u", order);
  59. warned = true;
  60. }
  61. return NULL;
  62. } else
  63. return __earlyonly_bootmem_alloc(node, size, size,
  64. __pa(MAX_DMA_ADDRESS));
  65. }
  66. static void * __meminit altmap_alloc_block_buf(unsigned long size,
  67. struct vmem_altmap *altmap);
  68. /* need to make sure size is all the same during early stage */
  69. void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node,
  70. struct vmem_altmap *altmap)
  71. {
  72. void *ptr;
  73. if (altmap)
  74. return altmap_alloc_block_buf(size, altmap);
  75. ptr = sparse_buffer_alloc(size);
  76. if (!ptr)
  77. ptr = vmemmap_alloc_block(size, node);
  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. static void * __meminit altmap_alloc_block_buf(unsigned long size,
  93. struct vmem_altmap *altmap)
  94. {
  95. unsigned long pfn, nr_pfns, nr_align;
  96. if (size & ~PAGE_MASK) {
  97. pr_warn_once("%s: allocations must be multiple of PAGE_SIZE (%ld)\n",
  98. __func__, size);
  99. return NULL;
  100. }
  101. pfn = vmem_altmap_next_pfn(altmap);
  102. nr_pfns = size >> PAGE_SHIFT;
  103. nr_align = 1UL << find_first_bit(&nr_pfns, BITS_PER_LONG);
  104. nr_align = ALIGN(pfn, nr_align) - pfn;
  105. if (nr_pfns + nr_align > vmem_altmap_nr_free(altmap))
  106. return NULL;
  107. altmap->alloc += nr_pfns;
  108. altmap->align += nr_align;
  109. pfn += nr_align;
  110. pr_debug("%s: pfn: %#lx alloc: %ld align: %ld nr: %#lx\n",
  111. __func__, pfn, altmap->alloc, altmap->align, nr_pfns);
  112. return __va(__pfn_to_phys(pfn));
  113. }
  114. void __meminit vmemmap_verify(pte_t *pte, int node,
  115. unsigned long start, unsigned long end)
  116. {
  117. unsigned long pfn = pte_pfn(*pte);
  118. int actual_node = early_pfn_to_nid(pfn);
  119. if (node_distance(actual_node, node) > LOCAL_DISTANCE)
  120. pr_warn_once("[%lx-%lx] potential offnode page_structs\n",
  121. start, end - 1);
  122. }
  123. pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
  124. struct vmem_altmap *altmap,
  125. struct page *reuse)
  126. {
  127. pte_t *pte = pte_offset_kernel(pmd, addr);
  128. if (pte_none(*pte)) {
  129. pte_t entry;
  130. void *p;
  131. if (!reuse) {
  132. p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
  133. if (!p)
  134. return NULL;
  135. } else {
  136. /*
  137. * When a PTE/PMD entry is freed from the init_mm
  138. * there's a free_pages() call to this page allocated
  139. * above. Thus this get_page() is paired with the
  140. * put_page_testzero() on the freeing path.
  141. * This can only called by certain ZONE_DEVICE path,
  142. * and through vmemmap_populate_compound_pages() when
  143. * slab is available.
  144. */
  145. get_page(reuse);
  146. p = page_to_virt(reuse);
  147. }
  148. entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
  149. set_pte_at(&init_mm, addr, pte, entry);
  150. }
  151. return pte;
  152. }
  153. static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)
  154. {
  155. void *p = vmemmap_alloc_block(size, node);
  156. if (!p)
  157. return NULL;
  158. memset(p, 0, size);
  159. return p;
  160. }
  161. pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
  162. {
  163. pmd_t *pmd = pmd_offset(pud, addr);
  164. if (pmd_none(*pmd)) {
  165. void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
  166. if (!p)
  167. return NULL;
  168. pmd_populate_kernel(&init_mm, pmd, p);
  169. }
  170. return pmd;
  171. }
  172. pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node)
  173. {
  174. pud_t *pud = pud_offset(p4d, addr);
  175. if (pud_none(*pud)) {
  176. void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
  177. if (!p)
  178. return NULL;
  179. pud_populate(&init_mm, pud, p);
  180. }
  181. return pud;
  182. }
  183. p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node)
  184. {
  185. p4d_t *p4d = p4d_offset(pgd, addr);
  186. if (p4d_none(*p4d)) {
  187. void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
  188. if (!p)
  189. return NULL;
  190. p4d_populate(&init_mm, p4d, p);
  191. }
  192. return p4d;
  193. }
  194. pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
  195. {
  196. pgd_t *pgd = pgd_offset_k(addr);
  197. if (pgd_none(*pgd)) {
  198. void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
  199. if (!p)
  200. return NULL;
  201. pgd_populate(&init_mm, pgd, p);
  202. }
  203. return pgd;
  204. }
  205. static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
  206. struct vmem_altmap *altmap,
  207. struct page *reuse)
  208. {
  209. pgd_t *pgd;
  210. p4d_t *p4d;
  211. pud_t *pud;
  212. pmd_t *pmd;
  213. pte_t *pte;
  214. pgd = vmemmap_pgd_populate(addr, node);
  215. if (!pgd)
  216. return NULL;
  217. p4d = vmemmap_p4d_populate(pgd, addr, node);
  218. if (!p4d)
  219. return NULL;
  220. pud = vmemmap_pud_populate(p4d, addr, node);
  221. if (!pud)
  222. return NULL;
  223. pmd = vmemmap_pmd_populate(pud, addr, node);
  224. if (!pmd)
  225. return NULL;
  226. pte = vmemmap_pte_populate(pmd, addr, node, altmap, reuse);
  227. if (!pte)
  228. return NULL;
  229. vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
  230. return pte;
  231. }
  232. static int __meminit vmemmap_populate_range(unsigned long start,
  233. unsigned long end, int node,
  234. struct vmem_altmap *altmap,
  235. struct page *reuse)
  236. {
  237. unsigned long addr = start;
  238. pte_t *pte;
  239. for (; addr < end; addr += PAGE_SIZE) {
  240. pte = vmemmap_populate_address(addr, node, altmap, reuse);
  241. if (!pte)
  242. return -ENOMEM;
  243. }
  244. return 0;
  245. }
  246. int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
  247. int node, struct vmem_altmap *altmap)
  248. {
  249. return vmemmap_populate_range(start, end, node, altmap, NULL);
  250. }
  251. /*
  252. * For compound pages bigger than section size (e.g. x86 1G compound
  253. * pages with 2M subsection size) fill the rest of sections as tail
  254. * pages.
  255. *
  256. * Note that memremap_pages() resets @nr_range value and will increment
  257. * it after each range successful onlining. Thus the value or @nr_range
  258. * at section memmap populate corresponds to the in-progress range
  259. * being onlined here.
  260. */
  261. static bool __meminit reuse_compound_section(unsigned long start_pfn,
  262. struct dev_pagemap *pgmap)
  263. {
  264. unsigned long nr_pages = pgmap_vmemmap_nr(pgmap);
  265. unsigned long offset = start_pfn -
  266. PHYS_PFN(pgmap->ranges[pgmap->nr_range].start);
  267. return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION;
  268. }
  269. static pte_t * __meminit compound_section_tail_page(unsigned long addr)
  270. {
  271. pte_t *pte;
  272. addr -= PAGE_SIZE;
  273. /*
  274. * Assuming sections are populated sequentially, the previous section's
  275. * page data can be reused.
  276. */
  277. pte = pte_offset_kernel(pmd_off_k(addr), addr);
  278. if (!pte)
  279. return NULL;
  280. return pte;
  281. }
  282. static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
  283. unsigned long start,
  284. unsigned long end, int node,
  285. struct dev_pagemap *pgmap)
  286. {
  287. unsigned long size, addr;
  288. pte_t *pte;
  289. int rc;
  290. if (reuse_compound_section(start_pfn, pgmap)) {
  291. pte = compound_section_tail_page(start);
  292. if (!pte)
  293. return -ENOMEM;
  294. /*
  295. * Reuse the page that was populated in the prior iteration
  296. * with just tail struct pages.
  297. */
  298. return vmemmap_populate_range(start, end, node, NULL,
  299. pte_page(*pte));
  300. }
  301. size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page));
  302. for (addr = start; addr < end; addr += size) {
  303. unsigned long next, last = addr + size;
  304. /* Populate the head page vmemmap page */
  305. pte = vmemmap_populate_address(addr, node, NULL, NULL);
  306. if (!pte)
  307. return -ENOMEM;
  308. /* Populate the tail pages vmemmap page */
  309. next = addr + PAGE_SIZE;
  310. pte = vmemmap_populate_address(next, node, NULL, NULL);
  311. if (!pte)
  312. return -ENOMEM;
  313. /*
  314. * Reuse the previous page for the rest of tail pages
  315. * See layout diagram in Documentation/mm/vmemmap_dedup.rst
  316. */
  317. next += PAGE_SIZE;
  318. rc = vmemmap_populate_range(next, last, node, NULL,
  319. pte_page(*pte));
  320. if (rc)
  321. return -ENOMEM;
  322. }
  323. return 0;
  324. }
  325. struct page * __meminit __populate_section_memmap(unsigned long pfn,
  326. unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
  327. struct dev_pagemap *pgmap)
  328. {
  329. unsigned long start = (unsigned long) pfn_to_page(pfn);
  330. unsigned long end = start + nr_pages * sizeof(struct page);
  331. int r;
  332. if (WARN_ON_ONCE(!IS_ALIGNED(pfn, PAGES_PER_SUBSECTION) ||
  333. !IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))
  334. return NULL;
  335. if (is_power_of_2(sizeof(struct page)) &&
  336. pgmap && pgmap_vmemmap_nr(pgmap) > 1 && !altmap)
  337. r = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);
  338. else
  339. r = vmemmap_populate(start, end, nid, altmap);
  340. if (r < 0)
  341. return NULL;
  342. return pfn_to_page(pfn);
  343. }