Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/spinlock.h> |
| 6 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 7 | #include <linux/mm.h> |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 8 | #include <linux/memremap.h> |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 9 | #include <linux/pagemap.h> |
| 10 | #include <linux/rmap.h> |
| 11 | #include <linux/swap.h> |
| 12 | #include <linux/swapops.h> |
| 13 | |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 14 | #include <linux/sched/signal.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 15 | #include <linux/rwsem.h> |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 16 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 17 | #include <linux/migrate.h> |
| 18 | #include <linux/mm_inline.h> |
| 19 | #include <linux/sched/mm.h> |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 20 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 21 | #include <asm/mmu_context.h> |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 22 | #include <asm/pgtable.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 | fc1d8e7 | 2019-05-13 17:19:08 -0700 | [diff] [blame] | 32 | typedef int (*set_dirty_func_t)(struct page *page); |
| 33 | |
| 34 | static void __put_user_pages_dirty(struct page **pages, |
| 35 | unsigned long npages, |
| 36 | set_dirty_func_t sdf) |
| 37 | { |
| 38 | unsigned long index; |
| 39 | |
| 40 | for (index = 0; index < npages; index++) { |
| 41 | struct page *page = compound_head(pages[index]); |
| 42 | |
| 43 | /* |
| 44 | * Checking PageDirty at this point may race with |
| 45 | * clear_page_dirty_for_io(), but that's OK. Two key cases: |
| 46 | * |
| 47 | * 1) This code sees the page as already dirty, so it skips |
| 48 | * the call to sdf(). That could happen because |
| 49 | * clear_page_dirty_for_io() called page_mkclean(), |
| 50 | * followed by set_page_dirty(). However, now the page is |
| 51 | * going to get written back, which meets the original |
| 52 | * intention of setting it dirty, so all is well: |
| 53 | * clear_page_dirty_for_io() goes on to call |
| 54 | * TestClearPageDirty(), and write the page back. |
| 55 | * |
| 56 | * 2) This code sees the page as clean, so it calls sdf(). |
| 57 | * The page stays dirty, despite being written back, so it |
| 58 | * gets written back again in the next writeback cycle. |
| 59 | * This is harmless. |
| 60 | */ |
| 61 | if (!PageDirty(page)) |
| 62 | sdf(page); |
| 63 | |
| 64 | put_user_page(page); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * put_user_pages_dirty() - release and dirty an array of gup-pinned pages |
| 70 | * @pages: array of pages to be marked dirty and released. |
| 71 | * @npages: number of pages in the @pages array. |
| 72 | * |
| 73 | * "gup-pinned page" refers to a page that has had one of the get_user_pages() |
| 74 | * variants called on that page. |
| 75 | * |
| 76 | * For each page in the @pages array, make that page (or its head page, if a |
| 77 | * compound page) dirty, if it was previously listed as clean. Then, release |
| 78 | * the page using put_user_page(). |
| 79 | * |
| 80 | * Please see the put_user_page() documentation for details. |
| 81 | * |
| 82 | * set_page_dirty(), which does not lock the page, is used here. |
| 83 | * Therefore, it is the caller's responsibility to ensure that this is |
| 84 | * safe. If not, then put_user_pages_dirty_lock() should be called instead. |
| 85 | * |
| 86 | */ |
| 87 | void put_user_pages_dirty(struct page **pages, unsigned long npages) |
| 88 | { |
| 89 | __put_user_pages_dirty(pages, npages, set_page_dirty); |
| 90 | } |
| 91 | EXPORT_SYMBOL(put_user_pages_dirty); |
| 92 | |
| 93 | /** |
| 94 | * put_user_pages_dirty_lock() - release and dirty an array of gup-pinned pages |
| 95 | * @pages: array of pages to be marked dirty and released. |
| 96 | * @npages: number of pages in the @pages array. |
| 97 | * |
| 98 | * For each page in the @pages array, make that page (or its head page, if a |
| 99 | * compound page) dirty, if it was previously listed as clean. Then, release |
| 100 | * the page using put_user_page(). |
| 101 | * |
| 102 | * Please see the put_user_page() documentation for details. |
| 103 | * |
| 104 | * This is just like put_user_pages_dirty(), except that it invokes |
| 105 | * set_page_dirty_lock(), instead of set_page_dirty(). |
| 106 | * |
| 107 | */ |
| 108 | void put_user_pages_dirty_lock(struct page **pages, unsigned long npages) |
| 109 | { |
| 110 | __put_user_pages_dirty(pages, npages, set_page_dirty_lock); |
| 111 | } |
| 112 | EXPORT_SYMBOL(put_user_pages_dirty_lock); |
| 113 | |
| 114 | /** |
| 115 | * put_user_pages() - release an array of gup-pinned pages. |
| 116 | * @pages: array of pages to be marked dirty and released. |
| 117 | * @npages: number of pages in the @pages array. |
| 118 | * |
| 119 | * For each page in the @pages array, release the page using put_user_page(). |
| 120 | * |
| 121 | * Please see the put_user_page() documentation for details. |
| 122 | */ |
| 123 | void put_user_pages(struct page **pages, unsigned long npages) |
| 124 | { |
| 125 | unsigned long index; |
| 126 | |
| 127 | /* |
| 128 | * TODO: this can be optimized for huge pages: if a series of pages is |
| 129 | * physically contiguous and part of the same compound page, then a |
| 130 | * single operation to the head page should suffice. |
| 131 | */ |
| 132 | for (index = 0; index < npages; index++) |
| 133 | put_user_page(pages[index]); |
| 134 | } |
| 135 | EXPORT_SYMBOL(put_user_pages); |
| 136 | |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 137 | #ifdef CONFIG_MMU |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 138 | static struct page *no_page_table(struct vm_area_struct *vma, |
| 139 | unsigned int flags) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 140 | { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 141 | /* |
| 142 | * When core dumping an enormous anonymous area that nobody |
| 143 | * has touched so far, we don't want to allocate unnecessary pages or |
| 144 | * page tables. Return error instead of NULL to skip handle_mm_fault, |
| 145 | * then get_dump_page() will return NULL to leave a hole in the dump. |
| 146 | * But we can only make this optimization where a hole would surely |
| 147 | * be zero-filled if handle_mm_fault() actually did handle it. |
| 148 | */ |
| 149 | if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault)) |
| 150 | return ERR_PTR(-EFAULT); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 154 | static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, |
| 155 | pte_t *pte, unsigned int flags) |
| 156 | { |
| 157 | /* No page to get reference */ |
| 158 | if (flags & FOLL_GET) |
| 159 | return -EFAULT; |
| 160 | |
| 161 | if (flags & FOLL_TOUCH) { |
| 162 | pte_t entry = *pte; |
| 163 | |
| 164 | if (flags & FOLL_WRITE) |
| 165 | entry = pte_mkdirty(entry); |
| 166 | entry = pte_mkyoung(entry); |
| 167 | |
| 168 | if (!pte_same(*pte, entry)) { |
| 169 | set_pte_at(vma->vm_mm, address, pte, entry); |
| 170 | update_mmu_cache(vma, address, pte); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Proper page table entry exists, but no corresponding struct page */ |
| 175 | return -EEXIST; |
| 176 | } |
| 177 | |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 178 | /* |
| 179 | * FOLL_FORCE can write to even unwritable pte's, but only |
| 180 | * after we've gone through a COW cycle and they are dirty. |
| 181 | */ |
| 182 | static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) |
| 183 | { |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 184 | return pte_write(pte) || |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 185 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); |
| 186 | } |
| 187 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 188 | static struct page *follow_page_pte(struct vm_area_struct *vma, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 189 | unsigned long address, pmd_t *pmd, unsigned int flags, |
| 190 | struct dev_pagemap **pgmap) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 191 | { |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 192 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 193 | struct page *page; |
| 194 | spinlock_t *ptl; |
| 195 | pte_t *ptep, pte; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 196 | |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 197 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 198 | if (unlikely(pmd_bad(*pmd))) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 199 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 200 | |
| 201 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 202 | pte = *ptep; |
| 203 | if (!pte_present(pte)) { |
| 204 | swp_entry_t entry; |
| 205 | /* |
| 206 | * KSM's break_ksm() relies upon recognizing a ksm page |
| 207 | * even while it is being migrated, so for that case we |
| 208 | * need migration_entry_wait(). |
| 209 | */ |
| 210 | if (likely(!(flags & FOLL_MIGRATION))) |
| 211 | goto no_page; |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 212 | if (pte_none(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 213 | goto no_page; |
| 214 | entry = pte_to_swp_entry(pte); |
| 215 | if (!is_migration_entry(entry)) |
| 216 | goto no_page; |
| 217 | pte_unmap_unlock(ptep, ptl); |
| 218 | migration_entry_wait(mm, pmd, address); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 219 | goto retry; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 220 | } |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 221 | if ((flags & FOLL_NUMA) && pte_protnone(pte)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 222 | goto no_page; |
Linus Torvalds | 19be0ea | 2016-10-13 13:07:36 -0700 | [diff] [blame] | 223 | if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 224 | pte_unmap_unlock(ptep, ptl); |
| 225 | return NULL; |
| 226 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 227 | |
| 228 | page = vm_normal_page(vma, address, pte); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 229 | if (!page && pte_devmap(pte) && (flags & FOLL_GET)) { |
| 230 | /* |
| 231 | * Only return device mapping pages in the FOLL_GET case since |
| 232 | * they are only valid while holding the pgmap reference. |
| 233 | */ |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 234 | *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); |
| 235 | if (*pgmap) |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 236 | page = pte_page(pte); |
| 237 | else |
| 238 | goto no_page; |
| 239 | } else if (unlikely(!page)) { |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 240 | if (flags & FOLL_DUMP) { |
| 241 | /* Avoid special (like zero) pages in core dumps */ |
| 242 | page = ERR_PTR(-EFAULT); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | if (is_zero_pfn(pte_pfn(pte))) { |
| 247 | page = pte_page(pte); |
| 248 | } else { |
| 249 | int ret; |
| 250 | |
| 251 | ret = follow_pfn_pte(vma, address, ptep, flags); |
| 252 | page = ERR_PTR(ret); |
| 253 | goto out; |
| 254 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 257 | if (flags & FOLL_SPLIT && PageTransCompound(page)) { |
| 258 | int ret; |
| 259 | get_page(page); |
| 260 | pte_unmap_unlock(ptep, ptl); |
| 261 | lock_page(page); |
| 262 | ret = split_huge_page(page); |
| 263 | unlock_page(page); |
| 264 | put_page(page); |
| 265 | if (ret) |
| 266 | return ERR_PTR(ret); |
| 267 | goto retry; |
| 268 | } |
| 269 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 270 | if (flags & FOLL_GET) { |
| 271 | if (unlikely(!try_get_page(page))) { |
| 272 | page = ERR_PTR(-ENOMEM); |
| 273 | goto out; |
| 274 | } |
| 275 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 276 | if (flags & FOLL_TOUCH) { |
| 277 | if ((flags & FOLL_WRITE) && |
| 278 | !pte_dirty(pte) && !PageDirty(page)) |
| 279 | set_page_dirty(page); |
| 280 | /* |
| 281 | * pte_mkyoung() would be more correct here, but atomic care |
| 282 | * is needed to avoid losing the dirty bit: it is easier to use |
| 283 | * mark_page_accessed(). |
| 284 | */ |
| 285 | mark_page_accessed(page); |
| 286 | } |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 287 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
Kirill A. Shutemov | e90309c | 2016-01-15 16:54:33 -0800 | [diff] [blame] | 288 | /* Do not mlock pte-mapped THP */ |
| 289 | if (PageTransCompound(page)) |
| 290 | goto out; |
| 291 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 292 | /* |
| 293 | * The preliminary mapping check is mainly to avoid the |
| 294 | * pointless overhead of lock_page on the ZERO_PAGE |
| 295 | * which might bounce very badly if there is contention. |
| 296 | * |
| 297 | * If the page is already locked, we don't need to |
| 298 | * handle it now - vmscan will handle it later if and |
| 299 | * when it attempts to reclaim the page. |
| 300 | */ |
| 301 | if (page->mapping && trylock_page(page)) { |
| 302 | lru_add_drain(); /* push cached pages to LRU */ |
| 303 | /* |
| 304 | * Because we lock page here, and migration is |
| 305 | * blocked by the pte's page reference, and we |
| 306 | * know the page is still mapped, we don't even |
| 307 | * need to check for file-cache page truncation. |
| 308 | */ |
| 309 | mlock_vma_page(page); |
| 310 | unlock_page(page); |
| 311 | } |
| 312 | } |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 313 | out: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 314 | pte_unmap_unlock(ptep, ptl); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 315 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 316 | no_page: |
| 317 | pte_unmap_unlock(ptep, ptl); |
| 318 | if (!pte_none(pte)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 319 | return NULL; |
| 320 | return no_page_table(vma, flags); |
| 321 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 322 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 323 | static struct page *follow_pmd_mask(struct vm_area_struct *vma, |
| 324 | unsigned long address, pud_t *pudp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 325 | unsigned int flags, |
| 326 | struct follow_page_context *ctx) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 327 | { |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 328 | pmd_t *pmd, pmdval; |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 329 | spinlock_t *ptl; |
| 330 | struct page *page; |
| 331 | struct mm_struct *mm = vma->vm_mm; |
| 332 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 333 | pmd = pmd_offset(pudp, address); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 334 | /* |
| 335 | * The READ_ONCE() will stabilize the pmdval in a register or |
| 336 | * on the stack so that it will stop changing under the code. |
| 337 | */ |
| 338 | pmdval = READ_ONCE(*pmd); |
| 339 | if (pmd_none(pmdval)) |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 340 | return no_page_table(vma, flags); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 341 | if (pmd_huge(pmdval) && vma->vm_flags & VM_HUGETLB) { |
Naoya Horiguchi | e66f17f | 2015-02-11 15:25:22 -0800 | [diff] [blame] | 342 | page = follow_huge_pmd(mm, address, pmd, flags); |
| 343 | if (page) |
| 344 | return page; |
| 345 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 346 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 347 | if (is_hugepd(__hugepd(pmd_val(pmdval)))) { |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 348 | page = follow_huge_pd(vma, address, |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 349 | __hugepd(pmd_val(pmdval)), flags, |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 350 | PMD_SHIFT); |
| 351 | if (page) |
| 352 | return page; |
| 353 | return no_page_table(vma, flags); |
| 354 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 355 | retry: |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 356 | if (!pmd_present(pmdval)) { |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 357 | if (likely(!(flags & FOLL_MIGRATION))) |
| 358 | return no_page_table(vma, flags); |
| 359 | VM_BUG_ON(thp_migration_supported() && |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 360 | !is_pmd_migration_entry(pmdval)); |
| 361 | if (is_pmd_migration_entry(pmdval)) |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 362 | pmd_migration_entry_wait(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 363 | pmdval = READ_ONCE(*pmd); |
| 364 | /* |
| 365 | * MADV_DONTNEED may convert the pmd to null because |
| 366 | * mmap_sem is held in read mode |
| 367 | */ |
| 368 | if (pmd_none(pmdval)) |
| 369 | return no_page_table(vma, flags); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 370 | goto retry; |
| 371 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 372 | if (pmd_devmap(pmdval)) { |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 373 | ptl = pmd_lock(mm, pmd); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 374 | page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 375 | spin_unlock(ptl); |
| 376 | if (page) |
| 377 | return page; |
| 378 | } |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 379 | if (likely(!pmd_trans_huge(pmdval))) |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 380 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 381 | |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 382 | if ((flags & FOLL_NUMA) && pmd_protnone(pmdval)) |
Aneesh Kumar K.V | db08f20 | 2017-02-24 14:59:53 -0800 | [diff] [blame] | 383 | return no_page_table(vma, flags); |
| 384 | |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 385 | retry_locked: |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 386 | ptl = pmd_lock(mm, pmd); |
Huang Ying | 6882728 | 2018-06-07 17:06:34 -0700 | [diff] [blame] | 387 | if (unlikely(pmd_none(*pmd))) { |
| 388 | spin_unlock(ptl); |
| 389 | return no_page_table(vma, flags); |
| 390 | } |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 391 | if (unlikely(!pmd_present(*pmd))) { |
| 392 | spin_unlock(ptl); |
| 393 | if (likely(!(flags & FOLL_MIGRATION))) |
| 394 | return no_page_table(vma, flags); |
| 395 | pmd_migration_entry_wait(mm, pmd); |
| 396 | goto retry_locked; |
| 397 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 398 | if (unlikely(!pmd_trans_huge(*pmd))) { |
| 399 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 400 | return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 69e68b4 | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 401 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 402 | if (flags & FOLL_SPLIT) { |
| 403 | int ret; |
| 404 | page = pmd_page(*pmd); |
| 405 | if (is_huge_zero_page(page)) { |
| 406 | spin_unlock(ptl); |
| 407 | ret = 0; |
Kirill A. Shutemov | 78ddc53 | 2016-01-15 16:52:42 -0800 | [diff] [blame] | 408 | split_huge_pmd(vma, pmd, address); |
Naoya Horiguchi | 337d9ab | 2016-07-26 15:24:03 -0700 | [diff] [blame] | 409 | if (pmd_trans_unstable(pmd)) |
| 410 | ret = -EBUSY; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 411 | } else { |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 412 | if (unlikely(!try_get_page(page))) { |
| 413 | spin_unlock(ptl); |
| 414 | return ERR_PTR(-ENOMEM); |
| 415 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 416 | spin_unlock(ptl); |
| 417 | lock_page(page); |
| 418 | ret = split_huge_page(page); |
| 419 | unlock_page(page); |
| 420 | put_page(page); |
Kirill A. Shutemov | baa355f | 2016-07-26 15:25:51 -0700 | [diff] [blame] | 421 | if (pmd_none(*pmd)) |
| 422 | return no_page_table(vma, flags); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | return ret ? ERR_PTR(ret) : |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 426 | follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 427 | } |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 428 | page = follow_trans_huge_pmd(vma, address, pmd, flags); |
| 429 | spin_unlock(ptl); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 430 | ctx->page_mask = HPAGE_PMD_NR - 1; |
Kirill A. Shutemov | 6742d29 | 2016-01-15 16:52:28 -0800 | [diff] [blame] | 431 | return page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 434 | static struct page *follow_pud_mask(struct vm_area_struct *vma, |
| 435 | unsigned long address, p4d_t *p4dp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 436 | unsigned int flags, |
| 437 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 438 | { |
| 439 | pud_t *pud; |
| 440 | spinlock_t *ptl; |
| 441 | struct page *page; |
| 442 | struct mm_struct *mm = vma->vm_mm; |
| 443 | |
| 444 | pud = pud_offset(p4dp, address); |
| 445 | if (pud_none(*pud)) |
| 446 | return no_page_table(vma, flags); |
| 447 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { |
| 448 | page = follow_huge_pud(mm, address, pud, flags); |
| 449 | if (page) |
| 450 | return page; |
| 451 | return no_page_table(vma, flags); |
| 452 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 453 | if (is_hugepd(__hugepd(pud_val(*pud)))) { |
| 454 | page = follow_huge_pd(vma, address, |
| 455 | __hugepd(pud_val(*pud)), flags, |
| 456 | PUD_SHIFT); |
| 457 | if (page) |
| 458 | return page; |
| 459 | return no_page_table(vma, flags); |
| 460 | } |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 461 | if (pud_devmap(*pud)) { |
| 462 | ptl = pud_lock(mm, pud); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 463 | page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 464 | spin_unlock(ptl); |
| 465 | if (page) |
| 466 | return page; |
| 467 | } |
| 468 | if (unlikely(pud_bad(*pud))) |
| 469 | return no_page_table(vma, flags); |
| 470 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 471 | return follow_pmd_mask(vma, address, pud, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 474 | static struct page *follow_p4d_mask(struct vm_area_struct *vma, |
| 475 | unsigned long address, pgd_t *pgdp, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 476 | unsigned int flags, |
| 477 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 478 | { |
| 479 | p4d_t *p4d; |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 480 | struct page *page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 481 | |
| 482 | p4d = p4d_offset(pgdp, address); |
| 483 | if (p4d_none(*p4d)) |
| 484 | return no_page_table(vma, flags); |
| 485 | BUILD_BUG_ON(p4d_huge(*p4d)); |
| 486 | if (unlikely(p4d_bad(*p4d))) |
| 487 | return no_page_table(vma, flags); |
| 488 | |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 489 | if (is_hugepd(__hugepd(p4d_val(*p4d)))) { |
| 490 | page = follow_huge_pd(vma, address, |
| 491 | __hugepd(p4d_val(*p4d)), flags, |
| 492 | P4D_SHIFT); |
| 493 | if (page) |
| 494 | return page; |
| 495 | return no_page_table(vma, flags); |
| 496 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 497 | return follow_pud_mask(vma, address, p4d, flags, ctx); |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
| 501 | * follow_page_mask - look up a page descriptor from a user-virtual address |
| 502 | * @vma: vm_area_struct mapping @address |
| 503 | * @address: virtual address to look up |
| 504 | * @flags: flags modifying lookup behaviour |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 505 | * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a |
| 506 | * pointer to output page_mask |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 507 | * |
| 508 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> |
| 509 | * |
Mike Rapoport | 7817955 | 2018-11-16 15:08:29 -0800 | [diff] [blame] | 510 | * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches |
| 511 | * the device's dev_pagemap metadata to avoid repeating expensive lookups. |
| 512 | * |
| 513 | * On output, the @ctx->page_mask is set according to the size of the page. |
| 514 | * |
| 515 | * 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] | 516 | * an error pointer if there is a mapping to something not represented |
| 517 | * by a page descriptor (see also vm_normal_page()). |
| 518 | */ |
Bharath Vedartham | a7030ae | 2019-07-11 20:54:34 -0700 | [diff] [blame] | 519 | 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] | 520 | unsigned long address, unsigned int flags, |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 521 | struct follow_page_context *ctx) |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 522 | { |
| 523 | pgd_t *pgd; |
| 524 | struct page *page; |
| 525 | struct mm_struct *mm = vma->vm_mm; |
| 526 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 527 | ctx->page_mask = 0; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 528 | |
| 529 | /* make this handle hugepd */ |
| 530 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); |
| 531 | if (!IS_ERR(page)) { |
| 532 | BUG_ON(flags & FOLL_GET); |
| 533 | return page; |
| 534 | } |
| 535 | |
| 536 | pgd = pgd_offset(mm, address); |
| 537 | |
| 538 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
| 539 | return no_page_table(vma, flags); |
| 540 | |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 541 | if (pgd_huge(*pgd)) { |
| 542 | page = follow_huge_pgd(mm, address, pgd, flags); |
| 543 | if (page) |
| 544 | return page; |
| 545 | return no_page_table(vma, flags); |
| 546 | } |
Aneesh Kumar K.V | 4dc7145 | 2017-07-06 15:38:56 -0700 | [diff] [blame] | 547 | if (is_hugepd(__hugepd(pgd_val(*pgd)))) { |
| 548 | page = follow_huge_pd(vma, address, |
| 549 | __hugepd(pgd_val(*pgd)), flags, |
| 550 | PGDIR_SHIFT); |
| 551 | if (page) |
| 552 | return page; |
| 553 | return no_page_table(vma, flags); |
| 554 | } |
Anshuman Khandual | faaa5b6 | 2017-07-06 15:38:50 -0700 | [diff] [blame] | 555 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 556 | return follow_p4d_mask(vma, address, pgd, flags, ctx); |
| 557 | } |
| 558 | |
| 559 | struct page *follow_page(struct vm_area_struct *vma, unsigned long address, |
| 560 | unsigned int foll_flags) |
| 561 | { |
| 562 | struct follow_page_context ctx = { NULL }; |
| 563 | struct page *page; |
| 564 | |
| 565 | page = follow_page_mask(vma, address, foll_flags, &ctx); |
| 566 | if (ctx.pgmap) |
| 567 | put_dev_pagemap(ctx.pgmap); |
| 568 | return page; |
Aneesh Kumar K.V | 080dbb6 | 2017-07-06 15:38:44 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 571 | static int get_gate_page(struct mm_struct *mm, unsigned long address, |
| 572 | unsigned int gup_flags, struct vm_area_struct **vma, |
| 573 | struct page **page) |
| 574 | { |
| 575 | pgd_t *pgd; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 576 | p4d_t *p4d; |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 577 | pud_t *pud; |
| 578 | pmd_t *pmd; |
| 579 | pte_t *pte; |
| 580 | int ret = -EFAULT; |
| 581 | |
| 582 | /* user gate pages are read-only */ |
| 583 | if (gup_flags & FOLL_WRITE) |
| 584 | return -EFAULT; |
| 585 | if (address > TASK_SIZE) |
| 586 | pgd = pgd_offset_k(address); |
| 587 | else |
| 588 | pgd = pgd_offset_gate(mm, address); |
| 589 | BUG_ON(pgd_none(*pgd)); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 590 | p4d = p4d_offset(pgd, address); |
| 591 | BUG_ON(p4d_none(*p4d)); |
| 592 | pud = pud_offset(p4d, address); |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 593 | BUG_ON(pud_none(*pud)); |
| 594 | pmd = pmd_offset(pud, address); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 595 | if (!pmd_present(*pmd)) |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 596 | return -EFAULT; |
| 597 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
| 598 | pte = pte_offset_map(pmd, address); |
| 599 | if (pte_none(*pte)) |
| 600 | goto unmap; |
| 601 | *vma = get_gate_vma(mm); |
| 602 | if (!page) |
| 603 | goto out; |
| 604 | *page = vm_normal_page(*vma, address, *pte); |
| 605 | if (!*page) { |
| 606 | if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte))) |
| 607 | goto unmap; |
| 608 | *page = pte_page(*pte); |
Jérôme Glisse | df6ad69 | 2017-09-08 16:12:24 -0700 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * This should never happen (a device public page in the gate |
| 612 | * area). |
| 613 | */ |
| 614 | if (is_device_public_page(*page)) |
| 615 | goto unmap; |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 616 | } |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 617 | if (unlikely(!try_get_page(*page))) { |
| 618 | ret = -ENOMEM; |
| 619 | goto unmap; |
| 620 | } |
Kirill A. Shutemov | f2b495c | 2014-06-04 16:08:11 -0700 | [diff] [blame] | 621 | out: |
| 622 | ret = 0; |
| 623 | unmap: |
| 624 | pte_unmap(pte); |
| 625 | return ret; |
| 626 | } |
| 627 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 628 | /* |
| 629 | * mmap_sem must be held on entry. If @nonblocking != NULL and |
| 630 | * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released. |
| 631 | * If it is, *@nonblocking will be set to 0 and -EBUSY returned. |
| 632 | */ |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 633 | static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, |
| 634 | unsigned long address, unsigned int *flags, int *nonblocking) |
| 635 | { |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 636 | unsigned int fault_flags = 0; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 637 | vm_fault_t ret; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 638 | |
Eric B Munson | de60f5f | 2015-11-05 18:51:36 -0800 | [diff] [blame] | 639 | /* mlock all present pages, but do not fault in new pages */ |
| 640 | if ((*flags & (FOLL_POPULATE | FOLL_MLOCK)) == FOLL_MLOCK) |
| 641 | return -ENOENT; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 642 | if (*flags & FOLL_WRITE) |
| 643 | fault_flags |= FAULT_FLAG_WRITE; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 644 | if (*flags & FOLL_REMOTE) |
| 645 | fault_flags |= FAULT_FLAG_REMOTE; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 646 | if (nonblocking) |
| 647 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 648 | if (*flags & FOLL_NOWAIT) |
| 649 | fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; |
Andres Lagar-Cavilla | 234b239 | 2014-09-17 10:51:48 -0700 | [diff] [blame] | 650 | if (*flags & FOLL_TRIED) { |
| 651 | VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY); |
| 652 | fault_flags |= FAULT_FLAG_TRIED; |
| 653 | } |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 654 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 655 | ret = handle_mm_fault(vma, address, fault_flags); |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 656 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 657 | int err = vm_fault_to_errno(ret, *flags); |
| 658 | |
| 659 | if (err) |
| 660 | return err; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 661 | BUG(); |
| 662 | } |
| 663 | |
| 664 | if (tsk) { |
| 665 | if (ret & VM_FAULT_MAJOR) |
| 666 | tsk->maj_flt++; |
| 667 | else |
| 668 | tsk->min_flt++; |
| 669 | } |
| 670 | |
| 671 | if (ret & VM_FAULT_RETRY) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 672 | if (nonblocking && !(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 673 | *nonblocking = 0; |
| 674 | return -EBUSY; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when |
| 679 | * necessary, even if maybe_mkwrite decided not to set pte_write. We |
| 680 | * can thus safely do subsequent page lookups as if they were reads. |
| 681 | * But only do so when looping for pte_write is futile: in some cases |
| 682 | * userspace may also be wanting to write to the gotten user page, |
| 683 | * which a read fault here might prevent (a readonly page might get |
| 684 | * reCOWed by userspace write). |
| 685 | */ |
| 686 | if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 687 | *flags |= FOLL_COW; |
Kirill A. Shutemov | 1674448 | 2014-06-04 16:08:12 -0700 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 691 | static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) |
| 692 | { |
| 693 | vm_flags_t vm_flags = vma->vm_flags; |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 694 | int write = (gup_flags & FOLL_WRITE); |
| 695 | int foreign = (gup_flags & FOLL_REMOTE); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 696 | |
| 697 | if (vm_flags & (VM_IO | VM_PFNMAP)) |
| 698 | return -EFAULT; |
| 699 | |
Willy Tarreau | 7f7ccc2 | 2018-05-11 08:11:44 +0200 | [diff] [blame] | 700 | if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma)) |
| 701 | return -EFAULT; |
| 702 | |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 703 | if (write) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 704 | if (!(vm_flags & VM_WRITE)) { |
| 705 | if (!(gup_flags & FOLL_FORCE)) |
| 706 | return -EFAULT; |
| 707 | /* |
| 708 | * We used to let the write,force case do COW in a |
| 709 | * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could |
| 710 | * set a breakpoint in a read-only mapping of an |
| 711 | * executable, without corrupting the file (yet only |
| 712 | * when that file had been opened for writing!). |
| 713 | * Anon pages in shared mappings are surprising: now |
| 714 | * just reject it. |
| 715 | */ |
Hugh Dickins | 4643536 | 2016-01-30 18:03:16 -0800 | [diff] [blame] | 716 | if (!is_cow_mapping(vm_flags)) |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 717 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 718 | } |
| 719 | } else if (!(vm_flags & VM_READ)) { |
| 720 | if (!(gup_flags & FOLL_FORCE)) |
| 721 | return -EFAULT; |
| 722 | /* |
| 723 | * Is there actually any vma we can reach here which does not |
| 724 | * have VM_MAYREAD set? |
| 725 | */ |
| 726 | if (!(vm_flags & VM_MAYREAD)) |
| 727 | return -EFAULT; |
| 728 | } |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 729 | /* |
| 730 | * gups are always data accesses, not instruction |
| 731 | * fetches, so execute=false here |
| 732 | */ |
| 733 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 734 | return -EFAULT; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 738 | /** |
| 739 | * __get_user_pages() - pin user pages in memory |
| 740 | * @tsk: task_struct of target task |
| 741 | * @mm: mm_struct of target mm |
| 742 | * @start: starting user address |
| 743 | * @nr_pages: number of pages from start to pin |
| 744 | * @gup_flags: flags modifying pin behaviour |
| 745 | * @pages: array that receives pointers to the pages pinned. |
| 746 | * Should be at least nr_pages long. Or NULL, if caller |
| 747 | * only intends to ensure the pages are faulted in. |
| 748 | * @vmas: array of pointers to vmas corresponding to each page. |
| 749 | * Or NULL if the caller does not require them. |
| 750 | * @nonblocking: whether waiting for disk IO or mmap_sem contention |
| 751 | * |
| 752 | * Returns number of pages pinned. This may be fewer than the number |
| 753 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 754 | * were pinned, returns -errno. Each page returned must be released |
| 755 | * with a put_page() call when it is finished with. vmas will only |
| 756 | * remain valid while mmap_sem is held. |
| 757 | * |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 758 | * Must be called with mmap_sem held. It may be released. See below. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 759 | * |
| 760 | * __get_user_pages walks a process's page tables and takes a reference to |
| 761 | * each struct page that each user address corresponds to at a given |
| 762 | * instant. That is, it takes the page that would be accessed if a user |
| 763 | * thread accesses the given user virtual address at that instant. |
| 764 | * |
| 765 | * This does not guarantee that the page exists in the user mappings when |
| 766 | * __get_user_pages returns, and there may even be a completely different |
| 767 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 768 | * and subsequently re faulted). However it does guarantee that the page |
| 769 | * won't be freed completely. And mostly callers simply care that the page |
| 770 | * contains data that was valid *at some point in time*. Typically, an IO |
| 771 | * or similar operation cannot guarantee anything stronger anyway because |
| 772 | * locks can't be held over the syscall boundary. |
| 773 | * |
| 774 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If |
| 775 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as |
| 776 | * appropriate) must be called after the page is finished with, and |
| 777 | * before put_page is called. |
| 778 | * |
| 779 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO |
| 780 | * or mmap_sem contention, and if waiting is needed to pin all pages, |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 781 | * *@nonblocking will be set to 0. Further, if @gup_flags does not |
| 782 | * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in |
| 783 | * this case. |
| 784 | * |
| 785 | * A caller using such a combination of @nonblocking and @gup_flags |
| 786 | * must therefore hold the mmap_sem for reading only, and recognize |
| 787 | * when it's been released. Otherwise, it must be held for either |
| 788 | * reading or writing and will not be released. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 789 | * |
| 790 | * In most cases, get_user_pages or get_user_pages_fast should be used |
| 791 | * instead of __get_user_pages. __get_user_pages should be used only if |
| 792 | * you need some special @gup_flags. |
| 793 | */ |
Lorenzo Stoakes | 0d73175 | 2016-10-24 10:57:25 +0100 | [diff] [blame] | 794 | static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 795 | unsigned long start, unsigned long nr_pages, |
| 796 | unsigned int gup_flags, struct page **pages, |
| 797 | struct vm_area_struct **vmas, int *nonblocking) |
| 798 | { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 799 | long ret = 0, i = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 800 | struct vm_area_struct *vma = NULL; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 801 | struct follow_page_context ctx = { NULL }; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 802 | |
| 803 | if (!nr_pages) |
| 804 | return 0; |
| 805 | |
| 806 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); |
| 807 | |
| 808 | /* |
| 809 | * If FOLL_FORCE is set then do not force a full fault as the hinting |
| 810 | * fault information is unrelated to the reference behaviour of a task |
| 811 | * using the address space |
| 812 | */ |
| 813 | if (!(gup_flags & FOLL_FORCE)) |
| 814 | gup_flags |= FOLL_NUMA; |
| 815 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 816 | do { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 817 | struct page *page; |
| 818 | unsigned int foll_flags = gup_flags; |
| 819 | unsigned int page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 820 | |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 821 | /* first iteration or cross vma bound */ |
| 822 | if (!vma || start >= vma->vm_end) { |
| 823 | vma = find_extend_vma(mm, start); |
| 824 | if (!vma && in_gate_area(mm, start)) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 825 | ret = get_gate_page(mm, start & PAGE_MASK, |
| 826 | gup_flags, &vma, |
| 827 | pages ? &pages[i] : NULL); |
| 828 | if (ret) |
John Hubbard | 08be37b | 2018-11-30 14:08:53 -0800 | [diff] [blame] | 829 | goto out; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 830 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 831 | goto next_page; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 832 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 833 | |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 834 | if (!vma || check_vma_flags(vma, gup_flags)) { |
| 835 | ret = -EFAULT; |
| 836 | goto out; |
| 837 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 838 | if (is_vm_hugetlb_page(vma)) { |
| 839 | i = follow_hugetlb_page(mm, vma, pages, vmas, |
| 840 | &start, &nr_pages, i, |
Andrea Arcangeli | 87ffc11 | 2017-02-22 15:43:13 -0800 | [diff] [blame] | 841 | gup_flags, nonblocking); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 842 | continue; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 843 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 844 | } |
| 845 | retry: |
| 846 | /* |
| 847 | * If we have a pending SIGKILL, don't keep faulting pages and |
| 848 | * potentially allocating memory. |
| 849 | */ |
Davidlohr Bueso | fa45f11 | 2019-01-03 15:28:55 -0800 | [diff] [blame] | 850 | if (fatal_signal_pending(current)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 851 | ret = -ERESTARTSYS; |
| 852 | goto out; |
| 853 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 854 | cond_resched(); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 855 | |
| 856 | page = follow_page_mask(vma, start, foll_flags, &ctx); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 857 | if (!page) { |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 858 | ret = faultin_page(tsk, vma, start, &foll_flags, |
| 859 | nonblocking); |
| 860 | switch (ret) { |
| 861 | case 0: |
| 862 | goto retry; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 863 | case -EBUSY: |
| 864 | ret = 0; |
| 865 | /* FALLTHRU */ |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 866 | case -EFAULT: |
| 867 | case -ENOMEM: |
| 868 | case -EHWPOISON: |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 869 | goto out; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 870 | case -ENOENT: |
| 871 | goto next_page; |
| 872 | } |
| 873 | BUG(); |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 874 | } else if (PTR_ERR(page) == -EEXIST) { |
| 875 | /* |
| 876 | * Proper page table entry exists, but no corresponding |
| 877 | * struct page. |
| 878 | */ |
| 879 | goto next_page; |
| 880 | } else if (IS_ERR(page)) { |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 881 | ret = PTR_ERR(page); |
| 882 | goto out; |
Kirill A. Shutemov | 1027e44 | 2015-09-04 15:47:55 -0700 | [diff] [blame] | 883 | } |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 884 | if (pages) { |
| 885 | pages[i] = page; |
| 886 | flush_anon_page(vma, page, start); |
| 887 | flush_dcache_page(page); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 888 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 889 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 890 | next_page: |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 891 | if (vmas) { |
| 892 | vmas[i] = vma; |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 893 | ctx.page_mask = 0; |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 894 | } |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 895 | page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); |
Kirill A. Shutemov | fa5bb20 | 2014-06-04 16:08:13 -0700 | [diff] [blame] | 896 | if (page_increm > nr_pages) |
| 897 | page_increm = nr_pages; |
| 898 | i += page_increm; |
| 899 | start += page_increm * PAGE_SIZE; |
| 900 | nr_pages -= page_increm; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 901 | } while (nr_pages); |
Keith Busch | df06b37 | 2018-10-26 15:10:28 -0700 | [diff] [blame] | 902 | out: |
| 903 | if (ctx.pgmap) |
| 904 | put_dev_pagemap(ctx.pgmap); |
| 905 | return i ? i : ret; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 906 | } |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 907 | |
Tobias Klauser | 771ab43 | 2016-12-12 16:41:53 -0800 | [diff] [blame] | 908 | static bool vma_permits_fault(struct vm_area_struct *vma, |
| 909 | unsigned int fault_flags) |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 910 | { |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 911 | bool write = !!(fault_flags & FAULT_FLAG_WRITE); |
| 912 | bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 913 | vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 914 | |
| 915 | if (!(vm_flags & vma->vm_flags)) |
| 916 | return false; |
| 917 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 918 | /* |
| 919 | * The architecture might have a hardware protection |
Dave Hansen | 1b2ee12 | 2016-02-12 13:02:21 -0800 | [diff] [blame] | 920 | * mechanism other than read/write that can deny access. |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 921 | * |
| 922 | * gup always represents data access, not instruction |
| 923 | * fetches, so execute=false here: |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 924 | */ |
Dave Hansen | d61172b | 2016-02-12 13:02:24 -0800 | [diff] [blame] | 925 | if (!arch_vma_access_permitted(vma, write, false, foreign)) |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 926 | return false; |
| 927 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 928 | return true; |
| 929 | } |
| 930 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 931 | /* |
| 932 | * fixup_user_fault() - manually resolve a user page fault |
| 933 | * @tsk: the task_struct to use for page fault accounting, or |
| 934 | * NULL if faults are not to be recorded. |
| 935 | * @mm: mm_struct of target mm |
| 936 | * @address: user address |
| 937 | * @fault_flags:flags to pass down to handle_mm_fault() |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 938 | * @unlocked: did we unlock the mmap_sem while retrying, maybe NULL if caller |
| 939 | * does not allow retry |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 940 | * |
| 941 | * This is meant to be called in the specific scenario where for locking reasons |
| 942 | * we try to access user memory in atomic context (within a pagefault_disable() |
| 943 | * section), this returns -EFAULT, and we want to resolve the user fault before |
| 944 | * trying again. |
| 945 | * |
| 946 | * Typically this is meant to be used by the futex code. |
| 947 | * |
| 948 | * The main difference with get_user_pages() is that this function will |
| 949 | * unconditionally call handle_mm_fault() which will in turn perform all the |
| 950 | * 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] | 951 | * 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] | 952 | * |
| 953 | * This is important for some architectures where those bits also gate the |
| 954 | * access permission to the page because they are maintained in software. On |
| 955 | * such architectures, gup() will not be enough to make a subsequent access |
| 956 | * succeed. |
| 957 | * |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 958 | * This function will not return with an unlocked mmap_sem. So it has not the |
| 959 | * same semantics wrt the @mm->mmap_sem as does filemap_fault(). |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 960 | */ |
| 961 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 962 | unsigned long address, unsigned int fault_flags, |
| 963 | bool *unlocked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 964 | { |
| 965 | struct vm_area_struct *vma; |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 966 | vm_fault_t ret, major = 0; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 967 | |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 968 | if (unlocked) |
| 969 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; |
| 970 | |
| 971 | retry: |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 972 | vma = find_extend_vma(mm, address); |
| 973 | if (!vma || address < vma->vm_start) |
| 974 | return -EFAULT; |
| 975 | |
Dave Hansen | d4925e0 | 2016-02-12 13:02:16 -0800 | [diff] [blame] | 976 | if (!vma_permits_fault(vma, fault_flags)) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 977 | return -EFAULT; |
| 978 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 979 | ret = handle_mm_fault(vma, address, fault_flags); |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 980 | major |= ret & VM_FAULT_MAJOR; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 981 | if (ret & VM_FAULT_ERROR) { |
James Morse | 9a291a7 | 2017-06-02 14:46:46 -0700 | [diff] [blame] | 982 | int err = vm_fault_to_errno(ret, 0); |
| 983 | |
| 984 | if (err) |
| 985 | return err; |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 986 | BUG(); |
| 987 | } |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 988 | |
| 989 | if (ret & VM_FAULT_RETRY) { |
| 990 | down_read(&mm->mmap_sem); |
| 991 | if (!(fault_flags & FAULT_FLAG_TRIED)) { |
| 992 | *unlocked = true; |
| 993 | fault_flags &= ~FAULT_FLAG_ALLOW_RETRY; |
| 994 | fault_flags |= FAULT_FLAG_TRIED; |
| 995 | goto retry; |
| 996 | } |
| 997 | } |
| 998 | |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 999 | if (tsk) { |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 1000 | if (major) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1001 | tsk->maj_flt++; |
| 1002 | else |
| 1003 | tsk->min_flt++; |
| 1004 | } |
| 1005 | return 0; |
| 1006 | } |
Paolo Bonzini | add6a0c | 2016-06-07 17:51:18 +0200 | [diff] [blame] | 1007 | EXPORT_SYMBOL_GPL(fixup_user_fault); |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1008 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1009 | static __always_inline long __get_user_pages_locked(struct task_struct *tsk, |
| 1010 | struct mm_struct *mm, |
| 1011 | unsigned long start, |
| 1012 | unsigned long nr_pages, |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1013 | struct page **pages, |
| 1014 | struct vm_area_struct **vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1015 | int *locked, |
Andrea Arcangeli | 0fd71a5 | 2015-02-11 15:27:20 -0800 | [diff] [blame] | 1016 | unsigned int flags) |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1017 | { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1018 | long ret, pages_done; |
| 1019 | bool lock_dropped; |
| 1020 | |
| 1021 | if (locked) { |
| 1022 | /* if VM_FAULT_RETRY can be returned, vmas become invalid */ |
| 1023 | BUG_ON(vmas); |
| 1024 | /* check caller initialized locked */ |
| 1025 | BUG_ON(*locked != 1); |
| 1026 | } |
| 1027 | |
| 1028 | if (pages) |
| 1029 | flags |= FOLL_GET; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1030 | |
| 1031 | pages_done = 0; |
| 1032 | lock_dropped = false; |
| 1033 | for (;;) { |
| 1034 | ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages, |
| 1035 | vmas, locked); |
| 1036 | if (!locked) |
| 1037 | /* VM_FAULT_RETRY couldn't trigger, bypass */ |
| 1038 | return ret; |
| 1039 | |
| 1040 | /* VM_FAULT_RETRY cannot return errors */ |
| 1041 | if (!*locked) { |
| 1042 | BUG_ON(ret < 0); |
| 1043 | BUG_ON(ret >= nr_pages); |
| 1044 | } |
| 1045 | |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1046 | if (ret > 0) { |
| 1047 | nr_pages -= ret; |
| 1048 | pages_done += ret; |
| 1049 | if (!nr_pages) |
| 1050 | break; |
| 1051 | } |
| 1052 | if (*locked) { |
Andrea Arcangeli | 96312e6 | 2018-03-09 15:51:06 -0800 | [diff] [blame] | 1053 | /* |
| 1054 | * VM_FAULT_RETRY didn't trigger or it was a |
| 1055 | * FOLL_NOWAIT. |
| 1056 | */ |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1057 | if (!pages_done) |
| 1058 | pages_done = ret; |
| 1059 | break; |
| 1060 | } |
Mike Rapoport | df17277 | 2019-05-31 22:30:33 -0700 | [diff] [blame] | 1061 | /* |
| 1062 | * VM_FAULT_RETRY triggered, so seek to the faulting offset. |
| 1063 | * For the prefault case (!pages) we only update counts. |
| 1064 | */ |
| 1065 | if (likely(pages)) |
| 1066 | pages += ret; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1067 | start += ret << PAGE_SHIFT; |
| 1068 | |
| 1069 | /* |
| 1070 | * Repeat on the address that fired VM_FAULT_RETRY |
| 1071 | * without FAULT_FLAG_ALLOW_RETRY but with |
| 1072 | * FAULT_FLAG_TRIED. |
| 1073 | */ |
| 1074 | *locked = 1; |
| 1075 | lock_dropped = true; |
| 1076 | down_read(&mm->mmap_sem); |
| 1077 | ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED, |
| 1078 | pages, NULL, NULL); |
| 1079 | if (ret != 1) { |
| 1080 | BUG_ON(ret > 1); |
| 1081 | if (!pages_done) |
| 1082 | pages_done = ret; |
| 1083 | break; |
| 1084 | } |
| 1085 | nr_pages--; |
| 1086 | pages_done++; |
| 1087 | if (!nr_pages) |
| 1088 | break; |
Mike Rapoport | df17277 | 2019-05-31 22:30:33 -0700 | [diff] [blame] | 1089 | if (likely(pages)) |
| 1090 | pages++; |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1091 | start += PAGE_SIZE; |
| 1092 | } |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1093 | if (lock_dropped && *locked) { |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1094 | /* |
| 1095 | * We must let the caller know we temporarily dropped the lock |
| 1096 | * and so the critical section protected by it was lost. |
| 1097 | */ |
| 1098 | up_read(&mm->mmap_sem); |
| 1099 | *locked = 0; |
| 1100 | } |
| 1101 | return pages_done; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1105 | * get_user_pages_remote() - pin user pages in memory |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1106 | * @tsk: the task_struct to use for page fault accounting, or |
| 1107 | * NULL if faults are not to be recorded. |
| 1108 | * @mm: mm_struct of target mm |
| 1109 | * @start: starting user address |
| 1110 | * @nr_pages: number of pages from start to pin |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1111 | * @gup_flags: flags modifying lookup behaviour |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1112 | * @pages: array that receives pointers to the pages pinned. |
| 1113 | * Should be at least nr_pages long. Or NULL, if caller |
| 1114 | * only intends to ensure the pages are faulted in. |
| 1115 | * @vmas: array of pointers to vmas corresponding to each page. |
| 1116 | * Or NULL if the caller does not require them. |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1117 | * @locked: pointer to lock flag indicating whether lock is held and |
| 1118 | * subsequently whether VM_FAULT_RETRY functionality can be |
| 1119 | * utilised. Lock must initially be held. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1120 | * |
| 1121 | * Returns number of pages pinned. This may be fewer than the number |
| 1122 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 1123 | * were pinned, returns -errno. Each page returned must be released |
| 1124 | * with a put_page() call when it is finished with. vmas will only |
| 1125 | * remain valid while mmap_sem is held. |
| 1126 | * |
| 1127 | * Must be called with mmap_sem held for read or write. |
| 1128 | * |
| 1129 | * get_user_pages walks a process's page tables and takes a reference to |
| 1130 | * each struct page that each user address corresponds to at a given |
| 1131 | * instant. That is, it takes the page that would be accessed if a user |
| 1132 | * thread accesses the given user virtual address at that instant. |
| 1133 | * |
| 1134 | * This does not guarantee that the page exists in the user mappings when |
| 1135 | * get_user_pages returns, and there may even be a completely different |
| 1136 | * page there in some cases (eg. if mmapped pagecache has been invalidated |
| 1137 | * and subsequently re faulted). However it does guarantee that the page |
| 1138 | * won't be freed completely. And mostly callers simply care that the page |
| 1139 | * contains data that was valid *at some point in time*. Typically, an IO |
| 1140 | * or similar operation cannot guarantee anything stronger anyway because |
| 1141 | * locks can't be held over the syscall boundary. |
| 1142 | * |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1143 | * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page |
| 1144 | * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must |
| 1145 | * be called after the page is finished with, and before put_page is called. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1146 | * |
| 1147 | * get_user_pages is typically used for fewer-copy IO operations, to get a |
| 1148 | * handle on the memory by some means other than accesses via the user virtual |
| 1149 | * addresses. The pages may be submitted for DMA to devices or accessed via |
| 1150 | * their kernel linear mapping (via the kmap APIs). Care should be taken to |
| 1151 | * use the correct cache flushing APIs. |
| 1152 | * |
| 1153 | * See also get_user_pages_fast, for performance critical applications. |
Andrea Arcangeli | f0818f4 | 2015-02-11 15:27:17 -0800 | [diff] [blame] | 1154 | * |
| 1155 | * get_user_pages should be phased out in favor of |
| 1156 | * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing |
| 1157 | * should use get_user_pages because it cannot pass |
| 1158 | * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1159 | */ |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1160 | long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm, |
| 1161 | unsigned long start, unsigned long nr_pages, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1162 | unsigned int gup_flags, struct page **pages, |
Lorenzo Stoakes | 5b56d49 | 2016-12-14 15:06:52 -0800 | [diff] [blame] | 1163 | struct vm_area_struct **vmas, int *locked) |
Kirill A. Shutemov | 4bbd4c7 | 2014-06-04 16:08:10 -0700 | [diff] [blame] | 1164 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1167 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1168 | * vmas. As there are no users of this flag in this call we simply |
| 1169 | * disallow this option for now. |
| 1170 | */ |
| 1171 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1172 | return -EINVAL; |
| 1173 | |
Lorenzo Stoakes | 859110d | 2016-10-13 01:20:11 +0100 | [diff] [blame] | 1174 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
Al Viro | e716712 | 2017-11-19 11:32:05 -0500 | [diff] [blame] | 1175 | locked, |
Lorenzo Stoakes | 9beae1e | 2016-10-13 01:20:17 +0100 | [diff] [blame] | 1176 | gup_flags | FOLL_TOUCH | FOLL_REMOTE); |
Dave Hansen | 1e98779 | 2016-02-12 13:01:54 -0800 | [diff] [blame] | 1177 | } |
| 1178 | EXPORT_SYMBOL(get_user_pages_remote); |
| 1179 | |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1180 | /** |
| 1181 | * populate_vma_page_range() - populate a range of pages in the vma. |
| 1182 | * @vma: target vma |
| 1183 | * @start: start address |
| 1184 | * @end: end address |
| 1185 | * @nonblocking: |
| 1186 | * |
| 1187 | * This takes care of mlocking the pages too if VM_LOCKED is set. |
| 1188 | * |
| 1189 | * return 0 on success, negative error code on error. |
| 1190 | * |
| 1191 | * vma->vm_mm->mmap_sem must be held. |
| 1192 | * |
| 1193 | * If @nonblocking is NULL, it may be held for read or write and will |
| 1194 | * be unperturbed. |
| 1195 | * |
| 1196 | * If @nonblocking is non-NULL, it must held for read only and may be |
| 1197 | * released. If it's released, *@nonblocking will be set to 0. |
| 1198 | */ |
| 1199 | long populate_vma_page_range(struct vm_area_struct *vma, |
| 1200 | unsigned long start, unsigned long end, int *nonblocking) |
| 1201 | { |
| 1202 | struct mm_struct *mm = vma->vm_mm; |
| 1203 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
| 1204 | int gup_flags; |
| 1205 | |
| 1206 | VM_BUG_ON(start & ~PAGE_MASK); |
| 1207 | VM_BUG_ON(end & ~PAGE_MASK); |
| 1208 | VM_BUG_ON_VMA(start < vma->vm_start, vma); |
| 1209 | VM_BUG_ON_VMA(end > vma->vm_end, vma); |
| 1210 | VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm); |
| 1211 | |
| 1212 | gup_flags = FOLL_TOUCH | FOLL_POPULATE | FOLL_MLOCK; |
| 1213 | if (vma->vm_flags & VM_LOCKONFAULT) |
| 1214 | gup_flags &= ~FOLL_POPULATE; |
| 1215 | /* |
| 1216 | * We want to touch writable mappings with a write fault in order |
| 1217 | * to break COW, except for shared mappings because these don't COW |
| 1218 | * and we would not want to dirty them for nothing. |
| 1219 | */ |
| 1220 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) |
| 1221 | gup_flags |= FOLL_WRITE; |
| 1222 | |
| 1223 | /* |
| 1224 | * We want mlock to succeed for regions that have any permissions |
| 1225 | * other than PROT_NONE. |
| 1226 | */ |
| 1227 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) |
| 1228 | gup_flags |= FOLL_FORCE; |
| 1229 | |
| 1230 | /* |
| 1231 | * We made sure addr is within a VMA, so the following will |
| 1232 | * not result in a stack expansion that recurses back here. |
| 1233 | */ |
| 1234 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, |
| 1235 | NULL, NULL, nonblocking); |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * __mm_populate - populate and/or mlock pages within a range of address space. |
| 1240 | * |
| 1241 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap |
| 1242 | * flags. VMAs must be already marked with the desired vm_flags, and |
| 1243 | * mmap_sem must not be held. |
| 1244 | */ |
| 1245 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) |
| 1246 | { |
| 1247 | struct mm_struct *mm = current->mm; |
| 1248 | unsigned long end, nstart, nend; |
| 1249 | struct vm_area_struct *vma = NULL; |
| 1250 | int locked = 0; |
| 1251 | long ret = 0; |
| 1252 | |
| 1253 | end = start + len; |
| 1254 | |
| 1255 | for (nstart = start; nstart < end; nstart = nend) { |
| 1256 | /* |
| 1257 | * We want to fault in pages for [nstart; end) address range. |
| 1258 | * Find first corresponding VMA. |
| 1259 | */ |
| 1260 | if (!locked) { |
| 1261 | locked = 1; |
| 1262 | down_read(&mm->mmap_sem); |
| 1263 | vma = find_vma(mm, nstart); |
| 1264 | } else if (nstart >= vma->vm_end) |
| 1265 | vma = vma->vm_next; |
| 1266 | if (!vma || vma->vm_start >= end) |
| 1267 | break; |
| 1268 | /* |
| 1269 | * Set [nstart; nend) to intersection of desired address |
| 1270 | * range with the first VMA. Also, skip undesirable VMA types. |
| 1271 | */ |
| 1272 | nend = min(end, vma->vm_end); |
| 1273 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) |
| 1274 | continue; |
| 1275 | if (nstart < vma->vm_start) |
| 1276 | nstart = vma->vm_start; |
| 1277 | /* |
| 1278 | * Now fault in a range of pages. populate_vma_page_range() |
| 1279 | * double checks the vma flags, so that it won't mlock pages |
| 1280 | * if the vma was already munlocked. |
| 1281 | */ |
| 1282 | ret = populate_vma_page_range(vma, nstart, nend, &locked); |
| 1283 | if (ret < 0) { |
| 1284 | if (ignore_errors) { |
| 1285 | ret = 0; |
| 1286 | continue; /* continue at next VMA */ |
| 1287 | } |
| 1288 | break; |
| 1289 | } |
| 1290 | nend = nstart + ret * PAGE_SIZE; |
| 1291 | ret = 0; |
| 1292 | } |
| 1293 | if (locked) |
| 1294 | up_read(&mm->mmap_sem); |
| 1295 | return ret; /* 0 or negative error code */ |
| 1296 | } |
| 1297 | |
| 1298 | /** |
| 1299 | * get_dump_page() - pin user page in memory while writing it to core dump |
| 1300 | * @addr: user address |
| 1301 | * |
| 1302 | * Returns struct page pointer of user page pinned for dump, |
| 1303 | * to be freed afterwards by put_page(). |
| 1304 | * |
| 1305 | * Returns NULL on any kind of failure - a hole must then be inserted into |
| 1306 | * the corefile, to preserve alignment with its headers; and also returns |
| 1307 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - |
| 1308 | * allowing a hole to be left in the corefile to save diskspace. |
| 1309 | * |
| 1310 | * Called without mmap_sem, but after all other threads have been killed. |
| 1311 | */ |
| 1312 | #ifdef CONFIG_ELF_CORE |
| 1313 | struct page *get_dump_page(unsigned long addr) |
| 1314 | { |
| 1315 | struct vm_area_struct *vma; |
| 1316 | struct page *page; |
| 1317 | |
| 1318 | if (__get_user_pages(current, current->mm, addr, 1, |
| 1319 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, |
| 1320 | NULL) < 1) |
| 1321 | return NULL; |
| 1322 | flush_cache_page(vma, addr, page_to_pfn(page)); |
| 1323 | return page; |
| 1324 | } |
| 1325 | #endif /* CONFIG_ELF_CORE */ |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 1326 | #else /* CONFIG_MMU */ |
| 1327 | static long __get_user_pages_locked(struct task_struct *tsk, |
| 1328 | struct mm_struct *mm, unsigned long start, |
| 1329 | unsigned long nr_pages, struct page **pages, |
| 1330 | struct vm_area_struct **vmas, int *locked, |
| 1331 | unsigned int foll_flags) |
| 1332 | { |
| 1333 | struct vm_area_struct *vma; |
| 1334 | unsigned long vm_flags; |
| 1335 | int i; |
| 1336 | |
| 1337 | /* calculate required read or write permissions. |
| 1338 | * If FOLL_FORCE is set, we only require the "MAY" flags. |
| 1339 | */ |
| 1340 | vm_flags = (foll_flags & FOLL_WRITE) ? |
| 1341 | (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); |
| 1342 | vm_flags &= (foll_flags & FOLL_FORCE) ? |
| 1343 | (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); |
| 1344 | |
| 1345 | for (i = 0; i < nr_pages; i++) { |
| 1346 | vma = find_vma(mm, start); |
| 1347 | if (!vma) |
| 1348 | goto finish_or_fault; |
| 1349 | |
| 1350 | /* protect what we can, including chardevs */ |
| 1351 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || |
| 1352 | !(vm_flags & vma->vm_flags)) |
| 1353 | goto finish_or_fault; |
| 1354 | |
| 1355 | if (pages) { |
| 1356 | pages[i] = virt_to_page(start); |
| 1357 | if (pages[i]) |
| 1358 | get_page(pages[i]); |
| 1359 | } |
| 1360 | if (vmas) |
| 1361 | vmas[i] = vma; |
| 1362 | start = (start + PAGE_SIZE) & PAGE_MASK; |
| 1363 | } |
| 1364 | |
| 1365 | return i; |
| 1366 | |
| 1367 | finish_or_fault: |
| 1368 | return i ? : -EFAULT; |
| 1369 | } |
| 1370 | #endif /* !CONFIG_MMU */ |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1371 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1372 | #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1373 | static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages) |
| 1374 | { |
| 1375 | long i; |
| 1376 | struct vm_area_struct *vma_prev = NULL; |
| 1377 | |
| 1378 | for (i = 0; i < nr_pages; i++) { |
| 1379 | struct vm_area_struct *vma = vmas[i]; |
| 1380 | |
| 1381 | if (vma == vma_prev) |
| 1382 | continue; |
| 1383 | |
| 1384 | vma_prev = vma; |
| 1385 | |
| 1386 | if (vma_is_fsdax(vma)) |
| 1387 | return true; |
| 1388 | } |
| 1389 | return false; |
| 1390 | } |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1391 | |
| 1392 | #ifdef CONFIG_CMA |
| 1393 | static struct page *new_non_cma_page(struct page *page, unsigned long private) |
| 1394 | { |
| 1395 | /* |
| 1396 | * We want to make sure we allocate the new page from the same node |
| 1397 | * as the source page. |
| 1398 | */ |
| 1399 | int nid = page_to_nid(page); |
| 1400 | /* |
| 1401 | * Trying to allocate a page for migration. Ignore allocation |
| 1402 | * failure warnings. We don't force __GFP_THISNODE here because |
| 1403 | * this node here is the node where we have CMA reservation and |
| 1404 | * in some case these nodes will have really less non movable |
| 1405 | * allocation memory. |
| 1406 | */ |
| 1407 | gfp_t gfp_mask = GFP_USER | __GFP_NOWARN; |
| 1408 | |
| 1409 | if (PageHighMem(page)) |
| 1410 | gfp_mask |= __GFP_HIGHMEM; |
| 1411 | |
| 1412 | #ifdef CONFIG_HUGETLB_PAGE |
| 1413 | if (PageHuge(page)) { |
| 1414 | struct hstate *h = page_hstate(page); |
| 1415 | /* |
| 1416 | * We don't want to dequeue from the pool because pool pages will |
| 1417 | * mostly be from the CMA region. |
| 1418 | */ |
| 1419 | return alloc_migrate_huge_page(h, gfp_mask, nid, NULL); |
| 1420 | } |
| 1421 | #endif |
| 1422 | if (PageTransHuge(page)) { |
| 1423 | struct page *thp; |
| 1424 | /* |
| 1425 | * ignore allocation failure warnings |
| 1426 | */ |
| 1427 | gfp_t thp_gfpmask = GFP_TRANSHUGE | __GFP_NOWARN; |
| 1428 | |
| 1429 | /* |
| 1430 | * Remove the movable mask so that we don't allocate from |
| 1431 | * CMA area again. |
| 1432 | */ |
| 1433 | thp_gfpmask &= ~__GFP_MOVABLE; |
| 1434 | thp = __alloc_pages_node(nid, thp_gfpmask, HPAGE_PMD_ORDER); |
| 1435 | if (!thp) |
| 1436 | return NULL; |
| 1437 | prep_transhuge_page(thp); |
| 1438 | return thp; |
| 1439 | } |
| 1440 | |
| 1441 | return __alloc_pages_node(nid, gfp_mask, 0); |
| 1442 | } |
| 1443 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1444 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1445 | struct mm_struct *mm, |
| 1446 | unsigned long start, |
| 1447 | unsigned long nr_pages, |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1448 | struct page **pages, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1449 | struct vm_area_struct **vmas, |
| 1450 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1451 | { |
| 1452 | long i; |
| 1453 | bool drain_allow = true; |
| 1454 | bool migrate_allow = true; |
| 1455 | LIST_HEAD(cma_page_list); |
| 1456 | |
| 1457 | check_again: |
| 1458 | for (i = 0; i < nr_pages; i++) { |
| 1459 | /* |
| 1460 | * If we get a page from the CMA zone, since we are going to |
| 1461 | * be pinning these entries, we might as well move them out |
| 1462 | * of the CMA zone if possible. |
| 1463 | */ |
| 1464 | if (is_migrate_cma_page(pages[i])) { |
| 1465 | |
| 1466 | struct page *head = compound_head(pages[i]); |
| 1467 | |
| 1468 | if (PageHuge(head)) { |
| 1469 | isolate_huge_page(head, &cma_page_list); |
| 1470 | } else { |
| 1471 | if (!PageLRU(head) && drain_allow) { |
| 1472 | lru_add_drain_all(); |
| 1473 | drain_allow = false; |
| 1474 | } |
| 1475 | |
| 1476 | if (!isolate_lru_page(head)) { |
| 1477 | list_add_tail(&head->lru, &cma_page_list); |
| 1478 | mod_node_page_state(page_pgdat(head), |
| 1479 | NR_ISOLATED_ANON + |
| 1480 | page_is_file_cache(head), |
| 1481 | hpage_nr_pages(head)); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | if (!list_empty(&cma_page_list)) { |
| 1488 | /* |
| 1489 | * drop the above get_user_pages reference. |
| 1490 | */ |
| 1491 | for (i = 0; i < nr_pages; i++) |
| 1492 | put_page(pages[i]); |
| 1493 | |
| 1494 | if (migrate_pages(&cma_page_list, new_non_cma_page, |
| 1495 | NULL, 0, MIGRATE_SYNC, MR_CONTIG_RANGE)) { |
| 1496 | /* |
| 1497 | * some of the pages failed migration. Do get_user_pages |
| 1498 | * without migration. |
| 1499 | */ |
| 1500 | migrate_allow = false; |
| 1501 | |
| 1502 | if (!list_empty(&cma_page_list)) |
| 1503 | putback_movable_pages(&cma_page_list); |
| 1504 | } |
| 1505 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1506 | * We did migrate all the pages, Try to get the page references |
| 1507 | * again migrating any new CMA pages which we failed to isolate |
| 1508 | * earlier. |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1509 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1510 | nr_pages = __get_user_pages_locked(tsk, mm, start, nr_pages, |
| 1511 | pages, vmas, NULL, |
| 1512 | gup_flags); |
| 1513 | |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1514 | if ((nr_pages > 0) && migrate_allow) { |
| 1515 | drain_allow = true; |
| 1516 | goto check_again; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | return nr_pages; |
| 1521 | } |
| 1522 | #else |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1523 | static long check_and_migrate_cma_pages(struct task_struct *tsk, |
| 1524 | struct mm_struct *mm, |
| 1525 | unsigned long start, |
| 1526 | unsigned long nr_pages, |
| 1527 | struct page **pages, |
| 1528 | struct vm_area_struct **vmas, |
| 1529 | unsigned int gup_flags) |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1530 | { |
| 1531 | return nr_pages; |
| 1532 | } |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 1533 | #endif /* CONFIG_CMA */ |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1534 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1535 | /* |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1536 | * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which |
| 1537 | * allows us to process the FOLL_LONGTERM flag. |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1538 | */ |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1539 | static long __gup_longterm_locked(struct task_struct *tsk, |
| 1540 | struct mm_struct *mm, |
| 1541 | unsigned long start, |
| 1542 | unsigned long nr_pages, |
| 1543 | struct page **pages, |
| 1544 | struct vm_area_struct **vmas, |
| 1545 | unsigned int gup_flags) |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1546 | { |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1547 | struct vm_area_struct **vmas_tmp = vmas; |
| 1548 | unsigned long flags = 0; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1549 | long rc, i; |
| 1550 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1551 | if (gup_flags & FOLL_LONGTERM) { |
| 1552 | if (!pages) |
| 1553 | return -EINVAL; |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1554 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1555 | if (!vmas_tmp) { |
| 1556 | vmas_tmp = kcalloc(nr_pages, |
| 1557 | sizeof(struct vm_area_struct *), |
| 1558 | GFP_KERNEL); |
| 1559 | if (!vmas_tmp) |
| 1560 | return -ENOMEM; |
| 1561 | } |
| 1562 | flags = memalloc_nocma_save(); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1563 | } |
| 1564 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1565 | rc = __get_user_pages_locked(tsk, mm, start, nr_pages, pages, |
| 1566 | vmas_tmp, NULL, gup_flags); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1567 | |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1568 | if (gup_flags & FOLL_LONGTERM) { |
| 1569 | memalloc_nocma_restore(flags); |
| 1570 | if (rc < 0) |
| 1571 | goto out; |
| 1572 | |
| 1573 | if (check_dax_vmas(vmas_tmp, rc)) { |
| 1574 | for (i = 0; i < rc; i++) |
| 1575 | put_page(pages[i]); |
| 1576 | rc = -EOPNOTSUPP; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | |
| 1580 | rc = check_and_migrate_cma_pages(tsk, mm, start, rc, pages, |
| 1581 | vmas_tmp, gup_flags); |
Aneesh Kumar K.V | 9a4e9f3 | 2019-03-05 15:47:44 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1584 | out: |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1585 | if (vmas_tmp != vmas) |
| 1586 | kfree(vmas_tmp); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1587 | return rc; |
| 1588 | } |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 1589 | #else /* !CONFIG_FS_DAX && !CONFIG_CMA */ |
| 1590 | static __always_inline long __gup_longterm_locked(struct task_struct *tsk, |
| 1591 | struct mm_struct *mm, |
| 1592 | unsigned long start, |
| 1593 | unsigned long nr_pages, |
| 1594 | struct page **pages, |
| 1595 | struct vm_area_struct **vmas, |
| 1596 | unsigned int flags) |
| 1597 | { |
| 1598 | return __get_user_pages_locked(tsk, mm, start, nr_pages, pages, vmas, |
| 1599 | NULL, flags); |
| 1600 | } |
| 1601 | #endif /* CONFIG_FS_DAX || CONFIG_CMA */ |
| 1602 | |
| 1603 | /* |
| 1604 | * This is the same as get_user_pages_remote(), just with a |
| 1605 | * less-flexible calling convention where we assume that the task |
| 1606 | * and mm being operated on are the current task's and don't allow |
| 1607 | * passing of a locked parameter. We also obviously don't pass |
| 1608 | * FOLL_REMOTE in here. |
| 1609 | */ |
| 1610 | long get_user_pages(unsigned long start, unsigned long nr_pages, |
| 1611 | unsigned int gup_flags, struct page **pages, |
| 1612 | struct vm_area_struct **vmas) |
| 1613 | { |
| 1614 | return __gup_longterm_locked(current, current->mm, start, nr_pages, |
| 1615 | pages, vmas, gup_flags | FOLL_TOUCH); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(get_user_pages); |
Dan Williams | 2bb6d28 | 2017-11-29 16:10:35 -0800 | [diff] [blame] | 1618 | |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1619 | /* |
| 1620 | * We can leverage the VM_FAULT_RETRY functionality in the page fault |
| 1621 | * paths better by using either get_user_pages_locked() or |
| 1622 | * get_user_pages_unlocked(). |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1623 | * |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1624 | * get_user_pages_locked() is suitable to replace the form: |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1625 | * |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1626 | * down_read(&mm->mmap_sem); |
| 1627 | * do_something() |
| 1628 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1629 | * up_read(&mm->mmap_sem); |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1630 | * |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1631 | * to: |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1632 | * |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1633 | * int locked = 1; |
| 1634 | * down_read(&mm->mmap_sem); |
| 1635 | * do_something() |
| 1636 | * get_user_pages_locked(tsk, mm, ..., pages, &locked); |
| 1637 | * if (locked) |
| 1638 | * up_read(&mm->mmap_sem); |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1639 | */ |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1640 | long get_user_pages_locked(unsigned long start, unsigned long nr_pages, |
| 1641 | unsigned int gup_flags, struct page **pages, |
| 1642 | int *locked) |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1643 | { |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1644 | /* |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1645 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1646 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1647 | * vmas. As there are no users of this flag in this call we simply |
| 1648 | * disallow this option for now. |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1649 | */ |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1650 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1651 | return -EINVAL; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1652 | |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1653 | return __get_user_pages_locked(current, current->mm, start, nr_pages, |
| 1654 | pages, NULL, locked, |
| 1655 | gup_flags | FOLL_TOUCH); |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1656 | } |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1657 | EXPORT_SYMBOL(get_user_pages_locked); |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1658 | |
| 1659 | /* |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1660 | * get_user_pages_unlocked() is suitable to replace the form: |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1661 | * |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1662 | * down_read(&mm->mmap_sem); |
| 1663 | * get_user_pages(tsk, mm, ..., pages, NULL); |
| 1664 | * up_read(&mm->mmap_sem); |
| 1665 | * |
| 1666 | * with: |
| 1667 | * |
| 1668 | * get_user_pages_unlocked(tsk, mm, ..., pages); |
| 1669 | * |
| 1670 | * It is functionally equivalent to get_user_pages_fast so |
| 1671 | * get_user_pages_fast should be used instead if specific gup_flags |
| 1672 | * (e.g. FOLL_FORCE) are not required. |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1673 | */ |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1674 | long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, |
| 1675 | struct page **pages, unsigned int gup_flags) |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1676 | { |
| 1677 | struct mm_struct *mm = current->mm; |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1678 | int locked = 1; |
| 1679 | long ret; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1680 | |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1681 | /* |
| 1682 | * FIXME: Current FOLL_LONGTERM behavior is incompatible with |
| 1683 | * FAULT_FLAG_ALLOW_RETRY because of the FS DAX check requirement on |
| 1684 | * vmas. As there are no users of this flag in this call we simply |
| 1685 | * disallow this option for now. |
| 1686 | */ |
| 1687 | if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) |
| 1688 | return -EINVAL; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1689 | |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1690 | down_read(&mm->mmap_sem); |
| 1691 | ret = __get_user_pages_locked(current, mm, start, nr_pages, pages, NULL, |
| 1692 | &locked, gup_flags | FOLL_TOUCH); |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1693 | if (locked) |
| 1694 | up_read(&mm->mmap_sem); |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1695 | return ret; |
Kirill A. Shutemov | acc3c8d | 2015-04-14 15:44:45 -0700 | [diff] [blame] | 1696 | } |
Christoph Hellwig | d3649f6 | 2019-07-11 20:57:18 -0700 | [diff] [blame] | 1697 | EXPORT_SYMBOL(get_user_pages_unlocked); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1698 | |
| 1699 | /* |
Christoph Hellwig | 67a929e | 2019-07-11 20:57:14 -0700 | [diff] [blame] | 1700 | * Fast GUP |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1701 | * |
| 1702 | * get_user_pages_fast attempts to pin user pages by walking the page |
| 1703 | * tables directly and avoids taking locks. Thus the walker needs to be |
| 1704 | * protected from page table pages being freed from under it, and should |
| 1705 | * block any THP splits. |
| 1706 | * |
| 1707 | * One way to achieve this is to have the walker disable interrupts, and |
| 1708 | * rely on IPIs from the TLB flushing code blocking before the page table |
| 1709 | * pages are freed. This is unsuitable for architectures that do not need |
| 1710 | * to broadcast an IPI when invalidating TLBs. |
| 1711 | * |
| 1712 | * Another way to achieve this is to batch up page table containing pages |
| 1713 | * belonging to more than one mm_user, then rcu_sched a callback to free those |
| 1714 | * pages. Disabling interrupts will allow the fast_gup walker to both block |
| 1715 | * the rcu_sched callback, and an IPI that we broadcast for splitting THPs |
| 1716 | * (which is a relatively rare event). The code below adopts this strategy. |
| 1717 | * |
| 1718 | * Before activating this code, please be aware that the following assumptions |
| 1719 | * are currently made: |
| 1720 | * |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1721 | * *) Either HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to |
| 1722 | * free pages containing page tables or TLB flushing requires IPI broadcast. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1723 | * |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1724 | * *) ptes can be read atomically by the architecture. |
| 1725 | * |
| 1726 | * *) access_ok is sufficient to validate userspace address ranges. |
| 1727 | * |
| 1728 | * The last two assumptions can be relaxed by the addition of helper functions. |
| 1729 | * |
| 1730 | * This code is based heavily on the PowerPC implementation by Nick Piggin. |
| 1731 | */ |
Christoph Hellwig | 67a929e | 2019-07-11 20:57:14 -0700 | [diff] [blame] | 1732 | #ifdef CONFIG_HAVE_FAST_GUP |
Christoph Hellwig | 39656e8 | 2019-07-11 20:56:49 -0700 | [diff] [blame] | 1733 | #ifdef CONFIG_GUP_GET_PTE_LOW_HIGH |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1734 | /* |
Christoph Hellwig | 39656e8 | 2019-07-11 20:56:49 -0700 | [diff] [blame] | 1735 | * WARNING: only to be used in the get_user_pages_fast() implementation. |
| 1736 | * |
| 1737 | * With get_user_pages_fast(), we walk down the pagetables without taking any |
| 1738 | * locks. For this we would like to load the pointers atomically, but sometimes |
| 1739 | * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What |
| 1740 | * we do have is the guarantee that a PTE will only either go from not present |
| 1741 | * to present, or present to not present or both -- it will not switch to a |
| 1742 | * completely different present page without a TLB flush in between; something |
| 1743 | * that we are blocking by holding interrupts off. |
| 1744 | * |
| 1745 | * Setting ptes from not present to present goes: |
| 1746 | * |
| 1747 | * ptep->pte_high = h; |
| 1748 | * smp_wmb(); |
| 1749 | * ptep->pte_low = l; |
| 1750 | * |
| 1751 | * And present to not present goes: |
| 1752 | * |
| 1753 | * ptep->pte_low = 0; |
| 1754 | * smp_wmb(); |
| 1755 | * ptep->pte_high = 0; |
| 1756 | * |
| 1757 | * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'. |
| 1758 | * We load pte_high *after* loading pte_low, which ensures we don't see an older |
| 1759 | * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't |
| 1760 | * picked up a changed pte high. We might have gotten rubbish values from |
| 1761 | * pte_low and pte_high, but we are guaranteed that pte_low will not have the |
| 1762 | * present bit set *unless* it is 'l'. Because get_user_pages_fast() only |
| 1763 | * operates on present ptes we're safe. |
| 1764 | */ |
| 1765 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 1766 | { |
| 1767 | pte_t pte; |
| 1768 | |
| 1769 | do { |
| 1770 | pte.pte_low = ptep->pte_low; |
| 1771 | smp_rmb(); |
| 1772 | pte.pte_high = ptep->pte_high; |
| 1773 | smp_rmb(); |
| 1774 | } while (unlikely(pte.pte_low != ptep->pte_low)); |
| 1775 | |
| 1776 | return pte; |
| 1777 | } |
| 1778 | #else /* CONFIG_GUP_GET_PTE_LOW_HIGH */ |
| 1779 | /* |
| 1780 | * We require that the PTE can be read atomically. |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1781 | */ |
| 1782 | static inline pte_t gup_get_pte(pte_t *ptep) |
| 1783 | { |
| 1784 | return READ_ONCE(*ptep); |
| 1785 | } |
Christoph Hellwig | 39656e8 | 2019-07-11 20:56:49 -0700 | [diff] [blame] | 1786 | #endif /* CONFIG_GUP_GET_PTE_LOW_HIGH */ |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1787 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1788 | static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages) |
| 1789 | { |
| 1790 | while ((*nr) - nr_start) { |
| 1791 | struct page *page = pages[--(*nr)]; |
| 1792 | |
| 1793 | ClearPageReferenced(page); |
| 1794 | put_page(page); |
| 1795 | } |
| 1796 | } |
| 1797 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1798 | /* |
| 1799 | * Return the compund head page with ref appropriately incremented, |
| 1800 | * or NULL if that failed. |
| 1801 | */ |
| 1802 | static inline struct page *try_get_compound_head(struct page *page, int refs) |
| 1803 | { |
| 1804 | struct page *head = compound_head(page); |
| 1805 | if (WARN_ON_ONCE(page_ref_count(head) < 0)) |
| 1806 | return NULL; |
| 1807 | if (unlikely(!page_cache_add_speculative(head, refs))) |
| 1808 | return NULL; |
| 1809 | return head; |
| 1810 | } |
| 1811 | |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1812 | #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1813 | 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] | 1814 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1815 | { |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1816 | struct dev_pagemap *pgmap = NULL; |
| 1817 | int nr_start = *nr, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1818 | pte_t *ptep, *ptem; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1819 | |
| 1820 | ptem = ptep = pte_offset_map(&pmd, addr); |
| 1821 | do { |
Kirill A. Shutemov | 0005d20 | 2017-03-16 18:26:51 +0300 | [diff] [blame] | 1822 | pte_t pte = gup_get_pte(ptep); |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1823 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1824 | |
| 1825 | /* |
| 1826 | * Similar to the PMD case below, NUMA hinting must take slow |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 1827 | * path using the pte_protnone check. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1828 | */ |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1829 | if (pte_protnone(pte)) |
| 1830 | goto pte_unmap; |
| 1831 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1832 | if (!pte_access_permitted(pte, flags & FOLL_WRITE)) |
Kirill A. Shutemov | e7884f8 | 2017-03-16 18:26:50 +0300 | [diff] [blame] | 1833 | goto pte_unmap; |
| 1834 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1835 | if (pte_devmap(pte)) { |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1836 | if (unlikely(flags & FOLL_LONGTERM)) |
| 1837 | goto pte_unmap; |
| 1838 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1839 | pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); |
| 1840 | if (unlikely(!pgmap)) { |
| 1841 | undo_dev_pagemap(nr, nr_start, pages); |
| 1842 | goto pte_unmap; |
| 1843 | } |
| 1844 | } else if (pte_special(pte)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1845 | goto pte_unmap; |
| 1846 | |
| 1847 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 1848 | page = pte_page(pte); |
| 1849 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1850 | head = try_get_compound_head(page, 1); |
| 1851 | if (!head) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1852 | goto pte_unmap; |
| 1853 | |
| 1854 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1855 | put_page(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1856 | goto pte_unmap; |
| 1857 | } |
| 1858 | |
Kirill A. Shutemov | 7aef417 | 2016-01-15 16:52:32 -0800 | [diff] [blame] | 1859 | VM_BUG_ON_PAGE(compound_head(page) != head, page); |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 1860 | |
| 1861 | SetPageReferenced(page); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1862 | pages[*nr] = page; |
| 1863 | (*nr)++; |
| 1864 | |
| 1865 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
| 1866 | |
| 1867 | ret = 1; |
| 1868 | |
| 1869 | pte_unmap: |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1870 | if (pgmap) |
| 1871 | put_dev_pagemap(pgmap); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1872 | pte_unmap(ptem); |
| 1873 | return ret; |
| 1874 | } |
| 1875 | #else |
| 1876 | |
| 1877 | /* |
| 1878 | * If we can't determine whether or not a pte is special, then fail immediately |
| 1879 | * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not |
| 1880 | * to be special. |
| 1881 | * |
| 1882 | * For a futex to be placed on a THP tail page, get_futex_key requires a |
| 1883 | * __get_user_pages_fast implementation that can pin pages. Thus it's still |
| 1884 | * useful to have gup_huge_pmd even if we can't operate on ptes. |
| 1885 | */ |
| 1886 | 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] | 1887 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1888 | { |
| 1889 | return 0; |
| 1890 | } |
Laurent Dufour | 3010a5e | 2018-06-07 17:06:08 -0700 | [diff] [blame] | 1891 | #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1892 | |
Oliver O'Halloran | 09180ca | 2017-09-06 16:20:58 -0700 | [diff] [blame] | 1893 | #if defined(__HAVE_ARCH_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1894 | static int __gup_device_huge(unsigned long pfn, unsigned long addr, |
| 1895 | unsigned long end, struct page **pages, int *nr) |
| 1896 | { |
| 1897 | int nr_start = *nr; |
| 1898 | struct dev_pagemap *pgmap = NULL; |
| 1899 | |
| 1900 | do { |
| 1901 | struct page *page = pfn_to_page(pfn); |
| 1902 | |
| 1903 | pgmap = get_dev_pagemap(pfn, pgmap); |
| 1904 | if (unlikely(!pgmap)) { |
| 1905 | undo_dev_pagemap(nr, nr_start, pages); |
| 1906 | return 0; |
| 1907 | } |
| 1908 | SetPageReferenced(page); |
| 1909 | pages[*nr] = page; |
| 1910 | get_page(page); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1911 | (*nr)++; |
| 1912 | pfn++; |
| 1913 | } while (addr += PAGE_SIZE, addr != end); |
Christoph Hellwig | 832d7aa | 2017-12-29 08:54:01 +0100 | [diff] [blame] | 1914 | |
| 1915 | if (pgmap) |
| 1916 | put_dev_pagemap(pgmap); |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1917 | return 1; |
| 1918 | } |
| 1919 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1920 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1921 | unsigned long end, struct page **pages, int *nr) |
| 1922 | { |
| 1923 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1924 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1925 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1926 | fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
| 1927 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1928 | return 0; |
| 1929 | |
| 1930 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 1931 | undo_dev_pagemap(nr, nr_start, pages); |
| 1932 | return 0; |
| 1933 | } |
| 1934 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1935 | } |
| 1936 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1937 | static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1938 | unsigned long end, struct page **pages, int *nr) |
| 1939 | { |
| 1940 | unsigned long fault_pfn; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1941 | int nr_start = *nr; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1942 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1943 | fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
| 1944 | if (!__gup_device_huge(fault_pfn, addr, end, pages, nr)) |
| 1945 | return 0; |
| 1946 | |
| 1947 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 1948 | undo_dev_pagemap(nr, nr_start, pages); |
| 1949 | return 0; |
| 1950 | } |
| 1951 | return 1; |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1952 | } |
| 1953 | #else |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1954 | static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1955 | unsigned long end, struct page **pages, int *nr) |
| 1956 | { |
| 1957 | BUILD_BUG(); |
| 1958 | return 0; |
| 1959 | } |
| 1960 | |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1961 | static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1962 | unsigned long end, struct page **pages, int *nr) |
| 1963 | { |
| 1964 | BUILD_BUG(); |
| 1965 | return 0; |
| 1966 | } |
| 1967 | #endif |
| 1968 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1969 | static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1970 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1971 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 1972 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1973 | int refs; |
| 1974 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 1975 | if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1976 | return 0; |
| 1977 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1978 | if (pmd_devmap(orig)) { |
| 1979 | if (unlikely(flags & FOLL_LONGTERM)) |
| 1980 | return 0; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 1981 | return __gup_device_huge_pmd(orig, pmdp, addr, end, pages, nr); |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 1982 | } |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 1983 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1984 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 1985 | page = pmd_page(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1986 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1987 | pages[*nr] = page; |
| 1988 | (*nr)++; |
| 1989 | page++; |
| 1990 | refs++; |
| 1991 | } while (addr += PAGE_SIZE, addr != end); |
| 1992 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 1993 | head = try_get_compound_head(pmd_page(orig), refs); |
| 1994 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 1995 | *nr -= refs; |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { |
| 2000 | *nr -= refs; |
| 2001 | while (refs--) |
| 2002 | put_page(head); |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 2006 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2007 | return 1; |
| 2008 | } |
| 2009 | |
| 2010 | static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2011 | unsigned long end, unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2012 | { |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 2013 | struct page *head, *page; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2014 | int refs; |
| 2015 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2016 | if (!pud_access_permitted(orig, flags & FOLL_WRITE)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2017 | return 0; |
| 2018 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2019 | if (pud_devmap(orig)) { |
| 2020 | if (unlikely(flags & FOLL_LONGTERM)) |
| 2021 | return 0; |
Dan Williams | a9b6de7 | 2018-04-19 21:32:19 -0700 | [diff] [blame] | 2022 | return __gup_device_huge_pud(orig, pudp, addr, end, pages, nr); |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2023 | } |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 2024 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2025 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 2026 | page = pud_page(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2027 | do { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2028 | pages[*nr] = page; |
| 2029 | (*nr)++; |
| 2030 | page++; |
| 2031 | refs++; |
| 2032 | } while (addr += PAGE_SIZE, addr != end); |
| 2033 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 2034 | head = try_get_compound_head(pud_page(orig), refs); |
| 2035 | if (!head) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2036 | *nr -= refs; |
| 2037 | return 0; |
| 2038 | } |
| 2039 | |
| 2040 | if (unlikely(pud_val(orig) != pud_val(*pudp))) { |
| 2041 | *nr -= refs; |
| 2042 | while (refs--) |
| 2043 | put_page(head); |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 2047 | SetPageReferenced(head); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2048 | return 1; |
| 2049 | } |
| 2050 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2051 | 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] | 2052 | unsigned long end, unsigned int flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2053 | struct page **pages, int *nr) |
| 2054 | { |
| 2055 | int refs; |
Kirill A. Shutemov | ddc58f2 | 2016-01-15 16:52:56 -0800 | [diff] [blame] | 2056 | struct page *head, *page; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2057 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2058 | if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2059 | return 0; |
| 2060 | |
Kirill A. Shutemov | b59f65f | 2017-03-16 18:26:53 +0300 | [diff] [blame] | 2061 | BUILD_BUG_ON(pgd_devmap(orig)); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2062 | refs = 0; |
Punit Agrawal | d63206e | 2017-07-06 15:39:39 -0700 | [diff] [blame] | 2063 | page = pgd_page(orig) + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2064 | do { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2065 | pages[*nr] = page; |
| 2066 | (*nr)++; |
| 2067 | page++; |
| 2068 | refs++; |
| 2069 | } while (addr += PAGE_SIZE, addr != end); |
| 2070 | |
Linus Torvalds | 8fde12c | 2019-04-11 10:49:19 -0700 | [diff] [blame] | 2071 | head = try_get_compound_head(pgd_page(orig), refs); |
| 2072 | if (!head) { |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2073 | *nr -= refs; |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { |
| 2078 | *nr -= refs; |
| 2079 | while (refs--) |
| 2080 | put_page(head); |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
Kirill A. Shutemov | e934805 | 2017-03-16 18:26:52 +0300 | [diff] [blame] | 2084 | SetPageReferenced(head); |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2085 | return 1; |
| 2086 | } |
| 2087 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2088 | static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2089 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2090 | { |
| 2091 | unsigned long next; |
| 2092 | pmd_t *pmdp; |
| 2093 | |
| 2094 | pmdp = pmd_offset(&pud, addr); |
| 2095 | do { |
Christian Borntraeger | 38c5ce9 | 2015-01-06 22:54:46 +0100 | [diff] [blame] | 2096 | pmd_t pmd = READ_ONCE(*pmdp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2097 | |
| 2098 | next = pmd_addr_end(addr, end); |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 2099 | if (!pmd_present(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2100 | return 0; |
| 2101 | |
Yu Zhao | 414fd08 | 2019-02-12 15:35:58 -0800 | [diff] [blame] | 2102 | if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || |
| 2103 | pmd_devmap(pmd))) { |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * NUMA hinting faults need to be handled in the GUP |
| 2106 | * slowpath for accounting purposes and so that they |
| 2107 | * can be serialised against THP migration. |
| 2108 | */ |
Mel Gorman | 8a0516e | 2015-02-12 14:58:22 -0800 | [diff] [blame] | 2109 | if (pmd_protnone(pmd)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2110 | return 0; |
| 2111 | |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2112 | if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2113 | pages, nr)) |
| 2114 | return 0; |
| 2115 | |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2116 | } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { |
| 2117 | /* |
| 2118 | * architecture have different format for hugetlbfs |
| 2119 | * pmd format and THP pmd format |
| 2120 | */ |
| 2121 | if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2122 | PMD_SHIFT, next, flags, pages, nr)) |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2123 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2124 | } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) |
Mario Leinweber | 2923117 | 2018-04-05 16:24:18 -0700 | [diff] [blame] | 2125 | return 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2126 | } while (pmdp++, addr = next, addr != end); |
| 2127 | |
| 2128 | return 1; |
| 2129 | } |
| 2130 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2131 | static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2132 | unsigned int flags, struct page **pages, int *nr) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2133 | { |
| 2134 | unsigned long next; |
| 2135 | pud_t *pudp; |
| 2136 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2137 | pudp = pud_offset(&p4d, addr); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2138 | do { |
Christian Borntraeger | e37c698 | 2014-12-07 21:41:33 +0100 | [diff] [blame] | 2139 | pud_t pud = READ_ONCE(*pudp); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2140 | |
| 2141 | next = pud_addr_end(addr, end); |
| 2142 | if (pud_none(pud)) |
| 2143 | return 0; |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2144 | if (unlikely(pud_huge(pud))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2145 | if (!gup_huge_pud(pud, pudp, addr, next, flags, |
Aneesh Kumar K.V | f30c59e | 2014-11-05 21:57:40 +0530 | [diff] [blame] | 2146 | pages, nr)) |
| 2147 | return 0; |
| 2148 | } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { |
| 2149 | if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2150 | PUD_SHIFT, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2151 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2152 | } else if (!gup_pmd_range(pud, addr, next, flags, pages, nr)) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2153 | return 0; |
| 2154 | } while (pudp++, addr = next, addr != end); |
| 2155 | |
| 2156 | return 1; |
| 2157 | } |
| 2158 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2159 | static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2160 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2161 | { |
| 2162 | unsigned long next; |
| 2163 | p4d_t *p4dp; |
| 2164 | |
| 2165 | p4dp = p4d_offset(&pgd, addr); |
| 2166 | do { |
| 2167 | p4d_t p4d = READ_ONCE(*p4dp); |
| 2168 | |
| 2169 | next = p4d_addr_end(addr, end); |
| 2170 | if (p4d_none(p4d)) |
| 2171 | return 0; |
| 2172 | BUILD_BUG_ON(p4d_huge(p4d)); |
| 2173 | if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { |
| 2174 | if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2175 | P4D_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2176 | return 0; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2177 | } else if (!gup_pud_range(p4d, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 2178 | return 0; |
| 2179 | } while (p4dp++, addr = next, addr != end); |
| 2180 | |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2184 | static void gup_pgd_range(unsigned long addr, unsigned long end, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2185 | unsigned int flags, struct page **pages, int *nr) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2186 | { |
| 2187 | unsigned long next; |
| 2188 | pgd_t *pgdp; |
| 2189 | |
| 2190 | pgdp = pgd_offset(current->mm, addr); |
| 2191 | do { |
| 2192 | pgd_t pgd = READ_ONCE(*pgdp); |
| 2193 | |
| 2194 | next = pgd_addr_end(addr, end); |
| 2195 | if (pgd_none(pgd)) |
| 2196 | return; |
| 2197 | if (unlikely(pgd_huge(pgd))) { |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2198 | if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2199 | pages, nr)) |
| 2200 | return; |
| 2201 | } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { |
| 2202 | if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2203 | PGDIR_SHIFT, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2204 | return; |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2205 | } else if (!gup_p4d_range(pgd, addr, next, flags, pages, nr)) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2206 | return; |
| 2207 | } while (pgdp++, addr = next, addr != end); |
| 2208 | } |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2209 | #else |
| 2210 | static inline void gup_pgd_range(unsigned long addr, unsigned long end, |
| 2211 | unsigned int flags, struct page **pages, int *nr) |
| 2212 | { |
| 2213 | } |
| 2214 | #endif /* CONFIG_HAVE_FAST_GUP */ |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2215 | |
| 2216 | #ifndef gup_fast_permitted |
| 2217 | /* |
| 2218 | * Check if it's allowed to use __get_user_pages_fast() for the range, or |
| 2219 | * we need to fall back to the slow version: |
| 2220 | */ |
Christoph Hellwig | 26f4c32 | 2019-07-11 20:56:45 -0700 | [diff] [blame] | 2221 | static bool gup_fast_permitted(unsigned long start, unsigned long end) |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2222 | { |
Christoph Hellwig | 26f4c32 | 2019-07-11 20:56:45 -0700 | [diff] [blame] | 2223 | return true; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2224 | } |
| 2225 | #endif |
| 2226 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2227 | /* |
| 2228 | * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to |
Michael S. Tsirkin | d081107 | 2018-04-13 15:35:23 -0700 | [diff] [blame] | 2229 | * the regular GUP. |
| 2230 | * Note a difference with get_user_pages_fast: this always returns the |
| 2231 | * number of pages pinned, 0 if no pages were pinned. |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2232 | * |
| 2233 | * If the architecture does not support this function, simply return with no |
| 2234 | * pages pinned. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2235 | */ |
| 2236 | int __get_user_pages_fast(unsigned long start, int nr_pages, int write, |
| 2237 | struct page **pages) |
| 2238 | { |
Wei Yang | d4faa40 | 2018-10-26 15:07:55 -0700 | [diff] [blame] | 2239 | unsigned long len, end; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2240 | unsigned long flags; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2241 | int nr = 0; |
| 2242 | |
Christoph Hellwig | f455c854 | 2019-07-11 20:56:41 -0700 | [diff] [blame] | 2243 | start = untagged_addr(start) & PAGE_MASK; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2244 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2245 | end = start + len; |
| 2246 | |
Christoph Hellwig | 26f4c32 | 2019-07-11 20:56:45 -0700 | [diff] [blame] | 2247 | if (end <= start) |
| 2248 | return 0; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2249 | if (unlikely(!access_ok((void __user *)start, len))) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2250 | return 0; |
| 2251 | |
| 2252 | /* |
| 2253 | * Disable interrupts. We use the nested form as we can already have |
| 2254 | * interrupts disabled by get_futex_key. |
| 2255 | * |
| 2256 | * With interrupts disabled, we block page table pages from being |
Fengguang Wu | 2ebe822 | 2018-10-30 15:10:51 -0700 | [diff] [blame] | 2257 | * freed from under us. See struct mmu_table_batch comments in |
| 2258 | * include/asm-generic/tlb.h for more details. |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2259 | * |
| 2260 | * We do not adopt an rcu_read_lock(.) here as we also want to |
| 2261 | * block IPIs that come from THPs splitting. |
| 2262 | */ |
| 2263 | |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2264 | if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && |
| 2265 | gup_fast_permitted(start, end)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2266 | local_irq_save(flags); |
Ira Weiny | b798bec | 2019-05-13 17:17:07 -0700 | [diff] [blame] | 2267 | gup_pgd_range(start, end, write ? FOLL_WRITE : 0, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2268 | local_irq_restore(flags); |
| 2269 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2270 | |
| 2271 | return nr; |
| 2272 | } |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2273 | EXPORT_SYMBOL_GPL(__get_user_pages_fast); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2274 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2275 | static int __gup_longterm_unlocked(unsigned long start, int nr_pages, |
| 2276 | unsigned int gup_flags, struct page **pages) |
| 2277 | { |
| 2278 | int ret; |
| 2279 | |
| 2280 | /* |
| 2281 | * FIXME: FOLL_LONGTERM does not work with |
| 2282 | * get_user_pages_unlocked() (see comments in that function) |
| 2283 | */ |
| 2284 | if (gup_flags & FOLL_LONGTERM) { |
| 2285 | down_read(¤t->mm->mmap_sem); |
| 2286 | ret = __gup_longterm_locked(current, current->mm, |
| 2287 | start, nr_pages, |
| 2288 | pages, NULL, gup_flags); |
| 2289 | up_read(¤t->mm->mmap_sem); |
| 2290 | } else { |
| 2291 | ret = get_user_pages_unlocked(start, nr_pages, |
| 2292 | pages, gup_flags); |
| 2293 | } |
| 2294 | |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2298 | /** |
| 2299 | * get_user_pages_fast() - pin user pages in memory |
| 2300 | * @start: starting user address |
| 2301 | * @nr_pages: number of pages from start to pin |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2302 | * @gup_flags: flags modifying pin behaviour |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2303 | * @pages: array that receives pointers to the pages pinned. |
| 2304 | * Should be at least nr_pages long. |
| 2305 | * |
| 2306 | * Attempt to pin user pages in memory without taking mm->mmap_sem. |
| 2307 | * If not successful, it will fall back to taking the lock and |
| 2308 | * calling get_user_pages(). |
| 2309 | * |
| 2310 | * Returns number of pages pinned. This may be fewer than the number |
| 2311 | * requested. If nr_pages is 0 or negative, returns 0. If no pages |
| 2312 | * were pinned, returns -errno. |
| 2313 | */ |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2314 | int get_user_pages_fast(unsigned long start, int nr_pages, |
| 2315 | unsigned int gup_flags, struct page **pages) |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2316 | { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2317 | unsigned long addr, len, end; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2318 | int nr = 0, ret = 0; |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2319 | |
Christoph Hellwig | f455c854 | 2019-07-11 20:56:41 -0700 | [diff] [blame] | 2320 | start = untagged_addr(start) & PAGE_MASK; |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2321 | addr = start; |
| 2322 | len = (unsigned long) nr_pages << PAGE_SHIFT; |
| 2323 | end = start + len; |
| 2324 | |
Christoph Hellwig | 26f4c32 | 2019-07-11 20:56:45 -0700 | [diff] [blame] | 2325 | if (end <= start) |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2326 | return 0; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2327 | if (unlikely(!access_ok((void __user *)start, len))) |
Michael S. Tsirkin | c61611f | 2018-04-13 15:35:20 -0700 | [diff] [blame] | 2328 | return -EFAULT; |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2329 | |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2330 | if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && |
| 2331 | gup_fast_permitted(start, end)) { |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2332 | local_irq_disable(); |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 2333 | gup_pgd_range(addr, end, gup_flags, pages, &nr); |
Kirill A. Shutemov | 5b65c467 | 2017-09-09 00:56:03 +0300 | [diff] [blame] | 2334 | local_irq_enable(); |
Kirill A. Shutemov | 73e10a6 | 2017-03-16 18:26:54 +0300 | [diff] [blame] | 2335 | ret = nr; |
| 2336 | } |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2337 | |
| 2338 | if (nr < nr_pages) { |
| 2339 | /* Try to get the remaining pages with get_user_pages */ |
| 2340 | start += nr << PAGE_SHIFT; |
| 2341 | pages += nr; |
| 2342 | |
Ira Weiny | 7af7556 | 2019-05-13 17:17:14 -0700 | [diff] [blame] | 2343 | ret = __gup_longterm_unlocked(start, nr_pages - nr, |
| 2344 | gup_flags, pages); |
Steve Capper | 2667f50 | 2014-10-09 15:29:14 -0700 | [diff] [blame] | 2345 | |
| 2346 | /* Have to be a bit careful with return values */ |
| 2347 | if (nr > 0) { |
| 2348 | if (ret < 0) |
| 2349 | ret = nr; |
| 2350 | else |
| 2351 | ret += nr; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | return ret; |
| 2356 | } |
Christoph Hellwig | 050a9ad | 2019-07-11 20:57:21 -0700 | [diff] [blame^] | 2357 | EXPORT_SYMBOL_GPL(get_user_pages_fast); |