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