Skip to contentSkip to conversation

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

PATCH Superseded Baoquan He 4 participants 22 messages May 12 – May 16

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Baoquan He

author reviewer
#0 lore raw

v6 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:
===
-v6:
* Fix a code bug Kairui found out when reviewing patch in mm/swapfile.c in patch 2/3. Has fixed it by moving it to appropriate place and add comment to explain.

-v5:
* Change the return value of init_swap_ops() as -EINVAL as per Chris's suggestion and adjust its invocation in swapon() accordingly.
* Add Chris and Usama's Ack tags.

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

PATCH v6 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 98d0a7a1c689..b7746b359471 100644
@@ -17033,7 +17033,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 7ed76592e20d..7c1a3a3a53f8 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
maintainer Acked-by on v6 1/3
#2 lore raw

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

Quoted 10 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.

Chris

#3 lore raw

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

In reply to Baoquan He
Quoted 2 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. At the same time it also (mostly) isn't about all swap I/O, but specifically about the block based I/O backend for swap, and secondarily a little bit about helpers for the swsusp I/O to the swap file, with a little bit generic swap I/O wrappers thrown in.

So the new new isn't grest either. And I'd rather wait with this until we can nicely split stuff out - the rest of this series is a good step toward that, and my swap_activate series is another. After that we should be able to stop creating swap_extent structures for SWP_FS_OPS-based I/O, and contain struct swap_extent with the bio code and actually create a coherent abstraction. This will require moving a bit more code around, though. My preference for the resulting name would be swap-block.c or swap-bio.c, but that's up for future discussion.

Can we just skip this for now?

author reviewer
#4 lore raw

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

Quoted 10 lines · Christoph Hellwig
Quoted 2 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. At the same time it also (mostly) isn't about all swap I/O, but specifically about the block based I/O backend for swap, and secondarily a little bit about helpers for the swsusp I/O to the swap file, with a little bit generic swap I/O wrappers thrown in.

So the new new isn't grest either. And I'd rather wait with this until

"So the new isn't great either." ?

Take it easy, I can feel this abstraction falls into your area?

Quoted 10 lines

we can nicely split stuff out - the rest of this series is a good step toward that, and my swap_activate series is another. After that we should be able to stop creating swap_extent structures for SWP_FS_OPS-based I/O, and contain struct swap_extent with the bio code and actually create a coherent abstraction. This will require moving a bit more code around, though. My preference for the resulting name would be swap-block.c or swap-bio.c, but that's up for future discussion.

Can we just skip this for now?

Yes, I agree with you, and the swap_extent creating thing sounds great, look forward to seeing it. I will drop this patch 1/3 in v7.

author reviewer
#5 lore raw

PATCH v6 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 |   2 +
 mm/swap.h            |  10 ++++-
 mm/swap_io.c         | 102 +++++++++++++++++++++++++------------------
 mm/swapfile.c        |   9 ++++
 mm/zswap.c           |   2 +-
 5 files changed, 80 insertions(+), 45 deletions(-)
include/linux/swap.h +2 -0
index aa89e1d30a77..85f15ec326c1 100644
@@ -243,6 +243,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.
  */
@@ -283,6 +284,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 +9 -0
index 4840fd40f36f..8c42632e6765 100644
@@ -3780,6 +3780,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 		goto free_swap_zswap;
 	}
 
+	/*
+	 * init_swap_ops() sets si->ops based on flags. It does not need
+	 * swapon_mutex, and must complete before enable_swap_info()
+	 * exposes the device.
+	 */
+	error = init_swap_ops(si);
+	if (error)
+		goto bad_swap_unlock_inode;
+
 	mutex_lock(&swapon_mutex);
 	prio = DEF_SWAP_PRIO;
 	if (swap_flags & SWAP_FLAG_PREFER)
mm/zswap.c +1 -1
index 4b5149173b0e..192401f46de4 100644
@@ -1054,7 +1054,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
#6 lore raw
Quoted 8 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.

opeations -> operations

Quoted 6 lines

A few nitpicks below:

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

