Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path

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>

authored by

Chenghao Duan and committed by
Andrew Morton
502d3c2a ed2a29dc

+6 -2
+6 -2
mm/memfd_luo.c
··· 410 410 struct inode *inode = file_inode(file); 411 411 struct address_space *mapping = inode->i_mapping; 412 412 struct folio *folio; 413 - long npages; 413 + long npages, nr_added_pages = 0; 414 414 int err = -EIO; 415 415 long i; 416 416 ··· 465 465 goto unlock_folio; 466 466 } 467 467 468 - shmem_recalc_inode(inode, npages, 0); 468 + nr_added_pages += npages; 469 469 folio_add_lru(folio); 470 470 folio_unlock(folio); 471 471 folio_put(folio); 472 472 } 473 + 474 + shmem_recalc_inode(inode, nr_added_pages, 0); 473 475 474 476 return 0; 475 477 ··· 490 488 if (folio) 491 489 folio_put(folio); 492 490 } 491 + 492 + shmem_recalc_inode(inode, nr_added_pages, 0); 493 493 494 494 return err; 495 495 }