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