1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/err.h> 5 #include <linux/spinlock.h> 6 7 #include <linux/mm.h> 8 #include <linux/memremap.h> 9 #include <linux/pagemap.h> 10 #include <linux/rmap.h> 11 #include <linux/swap.h> 12 #include <linux/swapops.h> 13 #include <linux/secretmem.h> 14 15 #include <linux/sched/signal.h> 16 #include <linux/rwsem.h> 17 #include <linux/hugetlb.h> 18 #include <linux/migrate.h> 19 #include <linux/mm_inline.h> 20 #include <linux/sched/mm.h> 21 #include <linux/shmem_fs.h> 22 23 #include <asm/mmu_context.h> 24 #include <asm/tlbflush.h> 25 26 #include "internal.h" 27 28 struct follow_page_context { 29 struct dev_pagemap *pgmap; 30 unsigned int page_mask; 31 }; 32 33 static inline void sanity_check_pinned_pages(struct page **pages, 34 unsigned long npages) 35 { 36 if (!IS_ENABLED(CONFIG_DEBUG_VM)) 37 return; 38 39 /* 40 * We only pin anonymous pages if they are exclusive. Once pinned, we 41 * can no longer turn them possibly shared and PageAnonExclusive() will 42 * stick around until the page is freed. 43 * 44 * We'd like to verify that our pinned anonymous pages are still mapped 45 * exclusively. The issue with anon THP is that we don't know how 46 * they are/were mapped when pinning them. However, for anon 47 * THP we can assume that either the given page (PTE-mapped THP) or 48 * the head page (PMD-mapped THP) should be PageAnonExclusive(). If 49 * neither is the case, there is certainly something wrong. 50 */ 51 for (; npages; npages--, pages++) { 52 struct page *page = *pages; 53 struct folio *folio = page_folio(page); 54 55 if (!folio_test_anon(folio)) 56 continue; 57 if (!folio_test_large(folio) || folio_test_hugetlb(folio)) 58 VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page), page); 59 else 60 /* Either a PTE-mapped or a PMD-mapped THP. */ 61 VM_BUG_ON_PAGE(!PageAnonExclusive(&folio->page) && 62 !PageAnonExclusive(page), page); 63 } 64 } 65 66 /* 67 * Return the folio with ref appropriately incremented, 68 * or NULL if that failed. 69 */ 70 static inline struct folio *try_get_folio(struct page *page, int refs) 71 { 72 struct folio *folio; 73 74 retry: 75 folio = page_folio(page); 76 if (WARN_ON_ONCE(folio_ref_count(folio) < 0)) 77 return NULL; 78 if (unlikely(!folio_ref_try_add_rcu(folio, refs))) 79 return NULL; 80 81 /* 82 * At this point we have a stable reference to the folio; but it 83 * could be that between calling page_folio() and the refcount 84 * increment, the folio was split, in which case we'd end up 85 * holding a reference on a folio that has nothing to do with the page 86 * we were given anymore. 87 * So now that the folio is stable, recheck that the page still 88 * belongs to this folio. 89 */ 90 if (unlikely(page_folio(page) != folio)) { 91 if (!put_devmap_managed_page_refs(&folio->page, refs)) 92 folio_put_refs(folio, refs); 93 goto retry; 94 } 95 96 return folio; 97 } 98 99 /** 100 * try_grab_folio() - Attempt to get or pin a folio. 101 * @page: pointer to page to be grabbed 102 * @refs: the value to (effectively) add to the folio's refcount 103 * @flags: gup flags: these are the FOLL_* flag values. 104 * 105 * "grab" names in this file mean, "look at flags to decide whether to use 106 * FOLL_PIN or FOLL_GET behavior, when incrementing the folio's refcount. 107 * 108 * Either FOLL_PIN or FOLL_GET (or neither) must be set, but not both at the 109 * same time. (That's true throughout the get_user_pages*() and 110 * pin_user_pages*() APIs.) Cases: 111 * 112 * FOLL_GET: folio's refcount will be incremented by @refs. 113 * 114 * FOLL_PIN on large folios: folio's refcount will be incremented by 115 * @refs, and its pincount will be incremented by @refs. 116 * 117 * FOLL_PIN on single-page folios: folio's refcount will be incremented by 118 * @refs * GUP_PIN_COUNTING_BIAS. 119 * 120 * Return: The folio containing @page (with refcount appropriately 121 * incremented) for success, or NULL upon failure. If neither FOLL_GET 122 * nor FOLL_PIN was set, that's considered failure, and furthermore, 123 * a likely bug in the caller, so a warning is also emitted. 124 */ 125 struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags) 126 { 127 struct folio *folio; 128 129 if (WARN_ON_ONCE((flags & (FOLL_GET | FOLL_PIN)) == 0)) 130 return NULL; 131 132 if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page))) 133 return NULL; 134 135 folio = try_get_folio(page, refs); 136 137 if (flags & FOLL_GET) 138 return folio; 139 140 /* FOLL_PIN is set */ 141 if (!folio) 142 return NULL; 143 144 /* 145 * Can't do FOLL_LONGTERM + FOLL_PIN gup fast path if not in a 146 * right zone, so fail and let the caller fall back to the slow 147 * path. 148 */ 149 if (unlikely((flags & FOLL_LONGTERM) && 150 !folio_is_longterm_pinnable(folio))) { 151 if (!put_devmap_managed_page_refs(&folio->page, refs)) 152 folio_put_refs(folio, refs); 153 return NULL; 154 } 155 156 /* 157 * When pinning a large folio, use an exact count to track it. 158 * 159 * However, be sure to *also* increment the normal folio 160 * refcount field at least once, so that the folio really 161 * is pinned. That's why the refcount from the earlier 162 * try_get_folio() is left intact. 163 */ 164 if (folio_test_large(folio)) 165 atomic_add(refs, &folio->_pincount); 166 else 167 folio_ref_add(folio, 168 refs * (GUP_PIN_COUNTING_BIAS - 1)); 169 /* 170 * Adjust the pincount before re-checking the PTE for changes. 171 * This is essentially a smp_mb() and is paired with a memory 172 * barrier in page_try_share_anon_rmap(). 173 */ 174 smp_mb__after_atomic(); 175 176 node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, refs); 177 178 return folio; 179 } 180 181 static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) 182 { 183 if (flags & FOLL_PIN) { 184 node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs); 185 if (folio_test_large(folio)) 186 atomic_sub(refs, &folio->_pincount); 187 else 188 refs *= GUP_PIN_COUNTING_BIAS; 189 } 190 191 if (!put_devmap_managed_page_refs(&folio->page, refs)) 192 folio_put_refs(folio, refs); 193 } 194 195 /** 196 * try_grab_page() - elevate a page's refcount by a flag-dependent amount 197 * @page: pointer to page to be grabbed 198 * @flags: gup flags: these are the FOLL_* flag values. 199 * 200 * This might not do anything at all, depending on the flags argument. 201 * 202 * "grab" names in this file mean, "look at flags to decide whether to use 203 * FOLL_PIN or FOLL_GET behavior, when incrementing the page's refcount. 204 * 205 * Either FOLL_PIN or FOLL_GET (or neither) may be set, but not both at the same 206 * time. Cases: please see the try_grab_folio() documentation, with 207 * "refs=1". 208 * 209 * Return: 0 for success, or if no action was required (if neither FOLL_PIN 210 * nor FOLL_GET was set, nothing is done). A negative error code for failure: 211 * 212 * -ENOMEM FOLL_GET or FOLL_PIN was set, but the page could not 213 * be grabbed. 214 */ 215 int __must_check try_grab_page(struct page *page, unsigned int flags) 216 { 217 struct folio *folio = page_folio(page); 218 219 if (WARN_ON_ONCE(folio_ref_count(folio) <= 0)) 220 return -ENOMEM; 221 222 if (unlikely(!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page))) 223 return -EREMOTEIO; 224 225 if (flags & FOLL_GET) 226 folio_ref_inc(folio); 227 else if (flags & FOLL_PIN) { 228 /* 229 * Similar to try_grab_folio(): be sure to *also* 230 * increment the normal page refcount field at least once, 231 * so that the page really is pinned. 232 */ 233 if (folio_test_large(folio)) { 234 folio_ref_add(folio, 1); 235 atomic_add(1, &folio->_pincount); 236 } else { 237 folio_ref_add(folio, GUP_PIN_COUNTING_BIAS); 238 } 239 240 node_stat_mod_folio(folio, NR_FOLL_PIN_ACQUIRED, 1); 241 } 242 243 return 0; 244 } 245 246 /** 247 * unpin_user_page() - release a dma-pinned page 248 * @page: pointer to page to be released 249 * 250 * Pages that were pinned via pin_user_pages*() must be released via either 251 * unpin_user_page(), or one of the unpin_user_pages*() routines. This is so 252 * that such pages can be separately tracked and uniquely handled. In 253 * particular, interactions with RDMA and filesystems need special handling. 254 */ 255 void unpin_user_page(struct page *page) 256 { 257 sanity_check_pinned_pages(&page, 1); 258 gup_put_folio(page_folio(page), 1, FOLL_PIN); 259 } 260 EXPORT_SYMBOL(unpin_user_page); 261 262 static inline struct folio *gup_folio_range_next(struct page *start, 263 unsigned long npages, unsigned long i, unsigned int *ntails) 264 { 265 struct page *next = nth_page(start, i); 266 struct folio *folio = page_folio(next); 267 unsigned int nr = 1; 268 269 if (folio_test_large(folio)) 270 nr = min_t(unsigned int, npages - i, 271 folio_nr_pages(folio) - folio_page_idx(folio, next)); 272 273 *ntails = nr; 274 return folio; 275 } 276 277 static inline struct folio *gup_folio_next(struct page **list, 278 unsigned long npages, unsigned long i, unsigned int *ntails) 279 { 280 struct folio *folio = page_folio(list[i]); 281 unsigned int nr; 282 283 for (nr = i + 1; nr < npages; nr++) { 284 if (page_folio(list[nr]) != folio) 285 break; 286 } 287 288 *ntails = nr - i; 289 return folio; 290 } 291 292 /** 293 * unpin_user_pages_dirty_lock() - release and optionally dirty gup-pinned pages 294 * @pages: array of pages to be maybe marked dirty, and definitely released. 295 * @npages: number of pages in the @pages array. 296 * @make_dirty: whether to mark the pages dirty 297 * 298 * "gup-pinned page" refers to a page that has had one of the get_user_pages() 299 * variants called on that page. 300 * 301 * For each page in the @pages array, make that page (or its head page, if a 302 * compound page) dirty, if @make_dirty is true, and if the page was previously 303 * listed as clean. In any case, releases all pages using unpin_user_page(), 304 * possibly via unpin_user_pages(), for the non-dirty case. 305 * 306 * Please see the unpin_user_page() documentation for details. 307 * 308 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is 309 * required, then the caller should a) verify that this is really correct, 310 * because _lock() is usually required, and b) hand code it: 311 * set_page_dirty_lock(), unpin_user_page(). 312 * 313 */ 314 void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages, 315 bool make_dirty) 316 { 317 unsigned long i; 318 struct folio *folio; 319 unsigned int nr; 320 321 if (!make_dirty) { 322 unpin_user_pages(pages, npages); 323 return; 324 } 325 326 sanity_check_pinned_pages(pages, npages); 327 for (i = 0; i < npages; i += nr) { 328 folio = gup_folio_next(pages, npages, i, &nr); 329 /* 330 * Checking PageDirty at this point may race with 331 * clear_page_dirty_for_io(), but that's OK. Two key 332 * cases: 333 * 334 * 1) This code sees the page as already dirty, so it 335 * skips the call to set_page_dirty(). That could happen 336 * because clear_page_dirty_for_io() called 337 * page_mkclean(), followed by set_page_dirty(). 338 * However, now the page is going to get written back, 339 * which meets the original intention of setting it 340 * dirty, so all is well: clear_page_dirty_for_io() goes 341 * on to call TestClearPageDirty(), and write the page 342 * back. 343 * 344 * 2) This code sees the page as clean, so it calls 345 * set_page_dirty(). The page stays dirty, despite being 346 * written back, so it gets written back again in the 347 * next writeback cycle. This is harmless. 348 */ 349 if (!folio_test_dirty(folio)) { 350 folio_lock(folio); 351 folio_mark_dirty(folio); 352 folio_unlock(folio); 353 } 354 gup_put_folio(folio, nr, FOLL_PIN); 355 } 356 } 357 EXPORT_SYMBOL(unpin_user_pages_dirty_lock); 358 359 /** 360 * unpin_user_page_range_dirty_lock() - release and optionally dirty 361 * gup-pinned page range 362 * 363 * @page: the starting page of a range maybe marked dirty, and definitely released. 364 * @npages: number of consecutive pages to release. 365 * @make_dirty: whether to mark the pages dirty 366 * 367 * "gup-pinned page range" refers to a range of pages that has had one of the 368 * pin_user_pages() variants called on that page. 369 * 370 * For the page ranges defined by [page .. page+npages], make that range (or 371 * its head pages, if a compound page) dirty, if @make_dirty is true, and if the 372 * page range was previously listed as clean. 373 * 374 * set_page_dirty_lock() is used internally. If instead, set_page_dirty() is 375 * required, then the caller should a) verify that this is really correct, 376 * because _lock() is usually required, and b) hand code it: 377 * set_page_dirty_lock(), unpin_user_page(). 378 * 379 */ 380 void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages, 381 bool make_dirty) 382 { 383 unsigned long i; 384 struct folio *folio; 385 unsigned int nr; 386 387 for (i = 0; i < npages; i += nr) { 388 folio = gup_folio_range_next(page, npages, i, &nr); 389 if (make_dirty && !folio_test_dirty(folio)) { 390 folio_lock(folio); 391 folio_mark_dirty(folio); 392 folio_unlock(folio); 393 } 394 gup_put_folio(folio, nr, FOLL_PIN); 395 } 396 } 397 EXPORT_SYMBOL(unpin_user_page_range_dirty_lock); 398 399 static void unpin_user_pages_lockless(struct page **pages, unsigned long npages) 400 { 401 unsigned long i; 402 struct folio *folio; 403 unsigned int nr; 404 405 /* 406 * Don't perform any sanity checks because we might have raced with 407 * fork() and some anonymous pages might now actually be shared -- 408 * which is why we're unpinning after all. 409 */ 410 for (i = 0; i < npages; i += nr) { 411 folio = gup_folio_next(pages, npages, i, &nr); 412 gup_put_folio(folio, nr, FOLL_PIN); 413 } 414 } 415 416 /** 417 * unpin_user_pages() - release an array of gup-pinned pages. 418 * @pages: array of pages to be marked dirty and released. 419 * @npages: number of pages in the @pages array. 420 * 421 * For each page in the @pages array, release the page using unpin_user_page(). 422 * 423 * Please see the unpin_user_page() documentation for details. 424 */ 425 void unpin_user_pages(struct page **pages, unsigned long npages) 426 { 427 unsigned long i; 428 struct folio *folio; 429 unsigned int nr; 430 431 /* 432 * If this WARN_ON() fires, then the system *might* be leaking pages (by 433 * leaving them pinned), but probably not. More likely, gup/pup returned 434 * a hard -ERRNO error to the caller, who erroneously passed it here. 435 */ 436 if (WARN_ON(IS_ERR_VALUE(npages))) 437 return; 438 439 sanity_check_pinned_pages(pages, npages); 440 for (i = 0; i < npages; i += nr) { 441 folio = gup_folio_next(pages, npages, i, &nr); 442 gup_put_folio(folio, nr, FOLL_PIN); 443 } 444 } 445 EXPORT_SYMBOL(unpin_user_pages); 446 447 /* 448 * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's 449 * lifecycle. Avoid setting the bit unless necessary, or it might cause write 450 * cache bouncing on large SMP machines for concurrent pinned gups. 451 */ 452 static inline void mm_set_has_pinned_flag(unsigned long *mm_flags) 453 { 454 if (!test_bit(MMF_HAS_PINNED, mm_flags)) 455 set_bit(MMF_HAS_PINNED, mm_flags); 456 } 457 458 #ifdef CONFIG_MMU 459 static struct page *no_page_table(struct vm_area_struct *vma, 460 unsigned int flags) 461 { 462 /* 463 * When core dumping an enormous anonymous area that nobody 464 * has touched so far, we don't want to allocate unnecessary pages or 465 * page tables. Return error instead of NULL to skip handle_mm_fault, 466 * then get_dump_page() will return NULL to leave a hole in the dump. 467 * But we can only make this optimization where a hole would surely 468 * be zero-filled if handle_mm_fault() actually did handle it. 469 */ 470 if ((flags & FOLL_DUMP) && 471 (vma_is_anonymous(vma) || !vma->vm_ops->fault)) 472 return ERR_PTR(-EFAULT); 473 return NULL; 474 } 475 476 static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, 477 pte_t *pte, unsigned int flags) 478 { 479 if (flags & FOLL_TOUCH) { 480 pte_t orig_entry = ptep_get(pte); 481 pte_t entry = orig_entry; 482 483 if (flags & FOLL_WRITE) 484 entry = pte_mkdirty(entry); 485 entry = pte_mkyoung(entry); 486 487 if (!pte_same(orig_entry, entry)) { 488 set_pte_at(vma->vm_mm, address, pte, entry); 489 update_mmu_cache(vma, address, pte); 490 } 491 } 492 493 /* Proper page table entry exists, but no corresponding struct page */ 494 return -EEXIST; 495 } 496 497 /* FOLL_FORCE can write to even unwritable PTEs in COW mappings. */ 498 static inline bool can_follow_write_pte(pte_t pte, struct page *page, 499 struct vm_area_struct *vma, 500 unsigned int flags) 501 { 502 /* If the pte is writable, we can write to the page. */ 503 if (pte_write(pte)) 504 return true; 505 506 /* Maybe FOLL_FORCE is set to override it? */ 507 if (!(flags & FOLL_FORCE)) 508 return false; 509 510 /* But FOLL_FORCE has no effect on shared mappings */ 511 if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED)) 512 return false; 513 514 /* ... or read-only private ones */ 515 if (!(vma->vm_flags & VM_MAYWRITE)) 516 return false; 517 518 /* ... or already writable ones that just need to take a write fault */ 519 if (vma->vm_flags & VM_WRITE) 520 return false; 521 522 /* 523 * See can_change_pte_writable(): we broke COW and could map the page 524 * writable if we have an exclusive anonymous page ... 525 */ 526 if (!page || !PageAnon(page) || !PageAnonExclusive(page)) 527 return false; 528 529 /* ... and a write-fault isn't required for other reasons. */ 530 if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte)) 531 return false; 532 return !userfaultfd_pte_wp(vma, pte); 533 } 534 535 static struct page *follow_page_pte(struct vm_area_struct *vma, 536 unsigned long address, pmd_t *pmd, unsigned int flags, 537 struct dev_pagemap **pgmap) 538 { 539 struct mm_struct *mm = vma->vm_mm; 540 struct page *page; 541 spinlock_t *ptl; 542 pte_t *ptep, pte; 543 int ret; 544 545 /* FOLL_GET and FOLL_PIN are mutually exclusive. */ 546 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == 547 (FOLL_PIN | FOLL_GET))) 548 return ERR_PTR(-EINVAL); 549 550 ptep = pte_offset_map_lock(mm, pmd, address, &ptl); 551 if (!ptep) 552 return no_page_table(vma, flags); 553 pte = ptep_get(ptep); 554 if (!pte_present(pte)) 555 goto no_page; 556 if (pte_protnone(pte) && !gup_can_follow_protnone(flags)) 557 goto no_page; 558 559 page = vm_normal_page(vma, address, pte); 560 561 /* 562 * We only care about anon pages in can_follow_write_pte() and don't 563 * have to worry about pte_devmap() because they are never anon. 564 */ 565 if ((flags & FOLL_WRITE) && 566 !can_follow_write_pte(pte, page, vma, flags)) { 567 page = NULL; 568 goto out; 569 } 570 571 if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { 572 /* 573 * Only return device mapping pages in the FOLL_GET or FOLL_PIN 574 * case since they are only valid while holding the pgmap 575 * reference. 576 */ 577 *pgmap = get_dev_pagemap(pte_pfn(pte), *pgmap); 578 if (*pgmap) 579 page = pte_page(pte); 580 else 581 goto no_page; 582 } else if (unlikely(!page)) { 583 if (flags & FOLL_DUMP) { 584 /* Avoid special (like zero) pages in core dumps */ 585 page = ERR_PTR(-EFAULT); 586 goto out; 587 } 588 589 if (is_zero_pfn(pte_pfn(pte))) { 590 page = pte_page(pte); 591 } else { 592 ret = follow_pfn_pte(vma, address, ptep, flags); 593 page = ERR_PTR(ret); 594 goto out; 595 } 596 } 597 598 if (!pte_write(pte) && gup_must_unshare(vma, flags, page)) { 599 page = ERR_PTR(-EMLINK); 600 goto out; 601 } 602 603 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && 604 !PageAnonExclusive(page), page); 605 606 /* try_grab_page() does nothing unless FOLL_GET or FOLL_PIN is set. */ 607 ret = try_grab_page(page, flags); 608 if (unlikely(ret)) { 609 page = ERR_PTR(ret); 610 goto out; 611 } 612 613 /* 614 * We need to make the page accessible if and only if we are going 615 * to access its content (the FOLL_PIN case). Please see 616 * Documentation/core-api/pin_user_pages.rst for details. 617 */ 618 if (flags & FOLL_PIN) { 619 ret = arch_make_page_accessible(page); 620 if (ret) { 621 unpin_user_page(page); 622 page = ERR_PTR(ret); 623 goto out; 624 } 625 } 626 if (flags & FOLL_TOUCH) { 627 if ((flags & FOLL_WRITE) && 628 !pte_dirty(pte) && !PageDirty(page)) 629 set_page_dirty(page); 630 /* 631 * pte_mkyoung() would be more correct here, but atomic care 632 * is needed to avoid losing the dirty bit: it is easier to use 633 * mark_page_accessed(). 634 */ 635 mark_page_accessed(page); 636 } 637 out: 638 pte_unmap_unlock(ptep, ptl); 639 return page; 640 no_page: 641 pte_unmap_unlock(ptep, ptl); 642 if (!pte_none(pte)) 643 return NULL; 644 return no_page_table(vma, flags); 645 } 646 647 static struct page *follow_pmd_mask(struct vm_area_struct *vma, 648 unsigned long address, pud_t *pudp, 649 unsigned int flags, 650 struct follow_page_context *ctx) 651 { 652 pmd_t *pmd, pmdval; 653 spinlock_t *ptl; 654 struct page *page; 655 struct mm_struct *mm = vma->vm_mm; 656 657 pmd = pmd_offset(pudp, address); 658 pmdval = pmdp_get_lockless(pmd); 659 if (pmd_none(pmdval)) 660 return no_page_table(vma, flags); 661 if (!pmd_present(pmdval)) 662 return no_page_table(vma, flags); 663 if (pmd_devmap(pmdval)) { 664 ptl = pmd_lock(mm, pmd); 665 page = follow_devmap_pmd(vma, address, pmd, flags, &ctx->pgmap); 666 spin_unlock(ptl); 667 if (page) 668 return page; 669 } 670 if (likely(!pmd_trans_huge(pmdval))) 671 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); 672 673 if (pmd_protnone(pmdval) && !gup_can_follow_protnone(flags)) 674 return no_page_table(vma, flags); 675 676 ptl = pmd_lock(mm, pmd); 677 if (unlikely(!pmd_present(*pmd))) { 678 spin_unlock(ptl); 679 return no_page_table(vma, flags); 680 } 681 if (unlikely(!pmd_trans_huge(*pmd))) { 682 spin_unlock(ptl); 683 return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); 684 } 685 if (flags & FOLL_SPLIT_PMD) { 686 spin_unlock(ptl); 687 split_huge_pmd(vma, pmd, address); 688 /* If pmd was left empty, stuff a page table in there quickly */ 689 return pte_alloc(mm, pmd) ? ERR_PTR(-ENOMEM) : 690 follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); 691 } 692 page = follow_trans_huge_pmd(vma, address, pmd, flags); 693 spin_unlock(ptl); 694 ctx->page_mask = HPAGE_PMD_NR - 1; 695 return page; 696 } 697 698 static struct page *follow_pud_mask(struct vm_area_struct *vma, 699 unsigned long address, p4d_t *p4dp, 700 unsigned int flags, 701 struct follow_page_context *ctx) 702 { 703 pud_t *pud; 704 spinlock_t *ptl; 705 struct page *page; 706 struct mm_struct *mm = vma->vm_mm; 707 708 pud = pud_offset(p4dp, address); 709 if (pud_none(*pud)) 710 return no_page_table(vma, flags); 711 if (pud_devmap(*pud)) { 712 ptl = pud_lock(mm, pud); 713 page = follow_devmap_pud(vma, address, pud, flags, &ctx->pgmap); 714 spin_unlock(ptl); 715 if (page) 716 return page; 717 } 718 if (unlikely(pud_bad(*pud))) 719 return no_page_table(vma, flags); 720 721 return follow_pmd_mask(vma, address, pud, flags, ctx); 722 } 723 724 static struct page *follow_p4d_mask(struct vm_area_struct *vma, 725 unsigned long address, pgd_t *pgdp, 726 unsigned int flags, 727 struct follow_page_context *ctx) 728 { 729 p4d_t *p4d; 730 731 p4d = p4d_offset(pgdp, address); 732 if (p4d_none(*p4d)) 733 return no_page_table(vma, flags); 734 BUILD_BUG_ON(p4d_huge(*p4d)); 735 if (unlikely(p4d_bad(*p4d))) 736 return no_page_table(vma, flags); 737 738 return follow_pud_mask(vma, address, p4d, flags, ctx); 739 } 740 741 /** 742 * follow_page_mask - look up a page descriptor from a user-virtual address 743 * @vma: vm_area_struct mapping @address 744 * @address: virtual address to look up 745 * @flags: flags modifying lookup behaviour 746 * @ctx: contains dev_pagemap for %ZONE_DEVICE memory pinning and a 747 * pointer to output page_mask 748 * 749 * @flags can have FOLL_ flags set, defined in <linux/mm.h> 750 * 751 * When getting pages from ZONE_DEVICE memory, the @ctx->pgmap caches 752 * the device's dev_pagemap metadata to avoid repeating expensive lookups. 753 * 754 * When getting an anonymous page and the caller has to trigger unsharing 755 * of a shared anonymous page first, -EMLINK is returned. The caller should 756 * trigger a fault with FAULT_FLAG_UNSHARE set. Note that unsharing is only 757 * relevant with FOLL_PIN and !FOLL_WRITE. 758 * 759 * On output, the @ctx->page_mask is set according to the size of the page. 760 * 761 * Return: the mapped (struct page *), %NULL if no mapping exists, or 762 * an error pointer if there is a mapping to something not represented 763 * by a page descriptor (see also vm_normal_page()). 764 */ 765 static struct page *follow_page_mask(struct vm_area_struct *vma, 766 unsigned long address, unsigned int flags, 767 struct follow_page_context *ctx) 768 { 769 pgd_t *pgd; 770 struct page *page; 771 struct mm_struct *mm = vma->vm_mm; 772 773 ctx->page_mask = 0; 774 775 /* 776 * Call hugetlb_follow_page_mask for hugetlb vmas as it will use 777 * special hugetlb page table walking code. This eliminates the 778 * need to check for hugetlb entries in the general walking code. 779 * 780 * hugetlb_follow_page_mask is only for follow_page() handling here. 781 * Ordinary GUP uses follow_hugetlb_page for hugetlb processing. 782 */ 783 if (is_vm_hugetlb_page(vma)) { 784 page = hugetlb_follow_page_mask(vma, address, flags); 785 if (!page) 786 page = no_page_table(vma, flags); 787 return page; 788 } 789 790 pgd = pgd_offset(mm, address); 791 792 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) 793 return no_page_table(vma, flags); 794 795 return follow_p4d_mask(vma, address, pgd, flags, ctx); 796 } 797 798 struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 799 unsigned int foll_flags) 800 { 801 struct follow_page_context ctx = { NULL }; 802 struct page *page; 803 804 if (vma_is_secretmem(vma)) 805 return NULL; 806 807 if (WARN_ON_ONCE(foll_flags & FOLL_PIN)) 808 return NULL; 809 810 page = follow_page_mask(vma, address, foll_flags, &ctx); 811 if (ctx.pgmap) 812 put_dev_pagemap(ctx.pgmap); 813 return page; 814 } 815 816 static int get_gate_page(struct mm_struct *mm, unsigned long address, 817 unsigned int gup_flags, struct vm_area_struct **vma, 818 struct page **page) 819 { 820 pgd_t *pgd; 821 p4d_t *p4d; 822 pud_t *pud; 823 pmd_t *pmd; 824 pte_t *pte; 825 pte_t entry; 826 int ret = -EFAULT; 827 828 /* user gate pages are read-only */ 829 if (gup_flags & FOLL_WRITE) 830 return -EFAULT; 831 if (address > TASK_SIZE) 832 pgd = pgd_offset_k(address); 833 else 834 pgd = pgd_offset_gate(mm, address); 835 if (pgd_none(*pgd)) 836 return -EFAULT; 837 p4d = p4d_offset(pgd, address); 838 if (p4d_none(*p4d)) 839 return -EFAULT; 840 pud = pud_offset(p4d, address); 841 if (pud_none(*pud)) 842 return -EFAULT; 843 pmd = pmd_offset(pud, address); 844 if (!pmd_present(*pmd)) 845 return -EFAULT; 846 pte = pte_offset_map(pmd, address); 847 if (!pte) 848 return -EFAULT; 849 entry = ptep_get(pte); 850 if (pte_none(entry)) 851 goto unmap; 852 *vma = get_gate_vma(mm); 853 if (!page) 854 goto out; 855 *page = vm_normal_page(*vma, address, entry); 856 if (!*page) { 857 if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(entry))) 858 goto unmap; 859 *page = pte_page(entry); 860 } 861 ret = try_grab_page(*page, gup_flags); 862 if (unlikely(ret)) 863 goto unmap; 864 out: 865 ret = 0; 866 unmap: 867 pte_unmap(pte); 868 return ret; 869 } 870 871 /* 872 * mmap_lock must be held on entry. If @flags has FOLL_UNLOCKABLE but not 873 * FOLL_NOWAIT, the mmap_lock may be released. If it is, *@locked will be set 874 * to 0 and -EBUSY returned. 875 */ 876 static int faultin_page(struct vm_area_struct *vma, 877 unsigned long address, unsigned int *flags, bool unshare, 878 int *locked) 879 { 880 unsigned int fault_flags = 0; 881 vm_fault_t ret; 882 883 if (*flags & FOLL_NOFAULT) 884 return -EFAULT; 885 if (*flags & FOLL_WRITE) 886 fault_flags |= FAULT_FLAG_WRITE; 887 if (*flags & FOLL_REMOTE) 888 fault_flags |= FAULT_FLAG_REMOTE; 889 if (*flags & FOLL_UNLOCKABLE) { 890 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 891 /* 892 * FAULT_FLAG_INTERRUPTIBLE is opt-in. GUP callers must set 893 * FOLL_INTERRUPTIBLE to enable FAULT_FLAG_INTERRUPTIBLE. 894 * That's because some callers may not be prepared to 895 * handle early exits caused by non-fatal signals. 896 */ 897 if (*flags & FOLL_INTERRUPTIBLE) 898 fault_flags |= FAULT_FLAG_INTERRUPTIBLE; 899 } 900 if (*flags & FOLL_NOWAIT) 901 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT; 902 if (*flags & FOLL_TRIED) { 903 /* 904 * Note: FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_TRIED 905 * can co-exist 906 */ 907 fault_flags |= FAULT_FLAG_TRIED; 908 } 909 if (unshare) { 910 fault_flags |= FAULT_FLAG_UNSHARE; 911 /* FAULT_FLAG_WRITE and FAULT_FLAG_UNSHARE are incompatible */ 912 VM_BUG_ON(fault_flags & FAULT_FLAG_WRITE); 913 } 914 915 ret = handle_mm_fault(vma, address, fault_flags, NULL); 916 917 if (ret & VM_FAULT_COMPLETED) { 918 /* 919 * With FAULT_FLAG_RETRY_NOWAIT we'll never release the 920 * mmap lock in the page fault handler. Sanity check this. 921 */ 922 WARN_ON_ONCE(fault_flags & FAULT_FLAG_RETRY_NOWAIT); 923 *locked = 0; 924 925 /* 926 * We should do the same as VM_FAULT_RETRY, but let's not 927 * return -EBUSY since that's not reflecting the reality of 928 * what has happened - we've just fully completed a page 929 * fault, with the mmap lock released. Use -EAGAIN to show 930 * that we want to take the mmap lock _again_. 931 */ 932 return -EAGAIN; 933 } 934 935 if (ret & VM_FAULT_ERROR) { 936 int err = vm_fault_to_errno(ret, *flags); 937 938 if (err) 939 return err; 940 BUG(); 941 } 942 943 if (ret & VM_FAULT_RETRY) { 944 if (!(fault_flags & FAULT_FLAG_RETRY_NOWAIT)) 945 *locked = 0; 946 return -EBUSY; 947 } 948 949 return 0; 950 } 951 952 /* 953 * Writing to file-backed mappings which require folio dirty tracking using GUP 954 * is a fundamentally broken operation, as kernel write access to GUP mappings 955 * do not adhere to the semantics expected by a file system. 956 * 957 * Consider the following scenario:- 958 * 959 * 1. A folio is written to via GUP which write-faults the memory, notifying 960 * the file system and dirtying the folio. 961 * 2. Later, writeback is triggered, resulting in the folio being cleaned and 962 * the PTE being marked read-only. 963 * 3. The GUP caller writes to the folio, as it is mapped read/write via the 964 * direct mapping. 965 * 4. The GUP caller, now done with the page, unpins it and sets it dirty 966 * (though it does not have to). 967 * 968 * This results in both data being written to a folio without writenotify, and 969 * the folio being dirtied unexpectedly (if the caller decides to do so). 970 */ 971 static bool writable_file_mapping_allowed(struct vm_area_struct *vma, 972 unsigned long gup_flags) 973 { 974 /* 975 * If we aren't pinning then no problematic write can occur. A long term 976 * pin is the most egregious case so this is the case we disallow. 977 */ 978 if ((gup_flags & (FOLL_PIN | FOLL_LONGTERM)) != 979 (FOLL_PIN | FOLL_LONGTERM)) 980 return true; 981 982 /* 983 * If the VMA does not require dirty tracking then no problematic write 984 * can occur either. 985 */ 986 return !vma_needs_dirty_tracking(vma); 987 } 988 989 static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) 990 { 991 vm_flags_t vm_flags = vma->vm_flags; 992 int write = (gup_flags & FOLL_WRITE); 993 int foreign = (gup_flags & FOLL_REMOTE); 994 bool vma_anon = vma_is_anonymous(vma); 995 996 if (vm_flags & (VM_IO | VM_PFNMAP)) 997 return -EFAULT; 998 999 if ((gup_flags & FOLL_ANON) && !vma_anon) 1000 return -EFAULT; 1001 1002 if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma)) 1003 return -EOPNOTSUPP; 1004 1005 if (vma_is_secretmem(vma)) 1006 return -EFAULT; 1007 1008 if (write) { 1009 if (!vma_anon && 1010 !writable_file_mapping_allowed(vma, gup_flags)) 1011 return -EFAULT; 1012 1013 if (!(vm_flags & VM_WRITE)) { 1014 if (!(gup_flags & FOLL_FORCE)) 1015 return -EFAULT; 1016 /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ 1017 if (is_vm_hugetlb_page(vma)) 1018 return -EFAULT; 1019 /* 1020 * We used to let the write,force case do COW in a 1021 * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could 1022 * set a breakpoint in a read-only mapping of an 1023 * executable, without corrupting the file (yet only 1024 * when that file had been opened for writing!). 1025 * Anon pages in shared mappings are surprising: now 1026 * just reject it. 1027 */ 1028 if (!is_cow_mapping(vm_flags)) 1029 return -EFAULT; 1030 } 1031 } else if (!(vm_flags & VM_READ)) { 1032 if (!(gup_flags & FOLL_FORCE)) 1033 return -EFAULT; 1034 /* 1035 * Is there actually any vma we can reach here which does not 1036 * have VM_MAYREAD set? 1037 */ 1038 if (!(vm_flags & VM_MAYREAD)) 1039 return -EFAULT; 1040 } 1041 /* 1042 * gups are always data accesses, not instruction 1043 * fetches, so execute=false here 1044 */ 1045 if (!arch_vma_access_permitted(vma, write, false, foreign)) 1046 return -EFAULT; 1047 return 0; 1048 } 1049 1050 /** 1051 * __get_user_pages() - pin user pages in memory 1052 * @mm: mm_struct of target mm 1053 * @start: starting user address 1054 * @nr_pages: number of pages from start to pin 1055 * @gup_flags: flags modifying pin behaviour 1056 * @pages: array that receives pointers to the pages pinned. 1057 * Should be at least nr_pages long. Or NULL, if caller 1058 * only intends to ensure the pages are faulted in. 1059 * @locked: whether we're still with the mmap_lock held 1060 * 1061 * Returns either number of pages pinned (which may be less than the 1062 * number requested), or an error. Details about the return value: 1063 * 1064 * -- If nr_pages is 0, returns 0. 1065 * -- If nr_pages is >0, but no pages were pinned, returns -errno. 1066 * -- If nr_pages is >0, and some pages were pinned, returns the number of 1067 * pages pinned. Again, this may be less than nr_pages. 1068 * -- 0 return value is possible when the fault would need to be retried. 1069 * 1070 * The caller is responsible for releasing returned @pages, via put_page(). 1071 * 1072 * Must be called with mmap_lock held. It may be released. See below. 1073 * 1074 * __get_user_pages walks a process's page tables and takes a reference to 1075 * each struct page that each user address corresponds to at a given 1076 * instant. That is, it takes the page that would be accessed if a user 1077 * thread accesses the given user virtual address at that instant. 1078 * 1079 * This does not guarantee that the page exists in the user mappings when 1080 * __get_user_pages returns, and there may even be a completely different 1081 * page there in some cases (eg. if mmapped pagecache has been invalidated 1082 * and subsequently re-faulted). However it does guarantee that the page 1083 * won't be freed completely. And mostly callers simply care that the page 1084 * contains data that was valid *at some point in time*. Typically, an IO 1085 * or similar operation cannot guarantee anything stronger anyway because 1086 * locks can't be held over the syscall boundary. 1087 * 1088 * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If 1089 * the page is written to, set_page_dirty (or set_page_dirty_lock, as 1090 * appropriate) must be called after the page is finished with, and 1091 * before put_page is called. 1092 * 1093 * If FOLL_UNLOCKABLE is set without FOLL_NOWAIT then the mmap_lock may 1094 * be released. If this happens *@locked will be set to 0 on return. 1095 * 1096 * A caller using such a combination of @gup_flags must therefore hold the 1097 * mmap_lock for reading only, and recognize when it's been released. Otherwise, 1098 * it must be held for either reading or writing and will not be released. 1099 * 1100 * In most cases, get_user_pages or get_user_pages_fast should be used 1101 * instead of __get_user_pages. __get_user_pages should be used only if 1102 * you need some special @gup_flags. 1103 */ 1104 static long __get_user_pages(struct mm_struct *mm, 1105 unsigned long start, unsigned long nr_pages, 1106 unsigned int gup_flags, struct page **pages, 1107 int *locked) 1108 { 1109 long ret = 0, i = 0; 1110 struct vm_area_struct *vma = NULL; 1111 struct follow_page_context ctx = { NULL }; 1112 1113 if (!nr_pages) 1114 return 0; 1115 1116 start = untagged_addr_remote(mm, start); 1117 1118 VM_BUG_ON(!!pages != !!(gup_flags & (FOLL_GET | FOLL_PIN))); 1119 1120 do { 1121 struct page *page; 1122 unsigned int foll_flags = gup_flags; 1123 unsigned int page_increm; 1124 1125 /* first iteration or cross vma bound */ 1126 if (!vma || start >= vma->vm_end) { 1127 vma = find_extend_vma(mm, start); 1128 if (!vma && in_gate_area(mm, start)) { 1129 ret = get_gate_page(mm, start & PAGE_MASK, 1130 gup_flags, &vma, 1131 pages ? &pages[i] : NULL); 1132 if (ret) 1133 goto out; 1134 ctx.page_mask = 0; 1135 goto next_page; 1136 } 1137 1138 if (!vma) { 1139 ret = -EFAULT; 1140 goto out; 1141 } 1142 ret = check_vma_flags(vma, gup_flags); 1143 if (ret) 1144 goto out; 1145 1146 if (is_vm_hugetlb_page(vma)) { 1147 i = follow_hugetlb_page(mm, vma, pages, 1148 &start, &nr_pages, i, 1149 gup_flags, locked); 1150 if (!*locked) { 1151 /* 1152 * We've got a VM_FAULT_RETRY 1153 * and we've lost mmap_lock. 1154 * We must stop here. 1155 */ 1156 BUG_ON(gup_flags & FOLL_NOWAIT); 1157 goto out; 1158 } 1159 continue; 1160 } 1161 } 1162 retry: 1163 /* 1164 * If we have a pending SIGKILL, don't keep faulting pages and 1165 * potentially allocating memory. 1166 */ 1167 if (fatal_signal_pending(current)) { 1168 ret = -EINTR; 1169 goto out; 1170 } 1171 cond_resched(); 1172 1173 page = follow_page_mask(vma, start, foll_flags, &ctx); 1174 if (!page || PTR_ERR(page) == -EMLINK) { 1175 ret = faultin_page(vma, start, &foll_flags, 1176 PTR_ERR(page) == -EMLINK, locked); 1177 switch (ret) { 1178 case 0: 1179 goto retry; 1180 case -EBUSY: 1181 case -EAGAIN: 1182 ret = 0; 1183 fallthrough; 1184 case -EFAULT: 1185 case -ENOMEM: 1186 case -EHWPOISON: 1187 goto out; 1188 } 1189 BUG(); 1190 } else if (PTR_ERR(page) == -EEXIST) { 1191 /* 1192 * Proper page table entry exists, but no corresponding 1193 * struct page. If the caller expects **pages to be 1194 * filled in, bail out now, because that can't be done 1195 * for this page. 1196 */ 1197 if (pages) { 1198 ret = PTR_ERR(page); 1199 goto out; 1200 } 1201 1202 goto next_page; 1203 } else if (IS_ERR(page)) { 1204 ret = PTR_ERR(page); 1205 goto out; 1206 } 1207 if (pages) { 1208 pages[i] = page; 1209 flush_anon_page(vma, page, start); 1210 flush_dcache_page(page); 1211 ctx.page_mask = 0; 1212 } 1213 next_page: 1214 page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask); 1215 if (page_increm > nr_pages) 1216 page_increm = nr_pages; 1217 i += page_increm; 1218 start += page_increm * PAGE_SIZE; 1219 nr_pages -= page_increm; 1220 } while (nr_pages); 1221 out: 1222 if (ctx.pgmap) 1223 put_dev_pagemap(ctx.pgmap); 1224 return i ? i : ret; 1225 } 1226 1227 static bool vma_permits_fault(struct vm_area_struct *vma, 1228 unsigned int fault_flags) 1229 { 1230 bool write = !!(fault_flags & FAULT_FLAG_WRITE); 1231 bool foreign = !!(fault_flags & FAULT_FLAG_REMOTE); 1232 vm_flags_t vm_flags = write ? VM_WRITE : VM_READ; 1233 1234 if (!(vm_flags & vma->vm_flags)) 1235 return false; 1236 1237 /* 1238 * The architecture might have a hardware protection 1239 * mechanism other than read/write that can deny access. 1240 * 1241 * gup always represents data access, not instruction 1242 * fetches, so execute=false here: 1243 */ 1244 if (!arch_vma_access_permitted(vma, write, false, foreign)) 1245 return false; 1246 1247 return true; 1248 } 1249 1250 /** 1251 * fixup_user_fault() - manually resolve a user page fault 1252 * @mm: mm_struct of target mm 1253 * @address: user address 1254 * @fault_flags:flags to pass down to handle_mm_fault() 1255 * @unlocked: did we unlock the mmap_lock while retrying, maybe NULL if caller 1256 * does not allow retry. If NULL, the caller must guarantee 1257 * that fault_flags does not contain FAULT_FLAG_ALLOW_RETRY. 1258 * 1259 * This is meant to be called in the specific scenario where for locking reasons 1260 * we try to access user memory in atomic context (within a pagefault_disable() 1261 * section), this returns -EFAULT, and we want to resolve the user fault before 1262 * trying again. 1263 * 1264 * Typically this is meant to be used by the futex code. 1265 * 1266 * The main difference with get_user_pages() is that this function will 1267 * unconditionally call handle_mm_fault() which will in turn perform all the 1268 * necessary SW fixup of the dirty and young bits in the PTE, while 1269 * get_user_pages() only guarantees to update these in the struct page. 1270 * 1271 * This is important for some architectures where those bits also gate the 1272 * access permission to the page because they are maintained in software. On 1273 * such architectures, gup() will not be enough to make a subsequent access 1274 * succeed. 1275 * 1276 * This function will not return with an unlocked mmap_lock. So it has not the 1277 * same semantics wrt the @mm->mmap_lock as does filemap_fault(). 1278 */ 1279 int fixup_user_fault(struct mm_struct *mm, 1280 unsigned long address, unsigned int fault_flags, 1281 bool *unlocked) 1282 { 1283 struct vm_area_struct *vma; 1284 vm_fault_t ret; 1285 1286 address = untagged_addr_remote(mm, address); 1287 1288 if (unlocked) 1289 fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 1290 1291 retry: 1292 vma = find_extend_vma(mm, address); 1293 if (!vma || address < vma->vm_start) 1294 return -EFAULT; 1295 1296 if (!vma_permits_fault(vma, fault_flags)) 1297 return -EFAULT; 1298 1299 if ((fault_flags & FAULT_FLAG_KILLABLE) && 1300 fatal_signal_pending(current)) 1301 return -EINTR; 1302 1303 ret = handle_mm_fault(vma, address, fault_flags, NULL); 1304 1305 if (ret & VM_FAULT_COMPLETED) { 1306 /* 1307 * NOTE: it's a pity that we need to retake the lock here 1308 * to pair with the unlock() in the callers. Ideally we 1309 * could tell the callers so they do not need to unlock. 1310 */ 1311 mmap_read_lock(mm); 1312 *unlocked = true; 1313 return 0; 1314 } 1315 1316 if (ret & VM_FAULT_ERROR) { 1317 int err = vm_fault_to_errno(ret, 0); 1318 1319 if (err) 1320 return err; 1321 BUG(); 1322 } 1323 1324 if (ret & VM_FAULT_RETRY) { 1325 mmap_read_lock(mm); 1326 *unlocked = true; 1327 fault_flags |= FAULT_FLAG_TRIED; 1328 goto retry; 1329 } 1330 1331 return 0; 1332 } 1333 EXPORT_SYMBOL_GPL(fixup_user_fault); 1334 1335 /* 1336 * GUP always responds to fatal signals. When FOLL_INTERRUPTIBLE is 1337 * specified, it'll also respond to generic signals. The caller of GUP 1338 * that has FOLL_INTERRUPTIBLE should take care of the GUP interruption. 1339 */ 1340 static bool gup_signal_pending(unsigned int flags) 1341 { 1342 if (fatal_signal_pending(current)) 1343 return true; 1344 1345 if (!(flags & FOLL_INTERRUPTIBLE)) 1346 return false; 1347 1348 return signal_pending(current); 1349 } 1350 1351 /* 1352 * Locking: (*locked == 1) means that the mmap_lock has already been acquired by 1353 * the caller. This function may drop the mmap_lock. If it does so, then it will 1354 * set (*locked = 0). 1355 * 1356 * (*locked == 0) means that the caller expects this function to acquire and 1357 * drop the mmap_lock. Therefore, the value of *locked will still be zero when 1358 * the function returns, even though it may have changed temporarily during 1359 * function execution. 1360 * 1361 * Please note that this function, unlike __get_user_pages(), will not return 0 1362 * for nr_pages > 0, unless FOLL_NOWAIT is used. 1363 */ 1364 static __always_inline long __get_user_pages_locked(struct mm_struct *mm, 1365 unsigned long start, 1366 unsigned long nr_pages, 1367 struct page **pages, 1368 int *locked, 1369 unsigned int flags) 1370 { 1371 long ret, pages_done; 1372 bool must_unlock = false; 1373 1374 /* 1375 * The internal caller expects GUP to manage the lock internally and the 1376 * lock must be released when this returns. 1377 */ 1378 if (!*locked) { 1379 if (mmap_read_lock_killable(mm)) 1380 return -EAGAIN; 1381 must_unlock = true; 1382 *locked = 1; 1383 } 1384 else 1385 mmap_assert_locked(mm); 1386 1387 if (flags & FOLL_PIN) 1388 mm_set_has_pinned_flag(&mm->flags); 1389 1390 /* 1391 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior 1392 * is to set FOLL_GET if the caller wants pages[] filled in (but has 1393 * carelessly failed to specify FOLL_GET), so keep doing that, but only 1394 * for FOLL_GET, not for the newer FOLL_PIN. 1395 * 1396 * FOLL_PIN always expects pages to be non-null, but no need to assert 1397 * that here, as any failures will be obvious enough. 1398 */ 1399 if (pages && !(flags & FOLL_PIN)) 1400 flags |= FOLL_GET; 1401 1402 pages_done = 0; 1403 for (;;) { 1404 ret = __get_user_pages(mm, start, nr_pages, flags, pages, 1405 locked); 1406 if (!(flags & FOLL_UNLOCKABLE)) { 1407 /* VM_FAULT_RETRY couldn't trigger, bypass */ 1408 pages_done = ret; 1409 break; 1410 } 1411 1412 /* VM_FAULT_RETRY or VM_FAULT_COMPLETED cannot return errors */ 1413 if (!*locked) { 1414 BUG_ON(ret < 0); 1415 BUG_ON(ret >= nr_pages); 1416 } 1417 1418 if (ret > 0) { 1419 nr_pages -= ret; 1420 pages_done += ret; 1421 if (!nr_pages) 1422 break; 1423 } 1424 if (*locked) { 1425 /* 1426 * VM_FAULT_RETRY didn't trigger or it was a 1427 * FOLL_NOWAIT. 1428 */ 1429 if (!pages_done) 1430 pages_done = ret; 1431 break; 1432 } 1433 /* 1434 * VM_FAULT_RETRY triggered, so seek to the faulting offset. 1435 * For the prefault case (!pages) we only update counts. 1436 */ 1437 if (likely(pages)) 1438 pages += ret; 1439 start += ret << PAGE_SHIFT; 1440 1441 /* The lock was temporarily dropped, so we must unlock later */ 1442 must_unlock = true; 1443 1444 retry: 1445 /* 1446 * Repeat on the address that fired VM_FAULT_RETRY 1447 * with both FAULT_FLAG_ALLOW_RETRY and 1448 * FAULT_FLAG_TRIED. Note that GUP can be interrupted 1449 * by fatal signals of even common signals, depending on 1450 * the caller's request. So we need to check it before we 1451 * start trying again otherwise it can loop forever. 1452 */ 1453 if (gup_signal_pending(flags)) { 1454 if (!pages_done) 1455 pages_done = -EINTR; 1456 break; 1457 } 1458 1459 ret = mmap_read_lock_killable(mm); 1460 if (ret) { 1461 BUG_ON(ret > 0); 1462 if (!pages_done) 1463 pages_done = ret; 1464 break; 1465 } 1466 1467 *locked = 1; 1468 ret = __get_user_pages(mm, start, 1, flags | FOLL_TRIED, 1469 pages, locked); 1470 if (!*locked) { 1471 /* Continue to retry until we succeeded */ 1472 BUG_ON(ret != 0); 1473 goto retry; 1474 } 1475 if (ret != 1) { 1476 BUG_ON(ret > 1); 1477 if (!pages_done) 1478 pages_done = ret; 1479 break; 1480 } 1481 nr_pages--; 1482 pages_done++; 1483 if (!nr_pages) 1484 break; 1485 if (likely(pages)) 1486 pages++; 1487 start += PAGE_SIZE; 1488 } 1489 if (must_unlock && *locked) { 1490 /* 1491 * We either temporarily dropped the lock, or the caller 1492 * requested that we both acquire and drop the lock. Either way, 1493 * we must now unlock, and notify the caller of that state. 1494 */ 1495 mmap_read_unlock(mm); 1496 *locked = 0; 1497 } 1498 return pages_done; 1499 } 1500 1501 /** 1502 * populate_vma_page_range() - populate a range of pages in the vma. 1503 * @vma: target vma 1504 * @start: start address 1505 * @end: end address 1506 * @locked: whether the mmap_lock is still held 1507 * 1508 * This takes care of mlocking the pages too if VM_LOCKED is set. 1509 * 1510 * Return either number of pages pinned in the vma, or a negative error 1511 * code on error. 1512 * 1513 * vma->vm_mm->mmap_lock must be held. 1514 * 1515 * If @locked is NULL, it may be held for read or write and will 1516 * be unperturbed. 1517 * 1518 * If @locked is non-NULL, it must held for read only and may be 1519 * released. If it's released, *@locked will be set to 0. 1520 */ 1521 long populate_vma_page_range(struct vm_area_struct *vma, 1522 unsigned long start, unsigned long end, int *locked) 1523 { 1524 struct mm_struct *mm = vma->vm_mm; 1525 unsigned long nr_pages = (end - start) / PAGE_SIZE; 1526 int local_locked = 1; 1527 int gup_flags; 1528 long ret; 1529 1530 VM_BUG_ON(!PAGE_ALIGNED(start)); 1531 VM_BUG_ON(!PAGE_ALIGNED(end)); 1532 VM_BUG_ON_VMA(start < vma->vm_start, vma); 1533 VM_BUG_ON_VMA(end > vma->vm_end, vma); 1534 mmap_assert_locked(mm); 1535 1536 /* 1537 * Rightly or wrongly, the VM_LOCKONFAULT case has never used 1538 * faultin_page() to break COW, so it has no work to do here. 1539 */ 1540 if (vma->vm_flags & VM_LOCKONFAULT) 1541 return nr_pages; 1542 1543 gup_flags = FOLL_TOUCH; 1544 /* 1545 * We want to touch writable mappings with a write fault in order 1546 * to break COW, except for shared mappings because these don't COW 1547 * and we would not want to dirty them for nothing. 1548 */ 1549 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) 1550 gup_flags |= FOLL_WRITE; 1551 1552 /* 1553 * We want mlock to succeed for regions that have any permissions 1554 * other than PROT_NONE. 1555 */ 1556 if (vma_is_accessible(vma)) 1557 gup_flags |= FOLL_FORCE; 1558 1559 if (locked) 1560 gup_flags |= FOLL_UNLOCKABLE; 1561 1562 /* 1563 * We made sure addr is within a VMA, so the following will 1564 * not result in a stack expansion that recurses back here. 1565 */ 1566 ret = __get_user_pages(mm, start, nr_pages, gup_flags, 1567 NULL, locked ? locked : &local_locked); 1568 lru_add_drain(); 1569 return ret; 1570 } 1571 1572 /* 1573 * faultin_vma_page_range() - populate (prefault) page tables inside the 1574 * given VMA range readable/writable 1575 * 1576 * This takes care of mlocking the pages, too, if VM_LOCKED is set. 1577 * 1578 * @vma: target vma 1579 * @start: start address 1580 * @end: end address 1581 * @write: whether to prefault readable or writable 1582 * @locked: whether the mmap_lock is still held 1583 * 1584 * Returns either number of processed pages in the vma, or a negative error 1585 * code on error (see __get_user_pages()). 1586 * 1587 * vma->vm_mm->mmap_lock must be held. The range must be page-aligned and 1588 * covered by the VMA. If it's released, *@locked will be set to 0. 1589 */ 1590 long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, 1591 unsigned long end, bool write, int *locked) 1592 { 1593 struct mm_struct *mm = vma->vm_mm; 1594 unsigned long nr_pages = (end - start) / PAGE_SIZE; 1595 int gup_flags; 1596 long ret; 1597 1598 VM_BUG_ON(!PAGE_ALIGNED(start)); 1599 VM_BUG_ON(!PAGE_ALIGNED(end)); 1600 VM_BUG_ON_VMA(start < vma->vm_start, vma); 1601 VM_BUG_ON_VMA(end > vma->vm_end, vma); 1602 mmap_assert_locked(mm); 1603 1604 /* 1605 * FOLL_TOUCH: Mark page accessed and thereby young; will also mark 1606 * the page dirty with FOLL_WRITE -- which doesn't make a 1607 * difference with !FOLL_FORCE, because the page is writable 1608 * in the page table. 1609 * FOLL_HWPOISON: Return -EHWPOISON instead of -EFAULT when we hit 1610 * a poisoned page. 1611 * !FOLL_FORCE: Require proper access permissions. 1612 */ 1613 gup_flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_UNLOCKABLE; 1614 if (write) 1615 gup_flags |= FOLL_WRITE; 1616 1617 /* 1618 * We want to report -EINVAL instead of -EFAULT for any permission 1619 * problems or incompatible mappings. 1620 */ 1621 if (check_vma_flags(vma, gup_flags)) 1622 return -EINVAL; 1623 1624 ret = __get_user_pages(mm, start, nr_pages, gup_flags, 1625 NULL, locked); 1626 lru_add_drain(); 1627 return ret; 1628 } 1629 1630 /* 1631 * __mm_populate - populate and/or mlock pages within a range of address space. 1632 * 1633 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap 1634 * flags. VMAs must be already marked with the desired vm_flags, and 1635 * mmap_lock must not be held. 1636 */ 1637 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) 1638 { 1639 struct mm_struct *mm = current->mm; 1640 unsigned long end, nstart, nend; 1641 struct vm_area_struct *vma = NULL; 1642 int locked = 0; 1643 long ret = 0; 1644 1645 end = start + len; 1646 1647 for (nstart = start; nstart < end; nstart = nend) { 1648 /* 1649 * We want to fault in pages for [nstart; end) address range. 1650 * Find first corresponding VMA. 1651 */ 1652 if (!locked) { 1653 locked = 1; 1654 mmap_read_lock(mm); 1655 vma = find_vma_intersection(mm, nstart, end); 1656 } else if (nstart >= vma->vm_end) 1657 vma = find_vma_intersection(mm, vma->vm_end, end); 1658 1659 if (!vma) 1660 break; 1661 /* 1662 * Set [nstart; nend) to intersection of desired address 1663 * range with the first VMA. Also, skip undesirable VMA types. 1664 */ 1665 nend = min(end, vma->vm_end); 1666 if (vma->vm_flags & (VM_IO | VM_PFNMAP)) 1667 continue; 1668 if (nstart < vma->vm_start) 1669 nstart = vma->vm_start; 1670 /* 1671 * Now fault in a range of pages. populate_vma_page_range() 1672 * double checks the vma flags, so that it won't mlock pages 1673 * if the vma was already munlocked. 1674 */ 1675 ret = populate_vma_page_range(vma, nstart, nend, &locked); 1676 if (ret < 0) { 1677 if (ignore_errors) { 1678 ret = 0; 1679 continue; /* continue at next VMA */ 1680 } 1681 break; 1682 } 1683 nend = nstart + ret * PAGE_SIZE; 1684 ret = 0; 1685 } 1686 if (locked) 1687 mmap_read_unlock(mm); 1688 return ret; /* 0 or negative error code */ 1689 } 1690 #else /* CONFIG_MMU */ 1691 static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, 1692 unsigned long nr_pages, struct page **pages, 1693 int *locked, unsigned int foll_flags) 1694 { 1695 struct vm_area_struct *vma; 1696 bool must_unlock = false; 1697 unsigned long vm_flags; 1698 long i; 1699 1700 if (!nr_pages) 1701 return 0; 1702 1703 /* 1704 * The internal caller expects GUP to manage the lock internally and the 1705 * lock must be released when this returns. 1706 */ 1707 if (!*locked) { 1708 if (mmap_read_lock_killable(mm)) 1709 return -EAGAIN; 1710 must_unlock = true; 1711 *locked = 1; 1712 } 1713 1714 /* calculate required read or write permissions. 1715 * If FOLL_FORCE is set, we only require the "MAY" flags. 1716 */ 1717 vm_flags = (foll_flags & FOLL_WRITE) ? 1718 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 1719 vm_flags &= (foll_flags & FOLL_FORCE) ? 1720 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 1721 1722 for (i = 0; i < nr_pages; i++) { 1723 vma = find_vma(mm, start); 1724 if (!vma) 1725 break; 1726 1727 /* protect what we can, including chardevs */ 1728 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || 1729 !(vm_flags & vma->vm_flags)) 1730 break; 1731 1732 if (pages) { 1733 pages[i] = virt_to_page((void *)start); 1734 if (pages[i]) 1735 get_page(pages[i]); 1736 } 1737 1738 start = (start + PAGE_SIZE) & PAGE_MASK; 1739 } 1740 1741 if (must_unlock && *locked) { 1742 mmap_read_unlock(mm); 1743 *locked = 0; 1744 } 1745 1746 return i ? : -EFAULT; 1747 } 1748 #endif /* !CONFIG_MMU */ 1749 1750 /** 1751 * fault_in_writeable - fault in userspace address range for writing 1752 * @uaddr: start of address range 1753 * @size: size of address range 1754 * 1755 * Returns the number of bytes not faulted in (like copy_to_user() and 1756 * copy_from_user()). 1757 */ 1758 size_t fault_in_writeable(char __user *uaddr, size_t size) 1759 { 1760 char __user *start = uaddr, *end; 1761 1762 if (unlikely(size == 0)) 1763 return 0; 1764 if (!user_write_access_begin(uaddr, size)) 1765 return size; 1766 if (!PAGE_ALIGNED(uaddr)) { 1767 unsafe_put_user(0, uaddr, out); 1768 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr); 1769 } 1770 end = (char __user *)PAGE_ALIGN((unsigned long)start + size); 1771 if (unlikely(end < start)) 1772 end = NULL; 1773 while (uaddr != end) { 1774 unsafe_put_user(0, uaddr, out); 1775 uaddr += PAGE_SIZE; 1776 } 1777 1778 out: 1779 user_write_access_end(); 1780 if (size > uaddr - start) 1781 return size - (uaddr - start); 1782 return 0; 1783 } 1784 EXPORT_SYMBOL(fault_in_writeable); 1785 1786 /** 1787 * fault_in_subpage_writeable - fault in an address range for writing 1788 * @uaddr: start of address range 1789 * @size: size of address range 1790 * 1791 * Fault in a user address range for writing while checking for permissions at 1792 * sub-page granularity (e.g. arm64 MTE). This function should be used when 1793 * the caller cannot guarantee forward progress of a copy_to_user() loop. 1794 * 1795 * Returns the number of bytes not faulted in (like copy_to_user() and 1796 * copy_from_user()). 1797 */ 1798 size_t fault_in_subpage_writeable(char __user *uaddr, size_t size) 1799 { 1800 size_t faulted_in; 1801 1802 /* 1803 * Attempt faulting in at page granularity first for page table 1804 * permission checking. The arch-specific probe_subpage_writeable() 1805 * functions may not check for this. 1806 */ 1807 faulted_in = size - fault_in_writeable(uaddr, size); 1808 if (faulted_in) 1809 faulted_in -= probe_subpage_writeable(uaddr, faulted_in); 1810 1811 return size - faulted_in; 1812 } 1813 EXPORT_SYMBOL(fault_in_subpage_writeable); 1814 1815 /* 1816 * fault_in_safe_writeable - fault in an address range for writing 1817 * @uaddr: start of address range 1818 * @size: length of address range 1819 * 1820 * Faults in an address range for writing. This is primarily useful when we 1821 * already know that some or all of the pages in the address range aren't in 1822 * memory. 1823 * 1824 * Unlike fault_in_writeable(), this function is non-destructive. 1825 * 1826 * Note that we don't pin or otherwise hold the pages referenced that we fault 1827 * in. There's no guarantee that they'll stay in memory for any duration of 1828 * time. 1829 * 1830 * Returns the number of bytes not faulted in, like copy_to_user() and 1831 * copy_from_user(). 1832 */ 1833 size_t fault_in_safe_writeable(const char __user *uaddr, size_t size) 1834 { 1835 unsigned long start = (unsigned long)uaddr, end; 1836 struct mm_struct *mm = current->mm; 1837 bool unlocked = false; 1838 1839 if (unlikely(size == 0)) 1840 return 0; 1841 end = PAGE_ALIGN(start + size); 1842 if (end < start) 1843 end = 0; 1844 1845 mmap_read_lock(mm); 1846 do { 1847 if (fixup_user_fault(mm, start, FAULT_FLAG_WRITE, &unlocked)) 1848 break; 1849 start = (start + PAGE_SIZE) & PAGE_MASK; 1850 } while (start != end); 1851 mmap_read_unlock(mm); 1852 1853 if (size > (unsigned long)uaddr - start) 1854 return size - ((unsigned long)uaddr - start); 1855 return 0; 1856 } 1857 EXPORT_SYMBOL(fault_in_safe_writeable); 1858 1859 /** 1860 * fault_in_readable - fault in userspace address range for reading 1861 * @uaddr: start of user address range 1862 * @size: size of user address range 1863 * 1864 * Returns the number of bytes not faulted in (like copy_to_user() and 1865 * copy_from_user()). 1866 */ 1867 size_t fault_in_readable(const char __user *uaddr, size_t size) 1868 { 1869 const char __user *start = uaddr, *end; 1870 volatile char c; 1871 1872 if (unlikely(size == 0)) 1873 return 0; 1874 if (!user_read_access_begin(uaddr, size)) 1875 return size; 1876 if (!PAGE_ALIGNED(uaddr)) { 1877 unsafe_get_user(c, uaddr, out); 1878 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr); 1879 } 1880 end = (const char __user *)PAGE_ALIGN((unsigned long)start + size); 1881 if (unlikely(end < start)) 1882 end = NULL; 1883 while (uaddr != end) { 1884 unsafe_get_user(c, uaddr, out); 1885 uaddr += PAGE_SIZE; 1886 } 1887 1888 out: 1889 user_read_access_end(); 1890 (void)c; 1891 if (size > uaddr - start) 1892 return size - (uaddr - start); 1893 return 0; 1894 } 1895 EXPORT_SYMBOL(fault_in_readable); 1896 1897 /** 1898 * get_dump_page() - pin user page in memory while writing it to core dump 1899 * @addr: user address 1900 * 1901 * Returns struct page pointer of user page pinned for dump, 1902 * to be freed afterwards by put_page(). 1903 * 1904 * Returns NULL on any kind of failure - a hole must then be inserted into 1905 * the corefile, to preserve alignment with its headers; and also returns 1906 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1907 * allowing a hole to be left in the corefile to save disk space. 1908 * 1909 * Called without mmap_lock (takes and releases the mmap_lock by itself). 1910 */ 1911 #ifdef CONFIG_ELF_CORE 1912 struct page *get_dump_page(unsigned long addr) 1913 { 1914 struct page *page; 1915 int locked = 0; 1916 int ret; 1917 1918 ret = __get_user_pages_locked(current->mm, addr, 1, &page, &locked, 1919 FOLL_FORCE | FOLL_DUMP | FOLL_GET); 1920 return (ret == 1) ? page : NULL; 1921 } 1922 #endif /* CONFIG_ELF_CORE */ 1923 1924 #ifdef CONFIG_MIGRATION 1925 /* 1926 * Returns the number of collected pages. Return value is always >= 0. 1927 */ 1928 static unsigned long collect_longterm_unpinnable_pages( 1929 struct list_head *movable_page_list, 1930 unsigned long nr_pages, 1931 struct page **pages) 1932 { 1933 unsigned long i, collected = 0; 1934 struct folio *prev_folio = NULL; 1935 bool drain_allow = true; 1936 1937 for (i = 0; i < nr_pages; i++) { 1938 struct folio *folio = page_folio(pages[i]); 1939 1940 if (folio == prev_folio) 1941 continue; 1942 prev_folio = folio; 1943 1944 if (folio_is_longterm_pinnable(folio)) 1945 continue; 1946 1947 collected++; 1948 1949 if (folio_is_device_coherent(folio)) 1950 continue; 1951 1952 if (folio_test_hugetlb(folio)) { 1953 isolate_hugetlb(folio, movable_page_list); 1954 continue; 1955 } 1956 1957 if (!folio_test_lru(folio) && drain_allow) { 1958 lru_add_drain_all(); 1959 drain_allow = false; 1960 } 1961 1962 if (!folio_isolate_lru(folio)) 1963 continue; 1964 1965 list_add_tail(&folio->lru, movable_page_list); 1966 node_stat_mod_folio(folio, 1967 NR_ISOLATED_ANON + folio_is_file_lru(folio), 1968 folio_nr_pages(folio)); 1969 } 1970 1971 return collected; 1972 } 1973 1974 /* 1975 * Unpins all pages and migrates device coherent pages and movable_page_list. 1976 * Returns -EAGAIN if all pages were successfully migrated or -errno for failure 1977 * (or partial success). 1978 */ 1979 static int migrate_longterm_unpinnable_pages( 1980 struct list_head *movable_page_list, 1981 unsigned long nr_pages, 1982 struct page **pages) 1983 { 1984 int ret; 1985 unsigned long i; 1986 1987 for (i = 0; i < nr_pages; i++) { 1988 struct folio *folio = page_folio(pages[i]); 1989 1990 if (folio_is_device_coherent(folio)) { 1991 /* 1992 * Migration will fail if the page is pinned, so convert 1993 * the pin on the source page to a normal reference. 1994 */ 1995 pages[i] = NULL; 1996 folio_get(folio); 1997 gup_put_folio(folio, 1, FOLL_PIN); 1998 1999 if (migrate_device_coherent_page(&folio->page)) { 2000 ret = -EBUSY; 2001 goto err; 2002 } 2003 2004 continue; 2005 } 2006 2007 /* 2008 * We can't migrate pages with unexpected references, so drop 2009 * the reference obtained by __get_user_pages_locked(). 2010 * Migrating pages have been added to movable_page_list after 2011 * calling folio_isolate_lru() which takes a reference so the 2012 * page won't be freed if it's migrating. 2013 */ 2014 unpin_user_page(pages[i]); 2015 pages[i] = NULL; 2016 } 2017 2018 if (!list_empty(movable_page_list)) { 2019 struct migration_target_control mtc = { 2020 .nid = NUMA_NO_NODE, 2021 .gfp_mask = GFP_USER | __GFP_NOWARN, 2022 }; 2023 2024 if (migrate_pages(movable_page_list, alloc_migration_target, 2025 NULL, (unsigned long)&mtc, MIGRATE_SYNC, 2026 MR_LONGTERM_PIN, NULL)) { 2027 ret = -ENOMEM; 2028 goto err; 2029 } 2030 } 2031 2032 putback_movable_pages(movable_page_list); 2033 2034 return -EAGAIN; 2035 2036 err: 2037 for (i = 0; i < nr_pages; i++) 2038 if (pages[i]) 2039 unpin_user_page(pages[i]); 2040 putback_movable_pages(movable_page_list); 2041 2042 return ret; 2043 } 2044 2045 /* 2046 * Check whether all pages are *allowed* to be pinned. Rather confusingly, all 2047 * pages in the range are required to be pinned via FOLL_PIN, before calling 2048 * this routine. 2049 * 2050 * If any pages in the range are not allowed to be pinned, then this routine 2051 * will migrate those pages away, unpin all the pages in the range and return 2052 * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then 2053 * call this routine again. 2054 * 2055 * If an error other than -EAGAIN occurs, this indicates a migration failure. 2056 * The caller should give up, and propagate the error back up the call stack. 2057 * 2058 * If everything is OK and all pages in the range are allowed to be pinned, then 2059 * this routine leaves all pages pinned and returns zero for success. 2060 */ 2061 static long check_and_migrate_movable_pages(unsigned long nr_pages, 2062 struct page **pages) 2063 { 2064 unsigned long collected; 2065 LIST_HEAD(movable_page_list); 2066 2067 collected = collect_longterm_unpinnable_pages(&movable_page_list, 2068 nr_pages, pages); 2069 if (!collected) 2070 return 0; 2071 2072 return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, 2073 pages); 2074 } 2075 #else 2076 static long check_and_migrate_movable_pages(unsigned long nr_pages, 2077 struct page **pages) 2078 { 2079 return 0; 2080 } 2081 #endif /* CONFIG_MIGRATION */ 2082 2083 /* 2084 * __gup_longterm_locked() is a wrapper for __get_user_pages_locked which 2085 * allows us to process the FOLL_LONGTERM flag. 2086 */ 2087 static long __gup_longterm_locked(struct mm_struct *mm, 2088 unsigned long start, 2089 unsigned long nr_pages, 2090 struct page **pages, 2091 int *locked, 2092 unsigned int gup_flags) 2093 { 2094 unsigned int flags; 2095 long rc, nr_pinned_pages; 2096 2097 if (!(gup_flags & FOLL_LONGTERM)) 2098 return __get_user_pages_locked(mm, start, nr_pages, pages, 2099 locked, gup_flags); 2100 2101 flags = memalloc_pin_save(); 2102 do { 2103 nr_pinned_pages = __get_user_pages_locked(mm, start, nr_pages, 2104 pages, locked, 2105 gup_flags); 2106 if (nr_pinned_pages <= 0) { 2107 rc = nr_pinned_pages; 2108 break; 2109 } 2110 2111 /* FOLL_LONGTERM implies FOLL_PIN */ 2112 rc = check_and_migrate_movable_pages(nr_pinned_pages, pages); 2113 } while (rc == -EAGAIN); 2114 memalloc_pin_restore(flags); 2115 return rc ? rc : nr_pinned_pages; 2116 } 2117 2118 /* 2119 * Check that the given flags are valid for the exported gup/pup interface, and 2120 * update them with the required flags that the caller must have set. 2121 */ 2122 static bool is_valid_gup_args(struct page **pages, int *locked, 2123 unsigned int *gup_flags_p, unsigned int to_set) 2124 { 2125 unsigned int gup_flags = *gup_flags_p; 2126 2127 /* 2128 * These flags not allowed to be specified externally to the gup 2129 * interfaces: 2130 * - FOLL_PIN/FOLL_TRIED/FOLL_FAST_ONLY are internal only 2131 * - FOLL_REMOTE is internal only and used on follow_page() 2132 * - FOLL_UNLOCKABLE is internal only and used if locked is !NULL 2133 */ 2134 if (WARN_ON_ONCE(gup_flags & (FOLL_PIN | FOLL_TRIED | FOLL_UNLOCKABLE | 2135 FOLL_REMOTE | FOLL_FAST_ONLY))) 2136 return false; 2137 2138 gup_flags |= to_set; 2139 if (locked) { 2140 /* At the external interface locked must be set */ 2141 if (WARN_ON_ONCE(*locked != 1)) 2142 return false; 2143 2144 gup_flags |= FOLL_UNLOCKABLE; 2145 } 2146 2147 /* FOLL_GET and FOLL_PIN are mutually exclusive. */ 2148 if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) == 2149 (FOLL_PIN | FOLL_GET))) 2150 return false; 2151 2152 /* LONGTERM can only be specified when pinning */ 2153 if (WARN_ON_ONCE(!(gup_flags & FOLL_PIN) && (gup_flags & FOLL_LONGTERM))) 2154 return false; 2155 2156 /* Pages input must be given if using GET/PIN */ 2157 if (WARN_ON_ONCE((gup_flags & (FOLL_GET | FOLL_PIN)) && !pages)) 2158 return false; 2159 2160 /* We want to allow the pgmap to be hot-unplugged at all times */ 2161 if (WARN_ON_ONCE((gup_flags & FOLL_LONGTERM) && 2162 (gup_flags & FOLL_PCI_P2PDMA))) 2163 return false; 2164 2165 *gup_flags_p = gup_flags; 2166 return true; 2167 } 2168 2169 #ifdef CONFIG_MMU 2170 /** 2171 * get_user_pages_remote() - pin user pages in memory 2172 * @mm: mm_struct of target mm 2173 * @start: starting user address 2174 * @nr_pages: number of pages from start to pin 2175 * @gup_flags: flags modifying lookup behaviour 2176 * @pages: array that receives pointers to the pages pinned. 2177 * Should be at least nr_pages long. Or NULL, if caller 2178 * only intends to ensure the pages are faulted in. 2179 * @locked: pointer to lock flag indicating whether lock is held and 2180 * subsequently whether VM_FAULT_RETRY functionality can be 2181 * utilised. Lock must initially be held. 2182 * 2183 * Returns either number of pages pinned (which may be less than the 2184 * number requested), or an error. Details about the return value: 2185 * 2186 * -- If nr_pages is 0, returns 0. 2187 * -- If nr_pages is >0, but no pages were pinned, returns -errno. 2188 * -- If nr_pages is >0, and some pages were pinned, returns the number of 2189 * pages pinned. Again, this may be less than nr_pages. 2190 * 2191 * The caller is responsible for releasing returned @pages, via put_page(). 2192 * 2193 * Must be called with mmap_lock held for read or write. 2194 * 2195 * get_user_pages_remote walks a process's page tables and takes a reference 2196 * to each struct page that each user address corresponds to at a given 2197 * instant. That is, it takes the page that would be accessed if a user 2198 * thread accesses the given user virtual address at that instant. 2199 * 2200 * This does not guarantee that the page exists in the user mappings when 2201 * get_user_pages_remote returns, and there may even be a completely different 2202 * page there in some cases (eg. if mmapped pagecache has been invalidated 2203 * and subsequently re-faulted). However it does guarantee that the page 2204 * won't be freed completely. And mostly callers simply care that the page 2205 * contains data that was valid *at some point in time*. Typically, an IO 2206 * or similar operation cannot guarantee anything stronger anyway because 2207 * locks can't be held over the syscall boundary. 2208 * 2209 * If gup_flags & FOLL_WRITE == 0, the page must not be written to. If the page 2210 * is written to, set_page_dirty (or set_page_dirty_lock, as appropriate) must 2211 * be called after the page is finished with, and before put_page is called. 2212 * 2213 * get_user_pages_remote is typically used for fewer-copy IO operations, 2214 * to get a handle on the memory by some means other than accesses 2215 * via the user virtual addresses. The pages may be submitted for 2216 * DMA to devices or accessed via their kernel linear mapping (via the 2217 * kmap APIs). Care should be taken to use the correct cache flushing APIs. 2218 * 2219 * See also get_user_pages_fast, for performance critical applications. 2220 * 2221 * get_user_pages_remote should be phased out in favor of 2222 * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing 2223 * should use get_user_pages_remote because it cannot pass 2224 * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault. 2225 */ 2226 long get_user_pages_remote(struct mm_struct *mm, 2227 unsigned long start, unsigned long nr_pages, 2228 unsigned int gup_flags, struct page **pages, 2229 int *locked) 2230 { 2231 int local_locked = 1; 2232 2233 if (!is_valid_gup_args(pages, locked, &gup_flags, 2234 FOLL_TOUCH | FOLL_REMOTE)) 2235 return -EINVAL; 2236 2237 return __get_user_pages_locked(mm, start, nr_pages, pages, 2238 locked ? locked : &local_locked, 2239 gup_flags); 2240 } 2241 EXPORT_SYMBOL(get_user_pages_remote); 2242 2243 #else /* CONFIG_MMU */ 2244 long get_user_pages_remote(struct mm_struct *mm, 2245 unsigned long start, unsigned long nr_pages, 2246 unsigned int gup_flags, struct page **pages, 2247 int *locked) 2248 { 2249 return 0; 2250 } 2251 #endif /* !CONFIG_MMU */ 2252 2253 /** 2254 * get_user_pages() - pin user pages in memory 2255 * @start: starting user address 2256 * @nr_pages: number of pages from start to pin 2257 * @gup_flags: flags modifying lookup behaviour 2258 * @pages: array that receives pointers to the pages pinned. 2259 * Should be at least nr_pages long. Or NULL, if caller 2260 * only intends to ensure the pages are faulted in. 2261 * 2262 * This is the same as get_user_pages_remote(), just with a less-flexible 2263 * calling convention where we assume that the mm being operated on belongs to 2264 * the current task, and doesn't allow passing of a locked parameter. We also 2265 * obviously don't pass FOLL_REMOTE in here. 2266 */ 2267 long get_user_pages(unsigned long start, unsigned long nr_pages, 2268 unsigned int gup_flags, struct page **pages) 2269 { 2270 int locked = 1; 2271 2272 if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_TOUCH)) 2273 return -EINVAL; 2274 2275 return __get_user_pages_locked(current->mm, start, nr_pages, pages, 2276 &locked, gup_flags); 2277 } 2278 EXPORT_SYMBOL(get_user_pages); 2279 2280 /* 2281 * get_user_pages_unlocked() is suitable to replace the form: 2282 * 2283 * mmap_read_lock(mm); 2284 * get_user_pages(mm, ..., pages, NULL); 2285 * mmap_read_unlock(mm); 2286 * 2287 * with: 2288 * 2289 * get_user_pages_unlocked(mm, ..., pages); 2290 * 2291 * It is functionally equivalent to get_user_pages_fast so 2292 * get_user_pages_fast should be used instead if specific gup_flags 2293 * (e.g. FOLL_FORCE) are not required. 2294 */ 2295 long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 2296 struct page **pages, unsigned int gup_flags) 2297 { 2298 int locked = 0; 2299 2300 if (!is_valid_gup_args(pages, NULL, &gup_flags, 2301 FOLL_TOUCH | FOLL_UNLOCKABLE)) 2302 return -EINVAL; 2303 2304 return __get_user_pages_locked(current->mm, start, nr_pages, pages, 2305 &locked, gup_flags); 2306 } 2307 EXPORT_SYMBOL(get_user_pages_unlocked); 2308 2309 /* 2310 * Fast GUP 2311 * 2312 * get_user_pages_fast attempts to pin user pages by walking the page 2313 * tables directly and avoids taking locks. Thus the walker needs to be 2314 * protected from page table pages being freed from under it, and should 2315 * block any THP splits. 2316 * 2317 * One way to achieve this is to have the walker disable interrupts, and 2318 * rely on IPIs from the TLB flushing code blocking before the page table 2319 * pages are freed. This is unsuitable for architectures that do not need 2320 * to broadcast an IPI when invalidating TLBs. 2321 * 2322 * Another way to achieve this is to batch up page table containing pages 2323 * belonging to more than one mm_user, then rcu_sched a callback to free those 2324 * pages. Disabling interrupts will allow the fast_gup walker to both block 2325 * the rcu_sched callback, and an IPI that we broadcast for splitting THPs 2326 * (which is a relatively rare event). The code below adopts this strategy. 2327 * 2328 * Before activating this code, please be aware that the following assumptions 2329 * are currently made: 2330 * 2331 * *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to 2332 * free pages containing page tables or TLB flushing requires IPI broadcast. 2333 * 2334 * *) ptes can be read atomically by the architecture. 2335 * 2336 * *) access_ok is sufficient to validate userspace address ranges. 2337 * 2338 * The last two assumptions can be relaxed by the addition of helper functions. 2339 * 2340 * This code is based heavily on the PowerPC implementation by Nick Piggin. 2341 */ 2342 #ifdef CONFIG_HAVE_FAST_GUP 2343 2344 /* 2345 * Used in the GUP-fast path to determine whether a pin is permitted for a 2346 * specific folio. 2347 * 2348 * This call assumes the caller has pinned the folio, that the lowest page table 2349 * level still points to this folio, and that interrupts have been disabled. 2350 * 2351 * Writing to pinned file-backed dirty tracked folios is inherently problematic 2352 * (see comment describing the writable_file_mapping_allowed() function). We 2353 * therefore try to avoid the most egregious case of a long-term mapping doing 2354 * so. 2355 * 2356 * This function cannot be as thorough as that one as the VMA is not available 2357 * in the fast path, so instead we whitelist known good cases and if in doubt, 2358 * fall back to the slow path. 2359 */ 2360 static bool folio_fast_pin_allowed(struct folio *folio, unsigned int flags) 2361 { 2362 struct address_space *mapping; 2363 unsigned long mapping_flags; 2364 2365 /* 2366 * If we aren't pinning then no problematic write can occur. A long term 2367 * pin is the most egregious case so this is the one we disallow. 2368 */ 2369 if ((flags & (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) != 2370 (FOLL_PIN | FOLL_LONGTERM | FOLL_WRITE)) 2371 return true; 2372 2373 /* The folio is pinned, so we can safely access folio fields. */ 2374 2375 if (WARN_ON_ONCE(folio_test_slab(folio))) 2376 return false; 2377 2378 /* hugetlb mappings do not require dirty-tracking. */ 2379 if (folio_test_hugetlb(folio)) 2380 return true; 2381 2382 /* 2383 * GUP-fast disables IRQs. When IRQS are disabled, RCU grace periods 2384 * cannot proceed, which means no actions performed under RCU can 2385 * proceed either. 2386 * 2387 * inodes and thus their mappings are freed under RCU, which means the 2388 * mapping cannot be freed beneath us and thus we can safely dereference 2389 * it. 2390 */ 2391 lockdep_assert_irqs_disabled(); 2392 2393 /* 2394 * However, there may be operations which _alter_ the mapping, so ensure 2395 * we read it once and only once. 2396 */ 2397 mapping = READ_ONCE(folio->mapping); 2398 2399 /* 2400 * The mapping may have been truncated, in any case we cannot determine 2401 * if this mapping is safe - fall back to slow path to determine how to 2402 * proceed. 2403 */ 2404 if (!mapping) 2405 return false; 2406 2407 /* Anonymous folios pose no problem. */ 2408 mapping_flags = (unsigned long)mapping & PAGE_MAPPING_FLAGS; 2409 if (mapping_flags) 2410 return mapping_flags & PAGE_MAPPING_ANON; 2411 2412 /* 2413 * At this point, we know the mapping is non-null and points to an 2414 * address_space object. The only remaining whitelisted file system is 2415 * shmem. 2416 */ 2417 return shmem_mapping(mapping); 2418 } 2419 2420 static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start, 2421 unsigned int flags, 2422 struct page **pages) 2423 { 2424 while ((*nr) - nr_start) { 2425 struct page *page = pages[--(*nr)]; 2426 2427 ClearPageReferenced(page); 2428 if (flags & FOLL_PIN) 2429 unpin_user_page(page); 2430 else 2431 put_page(page); 2432 } 2433 } 2434 2435 #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL 2436 /* 2437 * Fast-gup relies on pte change detection to avoid concurrent pgtable 2438 * operations. 2439 * 2440 * To pin the page, fast-gup needs to do below in order: 2441 * (1) pin the page (by prefetching pte), then (2) check pte not changed. 2442 * 2443 * For the rest of pgtable operations where pgtable updates can be racy 2444 * with fast-gup, we need to do (1) clear pte, then (2) check whether page 2445 * is pinned. 2446 * 2447 * Above will work for all pte-level operations, including THP split. 2448 * 2449 * For THP collapse, it's a bit more complicated because fast-gup may be 2450 * walking a pgtable page that is being freed (pte is still valid but pmd 2451 * can be cleared already). To avoid race in such condition, we need to 2452 * also check pmd here to make sure pmd doesn't change (corresponds to 2453 * pmdp_collapse_flush() in the THP collapse code path). 2454 */ 2455 static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, 2456 unsigned long end, unsigned int flags, 2457 struct page **pages, int *nr) 2458 { 2459 struct dev_pagemap *pgmap = NULL; 2460 int nr_start = *nr, ret = 0; 2461 pte_t *ptep, *ptem; 2462 2463 ptem = ptep = pte_offset_map(&pmd, addr); 2464 if (!ptep) 2465 return 0; 2466 do { 2467 pte_t pte = ptep_get_lockless(ptep); 2468 struct page *page; 2469 struct folio *folio; 2470 2471 if (pte_protnone(pte) && !gup_can_follow_protnone(flags)) 2472 goto pte_unmap; 2473 2474 if (!pte_access_permitted(pte, flags & FOLL_WRITE)) 2475 goto pte_unmap; 2476 2477 if (pte_devmap(pte)) { 2478 if (unlikely(flags & FOLL_LONGTERM)) 2479 goto pte_unmap; 2480 2481 pgmap = get_dev_pagemap(pte_pfn(pte), pgmap); 2482 if (unlikely(!pgmap)) { 2483 undo_dev_pagemap(nr, nr_start, flags, pages); 2484 goto pte_unmap; 2485 } 2486 } else if (pte_special(pte)) 2487 goto pte_unmap; 2488 2489 VM_BUG_ON(!pfn_valid(pte_pfn(pte))); 2490 page = pte_page(pte); 2491 2492 folio = try_grab_folio(page, 1, flags); 2493 if (!folio) 2494 goto pte_unmap; 2495 2496 if (unlikely(page_is_secretmem(page))) { 2497 gup_put_folio(folio, 1, flags); 2498 goto pte_unmap; 2499 } 2500 2501 if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || 2502 unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) { 2503 gup_put_folio(folio, 1, flags); 2504 goto pte_unmap; 2505 } 2506 2507 if (!folio_fast_pin_allowed(folio, flags)) { 2508 gup_put_folio(folio, 1, flags); 2509 goto pte_unmap; 2510 } 2511 2512 if (!pte_write(pte) && gup_must_unshare(NULL, flags, page)) { 2513 gup_put_folio(folio, 1, flags); 2514 goto pte_unmap; 2515 } 2516 2517 /* 2518 * We need to make the page accessible if and only if we are 2519 * going to access its content (the FOLL_PIN case). Please 2520 * see Documentation/core-api/pin_user_pages.rst for 2521 * details. 2522 */ 2523 if (flags & FOLL_PIN) { 2524 ret = arch_make_page_accessible(page); 2525 if (ret) { 2526 gup_put_folio(folio, 1, flags); 2527 goto pte_unmap; 2528 } 2529 } 2530 folio_set_referenced(folio); 2531 pages[*nr] = page; 2532 (*nr)++; 2533 } while (ptep++, addr += PAGE_SIZE, addr != end); 2534 2535 ret = 1; 2536 2537 pte_unmap: 2538 if (pgmap) 2539 put_dev_pagemap(pgmap); 2540 pte_unmap(ptem); 2541 return ret; 2542 } 2543 #else 2544 2545 /* 2546 * If we can't determine whether or not a pte is special, then fail immediately 2547 * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not 2548 * to be special. 2549 * 2550 * For a futex to be placed on a THP tail page, get_futex_key requires a 2551 * get_user_pages_fast_only implementation that can pin pages. Thus it's still 2552 * useful to have gup_huge_pmd even if we can't operate on ptes. 2553 */ 2554 static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, 2555 unsigned long end, unsigned int flags, 2556 struct page **pages, int *nr) 2557 { 2558 return 0; 2559 } 2560 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */ 2561 2562 #if defined(CONFIG_ARCH_HAS_PTE_DEVMAP) && defined(CONFIG_TRANSPARENT_HUGEPAGE) 2563 static int __gup_device_huge(unsigned long pfn, unsigned long addr, 2564 unsigned long end, unsigned int flags, 2565 struct page **pages, int *nr) 2566 { 2567 int nr_start = *nr; 2568 struct dev_pagemap *pgmap = NULL; 2569 2570 do { 2571 struct page *page = pfn_to_page(pfn); 2572 2573 pgmap = get_dev_pagemap(pfn, pgmap); 2574 if (unlikely(!pgmap)) { 2575 undo_dev_pagemap(nr, nr_start, flags, pages); 2576 break; 2577 } 2578 2579 if (!(flags & FOLL_PCI_P2PDMA) && is_pci_p2pdma_page(page)) { 2580 undo_dev_pagemap(nr, nr_start, flags, pages); 2581 break; 2582 } 2583 2584 SetPageReferenced(page); 2585 pages[*nr] = page; 2586 if (unlikely(try_grab_page(page, flags))) { 2587 undo_dev_pagemap(nr, nr_start, flags, pages); 2588 break; 2589 } 2590 (*nr)++; 2591 pfn++; 2592 } while (addr += PAGE_SIZE, addr != end); 2593 2594 put_dev_pagemap(pgmap); 2595 return addr == end; 2596 } 2597 2598 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, 2599 unsigned long end, unsigned int flags, 2600 struct page **pages, int *nr) 2601 { 2602 unsigned long fault_pfn; 2603 int nr_start = *nr; 2604 2605 fault_pfn = pmd_pfn(orig) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); 2606 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) 2607 return 0; 2608 2609 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { 2610 undo_dev_pagemap(nr, nr_start, flags, pages); 2611 return 0; 2612 } 2613 return 1; 2614 } 2615 2616 static int __gup_device_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, 2617 unsigned long end, unsigned int flags, 2618 struct page **pages, int *nr) 2619 { 2620 unsigned long fault_pfn; 2621 int nr_start = *nr; 2622 2623 fault_pfn = pud_pfn(orig) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); 2624 if (!__gup_device_huge(fault_pfn, addr, end, flags, pages, nr)) 2625 return 0; 2626 2627 if (unlikely(pud_val(orig) != pud_val(*pudp))) { 2628 undo_dev_pagemap(nr, nr_start, flags, pages); 2629 return 0; 2630 } 2631 return 1; 2632 } 2633 #else 2634 static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, 2635 unsigned long end, unsigned int flags, 2636 struct page **pages, int *nr) 2637 { 2638 BUILD_BUG(); 2639 return 0; 2640 } 2641 2642 static int __gup_device_huge_pud(pud_t pud, pud_t *pudp, unsigned long addr, 2643 unsigned long end, unsigned int flags, 2644 struct page **pages, int *nr) 2645 { 2646 BUILD_BUG(); 2647 return 0; 2648 } 2649 #endif 2650 2651 static int record_subpages(struct page *page, unsigned long addr, 2652 unsigned long end, struct page **pages) 2653 { 2654 int nr; 2655 2656 for (nr = 0; addr != end; nr++, addr += PAGE_SIZE) 2657 pages[nr] = nth_page(page, nr); 2658 2659 return nr; 2660 } 2661 2662 #ifdef CONFIG_ARCH_HAS_HUGEPD 2663 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end, 2664 unsigned long sz) 2665 { 2666 unsigned long __boundary = (addr + sz) & ~(sz-1); 2667 return (__boundary - 1 < end - 1) ? __boundary : end; 2668 } 2669 2670 static int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, 2671 unsigned long end, unsigned int flags, 2672 struct page **pages, int *nr) 2673 { 2674 unsigned long pte_end; 2675 struct page *page; 2676 struct folio *folio; 2677 pte_t pte; 2678 int refs; 2679 2680 pte_end = (addr + sz) & ~(sz-1); 2681 if (pte_end < end) 2682 end = pte_end; 2683 2684 pte = huge_ptep_get(ptep); 2685 2686 if (!pte_access_permitted(pte, flags & FOLL_WRITE)) 2687 return 0; 2688 2689 /* hugepages are never "special" */ 2690 VM_BUG_ON(!pfn_valid(pte_pfn(pte))); 2691 2692 page = nth_page(pte_page(pte), (addr & (sz - 1)) >> PAGE_SHIFT); 2693 refs = record_subpages(page, addr, end, pages + *nr); 2694 2695 folio = try_grab_folio(page, refs, flags); 2696 if (!folio) 2697 return 0; 2698 2699 if (unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) { 2700 gup_put_folio(folio, refs, flags); 2701 return 0; 2702 } 2703 2704 if (!folio_fast_pin_allowed(folio, flags)) { 2705 gup_put_folio(folio, refs, flags); 2706 return 0; 2707 } 2708 2709 if (!pte_write(pte) && gup_must_unshare(NULL, flags, &folio->page)) { 2710 gup_put_folio(folio, refs, flags); 2711 return 0; 2712 } 2713 2714 *nr += refs; 2715 folio_set_referenced(folio); 2716 return 1; 2717 } 2718 2719 static int gup_huge_pd(hugepd_t hugepd, unsigned long addr, 2720 unsigned int pdshift, unsigned long end, unsigned int flags, 2721 struct page **pages, int *nr) 2722 { 2723 pte_t *ptep; 2724 unsigned long sz = 1UL << hugepd_shift(hugepd); 2725 unsigned long next; 2726 2727 ptep = hugepte_offset(hugepd, addr, pdshift); 2728 do { 2729 next = hugepte_addr_end(addr, end, sz); 2730 if (!gup_hugepte(ptep, sz, addr, end, flags, pages, nr)) 2731 return 0; 2732 } while (ptep++, addr = next, addr != end); 2733 2734 return 1; 2735 } 2736 #else 2737 static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr, 2738 unsigned int pdshift, unsigned long end, unsigned int flags, 2739 struct page **pages, int *nr) 2740 { 2741 return 0; 2742 } 2743 #endif /* CONFIG_ARCH_HAS_HUGEPD */ 2744 2745 static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, 2746 unsigned long end, unsigned int flags, 2747 struct page **pages, int *nr) 2748 { 2749 struct page *page; 2750 struct folio *folio; 2751 int refs; 2752 2753 if (!pmd_access_permitted(orig, flags & FOLL_WRITE)) 2754 return 0; 2755 2756 if (pmd_devmap(orig)) { 2757 if (unlikely(flags & FOLL_LONGTERM)) 2758 return 0; 2759 return __gup_device_huge_pmd(orig, pmdp, addr, end, flags, 2760 pages, nr); 2761 } 2762 2763 page = nth_page(pmd_page(orig), (addr & ~PMD_MASK) >> PAGE_SHIFT); 2764 refs = record_subpages(page, addr, end, pages + *nr); 2765 2766 folio = try_grab_folio(page, refs, flags); 2767 if (!folio) 2768 return 0; 2769 2770 if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) { 2771 gup_put_folio(folio, refs, flags); 2772 return 0; 2773 } 2774 2775 if (!folio_fast_pin_allowed(folio, flags)) { 2776 gup_put_folio(folio, refs, flags); 2777 return 0; 2778 } 2779 if (!pmd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { 2780 gup_put_folio(folio, refs, flags); 2781 return 0; 2782 } 2783 2784 *nr += refs; 2785 folio_set_referenced(folio); 2786 return 1; 2787 } 2788 2789 static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, 2790 unsigned long end, unsigned int flags, 2791 struct page **pages, int *nr) 2792 { 2793 struct page *page; 2794 struct folio *folio; 2795 int refs; 2796 2797 if (!pud_access_permitted(orig, flags & FOLL_WRITE)) 2798 return 0; 2799 2800 if (pud_devmap(orig)) { 2801 if (unlikely(flags & FOLL_LONGTERM)) 2802 return 0; 2803 return __gup_device_huge_pud(orig, pudp, addr, end, flags, 2804 pages, nr); 2805 } 2806 2807 page = nth_page(pud_page(orig), (addr & ~PUD_MASK) >> PAGE_SHIFT); 2808 refs = record_subpages(page, addr, end, pages + *nr); 2809 2810 folio = try_grab_folio(page, refs, flags); 2811 if (!folio) 2812 return 0; 2813 2814 if (unlikely(pud_val(orig) != pud_val(*pudp))) { 2815 gup_put_folio(folio, refs, flags); 2816 return 0; 2817 } 2818 2819 if (!folio_fast_pin_allowed(folio, flags)) { 2820 gup_put_folio(folio, refs, flags); 2821 return 0; 2822 } 2823 2824 if (!pud_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { 2825 gup_put_folio(folio, refs, flags); 2826 return 0; 2827 } 2828 2829 *nr += refs; 2830 folio_set_referenced(folio); 2831 return 1; 2832 } 2833 2834 static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, 2835 unsigned long end, unsigned int flags, 2836 struct page **pages, int *nr) 2837 { 2838 int refs; 2839 struct page *page; 2840 struct folio *folio; 2841 2842 if (!pgd_access_permitted(orig, flags & FOLL_WRITE)) 2843 return 0; 2844 2845 BUILD_BUG_ON(pgd_devmap(orig)); 2846 2847 page = nth_page(pgd_page(orig), (addr & ~PGDIR_MASK) >> PAGE_SHIFT); 2848 refs = record_subpages(page, addr, end, pages + *nr); 2849 2850 folio = try_grab_folio(page, refs, flags); 2851 if (!folio) 2852 return 0; 2853 2854 if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) { 2855 gup_put_folio(folio, refs, flags); 2856 return 0; 2857 } 2858 2859 if (!pgd_write(orig) && gup_must_unshare(NULL, flags, &folio->page)) { 2860 gup_put_folio(folio, refs, flags); 2861 return 0; 2862 } 2863 2864 if (!folio_fast_pin_allowed(folio, flags)) { 2865 gup_put_folio(folio, refs, flags); 2866 return 0; 2867 } 2868 2869 *nr += refs; 2870 folio_set_referenced(folio); 2871 return 1; 2872 } 2873 2874 static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned long end, 2875 unsigned int flags, struct page **pages, int *nr) 2876 { 2877 unsigned long next; 2878 pmd_t *pmdp; 2879 2880 pmdp = pmd_offset_lockless(pudp, pud, addr); 2881 do { 2882 pmd_t pmd = pmdp_get_lockless(pmdp); 2883 2884 next = pmd_addr_end(addr, end); 2885 if (!pmd_present(pmd)) 2886 return 0; 2887 2888 if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) || 2889 pmd_devmap(pmd))) { 2890 if (pmd_protnone(pmd) && 2891 !gup_can_follow_protnone(flags)) 2892 return 0; 2893 2894 if (!gup_huge_pmd(pmd, pmdp, addr, next, flags, 2895 pages, nr)) 2896 return 0; 2897 2898 } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) { 2899 /* 2900 * architecture have different format for hugetlbfs 2901 * pmd format and THP pmd format 2902 */ 2903 if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, 2904 PMD_SHIFT, next, flags, pages, nr)) 2905 return 0; 2906 } else if (!gup_pte_range(pmd, pmdp, addr, next, flags, pages, nr)) 2907 return 0; 2908 } while (pmdp++, addr = next, addr != end); 2909 2910 return 1; 2911 } 2912 2913 static int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr, unsigned long end, 2914 unsigned int flags, struct page **pages, int *nr) 2915 { 2916 unsigned long next; 2917 pud_t *pudp; 2918 2919 pudp = pud_offset_lockless(p4dp, p4d, addr); 2920 do { 2921 pud_t pud = READ_ONCE(*pudp); 2922 2923 next = pud_addr_end(addr, end); 2924 if (unlikely(!pud_present(pud))) 2925 return 0; 2926 if (unlikely(pud_huge(pud) || pud_devmap(pud))) { 2927 if (!gup_huge_pud(pud, pudp, addr, next, flags, 2928 pages, nr)) 2929 return 0; 2930 } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) { 2931 if (!gup_huge_pd(__hugepd(pud_val(pud)), addr, 2932 PUD_SHIFT, next, flags, pages, nr)) 2933 return 0; 2934 } else if (!gup_pmd_range(pudp, pud, addr, next, flags, pages, nr)) 2935 return 0; 2936 } while (pudp++, addr = next, addr != end); 2937 2938 return 1; 2939 } 2940 2941 static int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr, unsigned long end, 2942 unsigned int flags, struct page **pages, int *nr) 2943 { 2944 unsigned long next; 2945 p4d_t *p4dp; 2946 2947 p4dp = p4d_offset_lockless(pgdp, pgd, addr); 2948 do { 2949 p4d_t p4d = READ_ONCE(*p4dp); 2950 2951 next = p4d_addr_end(addr, end); 2952 if (p4d_none(p4d)) 2953 return 0; 2954 BUILD_BUG_ON(p4d_huge(p4d)); 2955 if (unlikely(is_hugepd(__hugepd(p4d_val(p4d))))) { 2956 if (!gup_huge_pd(__hugepd(p4d_val(p4d)), addr, 2957 P4D_SHIFT, next, flags, pages, nr)) 2958 return 0; 2959 } else if (!gup_pud_range(p4dp, p4d, addr, next, flags, pages, nr)) 2960 return 0; 2961 } while (p4dp++, addr = next, addr != end); 2962 2963 return 1; 2964 } 2965 2966 static void gup_pgd_range(unsigned long addr, unsigned long end, 2967 unsigned int flags, struct page **pages, int *nr) 2968 { 2969 unsigned long next; 2970 pgd_t *pgdp; 2971 2972 pgdp = pgd_offset(current->mm, addr); 2973 do { 2974 pgd_t pgd = READ_ONCE(*pgdp); 2975 2976 next = pgd_addr_end(addr, end); 2977 if (pgd_none(pgd)) 2978 return; 2979 if (unlikely(pgd_huge(pgd))) { 2980 if (!gup_huge_pgd(pgd, pgdp, addr, next, flags, 2981 pages, nr)) 2982 return; 2983 } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) { 2984 if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr, 2985 PGDIR_SHIFT, next, flags, pages, nr)) 2986 return; 2987 } else if (!gup_p4d_range(pgdp, pgd, addr, next, flags, pages, nr)) 2988 return; 2989 } while (pgdp++, addr = next, addr != end); 2990 } 2991 #else 2992 static inline void gup_pgd_range(unsigned long addr, unsigned long end, 2993 unsigned int flags, struct page **pages, int *nr) 2994 { 2995 } 2996 #endif /* CONFIG_HAVE_FAST_GUP */ 2997 2998 #ifndef gup_fast_permitted 2999 /* 3000 * Check if it's allowed to use get_user_pages_fast_only() for the range, or 3001 * we need to fall back to the slow version: 3002 */ 3003 static bool gup_fast_permitted(unsigned long start, unsigned long end) 3004 { 3005 return true; 3006 } 3007 #endif 3008 3009 static unsigned long lockless_pages_from_mm(unsigned long start, 3010 unsigned long end, 3011 unsigned int gup_flags, 3012 struct page **pages) 3013 { 3014 unsigned long flags; 3015 int nr_pinned = 0; 3016 unsigned seq; 3017 3018 if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) || 3019 !gup_fast_permitted(start, end)) 3020 return 0; 3021 3022 if (gup_flags & FOLL_PIN) { 3023 seq = raw_read_seqcount(¤t->mm->write_protect_seq); 3024 if (seq & 1) 3025 return 0; 3026 } 3027 3028 /* 3029 * Disable interrupts. The nested form is used, in order to allow full, 3030 * general purpose use of this routine. 3031 * 3032 * With interrupts disabled, we block page table pages from being freed 3033 * from under us. See struct mmu_table_batch comments in 3034 * include/asm-generic/tlb.h for more details. 3035 * 3036 * We do not adopt an rcu_read_lock() here as we also want to block IPIs 3037 * that come from THPs splitting. 3038 */ 3039 local_irq_save(flags); 3040 gup_pgd_range(start, end, gup_flags, pages, &nr_pinned); 3041 local_irq_restore(flags); 3042 3043 /* 3044 * When pinning pages for DMA there could be a concurrent write protect 3045 * from fork() via copy_page_range(), in this case always fail fast GUP. 3046 */ 3047 if (gup_flags & FOLL_PIN) { 3048 if (read_seqcount_retry(¤t->mm->write_protect_seq, seq)) { 3049 unpin_user_pages_lockless(pages, nr_pinned); 3050 return 0; 3051 } else { 3052 sanity_check_pinned_pages(pages, nr_pinned); 3053 } 3054 } 3055 return nr_pinned; 3056 } 3057 3058 static int internal_get_user_pages_fast(unsigned long start, 3059 unsigned long nr_pages, 3060 unsigned int gup_flags, 3061 struct page **pages) 3062 { 3063 unsigned long len, end; 3064 unsigned long nr_pinned; 3065 int locked = 0; 3066 int ret; 3067 3068 if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | 3069 FOLL_FORCE | FOLL_PIN | FOLL_GET | 3070 FOLL_FAST_ONLY | FOLL_NOFAULT | 3071 FOLL_PCI_P2PDMA))) 3072 return -EINVAL; 3073 3074 if (gup_flags & FOLL_PIN) 3075 mm_set_has_pinned_flag(¤t->mm->flags); 3076 3077 if (!(gup_flags & FOLL_FAST_ONLY)) 3078 might_lock_read(¤t->mm->mmap_lock); 3079 3080 start = untagged_addr(start) & PAGE_MASK; 3081 len = nr_pages << PAGE_SHIFT; 3082 if (check_add_overflow(start, len, &end)) 3083 return 0; 3084 if (end > TASK_SIZE_MAX) 3085 return -EFAULT; 3086 if (unlikely(!access_ok((void __user *)start, len))) 3087 return -EFAULT; 3088 3089 nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages); 3090 if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY) 3091 return nr_pinned; 3092 3093 /* Slow path: try to get the remaining pages with get_user_pages */ 3094 start += nr_pinned << PAGE_SHIFT; 3095 pages += nr_pinned; 3096 ret = __gup_longterm_locked(current->mm, start, nr_pages - nr_pinned, 3097 pages, &locked, 3098 gup_flags | FOLL_TOUCH | FOLL_UNLOCKABLE); 3099 if (ret < 0) { 3100 /* 3101 * The caller has to unpin the pages we already pinned so 3102 * returning -errno is not an option 3103 */ 3104 if (nr_pinned) 3105 return nr_pinned; 3106 return ret; 3107 } 3108 return ret + nr_pinned; 3109 } 3110 3111 /** 3112 * get_user_pages_fast_only() - pin user pages in memory 3113 * @start: starting user address 3114 * @nr_pages: number of pages from start to pin 3115 * @gup_flags: flags modifying pin behaviour 3116 * @pages: array that receives pointers to the pages pinned. 3117 * Should be at least nr_pages long. 3118 * 3119 * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to 3120 * the regular GUP. 3121 * 3122 * If the architecture does not support this function, simply return with no 3123 * pages pinned. 3124 * 3125 * Careful, careful! COW breaking can go either way, so a non-write 3126 * access can get ambiguous page results. If you call this function without 3127 * 'write' set, you'd better be sure that you're ok with that ambiguity. 3128 */ 3129 int get_user_pages_fast_only(unsigned long start, int nr_pages, 3130 unsigned int gup_flags, struct page **pages) 3131 { 3132 /* 3133 * Internally (within mm/gup.c), gup fast variants must set FOLL_GET, 3134 * because gup fast is always a "pin with a +1 page refcount" request. 3135 * 3136 * FOLL_FAST_ONLY is required in order to match the API description of 3137 * this routine: no fall back to regular ("slow") GUP. 3138 */ 3139 if (!is_valid_gup_args(pages, NULL, &gup_flags, 3140 FOLL_GET | FOLL_FAST_ONLY)) 3141 return -EINVAL; 3142 3143 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); 3144 } 3145 EXPORT_SYMBOL_GPL(get_user_pages_fast_only); 3146 3147 /** 3148 * get_user_pages_fast() - pin user pages in memory 3149 * @start: starting user address 3150 * @nr_pages: number of pages from start to pin 3151 * @gup_flags: flags modifying pin behaviour 3152 * @pages: array that receives pointers to the pages pinned. 3153 * Should be at least nr_pages long. 3154 * 3155 * Attempt to pin user pages in memory without taking mm->mmap_lock. 3156 * If not successful, it will fall back to taking the lock and 3157 * calling get_user_pages(). 3158 * 3159 * Returns number of pages pinned. This may be fewer than the number requested. 3160 * If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns 3161 * -errno. 3162 */ 3163 int get_user_pages_fast(unsigned long start, int nr_pages, 3164 unsigned int gup_flags, struct page **pages) 3165 { 3166 /* 3167 * The caller may or may not have explicitly set FOLL_GET; either way is 3168 * OK. However, internally (within mm/gup.c), gup fast variants must set 3169 * FOLL_GET, because gup fast is always a "pin with a +1 page refcount" 3170 * request. 3171 */ 3172 if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_GET)) 3173 return -EINVAL; 3174 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); 3175 } 3176 EXPORT_SYMBOL_GPL(get_user_pages_fast); 3177 3178 /** 3179 * pin_user_pages_fast() - pin user pages in memory without taking locks 3180 * 3181 * @start: starting user address 3182 * @nr_pages: number of pages from start to pin 3183 * @gup_flags: flags modifying pin behaviour 3184 * @pages: array that receives pointers to the pages pinned. 3185 * Should be at least nr_pages long. 3186 * 3187 * Nearly the same as get_user_pages_fast(), except that FOLL_PIN is set. See 3188 * get_user_pages_fast() for documentation on the function arguments, because 3189 * the arguments here are identical. 3190 * 3191 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please 3192 * see Documentation/core-api/pin_user_pages.rst for further details. 3193 */ 3194 int pin_user_pages_fast(unsigned long start, int nr_pages, 3195 unsigned int gup_flags, struct page **pages) 3196 { 3197 if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN)) 3198 return -EINVAL; 3199 return internal_get_user_pages_fast(start, nr_pages, gup_flags, pages); 3200 } 3201 EXPORT_SYMBOL_GPL(pin_user_pages_fast); 3202 3203 /** 3204 * pin_user_pages_remote() - pin pages of a remote process 3205 * 3206 * @mm: mm_struct of target mm 3207 * @start: starting user address 3208 * @nr_pages: number of pages from start to pin 3209 * @gup_flags: flags modifying lookup behaviour 3210 * @pages: array that receives pointers to the pages pinned. 3211 * Should be at least nr_pages long. 3212 * @locked: pointer to lock flag indicating whether lock is held and 3213 * subsequently whether VM_FAULT_RETRY functionality can be 3214 * utilised. Lock must initially be held. 3215 * 3216 * Nearly the same as get_user_pages_remote(), except that FOLL_PIN is set. See 3217 * get_user_pages_remote() for documentation on the function arguments, because 3218 * the arguments here are identical. 3219 * 3220 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please 3221 * see Documentation/core-api/pin_user_pages.rst for details. 3222 */ 3223 long pin_user_pages_remote(struct mm_struct *mm, 3224 unsigned long start, unsigned long nr_pages, 3225 unsigned int gup_flags, struct page **pages, 3226 int *locked) 3227 { 3228 int local_locked = 1; 3229 3230 if (!is_valid_gup_args(pages, locked, &gup_flags, 3231 FOLL_PIN | FOLL_TOUCH | FOLL_REMOTE)) 3232 return 0; 3233 return __gup_longterm_locked(mm, start, nr_pages, pages, 3234 locked ? locked : &local_locked, 3235 gup_flags); 3236 } 3237 EXPORT_SYMBOL(pin_user_pages_remote); 3238 3239 /** 3240 * pin_user_pages() - pin user pages in memory for use by other devices 3241 * 3242 * @start: starting user address 3243 * @nr_pages: number of pages from start to pin 3244 * @gup_flags: flags modifying lookup behaviour 3245 * @pages: array that receives pointers to the pages pinned. 3246 * Should be at least nr_pages long. 3247 * 3248 * Nearly the same as get_user_pages(), except that FOLL_TOUCH is not set, and 3249 * FOLL_PIN is set. 3250 * 3251 * FOLL_PIN means that the pages must be released via unpin_user_page(). Please 3252 * see Documentation/core-api/pin_user_pages.rst for details. 3253 */ 3254 long pin_user_pages(unsigned long start, unsigned long nr_pages, 3255 unsigned int gup_flags, struct page **pages) 3256 { 3257 int locked = 1; 3258 3259 if (!is_valid_gup_args(pages, NULL, &gup_flags, FOLL_PIN)) 3260 return 0; 3261 return __gup_longterm_locked(current->mm, start, nr_pages, 3262 pages, &locked, gup_flags); 3263 } 3264 EXPORT_SYMBOL(pin_user_pages); 3265 3266 /* 3267 * pin_user_pages_unlocked() is the FOLL_PIN variant of 3268 * get_user_pages_unlocked(). Behavior is the same, except that this one sets 3269 * FOLL_PIN and rejects FOLL_GET. 3270 */ 3271 long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages, 3272 struct page **pages, unsigned int gup_flags) 3273 { 3274 int locked = 0; 3275 3276 if (!is_valid_gup_args(pages, NULL, &gup_flags, 3277 FOLL_PIN | FOLL_TOUCH | FOLL_UNLOCKABLE)) 3278 return 0; 3279 3280 return __gup_longterm_locked(current->mm, start, nr_pages, pages, 3281 &locked, gup_flags); 3282 } 3283 EXPORT_SYMBOL(pin_user_pages_unlocked); 3284