Skip to contentSkip to conversation

mm/iomap: add granular dirty and writeback accounting

PATCH In review Joanne Koong 5 participants 33 messages Aug 29, 2025 – Oct 4, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v2 cover mm/iomap: add granular dirty and writeback accounting

This patchset adds granular dirty and writeback stats accounting for large folios.

The dirty page balancing logic uses these stats to determine things like whether the ratelimit has been exceeded, the frequency with which pages need to be written back, if dirtying should be throttled, etc. Currently for large folios, if any byte in the folio is dirtied or written back, all the bytes in the folio are accounted as such.

In particular, there are four places where dirty and writeback stats get incremented and decremented as pages get dirtied and written back:
a) folio dirtying (filemap_dirty_folio() -> ... -> folio_account_dirtied())
- increments NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE,

     current->nr_dirtied

b) writing back a mapping (writeback_iter() -> ... -> folio_clear_dirty_for_io())
- decrements NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE

c) starting writeback on a folio (folio_start_writeback())
- increments WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

d) ending writeback on a folio (folio_end_writeback())
- decrements WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

Patches 1 to 9 adds support for the 4 cases above to take in the number of pages to be accounted, instead of accounting for the entire folio.

Patch 12 adds the iomap changes that uses these new APIs. This relies on the iomap folio state bitmap to track which pages are dirty (so that we avoid any double-counting). As such we can only do granular accounting if the block size >= PAGE_SIZE.

This patchset was run through xfstests using fuse passthrough hp (with an out-of-tree kernel patch enabling fuse large folios).

This is on top of commit 4f702205 ("Merge branch 'vfs-6.18.rust' into vfs.all") in Christian's vfs tree, and on top of the patchset that removes BDI_CAP_WRITEBACK_ACCT [1].

Local benchmarks were run on xfs by doing the following:

seting up xfs (per the xfstests readme):

# xfs_io -f -c "falloc 0 10g" test.img
# xfs_io -f -c "falloc 0 10g" scratch.img
# mkfs.xfs test.img
# losetup /dev/loop0 ./test.img
# losetup /dev/loop1 ./scratch.img
# mkdir -p /mnt/test && mount /dev/loop0 /mnt/test
# sudo sysctl -w vm.dirty_bytes=$((3276 * 1024 * 1024)) # roughly 20% of 16GB
# sudo sysctl -w vm.dirty_background_bytes=$((1638*1024*1024)) # roughly 10% of 16GB

running this test program (ai-generated) [2] which essentially writes out 2 GB of data 256 MB at a time and then spins up 15 threads to do 50-byte 50k writes.

On my VM, I saw the writes take around 3 seconds (with some variability of taking 0.3 seconds to 5 seconds sometimes) in the base version vs taking a pretty consistent 0.14 seconds with this patchset. It'd be much appreciated if someone could also run it on their local system to verify they see similar numbers.

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://pastebin.com/CbcwTXjq

Changelog
v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
v1 -> v2:
* Add documentation specifying caller expectations for the filemap_dirty_folio_pages() -> __folio_mark_dirty() callpath (Jan)
* Add requested iomap bitmap iteration refactoring (Christoph)
* Fix long lines (Christoph)

Joanne Koong (12):
mm: pass number of pages to __folio_start_writeback()
mm: pass number of pages to __folio_end_writeback()
mm: add folio_end_writeback_pages() helper
mm: pass number of pages dirtied to __folio_mark_dirty()
mm: add filemap_dirty_folio_pages() helper
mm: add __folio_clear_dirty_for_io() helper
mm: add no_stats_accounting bitfield to wbc
mm: refactor clearing dirty stats into helper function
mm: add clear_dirty_for_io_stats() helper
iomap: refactor dirty bitmap iteration
iomap: refactor uptodate bitmap iteration
iomap: add granular dirty and writeback accounting

 fs/btrfs/subpage.c         |   2 +-
 fs/buffer.c                |   6 +-
 fs/ext4/page-io.c          |   2 +-
 fs/iomap/buffered-io.c     | 281 ++++++++++++++++++++++++++++++-------
 include/linux/page-flags.h |   4 +-
 include/linux/pagemap.h    |   4 +-
 include/linux/writeback.h  |  10 ++
 mm/filemap.c               |  12 +-
 mm/internal.h              |   2 +-
 mm/page-writeback.c        | 115 +++++++++++----
 10 files changed, 346 insertions(+), 92 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/12 mm: pass number of pages to __folio_start_writeback()

Add an additional arg to __folio_start_writeback() that takes in the number of pages to write back.

---
 fs/btrfs/subpage.c         |  2 +-
 fs/ext4/page-io.c          |  2 +-
 include/linux/page-flags.h |  4 ++--
 mm/page-writeback.c        | 10 +++++-----
 4 files changed, 9 insertions(+), 9 deletions(-)
fs/btrfs/subpage.c +1 -1
index cb4f97833dc3..895e0c96a8fc 100644
@@ -456,7 +456,7 @@ void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,
 	 * ordering guarantees.
 	 */
 	if (!folio_test_writeback(folio))
-		__folio_start_writeback(folio, true);
+		__folio_start_writeback(folio, true, folio_nr_pages(folio));
 	if (!folio_test_dirty(folio)) {
 		struct address_space *mapping = folio_mapping(folio);
 		XA_STATE(xas, &mapping->i_pages, folio->index);
fs/ext4/page-io.c +1 -1
index 39abfeec5f36..6b12a6b869f8 100644
@@ -580,7 +580,7 @@ int ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *folio,
 		io_folio = page_folio(bounce_page);
 	}
 
-	__folio_start_writeback(folio, keep_towrite);
+	__folio_start_writeback(folio, keep_towrite, folio_nr_pages(folio));
 
 	/* Now submit buffers to write */
 	do {
include/linux/page-flags.h +2 -2
index 8d3fa3a91ce4..d1e0743217b7 100644
@@ -832,11 +832,11 @@ static __always_inline void SetPageUptodate(struct page *page)
 
 CLEARPAGEFLAG(Uptodate, uptodate, PF_NO_TAIL)
 
-void __folio_start_writeback(struct folio *folio, bool keep_write);
+void __folio_start_writeback(struct folio *folio, bool keep_write, long nr_pages);
 void set_page_writeback(struct page *page);
 
 #define folio_start_writeback(folio)			\
-	__folio_start_writeback(folio, false)
+	__folio_start_writeback(folio, false, folio_nr_pages(folio))
 
 static __always_inline bool folio_test_head(const struct folio *folio)
 {
mm/page-writeback.c +5 -5
index de669636120d..d1b2c91f0619 100644
@@ -3042,9 +3042,9 @@ bool __folio_end_writeback(struct folio *folio)
 	return ret;
 }
 
-void __folio_start_writeback(struct folio *folio, bool keep_write)
+void __folio_start_writeback(struct folio *folio, bool keep_write,
+		long nr_pages)
 {
-	long nr = folio_nr_pages(folio);
 	struct address_space *mapping = folio_mapping(folio);
 	int access_ret;
 
@@ -3065,7 +3065,7 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 		on_wblist = mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
 
 		xas_set_mark(&xas, PAGECACHE_TAG_WRITEBACK);
-		wb_stat_mod(wb, WB_WRITEBACK, nr);
+		wb_stat_mod(wb, WB_WRITEBACK, nr_pages);
 		if (!on_wblist) {
 			wb_inode_writeback_start(wb);
 			/*
@@ -3086,8 +3086,8 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
 		folio_test_set_writeback(folio);
 	}
 
-	lruvec_stat_mod_folio(folio, NR_WRITEBACK, nr);
-	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, nr);
+	lruvec_stat_mod_folio(folio, NR_WRITEBACK, nr_pages);
+	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, nr_pages);
 
 	access_ret = arch_make_folio_accessible(folio);
 	/*
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v2 01/12] mm: pass number of pages to __folio_start_writeback()

Quoted 2 lines · Joanne Koong

Add an additional arg to __folio_start_writeback() that takes in the number of pages to write back.

Usually we pass something like page+nr_pages so we know the actual range. I assume here this is not required, because we only care about using the #pages for accounting purposes, right?

Signature · 3 lines
Cheers

David / dhildenb
#3 lore raw

Re: [PATCH v2 01/12] mm: pass number of pages to __folio_start_writeback()

Quoted 7 lines · David Hildenbrand
Quoted 2 lines · Joanne Koong

Add an additional arg to __folio_start_writeback() that takes in the number of pages to write back.

Usually we pass something like page+nr_pages so we know the actual range. I assume here this is not required, because we only care about using the #pages for accounting purposes, right?

I think all the "nr_pages" here are actually the number of dirty pages in the folio, right? Or so I gather since later patches have iomap walking bitmaps to find all the set/clear bits. Perhaps that parameter ought to be called nr_dirty(_pages)?

--D

Quoted 6 lines
Signature · 3 lines
Cheers

David / dhildenb
#4 lore raw

Re: [PATCH v2 01/12] mm: pass number of pages to __folio_start_writeback()

Quoted 5 lines · Darrick J. Wong
Quoted 4 lines · David Hildenbrand
Quoted 2 lines · Joanne Koong

Add an additional arg to __folio_start_writeback() that takes in the number of pages to write back.

Quoted 8 lines
Quoted 3 lines

Usually we pass something like page+nr_pages so we know the actual range. I assume here this is not required, because we only care about using the #pages for accounting purposes, right?

I think all the "nr_pages" here are actually the number of dirty pages in the folio, right? Or so I gather since later patches have iomap walking bitmaps to find all the set/clear bits. Perhaps that parameter ought to be called nr_dirty(_pages)?

That would make perfect sense to me :)

Signature · 3 lines
Cheers

David / dhildenb
#5 lore raw

Re: [PATCH v2 01/12] mm: pass number of pages to __folio_start_writeback()

Quoted 11 lines · David Hildenbrand
Quoted 9 lines · Darrick J. Wong
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · Joanne Koong

Add an additional arg to __folio_start_writeback() that takes in the number of pages to write back.

Usually we pass something like page+nr_pages so we know the actual range. I assume here this is not required, because we only care about using the #pages for accounting purposes, right?

Quoted 6 lines
Quoted 4 lines

I think all the "nr_pages" here are actually the number of dirty pages in the folio, right? Or so I gather since later patches have iomap walking bitmaps to find all the set/clear bits. Perhaps that parameter ought to be called nr_dirty(_pages)?

That would make perfect sense to me :)

I will rename this to nr_dirty_pages to make this more clear / less confusing.

Thanks,
Joanne

Quoted 6 lines

--
Cheers

David / dhildenb

#6 lore raw

PATCH v2 2/12 mm: pass number of pages to __folio_end_writeback()

In reply to Joanne Koong

Add an additional arg to __folio_end_writeback() that takes in the number of pages that were written back.

---
 mm/filemap.c        |  2 +-
 mm/internal.h       |  2 +-
 mm/page-writeback.c | 13 ++++++-------
 3 files changed, 8 insertions(+), 9 deletions(-)
mm/filemap.c +1 -1
index 751838ef05e5..cbfb0f085eb6 100644
@@ -1657,7 +1657,7 @@ void folio_end_writeback(struct folio *folio)
 	 * reused before the folio_wake_bit().
 	 */
 	folio_get(folio);
-	if (__folio_end_writeback(folio))
+	if (__folio_end_writeback(folio, folio_nr_pages(folio)))
 		folio_wake_bit(folio, PG_writeback);
 
 	filemap_end_dropbehind_write(folio);
mm/internal.h +1 -1
index 45b725c3dc03..2eb156823d45 100644
@@ -438,7 +438,7 @@ static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)
 
 vm_fault_t do_swap_page(struct vm_fault *vmf);
 void folio_rotate_reclaimable(struct folio *folio);
-bool __folio_end_writeback(struct folio *folio);
+bool __folio_end_writeback(struct folio *folio, long nr_pages);
 void deactivate_file_folio(struct folio *folio);
 void folio_activate(struct folio *folio);
 
mm/page-writeback.c +6 -7
index d1b2c91f0619..65002552458a 100644
@@ -3006,9 +3006,8 @@ static void wb_inode_writeback_end(struct bdi_writeback *wb)
 	spin_unlock_irqrestore(&wb->work_lock, flags);
 }
 
-bool __folio_end_writeback(struct folio *folio)
+bool __folio_end_writeback(struct folio *folio, long nr_pages)
 {
-	long nr = folio_nr_pages(folio);
 	struct address_space *mapping = folio_mapping(folio);
 	bool ret;
 
@@ -3022,8 +3021,8 @@ bool __folio_end_writeback(struct folio *folio)
 		__xa_clear_mark(&mapping->i_pages, folio_index(folio),
 					PAGECACHE_TAG_WRITEBACK);
 
-		wb_stat_mod(wb, WB_WRITEBACK, -nr);
-		__wb_writeout_add(wb, nr);
+		wb_stat_mod(wb, WB_WRITEBACK, -nr_pages);
+		__wb_writeout_add(wb, nr_pages);
 		if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) {
 			wb_inode_writeback_end(wb);
 			if (mapping->host)
@@ -3035,9 +3034,9 @@ bool __folio_end_writeback(struct folio *folio)
 		ret = folio_xor_flags_has_waiters(folio, 1 << PG_writeback);
 	}
 
-	lruvec_stat_mod_folio(folio, NR_WRITEBACK, -nr);
-	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr);
-	node_stat_mod_folio(folio, NR_WRITTEN, nr);
+	lruvec_stat_mod_folio(folio, NR_WRITEBACK, -nr_pages);
+	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr_pages);
+	node_stat_mod_folio(folio, NR_WRITTEN, nr_pages);
 
 	return ret;
 }
