mm, swap: clean up swap cache mapping helper
Subsystems (9)
cover mm, swap: clean up swap cache mapping helper
From: Kairui Song <[email protected]>
This series removes usage of folio_index usage in fs/, and remove swap cache checking in folio_contains.
Currently, the swap cache is already no longer directly exposed to fs, and swap cache will be more different from page cache. Clean up the helpers first to simplify the code and eliminate the helpers used for resolving circular header dependency issue between filemap and swap headers.
Kairui Song (6):
fuse: drop usage of folio_index
btrfs: drop usage of folio_index
f2fs: drop usage of folio_index
filemap: do not use folio_contains for swap cache folios
mm: move folio_index to mm/swap.h and remove no longer needed helper mm, swap: remove no longer used swap mapping helper
fs/btrfs/extent_io.c | 2 +- fs/f2fs/data.c | 4 ++-- fs/f2fs/inline.c | 4 ++-- fs/f2fs/super.c | 2 +- fs/fuse/file.c | 4 ++-- include/linux/pagemap.h | 29 ++++------------------------- mm/memfd.c | 1 + mm/migrate.c | 1 + mm/swap.h | 18 ++++++++++++++++++ mm/swapfile.c | 15 --------------- 10 files changed, 32 insertions(+), 48 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/6 fuse: drop usage of folio_index
From: Kairui Song <[email protected]>
folio_index is only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just use folio->index instead.
It can't be a swap cache folio here. Swap mapping may only call into fs through swap_rw and that is not supported for fuse. So just drop it and use folio->index instead.
--- fs/fuse/file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/file.c +2 -2
@@ -487,7 +487,7 @@ static inline bool fuse_folio_is_writeback(struct inode *inode, struct folio *folio) {pgoff_t last =folio_next_index (folio) -1 ; -return fuse_range_is_writeback (inode,folio_index (folio), last); +return fuse_range_is_writeback (inode, folio->index, last); } static void fuse_wait_on_folio_writeback(struct inode *inode,
@@ -2349,7 +2349,7 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio,return true;/* Discontinuity */ -if (data->orig_folios[ap->num_folios -1 ]->index +1 !=folio_index (folio)) +if (data->orig_folios[ap->num_folios -1 ]->index +1 != folio->index)return true;/* Need to grow the pages array? If so, did the expansion fail? */
Signature · 1 line
2.49.0
Re: [PATCH 1/6] fuse: drop usage of folio_index
Quoted 8 lines · Kairui Song
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for fuse. So just drop it and usefolio->indexinstead.
Quoted 6 lines
uigned-off-by Kairui Song <[email protected]>Cc Miklos Szeredi <[email protected]>Cc Joanne Koong <[email protected]>Cc Josef Bacik <[email protected]>Signed-off-by Kairui Song <[email protected]>
fs/fuse/file.c static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, · line 2353
Quoted 7 lines
fs/fuse/file.c +1
-1@@ -2349,7 +2349,7 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio,return true;/* Discontinuity */ -if (data->orig_folios[ap->num_folios -1 ]->index +1 !=folio_index (folio)) +if (data->orig_folios[ap->num_folios -1 ]->index +1 != folio->index)return true;
This looks like a pre-existing bug.
- if (data->orig_folios[ap->num_folios - 1]->index + 1 != folio_index(folio))
+ prev_folio = data->orig_folios[ap->num_folios - 1];
+ if (prev_folio->index + folio_nr_pages(prev_folio) != folio->index)
return true;
Re: [PATCH 1/6] fuse: drop usage of folio_index
Quoted 10 lines · Matthew Wilcox
Quoted 8 lines · Kairui Song
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for fuse. So just drop it and usefolio->indexinstead.
Quoted 8 lines
Quoted 6 lines
uigned-off-by Kairui Song <[email protected]>Cc Miklos Szeredi <[email protected]>Cc Joanne Koong <[email protected]>Cc Josef Bacik <[email protected]>Signed-off-by Kairui Song <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>
Thanks for the review.
Quoted 12 lines
fs/fuse/file.c static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, · line 2353
Quoted 8 lines
fs/fuse/file.c +1
-1@@ -2349,7 +2349,7 @@ static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio,return true;/* Discontinuity */ -if (data->orig_folios[ap->num_folios -1 ]->index +1 !=folio_index (folio)) +if (data->orig_folios[ap->num_folios -1 ]->index +1 != folio->index)return true;This looks like a pre-existing bug.
fs/fuse/file.c static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio, · line 2353
Quoted 4 lines
fs/fuse/file.c +2
-1-if (data->orig_folios[ap->num_folios -1 ]->index +1 !=folio_index (folio)) + prev_folio = data->orig_folios[ap->num_folios -1 ]; +if (prev_folio->index +folio_nr_pages (prev_folio) != folio->index)return true;
It seems FUSE does not work with high order folios yet, a lot of allocation and operation here are assuming folio size == PAGE_SIZE. I think I'll just leave it here.
PATCH 2/6 btrfs: drop usage of folio_index
In reply to Kairui SongFrom: Kairui Song <[email protected]>
folio_index is only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just use folio->index instead.
It can't be a swap cache folio here. Swap mapping may only call into fs through swap_rw and that is not supported for btrfs. So just drop it and use folio->index instead.
--- fs/btrfs/extent_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_io.c +1 -1
@@ -3509,7 +3509,7 @@ static void btree_clear_folio_dirty_tag(struct folio *folio)xa_lock_irq (&folio->mapping->i_pages);if (!folio_test_dirty (folio))__xa_clear_mark (&folio->mapping->i_pages, -folio_index (folio),PAGECACHE_TAG_DIRTY ); + folio->index,PAGECACHE_TAG_DIRTY );xa_unlock_irq (&folio->mapping->i_pages); }
Signature · 1 line
2.49.0
Re: [PATCH 2/6] btrfs: drop usage of folio_index
Quoted 10 lines · Kairui Song
From: Kairui Song <[email protected]>
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for btrfs. So just drop it and usefolio->indexinstead.
Quoted 6 lines
Signed-off-by Kairui Song <[email protected]>Cc Chris Mason <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc Josef Bacik <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc David Sterba <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc [email protected] (open list:BTRFS FILE SYSTEM)Signed-off-by Kairui Song <[email protected]>
Re: [PATCH 2/6] btrfs: drop usage of folio_index
In reply to Kairui SongQuoted 5 lines · Kairui Song
From: Kairui Song <[email protected]>
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.
The patch looks good to me, but I'm afraid the next commit message is not accurate.
Quoted 4 lines
It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for btrfs. So just drop it and usefolio->indexinstead.
Btrfs supports swap file, it's just not through the swap_rw() callback.
In this particular case, the folio belongs to the metadata (btree) inode, thus it will never be swap cache folio.
With that changed, it looks good to me.
Thanks,
Qu
Quoted 24 lines
Signed-off-by Kairui Song <[email protected]>Cc Chris Mason <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc Josef Bacik <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc David Sterba <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc [email protected] (open list:BTRFS FILE SYSTEM)Signed-off-by Kairui Song <[email protected]>--- fs/btrfs/extent_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent_io.c +1
-1@@ -3509,7 +3509,7 @@ static void btree_clear_folio_dirty_tag(struct folio *folio)xa_lock_irq (&folio->mapping->i_pages);if (!folio_test_dirty (folio))__xa_clear_mark (&folio->mapping->i_pages, -folio_index (folio),PAGECACHE_TAG_DIRTY ); + folio->index,PAGECACHE_TAG_DIRTY );xa_unlock_irq (&folio->mapping->i_pages); }
Re: [PATCH 2/6] btrfs: drop usage of folio_index
Quoted 4 lines · Qu Wenruo
在 2025/4/28 04:29, Kairui Song 写道:
Quoted 14 lines
Quoted 5 lines
From: Kairui Song <[email protected]>
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.The patch looks good to me, but I'm afraid the next commit message is not accurate.
Quoted 4 lines
It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for btrfs. So just drop it and usefolio->indexinstead.
Thanks for the review.
Quoted 1 line
Btrfs supports swap file, it's just not through the
swap_rw()callback.
Right, I just meant btrfs is not using swap_rw. Of course it supports swap files. Let me update the commit message a bit to clarify that.
Quoted 37 lines
In this particular case, the folio belongs to the metadata (btree) inode, thus it will never be swap cache folio.
With that changed, it looks good to me.
Reviewed-by Qu Wenruo <[email protected]>Thanks,
QuQuoted 24 lines
Signed-off-by Kairui Song <[email protected]>Cc Chris Mason <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc Josef Bacik <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc David Sterba <[email protected]> (maintainer:BTRFS FILE SYSTEM)Cc [email protected] (open list:BTRFS FILE SYSTEM)Signed-off-by Kairui Song <[email protected]>--- fs/btrfs/extent_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/extent_io.c +1
-1@@ -3509,7 +3509,7 @@ static void btree_clear_folio_dirty_tag(struct folio *folio)xa_lock_irq (&folio->mapping->i_pages);if (!folio_test_dirty (folio))__xa_clear_mark (&folio->mapping->i_pages, -folio_index (folio),PAGECACHE_TAG_DIRTY ); + folio->index,PAGECACHE_TAG_DIRTY );xa_unlock_irq (&folio->mapping->i_pages); }
PATCH 3/6 f2fs: drop usage of folio_index
In reply to Kairui SongFrom: Kairui Song <[email protected]>
folio_index is only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just use folio->index instead.
It can't be a swap cache folio here. Swap mapping may only call into fs through swap_rw and that is not supported for f2fs. So just drop it and use folio->index instead.
--- fs/f2fs/data.c | 4 ++-- fs/f2fs/inline.c | 4 ++-- fs/f2fs/super.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
fs/f2fs/data.c +2 -2
@@ -2077,7 +2077,7 @@ static int f2fs_read_single_page(struct inode *inode, struct folio *folio,sector_t last_block;sector_t last_block_in_file;sector_t block_nr; -pgoff_t index =folio_index (folio); +pgoff_t index = folio->index;int ret =0 ; block_in_file = (sector_t )index;
@@ -2392,7 +2392,7 @@ static int f2fs_mpage_readpages(struct inode *inode, }#ifdef CONFIG_F2FS_FS_COMPRESSION - index =folio_index (folio); + index = folio->index;if (!f2fs_compressed_file (inode)) goto read_single_page;
fs/f2fs/inline.c +2 -2
@@ -86,7 +86,7 @@ void f2fs_do_read_inline_data(struct folio *folio, struct page *ipage)if (folio_test_uptodate (folio))return ; -f2fs_bug_on (F2FS_I_SB (inode),folio_index (folio)); +f2fs_bug_on (F2FS_I_SB (inode), folio->index);folio_zero_segment (folio,MAX_INLINE_DATA (inode),folio_size (folio));
@@ -130,7 +130,7 @@ int f2fs_read_inline_data(struct inode *inode, struct folio *folio)return -EAGAIN ; } -if (folio_index (folio)) +if (folio->index)folio_zero_segment (folio,0 ,folio_size (folio));else f2fs_do_read_inline_data (folio, ipage);
fs/f2fs/super.c +1 -1
@@ -3432,7 +3432,7 @@ static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio, bio =bio_alloc (sbi->sb->s_bdev,1 , opf,GFP_NOFS );/* it doesn't need to set crypto context for superblock update */ - bio->bi_iter.bi_sector =SECTOR_FROM_BLOCK (folio_index (folio)); + bio->bi_iter.bi_sector =SECTOR_FROM_BLOCK (folio->index);if (!bio_add_folio (bio, folio,folio_size (folio),0 ))f2fs_bug_on (sbi,1 );
Signature · 1 line
2.49.0
Re: [PATCH 3/6] f2fs: drop usage of folio_index
Quoted 8 lines · Kairui Song
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for f2fs. So just drop it and usefolio->indexinstead.
Quoted 5 lines
Signed-off-by Kairui Song <[email protected]>Cc Jaegeuk Kim <[email protected]> (maintainer:F2FS FILE SYSTEM)Cc Chao Yu <[email protected]> (maintainer:F2FS FILE SYSTEM)Cc [email protected] (open list:F2FS FILE SYSTEM)Signed-off-by Kairui Song <[email protected]>
fs/f2fs/inline.c int f2fs_read_inline_data(struct inode *inode, struct folio *folio) · line 136
Quoted 9 lines
fs/f2fs/inline.c +1
-1@@ -130,7 +130,7 @@ int f2fs_read_inline_data(struct inode *inode, struct folio *folio)return -EAGAIN ; } -if (folio_index (folio)) +if (folio->index)folio_zero_segment (folio,0 ,folio_size (folio));else f2fs_do_read_inline_data (folio, ipage);
This hunk is going to conflict with a pair of patches I sent to f2fs-devel a few weeks ago. I don't think there's any escaping it, just a heads-up.
Re: [PATCH 3/6] f2fs: drop usage of folio_index
Quoted 22 lines · Matthew Wilcox
Quoted 8 lines · Kairui Song
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for f2fs. So just drop it and usefolio->indexinstead.Quoted 5 lines
Signed-off-by Kairui Song <[email protected]>Cc Jaegeuk Kim <[email protected]> (maintainer:F2FS FILE SYSTEM)Cc Chao Yu <[email protected]> (maintainer:F2FS FILE SYSTEM)Cc [email protected] (open list:F2FS FILE SYSTEM)Signed-off-by Kairui Song <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>Quoted 4 lines
fs/f2fs/inline.c +0
-0@@ -130,7 +130,7 @@ int f2fs_read_inline_data(struct inode *inode, struct folio *folio)return -EAGAIN ; }
Quoted 9 lines
fs/f2fs/inline.c int f2fs_read_inline_data(struct inode *inode, struct folio *folio) · line 136
Quoted 5 lines
fs/f2fs/inline.c +1
-1-if (folio_index (folio)) +if (folio->index)folio_zero_segment (folio,0 ,folio_size (folio));else f2fs_do_read_inline_data (folio, ipage);This hunk is going to conflict with a pair of patches I sent to f2fs-devel a few weeks ago. I don't think there's any escaping it, just a heads-up.
Thanks for the info, this patch is just converting folio_index to folio->index so conflict should be easy to resolve I think? I can do a rebase later if that series is merged first.
Re: [f2fs-dev] [PATCH 3/6] f2fs: drop usage of folio_index
In reply to Kairui SongThis patch was applied to jaegeuk/f2fs.git (dev) by Andrew Morton <[email protected]>:
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Andrew Morton <[email protected]>:
Quoted 11 lines · On Mon, 28 Apr 2025 02:59:05 +0800 you wrote:
From: Kairui Song <[email protected]>
folio_indexis only needed for mixed usage of page cache and swap cache, for pure page cache usage, the caller can just usefolio->indexinstead.It can't be a swap cache folio here. Swap mapping may only call into fs through
swap_rwand that is not supported for f2fs. So just drop it and usefolio->indexinstead.[...]
Here is the summary with links:
- [f2fs-dev,3/6] f2fs: drop usage of folio_index
https://git.kernel.org/jaegeuk/f2fs/c/fe15ec046431
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
PATCH 4/6 filemap: do not use folio_contains for swap cache folios
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Currently, none of the folio_contains callers will encounter swap cache folios.
For fs/ callers, swap cache folios are never part of their workflow.
For filemap and truncate, folio_contains is only used for sanity checks to verify the folio index matches the expected lookup/invalidation target. The swap cache does not utilize filemap or truncate helpers in ways that would trigger these checks, as it mostly implements its own cache management.
Shmem won't interact with these sanity checks either unless thing went wrong, it would directly trigger a BUG, because swap cache index are unrelated to shmem index, and would almost certainly mismatch (unless on collide).
While some filemap helpers works for swap cache space, the swap cache is different from the page cache in many ways. So this helper will unlikely to work in a helpful way for swap cache folios.
So make it explicit here that folio_contains should not be used for swap cache folios. This helps to avoid misuse and remove the folio_index usage here.
--- include/linux/pagemap.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/pagemap.h +4 -4
@@ -935,14 +935,14 @@ static inline struct page *folio_file_page(struct folio *folio, pgoff_t index)* @folio: The folio. * @index: The page index within the file. * -* Context: The caller should have the page locked in order to prevent -* (eg) shmem from moving the page between the page cache and swap cache -* and changing its index in the middle of the operation. +* Context: The caller should ensure folio->index is stable and it's +* not added to the swap cache. * Return: true or false. */ static inline bool folio_contains (struct folio *folio,pgoff_t index) { -return index -folio_index (folio) <folio_nr_pages (folio); +VM_BUG_ON_FOLIO (folio_test_swapcache (folio), folio); +return index - folio->index <folio_nr_pages (folio); } unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
Signature · 1 line
2.49.0
Matthew Wilcox
maintainerRe: [PATCH 4/6] filemap: do not use folio_contains for swap cache folios
Quoted 9 lines · Kairui Song
For filemap and truncate,
folio_containsis only used for sanity checks to verify the folio index matches the expected lookup/invalidation target. The swap cache does not utilize filemap or truncate helpers in ways that would trigger these checks, as it mostly implements its own cache management.Shmem won't interact with these sanity checks either unless thing went wrong, it would directly trigger a BUG, because swap cache index are unrelated to shmem index, and would almost certainly mismatch (unless on collide).
It does happen though. If shmem is writing the folio to swap at the same time that the file containing the folio is being truncated, we can hit this.
Quoted 5 lines
- * Context: The caller should have the page locked in order to prevent
- * (eg) shmem from moving the page between the page cache and swap cache
- * and changing its index in the middle of the operation.
+ * Context: The caller should ensurefolio->indexis stable and it's
+ * not added to the swap cache.
I do think we need to keep the part about the folio being locked.
Re: [PATCH 4/6] filemap: do not use folio_contains for swap cache folios
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Kairui Song
For filemap and truncate,
folio_containsis only used for sanity checks to verify the folio index matches the expected lookup/invalidation target. The swap cache does not utilize filemap or truncate helpers in ways that would trigger these checks, as it mostly implements its own cache management.
Quoted 8 lines
Quoted 4 lines
Shmem won't interact with these sanity checks either unless thing went wrong, it would directly trigger a BUG, because swap cache index are unrelated to shmem index, and would almost certainly mismatch (unless on collide).
It does happen though. If shmem is writing the folio to swap at the same time that the file containing the folio is being truncated, we can hit this.
Thanks for the info! I didn't check it in detail because that would likley trigger a BUG_ON but so far I didn't see any BUG_ON commit from there.
Just checked there are two users in truncate:
One will lock the folio and check if folio->mapping != mapping first, on swapout shmem removes the folio from shmem mapping so this check will skip the folio_contains check.
Another seems might hit the check, the time window is extremely tiny though, only if the truncate's xa_is_value(folio) check passed while another CPU is running between folio_alloc_swap and shmem_delete_from_page_cache in shmem_writepage, then the next VM_BUG_ON_FOLIO(!folio_contains) will fail as folio is now a swap cache, not shmem folio anymore. Let me double check if this needs another fix.
Re: [PATCH 4/6] filemap: do not use folio_contains for swap cache folios
Quoted 28 lines · Kairui Song
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Kairui Song
For filemap and truncate,
folio_containsis only used for sanity checks to verify the folio index matches the expected lookup/invalidation target. The swap cache does not utilize filemap or truncate helpers in ways that would trigger these checks, as it mostly implements its own cache management.Quoted 8 lines
Quoted 4 lines
Shmem won't interact with these sanity checks either unless thing went wrong, it would directly trigger a BUG, because swap cache index are unrelated to shmem index, and would almost certainly mismatch (unless on collide).
It does happen though. If shmem is writing the folio to swap at the same time that the file containing the folio is being truncated, we can hit this.
Thanks for the info! I didn't check it in detail because that would likley trigger a
BUG_ONbut so far I didn't see anyBUG_ONcommit from there.Just checked there are two users in truncate:
One will lock the folio and check if
folio->mapping != mappingfirst, on swapout shmem removes the folio from shmem mapping so this check will skip thefolio_containscheck.
Quoted 7 lines
Another seems might hit the check, the time window is extremely tiny though, only if the truncate's
xa_is_value(folio)check passed while another CPU is running betweenfolio_alloc_swapandshmem_delete_from_page_cacheinshmem_writepage, then the nextVM_BUG_ON_FOLIO(!folio_contains)will fail as folio is now a swap cache, not shmem folio anymore. Let me double check if this needs another fix.
Checking all the code path, shmem managed to avoid all possible ways to call into truncate_inode_pages_range, this is the only function that seemed may call folio_contains with a swap cache folio. (except tiny-shmem, it uses this function directly for truncate, we can ignore that as it's basically just ramfs).
For truncate, shmem need to either zap a whole (large) swap/folio entry, or zero part of folio, or swapin a large folio so that part of it can be zero'ed (using shmem_get_partial_folio), the swapin part is a bit special so calling generic truncate helpers might cause unexpected behaviour.
Similar story for filemap lookup.
So shmem won't call into the truncate helper here that may risk calling folio_contains with swap cache.
Even if somehow it does, this commit won't change the BUG_ON behaviour, except now it tells the user the folio shouldn't be a swap cache at all, instead of reporting a buggy index. So I think this commit should be good to have to make the swap cache less exposed.
---
List of potential call chains that may call into the truncate helper here, and not initialized from other FS / block, none will be used by shmem.
do_dentry_open
/* filemap_nr_thps always 0 for shmem */
truncate_inode_pages
truncate_inode_pages_range
dquot_disable /* No quota file for shmem */
truncate_inode_pages
truncate_inode_pages_range
dquot_quota_sync /* No quota file for shmem */
truncate_inode_pages
truncate_inode_pages_range
truncate_inode_pages_final /* Override by shmem_evict_inode */
truncate_inode_pages
truncate_inode_pages_range
simple_setattr /* Override by shmem_setattr */
truncate_setsize
truncate_pagecache
truncate_inode_pages
truncate_inode_pages_range
put_aio_ring_file /* AIO calls it for private file */
truncate_setsize
truncate_pagecache
truncate_inode_pages
truncate_inode_pages_range
truncate_pagecache /* No user except other fs */
truncate_inode_pages
truncate_inode_pages_range
truncate_pagecache_range /* No user except other fs */
truncate_inode_pages_range
---
invalidate_inode_pages2 /* No user except other fs */
invalidate_inode_pages2_range
filemap_invalidate_pages /* only used by block / direct io */
invalidate_inode_pages2_range
filemap_invalidate_inode /* No user except other fs */
invalidate_inode_pages2_range
kiocb_invalidate_post_direct_write /* only used by block / direct io */
invalidate_inode_pages2_range
PATCH 5/6 mm: move folio_index to mm/swap.h and remove no longer needed helper
In reply to Kairui SongFrom: Kairui Song <[email protected]>
There are no remaining users of folio_index() outside the mm subsystem. Move it to mm/swap.h to co-locate it with swap_cache_index(), eliminating a forward declaration, and a function call overhead.
Also remove the helper that was used to fix circular header dependency issue.
--- include/linux/pagemap.h | 20 -------------------- mm/memfd.c | 1 + mm/migrate.c | 1 + mm/swap.h | 18 ++++++++++++++++++ mm/swapfile.c | 6 ------ 5 files changed, 20 insertions(+), 26 deletions(-)
include/linux/pagemap.h +0 -20
@@ -884,26 +884,6 @@ static inline struct page *grab_cache_page_nowait(struct address_space *mapping, mapping_gfp_mask(mapping)); } -extern pgoff_t __folio_swap_cache_index (struct folio *folio); - -/** -* folio_index - File index of a folio. -* @folio: The folio. -* -* For a folio which is either in the page cache or the swap cache, -* return its index within the address_space it belongs to. If you know -* the page is definitely in the page cache, you can look at the folio's -* index directly. -* -* Return: The index (offset in units of pages) of a folio in its file. -*/ -static inline pgoff_t folio_index (struct folio *folio) -{ -if (unlikely (folio_test_swapcache (folio))) -return __folio_swap_cache_index (folio); -return folio->index; -} -/** * folio_next_index - Get the index of the next folio. * @folio: The current folio.
mm/memfd.c +1 -0
@@ -20,6 +20,7 @@#include <linux/memfd.h> #include <linux/pid_namespace.h> #include <uapi/linux/memfd.h> +#include "swap.h" /* * We need a tag: a new tag would expand every xa_node by 8 bytes,
mm/migrate.c +1 -0
@@ -50,6 +50,7 @@#include <trace/events/migrate.h> #include "internal.h" +#include "swap.h" bool isolate_movable_page (struct page *page,isolate_mode_t mode) {
mm/swap.h +18 -0
@@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; } +/** +* folio_index - File index of a folio. +* @folio: The folio. +* +* For a folio which is either in the page cache or the swap cache, +* return its index within the address_space it belongs to. If you know +* the page is definitely in the page cache, you can look at the folio's +* index directly. +* +* Return: The index (offset in units of pages) of a folio in its file. +*/ +static inline pgoff_t folio_index (struct folio *folio) +{ +if (unlikely (folio_test_swapcache (folio))) +return swap_cache_index (folio->swap); +return folio->index; +} +void show_swap_cache_info (void );void *get_shadow_from_swap_cache (swp_entry_t entry); int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
mm/swapfile.c +0 -6
@@ -3662,12 +3662,6 @@ struct address_space *swapcache_mapping(struct folio *folio) }EXPORT_SYMBOL_GPL (swapcache_mapping); -pgoff_t __folio_swap_cache_index (struct folio *folio) -{ -return swap_cache_index (folio->swap); -} -EXPORT_SYMBOL_GPL (__folio_swap_cache_index); -/* * add_swap_count_continuation - called when a swap count is duplicated * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
kernel test robot noticed the following build errors:
Hi Kairui,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Kairui-Song/fuse-drop-usage-of-folio_index/20250428-030234 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250427185908.90450-6-ryncsn%40gmail.com patch subject: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20250428/[email protected]/config) compiler: m68k-linux-gcc (GCC) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250428/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
mm/page-writeback.c: In function '__folio_mark_dirty':
>> mm/page-writeback.c:2767:50: error: implicit declaration of function 'folio_index'; did you mean 'folio_inode'? [-Wimplicit-function-declaration]
2767 | __xa_set_mark(&mapping->i_pages, folio_index(folio),
| ^~~~~~~~~~~
| folio_inode
--
mm/gup.c: In function 'memfd_pin_folios':
>> mm/gup.c:3652:49: error: implicit declaration of function 'folio_index'; did you mean 'folio_inode'? [-Wimplicit-function-declaration]
3652 | next_idx != folio_index(fbatch.folios[i]))
| ^~~~~~~~~~~
| folio_inodevim +2767 mm/page-writeback.c
b9ea25152e5636 Konstantin Khlebnikov 2015-04-14 2742
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2743) /*
dc6e0ae5b1700c Kemeng Shi 2024-04-25 2744 * Mark the folio dirty, and set it dirty in the page cache.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2745) *
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2746) * If warn is true, then emit a warning if the folio is not uptodate and has
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2747) * not been truncated.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2748) *
a8cd9d4ce35eae Shakeel Butt 2024-10-24 2749 * It is the caller's responsibility to prevent the folio from being truncated
a8cd9d4ce35eae Shakeel Butt 2024-10-24 2750 * while this function is in progress, although it may have been truncated
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2751) * before this function is called. Most callers have the folio locked.
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2752) * A few have the folio blocked from truncation through other means (e.g.
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2753) * zap_vma_pages() has it mapped and is holding the page table lock).
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2754) * When called from mark_buffer_dirty(), the filesystem should hold a
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2755) * reference to the buffer_head that is being marked dirty, which causes
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2756) * try_to_free_buffers() to fail.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2757) */
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2758) void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2759) int warn)
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2760) {
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2761) unsigned long flags;
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2762)
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2763) xa_lock_irqsave(&mapping->i_pages, flags);
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2764) if (folio->mapping) { /* Race with truncate? */
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2765) WARN_ON_ONCE(warn && !folio_test_uptodate(folio));
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2766) folio_account_dirtied(folio, mapping);
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 @2767) __xa_set_mark(&mapping->i_pages, folio_index(folio),
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2768) PAGECACHE_TAG_DIRTY);
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2769) }
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2770) xa_unlock_irqrestore(&mapping->i_pages, flags);
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2771) }
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2772) Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
In reply to Kairui Songkernel test robot noticed the following build errors:
Hi Kairui,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Kairui-Song/fuse-drop-usage-of-folio_index/20250428-030234 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250427185908.90450-6-ryncsn%40gmail.com patch subject: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20250428/[email protected]/config) compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f819f46284f2a79790038e1f6649172789734ae8) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250428/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> mm/page-writeback.c:2767:36: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
2767 | __xa_set_mark(&mapping->i_pages, folio_index(folio),
| ^
mm/page-writeback.c:2767:36: note: did you mean 'folio_inode'?
include/linux/pagemap.h:567:29: note: 'folio_inode' declared here
567 | static inline struct inode *folio_inode(struct folio *folio)
| ^
mm/page-writeback.c:3047:38: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
3047 | __xa_clear_mark(&mapping->i_pages, folio_index(folio),
| ^
mm/page-writeback.c:3084:36: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
3084 | XA_STATE(xas, &mapping->i_pages, folio_index(folio));
| ^
3 errors generated.
--
>> mm/gup.c:3652:21: error: call to undeclared function 'folio_index'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
3652 | next_idx != folio_index(fbatch.folios[i]))
| ^ mm/gup.c:3652:21: note: did you mean 'folio_inode'? include/linux/pagemap.h:567:29: note: 'folio_inode' declared here
567 | static inline struct inode *folio_inode(struct folio *folio)
| ^
1 error generated.
vim +/folio_index +2767 mm/page-writeback.c
b9ea25152e5636 Konstantin Khlebnikov 2015-04-14 2742
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2743) /*
dc6e0ae5b1700c Kemeng Shi 2024-04-25 2744 * Mark the folio dirty, and set it dirty in the page cache.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2745) *
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2746) * If warn is true, then emit a warning if the folio is not uptodate and has
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2747) * not been truncated.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2748) *
a8cd9d4ce35eae Shakeel Butt 2024-10-24 2749 * It is the caller's responsibility to prevent the folio from being truncated
a8cd9d4ce35eae Shakeel Butt 2024-10-24 2750 * while this function is in progress, although it may have been truncated
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2751) * before this function is called. Most callers have the folio locked.
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2752) * A few have the folio blocked from truncation through other means (e.g.
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2753) * zap_vma_pages() has it mapped and is holding the page table lock).
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2754) * When called from mark_buffer_dirty(), the filesystem should hold a
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2755) * reference to the buffer_head that is being marked dirty, which causes
3d84d897920c75 Matthew Wilcox (Oracle 2024-04-16 2756) * try_to_free_buffers() to fail.
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2757) */
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2758) void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2759) int warn)
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2760) {
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2761) unsigned long flags;
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2762)
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2763) xa_lock_irqsave(&mapping->i_pages, flags);
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2764) if (folio->mapping) { /* Race with truncate? */
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2765) WARN_ON_ONCE(warn && !folio_test_uptodate(folio));
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 2766) folio_account_dirtied(folio, mapping);
203a3151661611 Matthew Wilcox (Oracle 2021-05-04 @2767) __xa_set_mark(&mapping->i_pages, folio_index(folio),
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2768) PAGECACHE_TAG_DIRTY);
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2769) }
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2770) xa_unlock_irqrestore(&mapping->i_pages, flags);
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2771) }
6e1cae881a0646 Matthew Wilcox (Oracle 2021-06-28 2772) Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Matthew Wilcox
maintainerRe: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
In reply to Kairui SongQuoted 12 lines · Kairui Song
+++
b/mm/swap.hmm/swap.h +7
-0@@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; } +/** +* folio_index - File index of a folio. +* @folio: The folio. +* +* For a folio which is either in the page cache or the swap cache, +* return its index within the address_space it belongs to. If you know +* the page is definitely in the page cache, you can look at the folio's
mm/swap.h static inline pgoff_t swap_cache_index(swp_entry_t entry) · line 69
Quoted 10 lines
mm/swap.h +10
-0+* index directly. +* +* Return: The index (offset in units of pages) of a folio in its file. +*/ +static inline pgoff_t folio_index (struct folio *folio) +{ +if (unlikely (folio_test_swapcache (folio))) +return swap_cache_index (folio->swap); +return folio->index; +}
I think the build warning can be cured by moving this outside CONFIG_SWAP
Re: [PATCH 5/6] mm: move folio_index to mm/swap.h and remove no longer needed helper
Quoted 19 lines · Matthew Wilcox
Quoted 17 lines · Kairui Song
+++
b/mm/swap.hmm/swap.h +12
-0@@ -50,6 +50,24 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; } +/** +* folio_index - File index of a folio. +* @folio: The folio. +* +* For a folio which is either in the page cache or the swap cache, +* return its index within the address_space it belongs to. If you know +* the page is definitely in the page cache, you can look at the folio's +* index directly. +* +* Return: The index (offset in units of pages) of a folio in its file. +*/ +static inline pgoff_t folio_index (struct folio *folio)
Quoted 9 lines
mm/swap.h static inline pgoff_t swap_cache_index(swp_entry_t entry) · line 69
Quoted 5 lines
mm/swap.h +5
-0+{ +if (unlikely (folio_test_swapcache (folio))) +return swap_cache_index (folio->swap); +return folio->index; +}I think the build warning can be cured by moving this outside
CONFIG_SWAP
Thanks, I will fix it in V2.
PATCH 6/6 mm, swap: remove no longer used swap mapping helper
In reply to Kairui SongFrom: Kairui Song <[email protected]>
This helper existed to fix the circular header dependency issue but it is no longer used, so remove it.
--- include/linux/pagemap.h | 1 - mm/swapfile.c | 9 --------- 2 files changed, 10 deletions(-)
include/linux/pagemap.h +0 -1
@@ -533,7 +533,6 @@ static inline void filemap_nr_thps_dec(struct address_space *mapping) }struct address_space *folio_mapping (struct folio *); -struct address_space *swapcache_mapping (struct folio *);/** * folio_flush_mapping - Find the file mapping this folio belongs to.
mm/swapfile.c +0 -9
@@ -3653,15 +3653,6 @@ struct swap_info_struct *swp_swap_info(swp_entry_t entry)return swap_type_to_swap_info (swp_type (entry)); } -/* -* out-of-line methods to avoid include hell. -*/ -struct address_space *swapcache_mapping (struct folio *folio) -{ -return swp_swap_info (folio->swap)->swap_file->f_mapping; -} -EXPORT_SYMBOL_GPL (swapcache_mapping); -/* * add_swap_count_continuation - called when a swap count is duplicated * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
Signature · 1 line
2.49.0
Re: [PATCH 6/6] mm, swap: remove no longer used swap mapping helper
Quoted 4 lines · Kairui Song
From: Kairui Song <[email protected]>
This helper existed to fix the circular header dependency issue but it is no longer used, so remove it.
Oh, good catch, It became unused with my commit 0d40cfe63a2f and I didn't notice.
Quoted 32 lines · Kairui Song
From: Kairui Song <[email protected]>
This series removes usage of
folio_indexusage infs/, and remove swap cache checking infolio_contains.Currently, the swap cache is already no longer directly exposed to fs, and swap cache will be more different from page cache. Clean up the helpers first to simplify the code and eliminate the helpers used for resolving circular header dependency issue between filemap and swap headers.
Kairui Song (6):
fuse: drop usage offolio_index
btrfs: drop usage offolio_index
f2fs: drop usage offolio_index
filemap: do not usefolio_containsfor swap cache folios
mm: movefolio_indextomm/swap.hand remove no longer needed helper mm, swap: remove no longer used swap mapping helperfs/btrfs/extent_io.c | 2 +- fs/f2fs/data.c | 4 ++-- fs/f2fs/inline.c | 4 ++-- fs/f2fs/super.c | 2 +- fs/fuse/file.c | 4 ++-- include/linux/pagemap.h | 29 ++++------------------------- mm/memfd.c | 1 + mm/migrate.c | 1 + mm/swap.h | 18 ++++++++++++++++++ mm/swapfile.c | 15 --------------- 10 files changed, 32 insertions(+), 48 deletions(-)
Quoted 3 lines
--
2.49.0
I just realized I added duplicated Sign-off-by in a few commits, sorry about it.
Matthew Wilcox
maintainerQuoted 2 lines · Kairui Song
This series removes usage of
folio_indexusage infs/, and remove swap cache checking infolio_contains.
Thanks. I've been removing them as I've come across them (eg fcd4904e2f69), but I haven't gone all out to try to remove it from all filesystems.