From: "Darrick J. Wong" <djwong@kernel.org>
To: Joanne Koong <joannelkoong@gmail.com>
Cc: brauner@kernel.org, hch@infradead.org, bfoster@redhat.com,
linux-fsdevel@vger.kernel.org, kernel-team@meta.com
Subject: Re: [PATCH v3 7/8] iomap: use find_next_bit() for dirty bitmap scanning
Date: Tue, 4 Nov 2025 17:42:33 -0800 [thread overview]
Message-ID: <20251105014233.GH196362@frogsfrogsfrogs> (raw)
In-Reply-To: <20251104205119.1600045-8-joannelkoong@gmail.com>
On Tue, Nov 04, 2025 at 12:51:18PM -0800, Joanne Koong wrote:
> Use find_next_bit()/find_next_zero_bit() for iomap dirty bitmap
> scanning. This uses __ffs() internally and is more efficient for
> finding the next dirty or clean bit than iterating through the bitmap
> range testing every bit.
>
> Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
> Suggested-by: Christoph Hellwig <hch@infradead.org>
Oh hey, that's a nice cleanup
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
--D
> ---
> fs/iomap/buffered-io.c | 61 ++++++++++++++++++++++++++++--------------
> 1 file changed, 41 insertions(+), 20 deletions(-)
>
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index 420fe2865927..3c9a4b773186 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -76,13 +76,34 @@ 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)
> +/*
> + * Find the next dirty block in the folio. end_blk is inclusive.
> + * 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;
> +}
> +
> +/*
> + * Find the next clean block in the folio. end_blk is inclusive.
> + * 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,
> @@ -94,18 +115,17 @@ 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;
> -
> - while (!ifs_block_is_dirty(folio, ifs, start_blk))
> - if (++start_blk == end_blk)
> - return 0;
> + i_blocks_per_folio(inode, folio)) - 1;
> + unsigned nblks;
>
> - while (start_blk + nblks < end_blk) {
> - if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
> - break;
> - nblks++;
> - }
> + start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
> + if (start_blk > end_blk)
> + return 0;
> + if (start_blk == end_blk)
> + nblks = 1;
> + else
> + 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;
> @@ -1161,7 +1181,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;
> @@ -1180,10 +1200,11 @@ 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 = ifs_next_clean_block(folio, first_blk, last_blk))
> + <= last_blk) {
> + punch(inode, folio_pos(folio) + (first_blk << blkbits),
> + 1 << blkbits, iomap);
> + first_blk++;
> }
> }
>
> --
> 2.47.3
>
>
next prev parent reply other threads:[~2025-11-05 1:42 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 20:51 [PATCH v3 0/8] iomap: buffered io changes Joanne Koong
2025-11-04 20:51 ` [PATCH v3 1/8] iomap: account for unaligned end offsets when truncating read range Joanne Koong
2025-11-05 1:27 ` Darrick J. Wong
2025-11-06 17:08 ` Joanne Koong
2025-11-06 23:08 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 2/8] docs: document iomap writeback's iomap_finish_folio_write() requirement Joanne Koong
2025-11-05 1:22 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 3/8] iomap: optimize pending async writeback accounting Joanne Koong
2025-11-05 1:28 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 4/8] iomap: simplify ->read_folio_range() error handling for reads Joanne Koong
2025-11-05 1:50 ` Darrick J. Wong
2025-11-06 17:17 ` Joanne Koong
2025-11-06 23:09 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 5/8] iomap: simplify when reads can be skipped for writes Joanne Koong
2025-11-05 1:40 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 6/8] iomap: use loff_t for file positions and offsets in writeback code Joanne Koong
2025-11-05 1:41 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 7/8] iomap: use find_next_bit() for dirty bitmap scanning Joanne Koong
2025-11-05 1:42 ` Darrick J. Wong [this message]
2025-11-04 20:51 ` [PATCH v3 8/8] iomap: use find_next_bit() for uptodate " Joanne Koong
2025-11-05 1:42 ` Darrick J. Wong
2025-11-11 12:09 ` [PATCH v3 0/8] iomap: buffered io changes Christian Brauner
2025-11-11 18:05 ` Joanne Koong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251105014233.GH196362@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=bfoster@redhat.com \
--cc=brauner@kernel.org \
--cc=hch@infradead.org \
--cc=joannelkoong@gmail.com \
--cc=kernel-team@meta.com \
--cc=linux-fsdevel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).