Skip to contentSkip to conversation

mm/shmem: don't release a swapin-error marker as a swap entry

PATCH Applied David Carlier 3 participants 5 messages Sep 20 – Sep 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Reported-by [email protected]
Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")
---
 mm/shmem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +4 -2
index b572c60f2af8..94f2c59c8cfc 100644
@@ -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
maintainer
#1 lore raw
Quoted 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 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.

Reported-by [email protected]
Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")

I guess we should backport - errors can happen and WARN can crash machines. What do others think?

reviewer
#2 lore raw
In reply to David Carlier
Quoted 29 lines · David Carlier

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.

Reported-by [email protected]
Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")
---
  mm/shmem.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +1 -0
index b572c60f2af8..94f2c59c8cfc 100644
@@ -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 -2
  	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;
  }

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.

#3 lore raw
Quoted 53 lines · Baolin Wang
Quoted 29 lines · David Carlier

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.

Reported-by [email protected]
Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")
---
  mm/shmem.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +1 -0
index b572c60f2af8..94f2c59c8cfc 100644
@@ -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 -2
      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;
  }

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().

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

reviewer Reviewed-by
#4 lore raw
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_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.

Reported-by [email protected]
Fixes ac2d3268284b ("mm/swapfile.c: remove the unneeded checking")
---
   mm/shmem.c | 6 ++++--
   1 file changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +1 -0
index b572c60f2af8..94f2c59c8cfc 100644
@@ -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 -2
       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;
   }

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().

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_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.

Yes, you are right. My memory is coming back :). So,