Linux-mm Archive on lore.kernel.org
 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 15/20] mm, swap: add vmstat and memcg counters for vswap
Date: Thu,  3 Dec 2026 14:00:05 -0800	[thread overview]
Message-ID: <20261203220010.1302207-16-nphamcs@gmail.com> (raw)
In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com>

Add nr_vswap to /proc/vmstat and vswap to cgroup's memory.stat to report
virtual swap usage.

These counters help diagnose swapping issues. Userspace can also use
vswap as a component of a workload's logical memory footprint when
making policy decisions.

Count cgroup virtual slots while they hold the owner ID. Restore the
statistic with that owner when failed writeback rolls back a swap-in.

Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 .../admin-guide/cgroup-v1/memory.rst          |  1 +
 Documentation/admin-guide/cgroup-v2.rst       |  3 +++
 include/linux/memcontrol.h                    |  1 +
 include/linux/swap.h                          | 10 ++++----
 include/linux/vmstat.h                        |  3 +++
 mm/memcontrol-v1.c                            |  5 +++-
 mm/memcontrol.c                               | 24 +++++++++++++++++--
 mm/swapfile.c                                 |  9 +++++--
 mm/vmstat.c                                   |  6 +++++
 9 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/Documentation/admin-guide/cgroup-v1/memory.rst b/Documentation/admin-guide/cgroup-v1/memory.rst
index 7d2a44af52c9..732b379f2612 100644
--- a/Documentation/admin-guide/cgroup-v1/memory.rst
+++ b/Documentation/admin-guide/cgroup-v1/memory.rst
@@ -555,6 +555,7 @@ memory.stat file includes following statistics:
                     cgroup.
     swap            # of bytes of swap usage
     swapcached      # of bytes of swap cached in memory
+    vswap           # of bytes of virtual swap usage
     dirty           # of bytes that are waiting to get written back to the disk.
     writeback       # of bytes of file/anon cache that are queued for syncing to
                     disk.
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index cd965ddeaf9e..b17789de7557 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1633,6 +1633,9 @@ The following nested keys are defined.
 		against memory usage, and against swap usage when the swap
 		entry has physical backing.
 
+	  vswap
+		Amount of virtual swap usage.
+
 	  anon_thp
 		Amount of memory used in anonymous mappings backed by
 		transparent hugepages
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 338406969510..3b43f20c9005 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -41,6 +41,7 @@ enum memcg_stat_item {
 	MEMCG_ZSWAP_B,
 	MEMCG_ZSWAPPED,
 	MEMCG_ZSWAP_INCOMP,
+	MEMCG_VSWAP,
 	MEMCG_NR_STAT,
 };
 
diff --git a/include/linux/swap.h b/include/linux/swap.h
index ae2676627b07..ae635d5d0bab 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -371,6 +371,7 @@ bool current_is_kswapd(void);
 
 #ifdef CONFIG_SWAP
 bool mem_cgroup_can_vswap(struct mem_cgroup *memcg);
+unsigned long vswap_usage_in_pages(void);
 
 int add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
 		unsigned long nr_pages, sector_t start_block);
@@ -578,13 +579,14 @@ static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
 	__mem_cgroup_dir_phys_swap_free(id, nr_pages);
 }
 
-void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages);
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
+			   bool is_vswap);
 static inline void mem_cgroup_swap_put(unsigned short id,
-				       unsigned int nr_pages)
+				       unsigned int nr_pages, bool is_vswap)
 {
 	if (mem_cgroup_disabled())
 		return;
-	__mem_cgroup_swap_put(id, nr_pages);
+	__mem_cgroup_swap_put(id, nr_pages, is_vswap);
 }
 
 long mem_cgroup_get_folio_swap_margin(const struct folio *folio);
@@ -618,7 +620,7 @@ static inline void mem_cgroup_dir_phys_swap_free(unsigned short id,
 }
 
 static inline void mem_cgroup_swap_put(unsigned short id,
-				       unsigned int nr_pages)
+				       unsigned int nr_pages, bool is_vswap)
 {
 }
 
diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
index 9df3587a2968..93f0e4a5f076 100644
--- a/include/linux/vmstat.h
+++ b/include/linux/vmstat.h
@@ -33,6 +33,9 @@ enum vm_stat_item {
 	NR_DIRTY_BG_THRESHOLD,
 	NR_MEMMAP_PAGES,	/* page metadata allocated through buddy allocator */
 	NR_MEMMAP_BOOT_PAGES,	/* page metadata allocated through boot allocator */
+#ifdef CONFIG_SWAP
+	NR_VSWAP,		/* pages occupying a virtual swap slot */
+#endif
 	NR_VM_STAT_ITEMS,
 };
 
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index eb61ab797c38..1a55c168c3eb 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -305,6 +305,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
 	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++)
@@ -399,7 +400,7 @@ void memcg1_swapin(struct folio *folio)
 		return;
 	if (is_vswap) {
 		mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile);
-		mem_cgroup_swap_put(id, nr_pages);
+		mem_cgroup_swap_put(id, nr_pages, true);
 	} else {
 		mem_cgroup_dir_phys_swap_free(id, nr_pages);
 	}