Quoted 5 lines
mm/swap_io.c +0 -5
-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);

This sanify check is dropped and not added back in anywhere. This is fine, but it might be better to have a similar VM_WARN_ON_FOLIO in swap_writeout?

Quoted 24 lines

void swap_read_folio(struct folio *folio, struct swap_iocb **plug)

 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
mm/swap_io.c +1 -7
@@ -642,13 +664,7 @@ 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);
-       }
+       sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 4840fd40f36f..8c42632e6765 100644
@@ -3780,6 +3780,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto free_swap_zswap;

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

Quoted 10 lines
mm/swapfile.c +8 -0
        }
 
+       /*
+        * init_swap_ops() sets si->ops based on flags. It does not need
+        * swapon_mutex, and must complete before enable_swap_info()
+        * exposes the device.
+        */
+       error = init_swap_ops(si);
+       if (error)
+               goto bad_swap_unlock_inode;

I checked the comment above previously and it looked good. But the error label seems not that correct after double check. inode->i_flags will keep the S_SWAPFILE flag. Maybe something like add a inode->i_flags &= ~S_SWAPFILE here and goto free_swap_zswap. Sorry I didn't check this part carefully last time.

But fortunately, init_swap_ops will never fail at this moment so the issue never triggers.

Quoted 18 lines
mm/swapfile.c +1 -0
+
        mutex_lock(&swapon_mutex);
        prio = DEF_SWAP_PRIO;
        if (swap_flags & SWAP_FLAG_PREFER)
mm/zswap.c +1 -2
index 4b5149173b0e..192401f46de4 100644
@@ -1054,7 +1054,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) {
--
Quoted 2 lines

2.52.0

Rest looks good to me!

author reviewer
#7 lore raw
Quoted 11 lines · Kairui Song
Quoted 8 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.

opeations -> operations

Will fix, thanks.

Quoted 10 lines
Quoted 7 lines

A few nitpicks below:

Quoted 9 lines

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

Quoted 5 lines
mm/swap_io.c +0 -5
-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);

This sanify check is dropped and not added back in anywhere. This is fine, but it might be better to have a similar VM_WARN_ON_FOLIO in swap_writeout?

Good catch. In the old code, it's called by both swap_writeout() and zswap_writeback_entry(). In zswap_writeback_entry(), the folio is allocated in-place, we don't need to worry about it now. I am fine to add it back in swap_writeout() because adding it inside all three swap_write_xxx() is a little too much.

Cscope tag: __swap_writepage
# line filename / context / line
1 288 /home/bhe/code/linux/mm/page_io.c <<swap_writeout>>

             __swap_writepage(folio, swap_plug);
   2   1053  /home/bhe/code/linux/mm/zswap.c <<zswap_writeback_entry>>
             __swap_writepage(folio, NULL);
Quoted 27 lines
Quoted 27 lines

void swap_read_folio(struct folio *folio, struct swap_iocb **plug)

 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
mm/swap_io.c +1 -7
@@ -642,13 +664,7 @@ 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);
-       }
+       sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 4840fd40f36f..8c42632e6765 100644
@@ -3780,6 +3780,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto free_swap_zswap;
        }
 
Quoted 14 lines

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

Quoted 8 lines
mm/swapfile.c +8 -0
+       /*
+        * init_swap_ops() sets si->ops based on flags. It does not need
+        * swapon_mutex, and must complete before enable_swap_info()
+        * exposes the device.
+        */
+       error = init_swap_ops(si);
+       if (error)
+               goto bad_swap_unlock_inode;

I checked the comment above previously and it looked good. But the error label seems not that correct after double check. inode->i_flags will keep the S_SWAPFILE flag. Maybe something like add a inode->i_flags &= ~S_SWAPFILE here and goto free_swap_zswap. Sorry I didn't check this part carefully last time.

Right. How about moving it zswap_swapon() because it only relies on si->flags setting currently?

Quoted 27 lines

But fortunately, init_swap_ops will never fail at this moment so the issue never triggers.

