Merge tag 'irqchip-4.19-2' of git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm...
[sfrench/cifs-2.6.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 *pgdp = pgd_offset(mm, addr);
58         pud_t *pudp;
59         pmd_t *pmdp;
60
61         *pgsize = PAGE_SIZE;
62         pudp = pud_offset(pgdp, addr);
63         pmdp = pmd_offset(pudp, addr);
64         if ((pte_t *)pmdp == 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         pte_t orig_pte = huge_ptep_get(ptep);
112         bool valid = pte_valid(orig_pte);
113         unsigned long i, saddr = addr;
114
115         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
116                 pte_t pte = ptep_get_and_clear(mm, addr, ptep);
117
118                 /*
119                  * If HW_AFDBM is enabled, then the HW could turn on
120                  * the dirty bit for any page in the set, so check
121                  * them all.  All hugetlb entries are already young.
122                  */
123                 if (pte_dirty(pte))
124                         orig_pte = pte_mkdirty(orig_pte);
125         }
126
127         if (valid) {
128                 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
129                 flush_tlb_range(&vma, saddr, addr);
130         }
131         return orig_pte;
132 }
133
134 /*
135  * Changing some bits of contiguous entries requires us to follow a
136  * Break-Before-Make approach, breaking the whole contiguous set
137  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
138  * "Misprogramming of the Contiguous bit", page D4-1762.
139  *
140  * This helper performs the break step for use cases where the
141  * original pte is not needed.
142  */
143 static void clear_flush(struct mm_struct *mm,
144                              unsigned long addr,
145                              pte_t *ptep,
146                              unsigned long pgsize,
147                              unsigned long ncontig)
148 {
149         struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
150         unsigned long i, saddr = addr;
151
152         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
153                 pte_clear(mm, addr, ptep);
154
155         flush_tlb_range(&vma, saddr, addr);
156 }
157
158 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
159                             pte_t *ptep, pte_t pte)
160 {
161         size_t pgsize;
162         int i;
163         int ncontig;
164         unsigned long pfn, dpfn;
165         pgprot_t hugeprot;
166
167         /*
168          * Code needs to be expanded to handle huge swap and migration
169          * entries. Needed for HUGETLB and MEMORY_FAILURE.
170          */
171         WARN_ON(!pte_present(pte));
172
173         if (!pte_cont(pte)) {
174                 set_pte_at(mm, addr, ptep, pte);
175                 return;
176         }
177
178         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
179         pfn = pte_pfn(pte);
180         dpfn = pgsize >> PAGE_SHIFT;
181         hugeprot = pte_pgprot(pte);
182
183         clear_flush(mm, addr, ptep, pgsize, ncontig);
184
185         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
186                 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
187 }
188
189 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
190                           pte_t *ptep, pte_t pte, unsigned long sz)
191 {
192         int i, ncontig;
193         size_t pgsize;
194
195         ncontig = num_contig_ptes(sz, &pgsize);
196
197         for (i = 0; i < ncontig; i++, ptep++)
198                 set_pte(ptep, pte);
199 }
200
201 pte_t *huge_pte_alloc(struct mm_struct *mm,
202                       unsigned long addr, unsigned long sz)
203 {
204         pgd_t *pgdp;
205         pud_t *pudp;
206         pmd_t *pmdp;
207         pte_t *ptep = NULL;
208
209         pgdp = pgd_offset(mm, addr);
210         pudp = pud_alloc(mm, pgdp, addr);
211         if (!pudp)
212                 return NULL;
213
214         if (sz == PUD_SIZE) {
215                 ptep = (pte_t *)pudp;
216         } else if (sz == (PAGE_SIZE * CONT_PTES)) {
217                 pmdp = pmd_alloc(mm, pudp, addr);
218
219                 WARN_ON(addr & (sz - 1));
220                 /*
221                  * Note that if this code were ever ported to the
222                  * 32-bit arm platform then it will cause trouble in
223                  * the case where CONFIG_HIGHPTE is set, since there
224                  * will be no pte_unmap() to correspond with this
225                  * pte_alloc_map().
226                  */
227                 ptep = pte_alloc_map(mm, pmdp, addr);
228         } else if (sz == PMD_SIZE) {
229                 if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
230                     pud_none(READ_ONCE(*pudp)))
231                         ptep = huge_pmd_share(mm, addr, pudp);
232                 else
233                         ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
234         } else if (sz == (PMD_SIZE * CONT_PMDS)) {
235                 pmdp = pmd_alloc(mm, pudp, addr);
236                 WARN_ON(addr & (sz - 1));
237                 return (pte_t *)pmdp;
238         }
239
240         return ptep;
241 }
242
243 pte_t *huge_pte_offset(struct mm_struct *mm,
244                        unsigned long addr, unsigned long sz)
245 {
246         pgd_t *pgdp;
247         pud_t *pudp, pud;
248         pmd_t *pmdp, pmd;
249
250         pgdp = pgd_offset(mm, addr);
251         if (!pgd_present(READ_ONCE(*pgdp)))
252                 return NULL;
253
254         pudp = pud_offset(pgdp, addr);
255         pud = READ_ONCE(*pudp);
256         if (sz != PUD_SIZE && pud_none(pud))
257                 return NULL;
258         /* hugepage or swap? */
259         if (pud_huge(pud) || !pud_present(pud))
260                 return (pte_t *)pudp;
261         /* table; check the next level */
262
263         if (sz == CONT_PMD_SIZE)
264                 addr &= CONT_PMD_MASK;
265
266         pmdp = pmd_offset(pudp, addr);
267         pmd = READ_ONCE(*pmdp);
268         if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
269             pmd_none(pmd))
270                 return NULL;
271         if (pmd_huge(pmd) || !pmd_present(pmd))
272                 return (pte_t *)pmdp;
273
274         if (sz == CONT_PTE_SIZE)
275                 return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
276
277         return NULL;
278 }
279
280 pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
281                          struct page *page, int writable)
282 {
283         size_t pagesize = huge_page_size(hstate_vma(vma));
284
285         if (pagesize == CONT_PTE_SIZE) {
286                 entry = pte_mkcont(entry);
287         } else if (pagesize == CONT_PMD_SIZE) {
288                 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
289         } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
290                 pr_warn("%s: unrecognized huge page size 0x%lx\n",
291                         __func__, pagesize);
292         }
293         return entry;
294 }
295
296 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
297                     pte_t *ptep, unsigned long sz)
298 {
299         int i, ncontig;
300         size_t pgsize;
301
302         ncontig = num_contig_ptes(sz, &pgsize);
303
304         for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
305                 pte_clear(mm, addr, ptep);
306 }
307
308 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
309                               unsigned long addr, pte_t *ptep)
310 {
311         int ncontig;
312         size_t pgsize;
313         pte_t orig_pte = huge_ptep_get(ptep);
314
315         if (!pte_cont(orig_pte))
316                 return ptep_get_and_clear(mm, addr, ptep);
317
318         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
319
320         return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
321 }
322
323 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
324                                unsigned long addr, pte_t *ptep,
325                                pte_t pte, int dirty)
326 {
327         int ncontig, i, changed = 0;
328         size_t pgsize = 0;
329         unsigned long pfn = pte_pfn(pte), dpfn;
330         pgprot_t hugeprot;
331         pte_t orig_pte;
332
333         if (!pte_cont(pte))
334                 return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
335
336         ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
337         dpfn = pgsize >> PAGE_SHIFT;
338
339         orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
340         if (!pte_same(orig_pte, pte))
341                 changed = 1;
342
343         /* Make sure we don't lose the dirty state */
344         if (pte_dirty(orig_pte))
345                 pte = pte_mkdirty(pte);
346
347         hugeprot = pte_pgprot(pte);
348         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
349                 set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
350
351         return changed;
352 }
353
354 void huge_ptep_set_wrprotect(struct mm_struct *mm,
355                              unsigned long addr, pte_t *ptep)
356 {
357         unsigned long pfn, dpfn;
358         pgprot_t hugeprot;
359         int ncontig, i;
360         size_t pgsize;
361         pte_t pte;
362
363         if (!pte_cont(READ_ONCE(*ptep))) {
364                 ptep_set_wrprotect(mm, addr, ptep);
365                 return;
366         }
367
368         ncontig = find_num_contig(mm, addr, ptep, &pgsize);
369         dpfn = pgsize >> PAGE_SHIFT;
370
371         pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
372         pte = pte_wrprotect(pte);
373
374         hugeprot = pte_pgprot(pte);
375         pfn = pte_pfn(pte);
376
377         for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
378                 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
379 }
380
381 void huge_ptep_clear_flush(struct vm_area_struct *vma,
382                            unsigned long addr, pte_t *ptep)
383 {
384         size_t pgsize;
385         int ncontig;
386
387         if (!pte_cont(READ_ONCE(*ptep))) {
388                 ptep_clear_flush(vma, addr, ptep);
389                 return;
390         }
391
392         ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
393         clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
394 }
395
396 static __init int setup_hugepagesz(char *opt)
397 {
398         unsigned long ps = memparse(opt, &opt);
399
400         switch (ps) {
401 #ifdef CONFIG_ARM64_4K_PAGES
402         case PUD_SIZE:
403 #endif
404         case PMD_SIZE * CONT_PMDS:
405         case PMD_SIZE:
406         case PAGE_SIZE * CONT_PTES:
407                 hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
408                 return 1;
409         }
410
411         hugetlb_bad_size();
412         pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
413         return 0;
414 }
415 __setup("hugepagesz=", setup_hugepagesz);
416
417 #ifdef CONFIG_ARM64_64K_PAGES
418 static __init int add_default_hugepagesz(void)
419 {
420         if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
421                 hugetlb_add_hstate(CONT_PTE_SHIFT);
422         return 0;
423 }
424 arch_initcall(add_default_hugepagesz);
425 #endif