if (gup_flags & FOLL_ANON && !vma_is_anonymous(vma))
                return -EFAULT;
 
+       if ((gup_flags & FOLL_LONGTERM) && vma_is_fsdax(vma))
+               return -EOPNOTSUPP;
+
        if (write) {
                if (!(vm_flags & VM_WRITE)) {
                        if (!(gup_flags & FOLL_FORCE))
                                goto next_page;
                        }
 
-                       if (!vma || check_vma_flags(vma, gup_flags)) {
+                       if (!vma) {
                                ret = -EFAULT;
                                goto out;
                        }
+                       ret = check_vma_flags(vma, gup_flags);
+                       if (ret)
+                               goto out;
+
                        if (is_vm_hugetlb_page(vma)) {
                                i = follow_hugetlb_page(mm, vma, pages, vmas,
                                                &start, &nr_pages, i,
 }
 #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)
-{
-       long i;
-       struct vm_area_struct *vma_prev = NULL;
-
-       for (i = 0; i < nr_pages; i++) {
-               struct vm_area_struct *vma = vmas[i];
-
-               if (vma == vma_prev)
-                       continue;
-
-               vma_prev = vma;
-
-               if (vma_is_fsdax(vma))
-                       return true;
-       }
-       return false;
-}
-
 #ifdef CONFIG_CMA
 static long check_and_migrate_cma_pages(struct mm_struct *mm,
                                        unsigned long start,
                                  struct vm_area_struct **vmas,
                                  unsigned int gup_flags)
 {
-       struct vm_area_struct **vmas_tmp = vmas;
        unsigned long flags = 0;
-       long rc, i;
+       long rc;
 
-       if (gup_flags & FOLL_LONGTERM) {
-               if (!pages)
-                       return -EINVAL;
-
-               if (!vmas_tmp) {
-                       vmas_tmp = kcalloc(nr_pages,
-                                          sizeof(struct vm_area_struct *),
-                                          GFP_KERNEL);
-                       if (!vmas_tmp)
-                               return -ENOMEM;
-               }
+       if (gup_flags & FOLL_LONGTERM)
                flags = memalloc_nocma_save();
-       }
 
-       rc = __get_user_pages_locked(mm, start, nr_pages, pages,
-                                    vmas_tmp, NULL, gup_flags);
+       rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL,
+                                    gup_flags);
 
        if (gup_flags & FOLL_LONGTERM) {
-               if (rc < 0)
-                       goto out;
-
-               if (check_dax_vmas(vmas_tmp, rc)) {
-                       if (gup_flags & FOLL_PIN)
-                               unpin_user_pages(pages, rc);
-                       else
-                               for (i = 0; i < rc; i++)
-                                       put_page(pages[i]);
-                       rc = -EOPNOTSUPP;
-                       goto out;
-               }
-
-               rc = check_and_migrate_cma_pages(mm, start, rc, pages,
-                                                vmas_tmp, gup_flags);
-out:
+               if (rc > 0)
+                       rc = check_and_migrate_cma_pages(mm, start, rc, pages,
+                                                        vmas, gup_flags);
                memalloc_nocma_restore(flags);
        }
-
-       if (vmas_tmp != vmas)
-               kfree(vmas_tmp);
        return rc;
 }
-#else /* !CONFIG_FS_DAX && !CONFIG_CMA */
-static __always_inline long __gup_longterm_locked(struct mm_struct *mm,
-                                                 unsigned long start,
-                                                 unsigned long nr_pages,
-                                                 struct page **pages,
-                                                 struct vm_area_struct **vmas,
-                                                 unsigned int flags)
-{
-       return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
-                                      NULL, flags);
-}
-#endif /* CONFIG_FS_DAX || CONFIG_CMA */
 
 static bool is_valid_gup_flags(unsigned int gup_flags)
 {