mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v12 0/3] mm/swap: swap tier phase 1: priority as tier, per-cgroup via debugfs
@ 2026-12-02  8:48 Youngjun Park
  2026-12-02  8:48 ` [PATCH v12 1/3] mm: swap: introduce the swap tier as a swap priority Youngjun Park
                   ` (2 more replies)
  0 siblings, 3 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

This is v12 of the swap tier series [1], reworked after the v11
discussion [2][3]. In this phase 1, a swap priority is the tier, and a
per-cgroup maximum swap priority is set through debugfs.

Motivation
==========

After the v10 discussion [4][5], I would like to first land the parts
that do not affect users, before anything touches memcg, with two goals
in mind.

- Make per-cgroup swap device selection possible.
- Serve as a bridge that causes no trouble when the tier interface and
  memcg support are introduced later.

Swap priority already gives us a tier-like structure.

- Devices with different priorities are used in priority order.
- Devices with the same priority are used round-robin.

So each distinct priority is one tier: a round-robin group, with
ordering only between tiers. The existing plists are already sorted this
way, so this series adds no tier list or new field. It only filters the
existing allocation walk by priority for a given memory cgroup.

This is phase 1. When tiers are extended later, the same view holds: a
tier is simply named by (or replaces) its priority, and the debugfs
limit becomes the memcg interface.

The swap queue of the per-priority allocation series [6] is such a
per-priority group. I think that concept could become the swap tier, if
the discussion goes that way. Kairui, Chris, Lian, what do you think?
Whichever lands first, I can rebase on the other.

Use case
========

Interactive groups should get the fast tiers, and batch groups should be
kept off them. A top-down priority limit is all this needs.

Per-cgroup swap in debugfs
==========================

Johannes suggested [2] trying swap priority as the tier. Based on that:

- Patch 1 introduces the swap tier as a swap priority.
- Patch 2 skips tiers above a memcg limit in the allocation path.
- Patch 3 sets the limit per cgroup through debugfs.

The effective limit is the lowest one among the cgroup and its
ancestors (parent 50, child 100 gives 50).

  # swapon -p 100 /dev/nvme0n1p2
  # swapon -p 50 /dev/sdb2
  # echo "/batch 50" > /sys/kernel/debug/swap/memcg_tiers

/batch now uses only sdb2. Writing "max" removes the limit. Limits are
keyed by cgroup ID; a removed cgroup is shown as [removed:<id>] until
deleted by ID.

Known limitations
=================

- Fast path: the per-CPU cluster cache is shared by all memcgs and can
  hand out a device above the limit, so limited cgroups skip it. Lian's
  series [6] makes the cluster per device, which removes the need.
- Lookup cost: limits are in a list outside struct mem_cgroup, walked on
  each slow path allocation. Slow only with many entries, and we set
  few. With the memcg interface it becomes a field check.
- Reclaim: mem_cgroup_get_nr_swap_pages() ignores the limit. To be
  handled with the memcg interface.

Future direction
================

- Add the memcg interface after virtual swap lands, as
  memory.swap.prio.max or memory.swap.tiers.max, whichever the
  discussion prefers.
- Explicit tiers (naming, e.g. /sys/kernel/mm/swap/tiers), only when a
  concrete need appears.
- Per-tier allocation policy, as a separate extension that needs its
  own discussion.

Testing
=======

QEMU (KASAN enabled) with two swap devices (priority 100 and 50) and a
memcg running a 150M anonymous memory hog.

- No limit / limit 100: swaps to the priority 100 device.
- Limit 50: swaps only to the priority 50 device.
- Limit below all devices: no swap is used and the cgroup OOMs.
- Parent and child limits: the lowest one applies.
- Invalid input is rejected; "max" removes the limit.
- Removed cgroup shown as [removed:<id>], recreated one starts without a
  limit, stale entry deleted by ID.

No WARNING/BUG/KASAN reports. checkpatch.pl --strict is clean.

Tool-generated content
======================

Following Documentation/process/generated-content.rst: I used GitHub
Copilot CLI (Claude Sonnet 5.5) to help write the code, the commit logs
and this cover letter, and to run the QEMU tests. The design is mine,
and I reviewed and tested the result.

Change log
==========

v12
- Use a swap priority as the tier; dropped the tier list and avail list
  walk.
- Replaced the debugfs tier mask with a per-cgroup max swap priority
  [2][3]; removed the tiers file.
- Split into three patches.

Changes up to v11 are in the v11 cover letter [1].

[1] https://lore.kernel.org/linux-mm/20260916183437.2946306-1-youngjun.park@lge.com/
[2] https://lore.kernel.org/linux-mm/20260916200434.GA5784@cmpxchg.org/
[3] https://lore.kernel.org/linux-mm/arfnkNBANl0fwrKb@gmail.com/
[4] https://lore.kernel.org/linux-mm/amDCIl51NoNPL7Op@cmpxchg.org/
[5] https://lore.kernel.org/linux-mm/amIlqHQ40baRoz3O@cmpxchg.org/
[6] https://lore.kernel.org/linux-mm/cover.1795439990.git.lianux.mm@gmail.com/

Youngjun Park (3):
  mm: swap: introduce the swap tier as a swap priority
  mm: swap: skip swap tiers above the memcg max tier
  mm: swap: add debugfs to set memcg max swap tier

 mm/swapfile.c | 239 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 238 insertions(+), 1 deletion(-)


base-commit: 1a7610248a3802e38124ce66b07eef12277be9a0
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [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

end of thread, other threads:[~2026-12-02  8:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park

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®