All of 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, chrisl@kernel.org,
	kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org,
	corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org,
	youngjun.park@lge.com, shikemeng@huaweicloud.com,
	yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com,
	klarasmodin@gmail.com, "Baoquan He" <hebaoquan@kylinos.cn>
Subject: Re: [PATCH v7 11/14] mm, swap: add xswap grow trigger on cluster allocation
Date: Mon, 30 Nov 2026 15:09:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-241470@kernel.org> (raw)
In-Reply-To: <20261129150755.191662-12-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: missing SWP_WRITEOK check in cluster_alloc_swap_entry()
  fallback grow
- [Medium] mm: premature xswap allocation failure on concurrent
  xswap_grow() bursts
- [Medium] mm: swap: xswap_grow() 4-chunk cap defeats XSWAP_GROW_UNTIL
  target
--

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

mm, swap: add xswap grow trigger on cluster allocation

This commit adds proactive and fallback grow triggers in
cluster_alloc_swap_entry() to map additional xswap cluster_info pages and
publish new clusters onto free_clusters as swap usage rises.

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 074c5114d129a..9dc6bdc1a6dec 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -68,6 +68,21 @@
>  #define XSWAP_GROW_CLUSTERS \
>  	max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
>  
> +/*
> + * Grow thresholds, as a percentage of the mapped range in use.  The target
> + * sits below the next grow trigger, so a grow that follows a rise in usage
> + * does not immediately trigger another one.
> + */
> +#define XSWAP_GROW_UNTIL	65
> +#define XSWAP_GROW_WHEN		85
> +#define XSWAP_GROW_CHUNKS	4

[ ... ]

> @@ -1325,6 +1340,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

[ ... ]

> @@ -1397,6 +1419,20 @@ 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, so grow before retrying.
> +	 */
> +	if (!found && (si->flags & SWP_XSWAP)) {
> +		if (READ_ONCE(si->nr_clusters_mapped) <
> +		    READ_ONCE(si->nr_clusters_max))
> +			xswap_grow(si, true);
> +		found = alloc_swap_scan_list(si, &si->free_clusters,
> +					     folio, false);
> +	}
> +#endif

[Severity: Medium]
Should this fallback grow also check si->flags & SWP_WRITEOK, and should
xswap_publish_clusters() recheck SWP_WRITEOK under si->lock?

Once swap_info_remove() clears SWP_WRITEOK during swapoff, every list scan
in cluster_alloc_swap_entry() fails because isolate_lock_cluster() bails
out when SWP_WRITEOK is clear.  An in-flight order-0 allocator reaching
this fallback then mistakes !found for cluster exhaustion and calls
xswap_grow(si, true), sleeping to allocate and map new cluster_vm pages,
linking new clusters onto si->free_clusters, and advancing
si->nr_clusters_mapped on a device being torn down.

