Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
From: Yosry Ahmed
Date: Wed Dec 02 2026 - 20:12:15 EST
On Wed, Dec 2, 2026 at 10:55 AM Chris Li <chrisl@xxxxxxxxxx> wrote:
>
> On Wed, Dec 2, 2026 at 1:57 AM Alexandre Ghiti <alexghiti@xxxxxxxx> wrote:
> >
> > Hi Chris,
> >
> > On Wed, Dec 2, 2026 at 8:30 AM Chris Li <chrisl@xxxxxxxxxx> wrote:
> > > > 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.
>
> Thanks for checking and raising the swapoff case, that is a good
> point. I am thinking zswap writeback can just add a reference count to
> the swap entry for the writeback and release it when done. That
> ensures the swap entry will not be freed during writeback. Then most
> of the races you mention for swap entry shouldn't need special
> handling. Swap off is different, draining the page table is not going
> to free those swap count own by the write back. Because swapoff is
> very slow and writeback is very fast in comparison. Mark the swap
> device as not in use then wait for outstanding writes to complete
> before trying to unmap the swap entry, we should be good? Can you
> check on that?
>
> >
> > But wouldn't that defeat the purpose of the swap cache as the
> > synchronization layer? We would have to reimplement that
> > synchronization inside zswap.
>
> Swap cache as a synchronization layer is designed for page fault
> synchronization, which is the tricky case to get right. That is why I
> don't want unnecessary complexity in the swap cache. It still
> synchronizes page faults for the write back case, independently of the
> writeback itself. Zswap, or the swap tier layer needs to manage the
> writeback lifecycle anyway. In your original approach, when writeback
> finishes, it needs to install the previous shadow back.
I think we still need to go through the swapcache to synchronize
incoming page faults on the writeback page, otherwise we could end up
with 2 copies of the page, right?