Skip to contentSkip to conversation

mm/swap: use swap_ops to register swap device's methods

PATCH In review Baoquan He 7 participants 25 messages Mar 2 – Mar 5

Subsystems (3)
lore
Share & tools
lore

Baoquan He

author
#0 lore raw

cover mm/swap: use swap_ops to register swap device's methods

This can simplify the code logic and benefit any new type of swap device added later.

And also do renaming in this patchset:
-------
file renaming:
---
mm/page_io.c to mm/swap_io.c

function renaming:
---
swap_writepage_* to swap_write_folio_* in file mm/swap_io.c

Baoquan He (3):
mm/swap: rename mm/page_io.c to mm/swap_io.c mm/swap: use swap_ops to register swap device's methods mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

 MAINTAINERS                 |   2 +-
 include/linux/swap.h        |  13 +++++
 mm/Makefile                 |   2 +-
 mm/swap.h                   |   3 +-
 mm/{page_io.c => swap_io.c} | 104 +++++++++++++++++++++---------------
 mm/swapfile.c               |   2 +
 mm/zswap.c                  |   3 +-
 7 files changed, 80 insertions(+), 49 deletions(-)
 rename mm/{page_io.c => swap_io.c} (91%)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH 1/3 mm/swap: rename mm/page_io.c to mm/swap_io.c

Codes in mm/page_io.c are only related to swap io, it has nothing to do with other page io.

Rename it to avoid confusion.

---
 MAINTAINERS                 | 2 +-
 mm/Makefile                 | 2 +-
 mm/swap.h                   | 2 +-
 mm/{page_io.c => swap_io.c} | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
 rename mm/{page_io.c => swap_io.c} (99%)
MAINTAINERS +1 -1
index 14899f1de77e..6ff65f8bc27b 100644
@@ -16945,7 +16945,7 @@ F:	Documentation/mm/swap-table.rst
 F:	include/linux/swap.h
 F:	include/linux/swapfile.h
 F:	include/linux/swapops.h
-F:	mm/page_io.c
+F:	mm/swap_io.c
 F:	mm/swap.c
 F:	mm/swap.h
 F:	mm/swap_table.h
mm/Makefile +1 -1
index 8ad2ab08244e..a65ac900096a 100644
@@ -75,7 +75,7 @@ ifdef CONFIG_MMU
 	obj-$(CONFIG_ADVISE_SYSCALLS)	+= madvise.o
 endif
 
-obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o
+obj-$(CONFIG_SWAP)	+= swap_io.o swap_state.o swapfile.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o
mm/swap.h +1 -1
index a77016f2423b..161185057993 100644
@@ -214,7 +214,7 @@ extern void __swap_cluster_free_entries(struct swap_info_struct *si,
 					struct swap_cluster_info *ci,
 					unsigned int ci_off, unsigned int nr_pages);
 
-/* linux/mm/page_io.c */
+/* linux/mm/swap_io.c */
 int sio_pool_init(void);
 struct swap_iocb;
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug);
mm/swap_io.c renamed from mm/page_io.c +1 -1
similarity index 99%
rename from mm/page_io.c
rename to mm/swap_io.c
index a2c034660c80..d1cdb10ba133 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- *  linux/mm/page_io.c
+ *  linux/mm/swap_io.c
  *
  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
  *
Signature · 1 line
2.52.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] mm/swap: rename mm/page_io.c to mm/swap_io.c

Quoted 3 lines · Baoquan He

Codes in mm/page_io.c are only related to swap io, it has nothing to do with other page io.

That is true. Meanwhile, swap.c and swap.h contain a fair amount of non-swap-related code, such as lru_xxx and even file-related functions like deactivate_file_folio().

Perhaps we should consider moving these to more
appropriate locations.

Quoted 4 lines

Rename it to avoid confusion.

author
#3 lore raw

Re: [PATCH 1/3] mm/swap: rename mm/page_io.c to mm/swap_io.c

Quoted 5 lines · Barry Song
Quoted 3 lines · Baoquan He

Codes in mm/page_io.c are only related to swap io, it has nothing to do with other page io.

Thanks for quick and careful reviewing on this patchset, Barry.

Quoted 6 lines

That is true. Meanwhile, swap.c and swap.h contain a fair amount of non-swap-related code, such as lru_xxx and even file-related functions like deactivate_file_folio().

Perhaps we should consider moving these to more
appropriate locations.

Yeah, agree. I will see if anyone else want to do the tidying up, otherwise I can do it later.

Quoted 8 lines
Quoted 5 lines

Rename it to avoid confusion.

maintainer Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] mm/swap: rename mm/page_io.c to mm/swap_io.c

In reply to Baoquan He
Quoted 6 lines · Baoquan He

Codes in mm/page_io.c are only related to swap io, it has nothing to do with other page io.

Rename it to avoid confusion.

I wonder if there's any historical context here. But regardless:

author
#5 lore raw

Re: [PATCH 1/3] mm/swap: rename mm/page_io.c to mm/swap_io.c

Quoted 9 lines · Nhat Pham
Quoted 6 lines · Baoquan He

Codes in mm/page_io.c are only related to swap io, it has nothing to do with other page io.

Rename it to avoid confusion.

I wonder if there's any historical context here. But regardless:

I traced it to 1.3.57, its name is mm/page_io.c and included only function rw_swap_page(). Linus named it at that time. No further historical context.

commit ecda87c064fea1d7322a357b78b99e04594bc6f3 (tag: 1.3.57)

Author: Linus Torvalds <[email protected]>
Date:   Fri Nov 23 15:10:30 2007 -0500
    Import 1.3.57
Quoted 3 lines
author
#7 lore raw

PATCH 2/3 mm/swap: use swap_ops to register swap device's methods