>  done:
>  	if (!(si->flags & SWP_SOLIDSTATE))
>  		spin_unlock(&si->global_cluster_lock);
> @@ -3986,8 +4022,39 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
>  }
>  
>  #ifdef CONFIG_XSWAP
> +/*
> + * Link the new clusters and publish the larger range in one si->lock
> + * section: linking first makes a cluster visible to an allocator only
> + * after it is on the free list.  @link_free is false for the first
> + * chunk, whose free list is not initialized yet.
> + */
> +static void xswap_publish_clusters(struct swap_info_struct *si,
> +				   unsigned long start_idx, unsigned long nr,
> +				   bool link_free)
> +{
> +	unsigned long i;
> +
> +	spin_lock(&si->lock);
> +	if (link_free) {
> +		for (i = start_idx; i < start_idx + nr; i++) {
> +			struct swap_cluster_info *ci = &si->cluster_info[i];
> +
> +			/* A concurrent grower cannot take these: xswap_lock. */
> +			if (ci->flags != CLUSTER_FLAG_NONE)
> +				continue;
> +			list_add_tail(&ci->list, &si->free_clusters);
> +			WRITE_ONCE(ci->flags, CLUSTER_FLAG_FREE);
> +			ci->order = 0;
> +		}
> +	}
> +	/* Pairs with the smp_load_acquire() in the readers of the range. */
> +	smp_store_release(&si->nr_clusters_mapped, start_idx + nr);
> +	spin_unlock(&si->lock);
> +}
> +
>  static int xswap_map_clusters(struct swap_info_struct *si,
> -			      unsigned long start_idx, unsigned long nr)
> +			      unsigned long start_idx, unsigned long nr,
> +			      bool link_free)
>  {
>  	/* vm_area_grow_pages() takes an offset into the area, not an address. */
>  	unsigned long vm_end = PAGE_ALIGN((size_t)(start_idx + nr) *

[ ... ]

> @@ -4023,14 +4090,65 @@ static int xswap_map_clusters(struct swap_info_struct *si,
>  	for (i = start_idx; i < start_idx + nr; i++)
>  		spin_lock_init(&si->cluster_info[i].lock);
>  
> -	/*
> -	 * Publish the new mappings and cluster lock initialization before
> -	 * the count; walkers without xswap_lock use smp_load_acquire().
> -	 */
> -	smp_store_release(&si->nr_clusters_mapped, start_idx + nr);
> +	/* The mappings and the locks are in place; make them visible. */
> +	xswap_publish_clusters(si, start_idx, nr, link_free);
>  	mutex_unlock(&si->xswap_lock);
>  	return 0;
>  }
> +static bool xswap_should_grow(struct swap_info_struct *si)
> +{
> +	unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
> +
> +	if (mapped >= READ_ONCE(si->nr_clusters_max))
> +		return false;
> +
> +	return swap_usage_in_pages(si) * 100 >=
> +	       mapped * SWAPFILE_CLUSTER * XSWAP_GROW_WHEN;
> +}
> +
> +/*
> + * Map more of the cluster_info array, up to GROW_UNTIL in use.  The caller
> + * holds percpu_swap_cluster.lock, which this drops while it sleeps.
> + */
> +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]
Does clamping nr_new to XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS defeat the
XSWAP_GROW_UNTIL (65%) hysteresis target once si->nr_clusters_mapped
reaches 13 chunks?

Bringing usage from 85% down to 65% requires growing by at least 4/13 of
mapped.  Once mapped exceeds 13 * XSWAP_GROW_CLUSTERS (~2.16 GiB with 48
byte swap_cluster_info), want - mapped is always larger than 4 chunks, so
xswap_grow() maps at most 4 chunks per call, keeping usage near 85% and
causing back-to-back synchronous xswap_grow() calls on subsequent
cluster_alloc_swap_entry() invocations.

> +	nr_new = min(nr_new, READ_ONCE(si->nr_clusters_max) - mapped);
> +	if (!nr_new)
> +		return 0;
> +
> +	/*
> +	 * Mapping pages can sleep.  The lock only guards the per-cpu
> +	 * cluster cache, which this path does not touch.
> +	 */
> +	local_unlock(&percpu_swap_cluster.lock);
> +	ret = xswap_map_clusters(si, mapped, nr_new, true);
> +	local_lock(&percpu_swap_cluster.lock);
> +	if (ret)
> +		return 0;
> +
> +	atomic_long_inc(&si->xswap_grows);
> +	return nr_new;
> +}

[Severity: Medium]
Can a burst of concurrent allocators still fail in
cluster_alloc_swap_entry() while unmapped clusters and free slots in
si->frag_clusters[0] remain?

CPU0                                  CPU1
cluster_alloc_swap_entry()
  xswap_grow(si, true)
    mapped = si->nr_clusters_mapped
    xswap_map_clusters(si, mapped, ...)
      mutex_lock(&si->xswap_lock)
      vm_area_grow_pages()
                                      cluster_alloc_swap_entry()
                                        xswap_grow(si, true)
                                          mapped = si->nr_clusters_mapped
                                          xswap_map_clusters(si, mapped, ...)
                                            mutex_lock(&si->xswap_lock)
      xswap_publish_clusters()
      mutex_unlock(&si->xswap_lock)
                                            start_idx != nr_clusters_mapped
                                            return -EAGAIN

While CPU0 sleeps in vm_area_grow_pages(), any concurrent allocator that
also enters xswap_grow() snapshots the same si->nr_clusters_mapped and
blocks on si->xswap_lock, only to abort with -EAGAIN after CPU0 publishes
one chunk.  In the fallback path, each waking thread then scans only
si->free_clusters once, even though any newly published cluster from which
another CPU has allocated a single slot is immediately moved by
relocate_cluster() to si->frag_clusters[0] with SWAPFILE_CLUSTER - 1 free
slots remaining.  If concurrent allocators drain si->free_clusters before
a waiter reaches alloc_swap_scan_list(si, &si->free_clusters, ...),
cluster_alloc_swap_entry() returns 0 despite free slots in
si->frag_clusters[0] and unmapped clusters below si->nr_clusters_max.

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


  reply	other threads:[~2026-11-30 15:09 UTC|newest]

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

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-241470@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baoquan.he@linux.dev \
    --cc=chengming.zhou@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=shikemeng@huaweicloud.com \
    --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 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.