From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org,
corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org,
youngjun.park@lge.com, shikemeng@huaweicloud.com,
yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com,
klarasmodin@gmail.com, linux-kernel@vger.kernel.org,
baoquan.he@linux.dev, Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v7 00/14] mm, swap: extendable swap devices (xswap phase I)
Date: Sun, 29 Nov 2026 23:07:41 +0800 [thread overview]
Message-ID: <20261129150755.191662-1-baoquan.he@linux.dev> (raw)
From: Baoquan He <hebaoquan@kylinos.cn>
A normal swap device has a fixed size, set when it is enabled. This does
not fit zswap well. With zswap the swapped data stays in RAM in
compressed form, so how much swap a machine can really use depends on
how well the workload compresses, not on a number we pick before we know
the workload. Size the device for the worst case and most of it is never
used; size it for the average case and the machine runs out of swap
while zswap still has free room.
xswap is a swap device with no backing file. In this phase its swap
entries hold pages in zswap, and its cluster_info array lives in a
VM_SPARSE area that is mapped a chunk at a time, so the device covers a
large swap address space and costs only the chunks it has actually
used. The device grows as the workload needs it.
This series is the device itself: the sparse cluster array, growth, and
the interfaces that create and destroy one. It is phase I of five; the
physical backend, the swap accounting, the swap-table backend records,
and the runtime shrink build on it.
Runtime shrink is left for a later series. The plan is to run it only
when the device has nothing live: no entry and no backend record points
into the tail, so no swap reader has to be tracked. It would lower
nr_clusters_mapped under si->lock and unmap the pages behind it; cluster
0 stays mapped because it holds the bad-header slot. The details are
still being worked out. Note that without shrink nothing breaks: the
device keeps the peak range, and only the cluster metadata for that range
stays allocated.
Design notes
------------
In this phase the device is only a swap address space. si->max is set
when the device is created and does not move for its lifetime; what
moves is how much of it is mapped. swap_info_struct carries
nr_clusters_max (the swap address space) and nr_clusters_mapped (how
many clusters are mapped, always from cluster 0). get_swap_device()
rejects an entry past the mapped range, and the walkers that scan a
device stop there.
Growth starts when 85% of the mapped range is in use and maps until
65%. The call comes from the cluster allocation path: growing maps pages
into the VM_SPARSE area, and mapping can sleep, so it happens before the
allocation that would otherwise fail. One grow maps at most four chunks,
and one chunk is PAGE_SIZE / sizeof(struct swap_cluster_info) clusters:
85 with the current struct size. Mapping a chunk and putting its
clusters on the free list happen in one si->lock section, and the larger
nr_clusters_mapped is published after the clusters are linked. An
allocator takes a cluster from the free list under si->lock, so it can
never see a cluster that the published range does not cover, and a
reader can never see an entry in one.
A burst of allocators can hold every mapped cluster off the free lists
while usage is still below the threshold. When an allocation finds no
usable cluster, it maps one more chunk and retries once.
Creation and teardown
---------------------
The device has no path. swapon(2) opens the given name as a path first,
and only when that open fails with -ENOENT is the name taken as a
request name; the exact string "xswap" asks for the file-less device. A
real file or directory called "xswap" keeps precedence, a path that
fails for another reason is returned unchanged, and "./xswap" or an
absolute path is a file as usual. This is the same rule swapoff(2) uses,
and the path is copied from userspace once.
There is one xswap device per system; a second create returns -EBUSY.
/proc/swaps shows it as "xswap", so swapoff xswap and swapoff -a both
tear it down. Its counters are in /sys/kernel/debug/xswap/xswap.stats:
type 0
clusters_max 5120
clusters_mapped 85
usage_pages 0
tail_free 84
grows 0
With xswap.max=10G the device has 5120 clusters (with a 2 MB swap
cluster); an idle device maps one chunk of 85 clusters and the tail scan
leaves 84 of them free.
Hibernation device discovery skips xswap devices: there is no block
device to resume from.
Configuration
-------------
CONFIG_XSWAP depends on SWAP, 64BIT and ZSWAP. CONFIG_XSWAP_DEFAULT_MAX
sets the default size of a new device; it takes a percentage of RAM, a
size, or "max", and defaults to "200%":
CONFIG_XSWAP_DEFAULT_MAX="200%"
xswap.max= on the kernel command line overrides it:
xswap.max=8G 8 GiB
xswap.max=300% three times RAM
xswap.max=max the largest size the device can address
The address space cannot move once the device exists, so the size is
settled at creation. That is why this is a boot parameter and not a
runtime knob. The default priority is the highest, SWAP_FLAG_PRIO_MASK,
so xswap is tried ahead of an ordinary swap device; a lower priority can
be passed in the swapon(2) flags.
The device needs zswap: without it swapout takes a swap entry and frees
no memory, so creating one fails with -EOPNOTSUPP.
A cgroup that cannot zswap (its zswap limit is zero) is not given an
xswap slot; the allocator skips the xswap devices for that folio.
Limitations
-----------
This phase has no physical backend. If zswap refuses a page the folio
stays in memory, so an xswap-only system can run out of memory once the
zswap pool is full. Keep a real swap device on such systems; phase II
adds the physical backend.
An incompressible page is refused for an xswap entry even when the pool
has room. An xswap entry is not put on the zswap writeback LRU in this
phase, so a page stored uncompressed could never be reclaimed. Phase II
gives xswap a writeback target and the page can then be stored and
written back.
xswap does not allocate large folios in this phase. The allocation walk
stops at the first device for a large folio, and a new xswap device
defaults to the highest priority, so the folio is offered to xswap,
rejected there, and split into base pages; a lower-priority block device
never gets it whole. Creating the xswap device below a block device in
priority avoids the split, at the cost of offering every page to the
block device first. The data is still swapped, in base pages.
The device does not return its unused tail while it is enabled; destroying
it returns the whole range.
When every mapped cluster is held elsewhere, the allocator maps one
more chunk and retries once. If that still finds nothing, the folio
stays in memory; it is not a correctness failure, but under pressure it
can look like a false OOM.
Overcommit
----------
An xswap device advertises its whole swap address space as swap
capacity: si->pages is what /proc/swaps and SwapTotal report, and what
nr_swap_pages uses to decide that anonymous memory can be reclaimed.
The backing is the zswap pool in this phase, and real swap devices in
the later phases; the swap address space is not backing store in either
case, yet it raises the commit limit that __vm_enough_memory() enforces.
Which semantics this should follow is not settled here. Counting the
swap address space lets a workload commit memory it can never back; not
counting it changes what SwapTotal means. swap_info_remove() skips its
commit-limit probe for xswap, so destroying a device does not fail on
headroom the device added and a workload may have committed into, and
real swap devices keep the probe. The limit can then fall below
Committed_AS; new allocations fail until Committed_AS drops. This is
left to a follow-up, together with the rest of the swap accounting.
Testing
-------
QEMU KVM guest, 8G RAM, booted with xswap.max=10G.
The shell's swapon(8) opens the path and reads the swap header itself, so
the util-linux swapon(8) cannot pass the request name through unless it
knows about "xswap"; a matching util-linux change that lets "swapon
xswap" do it will be posted separately. swapoff(8) passes the name to
swapoff(2), so "swapoff xswap" works as is. Without that change, create
the device with a small helper that calls swapon(2) directly:
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#define SWAP_FLAG_PREFER 0x8000
#define SWAP_FLAG_PRIO_MASK 0x7fff
int main(int argc, char **argv)
{
int flags = 0;
/* The request name is never a path; stay out of any cwd file. */
if (chdir("/"))
return 1;
if (argc > 1)
flags = SWAP_FLAG_PREFER |
(atoi(argv[1]) & SWAP_FLAG_PRIO_MASK);
if (syscall(SYS_swapon, "xswap", flags)) {
fprintf(stderr, "swapon(xswap): %s\n", strerror(errno));
return 1;
}
return 0;
}
# echo 1 > /sys/module/zswap/parameters/enabled
# ./xswap_on 100
# awk 'NR == 1 || /xswap/' /proc/swaps
Filename Type Size Used Priority
xswap xswap 10485756 0 100
The functional test reports 30 checks passed and 0 failed; the overcommit
case was run separately with vm.overcommit_memory=2:
- create and read the size; SwapTotal stays fixed and SwapFree stays
in [0, SwapTotal]
- grow past RAM; stop at the device size (the cgroup runs out of room
and the OOM killer takes the workload)
- an incompressible page is refused and stays resident
- a cgroup that cannot zswap gets no xswap slot
- swapoff xswap, swapoff -a, and a recreate at the same size
- 2000 create/destroy cycles
- overcommit: with vm.overcommit_memory=2, 8.15G of RAM and
overcommit_ratio 50 (CommitLimit 14.56G, Committed_AS 4.65G), a
10.49G device and its swapoff both succeed
The whole matrix ran on the 14 patches alone, on top of 23cd68262288;
no later phase was present. In addition:
- swapoff name semantics, 11 cases: a real file keeps precedence, a
dangling symlink reaches the device, a directory is EISDIR, only
the exact name is accepted, a stale name reports ENOENT, -a tears it
down, and destroying with 653,556 kB of live pages keeps the
workload alive
- 2000 create/destroy cycles: no per-cluster state is leaked (largest
slab growth 215 objects; vmap_area's +1565 is the known lazy
vmap_area freeing)
- dmesg is clean; the only unusual line is the memcg OOM kill that the
cgroup gate test asks for
Performance
-----------
The phase I configuration has no physical backend: the swap address space
only names pages that zswap holds, so the capacity is the zswap pool.
The measurement that matches this phase gives the base kernel a zram
swap device and xswap its sparse array, with both keeping every page in
zswap: memory.zswap.writeback=0 on the test cgroup, so nothing is
written back to the device, and THP off. The xswap arm has no other swap
device, so the physical backend added by later phases is not used. On an
x86-64 server (2 nodes, 72 CPUs, 252 GiB), usemem touches 64G in a 48G
cgroup, one thread, interleaved NUMA:
kernel rounds median wall swapout pages pswpout
base 6 151.8 s 6,833,914 0
xswap 5 145.2 s 6,833,856 0
The medians differ by 4.3%, which is below the run-to-run spread (base
131-166 s, xswap 138-161 s): the sparse slot array does not cost more
than the zram slot namespace. This usemem run was taken on the previous
mm-new snapshot (3d27e49eb905); the series in this posting is on
23cd68262288. The pageout and kernel-build runs reported in v6 exercise
the physical backend added by later phases and are not repeated here.
Changes since v6
================
- Rebased onto 23cd68262288 (mm-new, 7.4.0-rc4).
- Patch 1: the struct page array behind a VM_SPARSE area now grows with
the mapped range, to a power-of-two capacity, instead of being sized
for the whole address space on the first grow; if the allocation
fails, the old array is kept. vread_iter() also stops walking the area
when nr_pages is zero. (Sashiko)
- Patch 2: rebased over the current zswap store path; the entry is
initialized on every store and is put on the writeback LRU only when
it can be written back.
- Patch 11: when the allocator cannot find a mapped cluster below the
growth threshold, it maps one more chunk and retries once.
- Patch 13: a folio that cannot zswap no longer installs the device it
picked in the per-CPU cluster cache, and an xswap-only system fails
before walking the allocator.
- The other patches are unchanged.
- The functional matrix, swapoff-name and leak tests were rerun on the
14 patches alone.
Baoquan He (13):
mm/vmalloc: support vmalloc-backed VM_SPARSE areas
mm, swap: refactor free_swap_cluster_info to take swap_info_struct
mm, swap: use a VM_SPARSE cluster array for xswap devices
mm, swap: widen swap_info_struct max/pages to unsigned long
mm, swap: create the xswap device from swapon(2)
mm, swap: refactor swapoff and add xswap_destroy
mm, swap: require zswap for xswap devices
mm, swap: let the command line set the xswap device size
mm, swap: add CONFIG_XSWAP_DEFAULT_MAX
mm, swap: add xswap grow trigger on cluster allocation
mm, swap: add debugfs counters for xswap
mm, swap: do not give an xswap slot to a cgroup that cannot zswap
mm, swap: skip the overcommit probe when removing xswap
Chris Li (1):
mm: xswap support for zswap
.../admin-guide/kernel-parameters.txt | 9 +
include/linux/swap.h | 15 +-
include/linux/vmalloc.h | 2 +
mm/Kconfig | 20 +
mm/page_io.c | 13 +
mm/swap_state.c | 24 +-
mm/swapfile.c | 1078 +++++++++++++++--
mm/vmalloc.c | 171 ++-
mm/zswap.c | 34 +-
9 files changed, 1235 insertions(+), 131 deletions(-)
base-commit: 23cd682622886800b912219e307cf46a99f69c10
--
2.54.0
next reply other threads:[~2026-11-29 15:08 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 15:07 Baoquan He [this message]
2026-11-29 15:07 ` [PATCH v7 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
2026-11-29 15:07 ` [PATCH v7 02/14] mm: xswap support for zswap Baoquan He
2026-11-29 15:07 ` [PATCH v7 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
2026-11-29 15:07 ` [PATCH v7 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
2026-11-29 15:07 ` [PATCH v7 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
2026-11-29 15:07 ` [PATCH v7 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
2026-11-29 15:07 ` [PATCH v7 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
2026-11-29 15:07 ` [PATCH v7 08/14] mm, swap: require zswap for xswap devices Baoquan He
2026-11-29 15:07 ` [PATCH v7 09/14] mm, swap: let the command line set the xswap device size Baoquan He
2026-11-29 15:07 ` [PATCH v7 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
2026-11-29 15:07 ` [PATCH v7 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
2026-11-29 15:07 ` [PATCH v7 12/14] mm, swap: add debugfs counters for xswap Baoquan He
2026-11-29 15:07 ` [PATCH v7 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-11-29 15:07 ` [PATCH v7 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129150755.191662-1-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=kasong@tencent.com \
--cc=klarasmodin@gmail.com \
--cc=kunwu.chan@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=urezki@gmail.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®