Skip to content

Commit 7e04bf1

Browse files
prati0100akpm00
authored andcommitted
mm: memfd_luo: always dirty all folios
A dirty folio is one which has been written to. A clean folio is its opposite. Since a clean folio has no user data, it can be freed under memory pressure. memfd preservation with LUO saves the flag at preserve(). This is problematic. The folio might get dirtied later. Saving it at freeze() also doesn't work, since the dirty bit from PTE is normally synced at unmap and there might still be mappings of the file at freeze(). To see why this is a problem, say a folio is clean at preserve, but gets dirtied later. The serialized state of the folio will mark it as clean. After retrieve, the next kernel will see the folio as clean and might try to reclaim it under memory pressure. This will result in losing user data. Mark all folios of the file as dirty, and always set the MEMFD_LUO_FOLIO_DIRTY flag. This comes with the side effect of making all clean folios un-reclaimable. This is a cost that has to be paid for participants of live update. It is not expected to be a common use case to preserve a lot of clean folios anyway. Since the value of pfolio->flags is a constant now, drop the flags variable and set it directly. Link: https://lkml.kernel.org/r/20260223173931.2221759-3-pratyush@kernel.org Fixes: b3749f1 ("mm: memfd_luo: allow preserving memfd") Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Cc: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 50d7b43 commit 7e04bf1

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

mm/memfd_luo.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,33 @@ static int memfd_luo_preserve_folios(struct file *file,
146146
for (i = 0; i < nr_folios; i++) {
147147
struct memfd_luo_folio_ser *pfolio = &folios_ser[i];
148148
struct folio *folio = folios[i];
149-
unsigned int flags = 0;
150149

151150
err = kho_preserve_folio(folio);
152151
if (err)
153152
goto err_unpreserve;
154153

155154
folio_lock(folio);
156155

157-
if (folio_test_dirty(folio))
158-
flags |= MEMFD_LUO_FOLIO_DIRTY;
156+
/*
157+
* A dirty folio is one which has been written to. A clean folio
158+
* is its opposite. Since a clean folio does not carry user
159+
* data, it can be freed by page reclaim under memory pressure.
160+
*
161+
* Saving the dirty flag at prepare() time doesn't work since it
162+
* can change later. Saving it at freeze() also won't work
163+
* because the dirty bit is normally synced at unmap and there
164+
* might still be a mapping of the file at freeze().
165+
*
166+
* To see why this is a problem, say a folio is clean at
167+
* preserve, but gets dirtied later. The pfolio flags will mark
168+
* it as clean. After retrieve, the next kernel might try to
169+
* reclaim this folio under memory pressure, losing user data.
170+
*
171+
* Unconditionally mark it dirty to avoid this problem. This
172+
* comes at the cost of making clean folios un-reclaimable after
173+
* live update.
174+
*/
175+
folio_mark_dirty(folio);
159176

160177
/*
161178
* If the folio is not uptodate, it was fallocated but never
@@ -174,12 +191,11 @@ static int memfd_luo_preserve_folios(struct file *file,
174191
flush_dcache_folio(folio);
175192
folio_mark_uptodate(folio);
176193
}
177-
flags |= MEMFD_LUO_FOLIO_UPTODATE;
178194

179195
folio_unlock(folio);
180196

181197
pfolio->pfn = folio_pfn(folio);
182-
pfolio->flags = flags;
198+
pfolio->flags = MEMFD_LUO_FOLIO_DIRTY | MEMFD_LUO_FOLIO_UPTODATE;
183199
pfolio->index = folio->index;
184200
}
185201

0 commit comments

Comments
 (0)