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 07/14] mm, swap: refactor swapoff and add xswap_destroy
Date: Fri, 04 Dec 2026 16:48:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-250225@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-8-baoquan.he@linux.dev>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: swapoff() open-first relative lookup for xswap enables
  CWD shadowing
- [Medium] mm: xswap_destroy() holds uninterruptible xswap_ctl_lock
  across __swapoff()
- [Low] mm: unannotated data race on si->flags in /proc/swaps vs
  xswap_destroy()
--

commit 238343efb8d379276ae632b33dfaa8a1b5aa260e
Author: Baoquan He <hebaoquan@kylinos.cn>

mm, swap: refactor swapoff and add xswap_destroy

This commit extracts swap_info_remove() and __swapoff() from sys_swapoff()
to support file-less swap devices, and adds xswap_destroy() reached via the
name printed in /proc/swaps.

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

[ ... ]

> @@ -3375,15 +3383,30 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
>  	BUG_ON(!current->mm);
>  
>  	CLASS(filename, pathname)(specialfile);
> +	if (IS_ERR(pathname))
> +		return PTR_ERR(pathname);
> +
>  	victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
> -	if (IS_ERR(victim))
> -		return PTR_ERR(victim);
> +	if (IS_ERR(victim)) {
> +		/*
> +		 * /proc/swaps shows a name that cannot be opened; accept it
> +		 * so swapoff -a works.  Only -ENOENT means "no such path",
> +		 * so a file of that name keeps precedence.
> +		 */
> +		if (PTR_ERR(victim) != -ENOENT)
> +			return PTR_ERR(victim);
> +
> +		err = xswap_destroy_by_name(pathname->name);
> +		if (err == -ENOENT)
> +			return PTR_ERR(victim);
> +		return err;
> +	}

[Severity: Medium]
Can resolving "xswap" through file_open_name() first break swapoff -a
and allow working-directory shadowing?

file_open_name() resolves relative names against AT_FDCWD, so any
./xswap file or directory in the working directory makes swapoff -a fail
with -EINVAL or -EISDIR instead of reaching xswap_destroy_by_name().
Userspace then has no unambiguous path to deactivate xswap.

A symlink ./xswap can also redirect root's O_RDWR open to another swap
device or device node, and a dangling symlink returns -ENOENT and falls
through to xswap_destroy() despite a filesystem entry existing.

[ ... ]

> @@ -3485,13 +3544,10 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
>  	p->flags = 0;
>  	spin_unlock(&swap_lock);

[Severity: Low]
Is there an unannotated data race on p->flags between xswap teardown
and /proc/swaps readers?

swap_start(), swap_next(), and swap_type_str() hold only swapon_mutex
and read si->flags directly without READ_ONCE() or swap_lock:

CPU0                              CPU1
__swapoff()                       swap_start()
  mutex_lock(&swapon_mutex)
  ...
  mutex_unlock(&swapon_mutex)
                                    mutex_lock(&swapon_mutex)
  spin_lock(&swap_lock)
  p->flags = 0                      if (!si->swap_file &&
                                        !((si->flags & SWP_XSWAP) &&
                                          (si->flags & SWP_WRITEOK)))
                                      <-- data race on si->flags

del_from_avail_list() and reinsert_swap_info() also modify si->flags
without swapon_mutex or WRITE_ONCE().  This triggers KCSAN reports and
allows tearing across the two flag checks; annotating these accesses
with READ_ONCE()/WRITE_ONCE() would fix the race.

[ ... ]

> @@ -4114,7 +4170,57 @@ static int xswap_create(int prio)
>  	goto out;
>  }
>  
> -/* A name, not a path: only -ENOENT from opening it gets here. */
> +/* Tear down the file-less xswap device. */
> +static int xswap_destroy(void)
> +{
> +	struct swap_info_struct *p = NULL;
> +	unsigned int type;
> +	int err;
> +
> +	mutex_lock(&xswap_ctl_lock);
> +
> +	spin_lock(&swap_lock);
> +	/* Only a fully enabled device; a half-created one does not count. */
> +	for (type = 0; type < nr_swapfiles; type++) {
> +		struct swap_info_struct *q = swap_info[type];
> +
> +		if (q && (q->flags & (SWP_XSWAP | SWP_WRITEOK)) ==
> +			 (SWP_XSWAP | SWP_WRITEOK)) {
> +			p = q;
> +			break;
> +		}
> +	}
> +	if (!p) {
> +		spin_unlock(&swap_lock);
> +		err = -ENOENT;
> +		goto out;
> +	}
> +	/* Refuse swapoff while the device is pinned for hibernation */
> +	if (p->flags & SWP_HIBERNATION) {
> +		spin_unlock(&swap_lock);
> +		err = -EBUSY;
> +		goto out;
> +	}
> +
> +	err = swap_info_remove(p);
> +	spin_unlock(&swap_lock);
> +	if (!err)
> +		err = __swapoff(p);
> +out:
> +	mutex_unlock(&xswap_ctl_lock);
> +	return err;
> +}

[Severity: Medium]
Can holding xswap_ctl_lock across __swapoff() stall concurrent swapon
and swapoff callers in unkillable D-state?

__swapoff() runs try_to_unuse(), which retries without a cap while
draining swap, followed by synchronize_rcu(), wait_for_completion(),
and flush_work().  Both xswap_create() and xswap_destroy() take
xswap_ctl_lock with mutex_lock(), so a concurrent caller cannot be
killed and can trigger hung-task warnings or panics.

Releasing xswap_ctl_lock before __swapoff() still keeps the single
device invariant: swap_info_remove() already cleared SWP_WRITEOK (so
another xswap_destroy() gets -ENOENT), while SWP_XSWAP stays set until
__swapoff() finishes (so xswap_create() gets -EBUSY).

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


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