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 7A240606A336 for ; Thu, 3 Dec 2026 22:00:18 +0000 (UTC) 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=CjtOGdHX Received: by kanga.kvack.org (Postfix) id 7E8B06B05B2; Thu, 3 Dec 2026 17:00:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7992F6B05B3; Thu, 3 Dec 2026 17:00:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 660E86B05B4; Thu, 3 Dec 2026 17:00:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A9A386B05B2 for ; Thu, 3 Dec 2026 17:00:16 -0500 (EST) Received: from smtpin27.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 07C0012018A for ; Thu, 3 Dec 2026 22:00:15 +0000 (UTC) X-FDA: 85504040790.27.4D80661 Received: from mail-yx1-f54.google.com (mail-yx1-f54.google.com [74.125.224.54]) by imf15.hostedemail.com (Postfix) with ESMTP id 47F71A001B for ; Thu, 3 Dec 2026 22:00:13 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=CjtOGdHX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 74.125.224.54 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335213; b=qEimeGc1eIHAXQYciXzYnl2VqE4mwp17j7OkxAUhts7HI4OyfiBDu+ynt2HrO8iY7wO60B 6mKHAzYW0Rd6IOuBAbSvXbBZqnFPrsqXlQHvpooOA3eAqszKY7W/SGrUXDTiP5tYwLNQ4C EeuaKP2s3omx7fZqYiVgiWh6t3n4eGY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335213; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=BiWdppHdD/QVIHFhgbM6DsMsyoCcE2FevAI2nrylBdc=; b=5aWcsjxXDFwzlmIdrEp36GA2RWa99kKX8B5Veqwg2/DyGsuyzYtNWgj4hxr7Ez3oYDhGLq i9F2IYim074qCqRzotRdwJ5CDuu+X14vw+gcAgXNBextXHowENUH5mO2WegzYT1X6VYLyr jsmz+8wXJTFbuBxwBDlyCMV8SEJ3ejE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=CjtOGdHX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 74.125.224.54 as permitted sender) smtp.mailfrom=nphamcs@gmail.com Received: by mail-yx1-f54.google.com with SMTP id 956f58d0204a3-67ea52372f7so1287548d50.1 for ; Thu, 03 Dec 2026 14:00:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335212; x=1796940012; darn=kvack.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=CjtOGdHXSut/syn7BQeSsBXUTvaHvwYkjeZhCnw57eh6N/ocVTvuY4Xo53QoeftUQX khkVPUUxF7vWoArSE+gnfElexeeQVL0lz/cpJ11dX9hPPdRfnsyoOQwtJwiCeTuBwaPD DBo15DCrABOIWj6pKN8CbItpi23igmV9vubOSMBFpVAPaomsZ+yIZIDre9QXrGDGVLsY 8X3O+dejRhAIsUSDhp6naUSDT6YZnrQJQgUB1Djh+IwBhveREyBwOsucMASQW2+yFfkc 6YBUWUAc1ZIJldbiZIbGz3mqxpv0mVVkaqzo0E2+4dNkG287fOJBlqf5S8G1Vg3zybi0 Jasw== 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=BWzBuuyU3XnIaSg3Ogu62uQfya4ccTCdtf75T/lXa8u1ulAvtvbbBtgdVRbrgD9WjW +utIH2CuZV1OwTSlEUM7/mJ+avl92ZQquXi6xvnvaupgdJD9wJ8ZUQhh3/jRkI7dUYP8 eWglZIw9EyYtKjENyjj6+EO14NWbDbuhuc3HP3/RArpcxr+iHstotH2qPLoUMOaeY31L XccQToZU0s7258K5grNLOzQHl1VDz5AeC/PoTLxM+086t9Nx/l75VD0CaDsvPfj0KBTE xId5cBhsFUskTftfyfNzaDtWO3d8LokVdwgyDYq3N/Byco+Udb1qhtT5wg58eAistnDA D95A== X-Gm-Message-State: APuU4Z/XSQZu/t/4vRSa4IQy0q7CiS4v06V/H3+raSs8RG01bvDmsrIn W8RDv7MmStilCwVUEAxy7gzLZYs3K0Ws6rnK6+JGC9KR9d66fck8ovpZn1TYASyX X-Gm-Gg: AZFdTZc+yFoDMT+YYRZLdfpVsQaMLmeRpHSiaLgTJIolLvzAZVSPD/qwvkwDuqTpvFa Lhui4qIFcCPNMvWthFfazHgXAHQwE/7xhih1sc4bxACsuvF6vwUQBCvTGhQdysbDBwKwuYNWSuK UPt/qNeQwV8BxStvcFINazUOjDYL7+96axUk6Lix104w2nIIUeM2MYJg9hlOWC0bCKb9oCKkzNq EVntOWNoGZ4n3ILmyqpm9XPINxbGrkDcsGGrlrkwlIFJRU3o7+irqKwkOYAIF8wWrJBZemYj3Mu Xf6pUsfyGfdHKZLXskfXJwY+/NmWdN59zxVjkumfqaszMSMJFjgjJ201uqjAbu+msf5Br+RJzvq rkTd+f3KkHDXgBG7XnTNSQadpaVrkThaLFCfut52mS98CB/ypPP0GlX2Tj8VYW7qJzuUuo2gem3 bSU7AqfSQIS1RJsibQqnL6YfceYvPR4mK4pF9sNhwQT7suxCLWdKdILoSjF/4V9u+BTvwGKikjt jDIH2AOGRZXZtLRvYLSOxFE8S5/RhKvob5kG6w/5yER21Os086RFH3Q+Lt1v2Fo96ROIsVJemey KSWbrsLaQTIPa6fF+6vcbTLnXgElARvvMrVxmp8YSc3gU7juwDlmBV7hTNOVRXrFVLrHnG7rmQQ PFIpTmNOIHmuXQCyjie70P8QJDd6NMp9ASuitwLDw9dHBZNbDRuxd4UAGGb4= 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 47F71A001B X-Rspamd-Server: rspam11 X-Stat-Signature: 8rkxj67wg5id8y5hpxmx8c8o4czyymbn X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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-HE-Tag: 1796335213-128926 X-HE-Meta: U2FsdGVkX1+cbXAYNe3YXk4xA1w0SmzBchRH1nQ86U0Y/0N1+PQjJSTCtQT3v4w8KOVLooXwaofvsUezXiFaQh88ahisAspqsxwUGWcCQO3a2sSFOGLKoIrMXakYcDbsmul4cc/3xW36aIvGF677pkZpBeoD1LIINboExg7hgL76t+2lx71E8AvNpOwVM3Xy1qg5H8KAWaw+BaaHddkPchvI1ZcWs7Y6GbfpPKGEbKnT+jOJ6ji+1D4S+//3mdK7q4hJ2OIbAD0iAvGmhdv9+yHUKjj7gGhjRYwTdw8SACoMGuF/ZbF+1+Sfbw15aW3OCVsdY0QTVPcpchRnjHo5eVdfo2e155SUbPN8PAhUQLaq6KHSAG9I3QS8rRCHfsCc3xP8Zk5Uz8rd5OKI9XEZfXXWdUzarNEhOJ2ejY+FkIt+NAZgY8hsrdqpuh8vQXkIcgIvv9f/SYgWkWED5wwb3tnOW/6/n6yyPnHSid2y5aKqEu1NLACCKqiYeObp9Z7DN+dZae2D6glwDA5NKHJVbqQn1JS/Ed7lHeo8+7cyyBrJ6lwdTlGQtk+J9N8SJ6552y1D12fyoO67NonMQuw53KQVLq0mTAgZv+0PAXDmB1HrkRWpMlVvCQ5ixBTEzHvCXTly3o57wbTbbFCOCVUQwJIJq+lThfDEqUnVqhB54QMWt26pyQn5ZyMfngQxKJXDytKR8LF7aIz2Bo+ku7z4jEkavAcRm3wz/DmPcVktMhsiM4pejrt83AfmleYHR8wvGeqelbB0Akh+4lbhn63On0BazsoMi6xdE1pvhHaBjaHMQ9o2QKrhHr6c6QOxH7wo2/nqR6zKo7Ohp3AdNGYfoOfPlM8TcfVsPXKNhivODD3FmXlKSExIH0aVkQMmGklCL/7J/4RsNL2mA5V82+wSW6a+bFVsabteFVvrEZ+Tk6kjSfmqfoRGrycaStcIcjUGW+5XzRc0gM4whkuyjqB Hduu+ijI IywYz+2uXSKxyNlI/aoA90+VjQRLCscM3GOQPymU8Tej5j+iPRrgVCfL0Gv7HIF/Z/vQzcKaLc7m+VnhudHmWimGALMt05DsmsyD+9kmBOUFFbeFp3lzqDLpg3NHmQsawZYqA65HSqAzXUpsu/LoVkCzTYkPzviES7RH254Jg7YwbHKVkmyrFMeT815FHkUCswxjTV0+jU9KIHymtCwU1+vq95TR22pLlmGHO6W9rkM8qTaNCmrY5+6DtSv4KACIyGD4y07EOjYNaXkxTQ7KsEc8grs7tcvXTb14PPyF/MhiIdaZIdBAI9YZCmtWRInvvH/xVaiQARsXaZsDV4XB/Fo0LGKIHNyKeF0lOSQkyuBffY6qVrR/DTCU8hM3Sw8I8lLVe1SZsrkf7QcfvnqhAOKo9uGSOy5ekZhKHkW2ZbnHQgNDj6VfPVQYZ3k5BeBd44TL0CnuzWSrpm09O5wTgWXohxwV8/2HRyMRobuPuchoHncgIHWeSWutAM6okK++3wvAiuYmhxHzATDwkJT/ZWJOzfm+td3jr/WsMCXDg6bferAu1WdOjmCHdvyE24Num5qIa3hmF7N2gjtI2C9JXg5DBTZOu/9rIFDQhZlnnDxoOJ3fguAsx3IU0wJ6zHYOfs3BrClTDa+z4N5yJo8Bbs6GwDabsLouA94gF2L8AIcjW13DrQsJMrPNI8Oc5Oxew6d8wDZ+kOUyzP5trdG4bCiyuJKf6cEjobsNtrWemFgctA1hmPNjmqrmdF1+MoU6XdM3RcPJDmki3IMdseiyoiY7SRXL9q5kLOkce 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 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