Re: [RFC PATCH v4 03/18] mm/swap: cleanup and document swap device availability flag usage
From: Kunwu Chan
Date: Thu Nov 19 2026 - 22:58:56 EST
>
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> 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@xxxxxxxxxxx>
> Co-developed-by: Lian Wang <lianux.mm@xxxxxxxxx>
> Signed-off-by: Lian Wang <lianux.mm@xxxxxxxxx>
> ---
> 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