memblock: ensure there is no overflow in memblock_overlaps_region()
[platform/kernel/linux-rpi.git] / mm / gup.c
index 98eb8e6..6cb7d8a 100644 (file)
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -44,6 +44,23 @@ static void hpage_pincount_sub(struct page *page, int refs)
        atomic_sub(refs, compound_pincount_ptr(page));
 }
 
+/* Equivalent to calling put_page() @refs times. */
+static void put_page_refs(struct page *page, int refs)
+{
+#ifdef CONFIG_DEBUG_VM
+       if (VM_WARN_ON_ONCE_PAGE(page_ref_count(page) < refs, page))
+               return;
+#endif
+
+       /*
+        * Calling put_page() for each ref is unnecessarily slow. Only the last
+        * ref needs a put_page().
+        */
+       if (refs > 1)
+               page_ref_sub(page, refs - 1);
+       put_page(page);
+}
+
 /*
  * Return the compound head page with ref appropriately incremented,
  * or NULL if that failed.
@@ -56,6 +73,21 @@ static inline struct page *try_get_compound_head(struct page *page, int refs)
                return NULL;
        if (unlikely(!page_cache_add_speculative(head, refs)))
                return NULL;
+
+       /*
+        * At this point we have a stable reference to the head page; but it
+        * could be that between the compound_head() lookup and the refcount
+        * increment, the compound page was split, in which case we'd end up
+        * holding a reference on a page that has nothing to do with the page
+        * we were given anymore.
+        * So now that the head page is stable, recheck that the pages still
+        * belong together.
+        */
+       if (unlikely(compound_head(page) != head)) {
+               put_page_refs(head, refs);
+               return NULL;
+       }
+
        return head;
 }
 
@@ -96,6 +128,14 @@ static __maybe_unused struct page *try_grab_compound_head(struct page *page,
                        return NULL;
 
                /*
+                * CAUTION: Don't use compound_head() on the page before this
+                * point, the result won't be stable.
+                */
+               page = try_get_compound_head(page, refs);
+               if (!page)
+                       return NULL;
+
+               /*
                 * When pinning a compound page of order > 1 (which is what
                 * hpage_pincount_available() checks for), use an exact count to
                 * track it, via hpage_pincount_add/_sub().
@@ -103,15 +143,10 @@ static __maybe_unused struct page *try_grab_compound_head(struct page *page,
                 * However, be sure to *also* increment the normal page refcount
                 * field at least once, so that the page really is pinned.
                 */
-               if (!hpage_pincount_available(page))
-                       refs *= GUP_PIN_COUNTING_BIAS;
-
-               page = try_get_compound_head(page, refs);
-               if (!page)
-                       return NULL;
-
                if (hpage_pincount_available(page))
                        hpage_pincount_add(page, refs);
+               else
+                       page_ref_add(page, refs * (GUP_PIN_COUNTING_BIAS - 1));
 
                mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_ACQUIRED,
                                    orig_refs);
@@ -123,6 +158,21 @@ static __maybe_unused struct page *try_grab_compound_head(struct page *page,
        return NULL;
 }
 
