Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Baoquan He" <baoquan.he@linux.dev>, linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, corbet@lwn.net,
	chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com,
	hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com,
	baohua@kernel.org, youngjun.park@lge.com, kunwu.chan@gmail.com,
	klarasmodin@gmail.com, "Baoquan He" <hebaoquan@kylinos.cn>
Subject: Re: [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap
Date: Fri, 04 Dec 2026 16:48:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-250231@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-14-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] mm, swap: no_cache disables SSD per-CPU caching causing O(N^2)
  cluster scans
- [Medium] mm, swap: no_cache guard still overwrites percpu_swap_cluster
  on stale offset
- [Medium] mm, swap: no_cache list order fragments HDDs and skips
  nonfull_clusters
- [Medium] mm: folio_alloc_swap() forces redundant global
  css_rstat_flush() per folio
- [Medium] mm: failed order-0 folio_alloc_swap() contends on swap_lock
  and swap_avail_lock
- [Low] mm, swap: commit message omits no_cache caching and
  frag_clusters changes
--

commit 177a3ab0dbe1d28a173ce9251f7f626766cd240d
Author: Baoquan He <hebaoquan@kylinos.cn>

> mm, swap: do not give an xswap slot to a cgroup that cannot zswap
>
> At this point, an xswap slot holds only a zswap-backed page. It has no
> storage of its own. If the folio's cgroup cannot take another zswap
> page, giving it an xswap slot is wasted: zswap refuses the page and the
> slot cannot hold it.
>
> Skip xswap devices when the cgroup cannot zswap, and try a physical swap
> device instead.
>
> The check is obj_cgroup_may_zswap(): zswap_store() uses the same check
> but first tries to shrink the memcg, while the allocation-time check does
> not, so it is stricter.
>
> Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>

[Severity: Low]
The commit message only describes skipping xswap devices via
obj_cgroup_may_zswap(), omitting the no_cache plumbing, the
percpu_swap_cluster bypass on SSDs, and the frag_clusters-first policy in
cluster_alloc_swap_entry().  Could the changelog explain these allocator
changes and the rationale behind them?

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index b2d24e1a24f08..6c1cfa0d693b5 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -1240,6 +1242,10 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
>  	relocate_cluster(si, ci);
>  	swap_cluster_unlock(ci);
>  	if (si->flags & SWP_SOLIDSTATE) {
> +		if (no_cache &&
> +		    (this_cpu_read(percpu_swap_cluster.si[order]) != si ||
> +		     !this_cpu_read(percpu_swap_cluster.offset[order])))
> +			return found;
>  		this_cpu_write(percpu_swap_cluster.offset[order], next);
>  		this_cpu_write(percpu_swap_cluster.si[order], si);
>  	} else {

[Severity: Medium]
Can this no_cache guard still overwrite percpu_swap_cluster when si[order]
already matches si and offset[order] is non-zero?

If this CPU previously cached physical device si and that cluster later
became unusable, swap_alloc_fast() sees !cluster_is_usable(ci, order) and
returns false without clearing offset[order].  The following
swap_alloc_slow() with no_cache set then passes this check and caches a
fresh cluster on si, so subsequent may_zswap folios on this CPU bypass
higher-priority xswap via swap_alloc_fast().

Also, swap_cluster_populate() drops percpu_swap_cluster.lock across a
sleeping table allocation, so CPU migration can land on another CPU that
already caches si with a non-zero offset and clobber its cached offset.
Returning early whenever no_cache is true avoids both cases.

[ ... ]

> @@ -1376,20 +1385,33 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
>  	}
>  
>  new_cluster:
> +	/*
> +	 * A no_cache allocation cannot keep a per-CPU cluster, so pack it
> +	 * into a cluster already in use and leave the free ones to
> +	 * allocations that can use a whole one.
> +	 */
> +	if (no_cache && order < PMD_ORDER) {
> +		found = alloc_swap_scan_list(si, &si->frag_clusters[order],
> +					     folio, false, no_cache);
> +		if (found)
> +			goto done;
> +	}
> +
>  	if (swap_prefer_free_cluster(si)) {
> -		found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
> +		found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
>  		if (found)
>  			goto done;
>  	}
>  
>  	if (order < PMD_ORDER) {
> -		found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, true);
> +		found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
> +					     folio, true, no_cache);
>  		if (found)
>  			goto done;
>  	}

[Severity: Medium]
Does this no_cache branch fragment rotational swap devices by scanning
frag_clusters[order] ahead of free_clusters?  For non-SWP_SOLIDSTATE
devices, alloc_swap_scan_cluster() ignores no_cache and still caches next
in si->global_cluster->next[order], pinning a fragmented cluster for HDDs.

Also, when frag_clusters[order] is empty and swap_prefer_free_cluster(si)
is true, this takes a fresh cluster from free_clusters before checking
nonfull_clusters[order].  Checking SWP_SOLIDSTATE here and scanning
nonfull_clusters before free_clusters under no_cache would fix both.

[ ... ]

