blob: fbd14e5067581eb7cdeb3a9b2ffbf4b5ecf38d91 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -07002#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/gfp.h>
Joerg Roedele3e28812018-04-11 17:24:38 +02004#include <linux/hugetlb.h>
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -07005#include <asm/pgalloc.h>
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -07006#include <asm/pgtable.h>
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -07007#include <asm/tlb.h>
Ingo Molnara1d5a862008-06-20 15:34:46 +02008#include <asm/fixmap.h>
Toshi Kani6b637832015-04-14 15:47:32 -07009#include <asm/mtrr.h>
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -070010
Kirill A. Shutemov94d49eb2018-05-18 14:30:28 +030011#ifdef CONFIG_DYNAMIC_PHYSICAL_MASK
12phys_addr_t physical_mask __ro_after_init = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
13EXPORT_SYMBOL(physical_mask);
14#endif
15
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -080016#define PGALLOC_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO)
Vegard Nossum9e730232009-02-22 11:28:25 +010017
Ian Campbell14315592010-02-17 10:38:10 +000018#ifdef CONFIG_HIGHPTE
19#define PGALLOC_USER_GFP __GFP_HIGHMEM
20#else
21#define PGALLOC_USER_GFP 0
22#endif
23
24gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP;
25
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -070026pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
27{
Vladimir Davydov3e79ec72016-07-26 15:24:30 -070028 return (pte_t *)__get_free_page(PGALLOC_GFP & ~__GFP_ACCOUNT);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -070029}
30
31pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
32{
33 struct page *pte;
34
Ian Campbell14315592010-02-17 10:38:10 +000035 pte = alloc_pages(__userpte_alloc_gfp, 0);
Kirill A. Shutemovcecbd1b2013-11-14 14:31:47 -080036 if (!pte)
37 return NULL;
38 if (!pgtable_page_ctor(pte)) {
39 __free_page(pte);
40 return NULL;
41 }
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -070042 return pte;
43}
44
Ian Campbell14315592010-02-17 10:38:10 +000045static int __init setup_userpte(char *arg)
46{
47 if (!arg)
48 return -EINVAL;
49
50 /*
51 * "userpte=nohigh" disables allocation of user pagetables in
52 * high memory.
53 */
54 if (strcmp(arg, "nohigh") == 0)
55 __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
56 else
57 return -EINVAL;
58 return 0;
59}
60early_param("userpte", setup_userpte);
61
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +100062void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
Jeremy Fitzhardinge397f6872008-03-17 16:36:57 -070063{
64 pgtable_page_dtor(pte);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -070065 paravirt_release_pte(page_to_pfn(pte));
Vitaly Kuznetsov9e52fc22017-08-28 10:22:51 +020066 tlb_remove_table(tlb, pte);
Jeremy Fitzhardinge397f6872008-03-17 16:36:57 -070067}
68
Kirill A. Shutemov98233362015-04-14 15:46:14 -070069#if CONFIG_PGTABLE_LEVELS > 2
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +100070void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
Jeremy Fitzhardinge170fdff2008-03-17 16:36:58 -070071{
Kirill A. Shutemovc2836102013-11-21 14:32:09 -080072 struct page *page = virt_to_page(pmd);
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -070073 paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
Dave Hansen1de14c32013-04-12 16:23:54 -070074 /*
75 * NOTE! For PAE, any changes to the top page-directory-pointer-table
76 * entries need a full cr3 reload to flush.
77 */
78#ifdef CONFIG_X86_PAE
79 tlb->need_flush_all = 1;
80#endif
Kirill A. Shutemovc2836102013-11-21 14:32:09 -080081 pgtable_pmd_page_dtor(page);
Vitaly Kuznetsov9e52fc22017-08-28 10:22:51 +020082 tlb_remove_table(tlb, page);
Jeremy Fitzhardinge170fdff2008-03-17 16:36:58 -070083}
Jeremy Fitzhardinge5a5f8f42008-03-17 16:36:59 -070084
Kirill A. Shutemov98233362015-04-14 15:46:14 -070085#if CONFIG_PGTABLE_LEVELS > 3
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +100086void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
Jeremy Fitzhardinge5a5f8f42008-03-17 16:36:59 -070087{
Jeremy Fitzhardinge2761fa02008-03-17 16:37:02 -070088 paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
Vitaly Kuznetsov9e52fc22017-08-28 10:22:51 +020089 tlb_remove_table(tlb, virt_to_page(pud));
Jeremy Fitzhardinge5a5f8f42008-03-17 16:36:59 -070090}
Kirill A. Shutemovb8504052017-03-30 11:07:29 +030091
92#if CONFIG_PGTABLE_LEVELS > 4
93void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d)
94{
95 paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT);
Vitaly Kuznetsov9e52fc22017-08-28 10:22:51 +020096 tlb_remove_table(tlb, virt_to_page(p4d));
Kirill A. Shutemovb8504052017-03-30 11:07:29 +030097}
98#endif /* CONFIG_PGTABLE_LEVELS > 4 */
Kirill A. Shutemov98233362015-04-14 15:46:14 -070099#endif /* CONFIG_PGTABLE_LEVELS > 3 */
100#endif /* CONFIG_PGTABLE_LEVELS > 2 */
Jeremy Fitzhardinge170fdff2008-03-17 16:36:58 -0700101
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700102static inline void pgd_list_add(pgd_t *pgd)
103{
104 struct page *page = virt_to_page(pgd);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700105
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700106 list_add(&page->lru, &pgd_list);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700107}
108
109static inline void pgd_list_del(pgd_t *pgd)
110{
111 struct page *page = virt_to_page(pgd);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700112
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700113 list_del(&page->lru);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700114}
115
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700116#define UNSHARED_PTRS_PER_PGD \
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700117 (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700118
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700119
120static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
121{
Matthew Wilcoxa052f0a2018-06-07 17:08:57 -0700122 virt_to_page(pgd)->pt_mm = mm;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700123}
124
125struct mm_struct *pgd_page_get_mm(struct page *page)
126{
Matthew Wilcoxa052f0a2018-06-07 17:08:57 -0700127 return page->pt_mm;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700128}
129
130static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700131{
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700132 /* If the pgd points to a shared pagetable level (either the
133 ptes in non-PAE, or shared PMD in PAE), then just copy the
134 references from swapper_pg_dir. */
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700135 if (CONFIG_PGTABLE_LEVELS == 2 ||
136 (CONFIG_PGTABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
Kirill A. Shutemovb8504052017-03-30 11:07:29 +0300137 CONFIG_PGTABLE_LEVELS >= 4) {
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700138 clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
139 swapper_pg_dir + KERNEL_PGD_BOUNDARY,
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700140 KERNEL_PGD_PTRS);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700141 }
142
143 /* list required to sync kernel mapping updates */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700144 if (!SHARED_KERNEL_PMD) {
145 pgd_set_mm(pgd, mm);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700146 pgd_list_add(pgd);
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700147 }
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700148}
149
Jan Beulich17b74622008-08-29 12:51:32 +0100150static void pgd_dtor(pgd_t *pgd)
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700151{
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700152 if (SHARED_KERNEL_PMD)
153 return;
154
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800155 spin_lock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700156 pgd_list_del(pgd);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800157 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700158}
159
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -0700160/*
161 * List of all pgd's needed for non-PAE so it can invalidate entries
162 * in both cached and uncached pgd's; not needed for PAE since the
163 * kernel pmd is shared. If PAE were not to share the pmd a similar
164 * tactic would be needed. This is essentially codepath-based locking
165 * against pageattr.c; it is the unique case in which a valid change
166 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
167 * vmalloc faults work because attached pagetables are never freed.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100168 * -- nyc
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -0700169 */
170
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700171#ifdef CONFIG_X86_PAE
172/*
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700173 * In PAE mode, we need to do a cr3 reload (=tlb flush) when
174 * updating the top-level pagetable entries to guarantee the
175 * processor notices the update. Since this is expensive, and
176 * all 4 top-level entries are used almost immediately in a
177 * new process's life, we just pre-populate them here.
178 *
179 * Also, if we're in a paravirt environment where the kernel pmd is
180 * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
181 * and initialize the kernel pmds here.
182 */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400183#define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100184
185void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
186{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700187 paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100188
189 /* Note: almost everything apart from _PAGE_PRESENT is
190 reserved at the pmd (PDPT) level. */
191 set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
192
193 /*
194 * According to Intel App note "TLBs, Paging-Structure Caches,
195 * and Their Invalidation", April 2007, document 317080-001,
196 * section 8.1: in PAE mode we explicitly have to flush the
197 * TLB via cr3 if the top-level pgd is changed...
198 */
Shaohua Li4981d012011-03-16 11:37:29 +0800199 flush_tlb_mm(mm);
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100200}
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700201#else /* !CONFIG_X86_PAE */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400202
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700203/* No need to prepopulate any pagetable entries in non-PAE modes. */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400204#define PREALLOCATED_PMDS 0
205
206#endif /* CONFIG_X86_PAE */
207
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800208static void free_pmds(struct mm_struct *mm, pmd_t *pmds[])
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700209{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400210 int i;
211
212 for(i = 0; i < PREALLOCATED_PMDS; i++)
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800213 if (pmds[i]) {
214 pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400215 free_page((unsigned long)pmds[i]);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800216 mm_dec_nr_pmds(mm);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800217 }
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700218}
219
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800220static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[])
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700221{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400222 int i;
223 bool failed = false;
Vladimir Davydov3e79ec72016-07-26 15:24:30 -0700224 gfp_t gfp = PGALLOC_GFP;
225
226 if (mm == &init_mm)
227 gfp &= ~__GFP_ACCOUNT;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400228
229 for(i = 0; i < PREALLOCATED_PMDS; i++) {
Vladimir Davydov3e79ec72016-07-26 15:24:30 -0700230 pmd_t *pmd = (pmd_t *)__get_free_page(gfp);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800231 if (!pmd)
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400232 failed = true;
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800233 if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
Al Viro2a46eed2013-11-20 22:16:36 +0000234 free_page((unsigned long)pmd);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800235 pmd = NULL;
236 failed = true;
237 }
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800238 if (pmd)
239 mm_inc_nr_pmds(mm);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400240 pmds[i] = pmd;
241 }
242
243 if (failed) {
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800244 free_pmds(mm, pmds);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400245 return -ENOMEM;
246 }
247
248 return 0;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700249}
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400250
251/*
252 * Mop up any pmd pages which may still be attached to the pgd.
253 * Normally they will be freed by munmap/exit_mmap, but any pmd we
254 * preallocate which never got a corresponding vma will need to be
255 * freed manually.
256 */
257static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
258{
259 int i;
260
261 for(i = 0; i < PREALLOCATED_PMDS; i++) {
262 pgd_t pgd = pgdp[i];
263
264 if (pgd_val(pgd) != 0) {
265 pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
266
267 pgdp[i] = native_make_pgd(0);
268
269 paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
270 pmd_free(mm, pmd);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800271 mm_dec_nr_pmds(mm);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400272 }
273 }
274}
275
276static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
277{
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300278 p4d_t *p4d;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400279 pud_t *pud;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400280 int i;
281
Jeremy Fitzhardingecf3e5052008-08-08 13:46:07 -0700282 if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
283 return;
284
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300285 p4d = p4d_offset(pgd, 0);
286 pud = pud_offset(p4d, 0);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400287
Wanpeng Li73b44ff2013-07-08 16:00:17 -0700288 for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400289 pmd_t *pmd = pmds[i];
290
291 if (i >= KERNEL_PGD_BOUNDARY)
292 memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
293 sizeof(pmd_t) * PTRS_PER_PMD);
294
295 pud_populate(mm, pud, pmd);
296 }
297}
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700298
Fenghua Yu1db491f2015-01-15 20:30:01 -0800299/*
300 * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also
301 * assumes that pgd should be in one page.
302 *
303 * But kernel with PAE paging that is not running as a Xen domain
304 * only needs to allocate 32 bytes for pgd instead of one page.
305 */
306#ifdef CONFIG_X86_PAE
307
308#include <linux/slab.h>
309
310#define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
311#define PGD_ALIGN 32
312
313static struct kmem_cache *pgd_cache;
314
315static int __init pgd_cache_init(void)
316{
317 /*
318 * When PAE kernel is running as a Xen domain, it does not use
319 * shared kernel pmd. And this requires a whole page for pgd.
320 */
321 if (!SHARED_KERNEL_PMD)
322 return 0;
323
324 /*
325 * when PAE kernel is not running as a Xen domain, it uses
326 * shared kernel pmd. Shared kernel pmd does not require a whole
327 * page for pgd. We are able to just allocate a 32-byte for pgd.
328 * During boot time, we create a 32-byte slab for pgd table allocation.
329 */
330 pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
331 SLAB_PANIC, NULL);
332 if (!pgd_cache)
333 return -ENOMEM;
334
335 return 0;
336}
337core_initcall(pgd_cache_init);
338
339static inline pgd_t *_pgd_alloc(void)
340{
341 /*
342 * If no SHARED_KERNEL_PMD, PAE kernel is running as a Xen domain.
343 * We allocate one page for pgd.
344 */
345 if (!SHARED_KERNEL_PMD)
346 return (pgd_t *)__get_free_page(PGALLOC_GFP);
347
348 /*
349 * Now PAE kernel is not running as a Xen domain. We can allocate
350 * a 32-byte slab for pgd to save memory space.
351 */
352 return kmem_cache_alloc(pgd_cache, PGALLOC_GFP);
353}
354
355static inline void _pgd_free(pgd_t *pgd)
356{
357 if (!SHARED_KERNEL_PMD)
358 free_page((unsigned long)pgd);
359 else
360 kmem_cache_free(pgd_cache, pgd);
361}
362#else
Dave Hansend9e9a642017-12-04 15:07:39 +0100363
Fenghua Yu1db491f2015-01-15 20:30:01 -0800364static inline pgd_t *_pgd_alloc(void)
365{
Dave Hansend9e9a642017-12-04 15:07:39 +0100366 return (pgd_t *)__get_free_pages(PGALLOC_GFP, PGD_ALLOCATION_ORDER);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800367}
368
369static inline void _pgd_free(pgd_t *pgd)
370{
Dave Hansend9e9a642017-12-04 15:07:39 +0100371 free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800372}
373#endif /* CONFIG_X86_PAE */
374
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700375pgd_t *pgd_alloc(struct mm_struct *mm)
376{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400377 pgd_t *pgd;
378 pmd_t *pmds[PREALLOCATED_PMDS];
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700379
Fenghua Yu1db491f2015-01-15 20:30:01 -0800380 pgd = _pgd_alloc();
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400381
382 if (pgd == NULL)
383 goto out;
384
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700385 mm->pgd = pgd;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700386
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800387 if (preallocate_pmds(mm, pmds) != 0)
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400388 goto out_free_pgd;
389
390 if (paravirt_pgd_alloc(mm) != 0)
391 goto out_free_pmds;
392
393 /*
394 * Make sure that pre-populating the pmds is atomic with
395 * respect to anything walking the pgd_list, so that they
396 * never see a partially populated pgd.
397 */
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800398 spin_lock(&pgd_lock);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400399
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700400 pgd_ctor(mm, pgd);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400401 pgd_prepopulate_pmd(mm, pgd, pmds);
402
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800403 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700404
405 return pgd;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400406
407out_free_pmds:
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800408 free_pmds(mm, pmds);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400409out_free_pgd:
Fenghua Yu1db491f2015-01-15 20:30:01 -0800410 _pgd_free(pgd);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400411out:
412 return NULL;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700413}
414
415void pgd_free(struct mm_struct *mm, pgd_t *pgd)
416{
417 pgd_mop_up_pmds(mm, pgd);
418 pgd_dtor(pgd);
Jeremy Fitzhardingeeba00452008-06-25 00:19:12 -0400419 paravirt_pgd_free(mm, pgd);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800420 _pgd_free(pgd);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700421}
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700422
Rik van Riel0f9a9212012-11-06 09:54:47 +0000423/*
424 * Used to set accessed or dirty bits in the page table entries
425 * on other architectures. On x86, the accessed and dirty bits
426 * are tracked by hardware. However, do_wp_page calls this function
427 * to also make the pte writeable at the same time the dirty bit is
428 * set. In that case we do actually need to write the PTE.
429 */
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700430int ptep_set_access_flags(struct vm_area_struct *vma,
431 unsigned long address, pte_t *ptep,
432 pte_t entry, int dirty)
433{
434 int changed = !pte_same(*ptep, entry);
435
Juergen Gross87930012017-09-04 12:25:27 +0200436 if (changed && dirty)
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700437 *ptep = entry;
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700438
439 return changed;
440}
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700441
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800442#ifdef CONFIG_TRANSPARENT_HUGEPAGE
443int pmdp_set_access_flags(struct vm_area_struct *vma,
444 unsigned long address, pmd_t *pmdp,
445 pmd_t entry, int dirty)
446{
447 int changed = !pmd_same(*pmdp, entry);
448
449 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
450
451 if (changed && dirty) {
452 *pmdp = entry;
Ingo Molnar5e4bf1a2012-11-20 13:02:51 +0100453 /*
454 * We had a write-protection fault here and changed the pmd
455 * to to more permissive. No need to flush the TLB for that,
456 * #PF is architecturally guaranteed to do that and in the
457 * worst-case we'll generate a spurious fault.
458 */
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800459 }
460
461 return changed;
462}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800463
464int pudp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
465 pud_t *pudp, pud_t entry, int dirty)
466{
467 int changed = !pud_same(*pudp, entry);
468
469 VM_BUG_ON(address & ~HPAGE_PUD_MASK);
470
471 if (changed && dirty) {
472 *pudp = entry;
473 /*
474 * We had a write-protection fault here and changed the pud
475 * to to more permissive. No need to flush the TLB for that,
476 * #PF is architecturally guaranteed to do that and in the
477 * worst-case we'll generate a spurious fault.
478 */
479 }
480
481 return changed;
482}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800483#endif
484
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700485int ptep_test_and_clear_young(struct vm_area_struct *vma,
486 unsigned long addr, pte_t *ptep)
487{
488 int ret = 0;
489
490 if (pte_young(*ptep))
491 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
Thomas Gleixner48e23952008-05-24 17:24:34 +0200492 (unsigned long *) &ptep->pte);
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700493
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700494 return ret;
495}
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700496
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800497#ifdef CONFIG_TRANSPARENT_HUGEPAGE
498int pmdp_test_and_clear_young(struct vm_area_struct *vma,
499 unsigned long addr, pmd_t *pmdp)
500{
501 int ret = 0;
502
503 if (pmd_young(*pmdp))
504 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800505 (unsigned long *)pmdp);
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800506
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800507 return ret;
508}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800509int pudp_test_and_clear_young(struct vm_area_struct *vma,
510 unsigned long addr, pud_t *pudp)
511{
512 int ret = 0;
513
514 if (pud_young(*pudp))
515 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
516 (unsigned long *)pudp);
517
518 return ret;
519}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800520#endif
521
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700522int ptep_clear_flush_young(struct vm_area_struct *vma,
523 unsigned long address, pte_t *ptep)
524{
Shaohua Lib13b1d22014-04-08 15:58:09 +0800525 /*
526 * On x86 CPUs, clearing the accessed bit without a TLB flush
527 * doesn't cause data corruption. [ It could cause incorrect
528 * page aging and the (mistaken) reclaim of hot pages, but the
529 * chance of that should be relatively low. ]
530 *
531 * So as a performance optimization don't flush the TLB when
532 * clearing the accessed bit, it will eventually be flushed by
533 * a context switch or a VM operation anyway. [ In the rare
534 * event of it not getting flushed for a long time the delay
535 * shouldn't really matter because there's no real memory
536 * pressure for swapout to react to. ]
537 */
538 return ptep_test_and_clear_young(vma, address, ptep);
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700539}
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700540
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800541#ifdef CONFIG_TRANSPARENT_HUGEPAGE
542int pmdp_clear_flush_young(struct vm_area_struct *vma,
543 unsigned long address, pmd_t *pmdp)
544{
545 int young;
546
547 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
548
549 young = pmdp_test_and_clear_young(vma, address, pmdp);
550 if (young)
551 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
552
553 return young;
554}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800555#endif
556
Gustavo F. Padovanfd862dd2009-02-15 21:48:54 -0300557/**
558 * reserve_top_address - reserves a hole in the top of kernel address space
559 * @reserve - size of hole to reserve
560 *
561 * Can be used to relocate the fixmap area and poke a hole in the top
562 * of kernel address space to make room for a hypervisor.
563 */
564void __init reserve_top_address(unsigned long reserve)
565{
566#ifdef CONFIG_X86_32
567 BUG_ON(fixmaps_set > 0);
Andy Lutomirski73159fd2014-05-05 12:19:31 -0700568 __FIXADDR_TOP = round_down(-reserve, 1 << PMD_SHIFT) - PAGE_SIZE;
569 printk(KERN_INFO "Reserving virtual address space above 0x%08lx (rounded to 0x%08lx)\n",
570 -reserve, __FIXADDR_TOP + PAGE_SIZE);
Gustavo F. Padovanfd862dd2009-02-15 21:48:54 -0300571#endif
572}
573
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700574int fixmaps_set;
575
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700576void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700577{
578 unsigned long address = __fix_to_virt(idx);
579
580 if (idx >= __end_of_fixed_addresses) {
581 BUG();
582 return;
583 }
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700584 set_pte_vaddr(address, pte);
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700585 fixmaps_set++;
586}
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700587
Masami Hiramatsu3b3809a2009-04-09 10:55:33 -0700588void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
589 pgprot_t flags)
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700590{
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700591 /* Sanitize 'prot' against any unsupported bits: */
592 pgprot_val(flags) &= __default_kernel_pte_mask;
593
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700594 __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
595}
Toshi Kani6b637832015-04-14 15:47:32 -0700596
597#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Kirill A. Shutemovb8504052017-03-30 11:07:29 +0300598#ifdef CONFIG_X86_5LEVEL
599/**
600 * p4d_set_huge - setup kernel P4D mapping
601 *
602 * No 512GB pages yet -- always return 0
603 */
604int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
605{
606 return 0;
607}
608
609/**
610 * p4d_clear_huge - clear kernel P4D mapping when it is set
611 *
612 * No 512GB pages yet -- always return 0
613 */
614int p4d_clear_huge(p4d_t *p4d)
615{
616 return 0;
617}
618#endif
619
Toshi Kani3d3ca412015-05-26 10:28:07 +0200620/**
621 * pud_set_huge - setup kernel PUD mapping
622 *
Toshi Kanib73522e2015-05-26 10:28:10 +0200623 * MTRRs can override PAT memory types with 4KiB granularity. Therefore, this
624 * function sets up a huge page only if any of the following conditions are met:
625 *
626 * - MTRRs are disabled, or
627 *
628 * - MTRRs are enabled and the range is completely covered by a single MTRR, or
629 *
630 * - MTRRs are enabled and the corresponding MTRR memory type is WB, which
631 * has no effect on the requested PAT memory type.
632 *
633 * Callers should try to decrease page size (1GB -> 2MB -> 4K) if the bigger
634 * page mapping attempt fails.
Toshi Kani3d3ca412015-05-26 10:28:07 +0200635 *
636 * Returns 1 on success and 0 on failure.
637 */
Toshi Kani6b637832015-04-14 15:47:32 -0700638int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
639{
Toshi Kanib73522e2015-05-26 10:28:10 +0200640 u8 mtrr, uniform;
Toshi Kani6b637832015-04-14 15:47:32 -0700641
Toshi Kanib73522e2015-05-26 10:28:10 +0200642 mtrr = mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform);
643 if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
644 (mtrr != MTRR_TYPE_WRBACK))
Toshi Kani6b637832015-04-14 15:47:32 -0700645 return 0;
646
Joerg Roedele3e28812018-04-11 17:24:38 +0200647 /* Bail out if we are we on a populated non-leaf entry: */
648 if (pud_present(*pud) && !pud_huge(*pud))
649 return 0;
650
Toshi Kani6b637832015-04-14 15:47:32 -0700651 prot = pgprot_4k_2_large(prot);
652
653 set_pte((pte_t *)pud, pfn_pte(
654 (u64)addr >> PAGE_SHIFT,
655 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
656
657 return 1;
658}
659
Toshi Kani3d3ca412015-05-26 10:28:07 +0200660/**
661 * pmd_set_huge - setup kernel PMD mapping
662 *
Toshi Kanib73522e2015-05-26 10:28:10 +0200663 * See text over pud_set_huge() above.
Toshi Kani3d3ca412015-05-26 10:28:07 +0200664 *
665 * Returns 1 on success and 0 on failure.
666 */
Toshi Kani6b637832015-04-14 15:47:32 -0700667int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
668{
Toshi Kanib73522e2015-05-26 10:28:10 +0200669 u8 mtrr, uniform;
Toshi Kani6b637832015-04-14 15:47:32 -0700670
Toshi Kanib73522e2015-05-26 10:28:10 +0200671 mtrr = mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform);
672 if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
673 (mtrr != MTRR_TYPE_WRBACK)) {
674 pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-page mapping due to MTRR override.\n",
675 __func__, addr, addr + PMD_SIZE);
Toshi Kani6b637832015-04-14 15:47:32 -0700676 return 0;
Toshi Kanib73522e2015-05-26 10:28:10 +0200677 }
Toshi Kani6b637832015-04-14 15:47:32 -0700678
Joerg Roedele3e28812018-04-11 17:24:38 +0200679 /* Bail out if we are we on a populated non-leaf entry: */
680 if (pmd_present(*pmd) && !pmd_huge(*pmd))
681 return 0;
682
Toshi Kani6b637832015-04-14 15:47:32 -0700683 prot = pgprot_4k_2_large(prot);
684
685 set_pte((pte_t *)pmd, pfn_pte(
686 (u64)addr >> PAGE_SHIFT,
687 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
688
689 return 1;
690}
691
Toshi Kani3d3ca412015-05-26 10:28:07 +0200692/**
693 * pud_clear_huge - clear kernel PUD mapping when it is set
694 *
695 * Returns 1 on success and 0 on failure (no PUD map is found).
696 */
Toshi Kani6b637832015-04-14 15:47:32 -0700697int pud_clear_huge(pud_t *pud)
698{
699 if (pud_large(*pud)) {
700 pud_clear(pud);
701 return 1;
702 }
703
704 return 0;
705}
706
Toshi Kani3d3ca412015-05-26 10:28:07 +0200707/**
708 * pmd_clear_huge - clear kernel PMD mapping when it is set
709 *
710 * Returns 1 on success and 0 on failure (no PMD map is found).
711 */
Toshi Kani6b637832015-04-14 15:47:32 -0700712int pmd_clear_huge(pmd_t *pmd)
713{
714 if (pmd_large(*pmd)) {
715 pmd_clear(pmd);
716 return 1;
717 }
718
719 return 0;
720}
Toshi Kanib6bdb752018-03-22 16:17:20 -0700721
Toshi Kanif967db02018-06-27 08:13:46 -0600722#ifdef CONFIG_X86_64
Toshi Kanib6bdb752018-03-22 16:17:20 -0700723/**
724 * pud_free_pmd_page - Clear pud entry and free pmd page.
725 * @pud: Pointer to a PUD.
Chintan Pandya785a19f2018-06-27 08:13:47 -0600726 * @addr: Virtual address associated with pud.
Toshi Kanib6bdb752018-03-22 16:17:20 -0700727 *
728 * Context: The pud range has been unmaped and TLB purged.
729 * Return: 1 if clearing the entry succeeded. 0 otherwise.
730 */
Chintan Pandya785a19f2018-06-27 08:13:47 -0600731int pud_free_pmd_page(pud_t *pud, unsigned long addr)
Toshi Kanib6bdb752018-03-22 16:17:20 -0700732{
Toshi Kani28ee90f2018-03-22 16:17:24 -0700733 pmd_t *pmd;
734 int i;
735
736 if (pud_none(*pud))
737 return 1;
738
739 pmd = (pmd_t *)pud_page_vaddr(*pud);
740
741 for (i = 0; i < PTRS_PER_PMD; i++)
Chintan Pandya785a19f2018-06-27 08:13:47 -0600742 if (!pmd_free_pte_page(&pmd[i], addr + (i * PMD_SIZE)))
Toshi Kani28ee90f2018-03-22 16:17:24 -0700743 return 0;
744
745 pud_clear(pud);
746 free_page((unsigned long)pmd);
747
748 return 1;
Toshi Kanib6bdb752018-03-22 16:17:20 -0700749}
750
751/**
752 * pmd_free_pte_page - Clear pmd entry and free pte page.
753 * @pmd: Pointer to a PMD.
Chintan Pandya785a19f2018-06-27 08:13:47 -0600754 * @addr: Virtual address associated with pmd.
Toshi Kanib6bdb752018-03-22 16:17:20 -0700755 *
756 * Context: The pmd range has been unmaped and TLB purged.
757 * Return: 1 if clearing the entry succeeded. 0 otherwise.
758 */
Chintan Pandya785a19f2018-06-27 08:13:47 -0600759int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
Toshi Kanib6bdb752018-03-22 16:17:20 -0700760{
Toshi Kani28ee90f2018-03-22 16:17:24 -0700761 pte_t *pte;
762
763 if (pmd_none(*pmd))
764 return 1;
765
766 pte = (pte_t *)pmd_page_vaddr(*pmd);
767 pmd_clear(pmd);
768 free_page((unsigned long)pte);
769
770 return 1;
Toshi Kanib6bdb752018-03-22 16:17:20 -0700771}
Toshi Kanif967db02018-06-27 08:13:46 -0600772
773#else /* !CONFIG_X86_64 */
774
Chintan Pandya785a19f2018-06-27 08:13:47 -0600775int pud_free_pmd_page(pud_t *pud, unsigned long addr)
Toshi Kanif967db02018-06-27 08:13:46 -0600776{
777 return pud_none(*pud);
778}
779
780/*
781 * Disable free page handling on x86-PAE. This assures that ioremap()
782 * does not update sync'd pmd entries. See vmalloc_sync_one().
783 */
Chintan Pandya785a19f2018-06-27 08:13:47 -0600784int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
Toshi Kanif967db02018-06-27 08:13:46 -0600785{
786 return pmd_none(*pmd);
787}
788
789#endif /* CONFIG_X86_64 */
Toshi Kani6b637832015-04-14 15:47:32 -0700790#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */