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 14/20] mm, swap: defer memcg_table allocation for physical swap clusters
Date: Thu,  3 Dec 2026 14:00:04 -0800	[thread overview]
Message-ID: <20261203220010.1302207-15-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>

Stop allocating a memcg table for every physical swap cluster that only
ever holds vswap backings. The table costs SWAPFILE_CLUSTER *
sizeof(unsigned short) per cluster, 1 KB per 2 MB of swap on a 64-bit
kernel with 4 KB pages.

Such clusters never have their memcg_table read or written: vswap-layer
charging records on the vswap cluster's table, not the physical one.

Allocate eagerly for each vswap cluster and each new direct-use physical
cluster. A physical cluster otherwise allocates its owner table on the
first direct-use slot and skips it if all slots hold vswap backings.

If the existing cluster has no owner table, its locked swapcache folio
pins the new slots while allocation sleeps. Free the reservation if
allocation fails. Hibernation prepares the table before publishing a
raw slot.

Link: https://lore.kernel.org/all/sashiko-outbox-181084@kernel.org/
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 mm/swapfile.c | 127 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 104 insertions(+), 23 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index a1eef85b9eb6..30c1eed8adfe 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/blkdev.h>
+#include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/mm.h>
 #include <linux/sched/mm.h>
@@ -481,12 +482,13 @@ 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,
+				    struct swap_cluster_info *ci, bool direct,
 				    gfp_t gfp, bool locked)
 {
 	struct swap_table *table = NULL;
 #ifdef CONFIG_MEMCG
 	struct swap_memcg_table *memcg_table = NULL;
+	bool vswap = si && swap_is_vswap(si);
 #endif
 #if !SWAP_TABLE_HAS_ZEROFLAG
 	unsigned long *zero_bitmap = NULL;
@@ -505,8 +507,19 @@ static int swap_cluster_alloc_table(struct swap_info_struct *si,
 		goto lock;
 
 #ifdef CONFIG_MEMCG
-	if (!mem_cgroup_disabled()) {
-		memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+	/*
+	 * 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 (!memcg_table)
 			goto lock;
 	}
@@ -601,7 +614,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)
+			 struct swap_cluster_info *ci, bool direct)
 {
 	int ret;
 
@@ -616,7 +629,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, __GFP_HIGH | __GFP_NOMEMALLOC |
+	if (!swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
 					      __GFP_NOWARN, true))
 		return ci;
 
@@ -630,7 +643,7 @@ 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, __GFP_HIGH | __GFP_NOMEMALLOC |
+	ret = swap_cluster_alloc_table(si, ci, direct, __GFP_HIGH | __GFP_NOMEMALLOC |
 					     GFP_KERNEL, false);
 
 	/*
@@ -713,7 +726,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)
+		struct swap_info_struct *si, struct list_head *list, bool direct)
 {
 	struct swap_cluster_info *ci, *found = NULL;
 	u8 flags = CLUSTER_FLAG_NONE;
@@ -745,7 +758,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);
+		return swap_cluster_populate(si, found, direct);
 	}
 
 	return found;
@@ -920,7 +933,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, GFP_KERNEL, false);
+		ret = swap_cluster_alloc_table(alloc_si, ci, false, GFP_KERNEL, false);
 		if (ret)
 			goto out;
 	} else {
@@ -1173,7 +1186,9 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
 /* Try use a new cluster for current CPU and allocate from it. */
 static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 					    struct swap_cluster_info *ci,
-					    struct folio *folio, unsigned long offset)
+					    struct folio *folio,
+					    unsigned long offset,
+					    bool *nomem)
 {
 	unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
 	unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
@@ -1202,6 +1217,19 @@ 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;
@@ -1211,7 +1239,15 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
 		break;
 	}
 out:
-	relocate_cluster(si, ci);
+	/*
+	 * 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);
 	swap_cluster_unlock(ci);
 	if (swap_is_vswap(si)) {
 		this_cpu_write(percpu_vswap_cluster.offset[order], next);
@@ -1232,18 +1268,25 @@ 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);
+		struct swap_cluster_info *ci = isolate_lock_cluster(si, list, direct);
 		unsigned long offset;
 
 		if (!ci)
 			break;
 		offset = cluster_offset(si, ci);
-		found = alloc_swap_scan_cluster(si, ci, folio, offset);
+		found = alloc_swap_scan_cluster(si, ci, folio, offset, &nomem);
 		if (found)
 			break;
-	} while (scan_all);
+	} while (scan_all && !nomem);
 
 	return found;
 }
@@ -1268,7 +1311,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, GFP_ATOMIC | __GFP_NOWARN, true)) {
+	if (swap_cluster_alloc_table(si, ci, false, GFP_ATOMIC | __GFP_NOWARN, true)) {
 		spin_unlock(&ci->lock);
 		kfree(ci_dyn);
 		return SWAP_ENTRY_INVALID;
@@ -1292,7 +1335,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);
+	return alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
 }
 
 static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force)
@@ -1307,7 +1350,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))) {
+	while ((ci = isolate_lock_cluster(si, &si->full_clusters, false))) {
 		offset = cluster_offset(si, ci);
 		end = min(si->max, offset + SWAPFILE_CLUSTER);
 		to_scan--;
@@ -1394,7 +1437,8 @@ 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);
+			found = alloc_swap_scan_cluster(si, ci, folio, offset,
+							NULL);
 		} else {
 			swap_cluster_unlock(ci);
 		}
@@ -1434,7 +1478,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 	if (order < PMD_ORDER) {
 		/*
 		 * Scan only one fragment cluster is good enough. Order 0
-		 * allocation will surely success, and large allocation
+		 * allocation rarely fails, and large allocation
 		 * failure is not critical. Scanning one cluster still
 		 * keeps the list rotated and reclaimed (for clean swap cache).
 		 */
@@ -1449,7 +1493,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
 	/* Order 0 stealing from higher order */
 	for (int o = 1; o < SWAP_NR_ORDERS; o++) {
 		/*
-		 * Clusters here have at least one usable slots and can't fail order 0
+		 * Clusters here have at least one usable slots and rarely fail order 0
 		 * allocation, but reclaim may drop si->lock and race with another user.
 		 */
 		found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true);
@@ -1670,7 +1714,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);
+		found = alloc_swap_scan_cluster(si, ci, folio, offset, NULL);
 	} else if (ci) {
 		swap_cluster_unlock(ci);
 	}
@@ -2058,7 +2102,8 @@ 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);
+			alloc_swap_scan_cluster(vswap_si, ci, folio, offset,
+						NULL);
 		} else if (ci) {
 			swap_cluster_unlock(ci);
 		}
@@ -2079,8 +2124,37 @@ 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:
@@ -2092,7 +2166,13 @@ 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;
 }
 
@@ -2895,7 +2975,8 @@ 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);
+			offset = alloc_swap_scan_cluster(si, ci, NULL,
+							 pcp_offset, NULL);
 		else
 			swap_cluster_unlock(ci);
 	}
-- 
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 ` Nhat Pham [this message]
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 ` [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-15-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