Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 2 | * PPC Huge TLB Page Support for Kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 David Gibson, IBM Corporation. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on the IA-32 version: |
| 8 | * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com> |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
David Gibson | 883a3e5 | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 12 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/hugetlb.h> |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 15 | #include <linux/export.h> |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 16 | #include <linux/of_fdt.h> |
| 17 | #include <linux/memblock.h> |
Kumar Gala | 13020be | 2011-11-24 09:40:07 +0000 | [diff] [blame] | 18 | #include <linux/moduleparam.h> |
Aneesh Kumar K.V | 50791e6 | 2017-07-06 15:38:59 -0700 | [diff] [blame] | 19 | #include <linux/swap.h> |
| 20 | #include <linux/swapops.h> |
Christophe Leroy | 803d690 | 2018-08-13 13:19:52 +0000 | [diff] [blame] | 21 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/pgalloc.h> |
| 23 | #include <asm/tlb.h> |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 24 | #include <asm/setup.h> |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 25 | #include <asm/hugetlb.h> |
Aneesh Kumar K.V | 94171b1 | 2017-07-27 11:54:53 +0530 | [diff] [blame] | 26 | #include <asm/pte-walk.h> |
| 27 | |
Hari Bathini | 8597538 | 2018-04-10 19:11:31 +0530 | [diff] [blame] | 28 | bool hugetlb_disabled = false; |
| 29 | |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 30 | #define hugepd_none(hpd) (hpd_val(hpd) == 0) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 31 | |
Christophe Leroy | b12c07a | 2020-05-19 05:49:06 +0000 | [diff] [blame] | 32 | #define PTE_T_ORDER (__builtin_ffs(sizeof(pte_basic_t)) - \ |
| 33 | __builtin_ffs(sizeof(void *))) |
Christophe Leroy | 0356656 | 2018-11-29 14:07:05 +0000 | [diff] [blame] | 34 | |
Punit Agrawal | 7868a20 | 2017-07-06 15:39:42 -0700 | [diff] [blame] | 35 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 36 | { |
Aneesh Kumar K.V | 94171b1 | 2017-07-27 11:54:53 +0530 | [diff] [blame] | 37 | /* |
| 38 | * Only called for hugetlbfs pages, hence can ignore THP and the |
| 39 | * irq disabled walk. |
| 40 | */ |
| 41 | return __find_linux_pte(mm->pgd, addr, NULL, NULL); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp, |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 45 | unsigned long address, unsigned int pdshift, |
| 46 | unsigned int pshift, spinlock_t *ptl) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 47 | { |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 48 | struct kmem_cache *cachep; |
| 49 | pte_t *new; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 50 | int i; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 51 | int num_hugepd; |
| 52 | |
| 53 | if (pshift >= pdshift) { |
Christophe Leroy | 0356656 | 2018-11-29 14:07:05 +0000 | [diff] [blame] | 54 | cachep = PGT_CACHE(PTE_T_ORDER); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 55 | num_hugepd = 1 << (pshift - pdshift); |
| 56 | } else { |
| 57 | cachep = PGT_CACHE(pdshift - pshift); |
| 58 | num_hugepd = 1; |
| 59 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 60 | |
Christophe Leroy | d4870b8 | 2020-05-19 05:49:10 +0000 | [diff] [blame] | 61 | if (!cachep) { |
Aneesh Kumar K.V | 5d49275 | 2019-05-28 11:06:25 +0530 | [diff] [blame] | 62 | WARN_ONCE(1, "No page table cache created for hugetlb tables"); |
| 63 | return -ENOMEM; |
| 64 | } |
| 65 | |
Christophe Leroy | d4870b8 | 2020-05-19 05:49:10 +0000 | [diff] [blame] | 66 | new = kmem_cache_alloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL)); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 67 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 68 | BUG_ON(pshift > HUGEPD_SHIFT_MASK); |
| 69 | BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK); |
| 70 | |
Aneesh Kumar K.V | 5d49275 | 2019-05-28 11:06:25 +0530 | [diff] [blame] | 71 | if (!new) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 72 | return -ENOMEM; |
| 73 | |
Sukadev Bhattiprolu | 0eab46b | 2016-03-24 02:07:57 -0400 | [diff] [blame] | 74 | /* |
| 75 | * Make sure other cpus find the hugepd set only after a |
| 76 | * properly initialized page table is visible to them. |
| 77 | * For more details look for comment in __pte_alloc(). |
| 78 | */ |
| 79 | smp_wmb(); |
| 80 | |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 81 | spin_lock(ptl); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 82 | /* |
| 83 | * We have multiple higher-level entries that point to the same |
| 84 | * actual pte location. Fill in each as we go and backtrack on error. |
| 85 | * We need all of these so the DTLB pgtable walk code can find the |
| 86 | * right higher-level entry without knowing if it's a hugepage or not. |
| 87 | */ |
| 88 | for (i = 0; i < num_hugepd; i++, hpdp++) { |
| 89 | if (unlikely(!hugepd_none(*hpdp))) |
| 90 | break; |
Christophe Leroy | 5fb84fe | 2019-04-26 05:59:45 +0000 | [diff] [blame] | 91 | hugepd_populate(hpdp, new, pshift); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 92 | } |
| 93 | /* If we bailed from the for loop early, an error occurred, clean up */ |
| 94 | if (i < num_hugepd) { |
| 95 | for (i = i - 1 ; i >= 0; i--, hpdp--) |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 96 | *hpdp = __hugepd(0); |
Christophe Leroy | d4870b8 | 2020-05-19 05:49:10 +0000 | [diff] [blame] | 97 | kmem_cache_free(cachep, new); |
Christophe Leroy | 803d690 | 2018-08-13 13:19:52 +0000 | [diff] [blame] | 98 | } else { |
| 99 | kmemleak_ignore(new); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 100 | } |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 101 | spin_unlock(ptl); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 105 | /* |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 106 | * At this point we do the placement change only for BOOK3S 64. This would |
| 107 | * possibly work on other subarchs. |
| 108 | */ |
Peter Xu | aec44e0 | 2021-05-04 18:33:00 -0700 | [diff] [blame] | 109 | pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, |
| 110 | unsigned long addr, unsigned long sz) |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 111 | { |
| 112 | pgd_t *pg; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 113 | p4d_t *p4; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 114 | pud_t *pu; |
| 115 | pmd_t *pm; |
| 116 | hugepd_t *hpdp = NULL; |
| 117 | unsigned pshift = __ffs(sz); |
| 118 | unsigned pdshift = PGDIR_SHIFT; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 119 | spinlock_t *ptl; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 120 | |
| 121 | addr &= ~(sz-1); |
| 122 | pg = pgd_offset(mm, addr); |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 123 | p4 = p4d_offset(pg, addr); |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 124 | |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 125 | #ifdef CONFIG_PPC_BOOK3S_64 |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 126 | if (pshift == PGDIR_SHIFT) |
| 127 | /* 16GB huge page */ |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 128 | return (pte_t *) p4; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 129 | else if (pshift > PUD_SHIFT) { |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 130 | /* |
| 131 | * We need to use hugepd table |
| 132 | */ |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 133 | ptl = &mm->page_table_lock; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 134 | hpdp = (hugepd_t *)p4; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 135 | } else { |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 136 | pdshift = PUD_SHIFT; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 137 | pu = pud_alloc(mm, p4, addr); |
Aneesh Kumar K.V | 2230ebf | 2019-05-28 11:06:24 +0530 | [diff] [blame] | 138 | if (!pu) |
| 139 | return NULL; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 140 | if (pshift == PUD_SHIFT) |
| 141 | return (pte_t *)pu; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 142 | else if (pshift > PMD_SHIFT) { |
| 143 | ptl = pud_lockptr(mm, pu); |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 144 | hpdp = (hugepd_t *)pu; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 145 | } else { |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 146 | pdshift = PMD_SHIFT; |
| 147 | pm = pmd_alloc(mm, pu, addr); |
Aneesh Kumar K.V | 2230ebf | 2019-05-28 11:06:24 +0530 | [diff] [blame] | 148 | if (!pm) |
| 149 | return NULL; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 150 | if (pshift == PMD_SHIFT) |
| 151 | /* 16MB hugepage */ |
| 152 | return (pte_t *)pm; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 153 | else { |
| 154 | ptl = pmd_lockptr(mm, pm); |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 155 | hpdp = (hugepd_t *)pm; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 156 | } |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 159 | #else |
David Gibson | fdf743c | 2018-07-17 14:24:30 +1000 | [diff] [blame] | 160 | if (pshift >= PGDIR_SHIFT) { |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 161 | ptl = &mm->page_table_lock; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 162 | hpdp = (hugepd_t *)p4; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 163 | } else { |
| 164 | pdshift = PUD_SHIFT; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 165 | pu = pud_alloc(mm, p4, addr); |
Aneesh Kumar K.V | 2230ebf | 2019-05-28 11:06:24 +0530 | [diff] [blame] | 166 | if (!pu) |
| 167 | return NULL; |
David Gibson | fdf743c | 2018-07-17 14:24:30 +1000 | [diff] [blame] | 168 | if (pshift >= PUD_SHIFT) { |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 169 | ptl = pud_lockptr(mm, pu); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 170 | hpdp = (hugepd_t *)pu; |
| 171 | } else { |
| 172 | pdshift = PMD_SHIFT; |
| 173 | pm = pmd_alloc(mm, pu, addr); |
Aneesh Kumar K.V | 2230ebf | 2019-05-28 11:06:24 +0530 | [diff] [blame] | 174 | if (!pm) |
| 175 | return NULL; |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 176 | ptl = pmd_lockptr(mm, pm); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 177 | hpdp = (hugepd_t *)pm; |
| 178 | } |
| 179 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 180 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 181 | if (!hpdp) |
| 182 | return NULL; |
| 183 | |
Christophe Leroy | e47168f | 2020-08-31 08:30:44 +0000 | [diff] [blame] | 184 | if (IS_ENABLED(CONFIG_PPC_8xx) && pshift < PMD_SHIFT) |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 185 | return pte_alloc_map(mm, (pmd_t *)hpdp, addr); |
| 186 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 187 | BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp)); |
| 188 | |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 189 | if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, |
| 190 | pdshift, pshift, ptl)) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 193 | return hugepte_offset(*hpdp, addr, pdshift); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 194 | } |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 195 | |
Aneesh Kumar K.V | 79cc38d | 2017-07-28 10:31:26 +0530 | [diff] [blame] | 196 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 197 | /* |
| 198 | * Tracks gpages after the device tree is scanned and before the |
| 199 | * huge_boot_pages list is ready on pseries. |
| 200 | */ |
| 201 | #define MAX_NUMBER_GPAGES 1024 |
| 202 | __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES]; |
| 203 | __initdata static unsigned nr_gpages; |
| 204 | |
| 205 | /* |
| 206 | * Build list of addresses of gigantic pages. This function is used in early |
Anton Blanchard | 14ed740 | 2014-09-17 22:15:34 +1000 | [diff] [blame] | 207 | * boot before the buddy allocator is setup. |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 208 | */ |
Aneesh Kumar K.V | 79cc38d | 2017-07-28 10:31:26 +0530 | [diff] [blame] | 209 | void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages) |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 210 | { |
| 211 | if (!addr) |
| 212 | return; |
| 213 | while (number_of_pages > 0) { |
| 214 | gpage_freearray[nr_gpages] = addr; |
| 215 | nr_gpages++; |
| 216 | number_of_pages--; |
| 217 | addr += page_size; |
| 218 | } |
| 219 | } |
| 220 | |
Cédric Le Goater | 94b87d7 | 2021-01-04 15:31:58 +0100 | [diff] [blame] | 221 | static int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate) |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 222 | { |
| 223 | struct huge_bootmem_page *m; |
| 224 | if (nr_gpages == 0) |
| 225 | return 0; |
| 226 | m = phys_to_virt(gpage_freearray[--nr_gpages]); |
| 227 | gpage_freearray[nr_gpages] = 0; |
| 228 | list_add(&m->list, &huge_boot_pages); |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 229 | m->hstate = hstate; |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 230 | return 1; |
| 231 | } |
Zhenguo Yao | b538908 | 2021-11-05 13:43:28 -0700 | [diff] [blame] | 232 | |
| 233 | bool __init hugetlb_node_alloc_supported(void) |
| 234 | { |
| 235 | return false; |
| 236 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 237 | #endif |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 238 | |
Aneesh Kumar K.V | 79cc38d | 2017-07-28 10:31:26 +0530 | [diff] [blame] | 239 | |
Zhenguo Yao | b538908 | 2021-11-05 13:43:28 -0700 | [diff] [blame] | 240 | int __init alloc_bootmem_huge_page(struct hstate *h, int nid) |
Aneesh Kumar K.V | 79cc38d | 2017-07-28 10:31:26 +0530 | [diff] [blame] | 241 | { |
| 242 | |
| 243 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 244 | if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled()) |
| 245 | return pseries_alloc_bootmem_huge_page(h); |
| 246 | #endif |
Zhenguo Yao | b538908 | 2021-11-05 13:43:28 -0700 | [diff] [blame] | 247 | return __alloc_bootmem_huge_page(h, nid); |
Aneesh Kumar K.V | 79cc38d | 2017-07-28 10:31:26 +0530 | [diff] [blame] | 248 | } |
| 249 | |
Christophe Leroy | 4df4b27 | 2019-04-26 05:59:49 +0000 | [diff] [blame] | 250 | #ifndef CONFIG_PPC_BOOK3S_64 |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 251 | #define HUGEPD_FREELIST_SIZE \ |
| 252 | ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t)) |
| 253 | |
| 254 | struct hugepd_freelist { |
| 255 | struct rcu_head rcu; |
| 256 | unsigned int index; |
Gustavo A. R. Silva | 02bddf2 | 2020-05-07 13:57:55 -0500 | [diff] [blame] | 257 | void *ptes[]; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur); |
| 261 | |
| 262 | static void hugepd_free_rcu_callback(struct rcu_head *head) |
| 263 | { |
| 264 | struct hugepd_freelist *batch = |
| 265 | container_of(head, struct hugepd_freelist, rcu); |
| 266 | unsigned int i; |
| 267 | |
| 268 | for (i = 0; i < batch->index; i++) |
Christophe Leroy | 0356656 | 2018-11-29 14:07:05 +0000 | [diff] [blame] | 269 | kmem_cache_free(PGT_CACHE(PTE_T_ORDER), batch->ptes[i]); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 270 | |
| 271 | free_page((unsigned long)batch); |
| 272 | } |
| 273 | |
| 274 | static void hugepd_free(struct mmu_gather *tlb, void *hugepte) |
| 275 | { |
| 276 | struct hugepd_freelist **batchp; |
| 277 | |
Sebastian Siewior | 08a5bb2 | 2016-03-08 10:03:56 +0100 | [diff] [blame] | 278 | batchp = &get_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 279 | |
| 280 | if (atomic_read(&tlb->mm->mm_users) < 2 || |
Benjamin Herrenschmidt | b426e4b | 2017-07-24 14:28:01 +1000 | [diff] [blame] | 281 | mm_is_thread_local(tlb->mm)) { |
Christophe Leroy | 0356656 | 2018-11-29 14:07:05 +0000 | [diff] [blame] | 282 | kmem_cache_free(PGT_CACHE(PTE_T_ORDER), hugepte); |
Sebastian Siewior | 08a5bb2 | 2016-03-08 10:03:56 +0100 | [diff] [blame] | 283 | put_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
| 287 | if (*batchp == NULL) { |
| 288 | *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC); |
| 289 | (*batchp)->index = 0; |
| 290 | } |
| 291 | |
| 292 | (*batchp)->ptes[(*batchp)->index++] = hugepte; |
| 293 | if ((*batchp)->index == HUGEPD_FREELIST_SIZE) { |
Paul E. McKenney | 0422911 | 2018-11-05 16:53:13 -0800 | [diff] [blame] | 294 | call_rcu(&(*batchp)->rcu, hugepd_free_rcu_callback); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 295 | *batchp = NULL; |
| 296 | } |
Tiejun Chen | 94b09d7 | 2014-01-20 16:39:34 +0800 | [diff] [blame] | 297 | put_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 298 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 299 | #else |
| 300 | static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {} |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 301 | #endif |
| 302 | |
Christophe Leroy | 7bfe54b | 2020-11-06 13:20:54 +0000 | [diff] [blame] | 303 | /* Return true when the entry to be freed maps more than the area being freed */ |
| 304 | static bool range_is_outside_limits(unsigned long start, unsigned long end, |
| 305 | unsigned long floor, unsigned long ceiling, |
| 306 | unsigned long mask) |
| 307 | { |
| 308 | if ((start & mask) < floor) |
| 309 | return true; |
| 310 | if (ceiling) { |
| 311 | ceiling &= mask; |
| 312 | if (!ceiling) |
| 313 | return true; |
| 314 | } |
| 315 | return end - 1 > ceiling - 1; |
| 316 | } |
| 317 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 318 | static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift, |
| 319 | unsigned long start, unsigned long end, |
| 320 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 321 | { |
| 322 | pte_t *hugepte = hugepd_page(*hpdp); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 323 | int i; |
| 324 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 325 | unsigned long pdmask = ~((1UL << pdshift) - 1); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 326 | unsigned int num_hugepd = 1; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 327 | unsigned int shift = hugepd_shift(*hpdp); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 328 | |
| 329 | /* Note: On fsl the hpdp may be the first of several */ |
| 330 | if (shift > pdshift) |
| 331 | num_hugepd = 1 << (shift - pdshift); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 332 | |
Christophe Leroy | 7bfe54b | 2020-11-06 13:20:54 +0000 | [diff] [blame] | 333 | if (range_is_outside_limits(start, end, floor, ceiling, pdmask)) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 334 | return; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 335 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 336 | for (i = 0; i < num_hugepd; i++, hpdp++) |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 337 | *hpdp = __hugepd(0); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 338 | |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 339 | if (shift >= pdshift) |
| 340 | hugepd_free(tlb, hugepte); |
| 341 | else |
Aneesh Kumar K.V | fadd03c | 2018-06-14 16:01:52 +0530 | [diff] [blame] | 342 | pgtable_free_tlb(tlb, hugepte, |
| 343 | get_hugepd_cache_index(pdshift - shift)); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 344 | } |
| 345 | |
Christophe Leroy | 542db12 | 2020-08-31 07:58:19 +0000 | [diff] [blame] | 346 | static void hugetlb_free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, |
| 347 | unsigned long addr, unsigned long end, |
| 348 | unsigned long floor, unsigned long ceiling) |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 349 | { |
| 350 | pgtable_t token = pmd_pgtable(*pmd); |
| 351 | |
Christophe Leroy | 7bfe54b | 2020-11-06 13:20:54 +0000 | [diff] [blame] | 352 | if (range_is_outside_limits(addr, end, floor, ceiling, PMD_MASK)) |
Christophe Leroy | 542db12 | 2020-08-31 07:58:19 +0000 | [diff] [blame] | 353 | return; |
| 354 | |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 355 | pmd_clear(pmd); |
| 356 | pte_free_tlb(tlb, token, addr); |
| 357 | mm_dec_nr_ptes(tlb->mm); |
| 358 | } |
| 359 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 360 | static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 361 | unsigned long addr, unsigned long end, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 362 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 363 | { |
| 364 | pmd_t *pmd; |
| 365 | unsigned long next; |
| 366 | unsigned long start; |
| 367 | |
| 368 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 369 | do { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 370 | unsigned long more; |
| 371 | |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 372 | pmd = pmd_offset(pud, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 373 | next = pmd_addr_end(addr, end); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 374 | if (!is_hugepd(__hugepd(pmd_val(*pmd)))) { |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 375 | if (pmd_none_or_clear_bad(pmd)) |
| 376 | continue; |
| 377 | |
Aneesh Kumar K.V | 8bbd9f0 | 2013-06-19 12:04:26 +0530 | [diff] [blame] | 378 | /* |
| 379 | * if it is not hugepd pointer, we should already find |
| 380 | * it cleared. |
| 381 | */ |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 382 | WARN_ON(!IS_ENABLED(CONFIG_PPC_8xx)); |
| 383 | |
Christophe Leroy | 542db12 | 2020-08-31 07:58:19 +0000 | [diff] [blame] | 384 | hugetlb_free_pte_range(tlb, pmd, addr, end, floor, ceiling); |
Christophe Leroy | b250c8c | 2020-05-19 05:49:09 +0000 | [diff] [blame] | 385 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 386 | continue; |
Aneesh Kumar K.V | 8bbd9f0 | 2013-06-19 12:04:26 +0530 | [diff] [blame] | 387 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 388 | /* |
| 389 | * Increment next by the size of the huge mapping since |
| 390 | * there may be more than one entry at this level for a |
| 391 | * single hugepage, but all of them point to |
| 392 | * the same kmem cache that holds the hugepte. |
| 393 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 394 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd)); |
| 395 | if (more > next) |
| 396 | next = more; |
| 397 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 398 | free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT, |
| 399 | addr, next, floor, ceiling); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 400 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 401 | |
Christophe Leroy | 7bfe54b | 2020-11-06 13:20:54 +0000 | [diff] [blame] | 402 | if (range_is_outside_limits(start, end, floor, ceiling, PUD_MASK)) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 403 | return; |
| 404 | |
Christophe Leroy | 2198d493 | 2020-12-12 13:41:25 +0000 | [diff] [blame] | 405 | pmd = pmd_offset(pud, start & PUD_MASK); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 406 | pud_clear(pud); |
Christophe Leroy | 2198d493 | 2020-12-12 13:41:25 +0000 | [diff] [blame] | 407 | pmd_free_tlb(tlb, pmd, start & PUD_MASK); |
Scott Wood | 50c6a66 | 2015-04-10 19:37:34 -0500 | [diff] [blame] | 408 | mm_dec_nr_pmds(tlb->mm); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 409 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 410 | |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 411 | static void hugetlb_free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 412 | unsigned long addr, unsigned long end, |
| 413 | unsigned long floor, unsigned long ceiling) |
| 414 | { |
| 415 | pud_t *pud; |
| 416 | unsigned long next; |
| 417 | unsigned long start; |
| 418 | |
| 419 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 420 | do { |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 421 | pud = pud_offset(p4d, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 422 | next = pud_addr_end(addr, end); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 423 | if (!is_hugepd(__hugepd(pud_val(*pud)))) { |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 424 | if (pud_none_or_clear_bad(pud)) |
| 425 | continue; |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 426 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 427 | ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 428 | } else { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 429 | unsigned long more; |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 430 | /* |
| 431 | * Increment next by the size of the huge mapping since |
| 432 | * there may be more than one entry at this level for a |
| 433 | * single hugepage, but all of them point to |
| 434 | * the same kmem cache that holds the hugepte. |
| 435 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 436 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pud)); |
| 437 | if (more > next) |
| 438 | next = more; |
| 439 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 440 | free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT, |
| 441 | addr, next, floor, ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 442 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 443 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 444 | |
Christophe Leroy | 7bfe54b | 2020-11-06 13:20:54 +0000 | [diff] [blame] | 445 | if (range_is_outside_limits(start, end, floor, ceiling, PGDIR_MASK)) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 446 | return; |
| 447 | |
Christophe Leroy | 2198d493 | 2020-12-12 13:41:25 +0000 | [diff] [blame] | 448 | pud = pud_offset(p4d, start & PGDIR_MASK); |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 449 | p4d_clear(p4d); |
Christophe Leroy | 2198d493 | 2020-12-12 13:41:25 +0000 | [diff] [blame] | 450 | pud_free_tlb(tlb, pud, start & PGDIR_MASK); |
Kirill A. Shutemov | b4e98d9 | 2017-11-15 17:35:33 -0800 | [diff] [blame] | 451 | mm_dec_nr_puds(tlb->mm); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * This function frees user-level page tables of a process. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 456 | */ |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 457 | void hugetlb_free_pgd_range(struct mmu_gather *tlb, |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 458 | unsigned long addr, unsigned long end, |
| 459 | unsigned long floor, unsigned long ceiling) |
| 460 | { |
| 461 | pgd_t *pgd; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 462 | p4d_t *p4d; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 463 | unsigned long next; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 464 | |
| 465 | /* |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 466 | * Because there are a number of different possible pagetable |
| 467 | * layouts for hugepage ranges, we limit knowledge of how |
| 468 | * things should be laid out to the allocation path |
| 469 | * (huge_pte_alloc(), above). Everything else works out the |
| 470 | * structure as it goes from information in the hugepd |
| 471 | * pointers. That means that we can't here use the |
| 472 | * optimization used in the normal page free_pgd_range(), of |
| 473 | * checking whether we're actually covering a large enough |
| 474 | * range to have to do anything at the top level of the walk |
| 475 | * instead of at the bottom. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 476 | * |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 477 | * To make sense of this, you should probably go read the big |
| 478 | * block comment at the top of the normal free_pgd_range(), |
| 479 | * too. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 480 | */ |
| 481 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 482 | do { |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 483 | next = pgd_addr_end(addr, end); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 484 | pgd = pgd_offset(tlb->mm, addr); |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 485 | p4d = p4d_offset(pgd, addr); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 486 | if (!is_hugepd(__hugepd(pgd_val(*pgd)))) { |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 487 | if (p4d_none_or_clear_bad(p4d)) |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 488 | continue; |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 489 | hugetlb_free_pud_range(tlb, p4d, addr, next, floor, ceiling); |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 490 | } else { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 491 | unsigned long more; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 492 | /* |
| 493 | * Increment next by the size of the huge mapping since |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 494 | * there may be more than one entry at the pgd level |
| 495 | * for a single hugepage, but all of them point to the |
| 496 | * same kmem cache that holds the hugepte. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 497 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 498 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd)); |
| 499 | if (more > next) |
| 500 | next = more; |
| 501 | |
Mike Rapoport | 2fb4706 | 2020-06-04 16:46:44 -0700 | [diff] [blame] | 502 | free_hugepd_range(tlb, (hugepd_t *)p4d, PGDIR_SHIFT, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 503 | addr, next, floor, ceiling); |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 504 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 505 | } while (addr = next, addr != end); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 506 | } |
| 507 | |
Aneesh Kumar K.V | 50791e6 | 2017-07-06 15:38:59 -0700 | [diff] [blame] | 508 | struct page *follow_huge_pd(struct vm_area_struct *vma, |
| 509 | unsigned long address, hugepd_t hpd, |
| 510 | int flags, int pdshift) |
| 511 | { |
| 512 | pte_t *ptep; |
| 513 | spinlock_t *ptl; |
| 514 | struct page *page = NULL; |
| 515 | unsigned long mask; |
| 516 | int shift = hugepd_shift(hpd); |
| 517 | struct mm_struct *mm = vma->vm_mm; |
| 518 | |
| 519 | retry: |
Aneesh Kumar K.V | ed515b6 | 2018-06-01 13:54:24 +0530 | [diff] [blame] | 520 | /* |
| 521 | * hugepage directory entries are protected by mm->page_table_lock |
| 522 | * Use this instead of huge_pte_lockptr |
| 523 | */ |
Aneesh Kumar K.V | 50791e6 | 2017-07-06 15:38:59 -0700 | [diff] [blame] | 524 | ptl = &mm->page_table_lock; |
| 525 | spin_lock(ptl); |
| 526 | |
| 527 | ptep = hugepte_offset(hpd, address, pdshift); |
| 528 | if (pte_present(*ptep)) { |
| 529 | mask = (1UL << shift) - 1; |
| 530 | page = pte_page(*ptep); |
| 531 | page += ((address & mask) >> PAGE_SHIFT); |
| 532 | if (flags & FOLL_GET) |
| 533 | get_page(page); |
| 534 | } else { |
| 535 | if (is_hugetlb_entry_migration(*ptep)) { |
| 536 | spin_unlock(ptl); |
| 537 | __migration_entry_wait(mm, ptep, ptl); |
| 538 | goto retry; |
| 539 | } |
| 540 | } |
| 541 | spin_unlock(ptl); |
| 542 | return page; |
| 543 | } |
| 544 | |
Nicholas Piggin | debeda0 | 2021-12-02 00:41:50 +1000 | [diff] [blame] | 545 | #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA |
| 546 | static inline int file_to_psize(struct file *file) |
| 547 | { |
| 548 | struct hstate *hstate = hstate_file(file); |
| 549 | return shift_to_mmu_psize(huge_page_shift(hstate)); |
| 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 553 | unsigned long len, unsigned long pgoff, |
| 554 | unsigned long flags) |
| 555 | { |
Christophe Leroy | aa0ab02b | 2018-02-22 15:27:26 +0100 | [diff] [blame] | 556 | #ifdef CONFIG_PPC_RADIX_MMU |
Aneesh Kumar K.V | 4848376 | 2016-04-29 23:26:25 +1000 | [diff] [blame] | 557 | if (radix_enabled()) |
| 558 | return radix__hugetlb_get_unmapped_area(file, addr, len, |
| 559 | pgoff, flags); |
Christophe Leroy | aa0ab02b | 2018-02-22 15:27:26 +0100 | [diff] [blame] | 560 | #endif |
Nicholas Piggin | debeda0 | 2021-12-02 00:41:50 +1000 | [diff] [blame] | 561 | #ifdef CONFIG_PPC_MM_SLICES |
| 562 | return slice_get_unmapped_area(addr, len, flags, file_to_psize(file), 1); |
| 563 | #endif |
| 564 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
Becky Bruce | 7651295 | 2011-10-10 10:50:36 +0000 | [diff] [blame] | 566 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 568 | unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) |
| 569 | { |
Aneesh Kumar K.V | 2f5f0df | 2016-04-29 23:26:24 +1000 | [diff] [blame] | 570 | /* With radix we don't use slice, so derive it from vma*/ |
Christophe Leroy | 43ed790 | 2019-04-25 14:29:35 +0000 | [diff] [blame] | 571 | if (IS_ENABLED(CONFIG_PPC_MM_SLICES) && !radix_enabled()) { |
Nicholas Piggin | 014a32b | 2018-03-07 11:37:17 +1000 | [diff] [blame] | 572 | unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start); |
| 573 | |
Aneesh Kumar K.V | 2f5f0df | 2016-04-29 23:26:24 +1000 | [diff] [blame] | 574 | return 1UL << mmu_psize_to_shift(psize); |
Nicholas Piggin | 014a32b | 2018-03-07 11:37:17 +1000 | [diff] [blame] | 575 | } |
Dan Williams | 09135cc | 2018-04-05 16:24:21 -0700 | [diff] [blame] | 576 | return vma_kernel_pagesize(vma); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 579 | bool __init arch_hugetlb_valid_size(unsigned long size) |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 580 | { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 581 | int shift = __ffs(size); |
| 582 | int mmu_psize; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 583 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 584 | /* Check that it is a page size supported by the hardware and |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 585 | * that it fits within pagetable and slice limits. */ |
Christophe Leroy | 723f268 | 2019-04-26 05:59:46 +0000 | [diff] [blame] | 586 | if (size <= PAGE_SIZE || !is_power_of_2(size)) |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 587 | return false; |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 588 | |
Michael Ellerman | 7338874 | 2019-05-14 23:00:58 +1000 | [diff] [blame] | 589 | mmu_psize = check_and_get_huge_psize(shift); |
Christophe Leroy | 723f268 | 2019-04-26 05:59:46 +0000 | [diff] [blame] | 590 | if (mmu_psize < 0) |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 591 | return false; |
Aneesh Kumar K.V | a525108 | 2017-03-21 22:59:56 +0530 | [diff] [blame] | 592 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 593 | BUG_ON(mmu_psize_defs[mmu_psize].shift != shift); |
| 594 | |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 595 | return true; |
| 596 | } |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 597 | |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 598 | static int __init add_huge_page_size(unsigned long long size) |
| 599 | { |
| 600 | int shift = __ffs(size); |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 601 | |
Mike Kravetz | ae94da8 | 2020-06-03 16:00:34 -0700 | [diff] [blame] | 602 | if (!arch_hugetlb_valid_size((unsigned long)size)) |
| 603 | return -EINVAL; |
| 604 | |
Mike Kravetz | 3823783 | 2020-06-03 16:00:42 -0700 | [diff] [blame] | 605 | hugetlb_add_hstate(shift - PAGE_SHIFT); |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 606 | return 0; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 607 | } |
| 608 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 609 | static int __init hugetlbpage_init(void) |
| 610 | { |
Aneesh Kumar K.V | ac25ba6 | 2019-05-28 11:06:26 +0530 | [diff] [blame] | 611 | bool configured = false; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 612 | int psize; |
| 613 | |
Hari Bathini | 8597538 | 2018-04-10 19:11:31 +0530 | [diff] [blame] | 614 | if (hugetlb_disabled) { |
| 615 | pr_info("HugeTLB support is disabled!\n"); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Christophe Leroy | 4df4b27 | 2019-04-26 05:59:49 +0000 | [diff] [blame] | 619 | if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled() && |
| 620 | !mmu_has_feature(MMU_FTR_16M_PAGE)) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 621 | return -ENODEV; |
Christophe Leroy | 4df4b27 | 2019-04-26 05:59:49 +0000 | [diff] [blame] | 622 | |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 623 | for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 624 | unsigned shift; |
| 625 | unsigned pdshift; |
| 626 | |
| 627 | if (!mmu_psize_defs[psize].shift) |
| 628 | continue; |
| 629 | |
| 630 | shift = mmu_psize_to_shift(psize); |
| 631 | |
Aneesh Kumar K.V | 6fa5048 | 2018-03-30 17:34:08 +0530 | [diff] [blame] | 632 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 633 | if (shift > PGDIR_SHIFT) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 634 | continue; |
Aneesh Kumar K.V | 6fa5048 | 2018-03-30 17:34:08 +0530 | [diff] [blame] | 635 | else if (shift > PUD_SHIFT) |
| 636 | pdshift = PGDIR_SHIFT; |
| 637 | else if (shift > PMD_SHIFT) |
| 638 | pdshift = PUD_SHIFT; |
| 639 | else |
| 640 | pdshift = PMD_SHIFT; |
| 641 | #else |
David Gibson | fdf743c | 2018-07-17 14:24:30 +1000 | [diff] [blame] | 642 | if (shift < PUD_SHIFT) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 643 | pdshift = PMD_SHIFT; |
David Gibson | fdf743c | 2018-07-17 14:24:30 +1000 | [diff] [blame] | 644 | else if (shift < PGDIR_SHIFT) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 645 | pdshift = PUD_SHIFT; |
| 646 | else |
| 647 | pdshift = PGDIR_SHIFT; |
Aneesh Kumar K.V | 6fa5048 | 2018-03-30 17:34:08 +0530 | [diff] [blame] | 648 | #endif |
| 649 | |
| 650 | if (add_huge_page_size(1ULL << shift) < 0) |
| 651 | continue; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 652 | /* |
| 653 | * if we have pdshift and shift value same, we don't |
| 654 | * use pgt cache for hugepd. |
| 655 | */ |
Christophe Leroy | f2b67ef | 2020-02-06 13:50:28 +0000 | [diff] [blame] | 656 | if (pdshift > shift) { |
| 657 | if (!IS_ENABLED(CONFIG_PPC_8xx)) |
| 658 | pgtable_cache_add(pdshift - shift); |
| 659 | } else if (IS_ENABLED(CONFIG_PPC_FSL_BOOK3E) || |
| 660 | IS_ENABLED(CONFIG_PPC_8xx)) { |
Christophe Leroy | 1e03c7e2 | 2018-11-29 14:07:07 +0000 | [diff] [blame] | 661 | pgtable_cache_add(PTE_T_ORDER); |
Christophe Leroy | f2b67ef | 2020-02-06 13:50:28 +0000 | [diff] [blame] | 662 | } |
Aneesh Kumar K.V | ac25ba6 | 2019-05-28 11:06:26 +0530 | [diff] [blame] | 663 | |
| 664 | configured = true; |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 665 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 666 | |
Aneesh Kumar K.V | ac25ba6 | 2019-05-28 11:06:26 +0530 | [diff] [blame] | 667 | if (configured) { |
| 668 | if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE)) |
| 669 | hugetlbpage_init_default(); |
| 670 | } else |
| 671 | pr_info("Failed to initialize. Disabling HugeTLB"); |
Christophe Leroy | c5710cd2 | 2019-04-26 05:59:48 +0000 | [diff] [blame] | 672 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 673 | return 0; |
| 674 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 675 | |
Paul Gortmaker | 6f114281 | 2015-05-01 20:08:21 -0400 | [diff] [blame] | 676 | arch_initcall(hugetlbpage_init); |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 677 | |
Aneesh Kumar K.V | ef26b76 | 2020-07-13 20:37:48 +0530 | [diff] [blame] | 678 | void __init gigantic_hugetlb_cma_reserve(void) |
| 679 | { |
| 680 | unsigned long order = 0; |
| 681 | |
| 682 | if (radix_enabled()) |
| 683 | order = PUD_SHIFT - PAGE_SHIFT; |
| 684 | else if (!firmware_has_feature(FW_FEATURE_LPAR) && mmu_psize_defs[MMU_PAGE_16G].shift) |
| 685 | /* |
| 686 | * For pseries we do use ibm,expected#pages for reserving 16G pages. |
| 687 | */ |
| 688 | order = mmu_psize_to_shift(MMU_PAGE_16G) - PAGE_SHIFT; |
| 689 | |
| 690 | if (order) { |
| 691 | VM_WARN_ON(order < MAX_ORDER); |
| 692 | hugetlb_cma_reserve(order); |
| 693 | } |
| 694 | } |