Signature · 1 line
2.47.3
#7 lore raw

PATCH v2 3/12 mm: add folio_end_writeback_pages() helper

In reply to Joanne Koong

Add folio_end_writeback_pages() which takes in the number of pages written back.

---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/pagemap.h +1 -0
index 12a12dae727d..362900730247 100644
@@ -1221,6 +1221,7 @@ void folio_wait_writeback(struct folio *folio);
 int folio_wait_writeback_killable(struct folio *folio);
 void end_page_writeback(struct page *page);
 void folio_end_writeback(struct folio *folio);
+void folio_end_writeback_pages(struct folio *folio, long nr_pages);
 void folio_wait_stable(struct folio *folio);
 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
mm/filemap.c +9 -3
index cbfb0f085eb6..6d50afaff930 100644
@@ -1627,14 +1627,15 @@ static void filemap_end_dropbehind_write(struct folio *folio)
 }
 
 /**
- * folio_end_writeback - End writeback against a folio.
+ * folio_end_writeback_pages - End writeback against a folio.
  * @folio: The folio.
+ * @nr_pages: The number of pages written back.
  *
  * The folio must actually be under writeback.
  *
  * Context: May be called from process or interrupt context.
  */
-void folio_end_writeback(struct folio *folio)
+void folio_end_writeback_pages(struct folio *folio, long nr_pages)
 {
 	VM_BUG_ON_FOLIO(!folio_test_writeback(folio), folio);
 
@@ -1657,13 +1658,18 @@ void folio_end_writeback(struct folio *folio)
 	 * reused before the folio_wake_bit().
 	 */
 	folio_get(folio);
-	if (__folio_end_writeback(folio, folio_nr_pages(folio)))
+	if (__folio_end_writeback(folio, nr_pages))
 		folio_wake_bit(folio, PG_writeback);
 
 	filemap_end_dropbehind_write(folio);
 	acct_reclaim_writeback(folio);
 	folio_put(folio);
 }
+
+void folio_end_writeback(struct folio *folio)
+{
+	folio_end_writeback_pages(folio, folio_nr_pages(folio));
+}
 EXPORT_SYMBOL(folio_end_writeback);
 
 /**
Signature · 1 line
2.47.3
#8 lore raw

PATCH v2 4/12 mm: pass number of pages dirtied to __folio_mark_dirty()

In reply to Joanne Koong

Add an additional arg to __folio_mark_dirty() that takes in the number of pages dirtied, so that this can be passed to folio_account_dirtied() when it updates the stats.

---
 fs/buffer.c             |  6 ++++--
 include/linux/pagemap.h |  3 ++-
 mm/page-writeback.c     | 10 +++++-----
 3 files changed, 11 insertions(+), 8 deletions(-)
fs/buffer.c +4 -2
index 6a8752f7bbed..65c96c432800 100644
@@ -751,7 +751,8 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
 	spin_unlock(&mapping->i_private_lock);
 
 	if (newly_dirty)
-		__folio_mark_dirty(folio, mapping, 1);
+		__folio_mark_dirty(folio, mapping, 1,
+				   folio_nr_pages(folio));
 
 	if (newly_dirty)
 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
@@ -1203,7 +1204,8 @@ void mark_buffer_dirty(struct buffer_head *bh)
 		if (!folio_test_set_dirty(folio)) {
 			mapping = folio->mapping;
 			if (mapping)
-				__folio_mark_dirty(folio, mapping, 0);
+				__folio_mark_dirty(folio, mapping, 0,
+						   folio_nr_pages(folio));
 		}
 		if (mapping)
 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
include/linux/pagemap.h +2 -1
index 362900730247..48745f8f6dfe 100644
@@ -1223,7 +1223,8 @@ void end_page_writeback(struct page *page);
 void folio_end_writeback(struct folio *folio);
 void folio_end_writeback_pages(struct folio *folio, long nr_pages);
 void folio_wait_stable(struct folio *folio);
-void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
+void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn,
+		long nr_pages);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
 void __folio_cancel_dirty(struct folio *folio);
 static inline void folio_cancel_dirty(struct folio *folio)
mm/page-writeback.c +5 -5
index 65002552458a..e66eef2d1584 100644
@@ -2675,7 +2675,7 @@ EXPORT_SYMBOL(noop_dirty_folio);
  * NOTE: This relies on being atomic wrt interrupts.
  */
 static void folio_account_dirtied(struct folio *folio,
-		struct address_space *mapping)
+		struct address_space *mapping, long nr)
 {
 	struct inode *inode = mapping->host;
 
@@ -2683,7 +2683,6 @@ static void folio_account_dirtied(struct folio *folio,
 
 	if (mapping_can_writeback(mapping)) {
 		struct bdi_writeback *wb;
-		long nr = folio_nr_pages(folio);
 
 		inode_attach_wb(inode, folio);
 		wb = inode_to_wb(inode);
@@ -2731,14 +2730,14 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
  * try_to_free_buffers() to fail.
  */
 void __folio_mark_dirty(struct folio *folio, struct address_space *mapping,
-			     int warn)
+			     int warn, long nr_pages)
 {
 	unsigned long flags;
 
 	xa_lock_irqsave(&mapping->i_pages, flags);
 	if (folio->mapping) {	/* Race with truncate? */
 		WARN_ON_ONCE(warn && !folio_test_uptodate(folio));
-		folio_account_dirtied(folio, mapping);
+		folio_account_dirtied(folio, mapping, nr_pages);
 		__xa_set_mark(&mapping->i_pages, folio_index(folio),
 				PAGECACHE_TAG_DIRTY);
 	}
@@ -2769,7 +2768,8 @@ bool filemap_dirty_folio(struct address_space *mapping, struct folio *folio)
 	if (folio_test_set_dirty(folio))
 		return false;
 
-	__folio_mark_dirty(folio, mapping, !folio_test_private(folio));
+	__folio_mark_dirty(folio, mapping, !folio_test_private(folio),
+			folio_nr_pages(folio));
 
 	if (mapping->host) {
 		/* !PageAnon && !swapper_space */
Signature · 1 line
2.47.3
#9 lore raw

PATCH v2 5/12 mm: add filemap_dirty_folio_pages() helper

In reply to Joanne Koong

Add filemap_dirty_folio_pages() which is equivalent to filemap_dirty_folio() except it takes in the number of pages in the folio to account for as dirty when it updates internal dirty stats instead of accounting all pages in the folio as dirty. If the folio is already dirty, calling this function will still update the stats. As such, the caller is responsible for ensuring no overaccounting happens.

The same caller responsibilities apply here as for filemap_dirty_folio() (eg, should ensure this doesn't race with truncation/writeback).

---
 fs/buffer.c               |  4 ++--
 include/linux/pagemap.h   |  2 +-
 include/linux/writeback.h |  2 ++
 mm/page-writeback.c       | 41 +++++++++++++++++++++++++++++++++++----
 4 files changed, 42 insertions(+), 7 deletions(-)
fs/buffer.c +2 -2
index 65c96c432800..558591254fdb 100644
@@ -752,7 +752,7 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio)
 
 	if (newly_dirty)
 		__folio_mark_dirty(folio, mapping, 1,
-				   folio_nr_pages(folio));
+				   folio_nr_pages(folio), true);
 
 	if (newly_dirty)
 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
@@ -1205,7 +1205,7 @@ void mark_buffer_dirty(struct buffer_head *bh)
 			mapping = folio->mapping;
 			if (mapping)
 				__folio_mark_dirty(folio, mapping, 0,
-						   folio_nr_pages(folio));
+						   folio_nr_pages(folio), true);
 		}
 		if (mapping)
 			__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
include/linux/pagemap.h +1 -1
index 48745f8f6dfe..510bc6e0f70b 100644
@@ -1224,7 +1224,7 @@ void folio_end_writeback(struct folio *folio);
 void folio_end_writeback_pages(struct folio *folio, long nr_pages);
 void folio_wait_stable(struct folio *folio);
 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn,
-		long nr_pages);
+		long nr_pages, bool newly_dirty);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
 void __folio_cancel_dirty(struct folio *folio);
 static inline void folio_cancel_dirty(struct folio *folio)
include/linux/writeback.h +2 -0
index a2848d731a46..0df11d00cce2 100644
@@ -372,6 +372,8 @@ void tag_pages_for_writeback(struct address_space *mapping,
 			     pgoff_t start, pgoff_t end);
 
 bool filemap_dirty_folio(struct address_space *mapping, struct folio *folio);
+bool filemap_dirty_folio_pages(struct address_space *mapping,
+			       struct folio *folio, long nr_pages);
 bool folio_redirty_for_writepage(struct writeback_control *, struct folio *);
 bool redirty_page_for_writepage(struct writeback_control *, struct page *);
 
mm/page-writeback.c +37 -4

65 lines not on this page · raw message

Signature · 1 line
2.47.3
#10 lore raw

PATCH v2 6/12 mm: add __folio_clear_dirty_for_io() helper

In reply to Joanne Koong

Add __folio_clear_dirty_for_io() which takes in an arg for whether the folio and wb stats should be updated as part of the call or not.

---
 mm/page-writeback.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
mm/page-writeback.c +14 -5
index 1f862ab3c68d..fe39137f01d6 100644
@@ -2955,7 +2955,7 @@ EXPORT_SYMBOL(__folio_cancel_dirty);
  * This incoherency between the folio's dirty flag and xarray tag is
  * unfortunate, but it only exists while the folio is locked.
  */
-bool folio_clear_dirty_for_io(struct folio *folio)
+static bool __folio_clear_dirty_for_io(struct folio *folio, bool update_stats)
 {
 	struct address_space *mapping = folio_mapping(folio);
 	bool ret = false;
@@ -3004,10 +3004,14 @@ bool folio_clear_dirty_for_io(struct folio *folio)
 		 */
 		wb = unlocked_inode_to_wb_begin(inode, &cookie);
 		if (folio_test_clear_dirty(folio)) {
-			long nr = folio_nr_pages(folio);
-			lruvec_stat_mod_folio(folio, NR_FILE_DIRTY, -nr);
-			zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr);
-			wb_stat_mod(wb, WB_RECLAIMABLE, -nr);
+			if (update_stats) {
+				long nr = folio_nr_pages(folio);
+				lruvec_stat_mod_folio(folio, NR_FILE_DIRTY,
+						      -nr);
+				zone_stat_mod_folio(folio,
+						    NR_ZONE_WRITE_PENDING, -nr);
+				wb_stat_mod(wb, WB_RECLAIMABLE, -nr);
+			}
 			ret = true;
 		}
 		unlocked_inode_to_wb_end(inode, &cookie);
@@ -3015,6 +3019,11 @@ bool folio_clear_dirty_for_io(struct folio *folio)
 	}
 	return folio_test_clear_dirty(folio);
 }
+
+bool folio_clear_dirty_for_io(struct folio *folio)
+{
+	return __folio_clear_dirty_for_io(folio, true);
+}
 EXPORT_SYMBOL(folio_clear_dirty_for_io);
 
 static void wb_inode_writeback_start(struct bdi_writeback *wb)
Signature · 1 line
2.47.3
#11 lore raw

PATCH v2 7/12 mm: add no_stats_accounting bitfield to wbc

In reply to Joanne Koong

Add a no_stats_accounting bitfield to wbc that callers can set. Hook this up to __folio_clear_dirty_for_io() when preparing writeback.

This is so that for filesystems that implement granular dirty writeback for its large folios, the stats reflect only the dirty pages that are written back instead of all the pages in the folio, which helps enforce more accurate / less conservative dirty page balancing.

---
 include/linux/writeback.h | 7 +++++++
 mm/page-writeback.c       | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)
include/linux/writeback.h +7 -0
index 0df11d00cce2..f63a52b56dff 100644
@@ -71,6 +71,13 @@ struct writeback_control {
 	 */
 	unsigned no_cgroup_owner:1;
 
+	/*
+	 * Do not do any stats accounting. The caller will do this themselves.
+	 * This is useful for filesystems that implement granular dirty
+	 * writeback for its large folios.
+	 */
+	unsigned no_stats_accounting:1;
+
 	/* internal fields used by the ->writepages implementation: */
 	struct folio_batch fbatch;
 	pgoff_t index;
mm/page-writeback.c +2 -1
index fe39137f01d6..294339887e55 100644
@@ -2402,6 +2402,7 @@ void tag_pages_for_writeback(struct address_space *mapping,
 }
 EXPORT_SYMBOL(tag_pages_for_writeback);
 
+static bool __folio_clear_dirty_for_io(struct folio *folio, bool update_stats);
 static bool folio_prepare_writeback(struct address_space *mapping,
 		struct writeback_control *wbc, struct folio *folio)
 {
@@ -2428,7 +2429,7 @@ static bool folio_prepare_writeback(struct address_space *mapping,
 	}
 	BUG_ON(folio_test_writeback(folio));
 
-	if (!folio_clear_dirty_for_io(folio))
+	if (!__folio_clear_dirty_for_io(folio, !wbc->no_stats_accounting))
 		return false;
 
 	return true;
Signature · 1 line
2.47.3
#12 lore raw

PATCH v2 8/12 mm: refactor clearing dirty stats into helper function

In reply to Joanne Koong

Move logic for clearing dirty stats into a helper function both folio_account_cleaned() and __folio_clear_dirty_for_io() invoke.

---
 mm/page-writeback.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
mm/page-writeback.c +12 -11
index 294339887e55..e0410cfbe480 100644
@@ -2701,6 +2701,14 @@ static void folio_account_dirtied(struct folio *folio,
 	}
 }
 
+static void __clear_dirty_for_io_stats(struct folio *folio,
+			struct bdi_writeback *wb, long nr_pages)
+{
+	lruvec_stat_mod_folio(folio, NR_FILE_DIRTY, -nr_pages);
+	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr_pages);
+	wb_stat_mod(wb, WB_RECLAIMABLE, -nr_pages);
+}
+
 /*
  * Helper function for deaccounting dirty page without writeback.
  *
@@ -2709,9 +2717,7 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
 {
 	long nr = folio_nr_pages(folio);
 
-	lruvec_stat_mod_folio(folio, NR_FILE_DIRTY, -nr);
-	zone_stat_mod_folio(folio, NR_ZONE_WRITE_PENDING, -nr);
-	wb_stat_mod(wb, WB_RECLAIMABLE, -nr);
+	__clear_dirty_for_io_stats(folio, wb, nr);
 	task_io_account_cancelled_write(nr * PAGE_SIZE);
 }
 
@@ -3005,14 +3011,9 @@ static bool __folio_clear_dirty_for_io(struct folio *folio, bool update_stats)
 		 */
 		wb = unlocked_inode_to_wb_begin(inode, &cookie);
 		if (folio_test_clear_dirty(folio)) {
-			if (update_stats) {
-				long nr = folio_nr_pages(folio);
-				lruvec_stat_mod_folio(folio, NR_FILE_DIRTY,
-						      -nr);
-				zone_stat_mod_folio(folio,
-						    NR_ZONE_WRITE_PENDING, -nr);
-				wb_stat_mod(wb, WB_RECLAIMABLE, -nr);
-			}
+			if (update_stats)
+				__clear_dirty_for_io_stats(folio, wb,
+						folio_nr_pages(folio));
 			ret = true;
 		}
 		unlocked_inode_to_wb_end(inode, &cookie);
