FAILED: patch "[PATCH] mm/shmem: fix accounting of swapin-error markers" failed to apply to 6.6-stable tree
Subsystems (3)
Sasha Levin
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 66ce68a249f4ac56cd6968aa6d8ded20fb4b7d09
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 66ce68a249f4ac56cd6968aa6d8ded20fb4b7d09 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Thu, 29 Oct 2026 11:43:35 +0100
Subject: [PATCH] mm/shmem: fix accounting of swapin-error markers
A failed swap read replaces the swap entry with a swapin-error marker in the page-cache and subtracts its pages from "alloced" and "swapped"; however, it leaves their block charge in place.
Removing the marker during truncate then subtracts from "swapped" again. This can underflow the counters or make swapped reach zero while other pages remain swapped out, causing swapoff to loop without finding them.
When installing the marker, subtract only from "swapped" and let shmem_recalc_inode() reduce "alloced" and release the block charge.
Make shmem_free_swap() report successful removal separately from the number of swap pages freed, so removing a swapin-error marker does not subtract from "swapped" again.
It's a bit confusing, but replacing a swapped out page by a swapin-error marker in shmem_set_folio_swapin_error() behaves as follows:
Initially:
alloced=1, swapped=1, nrpages=0 Calling shmem_recalc_inode(inode, 0, -1) -> swapped = 0 -> freed = 1 - 0 - 0 = 1 -> alloced -= freed -> alloced=0, swapped=0, block charge released
--- mm/shmem.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)
mm/shmem.c +14 -17
@@ -1177,12 +1177,11 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) }/* -* Remove swap entry from page cache, free the swap and its page cache. Returns -* the number of pages being freed. 0 means entry not found in XArray (0 pages -* being freed). +* 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. */ -static long shmem_free_swap (struct address_space *mapping, -pgoff_t index,pgoff_t end,void *radswap) +static bool 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);const softleaf_t swp =radix_to_swp_entry (radswap);
@@ -1203,8 +1202,10 @@ static long shmem_free_swap(struct address_space *mapping,xas_unlock_irq (&xas);/* A swapin-error marker holds no swap slot, so just drop it. */ -if (nr_pages &&softleaf_is_swap (swp)) +if (nr_pages &&softleaf_is_swap (swp)) {swap_put_entries_direct (swp, nr_pages); + *nr_swaps_freed += nr_pages; + }return nr_pages; }
@@ -1358,8 +1359,8 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,if (xa_is_value (folio)) {if (unfalloc)continue ; - nr_swaps_freed +=shmem_free_swap (mapping, indices[i], - end -1 , folio); +shmem_free_swap (mapping, indices[i], end -1 , + folio, &nr_swaps_freed);continue ; }
@@ -1422,13 +1423,11 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,if (xa_is_value (folio)) {int order; -long swaps_freed;if (unfalloc)continue ; - swaps_freed =shmem_free_swap (mapping, indices[i], - end -1 , folio); -if (!swaps_freed) { +if (!shmem_free_swap (mapping, indices[i], end -1 , + folio, &nr_swaps_freed)) {pgoff_t base = indices[i]; order = shmem_confirm_swap(mapping, indices[i],
@@ -1449,7 +1448,6 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, index = base;break ; } - nr_swaps_freed += swaps_freed;continue ; }
@@ -2409,11 +2407,10 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,folio_put_swap (folio,NULL );swap_cache_del_folio (folio);/* -* Don't treat swapin error folio as alloced. Otherwise inode->i_blocks -* won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) -* in shmem_evict_inode(). +* Let shmem_recalc_inode() reduce alloced and release the block charge +* for the freed swap pages. */ -shmem_recalc_inode (inode, -nr_pages, -nr_pages); +shmem_recalc_inode (inode,0 , -nr_pages); } static int shmem_split_large_entry(struct inode *inode, pgoff_t index,
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/1 mm/shmem: fix accounting of swapin-error markers
From: "David Hildenbrand (Arm)" <[email protected]>
[ Upstream commit 66ce68a249f4ac56cd6968aa6d8ded20fb4b7d09 ]
A failed swap read replaces the swap entry with a swapin-error marker in the page-cache and subtracts its pages from "alloced" and "swapped"; however, it leaves their block charge in place.
Removing the marker during truncate then subtracts from "swapped" again. This can underflow the counters or make swapped reach zero while other pages remain swapped out, causing swapoff to loop without finding them.
When installing the marker, subtract only from "swapped" and let shmem_recalc_inode() reduce "alloced" and release the block charge.
Make shmem_free_swap() report successful removal separately from the number of swap pages freed, so removing a swapin-error marker does not subtract from "swapped" again.
It's a bit confusing, but replacing a swapped out page by a swapin-error marker in shmem_set_folio_swapin_error() behaves as follows:
Initially:
alloced=1, swapped=1, nrpages=0 Calling shmem_recalc_inode(inode, 0, -1) -> swapped = 0 -> freed = 1 - 0 - 0 = 1 -> alloced -= freed -> alloced=0, swapped=0, block charge released
--- mm/shmem.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
mm/shmem.c +19 -14
@@ -850,18 +850,24 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) }/* -* Remove swap entry from page cache, free the swap and its page cache. +* Remove a swap entry or swapin-error marker at index. +* Return whether it was removed, counting freed swap pages in nr_swaps_freed. */ -static int shmem_free_swap (struct address_space *mapping, -pgoff_t index,void *radswap) +static bool shmem_free_swap (struct address_space *mapping, +pgoff_t index,void *radswap,long *nr_swaps_freed) { +const swp_entry_t swp =radix_to_swp_entry (radswap);void *old; old =xa_cmpxchg_irq (&mapping->i_pages, index, radswap,NULL ,0 );if (old != radswap) -return -ENOENT ; -free_swap_and_cache (radix_to_swp_entry (radswap)); -return 0 ; +return false; +/* A swapin-error marker holds no swap slot, so just drop it. */ +if (!non_swap_entry (swp)) { +free_swap_and_cache (swp); + (*nr_swaps_freed)++; + } +return true; }/*
@@ -1014,8 +1020,8 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,if (xa_is_value (folio)) {if (unfalloc)continue ; - nr_swaps_freed += !shmem_free_swap (mapping, - indices[i], folio); +shmem_free_swap (mapping, indices[i], folio, + &nr_swaps_freed);continue ; }
@@ -1083,12 +1089,12 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,if (xa_is_value (folio)) {if (unfalloc)continue ; -if (shmem_free_swap (mapping, indices[i], folio)) { +if (!shmem_free_swap (mapping, indices[i], folio, + &nr_swaps_freed)) {/* Swap was replaced by page: retry */ index = indices[i];break ; } - nr_swaps_freed++;continue ; }
@@ -1842,11 +1848,10 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,folio_wait_writeback (folio);delete_from_swap_cache (folio);/* -* Don't treat swapin error folio as alloced. Otherwise inode->i_blocks -* won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) -* in shmem_evict_inode(). +* Let shmem_recalc_inode() reduce alloced and release the block charge +* for the freed swap pages. */ -shmem_recalc_inode (inode,-1 ,-1 ); +shmem_recalc_inode (inode,0 ,-1 );swap_free (swap); }
Signature · 1 line
2.53.0