Linux cgroups development
 help / color / mirror / Atom feed
From: Nhat Pham <nphamcs@gmail.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Chris Li" <chrisl@kernel.org>,
	"Kairui Song" <kasong@tencent.com>,
	"Kemeng Shi" <shikemeng@huaweicloud.com>,
	"Baoquan He" <baoquan.he@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	"Youngjun Park" <youngjun.park@lge.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Yosry Ahmed" <yosry@kernel.org>,
	"Chengming Zhou" <chengming.zhou@linux.dev>,
	"Qi Zheng" <qi.zheng@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Axel Rasmussen" <axelrasmussen@google.com>,
	"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Hugh Dickins" <hughd@google.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Karl Mehltretter" <kmehltretter@gmail.com>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
	"Marco Elver" <elver@google.com>,
	"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	"Jeff Layton" <jlayton@kernel.org>,
	linux-doc@vger.kernel.org, cgroups@vger.kernel.org,
	"Tejun Heo" <tj@kernel.org>, "Michal Koutný" <mkoutny@suse.com>
Subject: [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling
Date: Thu,  3 Dec 2026 14:00:07 -0800	[thread overview]
Message-ID: <20261203220010.1302207-18-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>

Ordinary swapcache folios and swap PTEs already use virtual
slots. Physical slots store reverse mappings or hibernation
reservations. Follow the virtual entry for swapin, writeout,
swapoff and cgroup accounting.

Release physical reservations independently of swapcache.
Store cgroup owners only on virtual ordinary swap slots.
Keep the physical device check when draining a swap area, and
check large-folio backing under the virtual cluster lock.

Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 .../admin-guide/cgroup-v1/memcg_test.rst      |   5 +-
 include/linux/swap.h                          |  23 +-
 mm/memcontrol-v1.c                            |  39 +--
 mm/memcontrol.c                               |  61 +----
 mm/memory.c                                   |  41 +--
 mm/page_io.c                                  |  53 ++--
 mm/shmem.c                                    |   8 +-
 mm/swap_state.c                               |  33 ++-
 mm/swapfile.c                                 | 254 ++++++------------
 mm/vmscan.c                                   |   9 +-
 mm/vswap.h                                    |  16 +-
 mm/zswap.c                                    |  61 +----
 12 files changed, 172 insertions(+), 431 deletions(-)

diff --git a/Documentation/admin-guide/cgroup-v1/memcg_test.rst b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
index 8d3fcba13450..f16098f32796 100644
--- a/Documentation/admin-guide/cgroup-v1/memcg_test.rst
+++ b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
@@ -43,9 +43,8 @@ Please note that implementation details can be changed.
 	mem_cgroup_uncharge()
 	  Called when a page's refcount goes down to 0.
 
-	mem_cgroup_dir_phys_swap_free()
-	  Called when swp_entry's refcnt goes down to 0. A charge against swap
-	  disappears.
+	mem_cgroup_swap_uncharge()
+	  Releases the swap-side charge on swap-in or slot free.
 
 3. charge-commit
 =======================
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 4fe3cabe6a39..99b63fa65853 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -570,23 +570,13 @@ static inline void mem_cgroup_swap_record(struct folio *folio,
 	__mem_cgroup_swap_record(folio, id);
 }
 
-void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages);
-static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
-						 unsigned int nr_pages)
-{
-	if (mem_cgroup_disabled())
-		return;
-	__mem_cgroup_dir_phys_swap_free(id, nr_pages);
-}
-
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
-			   bool is_vswap);
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages);
 static inline void mem_cgroup_swap_put(unsigned short id,
-				       unsigned int nr_pages, bool is_vswap)
+				      unsigned int nr_pages)
 {
 	if (mem_cgroup_disabled())
 		return;
-	__mem_cgroup_swap_put(id, nr_pages, is_vswap);
+	__mem_cgroup_swap_put(id, nr_pages);
 }
 
 long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
@@ -613,13 +603,8 @@ static inline void mem_cgroup_swap_record(struct folio *folio,
 {
 }
 
-static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
-						 unsigned int nr_pages)
-{
-}
-
 static inline void mem_cgroup_swap_put(unsigned short id,
-				       unsigned int nr_pages, bool is_vswap)
+				      unsigned int nr_pages)
 {
 }
 
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 1a55c168c3eb..570bb5006328 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -269,9 +269,9 @@ void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
  */
 void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
 {
+	struct swap_cluster_info_dynamic *ci_dyn;
 	struct mem_cgroup *memcg;
 	struct obj_cgroup *objcg;
-	struct swap_cluster_info_dynamic *ci_dyn;
 	unsigned int nr_entries, nr_swapfile;
 	unsigned int start, i;
 	unsigned short private_id;
@@ -303,15 +303,12 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
 	nr_entries = folio_nr_pages(folio);
 	start = swp_cluster_offset(folio->swap);
 	private_id = mem_cgroup_private_id_get(memcg, nr_entries);
-	nr_swapfile = nr_entries;
-	if (is_vswap_entry(folio->swap)) {
-		mod_memcg_state(memcg, MEMCG_VSWAP, nr_entries);
-		nr_swapfile = 0;
-		ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
-		for (i = start; i < start + nr_entries; i++)
-			if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
-				nr_swapfile++;
-	}
+	mod_memcg_state(memcg, MEMCG_VSWAP, nr_entries);
+	nr_swapfile = 0;
+	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+	for (i = start; i < start + nr_entries; i++)
+		if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+			nr_swapfile++;
 
 	if (nr_swapfile)
 		mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
@@ -354,12 +351,11 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
  */
 void memcg1_swapin(struct folio *folio)
 {
-	struct swap_cluster_info *ci;
 	struct swap_cluster_info_dynamic *ci_dyn;
+	struct swap_cluster_info *ci;
 	unsigned long nr_pages;
 	unsigned int start, i, nr_swapfile = 0;
 	unsigned short id;
-	bool is_vswap;
 
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -384,26 +380,19 @@ void memcg1_swapin(struct folio *folio)
 	 * let's not wait for it.  The page already received a
 	 * memory+swap charge, drop the swap entry duplicate.
 	 */
-	is_vswap = is_vswap_entry(folio->swap);
 	nr_pages = folio_nr_pages(folio);
 	start = swp_cluster_offset(folio->swap);
 	ci = swap_cluster_get_and_lock(folio);
 	id = __swap_cgroup_clear(ci, start, nr_pages);
-	if (id && is_vswap) {
-		ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
-		for (i = start; i < start + nr_pages; i++)
-			if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
-				nr_swapfile++;
-	}
+	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+	for (i = start; i < start + nr_pages; i++)
+		if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+			nr_swapfile++;
 	swap_cluster_unlock(ci);
 	if (!id)
 		return;
-	if (is_vswap) {
-		mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
-		mem_cgroup_swap_put(id, nr_pages, true);
-	} else {
-		mem_cgroup_dir_phys_swap_free(id, nr_pages);
-	}
+	mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
+	mem_cgroup_swap_put(id, nr_pages);
 }
 #endif
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 2a87f33bc56d..03f78a11138f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6053,27 +6053,24 @@ int __init mem_cgroup_init(void)
  */
 void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
 {
+	struct swap_cluster_info_dynamic *ci_dyn;
 	unsigned int start = swp_cluster_offset(folio->swap);
 	unsigned int end = start + folio_nr_pages(folio);
 	unsigned int i, nr = 0, nr_swapfile = 0;
 	struct mem_cgroup *memcg;
 	struct obj_cgroup *objcg;
-	struct swap_cluster_info_dynamic *ci_dyn;
 	unsigned short id;
-	bool is_vswap = is_vswap_entry(folio->swap);
 
 	if (mem_cgroup_disabled() || !do_memsw_account())
 		return;
 
 	lockdep_assert_held(&ci->lock);
-	if (is_vswap)
-		ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
 	for (i = start; i < end; i++) {
 		if (!__swp_tb_get_count(__swap_table_get(ci, i)))
 			continue;
 		nr++;
-		if (!is_vswap ||
-		    vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
+		if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE)
 			nr_swapfile++;
 	}
 	if (!nr)
@@ -6092,8 +6089,7 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
 		page_counter_charge(&memcg->memsw, nr);
 	if (nr_swapfile)
 		mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
-	if (is_vswap_entry(folio->swap))
-		mod_memcg_state(memcg, MEMCG_VSWAP, nr);
+	mod_memcg_state(memcg, MEMCG_VSWAP, nr);
 
 	for (i = start; i < end; i++)
 		if (__swp_tb_get_count(__swap_table_get(ci, i)))
@@ -6134,10 +6130,8 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
 	}
 
 	id = mem_cgroup_private_id_get(memcg, nr_pages);
-	if (is_vswap_entry(folio->swap)) {
-		memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
-		mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
-	}
+	memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+	mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
 	return id;
 }
 