Signature · 1 line
2.47.3
#13 lore raw

PATCH v2 9/12 mm: add clear_dirty_for_io_stats() helper

In reply to Joanne Koong

Add clear_dirty_for_io_stats() which clears dirty stats corresponding to a folio.

The main use case for this is for filesystems that implement granular dirty writeback for large folios. This allows them (after setting the wbc no_stats_accounting bitfield) to update dirty writeback stats only for the pages in the folio that are written back instead of for the entire folio, which helps enforce more accurate / less conservative dirty page balancing.

---
 include/linux/writeback.h |  1 +
 mm/page-writeback.c       | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/writeback.h +1 -0
index f63a52b56dff..2ae0bea03d48 100644
@@ -375,6 +375,7 @@ int write_cache_pages(struct address_space *mapping,
 		      void *data);
 int do_writepages(struct address_space *mapping, struct writeback_control *wbc);
 void writeback_set_ratelimit(void);
+void clear_dirty_for_io_stats(struct folio *folio, long nr_pages);
 void tag_pages_for_writeback(struct address_space *mapping,
 			     pgoff_t start, pgoff_t end);
 
mm/page-writeback.c +16 -0
index e0410cfbe480..726da7611cce 100644
@@ -2709,6 +2709,22 @@ static void __clear_dirty_for_io_stats(struct folio *folio,
 	wb_stat_mod(wb, WB_RECLAIMABLE, -nr_pages);
 }
 
