From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BA9E4606A32E for ; Sun, 29 Nov 2026 15:08:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=DUE7QKxW Received: by kanga.kvack.org (Postfix) id C14D56B05B0; Sun, 29 Nov 2026 10:08:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BC57C6B05B1; Sun, 29 Nov 2026 10:08:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB3EC6B05B2; Sun, 29 Nov 2026 10:08:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 836BD6B05B0 for ; Sun, 29 Nov 2026 10:08:08 -0500 (EST) Received: from smtpin22.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 020D21C3E4A for ; Sun, 29 Nov 2026 15:08:07 +0000 (UTC) X-FDA: 85488487056.22.9D52DBA Received: from mta0.migadu.com (out-92.mta0.migadu.com [91.218.175.92]) by imf10.hostedemail.com (Postfix) with ESMTP id 9B67AC000D for ; Sun, 29 Nov 2026 15:08:05 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DUE7QKxW; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.92 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795964886; b=fKRBiZeLA3QoXuKbBbH7kOJBaH0+ZEnQMHYwiaWsHEYwmS4PHZDX5dOmh2+o3Mda5cHpFP z6ARuYMuJzwLq42qSEBN0YMi/ICx0dbFZfou6T5bzgYPwha8Z/PKF2HC/Ket+sKxSa7CDS 4ovar/PKszzB9hv+y89T7v55yOazi3Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795964886; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=qS3X2+AGGpPK/Rn1hLtJ1dSbLtg+zrqfUJ/V/RNIY0w=; b=u0PLWtYQR+AAt7qt8iXrn0t8xeW+MKF3TJ+Ias44NZAIynRcKf3BcrQEIzLSIanpc5qfUk pzQ5S16rdhY/dVNoSAm7GHlqhASF75owLHBi5N7mXYKeIlU3jJRilnT2k2EzLpTMzdSj4P ZTvv+95ogLLBPdcp02/luzP1cXePFmg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DUE7QKxW; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.92 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=nZojjAFIWYDN32Q+xmyX9gW/inh0JXTp/INs7I7WRK8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964884; v=1; x=1796569684; b=DUE7QKxWydEvsun33FLwaou4UkveGjarm9sOaNmkRC5y50s0Qin1EXn3XcLaz3IVoiiDCYs+ A6O86KTkK6O0/vvp5Wq6imbI//3hbMkheX8IX6boJELMwGQqXHuaD1VnuUcGF+iK6ebV73rtGml UyGq6wQXTHvE+jTDrShHprNc= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id b0bf5cc52a2d0d40; Sun, 29 Nov 2026 15:08:03 +0000 X-Mizu-Trace-ID: b0bf5cc52a2d0d40 X-Migadu-Flow: FLOW_OUT From: Baoquan He 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 Subject: [PATCH v7 00/14] mm, swap: extendable swap devices (xswap phase I) Date: Sun, 29 Nov 2026 23:07:41 +0800 Message-ID: <20261129150755.191662-1-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9B67AC000D X-Stat-Signature: z37bdt3ea9kpyus817czneufxwh55brr X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-HE-Tag: 1795964885-393773 X-HE-Meta: U2FsdGVkX18qx3cpakKvKS93TpcKgJNpvessWpTCbpYXPzpNN7HoJ88bqNnCH1b1AhoHSXlk1sjcKwMqBpJzzrG6LqgDdRU3EBSNC/COZeZEtYYeqZJuxx5VPmHZa3xkFQ1ztjfTk4WtOPVbI4ymfS91hKe5peUV7aBm8dnjWVgjMBmVtupVqis+TO9S3z0Sb+xC+rA0oiaysfQ+nu3Ti/oINEhKcETPAthYywc4l1pm7rUsxKf/ud3aFpYak71KrVEpQoSHwpt4LF30PBQ/9Q/i3Q8r0J2LjQ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dTawz/xYclaNoaTi66+itV5A2jGXLlLGTs8Nm+YkKjJWC3zHcUE8grGn0tmv04MplirG1Ktimwx753jSme3plhIFbk0ooZX3GU8/XFKqCiroITv6m2BauZrnZbtlYfUuJDu9NaHXUJ7RHA1aEUgPFgrmW52Sy5kMS3dhZqZb1QHIus7w62h2XwEZuXDcRmCX6ri2thQFl Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Baoquan He 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 #include #include #include #include #include #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