+static void put_compound_head(struct page *page, int refs, unsigned int flags)
+{
+       if (flags & FOLL_PIN) {
+               mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
+                                   refs);
+
+               if (hpage_pincount_available(page))
+                       hpage_pincount_sub(page, refs);
+               else
+                       refs *= GUP_PIN_COUNTING_BIAS;
+       }
+
+       put_page_refs(page, refs);
+}
+
 /**
  * try_grab_page() - elevate a page's refcount by a flag-dependent amount
  *
@@ -177,41 +227,6 @@ bool __must_check try_grab_page(struct page *page, unsigned int flags)
        return true;
 }
 
-#ifdef CONFIG_DEV_PAGEMAP_OPS
-static bool __unpin_devmap_managed_user_page(struct page *page)
-{
-       int count, refs = 1;
-
-       if (!page_is_devmap_managed(page))
-               return false;
-
-       if (hpage_pincount_available(page))
-               hpage_pincount_sub(page, 1);
-       else
-               refs = GUP_PIN_COUNTING_BIAS;
-
-       count = page_ref_sub_return(page, refs);
-
-       mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED, 1);
-       /*
-        * devmap page refcounts are 1-based, rather than 0-based: if
-        * refcount is 1, then the page is free and the refcount is
-        * stable because nobody holds a reference on the page.
-        */
-       if (count == 1)
-               free_devmap_managed_page(page);
-       else if (!count)
-               __put_page(page);
-
-       return true;
-}
-#else
-static bool __unpin_devmap_managed_user_page(struct page *page)
-{
-       return false;
-}
-#endif /* CONFIG_DEV_PAGEMAP_OPS */
-
 /**
  * unpin_user_page() - release a dma-pinned page
  * @page:            pointer to page to be released
@@ -223,28 +238,7 @@ static bool __unpin_devmap_managed_user_page(struct page *page)
  */
 void unpin_user_page(struct page *page)
 {
-       int refs = 1;
-
-       page = compound_head(page);
-
-       /*
-        * For devmap managed pages we need to catch refcount transition from
-        * GUP_PIN_COUNTING_BIAS to 1, when refcount reach one it means the
-        * page is free and we need to inform the device driver through
-        * callback. See include/linux/memremap.h and HMM for details.
-        */
-       if (__unpin_devmap_managed_user_page(page))
-               return;
-
-       if (hpage_pincount_available(page))
-               hpage_pincount_sub(page, 1);
-       else
-               refs = GUP_PIN_COUNTING_BIAS;
-
-       if (page_ref_sub_and_test(page, refs))
-               __put_page(page);
-
-       mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED, 1);
+       put_compound_head(compound_head(page), 1, FOLL_PIN);
 }
 EXPORT_SYMBOL(unpin_user_page);
 