@@ -6199,37 +6193,6 @@ void __mem_cgroup_swap_record(struct folio *folio, unsigned short id)
 	swap_cluster_unlock(ci);
 }
 
-/**
- * __mem_cgroup_dir_phys_swap_free - uncharge and drop the reference for
- * directly mapped physical swap entries
- * @id: cgroup id to uncharge
- * @nr_pages: number of directly mapped physical swap entries
- *
- * This is roughly equivalent to calling mem_cgroup_swap_uncharge()
- * followed by mem_cgroup_swap_put(). Reuse the resolved owner to avoid
- * a second lookup.
- */
-void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages)
-{
-	struct obj_cgroup *objcg;
-	struct mem_cgroup *memcg;
-
-	rcu_read_lock();
-	objcg = obj_cgroup_from_private_id(id);
-	memcg = obj_cgroup_memcg(objcg);
-	if (memcg) {
-		if (!mem_cgroup_private_id_is_root(id)) {
-			if (do_memsw_account())
-				page_counter_uncharge(&memcg->memsw, nr_pages);
-			else
-				page_counter_uncharge(&memcg->swap, nr_pages);
-		}
-		mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
-		__mem_cgroup_private_id_put(objcg, id, nr_pages);
-	}
-	rcu_read_unlock();
-}
-
 /**
  * __mem_cgroup_swap_uncharge - release cgroup charges for a vswap range
  * @id: the private ID to uncharge (may be 0)
@@ -6275,21 +6238,17 @@ void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
 }
 
 /**
- * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
+ * __mem_cgroup_swap_put - drop accounting and private ID refs for virtual slots
  * @id: the pinned private ID
  * @nr_pages: number of refs to drop
- * @is_vswap: release virtual-swap accounting along with the refs
  */
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
-			   bool is_vswap)
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
 {
 	struct mem_cgroup *memcg;
 
 	guard(rcu)();
-	if (is_vswap) {
-		memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
-		mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
-	}
+	memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
+	mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
 	mem_cgroup_private_id_put(id, nr_pages);
 }
 
diff --git a/mm/memory.c b/mm/memory.c
index ae9d5e69622f..ef443d569419 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4746,8 +4746,7 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)
  * folio_free_swap only frees the swap cache to release the slot if swap
  * count is zero, so we don't need to check the swap count here.
  */
-static inline bool should_try_to_free_swap(struct swap_info_struct *si,
-					   struct folio *folio,
+static inline bool should_try_to_free_swap(struct folio *folio,
 					   struct vm_area_struct *vma,
 					   bool exclusive,
 					   unsigned int fault_flags)
@@ -4766,7 +4765,7 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
 	 * are fast, and meanwhile, swap cache pinning the slot deferring the
 	 * release of metadata or fragmentation is a more critical issue.
 	 */
-	if (swap_entry_backend_has_flag(si, folio->swap, SWP_SYNCHRONOUS_IO))
+	if (swap_entry_backend_has_flag(folio->swap, SWP_SYNCHRONOUS_IO))
 		return true;
 	if (mem_cgroup_swap_full(folio) || (vma->vm_flags & VM_LOCKED) ||
 	    folio_test_mlocked(folio))
@@ -4899,8 +4898,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
 	return orders;
 }
 
-static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
-						struct vm_fault *vmf)
+static unsigned long thp_swapin_suitable_orders(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	unsigned long orders;
@@ -4919,25 +4917,6 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
 
 	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.
-	 *
-	 * Vswap entries are checked later, under the cluster lock in
-	 * __swap_cache_add_check().
-	 */
-	if (!is_vswap_entry(entry) && !zswap_never_enabled())
-		return 0;
-
-	/*
-	 * Neighbouring slots of a swap file can lie in two extents, and a folio
-	 * is read with one bio. The flag is set in swapon() and stays, so the
-	 * race is benign.
-	 */
-	if (!is_vswap_entry(entry) && !data_race(si->flags & SWP_BLKDEV))
-		return 0;
-
 	/*
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * and suitable for swapping THP.
@@ -4973,8 +4952,7 @@ static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
 	return orders;
 }
 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */
-static unsigned long thp_swapin_suitable_orders(struct swap_info_struct *si,
-						struct vm_fault *vmf)
+static unsigned long thp_swapin_suitable_orders(struct vm_fault *vmf)
 {
 	return 0;
 }
@@ -5114,10 +5092,9 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 	if (!folio) {
 		/* Swapin bypasses readahead for SWP_SYNCHRONOUS_IO devices and zswap */
 		if (swap_entry_synchronous(si, entry) ||
-		    (is_vswap_entry(entry) &&
-		     swap_entry_backend_has_flag(si, entry, SWP_SYNCHRONOUS_IO)))
+		    swap_entry_backend_has_flag(entry, SWP_SYNCHRONOUS_IO))
 			folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
-					    thp_swapin_suitable_orders(si, vmf) | BIT(0),
+					    thp_swapin_suitable_orders(vmf) | BIT(0),
 					    vmf, NULL, 0);
 		else
 			folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, vmf);
