Re: [PATCH v5 14/19] mm, swap: cleanup swap entry management workflow

Kairui Song
2025-12-22T02:43:46Z

On Sat, Dec 20, 2025 at 12:02 PM Baoquan He <bhe@redhat.com> wrote:
>
> On 12/20/25 at 03:43am, Kairui Song wrote:
> > From: Kairui Song <kasong@tencent.com>
> >
> > The current swap entry allocation/freeing workflow has never had a clear
> > definition. This makes it hard to debug or add new optimizations.
> >
> > This commit introduces a proper definition of how swap entries would be
> > allocated and freed. Now, most operations are folio based, so they will
> > never exceed one swap cluster, and we now have a cleaner border between
> > swap and the rest of mm, making it much easier to follow and debug,
> > especially with new added sanity checks. Also making more optimization
> > possible.
> >
> > Swap entry will be mostly allocated and free with a folio bound.
> ~~~~
> freed, typo

Ack, nice catch.

> > The folio lock will be useful for resolving many swap ralated races.
> >
> > Now swap allocation (except hibernation) always starts with a folio in
> > the swap cache, and gets duped/freed protected by the folio lock:
> >
> > - folio_alloc_swap() - The only allocation entry point now.
> > Context: The folio must be locked.
> > This allocates one or a set of continuous swap slots for a folio and
> > binds them to the folio by adding the folio to the swap cache. The
> > swap slots' swap count start with zero value.
> >
> > - folio_dup_swap() - Increase the swap count of one or more entries.
> > Context: The folio must be locked and in the swap cache. For now, the
> > caller still has to lock the new swap entry owner (e.g., PTL).
> > This increases the ref count of swap entries allocated to a folio.
> > Newly allocated swap slots' count has to be increased by this helper
> > as the folio got unmapped (and swap entries got installed).
> >
> > - folio_put_swap() - Decrease the swap count of one or more entries.
> > Context: The folio must be locked and in the swap cache. For now, the
> > caller still has to lock the new swap entry owner (e.g., PTL).
> > This decreases the ref count of swap entries allocated to a folio.
> > Typically, swapin will decrease the swap count as the folio got
> > installed back and the swap entry got uninstalled
> >
> > This won't remove the folio from the swap cache and free the
> > slot. Lazy freeing of swap cache is helpful for reducing IO.
> > There is already a folio_free_swap() for immediate cache reclaim.
> > This part could be further optimized later.
> >
> > The above locking constraints could be further relaxed when the swap
> > table if fully implemented. Currently dup still needs the caller
> ~~ s/if/is/ typo

Ack, Thanks!

>
> > to lock the swap entry container (e.g. PTL), or a concurrent zap
> > may underflow the swap count.
> ......
>
>