From: Baoquan He <baoquan.he@linux.dev>
To: linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org,
kasong@tencent.com, hannes@cmpxchg.org, corbet@lwn.net,
yosry@kernel.org, nphamcs@gmail.com, muchun.song@linux.dev,
baohua@kernel.org, youngjun.park@lge.com, hughd@google.com,
baolin.wang@linux.alibaba.com, baoquan.he@linux.dev,
Baoquan He <hebaoquan@kylinos.cn>
Subject: [PATCH v2 18/32] mm, swap: split the swap memcg charge helpers
Date: Fri, 4 Dec 2026 02:13:05 +0800 [thread overview]
Message-ID: <20261203181319.901844-19-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Nhat Pham <nphamcs@gmail.com>
Split __mem_cgroup_try_charge_swap() into separate get, charge, record,
uncharge and put helpers, and factor mem_cgroup_may_zswap() out of
obj_cgroup_may_zswap(). Recording the owner of a swap slot and charging
it no longer have to happen together, so a later patch can charge swap
only once it gets physical backing.
No functional change.
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
.../admin-guide/cgroup-v1/memcg_test.rst | 2 +-
include/linux/memcontrol.h | 6 +
include/linux/swap.h | 60 +++++++-
mm/memcontrol-v1.c | 3 +-
mm/memcontrol.c | 141 +++++++++++++-----
mm/swapfile.c | 28 +++-
6 files changed, 185 insertions(+), 55 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v1/memcg_test.rst b/Documentation/admin-guide/cgroup-v1/memcg_test.rst
index d9951c319ef5..cd565626c435 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_swap_uncharge()
Called when swp_entry's refcnt goes down to 0. A charge against swap
disappears.
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index c683b1a15ae5..aaa7a035007f 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1968,6 +1968,7 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,
#if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush);
void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);
@@ -1976,6 +1977,11 @@ static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
{
return true;
}
+
+static inline bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
+{
+ return true;
+}
static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg,
size_t size)
{
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 815aee556991..8c1131d924ae 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -528,36 +528,80 @@ 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)
+bool __mem_cgroup_swap_get(struct folio *folio, unsigned short *id);
+static inline bool mem_cgroup_swap_get(struct folio *folio, unsigned short *id)
+{
+ if (mem_cgroup_disabled())
+ return false;
+ return __mem_cgroup_swap_get(folio, 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_try_charge_swap(folio);
+ return __mem_cgroup_swap_charge(id, nr_pages);
}
-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_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_uncharge_swap(id, nr_pages);
+ __mem_cgroup_swap_record(folio, id);
+}
+
+void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages);
+static inline void mem_cgroup_swap_uncharge(unsigned short id,
+ unsigned int nr_pages)
+{
+ if (mem_cgroup_disabled())
+ return;
+ __mem_cgroup_swap_uncharge(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);
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg);
bool mem_cgroup_swap_full(const struct folio *folio);
#else
-static inline int mem_cgroup_try_charge_swap(struct folio *folio)
+static inline bool mem_cgroup_swap_get(struct folio *folio, unsigned short *id)
+{
+ return false;
+}
+
+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,
+static inline void mem_cgroup_swap_record(struct folio *folio,
+ unsigned short id)
+{
+}
+
+static inline void mem_cgroup_swap_uncharge(unsigned short id,
unsigned int nr_pages)
{
}
+static inline void mem_cgroup_swap_put(unsigned short id,
+ unsigned int nr_pages)
+{
+}
+
static inline long mem_cgroup_get_folio_swap_margin(const struct folio *folio)
{
return PAGE_COUNTER_MAX;
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index b7f286888507..b75478457a0e 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -371,7 +371,8 @@ void memcg1_swapin(struct folio *folio)
id = __swap_cgroup_clear(ci, swp_cluster_offset(folio->swap),
nr_pages);
swap_cluster_unlock(ci);
- mem_cgroup_uncharge_swap(id, nr_pages);
+ mem_cgroup_swap_uncharge(id, nr_pages);
+ mem_cgroup_swap_put(id, nr_pages);
}
#endif
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index b75a61ddeded..0a2ef04e17f5 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6072,62 +6072,99 @@ int __init mem_cgroup_init(void)
#ifdef CONFIG_SWAP
/**
- * __mem_cgroup_try_charge_swap - try charging swap space for a folio
+ * __mem_cgroup_swap_get - take the private ID refs for a folio's swap slots
* @folio: folio being added to swap
+ * @id: out, the ID to record and later charge against
*
- * Try to charge @folio's memcg for the swap space at folio->swap.
+ * Takes one private ID ref per page of @folio on its memcg, or on its closest
+ * online ancestor if it has been offlined. The caller records and charges
+ * against whichever ID comes back, so both land on the same memcg.
*
- * Returns 0 on success, -ENOMEM on failure.
+ * Return: true if an owner was taken, false when there is nothing to account
+ * for. Drop the refs with __mem_cgroup_swap_put().
*/
-int __mem_cgroup_try_charge_swap(struct folio *folio)
+bool __mem_cgroup_swap_get(struct folio *folio, unsigned short *id)
{
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;
+ return false;
objcg = folio_objcg(folio);
VM_WARN_ON_ONCE_FOLIO(!objcg, folio);
if (!objcg)
- return 0;
+ return false;
scoped_guard(rcu) {
memcg = obj_cgroup_memcg(objcg);
if (!folio_test_swapcache(folio)) {
memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
- return 0;
+ return false;
}
- private_id = mem_cgroup_private_id_get(memcg, nr_pages);
+ *id = mem_cgroup_private_id_get(memcg, nr_pages);
+ }
+
+ return true;
+}
+
+/**
+ * __mem_cgroup_swap_charge - charge physical swap space
+ * @id: the ID the slots were recorded against
+ * @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())
+ return 0;
+
+ scoped_guard(rcu) {
+ memcg = mem_cgroup_from_private_id(id);
+ if (!memcg)
+ return 0;
- if (!mem_cgroup_private_id_is_root(private_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);
- mem_cgroup_private_id_put(private_id, nr_pages);
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 ID taken 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
- * @id: cgroup id to uncharge
+ * __mem_cgroup_swap_uncharge - uncharge physical swap space
+ * @id: the ID the slots were recorded against
* @nr_pages: the amount of swap space to uncharge
+ *
+ * The owner refs are not touched; drop them with __mem_cgroup_swap_put().
*/
-void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
+void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages)
{
struct obj_cgroup *objcg;
struct mem_cgroup *memcg;
@@ -6143,11 +6180,33 @@ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages)
page_counter_uncharge(&memcg->swap, nr_pages);
}
mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
- __mem_cgroup_private_id_put(objcg, id, nr_pages);
}
rcu_read_unlock();
}
+/**
+ * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
+ * @id: the ID taken by __mem_cgroup_swap_get()
+ * @nr_pages: number of refs to drop
+ *
+ * A separate step from __mem_cgroup_swap_uncharge(): a slot's owner is held
+ * from the allocation and released when the slot is freed, while the
+ * counter is charged and uncharged whenever the physical backing comes and
+ * goes.
+ */
+void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
+{
+ /*
+ * The refs are the thing being dropped, so an ID that no longer
+ * resolves has none left and there is nothing to do.
+ * mem_cgroup_private_id_put() would dereference the NULL instead.
+ */
+ rcu_read_lock();
+ if (obj_cgroup_from_private_id(id))
+ mem_cgroup_private_id_put(id, nr_pages);
+ rcu_read_unlock();
+}
+
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg)
{
long nr_swap_pages = get_nr_swap_pages();
@@ -6344,8 +6403,10 @@ static struct cftype swap_files[] = {
#ifdef CONFIG_ZSWAP
/**
- * obj_cgroup_may_zswap - check if this cgroup can zswap
- * @objcg: the object cgroup
+ * mem_cgroup_may_zswap - check if this cgroup can zswap
+ * @memcg: the memcg to query
+ * @may_flush: force-flush stats for an accurate check (sleeps). Pass false
+ * from atomic contexts; the check is then best-effort.
*
* Check if the hierarchical zswap limit has been reached.
*
@@ -6355,36 +6416,38 @@ static struct cftype swap_files[] = {
* spending cycles on compression when there is already no room left
* or zswap is disabled altogether somewhere in the hierarchy.
*/
-bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg, bool may_flush)
{
- struct mem_cgroup *memcg, *original_memcg;
- bool ret = true;
-
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
return true;
- original_memcg = get_mem_cgroup_from_objcg(objcg);
- for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
- memcg = parent_mem_cgroup(memcg)) {
+ for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
unsigned long max = READ_ONCE(memcg->zswap_max);
unsigned long pages;
if (max == PAGE_COUNTER_MAX)
continue;
- if (max == 0) {
- ret = false;
- break;
- }
+ if (max == 0)
+ return false;
/* Force flush to get accurate stats for charging */
- __mem_cgroup_flush_stats(memcg, true);
+ if (may_flush)
+ __mem_cgroup_flush_stats(memcg, true);
pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
- if (pages < max)
- continue;
- ret = false;
- break;
+ if (pages >= max)
+ return false;
}
- mem_cgroup_put(original_memcg);
+ return true;
+}
+
+bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+{
+ struct mem_cgroup *memcg;
+ bool ret;
+
+ memcg = get_mem_cgroup_from_objcg(objcg);
+ ret = mem_cgroup_may_zswap(memcg, true);
+ mem_cgroup_put(memcg);
return ret;
}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 463f53286a05..3ac91e88c24d 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2164,6 +2164,8 @@ int folio_alloc_swap(struct folio *folio, bool may_enter_fs)
{
unsigned int order = folio_order(folio);
unsigned int size = 1 << order;
+ unsigned short swap_id = 0;
+ /* True unless the gate below finds an xswap device to route away from. */
bool may_zswap = true;
bool zswap_checked = false;
@@ -2228,10 +2230,17 @@ int folio_alloc_swap(struct folio *folio, bool may_enter_fs)
}
}
- /* 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.
+ * The owner is taken here, then charged and recorded.
+ */
+ if (mem_cgroup_swap_get(folio, &swap_id)) {
+ if (unlikely(mem_cgroup_swap_charge(swap_id, size))) {
+ mem_cgroup_swap_put(swap_id, size);
+ swap_cache_del_folio(folio);
+ goto failed;
+ }
+ mem_cgroup_swap_record(folio, swap_id);
}
if (unlikely(!folio_test_swapcache(folio)))
@@ -2398,6 +2407,13 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
return ERR_PTR(-EIO);
}
+/* Uncharge a batch of freed swap slots and drop their owner refs. */
+static void memcg_swap_free(unsigned short id, unsigned int nr)
+{
+ mem_cgroup_swap_uncharge(id, nr);
+ mem_cgroup_swap_put(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).
@@ -2442,14 +2458,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);
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);
swap_range_free(si, ci_head + ci_start, nr_pages);
swap_cluster_assert_empty(ci, ci_start, nr_pages, false);
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:15 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 18:12 [PATCH v2 00/32] mm, swap: extendable swap devices (xswap phases II-IV) Baoquan He
2026-12-03 18:12 ` [PATCH v2 01/32] mm, swap: prepare the swap IO path for xswap backends Baoquan He
2026-12-03 18:12 ` [PATCH v2 02/32] mm, swap: tag a swap table entry with its owning xswap entry Baoquan He
2026-12-03 18:12 ` [PATCH v2 03/32] mm, swap: prepare the folio-less allocation path for xswap Baoquan He
2026-12-03 18:12 ` [PATCH v2 04/32] mm, swap: add a physical backend for xswap slots Baoquan He
2026-12-03 18:12 ` [PATCH v2 05/32] mm, swap: support swapoff of an xswap physical backend Baoquan He
2026-12-03 18:12 ` [PATCH v2 06/32] mm, swap: use the " Baoquan He
2026-12-03 18:12 ` [PATCH v2 07/32] mm, swap: fall back to disk when zswap refuses an xswap page Baoquan He
2026-12-03 18:12 ` [PATCH v2 08/32] mm, swap: reclaim physical slots backing cache-only xswap entries Baoquan He
2026-12-03 18:12 ` [PATCH v2 09/32] mm, swap: drop swap_folio_sector() Baoquan He
2026-12-03 18:12 ` [PATCH v2 10/32] mm, swap: drop a stale xswap physical slot range before reallocating Baoquan He
2026-12-03 18:12 ` [PATCH v2 11/32] mm, swap: publish the xswap backend owner before its reverse map Baoquan He
2026-12-03 18:12 ` [PATCH v2 12/32] mm, swap: skip a NOFS backend for xswap physical writeback Baoquan He
2026-12-03 18:13 ` [PATCH v2 13/32] mm, swap: pass the folio order through the swap slot allocator Baoquan He
2026-12-03 18:13 ` [PATCH v2 14/32] mm, swap: allocate a multi-slot range without a folio Baoquan He
2026-12-03 18:13 ` [PATCH v2 15/32] mm, swap: pass the folio order to the xswap physical backend Baoquan He
2026-12-03 18:13 ` [PATCH v2 16/32] mm, swap: enable large xswap folios with a contiguous physical slot range Baoquan He
2026-12-03 18:13 ` [PATCH v2 17/32] mm, swap: enable THP swapin for xswap entries Baoquan He
2026-12-03 18:13 ` Baoquan He [this message]
2026-12-03 18:13 ` [PATCH v2 19/32] mm, swap: do not charge zswap-backed " Baoquan He
2026-12-03 18:13 ` [PATCH v2 20/32] mm, swap: charge an xswap entry when it gets physical backing Baoquan He
2026-12-03 18:13 ` [PATCH v2 21/32] mm, swap: don't gate xswap on the physical swap free count Baoquan He
2026-12-03 18:13 ` [PATCH v2 22/32] mm, swap: do not retake the cluster lock when uncharging an xswap slot Baoquan He
2026-12-03 18:13 ` [PATCH v2 23/32] mm, swap: uncharge an xswap backend where its record is cleared Baoquan He
2026-12-03 18:13 ` [PATCH v2 24/32] mm, swap: account xswap backends for cgroup v1 Baoquan He
2026-12-03 18:13 ` [PATCH v2 25/32] mm, swap: drop a refused xswap backend slot range directly Baoquan He
2026-12-03 18:13 ` [PATCH v2 26/32] mm, swap: route xswap by the zswap eligibility without flushing Baoquan He
2026-12-03 18:13 ` [PATCH v2 27/32] mm, swap: keep a per-cluster backend record for every swap device Baoquan He
2026-12-03 18:13 ` [PATCH v2 28/32] mm, swap: encode the per-slot backend record Baoquan He
2026-12-03 18:13 ` [PATCH v2 29/32] mm, swap: add accessors for the " Baoquan He
2026-12-03 18:13 ` [PATCH v2 30/32] mm, zswap: index entries in the swap device's backend table Baoquan He
2026-12-03 18:13 ` [PATCH v2 31/32] mm, swap: release a range of backend handles at once Baoquan He
2026-12-03 18:13 ` [PATCH v2 32/32] mm, swap: only run the xswap backend hooks on xswap devices Baoquan He
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=20261203181319.901844-19-baoquan.he@linux.dev \
--to=baoquan.he@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.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