@@ -1595,54 +1589,60 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm,
                                        struct vm_area_struct **vmas,
                                        unsigned int gup_flags)
 {
-       unsigned long i;
-       unsigned long step;
-       bool drain_allow = true;
-       bool migrate_allow = true;
+       unsigned long i, isolation_error_count;
+       bool drain_allow;
        LIST_HEAD(cma_page_list);
        long ret = nr_pages;
+       struct page *prev_head, *head;
        struct migration_target_control mtc = {
                .nid = NUMA_NO_NODE,
                .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_NOWARN,
        };
 
 check_again:
-       for (i = 0; i < nr_pages;) {
-
-               struct page *head = compound_head(pages[i]);
-
-               /*
-                * gup may start from a tail page. Advance step by the left
-                * part.
-                */
-               step = compound_nr(head) - (pages[i] - head);
+       prev_head = NULL;
+       isolation_error_count = 0;
+       drain_allow = true;
+       for (i = 0; i < nr_pages; i++) {
+               head = compound_head(pages[i]);
+               if (head == prev_head)
+                       continue;
+               prev_head = head;
                /*
                 * If we get a page from the CMA zone, since we are going to
                 * be pinning these entries, we might as well move them out
                 * of the CMA zone if possible.
                 */
                if (is_migrate_cma_page(head)) {
-                       if (PageHuge(head))
-                               isolate_huge_page(head, &cma_page_list);
-                       else {
+                       if (PageHuge(head)) {
+                               if (!isolate_huge_page(head, &cma_page_list))
+                                       isolation_error_count++;
+                       } else {
                                if (!PageLRU(head) && drain_allow) {
                                        lru_add_drain_all();
                                        drain_allow = false;
                                }
 
-                               if (!isolate_lru_page(head)) {
-                                       list_add_tail(&head->lru, &cma_page_list);
-                                       mod_node_page_state(page_pgdat(head),
-                                                           NR_ISOLATED_ANON +
-                                                           page_is_file_lru(head),
-                                                           thp_nr_pages(head));
+                               if (isolate_lru_page(head)) {
+                                       isolation_error_count++;
+                                       continue;
                                }
+                               list_add_tail(&head->lru, &cma_page_list);
+                               mod_node_page_state(page_pgdat(head),
+                                                   NR_ISOLATED_ANON +
+                                                   page_is_file_lru(head),
+                                                   thp_nr_pages(head));
                        }
                }
-
-               i += step;
        }
 
+       /*
+        * If list is empty, and no isolation errors, means that all pages are
+        * in the correct zone.
+        */
+       if (list_empty(&cma_page_list) && !isolation_error_count)
+               return ret;
+
        if (!list_empty(&cma_page_list)) {
                /*
                 * drop the above get_user_pages reference.
@@ -1653,34 +1653,28 @@ check_again:
                        for (i = 0; i < nr_pages; i++)
                                put_page(pages[i]);
 
-               if (migrate_pages(&cma_page_list, alloc_migration_target, NULL,
-                       (unsigned long)&mtc, MIGRATE_SYNC, MR_CONTIG_RANGE)) {
-                       /*
-                        * some of the pages failed migration. Do get_user_pages
-                        * without migration.
-                        */
-                       migrate_allow = false;
-
+               ret = migrate_pages(&cma_page_list, alloc_migration_target,
+                                   NULL, (unsigned long)&mtc, MIGRATE_SYNC,
+                                   MR_CONTIG_RANGE);
+               if (ret) {
                        if (!list_empty(&cma_page_list))
                                putback_movable_pages(&cma_page_list);
+                       return ret > 0 ? -ENOMEM : ret;
                }
-               /*
-                * We did migrate all the pages, Try to get the page references
-                * again migrating any new CMA pages which we failed to isolate
-                * earlier.
-                */
-               ret = __get_user_pages_locked(mm, start, nr_pages,
-                                                  pages, vmas, NULL,
-                                                  gup_flags);
-
-               if ((ret > 0) && migrate_allow) {
-                       nr_pages = ret;
-                       drain_allow = true;
-                       goto check_again;
-               }
+
+               /* We unpinned pages before migration, pin them again */
+               ret = __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
+                                             NULL, gup_flags);
+               if (ret <= 0)
+                       return ret;
+               nr_pages = ret;
        }
 
-       return ret;
+       /*
+        * check again because pages were unpinned, and we also might have
+        * had isolation errors and need more pages to migrate.
+        */
+       goto check_again;
 }
 #else
 static long check_and_migrate_cma_pages(struct mm_struct *mm,
@@ -2062,29 +2056,6 @@ EXPORT_SYMBOL(get_user_pages_unlocked);
  * This code is based heavily on the PowerPC implementation by Nick Piggin.
  */
 #ifdef CONFIG_HAVE_FAST_GUP
-
-static void put_compound_head(struct page *page, int refs, unsigned int flags)
-{
-       if (flags & FOLL_PIN) {
-               mod_node_page_state(page_pgdat(page), NR_FOLL_PIN_RELEASED,
-                                   refs);
-
-               if (hpage_pincount_available(page))
-                       hpage_pincount_sub(page, refs);
-               else
-                       refs *= GUP_PIN_COUNTING_BIAS;
-       }
-
-       VM_BUG_ON_PAGE(page_ref_count(page) < refs, page);
-       /*
-        * Calling put_page() for each ref is unnecessarily slow. Only the last
-        * ref needs a put_page().
-        */
-       if (refs > 1)
-               page_ref_sub(page, refs - 1);
-       put_page(page);
-}
-
 #ifdef CONFIG_GUP_GET_PTE_LOW_HIGH
 
 /*
@@ -2677,13 +2648,61 @@ static int __gup_longterm_unlocked(unsigned long start, int nr_pages,
        return ret;
 }
 
-static int internal_get_user_pages_fast(unsigned long start, int nr_pages,
+static unsigned long lockless_pages_from_mm(unsigned long start,
+                                           unsigned long end,
+                                           unsigned int gup_flags,
+                                           struct page **pages)
+{
+       unsigned long flags;
+       int nr_pinned = 0;
+       unsigned seq;
+
+       if (!IS_ENABLED(CONFIG_HAVE_FAST_GUP) ||
+           !gup_fast_permitted(start, end))
+               return 0;
+
+       if (gup_flags & FOLL_PIN) {
+               seq = raw_read_seqcount(&current->mm->write_protect_seq);
+               if (seq & 1)
+                       return 0;
+       }
+
+       /*
+        * Disable interrupts. The nested form is used, in order to allow full,
+        * general purpose use of this routine.
+        *
+        * With interrupts disabled, we block page table pages from being freed
+        * from under us. See struct mmu_table_batch comments in
+        * include/asm-generic/tlb.h for more details.
+        *
+        * We do not adopt an rcu_read_lock() here as we also want to block IPIs
+        * that come from THPs splitting.
+        */
+       local_irq_save(flags);
+       gup_pgd_range(start, end, gup_flags, pages, &nr_pinned);
+       local_irq_restore(flags);
+
+       /*
+        * When pinning pages for DMA there could be a concurrent write protect
+        * from fork() via copy_page_range(), in this case always fail fast GUP.
+        */
+       if (gup_flags & FOLL_PIN) {
+               if (read_seqcount_retry(&current->mm->write_protect_seq, seq)) {
+                       unpin_user_pages(pages, nr_pinned);
+                       return 0;
+               }
+       }
+       return nr_pinned;
+}
+
+static int internal_get_user_pages_fast(unsigned long start,
+                                       unsigned long nr_pages,
                                        unsigned int gup_flags,
                                        struct page **pages)
 {
-       unsigned long addr, len, end;
-       unsigned long flags;
-       int nr_pinned = 0, ret = 0;
+       unsigned long len, end;
+       unsigned long nr_pinned;
+       int ret;
 
        if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM |
                                       FOLL_FORCE | FOLL_PIN | FOLL_GET |
@@ -2697,54 +2716,33 @@ static int internal_get_user_pages_fast(unsigned long start, int nr_pages,
                might_lock_read(&current->mm->mmap_lock);
 
        start = untagged_addr(start) & PAGE_MASK;
-       addr = start;
-       len = (unsigned long) nr_pages << PAGE_SHIFT;
-       end = start + len;
-
-       if (end <= start)
+       len = nr_pages << PAGE_SHIFT;
+       if (check_add_overflow(start, len, &end))
                return 0;
        if (unlikely(!access_ok((void __user *)start, len)))
                return -EFAULT;
 
-       /*
-        * Disable interrupts. The nested form is used, in order to allow
-        * full, general purpose use of this routine.
-        *
-        * With interrupts disabled, we block page table pages from being
-        * freed from under us. See struct mmu_table_batch comments in
-        * include/asm-generic/tlb.h for more details.
-        *
-        * We do not adopt an rcu_read_lock(.) here as we also want to
-        * block IPIs that come from THPs splitting.
-        */
-       if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && gup_fast_permitted(start, end)) {
-               unsigned long fast_flags = gup_flags;
-
-               local_irq_save(flags);
-               gup_pgd_range(addr, end, fast_flags, pages, &nr_pinned);
-               local_irq_restore(flags);
-               ret = nr_pinned;
-       }
-
-       if (nr_pinned < nr_pages && !(gup_flags & FOLL_FAST_ONLY)) {
-               /* Try to get the remaining pages with get_user_pages */
-               start += nr_pinned << PAGE_SHIFT;
-               pages += nr_pinned;
-
-               ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned,
-                                             gup_flags, pages);
+       nr_pinned = lockless_pages_from_mm(start, end, gup_flags, pages);
+       if (nr_pinned == nr_pages || gup_flags & FOLL_FAST_ONLY)
+               return nr_pinned;
 
-               /* Have to be a bit careful with return values */
-               if (nr_pinned > 0) {
-                       if (ret < 0)
-                               ret = nr_pinned;
-                       else
-                               ret += nr_pinned;
-               }
+       /* Slow path: try to get the remaining pages with get_user_pages */
+       start += nr_pinned << PAGE_SHIFT;
+       pages += nr_pinned;
+       ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags,
+                                     pages);
+       if (ret < 0) {
+               /*
+                * The caller has to unpin the pages we already pinned so
+                * returning -errno is not an option
+                */
+               if (nr_pinned)
+                       return nr_pinned;
+               return ret;
        }
-
-       return ret;
+       return ret + nr_pinned;
 }
+
 /**
  * get_user_pages_fast_only() - pin user pages in memory
  * @start:      starting user address