+void clear_dirty_for_io_stats(struct folio *folio, long nr_pages)
+{
+	struct address_space *mapping = folio_mapping(folio);
+	struct bdi_writeback *wb;
+	struct wb_lock_cookie cookie = {};
+	struct inode *inode;
+
+	if (!mapping || !mapping_can_writeback(mapping))
+		return;
+
+	inode = mapping->host;
+	wb = unlocked_inode_to_wb_begin(inode, &cookie);
+	__clear_dirty_for_io_stats(folio, wb, nr_pages);
+	unlocked_inode_to_wb_end(inode, &cookie);
+}
+
 /*
  * Helper function for deaccounting dirty page without writeback.
  *
Signature · 1 line
2.47.3
#14 lore raw

PATCH v2 10/12 iomap: refactor dirty bitmap iteration

In reply to Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap iteration. This uses __ffs() internally and is more efficient for finding the next dirty or clean bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 67 ++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 19 deletions(-)
fs/iomap/buffered-io.c +48 -19
index fd827398afd2..dc1a1f371412 100644
@@ -75,13 +75,42 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
 		folio_mark_uptodate(folio);
 }
 
-static inline bool ifs_block_is_dirty(struct folio *folio,
-		struct iomap_folio_state *ifs, int block)
+/**
+ * ifs_next_dirty_block - find the next dirty block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no dirty block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_dirty_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
 {
+	struct iomap_folio_state *ifs = folio->private;
 	struct inode *inode = folio->mapping->host;
-	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
+	unsigned int blks = i_blocks_per_folio(inode, folio);
+
+	return find_next_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
+}
+
+/**
+ * ifs_next_clean_block - find the next clean block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no clean block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_clean_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	struct inode *inode = folio->mapping->host;
+	unsigned int blks = i_blocks_per_folio(inode, folio);
 
-	return test_bit(block + blks_per_folio, ifs->state);
+	return find_next_zero_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
 }
 
 static unsigned ifs_find_dirty_range(struct folio *folio,
@@ -92,18 +121,15 @@ static unsigned ifs_find_dirty_range(struct folio *folio,
 		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;
+		i_blocks_per_folio(inode, folio)) - 1;
+	unsigned nblks;
 
-	while (!ifs_block_is_dirty(folio, ifs, start_blk))
-		if (++start_blk == end_blk)
-			return 0;
+	start_blk = ifs_next_dirty_block(folio, start_blk, end_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++;
-	}
+	nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk)
+		- start_blk;
 
 	*range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
 	return nblks << inode->i_blkbits;
@@ -1077,7 +1103,7 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 		struct folio *folio, loff_t start_byte, loff_t end_byte,
 		struct iomap *iomap, iomap_punch_t punch)
 {
-	unsigned int first_blk, last_blk, i;
+	unsigned int first_blk, last_blk;
 	loff_t last_byte;
 	u8 blkbits = inode->i_blkbits;
 	struct iomap_folio_state *ifs;
@@ -1096,10 +1122,13 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 			folio_pos(folio) + folio_size(folio) - 1);
 	first_blk = offset_in_folio(folio, start_byte) >> blkbits;
 	last_blk = offset_in_folio(folio, last_byte) >> blkbits;
-	for (i = first_blk; i <= last_blk; i++) {
-		if (!ifs_block_is_dirty(folio, ifs, i))
-			punch(inode, folio_pos(folio) + (i << blkbits),
-				    1 << blkbits, iomap);
+	while (first_blk <= last_blk) {
+		first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
+		if (first_blk > last_blk)
+			break;
+		punch(inode, folio_pos(folio) + (first_blk << blkbits),
+			1 << blkbits, iomap);
+		first_blk++;
 	}
 }
 
Signature · 1 line
2.47.3
#15 lore raw

Re: [PATCH v2 10/12] iomap: refactor dirty bitmap iteration

Quoted 75 lines · Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap iteration. This uses __ffs() internally and is more efficient for finding the next dirty or clean bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 67 ++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 19 deletions(-)
fs/iomap/buffered-io.c +36 -9
index fd827398afd2..dc1a1f371412 100644
@@ -75,13 +75,42 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
 		folio_mark_uptodate(folio);
 }
 
-static inline bool ifs_block_is_dirty(struct folio *folio,
-		struct iomap_folio_state *ifs, int block)
+/**
+ * ifs_next_dirty_block - find the next dirty block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no dirty block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_dirty_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
 {
+	struct iomap_folio_state *ifs = folio->private;
 	struct inode *inode = folio->mapping->host;
-	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
+	unsigned int blks = i_blocks_per_folio(inode, folio);
+
+	return find_next_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
+}
+
+/**
+ * ifs_next_clean_block - find the next clean block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no clean block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_clean_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	struct inode *inode = folio->mapping->host;
+	unsigned int blks = i_blocks_per_folio(inode, folio);
 
-	return test_bit(block + blks_per_folio, ifs->state);
+	return find_next_zero_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
 }
 
 static unsigned ifs_find_dirty_range(struct folio *folio,
@@ -92,18 +121,15 @@ static unsigned ifs_find_dirty_range(struct folio *folio,
 		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;
+		i_blocks_per_folio(inode, folio)) - 1;
+	unsigned nblks;
 
-	while (!ifs_block_is_dirty(folio, ifs, start_blk))
-		if (++start_blk == end_blk)
-			return 0;
+	start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);

fs/iomap/buffered-io.c static unsigned ifs_find_dirty_range(struct folio *folio, · line 132

Quoted 10 lines
fs/iomap/buffered-io.c +4 -5
+	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++;
-	}
+	nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk)
+		- start_blk;

Not a critical problem since it looks like the helper bumps end_blk, but something that stands out to me here as mildly annoying is that we check for (start > end) just above, clearly implying that start == end is possible, then go and pass start + 1 and end to the next call. It's not clear to me if that's worth changing to make end exclusive, but may be worth thinking about if you haven't already..

Brian

Quoted 34 lines
fs/iomap/buffered-io.c +8 -5
 	*range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
 	return nblks << inode->i_blkbits;
@@ -1077,7 +1103,7 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 		struct folio *folio, loff_t start_byte, loff_t end_byte,
 		struct iomap *iomap, iomap_punch_t punch)
 {
-	unsigned int first_blk, last_blk, i;
+	unsigned int first_blk, last_blk;
 	loff_t last_byte;
 	u8 blkbits = inode->i_blkbits;
 	struct iomap_folio_state *ifs;
@@ -1096,10 +1122,13 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 			folio_pos(folio) + folio_size(folio) - 1);
 	first_blk = offset_in_folio(folio, start_byte) >> blkbits;
 	last_blk = offset_in_folio(folio, last_byte) >> blkbits;
-	for (i = first_blk; i <= last_blk; i++) {
-		if (!ifs_block_is_dirty(folio, ifs, i))
-			punch(inode, folio_pos(folio) + (i << blkbits),
-				    1 << blkbits, iomap);
+	while (first_blk <= last_blk) {
+		first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
+		if (first_blk > last_blk)
+			break;
+		punch(inode, folio_pos(folio) + (first_blk << blkbits),
+			1 << blkbits, iomap);
+		first_blk++;
 	}
 }
 
Signature · 1 line
2.47.3
#16 lore raw

Re: [PATCH v2 10/12] iomap: refactor dirty bitmap iteration

Quoted 79 lines · Brian Foster
Quoted 78 lines · Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap iteration. This uses __ffs() internally and is more efficient for finding the next dirty or clean bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 67 ++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 19 deletions(-)
fs/iomap/buffered-io.c +38 -9
index fd827398afd2..dc1a1f371412 100644
@@ -75,13 +75,42 @@ static void iomap_set_range_uptodate(struct folio *folio, size_t off,
 		folio_mark_uptodate(folio);
 }
 
-static inline bool ifs_block_is_dirty(struct folio *folio,
-		struct iomap_folio_state *ifs, int block)
+/**
+ * ifs_next_dirty_block - find the next dirty block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no dirty block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_dirty_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
 {
+	struct iomap_folio_state *ifs = folio->private;
 	struct inode *inode = folio->mapping->host;
-	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
+	unsigned int blks = i_blocks_per_folio(inode, folio);
+
+	return find_next_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
+}
+
+/**
+ * ifs_next_clean_block - find the next clean block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no clean block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_clean_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	struct inode *inode = folio->mapping->host;
+	unsigned int blks = i_blocks_per_folio(inode, folio);
 
-	return test_bit(block + blks_per_folio, ifs->state);
+	return find_next_zero_bit(ifs->state, blks + end_blk + 1,
+		blks + start_blk) - blks;
 }
 
 static unsigned ifs_find_dirty_range(struct folio *folio,
@@ -92,18 +121,15 @@ static unsigned ifs_find_dirty_range(struct folio *folio,
 		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;
+		i_blocks_per_folio(inode, folio)) - 1;
+	unsigned nblks;
 
-	while (!ifs_block_is_dirty(folio, ifs, start_blk))
-		if (++start_blk == end_blk)
-			return 0;
+	start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+	if (start_blk > end_blk)
+		return 0;
 
Quoted 14 lines

fs/iomap/buffered-io.c static unsigned ifs_find_dirty_range(struct folio *folio, · line 132

Quoted 7 lines
fs/iomap/buffered-io.c +2 -5
-	while (start_blk + nblks < end_blk) {
-		if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
-			break;
-		nblks++;
-	}
+	nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk)
+		- start_blk;

Not a critical problem since it looks like the helper bumps end_blk, but something that stands out to me here as mildly annoying is that we check for (start > end) just above, clearly implying that start == end is possible, then go and pass start + 1 and end to the next call. It's not clear to me if that's worth changing to make end exclusive, but may be worth thinking about if you haven't already..

<nod> I was also wondering if there were overflow possibilities here.

Quoted 18 lines

Brian

Quoted 16 lines
fs/iomap/buffered-io.c +1 -1
 	*range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
 	return nblks << inode->i_blkbits;
@@ -1077,7 +1103,7 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 		struct folio *folio, loff_t start_byte, loff_t end_byte,
 		struct iomap *iomap, iomap_punch_t punch)
 {
-	unsigned int first_blk, last_blk, i;
+	unsigned int first_blk, last_blk;
 	loff_t last_byte;
 	u8 blkbits = inode->i_blkbits;
 	struct iomap_folio_state *ifs;
@@ -1096,10 +1122,13 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
 			folio_pos(folio) + folio_size(folio) - 1);
 	first_blk = offset_in_folio(folio, start_byte) >> blkbits;
 	last_blk = offset_in_folio(folio, last_byte) >> blkbits;
Quoted 8 lines

fs/iomap/buffered-io.c static void iomap_write_delalloc_ifs_punch(struct inode *inode, · line 1128

Quoted 8 lines
fs/iomap/buffered-io.c +4 -4
-	for (i = first_blk; i <= last_blk; i++) {
-		if (!ifs_block_is_dirty(folio, ifs, i))
-			punch(inode, folio_pos(folio) + (i << blkbits),
-				    1 << blkbits, iomap);
+	while (first_blk <= last_blk) {
+		first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
+		if (first_blk > last_blk)
+			break;

I was wondering if the loop control logic would be cleaner done as a for loop and came up with this monstrosity:

	for (first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
	     first_blk <= last_blk;
	     first_blk = ifs_next_clean_block(folio, first_blk + 1, last_blk)) {
		punch(inode, folio_pos(folio) + (first_blk << blkbits),
		      1 << blkbits, iomap);
	}

Yeah.... better living through macros?

#define for_each_clean_block(folio, blk, last_blk) \
	for ((blk) = ifs_next_clean_block((folio), (blk), (last_blk));
	     (blk) <= (last_blk);
	     (blk) = ifs_next_clean_block((folio), (blk) + 1, (last_blk)))

Somewhat cleaner:

for_each_clean_block(folio, first_blk, last_blk)
punch(inode, folio_pos(folio) + (first_blk << blkbits),
1 << blkbits, iomap);

<shrug>

--D

Quoted 12 lines
Quoted 10 lines
fs/iomap/buffered-io.c +3 -0
+		punch(inode, folio_pos(folio) + (first_blk << blkbits),
+			1 << blkbits, iomap);
+		first_blk++;
 	}
 }
 
Signature · 1 line
2.47.3
#17 lore raw

Re: [PATCH v2 10/12] iomap: refactor dirty bitmap iteration

Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Brian Foster
Quoted 11 lines · Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap iteration. This uses __ffs() internally and is more efficient for finding the next dirty or clean bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 67 ++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 19 deletions(-)
Quoted 4 lines
Quoted 4 lines

fs/iomap/buffered-io.c

Quoted 4 lines
fs/iomap/buffered-io.c +0 -0
index fd827398afd2..dc1a1f371412 100644

Sorry for the late reply on this, I didn't realize you and Brian had commented on this.

I'm going to pull this and the next patch (the uptodate bitmap refactoring one) out of this series and put them instead on top of this other patchset that does some other optimizations.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

static unsigned ifs_find_dirty_range(struct folio *folio,

fs/iomap/buffered-io.c +5 -5
@@ -92,18 +121,15 @@ static unsigned ifs_find_dirty_range(struct folio *folio,
            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;
+           i_blocks_per_folio(inode, folio)) - 1;
+   unsigned nblks;
 
-   while (!ifs_block_is_dirty(folio, ifs, start_blk))
-           if (++start_blk == end_blk)
-                   return 0;
+   start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+   if (start_blk > end_blk)
+           return 0;
 
Quoted 14 lines
Quoted 8 lines

fs/iomap/buffered-io.c static unsigned ifs_find_dirty_range(struct folio *folio, · line 132

Quoted 7 lines
fs/iomap/buffered-io.c +2 -5
-   while (start_blk + nblks < end_blk) {
-           if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
-                   break;
-           nblks++;
-   }
+   nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk)
+           - start_blk;
Quoted 6 lines

Not a critical problem since it looks like the helper bumps end_blk, but something that stands out to me here as mildly annoying is that we check for (start > end) just above, clearly implying that start == end is possible, then go and pass start + 1 and end to the next call. It's not clear to me if that's worth changing to make end exclusive, but may be worth thinking about if you haven't already..

My thinking with having 'end' be inclusive is that it imo makes the interface a lot more intuitive.

For example, for

    next = ifs_next_clean_block(folio, start, end);

with end being inclusive, then nothing in that range is clean if next > end Whereas with end being exclusive, that's only true if next >= end, which imo is more confusing.

If you and Darrick still much prefer having end be exclusive though, then I'm happy to make that change.

Quoted 2 lines

<nod> I was also wondering if there were overflow possibilities here.

I'm not sure what you had in mind for what would overflow, the end_blk being beyond the end of the bitmap? The find_next_bit()/find_next_zero_bit() interfaces protect against that. Or maybe you're referring to something else?

Quoted 45 lines
Quoted 19 lines

Brian

Quoted 16 lines
fs/iomap/buffered-io.c +1 -1
    *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
    return nblks << inode->i_blkbits;
@@ -1077,7 +1103,7 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
            struct folio *folio, loff_t start_byte, loff_t end_byte,
            struct iomap *iomap, iomap_punch_t punch)
 {
-   unsigned int first_blk, last_blk, i;
+   unsigned int first_blk, last_blk;
    loff_t last_byte;
    u8 blkbits = inode->i_blkbits;
    struct iomap_folio_state *ifs;
@@ -1096,10 +1122,13 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
                    folio_pos(folio) + folio_size(folio) - 1);
    first_blk = offset_in_folio(folio, start_byte) >> blkbits;
    last_blk = offset_in_folio(folio, last_byte) >> blkbits;
Quoted 8 lines

fs/iomap/buffered-io.c static void iomap_write_delalloc_ifs_punch(struct inode *inode, · line 1128

Quoted 8 lines
fs/iomap/buffered-io.c +4 -4
-   for (i = first_blk; i <= last_blk; i++) {
-           if (!ifs_block_is_dirty(folio, ifs, i))
-                   punch(inode, folio_pos(folio) + (i << blkbits),
-                               1 << blkbits, iomap);
+   while (first_blk <= last_blk) {
+           first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
+           if (first_blk > last_blk)
+                   break;

I was wondering if the loop control logic would be cleaner done as a for loop and came up with this monstrosity:

        for (first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
             first_blk <= last_blk;
             first_blk = ifs_next_clean_block(folio, first_blk + 1, last_blk)) {
                punch(inode, folio_pos(folio) + (first_blk << blkbits),
                      1 << blkbits, iomap);
        }

Yeah.... better living through macros?

#define for_each_clean_block(folio, blk, last_blk) \
        for ((blk) = ifs_next_clean_block((folio), (blk), (last_blk));
             (blk) <= (last_blk);
             (blk) = ifs_next_clean_block((folio), (blk) + 1, (last_blk)))
Quoted 6 lines

Somewhat cleaner:

for_each_clean_block(folio, first_blk, last_blk)
punch(inode, folio_pos(folio) + (first_blk << blkbits),
1 << blkbits, iomap);

Cool, this macro looks nice! I'll use this in the next version.

Thanks,
Joanne

Quoted 4 lines

<shrug>

--D

#18 lore raw

Re: [PATCH v2 10/12] iomap: refactor dirty bitmap iteration

Quoted 64 lines · Joanne Koong
Quoted 14 lines · Darrick J. Wong
Quoted 12 lines · Brian Foster
Quoted 11 lines · Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap iteration. This uses __ffs() internally and is more efficient for finding the next dirty or clean bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 67 ++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 19 deletions(-)
Quoted 4 lines
Quoted 4 lines

fs/iomap/buffered-io.c

Quoted 4 lines
fs/iomap/buffered-io.c +0 -0
index fd827398afd2..dc1a1f371412 100644

Sorry for the late reply on this, I didn't realize you and Brian had commented on this.

I'm going to pull this and the next patch (the uptodate bitmap refactoring one) out of this series and put them instead on top of this other patchset that does some other optimizations.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

static unsigned ifs_find_dirty_range(struct folio *folio,

fs/iomap/buffered-io.c +5 -5
@@ -92,18 +121,15 @@ static unsigned ifs_find_dirty_range(struct folio *folio,
            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;
+           i_blocks_per_folio(inode, folio)) - 1;
+   unsigned nblks;
 
-   while (!ifs_block_is_dirty(folio, ifs, start_blk))
-           if (++start_blk == end_blk)
-                   return 0;
+   start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+   if (start_blk > end_blk)
+           return 0;
 
Quoted 14 lines
Quoted 8 lines

fs/iomap/buffered-io.c static unsigned ifs_find_dirty_range(struct folio *folio, · line 132

Quoted 7 lines
fs/iomap/buffered-io.c +2 -5
-   while (start_blk + nblks < end_blk) {
-           if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
-                   break;
-           nblks++;
-   }
+   nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk)
+           - start_blk;
Quoted 6 lines

Not a critical problem since it looks like the helper bumps end_blk, but something that stands out to me here as mildly annoying is that we check for (start > end) just above, clearly implying that start == end is possible, then go and pass start + 1 and end to the next call. It's not clear to me if that's worth changing to make end exclusive, but may be worth thinking about if you haven't already..

My thinking with having 'end' be inclusive is that it imo makes the interface a lot more intuitive.

Quoted 6 lines

For example, for

    next = ifs_next_clean_block(folio, start, end);

with end being inclusive, then nothing in that range is clean if next > end Whereas with end being exclusive, that's only true if next >= end, which imo is more confusing.

Ah I guess for the exclusive case you could just write it as

     next = ifs_next_clean_block(folio, start, end + 1);

which would let you make the check "if (next > end)".

I'll play around with both versions and see which one looks better. You and Darrick may be right that we should just make it exclusive.

Thanks,
Joanne

Quoted 73 lines

If you and Darrick still much prefer having end be exclusive though, then I'm happy to make that change.

Quoted 2 lines

<nod> I was also wondering if there were overflow possibilities here.

I'm not sure what you had in mind for what would overflow, the end_blk being beyond the end of the bitmap? The find_next_bit()/find_next_zero_bit() interfaces protect against that. Or maybe you're referring to something else?

Quoted 45 lines
Quoted 19 lines

Brian

Quoted 16 lines
fs/iomap/buffered-io.c +1 -1
    *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
    return nblks << inode->i_blkbits;
@@ -1077,7 +1103,7 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
            struct folio *folio, loff_t start_byte, loff_t end_byte,
            struct iomap *iomap, iomap_punch_t punch)
 {
-   unsigned int first_blk, last_blk, i;
+   unsigned int first_blk, last_blk;
    loff_t last_byte;
    u8 blkbits = inode->i_blkbits;
    struct iomap_folio_state *ifs;
@@ -1096,10 +1122,13 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
                    folio_pos(folio) + folio_size(folio) - 1);
    first_blk = offset_in_folio(folio, start_byte) >> blkbits;
    last_blk = offset_in_folio(folio, last_byte) >> blkbits;
Quoted 8 lines

fs/iomap/buffered-io.c static void iomap_write_delalloc_ifs_punch(struct inode *inode, · line 1128

Quoted 8 lines
fs/iomap/buffered-io.c +4 -4
-   for (i = first_blk; i <= last_blk; i++) {
-           if (!ifs_block_is_dirty(folio, ifs, i))
-                   punch(inode, folio_pos(folio) + (i << blkbits),
-                               1 << blkbits, iomap);
+   while (first_blk <= last_blk) {
+           first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
+           if (first_blk > last_blk)
+                   break;

I was wondering if the loop control logic would be cleaner done as a for loop and came up with this monstrosity:

        for (first_blk = ifs_next_clean_block(folio, first_blk, last_blk);
             first_blk <= last_blk;
             first_blk = ifs_next_clean_block(folio, first_blk + 1, last_blk)) {
                punch(inode, folio_pos(folio) + (first_blk << blkbits),
                      1 << blkbits, iomap);
        }

Yeah.... better living through macros?

#define for_each_clean_block(folio, blk, last_blk) \
        for ((blk) = ifs_next_clean_block((folio), (blk), (last_blk));
             (blk) <= (last_blk);
             (blk) = ifs_next_clean_block((folio), (blk) + 1, (last_blk)))
Quoted 6 lines

Somewhat cleaner:

for_each_clean_block(folio, first_blk, last_blk)
punch(inode, folio_pos(folio) + (first_blk << blkbits),
1 << blkbits, iomap);

Cool, this macro looks nice! I'll use this in the next version.

Thanks,
Joanne

Quoted 4 lines

<shrug>

--D

#19 lore raw

PATCH v2 11/12 iomap: refactor uptodate bitmap iteration

In reply to Joanne Koong

Use find_next_bit()/find_next_zero_bit() for iomap uptodate bitmap iteration. This uses __ffs() internally and is more efficient for finding the next uptodate or non-uptodate bit than manually iterating through the bitmap range testing every bit.

---
 fs/iomap/buffered-io.c | 74 +++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 26 deletions(-)
fs/iomap/buffered-io.c +48 -26
index dc1a1f371412..4f021dcaaffe 100644
@@ -37,10 +37,36 @@ static inline bool ifs_is_fully_uptodate(struct folio *folio,
 	return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
 }
 
-static inline bool ifs_block_is_uptodate(struct iomap_folio_state *ifs,
-		unsigned int block)
+/**
+ * ifs_next_uptodate_block - find the next uptodate block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no uptodate block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_uptodate_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
 {
-	return test_bit(block, ifs->state);
+	struct iomap_folio_state *ifs = folio->private;
+
+	return find_next_bit(ifs->state, end_blk + 1, start_blk);
+}
+
+/**
+ * ifs_next_nonuptodate_block - find the next non-uptodate block in the folio
+ * @folio: The folio
+ * @start_blk: Block number to begin searching at
+ * @end_blk: Last block number (inclusive) to search
+ *
+ * If no non-uptodate block is found, this will return end_blk + 1.
+ */
+static unsigned ifs_next_nonuptodate_block(struct folio *folio,
+		unsigned start_blk, unsigned end_blk)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	return find_next_zero_bit(ifs->state, end_blk + 1, start_blk);
 }
 
 static bool ifs_set_range_uptodate(struct folio *folio,
@@ -266,24 +292,23 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
 	 * to avoid reading in already uptodate ranges.
 	 */
 	if (ifs) {
-		unsigned int i;
-
-		/* move forward for each leading block marked uptodate */
-		for (i = first; i <= last; i++) {
-			if (!ifs_block_is_uptodate(ifs, i))
-				break;
-			*pos += block_size;
-			poff += block_size;
-			plen -= block_size;
-			first++;
-		}
-
-		/* truncate len if we find any trailing uptodate block(s) */
-		while (++i <= last) {
-			if (ifs_block_is_uptodate(ifs, i)) {
-				plen -= (last - i + 1) * block_size;
-				last = i - 1;
-				break;
+		unsigned next, bytes;
+
+		/* find the next non-uptodate block */
+		next = ifs_next_nonuptodate_block(folio, first, last);
+		bytes = (next - first) << block_bits;
+		*pos += bytes;
+		poff += bytes;
+		WARN_ON_ONCE(bytes > plen);
+		plen -= bytes;
+		first = next;
+
+		if (++next <= last) {
+			/* truncate len if we find any trailing uptodate block(s) */
+			next = ifs_next_uptodate_block(folio, next, last);
+			if (next <= last) {
+				plen -= (last - next + 1) << block_bits;
+				last = next - 1;
 			}
 		}
 	}
@@ -607,7 +632,7 @@ bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
 {
 	struct iomap_folio_state *ifs = folio->private;
 	struct inode *inode = folio->mapping->host;
-	unsigned first, last, i;
+	unsigned first, last;
 
 	if (!ifs)
 		return false;
@@ -619,10 +644,7 @@ bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
 	first = from >> inode->i_blkbits;
 	last = (from + count - 1) >> inode->i_blkbits;
 
-	for (i = first; i <= last; i++)
-		if (!ifs_block_is_uptodate(ifs, i))
-			return false;
-	return true;
+	return ifs_next_nonuptodate_block(folio, first, last) > last;
 }
 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
 
Signature · 1 line
2.47.3
#20 lore raw

PATCH v2 12/12 iomap: add granular dirty and writeback accounting

In reply to Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +132 -8
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
 	spinlock_t		state_lock;
 	unsigned int		read_bytes_pending;
 	atomic_t		write_bytes_pending;
+	/* number of pages being currently written back */
+	unsigned		nr_pages_writeback;
 
 	/*
 	 * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
 		blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned blks, nblks = 0;
+
+	start_blk = 0;
+	blks = i_blocks_per_folio(inode, folio);
+	end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
 		struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
 		ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+		unsigned len)
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned nblks = 0;
+
+	start_blk = pos >> inode->i_blkbits;
+	end_blk = (pos + len - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		/* count how many clean blocks there are */
+		start_blk = ifs_next_clean_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);
+}
+
+static bool iomap_granular_dirty_pages(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!ifs)
+		return false;
+
+	return i_blocksize(folio->mapping->host) >= PAGE_SIZE;
+}
+
+static bool iomap_dirty_folio_range(struct address_space *mapping,
+			struct folio *folio, loff_t pos, unsigned len)
+{
+	long nr_new_dirty_pages;
+
+	if (!iomap_granular_dirty_pages(folio)) {
+		iomap_set_range_dirty(folio, pos, len);
+		return filemap_dirty_folio(mapping, folio);
+	}
+
+	nr_new_dirty_pages = iomap_get_range_newly_dirtied(folio, pos, len);
+	if (!nr_new_dirty_pages)
+		return false;
+
+	iomap_set_range_dirty(folio, pos, len);
+	return filemap_dirty_folio_pages(mapping, folio, nr_new_dirty_pages);
+}
+
 static struct iomap_folio_state *ifs_alloc(struct inode *inode,
 		struct folio *folio, unsigned int flags)
 {
@@ -712,8 +789,7 @@ bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
 	size_t len = folio_size(folio);
 
 	ifs_alloc(inode, folio, 0);
-	iomap_set_range_dirty(folio, 0, len);
-	return filemap_dirty_folio(mapping, folio);
+	return iomap_dirty_folio_range(mapping, folio, 0, len);
 }
 EXPORT_SYMBOL_GPL(iomap_dirty_folio);
 
