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 24/32] mm, swap: account xswap backends for cgroup v1
Date: Fri, 4 Dec 2026 02:13:11 +0800 [thread overview]
Message-ID: <20261203181319.901844-25-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261203181319.901844-1-baoquan.he@linux.dev>
From: Baoquan He <hebaoquan@kylinos.cn>
Cgroup v1 keeps the memsw charge on the swap slot, while v2 charges the
physical backing. xswap uncharges the backing in several paths, so it
must not touch memsw in v1 mode:
- memcg_swap_free() skipped the uncharge for every xswap slot, so a
zswap-only xswap slot leaked its memsw charge;
- the physical release paths uncharged memsw early, and swapin or slot
free then uncharged it again.
Add mem_cgroup_swap_memsw_accounted() so the swap code can tell the two
modes apart. In v1, leave memsw to the slot free/swapin path; in v2,
keep uncharging the physical backing when it is released.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 6 ++++++
mm/memcontrol.c | 6 ++++++
mm/swapfile.c | 26 ++++++++++++++------------
3 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 43c2e258a5df..749fbd363eee 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -581,6 +581,7 @@ static inline void mem_cgroup_swap_put(unsigned short id,
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);
+bool mem_cgroup_swap_memsw_accounted(void);
#else
static inline bool mem_cgroup_swap_get(struct folio *folio, unsigned short *id)
{
@@ -622,6 +623,11 @@ static inline bool mem_cgroup_swap_full(const struct folio *folio)
{
return vm_swap_full();
}
+
+static inline bool mem_cgroup_swap_memsw_accounted(void)
+{
+ return false;
+}
#endif
/* for_each_managed_zone_pgdat - helper macro to iterate over all managed zones in a pgdat up to
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 9091af061ff8..583aef97e4d8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6207,6 +6207,12 @@ void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
rcu_read_unlock();
}
+/* Cgroup v1 keeps the swap charge on the slot; v2 keeps it on the backing. */
+bool mem_cgroup_swap_memsw_accounted(void)
+{
+ return do_memsw_account();
+}
+
long mem_cgroup_get_nr_swap_pages(const struct mem_cgroup *memcg)
{
long nr_swap_pages;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 5de905970668..d8f09f46123a 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2417,13 +2417,12 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
}
/*
- * An xswap entry was not charged at allocation, so there is nothing to
- * uncharge here and only the owner refs go. Its backing, if any, was
- * uncharged when it was released.
+ * v2 charges the physical backing, not the xswap slot, so the backing
+ * release uncharged it. v1 keeps the memsw charge on the slot.
*/
static void memcg_swap_free(unsigned short id, unsigned int nr, bool is_xswap)
{
- if (!is_xswap)
+ if (!is_xswap || mem_cgroup_swap_memsw_accounted())
mem_cgroup_swap_uncharge(id, nr);
mem_cgroup_swap_put(id, nr);
}
@@ -3445,8 +3444,11 @@ static void xswap_release_slot_backend(struct swap_info_struct *si,
if (psi)
xswap_free_phys_slot(psi, entry, phys);
- /* ci->lock is held here, so read the owner out of the swap table directly. */
- if (id)
+ /*
+ * ci->lock is held here, so read the owner out of the swap table
+ * directly. v1 keeps the charge on the slot, so do not uncharge here.
+ */
+ if (id && !mem_cgroup_swap_memsw_accounted())
mem_cgroup_swap_uncharge(id, 1);
}
@@ -3678,10 +3680,10 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset)
folio_put(folio);
/*
- * Clear the record and uncharge under one lock. A concurrent slot
- * free then cannot uncharge twice or clear the id before we read it.
- * The mapped range cannot shrink while the device is pinned, so
- * @xoff still has a cluster here whenever @drop is set.
+ * Clear the record and uncharge (v2) under one lock. A concurrent
+ * slot free then cannot uncharge twice or clear the id before we
+ * read it. The mapped range cannot shrink while the device is
+ * pinned, so @xoff still has a cluster here whenever @drop is set.
*/
if (drop) {
xci = swap_cluster_lock(xsi, xoff);
@@ -3692,7 +3694,7 @@ static int xswap_unuse_rmap(struct swap_info_struct *si, unsigned long offset)
if (xswap_slot_backend(xci, slot).val == phys.val) {
id = xswap_entry_id(xci, slot);
WRITE_ONCE(xci->xs_table[slot], 0);
- if (id)
+ if (id && !mem_cgroup_swap_memsw_accounted())
mem_cgroup_swap_uncharge(id, 1);
}
swap_cluster_unlock(xci);
@@ -5020,7 +5022,7 @@ static void __xswap_backend_unlink(swp_entry_t entry, swp_entry_t phys,
unsigned short id = xswap_entry_id(ci, slot);
WRITE_ONCE(xs[slot], 0);
- if (id)
+ if (id && !mem_cgroup_swap_memsw_accounted())
mem_cgroup_swap_uncharge(id, 1);
}
swap_cluster_unlock(ci);
--
2.54.0
next prev parent reply other threads:[~2026-12-03 18:16 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 ` [PATCH v2 18/32] mm, swap: split the swap memcg charge helpers Baoquan He
2026-12-03 18:13 ` [PATCH v2 19/32] mm, swap: do not charge zswap-backed xswap entries 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 ` Baoquan He [this message]
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-25-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