Quoted 18 lines
mm/swapfile.c +1 -0
+
        mutex_lock(&swapon_mutex);
        prio = DEF_SWAP_PRIO;
        if (swap_flags & SWAP_FLAG_PREFER)
mm/zswap.c +1 -2
index 4b5149173b0e..192401f46de4 100644
@@ -1054,7 +1054,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) {
--
Quoted 2 lines

2.52.0

Rest looks good to me!

#8 lore raw
Quoted 91 lines · Baoquan He
Quoted 11 lines · Kairui Song
Quoted 8 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.

opeations -> operations

Will fix, thanks.

Quoted 10 lines
Quoted 7 lines

A few nitpicks below:

Quoted 9 lines

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

Quoted 5 lines
mm/swap_io.c +0 -5
-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);

This sanify check is dropped and not added back in anywhere. This is fine, but it might be better to have a similar VM_WARN_ON_FOLIO in swap_writeout?

Good catch. In the old code, it's called by both swap_writeout() and zswap_writeback_entry(). In zswap_writeback_entry(), the folio is allocated in-place, we don't need to worry about it now. I am fine to add it back in swap_writeout() because adding it inside all three swap_write_xxx() is a little too much.

Cscope tag: __swap_writepage
# line filename / context / line
1 288 /home/bhe/code/linux/mm/page_io.c <<swap_writeout>>

             __swap_writepage(folio, swap_plug);
   2   1053  /home/bhe/code/linux/mm/zswap.c <<zswap_writeback_entry>>
             __swap_writepage(folio, NULL);
Quoted 27 lines
Quoted 27 lines

void swap_read_folio(struct folio *folio, struct swap_iocb **plug)

 {
        struct swap_info_struct *sis = __swap_entry_to_info(folio->swap);
mm/swap_io.c +1 -7
@@ -642,13 +664,7 @@ 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);
-       }
+       sis->ops->read_folio(sis, folio, plug);
 
 finish:
        if (workingset) {
mm/swapfile.c +0 -0
index 4840fd40f36f..8c42632e6765 100644
@@ -3780,6 +3780,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto free_swap_zswap;
        }
 
Quoted 14 lines

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

Quoted 8 lines
mm/swapfile.c +8 -0
+       /*
+        * init_swap_ops() sets si->ops based on flags. It does not need
+        * swapon_mutex, and must complete before enable_swap_info()
+        * exposes the device.
+        */
+       error = init_swap_ops(si);
+       if (error)
+               goto bad_swap_unlock_inode;

I checked the comment above previously and it looked good. But the error label seems not that correct after double check. inode->i_flags will keep the S_SWAPFILE flag. Maybe something like add a inode->i_flags &= ~S_SWAPFILE here and goto free_swap_zswap. Sorry I didn't check this part carefully last time.

Quoted 2 lines

Right. How about moving it zswap_swapon() because it only relies on si->flags setting currently?

You mean before zswap_swapon()? Then that sounds good to me.

author reviewer
#9 lore raw
Quoted 4 lines · Kairui Song
Quoted 3 lines · Baoquan He
Quoted 1 line · Kairui Song

On Tue, May 12, 2026 at 6:50 PM Baoquan He <[email protected]> wrote:

......snip...

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 6 lines
mm/swapfile.c +0 -0
index 4840fd40f36f..8c42632e6765 100644
@@ -3780,6 +3780,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                goto free_swap_zswap;

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

Quoted 10 lines
mm/swapfile.c +8 -0
        }
 
+       /*
+        * init_swap_ops() sets si->ops based on flags. It does not need
+        * swapon_mutex, and must complete before enable_swap_info()
+        * exposes the device.
+        */
+       error = init_swap_ops(si);
+       if (error)
+               goto bad_swap_unlock_inode;

I checked the comment above previously and it looked good. But the error label seems not that correct after double check. inode->i_flags will keep the S_SWAPFILE flag. Maybe something like add a inode->i_flags &= ~S_SWAPFILE here and goto free_swap_zswap. Sorry I didn't check this part carefully last time.

Quoted 4 lines
Quoted 2 lines

