Re: [PATCH] mm: free the swap slot of a dirty swapin folio
From: Nhat Pham
Date: Thu Nov 26 2026 - 12:55:07 EST
On Thu, Nov 26, 2026 at 9:20 AM David Hildenbrand (Arm)
<david@xxxxxxxxxx> wrote:
>
> On 11/26/26 16:01, Alexandre Ghiti wrote:
> >
> >>
> >> But ... isn't this zswap specific? Why do we have zswap specific stuff in this
> >> common code?
> >>
> >> The comment already gives away that something is not quite right here.
> >
> > I am mostly interested in the zswap benefits, but nothing is zswap
> > specific here: do_swap_page() can also find a dirty folio in the swap
> > cache when reclaim bailed out before writeout, or when the swap write
> > failed. In all those cases, the copy in the slot is stale, so the slot
> > only reserves swap space. zswap is simply the common case, since
> > zswap_load() dirties the folio.
> >
> > What do you think?
>
> If it's generic, then the comment shouldn't mention zswap in the comment :)
+1. Maybe mention zswap as an example of when this can happen rather
than the sole motivation. I suspect this is a generic problem, with
zswap being a common/easy to trigger case :)
In general, seems like the whole point of caching these slots
(especially after Kairui optimized the swap allocator) is just to
avoid repeating pageout work in subsequent reclaim attempts. In zswap
case (where we invalidate the fault after page-in), that trade-off is
not worth it.
FWIW, zram is also doing the same thing:
/*
* Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
* cache can help save some IO or memory overhead, but these devices
* are fast, and meanwhile, swap cache pinning the slot deferring the
* release of metadata or fragmentation is a more critical issue.
*/
if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
return true;
so we kinda already have some backend-specific logic bled out to this
location. Maybe we can genericize this in the future :)
>
> And the patch description should be all about zswap.
>
> And we should measure the effect without zswap.
>
> So, folio dirty -> We'd need another writeback because the content on disk is stale.
>
> If we reclaim swap, the next reclaim run will have to re-allocate one.
>
>
> But why is this "is dirty" linked to exclusive?
I don't think we need exclusive here at all yeah. folio_free_swap()
already does all the necessary safety checks.
>
> What if we did
>
> /*
> * Free the swapcache if the folio is already dirty, or if we will dirty it
> * immediately afterwards by writing to it.
> */
> if (folio_test_dirty(folio))
> return true;
> return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
LGTM.