In reply to Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +13 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {
+	void (*read_folio)(struct swap_info_struct *sis,
+			   struct folio *folio,
+			   struct swap_iocb **plug);
+	void (*write_folio)(struct swap_info_struct *sis,
+			    struct folio *folio,
+			    struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);
+
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
 	struct work_struct reclaim_work; /* reclaim worker */
 	struct list_head discard_clusters; /* discard clusters list */
 	struct plist_node avail_list;   /* entry in swap_avail_head */
+	struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +59 -43

171 lines not on this page · raw message

mm/swapfile.c +2 -0
index 915bc93964db..af498f9af328 100644
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	/* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
 	enable_swap_info(si);
 
+	probe_swap_fs(si);
+
 	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,
 		K((unsigned long long)span),
mm/zswap.c +2 -1
index a399f7a10830..7ce906249c7a 100644
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	folio_set_reclaim(folio);
 
 	/* start writeback */
-	__swap_writepage(folio, NULL);
+	if (si->ops && si->ops->write_folio)
+		si->ops->write_folio(si, folio, NULL);
 
 out:
 	if (ret && ret != -EEXIST) {
Signature · 1 line
2.52.0
#8 lore raw
Quoted 34 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 

include/linux/swap.h enum { · line 235

Quoted 10 lines
include/linux/swap.h +10 -0
+struct swap_ops {
+       void (*read_folio)(struct swap_info_struct *sis,
+                          struct folio *folio,
+                          struct swap_iocb **plug);
+       void (*write_folio)(struct swap_info_struct *sis,
+                           struct folio *folio,
+                           struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);

Does probe_swap_fs sound a bit odd?
What about init_swap_ops? Not sure if we have a better name.

Do we really want it, along with swap_ops, to live in include/linux/swap.h? Could it be placed in mm/swap.h instead?

Quoted 37 lines
include/linux/swap.h +2 -0
+
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
        struct work_struct reclaim_work; /* reclaim worker */
        struct list_head discard_clusters; /* discard clusters list */
        struct plist_node avail_list;   /* entry in swap_avail_head */
+       struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +1 -0
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
        int ret = 0;
+       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
        if (folio_free_swap(folio))
                goto out_unlock;

mm/swap_io.c int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) · line 286

Quoted 6 lines
mm/swap_io.c +2 -1
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
                return AOP_WRITEPAGE_ACTIVATE;
        }
 
-       __swap_writepage(folio, swap_plug);
+       if (sis->ops && sis->ops->write_folio)
+               sis->ops->write_folio(sis, folio, swap_plug);

Do we want a swap_write_folio() wrapper?

Quoted 109 lines
mm/swap_io.c +18 -35
        return 0;
 out_unlock:
        folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
        mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+                             struct folio *folio,
+                             struct swap_iocb **swap_plug)
 {
        struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct file *swap_file = sis->swap_file;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
                *swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+                                    struct folio *folio,
+                                    struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
        __end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-       VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_FS_OPS, so the data_race
-        * is safe.
-        */
-       if (data_race(sis->flags & SWP_FS_OPS))
-               swap_writepage_fs(folio, swap_plug);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-        * is safe.
-        */
-       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-               swap_writepage_bdev_sync(folio, sis);
-       else
-               swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
        struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
        return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+                              struct folio *folio,
+                              struct swap_iocb **plug)
 {
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct swap_iocb *sio = NULL;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
                *plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
        put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+                                      struct folio *folio,
+                                      struct swap_iocb **plug)
 {
        struct bio *bio;
 

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 597

Quoted 7 lines
mm/swap_io.c +4 -0
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
+static struct swap_ops bdev_fs_swap_ops = {
+       .read_folio = swap_read_folio_fs,
+       .write_folio = swap_writepage_fs,
+};

const?

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 602

Quoted 5 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_sync_swap_ops = {
+       .read_folio = swap_read_folio_bdev_sync,
+       .write_folio = swap_writepage_bdev_sync,
+};

const?

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 607

Quoted 5 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_async_swap_ops = {
+       .read_folio = swap_read_folio_bdev_async,
+       .write_folio = swap_writepage_bdev_async,
+};

const?

Quoted 46 lines
mm/swap_io.c +24 -7
+
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_FS_OPS, so the data_race
+        * is safe.
+        */
+       if (data_race(sis->flags & SWP_FS_OPS))
+               sis->ops = &bdev_fs_swap_ops;
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+        * is safe.
+        */
+       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+               sis->ops = &bdev_sync_swap_ops;
+       else
+               sis->ops = &bdev_async_swap_ops;
+       return 0;
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
        /* We have to read from slower devices. Increase zswap protection. */
        zswap_folio_swapin(folio);
 
-       if (data_race(sis->flags & SWP_FS_OPS)) {
-               swap_read_folio_fs(folio, plug);
-       } else if (synchronous) {
-               swap_read_folio_bdev_sync(folio, sis);
-       } else {
-               swap_read_folio_bdev_async(folio, sis);
-       }
+       if (sis->ops && sis->ops->read_folio)
+               sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 915bc93964db..af498f9af328 100644

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3628

Quoted 4 lines
mm/swapfile.c +1 -0
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
        enable_swap_info(si);
 
+       probe_swap_fs(si);

Can we move this to enable_swap_info(), or perhaps even deeper into setup_swap_info()?

Quoted 9 lines
mm/swapfile.c +1 -0
+
        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,
                K((unsigned long long)span),
mm/zswap.c +0 -0
index a399f7a10830..7ce906249c7a 100644

mm/zswap.c static int zswap_writeback_entry(struct zswap_entry *entry, · line 1059

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       if (si->ops && si->ops->write_folio)
+               si->ops->write_folio(si, folio, NULL);

swap_write_folio() inline wrapper?

Quoted 6 lines
mm/zswap.c +0 -1
 out:
        if (ret && ret != -EEXIST) {
--

2.52.0

Thanks
Barry

author
#9 lore raw
Quoted 35 lines · Barry Song
Quoted 34 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
Quoted 13 lines

include/linux/swap.h enum { · line 235

Quoted 10 lines
include/linux/swap.h +10 -0
+struct swap_ops {
+       void (*read_folio)(struct swap_info_struct *sis,
+                          struct folio *folio,
+                          struct swap_iocb **plug);
+       void (*write_folio)(struct swap_info_struct *sis,
+                           struct folio *folio,
+                           struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);

Does probe_swap_fs sound a bit odd?
What about init_swap_ops? Not sure if we have a better name.

Both is fine to me, if no other suggestion, I can take init_swap_ops() instead.

Quoted 3 lines

Do we really want it, along with swap_ops, to live in include/linux/swap.h? Could it be placed in mm/swap.h instead?

You are right, putting it into mm/swap.h sounds better.

Quoted 41 lines
Quoted 41 lines
include/linux/swap.h +2 -0
+
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
        struct work_struct reclaim_work; /* reclaim worker */
        struct list_head discard_clusters; /* discard clusters list */
        struct plist_node avail_list;   /* entry in swap_avail_head */
+       struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +1 -0
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
        int ret = 0;
+       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
        if (folio_free_swap(folio))
                goto out_unlock;
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
                return AOP_WRITEPAGE_ACTIVATE;
        }
 
Quoted 5 lines

mm/swap_io.c int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) · line 286

Quoted 3 lines
mm/swap_io.c +2 -1
-       __swap_writepage(folio, swap_plug);
+       if (sis->ops && sis->ops->write_folio)
+               sis->ops->write_folio(sis, folio, swap_plug);

Do we want a swap_write_folio() wrapper?

Seems swap_writeout() is the counterpart of swap_read_folio(). I personally prefer to rename swap_writeout() to swap_write_folio(). And make a wrapper as _swap_write_folio()?

Quoted 113 lines
Quoted 113 lines
mm/swap_io.c +18 -35
        return 0;
 out_unlock:
        folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
        mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+                             struct folio *folio,
+                             struct swap_iocb **swap_plug)
 {
        struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct file *swap_file = sis->swap_file;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
                *swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+                                    struct folio *folio,
+                                    struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
        __end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-       VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_FS_OPS, so the data_race
-        * is safe.
-        */
-       if (data_race(sis->flags & SWP_FS_OPS))
-               swap_writepage_fs(folio, swap_plug);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-        * is safe.
-        */
-       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-               swap_writepage_bdev_sync(folio, sis);
-       else
-               swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
        struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
        return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+                              struct folio *folio,
+                              struct swap_iocb **plug)
 {
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct swap_iocb *sio = NULL;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
                *plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
        put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+                                      struct folio *folio,
+                                      struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
Quoted 6 lines

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 597

Quoted 4 lines
mm/swap_io.c +4 -0
+static struct swap_ops bdev_fs_swap_ops = {
+       .read_folio = swap_read_folio_fs,
+       .write_folio = swap_writepage_fs,
+};

const?

Right, will fix in v2.

Quoted 82 lines

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 602

Quoted 6 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_sync_swap_ops = {
+       .read_folio = swap_read_folio_bdev_sync,
+       .write_folio = swap_writepage_bdev_sync,
+};

const?

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 607

Quoted 5 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_async_swap_ops = {
+       .read_folio = swap_read_folio_bdev_async,
+       .write_folio = swap_writepage_bdev_async,
+};

const?

Quoted 46 lines
mm/swap_io.c +24 -7
+
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_FS_OPS, so the data_race
+        * is safe.
+        */
+       if (data_race(sis->flags & SWP_FS_OPS))
+               sis->ops = &bdev_fs_swap_ops;
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+        * is safe.
+        */
+       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+               sis->ops = &bdev_sync_swap_ops;
+       else
+               sis->ops = &bdev_async_swap_ops;
+       return 0;
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
        /* We have to read from slower devices. Increase zswap protection. */
        zswap_folio_swapin(folio);
 
-       if (data_race(sis->flags & SWP_FS_OPS)) {
-               swap_read_folio_fs(folio, plug);
-       } else if (synchronous) {
-               swap_read_folio_bdev_sync(folio, sis);
-       } else {
-               swap_read_folio_bdev_async(folio, sis);
-       }
+       if (sis->ops && sis->ops->read_folio)
+               sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 915bc93964db..af498f9af328 100644

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3628

Quoted 4 lines
mm/swapfile.c +1 -0
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
        enable_swap_info(si);
 
+       probe_swap_fs(si);

Can we move this to enable_swap_info(), or perhaps even deeper into setup_swap_info()?

Quoted 11 lines
mm/swapfile.c +1 -0
+
        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,
                K((unsigned long long)span),
mm/zswap.c +0 -0
index a399f7a10830..7ce906249c7a 100644
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
Quoted 6 lines

mm/zswap.c static int zswap_writeback_entry(struct zswap_entry *entry, · line 1059

Quoted 4 lines
mm/zswap.c +2 -1
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       if (si->ops && si->ops->write_folio)
+               si->ops->write_folio(si, folio, NULL);

swap_write_folio() inline wrapper?

Replied at above place. We will need a read_folio version too.

maintainer
#10 lore raw
In reply to Barry Song
Quoted 6 lines · Barry Song
Quoted 4 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Quoted 6 lines
Quoted 6 lines

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

Thanks for sending this series out for discussion.

Quoted 24 lines
Quoted 24 lines
---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
Quoted 13 lines

include/linux/swap.h enum { · line 235

Quoted 10 lines
include/linux/swap.h +10 -0
+struct swap_ops {
+       void (*read_folio)(struct swap_info_struct *sis,
+                          struct folio *folio,
+                          struct swap_iocb **plug);
+       void (*write_folio)(struct swap_info_struct *sis,
+                           struct folio *folio,
+                           struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);

Does probe_swap_fs sound a bit odd?
What about init_swap_ops? Not sure if we have a better name.

We can also call probe_swap_header(), which effectively detects which implementation of the swap device operations the header requesting.

Quoted 2 lines

Do we really want it, along with swap_ops, to live in include/linux/swap.h? Could it be placed in mm/swap.h instead?

That is a good point. The mm/swap.h is likely better (if possible) for the swap_ops related declaration because all those ops are internal swap behavior. We can move out later if there is a reason.

Quoted 41 lines
Quoted 41 lines
include/linux/swap.h +2 -0
+
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
        struct work_struct reclaim_work; /* reclaim worker */
        struct list_head discard_clusters; /* discard clusters list */
        struct plist_node avail_list;   /* entry in swap_avail_head */
+       struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +1 -0
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
        int ret = 0;
+       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
        if (folio_free_swap(folio))
                goto out_unlock;
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
                return AOP_WRITEPAGE_ACTIVATE;
        }
 
Quoted 5 lines

mm/swap_io.c int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) · line 286

Quoted 3 lines
mm/swap_io.c +2 -1
-       __swap_writepage(folio, swap_plug);
+       if (sis->ops && sis->ops->write_folio)
+               sis->ops->write_folio(sis, folio, swap_plug);

Do we want a swap_write_folio() wrapper?

I vote that it is better to keep these two lines as they are. No wrapper is needed. Is that wrapper used in only two places? Also we have too many swap_write_page/folio-like functions. Adding one doesn't provide enough value, it is likely causing more energy to distingish with the other similar functions.

Quoted 200 lines
Quoted 109 lines
mm/swap_io.c +18 -35
        return 0;
 out_unlock:
        folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
        mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+                             struct folio *folio,
+                             struct swap_iocb **swap_plug)
 {
        struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct file *swap_file = sis->swap_file;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
                *swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+                                    struct folio *folio,
+                                    struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
        __end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-       VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_FS_OPS, so the data_race
-        * is safe.
-        */
-       if (data_race(sis->flags & SWP_FS_OPS))
-               swap_writepage_fs(folio, swap_plug);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-        * is safe.
-        */
-       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-               swap_writepage_bdev_sync(folio, sis);
-       else
-               swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
        struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
        return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+                              struct folio *folio,
+                              struct swap_iocb **plug)
 {
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct swap_iocb *sio = NULL;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
                *plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
        put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+                                      struct folio *folio,
+                                      struct swap_iocb **plug)
 {
        struct bio *bio;
 

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 597

Quoted 7 lines
mm/swap_io.c +4 -0
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
+static struct swap_ops bdev_fs_swap_ops = {
+       .read_folio = swap_read_folio_fs,
+       .write_folio = swap_writepage_fs,
+};

const?

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 602

Quoted 5 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_sync_swap_ops = {
+       .read_folio = swap_read_folio_bdev_sync,
+       .write_folio = swap_writepage_bdev_sync,
+};

const?

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 607

Quoted 5 lines
mm/swap_io.c +5 -0
+
+static struct swap_ops bdev_async_swap_ops = {
+       .read_folio = swap_read_folio_bdev_async,
+       .write_folio = swap_writepage_bdev_async,
+};

const?

Quoted 46 lines
mm/swap_io.c +24 -7
+
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_FS_OPS, so the data_race
+        * is safe.
+        */
+       if (data_race(sis->flags & SWP_FS_OPS))
+               sis->ops = &bdev_fs_swap_ops;
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+        * is safe.
+        */
+       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+               sis->ops = &bdev_sync_swap_ops;
+       else
+               sis->ops = &bdev_async_swap_ops;
+       return 0;
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
        /* We have to read from slower devices. Increase zswap protection. */
        zswap_folio_swapin(folio);
 
-       if (data_race(sis->flags & SWP_FS_OPS)) {
-               swap_read_folio_fs(folio, plug);
-       } else if (synchronous) {
-               swap_read_folio_bdev_sync(folio, sis);
-       } else {
-               swap_read_folio_bdev_async(folio, sis);
-       }
+       if (sis->ops && sis->ops->read_folio)
+               sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 915bc93964db..af498f9af328 100644

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3628

Quoted 4 lines
mm/swapfile.c +1 -0
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
        enable_swap_info(si);
 
+       probe_swap_fs(si);

Can we move this to enable_swap_info(), or perhaps even deeper into setup_swap_info()?

Quoted 11 lines
mm/swapfile.c +1 -0
+
        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,
                K((unsigned long long)span),
mm/zswap.c +0 -0
index a399f7a10830..7ce906249c7a 100644
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
Quoted 6 lines

mm/zswap.c static int zswap_writeback_entry(struct zswap_entry *entry, · line 1059

Quoted 4 lines
mm/zswap.c +2 -1
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       if (si->ops && si->ops->write_folio)
+               si->ops->write_folio(si, folio, NULL);

swap_write_folio() inline wrapper?

Again, I think keeping it as is is fine.

Chris

reviewer
#11 lore raw
In reply to Baoquan He

On Mon, Mar 02, 2026 at 06:40:15PM +0800, Baoquan He wrote:

Hello Baoquan!

...

Quoted 13 lines
mm/swap_io.c +13 -0
+	.read_folio = swap_read_folio_bdev_sync,
+	.write_folio = swap_writepage_bdev_sync,
+};
+
+static struct swap_ops bdev_async_swap_ops = {
+	.read_folio = swap_read_folio_bdev_async,
+	.write_folio = swap_writepage_bdev_async,
+};
+
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 622

Quoted 10 lines
mm/swap_io.c +10 -0
+	 * but that will never affect SWP_FS_OPS, so the data_race
+	 * is safe.
+	 */
+	if (data_race(sis->flags & SWP_FS_OPS))
+		sis->ops = &bdev_fs_swap_ops;
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),
+	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+	 * is safe.
+	 */

The reference to scan_swap_map_slots appears to be outdated as the function no longer exists in the current codebase. It might be better to update this to a more generic term?

If comments are needed to be updated,
Similar comments in may_enter_fs likely need a similar update for consistency.

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 627

Quoted 5 lines
mm/swap_io.c +5 -0
+	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+		sis->ops = &bdev_sync_swap_ops;
+	else
+		sis->ops = &bdev_async_swap_ops;
+	return 0;

it seems return value is not needed.

Thanks
Youngjun Park

reviewer
#12 lore raw
In reply to Baoquan He
Quoted 31 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)

include/linux/swap.h enum { · line 233

Quoted 10 lines
include/linux/swap.h +8 -0
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {
+	void (*read_folio)(struct swap_info_struct *sis,
+			   struct folio *folio,
+			   struct swap_iocb **plug);
+	void (*write_folio)(struct swap_info_struct *sis,
+			    struct folio *folio,
+			    struct swap_iocb **plug);
+};

I think swap_iocb is only required for fs-swap
(swap_folio_read_fs/swap_folio_write_fs).

If the goal is to support fs-swap through swap_ops, it might be worth considering a more complete integration, including activate/deactivate and swap_rw from aops, rather than only adding read/write hooks.

So.. we could keep SWP_FS_OPS as-is for now and just split sync/async paths, and revisit a cleaner fs-swap integration later. (I mean removing fs ops, and call swap_read/write_folio_fs on sync/async ops.)

How do you think?

Thanks
Youngjun Park

maintainer
#13 lore raw
Quoted 47 lines · YoungJun Park
Quoted 31 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)

include/linux/swap.h enum { · line 233

Quoted 10 lines
include/linux/swap.h +8 -0
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {
+     void (*read_folio)(struct swap_info_struct *sis,
+                        struct folio *folio,
+                        struct swap_iocb **plug);
+     void (*write_folio)(struct swap_info_struct *sis,
+                         struct folio *folio,
+                         struct swap_iocb **plug);
+};

I think swap_iocb is only required for fs-swap
(swap_folio_read_fs/swap_folio_write_fs).

Quoted 1 line

If the goal is to support fs-swap through swap_ops, it might be worth

Consider this series as a starting point for discussion. Nothing is set to stone.

Quoted 2 lines

considering a more complete integration, including activate/deactivate and swap_rw from aops, rather than only adding read/write hooks.

Can be add as follow up patches to introduce active/deactive and swap_rw from aops. In fact I want to see it as incremental patches rather than add every possible swap_ops in one go. It is likely easier to review as well.

Quoted 4 lines

So.. we could keep SWP_FS_OPS as-is for now and just split sync/async paths, and revisit a cleaner fs-swap integration later. (I mean removing fs ops, and call swap_read/write_folio_fs on sync/async ops.)

You can propose incremental patches to add your additional swap ops and into the series as the later iteration.
The sync/async split is just a MVP to introduce the first step of the swap_ops.

Chris

author
#14 lore raw
Quoted 53 lines · Chris Li
Quoted 47 lines · YoungJun Park
Quoted 31 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)

include/linux/swap.h enum { · line 233

Quoted 10 lines
include/linux/swap.h +8 -0
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {
+     void (*read_folio)(struct swap_info_struct *sis,
+                        struct folio *folio,
+                        struct swap_iocb **plug);
+     void (*write_folio)(struct swap_info_struct *sis,
+                         struct folio *folio,
+                         struct swap_iocb **plug);
+};

I think swap_iocb is only required for fs-swap
(swap_folio_read_fs/swap_folio_write_fs).

Quoted 1 line

If the goal is to support fs-swap through swap_ops, it might be worth

Consider this series as a starting point for discussion. Nothing is set to stone.

Quoted 7 lines
Quoted 2 lines

considering a more complete integration, including activate/deactivate and swap_rw from aops, rather than only adding read/write hooks.

Can be add as follow up patches to introduce active/deactive and swap_rw from aops. In fact I want to see it as incremental patches rather than add every possible swap_ops in one go. It is likely easier to review as well.

Ack. And thanks for bring this up to Chris, I should add Chris's Suggested-by.

Quoted 8 lines
Quoted 4 lines

So.. we could keep SWP_FS_OPS as-is for now and just split sync/async paths, and revisit a cleaner fs-swap integration later. (I mean removing fs ops, and call swap_read/write_folio_fs on sync/async ops.)

You can propose incremental patches to add your additional swap ops and into the series as the later iteration.
The sync/async split is just a MVP to introduce the first step of the swap_ops.

Agree.

#15 lore raw
In reply to Baoquan He
Quoted 34 lines · On Mon, 2 Mar 2026 18:40:15 +0800 Baoquan He <[email protected]> wrote:

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +2 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {

include/linux/swap.h enum { · line 236

Quoted 10 lines
include/linux/swap.h +10 -0
+	void (*read_folio)(struct swap_info_struct *sis,
+			   struct folio *folio,
+			   struct swap_iocb **plug);
+	void (*write_folio)(struct swap_info_struct *sis,
+			    struct folio *folio,
+			    struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);
+

Would it be better to put these in mm/swap.h as they are only used in mm/?

Quoted 36 lines
include/linux/swap.h +1 -0
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
 	struct work_struct reclaim_work; /* reclaim worker */
 	struct list_head discard_clusters; /* discard clusters list */
 	struct plist_node avail_list;   /* entry in swap_avail_head */
+	struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +1 -0
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
 	int ret = 0;
+	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
 	if (folio_free_swap(folio))
 		goto out_unlock;

mm/swap_io.c int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) · line 286

Quoted 6 lines
mm/swap_io.c +2 -1
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 		return AOP_WRITEPAGE_ACTIVATE;
 	}
 
-	__swap_writepage(folio, swap_plug);
+	if (sis->ops && sis->ops->write_folio)
+		sis->ops->write_folio(sis, folio, swap_plug);

The old __swap_writepage() always dispatched to one of the three write functions unconditionally. If the guard condition is false (ops is NULL), swap_writeout() returns 0 (success) but the folio is never unlocked -- the write functions are the ones that call folio_unlock(). Would this leave the folio locked and lead to a deadlock? Similar issue in swap_read_folio.

Quoted 117 lines
mm/swap_io.c +23 -35
 	return 0;
 out_unlock:
 	folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+			      struct folio *folio,
+			      struct swap_iocb **swap_plug)
 {
 	struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 	struct file *swap_file = sis->swap_file;
 	loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
 		*swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+				     struct folio *folio,
+				     struct swap_iocb **plug)
 {
 	struct bio_vec bv;
 	struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
 	__end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+				      struct folio *folio,
+				      struct swap_iocb **plug)
 {
 	struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
 	submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-	/*
-	 * ->flags can be updated non-atomically (scan_swap_map_slots),
-	 * but that will never affect SWP_FS_OPS, so the data_race
-	 * is safe.
-	 */
-	if (data_race(sis->flags & SWP_FS_OPS))
-		swap_writepage_fs(folio, swap_plug);
-	/*
-	 * ->flags can be updated non-atomically (scan_swap_map_slots),
-	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-	 * is safe.
-	 */
-	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-		swap_writepage_bdev_sync(folio, sis);
-	else
-		swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
 	struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 	return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+			       struct folio *folio,
+			       struct swap_iocb **plug)
 {
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 	struct swap_iocb *sio = NULL;
 	loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
 		*plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+				      struct folio *folio,
+				      struct swap_iocb **plug)
 {
 	struct bio_vec bv;
 	struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
 	put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+				       struct folio *folio,
+				       struct swap_iocb **plug)
 {
 	struct bio *bio;
 
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
 	submit_bio(bio);
 }
 
+static struct swap_ops bdev_fs_swap_ops = {
+	.read_folio = swap_read_folio_fs,
+	.write_folio = swap_writepage_fs,
+};
+

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 608

Quoted 10 lines
mm/swap_io.c +10 -0
+static struct swap_ops bdev_sync_swap_ops = {
+	.read_folio = swap_read_folio_bdev_sync,
+	.write_folio = swap_writepage_bdev_sync,
+};
+
+static struct swap_ops bdev_async_swap_ops = {
+	.read_folio = swap_read_folio_bdev_async,
+	.write_folio = swap_writepage_bdev_async,
+};
+

Should we have all of these as static const struct swap_ops?

Quoted 9 lines
mm/swap_io.c +9 -0
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),
+	 * but that will never affect SWP_FS_OPS, so the data_race
+	 * is safe.
+	 */
+	if (data_race(sis->flags & SWP_FS_OPS))
+		sis->ops = &bdev_fs_swap_ops;

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 627

Quoted 10 lines
mm/swap_io.c +10 -0
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),
+	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+	 * is safe.
+	 */
+	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+		sis->ops = &bdev_sync_swap_ops;
+	else
+		sis->ops = &bdev_async_swap_ops;
+	return 0;

The return is always 0, so this function could be void.

Quoted 26 lines
mm/swap_io.c +4 -7
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	/* We have to read from slower devices. Increase zswap protection. */
 	zswap_folio_swapin(folio);
 
-	if (data_race(sis->flags & SWP_FS_OPS)) {
-		swap_read_folio_fs(folio, plug);
-	} else if (synchronous) {
-		swap_read_folio_bdev_sync(folio, sis);
-	} else {
-		swap_read_folio_bdev_async(folio, sis);
-	}
+	if (sis->ops && sis->ops->read_folio)
+		sis->ops->read_folio(sis, folio, plug);
 
 finish:
 	if (workingset) {
mm/swapfile.c +0 -0
index 915bc93964db..af498f9af328 100644

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3629

Quoted 5 lines
mm/swapfile.c +2 -0
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	/* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
 	enable_swap_info(si);
 
+	probe_swap_fs(si);
+

Should probe_swap_fs() be called before enable_swap_info() rather than after it? enable_swap_info() sets SWP_WRITEOK and adds the device to swap_active_head, making it available for allocation. At that point si->ops is still NULL. If another CPU allocates swap from the new device and reclaim writes to it before probe_swap_fs() runs, the write will be silently dropped.

Quoted 19 lines
mm/swapfile.c +0 -0
 	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,
 		K((unsigned long long)span),
mm/zswap.c +2 -1
index a399f7a10830..7ce906249c7a 100644
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	folio_set_reclaim(folio);
 
 	/* start writeback */
-	__swap_writepage(folio, NULL);
+	if (si->ops && si->ops->write_folio)
+		si->ops->write_folio(si, folio, NULL);
 
 out:
 	if (ret && ret != -EEXIST) {
Signature · 1 line
2.52.0
author
#16 lore raw
Quoted 35 lines · Usama Arif
Quoted 33 lines · On Mon, 2 Mar 2026 18:40:15 +0800 Baoquan He <[email protected]> wrote:

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +1 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
Quoted 13 lines

include/linux/swap.h enum { · line 236

Quoted 11 lines
include/linux/swap.h +11 -0
+struct swap_ops {
+	void (*read_folio)(struct swap_info_struct *sis,
+			   struct folio *folio,
+			   struct swap_iocb **plug);
+	void (*write_folio)(struct swap_info_struct *sis,
+			    struct folio *folio,
+			    struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);
+

Would it be better to put these in mm/swap.h as they are only used in mm/?

Right, other reviewers also pointed this out. Will change in v2.

Quoted 40 lines
Quoted 40 lines
include/linux/swap.h +1 -0
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
 	struct work_struct reclaim_work; /* reclaim worker */
 	struct list_head discard_clusters; /* discard clusters list */
 	struct plist_node avail_list;   /* entry in swap_avail_head */
+	struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +1 -0
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
 	int ret = 0;
+	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
 	if (folio_free_swap(folio))
 		goto out_unlock;
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 		return AOP_WRITEPAGE_ACTIVATE;
 	}
 
Quoted 9 lines

mm/swap_io.c int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug) · line 286

Quoted 3 lines
mm/swap_io.c +2 -1
-	__swap_writepage(folio, swap_plug);
+	if (sis->ops && sis->ops->write_folio)
+		sis->ops->write_folio(sis, folio, swap_plug);

The old __swap_writepage() always dispatched to one of the three write functions unconditionally. If the guard condition is false (ops is NULL), swap_writeout() returns 0 (success) but the folio is never unlocked -- the write functions are the ones that call folio_unlock(). Would this leave the folio locked and lead to a deadlock? Similar issue in swap_read_folio.

Hmm, for now NULL sis->ops won't happen. But we could have it in the future, means there could be a swap device w/o read/write_folio methods.

Quoted 122 lines
Quoted 122 lines
mm/swap_io.c +27 -35
 	return 0;
 out_unlock:
 	folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+			      struct folio *folio,
+			      struct swap_iocb **swap_plug)
 {
 	struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 	struct file *swap_file = sis->swap_file;
 	loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
 		*swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+				     struct folio *folio,
+				     struct swap_iocb **plug)
 {
 	struct bio_vec bv;
 	struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
 	__end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+				      struct folio *folio,
+				      struct swap_iocb **plug)
 {
 	struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
 	submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-	/*
-	 * ->flags can be updated non-atomically (scan_swap_map_slots),
-	 * but that will never affect SWP_FS_OPS, so the data_race
-	 * is safe.
-	 */
-	if (data_race(sis->flags & SWP_FS_OPS))
-		swap_writepage_fs(folio, swap_plug);
-	/*
-	 * ->flags can be updated non-atomically (scan_swap_map_slots),
-	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-	 * is safe.
-	 */
-	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-		swap_writepage_bdev_sync(folio, sis);
-	else
-		swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
 	struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
 	return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+			       struct folio *folio,
+			       struct swap_iocb **plug)
 {
-	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 	struct swap_iocb *sio = NULL;
 	loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
 		*plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+				      struct folio *folio,
+				      struct swap_iocb **plug)
 {
 	struct bio_vec bv;
 	struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
 	put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-		struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+				       struct folio *folio,
+				       struct swap_iocb **plug)
 {
 	struct bio *bio;
 
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
 	submit_bio(bio);
 }
 
+static struct swap_ops bdev_fs_swap_ops = {
+	.read_folio = swap_read_folio_fs,
+	.write_folio = swap_writepage_fs,
+};
+
+static struct swap_ops bdev_sync_swap_ops = {
+	.read_folio = swap_read_folio_bdev_sync,
+	.write_folio = swap_writepage_bdev_sync,
+};
Quoted 8 lines

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 608

Quoted 6 lines
mm/swap_io.c +6 -0
+
+static struct swap_ops bdev_async_swap_ops = {
+	.read_folio = swap_read_folio_bdev_async,
+	.write_folio = swap_writepage_bdev_async,
+};
+

Should we have all of these as static const struct swap_ops?

You are right, I will fix them in v2.

Quoted 14 lines
Quoted 14 lines
mm/swap_io.c +13 -0
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),
+	 * but that will never affect SWP_FS_OPS, so the data_race
+	 * is safe.
+	 */
+	if (data_race(sis->flags & SWP_FS_OPS))
+		sis->ops = &bdev_fs_swap_ops;
+	/*
+	 * ->flags can be updated non-atomically (scan_swap_map_slots),
+	 * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+	 * is safe.
Quoted 8 lines

mm/swap_io.c static void swap_read_folio_bdev_async(struct folio *folio, · line 627

Quoted 6 lines
mm/swap_io.c +6 -0
+	 */
+	else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+		sis->ops = &bdev_sync_swap_ops;
+	else
+		sis->ops = &bdev_async_swap_ops;
+	return 0;

The return is always 0, so this function could be void.

Yes, I will change.

Quoted 30 lines
Quoted 30 lines
mm/swap_io.c +4 -7
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
 	struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	/* We have to read from slower devices. Increase zswap protection. */
 	zswap_folio_swapin(folio);
 
-	if (data_race(sis->flags & SWP_FS_OPS)) {
-		swap_read_folio_fs(folio, plug);
-	} else if (synchronous) {
-		swap_read_folio_bdev_sync(folio, sis);
-	} else {
-		swap_read_folio_bdev_async(folio, sis);
-	}
+	if (sis->ops && sis->ops->read_folio)
+		sis->ops->read_folio(sis, folio, plug);
 
 finish:
 	if (workingset) {
mm/swapfile.c +0 -0
index 915bc93964db..af498f9af328 100644
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	/* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
 	enable_swap_info(si);
 
Quoted 9 lines

mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3629

Quoted 2 lines
mm/swapfile.c +2 -0
+	probe_swap_fs(si);
+

Should probe_swap_fs() be called before enable_swap_info() rather than after it? enable_swap_info() sets SWP_WRITEOK and adds the device to swap_active_head, making it available for allocation. At that point si->ops is still NULL. If another CPU allocates swap from the new device and reclaim writes to it before probe_swap_fs() runs, the write will be silently dropped.

Good catch and I agree with you. I will change to call probe_swap_fs() before enable_swap_info().

Quoted 21 lines
Quoted 20 lines
mm/swapfile.c +0 -0
 	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,
 		K((unsigned long long)span),
mm/zswap.c +2 -1
index a399f7a10830..7ce906249c7a 100644
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	folio_set_reclaim(folio);
 
 	/* start writeback */
-	__swap_writepage(folio, NULL);
+	if (si->ops && si->ops->write_folio)
+		si->ops->write_folio(si, folio, NULL);
 
 out:
 	if (ret && ret != -EEXIST) {
Signature · 1 line
2.52.0
maintainer
#17 lore raw
In reply to Baoquan He
Quoted 272 lines · Baoquan He

This simplifies codes and makes logic clearer. And also makes later any new swap device type being added easier to handle.

Currently there are three types of swap devices: bdev_fs, bdev_sync and bdev_async, and only operations read_folio and write_folio are included. In the future, there could be more swap device types added and more appropriate opeations adapted into swap_ops.

---
 include/linux/swap.h |  13 ++++++
 mm/swap.h            |   1 -
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   2 +
 mm/zswap.c           |   3 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +13 -0
index 0effe3cc50f5..448e5e66ec5c 100644
@@ -19,6 +19,7 @@
 struct notifier_block;
 
 struct bio;
+struct swap_iocb;
 
 struct pagevec;
 
@@ -222,6 +223,17 @@ enum {
 #define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10)
 #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX
 
+struct swap_ops {
+       void (*read_folio)(struct swap_info_struct *sis,
+                          struct folio *folio,
+                          struct swap_iocb **plug);
+       void (*write_folio)(struct swap_info_struct *sis,
+                           struct folio *folio,
+                           struct swap_iocb **plug);
+};
+
+int probe_swap_fs(struct swap_info_struct *sis);
+
 /*
  * The first page in the swap file is the swap header, which is always marked
  * bad to prevent it from being allocated as an entry. This also prevents the
@@ -284,6 +296,7 @@ struct swap_info_struct {
        struct work_struct reclaim_work; /* reclaim worker */
        struct list_head discard_clusters; /* discard clusters list */
        struct plist_node avail_list;   /* entry in swap_avail_head */
+       struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +0 -1
index 161185057993..c390df3f5889 100644
@@ -226,7 +226,6 @@ static inline void swap_read_unplug(struct swap_iocb *plug)
 }
 void swap_write_unplug(struct swap_iocb *sio);
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
mm/swap_io.c +59 -43
index d1cdb10ba133..47077b345ae3 100644
@@ -240,6 +240,7 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 {
        int ret = 0;
+       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
 
        if (folio_free_swap(folio))
                goto out_unlock;
@@ -281,7 +282,8 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
                return AOP_WRITEPAGE_ACTIVATE;
        }
 
-       __swap_writepage(folio, swap_plug);
+       if (sis->ops && sis->ops->write_folio)
+               sis->ops->write_folio(sis, folio, swap_plug);
        return 0;
 out_unlock:
        folio_unlock(folio);
@@ -371,10 +373,11 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
        mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
+static void swap_writepage_fs(struct swap_info_struct *sis,
+                             struct folio *folio,
+                             struct swap_iocb **swap_plug)
 {
        struct swap_iocb *sio = swap_plug ? *swap_plug : NULL;
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct file *swap_file = sis->swap_file;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -407,8 +410,9 @@ static void swap_writepage_fs(struct folio *folio, struct swap_iocb **swap_plug)
                *swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+                                    struct folio *folio,
+                                    struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -427,8 +431,9 @@ static void swap_writepage_bdev_sync(struct folio *folio,
        __end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -444,29 +449,6 @@ static void swap_writepage_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
-void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug)
-{
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
-
-       VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_FS_OPS, so the data_race
-        * is safe.
-        */
-       if (data_race(sis->flags & SWP_FS_OPS))
-               swap_writepage_fs(folio, swap_plug);
-       /*
-        * ->flags can be updated non-atomically (scan_swap_map_slots),
-        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
-        * is safe.
-        */
-       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
-               swap_writepage_bdev_sync(folio, sis);
-       else
-               swap_writepage_bdev_async(folio, sis);
-}
-
 void swap_write_unplug(struct swap_iocb *sio)
 {
        struct iov_iter from;
@@ -535,9 +517,10 @@ static bool swap_read_folio_zeromap(struct folio *folio)
        return true;
 }
 
-static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
+static void swap_read_folio_fs(struct swap_info_struct *sis,
+                              struct folio *folio,
+                              struct swap_iocb **plug)
 {
-       struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
        struct swap_iocb *sio = NULL;
        loff_t pos = swap_dev_pos(folio->swap);
 
@@ -569,8 +552,9 @@ static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug)
                *plug = sio;
 }
 
-static void swap_read_folio_bdev_sync(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_sync(struct swap_info_struct *sis,
+                                     struct folio *folio,
+                                     struct swap_iocb **plug)
 {
        struct bio_vec bv;
        struct bio bio;
@@ -591,8 +575,9 @@ static void swap_read_folio_bdev_sync(struct folio *folio,
        put_task_struct(current);
 }
 
-static void swap_read_folio_bdev_async(struct folio *folio,
-               struct swap_info_struct *sis)
+static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
+                                      struct folio *folio,
+                                      struct swap_iocb **plug)
 {
        struct bio *bio;
 
@@ -606,6 +591,42 @@ static void swap_read_folio_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
+static struct swap_ops bdev_fs_swap_ops = {
+       .read_folio = swap_read_folio_fs,
+       .write_folio = swap_writepage_fs,
+};
+
+static struct swap_ops bdev_sync_swap_ops = {
+       .read_folio = swap_read_folio_bdev_sync,
+       .write_folio = swap_writepage_bdev_sync,
+};
+
+static struct swap_ops bdev_async_swap_ops = {
+       .read_folio = swap_read_folio_bdev_async,
+       .write_folio = swap_writepage_bdev_async,
+};
+
+int probe_swap_fs(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_FS_OPS, so the data_race
+        * is safe.
+        */
+       if (data_race(sis->flags & SWP_FS_OPS))
+               sis->ops = &bdev_fs_swap_ops;
+       /*
+        * ->flags can be updated non-atomically (scan_swap_map_slots),
+        * but that will never affect SWP_SYNCHRONOUS_IO, so the data_race
+        * is safe.
+        */
+       else if (data_race(sis->flags & SWP_SYNCHRONOUS_IO))
+               sis->ops = &bdev_sync_swap_ops;
+       else
+               sis->ops = &bdev_async_swap_ops;
+       return 0;
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -640,13 +661,8 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
        /* We have to read from slower devices. Increase zswap protection. */
        zswap_folio_swapin(folio);
 
-       if (data_race(sis->flags & SWP_FS_OPS)) {
-               swap_read_folio_fs(folio, plug);
-       } else if (synchronous) {
-               swap_read_folio_bdev_sync(folio, sis);
-       } else {
-               swap_read_folio_bdev_async(folio, sis);
-       }
+       if (sis->ops && sis->ops->read_folio)
+               sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +2 -0
index 915bc93964db..af498f9af328 100644
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
        enable_swap_info(si);
 
+       probe_swap_fs(si);
+
        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,
                K((unsigned long long)span),
mm/zswap.c +0 -0
index a399f7a10830..7ce906249c7a 100644

mm/zswap.c static int zswap_writeback_entry(struct zswap_entry *entry, · line 1059

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       if (si->ops && si->ops->write_folio)
+               si->ops->write_folio(si, folio, NULL);

This one seems really weird to me TBH...

What's the point of a zswap writeback to a backend without writeback operation...? We're loading the coldest zswap objects back into memory...?

More generally, do you anticipate a swap backend with no write_folio? I can't imagine such backend.

That said, if they exist, they should be exempted from zswap writeback. At the very least, move this check to the beginning of zswap_writeback_entry(), right after swap device pinning (get_swap_device()):

1. If swap device has no writeback op, just bail. Don't even bother allocate swap cache, decompression, etc.

2. If you anticipate that this case will never happen, add a warn.

author
#18 lore raw
Quoted 1 line · Nhat Pham

On Mon, Mar 2, 2026 at 2:40 AM Baoquan He <[email protected]> wrote:

...snip...

Quoted 39 lines
Quoted 18 lines
mm/swapfile.c +2 -0
index 915bc93964db..af498f9af328 100644
@@ -3625,6 +3625,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
        enable_swap_info(si);
 
+       probe_swap_fs(si);
+
        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,
                K((unsigned long long)span),
mm/zswap.c +0 -0
index a399f7a10830..7ce906249c7a 100644

mm/zswap.c static int zswap_writeback_entry(struct zswap_entry *entry, · line 1059

Quoted 6 lines
mm/zswap.c +2 -1
@@ -1055,7 +1055,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       if (si->ops && si->ops->write_folio)
+               si->ops->write_folio(si, folio, NULL);

This one seems really weird to me TBH...

What's the point of a zswap writeback to a backend without writeback operation...? We're loading the coldest zswap objects back into memory...?

More generally, do you anticipate a swap backend with no write_folio? I can't imagine such backend.

That said, if they exist, they should be exempted from zswap writeback. At the very least, move this check to the beginning of zswap_writeback_entry(), right after swap device pinning (get_swap_device()):

Quoted 4 lines

1. If swap device has no writeback op, just bail. Don't even bother allocate swap cache, decompression, etc.

2. If you anticipate that this case will never happen, add a warn.

Good point. Currently, there's no case where a swap backend without write_folio. While in the future there could be, assume you have noticed the swap tier patchset introduced by Youngjun.

Here, the si->ops checking is purely defensive programming. I can add a warn at the beginnin of zswap_writeback_entry(). If there's any case involving no read/write_folio, adaption can be made at that time.

author
#19 lore raw

PATCH 3/3 mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

In reply to Baoquan He

All these swap_writepage_* functions are hanlding passed in folio, but not page. And this renaming make them consistent with the their counterpart swap_read_folio_* functions.

---
 mm/swap_io.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
mm/swap_io.c +6 -6
index 47077b345ae3..ad315b128e36 100644
@@ -373,7 +373,7 @@ static void sio_write_complete(struct kiocb *iocb, long ret)
 	mempool_free(sio, sio_pool);
 }
 
-static void swap_writepage_fs(struct swap_info_struct *sis,
+static void swap_write_folio_fs(struct swap_info_struct *sis,
 			      struct folio *folio,
 			      struct swap_iocb **swap_plug)
 {
@@ -410,7 +410,7 @@ static void swap_writepage_fs(struct swap_info_struct *sis,
 		*swap_plug = sio;
 }
 
-static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
+static void swap_write_folio_bdev_sync(struct swap_info_struct *sis,
 				     struct folio *folio,
 				     struct swap_iocb **plug)
 {
@@ -431,7 +431,7 @@ static void swap_writepage_bdev_sync(struct swap_info_struct *sis,
 	__end_swap_bio_write(&bio);
 }
 
-static void swap_writepage_bdev_async(struct swap_info_struct *sis,
+static void swap_write_folio_bdev_async(struct swap_info_struct *sis,
 				      struct folio *folio,
 				      struct swap_iocb **plug)
 {
@@ -593,17 +593,17 @@ static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
 
 static struct swap_ops bdev_fs_swap_ops = {
 	.read_folio = swap_read_folio_fs,
-	.write_folio = swap_writepage_fs,
+	.write_folio = swap_write_folio_fs,
 };
 
 static struct swap_ops bdev_sync_swap_ops = {
 	.read_folio = swap_read_folio_bdev_sync,
-	.write_folio = swap_writepage_bdev_sync,
+	.write_folio = swap_write_folio_bdev_sync,
 };
 
 static struct swap_ops bdev_async_swap_ops = {
 	.read_folio = swap_read_folio_bdev_async,
-	.write_folio = swap_writepage_bdev_async,
+	.write_folio = swap_write_folio_bdev_async,
 };
 
 int probe_swap_fs(struct swap_info_struct *sis)
Signature · 1 line
2.52.0
Reviewed-by on v1 3/3
#20 lore raw

Re: [PATCH 3/3] mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

Quoted 6 lines · Baoquan He

All these swap_writepage_* functions are hanlding passed in folio, but not page. And this renaming make them consistent with the their counterpart swap_read_folio_* functions.

LGTM,

Quoted 3 lines
---
 mm/swap_io.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
reviewer
#22 lore raw
In reply to Baoquan He
Quoted 19 lines · Baoquan He

This can simplify the code logic and benefit any new type of swap device added later.

And also do renaming in this patchset:
-------
file renaming:
---
mm/page_io.c to mm/swap_io.c

function renaming:
---
swap_writepage_* to swap_write_folio_* in file mm/swap_io.c

Baoquan He (3):
mm/swap: rename mm/page_io.c to mm/swap_io.c mm/swap: use swap_ops to register swap device's methods mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

Quoted 12 lines
 MAINTAINERS                 |   2 +-
 include/linux/swap.h        |  13 +++++
 mm/Makefile                 |   2 +-
 mm/swap.h                   |   3 +-
 mm/{page_io.c => swap_io.c} | 104 +++++++++++++++++++++---------------
 mm/swapfile.c               |   2 +
 mm/zswap.c                  |   3 +-
 7 files changed, 80 insertions(+), 49 deletions(-)
 rename mm/{page_io.c => swap_io.c} (91%)
Signature · 1 line
2.52.0

Hi Baoquan,

Thank you for the swap_ops infrastructure patch. This is very relevant to our flash swap work, and I believe it could significantly help with our future contributions if the framework is extended further.

As I understand it, the current patch serves as a foundation for future infrastructure. However, from our perspective, the swap ops are currently configured statically, and we would need a way to apply or register custom ops for our use case.

I have a few suggestions (mixed with questions) regarding extensibility.

1) Could the ops be replaced at activation time, similar to how SWP_FS_OPS works — i.e., giving block device drivers an opportunity to override the ops when the swap device is activated?

This would allow block devices to handle swap read/write through swap_ops rather than doing it in submit_bio, which feels more generalized and leaves room for future extension.

This direction would be the best fit for our use case. However, considering ongoing swap work that may involve managing multiple swap devices, I wonder whether binding ops to a single block device would still be appropriate in that scenario.

2) Would it be possible to defer swap device activation via a lazy registration interface, so that the ops can be registered before the device is fully activated?

This would open the door for entities other than block devices — such as kernel modules or other forms — to act as swap backends. (Of course, block devices could also benefit from this path.)

3) Alternatively, are there any plans for a static binding mechanism, or any other extensibility approach you have in mind?

As an additional thought, if this ops framework could eventually be extended to cover cluster allocation as well, it might enable a model where base swap functionality is provided by the core, while each vendor can freely plug in their own additional use cases on top — making the swap device truly extensible.

Any thoughts on these would be greatly appreciated.

Best regards,
Youngjun Park

author
#23 lore raw
Quoted 40 lines · YoungJun Park
Quoted 19 lines · Baoquan He

This can simplify the code logic and benefit any new type of swap device added later.

And also do renaming in this patchset:
-------
file renaming:
---
mm/page_io.c to mm/swap_io.c

function renaming:
---
swap_writepage_* to swap_write_folio_* in file mm/swap_io.c

Baoquan He (3):
mm/swap: rename mm/page_io.c to mm/swap_io.c mm/swap: use swap_ops to register swap device's methods mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

Quoted 12 lines
 MAINTAINERS                 |   2 +-
 include/linux/swap.h        |  13 +++++
 mm/Makefile                 |   2 +-
 mm/swap.h                   |   3 +-
 mm/{page_io.c => swap_io.c} | 104 +++++++++++++++++++++---------------
 mm/swapfile.c               |   2 +
 mm/zswap.c                  |   3 +-
 7 files changed, 80 insertions(+), 49 deletions(-)
 rename mm/{page_io.c => swap_io.c} (91%)
Signature · 1 line
2.52.0

Hi Baoquan,

Thank you for the swap_ops infrastructure patch. This is very relevant to our flash swap work, and I believe it could significantly help with our future contributions if the framework is extended further.

Quoted 4 lines

As I understand it, the current patch serves as a foundation for future infrastructure. However, from our perspective, the swap ops are currently configured statically, and we would need a way to apply or register custom ops for our use case.

Thanks a lot for your input about the future potential use cases of swap_ops, they are very valuable.

Yes, this patchset is posted as a infrastructure of swap_ops and a thread where we collect any potential extention or use case. The change is made based on the current kernel.

Quoted 13 lines

I have a few suggestions (mixed with questions) regarding extensibility.

1) Could the ops be replaced at activation time, similar to how SWP_FS_OPS works — i.e., giving block device drivers an opportunity to override the ops when the swap device is activated?

This would allow block devices to handle swap read/write through swap_ops rather than doing it in submit_bio, which feels more generalized and leaves room for future extension.

Quoted 5 lines

This direction would be the best fit for our use case. However, considering ongoing swap work that may involve managing multiple swap devices, I wonder whether binding ops to a single block device would still be appropriate in that scenario.

I think it's doable and make swap_ops more flexible and versatile to support different swap back ends.

As for binding ops to a swap devices conflicting with managing multiple swap devices, I may not get your point very clearly. Could you give a concrete use case to demonstrate?

Quoted 9 lines

2) Would it be possible to defer swap device activation via a lazy registration interface, so that the ops can be registered before the device is fully activated?

This would open the door for entities other than block devices — such as kernel modules or other forms — to act as swap backends. (Of course, block devices could also benefit from this path.)

Yes, I think it's doable. I will make change and send a draft to you for evaluation. By the way, do you have actual use case in which deferring swap activation is needed for justification?

Quoted 10 lines

3) Alternatively, are there any plans for a static binding mechanism, or any other extensibility approach you have in mind?

As an additional thought, if this ops framework could eventually be extended to cover cluster allocation as well, it might enable a model where base swap functionality is provided by the core, while each vendor can freely plug in their own additional use cases on top — making the swap device truly extensible.

Yes, this is planned task as I have mentioned in the LSF/MM topic mail.

Quoted 6 lines

Any thoughts on these would be greatly appreciated.

Best regards,
Youngjun Park

reviewer
#24 lore raw
Quoted 46 lines · Baoquan He
Quoted 40 lines · YoungJun Park
Quoted 19 lines · Baoquan He

This can simplify the code logic and benefit any new type of swap device added later.

And also do renaming in this patchset:
-------
file renaming:
---
mm/page_io.c to mm/swap_io.c

function renaming:
---
swap_writepage_* to swap_write_folio_* in file mm/swap_io.c

Baoquan He (3):
mm/swap: rename mm/page_io.c to mm/swap_io.c mm/swap: use swap_ops to register swap device's methods mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*

Quoted 12 lines
 MAINTAINERS                 |   2 +-
 include/linux/swap.h        |  13 +++++
 mm/Makefile                 |   2 +-
 mm/swap.h                   |   3 +-
 mm/{page_io.c => swap_io.c} | 104 +++++++++++++++++++++---------------
 mm/swapfile.c               |   2 +
 mm/zswap.c                  |   3 +-
 7 files changed, 80 insertions(+), 49 deletions(-)
 rename mm/{page_io.c => swap_io.c} (91%)
Signature · 1 line
2.52.0

Hi Baoquan,

Thank you for the swap_ops infrastructure patch. This is very relevant to our flash swap work, and I believe it could significantly help with our future contributions if the framework is extended further.

Quoted 4 lines

As I understand it, the current patch serves as a foundation for future infrastructure. However, from our perspective, the swap ops are currently configured statically, and we would need a way to apply or register custom ops for our use case.

Quoted 5 lines

Thanks a lot for your input about the future potential use cases of swap_ops, they are very valuable.
Yes, this patchset is posted as a infrastructure of swap_ops and a thread where we collect any potential extention or use case. The change is made based on the current kernel.

I understand. I brought up suggestions 1 and 2 because they are necessary from the perspective of immediate application. :D

Quoted 19 lines
Quoted 13 lines

I have a few suggestions (mixed with questions) regarding extensibility.

1) Could the ops be replaced at activation time, similar to how SWP_FS_OPS works — i.e., giving block device drivers an opportunity to override the ops when the swap device is activated?

This would allow block devices to handle swap read/write through swap_ops rather than doing it in submit_bio, which feels more generalized and leaves room for future extension.

Quoted 5 lines

This direction would be the best fit for our use case. However, considering ongoing swap work that may involve managing multiple swap devices, I wonder whether binding ops to a single block device would still be appropriate in that scenario.

Quoted 6 lines

I think it's doable and make swap_ops more flexible and versatile to support different swap back ends.

As for binding ops to a swap devices conflicting with managing multiple swap devices, I may not get your point very clearly. Could you give a concrete use case to demonstrate?

To clarify my point about binding ops: I was focusing on the initialization timing. Taking SWP_FS_OPS as an example, setup_swap_extents calls swap_activate and finally SWP_FS_OPS is set by filesystem.

Similarly, I am suggesting a mechanism that gives the block device (or another entity) an opportunity to register its specific swap_ops during the swapon phase by calling some kind of swap_active callback.
(And it may be not the block device as well?)

Quoted 5 lines
Quoted 5 lines

2) Would it be possible to defer swap device activation via a lazy registration interface, so that the ops can be registered before the device is fully activated?

Quoted 8 lines
Quoted 4 lines

This would open the door for entities other than block devices — such as kernel modules or other forms — to act as swap backends. (Of course, block devices could also benefit from this path.)

Yes, I think it's doable. I will make change and send a draft to you for evaluation. By the way, do you have actual use case in which deferring swap activation is needed for justification?

Regarding the use case for deferred activation: This becomes necessary if we don't use the activation-time binding mentioned in #1.

If we rely on dynamic registration, there is a time window between swapon and the actual registration of the ops. During this window, the swap device should not perform I/O since the ops aren't set yet. A lazy registration or deferred activation mechanism would handle this safety gap.

Quoted 9 lines
Quoted 4 lines

3) Alternatively, are there any plans for a static binding mechanism, or any other extensibility approach you have in mind?

How the swap back end was chosen. We can make it a swap mount option. We make the swapfile header look like a file system super block. The different swapfile header contains different back end types and options.

I found your comment about ops binding on LSF/MM/BPF suggestion. sorry for missing. Okay, I see. While I personally prefer the direction similar to SWP_FS_OPS infrastructure(or #2), the mount option or header approach is also a valid path!

Quoted 7 lines
Quoted 5 lines

As an additional thought, if this ops framework could eventually be extended to cover cluster allocation as well, it might enable a model where base swap functionality is provided by the core, while each vendor can freely plug in their own additional use cases on top — making the swap device truly extensible.

Yes, this is planned task as I have mentioned in the LSF/MM topic mail.

It is great to hear that custom cluster allocation is planned. this would allow vendors to implement optimizations specific to
their swap I look forward to that and will provide comments if we have concrete use cases.

Thanks a lot for your work
Youngjun Park