Right. How about moving it zswap_swapon() because it only relies on si->flags setting currently?

You mean before zswap_swapon()? Then that sounds good to me.

Yeah, just before zswap_swapon(). I missed a word.

#10 lore raw
In reply to Baoquan He
Quoted 4 lines

};

+struct swap_ops;
/*

Please keep empty spaces before comments. And keeping forward declarations at the beginning of the file makes them much easier to organize (although the current swap.h is a complete mess not only in this regard).

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 628

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 -EINVAL;

In the long run setting these from the helpers for ->swap_activate would make more sense. Also as ls long as the ops are a small statically defined set these checks here are a bit odd.

I guess we just need to land this ASAP, then my swap_activate series and then do a big round of cleanups for a coherent swap interface.

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

Quoted 6 lines
mm/swap_io.c +1 -5
-	} 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);

This is a really annoying double-indirection for the SWP_FS_OPS case. I think we should have one level of indirection here. One way to do this would be to remove the swap_rw operation and implement the swap_ops directly in nfs and cifs.

author reviewer
#11 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines

};

+struct swap_ops;
/*

Please keep empty spaces before comments. And keeping forward

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I may not follow, I noticed that all the comments in the code use tabs for indentation rather than spaces.

Quoted 3 lines

declarations at the beginning of the file makes them much easier to organize (although the current swap.h is a complete mess not only in this regard).

Sure, will move it at the beginning of linux/swap.h. Thanks.

Quoted 16 lines
Quoted 16 lines
mm/swap_io.c +15 -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;
+	/*
+	 * ->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
Quoted 8 lines

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

Quoted 4 lines
mm/swap_io.c +4 -0
+		sis->ops = &bdev_async_swap_ops;
+
+	if (!sis->ops || !sis->ops->read_folio || !sis->ops->write_folio)
+		return -EINVAL;

In the long run setting these from the helpers for ->swap_activate would make more sense. Also as ls long as the ops are a small statically defined set these checks here are a bit odd.

In fact, except of your ->swap_activate, I have my ->swap_activate too queued in my local branch. While from your comments, seems we don't have conflict because we have different concerns.

Please see Youngjun's comment and my reply in this thread:
https://lore.kernel.org/linux-mm/aaWiBaeSCILBCzqd@yjaykim-PowerEdge-T330/

Quoted 3 lines

I guess we just need to land this ASAP, then my swap_activate series and then do a big round of cleanups for a coherent swap interface.

This patchset is part I. I posted v1 to collect suggestions, later I took a leave, then people think the v1 can be merged firstly and helped to post v2. I later came back and continue to push this part. If you just found this patch series and come up with those cleanups, could you wait a moment until we merge this part and part II is posted? I think the split handling of block and fs in swap you mentioned is super cool, and is beyond our plan.

Quoted 14 lines

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

Quoted 7 lines
mm/swap_io.c +1 -5
-	} 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);

This is a really annoying double-indirection for the SWP_FS_OPS case. I think we should have one level of indirection here. One way to do this would be to remove the swap_rw operation and implement the swap_ops directly in nfs and cifs.

#12 lore raw
Quoted 7 lines · Baoquan He

This patchset is part I. I posted v1 to collect suggestions, later I took a leave, then people think the v1 can be merged firstly and helped to post v2. I later came back and continue to push this part. If you just found this patch series and come up with those cleanups, could you wait a moment until we merge this part and part II is posted? I think the split handling of block and fs in swap you mentioned is super cool, and is beyond our plan.

I've been looking at this a bit more, and I think it is going very much in the wrong direction. I've started drafting a version that moves block to the same model as fs to build up multi-folio bios, which actually allows for a clean abstraction.

maintainer
#13 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 7 lines · Baoquan He

This patchset is part I. I posted v1 to collect suggestions, later I took a leave, then people think the v1 can be merged firstly and helped to post v2. I later came back and continue to push this part. If you just found this patch series and come up with those cleanups, could you wait a moment until we merge this part and part II is posted? I think the split handling of block and fs in swap you mentioned is super cool, and is beyond our plan.

Quoted 4 lines

I've been looking at this a bit more, and I think it is going very much in the wrong direction. I've started drafting a version that moves block to the same model as fs to build up multi-folio bios, which actually allows for a clean abstraction.

That is for the swapin_readahead() where multiple folios can be read in one batch, right? I assume It should not impact the swap out path. Curious to see what you have in mind. The swap_vma_readahead() can hit more than one swap device. The current swap_ops is per swap device so your multi-folio bios will likely stay in the core swap layer. I will take a look at your new implementation; hopefully, the swap_ops can adapt to that as well.

Chris

maintainer
#14 lore raw
Quoted 6 lines · Chris Li
Quoted 5 lines

I've been looking at this a bit more, and I think it is going very much in the wrong direction. I've started drafting a version that moves block to the same model as fs to build up multi-folio bios, which actually allows for a clean abstraction.

Quoted 7 lines

That is for the swapin_readahead() where multiple folios can be read in one batch, right? I assume It should not impact the swap out path. Curious to see what you have in mind. The swap_vma_readahead() can hit more than one swap device. The current swap_ops is per swap device so your multi-folio bios will likely stay in the core swap layer. I will take a look at your new implementation; hopefully, the swap_ops can adapt to that as well.

Never mind that; I just saw your swap batching series. I will reply there.

Chris

#15 lore raw
In reply to Baoquan He

Looking a bit more I also think this abastraction is still leaky because the unplug calls for fs based swap aren't properly abstracted out.

(note that it would make sense to use them for block based swap as well, as building a single bio will be a lot more efficient than relying on block layer plugging, but that's a separate issue)

author reviewer
#16 lore raw
Quoted 2 lines · Christoph Hellwig

Looking a bit more I also think this abastraction is still leaky because the unplug calls for fs based swap aren't properly abstracted out.

Right, and this can be easily done by adding a unplug callback in swap_ops.

Quoted 4 lines

(note that it would make sense to use them for block based swap as well, as building a single bio will be a lot more efficient than relying on block layer plugging, but that's a separate issue)

OK, look forward to seeing it from your post.

author reviewer
#17 lore raw

PATCH v6 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 3f9aee5b6a4d..ff38adfde0fa 100644
@@ -374,7 +374,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)
 {
@@ -411,7 +411,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)
 {
@@ -432,7 +432,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)
 {
@@ -594,17 +594,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
Acked-by on v6 3/3
#18 lore raw

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

Quoted 11 lines · 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(-)
maintainer Acked-by on v6 3/3
#19 lore raw

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

In reply to Baoquan He
Quoted 8 lines · 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.

Chris

#20 lore raw

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

In reply to Baoquan He

mm/swap_io.c static void swap_read_folio_bdev_async(struct swap_info_struct *sis, · line 598

Quoted 6 lines

static const struct swap_ops bdev_fs_swap_ops = {

mm/swap_io.c +1 -1
 	.read_folio = swap_read_folio_fs,
-	.write_folio = swap_writepage_fs,
+	.write_folio = swap_write_folio_fs,
 };

The convention for method naming is prefix##method_name, i.e. swap_fs_read_folio, swap_fs_write_folio, swap_bdev_sync_read_folios, etc.

author reviewer
#21 lore raw

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

Quoted 10 lines · Christoph Hellwig

mm/swap_io.c static void swap_read_folio_bdev_async(struct swap_info_struct *sis, · line 598

Quoted 6 lines

static const struct swap_ops bdev_fs_swap_ops = {

mm/swap_io.c +1 -1
 	.read_folio = swap_read_folio_fs,
-	.write_folio = swap_writepage_fs,
+	.write_folio = swap_write_folio_fs,
 };

The convention for method naming is prefix##method_name, i.e. swap_fs_read_folio, swap_fs_write_folio, swap_bdev_sync_read_folios, etc.

To me _fs is a modifier to qualify the method type. If need change, we may need change all swap_readxxx_fs/bdev_sync/bdev_async and swap_writexxx likewise.