Re: [PATCH v5 00/19] mm/swap: introduce priority queues for swap device allocation
From: Chris Li
Date: Mon Nov 30 2026 - 11:50:51 EST
On Mon, Nov 23, 2026 at 6:16 AM Lian Wang <lianux.mm@xxxxxxxxx> wrote:
>
> Hi all,
>
> This is v5 of Kairui Song's swap priority queue series. Today, choosing
> among swap devices of equal priority means rotating a shared plist.
> With this series, each CPU advances its own position in a ring of
> equal-priority devices. The allocator selects a device before looking for
> slots on it. This keeps the existing global swap priorities and does not
> add a tier ABI or a per-memcg device-selection policy. Memcg swap
> accounting and limits are unchanged.
Here is my reading of it:
The core design implements a read-only swap queue structure, where
each queue contains an array of devices sharing the same priority
level. The rotation mechanism is tracked via a per-CPU iteration
structure off the swap queue, effectively serving as an index. This
design separates the data structure into a static, read-only component
and a dynamic component—the per-CPU index—which is updated
independently during rotation without modifying the read-only swap
queue itself.
Updating the read-only swap queue is expensive, requiring the write
lock during swap on/off. Otherwise for reading only take the rwsem for
the read lock. That is the common case because swap on/off is rare and
slow.
The idea is brilliant. I think that is a direction we should follow.
The implementation I have two complains:
1) A bit complex, the two level structure queue first then ring with
array of devices.
2) The dynamic allocation of the swap queue and the array of devices
creates a lot of code.
While I agree that the design is a significant step in the right
direction, I have concerns regarding the current implementation's
complexity, specifically the two-level structure (a queue of rings
containing arrays of devices) and the complexity introduced by dynamic
memory allocation for these structures.
I propose a flattened approach that might simplify the implementation:
1. Replace the two-level structure with a single, statically
allocated, priority-sorted array of swap devices.
2. Introduce a secondary static array that maps each priority level to
the index of the first device within the sorted device array.
3. Keep the per-CPU spinning index. Each device can include a pointer
or index back to its corresponding entry in the priority array.
Under this model, the device mask logic remains the same but operates
directly on the flattened array. During allocation, the system would
identify the first available device; if the device shares a priority
with others, it would use the per-CPU iteration index to locate the
current position and scan accordingly. This avoids the overhead of
dynamic structure management while keeping the spinning mechanism as
an optional step only when shared priorities are involved.
This structure seems much easier to manipulate and maintain. It should
be faster as well because the mask bit array can fit into one cache
line. Would you be open to exploring this alternative?
I can spend some time discussing this with you offline.
Chris
>
> Thanks to Kairui and Kunwu Chan for reviewing v4. I'm dropping the RFC
> tag and submitting the series for review.
>
> Changes since v4:
>
> - Rebased onto mm-unstable at 7d1a16abe4cc (7.4-rc3), retaining the
> upstream swap size reporting fix in swap_show() and swapon().
> - Following Kairui's review, renamed swap_ring_iterator to
> swap_queue_reader_head and ring_only to no_fallback. A large folio
> can still try other devices of the same priority after an allocation
> failure, but not a lower priority.
> - Following Kunwu's review, retained READ_ONCE() in the availability
> check, removed a misleading comment about slot reuse, and made both
> /proc/swaps iterators check the published swap_file under the read
> lock.
> - Split the off-list bit fix into patch 10, before the queue conversion.
> The plist allocator also needs to mask that bit when checking whether
> a swap device is full.
> - Revised patch 04 to distinguish all allocated descriptors from
> writable devices. drain_mmlist() needs only the usage count and no
> longer reads the flags. si_swapinfo() takes one flags snapshot when
> accounting for devices being taken offline. I have not carried the
> previous Reviewed-by on that patch.
>
> Testing:
>
> I ran Kairui's benchmarks on the 7.4-rc3 base (7d1a16abe4cc) before and
> after applying the then-current v5 patches.
>
> On one x86-64 host, both kernels built the same source (4dbe24720eb3)
> with make -j96 and eight 8-GiB ZRAM devices. Each build used a fresh
> output directory. The BRD test used twelve 8-GiB RAM disks, a 24-GiB
> memcg limit and one invocation per kernel:
>
> usemem --init-time -O -y -x -n 32 1536M
>
> Times are in seconds (means for kbuild).
>
> Workload Runs/kernel SYS: Before -> After Wall: Before -> After
> kbuild, 2 GiB 12 4193.23 -> 4271.06 167.80 -> 168.80
> kbuild, 3 GiB 12 2257.60 -> 2299.63 136.29 -> 136.77
> 12-BRD 1 141.16 -> 131.44 6.61 -> 6.32
>
> Both kbuild cases used 1.86% more system CPU time; elapsed time rose by
> 0.60% and 0.35%, respectively. In the single BRD run, throughput
> (48 GiB mapped divided by elapsed time) rose from 7436 to 7777 MiB/s
> (+4.59%). There was one boot per kernel and the baseline ran first.
> The BRD result alone does not establish a repeatable speedup.
>
> v4:
> https://lore.kernel.org/all/cover.1794666741.git.lianux.mm@xxxxxxxxx/
> v1 and original test method:
> https://lore.kernel.org/all/20260714-swap-pcp-priq-v1-0-de9b164ed419@xxxxxxxxxxx/
> Related swap tier work:
> https://lore.kernel.org/all/20260916183437.2946306-1-youngjun.park@xxxxxxx/
>
> Best regards,
> Lian Wang
>
> ---
>
> Kairui Song (12):
> mm/swap: remove unused parameter for reading swap header
> mm/swap: clean up hibernation error handling
> mm/swap: cleanup and document swap device availability flag usage
> mm/swap: introduce swap device iteration helper
> mm/swap: change the swapon lock into a percpu rwsem
> mm/swap: serialize swap publication before removing swapon mutex
> mm/swap: consolidate swap inuse accounting helpers
> mm/swap: add priority queue for swap device allocation
> mm/swap: remove available list
> mm/swap: bound synchronous discard outside the allocation reader
> mm/swap: drop swap active plist
> lib/plist.c: remove requeue function
>
> Lian Wang (6):
> mm/swap: avoid direct reclaim when populating swap cluster metadata
> mm/swap: mask the off-list bit when checking whether swap is full
> mm/swap: reuse per-CPU queue readers across priority changes
> mm/swap: advance the global queue cursor directly between priority
> rings
> mm/swap: skip reader rotation for single-device priority rings
> mm/swap: redistribute queue readers after membership changes
>
> Youngjun Park (1):
> mm/swap: change back to use each swap device's percpu cluster
>
> include/linux/plist.h | 2 -
> include/linux/swap.h | 45 +-
> lib/plist.c | 71 --
> mm/swap.h | 12 +-
> mm/swapfile.c | 1446 ++++++++++++++++++++++++++---------------
> 5 files changed, 963 insertions(+), 613 deletions(-)
>
>
> base-commit: 7d1a16abe4cc5dccd21e3d6262e44433a5885d27