@@ -5251,7 +5228,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 	 * large folio is fully applicable before use.
 	 */
 	if (!swap_cache_folio_mappable(folio, nr_pages)) {
-		if (!is_vswap_entry(entry) || !folio_test_dirty(folio))
+		if (!folio_test_dirty(folio))
 			swap_cache_del_folio(folio);
 		goto out_nomap;
 	}
@@ -5275,7 +5252,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 			 */
 			exclusive = true;
 		} else if (exclusive && folio_test_writeback(folio) &&
-			  swap_entry_backend_has_flag(si, entry, SWP_STABLE_WRITES)) {
+			  swap_entry_backend_has_flag(entry, SWP_STABLE_WRITES)) {
 			/*
 			 * This is tricky: not all swap backends support
 			 * concurrent page modifications while under writeback.
@@ -5378,7 +5355,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 	 * Do it after mapping, so raced page faults will likely see the folio
 	 * in swap cache and wait on the folio lock.
 	 */
-	if (should_try_to_free_swap(si, folio, vma, exclusive, vmf->flags))
+	if (should_try_to_free_swap(folio, vma, exclusive, vmf->flags))
 		folio_free_swap(folio);
 
 	folio_unlock(folio);
diff --git a/mm/page_io.c b/mm/page_io.c
index 854b0b71581d..0e90acc57c87 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -168,11 +168,9 @@ static void swap_zeromap_folio_set(struct folio *folio)
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
 
 	ci = swap_cluster_get_and_lock(folio);
-	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);
-	}
+	/* Drop any backing from an earlier swapout. */
+	voff = swp_cluster_offset(entry);
+	__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++;
@@ -209,7 +207,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
  */
 int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 {
-	swp_entry_t phys = folio->swap;
+	swp_entry_t phys;
 	int ret = 0;
 
 	if (folio_free_swap(folio))
@@ -231,18 +229,7 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 	 * acquired internally by swap_zeromap_folio_set/clear.
 	 */
 	if (is_folio_zero_filled(folio)) {
-		swp_entry_t entry = folio->swap;
-
 		swap_zeromap_folio_set(folio);
-
-		/*
-		 * The folio may have been stored in zswap before and dirtied
-		 * again while it stayed in the swap cache. Drop the old copy,
-		 * or zswap writeback would bring it back into the swap cache.
-		 */
-		if (!is_vswap_entry(entry))
-			zswap_invalidate(swp_type(entry), swp_offset(entry),
-					 folio_nr_pages(folio));
 		goto out_unlock;
 	}
 
@@ -254,13 +241,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 	swap_zeromap_folio_clear(folio);
 
 	/*
-	 * For vswap: release stale non-swapfile backings (e.g. ZSWAP from a
+	 * Release stale non-swapfile backings (e.g. ZSWAP from a
 	 * previous swapout cycle) so zswap_store or folio_realloc_swap
 	 * starts on clean slots. Contiguous PHYS backing is preserved for
 	 * reuse by folio_realloc_swap.
 	 */
-	if (is_vswap_entry(folio->swap))
-		folio_release_non_phys_swap_backing(folio);
+	folio_release_non_phys_swap_backing(folio);
 
 	if (zswap_store(folio)) {
 		count_mthp_stat(folio_order(folio), MTHP_STAT_ZSWPOUT);
@@ -275,12 +261,10 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 	}
 	rcu_read_unlock();
 
-	if (is_vswap_entry(folio->swap)) {
-		phys = folio_realloc_swap(folio);
-		if (!phys.val) {
-			folio_mark_dirty(folio);
-			return AOP_WRITEPAGE_ACTIVATE;
-		}
+	phys = folio_realloc_swap(folio);
+	if (!phys.val) {
+		folio_mark_dirty(folio);
+		return AOP_WRITEPAGE_ACTIVATE;
 	}
 
 	if (ctx->nofs &&
@@ -505,12 +489,11 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 
 void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
 {
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-	bool synchronous = sis->flags & SWP_SYNCHRONOUS_IO;
+	bool synchronous = false;
 	bool workingset = folio_test_workingset(folio);
 	swp_entry_t phys;
 
-	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio) && !synchronous, folio);
+	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
 	VM_BUG_ON_FOLIO(folio_test_uptodate(folio), folio);
 
@@ -519,14 +502,10 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
 	 * folio->swap virtual, so map it to its physical backing; a folio with
 	 * no backing has nothing to read.
 	 */
-	if (swap_is_vswap(sis)) {
-		phys = vswap_to_phys(folio->swap);
-		if (phys.val)
-			synchronous = __swap_entry_to_info(phys)->flags &
-				      SWP_SYNCHRONOUS_IO;
-	} else {
-		phys = folio->swap;
-	}
+	phys = vswap_to_phys(folio->swap);
+	if (phys.val)
+		synchronous = __swap_entry_to_info(phys)->flags &
+			      SWP_SYNCHRONOUS_IO;
 
 	/*
 	 * A batch this folio can't join is submitted before the pressure
diff --git a/mm/shmem.c b/mm/shmem.c
index a9000a443674..8edbb84b1e4a 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2574,7 +2574,6 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 	struct folio *folio = NULL;
 	int error, nr_pages, order;
 	pgoff_t offset;
-	bool vswap_unuse;
 
 	VM_BUG_ON(!*foliop || !xa_is_value(*foliop));
 	index_entry = radix_to_swp_entry(*foliop);
@@ -2603,16 +2602,15 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 		swap = swp_entry(swp_type(swap), swp_offset(swap) + offset);
 	}
 	target_swap = swap;
-	vswap_unuse = swapoff_type >= 0 && swap_is_vswap(si);
 
 	/* Look it up and read it in.. */
 	folio = swap_cache_get_folio(swap);
 	if (!folio) {
-		if (vswap_unuse ||
-		    swap_entry_backend_has_flag(si, swap, SWP_SYNCHRONOUS_IO)) {
+		if (swapoff_type >= 0 ||
+		    swap_entry_backend_has_flag(swap, SWP_SYNCHRONOUS_IO)) {
 			folio = shmem_swap_alloc_folio(inode, vmf, index,
 						       swap,
-						       vswap_unuse ? 0 : order,
+						       swapoff_type >= 0 ? 0 : order,
 						       gfp);
 		} else {
 			/* Cached swapin only supports order 0 folio */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index f7df517cf66a..b03871d63408 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -210,28 +210,25 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,
 		return 0;
 
 	/*
-	 * For a vswap entry batch, reject if the backing is not THP-amenable
+	 * Reject a batch if its backing is not THP-amenable
 	 * (e.g. uniformly ZSWAP, or mixed). The order-fallback loop in
 	 * __swap_cache_alloc_folio will retry with a smaller order on -EBUSY.
 	 */
-	if (is_vswap_entry(targ_entry)) {
-		ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
-		ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr),
-					    nr, &type);
-		if (ret != nr || type == VSWAP_ZSWAP)
+	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
+	ret = __vswap_check_backing(ci_dyn, round_down(ci_off, nr), nr, &type);
+	if (ret != nr || type == VSWAP_ZSWAP)
+		return -EBUSY;
+	if (type == VSWAP_SWAPFILE) {
+		struct swap_info_struct *si;
+		swp_entry_t phys;
+		unsigned long vt;
+
+		vt = __vtable_get(ci_dyn, round_down(ci_off, nr));
+		phys = vtable_to_phys(vt);
+		si = __swap_entry_to_info(phys);
+		/* Swapfile extents can end inside a large folio. */
+		if (!data_race(si->flags & SWP_BLKDEV))
 			return -EBUSY;
-		if (type == VSWAP_SWAPFILE) {
-			struct swap_info_struct *si;
-			swp_entry_t phys;
-			unsigned long vt;
-
-			vt = __vtable_get(ci_dyn, round_down(ci_off, nr));
-			phys = vtable_to_phys(vt);
-			si = __swap_entry_to_info(phys);
-			/* Swapfile extents can end inside a large folio. */
-			if (!data_race(si->flags & SWP_BLKDEV))
-				return -EBUSY;
-		}
 	}
 
 	is_zero = __swap_table_test_zero(ci, ci_off);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 72652a2099e9..8f38737d8aa6 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -482,7 +482,7 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
  * 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, bool direct,
+				    struct swap_cluster_info *ci,
 				    gfp_t gfp, bool locked)
 {
 	struct swap_table *table = NULL;
@@ -507,19 +507,8 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
 		goto lock;
 
 #ifdef CONFIG_MEMCG
-	/*
-	 * Backing-only clusters keep quota owners on the virtual cluster.
-	 * Physical owner metadata is prepared on demand for direct use
-	 * and physical-slot statistics.
-	 */
-	if ((!vswap_is_enabled() || vswap || direct) &&
-	    !mem_cgroup_disabled()) {
-		gfp_t memcg_gfp = gfp;
-
-		if (direct && !vswap)
-			memcg_gfp &= ~__GFP_NOMEMALLOC;
-
-		memcg_table = kzalloc_obj(*ci->memcg_table, memcg_gfp);
+	if (vswap && !mem_cgroup_disabled()) {
+		memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
 		if (!memcg_table)
 			goto lock;
 	}
@@ -614,7 +603,7 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
  */
 static struct swap_cluster_info *
 swap_cluster_populate(struct swap_info_struct *si,
-			 struct swap_cluster_info *ci, bool direct)
+			 struct swap_cluster_info *ci)
 {
 	int ret;
 
@@ -629,7 +618,7 @@ swap_cluster_populate(struct swap_info_struct *si,
 		lockdep_assert_held(&si->global_cluster_lock);
 	lockdep_assert_held(&ci->lock);
 
-	if (!swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
+	if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
 					      __GFP_NOWARN, true))
 		return ci;
 
@@ -643,8 +632,8 @@ swap_cluster_populate(struct swap_info_struct *si,
 		spin_unlock(&si->global_cluster_lock);
 	local_unlock(&percpu_swap_cluster.lock);
 
-	ret = swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
-					     GFP_KERNEL, false);
+	ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+					       GFP_KERNEL, false);
 
 	/*
 	 * Back to atomic context. We might have migrated to a new CPU with a
@@ -726,7 +715,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
  * this returns NULL for an non-empty list.
  */
 static struct swap_cluster_info *isolate_lock_cluster(
-		struct swap_info_struct *si, struct list_head *list, bool direct)
+		struct swap_info_struct *si, struct list_head *list)
 {
 	struct swap_cluster_info *ci, *found = NULL;
 	u8 flags = CLUSTER_FLAG_NONE;
@@ -758,7 +747,7 @@ static struct swap_cluster_info *isolate_lock_cluster(
 	if (found && flags == CLUSTER_FLAG_FREE) {
 		VM_WARN_ON_ONCE(list != &si->free_clusters);
 		VM_WARN_ON_ONCE(cluster_table_is_alloced(found));
-		return swap_cluster_populate(si, found, direct);
+		return swap_cluster_populate(si, found);
 	}
 
 	return found;
@@ -933,7 +922,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
 	need_table = !ci->count;
 	/* Need to allocate swap table first for initial bad slot marking. */
 	if (need_table) {
-		ret = swap_cluster_alloc_table(alloc_si, ci, false, GFP_KERNEL, false);
+		ret = swap_cluster_alloc_table(alloc_si, ci, GFP_KERNEL, false);
 		if (ret)
 			goto out;
 	} else {
@@ -1125,13 +1114,14 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
 		order = folio_order(folio);
 		nr_pages = 1 << order;
 		swap_cluster_assert_empty(ci, ci_off, nr_pages, false);
-		if (folio_test_swapcache(folio)) {
+		if (!swap_is_vswap(si)) {
 			/*
 			 * Folio already in the swap cache: we are allocating
 			 * physical backing for its vswap entry. Point each
 			 * physical slot back at its own vswap entry
 			 * (Vswap-tagged rmap).
 			 */
+			VM_WARN_ON(!folio_test_swapcache(folio));
 			VM_WARN_ON(!is_vswap_entry(folio->swap));
 			vswap_entry = folio->swap;
 			for (i = 0; i < nr_pages; i++) {
@@ -1187,8 +1177,7 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
 static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 					    struct swap_cluster_info *ci,
 					    struct folio *folio,
-					    unsigned long offset,
-					    bool *nomem)
+					    unsigned long offset)
 {
 	unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
 	unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
@@ -1217,19 +1206,6 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 			if (!ret)
 				continue;
 		}
-#ifdef CONFIG_MEMCG
-		/* Hibernation needs owner metadata before publishing a raw slot. */
-		if (vswap_is_enabled() && !folio &&
-		    !mem_cgroup_disabled() && !ci->memcg_table) {
-			ci->memcg_table = kzalloc_obj(*ci->memcg_table,
-						      GFP_ATOMIC | __GFP_NOWARN);
-			if (!ci->memcg_table) {
-				if (nomem)
-					*nomem = true;
-				goto out;
-			}
-		}
-#endif
 		if (!__swap_cluster_alloc_entries(si, ci, folio, offset % SWAPFILE_CLUSTER))
 			break;
 		found = offset;
@@ -1239,15 +1215,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 		break;
 	}
 out:
-	/*
-	 * On a discard-capable device, relocating a cluster whose memcg_table
-	 * allocation failed queues a discard for slots that were never used,
-	 * which folio_alloc_phys_swap() reads as progress and retries on.
-	 */
-	if (nomem && *nomem && !ci->count)
-		__free_cluster(si, ci);
-	else
-		relocate_cluster(si, ci);
+	relocate_cluster(si, ci);
 	swap_cluster_unlock(ci);
 	if (swap_is_vswap(si)) {
 		this_cpu_write(percpu_vswap_cluster.offset[order], next);
@@ -1268,25 +1236,18 @@ static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
 					 bool scan_all)
 {
 	unsigned int found = SWAP_ENTRY_INVALID;
-	bool nomem = false;
-	bool direct = !folio || !folio_test_swapcache(folio);
 
-	/*
-	 * In rare cases alloc_swap_scan_cluster() can fail due to
-	 * memcg_table allocation failure. Short-circuit to avoid looping
-	 * over the list indefinitely.
-	 */
 	do {
-		struct swap_cluster_info *ci = isolate_lock_cluster(si, list, direct);
+		struct swap_cluster_info *ci = isolate_lock_cluster(si, list);
 		unsigned long offset;
 
 		if (!ci)
 			break;
 		offset = cluster_offset(si, ci);
-		found = alloc_swap_scan_cluster(si, ci, folio, offset, &nomem);
+		found = alloc_swap_scan_cluster(si, ci, folio, offset);
 		if (found)
 			break;
-	} while (scan_all && !nomem);
+	} while (scan_all);
 
 	return found;
 }
@@ -1311,7 +1272,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
 	ci = &ci_dyn->ci;
 	spin_lock(&ci->lock);
 
-	if (swap_cluster_alloc_table(si, ci, false, GFP_ATOMIC | __GFP_NOWARN, true)) {
+	if (swap_cluster_alloc_table(si, ci, GFP_ATOMIC | __GFP_NOWARN, true)) {
 		spin_unlock(&ci->lock);
 		kfree(ci_dyn);
 		return SWAP_ENTRY_INVALID;
@@ -1335,7 +1296,7 @@ static unsigned int vswap_alloc_cluster(struct swap_info_struct *si,
 	}
 
 	offset = cluster_offset(si, ci);
-	return alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
+	return alloc_swap_scan_cluster(si, ci, folio, offset);
 }
 
 static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
@@ -1350,7 +1311,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 	if (force)
 		to_scan = swap_usage_in_pages(si) / SWAPFILE_CLUSTER;
 
-	while ((ci = isolate_lock_cluster(si, &si->full_clusters, false))) {
+	while ((ci = isolate_lock_cluster(si, &si->full_clusters))) {
 		offset = cluster_offset(si, ci);
 		end = min(si->max, offset + SWAPFILE_CLUSTER);
 		to_scan--;
@@ -1437,8 +1398,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 		if (cluster_is_usable(ci, order)) {
 			if (cluster_is_empty(ci))
 				offset = cluster_offset(si, ci);
-			found = alloc_swap_scan_cluster(si, ci, folio, offset,
-							NULL);
+			found = alloc_swap_scan_cluster(si, ci, folio, offset);
 		} else {
 			swap_cluster_unlock(ci);
 		}
@@ -1714,7 +1674,7 @@ static swp_entry_t swap_alloc_fast(struct folio *folio)
 	if (ci && cluster_is_usable(ci, order)) {
 		if (cluster_is_empty(ci))
 			offset = cluster_offset(si, ci);
-		found = alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
+		found = alloc_swap_scan_cluster(si, ci, folio, offset);
 	} else if (ci) {
 		swap_cluster_unlock(ci);
 	}
@@ -1943,8 +1903,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
 			}
 			/* count will be 0 after put, slot can be reclaimed */
 			need_reclaim = true;
-			if (swap_is_vswap(si))
-				vswap_mark_cache_only(ci, ci_off);
+			vswap_mark_cache_only(ci, ci_off);
 		}
 		/*
 		 * A count != 1 or cached slot can't be freed. Put its swap
@@ -2051,8 +2010,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
 			goto failed;
 		}
 	} while (++ci_off < ci_end);
-	if (swap_is_vswap(si))
-		vswap_clear_cache_only(ci, ci_start, nr);
+	vswap_clear_cache_only(ci, ci_start, nr);
 	swap_cluster_unlock(ci);
 	return 0;
 failed:
@@ -2114,8 +2072,7 @@ static bool vswap_alloc(struct folio *folio)
 		if (ci && cluster_is_usable(ci, order)) {
 			if (cluster_is_empty(ci))
 				offset = cluster_offset(vswap_si, ci);
-			alloc_swap_scan_cluster(vswap_si, ci, folio, offset,
-						NULL);
+			alloc_swap_scan_cluster(vswap_si, ci, folio, offset);
 		} else if (ci) {
 			swap_cluster_unlock(ci);
 		}
@@ -2136,37 +2093,8 @@ static bool vswap_alloc(struct folio *folio)
 	return false;
 }
 
-#ifdef CONFIG_MEMCG
-static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
-{
-	struct swap_cluster_info *ci = __swap_entry_to_cluster(entry);
-	struct swap_memcg_table *table __free(kfree) = NULL;
-
-	if (mem_cgroup_disabled() || READ_ONCE(ci->memcg_table))
-		return 0;
-
-	/* The caller's locked folio pins the slots while allocation sleeps. */
-	table = kzalloc_obj(*table, GFP_ATOMIC | __GFP_NOWARN);
-	if (!table)
-		table = kzalloc_obj(*table, __GFP_HIGH | GFP_KERNEL);
-	guard(spinlock)(&ci->lock);
-	if (ci->memcg_table)
-		return 0;
-	if (!table)
-		return -ENOMEM;
-	WRITE_ONCE(ci->memcg_table, no_free_ptr(table));
-	return 0;
-}
-#else
-static int swap_cluster_alloc_memcg_table(swp_entry_t entry)
-{
-	return 0;
-}
-#endif
-
 static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
 {
-	bool direct = !folio_test_swapcache(folio);
 	swp_entry_t entry;
 
 again:
@@ -2178,13 +2106,6 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio)
 
 	if (!entry.val && !folio_order(folio) && swap_sync_discard())
 		goto again;
-	if (!entry.val)
-		return entry;
-
-	if (direct && swap_cluster_alloc_memcg_table(entry)) {
-		swap_cache_del_folio(folio);
-		return (swp_entry_t){};
-	}
 	return entry;
 }
 
@@ -2516,6 +2437,7 @@ swp_entry_t folio_realloc_swap(struct folio *folio)
 		return (swp_entry_t){};
 	}
 
+
 	if (mem_cgroup_swap_charge(memcg_id, nr)) {
 		__swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry),
 						 __swap_entry_to_cluster(phys_entry),
@@ -2686,42 +2608,40 @@ static void __swap_cluster_finish_free(struct swap_info_struct *si,
 		partial_free_cluster(si, ci);
 }
 
-/*
- * Free physical swap slots that were backing vswap entries (Vswap-tagged).
- */
-static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
-					     struct swap_cluster_info *pci,
-					     unsigned int ci_start,
-					     unsigned int nr_pages)
+/* Backing owners live on virtual entries; raw hibernation is root-owned. */
+static void __swap_cluster_free_phys_slots(struct swap_info_struct *si,
+					   struct swap_cluster_info *ci,
+					   unsigned int ci_start,
+					   unsigned int nr_pages)
 {
 	unsigned int ci_off;
 
-	spin_lock_nested(&pci->lock, SINGLE_DEPTH_NESTING);
-	VM_WARN_ON(pci->count < nr_pages);
-	pci->count -= nr_pages;
+	lockdep_assert_held(&ci->lock);
+	VM_WARN_ON(ci->count < nr_pages);
+	ci->count -= nr_pages;
 	for (ci_off = ci_start; ci_off < ci_start + nr_pages; ci_off++) {
-		__swap_table_set(pci, ci_off, null_to_swp_tb());
+		__swap_table_set(ci, ci_off, null_to_swp_tb());
 		if (!SWAP_TABLE_HAS_ZEROFLAG)
-			__swap_table_clear_zero(pci, ci_off);
+			__swap_table_clear_zero(ci, ci_off);
 	}
-	__swap_cluster_finish_free(psi, pci, ci_start, nr_pages);
-	swap_cluster_unlock(pci);
+	__swap_cluster_finish_free(si, ci, ci_start, nr_pages);
 }
 
 /*
- * Release cgroup accounting for freed swap slots. Physical backing of
- * vswap entries was already uncharged by __vswap_release_backing().
+ * Free physical swap slots that were backing vswap entries (Vswap-tagged).
  */
-static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap)
+static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
+					     struct swap_cluster_info *pci,
+					     unsigned int ci_start,
+					     unsigned int nr_pages)
 {
-	if (is_vswap)
-		mem_cgroup_swap_put(id, nr, true);
-	else
-		mem_cgroup_dir_phys_swap_free(id, nr);
+	spin_lock_nested(&pci->lock, SINGLE_DEPTH_NESTING);
+	__swap_cluster_free_phys_slots(psi, pci, ci_start, nr_pages);
+	swap_cluster_unlock(pci);
 }
 
 /*
- * Free a set of swap slots after their swap count dropped to zero, or will be
+ * Free virtual swap slots after their swap count dropped to zero, or will be
  * zero after putting the last ref (saves one __swap_cluster_put_entry call).
  */
 void __swap_cluster_free_entries(struct swap_info_struct *si,
@@ -2732,23 +2652,20 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
 	unsigned short batch_id = 0, id_cur;
 	unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages;
 	unsigned int batch_off = ci_off;
-	bool is_vswap = swap_is_vswap(si);
 
 	VM_WARN_ON(ci->count < nr_pages);
 
-	if (is_vswap)
-		__vswap_release_backing(ci, ci_start, nr_pages);
+	__vswap_release_backing(ci, ci_start, nr_pages);
 
 	ci->count -= nr_pages;
 	do {
 		old_tb = __swap_table_get(ci, ci_off);
 		/*
 		 * Freeing is done after release of the last swap count
-		 * ref, or after swap cache is dropped. A hibernation slot
-		 * has no count and is freed directly by its owner.
+		 * ref, or after swap cache is dropped.
 		 */
-		VM_WARN_ON(!swp_tb_is_hibernation(old_tb) &&
-			   (!swp_tb_is_shadow(old_tb) || __swp_tb_get_count(old_tb) > 1));
+		VM_WARN_ON(!swp_tb_is_shadow(old_tb) ||
+			   __swp_tb_get_count(old_tb) > 1);
 
 		/* Resetting the slot to NULL also clears the inline flags. */
 		__swap_table_set(ci, ci_off, null_to_swp_tb());
@@ -2756,20 +2673,22 @@ void __swap_cluster_free_entries(struct swap_info_struct *si,
 			__swap_table_clear_zero(ci, ci_off);
 
 		/*
-		 * Uncharge swap slots by memcg in batches. Consecutive
-		 * slots with the same cgroup id are uncharged together.
+		 * Release cgroup accounting for freed swap slots. Physical
+		 * backing of vswap entries was already uncharged by
+		 * __vswap_release_backing().
 		 */
 		id_cur = __swap_cgroup_clear(ci, ci_off, 1);
 		if (batch_id != id_cur) {
 			if (batch_id)
-				memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
+				mem_cgroup_swap_put(batch_id,
+						    ci_off - batch_off);
 			batch_id = id_cur;
 			batch_off = ci_off;
 		}
 	} while (++ci_off < ci_end);
 
 	if (batch_id)
-		memcg_swap_free(batch_id, ci_off - batch_off, is_vswap);
+		mem_cgroup_swap_put(batch_id, ci_off - batch_off);
 
 	__swap_cluster_finish_free(si, ci, ci_start, nr_pages);
 }
@@ -2986,16 +2905,17 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
 	if (pcp_si == si && pcp_offset) {
 		ci = swap_cluster_lock(si, pcp_offset);
 		if (cluster_is_usable(ci, 0))
-			offset = alloc_swap_scan_cluster(si, ci, NULL,
-							 pcp_offset, NULL);
+			offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
 		else
 			swap_cluster_unlock(ci);
 	}
 	if (!offset)
 		offset = cluster_alloc_swap_entry(si, NULL);
 	local_unlock(&percpu_swap_cluster.lock);
-	if (offset)
+	if (offset) {
 		entry = swp_entry(si->type, offset);
+		mod_memcg_state(root_mem_cgroup, MEMCG_SWAP, 1);
+	}
 
 fail:
 	return entry;
@@ -3012,10 +2932,13 @@ void swap_free_hibernation_slot(swp_entry_t entry)
 	struct swap_info_struct *si = __swap_entry_to_info(entry);
 	struct swap_cluster_info *ci;
 	pgoff_t offset = swp_offset(entry);
+	unsigned int ci_off = offset % SWAPFILE_CLUSTER;
 
 	ci = swap_cluster_lock(si, offset);
-	__swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1);
+	VM_WARN_ON(!swp_tb_is_hibernation(__swap_table_get(ci, ci_off)));
+	__swap_cluster_free_phys_slots(si, ci, ci_off, 1);
 	swap_cluster_unlock(ci);
