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