From: sashiko-bot@kernel.org
To: "Baoquan He" <baoquan.he@linux.dev>, linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, urezki@gmail.com,
chrisl@kernel.org, kasong@tencent.com, nphamcs@gmail.com,
baohua@kernel.org, youngjun.park@lge.com,
shikemeng@huaweicloud.com, hannes@cmpxchg.org, yosry@kernel.org,
chengming.zhou@linux.dev, corbet@lwn.net, kunwu.chan@gmail.com,
klarasmodin@gmail.com, "Baoquan He" <hebaoquan@kylinos.cn>
Subject: Re: [PATCH v6 11/14] mm, swap: add xswap grow trigger on cluster allocation
Date: Mon, 23 Nov 2026 12:49:01 +0000 [thread overview]
Message-ID: <sashiko-outbox-229261@kernel.org> (raw)
In-Reply-To: <20261122124814.3489077-2-baoquan.he@linux.dev>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm, swap: premature xswap allocation failure below 85% usage
in xswap_grow()
--
commit 71fd2887206158954afee3bd83d348af42ba786c
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: add xswap grow trigger on cluster allocation
This commit grows the mapped xswap cluster range from
cluster_alloc_swap_entry() when swap usage reaches 85% of the mapped
range, mapping enough clusters to bring usage down to about 65%.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 074c5114d129a..213b66bb1d6de 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -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);
> +#endif
> +
> if (!(si->flags & SWP_SOLIDSTATE)) {
> /* Serialize HDD SWAP allocation for each device. */
> spin_lock(&si->global_cluster_lock);
> @@ -1397,6 +1419,12 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
> if (found)
> goto done;
> }
> +
> +#ifdef CONFIG_XSWAP
> + /* A concurrent free or grow may have added clusters; retry once. */
> + if (!found && (si->flags & SWP_XSWAP))
> + found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
> +#endif
> done:
> if (!(si->flags & SWP_SOLIDSTATE))
> spin_unlock(&si->global_cluster_lock);
[ ... ]
> @@ -4023,14 +4082,62 @@ 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)
> +{
> + 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)
> + return 0;
[Severity: Medium]
Can cluster_alloc_swap_entry() fail without growing the mapped cluster
range when concurrent CPUs isolate or lock all mapped clusters before
swap_usage_in_pages(si) reaches 85%?
Each CPU missing in swap_alloc_fast() isolates a whole 512-page cluster
from si->free_clusters (taking it off the lists while
swap_cluster_populate() allocates ci->table, or holding ci->lock while
allocating from it), whereas swap_usage_in_pages(si) only counts individual
allocated slots. When more CPUs allocate concurrently than there are
unlocked non-full mapped clusters (initially XSWAP_GROW_CLUSTERS),
xswap_should_grow() returns false and the !found fallback only re-scans
si->free_clusters without growing, returning SWAP_ENTRY_INVALID and failing
folio_alloc_swap() with -ENOMEM even though unmapped clusters remain.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261122123918.3486916-1-baoquan.he@linux.dev?part=11
next prev parent reply other threads:[~2026-11-23 12:49 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 12:39 [PATCH v6 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-11-22 12:39 ` [PATCH v6 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
2026-11-23 12:48 ` sashiko-bot
2026-11-22 12:39 ` [PATCH v6 02/14] mm: xswap support for zswap Baoquan He
2026-11-22 12:39 ` [PATCH v6 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
2026-11-22 12:39 ` [PATCH v6 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
2026-11-22 12:39 ` [PATCH v6 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
2026-11-22 12:39 ` [PATCH v6 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
2026-11-22 12:39 ` [PATCH v6 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
2026-11-22 12:39 ` [PATCH v6 08/14] mm, swap: require zswap for xswap devices Baoquan He
2026-11-22 12:39 ` [PATCH v6 09/14] mm, swap: let the command line set the xswap device size Baoquan He
2026-11-23 12:48 ` sashiko-bot
2026-11-22 12:48 ` [PATCH v6 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
2026-11-22 12:48 ` [PATCH v6 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
2026-11-23 12:49 ` sashiko-bot [this message]
2026-11-22 12:48 ` [PATCH v6 12/14] mm, swap: add debugfs counters for xswap Baoquan He
2026-11-22 12:48 ` [PATCH v6 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-11-23 12:49 ` sashiko-bot
2026-11-22 12:48 ` [PATCH v6 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-229261@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox