Re: [PATCH v2] mm/truncate: fix data loss when truncating straddling large folios
From: Andrew Morton
Date: Wed Sep 09 2026 - 19:20:52 EST
On Wed, 9 Sep 2026 14:23:39 +0800 Zhang Yi <yi.zhang@xxxxxxxxxxxxxxx> wrote:
> From: Zhang Yi <yi.zhang@xxxxxxxxxx>
>
> truncate_inode_partial_folio() splits a large folio so that the caller's
> truncate loop can drop the in-range sub-folios while keeping the
> out-of-range tail. The first split at the punch start edge is
> non-uniform, which leaves the sub-folio at the truncation end edge as
> large as possible, this means it may still straddle the range, holding
> both zeroed in-range and valid out-of-range data. The function then
> attempts a second split at offset + length to isolate that tail.
>
> If the second split fails the straddling sub-folio stays merged. The
> function returned true unconditionally on all exit paths of the success
> block, telling the caller it was fully handled. The caller kept its
> default end and the truncate loop truncated every sub-folio below it,
> including the merged straddler, discarding the valid out-of-range tail.
>
> For example, a 4-page order-2 folio punched from offset 0 to the middle
> of the last page:
>
> truncate_inode_pages_range()
> truncate_inode_partial_folio() # same_folio == true
> 1st split at page0 -> [p0, p1, p2-3] # non-uniform, success
> folio2 = p2-3 # straddles: p2 zeroed, p3 tail valid
> 2nd split of folio2 fails / cannot lock
> return true # BUG: caller keeps default end
> end = 3
> loop truncates p0, p1, p2-3 # p3's valid tail is lost
>
> This became reachable after commit 7460b470a131 ("mm/truncate: use
> folio_split() in truncate operation") replaced the atomic split_folio()
> with folio_split(), whose non-uniform split can partially split a folio
> and leave the end edge merged.
>
> It has gone unnoticed because a dirty large folio normally carries the
> filesystem's private data, for example buffer_head, so
> filemap_release_folio() -> iomap_release_folio() returns false on a
> dirty folio and folio_split() aborts with -EBUSY before any split,
> leaving the straddler safely unsplit. The bug is only reachable on paths
> that produce dirty large folios without filesystem private data, and it
> was caught on the upcoming ext4 iomap buffered I/O path when no ifs is
> attached.
So is there any way of being hit by these issues in 7.2 and earlier?
> mm/internal.h | 4 +--
> mm/shmem.c | 13 +++-----
> mm/truncate.c | 88 +++++++++++++++++++++++++++++++++++----------------
> 3 files changed, 67 insertions(+), 38 deletions(-)
I hope not, because that's quite a lump of code to be backporting.
Thanks. I'll queue it for testing and shall await your response to Zi
Yan's questions.
I see you've received Sashiko's feedback. Please let us know if
there's any validity to it.
https://sashiko.dev/#/patchset/20260909062339.473816-1-yi.zhang@xxxxxxxxxxxxxxx