Skip to contentSkip to conversation

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

PATCH Superseded Baoquan He 3 participants 12 messages Apr 17 – Apr 24

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Baoquan He

author reviewer
#0 lore raw

v4 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

Changlog:
===
-v4:
* Fix a typo opeations -> operations
* Fix a code bug inside init_swap_ops(). I was taking a change at the time, thought the change is trivial, so I only compiled but didn't run kernel to test in v3. Now fix it and test passed. Thanaks to Usama for catching the above two issues.

-v3:
* Rename setup_swap_ops() to init_swap_ops() which reflect the function behaviour a little better
* Check if sis->ops, sis->ops->read_folio and sis->ops->write_folio is NULL in init_swap_ops(), but not spread them where they are called. And once the checking failed, fail swapon immediately. This is suggested by Chris.
* Call init_swap_ops() before setup_swap_extents() invocation. This doesn't harm anything and can benefit later adding sis->ops->swap_activate method.

-v2:
* lots of cleanup for patch 2/3: renaming, moving data structures, and using const properly
* collected tags from Kairui, Nhat and Barry

-v1:
https://lore.kernel.org/linux-mm/[email protected]/

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        |   2 +
 mm/Makefile                 |   2 +-
 mm/swap.h                   |  12 ++++-
 mm/{page_io.c => swap_io.c} | 104 ++++++++++++++++++++----------------
 mm/swapfile.c               |   5 ++
 mm/zswap.c                  |   2 +-
 7 files changed, 79 insertions(+), 50 deletions(-)
 rename mm/{page_io.c => swap_io.c} (90%)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH v4 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, 3 insertions(+), 5 deletions(-)
 rename mm/{page_io.c => swap_io.c} (99%)
MAINTAINERS +1 -1
index 447189411512..c7b5b6f5fa50 100644
@@ -17002,7 +17002,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 +0 -2
similarity index 99%
rename from mm/page_io.c
rename to mm/swap_io.c
index 330abc5ab7b4..a62c02ab0551 100644
@@ -1,7 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- *  linux/mm/page_io.c
- *
  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
  *
  *  Swap reorganised 29.12.95, 
Signature · 1 line
2.52.0
author reviewer
#2 lore raw

PATCH v4 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 operations adapted into swap_ops.

---
 include/linux/swap.h |   2 +
 mm/swap.h            |  10 ++++-
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   5 +++
 mm/zswap.c           |   2 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +2 -0
index 4b1f13b5bbad..5fdda26f5c1d 100644
@@ -242,6 +242,7 @@ struct swap_sequential_cluster {
 	unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
 };
 
+struct swap_ops;
 /*
  * The in-memory structure used to track swap areas.
  */
@@ -282,6 +283,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 */
+	const struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +9 -1
index 161185057993..29bdc679fa98 100644
@@ -217,6 +217,15 @@ extern void __swap_cluster_free_entries(struct swap_info_struct *si,
 /* linux/mm/swap_io.c */
 int sio_pool_init(void);
 struct swap_iocb;
+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 init_swap_ops(struct swap_info_struct *sis);
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug);
 void __swap_read_unplug(struct swap_iocb *plug);
 static inline void swap_read_unplug(struct swap_iocb *plug)
@@ -226,7 +235,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 +5 -0
index 9174f1eeffb0..af81fa212f1e 100644
@@ -3518,6 +3518,11 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 		goto bad_swap_unlock_inode;
 	}
 
+	if (init_swap_ops(si)) {
+		error = -EINVAL;
+		goto bad_swap_unlock_inode;
+	}
+
 	si->max = maxpages;
 	si->pages = maxpages - 1;
 	nr_extents = setup_swap_extents(si, swap_file, &span);
mm/zswap.c +1 -1
index 0823cadd02b6..b699413a2526 100644
@@ -1061,7 +1061,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
 	folio_set_reclaim(folio);
 
 	/* start writeback */
