blob: 3ca59cf7a7f9406090e68366e8944c85ed506ff5 [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{
122 BUILD_BUG_ON(sizeof(virt_to_page(pgd)->index) < sizeof(mm));
123 virt_to_page(pgd)->index = (pgoff_t)mm;
124}
125
126struct mm_struct *pgd_page_get_mm(struct page *page)
127{
128 return (struct mm_struct *)page->index;
129}
130
131static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700132{
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700133 /* If the pgd points to a shared pagetable level (either the
134 ptes in non-PAE, or shared PMD in PAE), then just copy the
135 references from swapper_pg_dir. */
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700136 if (CONFIG_PGTABLE_LEVELS == 2 ||
137 (CONFIG_PGTABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
Kirill A. Shutemovb8504052017-03-30 11:07:29 +0300138 CONFIG_PGTABLE_LEVELS >= 4) {
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700139 clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
140 swapper_pg_dir + KERNEL_PGD_BOUNDARY,
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700141 KERNEL_PGD_PTRS);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700142 }
143
144 /* list required to sync kernel mapping updates */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700145 if (!SHARED_KERNEL_PMD) {
146 pgd_set_mm(pgd, mm);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700147 pgd_list_add(pgd);
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700148 }
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700149}
150
Jan Beulich17b74622008-08-29 12:51:32 +0100151static void pgd_dtor(pgd_t *pgd)
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700152{
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700153 if (SHARED_KERNEL_PMD)
154 return;
155
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800156 spin_lock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700157 pgd_list_del(pgd);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800158 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700159}
160
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -0700161/*
162 * List of all pgd's needed for non-PAE so it can invalidate entries
163 * in both cached and uncached pgd's; not needed for PAE since the
164 * kernel pmd is shared. If PAE were not to share the pmd a similar
165 * tactic would be needed. This is essentially codepath-based locking
166 * against pageattr.c; it is the unique case in which a valid change
167 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
168 * vmalloc faults work because attached pagetables are never freed.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100169 * -- nyc
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -0700170 */
171
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700172#ifdef CONFIG_X86_PAE
173/*
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700174 * In PAE mode, we need to do a cr3 reload (=tlb flush) when
175 * updating the top-level pagetable entries to guarantee the
176 * processor notices the update. Since this is expensive, and
177 * all 4 top-level entries are used almost immediately in a
178 * new process's life, we just pre-populate them here.
179 *
180 * Also, if we're in a paravirt environment where the kernel pmd is
181 * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
182 * and initialize the kernel pmds here.
183 */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400184#define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100185
186void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
187{
Jeremy Fitzhardinge6944a9c2008-03-17 16:37:01 -0700188 paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100189
190 /* Note: almost everything apart from _PAGE_PRESENT is
191 reserved at the pmd (PDPT) level. */
192 set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
193
194 /*
195 * According to Intel App note "TLBs, Paging-Structure Caches,
196 * and Their Invalidation", April 2007, document 317080-001,
197 * section 8.1: in PAE mode we explicitly have to flush the
198 * TLB via cr3 if the top-level pgd is changed...
199 */
Shaohua Li4981d012011-03-16 11:37:29 +0800200 flush_tlb_mm(mm);
Ingo Molnar1ec1fe72008-03-19 20:30:40 +0100201}
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700202#else /* !CONFIG_X86_PAE */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400203
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700204/* No need to prepopulate any pagetable entries in non-PAE modes. */
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400205#define PREALLOCATED_PMDS 0
206
207#endif /* CONFIG_X86_PAE */
208
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800209static void free_pmds(struct mm_struct *mm, pmd_t *pmds[])
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700210{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400211 int i;
212
213 for(i = 0; i < PREALLOCATED_PMDS; i++)
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800214 if (pmds[i]) {
215 pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400216 free_page((unsigned long)pmds[i]);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800217 mm_dec_nr_pmds(mm);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800218 }
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700219}
220
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800221static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[])
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700222{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400223 int i;
224 bool failed = false;
Vladimir Davydov3e79ec72016-07-26 15:24:30 -0700225 gfp_t gfp = PGALLOC_GFP;
226
227 if (mm == &init_mm)
228 gfp &= ~__GFP_ACCOUNT;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400229
230 for(i = 0; i < PREALLOCATED_PMDS; i++) {
Vladimir Davydov3e79ec72016-07-26 15:24:30 -0700231 pmd_t *pmd = (pmd_t *)__get_free_page(gfp);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800232 if (!pmd)
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400233 failed = true;
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800234 if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
Al Viro2a46eed2013-11-20 22:16:36 +0000235 free_page((unsigned long)pmd);
Kirill A. Shutemov09ef4932013-11-14 14:31:13 -0800236 pmd = NULL;
237 failed = true;
238 }
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800239 if (pmd)
240 mm_inc_nr_pmds(mm);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400241 pmds[i] = pmd;
242 }
243
244 if (failed) {
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800245 free_pmds(mm, pmds);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400246 return -ENOMEM;
247 }
248
249 return 0;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700250}
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400251
252/*
253 * Mop up any pmd pages which may still be attached to the pgd.
254 * Normally they will be freed by munmap/exit_mmap, but any pmd we
255 * preallocate which never got a corresponding vma will need to be
256 * freed manually.
257 */
258static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
259{
260 int i;
261
262 for(i = 0; i < PREALLOCATED_PMDS; i++) {
263 pgd_t pgd = pgdp[i];
264
265 if (pgd_val(pgd) != 0) {
266 pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
267
268 pgdp[i] = native_make_pgd(0);
269
270 paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
271 pmd_free(mm, pmd);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800272 mm_dec_nr_pmds(mm);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400273 }
274 }
275}
276
277static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
278{
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300279 p4d_t *p4d;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400280 pud_t *pud;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400281 int i;
282
Jeremy Fitzhardingecf3e5052008-08-08 13:46:07 -0700283 if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
284 return;
285
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300286 p4d = p4d_offset(pgd, 0);
287 pud = pud_offset(p4d, 0);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400288
Wanpeng Li73b44ff2013-07-08 16:00:17 -0700289 for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400290 pmd_t *pmd = pmds[i];
291
292 if (i >= KERNEL_PGD_BOUNDARY)
293 memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
294 sizeof(pmd_t) * PTRS_PER_PMD);
295
296 pud_populate(mm, pud, pmd);
297 }
298}
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700299
Fenghua Yu1db491f2015-01-15 20:30:01 -0800300/*
301 * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also
302 * assumes that pgd should be in one page.
303 *
304 * But kernel with PAE paging that is not running as a Xen domain
305 * only needs to allocate 32 bytes for pgd instead of one page.
306 */
307#ifdef CONFIG_X86_PAE
308
309#include <linux/slab.h>
310
311#define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
312#define PGD_ALIGN 32
313
314static struct kmem_cache *pgd_cache;
315
316static int __init pgd_cache_init(void)
317{
318 /*
319 * When PAE kernel is running as a Xen domain, it does not use
320 * shared kernel pmd. And this requires a whole page for pgd.
321 */
322 if (!SHARED_KERNEL_PMD)
323 return 0;
324
325 /*
326 * when PAE kernel is not running as a Xen domain, it uses
327 * shared kernel pmd. Shared kernel pmd does not require a whole
328 * page for pgd. We are able to just allocate a 32-byte for pgd.
329 * During boot time, we create a 32-byte slab for pgd table allocation.
330 */
331 pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
332 SLAB_PANIC, NULL);
333 if (!pgd_cache)
334 return -ENOMEM;
335
336 return 0;
337}
338core_initcall(pgd_cache_init);
339
340static inline pgd_t *_pgd_alloc(void)
341{
342 /*
343 * If no SHARED_KERNEL_PMD, PAE kernel is running as a Xen domain.
344 * We allocate one page for pgd.
345 */
346 if (!SHARED_KERNEL_PMD)
347 return (pgd_t *)__get_free_page(PGALLOC_GFP);
348
349 /*
350 * Now PAE kernel is not running as a Xen domain. We can allocate
351 * a 32-byte slab for pgd to save memory space.
352 */
353 return kmem_cache_alloc(pgd_cache, PGALLOC_GFP);
354}
355
356static inline void _pgd_free(pgd_t *pgd)
357{
358 if (!SHARED_KERNEL_PMD)
359 free_page((unsigned long)pgd);
360 else
361 kmem_cache_free(pgd_cache, pgd);
362}
363#else
Dave Hansend9e9a642017-12-04 15:07:39 +0100364
Fenghua Yu1db491f2015-01-15 20:30:01 -0800365static inline pgd_t *_pgd_alloc(void)
366{
Dave Hansend9e9a642017-12-04 15:07:39 +0100367 return (pgd_t *)__get_free_pages(PGALLOC_GFP, PGD_ALLOCATION_ORDER);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800368}
369
370static inline void _pgd_free(pgd_t *pgd)
371{
Dave Hansend9e9a642017-12-04 15:07:39 +0100372 free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800373}
374#endif /* CONFIG_X86_PAE */
375
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700376pgd_t *pgd_alloc(struct mm_struct *mm)
377{
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400378 pgd_t *pgd;
379 pmd_t *pmds[PREALLOCATED_PMDS];
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700380
Fenghua Yu1db491f2015-01-15 20:30:01 -0800381 pgd = _pgd_alloc();
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400382
383 if (pgd == NULL)
384 goto out;
385
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700386 mm->pgd = pgd;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700387
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800388 if (preallocate_pmds(mm, pmds) != 0)
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400389 goto out_free_pgd;
390
391 if (paravirt_pgd_alloc(mm) != 0)
392 goto out_free_pmds;
393
394 /*
395 * Make sure that pre-populating the pmds is atomic with
396 * respect to anything walking the pgd_list, so that they
397 * never see a partially populated pgd.
398 */
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800399 spin_lock(&pgd_lock);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400400
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700401 pgd_ctor(mm, pgd);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400402 pgd_prepopulate_pmd(mm, pgd, pmds);
403
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800404 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700405
406 return pgd;
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400407
408out_free_pmds:
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -0800409 free_pmds(mm, pmds);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400410out_free_pgd:
Fenghua Yu1db491f2015-01-15 20:30:01 -0800411 _pgd_free(pgd);
Jeremy Fitzhardinged8d59002008-06-25 00:19:13 -0400412out:
413 return NULL;
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700414}
415
416void pgd_free(struct mm_struct *mm, pgd_t *pgd)
417{
418 pgd_mop_up_pmds(mm, pgd);
419 pgd_dtor(pgd);
Jeremy Fitzhardingeeba00452008-06-25 00:19:12 -0400420 paravirt_pgd_free(mm, pgd);
Fenghua Yu1db491f2015-01-15 20:30:01 -0800421 _pgd_free(pgd);
Jeremy Fitzhardinge4f76cd32008-03-17 16:36:55 -0700422}
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700423
Rik van Riel0f9a9212012-11-06 09:54:47 +0000424/*
425 * Used to set accessed or dirty bits in the page table entries
426 * on other architectures. On x86, the accessed and dirty bits
427 * are tracked by hardware. However, do_wp_page calls this function
428 * to also make the pte writeable at the same time the dirty bit is
429 * set. In that case we do actually need to write the PTE.
430 */
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700431int ptep_set_access_flags(struct vm_area_struct *vma,
432 unsigned long address, pte_t *ptep,
433 pte_t entry, int dirty)
434{
435 int changed = !pte_same(*ptep, entry);
436
Juergen Gross87930012017-09-04 12:25:27 +0200437 if (changed && dirty)
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700438 *ptep = entry;
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700439
440 return changed;
441}
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700442
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800443#ifdef CONFIG_TRANSPARENT_HUGEPAGE
444int pmdp_set_access_flags(struct vm_area_struct *vma,
445 unsigned long address, pmd_t *pmdp,
446 pmd_t entry, int dirty)
447{
448 int changed = !pmd_same(*pmdp, entry);
449
450 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
451
452 if (changed && dirty) {
453 *pmdp = entry;
Ingo Molnar5e4bf1a2012-11-20 13:02:51 +0100454 /*
455 * We had a write-protection fault here and changed the pmd
456 * to to more permissive. No need to flush the TLB for that,
457 * #PF is architecturally guaranteed to do that and in the
458 * worst-case we'll generate a spurious fault.
459 */
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800460 }
461
462 return changed;
463}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800464
465int pudp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
466 pud_t *pudp, pud_t entry, int dirty)
467{
468 int changed = !pud_same(*pudp, entry);
469
470 VM_BUG_ON(address & ~HPAGE_PUD_MASK);
471
472 if (changed && dirty) {
473 *pudp = entry;
474 /*
475 * We had a write-protection fault here and changed the pud
476 * to to more permissive. No need to flush the TLB for that,
477 * #PF is architecturally guaranteed to do that and in the
478 * worst-case we'll generate a spurious fault.
479 */
480 }
481
482 return changed;
483}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800484#endif
485
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700486int ptep_test_and_clear_young(struct vm_area_struct *vma,
487 unsigned long addr, pte_t *ptep)
488{
489 int ret = 0;
490
491 if (pte_young(*ptep))
492 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
Thomas Gleixner48e23952008-05-24 17:24:34 +0200493 (unsigned long *) &ptep->pte);
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700494
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -0700495 return ret;
496}
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700497
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800498#ifdef CONFIG_TRANSPARENT_HUGEPAGE
499int pmdp_test_and_clear_young(struct vm_area_struct *vma,
500 unsigned long addr, pmd_t *pmdp)
501{
502 int ret = 0;
503
504 if (pmd_young(*pmdp))
505 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800506 (unsigned long *)pmdp);
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800507
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800508 return ret;
509}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800510int pudp_test_and_clear_young(struct vm_area_struct *vma,
511 unsigned long addr, pud_t *pudp)
512{
513 int ret = 0;
514
515 if (pud_young(*pudp))
516 ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
517 (unsigned long *)pudp);
518
519 return ret;
520}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800521#endif
522
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700523int ptep_clear_flush_young(struct vm_area_struct *vma,
524 unsigned long address, pte_t *ptep)
525{
Shaohua Lib13b1d22014-04-08 15:58:09 +0800526 /*
527 * On x86 CPUs, clearing the accessed bit without a TLB flush
528 * doesn't cause data corruption. [ It could cause incorrect
529 * page aging and the (mistaken) reclaim of hot pages, but the
530 * chance of that should be relatively low. ]
531 *
532 * So as a performance optimization don't flush the TLB when
533 * clearing the accessed bit, it will eventually be flushed by
534 * a context switch or a VM operation anyway. [ In the rare
535 * event of it not getting flushed for a long time the delay
536 * shouldn't really matter because there's no real memory
537 * pressure for swapout to react to. ]
538 */
539 return ptep_test_and_clear_young(vma, address, ptep);
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -0700540}
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700541
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800542#ifdef CONFIG_TRANSPARENT_HUGEPAGE
543int pmdp_clear_flush_young(struct vm_area_struct *vma,
544 unsigned long address, pmd_t *pmdp)
545{
546 int young;
547
548 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
549
550 young = pmdp_test_and_clear_young(vma, address, pmdp);
551 if (young)
552 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
553
554 return young;
555}
Andrea Arcangelidb3eb96f2011-01-13 15:46:41 -0800556#endif
557
Gustavo F. Padovanfd862dd2009-02-15 21:48:54 -0300558/**
559 * reserve_top_address - reserves a hole in the top of kernel address space
560 * @reserve - size of hole to reserve
561 *
562 * Can be used to relocate the fixmap area and poke a hole in the top
563 * of kernel address space to make room for a hypervisor.
564 */
565void __init reserve_top_address(unsigned long reserve)
566{
567#ifdef CONFIG_X86_32
568 BUG_ON(fixmaps_set > 0);
Andy Lutomirski73159fd2014-05-05 12:19:31 -0700569 __FIXADDR_TOP = round_down(-reserve, 1 << PMD_SHIFT) - PAGE_SIZE;
570 printk(KERN_INFO "Reserving virtual address space above 0x%08lx (rounded to 0x%08lx)\n",
571 -reserve, __FIXADDR_TOP + PAGE_SIZE);
Gustavo F. Padovanfd862dd2009-02-15 21:48:54 -0300572#endif
573}
574
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700575int fixmaps_set;
576
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700577void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700578{
579 unsigned long address = __fix_to_virt(idx);
580
581 if (idx >= __end_of_fixed_addresses) {
582 BUG();
583 return;
584 }
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700585 set_pte_vaddr(address, pte);
Jeremy Fitzhardinge7c7e6e02008-06-17 11:41:54 -0700586 fixmaps_set++;
587}
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700588
Masami Hiramatsu3b3809a2009-04-09 10:55:33 -0700589void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
590 pgprot_t flags)
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700591{
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700592 /* Sanitize 'prot' against any unsupported bits: */
593 pgprot_val(flags) &= __default_kernel_pte_mask;
594
Jeremy Fitzhardingeaeaaa592008-06-17 11:42:01 -0700595 __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
596}
Toshi Kani6b637832015-04-14 15:47:32 -0700597
598#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
Kirill A. Shutemovb8504052017-03-30 11:07:29 +0300599#ifdef CONFIG_X86_5LEVEL
600/**
601 * p4d_set_huge - setup kernel P4D mapping
602 *
603 * No 512GB pages yet -- always return 0
604 */
605int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
606{
607 return 0;
608}
609
610/**
611 * p4d_clear_huge - clear kernel P4D mapping when it is set
612 *
613 * No 512GB pages yet -- always return 0
614 */
615int p4d_clear_huge(p4d_t *p4d)
616{
617 return 0;
618}
619#endif
620
Toshi Kani3d3ca412015-05-26 10:28:07 +0200621/**
622 * pud_set_huge - setup kernel PUD mapping
623 *
Toshi Kanib73522e2015-05-26 10:28:10 +0200624 * MTRRs can override PAT memory types with 4KiB granularity. Therefore, this
625 * function sets up a huge page only if any of the following conditions are met:
626 *
627 * - MTRRs are disabled, or
628 *
629 * - MTRRs are enabled and the range is completely covered by a single MTRR, or
630 *
631 * - MTRRs are enabled and the corresponding MTRR memory type is WB, which
632 * has no effect on the requested PAT memory type.
633 *
634 * Callers should try to decrease page size (1GB -> 2MB -> 4K) if the bigger
635 * page mapping attempt fails.
Toshi Kani3d3ca412015-05-26 10:28:07 +0200636 *
637 * Returns 1 on success and 0 on failure.
638 */
Toshi Kani6b637832015-04-14 15:47:32 -0700639int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
640{
Toshi Kanib73522e2015-05-26 10:28:10 +0200641 u8 mtrr, uniform;
Toshi Kani6b637832015-04-14 15:47:32 -0700642
Toshi Kanib73522e2015-05-26 10:28:10 +0200643 mtrr = mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform);
644 if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
645 (mtrr != MTRR_TYPE_WRBACK))
Toshi Kani6b637832015-04-14 15:47:32 -0700646 return 0;
647
Joerg Roedele3e28812018-04-11 17:24:38 +0200648 /* Bail out if we are we on a populated non-leaf entry: */
649 if (pud_present(*pud) && !pud_huge(*pud))
650 return 0;
651
Toshi Kani6b637832015-04-14 15:47:32 -0700652 prot = pgprot_4k_2_large(prot);
653
654 set_pte((pte_t *)pud, pfn_pte(
655 (u64)addr >> PAGE_SHIFT,
656 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
657
658 return 1;
659}
660
Toshi Kani3d3ca412015-05-26 10:28:07 +0200661/**
662 * pmd_set_huge - setup kernel PMD mapping
663 *
Toshi Kanib73522e2015-05-26 10:28:10 +0200664 * See text over pud_set_huge() above.
Toshi Kani3d3ca412015-05-26 10:28:07 +0200665 *
666 * Returns 1 on success and 0 on failure.
667 */
Toshi Kani6b637832015-04-14 15:47:32 -0700668int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
669{
Toshi Kanib73522e2015-05-26 10:28:10 +0200670 u8 mtrr, uniform;
Toshi Kani6b637832015-04-14 15:47:32 -0700671
Toshi Kanib73522e2015-05-26 10:28:10 +0200672 mtrr = mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform);
673 if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
674 (mtrr != MTRR_TYPE_WRBACK)) {
675 pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-page mapping due to MTRR override.\n",
676 __func__, addr, addr + PMD_SIZE);
Toshi Kani6b637832015-04-14 15:47:32 -0700677 return 0;
Toshi Kanib73522e2015-05-26 10:28:10 +0200678 }
Toshi Kani6b637832015-04-14 15:47:32 -0700679
Joerg Roedele3e28812018-04-11 17:24:38 +0200680 /* Bail out if we are we on a populated non-leaf entry: */
681 if (pmd_present(*pmd) && !pmd_huge(*pmd))
682 return 0;
683
Toshi Kani6b637832015-04-14 15:47:32 -0700684 prot = pgprot_4k_2_large(prot);
685
686 set_pte((pte_t *)pmd, pfn_pte(
687 (u64)addr >> PAGE_SHIFT,
688 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
689
690 return 1;
691}
692
Toshi Kani3d3ca412015-05-26 10:28:07 +0200693/**
694 * pud_clear_huge - clear kernel PUD mapping when it is set
695 *
696 * Returns 1 on success and 0 on failure (no PUD map is found).
697 */
Toshi Kani6b637832015-04-14 15:47:32 -0700698int pud_clear_huge(pud_t *pud)
699{
700 if (pud_large(*pud)) {
701 pud_clear(pud);
702 return 1;
703 }
704
705 return 0;
706}
707
Toshi Kani3d3ca412015-05-26 10:28:07 +0200708/**
709 * pmd_clear_huge - clear kernel PMD mapping when it is set
710 *
711 * Returns 1 on success and 0 on failure (no PMD map is found).
712 */
Toshi Kani6b637832015-04-14 15:47:32 -0700713int pmd_clear_huge(pmd_t *pmd)
714{
715 if (pmd_large(*pmd)) {
716 pmd_clear(pmd);
717 return 1;
718 }
719
720 return 0;
721}
Toshi Kanib6bdb752018-03-22 16:17:20 -0700722
723/**
724 * pud_free_pmd_page - Clear pud entry and free pmd page.
725 * @pud: Pointer to a PUD.
726 *
727 * Context: The pud range has been unmaped and TLB purged.
728 * Return: 1 if clearing the entry succeeded. 0 otherwise.
729 */
730int pud_free_pmd_page(pud_t *pud)
731{
Toshi Kani28ee90f2018-03-22 16:17:24 -0700732 pmd_t *pmd;
733 int i;
734
735 if (pud_none(*pud))
736 return 1;
737
738 pmd = (pmd_t *)pud_page_vaddr(*pud);
739
740 for (i = 0; i < PTRS_PER_PMD; i++)
741 if (!pmd_free_pte_page(&pmd[i]))
742 return 0;
743
744 pud_clear(pud);
745 free_page((unsigned long)pmd);
746
747 return 1;
Toshi Kanib6bdb752018-03-22 16:17:20 -0700748}
749
750/**
751 * pmd_free_pte_page - Clear pmd entry and free pte page.
752 * @pmd: Pointer to a PMD.
753 *
754 * Context: The pmd range has been unmaped and TLB purged.
755 * Return: 1 if clearing the entry succeeded. 0 otherwise.
756 */
757int pmd_free_pte_page(pmd_t *pmd)
758{
Toshi Kani28ee90f2018-03-22 16:17:24 -0700759 pte_t *pte;
760
761 if (pmd_none(*pmd))
762 return 1;
763
764 pte = (pte_t *)pmd_page_vaddr(*pmd);
765 pmd_clear(pmd);
766 free_page((unsigned long)pte);
767
768 return 1;
Toshi Kanib6bdb752018-03-22 16:17:20 -0700769}
Toshi Kani6b637832015-04-14 15:47:32 -0700770#endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */