mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Li <chrisl@kernel.org>
To: Lian Wang <lianux.mm@gmail.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 Andrew Morton <akpm@linux-foundation.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>,
	 Kunwu Chan <kunwu.chan@linux.dev>, Kees Cook <kees@kernel.org>,
	 "Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH v5 04/19] mm/swap: introduce swap device iteration helper
Date: Thu, 26 Nov 2026 09:26:29 -0800	[thread overview]
Message-ID: <CACePvbU2cg1EVBjONWjYcsanDM0JS4eLXgX9aGYeEaXu4zZ2-w@mail.gmail.com> (raw)
In-Reply-To: <be6ef058f4a6babd3562270daa5b61c4ff9a6a50.1795439990.git.lianux.mm@gmail.com>

On Mon, Nov 23, 2026 at 6:16 AM Lian Wang <lianux.mm@gmail.com> wrote:
>
> From: Kairui Song <kasong@tencent.com>
>
> Separate iteration over allocated swap_info descriptors from iteration
> over writable devices. Hibernation lookups require SWP_USED and
> SWP_WRITEOK under swap_lock. Take a single snapshot for each flag
> test, since unrelated bits in the same word can change during setup.
>
> drain_mmlist() only needs the atomic usage count; walk every
> descriptor without loading flags there. In si_swapinfo(), count
> usage only for descriptors that are in use but no longer writable.
>
> Move the live-reference warning from __swap_type_to_info() into
> __swap_entry_to_info(). The iterator must inspect descriptors before
> applying its flag filter, including descriptors whose device reference
> is dead; entry-based lookup retains the warning.

The patches looks good. Some minor nitpick follows.

Acked-by: Chris Li <chrisl@kernel.org>

Chris

