OSDN Git Service

Merge tag '6.6-rc-ksmbd' of git://git.samba.org/ksmbd
[tomoyo/tomoyo-test1.git] / mm / gup.c
index 6e2f9e9..2f8a2d8 100644 (file)
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -811,7 +811,6 @@ static struct page *follow_page_mask(struct vm_area_struct *vma,
                              struct follow_page_context *ctx)
 {
        pgd_t *pgd;
-       struct page *page;
        struct mm_struct *mm = vma->vm_mm;
 
        ctx->page_mask = 0;
@@ -820,16 +819,10 @@ static struct page *follow_page_mask(struct vm_area_struct *vma,
         * Call hugetlb_follow_page_mask for hugetlb vmas as it will use
         * special hugetlb page table walking code.  This eliminates the
         * need to check for hugetlb entries in the general walking code.
-        *
-        * hugetlb_follow_page_mask is only for follow_page() handling here.
-        * Ordinary GUP uses follow_hugetlb_page for hugetlb processing.
         */
-       if (is_vm_hugetlb_page(vma)) {
-               page = hugetlb_follow_page_mask(vma, address, flags);
-               if (!page)
-                       page = no_page_table(vma, flags);
-               return page;
-       }
+       if (is_vm_hugetlb_page(vma))
+               return hugetlb_follow_page_mask(vma, address, flags,
+                                               &ctx->page_mask);
 
        pgd = pgd_offset(mm, address);
 
@@ -1058,7 +1051,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
                    !writable_file_mapping_allowed(vma, gup_flags))
                        return -EFAULT;
 
-               if (!(vm_flags & VM_WRITE)) {
+               if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) {
                        if (!(gup_flags & FOLL_FORCE))
                                return -EFAULT;
                        /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */
@@ -1215,7 +1208,7 @@ static long __get_user_pages(struct mm_struct *mm,
                        if (!vma && in_gate_area(mm, start)) {
                                ret = get_gate_page(mm, start & PAGE_MASK,
                                                gup_flags, &vma,
-                                               pages ? &pages[i] : NULL);
+                                               pages ? &page : NULL);
                                if (ret)
                                        goto out;
                                ctx.page_mask = 0;
@@ -1229,22 +1222,6 @@ static long __get_user_pages(struct mm_struct *mm,
                        ret = check_vma_flags(vma, gup_flags);
                        if (ret)
                                goto out;
-
-                       if (is_vm_hugetlb_page(vma)) {
-                               i = follow_hugetlb_page(mm, vma, pages,
-                                                       &start, &nr_pages, i,
-                                                       gup_flags, locked);
-                               if (!*locked) {
-                                       /*
-                                        * We've got a VM_FAULT_RETRY
-                                        * and we've lost mmap_lock.
-                                        * We must stop here.
-                                        */
-                                       BUG_ON(gup_flags & FOLL_NOWAIT);
-                                       goto out;
-                               }
-                               continue;
-                       }
                }
 retry:
                /*
@@ -1285,22 +1262,58 @@ retry:
                                ret = PTR_ERR(page);
                                goto out;
                        }
-
-                       goto next_page;
                } else if (IS_ERR(page)) {
                        ret = PTR_ERR(page);
                        goto out;
                }
-               if (pages) {
-                       pages[i] = page;
-                       flush_anon_page(vma, page, start);
-                       flush_dcache_page(page);
-                       ctx.page_mask = 0;
-               }
 next_page:
                page_increm = 1 + (~(start >> PAGE_SHIFT) & ctx.page_mask);
                if (page_increm > nr_pages)
                        page_increm = nr_pages;
+
+               if (pages) {
+                       struct page *subpage;
+                       unsigned int j;
+
+                       /*
+                        * This must be a large folio (and doesn't need to
+                        * be the whole folio; it can be part of it), do
+                        * the refcount work for all the subpages too.
+                        *
+                        * NOTE: here the page may not be the head page
+                        * e.g. when start addr is not thp-size aligned.
+                        * try_grab_folio() should have taken care of tail
+                        * pages.
+                        */
+                       if (page_increm > 1) {
+                               struct folio *folio;
+
+                               /*
+                                * Since we already hold refcount on the
+                                * large folio, this should never fail.
+                                */
+                               folio = try_grab_folio(page, page_increm - 1,
+                                                      foll_flags);
+                               if (WARN_ON_ONCE(!folio)) {
+                                       /*
+                                        * Release the 1st page ref if the
+                                        * folio is problematic, fail hard.
+                                        */
+                                       gup_put_folio(page_folio(page), 1,
+                                                     foll_flags);
+                                       ret = -EFAULT;
+                                       goto out;
+                               }
+                       }
+
+                       for (j = 0; j < page_increm; j++) {
+                               subpage = nth_page(page, j);
+                               pages[i + j] = subpage;
+                               flush_anon_page(vma, subpage, start + j * PAGE_SIZE);
+                               flush_dcache_page(subpage);
+                       }
+               }
+
                i += page_increm;
                start += page_increm * PAGE_SIZE;
                nr_pages -= page_increm;
@@ -2231,13 +2244,6 @@ static bool is_valid_gup_args(struct page **pages, int *locked,
                gup_flags |= FOLL_UNLOCKABLE;
        }
 
-       /*
-        * For now, always trigger NUMA hinting faults. Some GUP users like
-        * KVM require the hint to be as the calling context of GUP is
-        * functionally similar to a memory reference from task context.
-        */
-       gup_flags |= FOLL_HONOR_NUMA_FAULT;
-
        /* FOLL_GET and FOLL_PIN are mutually exclusive. */
        if (WARN_ON_ONCE((gup_flags & (FOLL_PIN | FOLL_GET)) ==
                         (FOLL_PIN | FOLL_GET)))
@@ -2594,7 +2600,7 @@ static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
                if (!folio)
                        goto pte_unmap;
 
-               if (unlikely(page_is_secretmem(page))) {
+               if (unlikely(folio_is_secretmem(folio))) {
                        gup_put_folio(folio, 1, flags);
                        goto pte_unmap;
                }