Re: [PATCH 02/32] mm/shmem: don't skip parts of a split swap entry when punching holes
From: Gregory Price (Meta)
Date: Wed Dec 02 2026 - 16:25:27 EST
On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
> The final loop in shmem_undo_range() looks up a batch of entries and
> calls shmem_free_swap() for each swap entry. Concurrently swapping in a
> folio that is smaller than the swap entry splits the entry. The first
> piece keeps the value of the original entry, so shmem_free_swap() only
> frees that piece and reports success. The loop then continues with the
> next entry of the batch, skipping the remaining pieces:
>
> hole punch read()
> ========== ======
> find_get_entries()
> index 192: swap, order 6
> index 256: swap, order 6
> shmem_swapin_folio(), index 200
> shmem_split_large_entry()
> shmem_free_swap(), index 192
> order 0 by now: frees 1 page
> shmem_free_swap(), index 256
> /* 193 to 255 are left behind */
>
> While truncate scans again until nothing is left, a hole punch only
> makes a single pass and does not revisit these entries.
>
> Consequently, punching a hole into a shmem/tmpfs file with large swap
> entries can leave old data behind if it races with a read of that range:
> fallocate() succeeds, but later reads return the old data instead of
> zeroes, and the swap space stays in use.
>
> Let's fix it by letting shmem_free_swap() return the number of pages the
> swap entry covered, and looking up the remaining entries again starting
> at the first index that was not freed.
>
> As the first loop in shmem_undo_range() already frees all swap entries
> within the range, these additional lookups are rare.
>
> This has low urgency, as the race is hard to hit.
>
> Fixes: 809bc86517cc ("mm: shmem: support large folio swap out")
> Cc: stable@xxxxxxxxxxxxxxx
> Reported-by: Chris Mason <mason@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
race is indeed hard to hit, i could not actually prove that i could
hit it even with the normal tricks. I did at least validate the
behavior looks otherwise unchanged and the return value is a nice
improvement.
Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
Reviewed-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
--
~Gregory