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

From: Chris Li

Date: Thu Dec 03 2026 - 11:47:47 EST


On Thu, Dec 3, 2026 at 6:25 AM Alexandre Ghiti <alexghiti@xxxxxxxx> wrote:
>
> Hi Chris,
>
> On Wed, Dec 2, 2026 at 7:55 PM 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?
>
> I prototyped your approach with my favourite llm to see what it takes,
> and three things stand out:

Thank you so much for trying it out. I really appriciat that. It is
just some idea or intuition I have. Thanks for validating it and
catching the fallouts.
>
> - the zswap entry has to stay until the write completes, so every
> writeback now holds both the compressed and the decompressed data at
> the same time. Since writeback is called under memory pressure, this
> is to me the major negative point.

That is a valid point; thanks for raising it. It is addressable by
setting aside some writeback buffer space up front. BTW, this benefits
the previous approach as well (see below).

It is less of an issue than it appears because:

First of all, with the previous approach, even when you free the
zsmalloc entry. The memory is likely not immediately returned to the
system during the writeback window. Zsmalloc manages compressed data
in size classes; it will likely share the size class buffer with other
data still occupying the page, meaning the memory isn't immediately
returned to the system (fragmentation in the zsmalloc pool)

Secondly, the write windows is pretty small; after it, the compressed
memory in zswap is freed with the zswap entry.

Third, one more reason to write compressed data instead :-)

> - struct zswap_entry needs a refcount (a racing swapin must not
> decompress from an entry the completion frees) which makes the struct
> grows from 48 to 56 bytes

The write back refcount can have a max value of one or two, right? Can
we use some atomic bits that fit into the padding area of the existing
zswap entry without increasing the zswap entry size? Current zswap
struct has:
unsigned int length;
bool referenced;
u8 pool_idx;
There should be room to fit a u8 for bit addressing.

> - the swap count taken by the writeback conflicts with exclusive swap PTEs.

That is a real issue. Again, thanks for catching it. If we use the
zswap refcount, can we count that instead of bumping up the swap
count? As long as the zswap entry isn't freed, it can still hold the
swap entry count from dropping to zero, right? I can use some help
validating that.

>
> Regarding swapoff, your idea works: stop new writebacks to the device
> and wait for the in-flight ones before try_to_unuse(), otherwise
> try_to_unuse() finds nothing in the swap cache to wait on and keeps
> retrying.

Thanks for confirming.

>
> Is this something we want to pursue? I can spend some time cleaning up
> the prototype (my LLM wrote most of it) and post it as an RFC.

Yes please. Let's address other concerns at a high level first before
diving into the patches. I think over all this is simpler and more
future-proof when we move to writing compressed data.

Chris

>
> Thanks,
>
> Alex
>
> >
> > >
> > > 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.
> >
> > Another point is to consider more advanced writeback, such as writing
> > back compressed data. In that case it shouldn't need to touch the swap
> > cache at all. That is a very useful feature because it reduces SSD
> > wear.
> > I argue that keeping the swap cache off the writeback path might be
> > simpler and more optimal overall.
> >
> > Chris