From: "Kunwu Chan" <kunwu.chan@linux.dev>
To: "Lian Wang" <lianux.mm@gmail.com>,
linux-mm@kvack.org, kunwu.chan@gmail.com
Cc: linux-kernel@vger.kernel.org,
"Andrew Morton" <akpm@linux-foundation.org>,
"Chris Li" <chrisl@kernel.org>, "Nhat Pham" <nphamcs@gmail.com>,
"Baoquan He" <baoquan.he@linux.dev>,
"Barry Song" <baohua@kernel.org>,
"Youngjun Park" <youngjun.park@lge.com>,
"Kemeng Shi" <shikemeng@huaweicloud.com>,
"Kairui Song" <kasong@tencent.com>,
"Jihan LIN" <linjh22s@gmail.com>, "Kees Cook" <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
"Thomas Gleixner" <tglx@kernel.org>,
linux-hardening@vger.kernel.org
Subject: Re: [RFC PATCH v4 06/18] mm/swap: serialize swap publication before removing swapon mutex
Date: Fri, 20 Nov 2026 04:30:16 +0000 [thread overview]
Message-ID: <f9cbc65ebeda9ebbc71c0488bb7df016f5c34961@linux.dev> (raw)
In-Reply-To: <ed10255256780c856689549e65654f8b01d40c2e.1794666741.git.lianux.mm@gmail.com>
>
> From: Kairui Song <kasong@tencent.com>
>
> Move publication of swap_file, resurrection of the device reference and
> the writable transition into one swapon_rwsem writer section. Split out
> a writer-held enable helper so initial publication can share this
> section, while failed swapoff can use the locking wrapper.
>
> With file publication and teardown serialized by the writer, remove
> swapon_mutex and use a topology reader across /proc/swaps iteration.
> Keep the SWP_USED filter when advancing the swap_info array.
>
> The publication boundary excludes proc readers from partially published
> state and gives the lockless live-reference users initialized device
> metadata before the device becomes an allocation candidate.
>
> Signed-off-by: Kairui Song <kasong@tencent.com>
> Co-developed-by: Lian Wang <lianux.mm@gmail.com>
> Signed-off-by: Lian Wang <lianux.mm@gmail.com>
> ---
> mm/swapfile.c | 39 +++++++++++++++++++--------------------
> 1 file changed, 19 insertions(+), 20 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index ae8421e18448..277a5c749987 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -139,9 +139,6 @@ static inline struct swap_info_struct *__swap_iter(int *i, unsigned long flag)
>
> static struct kmem_cache *swap_table_cachep;
>
> -/* Protects si->swap_file for /proc/swaps usage */
> -static DEFINE_MUTEX(swapon_mutex);
> -
> static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
> /* Activity counter to indicate that a swapon or swapoff has occurred */
> static atomic_t proc_poll_event = ATOMIC_INIT(0);
> @@ -3161,9 +3158,9 @@ static int setup_swap_extents(struct swap_info_struct *sis,
> * Mark a fully initialized swap device writable and expose it to the
> * allocator. The caller must have resurrected its percpu ref first.
> */
> -static void swap_device_enable(struct swap_info_struct *si)
> +static void __swap_device_enable(struct swap_info_struct *si)
> {
> - percpu_down_write(&swapon_rwsem);
> + lockdep_assert_held_write(&swapon_rwsem);
> spin_lock(&si->lock);
> spin_lock(&swap_avail_lock);
> si->flags |= SWP_WRITEOK;
> @@ -3173,11 +3170,16 @@ static void swap_device_enable(struct swap_info_struct *si)
> atomic_long_add(si->pages, &nr_swap_pages);
> total_swap_pages += si->pages;
> plist_add(&si->list, &swap_active_head);
> - percpu_up_write(&swapon_rwsem);
> -
> add_to_avail_list(si);
> }
>
> +static void swap_device_enable(struct swap_info_struct *si)
> +{
> + percpu_down_write(&swapon_rwsem);
> + __swap_device_enable(si);
> + percpu_up_write(&swapon_rwsem);
> +}
> +
> static int swap_device_disable(struct address_space *mapping,
> struct swap_info_struct **swap_info)
> {
> @@ -3340,7 +3342,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
> if (!(p->flags & SWP_SOLIDSTATE))
> atomic_dec(&nr_rotate_swap);
>
> - mutex_lock(&swapon_mutex);
> percpu_down_write(&swapon_rwsem);
> spin_lock(&p->lock);
> drain_mmlist();
> @@ -3355,7 +3356,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
> percpu_up_write(&swapon_rwsem);
> arch_swap_invalidate_area(p->type);
> zswap_swapoff(p->type);
> - mutex_unlock(&swapon_mutex);
> kfree(p->global_cluster);
> p->global_cluster = NULL;
> free_swap_cluster_info(cluster_info, maxpages);
> @@ -3401,20 +3401,18 @@ static __poll_t swaps_poll(struct file *file, poll_table *wait)
> return EPOLLIN | EPOLLRDNORM;
> }
>
> -/* iterator */
> static void *swap_start(struct seq_file *swap, loff_t *pos)
> {
> struct swap_info_struct *si;
> - int type;
> loff_t l = *pos;
>
> - mutex_lock(&swapon_mutex);
> + percpu_down_read(&swapon_rwsem);
>
> if (!l)
> return SEQ_START_TOKEN;
>
> - for (type = 0; (si = swap_type_to_info(type)); type++) {
> - if (!(si->swap_file))
> + for_each_swap(si) {
> + if (!si->swap_file)
> continue;
> if (!--l)
> return si;
> @@ -3435,7 +3433,7 @@ 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->flags & SWP_USED) || !(si->swap_file))
Could we keep checking just si->swap_file here and in swap_start()?
swapon() can set SWP_SOLIDSTATE before taking swapon_rwsem to
publish the file.
Thanks,
Kunwu
> continue;
> return si;
> }
> @@ -3445,7 +3443,7 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
>
> static void swap_stop(struct seq_file *swap, void *v)
> {
> - mutex_unlock(&swapon_mutex);
> + percpu_up_read(&swapon_rwsem);
> }
>
> static int swap_show(struct seq_file *swap, void *v)
> @@ -3958,7 +3956,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> goto free_swap_zswap;
> }
>
> - mutex_lock(&swapon_mutex);
> prio = DEF_SWAP_PRIO;
> if (swap_flags & SWAP_FLAG_PREFER)
> prio = swap_flags & SWAP_FLAG_PRIO_MASK;
> @@ -3970,11 +3967,14 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> si->prio = prio;
> si->list.prio = -si->prio;
> si->avail_list.prio = -si->prio;
> + /* Publish the file, live reference and writable state together. */
> + percpu_down_write(&swapon_rwsem);
> si->swap_file = swap_file;
>
> - /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
> + /* Make the reference live before exposing the writable device. */
> percpu_ref_resurrect(&si->users);
> - swap_device_enable(si);
> + __swap_device_enable(si);
> + percpu_up_write(&swapon_rwsem);
>
> pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s\n",
> K(si->pages), name->name, si->prio, nr_extents,
> @@ -3984,7 +3984,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
> (si->flags & SWP_AREA_DISCARD) ? "s" : "",
> (si->flags & SWP_PAGE_DISCARD) ? "c" : "");
>
> - mutex_unlock(&swapon_mutex);
> atomic_inc(&proc_poll_event);
> wake_up_interruptible(&proc_poll_wait);
>
Using the same rwsem for publication and /proc/swaps looks good.
next prev parent reply other threads:[~2026-11-20 4:30 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 14:47 [RFC PATCH v4 00/18] mm/swap: introduce priority queues for swap device allocation Lian Wang
2026-11-14 14:48 ` [RFC PATCH v4 01/18] mm/swap: remove unused parameter for reading swap header Lian Wang
2026-11-19 15:56 ` Kunwu Chan
2026-11-14 14:49 ` [RFC PATCH v4 02/18] mm/swap: clean up hibernation error handling Lian Wang
2026-11-19 15:57 ` Kunwu Chan
2026-11-14 14:49 ` [RFC PATCH v4 03/18] mm/swap: cleanup and document swap device availability flag usage Lian Wang
2026-11-20 3:58 ` Kunwu Chan
2026-11-14 14:49 ` [RFC PATCH v4 04/18] mm/swap: introduce swap device iteration helper Lian Wang
2026-11-20 3:59 ` Kunwu Chan
2026-11-14 14:49 ` [RFC PATCH v4 05/18] mm/swap: change the swapon lock into a percpu rwsem Lian Wang
2026-11-20 4:12 ` Kunwu Chan
2026-11-14 14:49 ` [RFC PATCH v4 06/18] mm/swap: serialize swap publication before removing swapon mutex Lian Wang
2026-11-20 4:30 ` Kunwu Chan [this message]
2026-11-14 14:50 ` [RFC PATCH v4 07/18] mm/swap: consolidate swap inuse accounting helpers Lian Wang
2026-11-20 4:30 ` Kunwu Chan
2026-11-14 14:50 ` [RFC PATCH v4 08/18] mm/swap: change back to use each swap device's percpu cluster Lian Wang
2026-11-20 4:31 ` Kunwu Chan
2026-11-14 14:50 ` [RFC PATCH v4 09/18] mm/swap: avoid direct reclaim when populating swap cluster metadata Lian Wang
2026-11-20 4:31 ` Kunwu Chan
2026-11-14 14:50 ` [RFC PATCH v4 10/18] mm/swap: add priority queue for swap device allocation Lian Wang
2026-11-16 3:51 ` Kairui Song
2026-11-16 11:11 ` Lian Wang
2026-11-20 4:34 ` Kunwu Chan
2026-11-14 14:51 ` [RFC PATCH v4 11/18] mm/swap: remove available list Lian Wang
2026-11-20 4:36 ` Kunwu Chan
2026-11-14 14:51 ` [RFC PATCH v4 12/18] mm/swap: bound synchronous discard outside the allocation reader Lian Wang
2026-11-20 4:36 ` Kunwu Chan
2026-11-14 14:51 ` [RFC PATCH v4 13/18] mm/swap: drop swap active plist Lian Wang
2026-11-20 4:37 ` Kunwu Chan
2026-11-14 14:51 ` [RFC PATCH v4 14/18] lib/plist.c: remove requeue function Lian Wang
2026-11-14 14:52 ` [RFC PATCH v4 15/18] mm/swap: reuse per-CPU queue readers across priority changes Lian Wang
2026-11-20 4:37 ` Kunwu Chan
2026-11-14 14:52 ` [RFC PATCH v4 16/18] mm/swap: advance the global queue cursor directly between priority rings Lian Wang
2026-11-20 4:38 ` Kunwu Chan
2026-11-14 14:52 ` [RFC PATCH v4 17/18] mm/swap: skip reader rotation for single-device " Lian Wang
2026-11-20 4:38 ` Kunwu Chan
2026-11-14 14:52 ` [RFC PATCH v4 18/18] mm/swap: redistribute queue readers after membership changes Lian Wang
2026-11-16 3:29 ` Kairui Song
2026-11-20 4:38 ` Kunwu Chan
2026-11-16 3:52 ` [RFC PATCH v4 00/18] mm/swap: introduce priority queues for swap device allocation Kairui Song
2026-11-16 11:10 ` Lian Wang
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=f9cbc65ebeda9ebbc71c0488bb7df016f5c34961@linux.dev \
--to=kunwu.chan@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=gustavoars@kernel.org \
--cc=kasong@tencent.com \
--cc=kees@kernel.org \
--cc=kunwu.chan@gmail.com \
--cc=lianux.mm@gmail.com \
--cc=linjh22s@gmail.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shikemeng@huaweicloud.com \
--cc=tglx@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