@@ -937,8 +1013,8 @@ static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
 		return false;
 	iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
-	iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
-	filemap_dirty_folio(inode->i_mapping, folio);
+	iomap_dirty_folio_range(inode->i_mapping, folio,
+			offset_in_folio(folio, pos), copied);
 	return true;
 }
 
@@ -1613,6 +1689,29 @@ void iomap_start_folio_write(struct inode *inode, struct folio *folio,
 }
 EXPORT_SYMBOL_GPL(iomap_start_folio_write);
 
+static void iomap_folio_start_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_start_writeback(folio);
+
+	__folio_start_writeback(folio, false, ifs->nr_pages_writeback);
+}
+
+static void iomap_folio_end_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_pages_writeback;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_end_writeback(folio);
+
+	nr_pages_writeback = ifs->nr_pages_writeback;
+	ifs->nr_pages_writeback = 0;
+	folio_end_writeback_pages(folio, nr_pages_writeback);
+}
+
 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 		size_t len)
 {
@@ -1622,7 +1721,7 @@ void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
 
 	if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
-		folio_end_writeback(folio);
+		iomap_folio_end_writeback(folio);
 }
 EXPORT_SYMBOL_GPL(iomap_finish_folio_write);
 
@@ -1710,6 +1809,21 @@ static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode,
 	return true;
 }
 
+static void iomap_update_dirty_stats(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_dirty_pages;
+
+	if (iomap_granular_dirty_pages(folio)) {
+		nr_dirty_pages = ifs_count_dirty_pages(folio);
+		ifs->nr_pages_writeback = nr_dirty_pages;
+	} else {
+		nr_dirty_pages = folio_nr_pages(folio);
+	}
+
+	clear_dirty_for_io_stats(folio, nr_dirty_pages);
+}
+
 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 {
 	struct iomap_folio_state *ifs = folio->private;
@@ -1727,6 +1841,8 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 
 	trace_iomap_writeback_folio(inode, pos, folio_size(folio));
 
+	iomap_update_dirty_stats(folio);
+
 	if (!iomap_writeback_handle_eof(folio, inode, &end_pos))
 		return 0;
 	WARN_ON_ONCE(end_pos <= pos);
@@ -1734,6 +1850,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	if (i_blocks_per_folio(inode, folio) > 1) {
 		if (!ifs) {
 			ifs = ifs_alloc(inode, folio, 0);
+			ifs->nr_pages_writeback = folio_nr_pages(folio);
 			iomap_set_range_dirty(folio, 0, end_pos - pos);
 		}
 
@@ -1751,7 +1868,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 * 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);
+	iomap_folio_start_writeback(folio);
 
 	/*
 	 * Walk through the folio to find dirty areas to write back.
@@ -1784,10 +1901,10 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 */
 	if (ifs) {
 		if (atomic_dec_and_test(&ifs->write_bytes_pending))
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	} else {
 		if (!wb_pending)
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	}
 	mapping_set_error(inode->i_mapping, error);
 	return error;
@@ -1809,6 +1926,13 @@ iomap_writepages(struct iomap_writepage_ctx *wpc)
 			PF_MEMALLOC))
 		return -EIO;
 
+	/*
+	 * iomap opts out of the default wbc stats accounting because it does
+	 * its own granular dirty/writeback accounting (see
+	 * iomap_update_dirty_stats()).
+	 */
+	wpc->wbc->no_stats_accounting = true;
+
 	while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) {
 		error = iomap_writeback_folio(wpc, folio);
 		folio_unlock(folio);
Signature · 1 line
2.47.3
#21 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 12 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

Quoted 3 lines

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

	(folio_size > PAGE_SIZE && folio_size > i_blocksize)

Hrm?

Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
 	spinlock_t		state_lock;
 	unsigned int		read_bytes_pending;
 	atomic_t		write_bytes_pending;
+	/* number of pages being currently written back */
+	unsigned		nr_pages_writeback;
 
 	/*
 	 * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
 		blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+	start_blk = 0;
+	blks = i_blocks_per_folio(inode, folio);
+	end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
 		struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
 		ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+		unsigned len)

iomap_count_clean_pages() ?

--D

Quoted 192 lines
fs/iomap/buffered-io.c +105 -8
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned nblks = 0;
+
+	start_blk = pos >> inode->i_blkbits;
+	end_blk = (pos + len - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		/* count how many clean blocks there are */
+		start_blk = ifs_next_clean_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);
+}
+
+static bool iomap_granular_dirty_pages(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!ifs)
+		return false;
+
+	return i_blocksize(folio->mapping->host) >= PAGE_SIZE;
+}
+
+static bool iomap_dirty_folio_range(struct address_space *mapping,
+			struct folio *folio, loff_t pos, unsigned len)
+{
+	long nr_new_dirty_pages;
+
+	if (!iomap_granular_dirty_pages(folio)) {
+		iomap_set_range_dirty(folio, pos, len);
+		return filemap_dirty_folio(mapping, folio);
+	}
+
+	nr_new_dirty_pages = iomap_get_range_newly_dirtied(folio, pos, len);
+	if (!nr_new_dirty_pages)
+		return false;
+
+	iomap_set_range_dirty(folio, pos, len);
+	return filemap_dirty_folio_pages(mapping, folio, nr_new_dirty_pages);
+}
+
 static struct iomap_folio_state *ifs_alloc(struct inode *inode,
 		struct folio *folio, unsigned int flags)
 {
@@ -712,8 +789,7 @@ bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
 	size_t len = folio_size(folio);
 
 	ifs_alloc(inode, folio, 0);
-	iomap_set_range_dirty(folio, 0, len);
-	return filemap_dirty_folio(mapping, folio);
+	return iomap_dirty_folio_range(mapping, folio, 0, len);
 }
 EXPORT_SYMBOL_GPL(iomap_dirty_folio);
 
@@ -937,8 +1013,8 @@ static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
 		return false;
 	iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
-	iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
-	filemap_dirty_folio(inode->i_mapping, folio);
+	iomap_dirty_folio_range(inode->i_mapping, folio,
+			offset_in_folio(folio, pos), copied);
 	return true;
 }
 
@@ -1613,6 +1689,29 @@ void iomap_start_folio_write(struct inode *inode, struct folio *folio,
 }
 EXPORT_SYMBOL_GPL(iomap_start_folio_write);
 
+static void iomap_folio_start_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_start_writeback(folio);
+
+	__folio_start_writeback(folio, false, ifs->nr_pages_writeback);
+}
+
+static void iomap_folio_end_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_pages_writeback;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_end_writeback(folio);
+
+	nr_pages_writeback = ifs->nr_pages_writeback;
+	ifs->nr_pages_writeback = 0;
+	folio_end_writeback_pages(folio, nr_pages_writeback);
+}
+
 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 		size_t len)
 {
@@ -1622,7 +1721,7 @@ void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
 
 	if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
-		folio_end_writeback(folio);
+		iomap_folio_end_writeback(folio);
 }
 EXPORT_SYMBOL_GPL(iomap_finish_folio_write);
 
@@ -1710,6 +1809,21 @@ static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode,
 	return true;
 }
 
+static void iomap_update_dirty_stats(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_dirty_pages;
+
+	if (iomap_granular_dirty_pages(folio)) {
+		nr_dirty_pages = ifs_count_dirty_pages(folio);
+		ifs->nr_pages_writeback = nr_dirty_pages;
+	} else {
+		nr_dirty_pages = folio_nr_pages(folio);
+	}
+
+	clear_dirty_for_io_stats(folio, nr_dirty_pages);
+}
+
 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 {
 	struct iomap_folio_state *ifs = folio->private;
@@ -1727,6 +1841,8 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 
 	trace_iomap_writeback_folio(inode, pos, folio_size(folio));
 
+	iomap_update_dirty_stats(folio);
+
 	if (!iomap_writeback_handle_eof(folio, inode, &end_pos))
 		return 0;
 	WARN_ON_ONCE(end_pos <= pos);
@@ -1734,6 +1850,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	if (i_blocks_per_folio(inode, folio) > 1) {
 		if (!ifs) {
 			ifs = ifs_alloc(inode, folio, 0);
+			ifs->nr_pages_writeback = folio_nr_pages(folio);
 			iomap_set_range_dirty(folio, 0, end_pos - pos);
 		}
 
@@ -1751,7 +1868,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 * 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);
+	iomap_folio_start_writeback(folio);
 
 	/*
 	 * Walk through the folio to find dirty areas to write back.
@@ -1784,10 +1901,10 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 */
 	if (ifs) {
 		if (atomic_dec_and_test(&ifs->write_bytes_pending))
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	} else {
 		if (!wb_pending)
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	}
 	mapping_set_error(inode->i_mapping, error);
 	return error;
@@ -1809,6 +1926,13 @@ iomap_writepages(struct iomap_writepage_ctx *wpc)
 			PF_MEMALLOC))
 		return -EIO;
 
