Start converting buffer_heads to use folios
Subsystems (6)
cover Start converting buffer_heads to use folios
I was hoping that filesystems would convert from buffer_heads to iomap, but that's not happening particularly quickly. So the buffer_head infrastructure needs to be converted from being page-based to being folio-based. This is the initial patchset that I hope Andrew will take for 6.3. I have a lot of followup patches, but many of them should go through individual filesystem trees (ext4, f2fs, etc). They can wait for 6.4.
Matthew Wilcox (Oracle) (12):
buffer: Add b_folio as an alias of b_page
buffer: Replace obvious uses of b_page with b_folio
buffer: Use b_folio in touch_buffer()
buffer: Use b_folio in end_buffer_async_read()
buffer: Use b_folio in end_buffer_async_write()
page_io: Remove buffer_head include
buffer: Use b_folio in mark_buffer_dirty()
gfs2: Replace obvious uses of b_page with b_folio
jbd2: Replace obvious uses of b_page with b_folio
nilfs2: Replace obvious uses of b_page with b_folio
reiserfs: Replace obvious uses of b_page with b_folio
mpage: Use b_folio in do_mpage_readpage()
fs/buffer.c | 54 +++++++++++++++++------------------ fs/gfs2/glops.c | 2 +- fs/gfs2/log.c | 2 +- fs/gfs2/meta_io.c | 2 +- fs/jbd2/commit.c | 8 ++---- fs/jbd2/journal.c | 2 +- fs/mpage.c | 2 +- fs/nilfs2/btnode.c | 2 +- fs/nilfs2/btree.c | 2 +- fs/nilfs2/gcinode.c | 2 +- fs/nilfs2/mdt.c | 4 +-- fs/nilfs2/segment.c | 2 +- fs/reiserfs/journal.c | 4 +-- fs/reiserfs/tail_conversion.c | 2 +- include/linux/buffer_head.h | 5 +++- mm/page_io.c | 1 - 16 files changed, 47 insertions(+), 49 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/12 buffer: Add b_folio as an alias of b_page
Buffer heads point to the allocation (ie the folio), not the page. This is currently the same thing for all filesystems that use buffer heads, so this is a safe transitional step.
--- include/linux/buffer_head.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/buffer_head.h +4 -1
@@ -61,7 +61,10 @@ typedef void (bh_end_io_t)(struct buffer_head *bh, int uptodate);struct buffer_head {unsigned long b_state;/* buffer state bitmap (see above) */ struct buffer_head *b_this_page;/* circular list of page's buffers */ -struct page *b_page;/* the page this bh is mapped to */ +union { +struct page *b_page;/* the page this bh is mapped to */ +struct folio *b_folio;/* the folio this bh is mapped to */ + };sector_t b_blocknr;/* start block number */ size_t b_size;/* size of mapping */
Signature · 1 line
2.35.1
PATCH 2/12 buffer: Replace obvious uses of b_page with b_folio
In reply to Matthew Wilcox (Oracle)These cases just check if it's NULL, or use b_page to get to the page's address space. They are assumptions that b_page never points to a tail page.
--- fs/buffer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/buffer.c +6 -6
@@ -321,7 +321,7 @@ static void end_buffer_async_read_io(struct buffer_head *bh, int uptodate) {/* Decrypt if needed */ if (uptodate && -fscrypt_inode_uses_fs_layer_crypto (bh->b_page->mapping->host)) { +fscrypt_inode_uses_fs_layer_crypto (bh->b_folio->mapping->host)) {struct decrypt_bh_ctx *ctx =kmalloc (sizeof (*ctx),GFP_ATOMIC );if (ctx) {
@@ -570,7 +570,7 @@ void write_boundary_block(struct block_device *bdev,void mark_buffer_dirty_inode (struct buffer_head *bh,struct inode *inode) {struct address_space *mapping = inode->i_mapping; -struct address_space *buffer_mapping = bh->b_page->mapping; +struct address_space *buffer_mapping = bh->b_folio->mapping;mark_buffer_dirty (bh);if (!mapping->private_data) {
@@ -1073,7 +1073,7 @@ __getblk_slow(struct block_device *bdev, sector_t block,* and then attach the address_space's inode to its superblock's dirty * inode list. * -* mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock, +* mark_buffer_dirty() is atomic. It takes bh->b_folio->mapping->private_lock, * i_pages lock and mapping->host->i_lock. */ void mark_buffer_dirty (struct buffer_head *bh)
@@ -1117,8 +1117,8 @@ void mark_buffer_write_io_error(struct buffer_head *bh)set_buffer_write_io_error (bh);/* FIXME: do we need to set this in both places? */ -if (bh->b_page && bh->b_page->mapping) -mapping_set_error (bh->b_page->mapping, -EIO ); +if (bh->b_folio && bh->b_folio->mapping) +mapping_set_error (bh->b_folio->mapping, -EIO );if (bh->b_assoc_map)mapping_set_error (bh->b_assoc_map, -EIO );rcu_read_lock ();
@@ -1154,7 +1154,7 @@ void __bforget(struct buffer_head *bh) {clear_buffer_dirty (bh);if (bh->b_assoc_map) { -struct address_space *buffer_mapping = bh->b_page->mapping; +struct address_space *buffer_mapping = bh->b_folio->mapping;spin_lock (&buffer_mapping->private_lock);list_del_init (&bh->b_assoc_buffers);
Signature · 1 line
2.35.1
PATCH 3/12 buffer: Use b_folio in touch_buffer()
In reply to Matthew Wilcox (Oracle)Removes a call to compound_head() in this path.
--- fs/buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/buffer.c +1 -1
@@ -60,7 +60,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,inline void touch_buffer (struct buffer_head *bh) {trace_block_touch_buffer (bh); -mark_page_accessed (bh->b_page); +folio_mark_accessed (bh->b_folio); }EXPORT_SYMBOL (touch_buffer);
Signature · 1 line
2.35.1
PATCH 4/12 buffer: Use b_folio in end_buffer_async_read()
In reply to Matthew Wilcox (Oracle)Removes a call to compound_head() in SetPageError(), saving 76 bytes of text.
--- fs/buffer.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
fs/buffer.c +9 -9
@@ -246,18 +246,18 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)unsigned long flags;struct buffer_head *first;struct buffer_head *tmp; -struct page *page; -int page_uptodate =1 ; +struct folio *folio; +int folio_uptodate =1 ;BUG_ON (!buffer_async_read (bh)); - page = bh->b_page; + folio = bh->b_folio;if (uptodate) {set_buffer_uptodate (bh); }else {clear_buffer_uptodate (bh);buffer_io_error (bh,", async page read" ); -SetPageError (page); +folio_set_error (folio); }/*
@@ -265,14 +265,14 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)* two buffer heads end IO at almost the same time and both * decide that the page is now completely done. */ - first =page_buffers (page); + first =folio_buffers (folio);spin_lock_irqsave (&first->b_uptodate_lock, flags);clear_buffer_async_read (bh);unlock_buffer (bh); tmp = bh; do { if (!buffer_uptodate(tmp)) - page_uptodate = 0; + folio_uptodate = 0; if (buffer_async_read(tmp)) { BUG_ON(!buffer_locked(tmp)); goto still_busy;
@@ -285,9 +285,9 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)* If all of the buffers are uptodate then we can set the page * uptodate. */ -if (page_uptodate) -SetPageUptodate (page); -unlock_page (page); +if (folio_uptodate) +folio_mark_uptodate (folio); +folio_unlock (folio);return ; still_busy:
Signature · 1 line
2.35.1
PATCH 5/12 buffer: Use b_folio in end_buffer_async_write()
In reply to Matthew Wilcox (Oracle)Save 76 bytes from avoiding the call to compound_head() in SetPageError(). Also avoid the call to compound_head() in end_page_writeback().
--- fs/buffer.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/buffer.c +5 -5
@@ -344,21 +344,21 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)unsigned long flags;struct buffer_head *first;struct buffer_head *tmp; -struct page *page; +struct folio *folio;BUG_ON (!buffer_async_write (bh)); - page = bh->b_page; + folio = bh->b_folio;if (uptodate) {set_buffer_uptodate (bh); }else {buffer_io_error (bh,", lost async page write" );mark_buffer_write_io_error (bh);clear_buffer_uptodate (bh); -SetPageError (page); +folio_set_error (folio); } - first =page_buffers (page); + first =folio_buffers (folio);spin_lock_irqsave (&first->b_uptodate_lock, flags);clear_buffer_async_write (bh);
@@ -372,7 +372,7 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate) tmp = tmp->b_this_page; }spin_unlock_irqrestore (&first->b_uptodate_lock, flags); -end_page_writeback (page); +folio_end_writeback (folio);return ; still_busy:
Signature · 1 line
2.35.1
PATCH 6/12 page_io: Remove buffer_head include
In reply to Matthew Wilcox (Oracle)page_io never uses buffer_heads to do I/O.
--- mm/page_io.c | 1 - 1 file changed, 1 deletion(-)
mm/page_io.c +0 -1
@@ -18,7 +18,6 @@#include <linux/swap.h> #include <linux/bio.h> #include <linux/swapops.h> -#include <linux/buffer_head.h> #include <linux/writeback.h> #include <linux/frontswap.h> #include <linux/blkdev.h>
Signature · 1 line
2.35.1
PATCH 7/12 buffer: Use b_folio in mark_buffer_dirty()
In reply to Matthew Wilcox (Oracle)Removes about four calls to compound_head(). Two of them are inline which removes 132 bytes from the kernel text.
--- fs/buffer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/buffer.c +6 -6
@@ -1095,16 +1095,16 @@ void mark_buffer_dirty(struct buffer_head *bh) }if (!test_set_buffer_dirty (bh)) { -struct page *page = bh->b_page; +struct folio *folio = bh->b_folio;struct address_space *mapping =NULL ; -lock_page_memcg (page); -if (!TestSetPageDirty (page)) { - mapping =page_mapping (page); +folio_memcg_lock (folio); +if (!folio_test_set_dirty (folio)) { + mapping = folio->mapping;if (mapping) -__set_page_dirty (page, mapping,0 ); +__folio_mark_dirty (folio, mapping,0 ); } -unlock_page_memcg (page); +folio_memcg_unlock (folio);if (mapping)__mark_inode_dirty (mapping->host,I_DIRTY_PAGES ); }
Signature · 1 line
2.35.1
PATCH 8/12 gfs2: Replace obvious uses of b_page with b_folio
In reply to Matthew Wilcox (Oracle)These places just use b_page to get to the buffer's address_space.
--- fs/gfs2/glops.c | 2 +- fs/gfs2/log.c | 2 +- fs/gfs2/meta_io.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
fs/gfs2/glops.c +1 -1
@@ -39,7 +39,7 @@ static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)"AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page " "state 0x%lx\n" , bh, (unsigned long long )bh->b_blocknr, bh->b_state, - bh->b_page->mapping, bh->b_page->flags); + bh->b_folio->mapping, bh->b_folio->flags);fs_err (sdp,"AIL glock %u:%llu mapping %p\n" , gl->gl_name.ln_type, gl->gl_name.ln_number,gfs2_glock2aspace (gl));
fs/gfs2/log.c +1 -1
@@ -127,7 +127,7 @@ __acquires(&sdp->sd_ail_lock)continue ; gl = bd->bd_gl;list_move (&bd->bd_ail_st_list, &tr->tr_ail1_list); - mapping = bh->b_page->mapping; + mapping = bh->b_folio->mapping;if (!mapping)continue ;spin_unlock (&sdp->sd_ail_lock);
fs/gfs2/meta_io.c +1 -1
@@ -334,7 +334,7 @@ int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)void gfs2_remove_from_journal (struct buffer_head *bh,int meta) { -struct address_space *mapping = bh->b_page->mapping; +struct address_space *mapping = bh->b_folio->mapping;struct gfs2_sbd *sdp =gfs2_mapping2sbd (mapping);struct gfs2_bufdata *bd = bh->b_private;struct gfs2_trans *tr = current->journal_info;
Signature · 1 line
2.35.1
PATCH 9/12 jbd2: Replace obvious uses of b_page with b_folio
In reply to Matthew Wilcox (Oracle)These places just use b_page to get to the buffer's address_space or have already been converted to folio.
--- fs/jbd2/commit.c | 8 ++------ fs/jbd2/journal.c | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-)
fs/jbd2/commit.c +2 -6
@@ -63,16 +63,12 @@ static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)static void release_buffer_page (struct buffer_head *bh) {struct folio *folio; -struct page *page;if (buffer_dirty (bh)) goto nope;if (atomic_read (&bh->b_count) !=1 ) goto nope; - page = bh->b_page; -if (!page) - goto nope; - folio =page_folio (page); + folio = bh->b_folio;if (folio->mapping) goto nope;
@@ -1040,7 +1036,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)* already detached from the mapping and buffers cannot * get reused. */ - mapping =READ_ONCE (bh->b_page->mapping); + mapping =READ_ONCE (bh->b_folio->mapping);if (mapping && !sb_is_blkdev_sb (mapping->host->i_sb)) {clear_buffer_mapped (bh);clear_buffer_new (bh);
fs/jbd2/journal.c +1 -1
@@ -2938,7 +2938,7 @@ struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) }else {J_ASSERT_BH (bh, (atomic_read (&bh->b_count) >0 ) || - (bh->b_page && bh->b_page->mapping)); + (bh->b_folio && bh->b_folio->mapping));if (!new_jh) {jbd_unlock_bh_journal_head (bh);
Signature · 1 line
2.35.1
PATCH 10/12 nilfs2: Replace obvious uses of b_page with b_folio
In reply to Matthew Wilcox (Oracle)These places just use b_page to get to the buffer's address_space or the index of the page the buffer is in.
--- fs/nilfs2/btnode.c | 2 +- fs/nilfs2/btree.c | 2 +- fs/nilfs2/gcinode.c | 2 +- fs/nilfs2/mdt.c | 4 ++-- fs/nilfs2/segment.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
fs/nilfs2/btnode.c +1 -1
@@ -188,7 +188,7 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc,struct page *opage = obh->b_page;lock_page (opage); retry: -/* BUG_ON(oldkey != obh->b_page->index); */ +/* BUG_ON(oldkey != obh->b_folio->index); */ if (unlikely (oldkey != opage->index)) NILFS_PAGE_BUG(opage, "invalid oldkey %lld (newkey=%lld)",
fs/nilfs2/btree.c +1 -1
@@ -398,7 +398,7 @@ int nilfs_btree_broken_node_block(struct buffer_head *bh)if (buffer_nilfs_checked (bh))return 0 ; - inode = bh->b_page->mapping->host; + inode = bh->b_folio->mapping->host; ret =nilfs_btree_node_broken ((struct nilfs_btree_node *)bh->b_data, bh->b_size, inode, bh->b_blocknr);if (likely (!ret))
fs/nilfs2/gcinode.c +1 -1
@@ -140,7 +140,7 @@ int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh) {wait_on_buffer (bh);if (!buffer_uptodate (bh)) { -struct inode *inode = bh->b_page->mapping->host; +struct inode *inode = bh->b_folio->mapping->host; nilfs_err(inode->i_sb, "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
fs/nilfs2/mdt.c +2 -2
@@ -563,7 +563,7 @@ int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)struct page *page;int blkbits = inode->i_blkbits; - page =grab_cache_page (shadow->inode->i_mapping, bh->b_page->index); + page =grab_cache_page (shadow->inode->i_mapping, bh->b_folio->index);if (!page)return -ENOMEM ;
@@ -595,7 +595,7 @@ nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)struct page *page;int n; - page =find_lock_page (shadow->inode->i_mapping, bh->b_page->index); + page =find_lock_page (shadow->inode->i_mapping, bh->b_folio->index);if (page) {if (page_has_buffers (page)) { n =bh_offset (bh) >> inode->i_blkbits;
fs/nilfs2/segment.c +1 -1
@@ -1581,7 +1581,7 @@ nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci, nblocks =le32_to_cpu (finfo->fi_nblocks); ndatablk =le32_to_cpu (finfo->fi_ndatablk); - inode = bh->b_page->mapping->host; + inode = bh->b_folio->mapping->host;if (mode ==SC_LSEG_DSYNC ) sc_op = &nilfs_sc_dsync_ops;
Signature · 1 line
2.35.1
PATCH 11/12 reiserfs: Replace obvious uses of b_page with b_folio
In reply to Matthew Wilcox (Oracle)These places just use b_page to get to the buffer's address_space or call page_folio() on b_page to get a folio.
--- fs/reiserfs/journal.c | 4 ++-- fs/reiserfs/tail_conversion.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
fs/reiserfs/journal.c +2 -2
@@ -601,7 +601,7 @@ static int journal_list_still_alive(struct super_block *s,*/ static void release_buffer_page (struct buffer_head *bh) { -struct folio *folio =page_folio (bh->b_page); +struct folio *folio = bh->b_folio;if (!folio->mapping &&folio_trylock (folio)) {folio_get (folio);put_bh (bh);
@@ -866,7 +866,7 @@ static int write_ordered_buffers(spinlock_t * lock,* will ever write the buffer. We're safe if we write the * page one last time after freeing the journal header. */ -if (buffer_dirty (bh) &&unlikely (bh->b_page->mapping ==NULL )) { +if (buffer_dirty (bh) &&unlikely (bh->b_folio->mapping ==NULL )) {spin_unlock (lock);write_dirty_buffer (bh,0 );spin_lock (lock);
fs/reiserfs/tail_conversion.c +1 -1
@@ -177,7 +177,7 @@ void reiserfs_unmap_buffer(struct buffer_head *bh)* BUG() on attempt to write not mapped buffer */ if ((!list_empty (&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) { -struct inode *inode = bh->b_page->mapping->host; +struct inode *inode = bh->b_folio->mapping->host;struct reiserfs_journal *j =SB_JOURNAL (inode->i_sb);spin_lock (&j->j_dirty_buffers_lock);list_del_init (&bh->b_assoc_buffers);
Signature · 1 line
2.35.1
PATCH 12/12 mpage: Use b_folio in do_mpage_readpage()
In reply to Matthew Wilcox (Oracle)Remove this conversion of a folio back to a page.
--- fs/mpage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/mpage.c +1 -1
@@ -198,7 +198,7 @@ static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)/* * Then do more get_blocks calls until we are done with this folio. */ - map_bh->b_page = &folio->page; + map_bh->b_folio = folio;while (page_block < blocks_per_page) { map_bh->b_state =0 ; map_bh->b_size =0 ;
Signature · 1 line
2.35.1
Quoted 7 lines · Matthew Wilcox (Oracle)
I was hoping that filesystems would convert from
buffer_headsto iomap, but that's not happening particularly quickly. So thebuffer_headinfrastructure needs to be converted from being page-based to being folio-based. This is the initial patchset that I hope Andrew will take for 6.3. I have a lot of followup patches, but many of them should go through individual filesystem trees (ext4, f2fs, etc). They can wait for 6.4.
FWIW I went through all the patches and they look fine to me so feel free to add:
Honza
Quoted 36 lines
Matthew Wilcox (Oracle) (12):
buffer: Addb_folioas an alias ofb_page
buffer: Replace obvious uses ofb_pagewithb_folio
buffer: Useb_foliointouch_buffer()
buffer: Useb_folioinend_buffer_async_read()
buffer: Useb_folioinend_buffer_async_write()
page_io: Removebuffer_headinclude
buffer: Useb_folioinmark_buffer_dirty()
gfs2: Replace obvious uses ofb_pagewithb_folio
jbd2: Replace obvious uses ofb_pagewithb_folio
nilfs2: Replace obvious uses ofb_pagewithb_folio
reiserfs: Replace obvious uses ofb_pagewithb_folio
mpage: Useb_folioindo_mpage_readpage()fs/buffer.c | 54 +++++++++++++++++------------------ fs/gfs2/glops.c | 2 +- fs/gfs2/log.c | 2 +- fs/gfs2/meta_io.c | 2 +- fs/jbd2/commit.c | 8 ++---- fs/jbd2/journal.c | 2 +- fs/mpage.c | 2 +- fs/nilfs2/btnode.c | 2 +- fs/nilfs2/btree.c | 2 +- fs/nilfs2/gcinode.c | 2 +- fs/nilfs2/mdt.c | 4 +-- fs/nilfs2/segment.c | 2 +- fs/reiserfs/journal.c | 4 +-- fs/reiserfs/tail_conversion.c | 2 +- include/linux/buffer_head.h | 5 +++- mm/page_io.c | 1 - 16 files changed, 47 insertions(+), 49 deletions(-)Signature · 1 line
2.35.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR