mm/gup: take mmap_lock in get_dump_page()
[platform/kernel/linux-starfive.git] / mm / gup.c
index e869c63..102877e 100644 (file)
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -329,6 +329,13 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
        unsigned long index;
 
        /*
+        * If this WARN_ON() fires, then the system *might* be leaking pages (by
+        * leaving them pinned), but probably not. More likely, gup/pup returned
+        * a hard -ERRNO error to the caller, who erroneously passed it here.
+        */
+       if (WARN_ON(IS_ERR_VALUE(npages)))
+               return;
+       /*
         * TODO: this can be optimized for huge pages: if a series of pages is
         * physically contiguous and part of the same compound page, then a
         * single operation to the head page should suffice.
@@ -1483,35 +1490,6 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
                mmap_read_unlock(mm);
        return ret;     /* 0 or negative error code */
 }
-
-/**
- * get_dump_page() - pin user page in memory while writing it to core dump
- * @addr: user address
- *
- * Returns struct page pointer of user page pinned for dump,
- * to be freed afterwards by put_page().
- *
- * Returns NULL on any kind of failure - a hole must then be inserted into
- * the corefile, to preserve alignment with its headers; and also returns
- * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
- * allowing a hole to be left in the corefile to save diskspace.
- *
- * Called without mmap_lock, but after all other threads have been killed.
- */
-#ifdef CONFIG_ELF_CORE
-struct page *get_dump_page(unsigned long addr)
-{
-       struct vm_area_struct *vma;
-       struct page *page;
-
-       if (__get_user_pages(current->mm, addr, 1,
-                            FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
-                            NULL) < 1)
-               return NULL;
-       flush_cache_page(vma, addr, page_to_pfn(page));
-       return page;
-}
-#endif /* CONFIG_ELF_CORE */
 #else /* CONFIG_MMU */
 static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
                unsigned long nr_pages, struct page **pages,
@@ -1557,6 +1535,38 @@ finish_or_fault:
 }
 #endif /* !CONFIG_MMU */
 
+/**
+ * get_dump_page() - pin user page in memory while writing it to core dump
+ * @addr: user address
+ *
+ * Returns struct page pointer of user page pinned for dump,
+ * to be freed afterwards by put_page().
+ *
+ * Returns NULL on any kind of failure - a hole must then be inserted into
+ * the corefile, to preserve alignment with its headers; and also returns
+ * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
+ * allowing a hole to be left in the corefile to save diskspace.
+ *
+ * Called without mmap_lock (takes and releases the mmap_lock by itself).
+ */
+#ifdef CONFIG_ELF_CORE
+struct page *get_dump_page(unsigned long addr)
+{
+       struct mm_struct *mm = current->mm;
+       struct page *page;
+       int locked = 1;
+       int ret;
+
+       if (mmap_read_lock_killable(mm))
+               return NULL;
+       ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked,
+                                     FOLL_FORCE | FOLL_DUMP | FOLL_GET);
+       if (locked)
+               mmap_read_unlock(mm);
+       return (ret == 1) ? page : NULL;
+}
+#endif /* CONFIG_ELF_CORE */
+
 #if defined(CONFIG_FS_DAX) || defined (CONFIG_CMA)
 static bool check_dax_vmas(struct vm_area_struct **vmas, long nr_pages)
 {
@@ -1747,6 +1757,25 @@ static __always_inline long __gup_longterm_locked(struct mm_struct *mm,
 }
 #endif /* CONFIG_FS_DAX || CONFIG_CMA */
 
+static bool is_valid_gup_flags(unsigned int gup_flags)
+{
+       /*
+        * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
+        * never directly by the caller, so enforce that with an assertion:
+        */
+       if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
+               return false;
+       /*
+        * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying
+        * that is, FOLL_LONGTERM is a specific case, more restrictive case of
+        * FOLL_PIN.
+        */
+       if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM))
+               return false;
+
+       return true;
+}
+
 #ifdef CONFIG_MMU
 static long __get_user_pages_remote(struct mm_struct *mm,
                                    unsigned long start, unsigned long nr_pages,
@@ -1842,11 +1871,7 @@ long get_user_pages_remote(struct mm_struct *mm,
                unsigned int gup_flags, struct page **pages,
                struct vm_area_struct **vmas, int *locked)
 {
-       /*
-        * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
-        * never directly by the caller, so enforce that with an assertion:
-        */
-       if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
+       if (!is_valid_gup_flags(gup_flags))
                return -EINVAL;
 
        return __get_user_pages_remote(mm, start, nr_pages, gup_flags,
@@ -1892,11 +1917,7 @@ long get_user_pages(unsigned long start, unsigned long nr_pages,
                unsigned int gup_flags, struct page **pages,
                struct vm_area_struct **vmas)
 {
-       /*
-        * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
-        * never directly by the caller, so enforce that with an assertion:
-        */
-       if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
+       if (!is_valid_gup_flags(gup_flags))
                return -EINVAL;
 
        return __gup_longterm_locked(current->mm, start, nr_pages,
@@ -2786,11 +2807,7 @@ EXPORT_SYMBOL_GPL(get_user_pages_fast_only);
 int get_user_pages_fast(unsigned long start, int nr_pages,
                        unsigned int gup_flags, struct page **pages)
 {
-       /*
-        * FOLL_PIN must only be set internally by the pin_user_pages*() APIs,
-        * never directly by the caller, so enforce that:
-        */
-       if (WARN_ON_ONCE(gup_flags & FOLL_PIN))
+       if (!is_valid_gup_flags(gup_flags))
                return -EINVAL;
 
        /*