Re: [RFC PATCH v4 10/18] mm/swap: add priority queue for swap device allocation
From: Kairui Song
Date: Sun Nov 15 2026 - 22:52:04 EST
On Sat, Nov 14, 2026 at 10:51 PM Lian Wang <lianux.mm@xxxxxxxxx> wrote:
>
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> Swap device selection rotates swap_avail_head with plist_requeue(),
> serializing cluster transitions on swap_avail_lock. Separate selection
> from device-local cluster allocation with same-priority device rings
> ordered from highest priority to lowest.
>
> Keep membership and sizes stable under swapon_rwsem during each
> allocation walk. Full or disabled devices remain as masked pointers.
> Serialize tag writers with swap_queue_update_lock and use READ_ONCE()
> and WRITE_ONCE() for complete tagged-pointer accesses.
>
> Each CPU has a starting offset and base-page quota per ring. A task-local
> cursor snapshots its starting device so retries visit every peer once,
> even if another task changes the shared reader. Disable migration across
> the sleepable walk. Large folios retry same-priority peers before asking
> the caller to split instead of falling through after one fragmented
> device.
>
> Prepare reader storage with GFP_NOIO outside the topology writer, then
> recheck priority and required length before publication. Transfer the
> prepared storage only on successful installation; otherwise free it
> after unlocking. Keep per-CPU backing allocation outside the writer's
> noreclaim scope because it filters caller GFP flags. Constrain small
> queue/ring allocations with a scoped noreclaim context and
> __GFP_NOMEMALLOC, restoring context before cleanup. Deletion keeps reader
> storage until replacement or an empty queue, avoiding shrinking backing
> allocations beneath the writer.
>
> These boundaries prevent queue updates from reclaiming into a swap
> allocation that would acquire a reader behind the same writer. The
> cluster metadata fallback also avoids direct reclaim while the walk
> retains its outer reader. These recursive paths were identified by
> source inspection rather than reproduced hangs.
>
> Publish swap_file, the live reference, queue membership and SWP_WRITEOK
> under the same writer. Complete the disable flag and queue mask
> transition before releasing that writer, so a new allocation reader
> cannot select a device that is no longer writable.
>
> Mask the off-list flag from the old in-use counter before its full-device
> comparison so concurrent freeing/refilling keeps availability state
> consistent.
>
> Keep the old available plist in parallel while migrating its remaining
> consumers.
>
> Link: https://lore.kernel.org/20260714-swap-pcp-priq-v1-9-de9b164ed419@xxxxxxxxxxx
Hmm, this patch is now quite different from what I posted in the link
above, e.g. there wasn't a "struct swap_queue_cursor", and some of the
naming in it is quite confusing:
e.g. "ring_only", I think what you actually want is "fallback_allowed"
or "no_fallback".
And since there are three iterator-like structs, we better reorganize
the naming; 'iterator' vs 'cursor' is confusing. e.g. maybe reame
swap_ring_iterator to swap_queue_reader_head.
And why do we need a "cursor->valid"? I see a following commit dropped
that, so perhaps accetable to keep it as intermediate step though.