From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335220; b=Ivygyqt3HYGvkTqKLvGXh8589F3R2qrwg+8vTeTsQ2Dc8LWL8JphUZzDTjjh8+gDQmFY /lVAsmHELGUIEN1QR3800MVM09QjnUidVOfIF02sHmhDlyvdJMlpxnd0h/BxfQCHPONUu n5anIWTO11gVxSILUJDgYMq9HzY0ziK4Lq1XZSakR+AcovoSTU+EZN/zOaEUH1zkKQYJh 90Tg8a3XHX5cz16HiaZvnu+InBwOkYxq5sdxkprSOTIxgcyvUOQhHAl2aYnRxGlSDRf9X H2cNnJihOUuMp/ymW+MetvGTGjU+fn/u/fGO7AksE05UXHRA3Ea/6z6jyEtbdxLSp+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335220; 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:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=tRijjg8QHuna3owVMOscohKV4zEiSsEdAYp3Zn+5rSo=; b=3mjTId8pm6bUO58yF+hlKGxvYffeMmOM4pAZcypfQS6WAIy4L3B0YlOzAfAoYDn3LjdO h1vIdDYe8/XzT4X4x/5wLSvWQJN7JEIpp4jqAlNmMNvPnuHGvKDeyKhiJq+DBjOs8oHuM uncliqtFSAeS05fNHRkzhHArnN5mrAuo0rV7K6yTCufaKTE6vPDaK2z/DoO1KmQbDjEJD 1HMb+RIODksqrAsNb6fZMuhJA4QkxmH0hbDkBPn4INF8Ejwaa17vAi+JC931ioxHH3t8a YSSd3fB0IVwDk8YEDSFdz8lYzvQn+BEUPWZKfvK0qFSBZZUoAUEGGV7N0jXa36yWzsQ== 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=eiXGt2bn; arc=none smtp.remote-ip=74.125.224.48 Received: from mail-yx1-f48.google.com (mail-yx1-f48.google.com [74.125.224.48]) (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 711C6606A695 for ; Thu, 3 Dec 2026 22:00:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 711C6606A695 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=eiXGt2bn Received: by mail-yx1-f48.google.com with SMTP id 956f58d0204a3-67be4f2f4d4so305717d50.2 for ; Thu, 03 Dec 2026 14:00:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335217; x=1796940017; darn=vger.kernel.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=tRijjg8QHuna3owVMOscohKV4zEiSsEdAYp3Zn+5rSo=; b=eiXGt2bn/JD3++PTmFExD5jXArkPnP3MiT9nOg35VImcGpXtzyLQXjNtIJLYyqtyYV B1PHX16UTLqyg0AJ8ymnMQitKRU3wKyOwv5aUqsaeW/jVWfnJxwaolt2SywOE+18u5Lf e5JI212fZc7VZ4gMLxdZ1f+Zqi+/VOjLlM0ndhQ3eMkRMngFMOZn05nKNiK1ZqXrYy4O FKySqozCqq3/+Ru9SggBo5YRY9H//I8jtNPJX2UyoUDQrcoZ6NtdzpD44Nwa5NfFujy7 3ZX6vkMF6OdGhlLSxtLA0CUdtiNVR9ASQ8XumSfRF29oWk4pmH984A7nkGQ2++/AFfa4 /fNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335217; x=1796940017; 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=tRijjg8QHuna3owVMOscohKV4zEiSsEdAYp3Zn+5rSo=; b=zLjOIzzgEh2QhtV6+QwO5Nqj9vyIeTOQMvBIYeQ8Jpp66Bcq6ASXB4owc+2IK4Mygh 0csNJDQz/UhT2UgXs3rVWck5q/vZXXejY1Hq7OUdYaRtx/X+ntrc0T4wAxJzcIs84NZd hYekeCoSyJvLR/GSRRqzwJ7DDsqxwGLnC+jn4kqZHdnZ9/AZrm4wLKyY/waFQoN40k27 SpQRnNrH4oEhLMK347MQZv0S68BuiBdmZtwEYtUqRnWnelkfi0L3bUg7oSXp1sBg1Q9h D2V78dymNQiVmU2F+27AxcmMio7oQwMR+f2h7jMCebN4zptqt1YdgyXz/FRttbbACR9F 2b1Q== X-Forwarded-Encrypted: i=1; AL+hErELd5VaLDtxhuQHb0ttS2ROWSu7sSgSfDPcAPX07odNzTzNzJqHde5Z8WKxhRvyebKskvvyAUyh@vger.kernel.org X-Gm-Message-State: APuU4Z82cXQ8/HZs6pch+X4GD56B9QIY2IGu6igxxkWW5UgSqqrmb1oN X+Z4hxBg5Q3QjIpoLQFhxcKDUGslWMGpaka86Q0QZLWwGba0U5qA3i14 X-Gm-Gg: AZFdTZdxqX9NroBfeQKNTX+Fpu+CqPFsOPj35s0fFYBvFk5eYhvRJRXZJrcLZbVML64 FATwbWmVNthkkOS0FdIWvNgqnn7MSi2tvMLOkTdYWGIhlNxglwNEbD1C7pz73q7VZj8bHoPxVhk gYchzqXFqM2x1K+JJIBCiiEWgN+r13nlcw9VCu/0+FOlODcIftvYGQJyjJ8P0EhP1HVFJajKjKG KBWIwND6am0m1V75dscyQOXlT348/pEmfIGfkiFLY0z/BzasgLu4wf4WZ86Fcm6UxBX0G5HOX6n 0PlA36ehC8m0ZIvSo151uTOuanPYApsAGADZDJs7OOru2HMpdltVziJ3snA2hD57PMLrEh6eXpE Nft3jJap8REV0IWRKBLHUxtpOwn9y1/RUQlDMj648PwlDUoZ3CRNYsG3KC+P6Q6sy/oxa1n5lK1 0kuYx3SOppbsZaM46I6KiT9r3zqgU8F1ZxweATFJ5cG5fc2IhUT9QNpLwRv8XBPpwxYnBWXiKTP Y+Z1SKscGQYm2PkJKsx76Qfzi1aHc6JXvKvgut1XfKv6fpxB8EQSXPSobCxau7hVKwNS/OYe7HU F+hNmvuvZr1xiVzPLelxFnqHnk8K15MJIbxTxi4sgEDcL1wju0lZeCZL8sbuoMysptPLjBad5LW ecJ0dll2XlmkxaGJeqsb8LA93KPS5SZDan31jLQ== X-Received: by 2002:a05:690e:1907:b0:67d:eb12:7315 with SMTP id 956f58d0204a3-67f7244c0cdmr37780d50.86.1796335217239; Thu, 03 Dec 2026 14:00:17 -0800 (PST) Received: from localhost ([2a03:2880:10ff:71::]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0c087d4bsm195965d50.4.2026.12.03.14.00.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:16 -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 03/20] mm/swap: prepare zswap and zero storage for virtual entries Date: Thu, 3 Dec 2026 13:59:53 -0800 Message-ID: <20261203220010.1302207-4-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-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 Virtual swap entries need backend state that survives swapcache changes. Store compressed entries in a per-cluster vtable and retain the swap table's zero bit for zero-filled pages. Store aligned zswap pointers directly in the vtable. Update virtual entry release and swapcache handling to release compressed backing through the vtable when entries are freed or their backing is replaced. Keep the virtual device unregistered until physical I/O and swapoff are ready. Once virtual slots are allocated, zswap rejection must have a working physical fallback. Suggested-by: Kairui Song Signed-off-by: Nhat Pham --- include/linux/zswap.h | 4 + mm/memory.c | 11 ++- mm/page_io.c | 17 +++- mm/swap.h | 1 + mm/swap_state.c | 8 ++ mm/swap_table.h | 2 + mm/swapfile.c | 87 ++++++++++++++++++++ mm/vswap.h | 182 ++++++++++++++++++++++++++++++++++++++++++ mm/zswap.c | 81 ++++++++++++++++--- 9 files changed, 374 insertions(+), 19 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 94746fb71bb6..b90f330f87cd 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -6,6 +6,7 @@ #include struct lruvec; +struct zswap_entry; extern atomic_long_t zswap_stored_pages; @@ -28,6 +29,7 @@ unsigned long zswap_total_pages(void); bool zswap_store(struct folio *folio); int zswap_load(struct folio *folio); void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries); +void zswap_entry_free(struct zswap_entry *entry); int zswap_swapon(int type, unsigned long nr_pages); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); @@ -55,6 +57,8 @@ static inline void zswap_invalidate(int type, pgoff_t offset, { } +static inline void zswap_entry_free(struct zswap_entry *entry) {} + static inline int zswap_swapon(int type, unsigned long nr_pages) { return 0; diff --git a/mm/memory.c b/mm/memory.c index 90fc7c395124..9ba6ebd58132 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -89,6 +89,7 @@ #include "pgalloc-track.h" #include "internal.h" #include "swap.h" +#include "vswap.h" #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST) #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid. @@ -4753,6 +4754,9 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si, { if (!folio_test_swapcache(folio)) return false; + /* A vswap entry holds no physical slot, so keeping it saves no IO. */ + if (is_vswap_entry(folio->swap)) + return true; /* * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap * cache can help save some IO or memory overhead, but these devices @@ -4910,12 +4914,16 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si, if (unlikely(userfaultfd_armed(vma))) return 0; + entry = softleaf_from_pte(vmf->orig_pte); + /* * A large swapped out folio could be partially or fully in zswap. We * lack handling for such cases, so fallback to swapping in order-0 * folio. + * + * THP swapin for vswap is not supported yet either. */ - if (!zswap_never_enabled()) + if (is_vswap_entry(entry) || !zswap_never_enabled()) return 0; /* @@ -4926,7 +4934,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si, if (!data_race(si->flags & SWP_BLKDEV)) return 0; - entry = softleaf_from_pte(vmf->orig_pte); /* * Get a list of all the (large) orders below PMD_ORDER that are enabled * and suitable for swapping THP. diff --git a/mm/page_io.c b/mm/page_io.c index 062bfce6f0de..9397ef83a60d 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -162,13 +162,18 @@ static void swap_zeromap_folio_set(struct folio *folio) int nr_pages = folio_nr_pages(folio); struct swap_cluster_info *ci; swp_entry_t entry = folio->swap; - unsigned int i; + unsigned int voff, i; VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); ci = swap_cluster_get_and_lock(folio); - for (i = 0; i < folio_nr_pages(folio); i++) { + if (is_vswap_entry(folio->swap)) { + /* Free any prior backing (e.g. ZSWAP entry from earlier swapout) */ + voff = swp_cluster_offset(folio->swap); + __vswap_release_backing(ci, voff, nr_pages); + } + for (i = 0; i < nr_pages; i++) { __swap_table_set_zero(ci, swp_cluster_offset(entry)); entry.val++; } @@ -234,8 +239,9 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) * again while it stayed in the swap cache. Drop the old copy, * or zswap writeback would bring it back into the swap cache. */ - zswap_invalidate(swp_type(entry), swp_offset(entry), - folio_nr_pages(folio)); + if (!is_vswap_entry(entry)) + zswap_invalidate(swp_type(entry), swp_offset(entry), + folio_nr_pages(folio)); goto out_unlock; } @@ -246,6 +252,9 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio) */ swap_zeromap_folio_clear(folio); + if (is_vswap_entry(folio->swap)) + folio_release_vswap_backing(folio); + if (zswap_store(folio)) { count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT); goto out_unlock; diff --git a/mm/swap.h b/mm/swap.h index 95c5be9c8c27..7b9a65232d19 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -71,6 +71,7 @@ struct swap_cluster_info_dynamic { struct swap_cluster_info ci; unsigned int index; /* for cluster_index() */ struct rcu_head rcu; + atomic_long_t *virtual_table; /* Backing pointers for vswap slots */ }; /* All on-list cluster must have a non-zero flag. */ diff --git a/mm/swap_state.c b/mm/swap_state.c index 436f82319425..110a0c095825 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -27,6 +27,7 @@ #include "internal.h" #include "swap_table.h" #include "swap.h" +#include "vswap.h" /* Swap readahead cluster size, as a power of 2 pages. */ static int page_cluster; @@ -202,6 +203,13 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci, if (nr == 1) return 0; + /* + * Reject a vswap batch so __swap_cache_alloc_folio falls back to + * order 0. + */ + if (is_vswap_entry(targ_entry)) + return -EBUSY; + is_zero = __swap_table_test_zero(ci, ci_off); ci_off = round_down(ci_off, nr); ci_end = ci_off + nr; diff --git a/mm/swap_table.h b/mm/swap_table.h index c21a48c98cc6..9c9e3cd3b37e 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -6,6 +6,8 @@ #include #include "swap.h" +extern struct swap_info_struct *vswap_si; + /* A typical flat array in each cluster as swap table */ struct swap_table { atomic_long_t entries[SWAPFILE_CLUSTER]; diff --git a/mm/swapfile.c b/mm/swapfile.c index 153e73b96a7a..1387c8bb1e97 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -669,6 +669,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info 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); + vswap_cluster_free_vtable(ci); kfree_rcu(ci_dyn, rcu); return; } @@ -1161,11 +1162,19 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si, return SWAP_ENTRY_INVALID; } + if (vswap_cluster_alloc_vtable(ci_dyn, GFP_ATOMIC | __GFP_NOWARN)) { + spin_unlock(&ci->lock); + swap_cluster_free_table(ci); + 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); + vswap_cluster_free_vtable(&ci_dyn->ci); kfree(ci_dyn); return SWAP_ENTRY_INVALID; } @@ -1941,6 +1950,73 @@ int folio_alloc_swap(struct folio *folio) return order ? -E2BIG : -ENOMEM; } +/** + * __vswap_release_backing - release the backing of a range of vtable slots + * @ci: the locked vswap cluster + * @ci_start: first slot offset within @ci + * @nr: number of slots + * + * Releases the backing of each slot in [@ci_start, @ci_start + @nr). + * Clears the zero marks if set. + * + * Context: caller must hold @ci->lock. + */ +void __vswap_release_backing(struct swap_cluster_info *ci, + unsigned int ci_start, unsigned int nr) +{ + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int ci_off; + unsigned long vt; + + lockdep_assert_held(&ci->lock); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + + for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) { + vt = __vtable_get(ci_dyn, ci_off); + + switch (vtable_type(vt)) { + case VSWAP_ZSWAP: + zswap_entry_free(vtable_to_zswap(vt)); + break; + case VSWAP_NONE: + break; + default: + /* VSWAP_ZERO/VSWAP_FOLIO are return-only, not vtable tags */ + break; + } + + __vtable_set(ci_dyn, ci_off, VSWAP_NONE); + /* Zero-backed state lives in swap_table; clear it too. */ + if (__swap_table_test_zero(ci, ci_off)) + __swap_table_clear_zero(ci, ci_off); + } +} + +/** + * folio_release_vswap_backing() - Drop all backing for a folio's vswap entry. + * @folio: the folio, occupying a virtual swap entry. + * + * Release whatever backing the folio's virtual swap slots currently hold and + * reset them to empty, so a fresh backing can be installed. Used when a + * folio's swap backend is replaced. + * + * Context: Caller must hold the folio lock; @folio must be in the swap cache + * and occupy a virtual swap entry. + */ +void folio_release_vswap_backing(struct folio *folio) +{ + struct swap_cluster_info *ci; + int nr = folio_nr_pages(folio); + unsigned int voff; + + ci = __swap_entry_to_cluster(folio->swap); + voff = swp_cluster_offset(folio->swap); + + spin_lock(&ci->lock); + __vswap_release_backing(ci, voff, nr); + spin_unlock(&ci->lock); +} + /** * folio_dup_swap() - Increase swap count of swap entries of a folio. * @folio: folio with swap entries bounded. @@ -2086,6 +2162,9 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, VM_WARN_ON(ci->count < nr_pages); + if (swap_is_vswap(si)) + __vswap_release_backing(ci, ci_start, nr_pages); + ci->count -= nr_pages; do { old_tb = __swap_table_get(ci, ci_off); @@ -3315,6 +3394,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si, swap_cluster_free_table(ci); } spin_unlock(&ci->lock); + vswap_cluster_free_vtable(ci); kfree(ci_dyn); } xa_destroy(&si->cluster_info_pool); @@ -3844,6 +3924,10 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, if (err) goto err; + err = vswap_cluster_alloc_vtable(ci_dyn, GFP_KERNEL); + if (err) + goto err; + goto setup_cluster_info; } @@ -4274,3 +4358,6 @@ static int __init swapfile_init(void) return 0; } subsys_initcall(swapfile_init); + +struct swap_info_struct *vswap_si; +DEFINE_STATIC_KEY_FALSE(vswap_key); diff --git a/mm/vswap.h b/mm/vswap.h index bf1606263e8e..f64b1543e01f 100644 --- a/mm/vswap.h +++ b/mm/vswap.h @@ -7,16 +7,198 @@ #ifndef _MM_VSWAP_H #define _MM_VSWAP_H +#include #include #include "swap.h" +struct zswap_entry; + +/* + * VSWAP_ZERO and VSWAP_FOLIO are derived from swap_table state. + */ +enum vswap_backing_type { + VSWAP_NONE = 0, + VSWAP_ZSWAP = 1, + VSWAP_ZERO, + VSWAP_FOLIO, +}; + #ifdef CONFIG_SWAP +#include "swap_table.h" +DECLARE_STATIC_KEY_FALSE(vswap_key); + +static inline bool vswap_is_enabled(void) +{ + return static_branch_unlikely(&vswap_key); +} + static inline bool is_vswap_entry(swp_entry_t entry) { return swap_is_vswap(__swap_entry_to_info(entry)); } +/* + * Virtual table entry encoding for vswap clusters. + * + * Zero represents no separate backing. An aligned zswap pointer + * has bit 0 clear and is stored directly in the vtable. + * + * vtable[i] = NONE does not by itself mean "free". The swap_table entry + * and the per-slot zero flag carry the rest of the state. The full + * per-slot state table is: + * + * vtable[i] | swap_table[i] | zero | meaning + * ----------+---------------+-------+-------------------------------- + * NONE | NULL | clear | truly free / unbacked + * NONE | PFN | clear | folio cached, no backing + * NONE | shadow | clear | evicted, no backing: data lost + * NONE | * | set | zero-backed; cached if PFN set + * ZSWAP | PFN | clear | folio cached + zswap entry + * ZSWAP | shadow / NULL | clear | evicted, only in zswap + * + * Locking: the caller must own and lock the slot's swapcache folio to + * stabilize its backing. The cluster lock only makes individual vtable + * reads atomic; it does not permit backend changes. Revalidate speculative + * reads under the folio lock before using them. + * + * Zero-backed slots use the swap_table per-slot zero flag (same as + * direct-mapped physical swap), via __swap_table_test_zero() and friends, + * which fall back to ci->zero_bitmap where the flag does not fit. Cached + * folios are read out of the swap_table PFN entry; there is no separate FOLIO + * vtable type because the folio pointer would duplicate that PFN and + * would go stale on folio migration / split. + */ + +#define VTABLE_TAG_BITS 1 +#define VTABLE_TAG_MASK ((1UL << VTABLE_TAG_BITS) - 1) + +static inline enum vswap_backing_type vtable_type(unsigned long vt) +{ + return vt ? VSWAP_ZSWAP : VSWAP_NONE; +} + +static inline struct zswap_entry *vtable_to_zswap(unsigned long vt) +{ + VM_WARN_ON(vtable_type(vt) != VSWAP_ZSWAP); + return (struct zswap_entry *)vt; +} + +/* Virtual table accessors */ + +static inline unsigned long __vtable_get(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int off) +{ + VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER); + return atomic_long_read(&ci_dyn->virtual_table[off]); +} + +static inline void __vtable_set(struct swap_cluster_info_dynamic *ci_dyn, + unsigned int off, unsigned long vt) +{ + VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER); + atomic_long_set(&ci_dyn->virtual_table[off], vt); +} + +/** + * vswap_lock_cluster - look up and lock the vswap cluster for an entry + * @entry: the virtual swap entry + * @voff: out param, receives @entry's slot offset within the cluster + * + * Return: the locked vswap cluster, or NULL if @entry has no live cluster. + */ +static inline struct swap_cluster_info_dynamic * +vswap_lock_cluster(swp_entry_t entry, unsigned int *voff) +{ + struct swap_cluster_info *ci; + + ci = swap_cluster_lock(__swap_entry_to_info(entry), swp_offset(entry)); + if (!ci) + return NULL; + *voff = swp_cluster_offset(entry); + return container_of(ci, struct swap_cluster_info_dynamic, ci); +} + +void __vswap_release_backing(struct swap_cluster_info *ci, + unsigned int ci_start, unsigned int nr); + +/** + * vswap_zswap_store - record a zswap entry as the backing for a vswap entry. + * @entry: the vswap entry + * @ze: the zswap entry now holding @entry's compressed data + * + * Releases @entry's previous backing, and sets the zswap entry @ze as the new + * backing. + * + * Context: takes and drops the vswap cluster lock internally. + */ +static inline void vswap_zswap_store(swp_entry_t entry, + struct zswap_entry *ze) +{ + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int voff; + + ci_dyn = vswap_lock_cluster(entry, &voff); + __vswap_release_backing(&ci_dyn->ci, voff, 1); + __vtable_set(ci_dyn, voff, (unsigned long)ze); + swap_cluster_unlock(&ci_dyn->ci); +} + +/** + * vswap_zswap_load - return the zswap entry backing a vswap entry + * @entry: the virtual swap entry + * + * Context: takes and drops the vswap cluster lock internally. + * Return: the backing zswap entry, or NULL if @entry is not zswap-backed. + */ +static inline struct zswap_entry *vswap_zswap_load(swp_entry_t entry) +{ + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int voff; + unsigned long vt; + + ci_dyn = vswap_lock_cluster(entry, &voff); + if (!ci_dyn) + return NULL; + vt = __vtable_get(ci_dyn, voff); + swap_cluster_unlock(&ci_dyn->ci); + + if (vtable_type(vt) != VSWAP_ZSWAP) + return NULL; + return vtable_to_zswap(vt); +} + +void folio_release_vswap_backing(struct folio *folio); + +static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn, + gfp_t gfp) +{ + ci_dyn->virtual_table = kcalloc(SWAPFILE_CLUSTER, + sizeof(*ci_dyn->virtual_table), gfp); + return ci_dyn->virtual_table ? 0 : -ENOMEM; +} + +static inline void vswap_cluster_free_vtable(struct swap_cluster_info *ci) +{ + struct swap_cluster_info_dynamic *ci_dyn; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + kfree(ci_dyn->virtual_table); + ci_dyn->virtual_table = NULL; +} + +#else /* !CONFIG_SWAP */ + +static inline bool vswap_is_enabled(void) +{ + return false; +} + +static inline bool is_vswap_entry(swp_entry_t entry) +{ + return false; +} + #endif /* CONFIG_SWAP */ #endif /* _MM_VSWAP_H */ diff --git a/mm/zswap.c b/mm/zswap.c index 4732d2938f4a..aab8ec782d03 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -40,6 +40,7 @@ #include #include "swap.h" +#include "vswap.h" #include "internal.h" /********************************* @@ -269,6 +270,25 @@ static inline struct xarray *swap_zswap_tree(swp_entry_t swp) return zswap_tree(swp_type(swp), swp_offset(swp)); } +static struct zswap_entry *zswap_entry_load(swp_entry_t swp) +{ + if (is_vswap_entry(swp)) + return vswap_zswap_load(swp); + return xa_load(swap_zswap_tree(swp), swp_offset(swp)); +} + +static struct zswap_entry *zswap_entry_store(swp_entry_t swp, + struct zswap_entry *entry) +{ + if (is_vswap_entry(swp)) { + vswap_zswap_store(swp, entry); + return NULL; + } + + return xa_store(swap_zswap_tree(swp), swp_offset(swp), entry, + GFP_KERNEL); +} + #define zswap_pool_debug(msg, p) \ pr_debug("%s pool %s\n", msg, (p)->tfm_name) @@ -783,7 +803,7 @@ static void zswap_entry_cache_free(struct zswap_entry *entry) * Carries out the common pattern of freeing an entry's zsmalloc allocation, * freeing the entry itself, and decrementing the number of stored pages. */ -static void zswap_entry_free(struct zswap_entry *entry) +void zswap_entry_free(struct zswap_entry *entry) { struct zswap_pool *pool = zswap_entry_pool(entry); @@ -1322,6 +1342,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg)) return 0; + if (vswap_is_enabled()) + return 0; + /* * The shrinker resumes swap writeback, which will enter block * and may enter fs. XXX: Harmonize with vmscan.c __GFP_FS @@ -1404,6 +1427,8 @@ static struct shrinker *zswap_alloc_shrinker(void) * Return: 0 if at least one entry was written back, -EAGAIN if entries * were scanned but none could be written back, or -ENOENT if @memcg has * writeback disabled, is a zombie cgroup, or has empty zswap LRUs. + * + * Also returns -ENOENT when vswap is enabled. */ static int shrink_memcg(struct mem_cgroup *memcg) { @@ -1414,6 +1439,9 @@ static int shrink_memcg(struct mem_cgroup *memcg) if (!mem_cgroup_zswap_writeback_enabled(memcg)) return -ENOENT; + if (vswap_is_enabled()) + return -ENOENT; + /* * Skip zombies because their LRUs are reparented and we would be * reclaiming from the parent instead of the dead memcg. @@ -1446,6 +1474,13 @@ static void shrink_worker(struct work_struct *w) int ret, failures = 0, attempts = 0; unsigned long thr; + /* + * When vswap is enabled, zswap entries are almost all vswap backed, + * with no slot to write back to. + */ + if (vswap_is_enabled()) + return; + /* Reclaim down to the accept threshold */ thr = zswap_accept_thr_pages(); @@ -1549,15 +1584,13 @@ static bool zswap_store_page(struct folio *folio, long index, goto compress_failed; /* - * Set pool_idx before the xa_store() below publishes the entry, or a + * Set pool_idx before the store below publishes the entry, or a * concurrent reader could resolve a stale pool_idx left by slab reuse * to an unrelated live pool. */ entry->pool_idx = pool->idx; - old = xa_store(swap_zswap_tree(page_swpentry), - swp_offset(page_swpentry), - entry, GFP_KERNEL); + old = zswap_entry_store(page_swpentry, entry); if (xa_is_err(old)) { int err = xa_err(old); @@ -1623,7 +1656,7 @@ bool zswap_store(struct folio *folio) struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; bool ret = false; - long index; + long index = 0; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1676,14 +1709,21 @@ bool zswap_store(struct folio *folio) if (!ret && zswap_pool_reached_full) queue_work(shrink_wq, &zswap_shrink_work); check_old: + if (ret) + return ret; + /* * If the zswap store fails or zswap is disabled, we must invalidate * the possibly stale entries which were previously stored at the * offsets corresponding to each page of the folio. Otherwise, * writeback could overwrite the new data in the swapfile. */ - if (!ret) + if (is_vswap_entry(swp)) { + if (index > 0) + folio_release_vswap_backing(folio); + } else { zswap_invalidate(swp_type(swp), swp_offset(swp), nr_pages); + } return ret; } @@ -1700,8 +1740,18 @@ bool zswap_store(struct folio *folio) bool zswap_is_present(swp_entry_t entry, unsigned int nr) { pgoff_t offset = swp_offset(entry); - struct xarray *tree = swap_zswap_tree(entry); + struct xarray *tree; unsigned long index = offset; + unsigned int i; + + /* Virtual entries have no native zswap tree. */ + if (is_vswap_entry(entry)) { + for (i = 0; i < nr; i++) + if (vswap_zswap_load(swp_entry(swp_type(entry), offset + i))) + return true; + return false; + } + tree = swap_zswap_tree(entry); if (zswap_never_enabled() || xa_empty(tree)) return false; @@ -1737,8 +1787,7 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr) int zswap_load(struct folio *folio) { swp_entry_t swp = folio->swap; - pgoff_t offset = swp_offset(swp); - struct xarray *tree = swap_zswap_tree(swp); + struct swap_info_struct *si = __swap_entry_to_info(swp); struct zswap_entry *entry; VM_WARN_ON_ONCE(!folio_test_locked(folio)); @@ -1762,7 +1811,7 @@ int zswap_load(struct folio *folio) return -ENOENT; } - entry = xa_load(tree, offset); + entry = zswap_entry_load(swp); if (!entry) return -ENOENT; @@ -1785,8 +1834,13 @@ int zswap_load(struct folio *folio) * compression work. */ folio_mark_dirty(folio); - xa_erase(tree, offset); - zswap_entry_free(entry); + + if (swap_is_vswap(si)) { + folio_release_vswap_backing(folio); + } else { + xa_erase(swap_zswap_tree(swp), swp_offset(swp)); + zswap_entry_free(entry); + } folio_unlock(folio); return 0; @@ -1928,6 +1982,7 @@ static int zswap_setup(void) struct zswap_pool *pool; int ret; + BUILD_BUG_ON(__alignof__(struct zswap_entry) <= VTABLE_TAG_MASK); zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); if (!zswap_entry_cache) { pr_err("entry cache creation failed\n"); -- 2.53.1-Meta