[PATCH] mm: memfd_luo: account restored large folios correctly

DaeMyung Kang
2026-04-13T16:51:02Z

memfd_luo_retrieve_folios() always passes 1 to
shmem_inode_acct_blocks() and shmem_recalc_inode(), even when the
restored folio spans multiple pages.

That mismatches the normal shmem path: shmem_add_to_page_cache()
accounts the folio in mapping->nrpages using folio_nr_pages(folio),
and shmem_alloc_and_add_folio() passes the same page count to
shmem_inode_acct_blocks() and shmem_recalc_inode().

As a result, restored large folios are under-accounted in shmem inode
and block accounting.

Use folio_nr_pages(folio) here as well.

Fixes: b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
---
mm/memfd_luo.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index b8edb9f981d7..6dd0284b90c6 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -402,6 +402,7 @@ static int memfd_luo_retrieve_folios(struct file *file,
const struct memfd_luo_folio_ser *pfolio = &folios_ser[i];
phys_addr_t phys;
u64 index;
+ long pages;
int flags;

if (!pfolio->pfn)
@@ -441,14 +442,16 @@ static int memfd_luo_retrieve_folios(struct file *file,
if (flags & MEMFD_LUO_FOLIO_DIRTY)
folio_mark_dirty(folio);

- err = shmem_inode_acct_blocks(inode, 1);
+ pages = folio_nr_pages(folio);
+
+ err = shmem_inode_acct_blocks(inode, pages);
if (err) {
pr_err("shmem: failed to account folio index %ld: %d\n",
i, err);
goto unlock_folio;
}

- shmem_recalc_inode(inode, 1, 0);
+ shmem_recalc_inode(inode, pages, 0);
folio_add_lru(folio);
folio_unlock(folio);
folio_put(folio);
--
2.43.0