@@ -1868,6 +1869,7 @@ static const unsigned int memcg1_stats[] = {
 #ifdef CONFIG_SWAP
 	MEMCG_SWAP,
 	NR_SWAPCACHE,
+	MEMCG_VSWAP,
 #endif
 };
 
@@ -1886,6 +1888,7 @@ static const char *const memcg1_stat_names[] = {
 #ifdef CONFIG_SWAP
 	"swap",
 	"swapcached",
+	"vswap",
 #endif
 };
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 571f375b2e7f..5811ad3889df 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -458,6 +458,9 @@ static const unsigned int memcg_stat_items[] = {
 	MEMCG_ZSWAP_B,
 	MEMCG_ZSWAPPED,
 	MEMCG_ZSWAP_INCOMP,
+#ifdef CONFIG_SWAP
+	MEMCG_VSWAP,
+#endif
 };
 
 #define NR_MEMCG_NODE_STAT_ITEMS ARRAY_SIZE(memcg_node_stat_items)
@@ -1620,6 +1623,7 @@ static const struct memory_stat memory_stats[] = {
 	{ "file_writeback",		NR_WRITEBACK			},
 #ifdef CONFIG_SWAP
 	{ "swapcached",			NR_SWAPCACHE			},
+	{ "vswap",			MEMCG_VSWAP			},
 #endif
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	{ "anon_thp",			NR_ANON_THPS			},
@@ -6088,6 +6092,8 @@ 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);
 
 	for (i = start; i < end; i++)
 		if (__swp_tb_get_count(__swap_table_get(ci, i)))
@@ -6110,6 +6116,7 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
 	unsigned int nr_pages = folio_nr_pages(folio);
 	struct mem_cgroup *memcg;
 	struct obj_cgroup *objcg;
+	unsigned short id;
 
 	if (do_memsw_account())
 		return 0;
@@ -6126,7 +6133,12 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
 		return 0;
 	}
 
-	return mem_cgroup_private_id_get(memcg, nr_pages);
+	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);
+	}
+	return id;
 }
 
 /**
@@ -6266,10 +6278,18 @@ 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
  * @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)
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
+			   bool is_vswap)
 {
+	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);
+	}
 	mem_cgroup_private_id_put(id, nr_pages);
 }
 
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 30c1eed8adfe..b0865d617512 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2230,7 +2230,7 @@ int folio_alloc_swap(struct folio *folio)
 	if (memcg_id) {
 		if (!is_vswap_entry(folio->swap) &&
 		    unlikely(mem_cgroup_swap_charge(memcg_id, size))) {
-			mem_cgroup_swap_put(memcg_id, size);
+			mem_cgroup_swap_put(memcg_id, size, false);
 			swap_cache_del_folio(folio);
 		} else {
 			mem_cgroup_swap_record(folio, memcg_id);
@@ -2704,7 +2704,7 @@ static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi,
 static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap)
 {
 	if (is_vswap)
-		mem_cgroup_swap_put(id, nr);
+		mem_cgroup_swap_put(id, nr, true);
 	else
 		mem_cgroup_dir_phys_swap_free(id, nr);
 }
@@ -4989,6 +4989,11 @@ early_param("vswap", early_vswap);
 /* vswap does no IO on its own. */
 static const struct swap_ops vswap_ops = { };
 
+unsigned long vswap_usage_in_pages(void)
+{
+	return vswap_is_enabled() ? swap_usage_in_pages(vswap_si) : 0;
+}
+
 static int vswap_used_get(void *data, u64 *val)
 {
 	*val = swap_usage_in_pages(vswap_si);
diff --git a/mm/vmstat.c b/mm/vmstat.c
index c6beac6be2bc..73fcd38e09d7 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1320,6 +1320,9 @@ const char * const vmstat_text[] = {
 	[I(NR_DIRTY_BG_THRESHOLD)]		= "nr_dirty_background_threshold",
 	[I(NR_MEMMAP_PAGES)]			= "nr_memmap_pages",
 	[I(NR_MEMMAP_BOOT_PAGES)]		= "nr_memmap_boot_pages",
+#ifdef CONFIG_SWAP
+	[I(NR_VSWAP)]				= "nr_vswap",
+#endif
 #undef I
 
 #if defined(CONFIG_VM_EVENT_COUNTERS)
@@ -1936,6 +1939,9 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos)
 			    v + NR_DIRTY_THRESHOLD);
 	v[NR_MEMMAP_PAGES] = atomic_long_read(&nr_memmap_pages);
 	v[NR_MEMMAP_BOOT_PAGES] = atomic_long_read(&nr_memmap_boot_pages);
+#ifdef CONFIG_SWAP
+	v[NR_VSWAP] = vswap_usage_in_pages();
+#endif
 	v += NR_VM_STAT_ITEMS;
 
 #ifdef CONFIG_VM_EVENT_COUNTERS
-- 
2.53.1-Meta



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

Thread overview: 25+ 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 ` Nhat Pham [this message]
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
2026-12-03 23:00   ` Nhat Pham
2026-12-03 23:05     ` Yosry Ahmed
2026-12-03 23:14       ` Nhat Pham

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-16-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