From: Xueyuan Chen <xueyuan.chen21@gmail.com>
To: akpm@linux-foundation.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
baohua@kernel.org, zhaonanzhe@xiaomi.com, hannes@cmpxchg.org,
mhocko@kernel.org, roman.gushchin@linux.dev,
shakeel.butt@linux.dev, muchun.song@linux.dev, chrisl@kernel.org,
kasong@tencent.com, shikemeng@huaweicloud.com, nphamcs@gmail.com,
bhe@redhat.com, youngjun.park@lge.com, david@kernel.org,
ljs@kernel.org, liam@infradead.org, vbabka@kernel.org,
rppt@kernel.org, surenb@google.com, qi.zheng@linux.dev,
axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com,
baolin.wang@linux.alibaba.com, hughd@google.com,
Xueyuan Chen <xueyuan.chen21@gmail.com>
Subject: [RFC PATCH v3 1/4] mm: add page_counter_margin()
Date: Fri, 17 Jul 2026 20:25:11 +0800 [thread overview]
Message-ID: <20260717122514.51514-2-xueyuan.chen21@gmail.com> (raw)
In-Reply-To: <20260717122514.51514-1-xueyuan.chen21@gmail.com>
From: Johannes Weiner <hannes@cmpxchg.org>
mem_cgroup_get_nr_swap_pages() open-codes the remaining capacity across
the memcg swap counter hierarchy.
Add page_counter_margin() to return the minimum usable space from a page
counter to the root, and use it in mem_cgroup_get_nr_swap_pages(). This
is a pure refactoring with no intended behavior change.
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Xueyuan Chen <xueyuan.chen21@gmail.com>
---
include/linux/page_counter.h | 1 +
mm/memcontrol.c | 9 +++------
mm/page_counter.c | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
index d649b6bbbc87..07b7cb12249c 100644
--- a/include/linux/page_counter.h
+++ b/include/linux/page_counter.h
@@ -68,6 +68,7 @@ static inline unsigned long page_counter_read(struct page_counter *counter)
return atomic_long_read(&counter->usage);
}
+long page_counter_margin(struct page_counter *counter);
void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
bool page_counter_try_charge(struct page_counter *counter,
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 177732fef010..1e10f493d2a9 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -5548,12 +5548,9 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
{
long nr_swap_pages = get_nr_swap_pages();
- if (mem_cgroup_disabled() || do_memsw_account())
- return nr_swap_pages;
- for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg))
- nr_swap_pages = min_t(long, nr_swap_pages,
- READ_ONCE(memcg->swap.max) -
- page_counter_read(&memcg->swap));
+ if (!mem_cgroup_disabled() && !do_memsw_account())
+ nr_swap_pages = min(nr_swap_pages, page_counter_margin(&memcg->swap));
+
return nr_swap_pages;
}
diff --git a/mm/page_counter.c b/mm/page_counter.c
index 661e0f2a5127..7300dca90632 100644
--- a/mm/page_counter.c
+++ b/mm/page_counter.c
@@ -46,6 +46,23 @@ static void propagate_protected_usage(struct page_counter *c,
}
}
+/**
+ * page_counter_margin - remaining usable space within hierarchical limits
+ * @counter: counter
+ */
+long page_counter_margin(struct page_counter *counter)
+{
+ long margin = PAGE_COUNTER_MAX;
+
+ do {
+ long m = READ_ONCE(counter->max) - page_counter_read(counter);
+
+ margin = min(margin, m);
+ } while ((counter = counter->parent));
+
+ return margin;
+}
+
/**
* page_counter_cancel - take pages out of the local counter
* @counter: counter
--
2.47.3
next prev parent reply other threads:[~2026-07-17 12:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-17 12:25 [RFC PATCH v3 0/4] mm: avoid large folio splits when swap is unavailable Xueyuan Chen
2026-07-17 12:25 ` Xueyuan Chen [this message]
2026-07-17 12:25 ` [RFC PATCH v3 2/4] mm: distinguish large folio swap allocation failures Xueyuan Chen
2026-07-21 8:15 ` Barry Song
2026-07-21 11:53 ` Johannes Weiner
2026-07-23 14:49 ` Xueyuan Chen
2026-07-21 16:13 ` Youngjun Park
2026-07-23 15:15 ` Xueyuan Chen
2026-07-17 12:25 ` [RFC PATCH v3 3/4] mm/vmscan: avoid pointless large folio splits without swap Xueyuan Chen
2026-07-21 8:00 ` Barry Song
2026-07-17 12:25 ` [RFC PATCH v3 4/4] mm/shmem: split large folios only on -E2BIG Xueyuan Chen
2026-07-21 7:57 ` Barry Song
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=20260717122514.51514-2-xueyuan.chen21@gmail.com \
--to=xueyuan.chen21@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bhe@redhat.com \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=qi.zheng@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=weixugc@google.com \
--cc=youngjun.park@lge.com \
--cc=yuanchu@google.com \
--cc=zhaonanzhe@xiaomi.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