Re: [PATCH] mm/shmem: don't drop a folio whose swap write failed

From: Baolin Wang

Date: Mon Nov 23 2026 - 21:40:04 EST




On 11/24/26 1:21 AM, Mani Tofigh via B4 Relay wrote:
From: Mani Tofigh <mt4022@xxxxxxxxxxxx>

shmem_swapin_folio() drops a large folio that does not fit the mapping
from the swap cache if the folio is neither dirty nor under writeback.
The folio can belong to another file and be under swap writeback, when
the swap entry was freed and reused before the swap cache lookup.

Swap write completion does not take the folio lock. On a failed write,
swap_write_end() marks the folio dirty and then ends writeback. Since
dirty is tested first, the folio can look clean and written:

CPU0 (swapin of file Y) CPU1 (swapout of file X)
----------------------- ------------------------
shmem_swapin_folio()
folio_test_dirty() == false
swap_write_end(), failed write
folio_mark_dirty()
folio_end_writeback()
folio_test_writeback() == false
swap_cache_del_folio()
/* X's data is lost */

The next swapin of X reads the slot whose write failed.

Test writeback first. Both flags are in folio->flags. If the
writeback test sees the write ended, the dirty test then sees the dirty
bit set before it.

With the fix, a folio whose swap write failed stays in the swap cache.

Fixes: fc2c19648d68 ("mm/shmem: fix swapin livelock on a mismatched large swap cache folio")
Suggested-by: Chris Mason <mason@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Mani Tofigh <mt4022@xxxxxxxxxxxx>
---

LGTM. Thanks.
Reviewed-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>