+	/*
+	 * iomap opts out of the default wbc stats accounting because it does
+	 * its own granular dirty/writeback accounting (see
+	 * iomap_update_dirty_stats()).
+	 */
+	wpc->wbc->no_stats_accounting = true;
+
 	while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) {
 		error = iomap_writeback_folio(wpc, folio);
 		folio_unlock(folio);
Signature · 1 line
2.47.3
#22 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 26 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

Quoted 3 lines

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Quoted 7 lines

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

	(folio_size > PAGE_SIZE && folio_size > i_blocksize)

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Brian

Quoted 262 lines
Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
 	spinlock_t		state_lock;
 	unsigned int		read_bytes_pending;
 	atomic_t		write_bytes_pending;
+	/* number of pages being currently written back */
+	unsigned		nr_pages_writeback;
 
 	/*
 	 * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
 		blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+	start_blk = 0;
+	blks = i_blocks_per_folio(inode, folio);
+	end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
 		struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
 		ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+		unsigned len)

iomap_count_clean_pages() ?

--D

Quoted 192 lines
fs/iomap/buffered-io.c +105 -8
+{
+	struct inode *inode = folio->mapping->host;
+	unsigned block_size = i_blocksize(inode);
+	unsigned start_blk, end_blk;
+	unsigned nblks = 0;
+
+	start_blk = pos >> inode->i_blkbits;
+	end_blk = (pos + len - 1) >> inode->i_blkbits;
+	end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+	while (start_blk <= end_blk) {
+		/* count how many clean blocks there are */
+		start_blk = ifs_next_clean_block(folio, start_blk, end_blk);
+		if (start_blk > end_blk)
+			break;
+		nblks++;
+		start_blk++;
+	}
+
+	return nblks * (block_size >> PAGE_SHIFT);
+}
+
+static bool iomap_granular_dirty_pages(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!ifs)
+		return false;
+
+	return i_blocksize(folio->mapping->host) >= PAGE_SIZE;
+}
+
+static bool iomap_dirty_folio_range(struct address_space *mapping,
+			struct folio *folio, loff_t pos, unsigned len)
+{
+	long nr_new_dirty_pages;
+
+	if (!iomap_granular_dirty_pages(folio)) {
+		iomap_set_range_dirty(folio, pos, len);
+		return filemap_dirty_folio(mapping, folio);
+	}
+
+	nr_new_dirty_pages = iomap_get_range_newly_dirtied(folio, pos, len);
+	if (!nr_new_dirty_pages)
+		return false;
+
+	iomap_set_range_dirty(folio, pos, len);
+	return filemap_dirty_folio_pages(mapping, folio, nr_new_dirty_pages);
+}
+
 static struct iomap_folio_state *ifs_alloc(struct inode *inode,
 		struct folio *folio, unsigned int flags)
 {
@@ -712,8 +789,7 @@ bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
 	size_t len = folio_size(folio);
 
 	ifs_alloc(inode, folio, 0);
-	iomap_set_range_dirty(folio, 0, len);
-	return filemap_dirty_folio(mapping, folio);
+	return iomap_dirty_folio_range(mapping, folio, 0, len);
 }
 EXPORT_SYMBOL_GPL(iomap_dirty_folio);
 
@@ -937,8 +1013,8 @@ static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
 		return false;
 	iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
-	iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
-	filemap_dirty_folio(inode->i_mapping, folio);
+	iomap_dirty_folio_range(inode->i_mapping, folio,
+			offset_in_folio(folio, pos), copied);
 	return true;
 }
 
@@ -1613,6 +1689,29 @@ void iomap_start_folio_write(struct inode *inode, struct folio *folio,
 }
 EXPORT_SYMBOL_GPL(iomap_start_folio_write);
 
+static void iomap_folio_start_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_start_writeback(folio);
+
+	__folio_start_writeback(folio, false, ifs->nr_pages_writeback);
+}
+
+static void iomap_folio_end_writeback(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_pages_writeback;
+
+	if (!iomap_granular_dirty_pages(folio))
+		return folio_end_writeback(folio);
+
+	nr_pages_writeback = ifs->nr_pages_writeback;
+	ifs->nr_pages_writeback = 0;
+	folio_end_writeback_pages(folio, nr_pages_writeback);
+}
+
 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 		size_t len)
 {
@@ -1622,7 +1721,7 @@ void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
 
 	if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
-		folio_end_writeback(folio);
+		iomap_folio_end_writeback(folio);
 }
 EXPORT_SYMBOL_GPL(iomap_finish_folio_write);
 
@@ -1710,6 +1809,21 @@ static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode,
 	return true;
 }
 
+static void iomap_update_dirty_stats(struct folio *folio)
+{
+	struct iomap_folio_state *ifs = folio->private;
+	long nr_dirty_pages;
+
+	if (iomap_granular_dirty_pages(folio)) {
+		nr_dirty_pages = ifs_count_dirty_pages(folio);
+		ifs->nr_pages_writeback = nr_dirty_pages;
+	} else {
+		nr_dirty_pages = folio_nr_pages(folio);
+	}
+
+	clear_dirty_for_io_stats(folio, nr_dirty_pages);
+}
+
 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 {
 	struct iomap_folio_state *ifs = folio->private;
@@ -1727,6 +1841,8 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 
 	trace_iomap_writeback_folio(inode, pos, folio_size(folio));
 
+	iomap_update_dirty_stats(folio);
+
 	if (!iomap_writeback_handle_eof(folio, inode, &end_pos))
 		return 0;
 	WARN_ON_ONCE(end_pos <= pos);
@@ -1734,6 +1850,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	if (i_blocks_per_folio(inode, folio) > 1) {
 		if (!ifs) {
 			ifs = ifs_alloc(inode, folio, 0);
+			ifs->nr_pages_writeback = folio_nr_pages(folio);
 			iomap_set_range_dirty(folio, 0, end_pos - pos);
 		}
 
@@ -1751,7 +1868,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 * 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);
+	iomap_folio_start_writeback(folio);
 
 	/*
 	 * Walk through the folio to find dirty areas to write back.
@@ -1784,10 +1901,10 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
 	 */
 	if (ifs) {
 		if (atomic_dec_and_test(&ifs->write_bytes_pending))
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	} else {
 		if (!wb_pending)
-			folio_end_writeback(folio);
+			iomap_folio_end_writeback(folio);
 	}
 	mapping_set_error(inode->i_mapping, error);
 	return error;
@@ -1809,6 +1926,13 @@ iomap_writepages(struct iomap_writepage_ctx *wpc)
 			PF_MEMALLOC))
 		return -EIO;
 
