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