Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path

From: Alexandre Ghiti

Date: Wed Dec 02 2026 - 04:57:15 EST


Hi Chris,

On Wed, Dec 2, 2026 at 8:30 AM Chris Li <chrisl@xxxxxxxxxx> wrote:
>
> >
> On Tue, Dec 1, 2026 at 10:17 AM Yosry Ahmed <yosry@xxxxxxxxxx> wrote:
> >
> > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@xxxxxxxx> wrote:
> > >
> > > When an anonymous folio is reclaimed, workingset_eviction() stores a
> > > "shadow" (the eviction cookie) in the swap slot so that a later swap-in
> > > can be recognised as a refault and, if the refault distance is short
> > > enough, the page can be re-activated. This is how anon workingset/refault
> > > detection has worked since commit aae466b0052e ("mm/swap: implement
> > > workingset detection for anonymous LRU").
> > >
> > > zswap writeback breaks this in two independent ways:
> > >
> > > - Over-count at writeback: the shrinker allocates a buffer folio in the
> > > swap cache, and the allocation path counts that folio as a refault.
> > >
> > > - Lost eviction cookie at reclaim: adding the buffer to the swap cache
> > > overwrites the slot's shadow, so the original cookie is lost; when the
> > > buffer folio is finally reclaimed a fresh, inaccurate cookie is minted
> > > in its place.
> > >
> > > This series preserves the shadow within zswap itself, without any new
> > > swap-table or swap-slot state. At writeback, instead of erasing the freed
> > > zswap entry, the captured shadow is parked in the zswap tree in its place,
> > > so it outlives the writeback buffer folio.
> > >
> > > Finally, the refault evaluation is moved out of the swap-cache allocator
> > > into the swap-in callers, so allocating the writeback buffer is no longer
> > > miscounted as a refault.
> >
> > I admittedly did not look at the individual patches yet, but this
> > seems to be too invasive. I also wonder how this would interact with
> > incoming vswap and swap tiering work. Seems like it would become a
> > generic problem whenever we need to move a page between swap tiers, we
> > have to allocate a folio in the swap cache, but we want to keep the
> > shadow intact and avoid reactivation.
>
> I feel it is too invasive as well.
>
> >
> > Seems like both problems can be fixed if we can teach the swap cache
> > about a folio being added for paths other than swapin? Patch 1 reads
>
> I am actually thinking the opposite. How about we avoid loading into
> the swap cache if it is not a page fault? The swap cache's folio
> loading assumes it is for a page fault; there are other counter side
> effects as well as shadows. If we keep it that way. During writeback,
> let zswap load into a folio, mark the zswap entry as writing back, and
> submit it to the block layer. Then when the io completes. There are
> two case. If there is no page fault for this entry, free the zswap
> entry and you are done. If a page fault occurs, the page fault handler
> can notice this zswap entry is writing back. Serve the swap cache
> normally by installing a new folio into the swap cache, but don't
> delete the zswap entry until the writeback completion callback. The
> data on the block device is outdated (useless).
> Do I miss any race condition with this approach?
>
> This fits into my original design where the swap cache is the top
> half, tightly coupled to the user page fault. Moving data between
> backends is at the bottom half of the swap entry.

Nothing seems to prevent your approach: the races we (my llm and I,
mostly my llm tbh) found (the slot freed and reused while the write is
still in flight, a second write to the same slot after a fault,
load/invalidate freeing an entry under writeback, swapoff waiting for
in-flight writes) can all be fixed.

But wouldn't that defeat the purpose of the swap cache as the
synchronization layer? We would have to reimplement that
synchronization inside zswap.

Thanks,

Alex

>
> Chris
>
> > like it does this, but seems like we still need patch 3 to keep the
> > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > refault out of the swap cache but kept allocating a new shadow?
> >
> > I wonder if both problems can be tackled at once if we handle this
> > within the swap cache, and if the implementation would be simpler.
>