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, 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 06/14] mm, swap: create the xswap device from swapon(2)
Date: Fri, 04 Dec 2026 16:48:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-250224@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-7-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm, swap: xswap_create() breaks large-folio swapout by omitting
  SWP_BLKDEV
- [Medium] mm: unsynchronized si->flags reads and SWP_WRITEOK gating in
  /proc/swaps
- [Medium] mm, swap: xswap_create() omits SWP_SYNCHRONOUS_IO, hurting
  packing and swapin
--

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

mm, swap: create the xswap device from swapon(2)

This commit allows swapon(2) to create a file-less xswap device when
passed the name "xswap" and no matching file exists, exposes it in
/proc/swaps, and skips it during hibernation device discovery.

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 912cfa68ba01a..4e598ab8da656 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -3497,7 +3522,8 @@ static void *swap_start(struct seq_file *swap, loff_t *pos)
>  		return SEQ_START_TOKEN;
>  
>  	for (type = 0; (si = swap_type_to_info(type)); type++) {
> -		if (!(si->swap_file))
> +		if (!si->swap_file &&
> +		    !((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK)))
>  			continue;
>  		if (!--l)
>  			return si;
> @@ -3518,7 +3544,8 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
>  
>  	++(*pos);
>  	for (; (si = swap_type_to_info(type)); type++) {
> -		if (!(si->swap_file))
> +		if (!si->swap_file &&
> +		    !((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK)))
>  			continue;
>  		return si;
>  	}

[Severity: Medium]
Can these unmarked reads of si->flags race with concurrent writers, and
does gating on SWP_WRITEOK hide xswap from /proc/swaps during swapoff?

swap_start() and swap_next() hold only swapon_mutex, which protects
si->swap_file rather than si->flags:

CPU0 (/proc/swaps)                CPU1 (swapoff)
swap_start()
  mutex_lock(&swapon_mutex)
                                  del_from_avail_list()
                                    spin_lock(&swap_lock)
                                    si->flags &= ~SWP_WRITEOK
                                    spin_unlock(&swap_lock)
  si->flags & SWP_WRITEOK  <-- data race

Similarly, xswap_create() modifies si->flags without holding swap_lock
after alloc_swap_info() has already published si in swap_info[], racing
with si_swapinfo() (via sysinfo(2)) which reads si->flags under swap_lock.

Checking SWP_WRITEOK here also hides the xswap device from /proc/swaps
as soon as swapoff clears SWP_WRITEOK before try_to_unuse(), instead of
keeping it visible to show unuse progress as established by commit
eca4d01b982d ("mm, swap: protect si->swap_file properly and use as a mount indicator").

[ ... ]

> +static int xswap_create(int prio)
> +{

[ ... ]

> +	si->bdev = NULL;
> +	si->swap_file = NULL;
> +	si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;

[Severity: High]
Does omitting SWP_BLKDEV here disable large-folio swapout system-wide
once xswap is active?

xswap defaults to DEF_XSWAP_PRIO (SWAP_FLAG_PRIO_MASK), placing it ahead
of real swap partitions on swap_avail_head.  When shrink_folio_list()
calls folio_alloc_swap() -> swap_alloc_slow() on a large folio,
cluster_alloc_swap_entry() rejects order > 0 because SWP_BLKDEV is unset.

swap_alloc_slow() then returns immediately for large folios without
trying lower-priority devices, so folio_alloc_swap() returns -E2BIG and
shrink_folio_list() splits every THP and mTHP into order-0 pages.  Since
SWP_BLKDEV paths dereference si->bdev, should cluster_alloc_swap_entry()
also allow SWP_XSWAP for order > 0 allocations?

[Severity: Medium]
Should SWP_SYNCHRONOUS_IO also be set on the RAM-only xswap device?

Without SWP_SYNCHRONOUS_IO, swap_prefer_free_cluster() returns true, so
cluster_alloc_swap_entry() allocates fresh free clusters and swap tables
before reusing nonfull clusters, inflating the lazy cluster footprint.

On read faults, should_try_to_free_swap() also returns false without
SWP_SYNCHRONOUS_IO, leaving folios pinned in swapcache even after
zswap_load() has deleted the zswap entry and dirtied the folio.

Finally, swap_entry_synchronous() falls back to an extra
zswap_is_present() XArray lookup on anonymous faults (and sends
zero-mapped entries through swapin_readahead()), while
shmem_swapin_folio() falls back to shmem_swapin_cluster() instead of
direct synchronous swap-in.

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


  reply	other threads:[~2026-12-04 16:48 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 [this message]
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
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-250224@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 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.