+	/*
+	 * iomap opts out of the default wbc stats accounting because it does
+	 * its own granular dirty/writeback accounting (see
+	 * iomap_update_dirty_stats()).
+	 */
+	wpc->wbc->no_stats_accounting = true;
+
 	while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) {
 		error = iomap_writeback_folio(wpc, folio);
 		folio_unlock(folio);
Signature · 1 line
2.47.3
#23 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 19 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

No, I don't think you're missing anything, it should have been done like this in the first place.

Quoted 68 lines

Brian

Quoted 66 lines
Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
    spinlock_t              state_lock;
    unsigned int            read_bytes_pending;
    atomic_t                write_bytes_pending;
+   /* number of pages being currently written back */
+   unsigned                nr_pages_writeback;
 
    /*
     * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
            blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+   struct inode *inode = folio->mapping->host;
+   unsigned block_size = i_blocksize(inode);
+   unsigned start_blk, end_blk;
+   unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+   start_blk = 0;
+   blks = i_blocks_per_folio(inode, folio);
+   end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+   end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+   while (start_blk <= end_blk) {
+           start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+           if (start_blk > end_blk)
+                   break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+           nblks++;
+           start_blk++;
+   }
+
+   return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
            struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
            ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+           unsigned len)

iomap_count_clean_pages() ?

Nice, a much clearer name.

I'll make the suggestions you listed above too, thanks for the pointers.

Thanks for taking a look at this, Darrick and Brian!

Quoted 3 lines
Quoted 3 lines

--D

#24 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 24 lines · Joanne Koong
Quoted 23 lines · Brian Foster
Quoted 21 lines · Darrick J. Wong
Quoted 12 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

Quoted 3 lines

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB?

Yes, that's quite possible. IIRC you can get 2MB folios for 8k fsblocks.

Quoted 5 lines

That's what I've been seeing on fuse
where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"?

I misspoke -- should have said "there will be no more than one (large) folio for a single fsblock". Sorry about the confusion; my old brain is still stuck in 2015 sometimes.

Quoted 3 lines

My understanding is that that only ensures the folio is at
least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Nope, your understanding is correct. :)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Oh! Yes, that's right, they /are/ equivalent!

Quoted 96 lines
Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

No, I don't think you're missing anything, it should have been done like this in the first place.

Quoted 68 lines

Brian

Quoted 66 lines
Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
    spinlock_t              state_lock;
    unsigned int            read_bytes_pending;
    atomic_t                write_bytes_pending;
+   /* number of pages being currently written back */
+   unsigned                nr_pages_writeback;
 
    /*
     * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
            blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+   struct inode *inode = folio->mapping->host;
+   unsigned block_size = i_blocksize(inode);
+   unsigned start_blk, end_blk;
+   unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+   start_blk = 0;
+   blks = i_blocks_per_folio(inode, folio);
+   end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+   end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+   while (start_blk <= end_blk) {
+           start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+           if (start_blk > end_blk)
+                   break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+           nblks++;
+           start_blk++;
+   }
+
+   return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
            struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
            ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+           unsigned len)

iomap_count_clean_pages() ?

Nice, a much clearer name.

I'll make the suggestions you listed above too, thanks for the pointers.

Thanks for taking a look at this, Darrick and Brian!

Quoted 3 lines
Quoted 3 lines

--D

#25 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

In reply to Joanne Koong
Quoted 68 lines · Joanne Koong
Quoted 19 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Brian

Quoted 78 lines
Quoted 68 lines

Brian

Quoted 66 lines
Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
    spinlock_t              state_lock;
    unsigned int            read_bytes_pending;
    atomic_t                write_bytes_pending;
+   /* number of pages being currently written back */
+   unsigned                nr_pages_writeback;
 
    /*
     * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
            blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+   struct inode *inode = folio->mapping->host;
+   unsigned block_size = i_blocksize(inode);
+   unsigned start_blk, end_blk;
+   unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+   start_blk = 0;
+   blks = i_blocks_per_folio(inode, folio);
+   end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+   end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+   while (start_blk <= end_blk) {
+           start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+           if (start_blk > end_blk)
+                   break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+           nblks++;
+           start_blk++;
+   }
+
+   return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
            struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
            ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+           unsigned len)

iomap_count_clean_pages() ?

Nice, a much clearer name.

I'll make the suggestions you listed above too, thanks for the pointers.

Thanks for taking a look at this, Darrick and Brian!

Quoted 3 lines
Quoted 3 lines

--D

#26 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 84 lines · Brian Foster
Quoted 68 lines · Joanne Koong
Quoted 19 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

Quoted 7 lines

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Yes, I think that's correct -- one has to track which basepages /were/ dirty, and then which ones become dirty after updating the ifs dirty bitmap.

For example, if you have a 1k fsblock filesystem, 4k base pages, and a 64k folio, you could write a single byte at offset 0, then come back and write to a byte at offset 1024. The first write will result in a charge of one basepage, but so will the second, I think. That results incharges for two dirty pages, when you've really only dirtied a single basepage.

Also, does (block_size >> PAGE_SHIFT) evaluate to ... zero?

--D

Quoted 82 lines

Brian

Quoted 78 lines
Quoted 68 lines

Brian

Quoted 66 lines
Quoted 28 lines
---
 fs/iomap/buffered-io.c | 140 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 8 deletions(-)
fs/iomap/buffered-io.c +8 -0
index 4f021dcaaffe..bf33a5361a39 100644
@@ -20,6 +20,8 @@ struct iomap_folio_state {
    spinlock_t              state_lock;
    unsigned int            read_bytes_pending;
    atomic_t                write_bytes_pending;
+   /* number of pages being currently written back */
+   unsigned                nr_pages_writeback;
 
    /*
     * Each block has two bits in this bitmap:
@@ -139,6 +141,29 @@ static unsigned ifs_next_clean_block(struct folio *folio,
            blks + start_blk) - blks;
 }
 
+static unsigned ifs_count_dirty_pages(struct folio *folio)
+{
+   struct inode *inode = folio->mapping->host;
+   unsigned block_size = i_blocksize(inode);
+   unsigned start_blk, end_blk;
+   unsigned blks, nblks = 0;

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 159

Quoted 10 lines
fs/iomap/buffered-io.c +10 -0
+
+   start_blk = 0;
+   blks = i_blocks_per_folio(inode, folio);
+   end_blk = (i_size_read(inode) - 1) >> inode->i_blkbits;
+   end_blk = min(end_blk, i_blocks_per_folio(inode, folio) - 1);
+
+   while (start_blk <= end_blk) {
+           start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
+           if (start_blk > end_blk)
+                   break;

Use your new helper?

nblks = ifs_next_clean_block(folio, start_blk + 1,
end_blk) - start_blk?

fs/iomap/buffered-io.c static unsigned ifs_next_clean_block(struct folio *folio, · line 164

Quoted 5 lines
fs/iomap/buffered-io.c +5 -0
+           nblks++;
+           start_blk++;
+   }
+
+   return nblks * (block_size >> PAGE_SHIFT);

I think this returns the number of dirty basepages in a given large folio? If that's the case then shouldn't this return long, like folio_nr_pages does?

fs/iomap/buffered-io.c static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) · line 249

Quoted 11 lines
fs/iomap/buffered-io.c +4 -0
+}
+
 static unsigned ifs_find_dirty_range(struct folio *folio,
            struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
 {
@@ -220,6 +245,58 @@ static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
            ifs_set_range_dirty(folio, ifs, off, len);
 }
 
+static long iomap_get_range_newly_dirtied(struct folio *folio, loff_t pos,
+           unsigned len)

iomap_count_clean_pages() ?

Nice, a much clearer name.

I'll make the suggestions you listed above too, thanks for the pointers.

Thanks for taking a look at this, Darrick and Brian!

Quoted 3 lines
Quoted 3 lines

--D

#27 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 98 lines · Darrick J. Wong
Quoted 84 lines · Brian Foster
Quoted 68 lines · Joanne Koong
Quoted 19 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

Quoted 7 lines

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Yes, I think that's correct -- one has to track which basepages /were/ dirty, and then which ones become dirty after updating the ifs dirty bitmap.

Quoted 6 lines

For example, if you have a 1k fsblock filesystem, 4k base pages, and a 64k folio, you could write a single byte at offset 0, then come back and write to a byte at offset 1024. The first write will result in a charge of one basepage, but so will the second, I think. That results incharges for two dirty pages, when you've really only dirtied a single basepage.

Does it matter though which blocks map to which pages? AFAIU, the "block size" is the granularity for disk io and is not really related to pages (eg for writing out to disk, only the block gets written, not the whole page). The stats (as i understand it) are used to throttle how much data gets written back to disk, and the primary thing it cares about is how many bytes that is, not how many pages, it's just that it's in PAGE_SIZE granularity because prior to iomap there was no dirty tracking of individual blocks within a page/folio; it seems like it suffices then to just keep track of total # of dirty blocks, multiply that by blocksize, and roundup divide that by PAGE_SIZE and pass that to the stats.

But, as Jan pointed out to me in his comment, the stats are also used for monitoring the health of reclaim, so maybe it does matter then how the blocks translate to pages.

I'll put this patchset on hold until there's more feedback from the mm side as to whether we should proceed or drop this.

Thanks,
Joanne

Quoted 6 lines

Also, does (block_size >> PAGE_SHIFT) evaluate to ... zero?

--D

Quoted 1 line

Brian

#28 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 106 lines · Joanne Koong
Quoted 98 lines · Darrick J. Wong
Quoted 84 lines · Brian Foster
Quoted 68 lines · Joanne Koong
Quoted 19 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

Quoted 7 lines

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Yes, I think that's correct -- one has to track which basepages /were/ dirty, and then which ones become dirty after updating the ifs dirty bitmap.

Quoted 6 lines

For example, if you have a 1k fsblock filesystem, 4k base pages, and a 64k folio, you could write a single byte at offset 0, then come back and write to a byte at offset 1024. The first write will result in a charge of one basepage, but so will the second, I think. That results incharges for two dirty pages, when you've really only dirtied a single basepage.

Quoted 12 lines

Does it matter though which blocks map to which pages? AFAIU, the "block size" is the granularity for disk io and is not really related to pages (eg for writing out to disk, only the block gets written, not the whole page). The stats (as i understand it) are used to throttle how much data gets written back to disk, and the primary thing it cares about is how many bytes that is, not how many pages, it's just that it's in PAGE_SIZE granularity because prior to iomap there was no dirty tracking of individual blocks within a page/folio; it seems like it suffices then to just keep track of total # of dirty blocks, multiply that by blocksize, and roundup divide that by PAGE_SIZE and pass that to the stats.

I suppose it may not matter in terms of the purpose of the mechanism itself. In fact if the whole thing could just be converted to track bytes, at least internally, then maybe that would eliminate some of the confusion in dealing with different granularity of units..? I have no idea how practical or appropriate that is, though. :)

The concern Darrick and I were discussing is more about maintaining accounting consistency in the event that we do continue translating blocks to pages and ultimately add support for the block size < page size case.

In that case the implication is that we'd still need to account something when we dirty a single block out of a page (i.e. use Darrick's example where we dirty a 1k fs block out of a 4k page). If we round that partial page case up to 1 dirty page and repeat as each 1k block is dirtied, then we have to make sure accounting remains consistent in the case where we dirty account each sub-block of a page through separate writes, but then clear dirty accounting for the entire folio once at writeback time.

But I suppose we are projecting the implementation a bit so it might not be worth getting this far into the weeds until you determine what direction you want to go with this and have more code to review. All in all, I do agree with Jan's general concern that I'd rather not have to deal with multiple variants of sub-page state tracking in iomap. It's already a challenge to support multiple different filesystems. This does seem like a useful enhancement to me however, so IMO it would be fine to just try and make it more generic (short of something more generic on the mm side or whatever) than it is currently.

Quoted 4 lines

But, as Jan pointed out to me in his comment, the stats are also used for monitoring the health of reclaim, so maybe it does matter then how the blocks translate to pages.

Random thought, but would having an additional/optional stat to track bytes (alongside the existing page granularity counts) help at all? For example, if throttling could use optional byte granular dirty/writeback counters when they are enabled instead of the traditional page granular, would that solve your problem and be less disruptive to other things that actually prefer the page count?

Brian

Quoted 15 lines

I'll put this patchset on hold until there's more feedback from the mm side as to whether we should proceed or drop this.

Thanks,
Joanne

Quoted 6 lines

Also, does (block_size >> PAGE_SHIFT) evaluate to ... zero?

--D

Quoted 1 line

Brian

maintainer
#29 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 153 lines · Brian Foster
Quoted 104 lines · Joanne Koong
Quoted 96 lines · Darrick J. Wong
Quoted 83 lines · Brian Foster
Quoted 67 lines · Joanne Koong
Quoted 18 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

Quoted 7 lines

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Yes, I think that's correct -- one has to track which basepages /were/ dirty, and then which ones become dirty after updating the ifs dirty bitmap.

Quoted 6 lines

For example, if you have a 1k fsblock filesystem, 4k base pages, and a 64k folio, you could write a single byte at offset 0, then come back and write to a byte at offset 1024. The first write will result in a charge of one basepage, but so will the second, I think. That results incharges for two dirty pages, when you've really only dirtied a single basepage.

Quoted 12 lines

Does it matter though which blocks map to which pages? AFAIU, the "block size" is the granularity for disk io and is not really related to pages (eg for writing out to disk, only the block gets written, not the whole page). The stats (as i understand it) are used to throttle how much data gets written back to disk, and the primary thing it cares about is how many bytes that is, not how many pages, it's just that it's in PAGE_SIZE granularity because prior to iomap there was no dirty tracking of individual blocks within a page/folio; it seems like it suffices then to just keep track of total # of dirty blocks, multiply that by blocksize, and roundup divide that by PAGE_SIZE and pass that to the stats.

I suppose it may not matter in terms of the purpose of the mechanism itself. In fact if the whole thing could just be converted to track bytes, at least internally, then maybe that would eliminate some of the confusion in dealing with different granularity of units..? I have no idea how practical or appropriate that is, though. :)

The concern Darrick and I were discussing is more about maintaining accounting consistency in the event that we do continue translating blocks to pages and ultimately add support for the block size < page size case.

In that case the implication is that we'd still need to account something when we dirty a single block out of a page (i.e. use Darrick's example where we dirty a 1k fs block out of a 4k page). If we round that partial page case up to 1 dirty page and repeat as each 1k block is dirtied, then we have to make sure accounting remains consistent in the case where we dirty account each sub-block of a page through separate writes, but then clear dirty accounting for the entire folio once at writeback time.

But I suppose we are projecting the implementation a bit so it might not be worth getting this far into the weeds until you determine what direction you want to go with this and have more code to review. All in all, I do agree with Jan's general concern that I'd rather not have to deal with multiple variants of sub-page state tracking in iomap. It's already a challenge to support multiple different filesystems. This does seem like a useful enhancement to me however, so IMO it would be fine to just try and make it more generic (short of something more generic on the mm side or whatever) than it is currently.

Quoted 4 lines

But, as Jan pointed out to me in his comment, the stats are also used for monitoring the health of reclaim, so maybe it does matter then how the blocks translate to pages.

Quoted 6 lines

Random thought, but would having an additional/optional stat to track bytes (alongside the existing page granularity counts) help at all? For example, if throttling could use optional byte granular dirty/writeback counters when they are enabled instead of the traditional page granular, would that solve your problem and be less disruptive to other things that actually prefer the page count?

FWIW my current thinking is that the best might be to do byte granularity tracking for wb_stat_ counters and leave current coarse-grained accounting for the zone / memcg stats. That way mm counters could be fully managed within mm code and iomap wouldn't have to care and writeback counters (which care about amount of IO, not amount of pinned memory) would be maintained by filesystems / iomap. We'd just need to come up with sensible rules where writeback counters should be updated when mm doesn't do it.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#30 lore raw

Re: [PATCH v2 12/12] iomap: add granular dirty and writeback accounting

Quoted 131 lines · Jan Kara
Quoted 129 lines · Brian Foster
Quoted 104 lines · Joanne Koong
Quoted 96 lines · Darrick J. Wong
Quoted 83 lines · Brian Foster
Quoted 67 lines · Joanne Koong
Quoted 18 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong
Quoted 15 lines · Joanne Koong

Add granular dirty and writeback accounting for large folios. These stats are used by the mm layer for dirty balancing and throttling. Having granular dirty and writeback accounting helps prevent over-aggressive balancing and throttling.

There are 4 places in iomap this commit affects:
a) filemap dirtying, which now calls filemap_dirty_folio_pages()
b) writeback_iter with setting the wbc->no_stats_accounting bit and calling clear_dirty_for_io_stats()
c) starting writeback, which now calls __folio_start_writeback()
d) ending writeback, which now calls folio_end_writeback_pages()

This relies on using the ifs->state dirty bitmap to track dirty pages in the folio. As such, this can only be utilized on filesystems where the block size >= PAGE_SIZE.

Quoted 6 lines
Quoted 6 lines

Er... is this statement correct? I thought that you wanted the granular dirty page accounting when it's possible that individual sub-pages of a folio could be dirty.

If i_blocksize >= PAGE_SIZE, then we'll have set the min folio order and there will be exactly one (large) folio for a single fsblock. Writeback

Oh interesting, this is the part I'm confused about. With i_blocksize

Quoted 1 line

= PAGE_SIZE, isn't there still the situation where the folio itself

could be a lot larger, like 1MB? That's what I've been seeing on fuse where "blocksize" == PAGE_SIZE == 4096. I see that xfs sets the min folio order through mapping_set_folio_min_order() but I'm not seeing how that ensures "there will be exactly one large folio for a single fsblock"? My understanding is that that only ensures the folio is at least the size of the fsblock but that the folio size can be larger than that too. Am I understanding this incorrectly?

Quoted 8 lines
Quoted 8 lines

must happen in units of fsblocks, so there's no point in doing the extra accounting calculations if there's only one fsblock.

Waitaminute, I think the logic to decide if you're going to use the granular accounting is:

      (folio_size > PAGE_SIZE && folio_size > i_blocksize)

Yeah, you're right about this - I had used "ifs && i_blocksize >= PAGE_SIZE" as the check, which translates to "i_blocks_per_folio > 1 && i_block_size >= PAGE_SIZE", which in effect does the same thing as what you wrote but has the additional (and now I'm realizing, unnecessary) stipulation that block_size can't be less than PAGE_SIZE.

Quoted 8 lines
Quoted 2 lines

Hrm?

I'm also a little confused why this needs to be restricted to blocksize gte PAGE_SIZE. The lower level helpers all seem to be managing block ranges, and then apparently just want to be able to use that directly as a page count (for accounting purposes).

Quoted 7 lines

Is there any reason the lower level functions couldn't return block units, then the higher level code can use a blocks_per_page or some such to translate that to a base page count..? As Darrick points out I assume you'd want to shortcut the folio_nr_pages() == 1 case to use a min page count of 1, but otherwise ISTM that would allow this to work with configs like 64k pagesize and 4k blocks as well. Am I missing something?

Quoted 3 lines

No, I don't think you're missing anything, it should have been done like this in the first place.

Ok. Something that came to mind after thinking about this some more is whether there is risk for the accounting to get wonky.. For example, consider 4k blocks, 64k pages, and then a large folio on top of that. If a couple or so blocks are dirtied at one time, you'd presumably want to account that as the minimum of 1 dirty page. Then if a couple more blocks are dirtied in the same large folio, how do you determine whether those blocks are a newly dirtied page or part of the already accounted dirty page? I wonder if perhaps this is the value of the no sub-page sized blocks restriction, because you can imply that newly dirtied blocks means newly dirtied pages..?

Quoted 7 lines

I suppose if that is an issue it might still be manageable. Perhaps we'd have to scan the bitmap in blks per page windows and use that to determine how many base pages are accounted for at any time. So for example, 3 dirty 4k blocks all within the same 64k page size window still accounts as 1 dirty page, vs. dirty blocks in multiple page size windows might mean multiple dirty pages, etc. That way writeback accounting remains consistent with dirty accounting. Hm?

Yes, I think that's correct -- one has to track which basepages /were/ dirty, and then which ones become dirty after updating the ifs dirty bitmap.

Quoted 6 lines

For example, if you have a 1k fsblock filesystem, 4k base pages, and a 64k folio, you could write a single byte at offset 0, then come back and write to a byte at offset 1024. The first write will result in a charge of one basepage, but so will the second, I think. That results incharges for two dirty pages, when you've really only dirtied a single basepage.

Quoted 12 lines

Does it matter though which blocks map to which pages? AFAIU, the "block size" is the granularity for disk io and is not really related to pages (eg for writing out to disk, only the block gets written, not the whole page). The stats (as i understand it) are used to throttle how much data gets written back to disk, and the primary thing it cares about is how many bytes that is, not how many pages, it's just that it's in PAGE_SIZE granularity because prior to iomap there was no dirty tracking of individual blocks within a page/folio; it seems like it suffices then to just keep track of total # of dirty blocks, multiply that by blocksize, and roundup divide that by PAGE_SIZE and pass that to the stats.

I suppose it may not matter in terms of the purpose of the mechanism itself. In fact if the whole thing could just be converted to track bytes, at least internally, then maybe that would eliminate some of the confusion in dealing with different granularity of units..? I have no idea how practical or appropriate that is, though. :)

The concern Darrick and I were discussing is more about maintaining accounting consistency in the event that we do continue translating blocks to pages and ultimately add support for the block size < page size case.

Quoted 8 lines
Quoted 8 lines

In that case the implication is that we'd still need to account something when we dirty a single block out of a page (i.e. use Darrick's example where we dirty a 1k fs block out of a 4k page). If we round that partial page case up to 1 dirty page and repeat as each 1k block is dirtied, then we have to make sure accounting remains consistent in the case where we dirty account each sub-block of a page through separate writes, but then clear dirty accounting for the entire folio once at writeback time.

Agreed, in the case where we do need to care about which block maps to which page, we could parse the bitmap in PAGE_SIZE chunks where if any bit in that range is marked dirty then the whole page is accounted for as dirty. I don't think this would add too much overhead given that we already need to iterate over bitmap ranges. Looking at this patchset again, I think we can even get rid of ifs_count_dirty_pages() entirely and just do the counting dynamically as blocks get dirtied, not sure if there was some reason I didn't do it this way earlier, but I think that works.

Quoted 6 lines
Quoted 6 lines

But I suppose we are projecting the implementation a bit so it might not be worth getting this far into the weeds until you determine what direction you want to go with this and have more code to review. All in all, I do agree with Jan's general concern that I'd rather not have to deal with multiple variants of sub-page state tracking in iomap. It's

I agree, I think we should try to keep the iomap stats accounting as simple as possible. I like Jan's idea of having iomap's accounting go towards bdi_writeback and leaving the other stuff untouched.

Quoted 17 lines
Quoted 10 lines

already a challenge to support multiple different filesystems. This does seem like a useful enhancement to me however, so IMO it would be fine to just try and make it more generic (short of something more generic on the mm side or whatever) than it is currently.

Quoted 4 lines

But, as Jan pointed out to me in his comment, the stats are also used for monitoring the health of reclaim, so maybe it does matter then how the blocks translate to pages.

Quoted 6 lines

Random thought, but would having an additional/optional stat to track bytes (alongside the existing page granularity counts) help at all? For example, if throttling could use optional byte granular dirty/writeback counters when they are enabled instead of the traditional page granular, would that solve your problem and be less disruptive to other things that actually prefer the page count?

Quoted 8 lines

FWIW my current thinking is that the best might be to do byte granularity tracking for wb_stat_ counters and leave current coarse-grained accounting for the zone / memcg stats. That way mm counters could be fully managed within mm code and iomap wouldn't have to care and writeback counters (which care about amount of IO, not amount of pinned memory) would be maintained by filesystems / iomap. We'd just need to come up with sensible rules where writeback counters should be updated when mm doesn't do it.

I like your idea a lot.

Thanks,
Joanne

Quoted 4 lines

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

maintainer
#31 lore raw
In reply to Joanne Koong

Hello!

Quoted 19 lines · Joanne Koong

This patchset adds granular dirty and writeback stats accounting for large folios.

The dirty page balancing logic uses these stats to determine things like whether the ratelimit has been exceeded, the frequency with which pages need to be written back, if dirtying should be throttled, etc. Currently for large folios, if any byte in the folio is dirtied or written back, all the bytes in the folio are accounted as such.

In particular, there are four places where dirty and writeback stats get incremented and decremented as pages get dirtied and written back:
a) folio dirtying (filemap_dirty_folio() -> ... -> folio_account_dirtied())
- increments NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE,

     current->nr_dirtied

b) writing back a mapping (writeback_iter() -> ... -> folio_clear_dirty_for_io())
- decrements NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE

Quoted 5 lines

c) starting writeback on a folio (folio_start_writeback())
- increments WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

d) ending writeback on a folio (folio_end_writeback())
- decrements WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

I was looking through the patch set. One general concern I have is that it all looks somewhat fragile. If you say start writeback on a folio with a granular function and happen to end writeback with a non-granular one, everything will run fine, just a permanent error in the counters will be introduced. Similarly with a dirtying / starting writeback mismatch. The practicality of this issue is demostrated by the fact that you didn't convert e.g. folio_redirty_for_writepage() so anybody using it together with fine-grained accounting will just silently mess up the counters. Another issue of a similar kind is that __folio_migrate_mapping() does not support fine-grained accounting (and doesn't even have a way to figure out proper amount to account) so again any page migration may introduce permanent errors into counters. One way to deal with this fragility would be to have a flag in the mapping that will determine whether the dirty accounting is done by MM or the filesystem (iomap code in your case) instead of determining it at the call site.

Another concern I have is the limitation to blocksize >= PAGE_SIZE you mention below. That is kind of annoying for filesystems because generally they also have to deal with cases of blocksize < PAGE_SIZE and having two ways of accounting in one codebase is a big maintenance burden. But this was discussed elsewhere in this series and I think you have settled on supporting blocksize < PAGE_SIZE as well?

Finally, there is one general issue for which I'd like to hear opinions of MM guys: Dirty throttling is a mechanism to avoid a situation where the dirty page cache consumes too big amount of memory which makes page reclaim hard and the machine thrashes as a result or goes OOM. Now if you dirty a 2MB folio, it really makes all those 2MB hard to reclaim (neither direct reclaim nor kswapd will be able to reclaim such folio) even though only 1KB in that folio needs actual writeback. In this sense it is actually correct to account whole big folio as dirty in the counters - if you accounted only 1KB or even 4KB (page), a user could with some effort make all page cache memory dirty and hard to reclaim without crossing the dirty limits. On the other hand if only 1KB in a folio trully needs writeback, the writeback will be generally significantly faster than with 2MB needing writeback. So in this sense it is correct to account amount to data that trully needs writeback.

I don't know what the right answer to this "conflict of interests" is. We could keep accounting full folios in the global / memcg counters (to protect memory reclaim) and do per page (or even finer) accounting in the bdi_writeback which is there to avoid excessive accumulation of dirty data (and thus long writeback times) against one device. This should still help your case with FUSE and strictlimit (which is generally constrained by bdi_writeback counters). One just needs to have a closer look how hard would it be to adapt writeback throttling logic to the different granularity of global counters and writeback counters...

Honza

Quoted 81 lines

Patches 1 to 9 adds support for the 4 cases above to take in the number of pages to be accounted, instead of accounting for the entire folio.

Patch 12 adds the iomap changes that uses these new APIs. This relies on the iomap folio state bitmap to track which pages are dirty (so that we avoid any double-counting). As such we can only do granular accounting if the block size >= PAGE_SIZE.

This patchset was run through xfstests using fuse passthrough hp (with an out-of-tree kernel patch enabling fuse large folios).

This is on top of commit 4f702205 ("Merge branch 'vfs-6.18.rust' into vfs.all") in Christian's vfs tree, and on top of the patchset that removes BDI_CAP_WRITEBACK_ACCT [1].

Local benchmarks were run on xfs by doing the following:

seting up xfs (per the xfstests readme):

# xfs_io -f -c "falloc 0 10g" test.img
# xfs_io -f -c "falloc 0 10g" scratch.img
# mkfs.xfs test.img
# losetup /dev/loop0 ./test.img
# losetup /dev/loop1 ./scratch.img
# mkdir -p /mnt/test && mount /dev/loop0 /mnt/test
# sudo sysctl -w vm.dirty_bytes=$((3276 * 1024 * 1024)) # roughly 20% of 16GB
# sudo sysctl -w vm.dirty_background_bytes=$((1638*1024*1024)) # roughly 10% of 16GB

running this test program (ai-generated) [2] which essentially writes out 2 GB of data 256 MB at a time and then spins up 15 threads to do 50-byte 50k writes.

On my VM, I saw the writes take around 3 seconds (with some variability of taking 0.3 seconds to 5 seconds sometimes) in the base version vs taking a pretty consistent 0.14 seconds with this patchset. It'd be much appreciated if someone could also run it on their local system to verify they see similar numbers.

Thanks,
Joanne

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://pastebin.com/CbcwTXjq

Changelog
v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
v1 -> v2:
* Add documentation specifying caller expectations for the filemap_dirty_folio_pages() -> __folio_mark_dirty() callpath (Jan)
* Add requested iomap bitmap iteration refactoring (Christoph)
* Fix long lines (Christoph)

Joanne Koong (12):
mm: pass number of pages to __folio_start_writeback()
mm: pass number of pages to __folio_end_writeback()
mm: add folio_end_writeback_pages() helper
mm: pass number of pages dirtied to __folio_mark_dirty()
mm: add filemap_dirty_folio_pages() helper
mm: add __folio_clear_dirty_for_io() helper
mm: add no_stats_accounting bitfield to wbc
mm: refactor clearing dirty stats into helper function
mm: add clear_dirty_for_io_stats() helper
iomap: refactor dirty bitmap iteration
iomap: refactor uptodate bitmap iteration
iomap: add granular dirty and writeback accounting

 fs/btrfs/subpage.c         |   2 +-
 fs/buffer.c                |   6 +-
 fs/ext4/page-io.c          |   2 +-
 fs/iomap/buffered-io.c     | 281 ++++++++++++++++++++++++++++++-------
 include/linux/page-flags.h |   4 +-
 include/linux/pagemap.h    |   4 +-
 include/linux/writeback.h  |  10 ++
 mm/filemap.c               |  12 +-
 mm/internal.h              |   2 +-
 mm/page-writeback.c        | 115 +++++++++++----
 10 files changed, 346 insertions(+), 92 deletions(-)
Signature · 1 line
2.47.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#32 lore raw
Quoted 77 lines · Jan Kara

Hello!

Quoted 19 lines · Joanne Koong

This patchset adds granular dirty and writeback stats accounting for large folios.

The dirty page balancing logic uses these stats to determine things like whether the ratelimit has been exceeded, the frequency with which pages need to be written back, if dirtying should be throttled, etc. Currently for large folios, if any byte in the folio is dirtied or written back, all the bytes in the folio are accounted as such.

In particular, there are four places where dirty and writeback stats get incremented and decremented as pages get dirtied and written back:
a) folio dirtying (filemap_dirty_folio() -> ... -> folio_account_dirtied())
- increments NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE,

     current->nr_dirtied

b) writing back a mapping (writeback_iter() -> ... -> folio_clear_dirty_for_io())
- decrements NR_FILE_DIRTY, NR_ZONE_WRITE_PENDING, WB_RECLAIMABLE

Quoted 5 lines

c) starting writeback on a folio (folio_start_writeback())
- increments WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

d) ending writeback on a folio (folio_end_writeback())
- decrements WB_WRITEBACK, NR_WRITEBACK, NR_ZONE_WRITE_PENDING

I was looking through the patch set. One general concern I have is that it all looks somewhat fragile. If you say start writeback on a folio with a granular function and happen to end writeback with a non-granular one, everything will run fine, just a permanent error in the counters will be introduced. Similarly with a dirtying / starting writeback mismatch. The practicality of this issue is demostrated by the fact that you didn't convert e.g. folio_redirty_for_writepage() so anybody using it together with fine-grained accounting will just silently mess up the counters. Another issue of a similar kind is that __folio_migrate_mapping() does not support fine-grained accounting (and doesn't even have a way to figure out proper amount to account) so again any page migration may introduce permanent errors into counters. One way to deal with this fragility would be to have a flag in the mapping that will determine whether the dirty accounting is done by MM or the filesystem (iomap code in your case) instead of determining it at the call site.

Another concern I have is the limitation to blocksize >= PAGE_SIZE you mention below. That is kind of annoying for filesystems because generally they also have to deal with cases of blocksize < PAGE_SIZE and having two ways of accounting in one codebase is a big maintenance burden. But this was discussed elsewhere in this series and I think you have settled on supporting blocksize < PAGE_SIZE as well?

Finally, there is one general issue for which I'd like to hear opinions of MM guys: Dirty throttling is a mechanism to avoid a situation where the dirty page cache consumes too big amount of memory which makes page reclaim hard and the machine thrashes as a result or goes OOM. Now if you dirty a 2MB folio, it really makes all those 2MB hard to reclaim (neither direct reclaim nor kswapd will be able to reclaim such folio) even though only 1KB in that folio needs actual writeback. In this sense it is actually correct to account whole big folio as dirty in the counters - if you accounted only 1KB or even 4KB (page), a user could with some effort make all page cache memory dirty and hard to reclaim without crossing the dirty limits. On the other hand if only 1KB in a folio trully needs writeback, the writeback will be generally significantly faster than with 2MB needing writeback. So in this sense it is correct to account amount to data that trully needs writeback.

I don't know what the right answer to this "conflict of interests" is. We could keep accounting full folios in the global / memcg counters (to protect memory reclaim) and do per page (or even finer) accounting in the bdi_writeback which is there to avoid excessive accumulation of dirty data (and thus long writeback times) against one device. This should still help your case with FUSE and strictlimit (which is generally constrained by bdi_writeback counters). One just needs to have a closer look how hard would it be to adapt writeback throttling logic to the different granularity of global counters and writeback counters...

Quoted 1 line

Honza

Hi Honza,

Thanks for sharing your thoughts on this. Those are good points, especially the last one about reclaim. I'm curious to hear too what the mm people think.

If it turns out this patchset is not actually that useful, I'm happy to drop it.

Thanks,
Joanne