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, 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 09/14] mm, swap: let the command line set the xswap device size
Date: Mon, 23 Nov 2026 12:48:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-229259@kernel.org> (raw)
In-Reply-To: <20261122123918.3486916-10-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm, swap: multi-GiB eager allocation in xswap_create() on
  xswap.max=max
--

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

mm, swap: let the command line set the xswap device size

This commit adds the xswap.max= kernel parameter to configure the size of a
new xswap device as a byte size, a percentage of RAM, or "max".

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index ba899e349e992..6ce5dbb3b1035 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> +/*
> + * The most a device can address, whatever xswap.max= asked for.  An xswap
> + * slot is still a swap entry, so the swap offset limit applies even though
> + * the data never goes through a physical address.
> + */
> +static unsigned long xswap_size_limit(void)
> +{
> +	unsigned long limit = swapfile_maximum_size;
> +
> +	/* A cluster index is an unsigned int. */
> +	if (limit / SWAPFILE_CLUSTER > UINT_MAX)
> +		limit = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER;
> +
> +	return limit;
> +}
> +
> +static unsigned long xswap_size_pages(void)
> +{
> +	unsigned long long pages;
> +
> +	if (xswap_size_max)
> +		return xswap_size_limit();

[Severity: Medium]
Can setting xswap.max=max cause multi-gigabyte eager allocations and a long
unscheduled loop during xswap_create()?

On 64-bit systems, xswap_size_limit() returns (unsigned long)UINT_MAX *
SWAPFILE_CLUSTER (~2.2 trillion pages with 4K pages and CONFIG_THP_SWAP=y,
or ~35.2 trillion pages with 64K pages).  Although si->cluster_info uses a
VM_SPARSE area to populate cluster metadata lazily, xswap_create() passes
the full maxpages to setup_swap_clusters_info() and zswap_swapon().

First, zswap_swapon() allocates and initializes one struct xarray per
ZSWAP_ADDRESS_SPACE_PAGES of si->max upfront without cond_resched():

mm/zswap.c:zswap_swapon() {
	...
	nr = DIV_ROUND_UP(nr_pages, ZSWAP_ADDRESS_SPACE_PAGES);
	trees = kvzalloc_objs(*tree, nr);
	if (!trees) {
		pr_err("alloc failed, zswap disabled for swap type %d\n", type);
		return -ENOMEM;
	}

	for (i = 0; i < nr; i++)
		xa_init(trees + i);
	...
}

At xswap_size_limit(), nr is 134,217,728 (2 GiB of xarrays on 4K-page
kernels, or over 10 GiB with CONFIG_LOCKDEP) up to 2,147,483,648 (32 GiB on
64K-page kernels).

Second, mapping the first cluster chunk calls vm_area_grow_pages() under
PF_MEMALLOC_NORECLAIM, which allocates area->pages for the entire 192 GiB
VM_SPARSE virtual range at once (384 MiB on 4K-page systems).

On systems with less physical RAM than these metadata arrays, swapon(2)
with xswap.max=max fails with -ENOMEM and a warn_alloc() splat; on larger
machines it pins multiple gigabytes of unswappable memory before any page
is swapped and stalls CPU in the xa_init() and swapoff xa_destroy() loops.

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


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