]> git.ipfire.org Git - thirdparty/kernel/stable.git/blobdiff - mm/shmem.c
mm: khugepaged: introduce khugepaged_enter_vma() helper
[thirdparty/kernel/stable.git] / mm / shmem.c
index d11dc37d332f9bb3d4fb0534614a9522f438c59b..89f6f4fec3f9a0a472e1db3269ee2cd6d0c53181 100644 (file)
@@ -135,8 +135,8 @@ static unsigned long shmem_default_max_inodes(void)
 }
 #endif
 
-static int shmem_swapin_page(struct inode *inode, pgoff_t index,
-                            struct page **pagep, enum sgp_type sgp,
+static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
+                            struct folio **foliop, enum sgp_type sgp,
                             gfp_t gfp, struct vm_area_struct *vma,
                             vm_fault_t *fault_type);
 static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
@@ -1159,69 +1159,64 @@ static void shmem_evict_inode(struct inode *inode)
 }
 
 static int shmem_find_swap_entries(struct address_space *mapping,
-                                  pgoff_t start, unsigned int nr_entries,
-                                  struct page **entries, pgoff_t *indices,
-                                  unsigned int type)
+                                  pgoff_t start, struct folio_batch *fbatch,
+                                  pgoff_t *indices, unsigned int type)
 {
        XA_STATE(xas, &mapping->i_pages, start);
-       struct page *page;
+       struct folio *folio;
        swp_entry_t entry;
        unsigned int ret = 0;
 
-       if (!nr_entries)
-               return 0;
-
        rcu_read_lock();
-       xas_for_each(&xas, page, ULONG_MAX) {
-               if (xas_retry(&xas, page))
+       xas_for_each(&xas, folio, ULONG_MAX) {
+               if (xas_retry(&xas, folio))
                        continue;
 
-               if (!xa_is_value(page))
+               if (!xa_is_value(folio))
                        continue;
 
-               entry = radix_to_swp_entry(page);
+               entry = radix_to_swp_entry(folio);
                if (swp_type(entry) != type)
                        continue;
 
                indices[ret] = xas.xa_index;
-               entries[ret] = page;
+               if (!folio_batch_add(fbatch, folio))
+                       break;
 
                if (need_resched()) {
                        xas_pause(&xas);
                        cond_resched_rcu();
                }
-               if (++ret == nr_entries)
-                       break;
        }
        rcu_read_unlock();
 
-       return ret;
+       return xas.xa_index;
 }
 
 /*
  * Move the swapped pages for an inode to page cache. Returns the count
  * of pages swapped in, or the error in case of failure.
  */
-static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
-                                   pgoff_t *indices)
+static int shmem_unuse_swap_entries(struct inode *inode,
+               struct folio_batch *fbatch, pgoff_t *indices)
 {
        int i = 0;
        int ret = 0;
        int error = 0;
        struct address_space *mapping = inode->i_mapping;
 
-       for (i = 0; i < pvec.nr; i++) {
-               struct page *page = pvec.pages[i];
+       for (i = 0; i < folio_batch_count(fbatch); i++) {
+               struct folio *folio = fbatch->folios[i];
 
-               if (!xa_is_value(page))
+               if (!xa_is_value(folio))
                        continue;
-               error = shmem_swapin_page(inode, indices[i],
-                                         &page, SGP_CACHE,
+               error = shmem_swapin_folio(inode, indices[i],
+                                         &folio, SGP_CACHE,
                                          mapping_gfp_mask(mapping),
                                          NULL, NULL);
                if (error == 0) {
-                       unlock_page(page);
-                       put_page(page);
+                       folio_unlock(folio);
+                       folio_put(folio);
                        ret++;
                }
                if (error == -ENOMEM)
@@ -1238,26 +1233,23 @@ static int shmem_unuse_inode(struct inode *inode, unsigned int type)
 {
        struct address_space *mapping = inode->i_mapping;
        pgoff_t start = 0;
-       struct pagevec pvec;
+       struct folio_batch fbatch;
        pgoff_t indices[PAGEVEC_SIZE];
        int ret = 0;
 
-       pagevec_init(&pvec);
        do {
-               unsigned int nr_entries = PAGEVEC_SIZE;
-
-               pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
-                                                 pvec.pages, indices, type);
-               if (pvec.nr == 0) {
+               folio_batch_init(&fbatch);
+               shmem_find_swap_entries(mapping, start, &fbatch, indices, type);
+               if (folio_batch_count(&fbatch) == 0) {
                        ret = 0;
                        break;
                }
 
-               ret = shmem_unuse_swap_entries(inode, pvec, indices);
+               ret = shmem_unuse_swap_entries(inode, &fbatch, indices);
                if (ret < 0)
                        break;
 
-               start = indices[pvec.nr - 1];
+               start = indices[folio_batch_count(&fbatch) - 1];
        } while (true);
 
        return ret;
@@ -1523,7 +1515,7 @@ static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp)
        return result;
 }
 
-static struct page *shmem_alloc_hugepage(gfp_t gfp,
+static struct folio *shmem_alloc_hugefolio(gfp_t gfp,
                struct shmem_inode_info *info, pgoff_t index)
 {
        struct vm_area_struct pvma;
@@ -1541,28 +1533,33 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
        shmem_pseudo_vma_destroy(&pvma);
        if (!folio)
                count_vm_event(THP_FILE_FALLBACK);
-       return &folio->page;
+       return folio;
 }
 
-static struct page *shmem_alloc_page(gfp_t gfp,
+static struct folio *shmem_alloc_folio(gfp_t gfp,
                        struct shmem_inode_info *info, pgoff_t index)
 {
        struct vm_area_struct pvma;
-       struct page *page;
+       struct folio *folio;
 
        shmem_pseudo_vma_init(&pvma, info, index);
-       page = alloc_page_vma(gfp, &pvma, 0);
+       folio = vma_alloc_folio(gfp, 0, &pvma, 0, false);
        shmem_pseudo_vma_destroy(&pvma);
 
-       return page;
+       return folio;
 }
 
-static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
-               struct inode *inode,
+static struct page *shmem_alloc_page(gfp_t gfp,
+                       struct shmem_inode_info *info, pgoff_t index)
+{
+       return &shmem_alloc_folio(gfp, info, index)->page;
+}
+
+static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode,
                pgoff_t index, bool huge)
 {
        struct shmem_inode_info *info = SHMEM_I(inode);
-       struct page *page;
+       struct folio *folio;
        int nr;
        int err = -ENOSPC;
 
@@ -1574,13 +1571,13 @@ static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
                goto failed;
 
        if (huge)
-               page = shmem_alloc_hugepage(gfp, info, index);
+               folio = shmem_alloc_hugefolio(gfp, info, index);
        else
-               page = shmem_alloc_page(gfp, info, index);
-       if (page) {
-               __SetPageLocked(page);
-               __SetPageSwapBacked(page);
-               return page;
+               folio = shmem_alloc_folio(gfp, info, index);
+       if (folio) {
+               __folio_set_locked(folio);
+               __folio_set_swapbacked(folio);
+               return folio;
        }
 
        err = -ENOMEM;
@@ -1682,22 +1679,22 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  * Returns 0 and the page in pagep if success. On failure, returns the
  * error code and NULL in *pagep.
  */
-static int shmem_swapin_page(struct inode *inode, pgoff_t index,
-                            struct page **pagep, enum sgp_type sgp,
+static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
+                            struct folio **foliop, enum sgp_type sgp,
                             gfp_t gfp, struct vm_area_struct *vma,
                             vm_fault_t *fault_type)
 {
        struct address_space *mapping = inode->i_mapping;
        struct shmem_inode_info *info = SHMEM_I(inode);
        struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL;
-       struct page *page = NULL;
-       struct folio *folio;
+       struct page *page;
+       struct folio *folio = NULL;
        swp_entry_t swap;
        int error;
 
-       VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
-       swap = radix_to_swp_entry(*pagep);
-       *pagep = NULL;
+       VM_BUG_ON(!*foliop || !xa_is_value(*foliop));
+       swap = radix_to_swp_entry(*foliop);
+       *foliop = NULL;
 
        /* Look it up and read it in.. */
        page = lookup_swap_cache(swap, NULL, 0);
@@ -1715,27 +1712,28 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index,
                        goto failed;
                }
        }
+       folio = page_folio(page);
 
        /* We have to do this with page locked to prevent races */
-       lock_page(page);
-       if (!PageSwapCache(page) || page_private(page) != swap.val ||
+       folio_lock(folio);
+       if (!folio_test_swapcache(folio) ||
+           folio_swap_entry(folio).val != swap.val ||
            !shmem_confirm_swap(mapping, index, swap)) {
                error = -EEXIST;
                goto unlock;
        }
-       if (!PageUptodate(page)) {
+       if (!folio_test_uptodate(folio)) {
                error = -EIO;
                goto failed;
        }
-       wait_on_page_writeback(page);
+       folio_wait_writeback(folio);
 
        /*
         * Some architectures may have to restore extra metadata to the
-        * physical page after reading from swap.
+        * folio after reading from swap.
         */
-       arch_swap_restore(swap, page);
+       arch_swap_restore(swap, folio);
 
-       folio = page_folio(page);
        if (shmem_should_replace_folio(folio, gfp)) {
                error = shmem_replace_page(&page, gfp, info, index);
                if (error)
@@ -1754,21 +1752,21 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index,
        spin_unlock_irq(&info->lock);
 
        if (sgp == SGP_WRITE)
-               mark_page_accessed(page);
+               folio_mark_accessed(folio);
 
-       delete_from_swap_cache(page);
-       set_page_dirty(page);
+       delete_from_swap_cache(&folio->page);
+       folio_mark_dirty(folio);
        swap_free(swap);
 
-       *pagep = page;
+       *foliop = folio;
        return 0;
 failed:
        if (!shmem_confirm_swap(mapping, index, swap))
                error = -EEXIST;
 unlock:
-       if (page) {
-               unlock_page(page);
-               put_page(page);
+       if (folio) {
+               folio_unlock(folio);
+               folio_put(folio);
        }
 
        return error;
@@ -1794,7 +1792,6 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
        struct shmem_sb_info *sbinfo;
        struct mm_struct *charge_mm;
        struct folio *folio;
-       struct page *page;
        pgoff_t hindex = index;
        gfp_t huge_gfp;
        int error;
@@ -1812,39 +1809,37 @@ repeat:
        sbinfo = SHMEM_SB(inode->i_sb);
        charge_mm = vma ? vma->vm_mm : NULL;
 
-       page = pagecache_get_page(mapping, index,
-                                       FGP_ENTRY | FGP_HEAD | FGP_LOCK, 0);
-
-       if (page && vma && userfaultfd_minor(vma)) {
-               if (!xa_is_value(page)) {
-                       unlock_page(page);
-                       put_page(page);
+       folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0);
+       if (folio && vma && userfaultfd_minor(vma)) {
+               if (!xa_is_value(folio)) {
+                       folio_unlock(folio);
+                       folio_put(folio);
                }
                *fault_type = handle_userfault(vmf, VM_UFFD_MINOR);
                return 0;
        }
 
-       if (xa_is_value(page)) {
-               error = shmem_swapin_page(inode, index, &page,
+       if (xa_is_value(folio)) {
+               error = shmem_swapin_folio(inode, index, &folio,
                                          sgp, gfp, vma, fault_type);
                if (error == -EEXIST)
                        goto repeat;
 
-               *pagep = page;
+               *pagep = &folio->page;
                return error;
        }
 
-       if (page) {
-               hindex = page->index;
+       if (folio) {
+               hindex = folio->index;
                if (sgp == SGP_WRITE)
-                       mark_page_accessed(page);
-               if (PageUptodate(page))
+                       folio_mark_accessed(folio);
+               if (folio_test_uptodate(folio))
                        goto out;
                /* fallocated page */
                if (sgp != SGP_READ)
                        goto clear;
-               unlock_page(page);
-               put_page(page);
+               folio_unlock(folio);
+               folio_put(folio);
        }
 
        /*
@@ -1871,17 +1866,16 @@ repeat:
 
        huge_gfp = vma_thp_gfp_mask(vma);
        huge_gfp = limit_gfp_mask(huge_gfp, gfp);
-       page = shmem_alloc_and_acct_page(huge_gfp, inode, index, true);
-       if (IS_ERR(page)) {
+       folio = shmem_alloc_and_acct_folio(huge_gfp, inode, index, true);
+       if (IS_ERR(folio)) {
 alloc_nohuge:
-               page = shmem_alloc_and_acct_page(gfp, inode,
-                                                index, false);
+               folio = shmem_alloc_and_acct_folio(gfp, inode, index, false);
        }
-       if (IS_ERR(page)) {
+       if (IS_ERR(folio)) {
                int retry = 5;
 
-               error = PTR_ERR(page);
-               page = NULL;
+               error = PTR_ERR(folio);
+               folio = NULL;
                if (error != -ENOSPC)
                        goto unlock;
                /*
@@ -1900,30 +1894,26 @@ alloc_nohuge:
                goto unlock;
        }
 
-       if (PageTransHuge(page))
-               hindex = round_down(index, HPAGE_PMD_NR);
-       else
-               hindex = index;
+       hindex = round_down(index, folio_nr_pages(folio));
 
        if (sgp == SGP_WRITE)
-               __SetPageReferenced(page);
+               __folio_set_referenced(folio);
 
-       folio = page_folio(page);
        error = shmem_add_to_page_cache(folio, mapping, hindex,
                                        NULL, gfp & GFP_RECLAIM_MASK,
                                        charge_mm);
        if (error)
                goto unacct;
-       lru_cache_add(page);
+       folio_add_lru(folio);
 
        spin_lock_irq(&info->lock);
-       info->alloced += compound_nr(page);
-       inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
+       info->alloced += folio_nr_pages(folio);
+       inode->i_blocks += BLOCKS_PER_PAGE << folio_order(folio);
        shmem_recalc_inode(inode);
        spin_unlock_irq(&info->lock);
        alloced = true;
 
-       if (PageTransHuge(page) &&
+       if (folio_test_pmd_mappable(folio) &&
            DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
                        hindex + HPAGE_PMD_NR - 1) {
                /*
@@ -1954,22 +1944,21 @@ clear:
         * but SGP_FALLOC on a page fallocated earlier must initialize
         * it now, lest undo on failure cancel our earlier guarantee.
         */
-       if (sgp != SGP_WRITE && !PageUptodate(page)) {
-               int i;
+       if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) {
+               long i, n = folio_nr_pages(folio);
 
-               for (i = 0; i < compound_nr(page); i++) {
-                       clear_highpage(page + i);
-                       flush_dcache_page(page + i);
-               }
-               SetPageUptodate(page);
+               for (i = 0; i < n; i++)
+                       clear_highpage(folio_page(folio, i));
+               flush_dcache_folio(folio);
+               folio_mark_uptodate(folio);
        }
 
        /* Perhaps the file has been truncated since we checked */
        if (sgp <= SGP_CACHE &&
            ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
                if (alloced) {
-                       ClearPageDirty(page);
-                       delete_from_page_cache(page);
+                       folio_clear_dirty(folio);
+                       filemap_remove_folio(folio);
                        spin_lock_irq(&info->lock);
                        shmem_recalc_inode(inode);
                        spin_unlock_irq(&info->lock);
@@ -1978,24 +1967,24 @@ clear:
                goto unlock;
        }
 out:
-       *pagep = page + index - hindex;
+       *pagep = folio_page(folio, index - hindex);
        return 0;
 
        /*
         * Error recovery.
         */
 unacct:
-       shmem_inode_unacct_blocks(inode, compound_nr(page));
+       shmem_inode_unacct_blocks(inode, folio_nr_pages(folio));
 
-       if (PageTransHuge(page)) {
-               unlock_page(page);
-               put_page(page);
+       if (folio_test_large(folio)) {
+               folio_unlock(folio);
+               folio_put(folio);
                goto alloc_nohuge;
        }
 unlock:
-       if (page) {
-               unlock_page(page);
-               put_page(page);
+       if (folio) {
+               folio_unlock(folio);
+               folio_put(folio);
        }
        if (error == -ENOSPC && !once++) {
                spin_lock_irq(&info->lock);
@@ -2243,11 +2232,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
 
        file_accessed(file);
        vma->vm_ops = &shmem_vm_ops;
-       if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
-                       ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
-                       (vma->vm_end & HPAGE_PMD_MASK)) {
-               khugepaged_enter(vma, vma->vm_flags);
-       }
+       khugepaged_enter_vma(vma, vma->vm_flags);
        return 0;
 }
 
@@ -4148,11 +4133,7 @@ int shmem_zero_setup(struct vm_area_struct *vma)
        vma->vm_file = file;
        vma->vm_ops = &shmem_vm_ops;
 
-       if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
-                       ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
-                       (vma->vm_end & HPAGE_PMD_MASK)) {
-               khugepaged_enter(vma, vma->vm_flags);
-       }
+       khugepaged_enter_vma(vma, vma->vm_flags);
 
        return 0;
 }