Skip to contentSkip to conversation

mm: memfd_luo: account restored large folios correctly

PATCH In review DaeMyung Kang 2 participants 2 messages Apr 13 – Apr 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: memfd_luo: account restored large folios correctly

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")
---
 mm/memfd_luo.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/memfd_luo.c +5 -2
index b8edb9f981d7..6dd0284b90c6 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi DaeMyung,

Quoted 9 lines · DaeMyung Kang

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().

Quoted 7 lines

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")

Thanks for the patch. This bug is already fixed by this patch [0].

[0] https://lore.kernel.org/linux-mm/[email protected]/

[...]

Signature · 2 lines
Regards,
Pratyush Yadav