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