> @@ -1657,18 +1688,24 @@ static bool swap_alloc_fast(struct folio *folio)
>  /* Rotate the device and switch to a new cluster */
> -static void swap_alloc_slow(struct folio *folio)
> +static void swap_alloc_slow(struct folio *folio, bool may_zswap)
>  {
>  	struct swap_info_struct *si, *next;
>  
>  	spin_lock(&swap_avail_lock);
>  start_over:
>  	plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
> +		/*
> +		 * Do not rotate a skipped device: the walk follows the
> +		 * rotation and would spin forever.
> +		 */
> +		if (!may_zswap && (si->flags & SWP_XSWAP))
> +			continue;
>  		/* Rotate the device and switch to a new cluster */
>  		plist_requeue(&si->avail_list, &swap_avail_head);
>  		spin_unlock(&swap_avail_lock);
>  		if (get_swap_device_info(si)) {
> -			cluster_alloc_swap_entry(si, folio);
> +			cluster_alloc_swap_entry(si, folio, !may_zswap);
>  			put_swap_device(si);
>  			if (folio_test_swapcache(folio))
>  				return;

[Severity: High]
Does passing no_cache = !may_zswap unconditionally cause a severe swapout
throughput regression on physical SSD swap devices for cgroups that cannot
zswap?

Once percpu_swap_cluster is empty, points to xswap, or reaches offset 0 on
a physical SSD, swap_alloc_fast() misses and alloc_swap_scan_cluster()
discards next under no_cache, permanently trapping later !may_zswap folios
on the slow path even when the physical SSD has higher priority than xswap.

Every folio then takes global swap_avail_lock twice (in
real_swap_has_room() and swap_alloc_slow()), takes si->lock twice (in
isolate_lock_cluster() and relocate_cluster()), and rescans
frag_clusters[order] from slot 0 in alloc_swap_scan_list().

Filling a 512-slot cluster degrades to O(SWAPFILE_CLUSTER^2) slot checks
under ci->lock, and tail insertion in move_cluster() rotates consecutive
allocations across different fragmented clusters, breaking sequential
swapout BIO merging.

[ ... ]

> @@ -2002,10 +2067,21 @@ int folio_alloc_swap(struct folio *folio)
>  {
>  	unsigned int order = folio_order(folio);
>  	unsigned int size = 1 << order;
> +	bool may_zswap = true;
> +	bool zswap_checked = false;
>  
>  	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
>  	VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
>  
> +	/*
> +	 * A folio zswap will not take must not get an xswap slot; without
> +	 * a device to route away from, skip the lookup.
> +	 */
> +	if (atomic_read(&nr_xswap_files)) {
> +		may_zswap = folio_may_zswap(folio);
> +		zswap_checked = true;
> +	}

[Severity: Medium]
Does calling folio_may_zswap() on every folio here double forced cross-CPU
rstat flushes during swapout?

When an xswap device is active and an ancestor memcg has a finite non-zero
memory.zswap.max, obj_cgroup_may_zswap() forces css_rstat_flush(), taking
the global memory cgroup rstat lock with IRQs disabled across all possible
CPUs.

Since zswap_store() still calls obj_cgroup_may_zswap() in swap_writeout(),
every swapped folio triggers two forced global rstat flushes, plus extra
flushes on zero-filled folios, large-folio split retries, and failed swap
allocations.

[ ... ]

> @@ -2025,16 +2101,36 @@ int folio_alloc_swap(struct folio *folio)
>  again:
> -	local_lock(&percpu_swap_cluster.lock);
> -	if (!swap_alloc_fast(folio))
> -		swap_alloc_slow(folio);
> -	local_unlock(&percpu_swap_cluster.lock);
> +	/*
> +	 * A folio that cannot zswap can only use a real device; skip the
> +	 * scan when none has room.  The failure is still reported below.
> +	 */
> +	if (may_zswap || real_swap_has_room()) {
> +		local_lock(&percpu_swap_cluster.lock);
> +		if (!swap_alloc_fast(folio, may_zswap))
> +			swap_alloc_slow(folio, may_zswap);
> +		local_unlock(&percpu_swap_cluster.lock);
> +	}
>  
>  	if (!order && unlikely(!folio_test_swapcache(folio))) {
>  		if (swap_sync_discard())
>  			goto again;
>  	}

[ ... ]

> @@ -2052,6 +2148,13 @@ int folio_alloc_swap(struct folio *folio)
>  	if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
>  		return -ENOMEM;
>  
> +	/*
> +	 * The folio can only use a real device and none has room; splitting
> +	 * cannot help.
> +	 */
> +	if (!may_zswap && !real_swap_has_room())
> +		return -ENOMEM;
> +
>  	return order ? -E2BIG : -ENOMEM;
>  }

[Severity: Medium]
Can failed order-0 allocations for !may_zswap cgroups contend heavily on
swap_lock and swap_avail_lock when no real swap device has room?

Because get_nr_swap_pages() still counts free xswap space, vmscan keeps
calling folio_alloc_swap() on every anonymous folio in a cgroup that cannot
zswap even on an xswap-only host.

Each failed order-0 call takes global swap_avail_lock in
real_swap_has_room() without checking nr_real_swapfiles first, then
unconditionally runs swap_sync_discard() under global swap_lock even though
xswap never sets SWP_PAGE_DISCARD.

At the failed label, real_swap_has_room() takes swap_avail_lock a second
time even when order is 0, where the next line already returns -ENOMEM.
Gating this check on order and fast-pathing nr_real_swapfiles == 0 for both
real_swap_has_room() and swap_sync_discard() would avoid the contention.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=13


  reply	other threads:[~2026-12-04 16:49 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-12-03 16:43 ` [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 02/14] mm: xswap support for zswap Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
2026-12-03 16:43 ` [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 08/14] mm, swap: require zswap for xswap devices Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 09/14] mm, swap: let the command line set the xswap device size Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
2026-12-03 16:43 ` [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 12/14] mm, swap: add debugfs counters for xswap Baoquan He
2026-12-04 16:48   ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-12-04 16:48   ` sashiko-bot [this message]
2026-12-03 16:43 ` [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
2026-12-04 16:48   ` sashiko-bot

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-250231@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=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hebaoquan@kylinos.cn \
    --cc=kasong@tencent.com \
    --cc=klarasmodin@gmail.com \
    --cc=kunwu.chan@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=urezki@gmail.com \
    --cc=yosry@kernel.org \
    --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