GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / arm64 / mm / hugetlbpage.c
1 /*
2  * arch/arm64/mm/hugetlbpage.c
3  *
4  * Copyright (C) 2013 Linaro Ltd.
5  *
6  * Based on arch/x86/mm/hugetlbpage.c.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include <linux/hugetlb.h>
22 #include <linux/pagemap.h>
23 #include <linux/err.h>
24 #include <linux/sysctl.h>
25 #include <asm/mman.h>
26 #include <asm/tlb.h>
27 #include <asm/tlbflush.h>
28 #include <asm/pgalloc.h>
29
30 int pmd_huge(pmd_t pmd)
31 {
32         return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
33 }
34
35 int pud_huge(pud_t pud)
36 {
37 #ifndef __PAGETABLE_PMD_FOLDED
38         return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
39 #else
40         return 0;
41 #endif
42 }
43
44 /*
45  * Select all bits except the pfn
46  */
47 static inline pgprot_t pte_pgprot(pte_t pte)
48 {
49         unsigned long pfn = pte_pfn(pte);
50
51         return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
52 }
53
54 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
55                            pte_t *ptep, size_t *pgsize)
56 {
57         pgd_t *pgd = pgd_offset(mm, addr);
58         pud_t *pud;
59         pmd_t *pmd;
60
61         *pgsize = PAGE_SIZE;
62         pud = pud_offset(pgd, addr);
63         pmd = pmd_offset(pud, addr);
64         if ((pte_t *)pmd == ptep) {
65                 *pgsize = PMD_SIZE;
66                 return CONT_PMDS;
67         }
68         return CONT_PTES;
69 }
70
71 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
72 {
73         int contig_ptes = 0;
74
75         *pgsize = size;
76
77         switch (size) {
78 #ifdef CONFIG_ARM64_4K_PAGES
79         case PUD_SIZE:
80 #endif
81         case PMD_SIZE:
82                 contig_ptes = 1;
83                 break;
84         case CONT_PMD_SIZE:
85                 *pgsize = PMD_SIZE;
86                 contig_ptes = CONT_PMDS;
87                 break;
88         case CONT_PTE_SIZE:
89                 *pgsize = PAGE_SIZE;
90                 contig_ptes = CONT_PTES;
91                 break;
92         }
93
94         return contig_ptes;
95 }
96
97 /*
98  * Changing some bits of contiguous entries requires us to follow a
99  * Break-Before-Make approach, breaking the whole contiguous set
100  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
101  * "Misprogramming of the Contiguous bit", page D4-1762.
102  *
103  * This helper performs the break step.
104  */
105 static pte_t get_clear_flush(struct mm_struct *mm,
106                              unsigned long addr,
107                              pte_t *ptep,
108                              unsigned long pgsize,
109                              unsigned long ncontig)
110 {
111         struct vm_area_struct vma = { .vm_mm = mm };
112         pte_t orig_pte = huge_ptep_get(ptep);
113         bool valid = pte_valid(orig_pte);
114         unsigned long i, saddr = addr;
115
116         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
117                 pte_t pte = ptep_get_and_clear(mm, addr, ptep);
118
119                 /*
120                  * If HW_AFDBM is enabled, then the HW could turn on
121                  * the dirty or accessed bit for any page in the set,
122                  * so check them all.
123                  */
124                 if (pte_dirty(pte))
125                         orig_pte = pte_mkdirty(orig_pte);
126
127                 if (pte_young(pte))
128                         orig_pte = pte_mkyoung(orig_pte);
129         }
130
131         if (valid)
132                 flush_tlb_range(&vma, saddr, addr);
133         return orig_pte;
134 }
135
136 /*
137  * Changing some bits of contiguous entries requires us to follow a
138  * Break-Before-Make approach, breaking the whole contiguous set
139  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
140  * "Misprogramming of the Contiguous bit", page D4-1762.
141  *
142  * This helper performs the break step for use cases where the
143  * original pte is not needed.
144  */
145 static void clear_flush(struct mm_struct *mm,
146                              unsigned long addr,
147                              pte_t *ptep,
148                              unsigned long pgsize,
149                              unsigned long ncontig)
150 {
151         struct vm_area_struct vma = { .vm_mm = mm };
152         unsigned long i, saddr = addr;
153
154         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
155                 pte_clear(mm, addr, ptep);
156
157         flush_tlb_range(&vma, saddr, addr);
158 }
159
160 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
161                             pte_t *ptep, pte_t pte)
162 {
163         size_t pgsize;
164         int i;
165         int ncontig;
166         unsigned long pfn, dpfn;
167         pgprot_t hugeprot;
168
169         /*
170          * Code needs to be expanded to handle huge swap and migration
171          * entries. Needed for HUGETLB and MEMORY_FAILURE.
172          */
173         WARN_ON(!pte_present(pte));
174
175         if (!pte_cont(pte)) {
176                 set_pte_at(mm, addr, ptep, pte);
177                 return;
178         }
179
180         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
181         pfn = pte_pfn(pte);
182         dpfn = pgsize >> PAGE_SHIFT;
183         hugeprot = pte_pgprot(pte);
184
185         clear_flush(mm, addr, ptep, pgsize, ncontig);
186
187         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) {
188                 pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
189                          pte_val(pfn_pte(pfn, hugeprot)));
190                 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
191         }
192 }
193
194 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
195                           pte_t *ptep, pte_t pte, unsigned long sz)
196 {
197         int i, ncontig;
198         size_t pgsize;
199
200         ncontig = num_contig_ptes(sz, &pgsize);
201
202         for (i = 0; i < ncontig; i++, ptep++)
203                 set_pte(ptep, pte);
204 }
205
206 pte_t *huge_pte_alloc(struct mm_struct *mm,
207                       unsigned long addr, unsigned long sz)
208 {
209         pgd_t *pgd;
210         pud_t *pud;
211         pte_t *pte = NULL;
212
213         pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
214         pgd = pgd_offset(mm, addr);
215         pud = pud_alloc(mm, pgd, addr);
216         if (!pud)
217                 return NULL;
218
219         if (sz == PUD_SIZE) {
220                 pte = (pte_t *)pud;
221         } else if (sz == (PAGE_SIZE * CONT_PTES)) {
222                 pmd_t *pmd = pmd_alloc(mm, pud, addr);
223
224                 WARN_ON(addr & (sz - 1));
225                 /*
226                  * Note that if this code were ever ported to the
227                  * 32-bit arm platform then it will cause trouble in
228                  * the case where CONFIG_HIGHPTE is set, since there
229                  * will be no pte_unmap() to correspond with this
230                  * pte_alloc_map().
231                  */
232                 pte = pte_alloc_map(mm, pmd, addr);
233         } else if (sz == PMD_SIZE) {
234                 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
235                     pud_none(*pud))
236                         pte = huge_pmd_share(mm, addr, pud);
237                 else
238                         pte = (pte_t *)pmd_alloc(mm, pud, addr);
239         } else if (sz == (PMD_SIZE * CONT_PMDS)) {
240                 pmd_t *pmd;
241
242                 pmd = pmd_alloc(mm, pud, addr);
243                 WARN_ON(addr & (sz - 1));
244                 return (pte_t *)pmd;
245         }
246
247         pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
248                sz, pte, pte_val(*pte));
249         return pte;
250 }
251
252 pte_t *huge_pte_offset(struct mm_struct *mm,
253                        unsigned long addr, unsigned long sz)
254 {
255         pgd_t *pgd;
256         pud_t *pud;
257         pmd_t *pmd;
258
259         pgd = pgd_offset(mm, addr);
260         pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
261         if (!pgd_present(*pgd))
262                 return NULL;
263
264         pud = pud_offset(pgd, addr);
265         if (sz != PUD_SIZE && pud_none(*pud))
266                 return NULL;
267         /* hugepage or swap? */
268         if (pud_huge(*pud) || !pud_present(*pud))
269                 return (pte_t *)pud;
270         /* table; check the next level */
271
272         if (sz == CONT_PMD_SIZE)
273                 addr &= CONT_PMD_MASK;
274
275         pmd = pmd_offset(pud, addr);
276         if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
277             pmd_none(*pmd))
278                 return NULL;
279         if (pmd_huge(*pmd) || !pmd_present(*pmd))
280                 return (pte_t *)pmd;
281
282         if (sz == CONT_PTE_SIZE) {
283                 pte_t *pte = pte_offset_kernel(pmd, (addr & CONT_PTE_MASK));
284                 return pte;
285         }
286
287         return NULL;
288 }
289
290 pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
291                          struct page *page, int writable)
292 {
293         size_t pagesize = huge_page_size(hstate_vma(vma));
294
295         if (pagesize == CONT_PTE_SIZE) {
296                 entry = pte_mkcont(entry);
297         } else if (pagesize == CONT_PMD_SIZE) {
298                 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
299         } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
300                 pr_warn("%s: unrecognized huge page size 0x%lx\n",
301                         __func__, pagesize);
302         }
303         return entry;
304 }
305
306 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
307                     pte_t *ptep, unsigned long sz)
308 {
309         int i, ncontig;
310         size_t pgsize;
311
312         ncontig = num_contig_ptes(sz, &pgsize);
313
314         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
315                 pte_clear(mm, addr, ptep);
316 }
317
318 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
319                               unsigned long addr, pte_t *ptep)
320 {
321         int ncontig;
322         size_t pgsize;
323         pte_t orig_pte = huge_ptep_get(ptep);
324
325         if (!pte_cont(orig_pte))
326                 return ptep_get_and_clear(mm, addr, ptep);
327
328         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
329
330         return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
331 }
332
333 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
334                                unsigned long addr, pte_t *ptep,
335                                pte_t pte, int dirty)
336 {
337         int ncontig, i, changed = 0;
338         size_t pgsize = 0;
339         unsigned long pfn = pte_pfn(pte), dpfn;
340         pgprot_t hugeprot;
341         pte_t orig_pte;
342
343         if (!pte_cont(pte))
344                 return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
345
346         ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
347         dpfn = pgsize >> PAGE_SHIFT;
348
349         orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
350         if (!pte_same(orig_pte, pte))
351                 changed = 1;
352
353         /* Make sure we don't lose the dirty or young state */
354         if (pte_dirty(orig_pte))
355                 pte = pte_mkdirty(pte);
356
357         if (pte_young(orig_pte))
358                 pte = pte_mkyoung(pte);
359
360         hugeprot = pte_pgprot(pte);
361         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
362                 set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
363
364         return changed;
365 }
366
367 void huge_ptep_set_wrprotect(struct mm_struct *mm,
368                              unsigned long addr, pte_t *ptep)
369 {
370         unsigned long pfn, dpfn;
371         pgprot_t hugeprot;
372         int ncontig, i;
373         size_t pgsize;
374         pte_t pte;
375
376         if (!pte_cont(*ptep)) {
377                 ptep_set_wrprotect(mm, addr, ptep);
378                 return;
379         }
380
381         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
382         dpfn = pgsize >> PAGE_SHIFT;
383
384         pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
385         pte = pte_wrprotect(pte);
386
387         hugeprot = pte_pgprot(pte);
388         pfn = pte_pfn(pte);
389
390         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
391                 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
392 }
393
394 void huge_ptep_clear_flush(struct vm_area_struct *vma,
395                            unsigned long addr, pte_t *ptep)
396 {
397         size_t pgsize;
398         int ncontig;
399
400         if (!pte_cont(*ptep)) {
401                 ptep_clear_flush(vma, addr, ptep);
402                 return;
403         }
404
405         ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
406         clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
407 }
408
409 static __init int setup_hugepagesz(char *opt)
410 {
411         unsigned long ps = memparse(opt, &opt);
412
413         switch (ps) {
414 #ifdef CONFIG_ARM64_4K_PAGES
415         case PUD_SIZE:
416 #endif
417         case PMD_SIZE * CONT_PMDS:
418         case PMD_SIZE:
419         case PAGE_SIZE * CONT_PTES:
420                 hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
421                 return 1;
422         }
423
424         hugetlb_bad_size();
425         pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
426         return 0;
427 }
428 __setup("hugepagesz=", setup_hugepagesz);
429
430 #ifdef CONFIG_ARM64_64K_PAGES
431 static __init int add_default_hugepagesz(void)
432 {
433         if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
434                 hugetlb_add_hstate(CONT_PTE_SHIFT);
435         return 0;
436 }
437 arch_initcall(add_default_hugepagesz);
438 #endif