From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335239; b=Hlx/62QdCvhLteG1kxtjlcJz2xsTMtcoMcW2KA348IPQQiblrys/pyZqMqTb1/VHWQ00 ycWQF6L6MP/fgj/Q1sQl93s2J6r/EMcoEFhUDjJOmrsXy0j4OKSpPqIFzx27USaQM4WQP spAb2ae5KS4ojJcSlCbwfF6sIN43xCvxS1wfruRX0A4387NSB7TalibO/5oF0LUjPOSeA itIapzI1UARhvYzKpV3lrVn/p96jNJG0L7J0RWYvgfUe95YB10CkCqJkidVsUJzqlvffw jlZmJnWX/yKqTxrbBh3ut2MCf85flblzFjFYUjWAg0hePKvMPaGNwk/YiBO4YPHi1qQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335239; 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=YPLtWbDmUS758Fy4TDKSV9BO6VA9rrGqP7EqdGoTRvY=; b=VDa0HwqKUjSAi7GiuemCNxsG0tA5fbtPdk/OspP5KGUc3uckvPals9w8dz63MrYc5jjN 5qjbSwiBAJPunCRuaBh7pmWATjKjYOwqQaw04MJgcoD+KzhyzVqUGkuQKGosNFYi5+OwO 11f68DGn/tqTdTx7z/1QV8bCCpxRzCy/1+K+SgXV4Wg2HGq0Kj1qYAPKLJ8OWdRaBwCEA B0e27P61cp1bA0KdkL5IFqEfMHvMV+cFtf4L6B/ZZqJ+YSLU8bzbjqwFabBUx71mfFTi5 7ym3j5uzTJ+16OYdYtQTSiE2C6S0oRrCYIhRXh2icvGLTxxh/V5oimnUFNnlPfEFVjA== 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=pfeeQiYB; arc=none smtp.remote-ip=209.85.128.179 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 42E47606A6A8 for ; Thu, 3 Dec 2026 22:00:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 42E47606A6A8 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=pfeeQiYB Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-8b3402105b9so2291317b3.0 for ; Thu, 03 Dec 2026 14:00:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335237; x=1796940037; 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=YPLtWbDmUS758Fy4TDKSV9BO6VA9rrGqP7EqdGoTRvY=; b=pfeeQiYBNfXF5+mivlSRkAOL7XdAAP/JTgb/hOSXBLxyDDqHK13mdv1m8YPbav0cn1 VeogNFwkmK5PTOv4lEa5SLDZebzYD4mQFI06HGQh1SKPUm1YuOgXXa4zEC3bEJhdEN21 ZQcIdA4IyQLgsDaZ4qPZzemi4CV0/ytT56tFY6UNunKms1UwFHVhZezb0SkWY4qgD4Kw f+U85Hliz3NIfULIizB9sznPrWw4xvnKMO8pCkew/ZLWrrBtf7/B+ZOxGpaTmx3VxwKb II8J2/HwYwTwOqe2QxOTe72YUayIDrcWmGD2Zk0FdMFKcy/PrHUP7BnGmw+i3p4r71k7 jM5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335237; x=1796940037; 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=YPLtWbDmUS758Fy4TDKSV9BO6VA9rrGqP7EqdGoTRvY=; b=Q/3l2bivZbaKyEFwyvLu7myB0bih8Aa1HJRfEuItP1nLFYWyD2pPwuXhM+4/qEHEEp XbE2a3W9RGNe/SUPpL92uSWHRekBB0Lz1rZiphkxGPmYnPvLtjHhHxoCk3L+q3ui0KzE CvEjU49JIFWYHx/1mWTUquK/ShAmGmZCy7KF/UEEZ8tMAzKRKNTk/PnvYzQ1Fj6LF8P2 wvYQEzY8g0liOAdK5CfssQzMW/IcR7ciIGq3qnqXeZR73SoSko3EOv8FAOAVdDm2+tQ6 l13aadwy5zumYVPK6/uypdTuD4HIJyxxVYWvshuE8VdKEN/tOTWaaSoNCMEVf4r6+V7u KfCQ== X-Forwarded-Encrypted: i=1; AL+hErE97oWSFEWAHbSiJ/lqzx3UYkVIjAJ26FETHrsTkiX/7qzHMu2XTVgFgSjDklptKaSaH6ND2l89@vger.kernel.org X-Gm-Message-State: APuU4Z9y8F5JDYWsSD7xGjcZexZJgRLL3LRs5ll4GFAlF6PS3UcV2wbW qLv4aqp7a1MTVqy+ZNPicndrGMDc4jWLMx7pD6+6eGmSL0NRM0B/xUXw X-Gm-Gg: AZFdTZcSoTUvl2PRcnoD1sxHZClgUTJxVVd+MGuR6K2948jROErdYwmekiSamiTpbSe qJR8XQ8REXsyAEukcdnjawwZ/sTLVSu1U0vSO58lu7ANsxyqjrab2vxu/SOjPtjMOjH23pfWMVH jsoNQrOFu+N4BZKpIqGSoAOTfJ10aetRWtGROYCHUNJBeVTYyX5cHKV7YbuOEU0HqPbv3q62zZE ZxRIko9vMCZka+UBYbgOkzOfWmngn06eTyeL4XfGGThJjuLcJOjEkiHaigkGY7TUnaSatWgAbOl FKdCUaYGCcrXP7uUW0VC8IWBB6uzYykQ3SBpfqZO4kj0Ar5kqdxiC5cVgN01r7bWU2oU0GeUHKw R2iIsARfl9ewWgsx54Hlx+aCX9F8NeDMu5nsYRDbnvIOuWKYGhp2TQ6NxqZ/L0aZw0b7h3S4bX2 jsG6ezDP5m540jCXKMnvGl6Jn298Y9kMBJ1OWqEp0TEJVYefmsNfFRrymhBq2gIAcio+NKW2TAq NDpnKeoKygmkda26M6j2NKJBEcl9K0/1jzZbiXWnn/L+scj7WL7NOhnVoesIdZn9SjzEMQgmsbK cnZFr3eBSB0vvL32GNzAtc8mm9htEp3Zok8SrYv8/JlFSxRVgQGf/rLoX/If3epRzjeDTN1Lb1G izDQr+yTPCw2tat7GlY7t5lc6PdRxluLVTuiJLg== X-Received: by 2002:a05:690c:78c:b0:8b4:d66a:1fec with SMTP id 00721157ae682-8b660d7427emr1118267b3.62.1796335237103; Thu, 03 Dec 2026 14:00:37 -0800 (PST) Received: from localhost ([2a03:2880:10ff:71::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660e12321sm896457b3.44.2026.12.03.14.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:36 -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 13/20] mm, swap: only charge physical swap entries Date: Thu, 3 Dec 2026 14:00:03 -0800 Message-ID: <20261203220010.1302207-14-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: cgroups@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 Zswap-backed and zero-filled pages occupy no swap space, but were charged against memcg->swap, the memcg's swap cgroup counter, as though they did. Charge memcg->swap when a vswap entry acquires physical backing rather than when it is allocated. With vswap enabled, this changes what the counter means and when the charge can fail: * Virtual swap entries count towards memory.swap.current only when backed by physical swap slots. * A cgroup can reclaim its anon memory even with memory.swap.max set to 0, provided zswap is allowed for it. * The charge can fail at writeback rather than at allocation. swap_writeout() returns AOP_WRITEPAGE_ACTIVATE and zswap_writeback_entry() returns -ENOMEM for a cgroup at its limit. * The zswap shrinker skips such a cgroup rather than walking its LRUs. Also refactor the swap memcg operations into separate get, record, charge, uncharge and put helpers, since recording the owner and charging it no longer happen at the same time. Direct-mapped physical swap charging is unchanged. In cgroup v1, vswap entries still count against memsw. MEMCG_SWAP counts physical slots, including slots backing virtual entries, rather than virtual slots. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham --- .../admin-guide/cgroup-v1/memcg_test.rst | 2 +- Documentation/admin-guide/cgroup-v2.rst | 51 ++--- include/linux/swap.h | 74 ++++++- mm/memcontrol-v1.c | 43 +++- mm/memcontrol-v1.h | 19 ++ mm/memcontrol.c | 185 ++++++++++++++---- mm/swapfile.c | 88 +++++++-- mm/zswap.c | 22 ++- 8 files changed, 383 insertions(+), 101 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v1/memcg_test.rst b/Documentation/admin-guide/cgroup-v1/memcg_test.rst index d9951c319ef5..8d3fcba13450 100644 --- a/Documentation/admin-guide/cgroup-v1/memcg_test.rst +++ b/Documentation/admin-guide/cgroup-v1/memcg_test.rst @@ -43,7 +43,7 @@ Please note that implementation details can be changed. mem_cgroup_uncharge() Called when a page's refcount goes down to 0. - mem_cgroup_uncharge_swap() + mem_cgroup_dir_phys_swap_free() Called when swp_entry's refcnt goes down to 0. A charge against swap disappears. diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index eeeaf635754c..cd965ddeaf9e 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1630,7 +1630,8 @@ The following nested keys are defined. swapcached Amount of swap cached in memory. The swapcache is accounted - against both memory and swap usage. + against memory usage, and against swap usage when the swap + entry has physical backing. anon_thp Amount of memory used in anonymous mappings backed by @@ -1861,16 +1862,17 @@ The following nested keys are defined. A read-only single value file which exists on non-root cgroups. - The total amount of swap currently being used by the cgroup - and its descendants. + The total amount of physical swap currently being used by the + cgroup and its descendants. memory.swap.high A read-write single value file which exists on non-root cgroups. The default is "max". - Swap usage throttle limit. If a cgroup's swap usage exceeds - this limit, all its further allocations will be throttled to - allow userspace to implement custom out-of-memory procedures. + Physical swap usage throttle limit. If a cgroup's physical + swap usage exceeds this limit, all its further allocations will + be throttled to allow userspace to implement custom + out-of-memory procedures. This limit marks a point of no return for the cgroup. It is NOT designed to manage the amount of swapping a workload does @@ -1883,8 +1885,9 @@ The following nested keys are defined. memory.swap.peak A read-write single value file which exists on non-root cgroups. - The max swap usage recorded for the cgroup and its descendants since - the creation of the cgroup or the most recent reset for that FD. + The max physical swap usage recorded for the cgroup and its + descendants since the creation of the cgroup or the most recent + reset for that FD. A write of any non-empty string to this file resets it to the current memory usage for subsequent reads through the same @@ -1894,8 +1897,9 @@ The following nested keys are defined. A read-write single value file which exists on non-root cgroups. The default is "max". - Swap usage hard limit. If a cgroup's swap usage reaches this - limit, anonymous memory of the cgroup will not be swapped out. + Physical swap usage hard limit. If a cgroup's physical swap + usage reaches this limit, anonymous memory of the cgroup will + not be swapped out to a physical swap device. memory.swap.events A read-only flat-keyed file which exists on non-root cgroups. @@ -1904,23 +1908,24 @@ The following nested keys are defined. modified event. high - The number of times the cgroup's swap usage was over - the high threshold. + The number of times the cgroup's physical swap usage + was over the high threshold. max - The number of times the cgroup's swap usage was about - to go over the max boundary and swap allocation - failed. + The number of times the cgroup's physical swap usage + was about to go over the max boundary and physical + swap allocation failed. fail - The number of times swap allocation failed either - because of running out of swap system-wide or max - limit. + The number of failed physical swap allocation or charge + attempts. This includes physical backing allocation for + virtual swap entries. - When reduced under the current usage, the existing swap - entries are reclaimed gradually and the swap usage may stay - higher than the limit for an extended period of time. This - reduces the impact on the workload and memory management. + When reduced under the current usage, the existing physical + swap entries are reclaimed gradually and the physical swap + usage may stay higher than the limit for an extended period of + time. This reduces the impact on the workload and memory + management. memory.zswap.current A read-only single value file which exists on non-root @@ -1950,8 +1955,6 @@ The following nested keys are defined. reclaim inefficiency after disabling writeback (because the same pages might be rejected again and again). - Note that this is subtly different from setting memory.swap.max to - 0, as it still allows for pages to be written to the zswap pool. This setting has no effect if zswap is disabled, and swapping is allowed unless memory.swap.max is set to 0. diff --git a/include/linux/swap.h b/include/linux/swap.h index 0c78121e2887..ae2676627b07 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -535,20 +535,56 @@ static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp) #endif #if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP) -int __mem_cgroup_try_charge_swap(struct folio *folio); -static inline int mem_cgroup_try_charge_swap(struct folio *folio) +unsigned short __mem_cgroup_swap_get(struct folio *folio); +static inline unsigned short mem_cgroup_swap_get(struct folio *folio) { if (mem_cgroup_disabled()) return 0; - return __mem_cgroup_try_charge_swap(folio); + return __mem_cgroup_swap_get(folio); } -extern void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages); -static inline void mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages) +void __mem_cgroup_swap_fail(unsigned short id); +static inline void mem_cgroup_swap_fail(unsigned short id) { if (mem_cgroup_disabled()) return; - __mem_cgroup_uncharge_swap(id, nr_pages); + __mem_cgroup_swap_fail(id); +} + +int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages); +static inline int mem_cgroup_swap_charge(unsigned short id, + unsigned int nr_pages) +{ + if (mem_cgroup_disabled()) + return 0; + return __mem_cgroup_swap_charge(id, nr_pages); +} + +void __mem_cgroup_swap_record(struct folio *folio, unsigned short id); +static inline void mem_cgroup_swap_record(struct folio *folio, + unsigned short id) +{ + if (mem_cgroup_disabled()) + return; + __mem_cgroup_swap_record(folio, id); +} + +void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages); +static inline void mem_cgroup_dir_phys_swap_free(unsigned short id, + unsigned int nr_pages) +{ + if (mem_cgroup_disabled()) + return; + __mem_cgroup_dir_phys_swap_free(id, nr_pages); +} + +void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages); +static inline void mem_cgroup_swap_put(unsigned short id, + unsigned int nr_pages) +{ + if (mem_cgroup_disabled()) + return; + __mem_cgroup_swap_put(id, nr_pages); } long mem_cgroup_get_folio_swap_margin(const struct folio *folio); @@ -556,13 +592,33 @@ long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg); bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages); bool mem_cgroup_swap_full(const struct folio *folio); #else -static inline int mem_cgroup_try_charge_swap(struct folio *folio) +static inline unsigned short mem_cgroup_swap_get(struct folio *folio) +{ + return 0; +} + +static inline void mem_cgroup_swap_fail(unsigned short id) +{ +} + +static inline int mem_cgroup_swap_charge(unsigned short id, + unsigned int nr_pages) { return 0; } -static inline void mem_cgroup_uncharge_swap(unsigned short id, - unsigned int nr_pages) +static inline void mem_cgroup_swap_record(struct folio *folio, + unsigned short id) +{ +} + +static inline void mem_cgroup_dir_phys_swap_free(unsigned short id, + unsigned int nr_pages) +{ +} + +static inline void mem_cgroup_swap_put(unsigned short id, + unsigned int nr_pages) { } diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index b7f286888507..eb61ab797c38 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -15,6 +15,7 @@ #include "internal.h" #include "swap.h" #include "swap_table.h" +#include "vswap.h" #include "memcontrol-v1.h" /* for OOM */ @@ -270,7 +271,9 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci) { struct mem_cgroup *memcg; struct obj_cgroup *objcg; - unsigned int nr_entries; + struct swap_cluster_info_dynamic *ci_dyn; + unsigned int nr_entries, nr_swapfile; + unsigned int start, i; unsigned short private_id; VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); @@ -298,10 +301,21 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci) * if the ID refers to the root memcg. */ nr_entries = folio_nr_pages(folio); + start = swp_cluster_offset(folio->swap); private_id = mem_cgroup_private_id_get(memcg, nr_entries); - mod_memcg_state(memcg, MEMCG_SWAP, nr_entries); + nr_swapfile = nr_entries; + if (is_vswap_entry(folio->swap)) { + nr_swapfile = 0; + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + for (i = start; i < start + nr_entries; i++) + if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE) + nr_swapfile++; + } + + if (nr_swapfile) + mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile); - __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, private_id); + __swap_cgroup_set(ci, start, nr_entries, private_id); folio_unqueue_deferred_split(folio); folio->memcg_data = 0; @@ -340,8 +354,11 @@ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci) void memcg1_swapin(struct folio *folio) { struct swap_cluster_info *ci; + struct swap_cluster_info_dynamic *ci_dyn; unsigned long nr_pages; + unsigned int start, i, nr_swapfile = 0; unsigned short id; + bool is_vswap; VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); @@ -366,12 +383,26 @@ void memcg1_swapin(struct folio *folio) * let's not wait for it. The page already received a * memory+swap charge, drop the swap entry duplicate. */ + is_vswap = is_vswap_entry(folio->swap); nr_pages = folio_nr_pages(folio); + start = swp_cluster_offset(folio->swap); ci = swap_cluster_get_and_lock(folio); - id = __swap_cgroup_clear(ci, swp_cluster_offset(folio->swap), - nr_pages); + id = __swap_cgroup_clear(ci, start, nr_pages); + if (id && is_vswap) { + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + for (i = start; i < start + nr_pages; i++) + if (vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE) + nr_swapfile++; + } swap_cluster_unlock(ci); - mem_cgroup_uncharge_swap(id, nr_pages); + if (!id) + return; + if (is_vswap) { + mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile); + mem_cgroup_swap_put(id, nr_pages); + } else { + mem_cgroup_dir_phys_swap_free(id, nr_pages); + } } #endif diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 820139657184..3eeac14aeecf 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -124,4 +124,23 @@ static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int #endif /* CONFIG_MEMCG_V1 */ +#if defined(CONFIG_MEMCG) && defined(CONFIG_SWAP) +void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages, + unsigned int nr_swapfile); +static inline void mem_cgroup_swap_uncharge(unsigned short id, + unsigned int nr_pages, + unsigned int nr_swapfile) +{ + if ((!nr_swapfile && !do_memsw_account()) || mem_cgroup_disabled()) + return; + __mem_cgroup_swap_uncharge(id, nr_pages, nr_swapfile); +} +#else +static inline void mem_cgroup_swap_uncharge(unsigned short id, + unsigned int nr_pages, + unsigned int nr_swapfile) +{ +} +#endif + #endif /* __MM_MEMCONTROL_V1_H */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 978c47ad4b93..571f375b2e7f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -66,6 +66,7 @@ #include "internal.h" #include "swap.h" #include "swap_table.h" +#include "vswap.h" #include #include #include "slab.h" @@ -6050,18 +6051,27 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci) { unsigned int start = swp_cluster_offset(folio->swap); unsigned int end = start + folio_nr_pages(folio); - unsigned int i, nr = 0; + unsigned int i, nr = 0, nr_swapfile = 0; struct mem_cgroup *memcg; struct obj_cgroup *objcg; + struct swap_cluster_info_dynamic *ci_dyn; unsigned short id; + bool is_vswap = is_vswap_entry(folio->swap); if (mem_cgroup_disabled() || !do_memsw_account()) return; lockdep_assert_held(&ci->lock); - for (i = start; i < end; i++) - if (__swp_tb_get_count(__swap_table_get(ci, i))) - nr++; + if (is_vswap) + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + for (i = start; i < end; i++) { + if (!__swp_tb_get_count(__swap_table_get(ci, i))) + continue; + nr++; + if (!is_vswap || + vtable_type(__vtable_get(ci_dyn, i)) == VSWAP_SWAPFILE) + nr_swapfile++; + } if (!nr) return; @@ -6076,7 +6086,8 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci) memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id)); if (!mem_cgroup_private_id_is_root(id)) page_counter_charge(&memcg->memsw, nr); - mod_memcg_state(memcg, MEMCG_SWAP, nr); + if (nr_swapfile) + mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile); for (i = start; i < end; i++) if (__swp_tb_get_count(__swap_table_get(ci, i))) @@ -6085,21 +6096,20 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci) #endif /** - * __mem_cgroup_try_charge_swap - try charging swap space for a folio + * __mem_cgroup_swap_get - pin the memcg to account a folio's swap slots to * @folio: folio being added to swap * - * Try to charge @folio's memcg for the swap space at folio->swap. + * Pins one live private ID ref per page of @folio. The caller charges and + * records using the returned ID, including when its owner is reparented. * - * Returns 0 on success, -ENOMEM on failure. + * Return: the pinned private ID, or 0 if there is nothing to account. Drop + * the pins with __mem_cgroup_swap_put(). */ -int __mem_cgroup_try_charge_swap(struct folio *folio) +unsigned short __mem_cgroup_swap_get(struct folio *folio) { unsigned int nr_pages = folio_nr_pages(folio); - struct swap_cluster_info *ci; - struct page_counter *counter; struct mem_cgroup *memcg; struct obj_cgroup *objcg; - unsigned short private_id; if (do_memsw_account()) return 0; @@ -6109,38 +6119,85 @@ int __mem_cgroup_try_charge_swap(struct folio *folio) if (!objcg) return 0; - scoped_guard(rcu) { - memcg = obj_cgroup_memcg(objcg); - if (!folio_test_swapcache(folio)) { - memcg_memory_event(memcg, MEMCG_SWAP_FAIL); - return 0; - } + guard(rcu)(); + memcg = obj_cgroup_memcg(objcg); + if (!folio_test_swapcache(folio)) { + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); + return 0; + } - private_id = mem_cgroup_private_id_get(memcg, nr_pages); + return mem_cgroup_private_id_get(memcg, nr_pages); +} - if (!mem_cgroup_private_id_is_root(private_id) && - !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { - memcg_memory_event(memcg, MEMCG_SWAP_MAX); - memcg_memory_event(memcg, MEMCG_SWAP_FAIL); - mem_cgroup_private_id_put(private_id, nr_pages); - return -ENOMEM; - } - mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); +/** + * __mem_cgroup_swap_fail - report a failed physical swap allocation + * @id: the pinned private ID of the swap owner (may be 0) + */ +void __mem_cgroup_swap_fail(unsigned short id) +{ + struct mem_cgroup *memcg; + + if (do_memsw_account() || !id) + return; + + guard(rcu)(); + memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id)); + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); +} + +/** + * __mem_cgroup_swap_charge - charge physical swap space + * @id: the private ID to charge (may be 0) + * @nr_pages: the amount of swap space to charge + * + * Return: 0 on success, -ENOMEM if memory.swap.max is exceeded. + */ +int __mem_cgroup_swap_charge(unsigned short id, unsigned int nr_pages) +{ + struct page_counter *counter; + struct mem_cgroup *memcg; + + if (do_memsw_account() || !id) + return 0; + + guard(rcu)(); + memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id)); + if (!mem_cgroup_private_id_is_root(id) && + !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { + memcg_memory_event(memcg, MEMCG_SWAP_MAX); + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); + return -ENOMEM; } + mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); + return 0; +} + +/** + * __mem_cgroup_swap_record - record the owner of a folio's swap slots + * @folio: folio being added to swap + * @id: the private ID pinned by __mem_cgroup_swap_get() + */ +void __mem_cgroup_swap_record(struct folio *folio, unsigned short id) +{ + struct swap_cluster_info *ci; ci = swap_cluster_get_and_lock(folio); - __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_pages, private_id); + __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), + folio_nr_pages(folio), id); swap_cluster_unlock(ci); - - return 0; } /** - * __mem_cgroup_uncharge_swap - uncharge swap space + * __mem_cgroup_dir_phys_swap_free - uncharge and drop the reference for + * directly mapped physical swap entries * @id: cgroup id to uncharge - * @nr_pages: the amount of swap space to uncharge + * @nr_pages: number of directly mapped physical swap entries + * + * This is roughly equivalent to calling mem_cgroup_swap_uncharge() + * followed by mem_cgroup_swap_put(). Reuse the resolved owner to avoid + * a second lookup. */ -void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages) +void __mem_cgroup_dir_phys_swap_free(unsigned short id, unsigned int nr_pages) { struct obj_cgroup *objcg; struct mem_cgroup *memcg; @@ -6161,6 +6218,61 @@ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages) rcu_read_unlock(); } +/** + * __mem_cgroup_swap_uncharge - release cgroup charges for a vswap range + * @id: the private ID to uncharge (may be 0) + * @nr_pages: total number of pages in the range + * @nr_swapfile: number of pages in the range backed by physical swap + * + * MEMCG_SWAP counts physically backed slots attributed to the swap + * owner. Subtract @nr_swapfile from it in both cgroup hierarchies. + * + * In v1, uncharge @nr_pages from memsw because it covers the whole + * range. In v2, uncharge @nr_swapfile from the swap counter because + * it covers physical backing. + * + * In v1, __memcg1_swapout() records the owner when a folio leaves + * swapcache, and memcg1_swapin() clears it when the folio returns. + * Both changes happen under the cluster lock. Backing release on a + * cached folio sees no owner after the swap-in charge transfer. + * + * In v2, the owner is recorded at swap allocation and remains while + * the folio is cached. Backing release balances the charge taken by + * folio_realloc_swap(). + * + * The caller releases the private ID references separately. + */ +void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages, + unsigned int nr_swapfile) +{ + struct mem_cgroup *memcg; + + if (!id) + return; + + guard(rcu)(); + memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id)); + if (!mem_cgroup_private_id_is_root(id)) { + if (do_memsw_account()) + page_counter_uncharge(&memcg->memsw, nr_pages); + else if (nr_swapfile) + page_counter_uncharge(&memcg->swap, nr_swapfile); + } + if (nr_swapfile) + mod_memcg_state(memcg, MEMCG_SWAP, -nr_swapfile); +} + +/** + * __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 + */ +void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages) +{ + guard(rcu)(); + mem_cgroup_private_id_put(id, nr_pages); +} + long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg) { long nr_swap_pages = get_nr_swap_pages(); @@ -6203,11 +6315,8 @@ long mem_cgroup_get_folio_swap_margin(const struct folio *folio) */ bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) { - if (!mem_cgroup_disabled() && !do_memsw_account() && - page_counter_margin(&memcg->swap) < nr_pages) - return false; - - return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >= nr_pages; + return mem_cgroup_can_vswap(memcg) || + mem_cgroup_get_nr_swap_pages(memcg) >= nr_pages; } bool mem_cgroup_swap_full(const struct folio *folio) diff --git a/mm/swapfile.c b/mm/swapfile.c index afc48d33cc40..a1eef85b9eb6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -47,6 +47,7 @@ #include #include +#include "memcontrol-v1.h" #include "swap_table.h" #include "vswap.h" #include "internal.h" @@ -2110,6 +2111,7 @@ static swp_entry_t folio_alloc_phys_swap(struct folio *folio) int folio_alloc_swap(struct folio *folio) { unsigned int order = folio_order(folio); + unsigned short memcg_id; unsigned int size = 1 << order; bool may_vswap; @@ -2139,10 +2141,20 @@ int folio_alloc_swap(struct folio *folio) if (!may_vswap || !vswap_alloc(folio)) folio_alloc_phys_swap(folio); - /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ - if (unlikely(mem_cgroup_try_charge_swap(folio))) { - swap_cache_del_folio(folio); - goto failed; + /* + * Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. + * A vswap entry has no physical swap yet, so only record the memcg. + * folio_realloc_swap() charges it once backing is allocated. + */ + memcg_id = mem_cgroup_swap_get(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); + swap_cache_del_folio(folio); + } else { + mem_cgroup_swap_record(folio, memcg_id); + } } if (unlikely(!folio_test_swapcache(folio))) @@ -2153,7 +2165,7 @@ int folio_alloc_swap(struct folio *folio) failed: if (get_nr_swap_pages() <= 0 && !may_vswap) return -ENOSPC; - if (mem_cgroup_get_folio_swap_margin(folio) <= 0) + if (!may_vswap && mem_cgroup_get_folio_swap_margin(folio) <= 0) return -ENOMEM; return order ? -E2BIG : -ENOMEM; @@ -2224,12 +2236,25 @@ void __vswap_release_backing(struct swap_cluster_info *ci, unsigned int ci_off; unsigned long vt; swp_entry_t phys_first = {}; + unsigned short batch_id, cur_id; + unsigned int batch_nr = 0, batch_nr_swapfile = 0; lockdep_assert_held(&ci->lock); ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + batch_id = __swap_cgroup_get(ci, ci_start); for (ci_off = ci_start; ci_off < ci_start + nr; ci_off++) { vt = __vtable_get(ci_dyn, ci_off); + cur_id = __swap_cgroup_get(ci, ci_off); + + if (cur_id != batch_id) { + mem_cgroup_swap_uncharge(batch_id, batch_nr, + batch_nr_swapfile); + batch_id = cur_id; + batch_nr = 0; + batch_nr_swapfile = 0; + } + batch_nr++; /* The free helper takes one contiguous run within one cluster. */ if (phys_off_start != phys_off_end && @@ -2247,6 +2272,7 @@ void __vswap_release_backing(struct swap_cluster_info *ci, switch (vtable_type(vt)) { case VSWAP_SWAPFILE: + batch_nr_swapfile++; if (phys_off_start == phys_off_end) { phys_first = vtable_to_phys(vt); phys_off_start = swp_offset(phys_first); @@ -2278,6 +2304,8 @@ void __vswap_release_backing(struct swap_cluster_info *ci, phys_off_start % SWAPFILE_CLUSTER, phys_off_end - phys_off_start); } + + mem_cgroup_swap_uncharge(batch_id, batch_nr, batch_nr_swapfile); } /** @@ -2368,6 +2396,8 @@ swp_entry_t folio_realloc_swap(struct folio *folio) struct swap_cluster_info *ci; struct swap_cluster_info_dynamic *ci_dyn; unsigned int voff; + unsigned long vt; + unsigned short memcg_id; swp_entry_t phys_entry = {}; swp_entry_t pe; int i, nr = folio_nr_pages(folio); @@ -2376,18 +2406,33 @@ swp_entry_t folio_realloc_swap(struct folio *folio) VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio); VM_WARN_ON(!is_vswap_entry(vswap_entry)); - phys_entry = vswap_to_phys(vswap_entry); - if (phys_entry.val) - return phys_entry; + voff = swp_cluster_offset(vswap_entry); + ci = __swap_entry_to_cluster(vswap_entry); + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + + spin_lock(&ci->lock); + vt = __vtable_get(ci_dyn, voff); + if (vtable_type(vt) == VSWAP_SWAPFILE) { + spin_unlock(&ci->lock); + return vtable_to_phys(vt); + } + memcg_id = __swap_cgroup_get(ci, voff); + spin_unlock(&ci->lock); phys_entry = folio_alloc_phys_swap(folio); - if (!phys_entry.val) + if (!phys_entry.val) { + mem_cgroup_swap_fail(memcg_id); return (swp_entry_t){}; + } - voff = swp_cluster_offset(vswap_entry); + if (mem_cgroup_swap_charge(memcg_id, nr)) { + __swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry), + __swap_entry_to_cluster(phys_entry), + swp_cluster_offset(phys_entry), + nr); + return (swp_entry_t){}; + } - ci = __swap_entry_to_cluster(vswap_entry); - ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); spin_lock(&ci->lock); /* * Install PHYS backing without freeing any prior contents of the @@ -2572,6 +2617,18 @@ static void __swap_cluster_free_phys_backing(struct swap_info_struct *psi, swap_cluster_unlock(pci); } +/* + * Release cgroup accounting for freed swap slots. Physical backing of + * vswap entries was already uncharged by __vswap_release_backing(). + */ +static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_vswap) +{ + if (is_vswap) + mem_cgroup_swap_put(id, nr); + else + mem_cgroup_dir_phys_swap_free(id, nr); +} + /* * Free a set of swap slots after their swap count dropped to zero, or will be * zero after putting the last ref (saves one __swap_cluster_put_entry call). @@ -2584,10 +2641,11 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, unsigned short batch_id = 0, id_cur; unsigned int ci_off = ci_start, ci_end = ci_start + nr_pages; unsigned int batch_off = ci_off; + bool is_vswap = swap_is_vswap(si); VM_WARN_ON(ci->count < nr_pages); - if (swap_is_vswap(si)) + if (is_vswap) __vswap_release_backing(ci, ci_start, nr_pages); ci->count -= nr_pages; @@ -2613,14 +2671,14 @@ void __swap_cluster_free_entries(struct swap_info_struct *si, id_cur = __swap_cgroup_clear(ci, ci_off, 1); if (batch_id != id_cur) { if (batch_id) - mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off); + memcg_swap_free(batch_id, ci_off - batch_off, is_vswap); batch_id = id_cur; batch_off = ci_off; } } while (++ci_off < ci_end); if (batch_id) - mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off); + memcg_swap_free(batch_id, ci_off - batch_off, is_vswap); __swap_cluster_finish_free(si, ci, ci_start, nr_pages); } diff --git a/mm/zswap.c b/mm/zswap.c index 54b4ddcf7d73..f1f0fdf3945d 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1220,11 +1220,17 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx, * With vswap disabled, all zswap entries use directly mapped physical swap * entries. With vswap enabled, such entries are rare. * - * A zswap-backed vswap entry needs physical swap capacity for writeback. + * A zswap-backed vswap entry needs physical swap capacity for writeback + * and cgroup swap margin where applicable. @memcg may be NULL to check + * device capacity alone. */ -static bool zswap_writeback_possible(void) +static bool zswap_writeback_possible(struct mem_cgroup *memcg) { - return !vswap_is_enabled() || get_nr_swap_pages() > 0; + if (!vswap_is_enabled()) + return true; + if (!memcg) + return get_nr_swap_pages() > 0; + return mem_cgroup_get_nr_swap_pages(memcg) > 0; } /* @@ -1372,7 +1378,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg)) return 0; - if (!zswap_writeback_possible()) + if (!zswap_writeback_possible(memcg)) return 0; /* @@ -1459,7 +1465,7 @@ static struct shrinker *zswap_alloc_shrinker(void) * writeback disabled, is a zombie cgroup, or has empty zswap LRUs. * * Also returns -ENOENT when vswap is enabled and there is no physical - * swap to write back to. + * swap for @memcg to write back to. */ static int shrink_memcg(struct mem_cgroup *memcg) { @@ -1470,7 +1476,7 @@ static int shrink_memcg(struct mem_cgroup *memcg) if (!mem_cgroup_zswap_writeback_enabled(memcg)) return -ENOENT; - if (!zswap_writeback_possible()) + if (!zswap_writeback_possible(memcg)) return -ENOENT; /* @@ -1505,7 +1511,7 @@ static void shrink_worker(struct work_struct *w) int ret, failures = 0, attempts = 0; unsigned long thr; - if (!zswap_writeback_possible()) + if (!zswap_writeback_possible(NULL)) return; /* Reclaim down to the accept threshold */ @@ -1586,7 +1592,7 @@ static void shrink_worker(struct work_struct *w) break; resched: cond_resched(); - } while (zswap_total_pages() > thr && zswap_writeback_possible()); + } while (zswap_total_pages() > thr && zswap_writeback_possible(NULL)); } /********************************* -- 2.53.1-Meta