-	__swap_writepage(folio, NULL);
+	si->ops->write_folio(si, folio, NULL);
 
 out:
 	if (ret && ret != -EEXIST) {
Signature · 1 line
2.52.0
maintainer Acked-by on v4 2/3
#3 lore raw
Quoted 9 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 operations adapted into swap_ops.

Quoted 4 lines

Overall looks good, I have some nitpick follows.

Quoted 132 lines
---
 include/linux/swap.h |   2 +
 mm/swap.h            |  10 ++++-
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   5 +++
 mm/zswap.c           |   2 +-
 5 files changed, 76 insertions(+), 45 deletions(-)
include/linux/swap.h +2 -0
index 4b1f13b5bbad..5fdda26f5c1d 100644
@@ -242,6 +242,7 @@ struct swap_sequential_cluster {
        unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */
 };
 
+struct swap_ops;
 /*
  * The in-memory structure used to track swap areas.
  */
@@ -282,6 +283,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 */
+       const struct swap_ops *ops;
 };
 
 static inline swp_entry_t page_swap_entry(struct page *page)
mm/swap.h +9 -1
index 161185057993..29bdc679fa98 100644
@@ -217,6 +217,15 @@ extern void __swap_cluster_free_entries(struct swap_info_struct *si,
 /* linux/mm/swap_io.c */
 int sio_pool_init(void);
 struct swap_iocb;
+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 init_swap_ops(struct swap_info_struct *sis);
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug);
 void __swap_read_unplug(struct swap_iocb *plug);
 static inline void swap_read_unplug(struct swap_iocb *plug)
@@ -226,7 +235,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 +11 -20
index a62c02ab0551..1b054a122f53 100644
@@ -238,6 +238,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;
@@ -279,7 +280,7 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
                return AOP_WRITEPAGE_ACTIVATE;
        }
 
-       __swap_writepage(folio, swap_plug);
+       sis->ops->write_folio(sis, folio, swap_plug);
        return 0;
 out_unlock:
        folio_unlock(folio);
@@ -369,10 +370,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);
 
@@ -405,8 +407,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;
@@ -425,8 +428,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;
 
@@ -442,29 +446,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,
-        * 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);
-       /*

mm/swap_io.c static void swap_writepage_bdev_async(struct folio *folio, · line 467

Quoted 10 lines
mm/swap_io.c +0 -10
-        * ->flags can be updated non-atomically,
-        * 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);
-}
-

Nice

Quoted 68 lines
mm/swap_io.c +33 -6
 void swap_write_unplug(struct swap_iocb *sio)
 {
        struct iov_iter from;
@@ -533,9 +514,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);
 
@@ -567,8 +549,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;
@@ -589,8 +572,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;
 
@@ -604,6 +588,44 @@ static void swap_read_folio_bdev_async(struct folio *folio,
        submit_bio(bio);
 }
 
+static const struct swap_ops bdev_fs_swap_ops = {
+       .read_folio = swap_read_folio_fs,
+       .write_folio = swap_writepage_fs,
+};
+
+static const struct swap_ops bdev_sync_swap_ops = {
+       .read_folio = swap_read_folio_bdev_sync,
+       .write_folio = swap_writepage_bdev_sync,
+};
+
+static const struct swap_ops bdev_async_swap_ops = {
+       .read_folio = swap_read_folio_bdev_async,
+       .write_folio = swap_writepage_bdev_async,
+};
+
+int init_swap_ops(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically, 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 624

Quoted 10 lines
mm/swap_io.c +10 -0
+        * ->flags can be updated non-atomically, 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;
+
+       if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+               return -1;

Nitpick:

For int type error return, you should use -EINVAL or some thing with error code. If you don't care about error code, change the return type to bool instead.

Quoted 9 lines
mm/swap_io.c +4 -0
+
+       return 0;
+}
+
 void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
@@ -638,13 +660,7 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
        /* We have to read from slower devices. Increase zswap protection. */

mm/swap_io.c void swap_read_folio(struct folio *folio, struct swap_iocb **plug) · line 663

Quoted 10 lines
mm/swap_io.c +1 -7
        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);
-       }
+       sis->ops->read_folio(sis, folio, plug);

