From: "Kunwu Chan" <kunwu.chan@linux.dev>
To: "Lian Wang" <lianux.mm@gmail.com>, linux-mm@kvack.org
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 03/18] mm/swap: cleanup and document swap device availability flag usage
Date: Fri, 20 Nov 2026 03:58:42 +0000 [thread overview]
Message-ID: <fbde457e42a2776addb2f44e1f2f95f8df5b2c54@linux.dev> (raw)
In-Reply-To: <517ab62dc9842b89c39f04f70d96bcc6717a884c.1794666741.git.lianux.mm@gmail.com>
>
> From: Kairui Song <kasong@tencent.com>
>
> Rework swap device flag and metadata handling to make the writable
> publication and disable boundaries explicit.
>
> Introduce swap_device_enable() and swap_device_disable(). Move
> SWP_WRITEOK updates into these boundaries. Clear the writable flag,
> set the off-list state and remove the available entry in one
> swap_avail_lock critical section, before releasing swap_lock.
>
> Preserve the existing swap_lock critical section across mapping lookup,
> hibernation checks and the disable transition. Drain cluster allocators
> after dropping swap_lock, retaining the device-identity protection
> without holding swap_lock over all cluster locks.
>
> Document the flag lifetime and locking rules.
>
> 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>
> ---
> include/linux/swap.h | 29 ++++--
> mm/swapfile.c | 205 ++++++++++++++++++++-----------------------
> 2 files changed, 118 insertions(+), 116 deletions(-)
>
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index 4f686709d71f..0fa77f5679a2 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -188,6 +188,24 @@ struct swap_extent {
> ((offsetof(union swap_header, magic.magic) - \
> offsetof(union swap_header, info.badpages)) / sizeof(int))
>
> +/*
> + * Swap device flags, except the ones documented below, all are immutable
> + * after exposed by swap_device_enable, and until the device is freed again
> + * (SWP_USED unset), except that teardown clears SWP_ACTIVATED after draining
> + * allocators and live references. The other exceptions:
> + * - SWP_USED: Protected by swap_lock. Indicates the device is inuse. Once
> + * set, won't be cleared unless all reference to this device is freed and
> + * swapoff finished.
> + * - SWP_WRITEOK: Protected by swap_lock, si->lock and swap_avail_lock.
> + * Clearing this flag is followed by waiting for all current cluster lock
> + * users to exit, so
> + * checking this flag while holding any of these locks ensures the device
> + * is safe to use at the moment. Note: clearing this flag doesn't affect
> + * pending IO or async requests, it only prevents further entry allocation
> + * or new async request (e.g. discard) from initiating.
> + * - SWP_HIBERNATION: Protected by swap_lock. Indicates if the device
> + * is pinned for hibernation.
> + */
> enum {
> SWP_USED = (1 << 0), /* is slot in swap_info[] used? */
> SWP_WRITEOK = (1 << 1), /* ok to write to this swap? */
> @@ -256,14 +274,9 @@ struct swap_info_struct {
> struct file *swap_file; /* seldom referenced */
> struct completion comp; /* seldom referenced */
> spinlock_t lock; /*
> - * protect map scan related fields like
> - * inuse_pages and all cluster lists.
> - * Other fields are only changed
> - * at swapon/swapoff, so are protected
> - * by swap_lock. changing flags need
> - * hold this lock and swap_lock. If
> - * both locks need hold, hold swap_lock
> - * first.
> + * Protect cluster lists. Other fields
> + * are only changed at swapon/swapoff,
> + * so are protected by swap_lock.
> */
> struct work_struct discard_work; /* discard worker */
> struct work_struct reclaim_work; /* reclaim worker */
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index c69a924acb74..6485c8020d3a 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -1268,28 +1268,23 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
>
> spin_lock(&swap_avail_lock);
>
> - if (swapoff) {
> - /*
> - * Forcefully remove it. Clear the SWP_WRITEOK flags for
> - * swapoff here so it's synchronized by both si->lock and
> - * swap_avail_lock, to ensure the result can be seen by
> - * add_to_avail_list.
> - */
> - lockdep_assert_held(&si->lock);
> - si->flags &= ~SWP_WRITEOK;
> - atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages);
> - } else {
> - /*
> - * If not called by swapoff, take it off-list only if it's
> - * full and SWAP_USAGE_OFFLIST_BIT is not set (strictly
> - * si->inuse_pages == pages), any concurrent slot freeing,
> - * or device already removed from plist by someone else
> - * will make this return false.
> - */
> + /*
> + * Force remove it only for swapoff. Else, take it off-list only if
> + * it's full and SWAP_USAGE_OFFLIST_BIT is not set (strictly
> + * si->inuse_pages == pages), so concurrent slot freeing, or
> + * concurrent list removal will make the cmpxchg fail and skip
> + * the removal.
> + */
> + if (!swapoff) {
> pages = si->pages;
> if (!atomic_long_try_cmpxchg(&si->inuse_pages, &pages,
> pages | SWAP_USAGE_OFFLIST_BIT))
> goto skip;
> + } else {
> + lockdep_assert_held(&swap_lock);
> + lockdep_assert_held(&si->lock);
> + si->flags &= ~SWP_WRITEOK;
> + atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages);
> }
>
> plist_del(&si->avail_list, &swap_avail_head);
> @@ -1299,21 +1294,21 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
> }
>
> /* SWAP_USAGE_OFFLIST_BIT can only be cleared by this helper. */
> -static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
> +static void add_to_avail_list(struct swap_info_struct *si)
> {
> long val;
> unsigned long pages;
>
> spin_lock(&swap_avail_lock);
>
> - /* Corresponding to SWP_WRITEOK clearing in del_from_avail_list */
> - if (swapon) {
> - lockdep_assert_held(&si->lock);
> - si->flags |= SWP_WRITEOK;
> - } else {
> - if (!(READ_ONCE(si->flags) & SWP_WRITEOK))
Can we keep READ_ONCE(si->flags) here? pin_hibernation_swap_type()
updates SWP_HIBERNATION in the same word without taking swap_avail_lock.
Thanks,
Kunwu
next prev parent reply other threads:[~2026-11-20 3:58 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 [this message]
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
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=fbde457e42a2776addb2f44e1f2f95f8df5b2c54@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=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 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.