Linux-mm Archive on lore.kernel.org
 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; 6+ 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] 6+ 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; 6+ 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 related	[flat|nested] 6+ 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-03  8:48   ` sashiko-bot
  2026-12-02  8:48 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park
  2 siblings, 1 reply; 6+ 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 related	[flat|nested] 6+ 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
  2026-12-03  8:48   ` sashiko-bot
  2 siblings, 1 reply; 6+ 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 related	[flat|nested] 6+ messages in thread

* Re: [PATCH v12 2/3] mm: swap: skip swap tiers above the memcg max tier
  2026-12-02  8:48 ` [PATCH v12 2/3] mm: swap: skip swap tiers above the memcg max tier Youngjun Park
@ 2026-12-03  8:48   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03  8:48 UTC (permalink / raw)
  To: Youngjun Park, Andrew Morton, linux-mm
  Cc: Johannes Weiner, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
	Baoquan He, Barry Song, Shakeel Butt, Yosry Ahmed, Lian

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm: swap: broken percpu_swap_cluster handling in
  swap_alloc_fast()
- [Medium] mm: swap: unfiltered discard and -E2BIG return in
  folio_alloc_swap()
--

commit c316a03b87811115cdd04e1144bda00848de29fa
Author: Youngjun Park <youngjun.park@lge.com>

mm: swap: skip swap tiers above the memcg max tier

This commit introduces a swap_memcg_max_tier() helper and skips the
per-CPU cluster fast path for tier-limited memcgs while filtering out
swap devices above the memcg's maximum tier in swap_alloc_slow().

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 95928d9f85da0..d30ee41f82176 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;

[Severity: High]
Does skipping swap_alloc_fast() whenever swap_memcg_max_tier(folio) !=
SWAP_TIER_MAX cause every allocation on a SWP_SOLIDSTATE device to isolate
a new cluster from si->free_clusters while still installing the lower-tier
cluster into percpu_swap_cluster?

When swap_alloc_fast() returns false, swap_alloc_slow() calls
cluster_alloc_swap_entry(si, folio).  On SWP_SOLIDSTATE devices,
cluster_alloc_swap_entry() does not use si->global_cluster; it jumps to
new_cluster: and pulls a fresh cluster from si->free_clusters via
alloc_swap_scan_list() -> alloc_swap_scan_cluster():

mm/swapfile.c:alloc_swap_scan_cluster() {
	...
out:
	relocate_cluster(si, ci);
	swap_cluster_unlock(ci);
	if (si->flags & SWP_SOLIDSTATE) {
		this_cpu_write(percpu_swap_cluster.offset[order], next);
		this_cpu_write(percpu_swap_cluster.si[order], si);
	} else {
		si->global_cluster->next[order] = next;
	}
	return found;
}

After allocating a single folio, relocate_cluster() moves the cluster with
its remaining free slots onto si->frag_clusters[order] and caches the rest
of the cluster in percpu_swap_cluster.  Because a limited memcg never uses
swap_alloc_fast(), its next allocation isolates yet another cluster from
si->free_clusters instead of consuming the remaining 511 slots, draining
si->free_clusters after 1/512th of the device is used and starving large
folio allocations.  At the same time, an unlimited memcg running on the
same CPU passes the check in swap_alloc_fast() and allocates from the
lower-tier device left in percpu_swap_cluster.

>  
>  	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);

[Severity: Medium]
When all swap devices with swap_tier(si) <= max are full while higher-tier
devices still have free space or pending discards, how does
folio_alloc_swap() handle the failed allocation?

mm/swapfile.c:folio_alloc_swap() {
	...
	if (!order && unlikely(!folio_test_swapcache(folio))) {
		if (swap_sync_discard())
			goto again;
	}
	...
failed:
	if (get_nr_swap_pages() <= 0)
		return -ENOSPC;
	if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
		return -ENOMEM;

	return order ? -E2BIG : -ENOMEM;
}

For a large folio (order > 0), get_nr_swap_pages() is still positive
because the higher-tier devices have free pages, so folio_alloc_swap()
returns -E2BIG instead of -ENOMEM and causes shrink_folio_list() and
shmem_writeout() to split the folio and retry order-0 allocations that
also cannot use the higher tier.  For order-0 folios, swap_sync_discard()
walks swap_active_head without checking swap_tier(si) <= max, so it
synchronously discards clusters on disallowed higher-tier devices and
loops back to again: even though swap_alloc_slow() will skip those
devices.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202084826.2131931-1-youngjun.park@lge.com?part=2


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

* Re: [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier
  2026-12-02  8:48 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park
@ 2026-12-03  8:48   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03  8:48 UTC (permalink / raw)
  To: Youngjun Park, Andrew Morton, linux-mm
  Cc: Johannes Weiner, Chris Li, Kairui Song, Kemeng Shi, Nhat Pham,
	Baoquan He, Barry Song, Shakeel Butt, Yosry Ahmed, Lian

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] mm: swap: tier limits break percpu_swap_cluster and exhaust
  free clusters
- [Medium] mm: swap: cgroup_get_e_css() resolves disabled memcg to
  ancestor
- [Medium] mm: swap: folio_alloc_swap() ignores memcg tier limit on
  fallback paths
- [Low] mm: swap: SWAP_TIER_MAX sentinel collides with valid priority
  32767
--

commit 2d5665a5aba7f63f5075e6bf592fe86a3b0bdc09
Author: Youngjun Park <youngjun.park@lge.com>

mm: swap: add debugfs to set memcg max swap tier

This commit adds /sys/kernel/debug/swap/memcg_tiers to configure a per-memcg
maximum swap tier (priority) and enforces the effective hierarchical limit in
swap_memcg_max_tier().

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d30ee41f82176..6acf177cb2dfc 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1439,14 +1441,219 @@ static inline short swap_tier(struct swap_info_struct *si)
[ ... ]
> +/* 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;
> +}

[Severity: High]
When swap_memcg_max_tier() returns a limit below SWAP_TIER_MAX, how does
swap allocation interact with percpu_swap_cluster on SWP_SOLIDSTATE
devices?

mm/swapfile.c:swap_alloc_fast() {
    ...
	/* 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;
    ...
}

Every swap allocation from a limited memcg skips swap_alloc_fast() even if
percpu_swap_cluster.si[order] already satisfies the tier limit, falling back
to swap_alloc_slow() -> cluster_alloc_swap_entry().  On SWP_SOLIDSTATE
devices, cluster_alloc_swap_entry() isolates a fresh free cluster from
si->free_clusters, allocates one folio, moves the cluster (with 511 free
slots remaining) to si->frag_clusters[ci->order] in relocate_cluster(), and
stores the next offset in percpu_swap_cluster.  Because the next allocation
from the limited memcg skips swap_alloc_fast() again, it abandons that
cluster and isolates another free cluster, draining all free clusters on the
device at 1 folio per cluster (0.2% usage for order-0 pages) and causing
large-folio swap allocations to fail with -E2BIG.

In addition, alloc_swap_scan_cluster() overwrites
percpu_swap_cluster.si[order] with the lower-tier device, so subsequent
allocations on the same CPU from unlimited memcgs pass the check in
swap_alloc_fast() and allocate from the lower-tier device instead of the
highest-priority tier.

> +
> +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);
> +}
[ ... ]
> +/* 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;

[Severity: Low]
Does using SWAP_TIER_MAX (SHRT_MAX, 32767) as the removal sentinel in
memcg_prio_set() collide with valid swap priority 32767?  Because swapon(2)
accepts priorities up to SWAP_FLAG_PRIO_MASK (32767) and memcg_prio_parse()
accepts "32767" via kstrtos16(), writing "<cgroup> 32767" removes the limit
(or fails with -ENOENT) instead of setting the maximum tier to 32767.

[ ... ]

> +/*
> + * "<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)
> +{
[ ... ]
> +	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);

[Severity: Medium]
What happens here when the memory controller is not enabled on the target
cgroup?  cgroup_get_e_css() returns the effective CSS of the nearest
ancestor with memory enabled (falling back to the root memcg), so id becomes
the ancestor's cgroup ID and memcg_prio_set() silently modifies or removes
the ancestor's tier limit.  Similarly, memcg_prio_show() uses
mem_cgroup_get_from_id(r->id), which also calls cgroup_get_e_css() and will
print the ancestor's path if memory is disabled on that cgroup.  Checking
css->cgroup == cgrp (or mem_cgroup_id(memcg) == r->id) would avoid operating
on an ancestor.

[ ... ]

> @@ -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;

[Severity: Medium]
When all swap devices at or below a memcg's max tier are full (or none exist)
while a higher-tier device still has free space, does folio_alloc_swap()
handle the failure paths in a tier-aware way?

mm/swapfile.c:folio_alloc_swap() {
    ...
	if (!order && unlikely(!folio_test_swapcache(folio))) {
		if (swap_sync_discard())
			goto again;
	}
    ...
failed:
	if (get_nr_swap_pages() <= 0)
		return -ENOSPC;
	if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
		return -ENOMEM;

	return order ? -E2BIG : -ENOMEM;
}

For large folios (order > 0), get_nr_swap_pages() is still positive because
of the higher-tier device, so folio_alloc_swap() returns -E2BIG and causes
shrink_folio_list() to split every large folio in the limited memcg even
though order-0 swap allocation cannot use the higher tier either.  For
order-0 folios, swap_sync_discard() discards pending clusters on any device
in swap_active_head without checking the folio's tier limit, looping back to
again until all pending discards on disallowed higher-tier devices are
drained.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202084826.2131931-1-youngjun.park@lge.com?part=3


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

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

Thread overview: 6+ 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-03  8:48   ` sashiko-bot
2026-12-02  8:48 ` [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier Youngjun Park
2026-12-03  8:48   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox