* [PATCH v12 1/3] mm: swap: introduce the swap tier as a swap priority
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 ` 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 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park
2 siblings, 0 replies; 4+ messages in thread
From: Youngjun Park @ 2026-12-02 8:48 UTC (permalink / raw)
To: Andrew Morton, linux-mm
Cc: Johannes Weiner, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Shakeel Butt, Yosry Ahmed, Lian,
Youngjun Park, linux-kernel
Per-cgroup swap device selection needs a notion of swap tier. Devices of
one priority are used round-robin and different priorities are strictly
ordered, so a priority already works as a tier. Add swap_tier() and
SWAP_TIER_MAX (no limit) to say so in code, instead of adding a separate
tier structure.
No functional change.
Suggested-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d59fa8220862..95928d9f85da 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1427,6 +1427,18 @@ static bool get_swap_device_info(struct swap_info_struct *si)
return true;
}
+/*
+ * A swap tier is a swap priority: devices of the same priority are used
+ * round-robin and different priorities are strictly ordered.
+ * SWAP_TIER_MAX means no tier limit.
+ */
+#define SWAP_TIER_MAX SHRT_MAX
+
+static inline short swap_tier(struct swap_info_struct *si)
+{
+ return si->prio;
+}
+
/*
* Fast path try to get swap entries with specified order from current
* CPU's swap entry pool (a cluster).
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v12 2/3] mm: swap: skip swap tiers above the memcg max tier
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 ` Youngjun Park
2026-12-02 8:48 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park
2 siblings, 0 replies; 4+ messages in thread
From: Youngjun Park @ 2026-12-02 8:48 UTC (permalink / raw)
To: Andrew Morton, linux-mm
Cc: Johannes Weiner, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Shakeel Butt, Yosry Ahmed, Lian,
Youngjun Park, linux-kernel
To allow per-cgroup swap device selection, make the allocation paths
skip swap tiers above a per-memcg maximum. The per-CPU cached cluster is
shared by all memcgs, so the fast path is skipped for a memcg with a
limit.
No limit can be set yet, so there is no functional change. The next
patch adds the interface to set it.
Signed-off-by: Youngjun Park <youngjun.park@lge.com>
---
mm/swapfile.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 95928d9f85da..d30ee41f8217 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1439,6 +1439,15 @@ static inline short swap_tier(struct swap_info_struct *si)
return si->prio;
}
+/*
+ * Highest swap tier the memcg of @folio may allocate from. No limit
+ * can be set yet.
+ */
+static inline short swap_memcg_max_tier(struct folio *folio)
+{
+ return SWAP_TIER_MAX;
+}
+
/*
* Fast path try to get swap entries with specified order from current
* CPU's swap entry pool (a cluster).
@@ -1456,7 +1465,12 @@ static bool swap_alloc_fast(struct folio *folio)
*/
si = this_cpu_read(percpu_swap_cluster.si[order]);
offset = this_cpu_read(percpu_swap_cluster.offset[order]);
- if (!si || !offset || !get_swap_device_info(si))
+ if (!si || !offset)
+ return false;
+ /* The per-CPU cluster is shared by all memcgs, so skip it if limited. */
+ if (swap_memcg_max_tier(folio) != SWAP_TIER_MAX)
+ return false;
+ if (!get_swap_device_info(si))
return false;
ci = swap_cluster_lock(si, offset);
@@ -1476,10 +1490,14 @@ static bool swap_alloc_fast(struct folio *folio)
static void swap_alloc_slow(struct folio *folio)
{
struct swap_info_struct *si, *next;
+ short max = swap_memcg_max_tier(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. */
+ if (swap_tier(si) > max)
+ continue;
/* Rotate the device and switch to a new cluster */
plist_requeue(&si->avail_list, &swap_avail_head);
spin_unlock(&swap_avail_lock);
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier
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
2 siblings, 0 replies; 4+ messages in thread
From: Youngjun Park @ 2026-12-02 8:48 UTC (permalink / raw)
To: Andrew Morton, linux-mm
Cc: Johannes Weiner, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
Baoquan He, Barry Song, Shakeel Butt, Yosry Ahmed, Lian,
Youngjun Park, linux-kernel
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
^ permalink raw reply [flat|nested] 4+ messages in thread