Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/spinlock.h> |
| 6 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 7 | #include <linux/mm.h> |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 8 | #include <linux/memremap.h> |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 9 | #include <linux/pagemap.h> |
| 10 | #include <linux/rmap.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/swapops.h> |
| 13 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 14 | #include <linux/sched/signal.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 15 | #include <linux/rwsem.h> |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 16 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 17 | #include <linux/migrate.h> |
| 18 | #include <linux/mm_inline.h> |
| 19 | #include <linux/sched/mm.h> |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 20 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 21 | #include <asm/mmu_context.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 22 | #include <asm/pgtable.h> |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 23 | #include <asm/tlbflush.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 24 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 27 | struct follow_page_context { |
| 28 | struct dev_pagemap *pgmap; |
| 29 | unsigned int page_mask; |
| 30 | }; |
| 31 | |
John Hubbard | fc1d8e7 | 2019-05-13 17:19:08 -0700 | [diff] [blame] | 32 | typedef int (*set_dirty_func_t)(struct page *page); |
| 33 | |
| 34 | static void __put_user_pages_dirty(struct page **pages, |
| 35 | unsigned long npages, |
| 36 | set_dirty_func_t sdf) |
| 37 | { |
| 38 | unsigned long index; |
| 39 | |
| 40 | for (index = 0; index < npages; index++) { |
| 41 | struct page *page = compound_head(pages[index]); |
| 42 | |
| 43 | /* |
| 44 | * Checking PageDirty at this point may race with |
| 45 | * clear_page_dirty_for_io(), but that's OK. Two key cases: |
| 46 | * |
| 47 | * 1) This code sees the page as already dirty, so it skips |
| 48 | * the call to sdf(). That could happen because |
| 49 | * clear_page_dirty_for_io() called page_mkclean(), |
| 50 | * followed by set_page_dirty(). However, now the page is |
| 51 | * going to get written back, which meets the original |
| 52 | * intention of setting it dirty, so all is well: |
| 53 | * clear_page_dirty_for_io() goes on to call |
| 54 | * TestClearPageDirty(), and write the page back. |
| 55 | * |
| 56 | * 2) This code sees the page as clean, so it calls sdf(). |
| 57 | * The page stays dirty, despite being written back, so it |
| 58 | * gets written back again in the next writeback cycle. |
| 59 | * This is harmless. |
| 60 | */ |
| 61 | if (!PageDirty(page)) |
| 62 | sdf(page); |
| 63 | |
| 64 | put_user_page(page); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * put_user_pages_dirty() - release and dirty an array of gup-pinned pages |
| 70 | * @pages: array of pages to be marked dirty and released. |
| 71 | * @npages: number of pages in the @pages array. |
| 72 | * |
| 73 | * "gup-pinned page" refers to a page that has had one of the get_user_pages() |
| 74 | * variants called on that page. |
| 75 | * |
| 76 | * For each page in the @pages array, make that page (or its head page, if a |
| 77 | * compound page) dirty, if it was previously listed as clean. Then, release |
| 78 | * the page using put_user_page(). |
| 79 | * |
| 80 | * Please see the put_user_page() documentation for details. |
| 81 | * |
| 82 | * set_page_dirty(), which does not lock the page, is used here. |
| 83 | * Therefore, it is the caller's responsibility to ensure that this is |
| 84 | * safe. If not, then put_user_pages_dirty_lock() should be called instead. |
| 85 | * |
| 86 | */ |
| 87 | void put_user_pages_dirty(struct page **pages, unsigned long npages) |
| 88 | { |
| 89 | __put_user_pages_dirty(pages, npages, set_page_dirty); |
| 90 | } |
| 91 | EXPORT_SYMBOL(put_user_pages_dirty); |
| 92 | |
| 93 | /** |
| 94 | * put_user_pages_dirty_lock() - release and dirty an array of gup-pinned pages |
| 95 | * @pages: array of pages to be marked dirty and released. |
| 96 | * @npages: number of pages in the @pages array. |
| 97 | * |
| 98 | * For each page in the @pages array, make that page (or its head page, if a |
| 99 | * compound page) dirty, if it was previously listed as clean. Then, release |
| 100 | * the page using put_user_page(). |
| 101 | * |
| 102 | * Please see the put_user_page() documentation for details. |
| 103 | * |
| 104 | * This is just like put_user_pages_dirty(), except that it invokes |
| 105 | * set_page_dirty_lock(), instead of set_page_dirty(). |
| 106 | * |
| 107 | */ |
| 108 | void put_user_pages_dirty_lock(struct page **pages, unsigned long npages) |
| 109 | { |
| 110 | __put_user_pages_dirty(pages, npages, set_page_dirty_lock); |
| 111 | } |
| 112 | EXPORT_SYMBOL(put_user_pages_dirty_lock); |
| 113 | |
| 114 | /** |
| 115 | * put_user_pages() - release an array of gup-pinned pages. |
| 116 | * @pages: array of pages to be marked dirty and released. |
| 117 | * @npages: number of pages in the @pages array. |
| 118 | * |
| 119 | * For each page in the @pages array, release the page using put_user_page(). |
| 120 | * |
| 121 | * Please see the put_user_page() documentation for details. |
| 122 | */ |
| 123 | void put_user_pages(struct page **pages, unsigned long npages) |
| 124 | { |
| 125 | unsigned long index; |
| 126 | |
| 127 | /* |
| 128 | * TODO: this can be optimized for huge pages: if a series of pages is |
| 129 | * physically contiguous and part of the same compound page, then a |
| 130 | * single operation to the head page should suffice. |
| 131 | */ |
| 132 | for (index = 0; index < npages; index++) |
| 133 | put_user_page(pages[index]); |
| 134 | } |
| 135 | EXPORT_SYMBOL(put_user_pages); |
| 136 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 137 | static struct page *no_page_table(struct vm_area_struct *vma, |
| 138 | unsigned int flags) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 139 | { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 140 | /* |
| 141 | * When core dumping an enormous anonymous area that nobody |
| 142 | * has touched so far, we don't want to allocate unnecessary pages or |
| 143 | * page tables. Return error instead of NULL to skip handle_mm_fault, |
| 144 | * then get_dump_page() will return NULL to leave a hole in the dump. |
| 145 | * But we can only make this optimization where a hole would surely |
| 146 | * be zero-filled if handle_mm_fault() actually did handle it. |
| 147 | */ |
| 148 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) |
| 149 | return ERR_PTR(-EFAULT); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 153 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
| 154 | pte_t *pte, unsigned int flags) |
| 155 | { |
| 156 | /* No page to get reference */ |
| 157 | if (flags & FOLL_GET) |
| 158 | return -EFAULT; |
| 159 | |
| 160 | if (flags & FOLL_TOUCH) { |
| 161 | pte_t entry = *pte; |
| 162 | |
| 163 | if (flags & FOLL_WRITE) |
| 164 | entry = pte_mkdirty(entry); |
| 165 | entry = pte_mkyoung(entry); |
| 166 | |
| 167 | if (!pte_same(*pte, entry)) { |
| 168 | set_pte_at(vma->vm_mm, address, pte, entry); |
| 169 | update_mmu_cache(vma, address, pte); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Proper page table entry exists, but no corresponding struct page */ |
| 174 | return -EEXIST; |
| 175 | } |
| 176 | |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 177 | /* |
| 178 | * FOLL_FORCE can write to even unwritable pte's, but only |
| 179 | * after we've gone through a COW cycle and they are dirty. |
| 180 | */ |
| 181 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) |
| 182 | { |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 183 | return pte_write(pte) || |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 184 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); |
| 185 | } |
| 186 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 187 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 188 | unsigned long address, pmd_t *pmd, unsigned int flags, |
| 189 | struct dev_pagemap **pgmap) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 190 | { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 191 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 192 | struct page *page; |
| 193 | spinlock_t *ptl; |
| 194 | pte_t *ptep, pte; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 195 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 196 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 197 | if (unlikely(pmd_bad(*pmd))) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 198 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 199 | |
| 200 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 201 | pte = *ptep; |
| 202 | if (!pte_present(pte)) { |
| 203 | swp_entry_t entry; |
| 204 | /* |
| 205 | * KSM's break_ksm() relies upon recognizing a ksm page |
| 206 | * even while it is being migrated, so for that case we |
| 207 | * need migration_entry_wait(). |
| 208 | */ |
| 209 | if (likely(!(flags & FOLL_MIGRATION))) |
| 210 | goto no_page; |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 211 | if (pte_none(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 212 | goto no_page; |
| 213 | entry = pte_to_swp_entry(pte); |
| 214 | if (!is_migration_entry(entry)) |
| 215 | goto no_page; |
| 216 | pte_unmap_unlock(ptep, ptl); |
| 217 | migration_entry_wait(mm, pmd, address); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 218 | goto retry; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 219 | } |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 220 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 221 | goto no_page; |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 222 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 223 | pte_unmap_unlock(ptep, ptl); |
| 224 | return NULL; |
| 225 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 226 | |
| 227 | page = vm_normal_page(vma, address, pte); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 228 | if (!page && pte_devmap(pte) && (flags & FOLL_GET)) { |
| 229 | /* |
| 230 | * Only return device mapping pages in the FOLL_GET case since |
| 231 | * they are only valid while holding the pgmap reference. |
| 232 | */ |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 233 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
| 234 | if (*pgmap) |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 235 | page = pte_page(pte); |
| 236 | else |
| 237 | goto no_page; |
| 238 | } else if (unlikely(!page)) { |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 239 | if (flags & FOLL_DUMP) { |
| 240 | /* Avoid special (like zero) pages in core dumps */ |
| 241 | page = ERR_PTR(-EFAULT); |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | if (is_zero_pfn(pte_pfn(pte))) { |
| 246 | page = pte_page(pte); |
| 247 | } else { |
| 248 | int ret; |
| 249 | |
| 250 | ret = follow_pfn_pte(vma, address, ptep, flags); |
| 251 | page = ERR_PTR(ret); |
| 252 | goto out; |
| 253 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 256 | if (flags & FOLL_SPLIT && PageTransCompound(page)) { |
| 257 | int ret; |
| 258 | get_page(page); |
| 259 | pte_unmap_unlock(ptep, ptl); |
| 260 | lock_page(page); |
| 261 | ret = split_huge_page(page); |
| 262 | unlock_page(page); |
| 263 | put_page(page); |
| 264 | if (ret) |
| 265 | return ERR_PTR(ret); |
| 266 | goto retry; |
| 267 | } |
| 268 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 269 | if (flags & FOLL_GET) { |
| 270 | if (unlikely(!try_get_page(page))) { |
| 271 | page = ERR_PTR(-ENOMEM); |
| 272 | goto out; |
| 273 | } |
| 274 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 275 | if (flags & FOLL_TOUCH) { |
| 276 | if ((flags & FOLL_WRITE) && |
| 277 | !pte_dirty(pte) && !PageDirty(page)) |
| 278 | set_page_dirty(page); |
| 279 | /* |
| 280 | * pte_mkyoung() would be more correct here, but atomic care |
| 281 | * is needed to avoid losing the dirty bit: it is easier to use |
| 282 | * mark_page_accessed(). |
| 283 | */ |
| 284 | mark_page_accessed(page); |
| 285 | } |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 286 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
Kirill A. Shutemov | e90309c | 2016-01-15 16:54:33 -0800 | [diff] [blame] | 287 | /* Do not mlock pte-mapped THP */ |
| 288 | if (PageTransCompound(page)) |
| 289 | goto out; |
| 290 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 291 | /* |
| 292 | * The preliminary mapping check is mainly to avoid the |
| 293 | * pointless overhead of lock_page on the ZERO_PAGE |
| 294 | * which might bounce very badly if there is contention. |
| 295 | * |
| 296 | * If the page is already locked, we don't need to |
| 297 | * handle it now - vmscan will handle it later if and |
| 298 | * when it attempts to reclaim the page. |
| 299 | */ |
| 300 | if (page->mapping && trylock_page(page)) { |
| 301 | lru_add_drain(); /* push cached pages to LRU */ |
| 302 | /* |
| 303 | * Because we lock page here, and migration is |
| 304 | * blocked by the pte's page reference, and we |
| 305 | * know the page is still mapped, we don't even |
| 306 | * need to check for file-cache page truncation. |
| 307 | */ |
| 308 | mlock_vma_page(page); |
| 309 | unlock_page(page); |
| 310 | } |
| 311 | } |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 312 | out: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 313 | pte_unmap_unlock(ptep, ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 314 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 315 | no_page: |
| 316 | pte_unmap_unlock(ptep, ptl); |
| 317 | if (!pte_none(pte)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 318 | return NULL; |
| 319 | return no_page_table(vma, flags); |
| 320 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 321 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 322 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
| 323 | unsigned long address, pud_t *pudp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 324 | unsigned int flags, |
| 325 | struct follow_page_context *ctx) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 326 | { |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 327 | pmd_t *pmd, pmdval; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 328 | spinlock_t *ptl; |
| 329 | struct page *page; |
| 330 | struct mm_struct *mm = vma->vm_mm; |
| 331 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 332 | pmd = pmd_offset(pudp, address); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 333 | /* |
| 334 | * The READ_ONCE() will stabilize the pmdval in a register or |
| 335 | * on the stack so that it will stop changing under the code. |
| 336 | */ |
| 337 | pmdval = READ_ONCE(*pmd); |
| 338 | if (pmd_none(pmdval)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 339 | return no_page_table(vma, flags); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 340 | if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 341 | page = follow_huge_pmd(mm, address, pmd, flags); |
| 342 | if (page) |
| 343 | return page; |
| 344 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 345 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 346 | if (is_hugepd(__hugepd(pmd_val(pmdval)))) { |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 347 | page = follow_huge_pd(vma, address, |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 348 | __hugepd(pmd_val(pmdval)), flags, |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 349 | PMD_SHIFT); |
| 350 | if (page) |
| 351 | return page; |
| 352 | return no_page_table(vma, flags); |
| 353 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 354 | retry: |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 355 | if (!pmd_present(pmdval)) { |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 356 | if (likely(!(flags & FOLL_MIGRATION))) |
| 357 | return no_page_table(vma, flags); |
| 358 | VM_BUG_ON(thp_migration_supported() && |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 359 | !is_pmd_migration_entry(pmdval)); |
| 360 | if (is_pmd_migration_entry(pmdval)) |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 361 | pmd_migration_entry_wait(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 362 | pmdval = READ_ONCE(*pmd); |
| 363 | /* |
| 364 | * MADV_DONTNEED may convert the pmd to null because |
| 365 | * mmap_sem is held in read mode |
| 366 | */ |
| 367 | if (pmd_none(pmdval)) |
| 368 | return no_page_table(vma, flags); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 369 | goto retry; |
| 370 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 371 | if (pmd_devmap(pmdval)) { |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 372 | ptl = pmd_lock(mm, pmd); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 373 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 374 | spin_unlock(ptl); |
| 375 | if (page) |
| 376 | return page; |
| 377 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 378 | if (likely(!pmd_trans_huge(pmdval))) |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 379 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 380 | |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 381 | if ((flags & FOLL_NUMA) && pmd_protnone(pmdval)) |
Aneesh Kumar K.V | db08f20 | 2017-02-24 14:59:53 -0800 | [diff] [blame] | 382 | return no_page_table(vma, flags); |
| 383 | |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 384 | retry_locked: |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 385 | ptl = pmd_lock(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 386 | if (unlikely(pmd_none(*pmd))) { |
| 387 | spin_unlock(ptl); |
| 388 | return no_page_table(vma, flags); |
| 389 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 390 | if (unlikely(!pmd_present(*pmd))) { |
| 391 | spin_unlock(ptl); |
| 392 | if (likely(!(flags & FOLL_MIGRATION))) |
| 393 | return no_page_table(vma, flags); |
| 394 | pmd_migration_entry_wait(mm, pmd); |
| 395 | goto retry_locked; |
| 396 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 397 | if (unlikely(!pmd_trans_huge(*pmd))) { |
| 398 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 399 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 400 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 401 | if (flags & FOLL_SPLIT) { |
| 402 | int ret; |
| 403 | page = pmd_page(*pmd); |
| 404 | if (is_huge_zero_page(page)) { |
| 405 | spin_unlock(ptl); |
| 406 | ret = 0; |
Kirill A. Shutemov | 78ddc53 | 2016-01-15 16:52:42 -0800 | [diff] [blame] | 407 | split_huge_pmd(vma, pmd, address); |
Naoya Horiguchi | 337d9ab | 2016-07-26 15:24:03 -0700 | [diff] [blame] | 408 | if (pmd_trans_unstable(pmd)) |
| 409 | ret = -EBUSY; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 410 | } else { |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 411 | if (unlikely(!try_get_page(page))) { |
| 412 | spin_unlock(ptl); |
| 413 | return ERR_PTR(-ENOMEM); |
| 414 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 415 | spin_unlock(ptl); |
| 416 | lock_page(page); |
| 417 | ret = split_huge_page(page); |
| 418 | unlock_page(page); |
| 419 | put_page(page); |
Kirill A. Shutemov | baa355f | 2016-07-26 15:25:51 -0700 | [diff] [blame] | 420 | if (pmd_none(*pmd)) |
| 421 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return ret ? ERR_PTR(ret) : |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 425 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 426 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 427 | page = follow_trans_huge_pmd(vma, address, pmd, flags); |
| 428 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 429 | ctx->page_mask = HPAGE_PMD_NR - 1; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 430 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 433 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
| 434 | unsigned long address, p4d_t *p4dp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 435 | unsigned int flags, |
| 436 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 437 | { |
| 438 | pud_t *pud; |
| 439 | spinlock_t *ptl; |
| 440 | struct page *page; |
| 441 | struct mm_struct *mm = vma->vm_mm; |
| 442 | |
| 443 | pud = pud_offset(p4dp, address); |
| 444 | if (pud_none(*pud)) |
| 445 | return no_page_table(vma, flags); |
| 446 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { |
| 447 | page = follow_huge_pud(mm, address, pud, flags); |
| 448 | if (page) |
| 449 | return page; |
| 450 | return no_page_table(vma, flags); |
| 451 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 452 | if (is_hugepd(__hugepd(pud_val(*pud)))) { |
| 453 | page = follow_huge_pd(vma, address, |
| 454 | __hugepd(pud_val(*pud)), flags, |
| 455 | PUD_SHIFT); |
| 456 | if (page) |
| 457 | return page; |
| 458 | return no_page_table(vma, flags); |
| 459 | } |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 460 | if (pud_devmap(*pud)) { |
| 461 | ptl = pud_lock(mm, pud); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 462 | page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 463 | spin_unlock(ptl); |
| 464 | if (page) |
| 465 | return page; |
| 466 | } |
| 467 | if (unlikely(pud_bad(*pud))) |
| 468 | return no_page_table(vma, flags); |
| 469 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 470 | return follow_pmd_mask(vma, address, pud, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 473 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
| 474 | unsigned long address, pgd_t *pgdp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 475 | unsigned int flags, |
| 476 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 477 | { |
| 478 | p4d_t *p4d; |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 479 | struct page *page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 480 | |
| 481 | p4d = p4d_offset(pgdp, address); |
| 482 | if (p4d_none(*p4d)) |
| 483 | return no_page_table(vma, flags); |
| 484 | BUILD_BUG_ON(p4d_huge(*p4d)); |
| 485 | if (unlikely(p4d_bad(*p4d))) |
| 486 | return no_page_table(vma, flags); |
| 487 | |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 488 | if (is_hugepd(__hugepd(p4d_val(*p4d)))) { |
| 489 | page = follow_huge_pd(vma, address, |
| 490 | __hugepd(p4d_val(*p4d)), flags, |
| 491 | P4D_SHIFT); |
| 492 | if (page) |
| 493 | return page; |
| 494 | return no_page_table(vma, flags); |
| 495 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 496 | return follow_pud_mask(vma, address, p4d, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * follow_page_mask - look up a page descriptor from a user-virtual address |
| 501 | * @vma: vm_area_struct mapping @address |
| 502 | * @address: virtual address to look up |
| 503 | * @flags: flags modifying lookup behaviour |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 504 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
| 505 | * pointer to output page_mask |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 506 | * |
| 507 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> |
| 508 | * |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 509 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
| 510 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. |
| 511 | * |
| 512 | * On output, the @ctx->page_mask is set according to the size of the page. |
| 513 | * |
| 514 | * Return: the mapped (struct page *), %NULL if no mapping exists, or |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 515 | * an error pointer if there is a mapping to something not represented |
| 516 | * by a page descriptor (see also vm_normal_page()). |
| 517 | */ |
| 518 | struct page *follow_page_mask(struct vm_area_struct *vma, |
| 519 | unsigned long address, unsigned int flags, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 520 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 521 | { |
| 522 | pgd_t *pgd; |
| 523 | struct page *page; |
| 524 | struct mm_struct *mm = vma->vm_mm; |
| 525 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 526 | ctx->page_mask = 0; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 527 | |
| 528 | /* make this handle hugepd */ |
| 529 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); |
| 530 | if (!IS_ERR(page)) { |
| 531 | BUG_ON(flags & FOLL_GET); |
| 532 | return page; |
| 533 | } |
| 534 | |
| 535 | pgd = pgd_offset(mm, address); |
| 536 | |
| 537 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
| 538 | return no_page_table(vma, flags); |
| 539 | |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 540 | if (pgd_huge(*pgd)) { |
| 541 | page = follow_huge_pgd(mm, address, pgd, flags); |
| 542 | if (page) |
| 543 | return page; |
| 544 | return no_page_table(vma, flags); |
| 545 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 546 | if (is_hugepd(__hugepd(pgd_val(*pgd)))) { |
| 547 | page = follow_huge_pd(vma, address, |
| 548 | __hugepd(pgd_val(*pgd)), flags, |
| 549 | PGDIR_SHIFT); |
| 550 | if (page) |
| 551 | return page; |
| 552 | return no_page_table(vma, flags); |
| 553 | } |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 554 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 555 | return follow_p4d_mask(vma, address, pgd, flags, ctx); |
| 556 | } |
| 557 | |
| 558 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
| 559 | unsigned int foll_flags) |
| 560 | { |
| 561 | struct follow_page_context ctx = { NULL }; |
| 562 | struct page *page; |
| 563 | |
| 564 | page = follow_page_mask(vma, address, foll_flags, &ctx); |
| 565 | if (ctx.pgmap) |
| 566 | put_dev_pagemap(ctx.pgmap); |
| 567 | return page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 570 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
| 571 | unsigned int gup_flags, struct vm_area_struct **vma, |
| 572 | struct page **page) |
| 573 | { |
| 574 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 575 | p4d_t *p4d; |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 576 | pud_t *pud; |
| 577 | pmd_t *pmd; |
| 578 | pte_t *pte; |
| 579 | int ret = -EFAULT; |
| 580 | |
| 581 | /* user gate pages are read-only */ |
| 582 | if (gup_flags & FOLL_WRITE) |
| 583 | return -EFAULT; |
| 584 | if (address > TASK_SIZE) |
| 585 | pgd = pgd_offset_k(address); |
| 586 | else |
| 587 | pgd = pgd_offset_gate(mm, address); |
| 588 | BUG_ON(pgd_none(*pgd)); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 589 | p4d = p4d_offset(pgd, address); |
| 590 | BUG_ON(p4d_none(*p4d)); |
| 591 | pud = pud_offset(p4d, address); |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 592 | BUG_ON(pud_none(*pud)); |
| 593 | pmd = pmd_offset(pud, address); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 594 | if (!pmd_present(*pmd)) |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 595 | return -EFAULT; |
| 596 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
| 597 | pte = pte_offset_map(pmd, address); |
| 598 | if (pte_none(*pte)) |
| 599 | goto unmap; |
| 600 | *vma = get_gate_vma(mm); |
| 601 | if (!page) |
| 602 | goto out; |
| 603 | *page = vm_normal_page(*vma, address, *pte); |
| 604 | if (!*page) { |
| 605 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) |
| 606 | goto unmap; |
| 607 | *page = pte_page(*pte); |
| 608 | } |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 609 | if (unlikely(!try_get_page(*page))) { |
| 610 | ret = -ENOMEM; |
| 611 | goto unmap; |
| 612 | } |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 613 | out: |
| 614 | ret = 0; |
| 615 | unmap: |
| 616 | pte_unmap(pte); |
| 617 | return ret; |
| 618 | } |
| 619 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 620 | /* |
| 621 | * mmap_sem must be held on entry. If @nonblocking != NULL and |
| 622 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. |
| 623 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. |
| 624 | */ |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 625 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
| 626 | unsigned long address, unsigned int *flags, int *nonblocking) |
| 627 | { |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 628 | unsigned int fault_flags = 0; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 629 | vm_fault_t ret; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 630 | |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 631 | /* mlock all present pages, but do not fault in new pages */ |
| 632 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) |
| 633 | return -ENOENT; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 634 | if (*flags & FOLL_WRITE) |
| 635 | fault_flags |= FAULT_FLAG_WRITE; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 636 | if (*flags & FOLL_REMOTE) |
| 637 | fault_flags |= FAULT_FLAG_REMOTE; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 638 | if (nonblocking) |
| 639 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 640 | if (*flags & FOLL_NOWAIT) |
| 641 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; |
Andres Lagar-Cavilla | 234b239 | 2014-09-17 10:51:48 -0700 | [diff] [blame] | 642 | if (*flags & FOLL_TRIED) { |
| 643 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); |
| 644 | fault_flags |= FAULT_FLAG_TRIED; |
| 645 | } |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 646 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 647 | ret = handle_mm_fault(vma, address, fault_flags); |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 648 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 649 | int err = vm_fault_to_errno(ret, *flags); |
| 650 | |
| 651 | if (err) |
| 652 | return err; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 653 | BUG(); |
| 654 | } |
| 655 | |
| 656 | if (tsk) { |
| 657 | if (ret & VM_FAULT_MAJOR) |
| 658 | tsk->maj_flt++; |
| 659 | else |
| 660 | tsk->min_flt++; |
| 661 | } |
| 662 | |
| 663 | if (ret & VM_FAULT_RETRY) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 664 | if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 665 | *nonblocking = 0; |
| 666 | return -EBUSY; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when |
| 671 | * necessary, even if maybe_mkwrite decided not to set pte_write. We |
| 672 | * can thus safely do subsequent page lookups as if they were reads. |
| 673 | * But only do so when looping for pte_write is futile: in some cases |
| 674 | * userspace may also be wanting to write to the gotten user page, |
| 675 | * which a read fault here might prevent (a readonly page might get |
| 676 | * reCOWed by userspace write). |
| 677 | */ |
| 678 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 679 | *flags |= FOLL_COW; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 680 | return 0; |
| 681 | } |
| 682 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 683 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
| 684 | { |
| 685 | vm_flags_t vm_flags = vma->vm_flags; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 686 | int write = (gup_flags & FOLL_WRITE); |
| 687 | int foreign = (gup_flags & FOLL_REMOTE); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 688 | |
| 689 | if (vm_flags & (VM_IO | VM_PFNMAP)) |
| 690 | return -EFAULT; |
| 691 | |
Willy Tarreau | 7f7ccc2 | 2018-05-11 08:11:44 +0200 | [diff] [blame] | 692 | if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma)) |
| 693 | return -EFAULT; |
| 694 | |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 695 | if (write) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 696 | if (!(vm_flags & VM_WRITE)) { |
| 697 | if (!(gup_flags & FOLL_FORCE)) |
| 698 | return -EFAULT; |
| 699 | /* |
| 700 | * We used to let the write,force case do COW in a |
| 701 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could |
| 702 | * set a breakpoint in a read-only mapping of an |
| 703 | * executable, without corrupting the file (yet only |
| 704 | * when that file had been opened for writing!). |
| 705 | * Anon pages in shared mappings are surprising: now |
| 706 | * just reject it. |
| 707 | */ |
Hugh Dickins | 4643536 | 2016-01-30 18:03:16 -0800 | [diff] [blame] | 708 | if (!is_cow_mapping(vm_flags)) |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 709 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 710 | } |
| 711 | } else if (!(vm_flags & VM_READ)) { |
| 712 | if (!(gup_flags & FOLL_FORCE)) |
| 713 | return -EFAULT; |
| 714 | /* |
| 715 | * Is there actually any vma we can reach here which does not |
| 716 | * have VM_MAYREAD set? |
| 717 | */ |
| 718 | if (!(vm_flags & VM_MAYREAD)) |
| 719 | return -EFAULT; |
| 720 | } |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 721 | /* |
| 722 | * gups are always data accesses, not instruction |
| 723 | * fetches, so execute=false here |
| 724 | */ |
| 725 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 726 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 730 | /** |
| 731 | * __get_user_pages() - pin user pages in memory |
| 732 | * @tsk: task_struct of target task |
| 733 | * @mm: mm_struct of target mm |
| 734 | * @start: starting user address |
| 735 | * @nr_pages: number of pages from start to pin |
| 736 | * @gup_flags: flags modifying pin behaviour |
| 737 | * @pages: array that receives pointers to the pages pinned. |
| 738 | * Should be at least nr_pages long. Or NULL, if caller |
| 739 | * only intends to ensure the pages are faulted in. |
| 740 | * @vmas: array of pointers to vmas corresponding to each page. |
| 741 | * Or NULL if the caller does not require them. |
| 742 | * @nonblocking: whether waiting for disk IO or mmap_sem contention |
| 743 | * |
| 744 | * Returns number of pages pinned. This may be fewer than the number |
| 745 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 746 | * were pinned, returns -errno. Each page returned must be released |
| 747 | * with a put_page() call when it is finished with. vmas will only |
| 748 | * remain valid while mmap_sem is held. |
| 749 | * |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 750 | * Must be called with mmap_sem held. It may be released. See below. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 751 | * |
| 752 | * __get_user_pages walks a process's page tables and takes a reference to |
| 753 | * each struct page that each user address corresponds to at a given |
| 754 | * instant. That is, it takes the page that would be accessed if a user |
| 755 | * thread accesses the given user virtual address at that instant. |
| 756 | * |
| 757 | * This does not guarantee that the page exists in the user mappings when |
| 758 | * __get_user_pages returns, and there may even be a completely different |
| 759 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 760 | * and subsequently re faulted). However it does guarantee that the page |
| 761 | * won't be freed completely. And mostly callers simply care that the page |
| 762 | * contains data that was valid *at some point in time*. Typically, an IO |
| 763 | * or similar operation cannot guarantee anything stronger anyway because |
| 764 | * locks can't be held over the syscall boundary. |
| 765 | * |
| 766 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If |
| 767 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as |
| 768 | * appropriate) must be called after the page is finished with, and |
| 769 | * before put_page is called. |
| 770 | * |
| 771 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO |
| 772 | * or mmap_sem contention, and if waiting is needed to pin all pages, |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 773 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
| 774 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in |
| 775 | * this case. |
| 776 | * |
| 777 | * A caller using such a combination of @nonblocking and @gup_flags |
| 778 | * must therefore hold the mmap_sem for reading only, and recognize |
| 779 | * when it's been released. Otherwise, it must be held for either |
| 780 | * reading or writing and will not be released. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 781 | * |
| 782 | * In most cases, get_user_pages or get_user_pages_fast should be used |
| 783 | * instead of __get_user_pages. __get_user_pages should be used only if |
| 784 | * you need some special @gup_flags. |
| 785 | */ |
Lorenzo Stoakes | 0d73175 | 2016-10-24 10:57:25 +0100 | [diff] [blame] | 786 | static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 787 | unsigned long start, unsigned long nr_pages, |
| 788 | unsigned int gup_flags, struct page **pages, |
| 789 | struct vm_area_struct **vmas, int *nonblocking) |
| 790 | { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 791 | long ret = 0, i = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 792 | struct vm_area_struct *vma = NULL; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 793 | struct follow_page_context ctx = { NULL }; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 794 | |
| 795 | if (!nr_pages) |
| 796 | return 0; |
| 797 | |
| 798 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); |
| 799 | |
| 800 | /* |
| 801 | * If FOLL_FORCE is set then do not force a full fault as the hinting |
| 802 | * fault information is unrelated to the reference behaviour of a task |
| 803 | * using the address space |
| 804 | */ |
| 805 | if (!(gup_flags & FOLL_FORCE)) |
| 806 | gup_flags |= FOLL_NUMA; |
| 807 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 808 | do { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 809 | struct page *page; |
| 810 | unsigned int foll_flags = gup_flags; |
| 811 | unsigned int page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 812 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 813 | /* first iteration or cross vma bound */ |
| 814 | if (!vma || start >= vma->vm_end) { |
| 815 | vma = find_extend_vma(mm, start); |
| 816 | if (!vma && in_gate_area(mm, start)) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 817 | ret = get_gate_page(mm, start & PAGE_MASK, |
| 818 | gup_flags, &vma, |
| 819 | pages ? &pages[i] : NULL); |
| 820 | if (ret) |
John Hubbard | 08be37b | 2018-11-30 14:08:53 -0800 | [diff] [blame] | 821 | goto out; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 822 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 823 | goto next_page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 824 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 825 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 826 | if (!vma || check_vma_flags(vma, gup_flags)) { |
| 827 | ret = -EFAULT; |
| 828 | goto out; |
| 829 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 830 | if (is_vm_hugetlb_page(vma)) { |
| 831 | i = follow_hugetlb_page(mm, vma, pages, vmas, |
| 832 | &start, &nr_pages, i, |
Andrea Arcangeli | 87ffc11 | 2017-02-22 15:43:13 -0800 | [diff] [blame] | 833 | gup_flags, nonblocking); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 834 | continue; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 835 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 836 | } |
| 837 | retry: |
| 838 | /* |
| 839 | * If we have a pending SIGKILL, don't keep faulting pages and |
| 840 | * potentially allocating memory. |
| 841 | */ |
Davidlohr Bueso | fa45f11 | 2019-01-03 15:28:55 -0800 | [diff] [blame] | 842 | if (fatal_signal_pending(current)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 843 | ret = -ERESTARTSYS; |
| 844 | goto out; |
| 845 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 846 | cond_resched(); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 847 | |
| 848 | page = follow_page_mask(vma, start, foll_flags, &ctx); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 849 | if (!page) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 850 | ret = faultin_page(tsk, vma, start, &foll_flags, |
| 851 | nonblocking); |
| 852 | switch (ret) { |
| 853 | case 0: |
| 854 | goto retry; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 855 | case -EBUSY: |
| 856 | ret = 0; |
| 857 | /* FALLTHRU */ |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 858 | case -EFAULT: |
| 859 | case -ENOMEM: |
| 860 | case -EHWPOISON: |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 861 | goto out; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 862 | case -ENOENT: |
| 863 | goto next_page; |
| 864 | } |
| 865 | BUG(); |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 866 | } else if (PTR_ERR(page) == -EEXIST) { |
| 867 | /* |
| 868 | * Proper page table entry exists, but no corresponding |
| 869 | * struct page. |
| 870 | */ |
| 871 | goto next_page; |
| 872 | } else if (IS_ERR(page)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 873 | ret = PTR_ERR(page); |
| 874 | goto out; |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 875 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 876 | if (pages) { |
| 877 | pages[i] = page; |
| 878 | flush_anon_page(vma, page, start); |
| 879 | flush_dcache_page(page); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 880 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 881 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 882 | next_page: |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 883 | if (vmas) { |
| 884 | vmas[i] = vma; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 885 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 886 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 887 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 888 | if (page_increm > nr_pages) |
| 889 | page_increm = nr_pages; |
| 890 | i += page_increm; |
| 891 | start += page_increm * PAGE_SIZE; |
| 892 | nr_pages -= page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 893 | } while (nr_pages); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 894 | out: |
| 895 | if (ctx.pgmap) |
| 896 | put_dev_pagemap(ctx.pgmap); |
| 897 | return i ? i : ret; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 898 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 899 | |
Tobias Klauser | 771ab43 | 2016-12-12 16:41:53 -0800 | [diff] [blame] | 900 | static bool vma_permits_fault(struct vm_area_struct *vma, |
| 901 | unsigned int fault_flags) |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 902 | { |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 903 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
| 904 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 905 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 906 | |
| 907 | if (!(vm_flags & vma->vm_flags)) |
| 908 | return false; |
| 909 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 910 | /* |
| 911 | * The architecture might have a hardware protection |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 912 | * mechanism other than read/write that can deny access. |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 913 | * |
| 914 | * gup always represents data access, not instruction |
| 915 | * fetches, so execute=false here: |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 916 | */ |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 917 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 918 | return false; |
| 919 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 920 | return true; |
| 921 | } |
| 922 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 923 | /* |
| 924 | * fixup_user_fault() - manually resolve a user page fault |
| 925 | * @tsk: the task_struct to use for page fault accounting, or |
| 926 | * NULL if faults are not to be recorded. |
| 927 | * @mm: mm_struct of target mm |
| 928 | * @address: user address |
| 929 | * @fault_flags:flags to pass down to handle_mm_fault() |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 930 | * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller |
| 931 | * does not allow retry |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 932 | * |
| 933 | * This is meant to be called in the specific scenario where for locking reasons |
| 934 | * we try to access user memory in atomic context (within a pagefault_disable() |
| 935 | * section), this returns -EFAULT, and we want to resolve the user fault before |
| 936 | * trying again. |
| 937 | * |
| 938 | * Typically this is meant to be used by the futex code. |
| 939 | * |
| 940 | * The main difference with get_user_pages() is that this function will |
| 941 | * unconditionally call handle_mm_fault() which will in turn perform all the |
| 942 | * necessary SW fixup of the dirty and young bits in the PTE, while |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 943 | * get_user_pages() only guarantees to update these in the struct page. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 944 | * |
| 945 | * This is important for some architectures where those bits also gate the |
| 946 | * access permission to the page because they are maintained in software. On |
| 947 | * such architectures, gup() will not be enough to make a subsequent access |
| 948 | * succeed. |
| 949 | * |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 950 | * This function will not return with an unlocked mmap_sem. So it has not the |
| 951 | * same semantics wrt the @mm->mmap_sem as does filemap_fault(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 952 | */ |
| 953 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 954 | unsigned long address, unsigned int fault_flags, |
| 955 | bool *unlocked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 956 | { |
| 957 | struct vm_area_struct *vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 958 | vm_fault_t ret, major = 0; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 959 | |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 960 | if (unlocked) |
| 961 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 962 | |
| 963 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 964 | vma = find_extend_vma(mm, address); |
| 965 | if (!vma || address < vma->vm_start) |
| 966 | return -EFAULT; |
| 967 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 968 | if (!vma_permits_fault(vma, fault_flags)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 969 | return -EFAULT; |
| 970 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 971 | ret = handle_mm_fault(vma, address, fault_flags); |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 972 | major |= ret & VM_FAULT_MAJOR; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 973 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 974 | int err = vm_fault_to_errno(ret, 0); |
| 975 | |
| 976 | if (err) |
| 977 | return err; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 978 | BUG(); |
| 979 | } |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 980 | |
| 981 | if (ret & VM_FAULT_RETRY) { |
| 982 | down_read(&mm->mmap_sem); |
| 983 | if (!(fault_flags & FAULT_FLAG_TRIED)) { |
| 984 | *unlocked = true; |
| 985 | fault_flags &= ~FAULT_FLAG_ALLOW_RETRY; |
| 986 | fault_flags |= FAULT_FLAG_TRIED; |
| 987 | goto retry; |
| 988 | } |
| 989 | } |
| 990 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 991 | if (tsk) { |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 992 | if (major) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 993 | tsk->maj_flt++; |
| 994 | else |
| 995 | tsk->min_flt++; |
| 996 | } |
| 997 | return 0; |
| 998 | } |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 999 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1000 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1001 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
| 1002 | struct mm_struct *mm, |
| 1003 | unsigned long start, |
| 1004 | unsigned long nr_pages, |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1005 | struct page **pages, |
| 1006 | struct vm_area_struct **vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1007 | int *locked, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 1008 | unsigned int flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1009 | { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1010 | long ret, pages_done; |
| 1011 | bool lock_dropped; |
| 1012 | |
| 1013 | if (locked) { |
| 1014 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ |
| 1015 | BUG_ON(vmas); |
| 1016 | /* check caller initialized locked */ |
| 1017 | BUG_ON(*locked != 1); |
| 1018 | } |
| 1019 | |
| 1020 | if (pages) |
| 1021 | flags |= FOLL_GET; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1022 | |
| 1023 | pages_done = 0; |
| 1024 | lock_dropped = false; |
| 1025 | for (;;) { |
| 1026 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, |
| 1027 | vmas, locked); |
| 1028 | if (!locked) |
| 1029 | /* VM_FAULT_RETRY couldn't trigger, bypass */ |
| 1030 | return ret; |
| 1031 | |
| 1032 | /* VM_FAULT_RETRY cannot return errors */ |
| 1033 | if (!*locked) { |
| 1034 | BUG_ON(ret < 0); |
| 1035 | BUG_ON(ret >= nr_pages); |
| 1036 | } |
| 1037 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1038 | if (ret > 0) { |
| 1039 | nr_pages -= ret; |
| 1040 | pages_done += ret; |
| 1041 | if (!nr_pages) |
| 1042 | break; |
| 1043 | } |
| 1044 | if (*locked) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 1045 | /* |
| 1046 | * VM_FAULT_RETRY didn't trigger or it was a |
| 1047 | * FOLL_NOWAIT. |
| 1048 | */ |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1049 | if (!pages_done) |
| 1050 | pages_done = ret; |
| 1051 | break; |
| 1052 | } |
Mike Rapoport | df17277 | 2019-05-31 22:30:33 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * VM_FAULT_RETRY triggered, so seek to the faulting offset. |
| 1055 | * For the prefault case (!pages) we only update counts. |
| 1056 | */ |
| 1057 | if (likely(pages)) |
| 1058 | pages += ret; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1059 | start += ret << PAGE_SHIFT; |
| 1060 | |
| 1061 | /* |
| 1062 | * Repeat on the address that fired VM_FAULT_RETRY |
| 1063 | * without FAULT_FLAG_ALLOW_RETRY but with |
| 1064 | * FAULT_FLAG_TRIED. |
| 1065 | */ |
| 1066 | *locked = 1; |
| 1067 | lock_dropped = true; |
| 1068 | down_read(&mm->mmap_sem); |
| 1069 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, |
| 1070 | pages, NULL, NULL); |
| 1071 | if (ret != 1) { |
| 1072 | BUG_ON(ret > 1); |
| 1073 | if (!pages_done) |
| 1074 | pages_done = ret; |
| 1075 | break; |
| 1076 | } |
| 1077 | nr_pages--; |
| 1078 | pages_done++; |
| 1079 | if (!nr_pages) |
| 1080 | break; |
Mike Rapoport | df17277 | 2019-05-31 22:30:33 -0700 | [diff] [blame] | 1081 | if (likely(pages)) |
| 1082 | pages++; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1083 | start += PAGE_SIZE; |
| 1084 | } |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1085 | if (lock_dropped && *locked) { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1086 | /* |
| 1087 | * We must let the caller know we temporarily dropped the lock |
| 1088 | * and so the critical section protected by it was lost. |
| 1089 | */ |
| 1090 | up_read(&mm->mmap_sem); |
| 1091 | *locked = 0; |
| 1092 | } |
| 1093 | return pages_done; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * We can leverage the VM_FAULT_RETRY functionality in the page fault |
| 1098 | * paths better by using either get_user_pages_locked() or |
| 1099 | * get_user_pages_unlocked(). |
| 1100 | * |
| 1101 | * get_user_pages_locked() is suitable to replace the form: |
| 1102 | * |
| 1103 | * down_read(&mm->mmap_sem); |
| 1104 | * do_something() |
| 1105 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1106 | * up_read(&mm->mmap_sem); |
| 1107 | * |
| 1108 | * to: |
| 1109 | * |
| 1110 | * int locked = 1; |
| 1111 | * down_read(&mm->mmap_sem); |
| 1112 | * do_something() |
| 1113 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); |
| 1114 | * if (locked) |
| 1115 | * up_read(&mm->mmap_sem); |
| 1116 | */ |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1117 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | 3b91317 | 2016-10-13 01:20:14 +0100 | [diff] [blame] | 1118 | unsigned int gup_flags, struct page **pages, |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1119 | int *locked) |
| 1120 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1121 | /* |
| 1122 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1123 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1124 | * vmas. As there are no users of this flag in this call we simply |
| 1125 | * disallow this option for now. |
| 1126 | */ |
| 1127 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1128 | return -EINVAL; |
| 1129 | |
Dave Hansen | cde7014 | 2016-02-12 13:01:55 -0800 | [diff] [blame] | 1130 | return __get_user_pages_locked(current, current->mm, start, nr_pages, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1131 | pages, NULL, locked, |
Lorenzo Stoakes | 3b91317 | 2016-10-13 01:20:14 +0100 | [diff] [blame] | 1132 | gup_flags | FOLL_TOUCH); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1133 | } |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1134 | EXPORT_SYMBOL(get_user_pages_locked); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1135 | |
| 1136 | /* |
| 1137 | * get_user_pages_unlocked() is suitable to replace the form: |
| 1138 | * |
| 1139 | * down_read(&mm->mmap_sem); |
| 1140 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1141 | * up_read(&mm->mmap_sem); |
| 1142 | * |
| 1143 | * with: |
| 1144 | * |
| 1145 | * get_user_pages_unlocked(tsk, mm, ..., pages); |
| 1146 | * |
| 1147 | * It is functionally equivalent to get_user_pages_fast so |
Lorenzo Stoakes | 80a7951 | 2016-12-12 16:42:46 -0800 | [diff] [blame] | 1148 | * get_user_pages_fast should be used instead if specific gup_flags |
| 1149 | * (e.g. FOLL_FORCE) are not required. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1150 | */ |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1151 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | c164154 | 2016-10-13 01:20:13 +0100 | [diff] [blame] | 1152 | struct page **pages, unsigned int gup_flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1153 | { |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1154 | struct mm_struct *mm = current->mm; |
| 1155 | int locked = 1; |
| 1156 | long ret; |
| 1157 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1158 | /* |
| 1159 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1160 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1161 | * vmas. As there are no users of this flag in this call we simply |
| 1162 | * disallow this option for now. |
| 1163 | */ |
| 1164 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1165 | return -EINVAL; |
| 1166 | |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1167 | down_read(&mm->mmap_sem); |
| 1168 | ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1169 | &locked, gup_flags | FOLL_TOUCH); |
Al Viro | c803c9c | 2017-11-18 14:17:46 -0500 | [diff] [blame] | 1170 | if (locked) |
| 1171 | up_read(&mm->mmap_sem); |
| 1172 | return ret; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1173 | } |
Ingo Molnar | c12d2da | 2016-04-04 10:24:58 +0200 | [diff] [blame] | 1174 | EXPORT_SYMBOL(get_user_pages_unlocked); |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1175 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1176 | /* |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1177 | * get_user_pages_remote() - pin user pages in memory |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1178 | * @tsk: the task_struct to use for page fault accounting, or |
| 1179 | * NULL if faults are not to be recorded. |
| 1180 | * @mm: mm_struct of target mm |
| 1181 | * @start: starting user address |
| 1182 | * @nr_pages: number of pages from start to pin |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1183 | * @gup_flags: flags modifying lookup behaviour |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1184 | * @pages: array that receives pointers to the pages pinned. |
| 1185 | * Should be at least nr_pages long. Or NULL, if caller |
| 1186 | * only intends to ensure the pages are faulted in. |
| 1187 | * @vmas: array of pointers to vmas corresponding to each page. |
| 1188 | * Or NULL if the caller does not require them. |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1189 | * @locked: pointer to lock flag indicating whether lock is held and |
| 1190 | * subsequently whether VM_FAULT_RETRY functionality can be |
| 1191 | * utilised. Lock must initially be held. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1192 | * |
| 1193 | * Returns number of pages pinned. This may be fewer than the number |
| 1194 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 1195 | * were pinned, returns -errno. Each page returned must be released |
| 1196 | * with a put_page() call when it is finished with. vmas will only |
| 1197 | * remain valid while mmap_sem is held. |
| 1198 | * |
| 1199 | * Must be called with mmap_sem held for read or write. |
| 1200 | * |
| 1201 | * get_user_pages walks a process's page tables and takes a reference to |
| 1202 | * each struct page that each user address corresponds to at a given |
| 1203 | * instant. That is, it takes the page that would be accessed if a user |
| 1204 | * thread accesses the given user virtual address at that instant. |
| 1205 | * |
| 1206 | * This does not guarantee that the page exists in the user mappings when |
| 1207 | * get_user_pages returns, and there may even be a completely different |
| 1208 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 1209 | * and subsequently re faulted). However it does guarantee that the page |
| 1210 | * won't be freed completely. And mostly callers simply care that the page |
| 1211 | * contains data that was valid *at some point in time*. Typically, an IO |
| 1212 | * or similar operation cannot guarantee anything stronger anyway because |
| 1213 | * locks can't be held over the syscall boundary. |
| 1214 | * |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1215 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page |
| 1216 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must |
| 1217 | * be called after the page is finished with, and before put_page is called. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1218 | * |
| 1219 | * get_user_pages is typically used for fewer-copy IO operations, to get a |
| 1220 | * handle on the memory by some means other than accesses via the user virtual |
| 1221 | * addresses. The pages may be submitted for DMA to devices or accessed via |
| 1222 | * their kernel linear mapping (via the kmap APIs). Care should be taken to |
| 1223 | * use the correct cache flushing APIs. |
| 1224 | * |
| 1225 | * See also get_user_pages_fast, for performance critical applications. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1226 | * |
| 1227 | * get_user_pages should be phased out in favor of |
| 1228 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
| 1229 | * should use get_user_pages because it cannot pass |
| 1230 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1231 | */ |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1232 | long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, |
| 1233 | unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1234 | unsigned int gup_flags, struct page **pages, |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1235 | struct vm_area_struct **vmas, int *locked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1236 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1239 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1240 | * vmas. As there are no users of this flag in this call we simply |
| 1241 | * disallow this option for now. |
| 1242 | */ |
| 1243 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1244 | return -EINVAL; |
| 1245 | |
Lorenzo Stoakes | 859110d | 2016-10-13 01:20:11 +0100 | [diff] [blame] | 1246 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1247 | locked, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1248 | gup_flags | FOLL_TOUCH | FOLL_REMOTE); |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1249 | } |
| 1250 | EXPORT_SYMBOL(get_user_pages_remote); |
| 1251 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1252 | #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1253 | static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages) |
| 1254 | { |
| 1255 | long i; |
| 1256 | struct vm_area_struct *vma_prev = NULL; |
| 1257 | |
| 1258 | for (i = 0; i < nr_pages; i++) { |
| 1259 | struct vm_area_struct *vma = vmas[i]; |
| 1260 | |
| 1261 | if (vma == vma_prev) |
| 1262 | continue; |
| 1263 | |
| 1264 | vma_prev = vma; |
| 1265 | |
| 1266 | if (vma_is_fsdax(vma)) |
| 1267 | return true; |
| 1268 | } |
| 1269 | return false; |
| 1270 | } |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1271 | |
| 1272 | #ifdef CONFIG_CMA |
| 1273 | static struct page *new_non_cma_page(struct page *page, unsigned long private) |
| 1274 | { |
| 1275 | /* |
| 1276 | * We want to make sure we allocate the new page from the same node |
| 1277 | * as the source page. |
| 1278 | */ |
| 1279 | int nid = page_to_nid(page); |
| 1280 | /* |
| 1281 | * Trying to allocate a page for migration. Ignore allocation |
| 1282 | * failure warnings. We don't force __GFP_THISNODE here because |
| 1283 | * this node here is the node where we have CMA reservation and |
| 1284 | * in some case these nodes will have really less non movable |
| 1285 | * allocation memory. |
| 1286 | */ |
| 1287 | gfp_t gfp_mask = GFP_USER | __GFP_NOWARN; |
| 1288 | |
| 1289 | if (PageHighMem(page)) |
| 1290 | gfp_mask |= __GFP_HIGHMEM; |
| 1291 | |
| 1292 | #ifdef CONFIG_HUGETLB_PAGE |
| 1293 | if (PageHuge(page)) { |
| 1294 | struct hstate *h = page_hstate(page); |
| 1295 | /* |
| 1296 | * We don't want to dequeue from the pool because pool pages will |
| 1297 | * mostly be from the CMA region. |
| 1298 | */ |
| 1299 | return alloc_migrate_huge_page(h, gfp_mask, nid, NULL); |
| 1300 | } |
| 1301 | #endif |
| 1302 | if (PageTransHuge(page)) { |
| 1303 | struct page *thp; |
| 1304 | /* |
| 1305 | * ignore allocation failure warnings |
| 1306 | */ |
| 1307 | gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN; |
| 1308 | |
| 1309 | /* |
| 1310 | * Remove the movable mask so that we don't allocate from |
| 1311 | * CMA area again. |
| 1312 | */ |
| 1313 | thp_gfpmask &= ~__GFP_MOVABLE; |
| 1314 | thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER); |
| 1315 | if (!thp) |
| 1316 | return NULL; |
| 1317 | prep_transhuge_page(thp); |
| 1318 | return thp; |
| 1319 | } |
| 1320 | |
| 1321 | return __alloc_pages_node(nid, gfp_mask, 0); |
| 1322 | } |
| 1323 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1324 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1325 | struct mm_struct *mm, |
| 1326 | unsigned long start, |
| 1327 | unsigned long nr_pages, |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1328 | struct page **pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1329 | struct vm_area_struct **vmas, |
| 1330 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1331 | { |
| 1332 | long i; |
| 1333 | bool drain_allow = true; |
| 1334 | bool migrate_allow = true; |
| 1335 | LIST_HEAD(cma_page_list); |
| 1336 | |
| 1337 | check_again: |
| 1338 | for (i = 0; i < nr_pages; i++) { |
| 1339 | /* |
| 1340 | * If we get a page from the CMA zone, since we are going to |
| 1341 | * be pinning these entries, we might as well move them out |
| 1342 | * of the CMA zone if possible. |
| 1343 | */ |
| 1344 | if (is_migrate_cma_page(pages[i])) { |
| 1345 | |
| 1346 | struct page *head = compound_head(pages[i]); |
| 1347 | |
| 1348 | if (PageHuge(head)) { |
| 1349 | isolate_huge_page(head, &cma_page_list); |
| 1350 | } else { |
| 1351 | if (!PageLRU(head) && drain_allow) { |
| 1352 | lru_add_drain_all(); |
| 1353 | drain_allow = false; |
| 1354 | } |
| 1355 | |
| 1356 | if (!isolate_lru_page(head)) { |
| 1357 | list_add_tail(&head->lru, &cma_page_list); |
| 1358 | mod_node_page_state(page_pgdat(head), |
| 1359 | NR_ISOLATED_ANON + |
| 1360 | page_is_file_cache(head), |
| 1361 | hpage_nr_pages(head)); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | if (!list_empty(&cma_page_list)) { |
| 1368 | /* |
| 1369 | * drop the above get_user_pages reference. |
| 1370 | */ |
| 1371 | for (i = 0; i < nr_pages; i++) |
| 1372 | put_page(pages[i]); |
| 1373 | |
| 1374 | if (migrate_pages(&cma_page_list, new_non_cma_page, |
| 1375 | NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) { |
| 1376 | /* |
| 1377 | * some of the pages failed migration. Do get_user_pages |
| 1378 | * without migration. |
| 1379 | */ |
| 1380 | migrate_allow = false; |
| 1381 | |
| 1382 | if (!list_empty(&cma_page_list)) |
| 1383 | putback_movable_pages(&cma_page_list); |
| 1384 | } |
| 1385 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1386 | * We did migrate all the pages, Try to get the page references |
| 1387 | * again migrating any new CMA pages which we failed to isolate |
| 1388 | * earlier. |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1389 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1390 | nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages, |
| 1391 | pages, vmas, NULL, |
| 1392 | gup_flags); |
| 1393 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1394 | if ((nr_pages > 0) && migrate_allow) { |
| 1395 | drain_allow = true; |
| 1396 | goto check_again; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | return nr_pages; |
| 1401 | } |
| 1402 | #else |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1403 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1404 | struct mm_struct *mm, |
| 1405 | unsigned long start, |
| 1406 | unsigned long nr_pages, |
| 1407 | struct page **pages, |
| 1408 | struct vm_area_struct **vmas, |
| 1409 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1410 | { |
| 1411 | return nr_pages; |
| 1412 | } |
| 1413 | #endif |
| 1414 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1415 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1416 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
| 1417 | * allows us to process the FOLL_LONGTERM flag. |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1418 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1419 | static long __gup_longterm_locked(struct task_struct *tsk, |
| 1420 | struct mm_struct *mm, |
| 1421 | unsigned long start, |
| 1422 | unsigned long nr_pages, |
| 1423 | struct page **pages, |
| 1424 | struct vm_area_struct **vmas, |
| 1425 | unsigned int gup_flags) |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1426 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1427 | struct vm_area_struct **vmas_tmp = vmas; |
| 1428 | unsigned long flags = 0; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1429 | long rc, i; |
| 1430 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1431 | if (gup_flags & FOLL_LONGTERM) { |
| 1432 | if (!pages) |
| 1433 | return -EINVAL; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1434 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1435 | if (!vmas_tmp) { |
| 1436 | vmas_tmp = kcalloc(nr_pages, |
| 1437 | sizeof(struct vm_area_struct *), |
| 1438 | GFP_KERNEL); |
| 1439 | if (!vmas_tmp) |
| 1440 | return -ENOMEM; |
| 1441 | } |
| 1442 | flags = memalloc_nocma_save(); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1445 | rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, |
| 1446 | vmas_tmp, NULL, gup_flags); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1447 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1448 | if (gup_flags & FOLL_LONGTERM) { |
| 1449 | memalloc_nocma_restore(flags); |
| 1450 | if (rc < 0) |
| 1451 | goto out; |
| 1452 | |
| 1453 | if (check_dax_vmas(vmas_tmp, rc)) { |
| 1454 | for (i = 0; i < rc; i++) |
| 1455 | put_page(pages[i]); |
| 1456 | rc = -EOPNOTSUPP; |
| 1457 | goto out; |
| 1458 | } |
| 1459 | |
| 1460 | rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages, |
| 1461 | vmas_tmp, gup_flags); |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1464 | out: |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1465 | if (vmas_tmp != vmas) |
| 1466 | kfree(vmas_tmp); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1467 | return rc; |
| 1468 | } |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1469 | #else /* !CONFIG_FS_DAX && !CONFIG_CMA */ |
| 1470 | static __always_inline long __gup_longterm_locked(struct task_struct *tsk, |
| 1471 | struct mm_struct *mm, |
| 1472 | unsigned long start, |
| 1473 | unsigned long nr_pages, |
| 1474 | struct page **pages, |
| 1475 | struct vm_area_struct **vmas, |
| 1476 | unsigned int flags) |
| 1477 | { |
| 1478 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
| 1479 | NULL, flags); |
| 1480 | } |
| 1481 | #endif /* CONFIG_FS_DAX || CONFIG_CMA */ |
| 1482 | |
| 1483 | /* |
| 1484 | * This is the same as get_user_pages_remote(), just with a |
| 1485 | * less-flexible calling convention where we assume that the task |
| 1486 | * and mm being operated on are the current task's and don't allow |
| 1487 | * passing of a locked parameter. We also obviously don't pass |
| 1488 | * FOLL_REMOTE in here. |
| 1489 | */ |
| 1490 | long get_user_pages(unsigned long start, unsigned long nr_pages, |
| 1491 | unsigned int gup_flags, struct page **pages, |
| 1492 | struct vm_area_struct **vmas) |
| 1493 | { |
| 1494 | return __gup_longterm_locked(current, current->mm, start, nr_pages, |
| 1495 | pages, vmas, gup_flags | FOLL_TOUCH); |
| 1496 | } |
| 1497 | EXPORT_SYMBOL(get_user_pages); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1498 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1499 | /** |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1500 | * populate_vma_page_range() - populate a range of pages in the vma. |
| 1501 | * @vma: target vma |
| 1502 | * @start: start address |
| 1503 | * @end: end address |
| 1504 | * @nonblocking: |
| 1505 | * |
| 1506 | * This takes care of mlocking the pages too if VM_LOCKED is set. |
| 1507 | * |
| 1508 | * return 0 on success, negative error code on error. |
| 1509 | * |
| 1510 | * vma->vm_mm->mmap_sem must be held. |
| 1511 | * |
| 1512 | * If @nonblocking is NULL, it may be held for read or write and will |
| 1513 | * be unperturbed. |
| 1514 | * |
| 1515 | * If @nonblocking is non-NULL, it must held for read only and may be |
| 1516 | * released. If it's released, *@nonblocking will be set to 0. |
| 1517 | */ |
| 1518 | long populate_vma_page_range(struct vm_area_struct *vma, |
| 1519 | unsigned long start, unsigned long end, int *nonblocking) |
| 1520 | { |
| 1521 | struct mm_struct *mm = vma->vm_mm; |
| 1522 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
| 1523 | int gup_flags; |
| 1524 | |
| 1525 | VM_BUG_ON(start & ~PAGE_MASK); |
| 1526 | VM_BUG_ON(end & ~PAGE_MASK); |
| 1527 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
| 1528 | VM_BUG_ON_VMA(end > vma->vm_end, vma); |
| 1529 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); |
| 1530 | |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 1531 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; |
| 1532 | if (vma->vm_flags & VM_LOCKONFAULT) |
| 1533 | gup_flags &= ~FOLL_POPULATE; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * We want to touch writable mappings with a write fault in order |
| 1536 | * to break COW, except for shared mappings because these don't COW |
| 1537 | * and we would not want to dirty them for nothing. |
| 1538 | */ |
| 1539 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) |
| 1540 | gup_flags |= FOLL_WRITE; |
| 1541 | |
| 1542 | /* |
| 1543 | * We want mlock to succeed for regions that have any permissions |
| 1544 | * other than PROT_NONE. |
| 1545 | */ |
| 1546 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) |
| 1547 | gup_flags |= FOLL_FORCE; |
| 1548 | |
| 1549 | /* |
| 1550 | * We made sure addr is within a VMA, so the following will |
| 1551 | * not result in a stack expansion that recurses back here. |
| 1552 | */ |
| 1553 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, |
| 1554 | NULL, NULL, nonblocking); |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * __mm_populate - populate and/or mlock pages within a range of address space. |
| 1559 | * |
| 1560 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap |
| 1561 | * flags. VMAs must be already marked with the desired vm_flags, and |
| 1562 | * mmap_sem must not be held. |
| 1563 | */ |
| 1564 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) |
| 1565 | { |
| 1566 | struct mm_struct *mm = current->mm; |
| 1567 | unsigned long end, nstart, nend; |
| 1568 | struct vm_area_struct *vma = NULL; |
| 1569 | int locked = 0; |
| 1570 | long ret = 0; |
| 1571 | |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1572 | end = start + len; |
| 1573 | |
| 1574 | for (nstart = start; nstart < end; nstart = nend) { |
| 1575 | /* |
| 1576 | * We want to fault in pages for [nstart; end) address range. |
| 1577 | * Find first corresponding VMA. |
| 1578 | */ |
| 1579 | if (!locked) { |
| 1580 | locked = 1; |
| 1581 | down_read(&mm->mmap_sem); |
| 1582 | vma = find_vma(mm, nstart); |
| 1583 | } else if (nstart >= vma->vm_end) |
| 1584 | vma = vma->vm_next; |
| 1585 | if (!vma || vma->vm_start >= end) |
| 1586 | break; |
| 1587 | /* |
| 1588 | * Set [nstart; nend) to intersection of desired address |
| 1589 | * range with the first VMA. Also, skip undesirable VMA types. |
| 1590 | */ |
| 1591 | nend = min(end, vma->vm_end); |
| 1592 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) |
| 1593 | continue; |
| 1594 | if (nstart < vma->vm_start) |
| 1595 | nstart = vma->vm_start; |
| 1596 | /* |
| 1597 | * Now fault in a range of pages. populate_vma_page_range() |
| 1598 | * double checks the vma flags, so that it won't mlock pages |
| 1599 | * if the vma was already munlocked. |
| 1600 | */ |
| 1601 | ret = populate_vma_page_range(vma, nstart, nend, &locked); |
| 1602 | if (ret < 0) { |
| 1603 | if (ignore_errors) { |
| 1604 | ret = 0; |
| 1605 | continue; /* continue at next VMA */ |
| 1606 | } |
| 1607 | break; |
| 1608 | } |
| 1609 | nend = nstart + ret * PAGE_SIZE; |
| 1610 | ret = 0; |
| 1611 | } |
| 1612 | if (locked) |
| 1613 | up_read(&mm->mmap_sem); |
| 1614 | return ret; /* 0 or negative error code */ |
| 1615 | } |
| 1616 | |
| 1617 | /** |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1618 | * get_dump_page() - pin user page in memory while writing it to core dump |
| 1619 | * @addr: user address |
| 1620 | * |
| 1621 | * Returns struct page pointer of user page pinned for dump, |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 1622 | * to be freed afterwards by put_page(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1623 | * |
| 1624 | * Returns NULL on any kind of failure - a hole must then be inserted into |
| 1625 | * the corefile, to preserve alignment with its headers; and also returns |
| 1626 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - |
| 1627 | * allowing a hole to be left in the corefile to save diskspace. |
| 1628 | * |
| 1629 | * Called without mmap_sem, but after all other threads have been killed. |
| 1630 | */ |
| 1631 | #ifdef CONFIG_ELF_CORE |
| 1632 | struct page *get_dump_page(unsigned long addr) |
| 1633 | { |
| 1634 | struct vm_area_struct *vma; |
| 1635 | struct page *page; |
| 1636 | |
| 1637 | if (__get_user_pages(current, current->mm, addr, 1, |
| 1638 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, |
| 1639 | NULL) < 1) |
| 1640 | return NULL; |
| 1641 | flush_cache_page(vma, addr, page_to_pfn(page)); |
| 1642 | return page; |
| 1643 | } |
| 1644 | #endif /* CONFIG_ELF_CORE */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1645 | |
| 1646 | /* |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1647 | * Generic Fast GUP |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1648 | * |
| 1649 | * get_user_pages_fast attempts to pin user pages by walking the page |
| 1650 | * tables directly and avoids taking locks. Thus the walker needs to be |
| 1651 | * protected from page table pages being freed from under it, and should |
| 1652 | * block any THP splits. |
| 1653 | * |
| 1654 | * One way to achieve this is to have the walker disable interrupts, and |
| 1655 | * rely on IPIs from the TLB flushing code blocking before the page table |
| 1656 | * pages are freed. This is unsuitable for architectures that do not need |
| 1657 | * to broadcast an IPI when invalidating TLBs. |
| 1658 | * |
| 1659 | * Another way to achieve this is to batch up page table containing pages |
| 1660 | * belonging to more than one mm_user, then rcu_sched a callback to free those |
| 1661 | * pages. Disabling interrupts will allow the fast_gup walker to both block |
| 1662 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs |
| 1663 | * (which is a relatively rare event). The code below adopts this strategy. |
| 1664 | * |
| 1665 | * Before activating this code, please be aware that the following assumptions |
| 1666 | * are currently made: |
| 1667 | * |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1668 | * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
| 1669 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1670 | * |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1671 | * *) ptes can be read atomically by the architecture. |
| 1672 | * |
| 1673 | * *) access_ok is sufficient to validate userspace address ranges. |
| 1674 | * |
| 1675 | * The last two assumptions can be relaxed by the addition of helper functions. |
| 1676 | * |
| 1677 | * This code is based heavily on the PowerPC implementation by Nick Piggin. |
| 1678 | */ |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1679 | #ifdef CONFIG_HAVE_GENERIC_GUP |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1680 | |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1681 | #ifndef gup_get_pte |
| 1682 | /* |
| 1683 | * We assume that the PTE can be read atomically. If this is not the case for |
| 1684 | * your architecture, please provide the helper. |
| 1685 | */ |
| 1686 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 1687 | { |
| 1688 | return READ_ONCE(*ptep); |
| 1689 | } |
| 1690 | #endif |
| 1691 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1692 | static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages) |
| 1693 | { |
| 1694 | while ((*nr) - nr_start) { |
| 1695 | struct page *page = pages[--(*nr)]; |
| 1696 | |
| 1697 | ClearPageReferenced(page); |
| 1698 | put_page(page); |
| 1699 | } |
| 1700 | } |
| 1701 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1702 | /* |
| 1703 | * Return the compund head page with ref appropriately incremented, |
| 1704 | * or NULL if that failed. |
| 1705 | */ |
| 1706 | static inline struct page *try_get_compound_head(struct page *page, int refs) |
| 1707 | { |
| 1708 | struct page *head = compound_head(page); |
| 1709 | if (WARN_ON_ONCE(page_ref_count(head) < 0)) |
| 1710 | return NULL; |
| 1711 | if (unlikely(!page_cache_add_speculative(head, refs))) |
| 1712 | return NULL; |
| 1713 | return head; |
| 1714 | } |
| 1715 | |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1716 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1717 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1718 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1719 | { |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1720 | struct dev_pagemap *pgmap = NULL; |
| 1721 | int nr_start = *nr, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1722 | pte_t *ptep, *ptem; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1723 | |
| 1724 | ptem = ptep = pte_offset_map(&pmd, addr); |
| 1725 | do { |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1726 | pte_t pte = gup_get_pte(ptep); |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1727 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * Similar to the PMD case below, NUMA hinting must take slow |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1731 | * path using the pte_protnone check. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1732 | */ |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1733 | if (pte_protnone(pte)) |
| 1734 | goto pte_unmap; |
| 1735 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1736 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1737 | goto pte_unmap; |
| 1738 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1739 | if (pte_devmap(pte)) { |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1740 | if (unlikely(flags & FOLL_LONGTERM)) |
| 1741 | goto pte_unmap; |
| 1742 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1743 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
| 1744 | if (unlikely(!pgmap)) { |
| 1745 | undo_dev_pagemap(nr, nr_start, pages); |
| 1746 | goto pte_unmap; |
| 1747 | } |
| 1748 | } else if (pte_special(pte)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1749 | goto pte_unmap; |
| 1750 | |
| 1751 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 1752 | page = pte_page(pte); |
| 1753 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1754 | head = try_get_compound_head(page, 1); |
| 1755 | if (!head) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1756 | goto pte_unmap; |
| 1757 | |
| 1758 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1759 | put_page(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1760 | goto pte_unmap; |
| 1761 | } |
| 1762 | |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1763 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1764 | |
| 1765 | SetPageReferenced(page); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1766 | pages[*nr] = page; |
| 1767 | (*nr)++; |
| 1768 | |
| 1769 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
| 1770 | |
| 1771 | ret = 1; |
| 1772 | |
| 1773 | pte_unmap: |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1774 | if (pgmap) |
| 1775 | put_dev_pagemap(pgmap); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1776 | pte_unmap(ptem); |
| 1777 | return ret; |
| 1778 | } |
| 1779 | #else |
| 1780 | |
| 1781 | /* |
| 1782 | * If we can't determine whether or not a pte is special, then fail immediately |
| 1783 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not |
| 1784 | * to be special. |
| 1785 | * |
| 1786 | * For a futex to be placed on a THP tail page, get_futex_key requires a |
| 1787 | * __get_user_pages_fast implementation that can pin pages. Thus it's still |
| 1788 | * useful to have gup_huge_pmd even if we can't operate on ptes. |
| 1789 | */ |
| 1790 | static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1791 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1792 | { |
| 1793 | return 0; |
| 1794 | } |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1795 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1796 | |
Oliver O'Halloran | 09180ca | 2017-09-06 16:20:58 -0700 | [diff] [blame] | 1797 | #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1798 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
| 1799 | unsigned long end, struct page **pages, int *nr) |
| 1800 | { |
| 1801 | int nr_start = *nr; |
| 1802 | struct dev_pagemap *pgmap = NULL; |
| 1803 | |
| 1804 | do { |
| 1805 | struct page *page = pfn_to_page(pfn); |
| 1806 | |
| 1807 | pgmap = get_dev_pagemap(pfn, pgmap); |
| 1808 | if (unlikely(!pgmap)) { |
| 1809 | undo_dev_pagemap(nr, nr_start, pages); |
| 1810 | return 0; |
| 1811 | } |
| 1812 | SetPageReferenced(page); |
| 1813 | pages[*nr] = page; |
| 1814 | get_page(page); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1815 | (*nr)++; |
| 1816 | pfn++; |
| 1817 | } while (addr += PAGE_SIZE, addr != end); |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1818 | |
| 1819 | if (pgmap) |
| 1820 | put_dev_pagemap(pgmap); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1821 | return 1; |
| 1822 | } |
| 1823 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1824 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1825 | unsigned long end, struct page **pages, int *nr) |
| 1826 | { |
| 1827 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1828 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1829 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1830 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 1831 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1832 | return 0; |
| 1833 | |
| 1834 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1835 | undo_dev_pagemap(nr, nr_start, pages); |
| 1836 | return 0; |
| 1837 | } |
| 1838 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1839 | } |
| 1840 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1841 | static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1842 | unsigned long end, struct page **pages, int *nr) |
| 1843 | { |
| 1844 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1845 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1846 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1847 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 1848 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1849 | return 0; |
| 1850 | |
| 1851 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1852 | undo_dev_pagemap(nr, nr_start, pages); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1856 | } |
| 1857 | #else |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1858 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1859 | unsigned long end, struct page **pages, int *nr) |
| 1860 | { |
| 1861 | BUILD_BUG(); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1865 | static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1866 | unsigned long end, struct page **pages, int *nr) |
| 1867 | { |
| 1868 | BUILD_BUG(); |
| 1869 | return 0; |
| 1870 | } |
| 1871 | #endif |
| 1872 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1873 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1874 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1875 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1876 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1877 | int refs; |
| 1878 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1879 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1880 | return 0; |
| 1881 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1882 | if (pmd_devmap(orig)) { |
| 1883 | if (unlikely(flags & FOLL_LONGTERM)) |
| 1884 | return 0; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1885 | return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr); |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1886 | } |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1887 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1888 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1889 | page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1890 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1891 | pages[*nr] = page; |
| 1892 | (*nr)++; |
| 1893 | page++; |
| 1894 | refs++; |
| 1895 | } while (addr += PAGE_SIZE, addr != end); |
| 1896 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1897 | head = try_get_compound_head(pmd_page(orig), refs); |
| 1898 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1899 | *nr -= refs; |
| 1900 | return 0; |
| 1901 | } |
| 1902 | |
| 1903 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1904 | *nr -= refs; |
| 1905 | while (refs--) |
| 1906 | put_page(head); |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1910 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1911 | return 1; |
| 1912 | } |
| 1913 | |
| 1914 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1915 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1916 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1917 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1918 | int refs; |
| 1919 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1920 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1921 | return 0; |
| 1922 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1923 | if (pud_devmap(orig)) { |
| 1924 | if (unlikely(flags & FOLL_LONGTERM)) |
| 1925 | return 0; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1926 | return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr); |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1927 | } |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1928 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1929 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1930 | page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1931 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1932 | pages[*nr] = page; |
| 1933 | (*nr)++; |
| 1934 | page++; |
| 1935 | refs++; |
| 1936 | } while (addr += PAGE_SIZE, addr != end); |
| 1937 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1938 | head = try_get_compound_head(pud_page(orig), refs); |
| 1939 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1940 | *nr -= refs; |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1945 | *nr -= refs; |
| 1946 | while (refs--) |
| 1947 | put_page(head); |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1951 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1952 | return 1; |
| 1953 | } |
| 1954 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1955 | static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1956 | unsigned long end, unsigned int flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1957 | struct page **pages, int *nr) |
| 1958 | { |
| 1959 | int refs; |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1960 | struct page *head, *page; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1961 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1962 | if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1963 | return 0; |
| 1964 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1965 | BUILD_BUG_ON(pgd_devmap(orig)); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1966 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1967 | page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1968 | do { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1969 | pages[*nr] = page; |
| 1970 | (*nr)++; |
| 1971 | page++; |
| 1972 | refs++; |
| 1973 | } while (addr += PAGE_SIZE, addr != end); |
| 1974 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1975 | head = try_get_compound_head(pgd_page(orig), refs); |
| 1976 | if (!head) { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1977 | *nr -= refs; |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { |
| 1982 | *nr -= refs; |
| 1983 | while (refs--) |
| 1984 | put_page(head); |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1988 | SetPageReferenced(head); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 1989 | return 1; |
| 1990 | } |
| 1991 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1992 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1993 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1994 | { |
| 1995 | unsigned long next; |
| 1996 | pmd_t *pmdp; |
| 1997 | |
| 1998 | pmdp = pmd_offset(&pud, addr); |
| 1999 | do { |
Christian Borntraeger | 38c5ce9 | 2015-01-06 22:54:46 +0100 | [diff] [blame] | 2000 | pmd_t pmd = READ_ONCE(*pmdp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2001 | |
| 2002 | next = pmd_addr_end(addr, end); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 2003 | if (!pmd_present(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2004 | return 0; |
| 2005 | |
Yu Zhao | 414fd08 | 2019-02-12 15:35:58 -0800 | [diff] [blame] | 2006 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || |
| 2007 | pmd_devmap(pmd))) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2008 | /* |
| 2009 | * NUMA hinting faults need to be handled in the GUP |
| 2010 | * slowpath for accounting purposes and so that they |
| 2011 | * can be serialised against THP migration. |
| 2012 | */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 2013 | if (pmd_protnone(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2014 | return 0; |
| 2015 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2016 | if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2017 | pages, nr)) |
| 2018 | return 0; |
| 2019 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2020 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
| 2021 | /* |
| 2022 | * architecture have different format for hugetlbfs |
| 2023 | * pmd format and THP pmd format |
| 2024 | */ |
| 2025 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2026 | PMD_SHIFT, next, flags, pages, nr)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2027 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2028 | } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 2029 | return 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2030 | } while (pmdp++, addr = next, addr != end); |
| 2031 | |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2035 | static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2036 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2037 | { |
| 2038 | unsigned long next; |
| 2039 | pud_t *pudp; |
| 2040 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2041 | pudp = pud_offset(&p4d, addr); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2042 | do { |
Christian Borntraeger | e37c698 | 2014-12-07 21:41:33 +0100 | [diff] [blame] | 2043 | pud_t pud = READ_ONCE(*pudp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2044 | |
| 2045 | next = pud_addr_end(addr, end); |
| 2046 | if (pud_none(pud)) |
| 2047 | return 0; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2048 | if (unlikely(pud_huge(pud))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2049 | if (!gup_huge_pud(pud, pudp, addr, next, flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2050 | pages, nr)) |
| 2051 | return 0; |
| 2052 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { |
| 2053 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2054 | PUD_SHIFT, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2055 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2056 | } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2057 | return 0; |
| 2058 | } while (pudp++, addr = next, addr != end); |
| 2059 | |
| 2060 | return 1; |
| 2061 | } |
| 2062 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2063 | static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2064 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2065 | { |
| 2066 | unsigned long next; |
| 2067 | p4d_t *p4dp; |
| 2068 | |
| 2069 | p4dp = p4d_offset(&pgd, addr); |
| 2070 | do { |
| 2071 | p4d_t p4d = READ_ONCE(*p4dp); |
| 2072 | |
| 2073 | next = p4d_addr_end(addr, end); |
| 2074 | if (p4d_none(p4d)) |
| 2075 | return 0; |
| 2076 | BUILD_BUG_ON(p4d_huge(p4d)); |
| 2077 | if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { |
| 2078 | if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2079 | P4D_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2080 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2081 | } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2082 | return 0; |
| 2083 | } while (p4dp++, addr = next, addr != end); |
| 2084 | |
| 2085 | return 1; |
| 2086 | } |
| 2087 | |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2088 | static void gup_pgd_range(unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2089 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2090 | { |
| 2091 | unsigned long next; |
| 2092 | pgd_t *pgdp; |
| 2093 | |
| 2094 | pgdp = pgd_offset(current->mm, addr); |
| 2095 | do { |
| 2096 | pgd_t pgd = READ_ONCE(*pgdp); |
| 2097 | |
| 2098 | next = pgd_addr_end(addr, end); |
| 2099 | if (pgd_none(pgd)) |
| 2100 | return; |
| 2101 | if (unlikely(pgd_huge(pgd))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2102 | if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2103 | pages, nr)) |
| 2104 | return; |
| 2105 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { |
| 2106 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2107 | PGDIR_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2108 | return; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2109 | } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2110 | return; |
| 2111 | } while (pgdp++, addr = next, addr != end); |
| 2112 | } |
| 2113 | |
| 2114 | #ifndef gup_fast_permitted |
| 2115 | /* |
| 2116 | * Check if it's allowed to use __get_user_pages_fast() for the range, or |
| 2117 | * we need to fall back to the slow version: |
| 2118 | */ |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2119 | bool gup_fast_permitted(unsigned long start, int nr_pages) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2120 | { |
| 2121 | unsigned long len, end; |
| 2122 | |
| 2123 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2124 | end = start + len; |
| 2125 | return end >= start; |
| 2126 | } |
| 2127 | #endif |
| 2128 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2129 | /* |
| 2130 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
Michael S. Tsirkin | d081107 | 2018-04-13 15:35:23 -0700 | [diff] [blame] | 2131 | * the regular GUP. |
| 2132 | * Note a difference with get_user_pages_fast: this always returns the |
| 2133 | * number of pages pinned, 0 if no pages were pinned. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2134 | */ |
| 2135 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 2136 | struct page **pages) |
| 2137 | { |
Wei Yang | d4faa40 | 2018-10-26 15:07:55 -0700 | [diff] [blame] | 2138 | unsigned long len, end; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2139 | unsigned long flags; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2140 | int nr = 0; |
| 2141 | |
| 2142 | start &= PAGE_MASK; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2143 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2144 | end = start + len; |
| 2145 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2146 | if (unlikely(!access_ok((void __user *)start, len))) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2147 | return 0; |
| 2148 | |
| 2149 | /* |
| 2150 | * Disable interrupts. We use the nested form as we can already have |
| 2151 | * interrupts disabled by get_futex_key. |
| 2152 | * |
| 2153 | * With interrupts disabled, we block page table pages from being |
Fengguang Wu | 2ebe822 | 2018-10-30 15:10:51 -0700 | [diff] [blame] | 2154 | * freed from under us. See struct mmu_table_batch comments in |
| 2155 | * include/asm-generic/tlb.h for more details. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2156 | * |
| 2157 | * We do not adopt an rcu_read_lock(.) here as we also want to |
| 2158 | * block IPIs that come from THPs splitting. |
| 2159 | */ |
| 2160 | |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2161 | if (gup_fast_permitted(start, nr_pages)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2162 | local_irq_save(flags); |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2163 | gup_pgd_range(start, end, write ? FOLL_WRITE : 0, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2164 | local_irq_restore(flags); |
| 2165 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2166 | |
| 2167 | return nr; |
| 2168 | } |
| 2169 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2170 | static int __gup_longterm_unlocked(unsigned long start, int nr_pages, |
| 2171 | unsigned int gup_flags, struct page **pages) |
| 2172 | { |
| 2173 | int ret; |
| 2174 | |
| 2175 | /* |
| 2176 | * FIXME: FOLL_LONGTERM does not work with |
| 2177 | * get_user_pages_unlocked() (see comments in that function) |
| 2178 | */ |
| 2179 | if (gup_flags & FOLL_LONGTERM) { |
| 2180 | down_read(¤t->mm->mmap_sem); |
| 2181 | ret = __gup_longterm_locked(current, current->mm, |
| 2182 | start, nr_pages, |
| 2183 | pages, NULL, gup_flags); |
| 2184 | up_read(¤t->mm->mmap_sem); |
| 2185 | } else { |
| 2186 | ret = get_user_pages_unlocked(start, nr_pages, |
| 2187 | pages, gup_flags); |
| 2188 | } |
| 2189 | |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2193 | /** |
| 2194 | * get_user_pages_fast() - pin user pages in memory |
| 2195 | * @start: starting user address |
| 2196 | * @nr_pages: number of pages from start to pin |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2197 | * @gup_flags: flags modifying pin behaviour |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2198 | * @pages: array that receives pointers to the pages pinned. |
| 2199 | * Should be at least nr_pages long. |
| 2200 | * |
| 2201 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 2202 | * If not successful, it will fall back to taking the lock and |
| 2203 | * calling get_user_pages(). |
| 2204 | * |
| 2205 | * Returns number of pages pinned. This may be fewer than the number |
| 2206 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 2207 | * were pinned, returns -errno. |
| 2208 | */ |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2209 | int get_user_pages_fast(unsigned long start, int nr_pages, |
| 2210 | unsigned int gup_flags, struct page **pages) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2211 | { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2212 | unsigned long addr, len, end; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2213 | int nr = 0, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2214 | |
| 2215 | start &= PAGE_MASK; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2216 | addr = start; |
| 2217 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2218 | end = start + len; |
| 2219 | |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2220 | if (nr_pages <= 0) |
| 2221 | return 0; |
| 2222 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2223 | if (unlikely(!access_ok((void __user *)start, len))) |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2224 | return -EFAULT; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2225 | |
Ira Weiny | ad8cfb9 | 2019-02-10 14:34:24 -0800 | [diff] [blame] | 2226 | if (gup_fast_permitted(start, nr_pages)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2227 | local_irq_disable(); |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2228 | gup_pgd_range(addr, end, gup_flags, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2229 | local_irq_enable(); |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2230 | ret = nr; |
| 2231 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2232 | |
| 2233 | if (nr < nr_pages) { |
| 2234 | /* Try to get the remaining pages with get_user_pages */ |
| 2235 | start += nr << PAGE_SHIFT; |
| 2236 | pages += nr; |
| 2237 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2238 | ret = __gup_longterm_unlocked(start, nr_pages - nr, |
| 2239 | gup_flags, pages); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2240 | |
| 2241 | /* Have to be a bit careful with return values */ |
| 2242 | if (nr > 0) { |
| 2243 | if (ret < 0) |
| 2244 | ret = nr; |
| 2245 | else |
| 2246 | ret += nr; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | return ret; |
| 2251 | } |
| 2252 | |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 2253 | #endif /* CONFIG_HAVE_GENERIC_GUP */ |