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, 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 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices
Date: Fri, 04 Dec 2026 16:48:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-250222@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-5-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: spurious SIGBUS in do_swap_page() for stale xswap entries
- [Low] mm: duplicated xswap cluster setup and teardown in swapfile.c
- [Low] mm: data race on area->pages in vm_area_grow_pages() vs
  show_busy_info()
--

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

mm, swap: use a VM_SPARSE cluster array for xswap devices

This commit adds CONFIG_XSWAP and backs an xswap device's cluster_info
array with a VM_SPARSE | VM_ALLOC vmalloc area mapped lazily in chunks.
It also updates get_swap_device(), find_next_to_unuse(),
wait_for_allocation(), and free_swap_cluster_info() to bound accesses by
nr_clusters_mapped.

> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 35a079f2bb767..de2feaa2a6287 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c

[ ... ]

> @@ -1983,6 +1992,20 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
>  	offset = swp_offset(entry);
>  	if (offset >= si->max)
>  		goto put_out;
> +#ifdef CONFIG_XSWAP
> +	if (si->flags & SWP_XSWAP) {
> +		/* Pairs with the release store in xswap_map_clusters(). */
> +		unsigned long mapped = smp_load_acquire(&si->nr_clusters_mapped);
> +
> +		/*
> +		 * xswap maps cluster_info lazily.  A stale entry can point
> +		 * past nr_clusters_mapped; reject it before anything
> +		 * dereferences cluster_info.
> +		 */
> +		if (offset / SWAPFILE_CLUSTER >= mapped)
> +			goto put_out;
> +	}
> +#endif
>  
>  	return si;

[Severity: Medium]
Can jumping to put_out here cause a spurious SIGBUS in do_swap_page()?

CPU0                              CPU1
do_swap_page()
  pte_unmap_same(vmf)
                                  swapoff()
                                    unuse_mm() /* mmap_read_lock */
                                    /* replaces swap PTE */
                                  swapon() /* xswap, reuses slot */
                                    xswap_map_clusters()
  get_swap_device(entry)
    goto put_out  <-- returns ERR_PTR(-EIO)
  ret = VM_FAULT_SIGBUS

do_swap_page() drops the page table lock in pte_unmap_same() before
calling get_swap_device(entry) under mmap_read_lock or the per-VMA read
lock.  Because unuse_mm() only takes mmap_read_lock, a concurrent
swapoff() followed by swapon() of an xswap device can reuse the same
swap_info[] slot with nr_clusters_mapped initialized to
XSWAP_GROW_CLUSTERS.

When do_swap_page() then passes the stale entry, put_out logs a
"Bad swap offset entry" error and returns ERR_PTR(-EIO), which
do_swap_page() turns directly into VM_FAULT_SIGBUS without re-locking
the page table lock to check whether the PTE changed.

[ ... ]

> @@ -3724,6 +3846,69 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
>  	int err = -ENOMEM;
>  	unsigned long i;
>  
> +#ifdef CONFIG_XSWAP
> +	if (si->flags & SWP_XSWAP) {
> +		unsigned long size = PAGE_ALIGN(nr_clusters * sizeof(*cluster_info));
> +		struct vm_struct *vm;
> +
> +		vm = get_vm_area(size, VM_SPARSE | VM_ALLOC);
> +		if (!vm)
> +			goto err;
> +
> +		cluster_info = vm->addr;
> +		si->cluster_vm = vm;
> +		si->nr_clusters_max = nr_clusters;
> +		si->cluster_info = cluster_info;
> +
> +		/* Must be initialized before xswap_map_clusters() locks it. */
> +		mutex_init(&si->xswap_lock);
> +
> +		err = xswap_map_clusters(si, 0, min_t(unsigned long,
> +					XSWAP_GROW_CLUSTERS, nr_clusters));
> +		if (err)
> +			goto err_free_vm;

[Severity: Low]
Is there a data race on area->pages between the initial
xswap_map_clusters() call and /proc/vmallocinfo?

get_vm_area() inserts the vm_struct onto vn->busy.head without
VM_UNINITIALIZED, making it immediately visible to show_busy_info().
On the first grow, vm_area_grow_pages() assigns area->pages without
vn->busy.lock:

mm/vmalloc.c:vm_area_grow_pages() {
    ...
		if (!old) {
			area->pages = pages;
		} else {
			memcpy(pages, old, area->nr_pages * sizeof(*pages));
			spin_lock(&vn->busy.lock);
			area->pages = pages;
			spin_unlock(&vn->busy.lock);
    ...
}

Concurrently, show_busy_info() reads v->pages under vn->busy.lock in
is_vmalloc_addr(v->pages), which can trigger a KCSAN report or an
inconsistent vpages tag.  Taking vn->busy.lock around area->pages = pages
in the !old branch too would close the race.

[ ... ]

> +		INIT_LIST_HEAD(&si->free_clusters);
> +		INIT_LIST_HEAD(&si->full_clusters);
> +		INIT_LIST_HEAD(&si->discard_clusters);
> +		for (i = 0; i < SWAP_NR_ORDERS; i++) {
> +			INIT_LIST_HEAD(&si->nonfull_clusters[i]);
> +			INIT_LIST_HEAD(&si->frag_clusters[i]);
> +		}
> +
> +		/*
> +		 * Cluster 0 holds the header slot, which is marked bad, so it
> +		 * is not entirely free.  si->max is cluster aligned, so no
> +		 * cluster holds a slot past the end of the device.
> +		 */
> +		for (i = 0; i < si->nr_clusters_mapped; i++) {
> +			struct swap_cluster_info *ci = &cluster_info[i];
> +
> +			if (ci->count) {
> +				ci->flags = CLUSTER_FLAG_NONFULL;
> +				list_add_tail(&ci->list, &si->nonfull_clusters[0]);
> +			} else {
> +				ci->flags = CLUSTER_FLAG_FREE;
> +				list_add_tail(&ci->list, &si->free_clusters);
> +			}
> +		}
> +
> +		return 0;
> +
> +err_free_vm:
> +		vfree(si->cluster_info);
> +		si->cluster_vm = NULL;
> +		si->cluster_info = NULL;
> +		si->nr_clusters_mapped = 0;
> +		return err;
> +	}
> +#endif /* CONFIG_XSWAP */

[Severity: Low]
Can the xswap path share the cluster list initialization, insertion loop,
and error cleanup with the non-xswap path below?

The list setup and cluster insertion loop here duplicate the ones at the
end of setup_swap_clusters_info(), and err_free_vm duplicates the xswap
teardown in free_swap_cluster_info() while omitting per-cluster table
cleanup, so jumping to the existing err label would also clean up
cluster 0's table on error.  The same per-cluster table freeing loop is
also duplicated between the xswap and non-xswap branches in
free_swap_cluster_info().

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


  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 [this message]
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
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-250222@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox