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