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 9D4AA606A6A6 for ; Thu, 3 Dec 2026 22:00:21 +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=bDbTtpOY Received: by kanga.kvack.org (Postfix) id B444A6B05B3; Thu, 3 Dec 2026 17:00:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF5466B05B4; Thu, 3 Dec 2026 17:00:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96E726B05B5; Thu, 3 Dec 2026 17:00:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 63FBF6B05B3 for ; Thu, 3 Dec 2026 17:00:18 -0500 (EST) Received: from smtpin25.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B9C68A0182 for ; Thu, 3 Dec 2026 22:00:16 +0000 (UTC) X-FDA: 85504040832.25.1C9E7A7 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) by imf16.hostedemail.com (Postfix) with ESMTP id DE065180015 for ; Thu, 3 Dec 2026 22:00:14 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=bDbTtpOY; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335215; b=c00b5C++VXMZmDGMyTdd/MnQXNhH+9wjQWL21QFkGMSakKd1ePAMcu9Dtv7p1PMbVzT5M+ 1j5s9RMo1XxsWnphPMfNROGnzeHlf+pTzxFVtkwCmVa6M0U9ujBJQ5KiPdAcjSB3QRIFYi gQa/tEGlP/McqXYV5FMJ3eERmf4dReM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335215; 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:in-reply-to:references:references:dkim-signature; bh=+pyy1QpZEg6nKoV0cFyvyQGtqsvC1xEBwFGoqWAST1A=; b=ZyvlXI5kUB7XdQyET813DiiRQBy/cuoZvlG82UWc36kqwNFZmUpyfNgEpDbXl0BzOWPGkB IgAGtKGtU9n33hv1Z3c/WFBYA+/vWlM/1agAbrJ2o+Bqdru+7vDvSuxMr9XkKIlTwCKQEG prPGbfeYC11ZZdWrMnwIeaeYTpma+88= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=bDbTtpOY; spf=pass (imf16.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-8b51389f58cso2732057b3.3 for ; Thu, 03 Dec 2026 14:00:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335214; x=1796940014; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+pyy1QpZEg6nKoV0cFyvyQGtqsvC1xEBwFGoqWAST1A=; b=bDbTtpOYp5DF5Du6/dc1imlM4TLWWsZKef+gYHWeVJBit5kL3oPJiD5bXiROJ2lUlX jGbsGLDoQp+DcMVnJMSGQzOp8lHGfUZSJjOSs0Dmt4K/zB5pzw1iFWeueXnT5tEuCAOQ D9PWW7kLrwSsEQEzg9xqT0TZHusc8ALnVjd0pwAIg95cpQu274NOkDKBE5Alp+XS7uwD PkaqY9mMpv0sBkzkzqp4gLC/gMyKvhEJ1NLRSkJg5lszenIIBx5JSV2E2UUNZ85oIZO6 UhQAIkoSK2rUXHAjhENl8O2dQcSFJdjiVmkFMNGulXU0D13j53P61nMAY4UqnJ5BHX0m LQlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335214; x=1796940014; h=content-transfer-encoding:mime-version:references:in-reply-to :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=+pyy1QpZEg6nKoV0cFyvyQGtqsvC1xEBwFGoqWAST1A=; b=m9FqXeO4qej228V90Wm2txrcd5eXHFyo+Ez/w5Nh/XFRpYnXIvSy5C18zCCWzlELXg jS9NCWQOlCgMZQVDAvwHeXchz9xzlDfUp7r4Pn073lX1hijgZ0tZEMKKQEt5emBHlDOL XD20UFojdsdPqYVqogkh8sdOTyuS949xb1Pd3fX1WGpV/NvT99DZejICKPPVWBLqIn/q Z4L4gf0Y/wRGiu5Xv74IFa+IsSBc1PANzLl7o1wA2fTttGz0FGDBkOymCFAQbx88AkRd a3JQDLKah74ssAeNWslVsbeCC5ouGHoNCDrQlECUnEaPe7FcNUT1f5MW5s698ANubSc6 jxEA== X-Gm-Message-State: APuU4Z/X6/+RgNGklOQT3N9GSbPmV1EJUTN/QIFdZjVgPEqIVhO1hbXu gHeVVGYDDqo3kmi6jPcrkNu8B3etFgTTjwpXz2aNmxL9RNgBuOZz83RdSX90W/NS X-Gm-Gg: AZFdTZcMOa05fYmMUjb6K3vjlIAISEFTGMC3urUKW51mqHyCiu21fQyM72g4VI6NEcf EkycQtUW8dP2NkTWzIcDqDw3A1oWBP4+6PVoNhJTMKCQ419vgPMUdKhk/S1pWcljVFvrdoxK7fK WSQhE4yE0Ag0VORRucyCgQggqWU55aopxEgqn/xjT/Ez9wSzQLjo3VuQVgaS0jfgFpZrW5I9bTr Tmo3IuFgTf6UcpZzj0SgPfVaFglctVlZ11PhCN2nWjaV5JA13pnHCIptuOtQVce7p3njtEa6X8o +cbgYHQMcbgedhJ1C9o8n2og0+/Updrt0Xt3h+hIl9GNriC7s3A8X4gQchGtdoozJ67eDCAv3Hu LAFOuQ1cb5urg0sWbSb1bx2ZY5OMQ8QhXmykvjcd4bM8nVxSF59g5Jk8xv7uEQ+jB2vhyVRvzdc puDrJRx1As1P4XzwH6dbtxb5+RfQsHvJBrIce+U+KmEhY5X6JYm+u0I3GXGmZTyzd5qDfiXJVVM Ws+7YbJloYfHYBVDE9MNjJkTmnyROFWsoEb7F9ohEeMz5qQfDWVdRAKPq16udkbNAzdSEoIVMZF yGRWWWx+CElTMTso4syZuj1ofrBnQoCtCaiHmg7NioFvOJQLdkEsvWXPWaIOwgwos15MYdf6M+e 3Rkg05LVYibgoLTbWO+pPWu6nr+thnRK0trc6 X-Received: by 2002:a05:690c:b90:b0:8b3:4a7a:599d with SMTP id 00721157ae682-8b660db688amr1159127b3.86.1796335213706; Thu, 03 Dec 2026 14:00:13 -0800 (PST) Received: from localhost ([2a03:2880:10ff:6::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660e10477sm970627b3.43.2026.12.03.14.00.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:12 -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 01/20] mm/swap: prepare dynamic clusters for vswap Date: Thu, 3 Dec 2026 13:59:51 -0800 Message-ID: <20261203220010.1302207-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: DE065180015 X-Rspam-User: X-Stat-Signature: d8u8jjifoacx3baa7xmxfgs693hmyooq X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,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: 1796335214-126091 X-HE-Meta: U2FsdGVkX1/GWo1WjQbmEd9brLliKGaVrCltB2cTYaJV8s9TmbHYZfFdSu9fBYbB1NTA06MsvnZo7Ti//FcbLZmDCwCN+H3ab0grkUZmTMoDVjtsykh+K8JQuDARHXWhrCtGNBgjIdvj6hDKh4kuldWypsQhj7ndjkD/I81GoPnf6c+25o8KZFqBngdNEM76/p4V1M22vojGLbDQ1qiF8rg+JGgP0yc+5TAtdM/EjSiWC4EJ/siEfn63PxeKeZ17pPm2fO8gyh3ZTtiQojejCooljBSUS1Ngas+MwI5TG4ZRinGiLIfmyVN81GqGdEWvbco72mDRRBGkcltF62wMdSUX7AHLlsBoKrGIFIHASFrvTUqCl2WtHa9Pg/c7ovVjl0N1mfPMCxdeEarC8qkPe6clWT+MewI3rNbXM3Q79Y4tqXpG++39C1nU+tIM1M75Ot+K7itG7gFMANnxaYvK4psP7YPgs+5pcLc5XisewnyIkUFkRpaZjyhMEks1GM0PkCqhNrpCY846G2yNFtNrJ/eGCwJ355HSbgaxEeWfp7Kv9Pp8qWgSko0wVUqIHzEvJ6vX2firXnY3InltwXnknjPoI/bKbPajoZ55i0iRvMH/33eHNcqA5ysMf2A/ycHAWIdiNhRdvlqny4eseavtWIN4g9fm/BMTGlk5HAhaxZhIhAjNfM38Q4Xs12612On/meGcZgnFPt10TWnTjDEMm78GggwzDcjTnnfrNi+HOjFuj1bNnODhC0M7rssCS3Zaa+21haL4NwT9HWxNnhbU0+VqMSHdtxRJrL5ptVetiG0EsT9lVodLmvF9oL+B4NZU2wUWSL01L2q7tj3bFZSq+adEXbIxhoGVwedZQZfdrpZXI3MkNNYp73mE7qwUw7hOp1vfqtA1YgjMz+NLizIJ+gKwFzfCuu5P4UvJoEsviNRukU9Xm65osHHhQuk8TyyQu4ImYZpXibALqBelZ0W EqNXo5VD CNRSGKXRACdoI65Bqq5fkNx3MH5hHcsVcS8bCNUhRRpR6E31xujzesCD2DVGU8gN+I4vyCCH+Qepnmx0A6EAPgb9PeJhTivDalUvBkbe8PbzOHWd+YFOvEtFLzRkDxX2kanvULXomHaG7VnyoACxqHcl8L8EAeXp+0PfBisyyMJGVl0fp7MI1+RvOokqYVQN0tG4mM0cLMqxVODNagEyuIW+cPx63QGOGAZEeNjqQ2LDgO1QGs3WbqadMCiI0RAL7LlQYx95kD2diNakb9ECpjHVrshtxb8rN01HqA6jOXjoFQFTZjN5IP7xPl4A+Y5x4xcNJE3Tn4U0pGT7UG6+YDEOQ90T16t3A8qivVq1Rr6SodWUJ3Dj7b4BnN84Ww6rkOCFCuBec7mdkNTZ5JATEHG/IStdPq6ctZrSS9HxCXWIVWkO9UhYDtiT38RK0n5UAGqd/cIihKDTctnKfy71MdUGn2wwVMthqToQ3xHB2yuBMZpy3y+5B2BlycdFffL0rFE6FeQGXIt7NgqgoQJdzbjF+/RT3BvwP0o4EeAyScw/Khjd9pGQs47If7+E6XkhzFSnoX0er/l/xZwXAxaaERqsJRxOZffUyG06sh8fIASUwCnLYO2dWa8ClAUkitz2SwRK+wQ8ELX9G3wmhfsR/W+ZPiefBxUsS6ZGMJNM8jt+DLuf3AcbS9a9iA8Em5ac2hy+pYb64gccTKnHIp5ziFyjX5HNgVY5sZtvI3pYgX7by/PqvkRJ8Rpv/TX7A8f8uylzng0Fqr7VmV0T8OCqugR1Qlp9FpqR3vfp6 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 A virtual device can span far more slots than it uses, so a dense descriptor array would scale with its maximum size. Store allocated virtual clusters in an XArray while retaining the indexed array for physical devices. Share cluster lookup, locking and swap-table allocation across the layouts. This lets the virtual allocator reuse the physical allocator's cluster machinery, with descriptors allocated on demand. Suggested-by: Kairui Song Signed-off-by: Nhat Pham --- MAINTAINERS | 1 + include/linux/swap.h | 9 ++ mm/page_io.c | 12 +++ mm/swap.h | 77 ++++++++++++++++- mm/swap_state.c | 43 +++++----- mm/swap_table.h | 25 ++++++ mm/swapfile.c | 198 ++++++++++++++++++++++++++++++++++++++----- mm/vswap.h | 22 +++++ mm/zswap.c | 6 ++ 9 files changed, 347 insertions(+), 46 deletions(-) create mode 100644 mm/vswap.h diff --git a/MAINTAINERS b/MAINTAINERS index 895c33a99acb..1b102d3ca838 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17907,6 +17907,7 @@ F: mm/swap.h F: mm/swap_state.c F: mm/swap_table.h F: mm/swapfile.c +F: mm/vswap.h MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE) M: David Hildenbrand diff --git a/include/linux/swap.h b/include/linux/swap.h index a3a98dc474c3..b3849df810f7 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -201,6 +201,7 @@ enum { SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */ SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */ SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */ + SWP_VSWAP = (1 << 14), /* virtual swap device */ /* add others here before... */ }; @@ -270,8 +271,14 @@ struct swap_info_struct { struct list_head discard_clusters; /* discard clusters list */ struct plist_node avail_list; /* entry in swap_avail_head */ const struct swap_ops *ops; + struct xarray cluster_info_pool; /* Xarray for vswap dynamic cluster info */ }; +static inline bool swap_is_vswap(struct swap_info_struct *si) +{ + return si->flags & SWP_VSWAP; +} + /** * folio_swap_entry - Return the swap entry at a page index within a folio. * @folio: The folio. @@ -434,6 +441,8 @@ void swap_free_hibernation_slot(swp_entry_t entry); static inline void put_swap_device(struct swap_info_struct *si) { + if (swap_is_vswap(si)) + return; percpu_ref_put(&si->users); } diff --git a/mm/page_io.c b/mm/page_io.c index a57cdcf2892d..62d1a5d42287 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -28,6 +28,7 @@ #include #include "swap.h" #include "swap_table.h" +#include "vswap.h" int generic_swapfile_activate(struct swap_info_struct *sis, struct file *swap_file, @@ -258,6 +259,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) } rcu_read_unlock(); + /* A vswap folio has no physical slot to write to. */ + if (is_vswap_entry(folio->swap)) { + folio_mark_dirty(folio); + return AOP_WRITEPAGE_ACTIVATE; + } + __swap_writeout(ctx, folio); return 0; out_unlock: @@ -515,6 +522,11 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio) if (zswap_load(folio) != -ENOENT) return; + if (unlikely(swap_is_vswap(sis))) { + folio_unlock(folio); + return; + } + /* We have to read from slower devices. Increase zswap protection. */ zswap_folio_swapin(folio); swap_add_folio(ctx, folio, READ); diff --git a/mm/swap.h b/mm/swap.h index d488a69c2809..0eaedbedc3d6 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -67,6 +67,12 @@ struct swap_cluster_info { struct list_head list; }; +struct swap_cluster_info_dynamic { + struct swap_cluster_info ci; + unsigned int index; /* for cluster_index() */ + struct rcu_head rcu; +}; + /* All on-list cluster must have a non-zero flag. */ enum swap_cluster_flags { CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */ @@ -77,6 +83,7 @@ enum swap_cluster_flags { CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG, CLUSTER_FLAG_FULL, CLUSTER_FLAG_DISCARD, + CLUSTER_FLAG_DEAD, /* Vswap dynamic cluster pending kfree_rcu */ CLUSTER_FLAG_MAX, }; @@ -120,12 +127,33 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) return __swap_type_to_info(swp_type(entry)); } +/** + * __swap_offset_to_cluster - look up the cluster holding a swap offset + * @si: the swap device + * @offset: the swap entry offset + * + * Context: A vswap cluster is freed by kfree_rcu(). Callers must hold the + * RCU read lock, or know the cluster is pinned by an in-use entry. + * + * Return: the cluster, or NULL if @si is a vswap device with no cluster + * allocated at @offset. + */ static inline struct swap_cluster_info *__swap_offset_to_cluster( struct swap_info_struct *si, pgoff_t offset) { + unsigned int cluster_idx = offset / SWAPFILE_CLUSTER; + VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ VM_WARN_ON_ONCE(offset >= roundup(si->max, SWAPFILE_CLUSTER)); - return &si->cluster_info[offset / SWAPFILE_CLUSTER]; + + if (swap_is_vswap(si)) { + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = xa_load(&si->cluster_info_pool, cluster_idx); + return ci_dyn ? &ci_dyn->ci : NULL; + } + + return &si->cluster_info[cluster_idx]; } static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry) @@ -134,10 +162,36 @@ static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entr swp_offset(entry)); } +static inline struct swap_cluster_info *__vswap_cluster_lock( + struct swap_info_struct *si, unsigned long offset, bool irq) +{ + struct swap_cluster_info *ci; + + rcu_read_lock(); + ci = __swap_offset_to_cluster(si, offset); + if (ci) { + if (irq) + spin_lock_irq(&ci->lock); + else + spin_lock(&ci->lock); + + /* The cluster can be torn down while we wait for the lock. */ + if (ci->flags == CLUSTER_FLAG_DEAD) { + if (irq) + spin_unlock_irq(&ci->lock); + else + spin_unlock(&ci->lock); + ci = NULL; + } + } + rcu_read_unlock(); + return ci; +} + static __always_inline struct swap_cluster_info *__swap_cluster_lock( struct swap_info_struct *si, unsigned long offset, bool irq) { - struct swap_cluster_info *ci = __swap_offset_to_cluster(si, offset); + struct swap_cluster_info *ci; /* * Nothing modifies swap cache in an IRQ context. All access to @@ -150,6 +204,11 @@ static __always_inline struct swap_cluster_info *__swap_cluster_lock( */ VM_WARN_ON_ONCE(!in_task()); VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ + + if (swap_is_vswap(si)) + return __vswap_cluster_lock(si, offset, irq); + + ci = __swap_offset_to_cluster(si, offset); if (irq) spin_lock_irq(&ci->lock); else @@ -160,10 +219,12 @@ static __always_inline struct swap_cluster_info *__swap_cluster_lock( /** * swap_cluster_lock - Lock and return the swap cluster of given offset. * @si: swap device the cluster belongs to. - * @offset: the swap entry offset, pointing to a valid slot. + * @offset: the swap entry offset. * * Context: The caller must ensure the offset is in the valid range and * protect the swap device with reference count or locks. + * Return: the locked cluster, or NULL if it is gone. Only a vswap device + * can return NULL, as its clusters are allocated and freed on demand. */ static inline struct swap_cluster_info *swap_cluster_lock( struct swap_info_struct *si, unsigned long offset) @@ -382,6 +443,16 @@ static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry) return NULL; } +static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry) +{ + return NULL; +} + +static inline unsigned int swp_cluster_offset(swp_entry_t entry) +{ + return 0; +} + static inline int folio_alloc_swap(struct folio *folio) { return -EINVAL; diff --git a/mm/swap_state.c b/mm/swap_state.c index da65d75ceead..436f82319425 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -92,13 +92,11 @@ void show_swap_cache_info(void) */ struct folio *swap_cache_get_folio(swp_entry_t entry) { - struct swap_cluster_info *ci = __swap_entry_to_cluster(entry); - unsigned int ci_off = swp_cluster_offset(entry); unsigned long swp_tb; struct folio *folio; for (;;) { - swp_tb = swap_table_get(ci, ci_off); + swp_tb = swap_table_lookup(entry); if (!swp_tb_is_folio(swp_tb)) return NULL; folio = swp_tb_to_folio(swp_tb); @@ -108,7 +106,7 @@ struct folio *swap_cache_get_folio(swp_entry_t entry) * The folio may have been freed and its memory reused before * we got the reference. Make sure it is still in the slot. */ - swp_tb = swap_table_get(ci, ci_off); + swp_tb = swap_table_lookup(entry); if (likely(swp_tb_is_folio(swp_tb) && swp_tb_to_folio(swp_tb) == folio)) return folio; @@ -129,8 +127,7 @@ bool swap_cache_has_folio(swp_entry_t entry) { unsigned long swp_tb; - swp_tb = swap_table_get(__swap_entry_to_cluster(entry), - swp_cluster_offset(entry)); + swp_tb = swap_table_lookup(entry); return swp_tb_is_folio(swp_tb); } @@ -146,8 +143,7 @@ void *swap_cache_get_shadow(swp_entry_t entry) { unsigned long swp_tb; - swp_tb = swap_table_get(__swap_entry_to_cluster(entry), - swp_cluster_offset(entry)); + swp_tb = swap_table_lookup(entry); if (swp_tb_is_shadow(swp_tb)) return swp_tb_to_shadow(swp_tb); return NULL; @@ -429,14 +425,16 @@ void __swap_cache_replace_folio(struct swap_cluster_info *ci, * -ENOENT / -EEXIST: Target swap entry is unavailable or cached, the caller * should abort or try to use the cached folio instead */ -static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, - swp_entry_t targ_entry, gfp_t gfp, +static struct folio *__swap_cache_alloc(swp_entry_t targ_entry, gfp_t gfp, unsigned int order, struct vm_fault *vmf, struct mempolicy *mpol, pgoff_t ilx) { int err; swp_entry_t entry; struct folio *folio; + struct swap_cluster_info *ci; + struct swap_info_struct *si = __swap_entry_to_info(targ_entry); + unsigned long offset = swp_offset(targ_entry); void *shadow = NULL; unsigned short memcg_id; unsigned long address, nr_pages = 1UL << order; @@ -446,9 +444,12 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, entry.val = round_down(targ_entry.val, nr_pages); /* Check if the slot and range are available, skip allocation if not */ - spin_lock(&ci->lock); - err = __swap_cache_add_check(ci, targ_entry, nr_pages, NULL, NULL); - spin_unlock(&ci->lock); + err = -ENOENT; + ci = swap_cluster_lock(si, offset); + if (ci) { + err = __swap_cache_add_check(ci, targ_entry, nr_pages, NULL, NULL); + swap_cluster_unlock(ci); + } if (unlikely(err)) return ERR_PTR(err); @@ -469,10 +470,13 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, return ERR_PTR(-ENOMEM); /* Double check the range is still not in conflict */ - spin_lock(&ci->lock); - err = __swap_cache_add_check(ci, targ_entry, nr_pages, &shadow, &memcg_id); + err = -ENOENT; + ci = swap_cluster_lock(si, offset); + if (ci) + err = __swap_cache_add_check(ci, targ_entry, nr_pages, &shadow, &memcg_id); if (unlikely(err)) { - spin_unlock(&ci->lock); + if (ci) + swap_cluster_unlock(ci); folio_put(folio); return ERR_PTR(err); } @@ -480,10 +484,11 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, __folio_set_locked(folio); __folio_set_swapbacked(folio); __swap_cache_do_add_folio(ci, folio, entry); - spin_unlock(&ci->lock); + swap_cluster_unlock(ci); if (mem_cgroup_swapin_charge_folio(folio, memcg_id, vmf ? vmf->vma->vm_mm : NULL, gfp)) { + /* The folio pins the cluster */ spin_lock(&ci->lock); __swap_cache_do_del_folio(ci, folio, entry, shadow); spin_unlock(&ci->lock); @@ -542,9 +547,7 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp, { int order, err; struct folio *ret; - struct swap_cluster_info *ci; - ci = __swap_entry_to_cluster(targ_entry); order = highest_order(orders); /* orders must be non-zero, and must not exceed cluster size. */ @@ -552,7 +555,7 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp, return ERR_PTR(-EINVAL); do { - ret = __swap_cache_alloc(ci, targ_entry, gfp, order, + ret = __swap_cache_alloc(targ_entry, gfp, order, vmf, mpol, ilx); if (!IS_ERR(ret)) break; diff --git a/mm/swap_table.h b/mm/swap_table.h index 072625d60d54..c21a48c98cc6 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -278,6 +278,31 @@ static inline unsigned long swap_table_get(struct swap_cluster_info *ci, return swp_tb; } +/* + * Resolve @entry's cluster and read its slot, both under RCU. A vswap + * cluster is allocated on demand and freed by kfree_rcu(), so a caller + * starting from an entry cannot resolve it beforehand. + */ +static inline unsigned long swap_table_lookup(swp_entry_t entry) +{ + struct swap_cluster_info *ci; + atomic_long_t *table; + unsigned long swp_tb; + + rcu_read_lock(); + ci = __swap_entry_to_cluster(entry); + if (!ci) { + rcu_read_unlock(); + return null_to_swp_tb(); + } + table = rcu_dereference(ci->table); + swp_tb = table ? atomic_long_read(&table[swp_cluster_offset(entry)]) + : null_to_swp_tb(); + rcu_read_unlock(); + + return swp_tb; +} + static inline void __swap_table_set_zero(struct swap_cluster_info *ci, unsigned int ci_off) { diff --git a/mm/swapfile.c b/mm/swapfile.c index d59fa8220862..818189c82ea9 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ #include #include #include "swap_table.h" +#include "vswap.h" #include "internal.h" #include "swap.h" @@ -399,6 +401,8 @@ static inline bool cluster_is_usable(struct swap_cluster_info *ci, int order) static inline unsigned int cluster_index(struct swap_info_struct *si, struct swap_cluster_info *ci) { + if (swap_is_vswap(si)) + return container_of(ci, struct swap_cluster_info_dynamic, ci)->index; return ci - si->cluster_info; } @@ -451,7 +455,7 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci) /* * Return with ci locked. An unlocked allocator also needs si's outer locks; - * initial bad-slot setup passes NULL because the device is not published. + * physical bad-slot setup passes NULL because the device is not published. */ static int swap_cluster_alloc_table(struct swap_info_struct *si, struct swap_cluster_info *ci, @@ -631,10 +635,15 @@ static void move_cluster(struct swap_info_struct *si, lockdep_assert_held(&ci->lock); spin_lock(&si->lock); - if (ci->flags == CLUSTER_FLAG_NONE) + if (!list) { + /* Going away. An isolated cluster is already off its list. */ + if (ci->flags != CLUSTER_FLAG_NONE) + list_del(&ci->list); + } else if (ci->flags == CLUSTER_FLAG_NONE) { list_add_tail(&ci->list, list); - else + } else { list_move_tail(&ci->list, list); + } spin_unlock(&si->lock); ci->flags = new_flags; } @@ -652,6 +661,18 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info { swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false); swap_cluster_free_table(ci); + + if (swap_is_vswap(si)) { + struct swap_cluster_info_dynamic *ci_dyn; + + /* vswap clusters are destroyed, not returned to free_clusters. */ + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + xa_erase(&si->cluster_info_pool, ci_dyn->index); + move_cluster(si, ci, NULL, CLUSTER_FLAG_DEAD); + kfree_rcu(ci_dyn, rcu); + return; + } + move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; } @@ -848,6 +869,8 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si, unsigned int ci_off = offset % SWAPFILE_CLUSTER; unsigned long idx = offset / SWAPFILE_CLUSTER; struct swap_cluster_info *ci; + struct swap_info_struct *alloc_si = swap_is_vswap(si) ? si : NULL; + bool need_table; int ret = 0; /* si->max may got shrunk by swap swap_activate() */ @@ -868,9 +891,10 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si, } ci = cluster_info + idx; + need_table = !ci->count; /* Need to allocate swap table first for initial bad slot marking. */ - if (!ci->count) { - ret = swap_cluster_alloc_table(NULL, ci, GFP_KERNEL, false); + if (need_table) { + ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false); if (ret) goto out; } else { @@ -885,6 +909,8 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si, } out: spin_unlock(&ci->lock); + if (need_table && alloc_si) + local_unlock(&percpu_swap_cluster.lock); WARN_ON(ci->count > SWAPFILE_CLUSTER); WARN_ON(ci->flags); @@ -905,6 +931,8 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, unsigned long offset = start, end = start + nr_pages; unsigned long swp_tb; + VM_WARN_ON_ONCE(swap_is_vswap(si)); + spin_unlock(&ci->lock); do { swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER); @@ -1107,6 +1135,45 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si, return found; } +static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, + struct folio *folio) +{ + struct swap_cluster_info_dynamic *ci_dyn; + struct swap_cluster_info *ci; + unsigned long offset; + + VM_WARN_ON(!swap_is_vswap(si)); + + ci_dyn = kzalloc_obj(*ci_dyn, GFP_ATOMIC | __GFP_NOWARN); + if (!ci_dyn) + return SWAP_ENTRY_INVALID; + + spin_lock_init(&ci_dyn->ci.lock); + INIT_LIST_HEAD(&ci_dyn->ci.list); + + /* Lock before publishing: xa_alloc makes the cluster findable by offset. */ + ci = &ci_dyn->ci; + spin_lock(&ci->lock); + + if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) { + spin_unlock(&ci->lock); + kfree(ci_dyn); + return SWAP_ENTRY_INVALID; + } + + if (xa_alloc(&si->cluster_info_pool, &ci_dyn->index, ci_dyn, + XA_LIMIT(1, DIV_ROUND_UP(si->max, SWAPFILE_CLUSTER) - 1), + GFP_ATOMIC | __GFP_NOWARN)) { + spin_unlock(&ci->lock); + swap_cluster_free_table(&ci_dyn->ci); + kfree(ci_dyn); + return SWAP_ENTRY_INVALID; + } + + offset = cluster_offset(si, ci); + return alloc_swap_scan_cluster(si, ci, folio, offset); +} + static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) { long to_scan = 1; @@ -1215,6 +1282,12 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, goto done; } + if (swap_is_vswap(si)) { + found = vswap_alloc_cluster(si, folio); + if (found) + goto done; + } + if (!swap_prefer_free_cluster(si)) { found = alloc_swap_scan_list(si, &si->free_clusters, folio, false); if (found) @@ -1351,8 +1424,10 @@ static bool swap_usage_add(struct swap_info_struct *si, unsigned int nr_entries) /* * If device is full, and SWAP_USAGE_OFFLIST_BIT is not set, * remove it from the plist. + * + * Vswap is never on the avail list, so skip it. */ - if (unlikely(val == si->pages)) { + if (unlikely(val == si->pages) && !swap_is_vswap(si)) { del_from_avail_list(si, false); return true; } @@ -1367,8 +1442,10 @@ static void swap_usage_sub(struct swap_info_struct *si, unsigned int nr_entries) /* * If device is not full, and SWAP_USAGE_OFFLIST_BIT is set, * add it to the plist. + * + * Vswap is never on the avail list, so skip it. */ - if (unlikely(val & SWAP_USAGE_OFFLIST_BIT)) + if (unlikely(val & SWAP_USAGE_OFFLIST_BIT) && !swap_is_vswap(si)) add_to_avail_list(si, false); } @@ -1413,6 +1490,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, static bool get_swap_device_info(struct swap_info_struct *si) { + /* The vswap device is always alive, so it needs no refcount. */ + if (swap_is_vswap(si)) + return true; + if (!percpu_ref_tryget_live(&si->users)) return false; /* @@ -1448,11 +1529,11 @@ static bool swap_alloc_fast(struct folio *folio) return false; ci = swap_cluster_lock(si, offset); - if (cluster_is_usable(ci, order)) { + if (ci && cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); alloc_swap_scan_cluster(si, ci, folio, offset); - } else { + } else if (ci) { swap_cluster_unlock(ci); } @@ -1574,6 +1655,7 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp) if (IS_ERR_OR_NULL(si)) return 0; + /* The source PTE pins the entry, so its cluster is alive. */ ci = __swap_offset_to_cluster(si, offset); ret = swap_extend_table_alloc(si, ci, swp_cluster_offset(entry), gfp); @@ -1984,7 +2066,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry) return NULL; put_out: pr_err_ratelimited("%s: %s%08lx\n", __func__, Bad_offset, entry.val); - percpu_ref_put(&si->users); + put_swap_device(si); return ERR_PTR(-EIO); } @@ -2065,7 +2147,7 @@ bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry) unsigned long swp_tb; ci = swap_cluster_lock(si, offset); - swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER); + swp_tb = __swap_table_get(ci, swp_cluster_offset(entry)); swap_cluster_unlock(ci); return swp_tb_get_count(swp_tb) > 0; @@ -2118,6 +2200,7 @@ static bool folio_maybe_swapped(struct folio *folio) VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); + /* Folio is locked and in swap cache, so ci->count > 0: cluster is alive. */ ci = __swap_entry_to_cluster(entry); ci_off = swp_cluster_offset(entry); ci_end = ci_off + folio_nr_pages(folio); @@ -2301,6 +2384,9 @@ static int __find_hibernation_swap_type(dev_t device, sector_t offset) if (!(sis->flags & SWP_WRITEOK)) continue; + /* vswap has no bdev, so it is never a hibernation target. */ + if (swap_is_vswap(sis)) + continue; if (device == sis->bdev->bd_dev) { struct swap_extent *se = first_se(sis); @@ -2488,6 +2574,9 @@ int find_first_swap(dev_t *device) if (!(sis->flags & SWP_WRITEOK)) continue; + /* vswap has no bdev, so it is never a hibernation target. */ + if (swap_is_vswap(sis)) + continue; *device = sis->bdev->bd_dev; spin_unlock(&swap_lock); return type; @@ -2714,8 +2803,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, &vmf); } if (!folio) { - swp_tb = swap_table_get(__swap_entry_to_cluster(entry), - swp_cluster_offset(entry)); + swp_tb = swap_table_lookup(entry); if (swp_tb_get_count(swp_tb) <= 0) continue; return -ENOMEM; @@ -3147,15 +3235,24 @@ static int setup_swap_extents(struct swap_info_struct *sis, static void _enable_swap_info(struct swap_info_struct *si) { - atomic_long_add(si->pages, &nr_swap_pages); - total_swap_pages += si->pages; + if (!swap_is_vswap(si)) { + atomic_long_add(si->pages, &nr_swap_pages); + total_swap_pages += si->pages; + } assert_spin_locked(&swap_lock); - plist_add(&si->list, &swap_active_head); + /* + * Vswap has no backing file and no swapoff support, so keep it + * off swap_active_head (used by swapoff filename lookup and + * swap_sync_discard) and swap_avail_head (physical allocator). + */ + if (!swap_is_vswap(si)) { + plist_add(&si->list, &swap_active_head); - /* Add back to available list */ - add_to_avail_list(si, true); + /* Add back to available list */ + add_to_avail_list(si, true); + } } /* @@ -3199,12 +3296,31 @@ static void wait_for_allocation(struct swap_info_struct *si) } } -static void free_swap_cluster_info(struct swap_cluster_info *cluster_info, +static void free_swap_cluster_info(struct swap_info_struct *si, + struct swap_cluster_info *cluster_info, unsigned long maxpages) { + struct swap_cluster_info_dynamic *ci_dyn; struct swap_cluster_info *ci; + unsigned long idx; int i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); + if (swap_is_vswap(si)) { + xa_for_each(&si->cluster_info_pool, idx, ci_dyn) { + ci = &ci_dyn->ci; + spin_lock(&ci->lock); + if (cluster_table_is_alloced(ci)) { + swap_cluster_assert_empty(ci, 0, + SWAPFILE_CLUSTER, true); + swap_cluster_free_table(ci); + } + spin_unlock(&ci->lock); + kfree(ci_dyn); + } + xa_destroy(&si->cluster_info_pool); + return; + } + if (!cluster_info) return; for (i = 0; i < nr_clusters; i++) { @@ -3351,7 +3467,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mutex_unlock(&swapon_mutex); kfree(p->global_cluster); p->global_cluster = NULL; - free_swap_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(p, cluster_info, maxpages); inode = mapping->host; @@ -3698,10 +3814,39 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, unsigned long maxpages) { unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); - struct swap_cluster_info *cluster_info; + struct swap_cluster_info *cluster_info = NULL; + struct swap_cluster_info_dynamic *ci_dyn = NULL; int err = -ENOMEM; unsigned long i; + /* A vswap device uses an xarray pool instead of a static array. */ + if (swap_is_vswap(si)) { + nr_clusters = 0; + xa_init_flags(&si->cluster_info_pool, XA_FLAGS_ALLOC); + + /* + * Pre-allocate cluster 0 and mark slot 0 (header page) + * as bad so the allocator never hands out page offset 0. + */ + ci_dyn = kzalloc_obj(*ci_dyn, GFP_KERNEL); + if (!ci_dyn) + goto err; + spin_lock_init(&ci_dyn->ci.lock); + INIT_LIST_HEAD(&ci_dyn->ci.list); + + err = xa_insert(&si->cluster_info_pool, 0, ci_dyn, GFP_KERNEL); + if (err) { + kfree(ci_dyn); + goto err; + } + + err = swap_cluster_setup_bad_slot(si, &ci_dyn->ci, 0, false); + if (err) + goto err; + + goto setup_cluster_info; + } + cluster_info = kvzalloc_objs(*cluster_info, nr_clusters); if (!cluster_info) goto err; @@ -3745,6 +3890,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, goto err; } +setup_cluster_info: INIT_LIST_HEAD(&si->free_clusters); INIT_LIST_HEAD(&si->full_clusters); INIT_LIST_HEAD(&si->discard_clusters); @@ -3766,10 +3912,16 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, } } + /* Slot 0 is bad, so cluster 0 never empties. The rest of it is usable. */ + if (swap_is_vswap(si)) { + ci_dyn->ci.flags = CLUSTER_FLAG_NONFULL; + list_add_tail(&ci_dyn->ci.list, &si->nonfull_clusters[0]); + } + si->cluster_info = cluster_info; return 0; err: - free_swap_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(si, cluster_info, maxpages); return err; } @@ -3991,7 +4143,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) si->global_cluster = NULL; inode = NULL; destroy_swap_extents(si, swap_file); - free_swap_cluster_info(si->cluster_info, si->max); + free_swap_cluster_info(si, si->cluster_info, si->max); si->cluster_info = NULL; /* * Clear the SWP_USED flag after all resources are freed so diff --git a/mm/vswap.h b/mm/vswap.h new file mode 100644 index 000000000000..bf1606263e8e --- /dev/null +++ b/mm/vswap.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Virtual swap space + * + * Copyright (C) 2026 Nhat Pham + */ +#ifndef _MM_VSWAP_H +#define _MM_VSWAP_H + +#include +#include "swap.h" + +#ifdef CONFIG_SWAP + +static inline bool is_vswap_entry(swp_entry_t entry) +{ + return swap_is_vswap(__swap_entry_to_info(entry)); +} + +#endif /* CONFIG_SWAP */ + +#endif /* _MM_VSWAP_H */ diff --git a/mm/zswap.c b/mm/zswap.c index 6a66b898ab47..ff3b0ceb10f4 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1093,6 +1093,12 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, if (IS_ERR_OR_NULL(si)) return -ENOENT; + /* Vswap entries have no physical backing to write to. */ + if (swap_is_vswap(si)) { + put_swap_device(si); + return -EINVAL; + } + mpol = get_task_policy(current); folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol, NO_INTERLEAVE_INDEX); -- 2.53.1-Meta