All of 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 2/3] mm: swap: skip swap tiers above the memcg max tier
Date: Thu, 03 Dec 2026 08:48:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-247384@kernel.org> (raw)
In-Reply-To: <20261202084826.2131931-3-youngjun.park@lge.com>

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


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

Thread overview: 8+ 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-04  7:28   ` Lian Wang
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 [this message]
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
2026-12-04  7:19 ` [PATCH v12 0/3] mm/swap: swap tier phase 1: priority as tier, per-cgroup via debugfs Lian Wang

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-247384@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.