From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335213; b=b+V1Wd/YCcv28RFIkilQDZ4jkIKWkbzxrBGrRCOD1Y78lL7qcqx9PUjkPtPxzwPocGcA UcLEn8xP79P6YKW1WtIH0sf3BkvI5/DIpiUn9X7FgjoT9z3MW6pVNFb/91czmq+9FOSN0 v2SaUdJAimze/sHIbZxz2ipJTeKOI/+wbd1HcRE2VbUoU0uVSq1qKeTIH3E3n2FOA4ciL Nr7WRXGGKvtfCnVnr5StvkgoeNrngJ1++vWtMrdPCXMzwuZ4UATgdSu9kPk1e29dlpssj f3eR8gcAytiJiFiy5bmmlstR/vGB5JTPkguAJmdKR4uwvB/W42x+0+TRbszj5TQcQdg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335213; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=BiWdppHdD/QVIHFhgbM6DsMsyoCcE2FevAI2nrylBdc=; b=v4fmCutlZbY0CfBm88dJXeNYcL9Y+zlaWE4McWKam/z67uYneSkVpD7lRqhjjSPO6bdu QmWE8HemnxgabrPXIWQB30MfLOPxegv2yWwWanvqVJAJ8ufmfip43H5mmajSdy1dxT/Ps YD1JZrG8q2kSez8tpbE3SSzJ28NxajiWHUonkfv88AMrMlfRwZz2rmXLwkJYMct7BNeQ2 Vyusadq80u10wxlHFvqzkebhvRyuHmMhNcpi6QfMtbmDmgs5be8O/yklWxD5GiFjJBOG/ 4+xG+jn9n/ec32cmSqxmsG3eolbD3pWePXO6w5DeYZkM9J0d2DvSMRScv2Z3EdusLcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fKgPLDnQ; arc=none smtp.remote-ip=74.125.224.43 Received: from mail-yx1-f43.google.com (mail-yx1-f43.google.com [74.125.224.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C365606A330 for ; Thu, 3 Dec 2026 22:00:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2C365606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fKgPLDnQ Received: by mail-yx1-f43.google.com with SMTP id 956f58d0204a3-67c246c0c60so1219520d50.0 for ; Thu, 03 Dec 2026 14:00:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335212; x=1796940012; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=BiWdppHdD/QVIHFhgbM6DsMsyoCcE2FevAI2nrylBdc=; b=fKgPLDnQwxlseW0U/bQtWY1iqRvrWml1rMQ6QH/WL4mNK1ttt0wz1pHsTNn6+ZlJQe M/Upk/fHjzu+KQQBTW6wQbzTvqmTCl0jMPkmd7Nhv8mEC4HIK78GeGQuYQqFrLGqMI+x iogPA2EExsQQWV9paMCzvduqrG9H3ocPSY87Cjwv0+fvln2e0V8LFNum57/AZn4peYtm 8UnBeJ9Ys3a4c9z1X/i7d5SBAvbkPsfwR1m8QaOYG1MxaqXVkBrtzy6Olm07TZcKmsXc R26F0SoeP5qymny5kbbzt29J+jeh4MFSNR4DFzgxGYh483aSs98JqcpB3EPNOFY3v6GQ HwYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335212; x=1796940012; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=BiWdppHdD/QVIHFhgbM6DsMsyoCcE2FevAI2nrylBdc=; b=aRJAUv5FoMt1Ja8Op/ADgDEdvA9uKZdDmKLvO5ox8W9SQzpd6ceNrj6YAxHf0Pm8JI Ac461NXyCeqekeOO/IFLEDZHS302OG48eEYl/mJh95yah0XkK+91pLL2/NnrfSmHp8jo YCaIUVEnZNSddrs8RQYKUXuqHrGbTe+xsts8X1dNd8NZnKV4AXfXl8TfdAAFoZR2KFUH gUzW+4JuPA00AKjot2Y2pc69BAgUIr6xWVVbm9j06WBaPVuH6mE0tP7PK0HDYjFER8OM e3QG/2uRXH2ALO93/Y+DniQaartEzIphnuPRMQ25Viv24eZYMmA99WTkG1WlpbwS0ztJ HPIA== X-Forwarded-Encrypted: i=1; AL+hErHK30llsK7wEiGL3Oixel+C7RV+m2vd3tQgHNS5GLEujDtYa8DUSNKNQNgtYPMjbzw/5T5VAqE5@vger.kernel.org X-Gm-Message-State: APuU4Z8f5PlKvWKdBSP17CkTIJI8+8Teyfg5Y84247HunuHV7ggIoTR6 83HMk/kdFSJc/74uFgJHLYNQfajMH9dLD2oAhZeEsC5jqIAYmr5JDgu1 X-Gm-Gg: AZFdTZcHKrzWDN3A1E8v31i6cDR5XTrXKPbxDNtxb+Flvhx6HLsKAqidO5KUZrp+/hC 6mmwiV0TcKS/VDqgjtLg8V3j1K8/JfROUK5yZs0Pa4F3eqtxEqj3L6xJpgkXao3iGQerWxjiGXS VbaH6QxGQCma+xi9/NSCN+MUxE8BHbU23FbXTJMAI/RV1aQlOKyJwLlT3ygr1w8nEqCMrMfckM5 AcKXufPT1VslCDsiYfjJfZ1DciILpuFVu6V6MM8ppIfENv007+md7zITiBJiAekJPB/qSinx4ag IrwbLzxe6RZPhZQx2hd+hrhWUg5JqSJZaET9R3g6RONjGX76s8fuNgGrGrCH6se55mi3X7QO76/ MClrHqFux44bRYdz2eQm8pMrldROz8B8ZvboIRTyvptvlQjuElHdQ+pgky0RY04l+7aWwpDMFYY gm3jyXS2nGDk5cr+gUiorfgE8Qc1Myd9THMd2tJaNYQvcWnZuiCk2gvK1FNg9oGUEIp99c7NEJa 5TpUhXmFyRynbzriqRok5py5PE0O5nkNrgmYceCImloJKcm9q2gMMvt5c7rzbltLqfJCVgR35/r h4qo4Q8fa+YhgXj7CpHLMv2Dyh8kZ1jXgFD4UyVgHfP/u7QeFs8uJ4SikkAQwWin4wzXPjfzF+w 8angQIiktRf5iVRne0gsUCbQhlwQmpfWCJj9M6J2j1kITRwJneI7slSZFGP0= X-Received: by 2002:a05:690e:1c09:b0:677:b7c3:12c3 with SMTP id 956f58d0204a3-67f6ec483bfmr52238d50.29.1796335211976; Thu, 03 Dec 2026 14:00:11 -0800 (PST) Received: from localhost ([2a03:2880:10ff:72::]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0cd1ba08sm194647d50.14.2026.12.03.14.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:11 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 00/20] mm: mandatory virtual swap Date: Thu, 3 Dec 2026 13:59:50 -0800 Message-ID: <20261203220010.1302207-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Changelog * v6 [v6] -> v7 * Append five patches that make vswap mandatory after the optional implementation. Keep all swap counts, cache entries and ordinary cgroup ownership at the virtual slot. * The mandatory conversion removes 437 lines net across 19 files. Calls to swap_is_vswap() or is_vswap_entry() fall from 61 to 25, excluding definitions and assertions. Physical cluster metadata shrinks by 16 bytes on x86-64 with CONFIG_MEMCG. * Rebase onto mm-unstable. Keep the cluster XArray in this series; the v7-compatible VM_SPARSE variant is evaluated separately. * Restructure the patches for review. Keep I/O preparation ahead of virtual-entry storage and allocation, and split allocation from the zswap/zero backends. * Split physical backing into allocator, backing-lookup, swapoff preparation and final wiring patches. Register the physical backend with the virtual layer. * Walk PTEs and shmem for physical swapoff. Extend cache-only backing reclaim to allocation-range scans. * Fix cgroup v1 accounting and failed zswap writeback rollback. Register the vswap statistic only in swap builds. * Enable 32-bit with a one-word vtable. * Expand the zswap shrinker benchmarks and add MariaDB read/write results. * v5 [v5] -> v6 * Added a new cgroup memory.stats counter for vswap slots (patch 11). * Fixed some small bugs: incorrect swap-allocation failure classification, nofs check in phys swap fallback path, etc. * Rebased onto a newer mm-unstable. * v4 [v4] -> v5 * OVERCOMMIT_GUESS now allows for 3xRAM margin when vswap is enabled, to take into account swapfile-less zswap usage (proposed by Johannes Weiner). * Limit vswap swapfile size to 8TB and drop the last patch, to avoid memcg private id refcnt saturation. * More assorted cleanups and fixes: anon swappability check, etc. * RFC: Replace the xarray with a new data structure (vmalloc array) (new patch 11). * Rebased onto mm-unstable. * v3 [v3] -> v4 * Replaced the runtime sysctl with a cmdline param, and remove CONFIG_VSWAP (suggested by Johannes Weiner). CONFIG_VSWAP_DEFAULT_ON now only gives the default value of the vswap cmdline parameter. * Refactor swap-related memcg operations into composable building blocks: reference acquisitions, charging, etc. (patch 8, suggested by Johannes Weiner). * Fixed vtable UAF bug reported by syzbot and Kunwu. * Rebased onto mm-unstable (minimal merge conflicts). * Re-run benchmarks (no signal change). * v2 [v2] -> v3: * Rebased onto current mm-unstable. * Add a runtime vm.vswap_enabled sysctl and CONFIG_VSWAP_DEFAULT_ON to gate vswap allocation. * More cleanups and small bug fixes. * Split THP swapin enablement into its own patch (patch 5). * Add production workload benchmark results, and drop RFC tag. * v1 [v1] -> v2: * Rebased to a newer mm-unstable tip. * Fix a bunch of assorted issues (incorrect zswap store failure rollback, vswap_init() failure handling, rmap-encoding collision, etc.) and clean up the code (rename a bunch of functions to more closely follow existing patterns, etc.). * Some more code clean up and simplification: some renamings to more closely follow existing patterns, move vswap backing check to __swap_cache_add_check, store zero state in the swap_table for vswap entries, etc.. Many of these are proposed by Kairui Song in [1]. * Defer memcg_table allocation on physical clusters until the first vswap-backing slot installs. Saves ~512 bytes per physical cluster that only serves vswap-backing slots (this is the new patch 8). * Widen swap_info_struct->max and ->pages (and the swapoff unuse-path index) so vswap supports ~8 PB of swap space (this is the new patch 9). * Split the physical-swap-backend patch into three for reviewability: the core backend (patch 3), zswap writeback to physical swap (patch 4), and reclaim of cache-only physical slots (patch 5). No functional change. * Add kerneldoc for the vswap API. * Add some benchmark numbers for zswap case. I. Context and Motivation ========================= Currently, when an anon page is swapped out, a slot in a backing swap device is allocated and stored in the page table entries that refer to the original page. This slot is also used as the "key" to find the swapped out content, as well as the index to swap data structures, such as the swap cache, or the swap cgroup mapping. Tying a swap entry to its backing slot in this way is performant and efficient when swap is purely just disk space, and swapoff is rare. However, the advent of many swap optimizations has exposed major drawbacks of this design. The first problem is that we occupy a physical slot in the swap space, even for pages that are NEVER expected to hit the disk: pages compressed and stored in the zswap pool, zero-filled pages, or pages rejected by both of these optimizations when zswap writeback is disabled. This is arguably the central shortcoming of zswap: * Resource-wise, it is hugely wasteful in terms of disk usage. At Meta, we size swapfile in the order of 25-50% of host RAM, depending on flash availability. This is a lot of flash for a fleet of our size, and with universal zswap enablement, most of this is wasted for zswap entries. * In deployments when no disk space can be afforded for swap (such as mobile and embedded devices), users cannot adopt zswap, and are forced to use zram. This is confusing for users, and creates extra burdens for developers, having to develop and maintain similar features for two separate swap backends (writeback, cgroup charging, THP support, etc.). For instance, see the discussion in [2]. * Tying zswap (and more generally, other in-memory swap backends) to the current physical swapfile infrastructure makes zswap implicitly statically sized. This does not make sense, as unlike disk swap, in which we consume a limited resource (disk space or swapfile space) to save another resource (memory), zswap consumes the same resource it is saving (memory). The more we zswap, the more memory we have available, not less. We are not rationing a limited resource when we limit the size of the zswap pool, but rather we are capping the resource (memory) saving potential of zswap. Under memory pressure, using more zswap is almost always better than the alternative (disk IOs, or even worse, OOMs), and dynamically sizing the zswap pool on demand allows the system to flexibly respond to these precarious scenarios. * Operationally, static provisioning the swapfile for zswap poses significant challenges, because the sysadmin has to prescribe how much swap is needed a priori, for each combination of (memory size x disk space x workload usage). It is even more complicated when we take into account the variance of memory compression, which changes the reclaim dynamics (and as a result, swap space size requirement). The problem is further exacerbated for users who rely on swap utilization (and exhaustion) as an OOM signal. All of these factors make it very difficult to configure the swapfile for zswap: too small of a swapfile and we risk preventable OOMs and limit the memory saving potentials of zswap; too big of a swapfile and we waste disk space and memory due to swap metadata overhead. This dilemma becomes more drastic in high memory systems, which can have up to TBs worth of memory. Swap virtualization is the answer to these issues, with three properties: 1. Decoupled backends. For zswap in particular, this means we eliminate the unused storage space, and allows zswap to be used in systems that do not have enough storage capacity for physical swap (without having to resort to silly hacks). Zero-filled swap pages and swap-cache-only folios also benefit here. 2. Dynamic swap space. Since virtual swap is not tied to any physical resource, we can make it effectively infinite and dynamically grow it on demand. This massively simplifies operational provisioning, and increases the utilization of compressed swap backends (zswap). Dynamicity also reduces overhead on unused swap capacity. 3. Efficient backend transfer. The virtualization scheme should not introduce PTE/rmap walking overhead for backend transfer. This is crucial for systems that want to support multiple swap backends in a tiering fashion (for e.g zswap -> disk swap). Deferring disk space allocation also gives us more opportunities to batch writeback I/O. For more historical contexts and references, please take a look at the cover letter of the older vswap submissions ([3] and [v2]). II. Design ========== Patches 1-15 build the optional vswap implementation, roughly equivalent to v6 in functionality. Patches 16-20 then make it the common layer for ordinary swapping. With CONFIG_SWAP enabled, vswap is initialized at boot. Anonymous and shmem swapping allocate virtual slots, regardless of the eventual backend. These swap entries can subsequently acquire backend on-demand, such as a zswap entry, or a slot on a physical swap device (as a fallback option or at zswap writeback time). We repurpose much of the existing swap_table infrastructure and swapfile allocator for vswap, with two notable differences: * Clusters are dynamically allocated on demand and managed through an xarray. This in turn allows us to avoid static provisioning and let swap space grow dynamically. * Each virtual cluster has a virtual_table that stores the backend information of the entries in the cluster (see below). The preparatory patches separate shared cluster machinery, I/O entry plumbing, virtual backend storage and virtual slot allocation. The physical-backend patches prepare reservation, backing lookup and swapoff matching before connecting the physical backend to vswap. The remaining patches add large swapin, zswap writeback, cache-only backing reclaim, counters and physical swap charging. Virtual slots can hold compressed or zero-filled data without reserving disk space; physical backing is acquired when it is needed. Diagrams: Virtual swap entry PTE swap_cluster_info_dynamic vswap_entry +---------------------------------+ (swp_entry_t) ------>| swap_cluster_info (ci) | | +----------------------------+ | | | swap_table | | | | PFN / Shadow | | | | count,flags,order | | | | lock, list | | | +----------------------------+ | | | | memcg_table (cgroup ownership) | | extend_table (large counts) | | virtual_table | | +----------------------------+ | | | NONE | | | | SWAPFILE(swp_entry_t) | | | | ZSWAP(struct zswap_entry*) | | | +----------------------------+ | +---------------------------------+ | | SWAPFILE resolves to v PHYSICAL CLUSTER (swap_cluster_info) +--------------------------+ | swap_table per-slot: | | NULL - free | | Vswap - vswap rmap | | Bad - unusable | | | | Vswap-backing slot: | | Vswap(C|swp_entry_t) | | rmap back to vswap | +--------------------------+ struct swap_cluster_info_dynamic { struct swap_cluster_info ci; /* swap_table, lock, etc. */ unsigned int index; /* position in xarray */ struct rcu_head rcu; /* kfree_rcu deferred free */ atomic_long_t *virtual_table; /* backend info, one word/slot */ unsigned int *extend_table; /* large swap counts */ #ifdef CONFIG_MEMCG struct swap_memcg_table *memcg_table; #endif #if !SWAP_TABLE_HAS_ZEROFLAG unsigned long *zero_bitmap; #endif }; Each vswap cluster (swap_cluster_info_dynamic) extends the classic swap_cluster_info struct with a virtual_table array that stores the backend information of each virtual swap entry in one word. The low bit distinguishes physical swap entries from aligned zswap pointers. NONE: 0 no separate backend ZSWAP: zswap_entry pointer compressed in zswap, low bit clear SWAPFILE: (swp_entry_t.val << 1) | 1 on a physical swapfile Zero-filled pages use the swap_table zero flag; cache folios use its PFN entries. Other design highlights: * For vswap, we have merged the zswap xarray tree with the swapfile-level clusters. The vtable holds the zswap pointer. * Backend transitions in the virtual_table are synchronized through the swap cache and the folio lock - the same mechanism that already serializes ordinary swap operations (swapin, swapout, zswap writeback, swap cache reclaim). IOW, we can only assume that the backend of a vswap entry is stable through swap cache/folio lock. Looking at the backend without this should be done at best for optimization purposes, as there is no guarantee that the backend will not change under the observer. * Vswap-tagged swap_table entries on physical clusters provide the rmap (physical -> virtual) lookup. Physical swapoff walks PTEs and shmem entries and checks their current backing under the folio lock. * Virtual swap entries count towards memory.swap.current only when backed by physical swap slots [4]. In cgroup v1, vswap entries are still accounted towards the memsw counter. III. Mandatory vswap ==================== In the v6 version, vswap keeps separate virtual and direct physical swap paths. At the last vswap sync, we discussed making vswap mandatory to simplify the implementation. Patches 16-20 apply this conversion after the optional vswap implementation. Anonymous and shmem swapping use virtual entries throughout, with swap counts, swapcache and cgroup ownership held at the virtual slot. Physical swap supplies disk backing and raw hibernation slots. Its swap table records reverse mappings to virtual slots, so backing reclaim can find the owner directly. Compressed zswap entries live in the vtable, removing per-device zswap trees. Initialize vswap automatically when CONFIG_SWAP is enabled, and move virtual-only fields out of physical clusters. IV. Benchmarks ============== Highlights ---------- The largest gain is with zswap and its shrinker. Host usemem throughput rises about 47% and wall time falls about 32%; kernel build wall time falls 3.6%. With zswap, usemem free time falls about 10-11%. MariaDB TPS rises 11.7% with the shrinker and 6.8% without it. Mean p95 latency falls 12.0% and 6.2%, respectively. Overall wall-time and throughput regressions are mostly a few percent in tight-memory microbenchmarks. Some separate costs are larger: cgrouped SSD usemem free time rises 3.8% and host-zram free time rises 7.0%. These overheads are a tradeoff for simplification: ordinary swapping shares the virtual layer, removing separate direct physical paths. Benchmark setup --------------- The usemem and kernel-build hosts have 52 logical CPUs and use the traditional LRU. Zswap uses zstd and a 64 GiB NVMe swapfile. Pure SSD and zram run with zswap disabled; zram uses zstd. Host usemem uses 56 GiB with one worker and 32.72 GiB usable RAM. Cgrouped usemem uses 20 GiB with one worker, 62.25 GiB usable RAM, memory.max=14 GiB and memory.swap.max=12 GiB. Kernel build uses 52 jobs, 62.25 GiB usable RAM and memory.max=3 GiB. MariaDB uses ten one-million-row tables, eight sysbench read/write threads, a 512 MiB pool and a 256 MiB shared cgroup. Each run has a 60-second warmup and a 300-second measurement. Usemem uses 16 ordinary rounds per kernel, kernel build uses 10, and MariaDB uses three. MariaDB physical swap capacity is about 2 GiB. Each kernel comparison uses a baseline measured on the same host. Benchmark results ----------------- Benchmark numbers (mean +/- standard deviation). Zswap ----- 56 GiB usemem Baseline vswap Throughput (KB/s) 389526 +/- 21687 382770 +/- 23967 Free (ms) 7355.5 +/- 108.2 6591.3 +/- 117.1 Wall (s) 177.50 +/- 10.41 179.89 +/- 11.94 System (s) 126.54 +/- 10.58 127.84 +/- 12.22 User (s) 45.56 +/- 0.24 46.21 +/- 0.29 20 GiB cgrouped usemem Baseline vswap Throughput (KB/s) 320452 +/- 1311 318941 +/- 2804 Free (ms) 1071.0 +/- 30.9 952.1 +/- 31.8 Wall (s) 74.70 +/- 0.30 74.93 +/- 0.67 System (s) 66.44 +/- 0.28 66.60 +/- 0.66 User (s) 8.01 +/- 0.07 8.08 +/- 0.10 Kernel build Baseline vswap Wall (s) 165.52 +/- 0.71 165.50 +/- 0.46 System (s) 718.01 +/- 25.68 722.80 +/- 19.18 User (s) 5108.85 +/- 2.01 5109.38 +/- 2.53 MariaDB read/write Baseline vswap TPS 88.06 +/- 10.14 94.08 +/- 9.02 p95 (ms) 244.62 +/- 35.55 229.43 +/- 21.53 MariaDB TPS rises 6.8% and mean p95 latency falls 6.2%. With zswap, host and cgrouped usemem free time falls by 10.4% and 11.1%, respectively. Earlier prototyping [v3] isolated the main free-time gain to merging zswap metadata into swap clusters and eliminating its separate entry XArray. Native zswap invalidation performs a tree walk for each entry. Vswap instead looks up one 512-slot cluster and frees entries through a flat vtable scan. Zswap with shrinker ------------------- 56 GiB usemem Baseline vswap Throughput (KB/s) 177258 +/- 8829 260196 +/- 7887 Free (ms) 4629.9 +/- 310.2 3842.2 +/- 131.0 Wall (s) 378.15 +/- 17.82 258.00 +/- 8.19 System (s) 150.82 +/- 6.14 163.83 +/- 14.57 User (s) 44.21 +/- 0.20 45.34 +/- 0.21 20 GiB cgrouped usemem Baseline vswap Throughput (KB/s) 296280 +/- 2296 293604 +/- 3179 Free (ms) 871.0 +/- 31.2 822.0 +/- 21.6 Wall (s) 80.52 +/- 0.62 81.20 +/- 0.88 System (s) 72.16 +/- 0.63 72.75 +/- 0.87 User (s) 8.03 +/- 0.08 8.12 +/- 0.07 Kernel build Baseline vswap Wall (s) 175.53 +/- 1.85 169.22 +/- 2.26 System (s) 686.57 +/- 32.78 702.54 +/- 38.29 User (s) 5091.57 +/- 3.90 5099.73 +/- 3.44 MariaDB read/write Baseline vswap TPS 383.74 +/- 22.17 428.59 +/- 14.09 p95 (ms) 46.83 +/- 5.38 41.20 +/- 3.44 MariaDB TPS rises 11.7% and mean p95 latency falls 12.0%. With zswap and its shrinker, host usemem throughput rises 46.8% and wall time falls 31.8%. Kernel build wall time falls 3.6%. The performance improvements likely come from more efficient zswap writeback I/O. In the discussion of Alexandre Ghiti's writeback batching patches [5], I suggested that deferring physical slot allocation could make writes more contiguous. Baseline reserves physical slots during initial swapout. Before writeback, some pages refault or are freed, allowing their slots to be released. This leaves holes between the entries still in zswap. The shrinker then visits those entries in LRU order, which differs from their original allocation order. Writes therefore often refer to separated physical slots and cannot be merged. Vswap defers physical slot allocation until writeback. Pages selected consecutively by the shrinker can receive adjacent slots, giving the existing I/O batcher more adjacent sectors to merge. Two traced builds per kernel of the earlier optional vswap version show the batching effect: Baseline vswap Mean write (KiB) 4.1 213.8 Requests per GiB ~256000 ~4900 SSD --- 56 GiB usemem Baseline vswap Throughput (KB/s) 357577 +/- 11258 371082 +/- 18030 Free (ms) 2594.8 +/- 211.1 2641.6 +/- 72.6 Wall (s) 187.70 +/- 6.02 181.08 +/- 8.32 System (s) 39.38 +/- 1.35 40.43 +/- 1.44 User (s) 45.70 +/- 0.60 46.99 +/- 0.53 20 GiB cgrouped usemem Baseline vswap Throughput (KB/s) 1131464 +/- 24242 1100676 +/- 26915 Free (ms) 596.3 +/- 15.2 619.1 +/- 24.0 Wall (s) 21.47 +/- 0.45 22.08 +/- 0.52 System (s) 12.72 +/- 0.49 13.09 +/- 0.78 User (s) 8.08 +/- 0.11 8.16 +/- 0.06 Kernel build Baseline vswap Wall (s) 178.75 +/- 3.15 179.46 +/- 2.44 System (s) 538.65 +/- 12.73 550.06 +/- 13.78 User (s) 5041.56 +/- 17.51 5039.50 +/- 10.76 MariaDB read/write Baseline vswap TPS 734.24 +/- 21.67 729.94 +/- 24.78 p95 (ms) 22.17 +/- 1.17 22.05 +/- 1.40 MariaDB throughput and p95 latency are close to baseline. Host SSD usemem throughput rises 3.8% and wall time falls 3.5%. Cgrouped usemem wall time rises 2.8% and free time rises 3.8%; kernel build wall time is close to baseline. Zram (zstd) ----------- 56 GiB usemem Baseline vswap Throughput (KiB/s) 348324 +/- 9720 346386 +/- 10348 Free (ms) 4437.9 +/- 63.0 4747.3 +/- 104.0 Wall (s) 194.24 +/- 5.61 195.63 +/- 5.95 System (s) 143.62 +/- 5.71 144.27 +/- 6.11 User (s) 45.39 +/- 0.22 46.07 +/- 0.22 20 GiB cgrouped usemem Baseline vswap Throughput (KiB/s) 391511 +/- 4501 384734 +/- 4919 Free (ms) 786.4 +/- 29.4 793.2 +/- 29.9 Wall (s) 61.06 +/- 0.72 62.13 +/- 0.80 System (s) 52.88 +/- 0.72 53.82 +/- 0.82 User (s) 7.98 +/- 0.07 8.09 +/- 0.13 Kernel build Baseline vswap Wall (s) 164.35 +/- 0.45 164.72 +/- 0.61 System (s) 649.15 +/- 24.58 658.50 +/- 30.36 User (s) 5115.35 +/- 1.66 5115.22 +/- 2.41 MariaDB read/write Baseline vswap TPS 817.97 +/- 25.41 815.76 +/- 16.05 p95 (ms) 22.74 +/- 1.90 22.71 +/- 1.23 With zram using zstd, host and cgrouped usemem wall time rises 0.7% and 1.8%, respectively. Host usemem free time rises 7.0%. Kernel-build wall time is close to baseline. MariaDB throughput and p95 latency are also close to baseline. V. References ============= [v1]: https://lore.kernel.org/all/20260528212955.1912856-1-nphamcs@gmail.com/ [v2]: https://lore.kernel.org/all/20260612193738.2183968-1-nphamcs@gmail.com/ [v3]: https://lore.kernel.org/all/20260806184254.3790858-1-nphamcs@gmail.com/ [v4]: https://lore.kernel.org/all/20260825153238.2695446-1-nphamcs@gmail.com/ [v5]: https://lore.kernel.org/all/20260918180241.3424851-1-nphamcs@gmail.com/ [v6]: https://lore.kernel.org/all/20261021060323.553433-1-nphamcs@gmail.com/ [1]: https://lore.kernel.org/all/CAMgjq7BhOn48xEyC=2j837R7qddfjeBVHMiRqdx8no4ZEBpBLg@mail.gmail.com/ [2]: https://lore.kernel.org/all/Zqe_Nab-Df1CN7iW@infradead.org/ [3]: https://lore.kernel.org/all/20260505153854.1612033-1-nphamcs@gmail.com/ [4]: https://lore.kernel.org/linux-mm/CAKEwX=P4syV38jAVCWq198r2OHXXc=xA-fx1dk6+qYef6yzxWQ@mail.gmail.com/ [5]: https://lore.kernel.org/all/CAKEwX=Mr=9hbGo+L+2418auHhA8EUAQAKYxNhXOXP0UZ_vHAtw@mail.gmail.com/ Nhat Pham (20): mm/swap: prepare dynamic clusters for vswap mm, swap: prepare the swap IO path for vswap mm/swap: prepare zswap and zero storage for virtual entries mm/swap: allocate virtual swap slots mm/swap: return physical entries from the swap allocator mm/swap: resolve physical backing for reads and device flags mm/swap: prepare physical swapoff for virtual backing mm/swap: support physical swap as a vswap backend mm, swap: enable THP swapin for vswap entries mm, swap: write back vswap zswap entries to physical swap mm, swap: reclaim physical slots backing cache-only vswap entries mm, swap: add debugfs counters for vswap mm, swap: only charge physical swap entries mm, swap: defer memcg_table allocation for physical swap clusters mm, swap: add vmstat and memcg counters for vswap mm/swap: make vswap mandatory mm/swap: drop direct physical swapcache handling mm/zswap: store compressed entries only in the vtable mm/swap: reclaim physical backing through reverse mappings mm/swap: move virtual swap metadata into vswap clusters .../admin-guide/cgroup-v1/memcg_test.rst | 5 +- .../admin-guide/cgroup-v1/memory.rst | 1 + Documentation/admin-guide/cgroup-v2.rst | 54 +- Documentation/admin-guide/mm/zswap.rst | 12 +- MAINTAINERS | 1 + include/linux/memcontrol.h | 13 + include/linux/swap.h | 79 +- include/linux/swap_ops.h | 11 +- include/linux/vmstat.h | 3 + include/linux/zswap.h | 15 +- mm/memcontrol-v1.c | 35 +- mm/memcontrol-v1.h | 19 + mm/memcontrol.c | 265 +++- mm/memory.c | 47 +- mm/page_io.c | 122 +- mm/shmem.c | 71 +- mm/swap.h | 94 +- mm/swap_state.c | 114 +- mm/swap_table.h | 133 +- mm/swapfile.c | 1280 ++++++++++++++--- mm/util.c | 22 +- mm/vmscan.c | 47 +- mm/vmstat.c | 6 + mm/vswap.h | 429 ++++++ mm/workingset.c | 2 +- mm/zswap.c | 249 ++-- 26 files changed, 2451 insertions(+), 678 deletions(-) create mode 100644 mm/vswap.h base-commit: e685fdf67323bade713c5f81015eda5094b3261b -- 2.53.1-Meta