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> |
| 18 | #include <linux/bootmem.h> |
Kumar Gala | 13020be | 2011-11-24 09:40:07 +0000 | [diff] [blame] | 19 | #include <linux/moduleparam.h> |
David Gibson | 883a3e5 | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 20 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/pgalloc.h> |
| 22 | #include <asm/tlb.h> |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 23 | #include <asm/setup.h> |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 24 | #include <asm/hugetlb.h> |
| 25 | |
| 26 | #ifdef CONFIG_HUGETLB_PAGE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Jon Tollefson | 9122434 | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 28 | #define PAGE_SHIFT_64K 16 |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 29 | #define PAGE_SHIFT_512K 19 |
| 30 | #define PAGE_SHIFT_8M 23 |
Jon Tollefson | 9122434 | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 31 | #define PAGE_SHIFT_16M 24 |
| 32 | #define PAGE_SHIFT_16G 34 |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 33 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 34 | unsigned int HPAGE_SHIFT; |
| 35 | |
| 36 | /* |
| 37 | * Tracks gpages after the device tree is scanned and before the |
Becky Bruce | a614688 | 2011-10-10 10:50:43 +0000 | [diff] [blame] | 38 | * huge_boot_pages list is ready. On non-Freescale implementations, this is |
| 39 | * just used to track 16G pages and so is a single array. FSL-based |
| 40 | * implementations may have more than one gpage size, so we need multiple |
| 41 | * arrays |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 42 | */ |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 43 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 44 | #define MAX_NUMBER_GPAGES 128 |
| 45 | struct psize_gpages { |
| 46 | u64 gpage_list[MAX_NUMBER_GPAGES]; |
| 47 | unsigned int nr_gpages; |
| 48 | }; |
| 49 | static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT]; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 50 | #else |
| 51 | #define MAX_NUMBER_GPAGES 1024 |
| 52 | static u64 gpage_freearray[MAX_NUMBER_GPAGES]; |
| 53 | static unsigned nr_gpages; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 54 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 55 | |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 56 | #define hugepd_none(hpd) (hpd_val(hpd) == 0) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 57 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 58 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
| 59 | { |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 60 | /* Only called for hugetlbfs pages, hence can ignore THP */ |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 61 | return __find_linux_pte_or_hugepte(mm->pgd, addr, NULL, NULL); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp, |
| 65 | unsigned long address, unsigned pdshift, unsigned pshift) |
| 66 | { |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 67 | struct kmem_cache *cachep; |
| 68 | pte_t *new; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 69 | int i; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 70 | int num_hugepd; |
| 71 | |
| 72 | if (pshift >= pdshift) { |
| 73 | cachep = hugepte_cache; |
| 74 | num_hugepd = 1 << (pshift - pdshift); |
| 75 | } else { |
| 76 | cachep = PGT_CACHE(pdshift - pshift); |
| 77 | num_hugepd = 1; |
| 78 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 79 | |
Michal Hocko | 2379a23 | 2016-06-24 14:49:12 -0700 | [diff] [blame] | 80 | new = kmem_cache_zalloc(cachep, GFP_KERNEL); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 81 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 82 | BUG_ON(pshift > HUGEPD_SHIFT_MASK); |
| 83 | BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK); |
| 84 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 85 | if (! new) |
| 86 | return -ENOMEM; |
| 87 | |
Sukadev Bhattiprolu | 0eab46b | 2016-03-24 02:07:57 -0400 | [diff] [blame] | 88 | /* |
| 89 | * Make sure other cpus find the hugepd set only after a |
| 90 | * properly initialized page table is visible to them. |
| 91 | * For more details look for comment in __pte_alloc(). |
| 92 | */ |
| 93 | smp_wmb(); |
| 94 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 95 | spin_lock(&mm->page_table_lock); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 96 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 97 | /* |
| 98 | * We have multiple higher-level entries that point to the same |
| 99 | * actual pte location. Fill in each as we go and backtrack on error. |
| 100 | * We need all of these so the DTLB pgtable walk code can find the |
| 101 | * right higher-level entry without knowing if it's a hugepage or not. |
| 102 | */ |
| 103 | for (i = 0; i < num_hugepd; i++, hpdp++) { |
| 104 | if (unlikely(!hugepd_none(*hpdp))) |
| 105 | break; |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 106 | else { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 107 | #ifdef CONFIG_PPC_BOOK3S_64 |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 108 | *hpdp = __hugepd(__pa(new) | |
| 109 | (shift_to_mmu_psize(pshift) << 2)); |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 110 | #elif defined(CONFIG_PPC_8xx) |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 111 | *hpdp = __hugepd(__pa(new) | |
| 112 | (pshift == PAGE_SHIFT_8M ? _PMD_PAGE_8M : |
| 113 | _PMD_PAGE_512K) | _PMD_PRESENT); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 114 | #else |
Aneesh Kumar K.V | cf9427b | 2013-04-28 09:37:29 +0000 | [diff] [blame] | 115 | /* We use the old format for PPC_FSL_BOOK3E */ |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 116 | *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 117 | #endif |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 118 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 119 | } |
| 120 | /* If we bailed from the for loop early, an error occurred, clean up */ |
| 121 | if (i < num_hugepd) { |
| 122 | for (i = i - 1 ; i >= 0; i--, hpdp--) |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 123 | *hpdp = __hugepd(0); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 124 | kmem_cache_free(cachep, new); |
| 125 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 126 | spin_unlock(&mm->page_table_lock); |
| 127 | return 0; |
| 128 | } |
| 129 | |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 130 | /* |
| 131 | * These macros define how to determine which level of the page table holds |
| 132 | * the hpdp. |
| 133 | */ |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 134 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 135 | #define HUGEPD_PGD_SHIFT PGDIR_SHIFT |
| 136 | #define HUGEPD_PUD_SHIFT PUD_SHIFT |
| 137 | #else |
| 138 | #define HUGEPD_PGD_SHIFT PUD_SHIFT |
| 139 | #define HUGEPD_PUD_SHIFT PMD_SHIFT |
| 140 | #endif |
| 141 | |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 142 | /* |
| 143 | * At this point we do the placement change only for BOOK3S 64. This would |
| 144 | * possibly work on other subarchs. |
| 145 | */ |
| 146 | pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz) |
| 147 | { |
| 148 | pgd_t *pg; |
| 149 | pud_t *pu; |
| 150 | pmd_t *pm; |
| 151 | hugepd_t *hpdp = NULL; |
| 152 | unsigned pshift = __ffs(sz); |
| 153 | unsigned pdshift = PGDIR_SHIFT; |
| 154 | |
| 155 | addr &= ~(sz-1); |
| 156 | pg = pgd_offset(mm, addr); |
| 157 | |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 158 | #ifdef CONFIG_PPC_BOOK3S_64 |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 159 | if (pshift == PGDIR_SHIFT) |
| 160 | /* 16GB huge page */ |
| 161 | return (pte_t *) pg; |
| 162 | else if (pshift > PUD_SHIFT) |
| 163 | /* |
| 164 | * We need to use hugepd table |
| 165 | */ |
| 166 | hpdp = (hugepd_t *)pg; |
| 167 | else { |
| 168 | pdshift = PUD_SHIFT; |
| 169 | pu = pud_alloc(mm, pg, addr); |
| 170 | if (pshift == PUD_SHIFT) |
| 171 | return (pte_t *)pu; |
| 172 | else if (pshift > PMD_SHIFT) |
| 173 | hpdp = (hugepd_t *)pu; |
| 174 | else { |
| 175 | pdshift = PMD_SHIFT; |
| 176 | pm = pmd_alloc(mm, pu, addr); |
| 177 | if (pshift == PMD_SHIFT) |
| 178 | /* 16MB hugepage */ |
| 179 | return (pte_t *)pm; |
| 180 | else |
| 181 | hpdp = (hugepd_t *)pm; |
| 182 | } |
| 183 | } |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 184 | #else |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 185 | if (pshift >= HUGEPD_PGD_SHIFT) { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 186 | hpdp = (hugepd_t *)pg; |
| 187 | } else { |
| 188 | pdshift = PUD_SHIFT; |
| 189 | pu = pud_alloc(mm, pg, addr); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 190 | if (pshift >= HUGEPD_PUD_SHIFT) { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 191 | hpdp = (hugepd_t *)pu; |
| 192 | } else { |
| 193 | pdshift = PMD_SHIFT; |
| 194 | pm = pmd_alloc(mm, pu, addr); |
| 195 | hpdp = (hugepd_t *)pm; |
| 196 | } |
| 197 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 198 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 199 | if (!hpdp) |
| 200 | return NULL; |
| 201 | |
| 202 | BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp)); |
| 203 | |
| 204 | if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift)) |
| 205 | return NULL; |
| 206 | |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 207 | return hugepte_offset(*hpdp, addr, pdshift); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 208 | } |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 209 | |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 210 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 211 | /* 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] | 212 | * boot before the buddy allocator is setup. |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 213 | */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 214 | void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages) |
| 215 | { |
| 216 | unsigned int idx = shift_to_mmu_psize(__ffs(page_size)); |
| 217 | int i; |
| 218 | |
| 219 | if (addr == 0) |
| 220 | return; |
| 221 | |
| 222 | gpage_freearray[idx].nr_gpages = number_of_pages; |
| 223 | |
| 224 | for (i = 0; i < number_of_pages; i++) { |
| 225 | gpage_freearray[idx].gpage_list[i] = addr; |
| 226 | addr += page_size; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Moves the gigantic page addresses from the temporary list to the |
| 232 | * huge_boot_pages list. |
| 233 | */ |
| 234 | int alloc_bootmem_huge_page(struct hstate *hstate) |
| 235 | { |
| 236 | struct huge_bootmem_page *m; |
Wanpeng Li | 2415cf1 | 2013-07-03 15:02:43 -0700 | [diff] [blame] | 237 | int idx = shift_to_mmu_psize(huge_page_shift(hstate)); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 238 | int nr_gpages = gpage_freearray[idx].nr_gpages; |
| 239 | |
| 240 | if (nr_gpages == 0) |
| 241 | return 0; |
| 242 | |
| 243 | #ifdef CONFIG_HIGHMEM |
| 244 | /* |
| 245 | * If gpages can be in highmem we can't use the trick of storing the |
| 246 | * data structure in the page; allocate space for this |
| 247 | */ |
Michael Ellerman | e39f223f | 2014-11-18 16:47:35 +1100 | [diff] [blame] | 248 | m = memblock_virt_alloc(sizeof(struct huge_bootmem_page), 0); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 249 | m->phys = gpage_freearray[idx].gpage_list[--nr_gpages]; |
| 250 | #else |
| 251 | m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]); |
| 252 | #endif |
| 253 | |
| 254 | list_add(&m->list, &huge_boot_pages); |
| 255 | gpage_freearray[idx].nr_gpages = nr_gpages; |
| 256 | gpage_freearray[idx].gpage_list[nr_gpages] = 0; |
| 257 | m->hstate = hstate; |
| 258 | |
| 259 | return 1; |
| 260 | } |
| 261 | /* |
| 262 | * Scan the command line hugepagesz= options for gigantic pages; store those in |
| 263 | * a list that we use to allocate the memory once all options are parsed. |
| 264 | */ |
| 265 | |
| 266 | unsigned long gpage_npages[MMU_PAGE_COUNT]; |
| 267 | |
Paul Gortmaker | 8952812 | 2012-05-07 10:32:22 -0400 | [diff] [blame] | 268 | static int __init do_gpage_early_setup(char *param, char *val, |
Luis R. Rodriguez | ecc8617 | 2015-03-30 16:20:03 -0700 | [diff] [blame] | 269 | const char *unused, void *arg) |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 270 | { |
| 271 | static phys_addr_t size; |
| 272 | unsigned long npages; |
| 273 | |
| 274 | /* |
| 275 | * The hugepagesz and hugepages cmdline options are interleaved. We |
| 276 | * use the size variable to keep track of whether or not this was done |
| 277 | * properly and skip over instances where it is incorrect. Other |
| 278 | * command-line parsing code will issue warnings, so we don't need to. |
| 279 | * |
| 280 | */ |
| 281 | if ((strcmp(param, "default_hugepagesz") == 0) || |
| 282 | (strcmp(param, "hugepagesz") == 0)) { |
| 283 | size = memparse(val, NULL); |
| 284 | } else if (strcmp(param, "hugepages") == 0) { |
| 285 | if (size != 0) { |
| 286 | if (sscanf(val, "%lu", &npages) <= 0) |
| 287 | npages = 0; |
James Yang | c4f3eb5 | 2014-11-14 12:32:24 -0600 | [diff] [blame] | 288 | if (npages > MAX_NUMBER_GPAGES) { |
| 289 | pr_warn("MMU: %lu pages requested for page " |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 290 | #ifdef CONFIG_PHYS_ADDR_T_64BIT |
James Yang | c4f3eb5 | 2014-11-14 12:32:24 -0600 | [diff] [blame] | 291 | "size %llu KB, limiting to " |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 292 | #else |
| 293 | "size %u KB, limiting to " |
| 294 | #endif |
James Yang | c4f3eb5 | 2014-11-14 12:32:24 -0600 | [diff] [blame] | 295 | __stringify(MAX_NUMBER_GPAGES) "\n", |
| 296 | npages, size / 1024); |
| 297 | npages = MAX_NUMBER_GPAGES; |
| 298 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 299 | gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages; |
| 300 | size = 0; |
| 301 | } |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* |
| 308 | * This function allocates physical space for pages that are larger than the |
| 309 | * buddy allocator can handle. We want to allocate these in highmem because |
| 310 | * the amount of lowmem is limited. This means that this function MUST be |
| 311 | * called before lowmem_end_addr is set up in MMU_init() in order for the lmb |
| 312 | * allocate to grab highmem. |
| 313 | */ |
| 314 | void __init reserve_hugetlb_gpages(void) |
| 315 | { |
| 316 | static __initdata char cmdline[COMMAND_LINE_SIZE]; |
| 317 | phys_addr_t size, base; |
| 318 | int i; |
| 319 | |
| 320 | strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE); |
Pawel Moll | 026cee0 | 2012-03-26 12:50:51 +1030 | [diff] [blame] | 321 | parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0, |
Luis R. Rodriguez | ecc8617 | 2015-03-30 16:20:03 -0700 | [diff] [blame] | 322 | NULL, &do_gpage_early_setup); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Walk gpage list in reverse, allocating larger page sizes first. |
| 326 | * Skip over unsupported sizes, or sizes that have 0 gpages allocated. |
| 327 | * When we reach the point in the list where pages are no longer |
| 328 | * considered gpages, we're done. |
| 329 | */ |
| 330 | for (i = MMU_PAGE_COUNT-1; i >= 0; i--) { |
| 331 | if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0) |
| 332 | continue; |
| 333 | else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT)) |
| 334 | break; |
| 335 | |
| 336 | size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i)); |
| 337 | base = memblock_alloc_base(size * gpage_npages[i], size, |
| 338 | MEMBLOCK_ALLOC_ANYWHERE); |
| 339 | add_gpage(base, size, gpage_npages[i]); |
| 340 | } |
| 341 | } |
| 342 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 343 | #else /* !PPC_FSL_BOOK3E */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 344 | |
| 345 | /* 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] | 346 | * boot before the buddy allocator is setup. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 347 | */ |
| 348 | void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages) |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 349 | { |
| 350 | if (!addr) |
| 351 | return; |
| 352 | while (number_of_pages > 0) { |
| 353 | gpage_freearray[nr_gpages] = addr; |
| 354 | nr_gpages++; |
| 355 | number_of_pages--; |
| 356 | addr += page_size; |
| 357 | } |
| 358 | } |
| 359 | |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 360 | /* Moves the gigantic page addresses from the temporary list to the |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 361 | * huge_boot_pages list. |
| 362 | */ |
| 363 | int alloc_bootmem_huge_page(struct hstate *hstate) |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 364 | { |
| 365 | struct huge_bootmem_page *m; |
| 366 | if (nr_gpages == 0) |
| 367 | return 0; |
| 368 | m = phys_to_virt(gpage_freearray[--nr_gpages]); |
| 369 | gpage_freearray[nr_gpages] = 0; |
| 370 | list_add(&m->list, &huge_boot_pages); |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 371 | m->hstate = hstate; |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 372 | return 1; |
| 373 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 374 | #endif |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 375 | |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 376 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 377 | #define HUGEPD_FREELIST_SIZE \ |
| 378 | ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t)) |
| 379 | |
| 380 | struct hugepd_freelist { |
| 381 | struct rcu_head rcu; |
| 382 | unsigned int index; |
| 383 | void *ptes[0]; |
| 384 | }; |
| 385 | |
| 386 | static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur); |
| 387 | |
| 388 | static void hugepd_free_rcu_callback(struct rcu_head *head) |
| 389 | { |
| 390 | struct hugepd_freelist *batch = |
| 391 | container_of(head, struct hugepd_freelist, rcu); |
| 392 | unsigned int i; |
| 393 | |
| 394 | for (i = 0; i < batch->index; i++) |
| 395 | kmem_cache_free(hugepte_cache, batch->ptes[i]); |
| 396 | |
| 397 | free_page((unsigned long)batch); |
| 398 | } |
| 399 | |
| 400 | static void hugepd_free(struct mmu_gather *tlb, void *hugepte) |
| 401 | { |
| 402 | struct hugepd_freelist **batchp; |
| 403 | |
Sebastian Siewior | 08a5bb2 | 2016-03-08 10:03:56 +0100 | [diff] [blame] | 404 | batchp = &get_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 405 | |
| 406 | if (atomic_read(&tlb->mm->mm_users) < 2 || |
| 407 | cpumask_equal(mm_cpumask(tlb->mm), |
| 408 | cpumask_of(smp_processor_id()))) { |
| 409 | kmem_cache_free(hugepte_cache, hugepte); |
Sebastian Siewior | 08a5bb2 | 2016-03-08 10:03:56 +0100 | [diff] [blame] | 410 | put_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
| 414 | if (*batchp == NULL) { |
| 415 | *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC); |
| 416 | (*batchp)->index = 0; |
| 417 | } |
| 418 | |
| 419 | (*batchp)->ptes[(*batchp)->index++] = hugepte; |
| 420 | if ((*batchp)->index == HUGEPD_FREELIST_SIZE) { |
| 421 | call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback); |
| 422 | *batchp = NULL; |
| 423 | } |
Tiejun Chen | 94b09d7 | 2014-01-20 16:39:34 +0800 | [diff] [blame] | 424 | put_cpu_var(hugepd_freelist_cur); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 425 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 426 | #else |
| 427 | static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {} |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 428 | #endif |
| 429 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 430 | static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift, |
| 431 | unsigned long start, unsigned long end, |
| 432 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 433 | { |
| 434 | pte_t *hugepte = hugepd_page(*hpdp); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 435 | int i; |
| 436 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 437 | unsigned long pdmask = ~((1UL << pdshift) - 1); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 438 | unsigned int num_hugepd = 1; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 439 | unsigned int shift = hugepd_shift(*hpdp); |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 440 | |
| 441 | /* Note: On fsl the hpdp may be the first of several */ |
| 442 | if (shift > pdshift) |
| 443 | num_hugepd = 1 << (shift - pdshift); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 444 | |
| 445 | start &= pdmask; |
| 446 | if (start < floor) |
| 447 | return; |
| 448 | if (ceiling) { |
| 449 | ceiling &= pdmask; |
| 450 | if (! ceiling) |
| 451 | return; |
| 452 | } |
| 453 | if (end - 1 > ceiling - 1) |
| 454 | return; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 455 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 456 | for (i = 0; i < num_hugepd; i++, hpdp++) |
Aneesh Kumar K.V | 20717e1 | 2016-12-14 10:07:53 +0530 | [diff] [blame] | 457 | *hpdp = __hugepd(0); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 458 | |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 459 | if (shift >= pdshift) |
| 460 | hugepd_free(tlb, hugepte); |
| 461 | else |
| 462 | pgtable_free_tlb(tlb, hugepte, pdshift - shift); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 463 | } |
| 464 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 465 | static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 466 | unsigned long addr, unsigned long end, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 467 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 468 | { |
| 469 | pmd_t *pmd; |
| 470 | unsigned long next; |
| 471 | unsigned long start; |
| 472 | |
| 473 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 474 | do { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 475 | unsigned long more; |
| 476 | |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 477 | pmd = pmd_offset(pud, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 478 | next = pmd_addr_end(addr, end); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 479 | if (!is_hugepd(__hugepd(pmd_val(*pmd)))) { |
Aneesh Kumar K.V | 8bbd9f0 | 2013-06-19 12:04:26 +0530 | [diff] [blame] | 480 | /* |
| 481 | * if it is not hugepd pointer, we should already find |
| 482 | * it cleared. |
| 483 | */ |
| 484 | WARN_ON(!pmd_none_or_clear_bad(pmd)); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 485 | continue; |
Aneesh Kumar K.V | 8bbd9f0 | 2013-06-19 12:04:26 +0530 | [diff] [blame] | 486 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 487 | /* |
| 488 | * Increment next by the size of the huge mapping since |
| 489 | * there may be more than one entry at this level for a |
| 490 | * single hugepage, but all of them point to |
| 491 | * the same kmem cache that holds the hugepte. |
| 492 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 493 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd)); |
| 494 | if (more > next) |
| 495 | next = more; |
| 496 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 497 | free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT, |
| 498 | addr, next, floor, ceiling); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 499 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 500 | |
| 501 | start &= PUD_MASK; |
| 502 | if (start < floor) |
| 503 | return; |
| 504 | if (ceiling) { |
| 505 | ceiling &= PUD_MASK; |
| 506 | if (!ceiling) |
| 507 | return; |
| 508 | } |
| 509 | if (end - 1 > ceiling - 1) |
| 510 | return; |
| 511 | |
| 512 | pmd = pmd_offset(pud, start); |
| 513 | pud_clear(pud); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 514 | pmd_free_tlb(tlb, pmd, start); |
Scott Wood | 50c6a66 | 2015-04-10 19:37:34 -0500 | [diff] [blame] | 515 | mm_dec_nr_pmds(tlb->mm); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 516 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 517 | |
| 518 | static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, |
| 519 | unsigned long addr, unsigned long end, |
| 520 | unsigned long floor, unsigned long ceiling) |
| 521 | { |
| 522 | pud_t *pud; |
| 523 | unsigned long next; |
| 524 | unsigned long start; |
| 525 | |
| 526 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 527 | do { |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 528 | pud = pud_offset(pgd, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 529 | next = pud_addr_end(addr, end); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 530 | if (!is_hugepd(__hugepd(pud_val(*pud)))) { |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 531 | if (pud_none_or_clear_bad(pud)) |
| 532 | continue; |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 533 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 534 | ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 535 | } else { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 536 | unsigned long more; |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 537 | /* |
| 538 | * Increment next by the size of the huge mapping since |
| 539 | * there may be more than one entry at this level for a |
| 540 | * single hugepage, but all of them point to |
| 541 | * the same kmem cache that holds the hugepte. |
| 542 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 543 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pud)); |
| 544 | if (more > next) |
| 545 | next = more; |
| 546 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 547 | free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT, |
| 548 | addr, next, floor, ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 549 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 550 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 551 | |
| 552 | start &= PGDIR_MASK; |
| 553 | if (start < floor) |
| 554 | return; |
| 555 | if (ceiling) { |
| 556 | ceiling &= PGDIR_MASK; |
| 557 | if (!ceiling) |
| 558 | return; |
| 559 | } |
| 560 | if (end - 1 > ceiling - 1) |
| 561 | return; |
| 562 | |
| 563 | pud = pud_offset(pgd, start); |
| 564 | pgd_clear(pgd); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 565 | pud_free_tlb(tlb, pud, start); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * This function frees user-level page tables of a process. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 570 | */ |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 571 | void hugetlb_free_pgd_range(struct mmu_gather *tlb, |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 572 | unsigned long addr, unsigned long end, |
| 573 | unsigned long floor, unsigned long ceiling) |
| 574 | { |
| 575 | pgd_t *pgd; |
| 576 | unsigned long next; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 577 | |
| 578 | /* |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 579 | * Because there are a number of different possible pagetable |
| 580 | * layouts for hugepage ranges, we limit knowledge of how |
| 581 | * things should be laid out to the allocation path |
| 582 | * (huge_pte_alloc(), above). Everything else works out the |
| 583 | * structure as it goes from information in the hugepd |
| 584 | * pointers. That means that we can't here use the |
| 585 | * optimization used in the normal page free_pgd_range(), of |
| 586 | * checking whether we're actually covering a large enough |
| 587 | * range to have to do anything at the top level of the walk |
| 588 | * instead of at the bottom. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 589 | * |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 590 | * To make sense of this, you should probably go read the big |
| 591 | * block comment at the top of the normal free_pgd_range(), |
| 592 | * too. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 593 | */ |
| 594 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 595 | do { |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 596 | next = pgd_addr_end(addr, end); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 597 | pgd = pgd_offset(tlb->mm, addr); |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 598 | if (!is_hugepd(__hugepd(pgd_val(*pgd)))) { |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 599 | if (pgd_none_or_clear_bad(pgd)) |
| 600 | continue; |
| 601 | hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling); |
| 602 | } else { |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 603 | unsigned long more; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 604 | /* |
| 605 | * Increment next by the size of the huge mapping since |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 606 | * there may be more than one entry at the pgd level |
| 607 | * for a single hugepage, but all of them point to the |
| 608 | * same kmem cache that holds the hugepte. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 609 | */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 610 | more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd)); |
| 611 | if (more > next) |
| 612 | next = more; |
| 613 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 614 | free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT, |
| 615 | addr, next, floor, ceiling); |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 616 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 617 | } while (addr = next, addr != end); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 618 | } |
| 619 | |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 620 | /* |
| 621 | * We are holding mmap_sem, so a parallel huge page collapse cannot run. |
| 622 | * To prevent hugepage split, disable irq. |
| 623 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | struct page * |
| 625 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 626 | { |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 627 | bool is_thp; |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 628 | pte_t *ptep, pte; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 629 | unsigned shift; |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 630 | unsigned long mask, flags; |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 631 | struct page *page = ERR_PTR(-EINVAL); |
| 632 | |
| 633 | local_irq_save(flags); |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 634 | ptep = find_linux_pte_or_hugepte(mm->pgd, address, &is_thp, &shift); |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 635 | if (!ptep) |
| 636 | goto no_page; |
| 637 | pte = READ_ONCE(*ptep); |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 638 | /* |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 639 | * Verify it is a huge page else bail. |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 640 | * Transparent hugepages are handled by generic code. We can skip them |
| 641 | * here. |
| 642 | */ |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 643 | if (!shift || is_thp) |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 644 | goto no_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 646 | if (!pte_present(pte)) { |
| 647 | page = NULL; |
| 648 | goto no_page; |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 649 | } |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 650 | mask = (1UL << shift) - 1; |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 651 | page = pte_page(pte); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 652 | if (page) |
| 653 | page += (address & mask) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Aneesh Kumar K.V | 7b868e8 | 2015-05-11 11:58:29 +0530 | [diff] [blame] | 655 | no_page: |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 656 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return page; |
| 658 | } |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | struct page * |
| 661 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 662 | pmd_t *pmd, int write) |
| 663 | { |
| 664 | BUG(); |
| 665 | return NULL; |
| 666 | } |
| 667 | |
Naoya Horiguchi | 61f77ed | 2015-02-11 15:25:15 -0800 | [diff] [blame] | 668 | struct page * |
| 669 | follow_huge_pud(struct mm_struct *mm, unsigned long address, |
| 670 | pud_t *pud, int write) |
| 671 | { |
| 672 | BUG(); |
| 673 | return NULL; |
| 674 | } |
| 675 | |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 676 | static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end, |
| 677 | unsigned long sz) |
| 678 | { |
| 679 | unsigned long __boundary = (addr + sz) & ~(sz-1); |
| 680 | return (__boundary - 1 < end - 1) ? __boundary : end; |
| 681 | } |
| 682 | |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 683 | int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift, |
| 684 | unsigned long end, int write, struct page **pages, int *nr) |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 685 | { |
| 686 | pte_t *ptep; |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 687 | unsigned long sz = 1UL << hugepd_shift(hugepd); |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 688 | unsigned long next; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 689 | |
| 690 | ptep = hugepte_offset(hugepd, addr, pdshift); |
| 691 | do { |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 692 | next = hugepte_addr_end(addr, end, sz); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 693 | if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr)) |
| 694 | return 0; |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 695 | } while (ptep++, addr = next, addr != end); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 696 | |
| 697 | return 1; |
| 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Becky Bruce | 7651295 | 2011-10-10 10:50:36 +0000 | [diff] [blame] | 700 | #ifdef CONFIG_PPC_MM_SLICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 702 | unsigned long len, unsigned long pgoff, |
| 703 | unsigned long flags) |
| 704 | { |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 705 | struct hstate *hstate = hstate_file(file); |
| 706 | int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate)); |
Brian King | 48f797d | 2008-12-04 04:07:54 +0000 | [diff] [blame] | 707 | |
Aneesh Kumar K.V | 4848376 | 2016-04-29 23:26:25 +1000 | [diff] [blame] | 708 | if (radix_enabled()) |
| 709 | return radix__hugetlb_get_unmapped_area(file, addr, len, |
| 710 | pgoff, flags); |
Michel Lespinasse | 34d0717 | 2013-04-29 11:53:52 -0700 | [diff] [blame] | 711 | return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
Becky Bruce | 7651295 | 2011-10-10 10:50:36 +0000 | [diff] [blame] | 713 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 715 | unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) |
| 716 | { |
Paul Mackerras | 25c29f9 | 2011-09-20 19:58:10 +0000 | [diff] [blame] | 717 | #ifdef CONFIG_PPC_MM_SLICES |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 718 | unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start); |
Aneesh Kumar K.V | 2f5f0df | 2016-04-29 23:26:24 +1000 | [diff] [blame] | 719 | /* With radix we don't use slice, so derive it from vma*/ |
| 720 | if (!radix_enabled()) |
| 721 | return 1UL << mmu_psize_to_shift(psize); |
| 722 | #endif |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 723 | if (!is_vm_hugetlb_page(vma)) |
| 724 | return PAGE_SIZE; |
| 725 | |
| 726 | return huge_page_size(hstate_vma(vma)); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static inline bool is_power_of_4(unsigned long x) |
| 730 | { |
| 731 | if (is_power_of_2(x)) |
| 732 | return (__ilog2(x) % 2) ? false : true; |
| 733 | return false; |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 734 | } |
| 735 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 736 | static int __init add_huge_page_size(unsigned long long size) |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 737 | { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 738 | int shift = __ffs(size); |
| 739 | int mmu_psize; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 740 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 741 | /* Check that it is a page size supported by the hardware and |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 742 | * that it fits within pagetable and slice limits. */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 743 | if (size <= PAGE_SIZE) |
| 744 | return -EINVAL; |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 745 | #if defined(CONFIG_PPC_FSL_BOOK3E) |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 746 | if (!is_power_of_4(size)) |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 747 | return -EINVAL; |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 748 | #elif !defined(CONFIG_PPC_8xx) |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 749 | if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT)) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 750 | return -EINVAL; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 751 | #endif |
Jon Tollefson | 9122434 | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 752 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 753 | if ((mmu_psize = shift_to_mmu_psize(shift)) < 0) |
| 754 | return -EINVAL; |
| 755 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 756 | BUG_ON(mmu_psize_defs[mmu_psize].shift != shift); |
| 757 | |
| 758 | /* Return if huge page size has already been setup */ |
| 759 | if (size_to_hstate(size)) |
| 760 | return 0; |
| 761 | |
| 762 | hugetlb_add_hstate(shift - PAGE_SHIFT); |
| 763 | |
| 764 | return 0; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static int __init hugepage_setup_sz(char *str) |
| 768 | { |
| 769 | unsigned long long size; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 770 | |
| 771 | size = memparse(str, &str); |
| 772 | |
Vaishali Thakkar | 71bf79c | 2016-05-19 17:11:14 -0700 | [diff] [blame] | 773 | if (add_huge_page_size(size) != 0) { |
| 774 | hugetlb_bad_size(); |
| 775 | pr_err("Invalid huge page size specified(%llu)\n", size); |
| 776 | } |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 777 | |
| 778 | return 1; |
| 779 | } |
| 780 | __setup("hugepagesz=", hugepage_setup_sz); |
| 781 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 782 | struct kmem_cache *hugepte_cache; |
| 783 | static int __init hugetlbpage_init(void) |
| 784 | { |
| 785 | int psize; |
| 786 | |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 787 | #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx) |
Aneesh Kumar K.V | 4848376 | 2016-04-29 23:26:25 +1000 | [diff] [blame] | 788 | if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE)) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 789 | return -ENODEV; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 790 | #endif |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 791 | for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 792 | unsigned shift; |
| 793 | unsigned pdshift; |
| 794 | |
| 795 | if (!mmu_psize_defs[psize].shift) |
| 796 | continue; |
| 797 | |
| 798 | shift = mmu_psize_to_shift(psize); |
| 799 | |
| 800 | if (add_huge_page_size(1ULL << shift) < 0) |
| 801 | continue; |
| 802 | |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 803 | if (shift < HUGEPD_PUD_SHIFT) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 804 | pdshift = PMD_SHIFT; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 805 | else if (shift < HUGEPD_PGD_SHIFT) |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 806 | pdshift = PUD_SHIFT; |
| 807 | else |
| 808 | pdshift = PGDIR_SHIFT; |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 809 | /* |
| 810 | * if we have pdshift and shift value same, we don't |
| 811 | * use pgt cache for hugepd. |
| 812 | */ |
Nicholas Piggin | bf5ca68 | 2017-01-04 01:55:17 +1000 | [diff] [blame] | 813 | if (pdshift > shift) |
Aneesh Kumar K.V | e2b3d20 | 2013-04-28 09:37:30 +0000 | [diff] [blame] | 814 | pgtable_cache_add(pdshift - shift, NULL); |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 815 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 816 | else if (!hugepte_cache) { |
| 817 | /* |
| 818 | * Create a kmem cache for hugeptes. The bottom bits in |
| 819 | * the pte have size information encoded in them, so |
| 820 | * align them to allow this |
| 821 | */ |
| 822 | hugepte_cache = kmem_cache_create("hugepte-cache", |
| 823 | sizeof(pte_t), |
| 824 | HUGEPD_SHIFT_MASK + 1, |
| 825 | 0, NULL); |
| 826 | if (hugepte_cache == NULL) |
| 827 | panic("%s: Unable to create kmem cache " |
| 828 | "for hugeptes\n", __func__); |
| 829 | |
| 830 | } |
| 831 | #endif |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 832 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 833 | |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 834 | #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx) |
| 835 | /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */ |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 836 | if (mmu_psize_defs[MMU_PAGE_4M].shift) |
| 837 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift; |
Christophe Leroy | 4b914286 | 2016-12-07 08:47:28 +0100 | [diff] [blame] | 838 | else if (mmu_psize_defs[MMU_PAGE_512K].shift) |
| 839 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 840 | #else |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 841 | /* Set default large page size. Currently, we pick 16M or 1M |
| 842 | * depending on what is available |
| 843 | */ |
| 844 | if (mmu_psize_defs[MMU_PAGE_16M].shift) |
| 845 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift; |
| 846 | else if (mmu_psize_defs[MMU_PAGE_1M].shift) |
| 847 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift; |
Aneesh Kumar K.V | 4848376 | 2016-04-29 23:26:25 +1000 | [diff] [blame] | 848 | else if (mmu_psize_defs[MMU_PAGE_2M].shift) |
| 849 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift; |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 850 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 851 | return 0; |
| 852 | } |
Christophe Leroy | 03bb2d6 | 2016-12-07 08:47:26 +0100 | [diff] [blame] | 853 | |
Paul Gortmaker | 6f11428 | 2015-05-01 20:08:21 -0400 | [diff] [blame] | 854 | arch_initcall(hugetlbpage_init); |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 855 | |
| 856 | void flush_dcache_icache_hugepage(struct page *page) |
| 857 | { |
| 858 | int i; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 859 | void *start; |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 860 | |
| 861 | BUG_ON(!PageCompound(page)); |
| 862 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 863 | for (i = 0; i < (1UL << compound_order(page)); i++) { |
| 864 | if (!PageHighMem(page)) { |
| 865 | __flush_dcache_icache(page_address(page+i)); |
| 866 | } else { |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 867 | start = kmap_atomic(page+i); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 868 | __flush_dcache_icache(start); |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 869 | kunmap_atomic(start); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 872 | } |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 873 | |
| 874 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 875 | |
| 876 | /* |
| 877 | * We have 4 cases for pgds and pmds: |
| 878 | * (1) invalid (all zeroes) |
| 879 | * (2) pointer to next table, as normal; bottom 6 bits == 0 |
Aneesh Kumar K.V | 6a119ea | 2015-12-01 09:06:54 +0530 | [diff] [blame] | 880 | * (3) leaf pte for huge page _PAGE_PTE set |
| 881 | * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 882 | * |
| 883 | * So long as we atomically load page table pointers we are safe against teardown, |
| 884 | * we can follow the address down to the the page and take a ref on it. |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 885 | * This function need to be called with interrupts disabled. We use this variant |
| 886 | * when we have MSR[EE] = 0 but the paca->soft_enabled = 1 |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 887 | */ |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 888 | |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 889 | pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 890 | bool *is_thp, unsigned *shift) |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 891 | { |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 892 | pgd_t pgd, *pgdp; |
| 893 | pud_t pud, *pudp; |
| 894 | pmd_t pmd, *pmdp; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 895 | pte_t *ret_pte; |
| 896 | hugepd_t *hpdp = NULL; |
| 897 | unsigned pdshift = PGDIR_SHIFT; |
| 898 | |
| 899 | if (shift) |
| 900 | *shift = 0; |
| 901 | |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 902 | if (is_thp) |
| 903 | *is_thp = false; |
| 904 | |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 905 | pgdp = pgdir + pgd_index(ea); |
Michael Ellerman | 4f9c53c | 2015-03-25 20:11:57 +1100 | [diff] [blame] | 906 | pgd = READ_ONCE(*pgdp); |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 907 | /* |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 908 | * Always operate on the local stack value. This make sure the |
| 909 | * value don't get updated by a parallel THP split/collapse, |
| 910 | * page fault or a page unmap. The return pte_t * is still not |
| 911 | * stable. So should be checked there for above conditions. |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 912 | */ |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 913 | if (pgd_none(pgd)) |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 914 | return NULL; |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 915 | else if (pgd_huge(pgd)) { |
| 916 | ret_pte = (pte_t *) pgdp; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 917 | goto out; |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 918 | } else if (is_hugepd(__hugepd(pgd_val(pgd)))) |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 919 | hpdp = (hugepd_t *)&pgd; |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 920 | else { |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 921 | /* |
| 922 | * Even if we end up with an unmap, the pgtable will not |
| 923 | * be freed, because we do an rcu free and here we are |
| 924 | * irq disabled |
| 925 | */ |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 926 | pdshift = PUD_SHIFT; |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 927 | pudp = pud_offset(&pgd, ea); |
Christian Borntraeger | da1a288 | 2015-01-06 22:47:41 +0100 | [diff] [blame] | 928 | pud = READ_ONCE(*pudp); |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 929 | |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 930 | if (pud_none(pud)) |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 931 | return NULL; |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 932 | else if (pud_huge(pud)) { |
| 933 | ret_pte = (pte_t *) pudp; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 934 | goto out; |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 935 | } else if (is_hugepd(__hugepd(pud_val(pud)))) |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 936 | hpdp = (hugepd_t *)&pud; |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 937 | else { |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 938 | pdshift = PMD_SHIFT; |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 939 | pmdp = pmd_offset(&pud, ea); |
Christian Borntraeger | da1a288 | 2015-01-06 22:47:41 +0100 | [diff] [blame] | 940 | pmd = READ_ONCE(*pmdp); |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 941 | /* |
| 942 | * A hugepage collapse is captured by pmd_none, because |
| 943 | * it mark the pmd none and do a hpte invalidate. |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 944 | */ |
Aneesh Kumar K.V | 7d6e7f7 | 2015-03-30 10:41:04 +0530 | [diff] [blame] | 945 | if (pmd_none(pmd)) |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 946 | return NULL; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 947 | |
Aneesh Kumar K.V | 891121e | 2015-10-09 08:32:21 +0530 | [diff] [blame] | 948 | if (pmd_trans_huge(pmd)) { |
| 949 | if (is_thp) |
| 950 | *is_thp = true; |
| 951 | ret_pte = (pte_t *) pmdp; |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | if (pmd_huge(pmd)) { |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 956 | ret_pte = (pte_t *) pmdp; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 957 | goto out; |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 958 | } else if (is_hugepd(__hugepd(pmd_val(pmd)))) |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 959 | hpdp = (hugepd_t *)&pmd; |
Aneesh Kumar K.V | ac52ae4 | 2013-06-20 14:30:17 +0530 | [diff] [blame] | 960 | else |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 961 | return pte_offset_kernel(&pmd, ea); |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | if (!hpdp) |
| 965 | return NULL; |
| 966 | |
Aneesh Kumar K.V | b30e759 | 2014-11-05 21:57:41 +0530 | [diff] [blame] | 967 | ret_pte = hugepte_offset(*hpdp, ea, pdshift); |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 968 | pdshift = hugepd_shift(*hpdp); |
| 969 | out: |
| 970 | if (shift) |
| 971 | *shift = pdshift; |
| 972 | return ret_pte; |
| 973 | } |
Aneesh Kumar K.V | 691e95f | 2015-03-30 10:41:03 +0530 | [diff] [blame] | 974 | EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte); |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 975 | |
| 976 | int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, |
| 977 | unsigned long end, int write, struct page **pages, int *nr) |
| 978 | { |
| 979 | unsigned long mask; |
| 980 | unsigned long pte_end; |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 981 | struct page *head, *page; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 982 | pte_t pte; |
| 983 | int refs; |
| 984 | |
| 985 | pte_end = (addr + sz) & ~(sz-1); |
| 986 | if (pte_end < end) |
| 987 | end = pte_end; |
| 988 | |
Michael Ellerman | 4f9c53c | 2015-03-25 20:11:57 +1100 | [diff] [blame] | 989 | pte = READ_ONCE(*ptep); |
Aneesh Kumar K.V | ac29c64 | 2016-04-29 23:25:34 +1000 | [diff] [blame] | 990 | mask = _PAGE_PRESENT | _PAGE_READ; |
Christophe Leroy | 6b8cb66 | 2016-09-19 12:58:54 +0200 | [diff] [blame] | 991 | |
| 992 | /* |
| 993 | * On some CPUs like the 8xx, _PAGE_RW hence _PAGE_WRITE is defined |
| 994 | * as 0 and _PAGE_RO has to be set when a page is not writable |
| 995 | */ |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 996 | if (write) |
Aneesh Kumar K.V | c7d5484 | 2016-04-29 23:25:30 +1000 | [diff] [blame] | 997 | mask |= _PAGE_WRITE; |
Christophe Leroy | 6b8cb66 | 2016-09-19 12:58:54 +0200 | [diff] [blame] | 998 | else |
| 999 | mask |= _PAGE_RO; |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 1000 | |
| 1001 | if ((pte_val(pte) & mask) != mask) |
| 1002 | return 0; |
| 1003 | |
| 1004 | /* hugepages are never "special" */ |
| 1005 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 1006 | |
| 1007 | refs = 0; |
| 1008 | head = pte_page(pte); |
| 1009 | |
| 1010 | page = head + ((addr & (sz-1)) >> PAGE_SHIFT); |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 1011 | do { |
| 1012 | VM_BUG_ON(compound_head(page) != head); |
| 1013 | pages[*nr] = page; |
| 1014 | (*nr)++; |
| 1015 | page++; |
| 1016 | refs++; |
| 1017 | } while (addr += PAGE_SIZE, addr != end); |
| 1018 | |
| 1019 | if (!page_cache_add_speculative(head, refs)) { |
| 1020 | *nr -= refs; |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
| 1025 | /* Could be optimized better */ |
| 1026 | *nr -= refs; |
| 1027 | while (refs--) |
| 1028 | put_page(head); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
Aneesh Kumar K.V | 2940999 | 2013-06-20 14:30:16 +0530 | [diff] [blame] | 1032 | return 1; |
| 1033 | } |