mm/filemap: make release_folio mandatory and add block_release_folio
Subsystems (15)
RFC PATCH 1/1 mm/filemap: make release_folio mandatory and add block_release_folio
Currently, filemap_release_folio() falls back to try_to_free_buffers() when a filesystem's address_space_operations doesn't provide a release_folio callback. This creates an inconsistent API where some buffer_head-based filesystems define release_folio while others rely on the fallback behavior.
Make the release_folio callback mandatory by:
1. Adding block_release_folio() in fs/buffer.c as the default implementation for buffer_head-based filesystems. This simply calls try_to_free_buffers().
2. Updating all buffer_head-based filesystems that use buffer_migrate_folio() to set .release_folio = block_release_folio in their address_space_operations.
3. Removing the NULL check and try_to_free_buffers() fallback in filemap_release_folio(). Now it always calls the release_folio callback when a mapping exists.
With these changes:
- Makes the pattern explicit across all buffer_head filesystems
- Prevents future bugs from missing release_folio implementations
Filesystems updated: adfs, affs, bfs, exfat, ext2, fat, hpfs, isofs, jfs, minix, nilfs2, ntfs3, omfs, udf, ufs, plus block/fops.c
Filesystems with existing custom implementations (ext4, gfs2, hfs, hfsplus, jfs metapage, ocfs2) are unchanged.
--- block/fops.c | 1 + fs/adfs/inode.c | 1 + fs/affs/file.c | 3 ++- fs/bfs/file.c | 1 + fs/buffer.c | 17 +++++++++++++++++ fs/exfat/inode.c | 1 + fs/ext2/inode.c | 1 + fs/fat/inode.c | 1 + fs/hpfs/file.c | 1 + fs/isofs/inode.c | 3 ++- fs/jfs/inode.c | 1 + fs/minix/inode.c | 3 ++- fs/nilfs2/inode.c | 1 + fs/nilfs2/mdt.c | 1 + fs/ntfs3/inode.c | 1 + fs/omfs/file.c | 1 + fs/udf/inode.c | 1 + fs/ufs/inode.c | 3 ++- include/linux/buffer_head.h | 2 ++ mm/filemap.c | 4 ++-- 20 files changed, 42 insertions(+), 6 deletions(-)
block/fops.c +1 -0
@@ -532,6 +532,7 @@ const struct address_space_operations def_blk_aops = { .write_end = blkdev_write_end, .migrate_folio = buffer_migrate_folio_norefs, .is_dirty_writeback = buffer_check_dirty_writeback, + .release_folio = block_release_folio, };#else /* CONFIG_BUFFER_HEAD */ static int blkdev_read_folio (struct file *file,struct folio *folio)
fs/adfs/inode.c +1 -0
@@ -83,6 +83,7 @@ static const struct address_space_operations adfs_aops = { .write_end = generic_write_end, .migrate_folio = buffer_migrate_folio, .bmap = _adfs_bmap, + .release_folio = block_release_folio, };/*
fs/affs/file.c +2 -1
@@ -464,7 +464,8 @@ const struct address_space_operations affs_aops = { .write_end = affs_write_end, .direct_IO = affs_direct_IO, .migrate_folio = buffer_migrate_folio, - .bmap = _affs_bmap + .bmap = _affs_bmap, + .release_folio = block_release_folio, }; static inline struct buffer_head *
fs/bfs/file.c +1 -0
@@ -198,6 +198,7 @@ const struct address_space_operations bfs_aops = { .write_end = generic_write_end, .migrate_folio = buffer_migrate_folio, .bmap = bfs_bmap, + .release_folio = block_release_folio, };const struct inode_operations bfs_file_inops;
fs/buffer.c +17 -0
@@ -2914,6 +2914,23 @@ drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)return false; } +/** +* block_release_folio - Release buffers attached to a BH-based filesystems. +* @folio: The folio. +* @gfp: The gfp parameter. +* +* This is the default release_folio implementation for buffer_head based +* filesystems. Filesystems with special requirements +* should implement their own release_folio callback. +* +* Return: true if buffers were released, false otherwise. +*/ +bool block_release_folio (struct folio *folio,gfp_t gfp) +{ +return try_to_free_buffers (folio); +} +EXPORT_SYMBOL (block_release_folio); +/** * try_to_free_buffers - Release buffers attached to this folio. * @folio: The folio.
fs/exfat/inode.c +1 -0
@@ -567,6 +567,7 @@ static const struct address_space_operations exfat_aops = { .direct_IO = exfat_direct_IO, .bmap = exfat_aop_bmap, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, };static inline unsigned long exfat_hash (loff_t i_pos)
fs/ext2/inode.c +1 -0
@@ -980,6 +980,7 @@ const struct address_space_operations ext2_aops = { .migrate_folio = buffer_migrate_folio, .is_partially_uptodate = block_is_partially_uptodate, .error_remove_folio = generic_error_remove_folio, + .release_folio = block_release_folio, };static const struct address_space_operations ext2_dax_aops = {
fs/fat/inode.c +1 -0
@@ -348,6 +348,7 @@ static const struct address_space_operations fat_aops = { .direct_IO = fat_direct_IO, .bmap = _fat_bmap, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, };/*
fs/hpfs/file.c +1 -0
@@ -252,6 +252,7 @@ const struct address_space_operations hpfs_aops = { .write_end = hpfs_write_end, .bmap = _hpfs_bmap, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, }; const struct file_operations hpfs_file_ops =
fs/isofs/inode.c +2 -1
@@ -1162,7 +1162,8 @@ static sector_t _isofs_bmap(struct address_space *mapping, sector_t block)static const struct address_space_operations isofs_aops = { .read_folio = isofs_read_folio, .readahead = isofs_readahead, - .bmap = _isofs_bmap + .bmap = _isofs_bmap, + .release_folio = block_release_folio, };static int isofs_read_level3_size (struct inode *inode)
fs/jfs/inode.c +1 -0
@@ -364,6 +364,7 @@ const struct address_space_operations jfs_aops = { .bmap = jfs_bmap, .direct_IO = jfs_direct_IO, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, };/*
fs/minix/inode.c +2 -1
@@ -486,7 +486,8 @@ static const struct address_space_operations minix_aops = { .write_end = generic_write_end, .migrate_folio = buffer_migrate_folio, .bmap = minix_bmap, - .direct_IO = noop_direct_IO + .direct_IO = noop_direct_IO, + .release_folio = block_release_folio, };static const struct inode_operations minix_symlink_inode_operations = {
fs/nilfs2/inode.c +1 -0
@@ -280,6 +280,7 @@ const struct address_space_operations nilfs_aops = { .direct_IO = nilfs_direct_IO, .migrate_folio = buffer_migrate_folio_norefs, .is_partially_uptodate = block_is_partially_uptodate, + .release_folio = block_release_folio, };const struct address_space_operations nilfs_buffer_cache_aops = {
fs/nilfs2/mdt.c +1 -0
@@ -443,6 +443,7 @@ static const struct address_space_operations def_mdt_aops = { .invalidate_folio = block_invalidate_folio, .writepages = nilfs_mdt_writeback, .migrate_folio = buffer_migrate_folio_norefs, + .release_folio = block_release_folio, };static const struct inode_operations def_mdt_iops;
fs/ntfs3/inode.c +1 -0
@@ -2102,6 +2102,7 @@ const struct address_space_operations ntfs_aops = { .dirty_folio = block_dirty_folio, .migrate_folio = buffer_migrate_folio, .invalidate_folio = block_invalidate_folio, + .release_folio = block_release_folio, };const struct address_space_operations ntfs_aops_cmpr = {
fs/omfs/file.c +1 -0
@@ -376,5 +376,6 @@ const struct address_space_operations omfs_aops = { .write_end = generic_write_end, .bmap = omfs_bmap, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, };
fs/udf/inode.c +1 -0
@@ -335,6 +335,7 @@ const struct address_space_operations udf_aops = { .direct_IO = udf_direct_IO, .bmap = udf_bmap, .migrate_folio = buffer_migrate_folio, + .release_folio = block_release_folio, };/*
fs/ufs/inode.c +2 -1
@@ -514,7 +514,8 @@ const struct address_space_operations ufs_aops = { .write_begin = ufs_write_begin, .write_end = ufs_write_end, .migrate_folio = buffer_migrate_folio, - .bmap = ufs_bmap + .bmap = ufs_bmap, + .release_folio = block_release_folio, };static void ufs_set_inode_ops (struct inode *inode)
include/linux/buffer_head.h +2 -0
@@ -515,6 +515,7 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio);void buffer_init (void );bool try_to_free_buffers (struct folio *folio); +bool block_release_folio (struct folio *folio,gfp_t gfp);int inode_has_buffers (struct inode *inode);void invalidate_inode_buffers (struct inode *inode);int remove_inode_buffers (struct inode *inode);
@@ -528,6 +529,7 @@ extern int buffer_heads_over_limit;static inline void buffer_init (void ) {}static inline bool try_to_free_buffers (struct folio *folio) {return true; } +static inline bool block_release_folio (struct folio *folio,gfp_t gfp) {return true; }static inline int inode_has_buffers (struct inode *inode) {return 0 ; }static inline void invalidate_inode_buffers (struct inode *inode) {}static inline int remove_inode_buffers (struct inode *inode) {return 1 ; }
mm/filemap.c +2 -2
@@ -4490,9 +4490,9 @@ bool filemap_release_folio(struct folio *folio, gfp_t gfp)if (folio_test_writeback (folio))return false; -if (mapping && mapping->a_ops->release_folio) +if (mapping)return mapping->a_ops->release_folio (folio, gfp); -return try_to_free_buffers (folio); +return true; }EXPORT_SYMBOL (filemap_release_folio);
Signature · 1 line
2.52.0
Jan Kara
maintainerQuoted 33 lines · Deepakkumar Karn
Currently,
filemap_release_folio()falls back totry_to_free_buffers()when a filesystem'saddress_space_operationsdoesn't provide arelease_foliocallback. This creates an inconsistent API where some buffer_head-based filesystems definerelease_foliowhile others rely on the fallback behavior.Make the
release_foliocallback mandatory by:1. Adding
block_release_folio()infs/buffer.cas the default implementation for buffer_head-based filesystems. This simply callstry_to_free_buffers().2. Updating all buffer_head-based filesystems that use
buffer_migrate_folio()to set .release_folio =block_release_folioin theiraddress_space_operations.3. Removing the NULL check and
try_to_free_buffers()fallback infilemap_release_folio(). Now it always calls therelease_foliocallback when a mapping exists.With these changes:
- Makes the pattern explicit across allbuffer_headfilesystems
- Prevents future bugs from missingrelease_folioimplementationsFilesystems updated: adfs, affs, bfs, exfat, ext2, fat, hpfs, isofs, jfs, minix, nilfs2, ntfs3, omfs, udf, ufs, plus
block/fops.cFilesystems with existing custom implementations (ext4, gfs2, hfs, hfsplus, jfs metapage, ocfs2) are unchanged.
Suggested-by Matthew Wilcox <[email protected]>Suggested-by Jan Kara <[email protected]>Signed-off-by Deepakkumar Karn <[email protected]>
...
fs/affs/file.c const struct address_space_operations affs_aops = { · line 470
Quoted 13 lines
fs/affs/file.c +2
-1@@ -464,7 +464,8 @@ const struct address_space_operations affs_aops = { .write_end = affs_write_end, .direct_IO = affs_direct_IO, .migrate_folio = buffer_migrate_folio, - .bmap = _affs_bmap + .bmap = _affs_bmap, + .release_folio = block_release_folio, };
I think affs_aops_ofs need .release_folio as well...
Also befs_aops, ecryptfs_aops, efs_aops, vxfs_aops, hfs_aops, hfsplus_aops, nilfs_buffer_cache_aops, qnx4_aops, qnx6_aops definitely need .release_folio. ntfs_aops_cmpr might need it as well - not sure.
fs/buffer.c drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free) · line 2919
Quoted 11 lines
fs/buffer.c +3
-0@@ -2914,6 +2914,23 @@ drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)return false; } +/** +* block_release_folio - Release buffers attached to a BH-based filesystems. +* @folio: The folio.
^^ "The folio with attached buffers."
fs/buffer.c drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free) · line 2920
Quoted 1 line
fs/buffer.c +1
-0+* @gfp: The gfp parameter.
^^ "Memory allocation flags (and I/O mode)."
fs/buffer.c drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free) · line 2927
Quoted 7 lines
fs/buffer.c +7
-0+* +* This is the default release_folio implementation for buffer_head based +* filesystems. Filesystems with special requirements +* should implement their own release_folio callback. +* +* Return: true if buffers were released, false otherwise. +*/
Otherwise the patch looks good to me.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Matthew Wilcox
maintainerQuoted 8 lines · Jan Kara
Quoted 2 lines
Filesystems updated: adfs, affs, bfs, exfat, ext2, fat, hpfs, isofs, jfs, minix, nilfs2, ntfs3, omfs, udf, ufs, plus
block/fops.cI think
affs_aops_ofsneed .release_folio as well...Also
befs_aops,ecryptfs_aops,efs_aops,vxfs_aops,hfs_aops,hfsplus_aops,nilfs_buffer_cache_aops,qnx4_aops,qnx6_aopsdefinitely need .release_folio.ntfs_aops_cmprmight need it as well - not sure.
If we were using a real programming language, we'd have a class for BH-based filesystems, inherit from it and override each method. But since we aren't, let's see what we can do in C ...
#define BH_DEFAULT_AOPS \ .dirty_folio = block_dirty_folio, \ .invalidate_folio = block_invalidate_folio, \ .migrate_folio = buffer_migrate_folio, \ .is_partially_uptodate = block_is_partially_uptodate, \ .error_remove_folio = generic_error_remove_folio,
As I understand C, later initialisers override earlier ones [1].
GCC does have an optional warning (-Woverride-init) which is included in -W but not -Wall. Some parts of the kernel explicitly turn this off, either like this:
arch/arm64/kvm/Makefile:CFLAGS_sys_regs.o += -Wno-override-init
or like this:
drivers/gpu/drm/i915/display/intel_display_device.c:__diag_ignore_all("-Woverride-init", "Allow field initialization overrides for display info");
I'd be happy to do either of those for filesystems.
[1] Test program:
struct ops { void (*frob)(void); };
static void my_frob(void) { return; }
void generic_frob(void);
struct ops my_ops = {
.frob = generic_frob,
.frob = my_frob,
};
int main(void)
{
return 0;
}$ gcc -Wall -o test2 test2.c
links without any missing symbols
Jan Kara
maintainerQuoted 14 lines · Matthew Wilcox
Quoted 8 lines · Jan Kara
Quoted 2 lines
Filesystems updated: adfs, affs, bfs, exfat, ext2, fat, hpfs, isofs, jfs, minix, nilfs2, ntfs3, omfs, udf, ufs, plus
block/fops.cI think
affs_aops_ofsneed .release_folio as well...Also
befs_aops,ecryptfs_aops,efs_aops,vxfs_aops,hfs_aops,hfsplus_aops,nilfs_buffer_cache_aops,qnx4_aops,qnx6_aopsdefinitely need .release_folio.ntfs_aops_cmprmight need it as well - not sure.If we were using a real programming language, we'd have a class for BH-based filesystems, inherit from it and override each method. But since we aren't, let's see what we can do in C ...
Quoted 8 lines
#define BH_DEFAULT_AOPS \ .dirty_folio = block_dirty_folio, \ .invalidate_folio = block_invalidate_folio, \ .migrate_folio = buffer_migrate_folio, \ .is_partially_uptodate = block_is_partially_uptodate, \ .error_remove_folio = generic_error_remove_folio,As I understand C, later initialisers override earlier ones [1].
Frankly, I'm not 100% convinced this will be a win and there is definitely a potential for surprises because filesystems tend to do weird things so this will need to carefully go filesystem by filesystem... So I wouldn't complicate this patch with a change like this. Let's clean up .release_folio first and we can look into cutting boilerplate initializations later.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 23 lines · Jan Kara
Quoted 21 lines · Matthew Wilcox
Quoted 8 lines · Jan Kara
Quoted 2 lines
Filesystems updated: adfs, affs, bfs, exfat, ext2, fat, hpfs, isofs, jfs, minix, nilfs2, ntfs3, omfs, udf, ufs, plus
block/fops.cI think
affs_aops_ofsneed .release_folio as well...Also
befs_aops,ecryptfs_aops,efs_aops,vxfs_aops,hfs_aops,hfsplus_aops,nilfs_buffer_cache_aops,qnx4_aops,qnx6_aopsdefinitely need .release_folio.ntfs_aops_cmprmight need it as well - not sure.If we were using a real programming language, we'd have a class for BH-based filesystems, inherit from it and override each method. But since we aren't, let's see what we can do in C ...
#define BH_DEFAULT_AOPS \ .dirty_folio = block_dirty_folio, \ .invalidate_folio = block_invalidate_folio, \ .migrate_folio = buffer_migrate_folio, \ .is_partially_uptodate = block_is_partially_uptodate, \ .error_remove_folio = generic_error_remove_folio,
Quoted 9 lines
Quoted 1 line
As I understand C, later initialisers override earlier ones [1].
Frankly, I'm not 100% convinced this will be a win and there is definitely a potential for surprises because filesystems tend to do weird things so this will need to carefully go filesystem by filesystem... So I wouldn't complicate this patch with a change like this. Let's clean up .release_folio first and we can look into cutting boilerplate initializations later.
Thank you Jan and Matthew for the review.
@Jan, I will address the feedback for adding release_folio, in missing filesystems. I will check
and add it wherever I find it missing/needed.
- affs_aops_ofs
- befs_aops
- ecryptfs_aops
- efs_aops
- vxfs_aops
- hfs_aops
- hfsplus_aops
- nilfs_buffer_cache_aops
- qnx4_aops
- qnx6_aops
- ntfs_aops_cmpr
As there are more changes, and to be honest this being one of my first patches of this scale, I am
trying to test it as thoroughly as possible.
@Matthew, I see what you were suggesting for the BH_DEFAULT_AOPS macro, it would be
nice to have, but let me first try to get all the filesystems that need the .release_folio
cleanup first. Once these changes are acceptable I can explore reducing boilerplate
suggestions as a follow-up.
Thanks,
Deepakkumar Karn