map multiple blocks per ->map_blocks in iomap writeback
Subsystems (6)
Hi all,
this series overhaults a large chunk of the iomap writeback code with the end result that ->map_blocks can now map multiple blocks at a time, at least as long as they are all inside the same folio.
On a sufficiently large system (32 cores in my case) this significantly reduces CPU usage for buffered write workloads on xfs, with a very minor improvement in write bandwith that might be within the measurement tolerance.
e.g. on a fio sequential write workload using io_uring I get these values (median out of 5 runs):
before:
cpu : usr=5.26%, sys=4.81%, ctx=4009750, majf=0, minf=13
WRITE: bw=1096MiB/s (1150MB/s), 1096MiB/s-1096MiB/s (1150MB/s-1150MB/s), io=970GiB (1042GB), run=906036-906036msec
with this series:
cpu : usr=4.95%, sys=2.72%, ctx=4084578, majf=0, minf=12
WRITE: bw=1111MiB/s (1165MB/s), 1111MiB/s-1111MiB/s (1165MB/s-1165MB/s), io=980GiB (1052GB), run=903234-903234msec
On systems with a small number of cores the cpu usage reduction is much lower and barely visible.
Changes since RFC:
- various commit message typo fixes
- minor formatting fixes
- keep the PF_MEMALLOC check and move it to iomap_writepages
- rename the offset argument to iomap_can_add_to_ioend to pos
- fix missing error handling in an earlier patch (only required for bisection, no change to the end result)
- remove a stray whitespace
- refactor ifs_find_dirty_range a bit to make it more readable
- add a patch to pass the dirty_len to the file system to make life for ext2 easier
Diffstat:
block/fops.c | 2 fs/gfs2/bmap.c | 2 fs/iomap/buffered-io.c | 576 +++++++++++++++++++++++-------------------------- fs/xfs/xfs_aops.c | 9 fs/zonefs/file.c | 3 include/linux/iomap.h | 19 + 6 files changed, 306 insertions(+), 305 deletions(-)
PATCH 1/14 iomap: clear the per-folio dirty bits on all writeback failures
write_cache_pages always clear the page dirty bit before calling into the file systems, and leaves folios with a writeback failure without the dirty bit after return. We also clear the per-block writeback bits for writeback failures unless no I/O has submitted, which will leave the folio in an inconsistent state where it doesn't have the folio dirty, but one or more per-block dirty bits. This seems to be due the place where the iomap_clear_range_dirty call was inserted into the existing not very clearly structured code when adding per-block dirty bit support and not actually intentional. Switch to always clearing the dirty on writeback failure.
--- fs/iomap/buffered-io.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
fs/iomap/buffered-io.c +11 -7
@@ -1843,16 +1843,10 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,if (unlikely (error)) {/* * Let the filesystem know what portion of the current page -* failed to map. If the page hasn't been added to ioend, it -* won't be affected by I/O completion and we must unlock it -* now. +* failed to map. */ if (wpc->ops->discard_folio) wpc->ops->discard_folio (folio, pos); -if (!count) { -folio_unlock (folio); - goto done; - } }/*
@@ -1861,6 +1855,16 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,* all the dirty bits in the folio here. */ iomap_clear_range_dirty (folio,0 ,folio_size (folio)); + +/* +* If the page hasn't been added to the ioend, it won't be affected by +* I/O completion and we must unlock it now. +*/ +if (error && !count) { +folio_unlock (folio); + goto done; + } +folio_start_writeback (folio);folio_unlock (folio);
Signature · 1 line
2.39.2
PATCH 2/14 iomap: treat inline data in iomap_writepage_map as an I/O error
In reply to Christoph Hellwigiomap_writepage_map aready warns about inline data, but then just ignores it. Treat it as an error and return -EIO.
--- fs/iomap/buffered-io.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/iomap/buffered-io.c +4 -2
@@ -1818,8 +1818,10 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,if (error)break ;trace_iomap_writepage_map (inode, &wpc->iomap); -if (WARN_ON_ONCE (wpc->iomap.type ==IOMAP_INLINE )) -continue ; +if (WARN_ON_ONCE (wpc->iomap.type ==IOMAP_INLINE )) { + error = -EIO ; +break ; + }if (wpc->iomap.type ==IOMAP_HOLE )continue ; iomap_add_to_ioend(inode, pos, folio, ifs, wpc, wbc,
Signature · 1 line
2.39.2
PATCH 3/14 iomap: move the io_folios field out of struct iomap_ioend
In reply to Christoph HellwigThe io_folios member in struct iomap_ioend counts the number of folios added to an ioend. It is only used at submission time and can thus be moved to iomap_writepage_ctx instead.
--- fs/iomap/buffered-io.c | 7 ++++--- include/linux/iomap.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-)
fs/iomap/buffered-io.c +4 -3
@@ -1685,10 +1685,11 @@ iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc, ioend->io_flags = wpc->iomap.flags; ioend->io_inode = inode; ioend->io_size =0 ; - ioend->io_folios =0 ; ioend->io_offset = offset; ioend->io_bio = bio; ioend->io_sector = sector; + + wpc->nr_folios =0 ;return ioend; }
@@ -1732,7 +1733,7 @@ iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,* also prevents long tight loops ending page writeback on all the * folios in the ioend. */ -if (wpc->ioend->io_folios >=IOEND_BATCH_SIZE ) +if (wpc->nr_folios >=IOEND_BATCH_SIZE )return false;return true; }
@@ -1829,7 +1830,7 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc, count++; }if (count) - wpc->ioend->io_folios++; + wpc->nr_folios++;WARN_ON_ONCE (!wpc->ioend && !list_empty (&submit_list));WARN_ON_ONCE (!folio_test_locked (folio));
include/linux/iomap.h +1 -1
@@ -293,7 +293,6 @@ struct iomap_ioend {struct list_head io_list;/* next ioend in chain */ u16 io_type;u16 io_flags;/* IOMAP_F_* */ -u32 io_folios;/* folios added to ioend */ struct inode *io_inode;/* file being written to */ size_t io_size;/* size of the extent */ loff_t io_offset;/* offset in the file */
@@ -329,6 +328,7 @@ struct iomap_writepage_ctx {struct iomap iomap;struct iomap_ioend *ioend;const struct iomap_writeback_ops *ops; +u32 nr_folios;/* folios added to the ioend */ };void iomap_finish_ioends (struct iomap_ioend *ioend,int error);
Signature · 1 line
2.39.2
PATCH 4/14 iomap: move the PF_MEMALLOC check to iomap_writepages
In reply to Christoph HellwigThe iomap writepage implementation has been removed in commit 478af190cb6c ("iomap: remove iomap_writepage") and this code is now only called through ->writepages which never happens from memory reclaim.
Nove the check from iomap_do_writepage to iomap_writepages so that is only called once per ->writepage invocation.
--- fs/iomap/buffered-io.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
fs/iomap/buffered-io.c +8 -16
@@ -1912,20 +1912,6 @@ static int iomap_do_writepage(struct folio *folio,trace_iomap_writepage (inode,folio_pos (folio),folio_size (folio)); -/* -* Refuse to write the folio out if we're called from reclaim context. -* -* This avoids stack overflows when called from deeply used stacks in -* random callers for direct reclaim or memcg reclaim. We explicitly -* allow reclaim from kswapd as the stack usage there is relatively low. -* -* This should never happen except in the case of a VM regression so -* warn about it. -*/ -if (WARN_ON_ONCE ((current->flags & (PF_MEMALLOC |PF_KSWAPD )) == -PF_MEMALLOC )) - goto redirty; -/* * Is this folio beyond the end of the file? *
@@ -1991,8 +1977,6 @@ static int iomap_do_writepage(struct folio *folio,return iomap_writepage_map (wpc, wbc, inode, folio, end_pos); -redirty: -folio_redirty_for_writepage (wbc, folio); unlock:folio_unlock (folio);return 0 ;
@@ -2005,6 +1989,14 @@ iomap_writepages(struct address_space *mapping, struct writeback_control *wbc, {int ret; +/* +* Writeback from reclaim context should never happen except in the case +* of a VM regression so warn about it and refuse to write the data. +*/ +if (WARN_ON_ONCE ((current->flags & (PF_MEMALLOC |PF_KSWAPD )) == +PF_MEMALLOC )) +return -EIO ; + wpc->ops = ops; ret =write_cache_pages (mapping, wbc, iomap_do_writepage, wpc);if (!wpc->ioend)
Signature · 1 line
2.39.2
PATCH 5/14 iomap: factor out a iomap_writepage_handle_eof helper
In reply to Christoph HellwigMost of iomap_do_writepage is dedidcated to handling a folio crossing or beyond i_size. Split this is into a separate helper and update the commens to deal with folios instead of pages and make them more readable.
--- fs/iomap/buffered-io.c | 128 ++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 66 deletions(-)
fs/iomap/buffered-io.c +62 -66
@@ -1768,6 +1768,64 @@ iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio,wbc_account_cgroup_owner (wbc, &folio->page, len); } +/* +* Check interaction of the folio with the file end. +* +* If the folio is entirely beyond i_size, return false. If it straddles +* i_size, adjust end_pos and zero all data beyond i_size. +*/ +static bool iomap_writepage_handle_eof (struct folio *folio,struct inode *inode, +u64 *end_pos) +{ +u64 isize =i_size_read (inode); + +if (*end_pos > isize) { +size_t poff =offset_in_folio (folio, isize); +pgoff_t end_index = isize >>PAGE_SHIFT ; + +/* +* If the folio is entirely ouside of i_size, skip it. +* +* This can happen due to a truncate operation that is in +* progress and in that case truncate will finish it off once +* we've dropped the folio lock. +* +* Note that the pgoff_t used for end_index is an unsigned long. +* If the given offset is greater than 16TB on a 32-bit system, +* then if we checked if the folio is fully outside i_size with +* "if (folio->index >= end_index + 1)", "end_index + 1" would +* overflow and evaluate to 0. Hence this folio would be +* redirtied and written out repeatedly, which would result in +* an infinite loop; the user program performing this operation +* would hang. Instead, we can detect this situation by +* checking if the folio is totally beyond i_size or if its +* offset is just equal to the EOF. +*/ +if (folio->index > end_index || + (folio->index == end_index && poff ==0 )) +return false; + +/* +* The folio straddles i_size. +* +* It must be zeroed out on each and every writepage invocation +* because it may be mmapped: +* +* A file is mapped in multiples of the page size. For a +* file that is not a multiple of the page size, the +* remaining memory is zeroed when mapped, and writes to that +* region are not written out to the file. +* +* Also adjust the writeback range to skip all blocks entirely +* beyond i_size. +*/ +folio_zero_segment (folio, poff,folio_size (folio)); + *end_pos = isize; + } + +return true; +} +/* * We implement an immediate ioend submission policy here to avoid needing to * chain multiple ioends and hence nest mempool allocations which can violate
@@ -1908,78 +1966,16 @@ static int iomap_do_writepage(struct folio *folio, {struct iomap_writepage_ctx *wpc = data;struct inode *inode = folio->mapping->host; -u64 end_pos, isize; +u64 end_pos =folio_pos (folio) +folio_size (folio);trace_iomap_writepage (inode,folio_pos (folio),folio_size (folio)); -/* -* Is this folio beyond the end of the file? -* -* The folio index is less than the end_index, adjust the end_pos -* to the highest offset that this folio should represent. -* ----------------------------------------------------- -* | file mapping | <EOF> | -* ----------------------------------------------------- -* | Page ... | Page N-2 | Page N-1 | Page N | | -* ^--------------------------------^----------|-------- -* | desired writeback range | see else | -* ---------------------------------^------------------| -*/ - isize =i_size_read (inode); - end_pos =folio_pos (folio) +folio_size (folio); -if (end_pos > isize) { -/* -* Check whether the page to write out is beyond or straddles -* i_size or not. -* ------------------------------------------------------- -* | file mapping | <EOF> | -* ------------------------------------------------------- -* | Page ... | Page N-2 | Page N-1 | Page N | Beyond | -* ^--------------------------------^-----------|--------- -* | | Straddles | -* ---------------------------------^-----------|--------| -*/ -size_t poff =offset_in_folio (folio, isize); -pgoff_t end_index = isize >>PAGE_SHIFT ; - -/* -* Skip the page if it's fully outside i_size, e.g. -* due to a truncate operation that's in progress. We've -* cleaned this page and truncate will finish things off for -* us. -* -* Note that the end_index is unsigned long. If the given -* offset is greater than 16TB on a 32-bit system then if we -* checked if the page is fully outside i_size with -* "if (page->index >= end_index + 1)", "end_index + 1" would -* overflow and evaluate to 0. Hence this page would be -* redirtied and written out repeatedly, which would result in -* an infinite loop; the user program performing this operation -* would hang. Instead, we can detect this situation by -* checking if the page is totally beyond i_size or if its -* offset is just equal to the EOF. -*/ -if (folio->index > end_index || - (folio->index == end_index && poff ==0 )) - goto unlock; - -/* -* The page straddles i_size. It must be zeroed out on each -* and every writepage invocation because it may be mmapped. -* "A file is mapped in multiples of the page size. For a file -* that is not a multiple of the page size, the remaining -* memory is zeroed when mapped, and writes to that region are -* not written out to the file." -*/ -folio_zero_segment (folio, poff,folio_size (folio)); - end_pos = isize; +if (!iomap_writepage_handle_eof (folio, inode, &end_pos)) { +folio_unlock (folio); +return 0 ; }return iomap_writepage_map (wpc, wbc, inode, folio, end_pos); - -unlock: -folio_unlock (folio); -return 0 ; }int
Signature · 1 line
2.39.2
PATCH 6/14 iomap: move all remaining per-folio logic into iomap_writepage_map
In reply to Christoph HellwigMove the tracepoint and the iomap check from iomap_do_writepage into iomap_writepage_map. This keeps all logic in one places, and leaves iomap_do_writepage just as the wrapper for the callback conventions of write_cache_pages, which will go away when that is converted to an iterator.
--- fs/iomap/buffered-io.c | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-)
fs/iomap/buffered-io.c +11 -23
@@ -1842,19 +1842,25 @@ static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,* At the end of a writeback pass, there will be a cached ioend remaining on the * writepage context that the caller will need to submit. */ -static int -iomap_writepage_map (struct iomap_writepage_ctx *wpc, -struct writeback_control *wbc,struct inode *inode, -struct folio *folio,u64 end_pos) +static int iomap_writepage_map (struct iomap_writepage_ctx *wpc, +struct writeback_control *wbc,struct folio *folio) {struct iomap_folio_state *ifs = folio->private; +struct inode *inode = folio->mapping->host;struct iomap_ioend *ioend, *next;unsigned len =i_blocksize (inode);unsigned nblocks =i_blocks_per_folio (inode, folio);u64 pos =folio_pos (folio); +u64 end_pos = pos +folio_size (folio);int error =0 , count =0 , i;LIST_HEAD (submit_list); +trace_iomap_writepage (inode, pos,folio_size (folio)); + +if (!iomap_writepage_handle_eof (folio, inode, &end_pos)) { +folio_unlock (folio); +return 0 ; + }WARN_ON_ONCE (end_pos <= pos);if (!ifs && nblocks >1 ) {
@@ -1954,28 +1960,10 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,return error; } -/* -* Write out a dirty page. -* -* For delalloc space on the page, we need to allocate space and flush it. -* For unwritten space on the page, we need to start the conversion to -* regular allocated space. -*/ static int iomap_do_writepage (struct folio *folio,struct writeback_control *wbc,void *data) { -struct iomap_writepage_ctx *wpc = data; -struct inode *inode = folio->mapping->host; -u64 end_pos =folio_pos (folio) +folio_size (folio); - -trace_iomap_writepage (inode,folio_pos (folio),folio_size (folio)); - -if (!iomap_writepage_handle_eof (folio, inode, &end_pos)) { -folio_unlock (folio); -return 0 ; - } - -return iomap_writepage_map (wpc, wbc, inode, folio, end_pos); +return iomap_writepage_map (data, wbc, folio); }int
Signature · 1 line
2.39.2
PATCH 7/14 iomap: clean up the iomap_alloc_ioend calling convention
In reply to Christoph HellwigSwitch to the same argument order as iomap_writepage_map and remove the ifs argument that can be trivially recalculated.
--- fs/iomap/buffered-io.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
fs/iomap/buffered-io.c +5 -6
@@ -1742,11 +1742,11 @@ iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,* Test to see if we have an existing ioend structure that we could append to * first; otherwise finish off the current ioend and start another. */ -static void -iomap_add_to_ioend (struct inode *inode,loff_t pos,struct folio *folio, -struct iomap_folio_state *ifs,struct iomap_writepage_ctx *wpc, -struct writeback_control *wbc,struct list_head *iolist) +static void iomap_add_to_ioend (struct iomap_writepage_ctx *wpc, +struct writeback_control *wbc,struct folio *folio, +struct inode *inode,loff_t pos,struct list_head *iolist) { +struct iomap_folio_state *ifs = folio->private;sector_t sector =iomap_sector (&wpc->iomap, pos);unsigned len =i_blocksize (inode);size_t poff =offset_in_folio (folio, pos);
@@ -1889,8 +1889,7 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc, }if (wpc->iomap.type ==IOMAP_HOLE )continue ; -iomap_add_to_ioend (inode, pos, folio, ifs, wpc, wbc, - &submit_list); +iomap_add_to_ioend (wpc, wbc, folio, inode, pos, &submit_list); count++; }if (count)
Signature · 1 line
2.39.2
PATCH 8/14 iomap: move the iomap_sector sector calculation out of iomap_add_to_ioend
In reply to Christoph HellwigThe calculation in iomap_sector is pretty trivial and most of the time iomap_add_to_ioend only callers either iomap_can_add_to_ioend or iomap_alloc_ioend from a single invocation.
Calculate the sector in the two lower level functions and stop passing it from iomap_add_to_ioend and update the iomap_alloc_ioend argument passing order to match that of iomap_add_to_ioend.
--- fs/iomap/buffered-io.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
fs/iomap/buffered-io.c +11 -14
@@ -1666,9 +1666,8 @@ iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,return 0 ; } -static struct iomap_ioend * -iomap_alloc_ioend (struct inode *inode,struct iomap_writepage_ctx *wpc, -loff_t offset,sector_t sector,struct writeback_control *wbc) +static struct iomap_ioend *iomap_alloc_ioend (struct iomap_writepage_ctx *wpc, +struct writeback_control *wbc,struct inode *inode,loff_t pos) {struct iomap_ioend *ioend;struct bio *bio;
@@ -1676,7 +1675,7 @@ iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc, bio =bio_alloc_bioset (wpc->iomap.bdev,BIO_MAX_VECS ,REQ_OP_WRITE |wbc_to_write_flags (wbc),GFP_NOFS , &iomap_ioend_bioset); - bio->bi_iter.bi_sector = sector; + bio->bi_iter.bi_sector =iomap_sector (&wpc->iomap, pos);wbc_init_bio (wbc, bio); ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
@@ -1685,9 +1684,9 @@ iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc, ioend->io_flags = wpc->iomap.flags; ioend->io_inode = inode; ioend->io_size =0 ; - ioend->io_offset = offset; + ioend->io_offset = pos; ioend->io_bio = bio; - ioend->io_sector = sector; + ioend->io_sector = bio->bi_iter.bi_sector; wpc->nr_folios =0 ;return ioend;
@@ -1715,18 +1714,17 @@ iomap_chain_bio(struct bio *prev)return new; } -static bool -iomap_can_add_to_ioend (struct iomap_writepage_ctx *wpc,loff_t offset, -sector_t sector) +static bool iomap_can_add_to_ioend (struct iomap_writepage_ctx *wpc,loff_t pos) {if ((wpc->iomap.flags &IOMAP_F_SHARED ) != (wpc->ioend->io_flags &IOMAP_F_SHARED ))return false;if (wpc->iomap.type != wpc->ioend->io_type)return false; -if (offset != wpc->ioend->io_offset + wpc->ioend->io_size) +if (pos != wpc->ioend->io_offset + wpc->ioend->io_size)return false; -if (sector !=bio_end_sector (wpc->ioend->io_bio)) +if (iomap_sector (&wpc->iomap, pos) != +bio_end_sector (wpc->ioend->io_bio))return false;/* * Limit ioend bio chain lengths to minimise IO completion latency. This
@@ -1747,14 +1745,13 @@ static void iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct inode *inode, loff_t pos, struct list_head *iolist) { struct iomap_folio_state *ifs = folio->private; -sector_t sector =iomap_sector (&wpc->iomap, pos);unsigned len =i_blocksize (inode);size_t poff =offset_in_folio (folio, pos); -if (!wpc->ioend || !iomap_can_add_to_ioend (wpc, pos, sector)) { +if (!wpc->ioend || !iomap_can_add_to_ioend (wpc, pos)) {if (wpc->ioend)list_add (&wpc->ioend->io_list, iolist); - wpc->ioend =iomap_alloc_ioend (inode, wpc, pos, sector, wbc); + wpc->ioend =iomap_alloc_ioend (wpc, wbc, inode, pos); }if (!bio_add_folio (wpc->ioend->io_bio, folio, len, poff)) {
Signature · 1 line
2.39.2
PATCH 9/14 iomap: don't chain bios
In reply to Christoph HellwigBack in the days when a single bio could only be filled to the hardware limits, and we scheduled a work item for each bio completion, chaining multiple bios for a single ioend made a lot of sense to reduce the number of completions. But these days bios can be filled until we reach the number of vectors or total size limit, which means we can always fit at least 1 megabyte worth of data in the worst case, but usually a lot more due to large folios. The only thing bio chaining is buying us now is to reduce the size of the allocation from an ioend with an embedded bio into a plain bio, which is a 52 bytes differences on 64-bit systems.
This is not worth the added complexity, so remove the bio chaining and only use the bio embedded into the ioend. This will help to simplify further changes to the iomap writeback code.
--- fs/iomap/buffered-io.c | 90 +++++++++++------------------------------- fs/xfs/xfs_aops.c | 6 +-- include/linux/iomap.h | 8 +++- 3 files changed, 32 insertions(+), 72 deletions(-)
fs/iomap/buffered-io.c +23 -67
@@ -1489,40 +1489,23 @@ static u32 iomap_finish_ioend(struct iomap_ioend *ioend, int error) {struct inode *inode = ioend->io_inode; -struct bio *bio = &ioend->io_inline_bio; -struct bio *last = ioend->io_bio, *next; -u64 start = bio->bi_iter.bi_sector; -loff_t offset = ioend->io_offset; -bool quiet =bio_flagged (bio,BIO_QUIET ); +struct bio *bio = &ioend->io_bio; +struct folio_iter fi;u32 folio_count =0 ; -for (bio = &ioend->io_inline_bio; bio; bio = next) { -struct folio_iter fi; - -/* -* For the last bio, bi_private points to the ioend, so we -* need to explicitly end the iteration here. -*/ -if (bio == last) - next =NULL ; -else - next = bio->bi_private; - -/* walk all folios in bio, ending page IO on them */ -bio_for_each_folio_all (fi, bio) { -iomap_finish_folio_write (inode, fi.folio, fi.length, - error); - folio_count++; - } -bio_put (bio); +/* walk all folios in bio, ending page IO on them */ +bio_for_each_folio_all (fi, bio) { +iomap_finish_folio_write (inode, fi.folio, fi.length, error); + folio_count++; } -/* The ioend has been freed by bio_put() */ -if (unlikely (error && !quiet)) { +if (unlikely (error && !bio_flagged (bio,BIO_QUIET ))) {printk_ratelimited (KERN_ERR "%s: writeback error on inode %lu, offset %lld, sector %llu" , - inode->i_sb->s_id, inode->i_ino, offset, start); + inode->i_sb->s_id, inode->i_ino, + ioend->io_offset, ioend->io_sector); } +bio_put (bio);/* frees the ioend */ return folio_count; }
@@ -1563,7 +1546,7 @@ EXPORT_SYMBOL_GPL(iomap_finish_ioends);static bool iomap_ioend_can_merge (struct iomap_ioend *ioend,struct iomap_ioend *next) { -if (ioend->io_bio->bi_status != next->io_bio->bi_status) +if (ioend->io_bio.bi_status != next->io_bio.bi_status)return false;if ((ioend->io_flags &IOMAP_F_SHARED ) ^ (next->io_flags &IOMAP_F_SHARED ))
@@ -1628,9 +1611,8 @@ EXPORT_SYMBOL_GPL(iomap_sort_ioends);static void iomap_writepage_end_bio (struct bio *bio) { -struct iomap_ioend *ioend = bio->bi_private; - -iomap_finish_ioend (ioend,blk_status_to_errno (bio->bi_status)); +iomap_finish_ioend (iomap_ioend_from_bio (bio), +blk_status_to_errno (bio->bi_status)); }/*
@@ -1645,9 +1627,6 @@ static int iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend, int error) { - ioend->io_bio->bi_private = ioend; - ioend->io_bio->bi_end_io = iomap_writepage_end_bio; - if (wpc->ops->prepare_ioend) error = wpc->ops->prepare_ioend (ioend, error);if (error) {
@@ -1657,12 +1636,12 @@ iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,* as there is only one reference to the ioend at this point in * time. */ - ioend->io_bio->bi_status =errno_to_blk_status (error); -bio_endio (ioend->io_bio); + ioend->io_bio.bi_status =errno_to_blk_status (error); +bio_endio (&ioend->io_bio);return error; } -submit_bio (ioend->io_bio); +submit_bio (&ioend->io_bio);return 0 ; }
@@ -1676,44 +1655,22 @@ static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc,REQ_OP_WRITE |wbc_to_write_flags (wbc), GFP_NOFS, &iomap_ioend_bioset); bio->bi_iter.bi_sector =iomap_sector (&wpc->iomap, pos); + bio->bi_end_io = iomap_writepage_end_bio;wbc_init_bio (wbc, bio); - ioend = container_of(bio, struct iomap_ioend, io_inline_bio); + ioend =iomap_ioend_from_bio (bio);INIT_LIST_HEAD (&ioend->io_list); ioend->io_type = wpc->iomap.type; ioend->io_flags = wpc->iomap.flags; ioend->io_inode = inode; ioend->io_size =0 ; ioend->io_offset = pos; - ioend->io_bio = bio; ioend->io_sector = bio->bi_iter.bi_sector; wpc->nr_folios =0 ;return ioend; } -/* -* Allocate a new bio, and chain the old bio to the new one. -* -* Note that we have to perform the chaining in this unintuitive order -* so that the bi_private linkage is set up in the right direction for the -* traversal in iomap_finish_ioend(). -*/ -static struct bio * -iomap_chain_bio (struct bio *prev) -{ -struct bio *new; - - new =bio_alloc (prev->bi_bdev,BIO_MAX_VECS , prev->bi_opf,GFP_NOFS ); -bio_clone_blkg_association (new, prev); - new->bi_iter.bi_sector =bio_end_sector (prev); - -bio_chain (prev, new); -bio_get (prev);/* for iomap_finish_ioend */ -submit_bio (prev); -return new; -} -static bool iomap_can_add_to_ioend (struct iomap_writepage_ctx *wpc,loff_t pos) { if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
@@ -1724,7 +1681,7 @@ static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos)if (pos != wpc->ioend->io_offset + wpc->ioend->io_size)return false;if (iomap_sector (&wpc->iomap, pos) != -bio_end_sector (wpc->ioend->io_bio)) +bio_end_sector (&wpc->ioend->io_bio))return false;/* * Limit ioend bio chain lengths to minimise IO completion latency. This
@@ -1749,15 +1706,14 @@ static void iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,size_t poff =offset_in_folio (folio, pos);if (!wpc->ioend || !iomap_can_add_to_ioend (wpc, pos)) { +new_ioend:if (wpc->ioend)list_add (&wpc->ioend->io_list, iolist); wpc->ioend =iomap_alloc_ioend (wpc, wbc, inode, pos); } -if (!bio_add_folio (wpc->ioend->io_bio, folio, len, poff)) { - wpc->ioend->io_bio =iomap_chain_bio (wpc->ioend->io_bio); -bio_add_folio_nofail (wpc->ioend->io_bio, folio, len, poff); - } +if (!bio_add_folio (&wpc->ioend->io_bio, folio, len, poff)) + goto new_ioend;if (ifs)atomic_add (len, &ifs->write_bytes_pending);
@@ -1988,7 +1944,7 @@ EXPORT_SYMBOL_GPL(iomap_writepages);static int __init iomap_init (void ) {return bioset_init (&iomap_ioend_bioset,4 * (PAGE_SIZE /SECTOR_SIZE ), - offsetof(struct iomap_ioend , io_inline_bio), + offsetof(struct iomap_ioend , io_bio),BIOSET_NEED_BVECS ); }fs_initcall (iomap_init);
fs/xfs/xfs_aops.c +3 -3
@@ -112,7 +112,7 @@ xfs_end_ioend(* longer dirty. If we don't remove delalloc blocks here, they become * stale and can corrupt free space accounting on unmount. */ - error =blk_status_to_errno (ioend->io_bio->bi_status); + error =blk_status_to_errno (ioend->io_bio.bi_status);if (unlikely (error)) {if (ioend->io_flags &IOMAP_F_SHARED ) {xfs_reflink_cancel_cow_range (ip, offset, size, true);
@@ -179,7 +179,7 @@ STATIC voidxfs_end_bio ( struct bio *bio) { -struct iomap_ioend *ioend = bio->bi_private; +struct iomap_ioend *ioend =iomap_ioend_from_bio (bio);struct xfs_inode *ip =XFS_I (ioend->io_inode);unsigned long flags;
@@ -444,7 +444,7 @@ xfs_prepare_ioend(/* send ioends that might require a transaction to the completion wq */ if (xfs_ioend_is_append (ioend) || ioend->io_type ==IOMAP_UNWRITTEN || (ioend->io_flags &IOMAP_F_SHARED )) - ioend->io_bio->bi_end_io = xfs_end_bio; + ioend->io_bio.bi_end_io = xfs_end_bio;return status; }
include/linux/iomap.h +6 -2
@@ -297,10 +297,14 @@ struct iomap_ioend {size_t io_size;/* size of the extent */ loff_t io_offset;/* offset in the file */ sector_t io_sector;/* start sector of ioend */ -struct bio *io_bio;/* bio being built */ -struct bio io_inline_bio;/* MUST BE LAST! */ +struct bio io_bio;/* MUST BE LAST! */ }; +static inline struct iomap_ioend *iomap_ioend_from_bio (struct bio *bio) +{ + return container_of(bio, struct iomap_ioend, io_bio); +} +struct iomap_writeback_ops {/* * Required, maps the blocks so that writeback can be performed on
Signature · 1 line
2.39.2
PATCH 10/14 iomap: only call mapping_set_error once for each failed bio
In reply to Christoph HellwigInstead of clling mapping_set_error once per folio, only do that once per bio, and consolidate all the writeback error handling code in iomap_finish_ioend.
--- fs/iomap/buffered-io.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
fs/iomap/buffered-io.c +14 -13
@@ -1464,15 +1464,10 @@ vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)EXPORT_SYMBOL_GPL (iomap_page_mkwrite);static void iomap_finish_folio_write (struct inode *inode,struct folio *folio, -size_t len,int error) +size_t len) {struct iomap_folio_state *ifs = folio->private; -if (error) { -folio_set_error (folio); -mapping_set_error (inode->i_mapping, error); - } -WARN_ON_ONCE (i_blocks_per_folio (inode, folio) >1 && !ifs);WARN_ON_ONCE (ifs &&atomic_read (&ifs->write_bytes_pending) <=0 );
@@ -1493,18 +1488,24 @@ iomap_finish_ioend(struct iomap_ioend *ioend, int error)struct folio_iter fi;u32 folio_count =0 ; +if (error) { +mapping_set_error (inode->i_mapping, error); +if (!bio_flagged (bio,BIO_QUIET )) { +pr_err_ratelimited ( +"%s: writeback error on inode %lu, offset %lld, sector %llu" , + inode->i_sb->s_id, inode->i_ino, + ioend->io_offset, ioend->io_sector); + } + } +/* walk all folios in bio, ending page IO on them */ bio_for_each_folio_all (fi, bio) { -iomap_finish_folio_write (inode, fi.folio, fi.length, error); +if (error) +folio_set_error (fi.folio); +iomap_finish_folio_write (inode, fi.folio, fi.length); folio_count++; } -if (unlikely (error && !bio_flagged (bio,BIO_QUIET ))) { -printk_ratelimited (KERN_ERR -"%s: writeback error on inode %lu, offset %lld, sector %llu" , - inode->i_sb->s_id, inode->i_ino, - ioend->io_offset, ioend->io_sector); - }bio_put (bio);/* frees the ioend */ return folio_count; }
Signature · 1 line
2.39.2
PATCH 11/14 iomap: factor out a iomap_writepage_map_block helper
In reply to Christoph HellwigSplit the loop body that calls into the file system to map a block and add it to the ioend into a separate helper to prefer for refactoring of the surrounding code.
Note that this was the only place in iomap_writepage_map that could return an error, so include the call to ->discard_folio into the new helper as that will help to avoid code duplication in the future.
--- fs/iomap/buffered-io.c | 70 ++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 27 deletions(-)
fs/iomap/buffered-io.c +43 -27
@@ -1722,6 +1722,45 @@ static void iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,wbc_account_cgroup_owner (wbc, &folio->page, len); } +static int iomap_writepage_map_blocks (struct iomap_writepage_ctx *wpc, +struct writeback_control *wbc,struct folio *folio, +struct inode *inode,u64 pos,unsigned *count, +struct list_head *submit_list) +{ +int error; + + error = wpc->ops->map_blocks (wpc, inode, pos); +if (error) + goto fail; +trace_iomap_writepage_map (inode, &wpc->iomap); + +switch (wpc->iomap.type) { +case IOMAP_INLINE : +WARN_ON_ONCE (1 ); + error = -EIO ; +break ; +case IOMAP_HOLE : +break ; +default : +iomap_add_to_ioend (wpc, wbc, folio, inode, pos, submit_list); + (*count)++; + } + +fail: +/* +* We cannot cancel the ioend directly here on error. We may have +* already set other pages under writeback and hence we have to run I/O +* completion to mark the error state of the pages under writeback +* appropriately. +* +* Just let the file system know what portion of the folio failed to +* map. +*/ +if (error && wpc->ops->discard_folio) + wpc->ops->discard_folio (folio, pos); +return error; +} +/* * Check interaction of the folio with the file end. *
@@ -1806,7 +1845,8 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,unsigned nblocks =i_blocks_per_folio (inode, folio);u64 pos =folio_pos (folio);u64 end_pos = pos +folio_size (folio); -int error =0 , count =0 , i; +unsigned count =0 ; +int error =0 , i;LIST_HEAD (submit_list);trace_iomap_writepage (inode, pos,folio_size (folio));
@@ -1832,19 +1872,10 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,for (i =0 ; i < nblocks && pos < end_pos; i++, pos += len) {if (ifs && !ifs_block_is_dirty (folio, ifs, i))continue ; - - error = wpc->ops->map_blocks (wpc, inode, pos); + error =iomap_writepage_map_blocks (wpc, wbc, folio, inode, pos, + &count, &submit_list);if (error)break ; -trace_iomap_writepage_map (inode, &wpc->iomap); -if (WARN_ON_ONCE (wpc->iomap.type ==IOMAP_INLINE )) { - error = -EIO ; -break ; - } -if (wpc->iomap.type ==IOMAP_HOLE ) -continue ; -iomap_add_to_ioend (wpc, wbc, folio, inode, pos, &submit_list); - count++; }if (count) wpc->nr_folios++;
@@ -1854,21 +1885,6 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,WARN_ON_ONCE (folio_test_writeback (folio));WARN_ON_ONCE (folio_test_dirty (folio)); -/* -* We cannot cancel the ioend directly here on error. We may have -* already set other pages under writeback and hence we have to run I/O -* completion to mark the error state of the pages under writeback -* appropriately. -*/ -if (unlikely (error)) { -/* -* Let the filesystem know what portion of the current page -* failed to map. -*/ -if (wpc->ops->discard_folio) - wpc->ops->discard_folio (folio, pos); - } -/* * We can have dirty bits set past end of file in page_mkwrite path * while mapping the last partial folio. Hence it's better to clear
Signature · 1 line
2.39.2
PATCH 12/14 iomap: submit ioends immediately
In reply to Christoph HellwigCurrently the writeback code delays submitting fill ioends until we reach the end of the folio. The reason for that is that otherwise the end I/O handler could clear the writeback bit before we've even finished submitting all I/O for the folio.
Add a bias to ifs->write_bytes_pending while we are submitting I/O for a folio so that it never reaches zero until all I/O is completed to prevent the early writeback bit clearing, and remove the now superfluous submit_list.
--- fs/iomap/buffered-io.c | 162 +++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 86 deletions(-)
fs/iomap/buffered-io.c +76 -86
@@ -1620,30 +1620,34 @@ static void iomap_writepage_end_bio(struct bio *bio)* Submit the final bio for an ioend. * * If @error is non-zero, it means that we have a situation where some part of -* the submission process has failed after we've marked pages for writeback -* and unlocked them. In this situation, we need to fail the bio instead of -* submitting it. This typically only happens on a filesystem shutdown. +* the submission process has failed after we've marked pages for writeback. +* We cannot cancel ioend directly in that case, so call the bio end I/O handler +* with the error status here to run the normal I/O completion handler to clear +* the writeback bit and let the file system proess the errors. */ -static int -iomap_submit_ioend (struct iomap_writepage_ctx *wpc,struct iomap_ioend *ioend, -int error) +static int iomap_submit_ioend (struct iomap_writepage_ctx *wpc,int error) { +if (!wpc->ioend) +return error; + +/* +* Let the file systems prepare the I/O submission and hook in an I/O +* comletion handler. This also needs to happen in case after a +* failure happened so that the file system end I/O handler gets called +* to clean up. +*/ if (wpc->ops->prepare_ioend) - error = wpc->ops->prepare_ioend (ioend, error); + error = wpc->ops->prepare_ioend (wpc->ioend, error); +if (error) { -/* -* If we're failing the IO now, just mark the ioend with an -* error and finish it. This will run IO completion immediately -* as there is only one reference to the ioend at this point in -* time. -*/ - ioend->io_bio.bi_status =errno_to_blk_status (error); -bio_endio (&ioend->io_bio); -return error; + wpc->ioend->io_bio.bi_status =errno_to_blk_status (error); +bio_endio (&wpc->ioend->io_bio); + }else { +submit_bio (&wpc->ioend->io_bio); } -submit_bio (&ioend->io_bio); -return 0 ; + wpc->ioend =NULL ; +return error; } static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc,
@@ -1697,19 +1701,28 @@ static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos)/* * Test to see if we have an existing ioend structure that we could append to * first; otherwise finish off the current ioend and start another. +* +* If a new ioend is created and cached, the old ioend is submitted to the block +* layer instantly. Batching optimisations are provided by higher level block +* plugging. +* +* At the end of a writeback pass, there will be a cached ioend remaining on the +* writepage context that the caller will need to submit. */ -static void iomap_add_to_ioend (struct iomap_writepage_ctx *wpc, +static int iomap_add_to_ioend (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio, -struct inode *inode,loff_t pos,struct list_head *iolist) +struct inode *inode,loff_t pos) {struct iomap_folio_state *ifs = folio->private;unsigned len =i_blocksize (inode);size_t poff =offset_in_folio (folio, pos); +int error;if (!wpc->ioend || !iomap_can_add_to_ioend (wpc, pos)) { new_ioend: -if (wpc->ioend) -list_add (&wpc->ioend->io_list, iolist); + error =iomap_submit_ioend (wpc,0 ); +if (error) +return error; wpc->ioend =iomap_alloc_ioend (wpc, wbc, inode, pos); }
@@ -1720,12 +1733,12 @@ static void iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,atomic_add (len, &ifs->write_bytes_pending); wpc->ioend->io_size += len;wbc_account_cgroup_owner (wbc, &folio->page, len); +return 0 ; }static int iomap_writepage_map_blocks (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio, -struct inode *inode,u64 pos,unsigned *count, -struct list_head *submit_list) +struct inode *inode,u64 pos,unsigned *count) {int error;
@@ -1742,8 +1755,9 @@ static int iomap_writepage_map_blocks(struct iomap_writepage_ctx *wpc,case IOMAP_HOLE :break ;default : -iomap_add_to_ioend (wpc, wbc, folio, inode, pos, submit_list); - (*count)++; + error =iomap_add_to_ioend (wpc, wbc, folio, inode, pos); +if (!error) + (*count)++; } fail:
@@ -1819,35 +1833,21 @@ static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,return true; } -/* -* We implement an immediate ioend submission policy here to avoid needing to -* chain multiple ioends and hence nest mempool allocations which can violate -* the forward progress guarantees we need to provide. The current ioend we're -* adding blocks to is cached in the writepage context, and if the new block -* doesn't append to the cached ioend, it will create a new ioend and cache that -* instead. -* -* If a new ioend is created and cached, the old ioend is returned and queued -* locally for submission once the entire page is processed or an error has been -* detected. While ioends are submitted immediately after they are completed, -* batching optimisations are provided by higher level block plugging. -* -* At the end of a writeback pass, there will be a cached ioend remaining on the -* writepage context that the caller will need to submit. -*/ static int iomap_writepage_map (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio) {struct iomap_folio_state *ifs = folio->private;struct inode *inode = folio->mapping->host; -struct iomap_ioend *ioend, *next;unsigned len =i_blocksize (inode);unsigned nblocks =i_blocks_per_folio (inode, folio);u64 pos =folio_pos (folio);u64 end_pos = pos +folio_size (folio);unsigned count =0 ;int error =0 , i; -LIST_HEAD (submit_list); + +WARN_ON_ONCE (!folio_test_locked (folio)); +WARN_ON_ONCE (folio_test_dirty (folio)); +WARN_ON_ONCE (folio_test_writeback (folio));trace_iomap_writepage (inode, pos,folio_size (folio));
@@ -1857,12 +1857,27 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc, }WARN_ON_ONCE (end_pos <= pos); -if (!ifs && nblocks >1 ) { - ifs =ifs_alloc (inode, folio,0 ); -iomap_set_range_dirty (folio,0 , end_pos - pos); +if (nblocks >1 ) { +if (!ifs) { + ifs =ifs_alloc (inode, folio,0 ); +iomap_set_range_dirty (folio,0 , end_pos - pos); + } + +/* +* Keep the I/O completion handler from clearing the writeback +* bit until we have submitted all blocks by adding a bias to +* ifs->write_bytes_pending, which is dropped after submitting +* all blocks. +*/ +WARN_ON_ONCE (atomic_read (&ifs->write_bytes_pending) !=0 ); +atomic_inc (&ifs->write_bytes_pending); } -WARN_ON_ONCE (ifs &&atomic_read (&ifs->write_bytes_pending) !=0 ); +/* +* Set the writeback bit ASAP, as the I/O completion for the single +* block per folio case happen hit as soon as we're submitting the bio. +*/ +folio_start_writeback (folio);/* * Walk through the folio to find areas to write back. If we
@@ -1873,18 +1888,13 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,if (ifs && !ifs_block_is_dirty (folio, ifs, i))continue ; error =iomap_writepage_map_blocks (wpc, wbc, folio, inode, pos, - &count, &submit_list); + &count);if (error)break ; }if (count) wpc->nr_folios++; -WARN_ON_ONCE (!wpc->ioend && !list_empty (&submit_list)); -WARN_ON_ONCE (!folio_test_locked (folio)); -WARN_ON_ONCE (folio_test_writeback (folio)); -WARN_ON_ONCE (folio_test_dirty (folio)); -/* * We can have dirty bits set past end of file in page_mkwrite path * while mapping the last partial folio. Hence it's better to clear
@@ -1893,38 +1903,20 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,iomap_clear_range_dirty (folio,0 ,folio_size (folio));/* -* If the page hasn't been added to the ioend, it won't be affected by -* I/O completion and we must unlock it now. +* Usually the writeback bit is cleared by the I/O completion handler. +* But we may end up either not actually writing any blocks, or (when +* there are multiple blocks in a folio) all I/O might have finished +* already at this point. In that case we need to clear the writeback +* bit ourselves right after unlocking the page. */ -if (error && !count) { -folio_unlock (folio); - goto done; - } - -folio_start_writeback (folio);folio_unlock (folio); - -/* -* Preserve the original error if there was one; catch -* submission errors here and propagate into subsequent ioend -* submissions. -*/ -list_for_each_entry_safe (ioend, next, &submit_list, io_list) { -int error2; - -list_del_init (&ioend->io_list); - error2 =iomap_submit_ioend (wpc, ioend, error); -if (error2 && !error) - error = error2; +if (ifs) { +if (atomic_dec_and_test (&ifs->write_bytes_pending)) +folio_end_writeback (folio); + }else { +if (!count) +folio_end_writeback (folio); } - -/* -* We can end up here with no error and nothing to write only if we race -* with a partial page truncate on a sub-page block sized filesystem. -*/ -if (!count) -folio_end_writeback (folio); -done:mapping_set_error (inode->i_mapping, error);return error; }
@@ -1952,9 +1944,7 @@ iomap_writepages(struct address_space *mapping, struct writeback_control *wbc, wpc->ops = ops; ret =write_cache_pages (mapping, wbc, iomap_do_writepage, wpc); -if (!wpc->ioend) -return ret; -return iomap_submit_ioend (wpc, wpc->ioend, ret); +return iomap_submit_ioend (wpc, ret); }EXPORT_SYMBOL_GPL (iomap_writepages);
Signature · 1 line
2.39.2
PATCH 13/14 iomap: map multiple blocks at a time
In reply to Christoph HellwigThe ->map_blocks interface returns a valid range for writeback, but we still call back into it for every block, which is a bit inefficient.
Change iomap_writepage_map to use the valid range in the map until the end of the folio or the dirty range inside the folio instead of calling back into every block.
Note that the range is not used over folio boundaries as we need to be able to check the mapping sequence count under the folio lock.
--- fs/iomap/buffered-io.c | 116 ++++++++++++++++++++++++++++------------- include/linux/iomap.h | 7 +++ 2 files changed, 88 insertions(+), 35 deletions(-)
fs/iomap/buffered-io.c +81 -35
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* * Copyright (C) 2010 Red Hat, Inc. -* Copyright (C) 2016-2019 Christoph Hellwig. +* Copyright (C) 2016-2023 Christoph Hellwig. */ #include <linux/module.h> #include <linux/compiler.h>
@@ -95,6 +95,44 @@ static inline bool ifs_block_is_dirty(struct folio *folio,return test_bit (block + blks_per_folio, ifs->state); } +static unsigned ifs_find_dirty_range (struct folio *folio, +struct iomap_folio_state *ifs,u64 *range_start,u64 range_end) +{ +struct inode *inode = folio->mapping->host; +unsigned start_blk = +offset_in_folio (folio, *range_start) >> inode->i_blkbits; +unsigned end_blk =min_not_zero ( +offset_in_folio (folio, range_end) >> inode->i_blkbits, +i_blocks_per_folio (inode, folio)); +unsigned nblks =1 ; + +while (!ifs_block_is_dirty (folio, ifs, start_blk)) +if (++start_blk == end_blk) +return 0 ; + +while (start_blk + nblks < end_blk) { +if (!ifs_block_is_dirty (folio, ifs, start_blk + nblks)) +break ; + nblks++; + } + + *range_start =folio_pos (folio) + (start_blk << inode->i_blkbits); +return nblks << inode->i_blkbits; +} + +static unsigned iomap_find_dirty_range (struct folio *folio,u64 *range_start, +u64 range_end) +{ +struct iomap_folio_state *ifs = folio->private; + +if (*range_start >= range_end) +return 0 ; + +if (ifs) +return ifs_find_dirty_range (folio, ifs, range_start, range_end); +return range_end - *range_start; +} +static void ifs_clear_range_dirty (struct folio *folio,struct iomap_folio_state *ifs,size_t off,size_t len) {
@@ -1711,10 +1749,9 @@ static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos)*/ static int iomap_add_to_ioend (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio, -struct inode *inode,loff_t pos) +struct inode *inode,loff_t pos,unsigned len) {struct iomap_folio_state *ifs = folio->private; -unsigned len =i_blocksize (inode);size_t poff =offset_in_folio (folio, pos);int error;
@@ -1738,29 +1775,41 @@ static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,static int iomap_writepage_map_blocks (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio, -struct inode *inode,u64 pos,unsigned *count) +struct inode *inode,u64 pos,unsigned dirty_len, +unsigned *count) {int error; - error = wpc->ops->map_blocks (wpc, inode, pos); -if (error) - goto fail; -trace_iomap_writepage_map (inode, &wpc->iomap); - -switch (wpc->iomap.type) { -case IOMAP_INLINE : -WARN_ON_ONCE (1 ); - error = -EIO ; -break ; -case IOMAP_HOLE : -break ; -default : - error =iomap_add_to_ioend (wpc, wbc, folio, inode, pos); -if (!error) - (*count)++; - } +do { +unsigned map_len; + + error = wpc->ops->map_blocks (wpc, inode, pos); +if (error) +break ; +trace_iomap_writepage_map (inode, &wpc->iomap); + + map_len =min_t (u64, dirty_len, + wpc->iomap.offset + wpc->iomap.length - pos); +WARN_ON_ONCE (!folio->private && map_len < dirty_len); + +switch (wpc->iomap.type) { +case IOMAP_INLINE : +WARN_ON_ONCE (1 ); + error = -EIO ; +break ; +case IOMAP_HOLE : +break ; +default : + error =iomap_add_to_ioend (wpc, wbc, folio, inode, pos, + map_len); +if (!error) + (*count)++; +break ; + } + dirty_len -= map_len; + pos += map_len; + }while (dirty_len && !error); -fail:/* * We cannot cancel the ioend directly here on error. We may have * already set other pages under writeback and hence we have to run I/O
@@ -1827,7 +1876,7 @@ static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,* beyond i_size. */ folio_zero_segment (folio, poff,folio_size (folio)); - *end_pos = isize; + *end_pos =round_up (isize,i_blocksize (inode)); }return true;
@@ -1838,12 +1887,11 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc, {struct iomap_folio_state *ifs = folio->private;struct inode *inode = folio->mapping->host; -unsigned len =i_blocksize (inode); -unsigned nblocks =i_blocks_per_folio (inode, folio);u64 pos =folio_pos (folio);u64 end_pos = pos +folio_size (folio);unsigned count =0 ; -int error =0 , i; +int error =0 ; +u32 rlen;WARN_ON_ONCE (!folio_test_locked (folio));WARN_ON_ONCE (folio_test_dirty (folio));
@@ -1857,7 +1905,7 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc, }WARN_ON_ONCE (end_pos <= pos); -if (nblocks >1 ) { +if (i_blocks_per_folio (inode, folio) >1 ) {if (!ifs) { ifs =ifs_alloc (inode, folio,0 );iomap_set_range_dirty (folio,0 , end_pos - pos);
@@ -1880,18 +1928,16 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,folio_start_writeback (folio);/* -* Walk through the folio to find areas to write back. If we -* run off the end of the current map or find the current map -* invalid, grab a new one. +* Walk through the folio to find dirty areas to write back. */ -for (i =0 ; i < nblocks && pos < end_pos; i++, pos += len) { -if (ifs && !ifs_block_is_dirty (folio, ifs, i)) -continue ; - error =iomap_writepage_map_blocks (wpc, wbc, folio, inode, pos, - &count); +while ((rlen =iomap_find_dirty_range (folio, &pos, end_pos))) { + error =iomap_writepage_map_blocks (wpc, wbc, folio, inode, + pos, rlen, &count);if (error)break ; + pos += rlen; } +if (count) wpc->nr_folios++;
include/linux/iomap.h +7 -0
@@ -309,6 +309,13 @@ struct iomap_writeback_ops {/* * Required, maps the blocks so that writeback can be performed on * the range starting at offset. +* +* Can return arbitrarily large regions, but we need to call into it at +* least once per folio to allow the file systems to synchronize with +* the write path that could be invalidating mappings. +* +* An existing mapping from a previous call to this method can be reused +* by the file system if it is still valid. */ int (*map_blocks)(struct iomap_writepage_ctx *wpc,struct inode *inode,loff_t offset);
Signature · 1 line
2.39.2
Re: [PATCH 13/14] iomap: map multiple blocks at a time
Hi, Christoph.
Quoted 16 lines · Christoph Hellwig
The ->map_blocks interface returns a valid range for writeback, but we still call back into it for every block, which is a bit inefficient.
Change
iomap_writepage_mapto use the valid range in the map until the end of the folio or the dirty range inside the folio instead of calling back into every block.Note that the range is not used over folio boundaries as we need to be able to check the mapping sequence count under the folio lock.
Signed-off-by Christoph Hellwig <[email protected]>--- fs/iomap/buffered-io.c | 116 ++++++++++++++++++++++++++++------------- include/linux/iomap.h | 7 +++ 2 files changed, 88 insertions(+), 35 deletions(-)
[..]
Quoted 28 lines
fs/iomap/buffered-io.c +3
-18@@ -1738,29 +1775,41 @@ static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,static int iomap_writepage_map_blocks (struct iomap_writepage_ctx *wpc,struct writeback_control *wbc,struct folio *folio, -struct inode *inode,u64 pos,unsigned *count) +struct inode *inode,u64 pos,unsigned dirty_len, +unsigned *count) {int error; - error = wpc->ops->map_blocks (wpc, inode, pos); -if (error) - goto fail; -trace_iomap_writepage_map (inode, &wpc->iomap); - -switch (wpc->iomap.type) { -case IOMAP_INLINE : -WARN_ON_ONCE (1 ); - error = -EIO ; -break ; -case IOMAP_HOLE : -break ; -default : - error =iomap_add_to_ioend (wpc, wbc, folio, inode, pos); -if (!error) - (*count)++; - } + do {
fs/iomap/buffered-io.c static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, · line 1793
Quoted 10 lines
fs/iomap/buffered-io.c +10
-0+unsigned map_len; + + error = wpc->ops->map_blocks (wpc, inode, pos); +if (error) +break ; +trace_iomap_writepage_map (inode, &wpc->iomap); + + map_len =min_t (u64, dirty_len, + wpc->iomap.offset + wpc->iomap.length - pos); +WARN_ON_ONCE (!folio->private && map_len < dirty_len);
While I was debugging this series on ext4, I would suggest try to add map_len or dirty_len into this trace point could be more convenient.
fs/iomap/buffered-io.c static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, · line 1801
Quoted 8 lines
fs/iomap/buffered-io.c +8
-0+ +switch (wpc->iomap.type) { +case IOMAP_INLINE : +WARN_ON_ONCE (1 ); + error = -EIO ; +break ; +case IOMAP_HOLE : +break ;
BTW, I want to ask an unrelated question of this patch series. Do you agree with me to add a IOMAP_DELAYED case and re-dirty folio here? The background is that on ext4, jbd2 thread call ext4_normal_submit_inode_data_buffers() submit data blocks in data=ordered mode, but it can only submit mapped blocks, now we skip unmapped blocks and re-dirty folios in ext4_do_writepages()->mpage_prepare_extent_to_map()->..->ext4_bio_write_folio(). So we have to inherit this logic when convert to iomap, I suppose ext4's ->map_blocks() return IOMAP_DELALLOC for this case, and iomap do something like:
+ case IOMAP_DELALLOC:
+ iomap_set_range_dirty(folio, offset_in_folio(folio, pos),
+ map_len);
+ folio_redirty_for_writepage(wbc, folio);
+ break;
Thanks,
Yi.
Quoted 24 lines
fs/iomap/buffered-io.c +11
-2+default : + error =iomap_add_to_ioend (wpc, wbc, folio, inode, pos, + map_len); +if (!error) + (*count)++; +break ; + } + dirty_len -= map_len; + pos += map_len; + }while (dirty_len && !error); -fail:/* * We cannot cancel the ioend directly here on error. We may have * already set other pages under writeback and hence we have to run I/O @@ -1827,7 +1876,7 @@ static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,* beyond i_size. */ folio_zero_segment (folio, poff,folio_size (folio)); - *end_pos = isize; + *end_pos =round_up (isize,i_blocksize (inode)); }return true;
Re: [PATCH 13/14] iomap: map multiple blocks at a time
Quoted 6 lines · Zhang Yi
Quoted 6 lines
fs/iomap/buffered-io.c +6
-0+ do { + unsigned map_len; + + error = wpc->ops->map_blocks(wpc, inode, pos); + if (error) + break;
Quoted 8 lines
fs/iomap/buffered-io.c static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, · line 1793
Quoted 5 lines
fs/iomap/buffered-io.c +5
-0+trace_iomap_writepage_map (inode, &wpc->iomap); + + map_len =min_t (u64, dirty_len, + wpc->iomap.offset + wpc->iomap.length - pos); +WARN_ON_ONCE (!folio->private && map_len < dirty_len);While I was debugging this series on ext4, I would suggest try to add
map_lenordirty_leninto this trace point could be more convenient.
That does seem useful, but it means we need to have an entirely new even class. Can I offload this to you for inclusion in your ext4 series? :)
Quoted 13 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ case IOMAP_HOLE: + break;BTW, I want to ask an unrelated question of this patch series. Do you agree with me to add a
IOMAP_DELAYEDcase and re-dirty folio here? The background is that on ext4, jbd2 thread callext4_normal_submit_inode_data_buffers()submit data blocks in data=ordered mode, but it can only submit mapped blocks, now we skip unmapped blocks and re-dirty folios in ext4_do_writepages()->mpage_prepare_extent_to_map()->..->ext4_bio_write_folio(). So we have to inherit this logic when convert to iomap, I suppose ext4's ->map_blocks() returnIOMAP_DELALLOCfor this case, and iomap do something like:
Review
Quoted 5 lines
(quoted diff) +5
-0+ case IOMAP_DELALLOC: + iomap_set_range_dirty(folio, offset_in_folio(folio, pos), + map_len); + folio_redirty_for_writepage(wbc, folio); + break;
I guess we could add it, but it feels pretty quirky to me, so it would at least need a very big comment.
But I think Ted mentioned a while ago that dropping the classic data=ordered mode for ext4 might be a good idea eventually no that ext4 can update the inode size at I/O completion time (Ted, correct me if I'm wrong). If that's the case it might make sense to just drop the ordered mode instead of adding these quirks to iomap.
Re: [PATCH 13/14] iomap: map multiple blocks at a time
Quoted 16 lines · Christoph Hellwig
Quoted 6 lines · Zhang Yi
Quoted 6 lines
fs/iomap/buffered-io.c +6
-0+ do { + unsigned map_len; + + error = wpc->ops->map_blocks(wpc, inode, pos); + if (error) + break;Quoted 8 lines
fs/iomap/buffered-io.c static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, · line 1793
Quoted 5 lines
fs/iomap/buffered-io.c +5
-0+trace_iomap_writepage_map (inode, &wpc->iomap); + + map_len =min_t (u64, dirty_len, + wpc->iomap.offset + wpc->iomap.length - pos); +WARN_ON_ONCE (!folio->private && map_len < dirty_len);While I was debugging this series on ext4, I would suggest try to add
map_lenordirty_leninto this trace point could be more convenient.
Quoted 4 lines
That does seem useful, but it means we need to have an entirely new even class. Can I offload this to you for inclusion in your ext4 series? :)
Sure, I'm glad to do it.
Quoted 19 lines
Quoted 13 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ case IOMAP_HOLE: + break;BTW, I want to ask an unrelated question of this patch series. Do you agree with me to add a
IOMAP_DELAYEDcase and re-dirty folio here? The background is that on ext4, jbd2 thread callext4_normal_submit_inode_data_buffers()submit data blocks in data=ordered mode, but it can only submit mapped blocks, now we skip unmapped blocks and re-dirty folios in ext4_do_writepages()->mpage_prepare_extent_to_map()->..->ext4_bio_write_folio(). So we have to inherit this logic when convert to iomap, I suppose ext4's ->map_blocks() returnIOMAP_DELALLOCfor this case, and iomap do something like:Review
Quoted 5 lines
(quoted diff) +5
-0+ case IOMAP_DELALLOC: + iomap_set_range_dirty(folio, offset_in_folio(folio, pos), + map_len); + folio_redirty_for_writepage(wbc, folio); + break;
Quoted 9 lines
I guess we could add it, but it feels pretty quirky to me, so it would at least need a very big comment.
But I think Ted mentioned a while ago that dropping the classic data=ordered mode for ext4 might be a good idea eventually no that ext4 can update the inode size at I/O completion time (Ted, correct me if I'm wrong). If that's the case it might make sense to just drop the ordered mode instead of adding these quirks to iomap.
Yeah, make sense, we could remove these quirks after ext4 drop data=ordered mode. For now, let me implement it according to this temporary method.
Thanks,
Yi.
PATCH 14/14 iomap: pass the length of the dirty region to ->map_blocks
In reply to Christoph HellwigLet the file system know how much dirty data exists at the passed in offset. This allows file systems to allocate the right amount of space that actually is written back if they can't eagerly convert (e.g. because they don't support unwritten extents).
--- block/fops.c | 2 +- fs/gfs2/bmap.c | 2 +- fs/iomap/buffered-io.c | 2 +- fs/xfs/xfs_aops.c | 3 ++- fs/zonefs/file.c | 3 ++- include/linux/iomap.h | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-)
block/fops.c +1 -1
@@ -467,7 +467,7 @@ static void blkdev_readahead(struct readahead_control *rac) }static int blkdev_map_blocks (struct iomap_writepage_ctx *wpc, -struct inode *inode,loff_t offset) +struct inode *inode,loff_t offset,unsigned int len) {loff_t isize =i_size_read (inode);
fs/gfs2/bmap.c +1 -1
@@ -2465,7 +2465,7 @@ int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length) }static int gfs2_map_blocks (struct iomap_writepage_ctx *wpc,struct inode *inode, -loff_t offset) +loff_t offset,unsigned int len) {int ret;
fs/iomap/buffered-io.c +1 -1
@@ -1783,7 +1783,7 @@ static int iomap_writepage_map_blocks(struct iomap_writepage_ctx *wpc, do { unsigned map_len; - error = wpc->ops->map_blocks(wpc, inode, pos); + error = wpc->ops->map_blocks(wpc, inode, pos, dirty_len); if (error) break; trace_iomap_writepage_map(inode, &wpc->iomap);
fs/xfs/xfs_aops.c +2 -1
@@ -276,7 +276,8 @@ static intxfs_map_blocks ( struct iomap_writepage_ctx *wpc, struct inode *inode, - loff_t offset) + loff_t offset, + unsigned int len) { struct xfs_inode *ip = XFS_I(inode);struct xfs_mount *mp = ip->i_mount;
fs/zonefs/file.c +2 -1
@@ -125,7 +125,8 @@ static void zonefs_readahead(struct readahead_control *rac)* which implies that the page range can only be within the fixed inode size. */ static int zonefs_write_map_blocks (struct iomap_writepage_ctx *wpc, -struct inode *inode,loff_t offset) +struct inode *inode,loff_t offset, +unsigned int len) {struct zonefs_zone *z =zonefs_inode_zone (inode);
include/linux/iomap.h +1 -1
@@ -318,7 +318,7 @@ struct iomap_writeback_ops {* by the file system if it is still valid. */ int (*map_blocks)(struct iomap_writepage_ctx *wpc,struct inode *inode, -loff_t offset); +loff_t offset,unsigned len);/* * Optional, allows the file systems to perform actions just before
Signature · 1 line
2.39.2
Quoted 10 lines · Christoph Hellwig
this series overhaults a large chunk of the iomap writeback code with the end result that ->map_blocks can now map multiple blocks at a time, at least as long as they are all inside the same folio.
On a sufficiently large system (32 cores in my case) this significantly reduces CPU usage for buffered write workloads on xfs, with a very minor improvement in write bandwith that might be within the measurement tolerance.
[...]
Darrick, Christoph, I gave us a separate branch for this. I thought about putting this on top of vfs.misc but I feel that this would be a bit ugly. Different layout is possible though.
---
Applied to the vfs.iomap branch of the vfs/vfs.git tree. Patches in the vfs.iomap branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.iomap
[01/14] iomap: clear the per-folio dirty bits on all writeback failures
https://git.kernel.org/vfs/vfs/c/7c821e1f5a5a [02/14] iomap: treat inline data in iomap_writepage_map as an I/O error https://git.kernel.org/vfs/vfs/c/6571f184afe3 [03/14] iomap: move the io_folios field out of struct iomap_ioend https://git.kernel.org/vfs/vfs/c/e5f9e159cf10 [04/14] iomap: move the PF_MEMALLOC check to iomap_writepages https://git.kernel.org/vfs/vfs/c/fc51566e62ef [05/14] iomap: factor out a iomap_writepage_handle_eof helper https://git.kernel.org/vfs/vfs/c/89d887160535 [06/14] iomap: move all remaining per-folio logic into iomap_writepage_map https://git.kernel.org/vfs/vfs/c/6d3bac5014bf [07/14] iomap: clean up the iomap_alloc_ioend calling convention https://git.kernel.org/vfs/vfs/c/d7acac8ed175 [08/14] iomap: move the iomap_sector sector calculation out of iomap_add_to_ioend https://git.kernel.org/vfs/vfs/c/a04db4e40bdb [09/14] iomap: don't chain bios https://git.kernel.org/vfs/vfs/c/7a579e360d15 [10/14] iomap: only call mapping_set_error once for each failed bio https://git.kernel.org/vfs/vfs/c/a64f2b75da6b [11/14] iomap: factor out a iomap_writepage_map_block helper https://git.kernel.org/vfs/vfs/c/3853862b0b77 [12/14] iomap: submit ioends immediately https://git.kernel.org/vfs/vfs/c/ae00bec07dee [13/14] iomap: map multiple blocks at a time https://git.kernel.org/vfs/vfs/c/2487070c95f4 [14/14] iomap: pass the length of the dirty region to ->map_blocks https://git.kernel.org/vfs/vfs/c/a828782eaff6
Quoted 2 lines · Christian Brauner
Applied to the vfs.iomap branch of the vfs/vfs.git tree. Patches in the vfs.iomap branch should appear in linux-next soon.
So it seems like this didn't make it to Linus for the 6.8 cycle, and also doesn't appear in current linux-next. Was that an oversight or did I miss something?
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Christian Brauner
Applied to the vfs.iomap branch of the vfs/vfs.git tree. Patches in the vfs.iomap branch should appear in linux-next soon.
So it seems like this didn't make it to Linus for the 6.8 cycle, and also doesn't appear in current linux-next. Was that an oversight or did I miss something?
Speaking solely for myself, I got covid like 2 days before you sent this and didn't get my brain back from the cleaners until ~2.5 weeks after. I totally forgot that any of this was going on. :(
--D
Christian Brauner
maintainerQuoted 5 lines · Darrick J. Wong
Quoted 4 lines · Christoph Hellwig
Quoted 2 lines · Christian Brauner
Applied to the vfs.iomap branch of the vfs/vfs.git tree. Patches in the vfs.iomap branch should appear in linux-next soon.
Quoted 7 lines
Quoted 3 lines
So it seems like this didn't make it to Linus for the 6.8 cycle, and also doesn't appear in current linux-next. Was that an oversight or did I miss something?
Speaking solely for myself, I got covid like 2 days before you sent this and didn't get my brain back from the cleaners until ~2.5 weeks after. I totally forgot that any of this was going on. :(
Bah, sorry to hear that.
Sorry Christoph and Darrick. This was my fault as I seemingly dropped that branch. I've rebased this onto v6.8-rc1 and it's now in vfs.iomap and merged into vfs.all.
Going forward I'll be sending reminders to double-check branches for any accidently dropped commits. Feel free to remind me in case I miss anything!