Nice.

Quoted 8 lines
mm/swap_io.c +0 -0
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 9174f1eeffb0..af81fa212f1e 100644

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

Quoted 8 lines
mm/swapfile.c +4 -0
@@ -3518,6 +3518,11 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto bad_swap_unlock_inode;
        }
 
 
+       if (init_swap_ops(si)) {
+               error = -EINVAL;
+               goto bad_swap_unlock_inode;
+       }

Assume you return the error code above. Here can be error = init_swap_ops(si)
if (error)

     goto ...;

Chris

Quoted 21 lines
mm/swapfile.c +1 -0
+
        si->max = maxpages;
        si->pages = maxpages - 1;
        nr_extents = setup_swap_extents(si, swap_file, &span);
mm/zswap.c +1 -2
index 0823cadd02b6..b699413a2526 100644
@@ -1061,7 +1061,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
        folio_set_reclaim(folio);
 
        /* start writeback */
-       __swap_writepage(folio, NULL);
+       si->ops->write_folio(si, folio, NULL);
 
 out:
        if (ret && ret != -EEXIST) {
--

2.52.0

author reviewer
#4 lore raw
Quoted 1 line · Chris Li

On Thu, Apr 16, 2026 at 8:40 PM Baoquan He <[email protected]> wrote:

...snip...

Quoted 21 lines
Quoted 9 lines
mm/swap_io.c +9 -0
+int init_swap_ops(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically, 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 624

Quoted 10 lines
mm/swap_io.c +10 -0
+        * ->flags can be updated non-atomically, 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;
+
+       if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+               return -1;

Nitpick:

Quoted 3 lines

For int type error return, you should use -EINVAL or some thing with error code. If you don't care about error code, change the return type to bool instead.

Thanks for careful reviewing, returning -EINVAL looks better, I will change like that and repost.

Thanks
Baoquan

maintainer
#5 lore raw
Quoted 25 lines · Baoquan He
Quoted 1 line · Chris Li

On Thu, Apr 16, 2026 at 8:40 PM Baoquan He <[email protected]> wrote:

...snip...

Quoted 21 lines
Quoted 9 lines
mm/swap_io.c +9 -0
+int init_swap_ops(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically, 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 624

Quoted 10 lines
mm/swap_io.c +10 -0
+        * ->flags can be updated non-atomically, 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;
+
+       if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+               return -1;

Nitpick:

Quoted 6 lines
Quoted 3 lines

For int type error return, you should use -EINVAL or some thing with error code. If you don't care about error code, change the return type to bool instead.

Thanks for careful reviewing, returning -EINVAL looks better, I will change like that and repost.

You don't have to repost it as a new series. You can post one small incremental update for the -EINVAL change and ask Andrew to fold it in.

Hi Andrew,

I think this swap ops series can be merge to mm-unstable with the review. Keep in mind that Baoquan might have a very minor follow up patch relate to -EINAL error code.

Chris

author reviewer
#6 lore raw
Quoted 37 lines · Chris Li
Quoted 25 lines · Baoquan He
Quoted 1 line · Chris Li

On Thu, Apr 16, 2026 at 8:40 PM Baoquan He <[email protected]> wrote:

...snip...

Quoted 21 lines
Quoted 9 lines
(quoted diff) +9 -0
+int init_swap_ops(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically, 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;
+       /*

Review

Quoted 10 lines
(quoted diff) +10 -0
+        * ->flags can be updated non-atomically, 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;
+
+       if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+               return -1;

Nitpick:

Quoted 6 lines
Quoted 3 lines

For int type error return, you should use -EINVAL or some thing with error code. If you don't care about error code, change the return type to bool instead.

Thanks for careful reviewing, returning -EINVAL looks better, I will change like that and repost.

You don't have to repost it as a new series. You can post one small incremental update for the -EINVAL change and ask Andrew to fold it in.

Quoted 5 lines

Hi Andrew,

I think this swap ops series can be merge to mm-unstable with the review. Keep in mind that Baoquan might have a very minor follow up patch relate to -EINAL error code.

The incremental update is as below. I have built and test it.

mm/swap_io.c +1 -1
index 77aa8373c087..e2710d5fb44e 100644
@@ -625,7 +625,7 @@ int init_swap_ops(struct swap_info_struct *sis)
 		sis->ops = &bdev_async_swap_ops;
 
 	if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
-		return -1;
+		return -EINVAL;
 
 	return 0;
 }
mm/swapfile.c +1 -3
index af81fa212f1e..7644049a0919 100644
@@ -3518,10 +3518,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 		goto bad_swap_unlock_inode;
 	}
 
-	if (init_swap_ops(si)) {
-		error = -EINVAL;
+	if (error = init_swap_ops(si))
 		goto bad_swap_unlock_inode;
-	}
 
 	si->max = maxpages;
 	si->pages = maxpages - 1;
Acked-by
#7 lore raw
Quoted 65 lines · Baoquan He
Quoted 37 lines · Chris Li
Quoted 25 lines · Baoquan He
Quoted 1 line · Chris Li

On Thu, Apr 16, 2026 at 8:40 PM Baoquan He <[email protected]> wrote:

...snip...

Quoted 21 lines
Quoted 9 lines
(quoted diff) +9 -0
+int init_swap_ops(struct swap_info_struct *sis)
+{
+       /*
+        * ->flags can be updated non-atomically, 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;
+       /*

Review

Quoted 10 lines
(quoted diff) +10 -0
+        * ->flags can be updated non-atomically, 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;
+
+       if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+               return -1;

Nitpick:

Quoted 6 lines
Quoted 3 lines

For int type error return, you should use -EINVAL or some thing with error code. If you don't care about error code, change the return type to bool instead.

Thanks for careful reviewing, returning -EINVAL looks better, I will change like that and repost.

You don't have to repost it as a new series. You can post one small incremental update for the -EINVAL change and ask Andrew to fold it in.

Quoted 5 lines

Hi Andrew,

I think this swap ops series can be merge to mm-unstable with the review. Keep in mind that Baoquan might have a very minor follow up patch relate to -EINAL error code.

The incremental update is as below. I have built and test it.

mm/swap_io.c +1 -1
index 77aa8373c087..e2710d5fb44e 100644
@@ -625,7 +625,7 @@ int init_swap_ops(struct swap_info_struct *sis)
 		sis->ops = &bdev_async_swap_ops;
 
 	if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
-		return -1;
+		return -EINVAL;
 
 	return 0;
 }
mm/swapfile.c +0 -0
index af81fa212f1e..7644049a0919 100644
@@ -3518,10 +3518,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 		goto bad_swap_unlock_inode;

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

Quoted 10 lines
mm/swapfile.c +1 -3
 	}
 
-	if (init_swap_ops(si)) {
-		error = -EINVAL;
+	if (error = init_swap_ops(si))
 		goto bad_swap_unlock_inode;
-	}
 
 	si->max = maxpages;
 	si->pages = maxpages - 1;
maintainer
#8 lore raw
In reply to Baoquan He

The incremental change code logic looks fine.

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

Quoted 12 lines · Baoquan He
mm/swapfile.c +1 -2
index af81fa212f1e..7644049a0919 100644
@@ -3518,10 +3518,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto bad_swap_unlock_inode;
        }
 
-       if (init_swap_ops(si)) {
-               error = -EINVAL;
+       if (error = init_swap_ops(si))

Some more nitpicks:

I think some compiler option might warn about assigning a variable inside an if condition. I suggest moving the assignment outside of the if condition expression. As it is, it looks very close to "error == init_swap_ops(si))", Some compiler might warn, "Is that what you mean instead?"

Chris

Quoted 7 lines
mm/swapfile.c +0 -1
                goto bad_swap_unlock_inode;
-       }
 
 
        si->max = maxpages;
        si->pages = maxpages - 1;
author reviewer
#9 lore raw
Quoted 18 lines · Chris Li

The incremental change code logic looks fine.

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

Quoted 12 lines · Baoquan He
mm/swapfile.c +1 -2
index af81fa212f1e..7644049a0919 100644
@@ -3518,10 +3518,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto bad_swap_unlock_inode;
        }
 
-       if (init_swap_ops(si)) {
-               error = -EINVAL;
+       if (error = init_swap_ops(si))

Some more nitpicks:

Quoted 5 lines

I think some compiler option might warn about assigning a variable inside an if condition. I suggest moving the assignment outside of the if condition expression. As it is, it looks very close to "error == init_swap_ops(si))", Some compiler might warn, "Is that what you mean instead?"

I personally prefer the one doing assignment inside if condition expression because it is a ittle neater with one less line. While it doesn't matter much.

Here comes the new version of the incremental update. Thanks.

mm/swap_io.c +1 -1
index 77aa8373c087..e2710d5fb44e 100644
@@ -625,7 +625,7 @@ int init_swap_ops(struct swap_info_struct *sis)
 		sis->ops = &bdev_async_swap_ops;
 
 	if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
-		return -1;
+		return -EINVAL;
 
 	return 0;
 }
mm/swapfile.c +2 -3
index af81fa212f1e..82d2c9b35b11 100644
@@ -3518,10 +3518,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 		goto bad_swap_unlock_inode;
 	}
 
-	if (init_swap_ops(si)) {
-		error = -EINVAL;
+	error = init_swap_ops(si);
+	if (error)
 		goto bad_swap_unlock_inode;
-	}
 
 	si->max = maxpages;
 	si->pages = maxpages - 1;
maintainer Acked-by
#10 lore raw
Quoted 26 lines · Baoquan He
Quoted 18 lines · Chris Li

The incremental change code logic looks fine.

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

Quoted 12 lines · Baoquan He
mm/swapfile.c +1 -2
index af81fa212f1e..7644049a0919 100644
@@ -3518,10 +3518,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto bad_swap_unlock_inode;
        }
 
-       if (init_swap_ops(si)) {
-               error = -EINVAL;
+       if (error = init_swap_ops(si))

Some more nitpicks:

Quoted 5 lines

I think some compiler option might warn about assigning a variable inside an if condition. I suggest moving the assignment outside of the if condition expression. As it is, it looks very close to "error == init_swap_ops(si))", Some compiler might warn, "Is that what you mean instead?"

Quoted 3 lines

I personally prefer the one doing assignment inside if condition expression because it is a ittle neater with one less line. While it doesn't matter much.

Yes, I understand it is a matter of personal preference. It will generate the same code.

Quoted 1 line

Here comes the new version of the incremental update. Thanks.

LGTM.

Chris

Quoted 32 lines
mm/swap_io.c +1 -1
index 77aa8373c087..e2710d5fb44e 100644
@@ -625,7 +625,7 @@ int init_swap_ops(struct swap_info_struct *sis)
                sis->ops = &bdev_async_swap_ops;
 
        if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
-               return -1;
+               return -EINVAL;
 
        return 0;
 }
mm/swapfile.c +2 -3
index af81fa212f1e..82d2c9b35b11 100644
@@ -3518,10 +3518,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto bad_swap_unlock_inode;
        }
 
-       if (init_swap_ops(si)) {
-               error = -EINVAL;
+       error = init_swap_ops(si);
+       if (error)
                goto bad_swap_unlock_inode;
-       }
 
        si->max = maxpages;
        si->pages = maxpages - 1;
author reviewer
#11 lore raw

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

In reply to Baoquan He

All these swap_writepage_* functions handle a passed-in folio, not a page. This renaming makes them consistent with 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 1b054a122f53..50a16148b919 100644
@@ -370,7 +370,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)
 {
@@ -407,7 +407,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)
 {
@@ -428,7 +428,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)
 {
@@ -590,17 +590,17 @@ static void swap_read_folio_bdev_async(struct swap_info_struct *sis,
 
 static const 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 const 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 const 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 init_swap_ops(struct swap_info_struct *sis)
Signature · 1 line
2.52.0