From: Youngjun Park <youngjun.park@lge.com>
To: Andrew Morton <akpm@linux-foundation.org>, linux-mm@kvack.org
Cc: Johannes Weiner <hannes@cmpxchg.org>,
Chris Li <chrisl@kernel.org>, Kairui Song <kasong@tencent.com>,
Kemeng Shi <shikemeng@huaweicloud.com>,
Nhat Pham <nphamcs@gmail.com>, Baoquan He <baoquan.he@linux.dev>,
Barry Song <baohua@kernel.org>,
Shakeel Butt <shakeel.butt@linux.dev>,
Yosry Ahmed <yosry.ahmed@linux.dev>, Lian <lianux.mm@gmail.com>,
Youngjun Park <youngjun.park@lge.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier
Date: Wed, 2 Dec 2026 17:48:26 +0900 [thread overview]
Message-ID: <20261202084826.2131931-4-youngjun.park@lge.com> (raw)
In-Reply-To: <20261202084826.2131931-1-youngjun.park@lge.com>
Add /sys/kernel/debug/swap/memcg_tiers to give a memory cgroup a maximum
swap tier, which is a swap priority. The effective maximum is the lowest
one among the cgroup and its ancestors, so a child cannot use a tier
its parent cannot. A child may set a lower max than its parent, but a
higher one has no effect.
# swapon -p 100 /dev/nvme0n1p2
# swapon -p 50 /dev/sdb2
# echo "/batch 50" > /sys/kernel/debug/swap/memcg_tiers
# cat /sys/kernel/debug/swap/memcg_tiers
/batch 50
/batch then only gets swap from sdb2. Writing "max" removes the limit:
# echo "/batch max" > /sys/kernel/debug/swap/memcg_tiers
Limits are keyed by cgroup ID, which is never reused, so a cgroup
recreated with the same path starts without a limit. The entry of a
removed cgroup is listed as [removed:<id>] until it is removed by ID:
# rmdir /sys/fs/cgroup/batch
# cat /sys/kernel/debug/swap/memcg_tiers
[removed:142] 50
# echo "142 max" > /sys/kernel/debug/swap/memcg_tiers
This is a step before tiers are made explicit in memcg, which would
come with memory.swap.prio.max (or memory.swap.tiers.max) once virtual
swap lands. memcg itself is not touched.
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 210 insertions(+), 3 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d30ee41f8217..6acf177cb2df 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -33,6 +33,8 @@
#include <linux/capability.h>
#include <linux/syscalls.h>
#include <linux/memcontrol.h>
+#include <linux/cgroup.h>
+#include <linux/debugfs.h>
#include <linux/poll.h>
#include <linux/oom.h>
#include <linux/swapfile.h>
@@ -1439,14 +1441,219 @@ static inline short swap_tier(struct swap_info_struct *si)
return si->prio;
}
+#if defined(CONFIG_MEMCG) && defined(CONFIG_DEBUG_FS)
/*
- * Highest swap tier the memcg of @folio may allocate from. No limit
- * can be set yet.
+ * Per-memcg maximum swap tier (priority), set through debugfs.
+ *
+ * This is a step before tiers are made explicit in memcg
+ * (memory.swap.prio.max or memory.swap.tiers.max).
+ *
+ * The cgroup may only use swap tiers at or below its max. The effective
+ * max is the lowest one among the cgroup and all its ancestors.
+ */
+struct memcg_swap_prio {
+ struct list_head node;
+ u64 id;
+ short max;
+ struct rcu_head rcu;
+};
+
+static LIST_HEAD(memcg_prio_list);
+static DEFINE_MUTEX(memcg_prio_lock);
+
+static struct memcg_swap_prio *memcg_prio_find(u64 id)
+{
+ struct memcg_swap_prio *r;
+
+ list_for_each_entry_rcu(r, &memcg_prio_list, node,
+ lockdep_is_held(&memcg_prio_lock)) {
+ if (r->id == id)
+ return r;
+ }
+
+ return NULL;
+}
+
+/* Highest swap tier the memcg of @folio may allocate from. */
+static short swap_memcg_max_tier(struct folio *folio)
+{
+ struct mem_cgroup *memcg;
+ struct memcg_swap_prio *r;
+ short max = SWAP_TIER_MAX;
+
+ if (mem_cgroup_disabled())
+ return max;
+
+ rcu_read_lock();
+ if (list_empty(&memcg_prio_list))
+ goto out;
+
+ for (memcg = folio_memcg(folio); memcg;
+ memcg = parent_mem_cgroup(memcg)) {
+ r = memcg_prio_find(mem_cgroup_id(memcg));
+ if (!r)
+ continue;
+ max = min_t(short, max, r->max);
+ }
+out:
+ rcu_read_unlock();
+
+ return max;
+}
+
+static void memcg_prio_free(struct memcg_swap_prio *r)
+{
+ list_del_rcu(&r->node);
+ kfree_rcu(r, rcu);
+}
+
+static int memcg_prio_parse(const char *tok, short *val)
+{
+ if (!strcmp(tok, "max")) {
+ *val = SWAP_TIER_MAX;
+ return 0;
+ }
+
+ return kstrtos16(tok, 0, val);
+}
+
+static int memcg_prio_show(struct seq_file *m, void *v)
+{
+ struct memcg_swap_prio *r;
+ struct mem_cgroup *memcg;
+ char *buf;
+
+ buf = kmalloc(PATH_MAX, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ mutex_lock(&memcg_prio_lock);
+ list_for_each_entry(r, &memcg_prio_list, node) {
+ memcg = mem_cgroup_get_from_id(r->id);
+ if (memcg) {
+ if (cgroup_path(memcg->css.cgroup, buf, PATH_MAX) < 0)
+ strscpy(buf, "?", PATH_MAX);
+ mem_cgroup_put(memcg);
+ } else {
+ snprintf(buf, PATH_MAX, "[removed:%llu]", r->id);
+ }
+ seq_printf(m, "%s %d\n", buf, r->max);
+ }
+ mutex_unlock(&memcg_prio_lock);
+ kfree(buf);
+
+ return 0;
+}
+
+static int memcg_prio_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, memcg_prio_show, NULL);
+}
+
+/* Set (or remove, for SWAP_TIER_MAX) the max tier of memcg @id. */
+static int memcg_prio_set(u64 id, short max)
+{
+ struct memcg_swap_prio *r;
+ int ret = 0;
+
+ mutex_lock(&memcg_prio_lock);
+ r = memcg_prio_find(id);
+ if (max == SWAP_TIER_MAX) {
+ if (r)
+ memcg_prio_free(r);
+ else
+ ret = -ENOENT;
+ } else if (r) {
+ WRITE_ONCE(r->max, max);
+ } else {
+ r = kzalloc_obj(*r);
+ if (r) {
+ r->id = id;
+ r->max = max;
+ list_add_tail_rcu(&r->node, &memcg_prio_list);
+ } else {
+ ret = -ENOMEM;
+ }
+ }
+ mutex_unlock(&memcg_prio_lock);
+
+ return ret;
+}
+
+/*
+ * "<cgroup path> <max_prio>" sets the limit, "<cgroup path> max" removes it.
+ * An entry of a removed cgroup is shown as [removed:<id>] and can only be
+ * dropped by "<id> max".
*/
+static ssize_t memcg_prio_write(struct file *file, const char __user *ubuf,
+ size_t count, loff_t *ppos)
+{
+ char *buf, *target, *maxs, *rest;
+ struct cgroup_subsys_state *css;
+ struct cgroup *cgrp;
+ short max;
+ u64 id;
+ int ret = -EINVAL;
+
+ if (count >= PATH_MAX + 32)
+ return -EINVAL;
+ buf = memdup_user_nul(ubuf, count);
+ if (IS_ERR(buf))
+ return PTR_ERR(buf);
+
+ rest = strim(buf);
+ target = strsep(&rest, " \t");
+ maxs = rest ? strsep(&rest, " \t") : NULL;
+ if (!*target || !maxs || !*maxs || (rest && *rest) ||
+ memcg_prio_parse(maxs, &max))
+ goto out;
+
+ if (*target == '/') {
+ cgrp = cgroup_get_from_path(target);
+ if (IS_ERR(cgrp)) {
+ ret = PTR_ERR(cgrp);
+ goto out;
+ }
+ css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
+ cgroup_put(cgrp);
+ if (!css)
+ goto out;
+ id = mem_cgroup_id(mem_cgroup_from_css(css));
+ css_put(css);
+ } else if (kstrtou64(target, 0, &id) || max != SWAP_TIER_MAX) {
+ goto out;
+ }
+
+ ret = memcg_prio_set(id, max);
+out:
+ kfree(buf);
+
+ return ret ? ret : count;
+}
+
+static const struct file_operations memcg_prio_fops = {
+ .open = memcg_prio_open,
+ .read = seq_read,
+ .write = memcg_prio_write,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static int __init swap_memcg_debugfs_init(void)
+{
+ struct dentry *root = debugfs_create_dir("swap", NULL);
+
+ debugfs_create_file("memcg_tiers", 0644, root, NULL,
+ &memcg_prio_fops);
+ return 0;
+}
+late_initcall(swap_memcg_debugfs_init);
+#else
static inline short swap_memcg_max_tier(struct folio *folio)
{
return SWAP_TIER_MAX;
}
+#endif /* CONFIG_MEMCG && CONFIG_DEBUG_FS */
/*
* Fast path try to get swap entries with specified order from current
@@ -1495,7 +1702,7 @@ static void swap_alloc_slow(struct folio *folio)
spin_lock(&swap_avail_lock);
start_over:
plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
- /* Skip tiers above the memcg limit. */
+ /* Skip tiers (priorities) above the memcg limit. */
if (swap_tier(si) > max)
continue;
/* Rotate the device and switch to a new cluster */
--
2.34.1
prev parent reply other threads:[~2026-12-02 8:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 8:48 [PATCH v12 0/3] mm/swap: swap tier phase 1: priority as tier, per-cgroup via debugfs Youngjun Park
2026-12-02 8:48 ` [PATCH v12 1/3] mm: swap: introduce the swap tier as a swap priority Youngjun Park
2026-12-02 8:48 ` [PATCH v12 2/3] mm: swap: skip swap tiers above the memcg max tier Youngjun Park
2026-12-02 8:48 ` Youngjun Park [this message]
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=20261202084826.2131931-4-youngjun.park@lge.com \
--to=youngjun.park@lge.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kasong@tencent.com \
--cc=lianux.mm@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shakeel.butt@linux.dev \
--cc=shikemeng@huaweicloud.com \
--cc=yosry.ahmed@linux.dev \
/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
all inboxes | Powered by JetHome®