mm/swap: use swap_ops to register swap device's methods
Subsystems (3)
Baoquan He
reviewerv5 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:
===
-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 | 4 ++
mm/zswap.c | 2 +-
7 files changed, 78 insertions(+), 50 deletions(-)
rename mm/{page_io.c => swap_io.c} (90%)Signature · 1 line
2.52.0
Baoquan He
reviewerPATCH v5 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
@@ -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
@@ -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
@@ -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
@@ -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
Baoquan He
reviewerPATCH v5 2/3 mm/swap: use swap_ops to register swap device's methods
In reply to Baoquan HeThis 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 | 4 ++ mm/zswap.c | 2 +- 5 files changed, 75 insertions(+), 45 deletions(-)
include/linux/swap.h +2 -0
@@ -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
@@ -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 +4 -0
@@ -3518,6 +3518,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + error =init_swap_ops (si); +if (error) + 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
@@ -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
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_syncandbdev_async, and only operationsread_folioandwrite_folioare included. In the future, there could be more swap device types added and more appropriate opeations adapted intoswap_ops.
Hi Baoquan,
Thanks for the patch. Sorry I was busy with travel and a few other series and didn't got a chance to look at a few previous versions.
mm/swap.h extern void __swap_cluster_free_entries(struct swap_info_struct *si, · line 227
Quoted 8 lines
mm/swap.h +8
-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); +};
Overall, I really like this idea, we can have a cleaner interface starting there.
mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3524
Quoted 12 lines
mm/swapfile.c +4
-0@@ -3518,6 +3518,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + error =init_swap_ops (si); +if (error) + goto bad_swap_unlock_inode; +
But this part seems wrong? init_swap_ops is called before setup_swap_extents -> swap_activate sets SWP_FS_OPS, or SWP_SYNCHRONOUS_IO is set below, so the branches in init_swap_ops never take effect, am I right? You need to move this someplace after these flags are set. Maybe right before swapon_mutex so the mutex is a clean barrier that the swap device will be used and all things before that will be seen by users, with some comments.
Baoquan He
reviewerQuoted 35 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_syncandbdev_async, and only operationsread_folioandwrite_folioare included. In the future, there could be more swap device types added and more appropriate opeations adapted intoswap_ops.Hi Baoquan,
Thanks for the patch. Sorry I was busy with travel and a few other series and didn't got a chance to look at a few previous versions.
mm/swap.h extern void __swap_cluster_free_entries(struct swap_info_struct *si, · line 227
Quoted 8 lines
mm/swap.h +8
-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); +};Overall, I really like this idea, we can have a cleaner interface starting there.
Quoted 8 lines
mm/swapfile.c +0
-0@@ -3518,6 +3518,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; }
Quoted 12 lines
mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3524
Quoted 4 lines
mm/swapfile.c +4
-0+ error =init_swap_ops (si); +if (error) + goto bad_swap_unlock_inode; +But this part seems wrong?
init_swap_opsis called beforesetup_swap_extents->swap_activatesetsSWP_FS_OPS, orSWP_SYNCHRONOUS_IOis set below, so the branches ininit_swap_opsnever take effect, am I right? You need to move this someplace after these flags are set. Maybe right beforeswapon_mutexso the mutex is a clean barrier that the swap device will be used and all things before that will be seen by users, with some comments.
You are right, this is wrong.
I checked my queued local patches, I added code relatd to activation/deaction and register/unregister according to Youngjun's comments. Later Barry helped post v2 when I took leave. I just picked code following Barry's v2 to post v3. This wrong code defaults to take bdev_async_swap_ops, so I didn't catch it when testing.
I will post v6 to fix it. Thanks a lot for careful reviewing and catching it.
Thanks
Baoquan
Baoquan He
reviewerQuoted 35 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_syncandbdev_async, and only operationsread_folioandwrite_folioare included. In the future, there could be more swap device types added and more appropriate opeations adapted intoswap_ops.Hi Baoquan,
Thanks for the patch. Sorry I was busy with travel and a few other series and didn't got a chance to look at a few previous versions.
Review
Quoted 8 lines
(quoted diff) +8
-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); +};Overall, I really like this idea, we can have a cleaner interface starting there.
Quoted 8 lines
mm/swapfile.c +0
-0@@ -3518,6 +3518,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; }
Quoted 12 lines
mm/swapfile.c SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) · line 3524
Quoted 4 lines
mm/swapfile.c +4
-0+ error =init_swap_ops (si); +if (error) + goto bad_swap_unlock_inode; +But this part seems wrong?
init_swap_opsis called beforesetup_swap_extents->swap_activatesetsSWP_FS_OPS, orSWP_SYNCHRONOUS_IOis set below, so the branches ininit_swap_opsnever take effect, am I right? You need to move this someplace after these flags are set. Maybe right beforeswapon_mutexso the mutex is a clean barrier that the swap device will be used and all things before that will be seen by users, with some comments.
Speaking of comments, do you think below sentences are OK to you?
mm/swapfile.c +9 -4
@@ -3518,10 +3518,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } - error =init_swap_ops (si); -if (error) - goto bad_swap_unlock_inode; - si->max = maxpages; si->pages = maxpages -1 ; nr_extents =setup_swap_extents (si, swap_file, &span);
@@ -3616,6 +3612,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 )
Quoted 20 lines · Baoquan He
Speaking of comments, do you think below sentences are OK to you?
mm/swapfile.c +0
-4@@ -3518,10 +3518,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } - error =init_swap_ops (si); -if (error) - goto bad_swap_unlock_inode; - si->max = maxpages; si->pages = maxpages -1 ; nr_extents =setup_swap_extents (si, swap_file, &span);@@ -3616,6 +3612,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 3623
Quoted 10 lines
mm/swapfile.c +9
-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; +
Right, LGTM!
Quoted 3 lines
mm/swapfile.c +0
-0mutex_lock (&swapon_mutex); prio =DEF_SWAP_PRIO ;if (swap_flags &SWAP_FLAG_PREFER )
Baoquan He
reviewerPATCH v5 3/3 mm/swap_io.c: rename swap_writepage_* to swap_write_folio_*
In reply to Baoquan HeAll 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
@@ -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