]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path
authorChenghao Duan <duanchenghao@kylinos.cn>
Thu, 26 Mar 2026 08:47:22 +0000 (16:47 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 18 Apr 2026 07:10:52 +0000 (00:10 -0700)
Move shmem_recalc_inode() out of the loop in memfd_luo_retrieve_folios()
to improve performance when restoring large memfds.

Currently, shmem_recalc_inode() is called for each folio during restore,
which is O(n) expensive operations.  This patch collects the number of
successfully added folios and calls shmem_recalc_inode() once after the
loop completes, reducing complexity to O(1).

Additionally, fix the error path to also call shmem_recalc_inode() for the
folios that were successfully added before the error occurred.

Link: https://lore.kernel.org/20260326084727.118437-3-duanchenghao@kylinos.cn
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Pratyush Yadav <pratyush@kernel.org>
Cc: Haoran Jiang <jianghaoran@kylinos.cn>
Cc: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memfd_luo.c

index ec4c3e1e2891cc5b46b7b5830c3319ff0bbd781a..865b044bee62bf1b3e6b7871854dfb70e5821973 100644 (file)
@@ -410,7 +410,7 @@ static int memfd_luo_retrieve_folios(struct file *file,
        struct inode *inode = file_inode(file);
        struct address_space *mapping = inode->i_mapping;
        struct folio *folio;
-       long npages;
+       long npages, nr_added_pages = 0;
        int err = -EIO;
        long i;
 
@@ -465,12 +465,14 @@ static int memfd_luo_retrieve_folios(struct file *file,
                        goto unlock_folio;
                }
 
-               shmem_recalc_inode(inode, npages, 0);
+               nr_added_pages += npages;
                folio_add_lru(folio);
                folio_unlock(folio);
                folio_put(folio);
        }
 
+       shmem_recalc_inode(inode, nr_added_pages, 0);
+
        return 0;
 
 unlock_folio:
@@ -489,6 +491,8 @@ put_folios:
                        folio_put(folio);
        }
 
+       shmem_recalc_inode(inode, nr_added_pages, 0);
+
        return err;
 }