blob: 7408cabed61aad66eed41b725d4f181eeb55fedf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Lameter8f6aac42007-10-16 01:24:13 -07002/*
3 * Virtual Memory Map support
4 *
Christoph Lametercde53532008-07-04 09:59:22 -07005 * (C) 2007 sgi. Christoph Lameter.
Christoph Lameter8f6aac42007-10-16 01:24:13 -07006 *
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
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013 * via TLBs. For those arches the virtual memory map is essentially
Christoph Lameter8f6aac42007-10-16 01:24:13 -070014 * 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 *
Andy Whitcroft29c71112007-10-16 01:24:14 -070018 * The architecture is expected to provide a vmemmap_populate() function
19 * to instantiate the mapping.
Christoph Lameter8f6aac42007-10-16 01:24:13 -070020 */
21#include <linux/mm.h>
22#include <linux/mmzone.h>
23#include <linux/bootmem.h>
Mike Rapoport97ad1082018-10-30 15:09:44 -070024#include <linux/memblock.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080025#include <linux/memremap.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070026#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070028#include <linux/spinlock.h>
29#include <linux/vmalloc.h>
Glauber de Oliveira Costa8bca44b2007-10-29 14:37:19 -070030#include <linux/sched.h>
Christoph Lameter8f6aac42007-10-16 01:24:13 -070031#include <asm/dma.h>
32#include <asm/pgalloc.h>
33#include <asm/pgtable.h>
34
35/*
36 * Allocate a block of memory to be used to back the virtual memory map
37 * or to back the page tables that are used to create the mapping.
38 * Uses the main allocators if they are available, else bootmem.
39 */
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080040
Fabian Frederickbd721ea2016-08-02 14:03:33 -070041static void * __ref __earlyonly_bootmem_alloc(int node,
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080042 unsigned long size,
43 unsigned long align,
44 unsigned long goal)
45{
Mike Rapoporteb31d552018-10-30 15:08:04 -070046 return memblock_alloc_try_nid_raw(size, align, goal,
Mike Rapoport97ad1082018-10-30 15:09:44 -070047 MEMBLOCK_ALLOC_ACCESSIBLE, node);
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080048}
49
Christoph Lameter8f6aac42007-10-16 01:24:13 -070050void * __meminit vmemmap_alloc_block(unsigned long size, int node)
51{
52 /* If the main allocator is up use that, fallback to bootmem. */
53 if (slab_is_available()) {
Michal Hockofcdaf8422017-11-15 17:38:56 -080054 gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
55 int order = get_order(size);
56 static bool warned;
Shaohua Lif52407c2009-09-21 17:01:19 -070057 struct page *page;
58
Michal Hockofcdaf8422017-11-15 17:38:56 -080059 page = alloc_pages_node(node, gfp_mask, order);
Christoph Lameter8f6aac42007-10-16 01:24:13 -070060 if (page)
61 return page_address(page);
Michal Hockofcdaf8422017-11-15 17:38:56 -080062
63 if (!warned) {
64 warn_alloc(gfp_mask & ~__GFP_NOWARN, NULL,
65 "vmemmap alloc failure: order:%u", order);
66 warned = true;
67 }
Christoph Lameter8f6aac42007-10-16 01:24:13 -070068 return NULL;
69 } else
KAMEZAWA Hiroyukie0dc3a52007-11-28 16:21:57 -080070 return __earlyonly_bootmem_alloc(node, size, size,
Christoph Lameter8f6aac42007-10-16 01:24:13 -070071 __pa(MAX_DMA_ADDRESS));
72}
73
Yinghai Lu9bdac912010-02-10 01:20:22 -080074/* need to make sure size is all the same during early stage */
Christoph Hellwiga8fc3572017-12-29 08:53:58 +010075void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node)
Yinghai Lu9bdac912010-02-10 01:20:22 -080076{
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -070077 void *ptr = sparse_buffer_alloc(size);
Yinghai Lu9bdac912010-02-10 01:20:22 -080078
Pavel Tatashin35fd1eb2018-08-17 15:49:21 -070079 if (!ptr)
80 ptr = vmemmap_alloc_block(size, node);
Yinghai Lu9bdac912010-02-10 01:20:22 -080081 return ptr;
82}
83
Dan Williams4b94ffd2016-01-15 16:56:22 -080084static unsigned long __meminit vmem_altmap_next_pfn(struct vmem_altmap *altmap)
85{
86 return altmap->base_pfn + altmap->reserve + altmap->alloc
87 + altmap->align;
88}
89
90static unsigned long __meminit vmem_altmap_nr_free(struct vmem_altmap *altmap)
91{
92 unsigned long allocated = altmap->alloc + altmap->align;
93
94 if (altmap->free > allocated)
95 return altmap->free - allocated;
96 return 0;
97}
98
99/**
Christoph Hellwigeb804532017-12-29 08:53:59 +0100100 * altmap_alloc_block_buf - allocate pages from the device page map
101 * @altmap: device page map
102 * @size: size (in bytes) of the allocation
Dan Williams4b94ffd2016-01-15 16:56:22 -0800103 *
Christoph Hellwigeb804532017-12-29 08:53:59 +0100104 * Allocations are aligned to the size of the request.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800105 */
Christoph Hellwiga8fc3572017-12-29 08:53:58 +0100106void * __meminit altmap_alloc_block_buf(unsigned long size,
Dan Williams4b94ffd2016-01-15 16:56:22 -0800107 struct vmem_altmap *altmap)
108{
Christoph Hellwigeb804532017-12-29 08:53:59 +0100109 unsigned long pfn, nr_pfns, nr_align;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800110
111 if (size & ~PAGE_MASK) {
112 pr_warn_once("%s: allocations must be multiple of PAGE_SIZE (%ld)\n",
113 __func__, size);
114 return NULL;
115 }
116
Christoph Hellwigeb804532017-12-29 08:53:59 +0100117 pfn = vmem_altmap_next_pfn(altmap);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800118 nr_pfns = size >> PAGE_SHIFT;
Christoph Hellwigeb804532017-12-29 08:53:59 +0100119 nr_align = 1UL << find_first_bit(&nr_pfns, BITS_PER_LONG);
120 nr_align = ALIGN(pfn, nr_align) - pfn;
121 if (nr_pfns + nr_align > vmem_altmap_nr_free(altmap))
122 return NULL;
123
124 altmap->alloc += nr_pfns;
125 altmap->align += nr_align;
126 pfn += nr_align;
127
Dan Williams4b94ffd2016-01-15 16:56:22 -0800128 pr_debug("%s: pfn: %#lx alloc: %ld align: %ld nr: %#lx\n",
129 __func__, pfn, altmap->alloc, altmap->align, nr_pfns);
Christoph Hellwigeb804532017-12-29 08:53:59 +0100130 return __va(__pfn_to_phys(pfn));
Dan Williams4b94ffd2016-01-15 16:56:22 -0800131}
132
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700133void __meminit vmemmap_verify(pte_t *pte, int node,
134 unsigned long start, unsigned long end)
135{
136 unsigned long pfn = pte_pfn(*pte);
137 int actual_node = early_pfn_to_nid(pfn);
138
David Rientjesb41ad142008-11-06 12:53:31 -0800139 if (node_distance(actual_node, node) > LOCAL_DISTANCE)
Joe Perches11705322016-03-17 14:19:50 -0700140 pr_warn("[%lx-%lx] potential offnode page_structs\n",
141 start, end - 1);
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700142}
143
Andy Whitcroft29c71112007-10-16 01:24:14 -0700144pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700145{
Andy Whitcroft29c71112007-10-16 01:24:14 -0700146 pte_t *pte = pte_offset_kernel(pmd, addr);
147 if (pte_none(*pte)) {
148 pte_t entry;
Christoph Hellwiga8fc3572017-12-29 08:53:58 +0100149 void *p = vmemmap_alloc_block_buf(PAGE_SIZE, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700150 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000151 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700152 entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
153 set_pte_at(&init_mm, addr, pte, entry);
154 }
155 return pte;
156}
157
Pavel Tatashinf7f99102017-11-15 17:36:44 -0800158static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)
159{
160 void *p = vmemmap_alloc_block(size, node);
161
162 if (!p)
163 return NULL;
164 memset(p, 0, size);
165
166 return p;
167}
168
Andy Whitcroft29c71112007-10-16 01:24:14 -0700169pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
170{
171 pmd_t *pmd = pmd_offset(pud, addr);
172 if (pmd_none(*pmd)) {
Pavel Tatashinf7f99102017-11-15 17:36:44 -0800173 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700174 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000175 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700176 pmd_populate_kernel(&init_mm, pmd, p);
177 }
178 return pmd;
179}
180
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300181pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node)
Andy Whitcroft29c71112007-10-16 01:24:14 -0700182{
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300183 pud_t *pud = pud_offset(p4d, addr);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700184 if (pud_none(*pud)) {
Pavel Tatashinf7f99102017-11-15 17:36:44 -0800185 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700186 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000187 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700188 pud_populate(&init_mm, pud, p);
189 }
190 return pud;
191}
192
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300193p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node)
194{
195 p4d_t *p4d = p4d_offset(pgd, addr);
196 if (p4d_none(*p4d)) {
Pavel Tatashinf7f99102017-11-15 17:36:44 -0800197 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300198 if (!p)
199 return NULL;
200 p4d_populate(&init_mm, p4d, p);
201 }
202 return p4d;
203}
204
Andy Whitcroft29c71112007-10-16 01:24:14 -0700205pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
206{
207 pgd_t *pgd = pgd_offset_k(addr);
208 if (pgd_none(*pgd)) {
Pavel Tatashinf7f99102017-11-15 17:36:44 -0800209 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700210 if (!p)
Al Viro9dce07f2008-03-29 03:07:28 +0000211 return NULL;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700212 pgd_populate(&init_mm, pgd, p);
213 }
214 return pgd;
215}
216
Johannes Weiner0aad8182013-04-29 15:07:50 -0700217int __meminit vmemmap_populate_basepages(unsigned long start,
218 unsigned long end, int node)
Andy Whitcroft29c71112007-10-16 01:24:14 -0700219{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700220 unsigned long addr = start;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700221 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300222 p4d_t *p4d;
Andy Whitcroft29c71112007-10-16 01:24:14 -0700223 pud_t *pud;
224 pmd_t *pmd;
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700225 pte_t *pte;
226
Andy Whitcroft29c71112007-10-16 01:24:14 -0700227 for (; addr < end; addr += PAGE_SIZE) {
228 pgd = vmemmap_pgd_populate(addr, node);
229 if (!pgd)
230 return -ENOMEM;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300231 p4d = vmemmap_p4d_populate(pgd, addr, node);
232 if (!p4d)
233 return -ENOMEM;
234 pud = vmemmap_pud_populate(p4d, addr, node);
Andy Whitcroft29c71112007-10-16 01:24:14 -0700235 if (!pud)
236 return -ENOMEM;
237 pmd = vmemmap_pmd_populate(pud, addr, node);
238 if (!pmd)
239 return -ENOMEM;
240 pte = vmemmap_pte_populate(pmd, addr, node);
241 if (!pte)
242 return -ENOMEM;
243 vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
244 }
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700245
246 return 0;
247}
248
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100249struct page * __meminit sparse_mem_map_populate(unsigned long pnum, int nid,
250 struct vmem_altmap *altmap)
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700251{
Johannes Weiner0aad8182013-04-29 15:07:50 -0700252 unsigned long start;
253 unsigned long end;
254 struct page *map;
255
256 map = pfn_to_page(pnum * PAGES_PER_SECTION);
257 start = (unsigned long)map;
258 end = (unsigned long)(map + PAGES_PER_SECTION);
259
Christoph Hellwig7b73d972017-12-29 08:53:54 +0100260 if (vmemmap_populate(start, end, nid, altmap))
Christoph Lameter8f6aac42007-10-16 01:24:13 -0700261 return NULL;
262
263 return map;
264}