Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65E4CC369A2 for ; Tue, 8 Apr 2025 15:00:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5F35280002; Tue, 8 Apr 2025 11:00:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE6E9280001; Tue, 8 Apr 2025 11:00:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B61C2280002; Tue, 8 Apr 2025 11:00:31 -0400 (EDT) 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 9188D280001 for ; Tue, 8 Apr 2025 11:00:31 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E5B3EAE450 for ; Tue, 8 Apr 2025 15:00:32 +0000 (UTC) X-FDA: 83311187904.19.92E6CCA Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) by imf11.hostedemail.com (Postfix) with ESMTP id BE6C840017 for ; Tue, 8 Apr 2025 15:00:30 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=xSqOQbZc; spf=pass (imf11.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.182 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744124431; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oQd2HuYVMB2uA19lAP4G3r1tUf16qS+73yi0Zu+QUgU=; b=ikZRpjROmnSE1cXrtPax9sqBxNERPXbDEpNHatuduyKqdMmfOtK+xDdmQXQat42z32MVBs 4vt7+yLRfUNFEcoMCVBAHmgSES7CvXl+oTj3A5xGVwphlVXS181pu7qBXr7Bit9S8tAPR5 sI+3+99tOQW8mOBt4d1yiMyyLXQa4IE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=xSqOQbZc; spf=pass (imf11.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.182 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744124431; a=rsa-sha256; cv=none; b=EEbS7aVLJIDIZSbuZ8oleJqkFB287YHrF40lxUPOb/SVxuk16YrFrgderg71DafRh//2RM xr+oQ07jAfL+NyoV9jfyxTodLVfefl6CqvyCIKe+T19ZEUAO0z6yYkSbIHeUg2RAV1l5y5 HKJTPlna7jltpl2G+4jFdVTcV4dGBTw= Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-7c58974ed57so584813985a.2 for ; Tue, 08 Apr 2025 08:00:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1744124430; x=1744729230; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=oQd2HuYVMB2uA19lAP4G3r1tUf16qS+73yi0Zu+QUgU=; b=xSqOQbZcBeVqMlJMye0BlKkGlN99h419Kz48Am6vN/qgHEaDnodTT/wyx+Ss0+4ATt IOYSKNcUaK87Jtfn6jiPeYZX15gv4rPvEF1zjMRSd6NIQEHFKRVB5HE3cH+iZmdyvAGv EXtmJyolV67Mbj44l6/p6hXBVdDjinXYXk/dZdocNJD1Glssc9Oz/xD4HMwTWzvOd4uC 8Axs6cgve6kpM5t7HHlYMPTY0ieBg3OgsnYyRHOsX2RSefIdrHZ5Hb9hh+jpq9KN294M ylMEe7KlYg0asiSC+VrS6W2ebPr4B6MhejL13ODdlRIpNInVKCZ87YPh7hYAm7BTUx2t DQ1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744124430; x=1744729230; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=oQd2HuYVMB2uA19lAP4G3r1tUf16qS+73yi0Zu+QUgU=; b=cW9hc2TTrRhgo/+RkYhkRJZSeu/MQdbsdlbrk477Y40aYPMkQQcVh3+K5lrBeKfvks 6kgHP3VN9iMtvBMjrN2Di3+QJrsmtz5CbFPR8FMQnsTSFYokAgEwsMBWr3WtQDbuJY5Z ihQJEbJLDbNSywaVOx+N3CgPZKZvHOltmf+9gcGLcscxyw60NE+C/J7K44zeG0CUfx8I sUFzePz8J9xqCO/QeoI+c3PWHW6eaCer74WdJiUN0uF6V+BguRp9r4PpthBcRmGV6AC3 YenicH/kevDX7hDFD8METlzcFn6YDUyKy+FJJffKr+XfrxBIYwdklDTxJ51BGbJRozCM kWiw== X-Gm-Message-State: AOJu0YxePybhtR858z+GBruXbNHo5hyERuksx4H7GdQhdGXwbdtpS3DR H2ZCvZG7N8GcLhb1DBot+UuYxmCCfYiQNaHLLfY9NNiDxQhrCTMwHhWA4qYupj0= X-Gm-Gg: ASbGncsFa4bh+AY8BtX3ky+LEGI6TT2JYgvw1m/mkNZ/QB5ZATPhGzrDUuJkBaEpQrD A/IMqVLS0FyOJoZIoZ4aRf0fF+urRBquPMKF3VGAoIS+vD/Td+YOjsw39NzSBI2Cwdcrg3ZPPMd L+NrEanp/05dc5Y7meAD82NywaRGajGklRfq3SjATpKMRvCg+Ly/o/r+hqfjwGndjeLiQIPiVwL ObIbGTfQZTGu/OGJSxqpIFVvqry7vyVX0/oKiPcGAGueaXKf93u3mstPEvQ+iQvFnpfxDS3guZZ BQ9czrujh06C6H+MG4bZtUYD0p6Z6XeN2fPJoubGJs4= X-Google-Smtp-Source: AGHT+IHoaiiimay/jkn42/i4mLcJ/qG34wLV5JJdRiDmu8nloHtzgfEmtFoYsMdVM1i1HbahBBGarQ== X-Received: by 2002:a05:620a:d8c:b0:7c5:5d9b:b617 with SMTP id af79cd13be357-7c774d531e4mr2153900785a.23.1744124424489; Tue, 08 Apr 2025 08:00:24 -0700 (PDT) Received: from localhost ([2603:7000:c01:2716:365a:60ff:fe62:ff29]) by smtp.gmail.com with UTF8SMTPSA id af79cd13be357-7c76e7354a3sm770574985a.10.2025.04.08.08.00.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Apr 2025 08:00:23 -0700 (PDT) Date: Tue, 8 Apr 2025 11:00:19 -0400 From: Johannes Weiner To: Nhat Pham Cc: linux-mm@kvack.org, akpm@linux-foundation.org, hughd@google.com, yosry.ahmed@linux.dev, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, len.brown@intel.com, chengming.zhou@linux.dev, kasong@tencent.com, chrisl@kernel.org, huang.ying.caritas@gmail.com, ryan.roberts@arm.com, viro@zeniv.linux.org.uk, baohua@kernel.org, osalvador@suse.de, lorenzo.stoakes@oracle.com, christophe.leroy@csgroup.eu, pavel@kernel.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-pm@vger.kernel.org Subject: Re: [RFC PATCH 04/14] mm: swap: swap cache support for virtualized swap Message-ID: <20250408150019.GB816@cmpxchg.org> References: <20250407234223.1059191-1-nphamcs@gmail.com> <20250407234223.1059191-5-nphamcs@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250407234223.1059191-5-nphamcs@gmail.com> X-Rspamd-Queue-Id: BE6C840017 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ra9pim5nn3k9purnjwhw67tfhoqqf1hz X-HE-Tag: 1744124430-579071 X-HE-Meta: U2FsdGVkX19aVJKyw3ox4M4G2TIguX0L5WwnMtXWkTevoOQjStYPMBCmQjJq+sBxN73xCFhhzgiHICY7o9Z2sAlXc3Zy3xppmmOzpObA4t+UXZTDR3VCLu5Du6vkPmJgdBRZ48wforwZ/VOzIIS6xlc2y6s8pdnwl7S8QRkKwkNy7kiW6feJcpWteqU7hOuH6W4oTIcj6PPzF79JWH9F0HTKNPxpbasTo/vcmIqWIT/zmmS0qsdmQ40TR5a/RXP5pNluHX0Xwqy1gZEVs/ZrVAzgawH27t7xxk/tI0ipPLkX9RKIa0KA04yv8LP3derl2n9PuJ+deBIVdBoPlEzMY5rtmfu2AEF2zKGcvYYU8HwmEf49VFKpRgPGIDha/8FH590cXNi5WSYYXizvLFVl9J7uoSykWUebbNa/t2JXY/JEH37cFPPGCyNEajlWBh2zqBBfbSaLNkC+3bVXTHf7Ii4xLt0Q8pWbEe1lEGCbNUprJpoqewjBSFfPJ/KFs1hAibXM7Ou1qlUy8/3LMsuoXktt7oezVmGmU1Yf3CSZpbwlqy4gVsXdXwBLiQfZUIzAHnT3w+EdAxnOwggtPYCDgETkXYYrIXfzSugt4fj0jH8cmolXcbczcYgPjhUImEQuPZ9YEf3aOFvppFimt41VGo/cj5rlE+gv4NYSEfH9rlgUbpqxQe8DA3XosPg8NX2J7KTjw4gINvn/E8c3+INxPoW3NmXefDD3ddlbcoms0rzcHaLrpzwPQtel+mB/6Cu+mOu3GYzadh1Na0z39Ssf6tpGRfQWboDLI8BzVxfEYFKm/kbXFXQIU4V+IRCtX3MVkJbZ82pbevskzLZ+0kzqz4T1R9yGA4u+wchrEoOBs5A+mKvlXj2lagV16yAAnfaRPh06MahwYklkT3NkaOzl8zKZaOBt0sb/E6+QOUAKbaWrW1on8UwU46ODZl9x8oNENUWvwURj4DhLad8bLEJ LGoT5sYm Kpkfm5DD1y7agwZsDGv+T57stELG361YOr2ssJp9g+3h2qG0GC4SmZelQjsbDiyONliMQz50hDNbIUpHws4pLPu2PaAHL92fY4kBMFe0E63cSPwpyEF2mud+aSc1RLFLKTp/Io46ij4F4ogHaksg8WfIM9HEZOCjbmUwaULWZ1ClRkeoNG7qU9m19f1jyoU5IHhbfGd6rGJrgMI/ZKcAEKn9aZQ3z4JNW10DXw2yDPoaeiGXQfy0Zh6yvUQqLD9wmDNeCkVLIGr/FsFN3qdEJ1wv7sNCb7EsJLqKFOhJz7GNdQNxJwln3NJdDTOgaDbFo0O2XxY8TSJ82eLxKiZsDgN5BFVvv6HOsrfLEUKP1YhCUcqmloHj+j+ISqqJab3ub9QQqqvNz3tgEu2OBB+JNCdaSvm1QwuIlgNcHjJx8FmwMTXzZbYVegT18SvvbkX3pVURk/7rRMnrR081iCL3LGlxrqUukJoAAtMRxtVuWHn4DVoldtUk2ODhjoMtJI6TlPtEvHu3qPWyvMI1IAKE2MxY+vg/9kzzsmGfoczDd6hVn2qZNrxaCeRxhPQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, Apr 07, 2025 at 04:42:05PM -0700, Nhat Pham wrote: > Currently, the swap cache code assumes that the swap space is of a fixed > size. The virtual swap space is dynamically sized, so the existing > partitioning code cannot be easily reused. A dynamic partitioning is > planned, but for now keep the design simple and just use a flat > swapcache for vswap. > > Since the vswap's implementation has begun to diverge from the old > implementation, we also introduce a new build config > (CONFIG_VIRTUAL_SWAP). Users who do not select this config will get the > old implementation, with no behavioral change. > > Signed-off-by: Nhat Pham > --- > mm/Kconfig | 13 ++++++++++ > mm/swap.h | 22 ++++++++++------ > mm/swap_state.c | 68 +++++++++++++++++++++++++++++++++++++++++-------- > 3 files changed, 85 insertions(+), 18 deletions(-) > > diff --git a/mm/Kconfig b/mm/Kconfig > index 1b501db06417..1a6acdb64333 100644 > --- a/mm/Kconfig > +++ b/mm/Kconfig > @@ -22,6 +22,19 @@ menuconfig SWAP > used to provide more virtual memory than the actual RAM present > in your computer. If unsure say Y. > > +config VIRTUAL_SWAP > + bool "Swap space virtualization" > + depends on SWAP > + default n > + help > + When this is selected, the kernel is built with the new swap > + design. This will allow us to decouple the swap backends > + (zswap, on-disk swapfile, etc.), and save disk space when we > + use zswap (or the zero-filled swap page optimization). > + > + There might be more lock contentions with heavy swap use, since > + the swap cache is no longer range partitioned. > + > config ZSWAP > bool "Compressed cache for swap pages" > depends on SWAP > diff --git a/mm/swap.h b/mm/swap.h > index d5f8effa8015..06e20b1d79c4 100644 > --- a/mm/swap.h > +++ b/mm/swap.h > @@ -22,22 +22,27 @@ void swap_write_unplug(struct swap_iocb *sio); > int swap_writepage(struct page *page, struct writeback_control *wbc); > void __swap_writepage(struct folio *folio, struct writeback_control *wbc); > > -/* linux/mm/swap_state.c */ > -/* One swap address space for each 64M swap space */ > +/* Return the swap device position of the swap slot. */ > +static inline loff_t swap_slot_pos(swp_slot_t slot) > +{ > + return ((loff_t)swp_slot_offset(slot)) << PAGE_SHIFT; > +} In the same vein as the previous email, please avoid mixing moves, renames and new code as much as possible. This makes it quite hard to follow what's going on. I think it would be better if you structure the series as follows: 1. Prep patches. Separate patches for moves, renames, new code. 3. mm: vswap - config VIRTUAL_SWAP - mm/vswap.c with skeleton data structures, init/exit, Makefile hookup 4. (temporarily) flatten existing address spaces IMO you can do the swapcache and zswap in one patch 5+. conversion patches Grow mm/vswap.c as you add discrete components like the descriptor allocator, swapoff locking, the swap_cgroup tracker etc. You're mostly doing this part already. But try to order them by complexity and on a "core to periphery" gradient. I.e. swapoff locking should probably come before cgroup stuff. Insert move and rename patches at points where they make the most sense. I.e. if they can be understood in the current upstream code already, put them with step 1 prep patches. If you find a move or a rename can only be understood in the context of one of the components, put them in a prep patch right before that one. > @@ -260,6 +269,28 @@ void delete_from_swap_cache(struct folio *folio) > folio_ref_sub(folio, folio_nr_pages(folio)); > } > > +#ifdef CONFIG_VIRTUAL_SWAP > +void clear_shadow_from_swap_cache(int type, unsigned long begin, > + unsigned long end) > +{ > + swp_slot_t slot = swp_slot(type, begin); > + swp_entry_t entry = swp_slot_to_swp_entry(slot); > + unsigned long index = swap_cache_index(entry); > + struct address_space *address_space = swap_address_space(entry); > + void *old; > + XA_STATE(xas, &address_space->i_pages, index); > + > + xas_set_update(&xas, workingset_update_node); > + > + xa_lock_irq(&address_space->i_pages); > + xas_for_each(&xas, old, entry.val + end - begin) { > + if (!xa_is_value(old)) > + continue; > + xas_store(&xas, NULL); > + } > + xa_unlock_irq(&address_space->i_pages); I don't think you need separate functions for this, init, exit etc. if you tweak the macros to resolve to one tree. The current code already works if swapfiles are smaller than SWAP_ADDRESS_SPACE_PAGES and there is only one tree, after all. This would save a lot of duplication and keep ifdefs more confined.