[PATCH 1/2] mm: memfd_luo: always make all folios uptodate

From: Pratyush Yadav

Date: Mon Feb 23 2026 - 12:44:25 EST


From: "Pratyush Yadav (Google)" <pratyush@xxxxxxxxxx>

When a folio is added to a shmem file via fallocate, it is not zeroed on
allocation. This is done as a performance optimization since it is
possible the folio will never end up being used at all. When the folio
is used, shmem checks for the uptodate flag, and if absent, zeroes the
folio (and sets the flag) before returning to user.

With LUO, the flags of each folio are saved at preserve time. It is
possible to have a memfd with some folios fallocated but not uptodate.
For those, the uptodate flag doesn't get saved. The folios might later
end up being used and become uptodate. They would get passed to the next
kernel via KHO correctly since they did get preserved. But they won't
have the MEMFD_LUO_FOLIO_UPTODATE flag.

This means that when the memfd is retrieved, the folios will be added to
the shmem file without the uptodate flag. They will be zeroed before
first use, losing the data in those folios.

Since we take a big performance hit in allocating, zeroing, and pinning
all folios at prepare time anyway, take some more and zero all
non-uptodate ones too.

Later when there is a stronger need to make prepare faster, this can be
optimized.

To avoid racing with another uptodate operation, take the folio lock.

Fixes: b3749f174d68 ("mm: memfd_luo: allow preserving memfd")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Pratyush Yadav (Google) <pratyush@xxxxxxxxxx>
---
mm/memfd_luo.c | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index a34fccc23b6a..ccbf1337f650 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -152,10 +152,31 @@ static int memfd_luo_preserve_folios(struct file *file,
if (err)
goto err_unpreserve;

+ folio_lock(folio);
+
if (folio_test_dirty(folio))
flags |= MEMFD_LUO_FOLIO_DIRTY;
- if (folio_test_uptodate(folio))
- flags |= MEMFD_LUO_FOLIO_UPTODATE;
+
+ /*
+ * If the folio is not uptodate, it was fallocated but never
+ * used. Saving this flag at prepare() doesn't work since it
+ * might change later when someone uses the folio.
+ *
+ * Since we have taken the performance penalty of allocating,
+ * zeroing, and pinning all the folios in the holes, take a bit
+ * more and zero all non-uptodate folios too.
+ *
+ * NOTE: For someone looking to improve preserve performance,
+ * this is a good place to look.
+ */
+ if (!folio_test_uptodate(folio)) {
+ folio_zero_range(folio, 0, folio_size(folio));
+ flush_dcache_folio(folio);
+ folio_mark_uptodate(folio);
+ }
+ flags |= MEMFD_LUO_FOLIO_UPTODATE;
+
+ folio_unlock(folio);

pfolio->pfn = folio_pfn(folio);
pfolio->flags = flags;
--
2.53.0.371.g1d285c8824-goog