+	mod_memcg_state(root_mem_cgroup, MEMCG_SWAP, -1);
 }
 
 static int __find_hibernation_swap_type(dev_t device, sector_t offset)
@@ -3288,16 +3211,11 @@ bool swap_entry_is_backed_by(swp_entry_t entry, unsigned int type)
 
 	if (!softleaf_is_swap(entry))
 		return false;
-	/* Direct entries retain their physical slots while zswapped. */
-	if (swp_type(entry) == type)
-		return true;
 	si = get_swap_device(entry);
 	if (IS_ERR_OR_NULL(si))
 		return false;
-	if (swap_is_vswap(si)) {
-		phys = vswap_to_phys(entry);
-		match = phys.val && swp_type(phys) == type;
-	}
+	phys = vswap_to_phys(entry);
+	match = phys.val && swp_type(phys) == type;
 	put_swap_device(si);
 	return match;
 }
@@ -3326,16 +3244,6 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 	    !swap_entry_is_backed_by(entry, type))
 		return 0;
 
-	/*
-	 * A large folio that a fault didn't map can't be mapped one PTE at a
-	 * time here, so drop it and swap in small folios instead.
-	 */
-	if (unlikely(!is_vswap_entry(entry) &&
-		     !swap_cache_folio_mappable(folio, 1))) {
-		swap_cache_del_folio(folio);
-		return 0;
-	}
-
 	swapcache = folio;
 	folio = ksm_might_need_to_copy(folio,
 				       folio_file_page(folio, swp_offset(entry)), vma, addr);
