From: Lian Wang <lianux.mm@gmail.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev,
baohua@kernel.org, youngjun.park@lge.com,
shikemeng@huaweicloud.com, kasong@tencent.com,
linjh22s@gmail.com, kunwu.chan@linux.dev, kees@kernel.org,
gustavoars@kernel.org, tglx@kernel.org,
linux-hardening@vger.kernel.org
Subject: [RFC PATCH v3 04/18] mm/swap: introduce swap device iteration helper
Date: Wed, 28 Oct 2026 00:05:13 +0800 [thread overview]
Message-ID: <f2d75ba96189520fe66a99ea9c7c0d9894b8ec22.1793106426.git.lianux.mm@gmail.com> (raw)
In-Reply-To: <cover.1793106426.git.lianux.mm@gmail.com>
From: Kairui Song <kasong@tencent.com>
There are many users that access the swap info array just to iterate
through the swap devices to find usable ones. Introduce a generic helper
for this to prepare for dropping the lock convention.
Also slightly adjust __swap_type_to_info to allow access of swapped off
devices by moving the sanity check into its only current caller, and this
way makes more sense too: the only caller is __swap_entry_to_info, where
we should never see an entry referring to a dead device.
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, 53 insertions(+), 36 deletions(-)
diff --git a/mm/swap.h b/mm/swap.h
index d5bf21f517dc..d46d09b8c21f 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -107,16 +107,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 4adcfbbb3730..b72f4fc3e811 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);
+ while (*i < nr_swapfiles) {
+ struct swap_info_struct *si = __swap_type_to_info(*i);
+
+ VM_WARN_ON(!si);
+ (*i)++;
+ if (flag && !((si->flags & flag) == flag))
+ continue;
+ return si;
+ }
+ return NULL;
+}
+
+#define __for_each_swap(si, flag) \
+ for (int __i = 0; ((si) = __swap_iter(&__i, flag));)
+
+/*
+ * for_each_swap - iterate through all allocated and inuse swap devices
+ * @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(si) __for_each_swap(si, SWP_USED)
+#define for_each_avail_swap(si) __for_each_swap(si, SWP_USED | SWP_WRITEOK)
+
static struct kmem_cache *swap_table_cachep;
/* Protects si->swap_file for /proc/swaps usage */
@@ -2229,26 +2257,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;
}
@@ -2415,16 +2435,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);
@@ -2931,12 +2948,14 @@ success:
*/
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(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);
@@ -3931,14 +3950,12 @@ out:
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];
-
- if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
+ for_each_swap(si) {
+ if (!(si->flags & SWP_WRITEOK))
nr_to_be_unused += swap_usage_in_pages(si);
}
val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
next prev parent reply other threads:[~2026-10-27 16:05 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 15:58 [RFC PATCH v3 00/18] mm/swap: introduce priority queues for swap device allocation Lian Wang
2026-10-27 16:03 ` [RFC PATCH v3 01/18] mm/swap: remove unused parameter for reading swap header Lian Wang
2026-10-27 16:03 ` [RFC PATCH v3 02/18] mm/swap: slightly cleanup the code for hibernation error handling Lian Wang
2026-10-27 16:04 ` [RFC PATCH v3 03/18] mm/swap: cleanup and document swap device availability flag usage Lian Wang
2026-10-27 16:05 ` Lian Wang [this message]
2026-10-27 16:05 ` [RFC PATCH v3 05/18] mm/swap: change the swapon lock into a percpu rwsem Lian Wang
2026-10-27 16:06 ` [RFC PATCH v3 06/18] mm/swap: serialize swap publication before removing swapon mutex Lian Wang
2026-10-27 16:07 ` [RFC PATCH v3 07/18] mm/swap: consolidate swap inuse accounting helpers Lian Wang
2026-10-27 16:07 ` [RFC PATCH v3 08/18] mm/swap: change back to use each swap device's percpu cluster Lian Wang
2026-10-27 16:08 ` [RFC PATCH v3 09/18] mm/swap: avoid direct reclaim when populating swap cluster metadata Lian Wang
2026-10-27 16:08 ` [RFC PATCH v3 10/18] mm/swap: add priority queue for swap device allocation Lian Wang
2026-10-27 16:09 ` [RFC PATCH v3 11/18] mm/swap: remove available list Lian Wang
2026-10-27 16:10 ` [RFC PATCH v3 12/18] mm/swap: bound synchronous discard during allocation Lian Wang
2026-10-27 16:11 ` [RFC PATCH v3 13/18] mm/swap: drop swap active plist Lian Wang
2026-10-27 16:11 ` [RFC PATCH v3 14/18] lib/plist.c: remove requeue function Lian Wang
2026-10-27 16:12 ` [RFC PATCH v3 15/18] mm/swap: reuse per-CPU queue readers across priority changes Lian Wang
2026-10-27 16:12 ` [RFC PATCH v3 16/18] mm/swap: advance the global queue cursor directly between priority rings Lian Wang
2026-10-27 16:13 ` [RFC PATCH v3 17/18] mm/swap: skip reader rotation for single-device " Lian Wang
2026-10-27 16:14 ` [RFC PATCH v3 18/18] 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=f2d75ba96189520fe66a99ea9c7c0d9894b8ec22.1793106426.git.lianux.mm@gmail.com \
--to=lianux.mm@gmail.com \
--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@linux.dev \
--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®