Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Youngjun Park" <youngjun.park@lge.com>,
	"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>
Subject: Re: [PATCH v12 3/3] mm: swap: add debugfs to set memcg max swap tier
Date: Thu, 03 Dec 2026 08:48:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-247385@kernel.org> (raw)
In-Reply-To: <20261202084826.2131931-4-youngjun.park@lge.com>

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


      reply	other threads:[~2026-12-03  8:48 UTC|newest]

Thread overview: 6+ 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-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 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=sashiko-outbox-247385@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=yosry.ahmed@linux.dev \
    --cc=youngjun.park@lge.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