@@ -3373,8 +3281,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 		goto setpte;
 	}
 
-	if (is_vswap_entry(entry) && !folio_test_anon(folio) &&
-	    folio_test_large(folio)) {
+	if (!folio_test_anon(folio) && folio_test_large(folio)) {
 		if (folio_test_dirty(folio)) {
 			ret = -EBUSY;
 			goto out;
@@ -3484,15 +3391,10 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 				.pmd = pmd,
 			};
 
-			if (is_vswap_entry(entry)) {
-				folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE,
-						    BIT(0), &vmf, NULL, 0);
-				if (IS_ERR(folio))
-					folio = NULL;
-			} else {
-				folio = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE,
-							&vmf);
-			}
+			folio = swapin_sync(entry, GFP_HIGHUSER_MOVABLE, BIT(0),
+					    &vmf, NULL, 0);
+			if (IS_ERR(folio))
+				folio = NULL;
 		}
 		if (!folio) {
 			swp_tb = swap_table_lookup(entry);
@@ -3750,14 +3652,12 @@ static int try_to_unuse(unsigned int type)
 		 */
 		folio_lock(folio);
 		folio_wait_writeback(folio);
-		if (is_vswap_entry(entry)) {
-			if (!folio_matches_swap_entry(folio, entry))
-				goto unlock_cache;
-			phys = vswap_to_phys(entry);
-			if (!phys.val || swp_type(phys) != type ||
-			    swp_offset(phys) != i)
-				goto unlock_cache;
-		}
+		if (!folio_matches_swap_entry(folio, entry))
+			goto unlock_cache;
+		phys = vswap_to_phys(entry);
+		if (!phys.val || swp_type(phys) != type ||
+		    swp_offset(phys) != i)
+			goto unlock_cache;
 		folio_free_swap(folio);
 unlock_cache:
 		folio_unlock(folio);
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 42ab70acdf5c..ffe2795ce495 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1026,12 +1026,9 @@ static bool may_enter_fs(struct folio *folio, gfp_t gfp_mask)
 	if (!folio_test_swapcache(folio) || !(gfp_mask & __GFP_IO))
 		return false;
 
-	entry = folio->swap;
-	if (is_vswap_entry(entry)) {
-		entry = vswap_to_phys(entry);
-		if (!entry.val)
-			return true;
-	}
+	entry = vswap_to_phys(folio->swap);
+	if (!entry.val)
+		return true;
 
 	return !(__swap_entry_to_info(entry)->ops->flags &
 		 SWAP_OPS_F_REQUIRE_NOFS);
diff --git a/mm/vswap.h b/mm/vswap.h
index c02aa8f6da57..f3a55414c6b7 100644
--- a/mm/vswap.h
+++ b/mm/vswap.h
@@ -341,8 +341,7 @@ static inline int vswap_check_backing(swp_entry_t entry, int nr,
  *                          range of physical swap slots.
  * @folio: a swap-cache resident folio
  *
- * Return: %true if @folio->swap is not a vswap entry, or if these vswap
- * entries are backed by a contiguous range of physical slots.
+ * Return: %true if the folio has a contiguous range of physical slots.
  */
 static inline bool folio_phys_swap_backed(struct folio *folio)
 {
@@ -350,9 +349,8 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
 	int nr = folio_nr_pages(folio);
 	enum vswap_backing_type type;
 
-	return !is_vswap_entry(entry) ||
-	       (vswap_check_backing(entry, nr, &type) == nr &&
-		type == VSWAP_SWAPFILE);
+	return vswap_check_backing(entry, nr, &type) == nr &&
+	       type == VSWAP_SWAPFILE;
 }
 
 static inline int vswap_cluster_alloc_vtable(struct swap_cluster_info_dynamic *ci_dyn,
@@ -403,22 +401,18 @@ static inline bool folio_phys_swap_backed(struct folio *folio)
 
 /*
  * Test a per-backend swap flag (SWP_SYNCHRONOUS_IO, SWP_STABLE_WRITES, ...)
- * for @entry. For a vswap entry the property belongs to the current
+ * for the virtual @entry. The property belongs to the current
  * physical backing rather than vswap_si itself; resolve to the backing
  * and test there. Returns false for zswap/zero/unbacked vswap entries
  * as they don't have a backing bdev.
  */
-static inline bool swap_entry_backend_has_flag(struct swap_info_struct *si,
-					       swp_entry_t entry,
+static inline bool swap_entry_backend_has_flag(swp_entry_t entry,
 					       unsigned long flag)
 {
 	struct swap_info_struct *phys_si;
 	swp_entry_t phys;
 	bool has_flag;
 
-	if (!swap_is_vswap(si))
-		return data_race(si->flags & flag);
-
 	phys = vswap_to_phys(entry);
 	if (!phys.val)
 		return false;
diff --git a/mm/zswap.c b/mm/zswap.c
index bab1d52aa24c..e527a34e52a8 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1101,12 +1101,10 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 				 struct zswap_entry *entry,
 				 swp_entry_t swpentry)
 {
-	pgoff_t offset = swp_offset(swpentry);
 	struct folio *folio;
 	struct mempolicy *mpol;
 	struct swap_info_struct *si;
 	swp_entry_t phys = {};
-	bool is_vswap;
 	int ret = 0;
 
 	/* try to allocate swap cache folio */
@@ -1114,7 +1112,6 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 	if (IS_ERR_OR_NULL(si))
 		return -ENOENT;
 
-	is_vswap = swap_is_vswap(si);
 	mpol = get_task_policy(current);
 	folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
 					 NO_INTERLEAVE_INDEX);
@@ -1135,8 +1132,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 	/*
 	 * folio is locked, and the swapcache is now secured against
 	 * concurrent swapping to and from the slot, and concurrent
-	 * swapoff so we can safely dereference the zswap tree (or vswap
-	 * vtable) here.
+	 * swapoff so we can safely dereference the vswap vtable here.
 	 * Verify that the swap entry hasn't been invalidated and recycled
 	 * behind our backs, to avoid overwriting a new swap folio with
 	 * old compressed data. Only when this is successful can the entry
@@ -1147,16 +1143,14 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 		goto err;
 	}
 
-	if (is_vswap) {
-		/*
-		 * Allocate physical backing before decompress so a failure
-		 * wastes no work.
-		 */
-		phys = folio_realloc_swap(folio);
-		if (!phys.val) {
-			ret = -ENOMEM;
-			goto err;
-		}
+	/*
+	 * Allocate physical backing before decompress so a failure
+	 * wastes no work.
+	 */
+	phys = folio_realloc_swap(folio);
+	if (!phys.val) {
+		ret = -ENOMEM;
+		goto err;
 	}
 
 	if (!zswap_decompress(entry, folio)) {
@@ -1166,14 +1160,10 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 		 * Restore the zswap entry to the vtable, which also frees the
 		 * allocated physical swap space.
 		 */
-		if (is_vswap)
-			vswap_zswap_store(swpentry, entry);
+		vswap_zswap_store(swpentry, entry);
 		goto err;
 	}
 
-	if (!is_vswap)
-		xa_erase(swap_zswap_tree(swpentry), offset);
-
 	count_vm_event(ZSWPWB);
 	if (entry->objcg)
 		count_objcg_events(entry->objcg, ZSWPWB, 1);
@@ -1199,10 +1189,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
 	folio_put(folio);
 
 	/* start writeback */
-	if (is_vswap)
-		__swap_writeout(ctx, folio, phys);
-	else
-		__swap_writeout(ctx, folio, folio->swap);
+	__swap_writeout(ctx, folio, phys);
 
 	return 0;
 
@@ -1805,9 +1792,7 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr)
  * of the following error codes:
  *
  *  -EIO: if the swapped out content was in zswap but could not be handed
- *  back, either because decompression failed or because a slot in a
- *  large-folio range is still in zswap and zswap cannot reconstruct a large
- *  folio from per-page entries. The folio is unlocked, but NOT marked
+ *  back because decompression failed. The folio is unlocked, but NOT marked
  *  up-to-date, so that an IO error is emitted (e.g. do_swap_page() will
  *  SIGBUS).
  *
@@ -1817,7 +1802,6 @@ bool zswap_is_present(swp_entry_t entry, unsigned int nr)
 int zswap_load(struct folio *folio)
 {
 	swp_entry_t swp = folio->swap;
-	struct swap_info_struct *si = __swap_entry_to_info(swp);
 	struct zswap_entry *entry;
 
 	VM_WARN_ON_ONCE(!folio_test_locked(folio));
@@ -1827,23 +1811,11 @@ int zswap_load(struct folio *folio)
 		return -ENOENT;
 
 	/*
-	 * A large folio can legitimately reach zswap_load() with its whole
-	 * range on the backing device, so scan the range rather than rejecting
-	 * it outright. The caller has pinned every slot, so zswap cannot start
-	 * a store or a writeback into the range while we look.
-	 *
 	 * A vswap batch is checked when the folio enters the swap cache, and
 	 * its backing cannot change after that.
 	 */
-	if (folio_test_large(folio)) {
-		if (WARN_ON_ONCE(!swap_is_vswap(si) &&
-				 zswap_is_present(swp,
-						  folio_nr_pages(folio)))) {
-			folio_unlock(folio);
-			return -EIO;
-		}
+	if (folio_test_large(folio))
 		return -ENOENT;
-	}
 
 	entry = zswap_entry_load(swp);
 	if (!entry)
@@ -1869,12 +1841,7 @@ int zswap_load(struct folio *folio)
 	 */
 	folio_mark_dirty(folio);
 
-	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_release_vswap_backing(folio);
 
 	folio_unlock(folio);
 	return 0;
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-12-03 22:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
2026-12-03 21:59 ` [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
2026-12-03 22:00 ` [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
2026-12-03 22:00 ` Nhat Pham [this message]
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
2026-12-03 22:00 ` [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters Nhat Pham
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203220010.1302207-18-nphamcs@gmail.com \
    --to=nphamcs@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=bp@alien8.de \
    --cc=cgroups@vger.kernel.org \
    --cc=chengming.zhou@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=david@kernel.org \
    --cc=elver@google.com \
    --cc=enelsonmoore@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jlayton@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kmehltretter@gmail.com \
    --cc=liam@infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=qi.zheng@linux.dev \
    --cc=rdunlap@infradead.org \
    --cc=roman.gushchin@linux.dev \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=skhan@linuxfoundation.org \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=yosry@kernel.org \
    --cc=youngjun.park@lge.com \
    --cc=yuanchu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox