From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335241; b=GiCfg8HhAfrmx6tXQc5n7pXaa33RfRKvygy5BT7TuOV4I2FTkm1jKsEz6PM32n4+jbh7 CcmfzcCE9JBasTxqY9NaN6cXX+uojoV9MRx5aiIlVdr7qIsWiv1o73KmqWGlmYbrbuFJK SIJbWNfE6ZgNcIJ0gsRjO19/pAc9odTGwEIBkGGpIf7OmHoH+Rgbq6heLdtLxcwkiufD4 eh4NieVbY8XkvmwaEhurD4XslfpDElP96SU58IwEYFY5sKyjcBLmJIxou5wUZcCYsqchj dv1wDiY0JeUfALbyoJ3YT7x6qlnbd0llQlxraa0AsyLyBt4CEakVpPf9mrT5G/+z3/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335241; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=mcLp9JyOWTk9m43m3GAxe2W56iWkpAkSGfC34KOlVLE=; b=oAvfn7F35EcoedGtRys++j4V1cLfRW8dGRc2suo8pePJimbPVMm7zQlG4kMFBGK9+Exw OpukdwLtIGY0l+sit6qQR9L0lG0uvMpK4tEvrYzW3FxYU5uVUBa/sYYUAmL1R7G4IKKo/ ynlMX7ywzfl+CWioK7BUxTEqMH2BlD1BiEu3HSqaijhkdRAmH2XyVfoX/IXepvog5OyRo uiNZn2swKbGtzZFDIt8NzHky46apGiTahgYaocNLK6BqDWoIIH5+spqFMhqnX6KnurFej kxJDB6uHlfWs17n2CKGo6mjhTVBClCQgUW4P1yxzGnAUqYp0U+ERAoyV3FGGUYDkQ4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hF87NSAR; arc=none smtp.remote-ip=209.85.128.180 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BE8C606A6B3 for ; Thu, 3 Dec 2026 22:00:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7BE8C606A6B3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=hF87NSAR Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-8b4fb5d96b3so5353507b3.1 for ; Thu, 03 Dec 2026 14:00:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335241; x=1796940041; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mcLp9JyOWTk9m43m3GAxe2W56iWkpAkSGfC34KOlVLE=; b=hF87NSAR1fkoIaFkJ2oDeyVXqCAbo/YxhB7hVEmT3LDUh+YFuS51xRgiBBCHw1p4TK YXjxrPPL4osLpPWKueZkqbN9bXhe3KA7YnlGDhzglCxm/HqRDTV9NVQCaoMtGEQj2dAH ibP16Zg5hMMT7eIGCRyMaNBzJTajRaTW82DUJHZmd2FsF0x8ILfM0wAtlQ5ayXZGPjFg 5+xU9U/5wRJMIljRyZvfmviqflKe0fSBo/O2o5KQ9k8jJWwd063NXIFBqzi1YV2D0d8E dceG3La3w90ZAIgLrtr42nZzBndRun3yppJMpQCm6WBA01H4MtMP9RGrGrTnLR2AH+F3 /Uww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335241; x=1796940041; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=mcLp9JyOWTk9m43m3GAxe2W56iWkpAkSGfC34KOlVLE=; b=IpkqyHRa/l8bwKx+73Mlwy/85cDnpSnho/5L4LtOWuBiKQeMj1HSnDYhvHjkIRJwDu hHCS6G4DYmovz9A3Yobx3mphZqb9AqgxivvGBBLsNttHDRbINn8qFO4CUY+d9kGEUZya gxPtJoGFRcMNt/Z49Q7JFTcBvGLi9Xi0kU7ZXS4e2N7YwYU/8imUlE5YtUDQJys1eGNy TpEu4S5i8ba7buTJIATWg5vstxmxPou5+I86trEIEAyYb3LksWLIYV//TcKWSVku6zsO rX6bykbj3mZRs/judKrHWW8NpWYJYoun+mt4sKvzO/Gqd20Aagzr0LoTgliwI/qAaYMs Bkaw== X-Forwarded-Encrypted: i=1; AL+hErGX6mopN4ocCfZap7SzQR8yIExlxx/vjo4FdWw8hS8EmLu+GHeNmY39bys6DFCty1ayWHWlk399hT4=@vger.kernel.org X-Gm-Message-State: APuU4Z9qomZHq5WniWW+W70VENA8pcPBxDefIslSjAEJ9YXuL27tadqY p2xpbf/jBYV60ABdQhTsVlAVfGudh3/vz/fcZEZm1vX3/B4zBcw8BcJY X-Gm-Gg: AZFdTZdnMtKbyhMzHlUzEdwx6wVwLcF98VE8tTKuxv0BkWSouet8O+8lOpsLS/I+vG6 r9poPbr/uHifOtnBDLrSilV2Zck70L5ytD3KVR8cZd7EvfRhcH3v+CnEu99QnWSa861VHtpBCpS BGfxVhmMhSriv5terEV+kriUL8YgT9Ib0tJq27cGkGOqFYq9NmVTqJD6EU3S5pXNOTUK8BDW9b7 GwWg8l+Ehf3abv28J1Zy3xxlBUQhjC/EdArzDSkbZCFwNH2HAK0GbT8FgB38+Jtry7NRm0FV0G2 Vw1njwGNv4Jln5daMZu0+yb5dI3lfxjZQveUTzzz3QCZp7NeMxLxOt/CIG91z7yb0/sQaAkt0zC HyF9gqzYf0mqfZG2MScvRJ2vDqONYQMGJ1WwwOLDqTlIQVESkNBqfgFjorZpHTBmx8BjqFcmoBo EYJGbaM11+Ub22Pi2tDjZr6cFsEBmCN5Fk9uzw6s3erY0OZ2UUC1C9WosBkQ9vE8Gb/RD1yiOey E1LUt9Ei+rcDznipfn6TYMALIl2JYhOjtKkymEnb98ZAcWHVAOt7vZkm0Ag/RvG1lJNL9cZmvq7 BQh8EA/Sfo3IwRmc/NDTf3jyir0+nGI3E3X4N1Fhyw5jJ0df5Ni1Tzf1Fab2FYJ421tg2cZUMal qR0xOT1H6zRjhKZiU6UO7VivzxPePrSYfZjnj X-Received: by 2002:a05:690c:498f:b0:8b5:3360:f29f with SMTP id 00721157ae682-8b5f5d182e1mr18895357b3.14.1796335240329; Thu, 03 Dec 2026 14:00:40 -0800 (PST) Received: from localhost ([2a03:2880:10ff:3::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660e183acsm959807b3.45.2026.12.03.14.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:39 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Date: Thu, 3 Dec 2026 14:00:05 -0800 Message-ID: <20261203220010.1302207-16-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Nhat Pham --- .../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