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