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 19/20] mm/swap: reclaim physical backing through reverse mappings
Date: Thu,  3 Dec 2026 14:00:09 -0800	[thread overview]
Message-ID: <20261203220010.1302207-20-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>

The physical allocator can reuse slots backing a cache-only
virtual entry. Recognize those slots from the reverse mapping
in both the full-cluster and range scans.

The last remaining caller of __try_to_reclaim_swap() drops a
virtual swap count. It already asks for unmapped folios or
physical backing held by a cgroup under swap pressure. Keep
that test directly and remove the flags.

Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 mm/swap_table.h |  5 ++++
 mm/swapfile.c   | 64 ++++++++++++-------------------------------------
 2 files changed, 20 insertions(+), 49 deletions(-)

diff --git a/mm/swap_table.h b/mm/swap_table.h
index 4f4f3100e5f9..f723ce4f2fca 100644
--- a/mm/swap_table.h
+++ b/mm/swap_table.h
@@ -431,6 +431,11 @@ static inline bool swp_tb_is_vswap(unsigned long swp_tb)
 	return (swp_tb & SWP_TB_VSWAP_MARK_MASK) == SWP_TB_VSWAP_MARK;
 }
 
+static inline bool swp_tb_is_vswap_cache_only(unsigned long swp_tb)
+{
+	return swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY);
+}
+
 #ifdef CONFIG_SWAP
 static inline unsigned long swp_entry_to_swp_tb_vswap(swp_entry_t entry)
 {
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d71fccce5ae3..3f6c56c51c2c 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -188,16 +188,6 @@ static long swap_usage_in_pages(struct swap_info_struct *si)
 	return atomic_long_read(&si->inuse_pages) & SWAP_USAGE_COUNTER_MASK;
 }
 
-/* Reclaim the swap entry anyway if possible */
-#define TTRS_ANYWAY		0x1
-/*
- * Reclaim the swap entry if there are no more mappings of the
- * corresponding page
- */
-#define TTRS_UNMAPPED		0x2
-/* Reclaim the swap entry if swap is getting full */
-#define TTRS_FULL		0x4
-
 static bool swap_only_has_cache(struct swap_cluster_info *ci,
 				unsigned long offset, int nr_pages)
 {
@@ -221,7 +211,7 @@ static bool swap_only_has_cache(struct swap_cluster_info *ci,
  * folio was associated with the swap entry.
  */
 static int __try_to_reclaim_swap(struct swap_info_struct *si,
-				 unsigned long offset, unsigned long flags)
+				 unsigned long offset)
 {
 	const swp_entry_t entry = swp_entry(si->type, offset);
 	struct swap_cluster_info *ci;
@@ -257,10 +247,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,
 	}
 	offset = swp_offset(folio->swap);
 
-	need_reclaim = ((flags & TTRS_ANYWAY) ||
-			((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) ||
-			((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) &&
-			 folio_phys_swap_backed(folio)));
+	need_reclaim = !folio_mapped(folio) ||
+		(mem_cgroup_swap_full(folio) && folio_phys_swap_backed(folio));
 	if (!need_reclaim || !folio_swapcache_freeable(folio))
 		goto out_unlock;
 
@@ -1014,19 +1002,13 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
 	spin_unlock(&ci->lock);
 	do {
 		swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
-		if (swp_tb_is_vswap(swp_tb) &&
-		    (swp_tb & SWP_RMAP_CACHE_ONLY)) {
-			vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
-			if (try_to_reclaim_vswap_backing(si, offset,
-							 vswap_entry) < 0)
-				break;
+		if (swp_tb_is_null(swp_tb))
 			continue;
-		}
-		if (swp_tb_get_count(swp_tb))
+		if (!swp_tb_is_vswap_cache_only(swp_tb))
+			break;
+		vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
+		if (try_to_reclaim_vswap_backing(si, offset, vswap_entry) < 0)
 			break;
-		if (swp_tb_is_folio(swp_tb))
-			if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0)
-				break;
 	} while (++offset < end);
 	spin_lock(&ci->lock);
 
@@ -1057,8 +1039,7 @@ static bool cluster_reclaim_range(struct swap_info_struct *si,
 	return true;
 }
 
-static bool cluster_scan_range(struct swap_info_struct *si,
-			       struct swap_cluster_info *ci,
+static bool cluster_scan_range(struct swap_cluster_info *ci,
 			       unsigned long offset, unsigned int nr_pages,
 			       bool *need_reclaim)
 {
@@ -1070,16 +1051,12 @@ static bool cluster_scan_range(struct swap_info_struct *si,
 		swp_tb = __swap_table_get(ci, ci_off);
 		if (swp_tb_is_null(swp_tb))
 			continue;
-		if ((swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) ||
-		    (swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY))) {
-			/* vswap slots are abundant; never reclaim to reuse one */
-			if (swap_is_vswap(si) || !vm_swap_full())
-				return false;
+		if (swp_tb_is_vswap_cache_only(swp_tb) && vm_swap_full()) {
 			*need_reclaim = true;
 			continue;
 		}
 		/* Slot with zero count can only be NULL or folio */
-		VM_WARN_ON(!swp_tb_get_count(swp_tb));
+		VM_WARN_ON(swp_tb_is_shadow(swp_tb) && !__swp_tb_get_count(swp_tb));
 		return false;
 	} while (++ci_off < ci_end);
 
@@ -1194,7 +1171,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 
 	for (end -= nr_pages; offset <= end; offset += nr_pages) {
 		need_reclaim = false;
-		if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim))
+		if (!cluster_scan_range(ci, offset, nr_pages, &need_reclaim))
 			continue;
 		if (need_reclaim) {
 			ret = cluster_reclaim_range(si, ci, offset, order, &usable);
@@ -1318,17 +1295,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
 
 		while (offset < end) {
 			swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER);
-			if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) {
-				spin_unlock(&ci->lock);
-				nr_reclaim = __try_to_reclaim_swap(si, offset,
-								   TTRS_ANYWAY);
-				spin_lock(&ci->lock);
-				if (nr_reclaim) {
-					offset += abs(nr_reclaim);
-					continue;
-				}
-			} else if (swp_tb_is_vswap(swp_tb) &&
-				   (swp_tb & SWP_RMAP_CACHE_ONLY)) {
+			if (swp_tb_is_vswap_cache_only(swp_tb)) {
 				vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb);
 				spin_unlock(&ci->lock);
 				nr_reclaim = try_to_reclaim_vswap_backing(si, offset,
@@ -1923,8 +1890,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si,
 		return;
 
 	do {
-		nr_reclaimed = __try_to_reclaim_swap(si, offset,
-						     TTRS_UNMAPPED | TTRS_FULL);
+		nr_reclaimed = __try_to_reclaim_swap(si, offset);
 		offset++;
 		if (nr_reclaimed)
 			offset = round_up(offset, abs(nr_reclaimed));
@@ -2795,7 +2761,7 @@ static bool folio_swapcache_freeable(struct folio *folio)
 	/*
 	 * Once hibernation has begun to create its image of memory,
 	 * there's a danger that one of the calls to folio_free_swap()
-	 * - most probably a call from __try_to_reclaim_swap() while
+	 * - most probably a call from try_to_reclaim_vswap_backing() while
 	 * hibernation is allocating its own swap pages for the image,
 	 * but conceivably even a call from memory reclaim - will free
 	 * the swap from a folio which has already been recorded in the
-- 
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 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
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 ` Nhat Pham [this message]
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-20-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