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 11/14] mm, swap: add xswap grow trigger on cluster allocation
Date: Fri, 04 Dec 2026 16:48:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-250229@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-12-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] mm: xswap_grow() requests multiple chunks when force is true
- [Medium] mm: xswap forced grow fails prematurely on concurrent bursts
- [Medium] mm: proactive xswap_grow() stalls concurrent allocators on
  xswap_lock
- [Medium] mm: cluster_alloc_swap_entry() rescans lists even when xswap
  did not grow
- [Medium] mm: cluster_alloc_swap_entry() grows xswap when free_clusters
  is non-empty
--

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

mm, swap: add xswap grow trigger on cluster allocation

This patch adds proactive and fallback growth of the mapped xswap
cluster_info range in cluster_alloc_swap_entry(), growing toward 65%
usage in bounded chunks once mapped usage reaches 85% and retrying after
a forced grow when no cluster is found.

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 074c5114d129a..4d6a46346b935 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -1325,6 +1345,13 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
>  	if (order && !(si->flags & SWP_BLKDEV))
>  		return 0;
>  
> +#ifdef CONFIG_XSWAP
> +	/* Grow before allocating: mapping pages can sleep. */
> +	if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) &&
> +	    xswap_should_grow(si))
> +		xswap_grow(si, false);
> +#endif

[Severity: Medium]
Does this stall concurrent allocators on si->xswap_lock once usage
reaches 85%?

Since si->nr_clusters_mapped is only updated at the end of
xswap_publish_clusters(), other CPUs entering cluster_alloc_swap_entry()
while a grow sleeps in vm_area_grow_pages() also call xswap_grow() and
block on mutex_lock(&si->xswap_lock) in xswap_map_clusters().

Those waiters then wake up serially just to fail the stale start_idx
check with -EAGAIN, instead of allocating from the remaining 15% free
clusters.  Using mutex_trylock(&si->xswap_lock) for non-forced grows
would let them proceed while one grow is already in flight.

[ ... ]

> @@ -1397,6 +1424,22 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
>  		if (found)
>  			goto done;
>  	}
> +
> +#ifdef CONFIG_XSWAP
> +	/*
> +	 * A burst of allocators can hold every mapped cluster while usage is
> +	 * still below the growth threshold.  Grow and rescan all lists: a
> +	 * cluster published by another grower may already be on a frag list.
> +	 */
> +	if (!found && (si->flags & SWP_XSWAP) &&
> +	    (READ_ONCE(si->flags) & SWP_WRITEOK) && !retried) {
> +		retried = true;
> +		if (READ_ONCE(si->nr_clusters_mapped) <
> +		    READ_ONCE(si->nr_clusters_max))
> +			xswap_grow(si, true);
> +		goto new_cluster;
> +	}
> +#endif

[Severity: Medium]
Can a concurrent burst still fail allocation here while unmapped clusters
remain below si->nr_clusters_max?

Because xswap_grow() collapses -EAGAIN from xswap_map_clusters() to 0
and retried caps the fallback to a single attempt, excess threads in a
burst (more than XSWAP_GROW_CLUSTERS) or a thread racing with an
in-flight grow can lose the newly published clusters and return 0:

CPU0                              CPU1
cluster_alloc_swap_entry()        cluster_alloc_swap_entry()
  !found, retried = true            !found, retried = true
  xswap_grow(si, true)              xswap_grow(si, true)
    mapped = N                        mapped = N
    xswap_map_clusters(N, ...)
      mutex_lock(&si->xswap_lock)     mutex_lock(&si->xswap_lock)
      publish N + chunk
      mutex_unlock(&si->xswap_lock)
                                      start_idx != nr_clusters_mapped
                                      return -EAGAIN (returned as 0)
  goto new_cluster
    isolate new clusters
                                    goto new_cluster
                                      lists empty, retried == true
                                      return 0  <-- premature -ENOMEM

Once another thread isolates the new clusters (which stay off-list while
swap_cluster_populate() can sleep), CPU1 cannot retry a forced grow and
folio_alloc_swap() fails with -ENOMEM despite unmapped clusters
remaining.

[Severity: Medium]
Should goto new_cluster be skipped when si->nr_clusters_mapped did not
advance?

When nr_clusters_mapped has already reached nr_clusters_max, or when
xswap_grow(si, true) fails with -ENOMEM, this still jumps back to
new_cluster and repeats the entire scan across all cluster lists and
swap_reclaim_full_clusters() on every failed allocation.

Snapshotting nr_clusters_mapped before new_cluster and retrying only if
xswap_grow() succeeded or nr_clusters_mapped increased would avoid the
redundant pass while still catching clusters published by another grower.

[Severity: Medium]
Can this grow xswap even when si->free_clusters is not empty?

If swap_cluster_populate() fails (for example with -ENOMEM in
swap_cluster_alloc_table() or -EAGAIN), it returns the cluster to
si->free_clusters and returns NULL, which makes alloc_swap_scan_list()
break out and return 0 while free clusters remain on the list.

When nonfull and frag lists have no slots, cluster_alloc_swap_entry()
then calls xswap_grow(si, true) without checking list_empty() on
si->free_clusters, mapping additional permanent vmalloc pages for
cluster_info even though free clusters are already available.

[ ... ]

> +static unsigned long xswap_grow(struct swap_info_struct *si, bool force)
> +{
> +	unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
> +	unsigned long nr_new, want;
> +	int ret;
> +
> +	lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
> +
> +	want = DIV_ROUND_UP(swap_usage_in_pages(si) * 100,
> +			    XSWAP_GROW_UNTIL * SWAPFILE_CLUSTER);
> +	if (want <= mapped) {
> +		if (!force)
> +			return 0;
> +		nr_new = XSWAP_GROW_CLUSTERS;
> +	} else {
> +		nr_new = rounddown(want - mapped, XSWAP_GROW_CLUSTERS);
> +	}
> +	if (!nr_new)
> +		nr_new = XSWAP_GROW_CLUSTERS;
> +	if (nr_new > XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS)
> +		nr_new = XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS;

[Severity: Medium]
Should xswap_grow() cap nr_new to a single XSWAP_GROW_CLUSTERS chunk when
force is true?

Here force is only checked in the want <= mapped branch.  Whenever usage
exceeds 65% (XSWAP_GROW_UNTIL), want > mapped holds and a forced grow
takes the else branch, requesting up to XSWAP_GROW_CHUNKS (4) chunks
instead of the single chunk described in the commit message.

Because vm_area_grow_pages() is all-or-nothing under
memalloc_noreclaim_save() and __GFP_NOMEMALLOC, if the proactive 4-chunk
grow fails with -ENOMEM under memory pressure, the fallback
xswap_grow(si, true) repeats the exact same 4-chunk request and starves
swap allocation even when a single page is available.

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


  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 [this message]
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
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-250229@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