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, 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 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap
Date: Mon, 23 Nov 2026 12:49:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-229263@kernel.org> (raw)
In-Reply-To: <20261122124814.3489077-4-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm, swap: percpu_swap_cluster priority inversion in
  swap_alloc_fast()
- [Medium] mm, swap: futile global lock contention in folio_alloc_swap()
  without real swap
--

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

mm, swap: do not give an xswap slot to a cgroup that cannot zswap

This commit skips xswap devices during swap slot allocation when the
folio's cgroup cannot accept another zswap page according to
obj_cgroup_may_zswap().

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 462efeef887a8..90f17edce1d04 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -1612,7 +1618,7 @@ static bool get_swap_device_info(struct swap_info_struct *si)
>   * Fast path try to get swap entries with specified order from current
>   * CPU's swap entry pool (a cluster).
>   */
> -static bool swap_alloc_fast(struct folio *folio)
> +static bool swap_alloc_fast(struct folio *folio, bool may_zswap)
>  {
>  	unsigned int order = folio_order(folio);
>  	struct swap_cluster_info *ci;
> @@ -1628,6 +1634,11 @@ static bool swap_alloc_fast(struct folio *folio)
>  	if (!si || !offset || !get_swap_device_info(si))
>  		return false;
>  
> +	if (!may_zswap && (si->flags & SWP_XSWAP)) {
> +		put_swap_device(si);
> +		return false;
> +	}
> +
>  	ci = swap_cluster_lock(si, offset);
>  	if (cluster_is_usable(ci, order)) {
>  		if (cluster_is_empty(ci))
> @@ -1642,13 +1653,19 @@ 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);

[Severity: Medium]
Can a !may_zswap allocation overwrite percpu_swap_cluster.si[order] and
route later zswap-eligible folios on the same CPU to a lower-priority
physical swap device?

When may_zswap is false, swap_alloc_fast() rejects the cached xswap
device and swap_alloc_slow() falls back to a physical swap device.  If
that physical device has SWP_SOLIDSTATE set, cluster_alloc_swap_entry()
-> alloc_swap_scan_cluster() installs it into percpu_swap_cluster:

mm/swapfile.c:alloc_swap_scan_cluster() {
	...
	if (si->flags & SWP_SOLIDSTATE) {
		this_cpu_write(percpu_swap_cluster.offset[order], next);
		this_cpu_write(percpu_swap_cluster.si[order], si);
	} else {
	...
}

Because swap_alloc_fast() only rejects SWP_XSWAP when !may_zswap,
subsequent folios with may_zswap == true on that CPU will allocate up to
the remaining SWAPFILE_CLUSTER - 1 slots from the lower-priority physical
swap device even while higher-priority xswap space is available.

[ ... ]

> @@ -1987,10 +2015,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;
> +	}
> +
>  	if (order) {
>  		/*
>  		 * Reject large allocation when THP_SWAP is disabled. Check below
> @@ -2011,8 +2050,8 @@ int folio_alloc_swap(struct folio *folio)
>  
>  again:
>  	local_lock(&percpu_swap_cluster.lock);
> -	if (!swap_alloc_fast(folio))
> -		swap_alloc_slow(folio);
> +	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))) {
> @@ -2020,6 +2059,20 @@ int folio_alloc_swap(struct folio *folio)
>  			goto again;
>  	}
>  
> +	/*
> +	 * The gate was skipped when there was no device; one may have
> +	 * appeared since, so ask the slot itself.
> +	 */
> +	if (unlikely(!zswap_checked && folio_test_swapcache(folio) &&
> +		     (__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP))) {
> +		zswap_checked = true;
> +		may_zswap = folio_may_zswap(folio);
> +		if (!may_zswap) {
> +			swap_cache_del_folio(folio);
> +			goto again;
> +		}
> +	}
> +
>  	/* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
>  	if (unlikely(mem_cgroup_try_charge_swap(folio))) {
>  		swap_cache_del_folio(folio);
> @@ -2037,6 +2090,13 @@ int folio_alloc_swap(struct folio *folio)
>  	if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
>  		return -ENOMEM;
>  
> +	/*
> +	 * Only xswap devices exist and the folio cannot use them; splitting
> +	 * cannot help.
> +	 */
> +	if (!may_zswap && !atomic_read(&nr_real_swapfiles))
> +		return -ENOMEM;
> +
>  	return order ? -E2BIG : -ENOMEM;
>  }

[Severity: Medium]
Should folio_alloc_swap() skip the allocation loop when !may_zswap &&
!atomic_read(&nr_real_swapfiles)?

When a cgroup has reached memory.zswap.max on an xswap-only system,
checking this condition only under failed: causes every reclaimed folio
to acquire percpu_swap_cluster.lock, take the global swap_avail_lock in
swap_alloc_slow(), and for order-0 folios take the global swap_lock in
swap_sync_discard() even though no usable swap device exists.

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


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