mm/shmem: don't release a swapin-error marker as a swap entry
Subsystems (2)
PATCH 1/1 mm/shmem: don't release a swapin-error marker as a swap entry
A failed shmem swapin frees the swap slot and leaves a PTE_MARKER_POISONED entry in the page cache. On truncate or eviction shmem_free_swap() passes that marker to swap_put_entries_direct(), which warns because it is not a swap entry. There is nothing left to release either way.
Skip the release for non-swap entries, as every other caller already does.
--- mm/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +4 -2
@@ -1184,6 +1184,7 @@ static long shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap) { XA_STATE(xas, &mapping->i_pages, index); +const softleaf_t swp =radix_to_swp_entry (radswap);unsigned int nr_pages =0 ;pgoff_t base;void *entry;
@@ -1200,8 +1201,9 @@ static long shmem_free_swap(struct address_space *mapping, }xas_unlock_irq (&xas); -if (nr_pages) -swap_put_entries_direct (radix_to_swp_entry (radswap), nr_pages); +/* A swapin-error marker holds no swap slot, so just drop it. */ +if (nr_pages &&softleaf_is_swap (swp)) +swap_put_entries_direct (swp, nr_pages);return nr_pages; }
Signature · 1 line
2.55.0
Andrew Morton
maintainerQuoted 1 line · On Sun, 20 Sep 2026 16:50:02 +0100 David Carlier <[email protected]> wrote:
A failed shmem swapin
from -EIO, for example.
Quoted 11 lines
frees the swap slot and leaves a
PTE_MARKER_POISONED
entry in the page cache. On truncate or evictionshmem_free_swap()passes that marker toswap_put_entries_direct(), which warns because it is not a swap entry. There is nothing left to release either way.Skip the release for non-swap entries, as every other caller already does.
Reported-by [email protected]Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")Signed-off-by David Carlier <[email protected]>
I guess we should backport - errors can happen and WARN can crash machines. What do others think?
Baolin Wang
reviewerQuoted 29 lines · David Carlier
A failed shmem swapin frees the swap slot and leaves a
PTE_MARKER_POISONEDentry in the page cache. On truncate or evictionshmem_free_swap()passes that marker toswap_put_entries_direct(), which warns because it is not a swap entry. There is nothing left to release either way.Skip the release for non-swap entries, as every other caller already does.
Reported-by [email protected]Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")Signed-off-by David Carlier <[email protected]>--- mm/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/shmem.c +1
-0@@ -1184,6 +1184,7 @@ static long shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap) { XA_STATE(xas, &mapping->i_pages, index); +const softleaf_t swp =radix_to_swp_entry (radswap);unsigned int nr_pages =0 ;pgoff_t base;void *entry;@@ -1200,8 +1201,9 @@ static long shmem_free_swap(struct address_space *mapping, }
mm/shmem.c static long shmem_free_swap(struct address_space *mapping, · line 1209
Quoted 10 lines
mm/shmem.c +3
-2xas_unlock_irq (&xas); -if (nr_pages) -swap_put_entries_direct (radix_to_swp_entry (radswap), nr_pages); +/* A swapin-error marker holds no swap slot, so just drop it. */ +if (nr_pages &&softleaf_is_swap (swp)) +swap_put_entries_direct (swp, nr_pages);return nr_pages; }
Makes sense to me.
But another issue caught my eye. Since we already call shmem_recalc_inode() to decrement 'info->swapped' when handling poisoned entries in shmem_set_folio_swapin_error(), shmem_free_swap() still returning 'nr_pages' for poisoned entries would cause a second call to shmem_recalc_inode(inode, 0, -nr_swaps_freed), thus triggerring WARN_ON(i_blocks) in shmem_evict_inode().
I'm not sure if you've observed this warning. IIUC, shmem_free_swap() should return 0 for poisoned entries.
Quoted 53 lines · Baolin Wang
Quoted 29 lines · David Carlier
A failed shmem swapin frees the swap slot and leaves a
PTE_MARKER_POISONEDentry in the page cache. On truncate or evictionshmem_free_swap()passes that marker toswap_put_entries_direct(), which warns because it is not a swap entry. There is nothing left to release either way.Skip the release for non-swap entries, as every other caller already does.
Reported-by [email protected]Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")Signed-off-by David Carlier <[email protected]>--- mm/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/shmem.c +1
-0@@ -1184,6 +1184,7 @@ static long shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap) { XA_STATE(xas, &mapping->i_pages, index); +const softleaf_t swp =radix_to_swp_entry (radswap);unsigned int nr_pages =0 ;pgoff_t base;void *entry;@@ -1200,8 +1201,9 @@ static long shmem_free_swap(struct address_space *mapping, }mm/shmem.c static long shmem_free_swap(struct address_space *mapping, · line 1209
Quoted 10 lines
mm/shmem.c +3
-2xas_unlock_irq (&xas); -if (nr_pages) -swap_put_entries_direct (radix_to_swp_entry (radswap), nr_pages); +/* A swapin-error marker holds no swap slot, so just drop it. */ +if (nr_pages &&softleaf_is_swap (swp)) +swap_put_entries_direct (swp, nr_pages);return nr_pages; }Makes sense to me.
But another issue caught my eye. Since we already call
shmem_recalc_inode()to decrement 'info->swapped' when handling poisoned entries inshmem_set_folio_swapin_error(),shmem_free_swap()still returning 'nr_pages' for poisoned entries would cause a second call toshmem_recalc_inode(inode, 0, -nr_swaps_freed), thus triggerringWARN_ON(i_blocks)inshmem_evict_inode().
Quoted 2 lines
I'm not sure if you've observed this warning. IIUC,
shmem_free_swap()should return 0 for poisoned entries.
Hi Baolin,
Thanks for the review.
I think returning nr_pages is needed. shmem_set_folio_swapin_error() decrements both alloced and swapped, so shmem_recalc_inode() sees nothing freed and the block stays accounted. The second decrement in shmem_undo_range() is what finally releases it. Returning 0 there would leave i_blocks non-zero at evict time.
Cheers
Quoted 60 lines · David CARLIER
Quoted 53 lines · Baolin Wang
Quoted 29 lines · David Carlier
A failed shmem swapin frees the swap slot and leaves a
PTE_MARKER_POISONEDentry in the page cache. On truncate or evictionshmem_free_swap()passes that marker toswap_put_entries_direct(), which warns because it is not a swap entry. There is nothing left to release either way.Skip the release for non-swap entries, as every other caller already does.
Reported-by [email protected]Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")Signed-off-by David Carlier <[email protected]>--- mm/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/shmem.c +1
-0@@ -1184,6 +1184,7 @@ static long shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap) { XA_STATE(xas, &mapping->i_pages, index); +const softleaf_t swp =radix_to_swp_entry (radswap);unsigned int nr_pages =0 ;pgoff_t base;void *entry;@@ -1200,8 +1201,9 @@ static long shmem_free_swap(struct address_space *mapping, }mm/shmem.c static long shmem_free_swap(struct address_space *mapping, · line 1209
Quoted 10 lines
mm/shmem.c +3
-2xas_unlock_irq (&xas); -if (nr_pages) -swap_put_entries_direct (radix_to_swp_entry (radswap), nr_pages); +/* A swapin-error marker holds no swap slot, so just drop it. */ +if (nr_pages &&softleaf_is_swap (swp)) +swap_put_entries_direct (swp, nr_pages);return nr_pages; }Makes sense to me.
But another issue caught my eye. Since we already call
shmem_recalc_inode()to decrement 'info->swapped' when handling poisoned entries inshmem_set_folio_swapin_error(),shmem_free_swap()still returning 'nr_pages' for poisoned entries would cause a second call toshmem_recalc_inode(inode, 0, -nr_swaps_freed), thus triggerringWARN_ON(i_blocks)inshmem_evict_inode().Quoted 2 lines
I'm not sure if you've observed this warning. IIUC,
shmem_free_swap()should return 0 for poisoned entries.Hi Baolin,
Quoted 7 lines
Thanks for the review.
I think returning
nr_pagesis needed.shmem_set_folio_swapin_error()decrements both alloced and swapped, soshmem_recalc_inode()sees nothing freed and the block stays accounted. The second decrement inshmem_undo_range()is what finally releases it. Returning 0 there would leavei_blocksnon-zero at evict time.
Yes, you are right. My memory is coming back :). So,