Re: [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio

Kairui Song
2025-09-12T12:37:20Z

On Fri, Sep 12, 2025 at 4:22 PM Baolin Wang
<baolin.wang@linux.alibaba.com> wrote:
> On 2025/9/11 00:08, Kairui Song wrote:
> > From: Kairui Song <kasong@tencent.com>
> >
> > Shmem may replace a folio in the swap cache if the cached one doesn't
> > fit the swapin's GFP zone. When doing so, shmem has already double
> > checked that the swap cache folio is locked, still has the swap cache
> > flag set, and contains the wanted swap entry. So it is impossible to
> > fail due to an XArray mismatch. There is even a comment for that.
> >
> > Delete the defensive error handling path, and add a WARN_ON instead:
> > if that happened, something has broken the basic principle of how the
> > swap cache works, we should catch and fix that.
> >
> > Signed-off-by: Kairui Song <kasong@tencent.com>
> > Reviewed-by: David Hildenbrand <david@redhat.com>
> > ---
> > mm/shmem.c | 42 ++++++++++++------------------------------
> > 1 file changed, 12 insertions(+), 30 deletions(-)
> >
> > diff --git a/mm/shmem.c b/mm/shmem.c
> > index 410f27bc4752..5f395fab489c 100644
> > --- a/mm/shmem.c
> > +++ b/mm/shmem.c
> > @@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
> > }
> >
> > /*
> > - * The delete_from_swap_cache() below could be left for
> > + * The swap_cache_del_folio() below could be left for
> > * shrink_folio_list()'s folio_free_swap() to dispose of;
> > * but I'm a little nervous about letting this folio out of
> > * shmem_writeout() in a hybrid half-tmpfs-half-swap state
> > * e.g. folio_mapping(folio) might give an unexpected answer.
> > */
> > - delete_from_swap_cache(folio);
> > + swap_cache_del_folio(folio);
> > goto redirty;
> > }
> > if (nr_pages > 1)
> > @@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode,
> > new->swap = entry;
> >
> > memcg1_swapin(entry, nr_pages);
> > - shadow = get_shadow_from_swap_cache(entry);
> > + shadow = swap_cache_get_shadow(entry);
>
> Again, there are still some issues with the patch split. The swapcache
> related APIs replacement should be placed in Patch 8, otherwise there
> will be buidling errors after applying Patch 8.
>
> With this issue fixed:
> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>

Hi Baolin

Yeah you are right, I need to move these few changes to patch 8.

BTW I also found that the WARN_ON and irq unlock needs following fix,
the stats update need to be done with irq disabled:

diff --git a/mm/shmem.c b/mm/shmem.c
index 957e40caba6e..c4d491c93506 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2121,14 +2121,14 @@ static int shmem_replace_folio(struct folio
**foliop, gfp_t gfp,
/* Swap cache still stores N entries instead of a high-order entry */
xa_lock_irq(&swap_mapping->i_pages);
for (i = 0; i < nr_pages; i++) {
- WARN_ON_ONCE(xas_store(&xas, new));
+ WARN_ON_ONCE(xas_store(&xas, new) != old);
xas_next(&xas);
}
- xa_unlock_irq(&swap_mapping->i_pages);

mem_cgroup_replace_folio(old, new);
shmem_update_stats(new, nr_pages);
shmem_update_stats(old, -nr_pages);
+ xa_unlock_irq(&swap_mapping->i_pages);

folio_add_lru(new);
*foliop = new;