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@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/shmem.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index fca7c1f6c8574..afe6c80b6f48c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1206,9 +1206,11 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) /* * Remove a swap entry or swapin-error marker wholly within [index, end]. - * Return whether it was removed, counting freed swap pages in nr_swaps_freed. + * Return the number of pages the entry covered, or 0 if it was not removed. + * Only a swap entry holds swap slots: their number is added to + * nr_swaps_freed. */ -static bool shmem_free_swap(struct address_space *mapping, +static unsigned int shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap, long *nr_swaps_freed) { XA_STATE(xas, &mapping->i_pages, index); @@ -1473,12 +1475,15 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, folio = fbatch.folios[i]; if (xa_is_value(folio)) { + unsigned int nr_pages; int order; if (unfalloc) continue; - if (!shmem_free_swap(mapping, indices[i], end - 1, - folio, &nr_swaps_freed)) { + nr_pages = shmem_free_swap(mapping, indices[i], + end - 1, folio, + &nr_swaps_freed); + if (!nr_pages) { pgoff_t base = indices[i]; order = shmem_confirm_swap(mapping, indices[i], @@ -1499,7 +1504,13 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, index = base; break; } - continue; + /* + * A swapin may have split the entry after the + * lookup, so only a part of it might have been + * removed. Look up the rest. + */ + index = indices[i] + nr_pages; + break; } folio_lock(folio); -- 2.56.1