>
> Signed-off-by: Kairui Song <kasong@tencent.com>
> Signed-off-by: Lian Wang <lianux.mm@gmail.com>
> ---
>  mm/swap.h     | 12 ++++----
>  mm/swapfile.c | 77 ++++++++++++++++++++++++++++++++-------------------
>  2 files changed, 54 insertions(+), 35 deletions(-)
>
> diff --git a/mm/swap.h b/mm/swap.h
> index d488a69c2809..70fd3ab655ff 100644
> --- a/mm/swap.h
> +++ b/mm/swap.h
> @@ -108,16 +108,16 @@ static inline unsigned int swp_cluster_offset(swp_entry_t entry)
>   */
>  static inline struct swap_info_struct *__swap_type_to_info(int type)
>  {
> -       struct swap_info_struct *si;
> -
> -       si = READ_ONCE(swap_info[type]); /* rcu_dereference() */
> -       VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
> -       return si;
> +       return READ_ONCE(swap_info[type]); /* rcu_dereference() */
>  }
>
>  static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry)
>  {
> -       return __swap_type_to_info(swp_type(entry));
> +       struct swap_info_struct *si;
> +
> +       si = __swap_type_to_info(swp_type(entry)); /* rcu_dereference() */
> +       VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
> +       return si;
>  }
>
>  static inline struct swap_cluster_info *__swap_offset_to_cluster(
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index f22197a6286c..56b75770d320 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -106,6 +106,34 @@ static DEFINE_SPINLOCK(swap_avail_lock);
>
>  struct swap_info_struct *swap_info[MAX_SWAPFILES];
>
> +static inline struct swap_info_struct *__swap_iter(int *i, unsigned long flag)
> +{
> +       lockdep_assert_held(&swap_lock);
Nitpick: Suggest have a internal version of "int _i", then sync to
"int *i" at function exit.

> +       while (*i < nr_swapfiles) {

The trick part of this is that, if the caller change *i between you
test *i < nr_swapfile and you use *i for __swap_type_to_info(*i), you
can visit out of bound array.  So having an internal version of  *i as
scalor will remove that abuse. It is likely the compiler can generate
better code as well because it avoids needing to know about pointer
aliasing with pointer. I think your later verison might need to use
more complicate inter state that "int i", so you might as well make it
a inter struct which contains "int index" now.

> +               struct swap_info_struct *si = __swap_type_to_info(*i);
> +
> +               VM_WARN_ON(!si);

You don't need this WARN ON. When i < nr_swapfile, the swap_info[i]
array can never the NULL. If you are paranoid you can continue if "si"
is NULL.

> +               (*i)++;
> +               if (flag && (READ_ONCE(si->flags) & flag) != flag)
> +                       continue;
> +               return si;
> +       }
> +       return NULL;
> +}
> +
> +#define __for_each_swap(si, flag)                      \
> +       for (int __i = 0; ((si) = __swap_iter(&__i, flag));)

This is a bit too cose to for_each_swap_info. May be __for_each_swap_dev_flag()

> +
> +/*
> + * for_each_swap_info - iterate over all allocated swap descriptors
> + * @si: the iterator
> + *
> + * Context: The caller must hold swap_lock. The lock may be dropped during
> + * the loop body but must be re-acquired before the next iteration.
> + */
> +#define for_each_swap_info(si) __for_each_swap(si, 0)

NITPICK: for_each_swap_dev()?

> +#define for_each_avail_swap(si) __for_each_swap(si, SWP_USED | SWP_WRITEOK)

NITPICK: for_each_avail_swap_dev()?

> +
>  static struct kmem_cache *swap_table_cachep;
>
>  /* Protects si->swap_file for /proc/swaps usage */
> @@ -2284,26 +2312,18 @@ void swap_free_hibernation_slot(swp_entry_t entry)
>
>  static int __find_hibernation_swap_type(dev_t device, sector_t offset)
>  {
> -       int type;
> -
> -       lockdep_assert_held(&swap_lock);
> +       struct swap_info_struct *si;
>
>         if (!device)
>                 return -EINVAL;
>
> -       for (type = 0; type < nr_swapfiles; type++) {
> -               struct swap_info_struct *sis = swap_info[type];
> -
> -               if (!(sis->flags & SWP_WRITEOK))
> -                       continue;
> -
> -               if (device == sis->bdev->bd_dev) {
> -                       struct swap_extent *se = first_se(sis);
> -
> -                       if (se->start_block == offset)
> -                               return type;
> +       for_each_avail_swap(si) {
> +               if (device == si->bdev->bd_dev) {
> +                       if (first_se(si)->start_block == offset)
> +                               return si->type;
>                 }
>         }
> +
>         return -ENODEV;
>  }
>
> @@ -2470,16 +2490,13 @@ int find_hibernation_swap_type(dev_t device, sector_t offset)
>
>  int find_first_swap(dev_t *device)
>  {
> -       int type, ret = -ENODEV;
> +       int ret = -ENODEV;
> +       struct swap_info_struct *si;
>
>         spin_lock(&swap_lock);
> -       for (type = 0; type < nr_swapfiles; type++) {
> -               struct swap_info_struct *sis = swap_info[type];
> -
> -               if (!(sis->flags & SWP_WRITEOK))
> -                       continue;
> -               *device = sis->bdev->bd_dev;
> -               ret = type;
> +       for_each_avail_swap(si) {
> +               *device = si->bdev->bd_dev;
> +               ret = si->type;
>                 break;
>         }
>         spin_unlock(&swap_lock);
> @@ -2996,12 +3013,14 @@ static int try_to_unuse(unsigned int type)
>   */
>  static void drain_mmlist(void)
>  {
> +       struct swap_info_struct *si;
>         struct list_head *p, *next;
> -       unsigned int type;
>
> -       for (type = 0; type < nr_swapfiles; type++)
> -               if (swap_usage_in_pages(swap_info[type]))
> +       for_each_swap_info(si) {
> +               if (swap_usage_in_pages(si))
>                         return;
> +       }
> +
>         spin_lock(&mmlist_lock);
>         list_for_each_safe(p, next, &init_mm.mmlist)
>                 list_del_init(p);
> @@ -3997,14 +4016,14 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
>
>  void si_swapinfo(struct sysinfo *val)
>  {
> -       unsigned int type;
> +       struct swap_info_struct *si;
>         unsigned long nr_to_be_unused = 0;
>
>         spin_lock(&swap_lock);
> -       for (type = 0; type < nr_swapfiles; type++) {
> -               struct swap_info_struct *si = swap_info[type];
> +       for_each_swap_info(si) {
> +               unsigned long flags = READ_ONCE(si->flags);

Hmm. There is a READ_ONCE(si->flags) inside for_each_swap_info() as
well. Now you have two READ_ONCE(si->flags). How do you ensure that
flag between two READ_ONCE is always synced?

>
> -               if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
> +               if ((flags & (SWP_USED | SWP_WRITEOK)) == SWP_USED)

I think for_each_swap_info() requested flags as SWP_USED |
SWP_WRITEOK. Is this intent here to catch the race that SWP_WRITEOK
get dropped after the for_each_swap_info()?

Chris

>                         nr_to_be_unused += swap_usage_in_pages(si);
>         }
>         val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;

  reply	other threads:[~2026-11-26 17:26 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 14:15 [PATCH v5 00/19] mm/swap: introduce priority queues for swap device allocation Lian Wang
2026-11-23 14:16 ` [PATCH v5 01/19] mm/swap: remove unused parameter for reading swap header Lian Wang
2026-11-26  5:54   ` Chris Li
2026-11-23 14:16 ` [PATCH v5 02/19] mm/swap: clean up hibernation error handling Lian Wang
2026-11-26  5:59   ` Chris Li
2026-11-26 12:14     ` Lian Wang
2026-11-26 16:31       ` Chris Li
2026-11-23 14:16 ` [PATCH v5 03/19] mm/swap: cleanup and document swap device availability flag usage Lian Wang
2026-11-26  6:54   ` Chris Li
2026-11-26 12:30     ` Lian Wang
2026-11-23 14:16 ` [PATCH v5 04/19] mm/swap: introduce swap device iteration helper Lian Wang
2026-11-26 17:26   ` Chris Li [this message]
2026-11-23 14:16 ` [PATCH v5 05/19] mm/swap: change the swapon lock into a percpu rwsem Lian Wang
2026-11-26 18:03   ` Chris Li
2026-11-23 14:16 ` [PATCH v5 06/19] mm/swap: serialize swap publication before removing swapon mutex Lian Wang
2026-11-26 18:17   ` Chris Li
2026-11-23 14:17 ` [PATCH v5 07/19] mm/swap: consolidate swap inuse accounting helpers Lian Wang
2026-11-26 18:27   ` Chris Li
2026-11-23 14:17 ` [PATCH v5 08/19] mm/swap: change back to use each swap device's percpu cluster Lian Wang
2026-11-26 18:28   ` Chris Li
2026-11-23 14:18 ` [PATCH v5 09/19] mm/swap: avoid direct reclaim when populating swap cluster metadata Lian Wang
2026-11-26 18:34   ` Chris Li
2026-11-23 14:18 ` [PATCH v5 10/19] mm/swap: mask the off-list bit when checking whether swap is full Lian Wang
2026-11-26 20:09   ` Chris Li
2026-11-23 14:18 ` [PATCH v5 11/19] mm/swap: add priority queue for swap device allocation Lian Wang
2026-11-23 14:18 ` [PATCH v5 12/19] mm/swap: remove available list Lian Wang
2026-11-23 14:18 ` [PATCH v5 13/19] mm/swap: bound synchronous discard outside the allocation reader Lian Wang
2026-11-23 14:19 ` [PATCH v5 14/19] mm/swap: drop swap active plist Lian Wang
2026-11-23 14:19 ` [PATCH v5 15/19] lib/plist.c: remove requeue function Lian Wang
2026-11-23 14:19 ` [PATCH v5 16/19] mm/swap: reuse per-CPU queue readers across priority changes Lian Wang
2026-11-23 14:19 ` [PATCH v5 17/19] mm/swap: advance the global queue cursor directly between priority rings Lian Wang
2026-11-23 14:19 ` [PATCH v5 18/19] mm/swap: skip reader rotation for single-device " Lian Wang
2026-11-23 14:20 ` [PATCH v5 19/19] mm/swap: redistribute queue readers after membership changes 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=CACePvbU2cg1EVBjONWjYcsanDM0JS4eLXgX9aGYeEaXu4zZ2-w@mail.gmail.com \
    --to=chrisl@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baoquan.he@linux.dev \
    --cc=gustavoars@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kees@kernel.org \
    --cc=kunwu.chan@linux.dev \
    --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

all inboxes | Powered by JetHome®